]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Implement P1814R0, CTAD for alias templates.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 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
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) vec<tree, va_gc> *saved_access_scope;
71
72 /* Live only within one (recursive) call to tsubst_expr. We use
73 this to pass the statement expression node from the STMT_EXPR
74 to the EXPR_STMT that is its result. */
75 static tree cur_stmt_expr;
76
77 // -------------------------------------------------------------------------- //
78 // Local Specialization Stack
79 //
80 // Implementation of the RAII helper for creating new local
81 // specializations.
82 local_specialization_stack::local_specialization_stack (lss_policy policy)
83 : saved (local_specializations)
84 {
85 if (policy == lss_blank || !saved)
86 local_specializations = new hash_map<tree, tree>;
87 else
88 local_specializations = new hash_map<tree, tree>(*saved);
89 }
90
91 local_specialization_stack::~local_specialization_stack ()
92 {
93 delete local_specializations;
94 local_specializations = saved;
95 }
96
97 /* True if we've recursed into fn_type_unification too many times. */
98 static bool excessive_deduction_depth;
99
100 struct GTY((for_user)) spec_entry
101 {
102 tree tmpl;
103 tree args;
104 tree spec;
105 };
106
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 {
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
111 };
112
113 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114
115 static GTY (()) hash_table<spec_hasher> *type_specializations;
116
117 /* Contains canonical template parameter types. The vector is indexed by
118 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
119 TREE_LIST, whose TREE_VALUEs contain the canonical template
120 parameters of various types and levels. */
121 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122
123 #define UNIFY_ALLOW_NONE 0
124 #define UNIFY_ALLOW_MORE_CV_QUAL 1
125 #define UNIFY_ALLOW_LESS_CV_QUAL 2
126 #define UNIFY_ALLOW_DERIVED 4
127 #define UNIFY_ALLOW_INTEGER 8
128 #define UNIFY_ALLOW_OUTER_LEVEL 16
129 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
130 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131
132 enum template_base_result {
133 tbr_incomplete_type,
134 tbr_ambiguous_baseclass,
135 tbr_success
136 };
137
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int check_non_deducible_conversion (tree, tree, int, int,
156 struct conversion **, bool);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
188 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
189 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
191 static bool check_specialization_scope (void);
192 static tree process_partial_specialization (tree);
193 static void set_current_access_from_decl (tree);
194 static enum template_base_result get_template_base (tree, tree, tree, tree,
195 bool , tree *);
196 static tree try_class_unification (tree, tree, tree, tree, bool);
197 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
198 tree, tree);
199 static bool template_template_parm_bindings_ok_p (tree, tree);
200 static void tsubst_default_arguments (tree, tsubst_flags_t);
201 static tree for_each_template_parm_r (tree *, int *, void *);
202 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
203 static void copy_default_args_to_explicit_spec (tree);
204 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
205 static bool dependent_template_arg_p (tree);
206 static bool any_template_arguments_need_structural_equality_p (tree);
207 static bool dependent_type_p_r (tree);
208 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_decl (tree, tree, tsubst_flags_t);
210 static void perform_typedefs_access_check (tree tmpl, tree targs);
211 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
212 location_t);
213 static tree listify (tree);
214 static tree listify_autos (tree, tree);
215 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
216 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
217 static bool complex_alias_template_p (const_tree tmpl);
218 static tree get_underlying_template (tree);
219 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
220 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
221 static tree make_argument_pack (tree);
222 static void register_parameter_specializations (tree, tree);
223 static tree enclosing_instantiation_of (tree tctx);
224
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
229
230 void
231 push_access_scope (tree t)
232 {
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
235
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
242
243 if (TREE_CODE (t) == FUNCTION_DECL)
244 {
245 vec_safe_push (saved_access_scope, current_function_decl);
246 current_function_decl = t;
247 }
248 }
249
250 /* Restore the scope set up by push_access_scope. T is the node we
251 are processing. */
252
253 void
254 pop_access_scope (tree t)
255 {
256 if (TREE_CODE (t) == FUNCTION_DECL)
257 current_function_decl = saved_access_scope->pop();
258
259 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
260 pop_nested_class ();
261 else
262 pop_from_top_level ();
263 }
264
265 /* Do any processing required when DECL (a member template
266 declaration) is finished. Returns the TEMPLATE_DECL corresponding
267 to DECL, unless it is a specialization, in which case the DECL
268 itself is returned. */
269
270 tree
271 finish_member_template_decl (tree decl)
272 {
273 if (decl == error_mark_node)
274 return error_mark_node;
275
276 gcc_assert (DECL_P (decl));
277
278 if (TREE_CODE (decl) == TYPE_DECL)
279 {
280 tree type;
281
282 type = TREE_TYPE (decl);
283 if (type == error_mark_node)
284 return error_mark_node;
285 if (MAYBE_CLASS_TYPE_P (type)
286 && CLASSTYPE_TEMPLATE_INFO (type)
287 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
288 {
289 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
290 check_member_template (tmpl);
291 return tmpl;
292 }
293 return NULL_TREE;
294 }
295 else if (TREE_CODE (decl) == FIELD_DECL)
296 error_at (DECL_SOURCE_LOCATION (decl),
297 "data member %qD cannot be a member template", decl);
298 else if (DECL_TEMPLATE_INFO (decl))
299 {
300 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
301 {
302 check_member_template (DECL_TI_TEMPLATE (decl));
303 return DECL_TI_TEMPLATE (decl);
304 }
305 else
306 return decl;
307 }
308 else
309 error_at (DECL_SOURCE_LOCATION (decl),
310 "invalid member template declaration %qD", decl);
311
312 return error_mark_node;
313 }
314
315 /* Create a template info node. */
316
317 tree
318 build_template_info (tree template_decl, tree template_args)
319 {
320 tree result = make_node (TEMPLATE_INFO);
321 TI_TEMPLATE (result) = template_decl;
322 TI_ARGS (result) = template_args;
323 return result;
324 }
325
326 /* Return the template info node corresponding to T, whatever T is. */
327
328 tree
329 get_template_info (const_tree t)
330 {
331 tree tinfo = NULL_TREE;
332
333 if (!t || t == error_mark_node)
334 return NULL;
335
336 if (TREE_CODE (t) == NAMESPACE_DECL
337 || TREE_CODE (t) == PARM_DECL)
338 return NULL;
339
340 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
341 tinfo = DECL_TEMPLATE_INFO (t);
342
343 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
344 t = TREE_TYPE (t);
345
346 if (OVERLOAD_TYPE_P (t))
347 tinfo = TYPE_TEMPLATE_INFO (t);
348 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
349 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
350
351 return tinfo;
352 }
353
354 /* Returns the template nesting level of the indicated class TYPE.
355
356 For example, in:
357 template <class T>
358 struct A
359 {
360 template <class U>
361 struct B {};
362 };
363
364 A<T>::B<U> has depth two, while A<T> has depth one.
365 Both A<T>::B<int> and A<int>::B<U> have depth one, if
366 they are instantiations, not specializations.
367
368 This function is guaranteed to return 0 if passed NULL_TREE so
369 that, for example, `template_class_depth (current_class_type)' is
370 always safe. */
371
372 int
373 template_class_depth (tree type)
374 {
375 int depth;
376
377 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
378 {
379 tree tinfo = get_template_info (type);
380
381 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
382 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
383 ++depth;
384
385 if (DECL_P (type))
386 type = CP_DECL_CONTEXT (type);
387 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
388 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
389 else
390 type = CP_TYPE_CONTEXT (type);
391 }
392
393 return depth;
394 }
395
396 /* Return TRUE if NODE instantiates a template that has arguments of
397 its own, be it directly a primary template or indirectly through a
398 partial specializations. */
399 static bool
400 instantiates_primary_template_p (tree node)
401 {
402 tree tinfo = get_template_info (node);
403 if (!tinfo)
404 return false;
405
406 tree tmpl = TI_TEMPLATE (tinfo);
407 if (PRIMARY_TEMPLATE_P (tmpl))
408 return true;
409
410 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
411 return false;
412
413 /* So now we know we have a specialization, but it could be a full
414 or a partial specialization. To tell which, compare the depth of
415 its template arguments with those of its context. */
416
417 tree ctxt = DECL_CONTEXT (tmpl);
418 tree ctinfo = get_template_info (ctxt);
419 if (!ctinfo)
420 return true;
421
422 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
423 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
424 }
425
426 /* Subroutine of maybe_begin_member_template_processing.
427 Returns true if processing DECL needs us to push template parms. */
428
429 static bool
430 inline_needs_template_parms (tree decl, bool nsdmi)
431 {
432 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
433 return false;
434
435 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
436 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
437 }
438
439 /* Subroutine of maybe_begin_member_template_processing.
440 Push the template parms in PARMS, starting from LEVELS steps into the
441 chain, and ending at the beginning, since template parms are listed
442 innermost first. */
443
444 static void
445 push_inline_template_parms_recursive (tree parmlist, int levels)
446 {
447 tree parms = TREE_VALUE (parmlist);
448 int i;
449
450 if (levels > 1)
451 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
452
453 ++processing_template_decl;
454 current_template_parms
455 = tree_cons (size_int (processing_template_decl),
456 parms, current_template_parms);
457 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
458
459 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
460 NULL);
461 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
462 {
463 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
464
465 if (error_operand_p (parm))
466 continue;
467
468 gcc_assert (DECL_P (parm));
469
470 switch (TREE_CODE (parm))
471 {
472 case TYPE_DECL:
473 case TEMPLATE_DECL:
474 pushdecl (parm);
475 break;
476
477 case PARM_DECL:
478 /* Push the CONST_DECL. */
479 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
480 break;
481
482 default:
483 gcc_unreachable ();
484 }
485 }
486 }
487
488 /* Restore the template parameter context for a member template, a
489 friend template defined in a class definition, or a non-template
490 member of template class. */
491
492 void
493 maybe_begin_member_template_processing (tree decl)
494 {
495 tree parms;
496 int levels = 0;
497 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
498
499 if (nsdmi)
500 {
501 tree ctx = DECL_CONTEXT (decl);
502 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
503 /* Disregard full specializations (c++/60999). */
504 && uses_template_parms (ctx)
505 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
506 }
507
508 if (inline_needs_template_parms (decl, nsdmi))
509 {
510 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
511 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
512
513 if (DECL_TEMPLATE_SPECIALIZATION (decl))
514 {
515 --levels;
516 parms = TREE_CHAIN (parms);
517 }
518
519 push_inline_template_parms_recursive (parms, levels);
520 }
521
522 /* Remember how many levels of template parameters we pushed so that
523 we can pop them later. */
524 inline_parm_levels.safe_push (levels);
525 }
526
527 /* Undo the effects of maybe_begin_member_template_processing. */
528
529 void
530 maybe_end_member_template_processing (void)
531 {
532 int i;
533 int last;
534
535 if (inline_parm_levels.length () == 0)
536 return;
537
538 last = inline_parm_levels.pop ();
539 for (i = 0; i < last; ++i)
540 {
541 --processing_template_decl;
542 current_template_parms = TREE_CHAIN (current_template_parms);
543 poplevel (0, 0, 0);
544 }
545 }
546
547 /* Return a new template argument vector which contains all of ARGS,
548 but has as its innermost set of arguments the EXTRA_ARGS. */
549
550 static tree
551 add_to_template_args (tree args, tree extra_args)
552 {
553 tree new_args;
554 int extra_depth;
555 int i;
556 int j;
557
558 if (args == NULL_TREE || extra_args == error_mark_node)
559 return extra_args;
560
561 extra_depth = TMPL_ARGS_DEPTH (extra_args);
562 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
563
564 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
565 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
566
567 for (j = 1; j <= extra_depth; ++j, ++i)
568 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
569
570 return new_args;
571 }
572
573 /* Like add_to_template_args, but only the outermost ARGS are added to
574 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
575 (EXTRA_ARGS) levels are added. This function is used to combine
576 the template arguments from a partial instantiation with the
577 template arguments used to attain the full instantiation from the
578 partial instantiation. */
579
580 tree
581 add_outermost_template_args (tree args, tree extra_args)
582 {
583 tree new_args;
584
585 /* If there are more levels of EXTRA_ARGS than there are ARGS,
586 something very fishy is going on. */
587 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
588
589 /* If *all* the new arguments will be the EXTRA_ARGS, just return
590 them. */
591 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
592 return extra_args;
593
594 /* For the moment, we make ARGS look like it contains fewer levels. */
595 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
596
597 new_args = add_to_template_args (args, extra_args);
598
599 /* Now, we restore ARGS to its full dimensions. */
600 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
601
602 return new_args;
603 }
604
605 /* Return the N levels of innermost template arguments from the ARGS. */
606
607 tree
608 get_innermost_template_args (tree args, int n)
609 {
610 tree new_args;
611 int extra_levels;
612 int i;
613
614 gcc_assert (n >= 0);
615
616 /* If N is 1, just return the innermost set of template arguments. */
617 if (n == 1)
618 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
619
620 /* If we're not removing anything, just return the arguments we were
621 given. */
622 extra_levels = TMPL_ARGS_DEPTH (args) - n;
623 gcc_assert (extra_levels >= 0);
624 if (extra_levels == 0)
625 return args;
626
627 /* Make a new set of arguments, not containing the outer arguments. */
628 new_args = make_tree_vec (n);
629 for (i = 1; i <= n; ++i)
630 SET_TMPL_ARGS_LEVEL (new_args, i,
631 TMPL_ARGS_LEVEL (args, i + extra_levels));
632
633 return new_args;
634 }
635
636 /* The inverse of get_innermost_template_args: Return all but the innermost
637 EXTRA_LEVELS levels of template arguments from the ARGS. */
638
639 static tree
640 strip_innermost_template_args (tree args, int extra_levels)
641 {
642 tree new_args;
643 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
644 int i;
645
646 gcc_assert (n >= 0);
647
648 /* If N is 1, just return the outermost set of template arguments. */
649 if (n == 1)
650 return TMPL_ARGS_LEVEL (args, 1);
651
652 /* If we're not removing anything, just return the arguments we were
653 given. */
654 gcc_assert (extra_levels >= 0);
655 if (extra_levels == 0)
656 return args;
657
658 /* Make a new set of arguments, not containing the inner arguments. */
659 new_args = make_tree_vec (n);
660 for (i = 1; i <= n; ++i)
661 SET_TMPL_ARGS_LEVEL (new_args, i,
662 TMPL_ARGS_LEVEL (args, i));
663
664 return new_args;
665 }
666
667 /* We've got a template header coming up; push to a new level for storing
668 the parms. */
669
670 void
671 begin_template_parm_list (void)
672 {
673 /* We use a non-tag-transparent scope here, which causes pushtag to
674 put tags in this scope, rather than in the enclosing class or
675 namespace scope. This is the right thing, since we want
676 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
677 global template class, push_template_decl handles putting the
678 TEMPLATE_DECL into top-level scope. For a nested template class,
679 e.g.:
680
681 template <class T> struct S1 {
682 template <class T> struct S2 {};
683 };
684
685 pushtag contains special code to insert the TEMPLATE_DECL for S2
686 at the right scope. */
687 begin_scope (sk_template_parms, NULL);
688 ++processing_template_decl;
689 ++processing_template_parmlist;
690 note_template_header (0);
691
692 /* Add a dummy parameter level while we process the parameter list. */
693 current_template_parms
694 = tree_cons (size_int (processing_template_decl),
695 make_tree_vec (0),
696 current_template_parms);
697 }
698
699 /* This routine is called when a specialization is declared. If it is
700 invalid to declare a specialization here, an error is reported and
701 false is returned, otherwise this routine will return true. */
702
703 static bool
704 check_specialization_scope (void)
705 {
706 tree scope = current_scope ();
707
708 /* [temp.expl.spec]
709
710 An explicit specialization shall be declared in the namespace of
711 which the template is a member, or, for member templates, in the
712 namespace of which the enclosing class or enclosing class
713 template is a member. An explicit specialization of a member
714 function, member class or static data member of a class template
715 shall be declared in the namespace of which the class template
716 is a member. */
717 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
718 {
719 error ("explicit specialization in non-namespace scope %qD", scope);
720 return false;
721 }
722
723 /* [temp.expl.spec]
724
725 In an explicit specialization declaration for a member of a class
726 template or a member template that appears in namespace scope,
727 the member template and some of its enclosing class templates may
728 remain unspecialized, except that the declaration shall not
729 explicitly specialize a class member template if its enclosing
730 class templates are not explicitly specialized as well. */
731 if (current_template_parms)
732 {
733 error ("enclosing class templates are not explicitly specialized");
734 return false;
735 }
736
737 return true;
738 }
739
740 /* We've just seen template <>. */
741
742 bool
743 begin_specialization (void)
744 {
745 begin_scope (sk_template_spec, NULL);
746 note_template_header (1);
747 return check_specialization_scope ();
748 }
749
750 /* Called at then end of processing a declaration preceded by
751 template<>. */
752
753 void
754 end_specialization (void)
755 {
756 finish_scope ();
757 reset_specialization ();
758 }
759
760 /* Any template <>'s that we have seen thus far are not referring to a
761 function specialization. */
762
763 void
764 reset_specialization (void)
765 {
766 processing_specialization = 0;
767 template_header_count = 0;
768 }
769
770 /* We've just seen a template header. If SPECIALIZATION is nonzero,
771 it was of the form template <>. */
772
773 static void
774 note_template_header (int specialization)
775 {
776 processing_specialization = specialization;
777 template_header_count++;
778 }
779
780 /* We're beginning an explicit instantiation. */
781
782 void
783 begin_explicit_instantiation (void)
784 {
785 gcc_assert (!processing_explicit_instantiation);
786 processing_explicit_instantiation = true;
787 }
788
789
790 void
791 end_explicit_instantiation (void)
792 {
793 gcc_assert (processing_explicit_instantiation);
794 processing_explicit_instantiation = false;
795 }
796
797 /* An explicit specialization or partial specialization of TMPL is being
798 declared. Check that the namespace in which the specialization is
799 occurring is permissible. Returns false iff it is invalid to
800 specialize TMPL in the current namespace. */
801
802 static bool
803 check_specialization_namespace (tree tmpl)
804 {
805 tree tpl_ns = decl_namespace_context (tmpl);
806
807 /* [tmpl.expl.spec]
808
809 An explicit specialization shall be declared in a namespace enclosing the
810 specialized template. An explicit specialization whose declarator-id is
811 not qualified shall be declared in the nearest enclosing namespace of the
812 template, or, if the namespace is inline (7.3.1), any namespace from its
813 enclosing namespace set. */
814 if (current_scope() != DECL_CONTEXT (tmpl)
815 && !at_namespace_scope_p ())
816 {
817 error ("specialization of %qD must appear at namespace scope", tmpl);
818 return false;
819 }
820
821 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
822 /* Same or enclosing namespace. */
823 return true;
824 else
825 {
826 auto_diagnostic_group d;
827 if (permerror (input_location,
828 "specialization of %qD in different namespace", tmpl))
829 inform (DECL_SOURCE_LOCATION (tmpl),
830 " from definition of %q#D", tmpl);
831 return false;
832 }
833 }
834
835 /* SPEC is an explicit instantiation. Check that it is valid to
836 perform this explicit instantiation in the current namespace. */
837
838 static void
839 check_explicit_instantiation_namespace (tree spec)
840 {
841 tree ns;
842
843 /* DR 275: An explicit instantiation shall appear in an enclosing
844 namespace of its template. */
845 ns = decl_namespace_context (spec);
846 if (!is_nested_namespace (current_namespace, ns))
847 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
848 "(which does not enclose namespace %qD)",
849 spec, current_namespace, ns);
850 }
851
852 /* Returns the type of a template specialization only if that
853 specialization needs to be defined. Otherwise (e.g., if the type has
854 already been defined), the function returns NULL_TREE. */
855
856 static tree
857 maybe_new_partial_specialization (tree type)
858 {
859 /* An implicit instantiation of an incomplete type implies
860 the definition of a new class template.
861
862 template<typename T>
863 struct S;
864
865 template<typename T>
866 struct S<T*>;
867
868 Here, S<T*> is an implicit instantiation of S whose type
869 is incomplete. */
870 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
871 return type;
872
873 /* It can also be the case that TYPE is a completed specialization.
874 Continuing the previous example, suppose we also declare:
875
876 template<typename T>
877 requires Integral<T>
878 struct S<T*>;
879
880 Here, S<T*> refers to the specialization S<T*> defined
881 above. However, we need to differentiate definitions because
882 we intend to define a new partial specialization. In this case,
883 we rely on the fact that the constraints are different for
884 this declaration than that above.
885
886 Note that we also get here for injected class names and
887 late-parsed template definitions. We must ensure that we
888 do not create new type declarations for those cases. */
889 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
890 {
891 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
892 tree args = CLASSTYPE_TI_ARGS (type);
893
894 /* If there are no template parameters, this cannot be a new
895 partial template specialization? */
896 if (!current_template_parms)
897 return NULL_TREE;
898
899 /* The injected-class-name is not a new partial specialization. */
900 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
901 return NULL_TREE;
902
903 /* If the constraints are not the same as those of the primary
904 then, we can probably create a new specialization. */
905 tree type_constr = current_template_constraints ();
906
907 if (type == TREE_TYPE (tmpl))
908 {
909 tree main_constr = get_constraints (tmpl);
910 if (equivalent_constraints (type_constr, main_constr))
911 return NULL_TREE;
912 }
913
914 /* Also, if there's a pre-existing specialization with matching
915 constraints, then this also isn't new. */
916 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
917 while (specs)
918 {
919 tree spec_tmpl = TREE_VALUE (specs);
920 tree spec_args = TREE_PURPOSE (specs);
921 tree spec_constr = get_constraints (spec_tmpl);
922 if (comp_template_args (args, spec_args)
923 && equivalent_constraints (type_constr, spec_constr))
924 return NULL_TREE;
925 specs = TREE_CHAIN (specs);
926 }
927
928 /* Create a new type node (and corresponding type decl)
929 for the newly declared specialization. */
930 tree t = make_class_type (TREE_CODE (type));
931 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
932 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
933
934 /* We only need a separate type node for storing the definition of this
935 partial specialization; uses of S<T*> are unconstrained, so all are
936 equivalent. So keep TYPE_CANONICAL the same. */
937 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
938
939 /* Build the corresponding type decl. */
940 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
941 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
942 DECL_SOURCE_LOCATION (d) = input_location;
943 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
944 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
945
946 return t;
947 }
948
949 return NULL_TREE;
950 }
951
952 /* The TYPE is being declared. If it is a template type, that means it
953 is a partial specialization. Do appropriate error-checking. */
954
955 tree
956 maybe_process_partial_specialization (tree type)
957 {
958 tree context;
959
960 if (type == error_mark_node)
961 return error_mark_node;
962
963 /* A lambda that appears in specialization context is not itself a
964 specialization. */
965 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
966 return type;
967
968 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
969 {
970 error ("name of class shadows template template parameter %qD",
971 TYPE_NAME (type));
972 return error_mark_node;
973 }
974
975 context = TYPE_CONTEXT (type);
976
977 if (TYPE_ALIAS_P (type))
978 {
979 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
980
981 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
982 error ("specialization of alias template %qD",
983 TI_TEMPLATE (tinfo));
984 else
985 error ("explicit specialization of non-template %qT", type);
986 return error_mark_node;
987 }
988 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
989 {
990 /* This is for ordinary explicit specialization and partial
991 specialization of a template class such as:
992
993 template <> class C<int>;
994
995 or:
996
997 template <class T> class C<T*>;
998
999 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1000
1001 if (tree t = maybe_new_partial_specialization (type))
1002 {
1003 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1004 && !at_namespace_scope_p ())
1005 return error_mark_node;
1006 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1007 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1008 if (processing_template_decl)
1009 {
1010 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1011 if (decl == error_mark_node)
1012 return error_mark_node;
1013 return TREE_TYPE (decl);
1014 }
1015 }
1016 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1017 error ("specialization of %qT after instantiation", type);
1018 else if (errorcount && !processing_specialization
1019 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1020 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1021 /* Trying to define a specialization either without a template<> header
1022 or in an inappropriate place. We've already given an error, so just
1023 bail now so we don't actually define the specialization. */
1024 return error_mark_node;
1025 }
1026 else if (CLASS_TYPE_P (type)
1027 && !CLASSTYPE_USE_TEMPLATE (type)
1028 && CLASSTYPE_TEMPLATE_INFO (type)
1029 && context && CLASS_TYPE_P (context)
1030 && CLASSTYPE_TEMPLATE_INFO (context))
1031 {
1032 /* This is for an explicit specialization of member class
1033 template according to [temp.expl.spec/18]:
1034
1035 template <> template <class U> class C<int>::D;
1036
1037 The context `C<int>' must be an implicit instantiation.
1038 Otherwise this is just a member class template declared
1039 earlier like:
1040
1041 template <> class C<int> { template <class U> class D; };
1042 template <> template <class U> class C<int>::D;
1043
1044 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1045 while in the second case, `C<int>::D' is a primary template
1046 and `C<T>::D' may not exist. */
1047
1048 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1049 && !COMPLETE_TYPE_P (type))
1050 {
1051 tree t;
1052 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1053
1054 if (current_namespace
1055 != decl_namespace_context (tmpl))
1056 {
1057 if (permerror (input_location,
1058 "specialization of %qD in different namespace",
1059 type))
1060 inform (DECL_SOURCE_LOCATION (tmpl),
1061 "from definition of %q#D", tmpl);
1062 }
1063
1064 /* Check for invalid specialization after instantiation:
1065
1066 template <> template <> class C<int>::D<int>;
1067 template <> template <class U> class C<int>::D; */
1068
1069 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1070 t; t = TREE_CHAIN (t))
1071 {
1072 tree inst = TREE_VALUE (t);
1073 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1074 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1075 {
1076 /* We already have a full specialization of this partial
1077 instantiation, or a full specialization has been
1078 looked up but not instantiated. Reassign it to the
1079 new member specialization template. */
1080 spec_entry elt;
1081 spec_entry *entry;
1082
1083 elt.tmpl = most_general_template (tmpl);
1084 elt.args = CLASSTYPE_TI_ARGS (inst);
1085 elt.spec = inst;
1086
1087 type_specializations->remove_elt (&elt);
1088
1089 elt.tmpl = tmpl;
1090 CLASSTYPE_TI_ARGS (inst)
1091 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1092
1093 spec_entry **slot
1094 = type_specializations->find_slot (&elt, INSERT);
1095 entry = ggc_alloc<spec_entry> ();
1096 *entry = elt;
1097 *slot = entry;
1098 }
1099 else
1100 /* But if we've had an implicit instantiation, that's a
1101 problem ([temp.expl.spec]/6). */
1102 error ("specialization %qT after instantiation %qT",
1103 type, inst);
1104 }
1105
1106 /* Mark TYPE as a specialization. And as a result, we only
1107 have one level of template argument for the innermost
1108 class template. */
1109 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1110 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1111 CLASSTYPE_TI_ARGS (type)
1112 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1113 }
1114 }
1115 else if (processing_specialization)
1116 {
1117 /* Someday C++0x may allow for enum template specialization. */
1118 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1119 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1120 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1121 "of %qD not allowed by ISO C++", type);
1122 else
1123 {
1124 error ("explicit specialization of non-template %qT", type);
1125 return error_mark_node;
1126 }
1127 }
1128
1129 return type;
1130 }
1131
1132 /* Returns nonzero if we can optimize the retrieval of specializations
1133 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1134 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1135
1136 static inline bool
1137 optimize_specialization_lookup_p (tree tmpl)
1138 {
1139 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1140 && DECL_CLASS_SCOPE_P (tmpl)
1141 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1142 parameter. */
1143 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1144 /* The optimized lookup depends on the fact that the
1145 template arguments for the member function template apply
1146 purely to the containing class, which is not true if the
1147 containing class is an explicit or partial
1148 specialization. */
1149 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1150 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1151 && !DECL_CONV_FN_P (tmpl)
1152 /* It is possible to have a template that is not a member
1153 template and is not a member of a template class:
1154
1155 template <typename T>
1156 struct S { friend A::f(); };
1157
1158 Here, the friend function is a template, but the context does
1159 not have template information. The optimized lookup relies
1160 on having ARGS be the template arguments for both the class
1161 and the function template. */
1162 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1163 }
1164
1165 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1166 gone through coerce_template_parms by now. */
1167
1168 static void
1169 verify_unstripped_args_1 (tree inner)
1170 {
1171 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1172 {
1173 tree arg = TREE_VEC_ELT (inner, i);
1174 if (TREE_CODE (arg) == TEMPLATE_DECL)
1175 /* OK */;
1176 else if (TYPE_P (arg))
1177 gcc_assert (strip_typedefs (arg, NULL) == arg);
1178 else if (ARGUMENT_PACK_P (arg))
1179 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1180 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1181 /* Allow typedefs on the type of a non-type argument, since a
1182 parameter can have them. */;
1183 else
1184 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1185 }
1186 }
1187
1188 static void
1189 verify_unstripped_args (tree args)
1190 {
1191 ++processing_template_decl;
1192 if (!any_dependent_template_arguments_p (args))
1193 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1194 --processing_template_decl;
1195 }
1196
1197 /* Retrieve the specialization (in the sense of [temp.spec] - a
1198 specialization is either an instantiation or an explicit
1199 specialization) of TMPL for the given template ARGS. If there is
1200 no such specialization, return NULL_TREE. The ARGS are a vector of
1201 arguments, or a vector of vectors of arguments, in the case of
1202 templates with more than one level of parameters.
1203
1204 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1205 then we search for a partial specialization matching ARGS. This
1206 parameter is ignored if TMPL is not a class template.
1207
1208 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1209 result is a NONTYPE_ARGUMENT_PACK. */
1210
1211 static tree
1212 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1213 {
1214 if (tmpl == NULL_TREE)
1215 return NULL_TREE;
1216
1217 if (args == error_mark_node)
1218 return NULL_TREE;
1219
1220 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1221 || TREE_CODE (tmpl) == FIELD_DECL);
1222
1223 /* There should be as many levels of arguments as there are
1224 levels of parameters. */
1225 gcc_assert (TMPL_ARGS_DEPTH (args)
1226 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1227 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1228 : template_class_depth (DECL_CONTEXT (tmpl))));
1229
1230 if (flag_checking)
1231 verify_unstripped_args (args);
1232
1233 /* Lambda functions in templates aren't instantiated normally, but through
1234 tsubst_lambda_expr. */
1235 if (lambda_fn_in_template_p (tmpl))
1236 return NULL_TREE;
1237
1238 if (optimize_specialization_lookup_p (tmpl))
1239 {
1240 /* The template arguments actually apply to the containing
1241 class. Find the class specialization with those
1242 arguments. */
1243 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1244 tree class_specialization
1245 = retrieve_specialization (class_template, args, 0);
1246 if (!class_specialization)
1247 return NULL_TREE;
1248
1249 /* Find the instance of TMPL. */
1250 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1251 for (ovl_iterator iter (fns); iter; ++iter)
1252 {
1253 tree fn = *iter;
1254 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1255 /* using-declarations can add base methods to the method vec,
1256 and we don't want those here. */
1257 && DECL_CONTEXT (fn) == class_specialization)
1258 return fn;
1259 }
1260 return NULL_TREE;
1261 }
1262 else
1263 {
1264 spec_entry *found;
1265 spec_entry elt;
1266 hash_table<spec_hasher> *specializations;
1267
1268 elt.tmpl = tmpl;
1269 elt.args = args;
1270 elt.spec = NULL_TREE;
1271
1272 if (DECL_CLASS_TEMPLATE_P (tmpl))
1273 specializations = type_specializations;
1274 else
1275 specializations = decl_specializations;
1276
1277 if (hash == 0)
1278 hash = spec_hasher::hash (&elt);
1279 found = specializations->find_with_hash (&elt, hash);
1280 if (found)
1281 return found->spec;
1282 }
1283
1284 return NULL_TREE;
1285 }
1286
1287 /* Like retrieve_specialization, but for local declarations. */
1288
1289 tree
1290 retrieve_local_specialization (tree tmpl)
1291 {
1292 if (local_specializations == NULL)
1293 return NULL_TREE;
1294
1295 tree *slot = local_specializations->get (tmpl);
1296 return slot ? *slot : NULL_TREE;
1297 }
1298
1299 /* Returns nonzero iff DECL is a specialization of TMPL. */
1300
1301 int
1302 is_specialization_of (tree decl, tree tmpl)
1303 {
1304 tree t;
1305
1306 if (TREE_CODE (decl) == FUNCTION_DECL)
1307 {
1308 for (t = decl;
1309 t != NULL_TREE;
1310 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1311 if (t == tmpl)
1312 return 1;
1313 }
1314 else
1315 {
1316 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1317
1318 for (t = TREE_TYPE (decl);
1319 t != NULL_TREE;
1320 t = CLASSTYPE_USE_TEMPLATE (t)
1321 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1322 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1323 return 1;
1324 }
1325
1326 return 0;
1327 }
1328
1329 /* Returns nonzero iff DECL is a specialization of friend declaration
1330 FRIEND_DECL according to [temp.friend]. */
1331
1332 bool
1333 is_specialization_of_friend (tree decl, tree friend_decl)
1334 {
1335 bool need_template = true;
1336 int template_depth;
1337
1338 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1339 || TREE_CODE (decl) == TYPE_DECL);
1340
1341 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1342 of a template class, we want to check if DECL is a specialization
1343 if this. */
1344 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1345 && DECL_TEMPLATE_INFO (friend_decl)
1346 && !DECL_USE_TEMPLATE (friend_decl))
1347 {
1348 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1349 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1350 need_template = false;
1351 }
1352 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1353 && !PRIMARY_TEMPLATE_P (friend_decl))
1354 need_template = false;
1355
1356 /* There is nothing to do if this is not a template friend. */
1357 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1358 return false;
1359
1360 if (is_specialization_of (decl, friend_decl))
1361 return true;
1362
1363 /* [temp.friend/6]
1364 A member of a class template may be declared to be a friend of a
1365 non-template class. In this case, the corresponding member of
1366 every specialization of the class template is a friend of the
1367 class granting friendship.
1368
1369 For example, given a template friend declaration
1370
1371 template <class T> friend void A<T>::f();
1372
1373 the member function below is considered a friend
1374
1375 template <> struct A<int> {
1376 void f();
1377 };
1378
1379 For this type of template friend, TEMPLATE_DEPTH below will be
1380 nonzero. To determine if DECL is a friend of FRIEND, we first
1381 check if the enclosing class is a specialization of another. */
1382
1383 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1384 if (template_depth
1385 && DECL_CLASS_SCOPE_P (decl)
1386 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1387 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1388 {
1389 /* Next, we check the members themselves. In order to handle
1390 a few tricky cases, such as when FRIEND_DECL's are
1391
1392 template <class T> friend void A<T>::g(T t);
1393 template <class T> template <T t> friend void A<T>::h();
1394
1395 and DECL's are
1396
1397 void A<int>::g(int);
1398 template <int> void A<int>::h();
1399
1400 we need to figure out ARGS, the template arguments from
1401 the context of DECL. This is required for template substitution
1402 of `T' in the function parameter of `g' and template parameter
1403 of `h' in the above examples. Here ARGS corresponds to `int'. */
1404
1405 tree context = DECL_CONTEXT (decl);
1406 tree args = NULL_TREE;
1407 int current_depth = 0;
1408
1409 while (current_depth < template_depth)
1410 {
1411 if (CLASSTYPE_TEMPLATE_INFO (context))
1412 {
1413 if (current_depth == 0)
1414 args = TYPE_TI_ARGS (context);
1415 else
1416 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1417 current_depth++;
1418 }
1419 context = TYPE_CONTEXT (context);
1420 }
1421
1422 if (TREE_CODE (decl) == FUNCTION_DECL)
1423 {
1424 bool is_template;
1425 tree friend_type;
1426 tree decl_type;
1427 tree friend_args_type;
1428 tree decl_args_type;
1429
1430 /* Make sure that both DECL and FRIEND_DECL are templates or
1431 non-templates. */
1432 is_template = DECL_TEMPLATE_INFO (decl)
1433 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1434 if (need_template ^ is_template)
1435 return false;
1436 else if (is_template)
1437 {
1438 /* If both are templates, check template parameter list. */
1439 tree friend_parms
1440 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1441 args, tf_none);
1442 if (!comp_template_parms
1443 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1444 friend_parms))
1445 return false;
1446
1447 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1448 }
1449 else
1450 decl_type = TREE_TYPE (decl);
1451
1452 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1453 tf_none, NULL_TREE);
1454 if (friend_type == error_mark_node)
1455 return false;
1456
1457 /* Check if return types match. */
1458 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1459 return false;
1460
1461 /* Check if function parameter types match, ignoring the
1462 `this' parameter. */
1463 friend_args_type = TYPE_ARG_TYPES (friend_type);
1464 decl_args_type = TYPE_ARG_TYPES (decl_type);
1465 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1466 friend_args_type = TREE_CHAIN (friend_args_type);
1467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1468 decl_args_type = TREE_CHAIN (decl_args_type);
1469
1470 return compparms (decl_args_type, friend_args_type);
1471 }
1472 else
1473 {
1474 /* DECL is a TYPE_DECL */
1475 bool is_template;
1476 tree decl_type = TREE_TYPE (decl);
1477
1478 /* Make sure that both DECL and FRIEND_DECL are templates or
1479 non-templates. */
1480 is_template
1481 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1482 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1483
1484 if (need_template ^ is_template)
1485 return false;
1486 else if (is_template)
1487 {
1488 tree friend_parms;
1489 /* If both are templates, check the name of the two
1490 TEMPLATE_DECL's first because is_friend didn't. */
1491 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1492 != DECL_NAME (friend_decl))
1493 return false;
1494
1495 /* Now check template parameter list. */
1496 friend_parms
1497 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1498 args, tf_none);
1499 return comp_template_parms
1500 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1501 friend_parms);
1502 }
1503 else
1504 return (DECL_NAME (decl)
1505 == DECL_NAME (friend_decl));
1506 }
1507 }
1508 return false;
1509 }
1510
1511 /* Register the specialization SPEC as a specialization of TMPL with
1512 the indicated ARGS. IS_FRIEND indicates whether the specialization
1513 is actually just a friend declaration. ATTRLIST is the list of
1514 attributes that the specialization is declared with or NULL when
1515 it isn't. Returns SPEC, or an equivalent prior declaration, if
1516 available.
1517
1518 We also store instantiations of field packs in the hash table, even
1519 though they are not themselves templates, to make lookup easier. */
1520
1521 static tree
1522 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1523 hashval_t hash)
1524 {
1525 tree fn;
1526 spec_entry **slot = NULL;
1527 spec_entry elt;
1528
1529 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1530 || (TREE_CODE (tmpl) == FIELD_DECL
1531 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1532
1533 if (TREE_CODE (spec) == FUNCTION_DECL
1534 && uses_template_parms (DECL_TI_ARGS (spec)))
1535 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1536 register it; we want the corresponding TEMPLATE_DECL instead.
1537 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1538 the more obvious `uses_template_parms (spec)' to avoid problems
1539 with default function arguments. In particular, given
1540 something like this:
1541
1542 template <class T> void f(T t1, T t = T())
1543
1544 the default argument expression is not substituted for in an
1545 instantiation unless and until it is actually needed. */
1546 return spec;
1547
1548 if (optimize_specialization_lookup_p (tmpl))
1549 /* We don't put these specializations in the hash table, but we might
1550 want to give an error about a mismatch. */
1551 fn = retrieve_specialization (tmpl, args, 0);
1552 else
1553 {
1554 elt.tmpl = tmpl;
1555 elt.args = args;
1556 elt.spec = spec;
1557
1558 if (hash == 0)
1559 hash = spec_hasher::hash (&elt);
1560
1561 slot =
1562 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1563 if (*slot)
1564 fn = ((spec_entry *) *slot)->spec;
1565 else
1566 fn = NULL_TREE;
1567 }
1568
1569 /* We can sometimes try to re-register a specialization that we've
1570 already got. In particular, regenerate_decl_from_template calls
1571 duplicate_decls which will update the specialization list. But,
1572 we'll still get called again here anyhow. It's more convenient
1573 to simply allow this than to try to prevent it. */
1574 if (fn == spec)
1575 return spec;
1576 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1577 {
1578 if (DECL_TEMPLATE_INSTANTIATION (fn))
1579 {
1580 if (DECL_ODR_USED (fn)
1581 || DECL_EXPLICIT_INSTANTIATION (fn))
1582 {
1583 error ("specialization of %qD after instantiation",
1584 fn);
1585 return error_mark_node;
1586 }
1587 else
1588 {
1589 tree clone;
1590 /* This situation should occur only if the first
1591 specialization is an implicit instantiation, the
1592 second is an explicit specialization, and the
1593 implicit instantiation has not yet been used. That
1594 situation can occur if we have implicitly
1595 instantiated a member function and then specialized
1596 it later.
1597
1598 We can also wind up here if a friend declaration that
1599 looked like an instantiation turns out to be a
1600 specialization:
1601
1602 template <class T> void foo(T);
1603 class S { friend void foo<>(int) };
1604 template <> void foo(int);
1605
1606 We transform the existing DECL in place so that any
1607 pointers to it become pointers to the updated
1608 declaration.
1609
1610 If there was a definition for the template, but not
1611 for the specialization, we want this to look as if
1612 there were no definition, and vice versa. */
1613 DECL_INITIAL (fn) = NULL_TREE;
1614 duplicate_decls (spec, fn, is_friend);
1615 /* The call to duplicate_decls will have applied
1616 [temp.expl.spec]:
1617
1618 An explicit specialization of a function template
1619 is inline only if it is explicitly declared to be,
1620 and independently of whether its function template
1621 is.
1622
1623 to the primary function; now copy the inline bits to
1624 the various clones. */
1625 FOR_EACH_CLONE (clone, fn)
1626 {
1627 DECL_DECLARED_INLINE_P (clone)
1628 = DECL_DECLARED_INLINE_P (fn);
1629 DECL_SOURCE_LOCATION (clone)
1630 = DECL_SOURCE_LOCATION (fn);
1631 DECL_DELETED_FN (clone)
1632 = DECL_DELETED_FN (fn);
1633 }
1634 check_specialization_namespace (tmpl);
1635
1636 return fn;
1637 }
1638 }
1639 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1640 {
1641 tree dd = duplicate_decls (spec, fn, is_friend);
1642 if (dd == error_mark_node)
1643 /* We've already complained in duplicate_decls. */
1644 return error_mark_node;
1645
1646 if (dd == NULL_TREE && DECL_INITIAL (spec))
1647 /* Dup decl failed, but this is a new definition. Set the
1648 line number so any errors match this new
1649 definition. */
1650 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1651
1652 return fn;
1653 }
1654 }
1655 else if (fn)
1656 return duplicate_decls (spec, fn, is_friend);
1657
1658 /* A specialization must be declared in the same namespace as the
1659 template it is specializing. */
1660 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1661 && !check_specialization_namespace (tmpl))
1662 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1663
1664 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1665 {
1666 spec_entry *entry = ggc_alloc<spec_entry> ();
1667 gcc_assert (tmpl && args && spec);
1668 *entry = elt;
1669 *slot = entry;
1670 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1671 && PRIMARY_TEMPLATE_P (tmpl)
1672 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1673 || variable_template_p (tmpl))
1674 /* If TMPL is a forward declaration of a template function, keep a list
1675 of all specializations in case we need to reassign them to a friend
1676 template later in tsubst_friend_function.
1677
1678 Also keep a list of all variable template instantiations so that
1679 process_partial_specialization can check whether a later partial
1680 specialization would have used it. */
1681 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1682 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1683 }
1684
1685 return spec;
1686 }
1687
1688 /* Returns true iff two spec_entry nodes are equivalent. */
1689
1690 int comparing_specializations;
1691
1692 bool
1693 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1694 {
1695 int equal;
1696
1697 ++comparing_specializations;
1698 equal = (e1->tmpl == e2->tmpl
1699 && comp_template_args (e1->args, e2->args));
1700 if (equal && flag_concepts
1701 /* tmpl could be a FIELD_DECL for a capture pack. */
1702 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1703 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1704 && uses_template_parms (e1->args))
1705 {
1706 /* Partial specializations of a variable template can be distinguished by
1707 constraints. */
1708 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1709 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1710 equal = equivalent_constraints (c1, c2);
1711 }
1712 --comparing_specializations;
1713
1714 return equal;
1715 }
1716
1717 /* Returns a hash for a template TMPL and template arguments ARGS. */
1718
1719 static hashval_t
1720 hash_tmpl_and_args (tree tmpl, tree args)
1721 {
1722 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1723 return iterative_hash_template_arg (args, val);
1724 }
1725
1726 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1727 ignoring SPEC. */
1728
1729 hashval_t
1730 spec_hasher::hash (spec_entry *e)
1731 {
1732 return hash_tmpl_and_args (e->tmpl, e->args);
1733 }
1734
1735 /* Recursively calculate a hash value for a template argument ARG, for use
1736 in the hash tables of template specializations. */
1737
1738 hashval_t
1739 iterative_hash_template_arg (tree arg, hashval_t val)
1740 {
1741 unsigned HOST_WIDE_INT i;
1742 enum tree_code code;
1743 char tclass;
1744
1745 if (arg == NULL_TREE)
1746 return iterative_hash_object (arg, val);
1747
1748 if (!TYPE_P (arg))
1749 STRIP_NOPS (arg);
1750
1751 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1752 gcc_unreachable ();
1753
1754 code = TREE_CODE (arg);
1755 tclass = TREE_CODE_CLASS (code);
1756
1757 val = iterative_hash_object (code, val);
1758
1759 switch (code)
1760 {
1761 case ERROR_MARK:
1762 return val;
1763
1764 case IDENTIFIER_NODE:
1765 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1766
1767 case TREE_VEC:
1768 {
1769 int i, len = TREE_VEC_LENGTH (arg);
1770 for (i = 0; i < len; ++i)
1771 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1772 return val;
1773 }
1774
1775 case TYPE_PACK_EXPANSION:
1776 case EXPR_PACK_EXPANSION:
1777 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1778 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1779
1780 case TYPE_ARGUMENT_PACK:
1781 case NONTYPE_ARGUMENT_PACK:
1782 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1783
1784 case TREE_LIST:
1785 for (; arg; arg = TREE_CHAIN (arg))
1786 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1787 return val;
1788
1789 case OVERLOAD:
1790 for (lkp_iterator iter (arg); iter; ++iter)
1791 val = iterative_hash_template_arg (*iter, val);
1792 return val;
1793
1794 case CONSTRUCTOR:
1795 {
1796 tree field, value;
1797 iterative_hash_template_arg (TREE_TYPE (arg), val);
1798 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1799 {
1800 val = iterative_hash_template_arg (field, val);
1801 val = iterative_hash_template_arg (value, val);
1802 }
1803 return val;
1804 }
1805
1806 case PARM_DECL:
1807 if (!DECL_ARTIFICIAL (arg))
1808 {
1809 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1810 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1811 }
1812 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1813
1814 case TARGET_EXPR:
1815 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1816
1817 case PTRMEM_CST:
1818 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1819 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1820
1821 case TEMPLATE_PARM_INDEX:
1822 val = iterative_hash_template_arg
1823 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1824 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1825 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1826
1827 case TRAIT_EXPR:
1828 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1829 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1830 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1831
1832 case BASELINK:
1833 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1834 val);
1835 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1836 val);
1837
1838 case MODOP_EXPR:
1839 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1840 code = TREE_CODE (TREE_OPERAND (arg, 1));
1841 val = iterative_hash_object (code, val);
1842 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1843
1844 case LAMBDA_EXPR:
1845 /* [temp.over.link] Two lambda-expressions are never considered
1846 equivalent.
1847
1848 So just hash the closure type. */
1849 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1850
1851 case CAST_EXPR:
1852 case IMPLICIT_CONV_EXPR:
1853 case STATIC_CAST_EXPR:
1854 case REINTERPRET_CAST_EXPR:
1855 case CONST_CAST_EXPR:
1856 case DYNAMIC_CAST_EXPR:
1857 case NEW_EXPR:
1858 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1859 /* Now hash operands as usual. */
1860 break;
1861
1862 case CALL_EXPR:
1863 {
1864 tree fn = CALL_EXPR_FN (arg);
1865 if (tree name = dependent_name (fn))
1866 {
1867 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1868 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1869 fn = name;
1870 }
1871 val = iterative_hash_template_arg (fn, val);
1872 call_expr_arg_iterator ai;
1873 for (tree x = first_call_expr_arg (arg, &ai); x;
1874 x = next_call_expr_arg (&ai))
1875 val = iterative_hash_template_arg (x, val);
1876 return val;
1877 }
1878
1879 default:
1880 break;
1881 }
1882
1883 switch (tclass)
1884 {
1885 case tcc_type:
1886 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1887 {
1888 // We want an alias specialization that survived strip_typedefs
1889 // to hash differently from its TYPE_CANONICAL, to avoid hash
1890 // collisions that compare as different in template_args_equal.
1891 // These could be dependent specializations that strip_typedefs
1892 // left alone, or untouched specializations because
1893 // coerce_template_parms returns the unconverted template
1894 // arguments if it sees incomplete argument packs.
1895 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1896 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1897 }
1898 if (TYPE_CANONICAL (arg))
1899 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1900 val);
1901 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1902 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1903 /* Otherwise just compare the types during lookup. */
1904 return val;
1905
1906 case tcc_declaration:
1907 case tcc_constant:
1908 return iterative_hash_expr (arg, val);
1909
1910 default:
1911 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1912 {
1913 unsigned n = cp_tree_operand_length (arg);
1914 for (i = 0; i < n; ++i)
1915 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1916 return val;
1917 }
1918 }
1919 gcc_unreachable ();
1920 return 0;
1921 }
1922
1923 /* Unregister the specialization SPEC as a specialization of TMPL.
1924 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1925 if the SPEC was listed as a specialization of TMPL.
1926
1927 Note that SPEC has been ggc_freed, so we can't look inside it. */
1928
1929 bool
1930 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1931 {
1932 spec_entry *entry;
1933 spec_entry elt;
1934
1935 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1936 elt.args = TI_ARGS (tinfo);
1937 elt.spec = NULL_TREE;
1938
1939 entry = decl_specializations->find (&elt);
1940 if (entry != NULL)
1941 {
1942 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1943 gcc_assert (new_spec != NULL_TREE);
1944 entry->spec = new_spec;
1945 return 1;
1946 }
1947
1948 return 0;
1949 }
1950
1951 /* Like register_specialization, but for local declarations. We are
1952 registering SPEC, an instantiation of TMPL. */
1953
1954 void
1955 register_local_specialization (tree spec, tree tmpl)
1956 {
1957 gcc_assert (tmpl != spec);
1958 local_specializations->put (tmpl, spec);
1959 }
1960
1961 /* TYPE is a class type. Returns true if TYPE is an explicitly
1962 specialized class. */
1963
1964 bool
1965 explicit_class_specialization_p (tree type)
1966 {
1967 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1968 return false;
1969 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1970 }
1971
1972 /* Print the list of functions at FNS, going through all the overloads
1973 for each element of the list. Alternatively, FNS cannot be a
1974 TREE_LIST, in which case it will be printed together with all the
1975 overloads.
1976
1977 MORE and *STR should respectively be FALSE and NULL when the function
1978 is called from the outside. They are used internally on recursive
1979 calls. print_candidates manages the two parameters and leaves NULL
1980 in *STR when it ends. */
1981
1982 static void
1983 print_candidates_1 (tree fns, char **str, bool more = false)
1984 {
1985 if (TREE_CODE (fns) == TREE_LIST)
1986 for (; fns; fns = TREE_CHAIN (fns))
1987 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1988 else
1989 for (lkp_iterator iter (fns); iter;)
1990 {
1991 tree cand = *iter;
1992 ++iter;
1993
1994 const char *pfx = *str;
1995 if (!pfx)
1996 {
1997 if (more || iter)
1998 pfx = _("candidates are:");
1999 else
2000 pfx = _("candidate is:");
2001 *str = get_spaces (pfx);
2002 }
2003 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2004 }
2005 }
2006
2007 /* Print the list of candidate FNS in an error message. FNS can also
2008 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2009
2010 void
2011 print_candidates (tree fns)
2012 {
2013 char *str = NULL;
2014 print_candidates_1 (fns, &str);
2015 free (str);
2016 }
2017
2018 /* Get a (possibly) constrained template declaration for the
2019 purpose of ordering candidates. */
2020 static tree
2021 get_template_for_ordering (tree list)
2022 {
2023 gcc_assert (TREE_CODE (list) == TREE_LIST);
2024 tree f = TREE_VALUE (list);
2025 if (tree ti = DECL_TEMPLATE_INFO (f))
2026 return TI_TEMPLATE (ti);
2027 return f;
2028 }
2029
2030 /* Among candidates having the same signature, return the
2031 most constrained or NULL_TREE if there is no best candidate.
2032 If the signatures of candidates vary (e.g., template
2033 specialization vs. member function), then there can be no
2034 most constrained.
2035
2036 Note that we don't compare constraints on the functions
2037 themselves, but rather those of their templates. */
2038 static tree
2039 most_constrained_function (tree candidates)
2040 {
2041 // Try to find the best candidate in a first pass.
2042 tree champ = candidates;
2043 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2044 {
2045 int winner = more_constrained (get_template_for_ordering (champ),
2046 get_template_for_ordering (c));
2047 if (winner == -1)
2048 champ = c; // The candidate is more constrained
2049 else if (winner == 0)
2050 return NULL_TREE; // Neither is more constrained
2051 }
2052
2053 // Verify that the champ is better than previous candidates.
2054 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2055 if (!more_constrained (get_template_for_ordering (champ),
2056 get_template_for_ordering (c)))
2057 return NULL_TREE;
2058 }
2059
2060 return champ;
2061 }
2062
2063
2064 /* Returns the template (one of the functions given by TEMPLATE_ID)
2065 which can be specialized to match the indicated DECL with the
2066 explicit template args given in TEMPLATE_ID. The DECL may be
2067 NULL_TREE if none is available. In that case, the functions in
2068 TEMPLATE_ID are non-members.
2069
2070 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2071 specialization of a member template.
2072
2073 The TEMPLATE_COUNT is the number of references to qualifying
2074 template classes that appeared in the name of the function. See
2075 check_explicit_specialization for a more accurate description.
2076
2077 TSK indicates what kind of template declaration (if any) is being
2078 declared. TSK_TEMPLATE indicates that the declaration given by
2079 DECL, though a FUNCTION_DECL, has template parameters, and is
2080 therefore a template function.
2081
2082 The template args (those explicitly specified and those deduced)
2083 are output in a newly created vector *TARGS_OUT.
2084
2085 If it is impossible to determine the result, an error message is
2086 issued. The error_mark_node is returned to indicate failure. */
2087
2088 static tree
2089 determine_specialization (tree template_id,
2090 tree decl,
2091 tree* targs_out,
2092 int need_member_template,
2093 int template_count,
2094 tmpl_spec_kind tsk)
2095 {
2096 tree fns;
2097 tree targs;
2098 tree explicit_targs;
2099 tree candidates = NULL_TREE;
2100
2101 /* A TREE_LIST of templates of which DECL may be a specialization.
2102 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2103 corresponding TREE_PURPOSE is the set of template arguments that,
2104 when used to instantiate the template, would produce a function
2105 with the signature of DECL. */
2106 tree templates = NULL_TREE;
2107 int header_count;
2108 cp_binding_level *b;
2109
2110 *targs_out = NULL_TREE;
2111
2112 if (template_id == error_mark_node || decl == error_mark_node)
2113 return error_mark_node;
2114
2115 /* We shouldn't be specializing a member template of an
2116 unspecialized class template; we already gave an error in
2117 check_specialization_scope, now avoid crashing. */
2118 if (!VAR_P (decl)
2119 && template_count && DECL_CLASS_SCOPE_P (decl)
2120 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2121 {
2122 gcc_assert (errorcount);
2123 return error_mark_node;
2124 }
2125
2126 fns = TREE_OPERAND (template_id, 0);
2127 explicit_targs = TREE_OPERAND (template_id, 1);
2128
2129 if (fns == error_mark_node)
2130 return error_mark_node;
2131
2132 /* Check for baselinks. */
2133 if (BASELINK_P (fns))
2134 fns = BASELINK_FUNCTIONS (fns);
2135
2136 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2137 {
2138 error_at (DECL_SOURCE_LOCATION (decl),
2139 "%qD is not a function template", fns);
2140 return error_mark_node;
2141 }
2142 else if (VAR_P (decl) && !variable_template_p (fns))
2143 {
2144 error ("%qD is not a variable template", fns);
2145 return error_mark_node;
2146 }
2147
2148 /* Count the number of template headers specified for this
2149 specialization. */
2150 header_count = 0;
2151 for (b = current_binding_level;
2152 b->kind == sk_template_parms;
2153 b = b->level_chain)
2154 ++header_count;
2155
2156 tree orig_fns = fns;
2157
2158 if (variable_template_p (fns))
2159 {
2160 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2161 targs = coerce_template_parms (parms, explicit_targs, fns,
2162 tf_warning_or_error,
2163 /*req_all*/true, /*use_defarg*/true);
2164 if (targs != error_mark_node)
2165 templates = tree_cons (targs, fns, templates);
2166 }
2167 else for (lkp_iterator iter (fns); iter; ++iter)
2168 {
2169 tree fn = *iter;
2170
2171 if (TREE_CODE (fn) == TEMPLATE_DECL)
2172 {
2173 tree decl_arg_types;
2174 tree fn_arg_types;
2175 tree insttype;
2176
2177 /* In case of explicit specialization, we need to check if
2178 the number of template headers appearing in the specialization
2179 is correct. This is usually done in check_explicit_specialization,
2180 but the check done there cannot be exhaustive when specializing
2181 member functions. Consider the following code:
2182
2183 template <> void A<int>::f(int);
2184 template <> template <> void A<int>::f(int);
2185
2186 Assuming that A<int> is not itself an explicit specialization
2187 already, the first line specializes "f" which is a non-template
2188 member function, whilst the second line specializes "f" which
2189 is a template member function. So both lines are syntactically
2190 correct, and check_explicit_specialization does not reject
2191 them.
2192
2193 Here, we can do better, as we are matching the specialization
2194 against the declarations. We count the number of template
2195 headers, and we check if they match TEMPLATE_COUNT + 1
2196 (TEMPLATE_COUNT is the number of qualifying template classes,
2197 plus there must be another header for the member template
2198 itself).
2199
2200 Notice that if header_count is zero, this is not a
2201 specialization but rather a template instantiation, so there
2202 is no check we can perform here. */
2203 if (header_count && header_count != template_count + 1)
2204 continue;
2205
2206 /* Check that the number of template arguments at the
2207 innermost level for DECL is the same as for FN. */
2208 if (current_binding_level->kind == sk_template_parms
2209 && !current_binding_level->explicit_spec_p
2210 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2211 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2212 (current_template_parms))))
2213 continue;
2214
2215 /* DECL might be a specialization of FN. */
2216 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2217 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2218
2219 /* For a non-static member function, we need to make sure
2220 that the const qualification is the same. Since
2221 get_bindings does not try to merge the "this" parameter,
2222 we must do the comparison explicitly. */
2223 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2224 {
2225 if (!same_type_p (TREE_VALUE (fn_arg_types),
2226 TREE_VALUE (decl_arg_types)))
2227 continue;
2228
2229 /* And the ref-qualification. */
2230 if (type_memfn_rqual (TREE_TYPE (decl))
2231 != type_memfn_rqual (TREE_TYPE (fn)))
2232 continue;
2233 }
2234
2235 /* Skip the "this" parameter and, for constructors of
2236 classes with virtual bases, the VTT parameter. A
2237 full specialization of a constructor will have a VTT
2238 parameter, but a template never will. */
2239 decl_arg_types
2240 = skip_artificial_parms_for (decl, decl_arg_types);
2241 fn_arg_types
2242 = skip_artificial_parms_for (fn, fn_arg_types);
2243
2244 /* Function templates cannot be specializations; there are
2245 no partial specializations of functions. Therefore, if
2246 the type of DECL does not match FN, there is no
2247 match.
2248
2249 Note that it should never be the case that we have both
2250 candidates added here, and for regular member functions
2251 below. */
2252 if (tsk == tsk_template)
2253 {
2254 if (compparms (fn_arg_types, decl_arg_types))
2255 candidates = tree_cons (NULL_TREE, fn, candidates);
2256 continue;
2257 }
2258
2259 /* See whether this function might be a specialization of this
2260 template. Suppress access control because we might be trying
2261 to make this specialization a friend, and we have already done
2262 access control for the declaration of the specialization. */
2263 push_deferring_access_checks (dk_no_check);
2264 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2265 pop_deferring_access_checks ();
2266
2267 if (!targs)
2268 /* We cannot deduce template arguments that when used to
2269 specialize TMPL will produce DECL. */
2270 continue;
2271
2272 if (uses_template_parms (targs))
2273 /* We deduced something involving 'auto', which isn't a valid
2274 template argument. */
2275 continue;
2276
2277 /* Remove, from the set of candidates, all those functions
2278 whose constraints are not satisfied. */
2279 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2280 continue;
2281
2282 // Then, try to form the new function type.
2283 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2284 if (insttype == error_mark_node)
2285 continue;
2286 fn_arg_types
2287 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2288 if (!compparms (fn_arg_types, decl_arg_types))
2289 continue;
2290
2291 /* Save this template, and the arguments deduced. */
2292 templates = tree_cons (targs, fn, templates);
2293 }
2294 else if (need_member_template)
2295 /* FN is an ordinary member function, and we need a
2296 specialization of a member template. */
2297 ;
2298 else if (TREE_CODE (fn) != FUNCTION_DECL)
2299 /* We can get IDENTIFIER_NODEs here in certain erroneous
2300 cases. */
2301 ;
2302 else if (!DECL_FUNCTION_MEMBER_P (fn))
2303 /* This is just an ordinary non-member function. Nothing can
2304 be a specialization of that. */
2305 ;
2306 else if (DECL_ARTIFICIAL (fn))
2307 /* Cannot specialize functions that are created implicitly. */
2308 ;
2309 else
2310 {
2311 tree decl_arg_types;
2312
2313 /* This is an ordinary member function. However, since
2314 we're here, we can assume its enclosing class is a
2315 template class. For example,
2316
2317 template <typename T> struct S { void f(); };
2318 template <> void S<int>::f() {}
2319
2320 Here, S<int>::f is a non-template, but S<int> is a
2321 template class. If FN has the same type as DECL, we
2322 might be in business. */
2323
2324 if (!DECL_TEMPLATE_INFO (fn))
2325 /* Its enclosing class is an explicit specialization
2326 of a template class. This is not a candidate. */
2327 continue;
2328
2329 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2330 TREE_TYPE (TREE_TYPE (fn))))
2331 /* The return types differ. */
2332 continue;
2333
2334 /* Adjust the type of DECL in case FN is a static member. */
2335 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2336 if (DECL_STATIC_FUNCTION_P (fn)
2337 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2338 decl_arg_types = TREE_CHAIN (decl_arg_types);
2339
2340 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2341 decl_arg_types))
2342 continue;
2343
2344 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2345 && (type_memfn_rqual (TREE_TYPE (decl))
2346 != type_memfn_rqual (TREE_TYPE (fn))))
2347 continue;
2348
2349 // If the deduced arguments do not satisfy the constraints,
2350 // this is not a candidate.
2351 if (flag_concepts && !constraints_satisfied_p (fn))
2352 continue;
2353
2354 // Add the candidate.
2355 candidates = tree_cons (NULL_TREE, fn, candidates);
2356 }
2357 }
2358
2359 if (templates && TREE_CHAIN (templates))
2360 {
2361 /* We have:
2362
2363 [temp.expl.spec]
2364
2365 It is possible for a specialization with a given function
2366 signature to be instantiated from more than one function
2367 template. In such cases, explicit specification of the
2368 template arguments must be used to uniquely identify the
2369 function template specialization being specialized.
2370
2371 Note that here, there's no suggestion that we're supposed to
2372 determine which of the candidate templates is most
2373 specialized. However, we, also have:
2374
2375 [temp.func.order]
2376
2377 Partial ordering of overloaded function template
2378 declarations is used in the following contexts to select
2379 the function template to which a function template
2380 specialization refers:
2381
2382 -- when an explicit specialization refers to a function
2383 template.
2384
2385 So, we do use the partial ordering rules, at least for now.
2386 This extension can only serve to make invalid programs valid,
2387 so it's safe. And, there is strong anecdotal evidence that
2388 the committee intended the partial ordering rules to apply;
2389 the EDG front end has that behavior, and John Spicer claims
2390 that the committee simply forgot to delete the wording in
2391 [temp.expl.spec]. */
2392 tree tmpl = most_specialized_instantiation (templates);
2393 if (tmpl != error_mark_node)
2394 {
2395 templates = tmpl;
2396 TREE_CHAIN (templates) = NULL_TREE;
2397 }
2398 }
2399
2400 // Concepts allows multiple declarations of member functions
2401 // with the same signature. Like above, we need to rely on
2402 // on the partial ordering of those candidates to determine which
2403 // is the best.
2404 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2405 {
2406 if (tree cand = most_constrained_function (candidates))
2407 {
2408 candidates = cand;
2409 TREE_CHAIN (cand) = NULL_TREE;
2410 }
2411 }
2412
2413 if (templates == NULL_TREE && candidates == NULL_TREE)
2414 {
2415 error ("template-id %qD for %q+D does not match any template "
2416 "declaration", template_id, decl);
2417 if (header_count && header_count != template_count + 1)
2418 inform (DECL_SOURCE_LOCATION (decl),
2419 "saw %d %<template<>%>, need %d for "
2420 "specializing a member function template",
2421 header_count, template_count + 1);
2422 else
2423 print_candidates (orig_fns);
2424 return error_mark_node;
2425 }
2426 else if ((templates && TREE_CHAIN (templates))
2427 || (candidates && TREE_CHAIN (candidates))
2428 || (templates && candidates))
2429 {
2430 error ("ambiguous template specialization %qD for %q+D",
2431 template_id, decl);
2432 candidates = chainon (candidates, templates);
2433 print_candidates (candidates);
2434 return error_mark_node;
2435 }
2436
2437 /* We have one, and exactly one, match. */
2438 if (candidates)
2439 {
2440 tree fn = TREE_VALUE (candidates);
2441 *targs_out = copy_node (DECL_TI_ARGS (fn));
2442
2443 /* Propagate the candidate's constraints to the declaration. */
2444 set_constraints (decl, get_constraints (fn));
2445
2446 /* DECL is a re-declaration or partial instantiation of a template
2447 function. */
2448 if (TREE_CODE (fn) == TEMPLATE_DECL)
2449 return fn;
2450 /* It was a specialization of an ordinary member function in a
2451 template class. */
2452 return DECL_TI_TEMPLATE (fn);
2453 }
2454
2455 /* It was a specialization of a template. */
2456 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2457 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2458 {
2459 *targs_out = copy_node (targs);
2460 SET_TMPL_ARGS_LEVEL (*targs_out,
2461 TMPL_ARGS_DEPTH (*targs_out),
2462 TREE_PURPOSE (templates));
2463 }
2464 else
2465 *targs_out = TREE_PURPOSE (templates);
2466 return TREE_VALUE (templates);
2467 }
2468
2469 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2470 but with the default argument values filled in from those in the
2471 TMPL_TYPES. */
2472
2473 static tree
2474 copy_default_args_to_explicit_spec_1 (tree spec_types,
2475 tree tmpl_types)
2476 {
2477 tree new_spec_types;
2478
2479 if (!spec_types)
2480 return NULL_TREE;
2481
2482 if (spec_types == void_list_node)
2483 return void_list_node;
2484
2485 /* Substitute into the rest of the list. */
2486 new_spec_types =
2487 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2488 TREE_CHAIN (tmpl_types));
2489
2490 /* Add the default argument for this parameter. */
2491 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2492 TREE_VALUE (spec_types),
2493 new_spec_types);
2494 }
2495
2496 /* DECL is an explicit specialization. Replicate default arguments
2497 from the template it specializes. (That way, code like:
2498
2499 template <class T> void f(T = 3);
2500 template <> void f(double);
2501 void g () { f (); }
2502
2503 works, as required.) An alternative approach would be to look up
2504 the correct default arguments at the call-site, but this approach
2505 is consistent with how implicit instantiations are handled. */
2506
2507 static void
2508 copy_default_args_to_explicit_spec (tree decl)
2509 {
2510 tree tmpl;
2511 tree spec_types;
2512 tree tmpl_types;
2513 tree new_spec_types;
2514 tree old_type;
2515 tree new_type;
2516 tree t;
2517 tree object_type = NULL_TREE;
2518 tree in_charge = NULL_TREE;
2519 tree vtt = NULL_TREE;
2520
2521 /* See if there's anything we need to do. */
2522 tmpl = DECL_TI_TEMPLATE (decl);
2523 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2524 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2525 if (TREE_PURPOSE (t))
2526 break;
2527 if (!t)
2528 return;
2529
2530 old_type = TREE_TYPE (decl);
2531 spec_types = TYPE_ARG_TYPES (old_type);
2532
2533 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2534 {
2535 /* Remove the this pointer, but remember the object's type for
2536 CV quals. */
2537 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2538 spec_types = TREE_CHAIN (spec_types);
2539 tmpl_types = TREE_CHAIN (tmpl_types);
2540
2541 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2542 {
2543 /* DECL may contain more parameters than TMPL due to the extra
2544 in-charge parameter in constructors and destructors. */
2545 in_charge = spec_types;
2546 spec_types = TREE_CHAIN (spec_types);
2547 }
2548 if (DECL_HAS_VTT_PARM_P (decl))
2549 {
2550 vtt = spec_types;
2551 spec_types = TREE_CHAIN (spec_types);
2552 }
2553 }
2554
2555 /* Compute the merged default arguments. */
2556 new_spec_types =
2557 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2558
2559 /* Compute the new FUNCTION_TYPE. */
2560 if (object_type)
2561 {
2562 if (vtt)
2563 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2564 TREE_VALUE (vtt),
2565 new_spec_types);
2566
2567 if (in_charge)
2568 /* Put the in-charge parameter back. */
2569 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2570 TREE_VALUE (in_charge),
2571 new_spec_types);
2572
2573 new_type = build_method_type_directly (object_type,
2574 TREE_TYPE (old_type),
2575 new_spec_types);
2576 }
2577 else
2578 new_type = build_function_type (TREE_TYPE (old_type),
2579 new_spec_types);
2580 new_type = cp_build_type_attribute_variant (new_type,
2581 TYPE_ATTRIBUTES (old_type));
2582 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2583
2584 TREE_TYPE (decl) = new_type;
2585 }
2586
2587 /* Return the number of template headers we expect to see for a definition
2588 or specialization of CTYPE or one of its non-template members. */
2589
2590 int
2591 num_template_headers_for_class (tree ctype)
2592 {
2593 int num_templates = 0;
2594
2595 while (ctype && CLASS_TYPE_P (ctype))
2596 {
2597 /* You're supposed to have one `template <...>' for every
2598 template class, but you don't need one for a full
2599 specialization. For example:
2600
2601 template <class T> struct S{};
2602 template <> struct S<int> { void f(); };
2603 void S<int>::f () {}
2604
2605 is correct; there shouldn't be a `template <>' for the
2606 definition of `S<int>::f'. */
2607 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2608 /* If CTYPE does not have template information of any
2609 kind, then it is not a template, nor is it nested
2610 within a template. */
2611 break;
2612 if (explicit_class_specialization_p (ctype))
2613 break;
2614 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2615 ++num_templates;
2616
2617 ctype = TYPE_CONTEXT (ctype);
2618 }
2619
2620 return num_templates;
2621 }
2622
2623 /* Do a simple sanity check on the template headers that precede the
2624 variable declaration DECL. */
2625
2626 void
2627 check_template_variable (tree decl)
2628 {
2629 tree ctx = CP_DECL_CONTEXT (decl);
2630 int wanted = num_template_headers_for_class (ctx);
2631 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2632 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2633 {
2634 if (cxx_dialect < cxx14)
2635 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2636 "variable templates only available with "
2637 "%<-std=c++14%> or %<-std=gnu++14%>");
2638
2639 // Namespace-scope variable templates should have a template header.
2640 ++wanted;
2641 }
2642 if (template_header_count > wanted)
2643 {
2644 auto_diagnostic_group d;
2645 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2646 "too many template headers for %qD "
2647 "(should be %d)",
2648 decl, wanted);
2649 if (warned && CLASS_TYPE_P (ctx)
2650 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2651 inform (DECL_SOURCE_LOCATION (decl),
2652 "members of an explicitly specialized class are defined "
2653 "without a template header");
2654 }
2655 }
2656
2657 /* An explicit specialization whose declarator-id or class-head-name is not
2658 qualified shall be declared in the nearest enclosing namespace of the
2659 template, or, if the namespace is inline (7.3.1), any namespace from its
2660 enclosing namespace set.
2661
2662 If the name declared in the explicit instantiation is an unqualified name,
2663 the explicit instantiation shall appear in the namespace where its template
2664 is declared or, if that namespace is inline (7.3.1), any namespace from its
2665 enclosing namespace set. */
2666
2667 void
2668 check_unqualified_spec_or_inst (tree t, location_t loc)
2669 {
2670 tree tmpl = most_general_template (t);
2671 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2672 && !is_nested_namespace (current_namespace,
2673 CP_DECL_CONTEXT (tmpl), true))
2674 {
2675 if (processing_specialization)
2676 permerror (loc, "explicit specialization of %qD outside its "
2677 "namespace must use a nested-name-specifier", tmpl);
2678 else if (processing_explicit_instantiation
2679 && cxx_dialect >= cxx11)
2680 /* This was allowed in C++98, so only pedwarn. */
2681 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2682 "outside its namespace must use a nested-name-"
2683 "specifier", tmpl);
2684 }
2685 }
2686
2687 /* Warn for a template specialization SPEC that is missing some of a set
2688 of function or type attributes that the template TEMPL is declared with.
2689 ATTRLIST is a list of additional attributes that SPEC should be taken
2690 to ultimately be declared with. */
2691
2692 static void
2693 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2694 {
2695 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2696 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2697
2698 /* Avoid warning if the difference between the primary and
2699 the specialization is not in one of the attributes below. */
2700 const char* const blacklist[] = {
2701 "alloc_align", "alloc_size", "assume_aligned", "format",
2702 "format_arg", "malloc", "nonnull", NULL
2703 };
2704
2705 /* Put together a list of the black listed attributes that the primary
2706 template is declared with that the specialization is not, in case
2707 it's not apparent from the most recent declaration of the primary. */
2708 pretty_printer str;
2709 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2710 blacklist, &str);
2711
2712 if (!nattrs)
2713 return;
2714
2715 auto_diagnostic_group d;
2716 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2717 "explicit specialization %q#D may be missing attributes",
2718 spec))
2719 inform (DECL_SOURCE_LOCATION (tmpl),
2720 nattrs > 1
2721 ? G_("missing primary template attributes %s")
2722 : G_("missing primary template attribute %s"),
2723 pp_formatted_text (&str));
2724 }
2725
2726 /* Check to see if the function just declared, as indicated in
2727 DECLARATOR, and in DECL, is a specialization of a function
2728 template. We may also discover that the declaration is an explicit
2729 instantiation at this point.
2730
2731 Returns DECL, or an equivalent declaration that should be used
2732 instead if all goes well. Issues an error message if something is
2733 amiss. Returns error_mark_node if the error is not easily
2734 recoverable.
2735
2736 FLAGS is a bitmask consisting of the following flags:
2737
2738 2: The function has a definition.
2739 4: The function is a friend.
2740
2741 The TEMPLATE_COUNT is the number of references to qualifying
2742 template classes that appeared in the name of the function. For
2743 example, in
2744
2745 template <class T> struct S { void f(); };
2746 void S<int>::f();
2747
2748 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2749 classes are not counted in the TEMPLATE_COUNT, so that in
2750
2751 template <class T> struct S {};
2752 template <> struct S<int> { void f(); }
2753 template <> void S<int>::f();
2754
2755 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2756 invalid; there should be no template <>.)
2757
2758 If the function is a specialization, it is marked as such via
2759 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2760 is set up correctly, and it is added to the list of specializations
2761 for that template. */
2762
2763 tree
2764 check_explicit_specialization (tree declarator,
2765 tree decl,
2766 int template_count,
2767 int flags,
2768 tree attrlist)
2769 {
2770 int have_def = flags & 2;
2771 int is_friend = flags & 4;
2772 bool is_concept = flags & 8;
2773 int specialization = 0;
2774 int explicit_instantiation = 0;
2775 int member_specialization = 0;
2776 tree ctype = DECL_CLASS_CONTEXT (decl);
2777 tree dname = DECL_NAME (decl);
2778 tmpl_spec_kind tsk;
2779
2780 if (is_friend)
2781 {
2782 if (!processing_specialization)
2783 tsk = tsk_none;
2784 else
2785 tsk = tsk_excessive_parms;
2786 }
2787 else
2788 tsk = current_tmpl_spec_kind (template_count);
2789
2790 switch (tsk)
2791 {
2792 case tsk_none:
2793 if (processing_specialization && !VAR_P (decl))
2794 {
2795 specialization = 1;
2796 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2797 }
2798 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2799 {
2800 if (is_friend)
2801 /* This could be something like:
2802
2803 template <class T> void f(T);
2804 class S { friend void f<>(int); } */
2805 specialization = 1;
2806 else
2807 {
2808 /* This case handles bogus declarations like template <>
2809 template <class T> void f<int>(); */
2810
2811 error_at (cp_expr_loc_or_input_loc (declarator),
2812 "template-id %qE in declaration of primary template",
2813 declarator);
2814 return decl;
2815 }
2816 }
2817 break;
2818
2819 case tsk_invalid_member_spec:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node;
2823
2824 case tsk_invalid_expl_inst:
2825 error ("template parameter list used in explicit instantiation");
2826
2827 /* Fall through. */
2828
2829 case tsk_expl_inst:
2830 if (have_def)
2831 error ("definition provided for explicit instantiation");
2832
2833 explicit_instantiation = 1;
2834 break;
2835
2836 case tsk_excessive_parms:
2837 case tsk_insufficient_parms:
2838 if (tsk == tsk_excessive_parms)
2839 error ("too many template parameter lists in declaration of %qD",
2840 decl);
2841 else if (template_header_count)
2842 error("too few template parameter lists in declaration of %qD", decl);
2843 else
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl);
2846
2847 /* Fall through. */
2848 case tsk_expl_spec:
2849 if (is_concept)
2850 error ("explicit specialization declared %<concept%>");
2851
2852 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2855 break;
2856
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 if (ctype)
2859 member_specialization = 1;
2860 else
2861 specialization = 1;
2862 break;
2863
2864 case tsk_template:
2865 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2866 {
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2869
2870 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2871 error_at (cp_expr_loc_or_input_loc (declarator),
2872 "template-id %qE in declaration of primary template",
2873 declarator);
2874 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2875 {
2876 /* Partial specialization of variable template. */
2877 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2878 specialization = 1;
2879 goto ok;
2880 }
2881 else if (cxx_dialect < cxx14)
2882 error_at (cp_expr_loc_or_input_loc (declarator),
2883 "non-type partial specialization %qE "
2884 "is not allowed", declarator);
2885 else
2886 error_at (cp_expr_loc_or_input_loc (declarator),
2887 "non-class, non-variable partial specialization %qE "
2888 "is not allowed", declarator);
2889 return decl;
2890 ok:;
2891 }
2892
2893 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2894 /* This is a specialization of a member template, without
2895 specialization the containing class. Something like:
2896
2897 template <class T> struct S {
2898 template <class U> void f (U);
2899 };
2900 template <> template <class U> void S<int>::f(U) {}
2901
2902 That's a specialization -- but of the entire template. */
2903 specialization = 1;
2904 break;
2905
2906 default:
2907 gcc_unreachable ();
2908 }
2909
2910 if ((specialization || member_specialization)
2911 /* This doesn't apply to variable templates. */
2912 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2913 {
2914 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2915 for (; t; t = TREE_CHAIN (t))
2916 if (TREE_PURPOSE (t))
2917 {
2918 permerror (input_location,
2919 "default argument specified in explicit specialization");
2920 break;
2921 }
2922 }
2923
2924 if (specialization || member_specialization || explicit_instantiation)
2925 {
2926 tree tmpl = NULL_TREE;
2927 tree targs = NULL_TREE;
2928 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2929
2930 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2931 if (!was_template_id)
2932 {
2933 tree fns;
2934
2935 gcc_assert (identifier_p (declarator));
2936 if (ctype)
2937 fns = dname;
2938 else
2939 {
2940 /* If there is no class context, the explicit instantiation
2941 must be at namespace scope. */
2942 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2943
2944 /* Find the namespace binding, using the declaration
2945 context. */
2946 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2947 false, true);
2948 if (fns == error_mark_node)
2949 /* If lookup fails, look for a friend declaration so we can
2950 give a better diagnostic. */
2951 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2952 /*type*/false, /*complain*/true,
2953 /*hidden*/true);
2954
2955 if (fns == error_mark_node || !is_overloaded_fn (fns))
2956 {
2957 error ("%qD is not a template function", dname);
2958 fns = error_mark_node;
2959 }
2960 }
2961
2962 declarator = lookup_template_function (fns, NULL_TREE);
2963 }
2964
2965 if (declarator == error_mark_node)
2966 return error_mark_node;
2967
2968 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2969 {
2970 if (!explicit_instantiation)
2971 /* A specialization in class scope. This is invalid,
2972 but the error will already have been flagged by
2973 check_specialization_scope. */
2974 return error_mark_node;
2975 else
2976 {
2977 /* It's not valid to write an explicit instantiation in
2978 class scope, e.g.:
2979
2980 class C { template void f(); }
2981
2982 This case is caught by the parser. However, on
2983 something like:
2984
2985 template class C { void f(); };
2986
2987 (which is invalid) we can get here. The error will be
2988 issued later. */
2989 ;
2990 }
2991
2992 return decl;
2993 }
2994 else if (ctype != NULL_TREE
2995 && (identifier_p (TREE_OPERAND (declarator, 0))))
2996 {
2997 // We'll match variable templates in start_decl.
2998 if (VAR_P (decl))
2999 return decl;
3000
3001 /* Find the list of functions in ctype that have the same
3002 name as the declared function. */
3003 tree name = TREE_OPERAND (declarator, 0);
3004
3005 if (constructor_name_p (name, ctype))
3006 {
3007 if (DECL_CONSTRUCTOR_P (decl)
3008 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3009 : !CLASSTYPE_DESTRUCTOR (ctype))
3010 {
3011 /* From [temp.expl.spec]:
3012
3013 If such an explicit specialization for the member
3014 of a class template names an implicitly-declared
3015 special member function (clause _special_), the
3016 program is ill-formed.
3017
3018 Similar language is found in [temp.explicit]. */
3019 error ("specialization of implicitly-declared special member function");
3020 return error_mark_node;
3021 }
3022
3023 name = DECL_NAME (decl);
3024 }
3025
3026 /* For a type-conversion operator, We might be looking for
3027 `operator int' which will be a specialization of
3028 `operator T'. Grab all the conversion operators, and
3029 then select from them. */
3030 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3031 ? conv_op_identifier : name);
3032
3033 if (fns == NULL_TREE)
3034 {
3035 error ("no member function %qD declared in %qT", name, ctype);
3036 return error_mark_node;
3037 }
3038 else
3039 TREE_OPERAND (declarator, 0) = fns;
3040 }
3041
3042 /* Figure out what exactly is being specialized at this point.
3043 Note that for an explicit instantiation, even one for a
3044 member function, we cannot tell a priori whether the
3045 instantiation is for a member template, or just a member
3046 function of a template class. Even if a member template is
3047 being instantiated, the member template arguments may be
3048 elided if they can be deduced from the rest of the
3049 declaration. */
3050 tmpl = determine_specialization (declarator, decl,
3051 &targs,
3052 member_specialization,
3053 template_count,
3054 tsk);
3055
3056 if (!tmpl || tmpl == error_mark_node)
3057 /* We couldn't figure out what this declaration was
3058 specializing. */
3059 return error_mark_node;
3060 else
3061 {
3062 if (TREE_CODE (decl) == FUNCTION_DECL
3063 && DECL_HIDDEN_FRIEND_P (tmpl))
3064 {
3065 auto_diagnostic_group d;
3066 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3067 "friend declaration %qD is not visible to "
3068 "explicit specialization", tmpl))
3069 inform (DECL_SOURCE_LOCATION (tmpl),
3070 "friend declaration here");
3071 }
3072 else if (!ctype && !is_friend
3073 && CP_DECL_CONTEXT (decl) == current_namespace)
3074 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3075
3076 tree gen_tmpl = most_general_template (tmpl);
3077
3078 if (explicit_instantiation)
3079 {
3080 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3081 is done by do_decl_instantiation later. */
3082
3083 int arg_depth = TMPL_ARGS_DEPTH (targs);
3084 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3085
3086 if (arg_depth > parm_depth)
3087 {
3088 /* If TMPL is not the most general template (for
3089 example, if TMPL is a friend template that is
3090 injected into namespace scope), then there will
3091 be too many levels of TARGS. Remove some of them
3092 here. */
3093 int i;
3094 tree new_targs;
3095
3096 new_targs = make_tree_vec (parm_depth);
3097 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3098 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3099 = TREE_VEC_ELT (targs, i);
3100 targs = new_targs;
3101 }
3102
3103 return instantiate_template (tmpl, targs, tf_error);
3104 }
3105
3106 /* If we thought that the DECL was a member function, but it
3107 turns out to be specializing a static member function,
3108 make DECL a static member function as well. */
3109 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3110 && DECL_STATIC_FUNCTION_P (tmpl)
3111 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3112 revert_static_member_fn (decl);
3113
3114 /* If this is a specialization of a member template of a
3115 template class, we want to return the TEMPLATE_DECL, not
3116 the specialization of it. */
3117 if (tsk == tsk_template && !was_template_id)
3118 {
3119 tree result = DECL_TEMPLATE_RESULT (tmpl);
3120 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3121 DECL_INITIAL (result) = NULL_TREE;
3122 if (have_def)
3123 {
3124 tree parm;
3125 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3126 DECL_SOURCE_LOCATION (result)
3127 = DECL_SOURCE_LOCATION (decl);
3128 /* We want to use the argument list specified in the
3129 definition, not in the original declaration. */
3130 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3131 for (parm = DECL_ARGUMENTS (result); parm;
3132 parm = DECL_CHAIN (parm))
3133 DECL_CONTEXT (parm) = result;
3134 }
3135 return register_specialization (tmpl, gen_tmpl, targs,
3136 is_friend, 0);
3137 }
3138
3139 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3140 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3141
3142 if (was_template_id)
3143 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3144
3145 /* Inherit default function arguments from the template
3146 DECL is specializing. */
3147 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3148 copy_default_args_to_explicit_spec (decl);
3149
3150 /* This specialization has the same protection as the
3151 template it specializes. */
3152 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3153 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3154
3155 /* 7.1.1-1 [dcl.stc]
3156
3157 A storage-class-specifier shall not be specified in an
3158 explicit specialization...
3159
3160 The parser rejects these, so unless action is taken here,
3161 explicit function specializations will always appear with
3162 global linkage.
3163
3164 The action recommended by the C++ CWG in response to C++
3165 defect report 605 is to make the storage class and linkage
3166 of the explicit specialization match the templated function:
3167
3168 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3169 */
3170 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3171 {
3172 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3173 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3174
3175 /* A concept cannot be specialized. */
3176 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3177 {
3178 error ("explicit specialization of function concept %qD",
3179 gen_tmpl);
3180 return error_mark_node;
3181 }
3182
3183 /* This specialization has the same linkage and visibility as
3184 the function template it specializes. */
3185 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3186 if (! TREE_PUBLIC (decl))
3187 {
3188 DECL_INTERFACE_KNOWN (decl) = 1;
3189 DECL_NOT_REALLY_EXTERN (decl) = 1;
3190 }
3191 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3192 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3193 {
3194 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3195 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3196 }
3197 }
3198
3199 /* If DECL is a friend declaration, declared using an
3200 unqualified name, the namespace associated with DECL may
3201 have been set incorrectly. For example, in:
3202
3203 template <typename T> void f(T);
3204 namespace N {
3205 struct S { friend void f<int>(int); }
3206 }
3207
3208 we will have set the DECL_CONTEXT for the friend
3209 declaration to N, rather than to the global namespace. */
3210 if (DECL_NAMESPACE_SCOPE_P (decl))
3211 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3212
3213 if (is_friend && !have_def)
3214 /* This is not really a declaration of a specialization.
3215 It's just the name of an instantiation. But, it's not
3216 a request for an instantiation, either. */
3217 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3218 else if (TREE_CODE (decl) == FUNCTION_DECL)
3219 /* A specialization is not necessarily COMDAT. */
3220 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3221 && DECL_DECLARED_INLINE_P (decl));
3222 else if (VAR_P (decl))
3223 DECL_COMDAT (decl) = false;
3224
3225 /* If this is a full specialization, register it so that we can find
3226 it again. Partial specializations will be registered in
3227 process_partial_specialization. */
3228 if (!processing_template_decl)
3229 {
3230 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3231
3232 decl = register_specialization (decl, gen_tmpl, targs,
3233 is_friend, 0);
3234 }
3235
3236
3237 /* A 'structor should already have clones. */
3238 gcc_assert (decl == error_mark_node
3239 || variable_template_p (tmpl)
3240 || !(DECL_CONSTRUCTOR_P (decl)
3241 || DECL_DESTRUCTOR_P (decl))
3242 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3243 }
3244 }
3245
3246 return decl;
3247 }
3248
3249 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3250 parameters. These are represented in the same format used for
3251 DECL_TEMPLATE_PARMS. */
3252
3253 int
3254 comp_template_parms (const_tree parms1, const_tree parms2)
3255 {
3256 const_tree p1;
3257 const_tree p2;
3258
3259 if (parms1 == parms2)
3260 return 1;
3261
3262 for (p1 = parms1, p2 = parms2;
3263 p1 != NULL_TREE && p2 != NULL_TREE;
3264 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3265 {
3266 tree t1 = TREE_VALUE (p1);
3267 tree t2 = TREE_VALUE (p2);
3268 int i;
3269
3270 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3271 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3272
3273 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3274 return 0;
3275
3276 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3277 {
3278 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3279 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3280
3281 /* If either of the template parameters are invalid, assume
3282 they match for the sake of error recovery. */
3283 if (error_operand_p (parm1) || error_operand_p (parm2))
3284 return 1;
3285
3286 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3287 return 0;
3288
3289 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3290 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3291 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3292 continue;
3293 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3294 return 0;
3295 }
3296 }
3297
3298 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3299 /* One set of parameters has more parameters lists than the
3300 other. */
3301 return 0;
3302
3303 return 1;
3304 }
3305
3306 /* Returns true if two template parameters are declared with
3307 equivalent constraints. */
3308
3309 static bool
3310 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3311 {
3312 tree req1 = TREE_TYPE (parm1);
3313 tree req2 = TREE_TYPE (parm2);
3314 if (!req1 != !req2)
3315 return false;
3316 if (req1)
3317 return cp_tree_equal (req1, req2);
3318 return true;
3319 }
3320
3321 /* Returns true when two template parameters are equivalent. */
3322
3323 static bool
3324 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3325 {
3326 tree decl1 = TREE_VALUE (parm1);
3327 tree decl2 = TREE_VALUE (parm2);
3328
3329 /* If either of the template parameters are invalid, assume
3330 they match for the sake of error recovery. */
3331 if (error_operand_p (decl1) || error_operand_p (decl2))
3332 return true;
3333
3334 /* ... they declare parameters of the same kind. */
3335 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3336 return false;
3337
3338 /* ... one parameter was introduced by a parameter declaration, then
3339 both are. This case arises as a result of eagerly rewriting declarations
3340 during parsing. */
3341 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3342 return false;
3343
3344 /* ... if either declares a pack, they both do. */
3345 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3346 return false;
3347
3348 if (TREE_CODE (decl1) == PARM_DECL)
3349 {
3350 /* ... if they declare non-type parameters, the types are equivalent. */
3351 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3352 return false;
3353 }
3354 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3355 {
3356 /* ... if they declare template template parameters, their template
3357 parameter lists are equivalent. */
3358 if (!template_heads_equivalent_p (decl1, decl2))
3359 return false;
3360 }
3361
3362 /* ... if they are declared with a qualified-concept name, they both
3363 are, and those names are equivalent. */
3364 return template_parameter_constraints_equivalent_p (parm1, parm2);
3365 }
3366
3367 /* Returns true if two template parameters lists are equivalent.
3368 Two template parameter lists are equivalent if they have the
3369 same length and their corresponding parameters are equivalent.
3370
3371 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3372 data structure returned by DECL_TEMPLATE_PARMS.
3373
3374 This is generally the same implementation as comp_template_parms
3375 except that it also the concept names and arguments used to
3376 introduce parameters. */
3377
3378 static bool
3379 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3380 {
3381 if (parms1 == parms2)
3382 return true;
3383
3384 const_tree p1 = parms1;
3385 const_tree p2 = parms2;
3386 while (p1 != NULL_TREE && p2 != NULL_TREE)
3387 {
3388 tree list1 = TREE_VALUE (p1);
3389 tree list2 = TREE_VALUE (p2);
3390
3391 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3392 return 0;
3393
3394 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3395 {
3396 tree parm1 = TREE_VEC_ELT (list1, i);
3397 tree parm2 = TREE_VEC_ELT (list2, i);
3398 if (!template_parameters_equivalent_p (parm1, parm2))
3399 return false;
3400 }
3401
3402 p1 = TREE_CHAIN (p1);
3403 p2 = TREE_CHAIN (p2);
3404 }
3405
3406 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3407 return false;
3408
3409 return true;
3410 }
3411
3412 /* Return true if the requires-clause of the template parameter lists are
3413 equivalent and false otherwise. */
3414 static bool
3415 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3416 {
3417 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3418 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3419 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3420 return false;
3421 if (!cp_tree_equal (req1, req2))
3422 return false;
3423 return true;
3424 }
3425
3426 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3427 Two template heads are equivalent if their template parameter
3428 lists are equivalent and their requires clauses are equivalent.
3429
3430 In pre-C++20, this is equivalent to calling comp_template_parms
3431 for the template parameters of TMPL1 and TMPL2. */
3432
3433 bool
3434 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3435 {
3436 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3437 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3438
3439 /* Don't change the matching rules for pre-C++20. */
3440 if (cxx_dialect < cxx2a)
3441 return comp_template_parms (parms1, parms2);
3442
3443 /* ... have the same number of template parameters, and their
3444 corresponding parameters are equivalent. */
3445 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3446 return false;
3447
3448 /* ... if either has a requires-clause, they both do and their
3449 corresponding constraint-expressions are equivalent. */
3450 return template_requirements_equivalent_p (parms1, parms2);
3451 }
3452
3453 /* Determine whether PARM is a parameter pack. */
3454
3455 bool
3456 template_parameter_pack_p (const_tree parm)
3457 {
3458 /* Determine if we have a non-type template parameter pack. */
3459 if (TREE_CODE (parm) == PARM_DECL)
3460 return (DECL_TEMPLATE_PARM_P (parm)
3461 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3462 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3463 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3464
3465 /* If this is a list of template parameters, we could get a
3466 TYPE_DECL or a TEMPLATE_DECL. */
3467 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3468 parm = TREE_TYPE (parm);
3469
3470 /* Otherwise it must be a type template parameter. */
3471 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3472 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3473 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3474 }
3475
3476 /* Determine if T is a function parameter pack. */
3477
3478 bool
3479 function_parameter_pack_p (const_tree t)
3480 {
3481 if (t && TREE_CODE (t) == PARM_DECL)
3482 return DECL_PACK_P (t);
3483 return false;
3484 }
3485
3486 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3487 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3488
3489 tree
3490 get_function_template_decl (const_tree primary_func_tmpl_inst)
3491 {
3492 if (! primary_func_tmpl_inst
3493 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3494 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3495 return NULL;
3496
3497 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3498 }
3499
3500 /* Return true iff the function parameter PARAM_DECL was expanded
3501 from the function parameter pack PACK. */
3502
3503 bool
3504 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3505 {
3506 if (DECL_ARTIFICIAL (param_decl)
3507 || !function_parameter_pack_p (pack))
3508 return false;
3509
3510 /* The parameter pack and its pack arguments have the same
3511 DECL_PARM_INDEX. */
3512 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3513 }
3514
3515 /* Determine whether ARGS describes a variadic template args list,
3516 i.e., one that is terminated by a template argument pack. */
3517
3518 static bool
3519 template_args_variadic_p (tree args)
3520 {
3521 int nargs;
3522 tree last_parm;
3523
3524 if (args == NULL_TREE)
3525 return false;
3526
3527 args = INNERMOST_TEMPLATE_ARGS (args);
3528 nargs = TREE_VEC_LENGTH (args);
3529
3530 if (nargs == 0)
3531 return false;
3532
3533 last_parm = TREE_VEC_ELT (args, nargs - 1);
3534
3535 return ARGUMENT_PACK_P (last_parm);
3536 }
3537
3538 /* Generate a new name for the parameter pack name NAME (an
3539 IDENTIFIER_NODE) that incorporates its */
3540
3541 static tree
3542 make_ith_pack_parameter_name (tree name, int i)
3543 {
3544 /* Munge the name to include the parameter index. */
3545 #define NUMBUF_LEN 128
3546 char numbuf[NUMBUF_LEN];
3547 char* newname;
3548 int newname_len;
3549
3550 if (name == NULL_TREE)
3551 return name;
3552 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3553 newname_len = IDENTIFIER_LENGTH (name)
3554 + strlen (numbuf) + 2;
3555 newname = (char*)alloca (newname_len);
3556 snprintf (newname, newname_len,
3557 "%s#%i", IDENTIFIER_POINTER (name), i);
3558 return get_identifier (newname);
3559 }
3560
3561 /* Return true if T is a primary function, class or alias template
3562 specialization, not including the template pattern. */
3563
3564 bool
3565 primary_template_specialization_p (const_tree t)
3566 {
3567 if (!t)
3568 return false;
3569
3570 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3571 return (DECL_LANG_SPECIFIC (t)
3572 && DECL_USE_TEMPLATE (t)
3573 && DECL_TEMPLATE_INFO (t)
3574 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3575 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3576 return (CLASSTYPE_TEMPLATE_INFO (t)
3577 && CLASSTYPE_USE_TEMPLATE (t)
3578 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3579 else if (alias_template_specialization_p (t, nt_transparent))
3580 return true;
3581 return false;
3582 }
3583
3584 /* Return true if PARM is a template template parameter. */
3585
3586 bool
3587 template_template_parameter_p (const_tree parm)
3588 {
3589 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3590 }
3591
3592 /* Return true iff PARM is a DECL representing a type template
3593 parameter. */
3594
3595 bool
3596 template_type_parameter_p (const_tree parm)
3597 {
3598 return (parm
3599 && (TREE_CODE (parm) == TYPE_DECL
3600 || TREE_CODE (parm) == TEMPLATE_DECL)
3601 && DECL_TEMPLATE_PARM_P (parm));
3602 }
3603
3604 /* Return the template parameters of T if T is a
3605 primary template instantiation, NULL otherwise. */
3606
3607 tree
3608 get_primary_template_innermost_parameters (const_tree t)
3609 {
3610 tree parms = NULL, template_info = NULL;
3611
3612 if ((template_info = get_template_info (t))
3613 && primary_template_specialization_p (t))
3614 parms = INNERMOST_TEMPLATE_PARMS
3615 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3616
3617 return parms;
3618 }
3619
3620 /* Return the template parameters of the LEVELth level from the full list
3621 of template parameters PARMS. */
3622
3623 tree
3624 get_template_parms_at_level (tree parms, int level)
3625 {
3626 tree p;
3627 if (!parms
3628 || TREE_CODE (parms) != TREE_LIST
3629 || level > TMPL_PARMS_DEPTH (parms))
3630 return NULL_TREE;
3631
3632 for (p = parms; p; p = TREE_CHAIN (p))
3633 if (TMPL_PARMS_DEPTH (p) == level)
3634 return p;
3635
3636 return NULL_TREE;
3637 }
3638
3639 /* Returns the template arguments of T if T is a template instantiation,
3640 NULL otherwise. */
3641
3642 tree
3643 get_template_innermost_arguments (const_tree t)
3644 {
3645 tree args = NULL, template_info = NULL;
3646
3647 if ((template_info = get_template_info (t))
3648 && TI_ARGS (template_info))
3649 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3650
3651 return args;
3652 }
3653
3654 /* Return the argument pack elements of T if T is a template argument pack,
3655 NULL otherwise. */
3656
3657 tree
3658 get_template_argument_pack_elems (const_tree t)
3659 {
3660 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3661 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3662 return NULL;
3663
3664 return ARGUMENT_PACK_ARGS (t);
3665 }
3666
3667 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3668 ARGUMENT_PACK_SELECT represents. */
3669
3670 static tree
3671 argument_pack_select_arg (tree t)
3672 {
3673 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3674 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3675
3676 /* If the selected argument is an expansion E, that most likely means we were
3677 called from gen_elem_of_pack_expansion_instantiation during the
3678 substituting of an argument pack (of which the Ith element is a pack
3679 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3680 In this case, the Ith element resulting from this substituting is going to
3681 be a pack expansion, which pattern is the pattern of E. Let's return the
3682 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3683 resulting pack expansion from it. */
3684 if (PACK_EXPANSION_P (arg))
3685 {
3686 /* Make sure we aren't throwing away arg info. */
3687 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3688 arg = PACK_EXPANSION_PATTERN (arg);
3689 }
3690
3691 return arg;
3692 }
3693
3694
3695 /* True iff FN is a function representing a built-in variadic parameter
3696 pack. */
3697
3698 bool
3699 builtin_pack_fn_p (tree fn)
3700 {
3701 if (!fn
3702 || TREE_CODE (fn) != FUNCTION_DECL
3703 || !DECL_IS_BUILTIN (fn))
3704 return false;
3705
3706 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3707 return true;
3708
3709 return false;
3710 }
3711
3712 /* True iff CALL is a call to a function representing a built-in variadic
3713 parameter pack. */
3714
3715 static bool
3716 builtin_pack_call_p (tree call)
3717 {
3718 if (TREE_CODE (call) != CALL_EXPR)
3719 return false;
3720 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3721 }
3722
3723 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3724
3725 static tree
3726 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3727 tree in_decl)
3728 {
3729 tree ohi = CALL_EXPR_ARG (call, 0);
3730 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3731 false/*fn*/, true/*int_cst*/);
3732
3733 if (value_dependent_expression_p (hi))
3734 {
3735 if (hi != ohi)
3736 {
3737 call = copy_node (call);
3738 CALL_EXPR_ARG (call, 0) = hi;
3739 }
3740 tree ex = make_pack_expansion (call, complain);
3741 tree vec = make_tree_vec (1);
3742 TREE_VEC_ELT (vec, 0) = ex;
3743 return vec;
3744 }
3745 else
3746 {
3747 hi = cxx_constant_value (hi);
3748 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3749
3750 /* Calculate the largest value of len that won't make the size of the vec
3751 overflow an int. The compiler will exceed resource limits long before
3752 this, but it seems a decent place to diagnose. */
3753 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3754
3755 if (len < 0 || len > max)
3756 {
3757 if ((complain & tf_error)
3758 && hi != error_mark_node)
3759 error ("argument to %<__integer_pack%> must be between 0 and %d",
3760 max);
3761 return error_mark_node;
3762 }
3763
3764 tree vec = make_tree_vec (len);
3765
3766 for (int i = 0; i < len; ++i)
3767 TREE_VEC_ELT (vec, i) = size_int (i);
3768
3769 return vec;
3770 }
3771 }
3772
3773 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3774 CALL. */
3775
3776 static tree
3777 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3778 tree in_decl)
3779 {
3780 if (!builtin_pack_call_p (call))
3781 return NULL_TREE;
3782
3783 tree fn = CALL_EXPR_FN (call);
3784
3785 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3786 return expand_integer_pack (call, args, complain, in_decl);
3787
3788 return NULL_TREE;
3789 }
3790
3791 /* Structure used to track the progress of find_parameter_packs_r. */
3792 struct find_parameter_pack_data
3793 {
3794 /* TREE_LIST that will contain all of the parameter packs found by
3795 the traversal. */
3796 tree* parameter_packs;
3797
3798 /* Set of AST nodes that have been visited by the traversal. */
3799 hash_set<tree> *visited;
3800
3801 /* True iff we're making a type pack expansion. */
3802 bool type_pack_expansion_p;
3803 };
3804
3805 /* Identifies all of the argument packs that occur in a template
3806 argument and appends them to the TREE_LIST inside DATA, which is a
3807 find_parameter_pack_data structure. This is a subroutine of
3808 make_pack_expansion and uses_parameter_packs. */
3809 static tree
3810 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3811 {
3812 tree t = *tp;
3813 struct find_parameter_pack_data* ppd =
3814 (struct find_parameter_pack_data*)data;
3815 bool parameter_pack_p = false;
3816
3817 /* Handle type aliases/typedefs. */
3818 if (TYPE_ALIAS_P (t))
3819 {
3820 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3821 cp_walk_tree (&TI_ARGS (tinfo),
3822 &find_parameter_packs_r,
3823 ppd, ppd->visited);
3824 *walk_subtrees = 0;
3825 return NULL_TREE;
3826 }
3827
3828 /* Identify whether this is a parameter pack or not. */
3829 switch (TREE_CODE (t))
3830 {
3831 case TEMPLATE_PARM_INDEX:
3832 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3833 parameter_pack_p = true;
3834 break;
3835
3836 case TEMPLATE_TYPE_PARM:
3837 t = TYPE_MAIN_VARIANT (t);
3838 /* FALLTHRU */
3839 case TEMPLATE_TEMPLATE_PARM:
3840 /* If the placeholder appears in the decl-specifier-seq of a function
3841 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3842 is a pack expansion, the invented template parameter is a template
3843 parameter pack. */
3844 if (ppd->type_pack_expansion_p && is_auto (t))
3845 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3846 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3847 parameter_pack_p = true;
3848 break;
3849
3850 case FIELD_DECL:
3851 case PARM_DECL:
3852 if (DECL_PACK_P (t))
3853 {
3854 /* We don't want to walk into the type of a PARM_DECL,
3855 because we don't want to see the type parameter pack. */
3856 *walk_subtrees = 0;
3857 parameter_pack_p = true;
3858 }
3859 break;
3860
3861 case VAR_DECL:
3862 if (DECL_PACK_P (t))
3863 {
3864 /* We don't want to walk into the type of a variadic capture proxy,
3865 because we don't want to see the type parameter pack. */
3866 *walk_subtrees = 0;
3867 parameter_pack_p = true;
3868 }
3869 else if (variable_template_specialization_p (t))
3870 {
3871 cp_walk_tree (&DECL_TI_ARGS (t),
3872 find_parameter_packs_r,
3873 ppd, ppd->visited);
3874 *walk_subtrees = 0;
3875 }
3876 break;
3877
3878 case CALL_EXPR:
3879 if (builtin_pack_call_p (t))
3880 parameter_pack_p = true;
3881 break;
3882
3883 case BASES:
3884 parameter_pack_p = true;
3885 break;
3886 default:
3887 /* Not a parameter pack. */
3888 break;
3889 }
3890
3891 if (parameter_pack_p)
3892 {
3893 /* Add this parameter pack to the list. */
3894 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3895 }
3896
3897 if (TYPE_P (t))
3898 cp_walk_tree (&TYPE_CONTEXT (t),
3899 &find_parameter_packs_r, ppd, ppd->visited);
3900
3901 /* This switch statement will return immediately if we don't find a
3902 parameter pack. */
3903 switch (TREE_CODE (t))
3904 {
3905 case TEMPLATE_PARM_INDEX:
3906 return NULL_TREE;
3907
3908 case BOUND_TEMPLATE_TEMPLATE_PARM:
3909 /* Check the template itself. */
3910 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3911 &find_parameter_packs_r, ppd, ppd->visited);
3912 /* Check the template arguments. */
3913 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3914 ppd->visited);
3915 *walk_subtrees = 0;
3916 return NULL_TREE;
3917
3918 case TEMPLATE_TYPE_PARM:
3919 case TEMPLATE_TEMPLATE_PARM:
3920 return NULL_TREE;
3921
3922 case PARM_DECL:
3923 return NULL_TREE;
3924
3925 case DECL_EXPR:
3926 /* Ignore the declaration of a capture proxy for a parameter pack. */
3927 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3928 *walk_subtrees = 0;
3929 return NULL_TREE;
3930
3931 case RECORD_TYPE:
3932 if (TYPE_PTRMEMFUNC_P (t))
3933 return NULL_TREE;
3934 /* Fall through. */
3935
3936 case UNION_TYPE:
3937 case ENUMERAL_TYPE:
3938 if (TYPE_TEMPLATE_INFO (t))
3939 cp_walk_tree (&TYPE_TI_ARGS (t),
3940 &find_parameter_packs_r, ppd, ppd->visited);
3941
3942 *walk_subtrees = 0;
3943 return NULL_TREE;
3944
3945 case TEMPLATE_DECL:
3946 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3947 return NULL_TREE;
3948 gcc_fallthrough();
3949
3950 case CONSTRUCTOR:
3951 cp_walk_tree (&TREE_TYPE (t),
3952 &find_parameter_packs_r, ppd, ppd->visited);
3953 return NULL_TREE;
3954
3955 case TYPENAME_TYPE:
3956 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3957 ppd, ppd->visited);
3958 *walk_subtrees = 0;
3959 return NULL_TREE;
3960
3961 case TYPE_PACK_EXPANSION:
3962 case EXPR_PACK_EXPANSION:
3963 *walk_subtrees = 0;
3964 return NULL_TREE;
3965
3966 case INTEGER_TYPE:
3967 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3968 ppd, ppd->visited);
3969 *walk_subtrees = 0;
3970 return NULL_TREE;
3971
3972 case IDENTIFIER_NODE:
3973 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3974 ppd->visited);
3975 *walk_subtrees = 0;
3976 return NULL_TREE;
3977
3978 case LAMBDA_EXPR:
3979 {
3980 /* Look at explicit captures. */
3981 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3982 cap; cap = TREE_CHAIN (cap))
3983 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3984 ppd->visited);
3985 /* Since we defer implicit capture, look in the parms and body. */
3986 tree fn = lambda_function (t);
3987 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3988 ppd->visited);
3989 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3990 ppd->visited);
3991 *walk_subtrees = 0;
3992 return NULL_TREE;
3993 }
3994
3995 case DECLTYPE_TYPE:
3996 {
3997 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3998 type_pack_expansion_p to false so that any placeholders
3999 within the expression don't get marked as parameter packs. */
4000 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4001 ppd->type_pack_expansion_p = false;
4002 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4003 ppd, ppd->visited);
4004 ppd->type_pack_expansion_p = type_pack_expansion_p;
4005 *walk_subtrees = 0;
4006 return NULL_TREE;
4007 }
4008
4009 case IF_STMT:
4010 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4011 ppd, ppd->visited);
4012 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4013 ppd, ppd->visited);
4014 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4015 ppd, ppd->visited);
4016 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4017 *walk_subtrees = 0;
4018 return NULL_TREE;
4019
4020 default:
4021 return NULL_TREE;
4022 }
4023
4024 return NULL_TREE;
4025 }
4026
4027 /* Determines if the expression or type T uses any parameter packs. */
4028 tree
4029 uses_parameter_packs (tree t)
4030 {
4031 tree parameter_packs = NULL_TREE;
4032 struct find_parameter_pack_data ppd;
4033 ppd.parameter_packs = &parameter_packs;
4034 ppd.visited = new hash_set<tree>;
4035 ppd.type_pack_expansion_p = false;
4036 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4037 delete ppd.visited;
4038 return parameter_packs;
4039 }
4040
4041 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4042 representation a base-class initializer into a parameter pack
4043 expansion. If all goes well, the resulting node will be an
4044 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4045 respectively. */
4046 tree
4047 make_pack_expansion (tree arg, tsubst_flags_t complain)
4048 {
4049 tree result;
4050 tree parameter_packs = NULL_TREE;
4051 bool for_types = false;
4052 struct find_parameter_pack_data ppd;
4053
4054 if (!arg || arg == error_mark_node)
4055 return arg;
4056
4057 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4058 {
4059 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4060 class initializer. In this case, the TREE_PURPOSE will be a
4061 _TYPE node (representing the base class expansion we're
4062 initializing) and the TREE_VALUE will be a TREE_LIST
4063 containing the initialization arguments.
4064
4065 The resulting expansion looks somewhat different from most
4066 expansions. Rather than returning just one _EXPANSION, we
4067 return a TREE_LIST whose TREE_PURPOSE is a
4068 TYPE_PACK_EXPANSION containing the bases that will be
4069 initialized. The TREE_VALUE will be identical to the
4070 original TREE_VALUE, which is a list of arguments that will
4071 be passed to each base. We do not introduce any new pack
4072 expansion nodes into the TREE_VALUE (although it is possible
4073 that some already exist), because the TREE_PURPOSE and
4074 TREE_VALUE all need to be expanded together with the same
4075 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4076 resulting TREE_PURPOSE will mention the parameter packs in
4077 both the bases and the arguments to the bases. */
4078 tree purpose;
4079 tree value;
4080 tree parameter_packs = NULL_TREE;
4081
4082 /* Determine which parameter packs will be used by the base
4083 class expansion. */
4084 ppd.visited = new hash_set<tree>;
4085 ppd.parameter_packs = &parameter_packs;
4086 ppd.type_pack_expansion_p = false;
4087 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4088 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4089 &ppd, ppd.visited);
4090
4091 if (parameter_packs == NULL_TREE)
4092 {
4093 if (complain & tf_error)
4094 error ("base initializer expansion %qT contains no parameter packs",
4095 arg);
4096 delete ppd.visited;
4097 return error_mark_node;
4098 }
4099
4100 if (TREE_VALUE (arg) != void_type_node)
4101 {
4102 /* Collect the sets of parameter packs used in each of the
4103 initialization arguments. */
4104 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4105 {
4106 /* Determine which parameter packs will be expanded in this
4107 argument. */
4108 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4109 &ppd, ppd.visited);
4110 }
4111 }
4112
4113 delete ppd.visited;
4114
4115 /* Create the pack expansion type for the base type. */
4116 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4117 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4118 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4119 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4120
4121 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4122 they will rarely be compared to anything. */
4123 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4124
4125 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4126 }
4127
4128 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4129 for_types = true;
4130
4131 /* Build the PACK_EXPANSION_* node. */
4132 result = for_types
4133 ? cxx_make_type (TYPE_PACK_EXPANSION)
4134 : make_node (EXPR_PACK_EXPANSION);
4135 SET_PACK_EXPANSION_PATTERN (result, arg);
4136 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4137 {
4138 /* Propagate type and const-expression information. */
4139 TREE_TYPE (result) = TREE_TYPE (arg);
4140 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4141 /* Mark this read now, since the expansion might be length 0. */
4142 mark_exp_read (arg);
4143 }
4144 else
4145 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4146 they will rarely be compared to anything. */
4147 SET_TYPE_STRUCTURAL_EQUALITY (result);
4148
4149 /* Determine which parameter packs will be expanded. */
4150 ppd.parameter_packs = &parameter_packs;
4151 ppd.visited = new hash_set<tree>;
4152 ppd.type_pack_expansion_p = TYPE_P (arg);
4153 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4154 delete ppd.visited;
4155
4156 /* Make sure we found some parameter packs. */
4157 if (parameter_packs == NULL_TREE)
4158 {
4159 if (complain & tf_error)
4160 {
4161 if (TYPE_P (arg))
4162 error ("expansion pattern %qT contains no parameter packs", arg);
4163 else
4164 error ("expansion pattern %qE contains no parameter packs", arg);
4165 }
4166 return error_mark_node;
4167 }
4168 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4169
4170 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4171
4172 return result;
4173 }
4174
4175 /* Checks T for any "bare" parameter packs, which have not yet been
4176 expanded, and issues an error if any are found. This operation can
4177 only be done on full expressions or types (e.g., an expression
4178 statement, "if" condition, etc.), because we could have expressions like:
4179
4180 foo(f(g(h(args)))...)
4181
4182 where "args" is a parameter pack. check_for_bare_parameter_packs
4183 should not be called for the subexpressions args, h(args),
4184 g(h(args)), or f(g(h(args))), because we would produce erroneous
4185 error messages.
4186
4187 Returns TRUE and emits an error if there were bare parameter packs,
4188 returns FALSE otherwise. */
4189 bool
4190 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4191 {
4192 tree parameter_packs = NULL_TREE;
4193 struct find_parameter_pack_data ppd;
4194
4195 if (!processing_template_decl || !t || t == error_mark_node)
4196 return false;
4197
4198 /* A lambda might use a parameter pack from the containing context. */
4199 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4200 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4201 return false;
4202
4203 if (TREE_CODE (t) == TYPE_DECL)
4204 t = TREE_TYPE (t);
4205
4206 ppd.parameter_packs = &parameter_packs;
4207 ppd.visited = new hash_set<tree>;
4208 ppd.type_pack_expansion_p = false;
4209 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4210 delete ppd.visited;
4211
4212 if (parameter_packs)
4213 {
4214 if (loc == UNKNOWN_LOCATION)
4215 loc = cp_expr_loc_or_input_loc (t);
4216 error_at (loc, "parameter packs not expanded with %<...%>:");
4217 while (parameter_packs)
4218 {
4219 tree pack = TREE_VALUE (parameter_packs);
4220 tree name = NULL_TREE;
4221
4222 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4223 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4224 name = TYPE_NAME (pack);
4225 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4226 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4227 else if (TREE_CODE (pack) == CALL_EXPR)
4228 name = DECL_NAME (CALL_EXPR_FN (pack));
4229 else
4230 name = DECL_NAME (pack);
4231
4232 if (name)
4233 inform (loc, " %qD", name);
4234 else
4235 inform (loc, " %s", "<anonymous>");
4236
4237 parameter_packs = TREE_CHAIN (parameter_packs);
4238 }
4239
4240 return true;
4241 }
4242
4243 return false;
4244 }
4245
4246 /* Expand any parameter packs that occur in the template arguments in
4247 ARGS. */
4248 tree
4249 expand_template_argument_pack (tree args)
4250 {
4251 if (args == error_mark_node)
4252 return error_mark_node;
4253
4254 tree result_args = NULL_TREE;
4255 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4256 int num_result_args = -1;
4257 int non_default_args_count = -1;
4258
4259 /* First, determine if we need to expand anything, and the number of
4260 slots we'll need. */
4261 for (in_arg = 0; in_arg < nargs; ++in_arg)
4262 {
4263 tree arg = TREE_VEC_ELT (args, in_arg);
4264 if (arg == NULL_TREE)
4265 return args;
4266 if (ARGUMENT_PACK_P (arg))
4267 {
4268 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4269 if (num_result_args < 0)
4270 num_result_args = in_arg + num_packed;
4271 else
4272 num_result_args += num_packed;
4273 }
4274 else
4275 {
4276 if (num_result_args >= 0)
4277 num_result_args++;
4278 }
4279 }
4280
4281 /* If no expansion is necessary, we're done. */
4282 if (num_result_args < 0)
4283 return args;
4284
4285 /* Expand arguments. */
4286 result_args = make_tree_vec (num_result_args);
4287 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4288 non_default_args_count =
4289 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4290 for (in_arg = 0; in_arg < nargs; ++in_arg)
4291 {
4292 tree arg = TREE_VEC_ELT (args, in_arg);
4293 if (ARGUMENT_PACK_P (arg))
4294 {
4295 tree packed = ARGUMENT_PACK_ARGS (arg);
4296 int i, num_packed = TREE_VEC_LENGTH (packed);
4297 for (i = 0; i < num_packed; ++i, ++out_arg)
4298 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4299 if (non_default_args_count > 0)
4300 non_default_args_count += num_packed - 1;
4301 }
4302 else
4303 {
4304 TREE_VEC_ELT (result_args, out_arg) = arg;
4305 ++out_arg;
4306 }
4307 }
4308 if (non_default_args_count >= 0)
4309 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4310 return result_args;
4311 }
4312
4313 /* Checks if DECL shadows a template parameter.
4314
4315 [temp.local]: A template-parameter shall not be redeclared within its
4316 scope (including nested scopes).
4317
4318 Emits an error and returns TRUE if the DECL shadows a parameter,
4319 returns FALSE otherwise. */
4320
4321 bool
4322 check_template_shadow (tree decl)
4323 {
4324 tree olddecl;
4325
4326 /* If we're not in a template, we can't possibly shadow a template
4327 parameter. */
4328 if (!current_template_parms)
4329 return true;
4330
4331 /* Figure out what we're shadowing. */
4332 decl = OVL_FIRST (decl);
4333 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4334
4335 /* If there's no previous binding for this name, we're not shadowing
4336 anything, let alone a template parameter. */
4337 if (!olddecl)
4338 return true;
4339
4340 /* If we're not shadowing a template parameter, we're done. Note
4341 that OLDDECL might be an OVERLOAD (or perhaps even an
4342 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4343 node. */
4344 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4345 return true;
4346
4347 /* We check for decl != olddecl to avoid bogus errors for using a
4348 name inside a class. We check TPFI to avoid duplicate errors for
4349 inline member templates. */
4350 if (decl == olddecl
4351 || (DECL_TEMPLATE_PARM_P (decl)
4352 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4353 return true;
4354
4355 /* Don't complain about the injected class name, as we've already
4356 complained about the class itself. */
4357 if (DECL_SELF_REFERENCE_P (decl))
4358 return false;
4359
4360 if (DECL_TEMPLATE_PARM_P (decl))
4361 error ("declaration of template parameter %q+D shadows "
4362 "template parameter", decl);
4363 else
4364 error ("declaration of %q+#D shadows template parameter", decl);
4365 inform (DECL_SOURCE_LOCATION (olddecl),
4366 "template parameter %qD declared here", olddecl);
4367 return false;
4368 }
4369
4370 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4371 ORIG_LEVEL, DECL, and TYPE. */
4372
4373 static tree
4374 build_template_parm_index (int index,
4375 int level,
4376 int orig_level,
4377 tree decl,
4378 tree type)
4379 {
4380 tree t = make_node (TEMPLATE_PARM_INDEX);
4381 TEMPLATE_PARM_IDX (t) = index;
4382 TEMPLATE_PARM_LEVEL (t) = level;
4383 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4384 TEMPLATE_PARM_DECL (t) = decl;
4385 TREE_TYPE (t) = type;
4386 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4387 TREE_READONLY (t) = TREE_READONLY (decl);
4388
4389 return t;
4390 }
4391
4392 /* Find the canonical type parameter for the given template type
4393 parameter. Returns the canonical type parameter, which may be TYPE
4394 if no such parameter existed. */
4395
4396 static tree
4397 canonical_type_parameter (tree type)
4398 {
4399 tree list;
4400 int idx = TEMPLATE_TYPE_IDX (type);
4401 if (!canonical_template_parms)
4402 vec_alloc (canonical_template_parms, idx + 1);
4403
4404 if (canonical_template_parms->length () <= (unsigned) idx)
4405 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4406
4407 list = (*canonical_template_parms)[idx];
4408 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4409 list = TREE_CHAIN (list);
4410
4411 if (list)
4412 return TREE_VALUE (list);
4413 else
4414 {
4415 (*canonical_template_parms)[idx]
4416 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4417 return type;
4418 }
4419 }
4420
4421 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4422 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4423 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4424 new one is created. */
4425
4426 static tree
4427 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4428 tsubst_flags_t complain)
4429 {
4430 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4431 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4432 != TEMPLATE_PARM_LEVEL (index) - levels)
4433 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4434 {
4435 tree orig_decl = TEMPLATE_PARM_DECL (index);
4436
4437 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4438 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4439 type);
4440 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4441 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4442 DECL_ARTIFICIAL (decl) = 1;
4443 SET_DECL_TEMPLATE_PARM_P (decl);
4444
4445 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4446 TEMPLATE_PARM_LEVEL (index) - levels,
4447 TEMPLATE_PARM_ORIG_LEVEL (index),
4448 decl, type);
4449 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4450 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4451 = TEMPLATE_PARM_PARAMETER_PACK (index);
4452
4453 /* Template template parameters need this. */
4454 tree inner = decl;
4455 if (TREE_CODE (decl) == TEMPLATE_DECL)
4456 {
4457 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4458 TYPE_DECL, DECL_NAME (decl), type);
4459 DECL_TEMPLATE_RESULT (decl) = inner;
4460 DECL_ARTIFICIAL (inner) = true;
4461 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4462 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4463 }
4464
4465 /* Attach the TPI to the decl. */
4466 if (TREE_CODE (inner) == TYPE_DECL)
4467 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4468 else
4469 DECL_INITIAL (decl) = tpi;
4470 }
4471
4472 return TEMPLATE_PARM_DESCENDANTS (index);
4473 }
4474
4475 /* Process information from new template parameter PARM and append it
4476 to the LIST being built. This new parameter is a non-type
4477 parameter iff IS_NON_TYPE is true. This new parameter is a
4478 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4479 is in PARM_LOC. */
4480
4481 tree
4482 process_template_parm (tree list, location_t parm_loc, tree parm,
4483 bool is_non_type, bool is_parameter_pack)
4484 {
4485 tree decl = 0;
4486 int idx = 0;
4487
4488 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4489 tree defval = TREE_PURPOSE (parm);
4490 tree constr = TREE_TYPE (parm);
4491
4492 if (list)
4493 {
4494 tree p = tree_last (list);
4495
4496 if (p && TREE_VALUE (p) != error_mark_node)
4497 {
4498 p = TREE_VALUE (p);
4499 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4500 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4501 else
4502 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4503 }
4504
4505 ++idx;
4506 }
4507
4508 if (is_non_type)
4509 {
4510 parm = TREE_VALUE (parm);
4511
4512 SET_DECL_TEMPLATE_PARM_P (parm);
4513
4514 if (TREE_TYPE (parm) != error_mark_node)
4515 {
4516 /* [temp.param]
4517
4518 The top-level cv-qualifiers on the template-parameter are
4519 ignored when determining its type. */
4520 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4521 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4522 TREE_TYPE (parm) = error_mark_node;
4523 else if (uses_parameter_packs (TREE_TYPE (parm))
4524 && !is_parameter_pack
4525 /* If we're in a nested template parameter list, the template
4526 template parameter could be a parameter pack. */
4527 && processing_template_parmlist == 1)
4528 {
4529 /* This template parameter is not a parameter pack, but it
4530 should be. Complain about "bare" parameter packs. */
4531 check_for_bare_parameter_packs (TREE_TYPE (parm));
4532
4533 /* Recover by calling this a parameter pack. */
4534 is_parameter_pack = true;
4535 }
4536 }
4537
4538 /* A template parameter is not modifiable. */
4539 TREE_CONSTANT (parm) = 1;
4540 TREE_READONLY (parm) = 1;
4541 decl = build_decl (parm_loc,
4542 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4543 TREE_CONSTANT (decl) = 1;
4544 TREE_READONLY (decl) = 1;
4545 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4546 = build_template_parm_index (idx, processing_template_decl,
4547 processing_template_decl,
4548 decl, TREE_TYPE (parm));
4549
4550 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4551 = is_parameter_pack;
4552 }
4553 else
4554 {
4555 tree t;
4556 parm = TREE_VALUE (TREE_VALUE (parm));
4557
4558 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4559 {
4560 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4561 /* This is for distinguishing between real templates and template
4562 template parameters */
4563 TREE_TYPE (parm) = t;
4564 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4565 decl = parm;
4566 }
4567 else
4568 {
4569 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4570 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4571 decl = build_decl (parm_loc,
4572 TYPE_DECL, parm, t);
4573 }
4574
4575 TYPE_NAME (t) = decl;
4576 TYPE_STUB_DECL (t) = decl;
4577 parm = decl;
4578 TEMPLATE_TYPE_PARM_INDEX (t)
4579 = build_template_parm_index (idx, processing_template_decl,
4580 processing_template_decl,
4581 decl, TREE_TYPE (parm));
4582 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4583 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4584 }
4585 DECL_ARTIFICIAL (decl) = 1;
4586 SET_DECL_TEMPLATE_PARM_P (decl);
4587
4588 /* Build requirements for the type/template parameter.
4589 This must be done after SET_DECL_TEMPLATE_PARM_P or
4590 process_template_parm could fail. */
4591 tree reqs = finish_shorthand_constraint (parm, constr);
4592
4593 decl = pushdecl (decl);
4594 if (!is_non_type)
4595 parm = decl;
4596
4597 /* Build the parameter node linking the parameter declaration,
4598 its default argument (if any), and its constraints (if any). */
4599 parm = build_tree_list (defval, parm);
4600 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4601
4602 return chainon (list, parm);
4603 }
4604
4605 /* The end of a template parameter list has been reached. Process the
4606 tree list into a parameter vector, converting each parameter into a more
4607 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4608 as PARM_DECLs. */
4609
4610 tree
4611 end_template_parm_list (tree parms)
4612 {
4613 int nparms;
4614 tree parm, next;
4615 tree saved_parmlist = make_tree_vec (list_length (parms));
4616
4617 /* Pop the dummy parameter level and add the real one. */
4618 current_template_parms = TREE_CHAIN (current_template_parms);
4619
4620 current_template_parms
4621 = tree_cons (size_int (processing_template_decl),
4622 saved_parmlist, current_template_parms);
4623
4624 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4625 {
4626 next = TREE_CHAIN (parm);
4627 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4628 TREE_CHAIN (parm) = NULL_TREE;
4629 }
4630
4631 --processing_template_parmlist;
4632
4633 return saved_parmlist;
4634 }
4635
4636 // Explicitly indicate the end of the template parameter list. We assume
4637 // that the current template parameters have been constructed and/or
4638 // managed explicitly, as when creating new template template parameters
4639 // from a shorthand constraint.
4640 void
4641 end_template_parm_list ()
4642 {
4643 --processing_template_parmlist;
4644 }
4645
4646 /* end_template_decl is called after a template declaration is seen. */
4647
4648 void
4649 end_template_decl (void)
4650 {
4651 reset_specialization ();
4652
4653 if (! processing_template_decl)
4654 return;
4655
4656 /* This matches the pushlevel in begin_template_parm_list. */
4657 finish_scope ();
4658
4659 --processing_template_decl;
4660 current_template_parms = TREE_CHAIN (current_template_parms);
4661 }
4662
4663 /* Takes a TREE_LIST representing a template parameter and convert it
4664 into an argument suitable to be passed to the type substitution
4665 functions. Note that If the TREE_LIST contains an error_mark
4666 node, the returned argument is error_mark_node. */
4667
4668 tree
4669 template_parm_to_arg (tree t)
4670 {
4671
4672 if (t == NULL_TREE
4673 || TREE_CODE (t) != TREE_LIST)
4674 return t;
4675
4676 if (error_operand_p (TREE_VALUE (t)))
4677 return error_mark_node;
4678
4679 t = TREE_VALUE (t);
4680
4681 if (TREE_CODE (t) == TYPE_DECL
4682 || TREE_CODE (t) == TEMPLATE_DECL)
4683 {
4684 t = TREE_TYPE (t);
4685
4686 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4687 {
4688 /* Turn this argument into a TYPE_ARGUMENT_PACK
4689 with a single element, which expands T. */
4690 tree vec = make_tree_vec (1);
4691 if (CHECKING_P)
4692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4693
4694 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4695
4696 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4697 SET_ARGUMENT_PACK_ARGS (t, vec);
4698 }
4699 }
4700 else
4701 {
4702 t = DECL_INITIAL (t);
4703
4704 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4705 {
4706 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4707 with a single element, which expands T. */
4708 tree vec = make_tree_vec (1);
4709 if (CHECKING_P)
4710 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4711
4712 t = convert_from_reference (t);
4713 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4714
4715 t = make_node (NONTYPE_ARGUMENT_PACK);
4716 SET_ARGUMENT_PACK_ARGS (t, vec);
4717 }
4718 else
4719 t = convert_from_reference (t);
4720 }
4721 return t;
4722 }
4723
4724 /* Given a single level of template parameters (a TREE_VEC), return it
4725 as a set of template arguments. */
4726
4727 tree
4728 template_parms_level_to_args (tree parms)
4729 {
4730 tree a = copy_node (parms);
4731 TREE_TYPE (a) = NULL_TREE;
4732 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4733 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4734
4735 if (CHECKING_P)
4736 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4737
4738 return a;
4739 }
4740
4741 /* Given a set of template parameters, return them as a set of template
4742 arguments. The template parameters are represented as a TREE_VEC, in
4743 the form documented in cp-tree.h for template arguments. */
4744
4745 tree
4746 template_parms_to_args (tree parms)
4747 {
4748 tree header;
4749 tree args = NULL_TREE;
4750 int length = TMPL_PARMS_DEPTH (parms);
4751 int l = length;
4752
4753 /* If there is only one level of template parameters, we do not
4754 create a TREE_VEC of TREE_VECs. Instead, we return a single
4755 TREE_VEC containing the arguments. */
4756 if (length > 1)
4757 args = make_tree_vec (length);
4758
4759 for (header = parms; header; header = TREE_CHAIN (header))
4760 {
4761 tree a = template_parms_level_to_args (TREE_VALUE (header));
4762
4763 if (length > 1)
4764 TREE_VEC_ELT (args, --l) = a;
4765 else
4766 args = a;
4767 }
4768
4769 return args;
4770 }
4771
4772 /* Within the declaration of a template, return the currently active
4773 template parameters as an argument TREE_VEC. */
4774
4775 static tree
4776 current_template_args (void)
4777 {
4778 return template_parms_to_args (current_template_parms);
4779 }
4780
4781 /* Return the fully generic arguments for of TMPL, i.e. what
4782 current_template_args would be while parsing it. */
4783
4784 tree
4785 generic_targs_for (tree tmpl)
4786 {
4787 if (tmpl == NULL_TREE)
4788 return NULL_TREE;
4789 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4790 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4791 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4792 template parameter, it has no TEMPLATE_INFO; for a partial
4793 specialization, it has the arguments for the primary template, and we
4794 want the arguments for the partial specialization. */;
4795 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4796 if (tree ti = get_template_info (result))
4797 return TI_ARGS (ti);
4798 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4799 }
4800
4801 /* Update the declared TYPE by doing any lookups which were thought to be
4802 dependent, but are not now that we know the SCOPE of the declarator. */
4803
4804 tree
4805 maybe_update_decl_type (tree orig_type, tree scope)
4806 {
4807 tree type = orig_type;
4808
4809 if (type == NULL_TREE)
4810 return type;
4811
4812 if (TREE_CODE (orig_type) == TYPE_DECL)
4813 type = TREE_TYPE (type);
4814
4815 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4816 && dependent_type_p (type)
4817 /* Don't bother building up the args in this case. */
4818 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4819 {
4820 /* tsubst in the args corresponding to the template parameters,
4821 including auto if present. Most things will be unchanged, but
4822 make_typename_type and tsubst_qualified_id will resolve
4823 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4824 tree args = current_template_args ();
4825 tree auto_node = type_uses_auto (type);
4826 tree pushed;
4827 if (auto_node)
4828 {
4829 tree auto_vec = make_tree_vec (1);
4830 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4831 args = add_to_template_args (args, auto_vec);
4832 }
4833 pushed = push_scope (scope);
4834 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4835 if (pushed)
4836 pop_scope (scope);
4837 }
4838
4839 if (type == error_mark_node)
4840 return orig_type;
4841
4842 if (TREE_CODE (orig_type) == TYPE_DECL)
4843 {
4844 if (same_type_p (type, TREE_TYPE (orig_type)))
4845 type = orig_type;
4846 else
4847 type = TYPE_NAME (type);
4848 }
4849 return type;
4850 }
4851
4852 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4853 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4854 the new template is a member template. */
4855
4856 static tree
4857 build_template_decl (tree decl, tree parms, bool member_template_p)
4858 {
4859 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4860 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4861 DECL_TEMPLATE_PARMS (tmpl) = parms;
4862 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4863 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4864 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4865
4866 return tmpl;
4867 }
4868
4869 struct template_parm_data
4870 {
4871 /* The level of the template parameters we are currently
4872 processing. */
4873 int level;
4874
4875 /* The index of the specialization argument we are currently
4876 processing. */
4877 int current_arg;
4878
4879 /* An array whose size is the number of template parameters. The
4880 elements are nonzero if the parameter has been used in any one
4881 of the arguments processed so far. */
4882 int* parms;
4883
4884 /* An array whose size is the number of template arguments. The
4885 elements are nonzero if the argument makes use of template
4886 parameters of this level. */
4887 int* arg_uses_template_parms;
4888 };
4889
4890 /* Subroutine of push_template_decl used to see if each template
4891 parameter in a partial specialization is used in the explicit
4892 argument list. If T is of the LEVEL given in DATA (which is
4893 treated as a template_parm_data*), then DATA->PARMS is marked
4894 appropriately. */
4895
4896 static int
4897 mark_template_parm (tree t, void* data)
4898 {
4899 int level;
4900 int idx;
4901 struct template_parm_data* tpd = (struct template_parm_data*) data;
4902
4903 template_parm_level_and_index (t, &level, &idx);
4904
4905 if (level == tpd->level)
4906 {
4907 tpd->parms[idx] = 1;
4908 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4909 }
4910
4911 /* In C++17 the type of a non-type argument is a deduced context. */
4912 if (cxx_dialect >= cxx17
4913 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4914 for_each_template_parm (TREE_TYPE (t),
4915 &mark_template_parm,
4916 data,
4917 NULL,
4918 /*include_nondeduced_p=*/false);
4919
4920 /* Return zero so that for_each_template_parm will continue the
4921 traversal of the tree; we want to mark *every* template parm. */
4922 return 0;
4923 }
4924
4925 /* Process the partial specialization DECL. */
4926
4927 static tree
4928 process_partial_specialization (tree decl)
4929 {
4930 tree type = TREE_TYPE (decl);
4931 tree tinfo = get_template_info (decl);
4932 tree maintmpl = TI_TEMPLATE (tinfo);
4933 tree specargs = TI_ARGS (tinfo);
4934 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4935 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4936 tree inner_parms;
4937 tree inst;
4938 int nargs = TREE_VEC_LENGTH (inner_args);
4939 int ntparms;
4940 int i;
4941 bool did_error_intro = false;
4942 struct template_parm_data tpd;
4943 struct template_parm_data tpd2;
4944
4945 gcc_assert (current_template_parms);
4946
4947 /* A concept cannot be specialized. */
4948 if (flag_concepts && variable_concept_p (maintmpl))
4949 {
4950 error ("specialization of variable concept %q#D", maintmpl);
4951 return error_mark_node;
4952 }
4953
4954 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4955 ntparms = TREE_VEC_LENGTH (inner_parms);
4956
4957 /* We check that each of the template parameters given in the
4958 partial specialization is used in the argument list to the
4959 specialization. For example:
4960
4961 template <class T> struct S;
4962 template <class T> struct S<T*>;
4963
4964 The second declaration is OK because `T*' uses the template
4965 parameter T, whereas
4966
4967 template <class T> struct S<int>;
4968
4969 is no good. Even trickier is:
4970
4971 template <class T>
4972 struct S1
4973 {
4974 template <class U>
4975 struct S2;
4976 template <class U>
4977 struct S2<T>;
4978 };
4979
4980 The S2<T> declaration is actually invalid; it is a
4981 full-specialization. Of course,
4982
4983 template <class U>
4984 struct S2<T (*)(U)>;
4985
4986 or some such would have been OK. */
4987 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4988 tpd.parms = XALLOCAVEC (int, ntparms);
4989 memset (tpd.parms, 0, sizeof (int) * ntparms);
4990
4991 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4992 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4993 for (i = 0; i < nargs; ++i)
4994 {
4995 tpd.current_arg = i;
4996 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4997 &mark_template_parm,
4998 &tpd,
4999 NULL,
5000 /*include_nondeduced_p=*/false);
5001 }
5002 for (i = 0; i < ntparms; ++i)
5003 if (tpd.parms[i] == 0)
5004 {
5005 /* One of the template parms was not used in a deduced context in the
5006 specialization. */
5007 if (!did_error_intro)
5008 {
5009 error ("template parameters not deducible in "
5010 "partial specialization:");
5011 did_error_intro = true;
5012 }
5013
5014 inform (input_location, " %qD",
5015 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5016 }
5017
5018 if (did_error_intro)
5019 return error_mark_node;
5020
5021 /* [temp.class.spec]
5022
5023 The argument list of the specialization shall not be identical to
5024 the implicit argument list of the primary template. */
5025 tree main_args
5026 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5027 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5028 && (!flag_concepts
5029 || !strictly_subsumes (current_template_constraints (),
5030 inner_args, maintmpl)))
5031 {
5032 if (!flag_concepts)
5033 error ("partial specialization %q+D does not specialize "
5034 "any template arguments; to define the primary template, "
5035 "remove the template argument list", decl);
5036 else
5037 error ("partial specialization %q+D does not specialize any "
5038 "template arguments and is not more constrained than "
5039 "the primary template; to define the primary template, "
5040 "remove the template argument list", decl);
5041 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5042 }
5043
5044 /* A partial specialization that replaces multiple parameters of the
5045 primary template with a pack expansion is less specialized for those
5046 parameters. */
5047 if (nargs < DECL_NTPARMS (maintmpl))
5048 {
5049 error ("partial specialization is not more specialized than the "
5050 "primary template because it replaces multiple parameters "
5051 "with a pack expansion");
5052 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5053 /* Avoid crash in process_partial_specialization. */
5054 return decl;
5055 }
5056
5057 /* If we aren't in a dependent class, we can actually try deduction. */
5058 else if (tpd.level == 1
5059 /* FIXME we should be able to handle a partial specialization of a
5060 partial instantiation, but currently we can't (c++/41727). */
5061 && TMPL_ARGS_DEPTH (specargs) == 1
5062 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5063 {
5064 auto_diagnostic_group d;
5065 if (permerror (input_location, "partial specialization %qD is not "
5066 "more specialized than", decl))
5067 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5068 maintmpl);
5069 }
5070
5071 /* [temp.class.spec]
5072
5073 A partially specialized non-type argument expression shall not
5074 involve template parameters of the partial specialization except
5075 when the argument expression is a simple identifier.
5076
5077 The type of a template parameter corresponding to a specialized
5078 non-type argument shall not be dependent on a parameter of the
5079 specialization.
5080
5081 Also, we verify that pack expansions only occur at the
5082 end of the argument list. */
5083 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
5084 tpd2.parms = 0;
5085 for (i = 0; i < nargs; ++i)
5086 {
5087 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5088 tree arg = TREE_VEC_ELT (inner_args, i);
5089 tree packed_args = NULL_TREE;
5090 int j, len = 1;
5091
5092 if (ARGUMENT_PACK_P (arg))
5093 {
5094 /* Extract the arguments from the argument pack. We'll be
5095 iterating over these in the following loop. */
5096 packed_args = ARGUMENT_PACK_ARGS (arg);
5097 len = TREE_VEC_LENGTH (packed_args);
5098 }
5099
5100 for (j = 0; j < len; j++)
5101 {
5102 if (packed_args)
5103 /* Get the Jth argument in the parameter pack. */
5104 arg = TREE_VEC_ELT (packed_args, j);
5105
5106 if (PACK_EXPANSION_P (arg))
5107 {
5108 /* Pack expansions must come at the end of the
5109 argument list. */
5110 if ((packed_args && j < len - 1)
5111 || (!packed_args && i < nargs - 1))
5112 {
5113 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5114 error ("parameter pack argument %qE must be at the "
5115 "end of the template argument list", arg);
5116 else
5117 error ("parameter pack argument %qT must be at the "
5118 "end of the template argument list", arg);
5119 }
5120 }
5121
5122 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5123 /* We only care about the pattern. */
5124 arg = PACK_EXPANSION_PATTERN (arg);
5125
5126 if (/* These first two lines are the `non-type' bit. */
5127 !TYPE_P (arg)
5128 && TREE_CODE (arg) != TEMPLATE_DECL
5129 /* This next two lines are the `argument expression is not just a
5130 simple identifier' condition and also the `specialized
5131 non-type argument' bit. */
5132 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5133 && !((REFERENCE_REF_P (arg)
5134 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5135 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5136 {
5137 if ((!packed_args && tpd.arg_uses_template_parms[i])
5138 || (packed_args && uses_template_parms (arg)))
5139 error_at (cp_expr_loc_or_input_loc (arg),
5140 "template argument %qE involves template "
5141 "parameter(s)", arg);
5142 else
5143 {
5144 /* Look at the corresponding template parameter,
5145 marking which template parameters its type depends
5146 upon. */
5147 tree type = TREE_TYPE (parm);
5148
5149 if (!tpd2.parms)
5150 {
5151 /* We haven't yet initialized TPD2. Do so now. */
5152 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5153 /* The number of parameters here is the number in the
5154 main template, which, as checked in the assertion
5155 above, is NARGS. */
5156 tpd2.parms = XALLOCAVEC (int, nargs);
5157 tpd2.level =
5158 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5159 }
5160
5161 /* Mark the template parameters. But this time, we're
5162 looking for the template parameters of the main
5163 template, not in the specialization. */
5164 tpd2.current_arg = i;
5165 tpd2.arg_uses_template_parms[i] = 0;
5166 memset (tpd2.parms, 0, sizeof (int) * nargs);
5167 for_each_template_parm (type,
5168 &mark_template_parm,
5169 &tpd2,
5170 NULL,
5171 /*include_nondeduced_p=*/false);
5172
5173 if (tpd2.arg_uses_template_parms [i])
5174 {
5175 /* The type depended on some template parameters.
5176 If they are fully specialized in the
5177 specialization, that's OK. */
5178 int j;
5179 int count = 0;
5180 for (j = 0; j < nargs; ++j)
5181 if (tpd2.parms[j] != 0
5182 && tpd.arg_uses_template_parms [j])
5183 ++count;
5184 if (count != 0)
5185 error_n (input_location, count,
5186 "type %qT of template argument %qE depends "
5187 "on a template parameter",
5188 "type %qT of template argument %qE depends "
5189 "on template parameters",
5190 type,
5191 arg);
5192 }
5193 }
5194 }
5195 }
5196 }
5197
5198 /* We should only get here once. */
5199 if (TREE_CODE (decl) == TYPE_DECL)
5200 gcc_assert (!COMPLETE_TYPE_P (type));
5201
5202 // Build the template decl.
5203 tree tmpl = build_template_decl (decl, current_template_parms,
5204 DECL_MEMBER_TEMPLATE_P (maintmpl));
5205 TREE_TYPE (tmpl) = type;
5206 DECL_TEMPLATE_RESULT (tmpl) = decl;
5207 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5208 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5209 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5210
5211 /* Give template template parms a DECL_CONTEXT of the template
5212 for which they are a parameter. */
5213 for (i = 0; i < ntparms; ++i)
5214 {
5215 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5216 if (TREE_CODE (parm) == TEMPLATE_DECL)
5217 DECL_CONTEXT (parm) = tmpl;
5218 }
5219
5220 if (VAR_P (decl))
5221 /* We didn't register this in check_explicit_specialization so we could
5222 wait until the constraints were set. */
5223 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5224 else
5225 associate_classtype_constraints (type);
5226
5227 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5228 = tree_cons (specargs, tmpl,
5229 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5230 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5231
5232 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5233 inst = TREE_CHAIN (inst))
5234 {
5235 tree instance = TREE_VALUE (inst);
5236 if (TYPE_P (instance)
5237 ? (COMPLETE_TYPE_P (instance)
5238 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5239 : DECL_TEMPLATE_INSTANTIATION (instance))
5240 {
5241 tree spec = most_specialized_partial_spec (instance, tf_none);
5242 tree inst_decl = (DECL_P (instance)
5243 ? instance : TYPE_NAME (instance));
5244 if (!spec)
5245 /* OK */;
5246 else if (spec == error_mark_node)
5247 permerror (input_location,
5248 "declaration of %qD ambiguates earlier template "
5249 "instantiation for %qD", decl, inst_decl);
5250 else if (TREE_VALUE (spec) == tmpl)
5251 permerror (input_location,
5252 "partial specialization of %qD after instantiation "
5253 "of %qD", decl, inst_decl);
5254 }
5255 }
5256
5257 return decl;
5258 }
5259
5260 /* PARM is a template parameter of some form; return the corresponding
5261 TEMPLATE_PARM_INDEX. */
5262
5263 static tree
5264 get_template_parm_index (tree parm)
5265 {
5266 if (TREE_CODE (parm) == PARM_DECL
5267 || TREE_CODE (parm) == CONST_DECL)
5268 parm = DECL_INITIAL (parm);
5269 else if (TREE_CODE (parm) == TYPE_DECL
5270 || TREE_CODE (parm) == TEMPLATE_DECL)
5271 parm = TREE_TYPE (parm);
5272 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5273 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5274 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5275 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5276 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5277 return parm;
5278 }
5279
5280 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5281 parameter packs used by the template parameter PARM. */
5282
5283 static void
5284 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5285 {
5286 /* A type parm can't refer to another parm. */
5287 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5288 return;
5289 else if (TREE_CODE (parm) == PARM_DECL)
5290 {
5291 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5292 ppd, ppd->visited);
5293 return;
5294 }
5295
5296 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5297
5298 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5299 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5300 {
5301 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5302 if (template_parameter_pack_p (p))
5303 /* Any packs in the type are expanded by this parameter. */;
5304 else
5305 fixed_parameter_pack_p_1 (p, ppd);
5306 }
5307 }
5308
5309 /* PARM is a template parameter pack. Return any parameter packs used in
5310 its type or the type of any of its template parameters. If there are
5311 any such packs, it will be instantiated into a fixed template parameter
5312 list by partial instantiation rather than be fully deduced. */
5313
5314 tree
5315 fixed_parameter_pack_p (tree parm)
5316 {
5317 /* This can only be true in a member template. */
5318 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5319 return NULL_TREE;
5320 /* This can only be true for a parameter pack. */
5321 if (!template_parameter_pack_p (parm))
5322 return NULL_TREE;
5323 /* A type parm can't refer to another parm. */
5324 if (TREE_CODE (parm) == TYPE_DECL)
5325 return NULL_TREE;
5326
5327 tree parameter_packs = NULL_TREE;
5328 struct find_parameter_pack_data ppd;
5329 ppd.parameter_packs = &parameter_packs;
5330 ppd.visited = new hash_set<tree>;
5331 ppd.type_pack_expansion_p = false;
5332
5333 fixed_parameter_pack_p_1 (parm, &ppd);
5334
5335 delete ppd.visited;
5336 return parameter_packs;
5337 }
5338
5339 /* Check that a template declaration's use of default arguments and
5340 parameter packs is not invalid. Here, PARMS are the template
5341 parameters. IS_PRIMARY is true if DECL is the thing declared by
5342 a primary template. IS_PARTIAL is true if DECL is a partial
5343 specialization.
5344
5345 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5346 function template declaration or a friend class template
5347 declaration. In the function case, 1 indicates a declaration, 2
5348 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5349 emitted for extraneous default arguments.
5350
5351 Returns TRUE if there were no errors found, FALSE otherwise. */
5352
5353 bool
5354 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5355 bool is_partial, int is_friend_decl)
5356 {
5357 const char *msg;
5358 int last_level_to_check;
5359 tree parm_level;
5360 bool no_errors = true;
5361
5362 /* [temp.param]
5363
5364 A default template-argument shall not be specified in a
5365 function template declaration or a function template definition, nor
5366 in the template-parameter-list of the definition of a member of a
5367 class template. */
5368
5369 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5370 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5371 /* You can't have a function template declaration in a local
5372 scope, nor you can you define a member of a class template in a
5373 local scope. */
5374 return true;
5375
5376 if ((TREE_CODE (decl) == TYPE_DECL
5377 && TREE_TYPE (decl)
5378 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5379 || (TREE_CODE (decl) == FUNCTION_DECL
5380 && LAMBDA_FUNCTION_P (decl)))
5381 /* A lambda doesn't have an explicit declaration; don't complain
5382 about the parms of the enclosing class. */
5383 return true;
5384
5385 if (current_class_type
5386 && !TYPE_BEING_DEFINED (current_class_type)
5387 && DECL_LANG_SPECIFIC (decl)
5388 && DECL_DECLARES_FUNCTION_P (decl)
5389 /* If this is either a friend defined in the scope of the class
5390 or a member function. */
5391 && (DECL_FUNCTION_MEMBER_P (decl)
5392 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5393 : DECL_FRIEND_CONTEXT (decl)
5394 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5395 : false)
5396 /* And, if it was a member function, it really was defined in
5397 the scope of the class. */
5398 && (!DECL_FUNCTION_MEMBER_P (decl)
5399 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5400 /* We already checked these parameters when the template was
5401 declared, so there's no need to do it again now. This function
5402 was defined in class scope, but we're processing its body now
5403 that the class is complete. */
5404 return true;
5405
5406 /* Core issue 226 (C++0x only): the following only applies to class
5407 templates. */
5408 if (is_primary
5409 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5410 {
5411 /* [temp.param]
5412
5413 If a template-parameter has a default template-argument, all
5414 subsequent template-parameters shall have a default
5415 template-argument supplied. */
5416 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5417 {
5418 tree inner_parms = TREE_VALUE (parm_level);
5419 int ntparms = TREE_VEC_LENGTH (inner_parms);
5420 int seen_def_arg_p = 0;
5421 int i;
5422
5423 for (i = 0; i < ntparms; ++i)
5424 {
5425 tree parm = TREE_VEC_ELT (inner_parms, i);
5426
5427 if (parm == error_mark_node)
5428 continue;
5429
5430 if (TREE_PURPOSE (parm))
5431 seen_def_arg_p = 1;
5432 else if (seen_def_arg_p
5433 && !template_parameter_pack_p (TREE_VALUE (parm)))
5434 {
5435 error ("no default argument for %qD", TREE_VALUE (parm));
5436 /* For better subsequent error-recovery, we indicate that
5437 there should have been a default argument. */
5438 TREE_PURPOSE (parm) = error_mark_node;
5439 no_errors = false;
5440 }
5441 else if (!is_partial
5442 && !is_friend_decl
5443 /* Don't complain about an enclosing partial
5444 specialization. */
5445 && parm_level == parms
5446 && TREE_CODE (decl) == TYPE_DECL
5447 && i < ntparms - 1
5448 && template_parameter_pack_p (TREE_VALUE (parm))
5449 /* A fixed parameter pack will be partially
5450 instantiated into a fixed length list. */
5451 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5452 {
5453 /* A primary class template can only have one
5454 parameter pack, at the end of the template
5455 parameter list. */
5456
5457 error ("parameter pack %q+D must be at the end of the"
5458 " template parameter list", TREE_VALUE (parm));
5459
5460 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5461 = error_mark_node;
5462 no_errors = false;
5463 }
5464 }
5465 }
5466 }
5467
5468 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5469 || is_partial
5470 || !is_primary
5471 || is_friend_decl)
5472 /* For an ordinary class template, default template arguments are
5473 allowed at the innermost level, e.g.:
5474 template <class T = int>
5475 struct S {};
5476 but, in a partial specialization, they're not allowed even
5477 there, as we have in [temp.class.spec]:
5478
5479 The template parameter list of a specialization shall not
5480 contain default template argument values.
5481
5482 So, for a partial specialization, or for a function template
5483 (in C++98/C++03), we look at all of them. */
5484 ;
5485 else
5486 /* But, for a primary class template that is not a partial
5487 specialization we look at all template parameters except the
5488 innermost ones. */
5489 parms = TREE_CHAIN (parms);
5490
5491 /* Figure out what error message to issue. */
5492 if (is_friend_decl == 2)
5493 msg = G_("default template arguments may not be used in function template "
5494 "friend re-declaration");
5495 else if (is_friend_decl)
5496 msg = G_("default template arguments may not be used in template "
5497 "friend declarations");
5498 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5499 msg = G_("default template arguments may not be used in function templates "
5500 "without %<-std=c++11%> or %<-std=gnu++11%>");
5501 else if (is_partial)
5502 msg = G_("default template arguments may not be used in "
5503 "partial specializations");
5504 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5505 msg = G_("default argument for template parameter for class enclosing %qD");
5506 else
5507 /* Per [temp.param]/9, "A default template-argument shall not be
5508 specified in the template-parameter-lists of the definition of
5509 a member of a class template that appears outside of the member's
5510 class.", thus if we aren't handling a member of a class template
5511 there is no need to examine the parameters. */
5512 return true;
5513
5514 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5515 /* If we're inside a class definition, there's no need to
5516 examine the parameters to the class itself. On the one
5517 hand, they will be checked when the class is defined, and,
5518 on the other, default arguments are valid in things like:
5519 template <class T = double>
5520 struct S { template <class U> void f(U); };
5521 Here the default argument for `S' has no bearing on the
5522 declaration of `f'. */
5523 last_level_to_check = template_class_depth (current_class_type) + 1;
5524 else
5525 /* Check everything. */
5526 last_level_to_check = 0;
5527
5528 for (parm_level = parms;
5529 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5530 parm_level = TREE_CHAIN (parm_level))
5531 {
5532 tree inner_parms = TREE_VALUE (parm_level);
5533 int i;
5534 int ntparms;
5535
5536 ntparms = TREE_VEC_LENGTH (inner_parms);
5537 for (i = 0; i < ntparms; ++i)
5538 {
5539 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5540 continue;
5541
5542 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5543 {
5544 if (msg)
5545 {
5546 no_errors = false;
5547 if (is_friend_decl == 2)
5548 return no_errors;
5549
5550 error (msg, decl);
5551 msg = 0;
5552 }
5553
5554 /* Clear out the default argument so that we are not
5555 confused later. */
5556 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5557 }
5558 }
5559
5560 /* At this point, if we're still interested in issuing messages,
5561 they must apply to classes surrounding the object declared. */
5562 if (msg)
5563 msg = G_("default argument for template parameter for class "
5564 "enclosing %qD");
5565 }
5566
5567 return no_errors;
5568 }
5569
5570 /* Worker for push_template_decl_real, called via
5571 for_each_template_parm. DATA is really an int, indicating the
5572 level of the parameters we are interested in. If T is a template
5573 parameter of that level, return nonzero. */
5574
5575 static int
5576 template_parm_this_level_p (tree t, void* data)
5577 {
5578 int this_level = *(int *)data;
5579 int level;
5580
5581 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5582 level = TEMPLATE_PARM_LEVEL (t);
5583 else
5584 level = TEMPLATE_TYPE_LEVEL (t);
5585 return level == this_level;
5586 }
5587
5588 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5589 DATA is really an int, indicating the innermost outer level of parameters.
5590 If T is a template parameter of that level or further out, return
5591 nonzero. */
5592
5593 static int
5594 template_parm_outer_level (tree t, void *data)
5595 {
5596 int this_level = *(int *)data;
5597 int level;
5598
5599 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5600 level = TEMPLATE_PARM_LEVEL (t);
5601 else
5602 level = TEMPLATE_TYPE_LEVEL (t);
5603 return level <= this_level;
5604 }
5605
5606 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5607 parameters given by current_template_args, or reuses a
5608 previously existing one, if appropriate. Returns the DECL, or an
5609 equivalent one, if it is replaced via a call to duplicate_decls.
5610
5611 If IS_FRIEND is true, DECL is a friend declaration. */
5612
5613 tree
5614 push_template_decl_real (tree decl, bool is_friend)
5615 {
5616 tree tmpl;
5617 tree args;
5618 tree info;
5619 tree ctx;
5620 bool is_primary;
5621 bool is_partial;
5622 int new_template_p = 0;
5623 /* True if the template is a member template, in the sense of
5624 [temp.mem]. */
5625 bool member_template_p = false;
5626
5627 if (decl == error_mark_node || !current_template_parms)
5628 return error_mark_node;
5629
5630 /* See if this is a partial specialization. */
5631 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5632 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5633 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5634 || (VAR_P (decl)
5635 && DECL_LANG_SPECIFIC (decl)
5636 && DECL_TEMPLATE_SPECIALIZATION (decl)
5637 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5638
5639 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5640 is_friend = true;
5641
5642 if (is_friend)
5643 /* For a friend, we want the context of the friend, not
5644 the type of which it is a friend. */
5645 ctx = CP_DECL_CONTEXT (decl);
5646 else if (CP_DECL_CONTEXT (decl)
5647 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5648 /* In the case of a virtual function, we want the class in which
5649 it is defined. */
5650 ctx = CP_DECL_CONTEXT (decl);
5651 else
5652 /* Otherwise, if we're currently defining some class, the DECL
5653 is assumed to be a member of the class. */
5654 ctx = current_scope ();
5655
5656 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5657 ctx = NULL_TREE;
5658
5659 if (!DECL_CONTEXT (decl))
5660 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5661
5662 /* See if this is a primary template. */
5663 if (is_friend && ctx
5664 && uses_template_parms_level (ctx, processing_template_decl))
5665 /* A friend template that specifies a class context, i.e.
5666 template <typename T> friend void A<T>::f();
5667 is not primary. */
5668 is_primary = false;
5669 else if (TREE_CODE (decl) == TYPE_DECL
5670 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5671 is_primary = false;
5672 else
5673 is_primary = template_parm_scope_p ();
5674
5675 if (is_primary)
5676 {
5677 warning (OPT_Wtemplates, "template %qD declared", decl);
5678
5679 if (DECL_CLASS_SCOPE_P (decl))
5680 member_template_p = true;
5681 if (TREE_CODE (decl) == TYPE_DECL
5682 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5683 {
5684 error ("template class without a name");
5685 return error_mark_node;
5686 }
5687 else if (TREE_CODE (decl) == FUNCTION_DECL)
5688 {
5689 if (member_template_p)
5690 {
5691 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5692 error ("member template %qD may not have virt-specifiers", decl);
5693 }
5694 if (DECL_DESTRUCTOR_P (decl))
5695 {
5696 /* [temp.mem]
5697
5698 A destructor shall not be a member template. */
5699 error_at (DECL_SOURCE_LOCATION (decl),
5700 "destructor %qD declared as member template", decl);
5701 return error_mark_node;
5702 }
5703 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5704 && (!prototype_p (TREE_TYPE (decl))
5705 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5706 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5707 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5708 == void_list_node)))
5709 {
5710 /* [basic.stc.dynamic.allocation]
5711
5712 An allocation function can be a function
5713 template. ... Template allocation functions shall
5714 have two or more parameters. */
5715 error ("invalid template declaration of %qD", decl);
5716 return error_mark_node;
5717 }
5718 }
5719 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5720 && CLASS_TYPE_P (TREE_TYPE (decl)))
5721 {
5722 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5723 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5724 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5725 {
5726 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5727 if (TREE_CODE (t) == TYPE_DECL)
5728 t = TREE_TYPE (t);
5729 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5730 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5731 }
5732 }
5733 else if (TREE_CODE (decl) == TYPE_DECL
5734 && TYPE_DECL_ALIAS_P (decl))
5735 /* alias-declaration */
5736 gcc_assert (!DECL_ARTIFICIAL (decl));
5737 else if (VAR_P (decl))
5738 /* C++14 variable template. */;
5739 else if (TREE_CODE (decl) == CONCEPT_DECL)
5740 /* C++2a concept definitions. */;
5741 else
5742 {
5743 error ("template declaration of %q#D", decl);
5744 return error_mark_node;
5745 }
5746 }
5747
5748 /* Check to see that the rules regarding the use of default
5749 arguments are not being violated. We check args for a friend
5750 functions when we know whether it's a definition, introducing
5751 declaration or re-declaration. */
5752 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5753 check_default_tmpl_args (decl, current_template_parms,
5754 is_primary, is_partial, is_friend);
5755
5756 /* Ensure that there are no parameter packs in the type of this
5757 declaration that have not been expanded. */
5758 if (TREE_CODE (decl) == FUNCTION_DECL)
5759 {
5760 /* Check each of the arguments individually to see if there are
5761 any bare parameter packs. */
5762 tree type = TREE_TYPE (decl);
5763 tree arg = DECL_ARGUMENTS (decl);
5764 tree argtype = TYPE_ARG_TYPES (type);
5765
5766 while (arg && argtype)
5767 {
5768 if (!DECL_PACK_P (arg)
5769 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5770 {
5771 /* This is a PARM_DECL that contains unexpanded parameter
5772 packs. We have already complained about this in the
5773 check_for_bare_parameter_packs call, so just replace
5774 these types with ERROR_MARK_NODE. */
5775 TREE_TYPE (arg) = error_mark_node;
5776 TREE_VALUE (argtype) = error_mark_node;
5777 }
5778
5779 arg = DECL_CHAIN (arg);
5780 argtype = TREE_CHAIN (argtype);
5781 }
5782
5783 /* Check for bare parameter packs in the return type and the
5784 exception specifiers. */
5785 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5786 /* Errors were already issued, set return type to int
5787 as the frontend doesn't expect error_mark_node as
5788 the return type. */
5789 TREE_TYPE (type) = integer_type_node;
5790 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5791 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5792 }
5793 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5794 && TYPE_DECL_ALIAS_P (decl))
5795 ? DECL_ORIGINAL_TYPE (decl)
5796 : TREE_TYPE (decl)))
5797 {
5798 TREE_TYPE (decl) = error_mark_node;
5799 return error_mark_node;
5800 }
5801
5802 if (is_partial)
5803 return process_partial_specialization (decl);
5804
5805 args = current_template_args ();
5806
5807 if (!ctx
5808 || TREE_CODE (ctx) == FUNCTION_DECL
5809 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5810 || (TREE_CODE (decl) == TYPE_DECL
5811 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5812 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5813 {
5814 if (DECL_LANG_SPECIFIC (decl)
5815 && DECL_TEMPLATE_INFO (decl)
5816 && DECL_TI_TEMPLATE (decl))
5817 tmpl = DECL_TI_TEMPLATE (decl);
5818 /* If DECL is a TYPE_DECL for a class-template, then there won't
5819 be DECL_LANG_SPECIFIC. The information equivalent to
5820 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5821 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5822 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5823 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5824 {
5825 /* Since a template declaration already existed for this
5826 class-type, we must be redeclaring it here. Make sure
5827 that the redeclaration is valid. */
5828 redeclare_class_template (TREE_TYPE (decl),
5829 current_template_parms,
5830 current_template_constraints ());
5831 /* We don't need to create a new TEMPLATE_DECL; just use the
5832 one we already had. */
5833 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5834 }
5835 else
5836 {
5837 tmpl = build_template_decl (decl, current_template_parms,
5838 member_template_p);
5839 new_template_p = 1;
5840
5841 if (DECL_LANG_SPECIFIC (decl)
5842 && DECL_TEMPLATE_SPECIALIZATION (decl))
5843 {
5844 /* A specialization of a member template of a template
5845 class. */
5846 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5847 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5848 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5849 }
5850 }
5851 }
5852 else
5853 {
5854 tree a, t, current, parms;
5855 int i;
5856 tree tinfo = get_template_info (decl);
5857
5858 if (!tinfo)
5859 {
5860 error ("template definition of non-template %q#D", decl);
5861 return error_mark_node;
5862 }
5863
5864 tmpl = TI_TEMPLATE (tinfo);
5865
5866 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5867 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5868 && DECL_TEMPLATE_SPECIALIZATION (decl)
5869 && DECL_MEMBER_TEMPLATE_P (tmpl))
5870 {
5871 tree new_tmpl;
5872
5873 /* The declaration is a specialization of a member
5874 template, declared outside the class. Therefore, the
5875 innermost template arguments will be NULL, so we
5876 replace them with the arguments determined by the
5877 earlier call to check_explicit_specialization. */
5878 args = DECL_TI_ARGS (decl);
5879
5880 new_tmpl
5881 = build_template_decl (decl, current_template_parms,
5882 member_template_p);
5883 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5884 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5885 DECL_TI_TEMPLATE (decl) = new_tmpl;
5886 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5887 DECL_TEMPLATE_INFO (new_tmpl)
5888 = build_template_info (tmpl, args);
5889
5890 register_specialization (new_tmpl,
5891 most_general_template (tmpl),
5892 args,
5893 is_friend, 0);
5894 return decl;
5895 }
5896
5897 /* Make sure the template headers we got make sense. */
5898
5899 parms = DECL_TEMPLATE_PARMS (tmpl);
5900 i = TMPL_PARMS_DEPTH (parms);
5901 if (TMPL_ARGS_DEPTH (args) != i)
5902 {
5903 error ("expected %d levels of template parms for %q#D, got %d",
5904 i, decl, TMPL_ARGS_DEPTH (args));
5905 DECL_INTERFACE_KNOWN (decl) = 1;
5906 return error_mark_node;
5907 }
5908 else
5909 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5910 {
5911 a = TMPL_ARGS_LEVEL (args, i);
5912 t = INNERMOST_TEMPLATE_PARMS (parms);
5913
5914 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5915 {
5916 if (current == decl)
5917 error ("got %d template parameters for %q#D",
5918 TREE_VEC_LENGTH (a), decl);
5919 else
5920 error ("got %d template parameters for %q#T",
5921 TREE_VEC_LENGTH (a), current);
5922 error (" but %d required", TREE_VEC_LENGTH (t));
5923 /* Avoid crash in import_export_decl. */
5924 DECL_INTERFACE_KNOWN (decl) = 1;
5925 return error_mark_node;
5926 }
5927
5928 if (current == decl)
5929 current = ctx;
5930 else if (current == NULL_TREE)
5931 /* Can happen in erroneous input. */
5932 break;
5933 else
5934 current = get_containing_scope (current);
5935 }
5936
5937 /* Check that the parms are used in the appropriate qualifying scopes
5938 in the declarator. */
5939 if (!comp_template_args
5940 (TI_ARGS (tinfo),
5941 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5942 {
5943 error ("template arguments to %qD do not match original "
5944 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5945 if (!uses_template_parms (TI_ARGS (tinfo)))
5946 inform (input_location, "use %<template<>%> for"
5947 " an explicit specialization");
5948 /* Avoid crash in import_export_decl. */
5949 DECL_INTERFACE_KNOWN (decl) = 1;
5950 return error_mark_node;
5951 }
5952 }
5953
5954 DECL_TEMPLATE_RESULT (tmpl) = decl;
5955 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5956
5957 /* Push template declarations for global functions and types. Note
5958 that we do not try to push a global template friend declared in a
5959 template class; such a thing may well depend on the template
5960 parameters of the class. */
5961 if (new_template_p && !ctx
5962 && !(is_friend && template_class_depth (current_class_type) > 0))
5963 {
5964 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5965 if (tmpl == error_mark_node)
5966 return error_mark_node;
5967
5968 /* Hide template friend classes that haven't been declared yet. */
5969 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5970 {
5971 DECL_ANTICIPATED (tmpl) = 1;
5972 DECL_FRIEND_P (tmpl) = 1;
5973 }
5974 }
5975
5976 if (is_primary)
5977 {
5978 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5979
5980 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5981
5982 /* Give template template parms a DECL_CONTEXT of the template
5983 for which they are a parameter. */
5984 parms = INNERMOST_TEMPLATE_PARMS (parms);
5985 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5986 {
5987 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5988 if (TREE_CODE (parm) == TEMPLATE_DECL)
5989 DECL_CONTEXT (parm) = tmpl;
5990 }
5991
5992 if (TREE_CODE (decl) == TYPE_DECL
5993 && TYPE_DECL_ALIAS_P (decl))
5994 {
5995 if (tree constr
5996 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
5997 {
5998 /* ??? Why don't we do this here for all templates? */
5999 constr = build_constraints (constr, NULL_TREE);
6000 set_constraints (decl, constr);
6001 }
6002 if (complex_alias_template_p (tmpl))
6003 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6004 }
6005 }
6006
6007 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
6008 back to its most general template. If TMPL is a specialization,
6009 ARGS may only have the innermost set of arguments. Add the missing
6010 argument levels if necessary. */
6011 if (DECL_TEMPLATE_INFO (tmpl))
6012 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6013
6014 info = build_template_info (tmpl, args);
6015
6016 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6017 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6018 else
6019 {
6020 if (is_primary)
6021 retrofit_lang_decl (decl);
6022 if (DECL_LANG_SPECIFIC (decl))
6023 DECL_TEMPLATE_INFO (decl) = info;
6024 }
6025
6026 if (flag_implicit_templates
6027 && !is_friend
6028 && TREE_PUBLIC (decl)
6029 && VAR_OR_FUNCTION_DECL_P (decl))
6030 /* Set DECL_COMDAT on template instantiations; if we force
6031 them to be emitted by explicit instantiation,
6032 mark_needed will tell cgraph to do the right thing. */
6033 DECL_COMDAT (decl) = true;
6034
6035 return DECL_TEMPLATE_RESULT (tmpl);
6036 }
6037
6038 tree
6039 push_template_decl (tree decl)
6040 {
6041 return push_template_decl_real (decl, false);
6042 }
6043
6044 /* FN is an inheriting constructor that inherits from the constructor
6045 template INHERITED; turn FN into a constructor template with a matching
6046 template header. */
6047
6048 tree
6049 add_inherited_template_parms (tree fn, tree inherited)
6050 {
6051 tree inner_parms
6052 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6053 inner_parms = copy_node (inner_parms);
6054 tree parms
6055 = tree_cons (size_int (processing_template_decl + 1),
6056 inner_parms, current_template_parms);
6057 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6058 tree args = template_parms_to_args (parms);
6059 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6060 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6061 DECL_TEMPLATE_RESULT (tmpl) = fn;
6062 DECL_ARTIFICIAL (tmpl) = true;
6063 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6064 return tmpl;
6065 }
6066
6067 /* Called when a class template TYPE is redeclared with the indicated
6068 template PARMS, e.g.:
6069
6070 template <class T> struct S;
6071 template <class T> struct S {}; */
6072
6073 bool
6074 redeclare_class_template (tree type, tree parms, tree cons)
6075 {
6076 tree tmpl;
6077 tree tmpl_parms;
6078 int i;
6079
6080 if (!TYPE_TEMPLATE_INFO (type))
6081 {
6082 error ("%qT is not a template type", type);
6083 return false;
6084 }
6085
6086 tmpl = TYPE_TI_TEMPLATE (type);
6087 if (!PRIMARY_TEMPLATE_P (tmpl))
6088 /* The type is nested in some template class. Nothing to worry
6089 about here; there are no new template parameters for the nested
6090 type. */
6091 return true;
6092
6093 if (!parms)
6094 {
6095 error ("template specifiers not specified in declaration of %qD",
6096 tmpl);
6097 return false;
6098 }
6099
6100 parms = INNERMOST_TEMPLATE_PARMS (parms);
6101 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6102
6103 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6104 {
6105 error_n (input_location, TREE_VEC_LENGTH (parms),
6106 "redeclared with %d template parameter",
6107 "redeclared with %d template parameters",
6108 TREE_VEC_LENGTH (parms));
6109 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6110 "previous declaration %qD used %d template parameter",
6111 "previous declaration %qD used %d template parameters",
6112 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6113 return false;
6114 }
6115
6116 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6117 {
6118 tree tmpl_parm;
6119 tree parm;
6120 tree tmpl_default;
6121 tree parm_default;
6122
6123 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6124 || TREE_VEC_ELT (parms, i) == error_mark_node)
6125 continue;
6126
6127 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6128 if (error_operand_p (tmpl_parm))
6129 return false;
6130
6131 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6132 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6133 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6134
6135 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6136 TEMPLATE_DECL. */
6137 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6138 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6139 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6140 || (TREE_CODE (tmpl_parm) != PARM_DECL
6141 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6142 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6143 || (TREE_CODE (tmpl_parm) == PARM_DECL
6144 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6145 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6146 {
6147 error ("template parameter %q+#D", tmpl_parm);
6148 error ("redeclared here as %q#D", parm);
6149 return false;
6150 }
6151
6152 /* The parameters can be declared to introduce different
6153 constraints. */
6154 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6155 tree p2 = TREE_VEC_ELT (parms, i);
6156 if (!template_parameter_constraints_equivalent_p (p1, p2))
6157 {
6158 error ("declaration of template parameter %q+#D with different "
6159 "constraints", parm);
6160 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6161 "original declaration appeared here");
6162 return false;
6163 }
6164
6165 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6166 {
6167 /* We have in [temp.param]:
6168
6169 A template-parameter may not be given default arguments
6170 by two different declarations in the same scope. */
6171 error_at (input_location, "redefinition of default argument for %q#D", parm);
6172 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6173 "original definition appeared here");
6174 return false;
6175 }
6176
6177 if (parm_default != NULL_TREE)
6178 /* Update the previous template parameters (which are the ones
6179 that will really count) with the new default value. */
6180 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6181 else if (tmpl_default != NULL_TREE)
6182 /* Update the new parameters, too; they'll be used as the
6183 parameters for any members. */
6184 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6185
6186 /* Give each template template parm in this redeclaration a
6187 DECL_CONTEXT of the template for which they are a parameter. */
6188 if (TREE_CODE (parm) == TEMPLATE_DECL)
6189 {
6190 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6191 DECL_CONTEXT (parm) = tmpl;
6192 }
6193
6194 if (TREE_CODE (parm) == TYPE_DECL)
6195 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6196 }
6197
6198 tree ci = get_constraints (tmpl);
6199 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6200 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6201
6202 /* Two classes with different constraints declare different entities. */
6203 if (!cp_tree_equal (req1, req2))
6204 {
6205 error_at (input_location, "redeclaration %q#D with different "
6206 "constraints", tmpl);
6207 inform (DECL_SOURCE_LOCATION (tmpl),
6208 "original declaration appeared here");
6209 return false;
6210 }
6211
6212 return true;
6213 }
6214
6215 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6216 to be used when the caller has already checked
6217 (processing_template_decl
6218 && !instantiation_dependent_expression_p (expr)
6219 && potential_constant_expression (expr))
6220 and cleared processing_template_decl. */
6221
6222 tree
6223 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6224 {
6225 return tsubst_copy_and_build (expr,
6226 /*args=*/NULL_TREE,
6227 complain,
6228 /*in_decl=*/NULL_TREE,
6229 /*function_p=*/false,
6230 /*integral_constant_expression_p=*/true);
6231 }
6232
6233 /* Simplify EXPR if it is a non-dependent expression. Returns the
6234 (possibly simplified) expression. */
6235
6236 tree
6237 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6238 {
6239 if (expr == NULL_TREE)
6240 return NULL_TREE;
6241
6242 /* If we're in a template, but EXPR isn't value dependent, simplify
6243 it. We're supposed to treat:
6244
6245 template <typename T> void f(T[1 + 1]);
6246 template <typename T> void f(T[2]);
6247
6248 as two declarations of the same function, for example. */
6249 if (processing_template_decl
6250 && is_nondependent_constant_expression (expr))
6251 {
6252 processing_template_decl_sentinel s;
6253 expr = instantiate_non_dependent_expr_internal (expr, complain);
6254 }
6255 return expr;
6256 }
6257
6258 tree
6259 instantiate_non_dependent_expr (tree expr)
6260 {
6261 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6262 }
6263
6264 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6265 an uninstantiated expression. */
6266
6267 tree
6268 instantiate_non_dependent_or_null (tree expr)
6269 {
6270 if (expr == NULL_TREE)
6271 return NULL_TREE;
6272 if (processing_template_decl)
6273 {
6274 if (!is_nondependent_constant_expression (expr))
6275 expr = NULL_TREE;
6276 else
6277 {
6278 processing_template_decl_sentinel s;
6279 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6280 }
6281 }
6282 return expr;
6283 }
6284
6285 /* True iff T is a specialization of a variable template. */
6286
6287 bool
6288 variable_template_specialization_p (tree t)
6289 {
6290 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6291 return false;
6292 tree tmpl = DECL_TI_TEMPLATE (t);
6293 return variable_template_p (tmpl);
6294 }
6295
6296 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6297 template declaration, or a TYPE_DECL for an alias declaration. */
6298
6299 bool
6300 alias_type_or_template_p (tree t)
6301 {
6302 if (t == NULL_TREE)
6303 return false;
6304 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6305 || (TYPE_P (t)
6306 && TYPE_NAME (t)
6307 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6308 || DECL_ALIAS_TEMPLATE_P (t));
6309 }
6310
6311 /* If T is a specialization of an alias template, return it; otherwise return
6312 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6313
6314 tree
6315 alias_template_specialization_p (const_tree t,
6316 bool transparent_typedefs)
6317 {
6318 if (!TYPE_P (t))
6319 return NULL_TREE;
6320
6321 /* It's an alias template specialization if it's an alias and its
6322 TYPE_NAME is a specialization of a primary template. */
6323 if (typedef_variant_p (t))
6324 {
6325 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6326 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6327 return CONST_CAST_TREE (t);
6328 if (transparent_typedefs)
6329 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6330 (TYPE_NAME (t)),
6331 transparent_typedefs);
6332 }
6333
6334 return NULL_TREE;
6335 }
6336
6337 /* An alias template is complex from a SFINAE perspective if a template-id
6338 using that alias can be ill-formed when the expansion is not, as with
6339 the void_t template. We determine this by checking whether the
6340 expansion for the alias template uses all its template parameters. */
6341
6342 struct uses_all_template_parms_data
6343 {
6344 int level;
6345 bool *seen;
6346 };
6347
6348 static int
6349 uses_all_template_parms_r (tree t, void *data_)
6350 {
6351 struct uses_all_template_parms_data &data
6352 = *(struct uses_all_template_parms_data*)data_;
6353 tree idx = get_template_parm_index (t);
6354
6355 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6356 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6357 return 0;
6358 }
6359
6360 static bool
6361 complex_alias_template_p (const_tree tmpl)
6362 {
6363 /* A renaming alias isn't complex. */
6364 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6365 return false;
6366
6367 /* Any other constrained alias is complex. */
6368 if (get_constraints (tmpl))
6369 return true;
6370
6371 struct uses_all_template_parms_data data;
6372 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6373 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6374 data.level = TMPL_PARMS_DEPTH (parms);
6375 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6376 data.seen = XALLOCAVEC (bool, len);
6377 for (int i = 0; i < len; ++i)
6378 data.seen[i] = false;
6379
6380 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6381 for (int i = 0; i < len; ++i)
6382 if (!data.seen[i])
6383 return true;
6384 return false;
6385 }
6386
6387 /* If T is a specialization of a complex alias template with dependent
6388 template-arguments, return it; otherwise return NULL_TREE. If T is a
6389 typedef to such a specialization, return the specialization. */
6390
6391 tree
6392 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6393 {
6394 if (!TYPE_P (t) || !typedef_variant_p (t))
6395 return NULL_TREE;
6396
6397 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6398 if (tinfo
6399 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6400 && (any_dependent_template_arguments_p
6401 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6402 return CONST_CAST_TREE (t);
6403
6404 if (transparent_typedefs)
6405 {
6406 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6407 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6408 }
6409
6410 return NULL_TREE;
6411 }
6412
6413 /* Return the number of innermost template parameters in TMPL. */
6414
6415 static int
6416 num_innermost_template_parms (const_tree tmpl)
6417 {
6418 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6419 return TREE_VEC_LENGTH (parms);
6420 }
6421
6422 /* Return either TMPL or another template that it is equivalent to under DR
6423 1286: An alias that just changes the name of a template is equivalent to
6424 the other template. */
6425
6426 static tree
6427 get_underlying_template (tree tmpl)
6428 {
6429 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6430 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6431 {
6432 /* Determine if the alias is equivalent to an underlying template. */
6433 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6434 /* The underlying type may have been ill-formed. Don't proceed. */
6435 if (!orig_type)
6436 break;
6437 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6438 if (!tinfo)
6439 break;
6440
6441 tree underlying = TI_TEMPLATE (tinfo);
6442 if (!PRIMARY_TEMPLATE_P (underlying)
6443 || (num_innermost_template_parms (tmpl)
6444 != num_innermost_template_parms (underlying)))
6445 break;
6446
6447 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6448 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6449 break;
6450
6451 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6452 it's appropriate to treat a less-constrained alias as equivalent. */
6453 if (!at_least_as_constrained (underlying, tmpl))
6454 break;
6455
6456 /* Alias is equivalent. Strip it and repeat. */
6457 tmpl = underlying;
6458 }
6459
6460 return tmpl;
6461 }
6462
6463 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6464 must be a reference-to-function or a pointer-to-function type, as specified
6465 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6466 and check that the resulting function has external linkage. */
6467
6468 static tree
6469 convert_nontype_argument_function (tree type, tree expr,
6470 tsubst_flags_t complain)
6471 {
6472 tree fns = expr;
6473 tree fn, fn_no_ptr;
6474 linkage_kind linkage;
6475
6476 fn = instantiate_type (type, fns, tf_none);
6477 if (fn == error_mark_node)
6478 return error_mark_node;
6479
6480 if (value_dependent_expression_p (fn))
6481 goto accept;
6482
6483 fn_no_ptr = strip_fnptr_conv (fn);
6484 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6485 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6486 if (BASELINK_P (fn_no_ptr))
6487 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6488
6489 /* [temp.arg.nontype]/1
6490
6491 A template-argument for a non-type, non-template template-parameter
6492 shall be one of:
6493 [...]
6494 -- the address of an object or function with external [C++11: or
6495 internal] linkage. */
6496
6497 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6498 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6499 {
6500 if (complain & tf_error)
6501 {
6502 location_t loc = cp_expr_loc_or_input_loc (expr);
6503 error_at (loc, "%qE is not a valid template argument for type %qT",
6504 expr, type);
6505 if (TYPE_PTR_P (type))
6506 inform (loc, "it must be the address of a function "
6507 "with external linkage");
6508 else
6509 inform (loc, "it must be the name of a function with "
6510 "external linkage");
6511 }
6512 return NULL_TREE;
6513 }
6514
6515 linkage = decl_linkage (fn_no_ptr);
6516 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6517 {
6518 if (complain & tf_error)
6519 {
6520 location_t loc = cp_expr_loc_or_input_loc (expr);
6521 if (cxx_dialect >= cxx11)
6522 error_at (loc, "%qE is not a valid template argument for type "
6523 "%qT because %qD has no linkage",
6524 expr, type, fn_no_ptr);
6525 else
6526 error_at (loc, "%qE is not a valid template argument for type "
6527 "%qT because %qD does not have external linkage",
6528 expr, type, fn_no_ptr);
6529 }
6530 return NULL_TREE;
6531 }
6532
6533 accept:
6534 if (TYPE_REF_P (type))
6535 {
6536 if (REFERENCE_REF_P (fn))
6537 fn = TREE_OPERAND (fn, 0);
6538 else
6539 fn = build_address (fn);
6540 }
6541 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6542 fn = build_nop (type, fn);
6543
6544 return fn;
6545 }
6546
6547 /* Subroutine of convert_nontype_argument.
6548 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6549 Emit an error otherwise. */
6550
6551 static bool
6552 check_valid_ptrmem_cst_expr (tree type, tree expr,
6553 tsubst_flags_t complain)
6554 {
6555 tree orig_expr = expr;
6556 STRIP_NOPS (expr);
6557 if (null_ptr_cst_p (expr))
6558 return true;
6559 if (TREE_CODE (expr) == PTRMEM_CST
6560 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6561 PTRMEM_CST_CLASS (expr)))
6562 return true;
6563 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6564 return true;
6565 if (processing_template_decl
6566 && TREE_CODE (expr) == ADDR_EXPR
6567 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6568 return true;
6569 if (complain & tf_error)
6570 {
6571 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6572 error_at (loc, "%qE is not a valid template argument for type %qT",
6573 orig_expr, type);
6574 if (TREE_CODE (expr) != PTRMEM_CST)
6575 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6576 else
6577 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6578 }
6579 return false;
6580 }
6581
6582 /* Returns TRUE iff the address of OP is value-dependent.
6583
6584 14.6.2.4 [temp.dep.temp]:
6585 A non-integral non-type template-argument is dependent if its type is
6586 dependent or it has either of the following forms
6587 qualified-id
6588 & qualified-id
6589 and contains a nested-name-specifier which specifies a class-name that
6590 names a dependent type.
6591
6592 We generalize this to just say that the address of a member of a
6593 dependent class is value-dependent; the above doesn't cover the
6594 address of a static data member named with an unqualified-id. */
6595
6596 static bool
6597 has_value_dependent_address (tree op)
6598 {
6599 STRIP_ANY_LOCATION_WRAPPER (op);
6600
6601 /* We could use get_inner_reference here, but there's no need;
6602 this is only relevant for template non-type arguments, which
6603 can only be expressed as &id-expression. */
6604 if (DECL_P (op))
6605 {
6606 tree ctx = CP_DECL_CONTEXT (op);
6607 if (TYPE_P (ctx) && dependent_type_p (ctx))
6608 return true;
6609 }
6610
6611 return false;
6612 }
6613
6614 /* The next set of functions are used for providing helpful explanatory
6615 diagnostics for failed overload resolution. Their messages should be
6616 indented by two spaces for consistency with the messages in
6617 call.c */
6618
6619 static int
6620 unify_success (bool /*explain_p*/)
6621 {
6622 return 0;
6623 }
6624
6625 /* Other failure functions should call this one, to provide a single function
6626 for setting a breakpoint on. */
6627
6628 static int
6629 unify_invalid (bool /*explain_p*/)
6630 {
6631 return 1;
6632 }
6633
6634 static int
6635 unify_parameter_deduction_failure (bool explain_p, tree parm)
6636 {
6637 if (explain_p)
6638 inform (input_location,
6639 " couldn%'t deduce template parameter %qD", parm);
6640 return unify_invalid (explain_p);
6641 }
6642
6643 static int
6644 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6645 {
6646 if (explain_p)
6647 inform (input_location,
6648 " types %qT and %qT have incompatible cv-qualifiers",
6649 parm, arg);
6650 return unify_invalid (explain_p);
6651 }
6652
6653 static int
6654 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6655 {
6656 if (explain_p)
6657 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6658 return unify_invalid (explain_p);
6659 }
6660
6661 static int
6662 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6663 {
6664 if (explain_p)
6665 inform (input_location,
6666 " template parameter %qD is not a parameter pack, but "
6667 "argument %qD is",
6668 parm, arg);
6669 return unify_invalid (explain_p);
6670 }
6671
6672 static int
6673 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6674 {
6675 if (explain_p)
6676 inform (input_location,
6677 " template argument %qE does not match "
6678 "pointer-to-member constant %qE",
6679 arg, parm);
6680 return unify_invalid (explain_p);
6681 }
6682
6683 static int
6684 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6685 {
6686 if (explain_p)
6687 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6688 return unify_invalid (explain_p);
6689 }
6690
6691 static int
6692 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6693 {
6694 if (explain_p)
6695 inform (input_location,
6696 " inconsistent parameter pack deduction with %qT and %qT",
6697 old_arg, new_arg);
6698 return unify_invalid (explain_p);
6699 }
6700
6701 static int
6702 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6703 {
6704 if (explain_p)
6705 {
6706 if (TYPE_P (parm))
6707 inform (input_location,
6708 " deduced conflicting types for parameter %qT (%qT and %qT)",
6709 parm, first, second);
6710 else
6711 inform (input_location,
6712 " deduced conflicting values for non-type parameter "
6713 "%qE (%qE and %qE)", parm, first, second);
6714 }
6715 return unify_invalid (explain_p);
6716 }
6717
6718 static int
6719 unify_vla_arg (bool explain_p, tree arg)
6720 {
6721 if (explain_p)
6722 inform (input_location,
6723 " variable-sized array type %qT is not "
6724 "a valid template argument",
6725 arg);
6726 return unify_invalid (explain_p);
6727 }
6728
6729 static int
6730 unify_method_type_error (bool explain_p, tree arg)
6731 {
6732 if (explain_p)
6733 inform (input_location,
6734 " member function type %qT is not a valid template argument",
6735 arg);
6736 return unify_invalid (explain_p);
6737 }
6738
6739 static int
6740 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6741 {
6742 if (explain_p)
6743 {
6744 if (least_p)
6745 inform_n (input_location, wanted,
6746 " candidate expects at least %d argument, %d provided",
6747 " candidate expects at least %d arguments, %d provided",
6748 wanted, have);
6749 else
6750 inform_n (input_location, wanted,
6751 " candidate expects %d argument, %d provided",
6752 " candidate expects %d arguments, %d provided",
6753 wanted, have);
6754 }
6755 return unify_invalid (explain_p);
6756 }
6757
6758 static int
6759 unify_too_many_arguments (bool explain_p, int have, int wanted)
6760 {
6761 return unify_arity (explain_p, have, wanted);
6762 }
6763
6764 static int
6765 unify_too_few_arguments (bool explain_p, int have, int wanted,
6766 bool least_p = false)
6767 {
6768 return unify_arity (explain_p, have, wanted, least_p);
6769 }
6770
6771 static int
6772 unify_arg_conversion (bool explain_p, tree to_type,
6773 tree from_type, tree arg)
6774 {
6775 if (explain_p)
6776 inform (cp_expr_loc_or_input_loc (arg),
6777 " cannot convert %qE (type %qT) to type %qT",
6778 arg, from_type, to_type);
6779 return unify_invalid (explain_p);
6780 }
6781
6782 static int
6783 unify_no_common_base (bool explain_p, enum template_base_result r,
6784 tree parm, tree arg)
6785 {
6786 if (explain_p)
6787 switch (r)
6788 {
6789 case tbr_ambiguous_baseclass:
6790 inform (input_location, " %qT is an ambiguous base class of %qT",
6791 parm, arg);
6792 break;
6793 default:
6794 inform (input_location, " %qT is not derived from %qT", arg, parm);
6795 break;
6796 }
6797 return unify_invalid (explain_p);
6798 }
6799
6800 static int
6801 unify_inconsistent_template_template_parameters (bool explain_p)
6802 {
6803 if (explain_p)
6804 inform (input_location,
6805 " template parameters of a template template argument are "
6806 "inconsistent with other deduced template arguments");
6807 return unify_invalid (explain_p);
6808 }
6809
6810 static int
6811 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6812 {
6813 if (explain_p)
6814 inform (input_location,
6815 " cannot deduce a template for %qT from non-template type %qT",
6816 parm, arg);
6817 return unify_invalid (explain_p);
6818 }
6819
6820 static int
6821 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6822 {
6823 if (explain_p)
6824 inform (input_location,
6825 " template argument %qE does not match %qE", arg, parm);
6826 return unify_invalid (explain_p);
6827 }
6828
6829 /* True if T is a C++20 template parameter object to store the argument for a
6830 template parameter of class type. */
6831
6832 bool
6833 template_parm_object_p (const_tree t)
6834 {
6835 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6836 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6837 }
6838
6839 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6840 argument for TYPE, points to an unsuitable object. */
6841
6842 static bool
6843 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6844 {
6845 switch (TREE_CODE (expr))
6846 {
6847 CASE_CONVERT:
6848 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6849 complain);
6850
6851 case TARGET_EXPR:
6852 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6853 complain);
6854
6855 case CONSTRUCTOR:
6856 {
6857 unsigned i; tree elt;
6858 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6859 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6860 return true;
6861 }
6862 break;
6863
6864 case ADDR_EXPR:
6865 {
6866 tree decl = TREE_OPERAND (expr, 0);
6867
6868 if (!VAR_P (decl))
6869 {
6870 if (complain & tf_error)
6871 error_at (cp_expr_loc_or_input_loc (expr),
6872 "%qE is not a valid template argument of type %qT "
6873 "because %qE is not a variable", expr, type, decl);
6874 return true;
6875 }
6876 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6877 {
6878 if (complain & tf_error)
6879 error_at (cp_expr_loc_or_input_loc (expr),
6880 "%qE is not a valid template argument of type %qT "
6881 "in C++98 because %qD does not have external linkage",
6882 expr, type, decl);
6883 return true;
6884 }
6885 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6886 && decl_linkage (decl) == lk_none)
6887 {
6888 if (complain & tf_error)
6889 error_at (cp_expr_loc_or_input_loc (expr),
6890 "%qE is not a valid template argument of type %qT "
6891 "because %qD has no linkage", expr, type, decl);
6892 return true;
6893 }
6894 /* C++17: For a non-type template-parameter of reference or pointer
6895 type, the value of the constant expression shall not refer to (or
6896 for a pointer type, shall not be the address of):
6897 * a subobject (4.5),
6898 * a temporary object (15.2),
6899 * a string literal (5.13.5),
6900 * the result of a typeid expression (8.2.8), or
6901 * a predefined __func__ variable (11.4.1). */
6902 else if (DECL_ARTIFICIAL (decl))
6903 {
6904 if (complain & tf_error)
6905 error ("the address of %qD is not a valid template argument",
6906 decl);
6907 return true;
6908 }
6909 else if (!same_type_ignoring_top_level_qualifiers_p
6910 (strip_array_types (TREE_TYPE (type)),
6911 strip_array_types (TREE_TYPE (decl))))
6912 {
6913 if (complain & tf_error)
6914 error ("the address of the %qT subobject of %qD is not a "
6915 "valid template argument", TREE_TYPE (type), decl);
6916 return true;
6917 }
6918 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6919 {
6920 if (complain & tf_error)
6921 error ("the address of %qD is not a valid template argument "
6922 "because it does not have static storage duration",
6923 decl);
6924 return true;
6925 }
6926 }
6927 break;
6928
6929 default:
6930 if (!INDIRECT_TYPE_P (type))
6931 /* We're only concerned about pointers and references here. */;
6932 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6933 /* Null pointer values are OK in C++11. */;
6934 else
6935 {
6936 if (VAR_P (expr))
6937 {
6938 if (complain & tf_error)
6939 error ("%qD is not a valid template argument "
6940 "because %qD is a variable, not the address of "
6941 "a variable", expr, expr);
6942 return true;
6943 }
6944 else
6945 {
6946 if (complain & tf_error)
6947 error ("%qE is not a valid template argument for %qT "
6948 "because it is not the address of a variable",
6949 expr, type);
6950 return true;
6951 }
6952 }
6953 }
6954 return false;
6955
6956 }
6957
6958 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6959 template argument EXPR. */
6960
6961 static tree
6962 get_template_parm_object (tree expr, tsubst_flags_t complain)
6963 {
6964 if (TREE_CODE (expr) == TARGET_EXPR)
6965 expr = TARGET_EXPR_INITIAL (expr);
6966
6967 if (!TREE_CONSTANT (expr))
6968 {
6969 if ((complain & tf_error)
6970 && require_rvalue_constant_expression (expr))
6971 cxx_constant_value (expr);
6972 return error_mark_node;
6973 }
6974 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6975 return error_mark_node;
6976
6977 tree name = mangle_template_parm_object (expr);
6978 tree decl = get_global_binding (name);
6979 if (decl)
6980 return decl;
6981
6982 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6983 decl = create_temporary_var (type);
6984 TREE_STATIC (decl) = true;
6985 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6986 TREE_READONLY (decl) = true;
6987 DECL_NAME (decl) = name;
6988 SET_DECL_ASSEMBLER_NAME (decl, name);
6989 DECL_CONTEXT (decl) = global_namespace;
6990 comdat_linkage (decl);
6991 pushdecl_top_level_and_finish (decl, expr);
6992 return decl;
6993 }
6994
6995 /* Attempt to convert the non-type template parameter EXPR to the
6996 indicated TYPE. If the conversion is successful, return the
6997 converted value. If the conversion is unsuccessful, return
6998 NULL_TREE if we issued an error message, or error_mark_node if we
6999 did not. We issue error messages for out-and-out bad template
7000 parameters, but not simply because the conversion failed, since we
7001 might be just trying to do argument deduction. Both TYPE and EXPR
7002 must be non-dependent.
7003
7004 The conversion follows the special rules described in
7005 [temp.arg.nontype], and it is much more strict than an implicit
7006 conversion.
7007
7008 This function is called twice for each template argument (see
7009 lookup_template_class for a more accurate description of this
7010 problem). This means that we need to handle expressions which
7011 are not valid in a C++ source, but can be created from the
7012 first call (for instance, casts to perform conversions). These
7013 hacks can go away after we fix the double coercion problem. */
7014
7015 static tree
7016 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7017 {
7018 tree expr_type;
7019 location_t loc = cp_expr_loc_or_input_loc (expr);
7020
7021 /* Detect immediately string literals as invalid non-type argument.
7022 This special-case is not needed for correctness (we would easily
7023 catch this later), but only to provide better diagnostic for this
7024 common user mistake. As suggested by DR 100, we do not mention
7025 linkage issues in the diagnostic as this is not the point. */
7026 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7027 {
7028 if (complain & tf_error)
7029 error ("%qE is not a valid template argument for type %qT "
7030 "because string literals can never be used in this context",
7031 expr, type);
7032 return NULL_TREE;
7033 }
7034
7035 /* Add the ADDR_EXPR now for the benefit of
7036 value_dependent_expression_p. */
7037 if (TYPE_PTROBV_P (type)
7038 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7039 {
7040 expr = decay_conversion (expr, complain);
7041 if (expr == error_mark_node)
7042 return error_mark_node;
7043 }
7044
7045 /* If we are in a template, EXPR may be non-dependent, but still
7046 have a syntactic, rather than semantic, form. For example, EXPR
7047 might be a SCOPE_REF, rather than the VAR_DECL to which the
7048 SCOPE_REF refers. Preserving the qualifying scope is necessary
7049 so that access checking can be performed when the template is
7050 instantiated -- but here we need the resolved form so that we can
7051 convert the argument. */
7052 bool non_dep = false;
7053 if (TYPE_REF_OBJ_P (type)
7054 && has_value_dependent_address (expr))
7055 /* If we want the address and it's value-dependent, don't fold. */;
7056 else if (processing_template_decl
7057 && is_nondependent_constant_expression (expr))
7058 non_dep = true;
7059 if (error_operand_p (expr))
7060 return error_mark_node;
7061 expr_type = TREE_TYPE (expr);
7062
7063 /* If the argument is non-dependent, perform any conversions in
7064 non-dependent context as well. */
7065 processing_template_decl_sentinel s (non_dep);
7066 if (non_dep)
7067 expr = instantiate_non_dependent_expr_internal (expr, complain);
7068
7069 if (value_dependent_expression_p (expr))
7070 expr = canonicalize_expr_argument (expr, complain);
7071
7072 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7073 to a non-type argument of "nullptr". */
7074 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7075 expr = fold_simple (convert (type, expr));
7076
7077 /* In C++11, integral or enumeration non-type template arguments can be
7078 arbitrary constant expressions. Pointer and pointer to
7079 member arguments can be general constant expressions that evaluate
7080 to a null value, but otherwise still need to be of a specific form. */
7081 if (cxx_dialect >= cxx11)
7082 {
7083 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7084 /* A PTRMEM_CST is already constant, and a valid template
7085 argument for a parameter of pointer to member type, we just want
7086 to leave it in that form rather than lower it to a
7087 CONSTRUCTOR. */;
7088 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7089 || cxx_dialect >= cxx17)
7090 {
7091 /* Calling build_converted_constant_expr might create a call to
7092 a conversion function with a value-dependent argument, which
7093 could invoke taking the address of a temporary representing
7094 the result of the conversion. */
7095 if (COMPOUND_LITERAL_P (expr)
7096 && CONSTRUCTOR_IS_DEPENDENT (expr)
7097 && MAYBE_CLASS_TYPE_P (expr_type)
7098 && TYPE_HAS_CONVERSION (expr_type))
7099 {
7100 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
7101 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7102 return expr;
7103 }
7104 /* C++17: A template-argument for a non-type template-parameter shall
7105 be a converted constant expression (8.20) of the type of the
7106 template-parameter. */
7107 expr = build_converted_constant_expr (type, expr, complain);
7108 if (expr == error_mark_node)
7109 /* Make sure we return NULL_TREE only if we have really issued
7110 an error, as described above. */
7111 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7112 expr = maybe_constant_value (expr, NULL_TREE,
7113 /*manifestly_const_eval=*/true);
7114 expr = convert_from_reference (expr);
7115 }
7116 else if (TYPE_PTR_OR_PTRMEM_P (type))
7117 {
7118 tree folded = maybe_constant_value (expr, NULL_TREE,
7119 /*manifestly_const_eval=*/true);
7120 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7121 : null_member_pointer_value_p (folded))
7122 expr = folded;
7123 }
7124 }
7125
7126 if (TYPE_REF_P (type))
7127 expr = mark_lvalue_use (expr);
7128 else
7129 expr = mark_rvalue_use (expr);
7130
7131 /* HACK: Due to double coercion, we can get a
7132 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7133 which is the tree that we built on the first call (see
7134 below when coercing to reference to object or to reference to
7135 function). We just strip everything and get to the arg.
7136 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7137 for examples. */
7138 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7139 {
7140 tree probe_type, probe = expr;
7141 if (REFERENCE_REF_P (probe))
7142 probe = TREE_OPERAND (probe, 0);
7143 probe_type = TREE_TYPE (probe);
7144 if (TREE_CODE (probe) == NOP_EXPR)
7145 {
7146 /* ??? Maybe we could use convert_from_reference here, but we
7147 would need to relax its constraints because the NOP_EXPR
7148 could actually change the type to something more cv-qualified,
7149 and this is not folded by convert_from_reference. */
7150 tree addr = TREE_OPERAND (probe, 0);
7151 if (TYPE_REF_P (probe_type)
7152 && TREE_CODE (addr) == ADDR_EXPR
7153 && TYPE_PTR_P (TREE_TYPE (addr))
7154 && (same_type_ignoring_top_level_qualifiers_p
7155 (TREE_TYPE (probe_type),
7156 TREE_TYPE (TREE_TYPE (addr)))))
7157 {
7158 expr = TREE_OPERAND (addr, 0);
7159 expr_type = TREE_TYPE (probe_type);
7160 }
7161 }
7162 }
7163
7164 /* [temp.arg.nontype]/5, bullet 1
7165
7166 For a non-type template-parameter of integral or enumeration type,
7167 integral promotions (_conv.prom_) and integral conversions
7168 (_conv.integral_) are applied. */
7169 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7170 {
7171 if (cxx_dialect < cxx11)
7172 {
7173 tree t = build_converted_constant_expr (type, expr, complain);
7174 t = maybe_constant_value (t);
7175 if (t != error_mark_node)
7176 expr = t;
7177 }
7178
7179 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7180 return error_mark_node;
7181
7182 /* Notice that there are constant expressions like '4 % 0' which
7183 do not fold into integer constants. */
7184 if (TREE_CODE (expr) != INTEGER_CST
7185 && !value_dependent_expression_p (expr))
7186 {
7187 if (complain & tf_error)
7188 {
7189 int errs = errorcount, warns = warningcount + werrorcount;
7190 if (!require_potential_constant_expression (expr))
7191 expr = error_mark_node;
7192 else
7193 expr = cxx_constant_value (expr);
7194 if (errorcount > errs || warningcount + werrorcount > warns)
7195 inform (loc, "in template argument for type %qT", type);
7196 if (expr == error_mark_node)
7197 return NULL_TREE;
7198 /* else cxx_constant_value complained but gave us
7199 a real constant, so go ahead. */
7200 if (TREE_CODE (expr) != INTEGER_CST)
7201 {
7202 /* Some assemble time constant expressions like
7203 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7204 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7205 as we can emit them into .rodata initializers of
7206 variables, yet they can't fold into an INTEGER_CST at
7207 compile time. Refuse them here. */
7208 gcc_checking_assert (reduced_constant_expression_p (expr));
7209 error_at (loc, "template argument %qE for type %qT not "
7210 "a constant integer", expr, type);
7211 return NULL_TREE;
7212 }
7213 }
7214 else
7215 return NULL_TREE;
7216 }
7217
7218 /* Avoid typedef problems. */
7219 if (TREE_TYPE (expr) != type)
7220 expr = fold_convert (type, expr);
7221 }
7222 /* [temp.arg.nontype]/5, bullet 2
7223
7224 For a non-type template-parameter of type pointer to object,
7225 qualification conversions (_conv.qual_) and the array-to-pointer
7226 conversion (_conv.array_) are applied. */
7227 else if (TYPE_PTROBV_P (type))
7228 {
7229 tree decayed = expr;
7230
7231 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7232 decay_conversion or an explicit cast. If it's a problematic cast,
7233 we'll complain about it below. */
7234 if (TREE_CODE (expr) == NOP_EXPR)
7235 {
7236 tree probe = expr;
7237 STRIP_NOPS (probe);
7238 if (TREE_CODE (probe) == ADDR_EXPR
7239 && TYPE_PTR_P (TREE_TYPE (probe)))
7240 {
7241 expr = probe;
7242 expr_type = TREE_TYPE (expr);
7243 }
7244 }
7245
7246 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7247
7248 A template-argument for a non-type, non-template template-parameter
7249 shall be one of: [...]
7250
7251 -- the name of a non-type template-parameter;
7252 -- the address of an object or function with external linkage, [...]
7253 expressed as "& id-expression" where the & is optional if the name
7254 refers to a function or array, or if the corresponding
7255 template-parameter is a reference.
7256
7257 Here, we do not care about functions, as they are invalid anyway
7258 for a parameter of type pointer-to-object. */
7259
7260 if (value_dependent_expression_p (expr))
7261 /* Non-type template parameters are OK. */
7262 ;
7263 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7264 /* Null pointer values are OK in C++11. */;
7265 else if (TREE_CODE (expr) != ADDR_EXPR
7266 && !INDIRECT_TYPE_P (expr_type))
7267 /* Other values, like integer constants, might be valid
7268 non-type arguments of some other type. */
7269 return error_mark_node;
7270 else if (invalid_tparm_referent_p (type, expr, complain))
7271 return NULL_TREE;
7272
7273 expr = decayed;
7274
7275 expr = perform_qualification_conversions (type, expr);
7276 if (expr == error_mark_node)
7277 return error_mark_node;
7278 }
7279 /* [temp.arg.nontype]/5, bullet 3
7280
7281 For a non-type template-parameter of type reference to object, no
7282 conversions apply. The type referred to by the reference may be more
7283 cv-qualified than the (otherwise identical) type of the
7284 template-argument. The template-parameter is bound directly to the
7285 template-argument, which must be an lvalue. */
7286 else if (TYPE_REF_OBJ_P (type))
7287 {
7288 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7289 expr_type))
7290 return error_mark_node;
7291
7292 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7293 {
7294 if (complain & tf_error)
7295 error ("%qE is not a valid template argument for type %qT "
7296 "because of conflicts in cv-qualification", expr, type);
7297 return NULL_TREE;
7298 }
7299
7300 if (!lvalue_p (expr))
7301 {
7302 if (complain & tf_error)
7303 error ("%qE is not a valid template argument for type %qT "
7304 "because it is not an lvalue", expr, type);
7305 return NULL_TREE;
7306 }
7307
7308 /* [temp.arg.nontype]/1
7309
7310 A template-argument for a non-type, non-template template-parameter
7311 shall be one of: [...]
7312
7313 -- the address of an object or function with external linkage. */
7314 if (INDIRECT_REF_P (expr)
7315 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7316 {
7317 expr = TREE_OPERAND (expr, 0);
7318 if (DECL_P (expr))
7319 {
7320 if (complain & tf_error)
7321 error ("%q#D is not a valid template argument for type %qT "
7322 "because a reference variable does not have a constant "
7323 "address", expr, type);
7324 return NULL_TREE;
7325 }
7326 }
7327
7328 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7329 && value_dependent_expression_p (expr))
7330 /* OK, dependent reference. We don't want to ask whether a DECL is
7331 itself value-dependent, since what we want here is its address. */;
7332 else
7333 {
7334 expr = build_address (expr);
7335
7336 if (invalid_tparm_referent_p (type, expr, complain))
7337 return NULL_TREE;
7338 }
7339
7340 if (!same_type_p (type, TREE_TYPE (expr)))
7341 expr = build_nop (type, expr);
7342 }
7343 /* [temp.arg.nontype]/5, bullet 4
7344
7345 For a non-type template-parameter of type pointer to function, only
7346 the function-to-pointer conversion (_conv.func_) is applied. If the
7347 template-argument represents a set of overloaded functions (or a
7348 pointer to such), the matching function is selected from the set
7349 (_over.over_). */
7350 else if (TYPE_PTRFN_P (type))
7351 {
7352 /* If the argument is a template-id, we might not have enough
7353 context information to decay the pointer. */
7354 if (!type_unknown_p (expr_type))
7355 {
7356 expr = decay_conversion (expr, complain);
7357 if (expr == error_mark_node)
7358 return error_mark_node;
7359 }
7360
7361 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7362 /* Null pointer values are OK in C++11. */
7363 return perform_qualification_conversions (type, expr);
7364
7365 expr = convert_nontype_argument_function (type, expr, complain);
7366 if (!expr || expr == error_mark_node)
7367 return expr;
7368 }
7369 /* [temp.arg.nontype]/5, bullet 5
7370
7371 For a non-type template-parameter of type reference to function, no
7372 conversions apply. If the template-argument represents a set of
7373 overloaded functions, the matching function is selected from the set
7374 (_over.over_). */
7375 else if (TYPE_REFFN_P (type))
7376 {
7377 if (TREE_CODE (expr) == ADDR_EXPR)
7378 {
7379 if (complain & tf_error)
7380 {
7381 error ("%qE is not a valid template argument for type %qT "
7382 "because it is a pointer", expr, type);
7383 inform (input_location, "try using %qE instead",
7384 TREE_OPERAND (expr, 0));
7385 }
7386 return NULL_TREE;
7387 }
7388
7389 expr = convert_nontype_argument_function (type, expr, complain);
7390 if (!expr || expr == error_mark_node)
7391 return expr;
7392 }
7393 /* [temp.arg.nontype]/5, bullet 6
7394
7395 For a non-type template-parameter of type pointer to member function,
7396 no conversions apply. If the template-argument represents a set of
7397 overloaded member functions, the matching member function is selected
7398 from the set (_over.over_). */
7399 else if (TYPE_PTRMEMFUNC_P (type))
7400 {
7401 expr = instantiate_type (type, expr, tf_none);
7402 if (expr == error_mark_node)
7403 return error_mark_node;
7404
7405 /* [temp.arg.nontype] bullet 1 says the pointer to member
7406 expression must be a pointer-to-member constant. */
7407 if (!value_dependent_expression_p (expr)
7408 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7409 return NULL_TREE;
7410
7411 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7412 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7413 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7414 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7415 }
7416 /* [temp.arg.nontype]/5, bullet 7
7417
7418 For a non-type template-parameter of type pointer to data member,
7419 qualification conversions (_conv.qual_) are applied. */
7420 else if (TYPE_PTRDATAMEM_P (type))
7421 {
7422 /* [temp.arg.nontype] bullet 1 says the pointer to member
7423 expression must be a pointer-to-member constant. */
7424 if (!value_dependent_expression_p (expr)
7425 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7426 return NULL_TREE;
7427
7428 expr = perform_qualification_conversions (type, expr);
7429 if (expr == error_mark_node)
7430 return expr;
7431 }
7432 else if (NULLPTR_TYPE_P (type))
7433 {
7434 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7435 {
7436 if (complain & tf_error)
7437 error ("%qE is not a valid template argument for type %qT "
7438 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7439 return NULL_TREE;
7440 }
7441 return expr;
7442 }
7443 else if (CLASS_TYPE_P (type))
7444 {
7445 /* Replace the argument with a reference to the corresponding template
7446 parameter object. */
7447 if (!value_dependent_expression_p (expr))
7448 expr = get_template_parm_object (expr, complain);
7449 if (expr == error_mark_node)
7450 return NULL_TREE;
7451 }
7452 /* A template non-type parameter must be one of the above. */
7453 else
7454 gcc_unreachable ();
7455
7456 /* Sanity check: did we actually convert the argument to the
7457 right type? */
7458 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7459 (type, TREE_TYPE (expr)));
7460 return convert_from_reference (expr);
7461 }
7462
7463 /* Subroutine of coerce_template_template_parms, which returns 1 if
7464 PARM_PARM and ARG_PARM match using the rule for the template
7465 parameters of template template parameters. Both PARM and ARG are
7466 template parameters; the rest of the arguments are the same as for
7467 coerce_template_template_parms.
7468 */
7469 static int
7470 coerce_template_template_parm (tree parm,
7471 tree arg,
7472 tsubst_flags_t complain,
7473 tree in_decl,
7474 tree outer_args)
7475 {
7476 if (arg == NULL_TREE || error_operand_p (arg)
7477 || parm == NULL_TREE || error_operand_p (parm))
7478 return 0;
7479
7480 if (TREE_CODE (arg) != TREE_CODE (parm))
7481 return 0;
7482
7483 switch (TREE_CODE (parm))
7484 {
7485 case TEMPLATE_DECL:
7486 /* We encounter instantiations of templates like
7487 template <template <template <class> class> class TT>
7488 class C; */
7489 {
7490 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7491 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7492
7493 if (!coerce_template_template_parms
7494 (parmparm, argparm, complain, in_decl, outer_args))
7495 return 0;
7496 }
7497 /* Fall through. */
7498
7499 case TYPE_DECL:
7500 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7501 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7502 /* Argument is a parameter pack but parameter is not. */
7503 return 0;
7504 break;
7505
7506 case PARM_DECL:
7507 /* The tsubst call is used to handle cases such as
7508
7509 template <int> class C {};
7510 template <class T, template <T> class TT> class D {};
7511 D<int, C> d;
7512
7513 i.e. the parameter list of TT depends on earlier parameters. */
7514 if (!uses_template_parms (TREE_TYPE (arg)))
7515 {
7516 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7517 if (!uses_template_parms (t)
7518 && !same_type_p (t, TREE_TYPE (arg)))
7519 return 0;
7520 }
7521
7522 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7523 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7524 /* Argument is a parameter pack but parameter is not. */
7525 return 0;
7526
7527 break;
7528
7529 default:
7530 gcc_unreachable ();
7531 }
7532
7533 return 1;
7534 }
7535
7536 /* Coerce template argument list ARGLIST for use with template
7537 template-parameter TEMPL. */
7538
7539 static tree
7540 coerce_template_args_for_ttp (tree templ, tree arglist,
7541 tsubst_flags_t complain)
7542 {
7543 /* Consider an example where a template template parameter declared as
7544
7545 template <class T, class U = std::allocator<T> > class TT
7546
7547 The template parameter level of T and U are one level larger than
7548 of TT. To proper process the default argument of U, say when an
7549 instantiation `TT<int>' is seen, we need to build the full
7550 arguments containing {int} as the innermost level. Outer levels,
7551 available when not appearing as default template argument, can be
7552 obtained from the arguments of the enclosing template.
7553
7554 Suppose that TT is later substituted with std::vector. The above
7555 instantiation is `TT<int, std::allocator<T> >' with TT at
7556 level 1, and T at level 2, while the template arguments at level 1
7557 becomes {std::vector} and the inner level 2 is {int}. */
7558
7559 tree outer = DECL_CONTEXT (templ);
7560 if (outer)
7561 outer = generic_targs_for (outer);
7562 else if (current_template_parms)
7563 {
7564 /* This is an argument of the current template, so we haven't set
7565 DECL_CONTEXT yet. */
7566 tree relevant_template_parms;
7567
7568 /* Parameter levels that are greater than the level of the given
7569 template template parm are irrelevant. */
7570 relevant_template_parms = current_template_parms;
7571 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7572 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7573 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7574
7575 outer = template_parms_to_args (relevant_template_parms);
7576 }
7577
7578 if (outer)
7579 arglist = add_to_template_args (outer, arglist);
7580
7581 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7582 return coerce_template_parms (parmlist, arglist, templ,
7583 complain,
7584 /*require_all_args=*/true,
7585 /*use_default_args=*/true);
7586 }
7587
7588 /* A cache of template template parameters with match-all default
7589 arguments. */
7590 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7591
7592 /* T is a bound template template-parameter. Copy its arguments into default
7593 arguments of the template template-parameter's template parameters. */
7594
7595 static tree
7596 add_defaults_to_ttp (tree otmpl)
7597 {
7598 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7599 return *c;
7600
7601 tree ntmpl = copy_node (otmpl);
7602
7603 tree ntype = copy_node (TREE_TYPE (otmpl));
7604 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7605 TYPE_MAIN_VARIANT (ntype) = ntype;
7606 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7607 TYPE_NAME (ntype) = ntmpl;
7608 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7609
7610 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7611 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7612 TEMPLATE_PARM_DECL (idx) = ntmpl;
7613 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7614
7615 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7616 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7617 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7618 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7619 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7620 {
7621 tree o = TREE_VEC_ELT (vec, i);
7622 if (!template_parameter_pack_p (TREE_VALUE (o)))
7623 {
7624 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7625 TREE_PURPOSE (n) = any_targ_node;
7626 }
7627 }
7628
7629 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7630 return ntmpl;
7631 }
7632
7633 /* ARG is a bound potential template template-argument, and PARGS is a list
7634 of arguments for the corresponding template template-parameter. Adjust
7635 PARGS as appropriate for application to ARG's template, and if ARG is a
7636 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7637 arguments to the template template parameter. */
7638
7639 static tree
7640 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7641 {
7642 ++processing_template_decl;
7643 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7644 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7645 {
7646 /* When comparing two template template-parameters in partial ordering,
7647 rewrite the one currently being used as an argument to have default
7648 arguments for all parameters. */
7649 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7650 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7651 if (pargs != error_mark_node)
7652 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7653 TYPE_TI_ARGS (arg));
7654 }
7655 else
7656 {
7657 tree aparms
7658 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7659 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7660 /*require_all*/true,
7661 /*use_default*/true);
7662 }
7663 --processing_template_decl;
7664 return pargs;
7665 }
7666
7667 /* Subroutine of unify for the case when PARM is a
7668 BOUND_TEMPLATE_TEMPLATE_PARM. */
7669
7670 static int
7671 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7672 bool explain_p)
7673 {
7674 tree parmvec = TYPE_TI_ARGS (parm);
7675 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7676
7677 /* The template template parm might be variadic and the argument
7678 not, so flatten both argument lists. */
7679 parmvec = expand_template_argument_pack (parmvec);
7680 argvec = expand_template_argument_pack (argvec);
7681
7682 if (flag_new_ttp)
7683 {
7684 /* In keeping with P0522R0, adjust P's template arguments
7685 to apply to A's template; then flatten it again. */
7686 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7687 nparmvec = expand_template_argument_pack (nparmvec);
7688
7689 if (unify (tparms, targs, nparmvec, argvec,
7690 UNIFY_ALLOW_NONE, explain_p))
7691 return 1;
7692
7693 /* If the P0522 adjustment eliminated a pack expansion, deduce
7694 empty packs. */
7695 if (flag_new_ttp
7696 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7697 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7698 DEDUCE_EXACT, /*sub*/true, explain_p))
7699 return 1;
7700 }
7701 else
7702 {
7703 /* Deduce arguments T, i from TT<T> or TT<i>.
7704 We check each element of PARMVEC and ARGVEC individually
7705 rather than the whole TREE_VEC since they can have
7706 different number of elements, which is allowed under N2555. */
7707
7708 int len = TREE_VEC_LENGTH (parmvec);
7709
7710 /* Check if the parameters end in a pack, making them
7711 variadic. */
7712 int parm_variadic_p = 0;
7713 if (len > 0
7714 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7715 parm_variadic_p = 1;
7716
7717 for (int i = 0; i < len - parm_variadic_p; ++i)
7718 /* If the template argument list of P contains a pack
7719 expansion that is not the last template argument, the
7720 entire template argument list is a non-deduced
7721 context. */
7722 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7723 return unify_success (explain_p);
7724
7725 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7726 return unify_too_few_arguments (explain_p,
7727 TREE_VEC_LENGTH (argvec), len);
7728
7729 for (int i = 0; i < len - parm_variadic_p; ++i)
7730 if (unify (tparms, targs,
7731 TREE_VEC_ELT (parmvec, i),
7732 TREE_VEC_ELT (argvec, i),
7733 UNIFY_ALLOW_NONE, explain_p))
7734 return 1;
7735
7736 if (parm_variadic_p
7737 && unify_pack_expansion (tparms, targs,
7738 parmvec, argvec,
7739 DEDUCE_EXACT,
7740 /*subr=*/true, explain_p))
7741 return 1;
7742 }
7743
7744 return 0;
7745 }
7746
7747 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7748 template template parameters. Both PARM_PARMS and ARG_PARMS are
7749 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7750 or PARM_DECL.
7751
7752 Consider the example:
7753 template <class T> class A;
7754 template<template <class U> class TT> class B;
7755
7756 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7757 the parameters to A, and OUTER_ARGS contains A. */
7758
7759 static int
7760 coerce_template_template_parms (tree parm_parms,
7761 tree arg_parms,
7762 tsubst_flags_t complain,
7763 tree in_decl,
7764 tree outer_args)
7765 {
7766 int nparms, nargs, i;
7767 tree parm, arg;
7768 int variadic_p = 0;
7769
7770 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7771 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7772
7773 nparms = TREE_VEC_LENGTH (parm_parms);
7774 nargs = TREE_VEC_LENGTH (arg_parms);
7775
7776 if (flag_new_ttp)
7777 {
7778 /* P0522R0: A template template-parameter P is at least as specialized as
7779 a template template-argument A if, given the following rewrite to two
7780 function templates, the function template corresponding to P is at
7781 least as specialized as the function template corresponding to A
7782 according to the partial ordering rules for function templates
7783 ([temp.func.order]). Given an invented class template X with the
7784 template parameter list of A (including default arguments):
7785
7786 * Each of the two function templates has the same template parameters,
7787 respectively, as P or A.
7788
7789 * Each function template has a single function parameter whose type is
7790 a specialization of X with template arguments corresponding to the
7791 template parameters from the respective function template where, for
7792 each template parameter PP in the template parameter list of the
7793 function template, a corresponding template argument AA is formed. If
7794 PP declares a parameter pack, then AA is the pack expansion
7795 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7796
7797 If the rewrite produces an invalid type, then P is not at least as
7798 specialized as A. */
7799
7800 /* So coerce P's args to apply to A's parms, and then deduce between A's
7801 args and the converted args. If that succeeds, A is at least as
7802 specialized as P, so they match.*/
7803 tree pargs = template_parms_level_to_args (parm_parms);
7804 pargs = add_outermost_template_args (outer_args, pargs);
7805 ++processing_template_decl;
7806 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7807 /*require_all*/true, /*use_default*/true);
7808 --processing_template_decl;
7809 if (pargs != error_mark_node)
7810 {
7811 tree targs = make_tree_vec (nargs);
7812 tree aargs = template_parms_level_to_args (arg_parms);
7813 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7814 /*explain*/false))
7815 return 1;
7816 }
7817 }
7818
7819 /* Determine whether we have a parameter pack at the end of the
7820 template template parameter's template parameter list. */
7821 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7822 {
7823 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7824
7825 if (error_operand_p (parm))
7826 return 0;
7827
7828 switch (TREE_CODE (parm))
7829 {
7830 case TEMPLATE_DECL:
7831 case TYPE_DECL:
7832 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7833 variadic_p = 1;
7834 break;
7835
7836 case PARM_DECL:
7837 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7838 variadic_p = 1;
7839 break;
7840
7841 default:
7842 gcc_unreachable ();
7843 }
7844 }
7845
7846 if (nargs != nparms
7847 && !(variadic_p && nargs >= nparms - 1))
7848 return 0;
7849
7850 /* Check all of the template parameters except the parameter pack at
7851 the end (if any). */
7852 for (i = 0; i < nparms - variadic_p; ++i)
7853 {
7854 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7855 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7856 continue;
7857
7858 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7859 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7860
7861 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7862 outer_args))
7863 return 0;
7864
7865 }
7866
7867 if (variadic_p)
7868 {
7869 /* Check each of the template parameters in the template
7870 argument against the template parameter pack at the end of
7871 the template template parameter. */
7872 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7873 return 0;
7874
7875 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7876
7877 for (; i < nargs; ++i)
7878 {
7879 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7880 continue;
7881
7882 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7883
7884 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7885 outer_args))
7886 return 0;
7887 }
7888 }
7889
7890 return 1;
7891 }
7892
7893 /* Verifies that the deduced template arguments (in TARGS) for the
7894 template template parameters (in TPARMS) represent valid bindings,
7895 by comparing the template parameter list of each template argument
7896 to the template parameter list of its corresponding template
7897 template parameter, in accordance with DR150. This
7898 routine can only be called after all template arguments have been
7899 deduced. It will return TRUE if all of the template template
7900 parameter bindings are okay, FALSE otherwise. */
7901 bool
7902 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7903 {
7904 int i, ntparms = TREE_VEC_LENGTH (tparms);
7905 bool ret = true;
7906
7907 /* We're dealing with template parms in this process. */
7908 ++processing_template_decl;
7909
7910 targs = INNERMOST_TEMPLATE_ARGS (targs);
7911
7912 for (i = 0; i < ntparms; ++i)
7913 {
7914 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7915 tree targ = TREE_VEC_ELT (targs, i);
7916
7917 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7918 {
7919 tree packed_args = NULL_TREE;
7920 int idx, len = 1;
7921
7922 if (ARGUMENT_PACK_P (targ))
7923 {
7924 /* Look inside the argument pack. */
7925 packed_args = ARGUMENT_PACK_ARGS (targ);
7926 len = TREE_VEC_LENGTH (packed_args);
7927 }
7928
7929 for (idx = 0; idx < len; ++idx)
7930 {
7931 tree targ_parms = NULL_TREE;
7932
7933 if (packed_args)
7934 /* Extract the next argument from the argument
7935 pack. */
7936 targ = TREE_VEC_ELT (packed_args, idx);
7937
7938 if (PACK_EXPANSION_P (targ))
7939 /* Look at the pattern of the pack expansion. */
7940 targ = PACK_EXPANSION_PATTERN (targ);
7941
7942 /* Extract the template parameters from the template
7943 argument. */
7944 if (TREE_CODE (targ) == TEMPLATE_DECL)
7945 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7946 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7947 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7948
7949 /* Verify that we can coerce the template template
7950 parameters from the template argument to the template
7951 parameter. This requires an exact match. */
7952 if (targ_parms
7953 && !coerce_template_template_parms
7954 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7955 targ_parms,
7956 tf_none,
7957 tparm,
7958 targs))
7959 {
7960 ret = false;
7961 goto out;
7962 }
7963 }
7964 }
7965 }
7966
7967 out:
7968
7969 --processing_template_decl;
7970 return ret;
7971 }
7972
7973 /* Since type attributes aren't mangled, we need to strip them from
7974 template type arguments. */
7975
7976 static tree
7977 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7978 {
7979 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7980 return arg;
7981 bool removed_attributes = false;
7982 tree canon = strip_typedefs (arg, &removed_attributes);
7983 if (removed_attributes
7984 && (complain & tf_warning))
7985 warning (OPT_Wignored_attributes,
7986 "ignoring attributes on template argument %qT", arg);
7987 return canon;
7988 }
7989
7990 /* And from inside dependent non-type arguments like sizeof(Type). */
7991
7992 static tree
7993 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7994 {
7995 if (!arg || arg == error_mark_node)
7996 return arg;
7997 bool removed_attributes = false;
7998 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7999 if (removed_attributes
8000 && (complain & tf_warning))
8001 warning (OPT_Wignored_attributes,
8002 "ignoring attributes in template argument %qE", arg);
8003 return canon;
8004 }
8005
8006 // A template declaration can be substituted for a constrained
8007 // template template parameter only when the argument is more
8008 // constrained than the parameter.
8009 static bool
8010 is_compatible_template_arg (tree parm, tree arg)
8011 {
8012 tree parm_cons = get_constraints (parm);
8013
8014 /* For now, allow constrained template template arguments
8015 and unconstrained template template parameters. */
8016 if (parm_cons == NULL_TREE)
8017 return true;
8018
8019 /* If the template parameter is constrained, we need to rewrite its
8020 constraints in terms of the ARG's template parameters. This ensures
8021 that all of the template parameter types will have the same depth.
8022
8023 Note that this is only valid when coerce_template_template_parm is
8024 true for the innermost template parameters of PARM and ARG. In other
8025 words, because coercion is successful, this conversion will be valid. */
8026 tree new_args = NULL_TREE;
8027 if (parm_cons)
8028 {
8029 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8030 new_args = template_parms_level_to_args (aparms);
8031 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8032 tf_none, NULL_TREE);
8033 if (parm_cons == error_mark_node)
8034 return false;
8035 }
8036
8037 return weakly_subsumes (parm_cons, new_args, arg);
8038 }
8039
8040 // Convert a placeholder argument into a binding to the original
8041 // parameter. The original parameter is saved as the TREE_TYPE of
8042 // ARG.
8043 static inline tree
8044 convert_wildcard_argument (tree parm, tree arg)
8045 {
8046 TREE_TYPE (arg) = parm;
8047 return arg;
8048 }
8049
8050 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8051 because one of them is dependent. But we need to represent the
8052 conversion for the benefit of cp_tree_equal. */
8053
8054 static tree
8055 maybe_convert_nontype_argument (tree type, tree arg)
8056 {
8057 /* Auto parms get no conversion. */
8058 if (type_uses_auto (type))
8059 return arg;
8060 /* We don't need or want to add this conversion now if we're going to use the
8061 argument for deduction. */
8062 if (value_dependent_expression_p (arg))
8063 return arg;
8064
8065 type = cv_unqualified (type);
8066 tree argtype = TREE_TYPE (arg);
8067 if (same_type_p (type, argtype))
8068 return arg;
8069
8070 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8071 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8072 return arg;
8073 }
8074
8075 /* Convert the indicated template ARG as necessary to match the
8076 indicated template PARM. Returns the converted ARG, or
8077 error_mark_node if the conversion was unsuccessful. Error and
8078 warning messages are issued under control of COMPLAIN. This
8079 conversion is for the Ith parameter in the parameter list. ARGS is
8080 the full set of template arguments deduced so far. */
8081
8082 static tree
8083 convert_template_argument (tree parm,
8084 tree arg,
8085 tree args,
8086 tsubst_flags_t complain,
8087 int i,
8088 tree in_decl)
8089 {
8090 tree orig_arg;
8091 tree val;
8092 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8093
8094 if (parm == error_mark_node || error_operand_p (arg))
8095 return error_mark_node;
8096
8097 /* Trivially convert placeholders. */
8098 if (TREE_CODE (arg) == WILDCARD_DECL)
8099 return convert_wildcard_argument (parm, arg);
8100
8101 if (arg == any_targ_node)
8102 return arg;
8103
8104 if (TREE_CODE (arg) == TREE_LIST
8105 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8106 {
8107 /* The template argument was the name of some
8108 member function. That's usually
8109 invalid, but static members are OK. In any
8110 case, grab the underlying fields/functions
8111 and issue an error later if required. */
8112 TREE_TYPE (arg) = unknown_type_node;
8113 }
8114
8115 orig_arg = arg;
8116
8117 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8118 requires_type = (TREE_CODE (parm) == TYPE_DECL
8119 || requires_tmpl_type);
8120
8121 /* When determining whether an argument pack expansion is a template,
8122 look at the pattern. */
8123 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8124 arg = PACK_EXPANSION_PATTERN (arg);
8125
8126 /* Deal with an injected-class-name used as a template template arg. */
8127 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8128 {
8129 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8130 if (TREE_CODE (t) == TEMPLATE_DECL)
8131 {
8132 if (cxx_dialect >= cxx11)
8133 /* OK under DR 1004. */;
8134 else if (complain & tf_warning_or_error)
8135 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8136 " used as template template argument", TYPE_NAME (arg));
8137 else if (flag_pedantic_errors)
8138 t = arg;
8139
8140 arg = t;
8141 }
8142 }
8143
8144 is_tmpl_type =
8145 ((TREE_CODE (arg) == TEMPLATE_DECL
8146 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8147 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8148 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8149 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8150
8151 if (is_tmpl_type
8152 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8153 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8154 arg = TYPE_STUB_DECL (arg);
8155
8156 is_type = TYPE_P (arg) || is_tmpl_type;
8157
8158 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8159 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8160 {
8161 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8162 {
8163 if (complain & tf_error)
8164 error ("invalid use of destructor %qE as a type", orig_arg);
8165 return error_mark_node;
8166 }
8167
8168 permerror (input_location,
8169 "to refer to a type member of a template parameter, "
8170 "use %<typename %E%>", orig_arg);
8171
8172 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8173 TREE_OPERAND (arg, 1),
8174 typename_type,
8175 complain);
8176 arg = orig_arg;
8177 is_type = 1;
8178 }
8179 if (is_type != requires_type)
8180 {
8181 if (in_decl)
8182 {
8183 if (complain & tf_error)
8184 {
8185 error ("type/value mismatch at argument %d in template "
8186 "parameter list for %qD",
8187 i + 1, in_decl);
8188 if (is_type)
8189 {
8190 /* The template argument is a type, but we're expecting
8191 an expression. */
8192 inform (input_location,
8193 " expected a constant of type %qT, got %qT",
8194 TREE_TYPE (parm),
8195 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8196 /* [temp.arg]/2: "In a template-argument, an ambiguity
8197 between a type-id and an expression is resolved to a
8198 type-id, regardless of the form of the corresponding
8199 template-parameter." So give the user a clue. */
8200 if (TREE_CODE (arg) == FUNCTION_TYPE)
8201 inform (input_location, " ambiguous template argument "
8202 "for non-type template parameter is treated as "
8203 "function type");
8204 }
8205 else if (requires_tmpl_type)
8206 inform (input_location,
8207 " expected a class template, got %qE", orig_arg);
8208 else
8209 inform (input_location,
8210 " expected a type, got %qE", orig_arg);
8211 }
8212 }
8213 return error_mark_node;
8214 }
8215 if (is_tmpl_type ^ requires_tmpl_type)
8216 {
8217 if (in_decl && (complain & tf_error))
8218 {
8219 error ("type/value mismatch at argument %d in template "
8220 "parameter list for %qD",
8221 i + 1, in_decl);
8222 if (is_tmpl_type)
8223 inform (input_location,
8224 " expected a type, got %qT", DECL_NAME (arg));
8225 else
8226 inform (input_location,
8227 " expected a class template, got %qT", orig_arg);
8228 }
8229 return error_mark_node;
8230 }
8231
8232 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8233 /* We already did the appropriate conversion when packing args. */
8234 val = orig_arg;
8235 else if (is_type)
8236 {
8237 if (requires_tmpl_type)
8238 {
8239 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8240 /* The number of argument required is not known yet.
8241 Just accept it for now. */
8242 val = orig_arg;
8243 else
8244 {
8245 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8246 tree argparm;
8247
8248 /* Strip alias templates that are equivalent to another
8249 template. */
8250 arg = get_underlying_template (arg);
8251 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8252
8253 if (coerce_template_template_parms (parmparm, argparm,
8254 complain, in_decl,
8255 args))
8256 {
8257 val = arg;
8258
8259 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8260 TEMPLATE_DECL. */
8261 if (val != error_mark_node)
8262 {
8263 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8264 val = TREE_TYPE (val);
8265 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8266 val = make_pack_expansion (val, complain);
8267 }
8268 }
8269 else
8270 {
8271 if (in_decl && (complain & tf_error))
8272 {
8273 error ("type/value mismatch at argument %d in "
8274 "template parameter list for %qD",
8275 i + 1, in_decl);
8276 inform (input_location,
8277 " expected a template of type %qD, got %qT",
8278 parm, orig_arg);
8279 }
8280
8281 val = error_mark_node;
8282 }
8283
8284 // Check that the constraints are compatible before allowing the
8285 // substitution.
8286 if (val != error_mark_node)
8287 if (!is_compatible_template_arg (parm, arg))
8288 {
8289 if (in_decl && (complain & tf_error))
8290 {
8291 error ("constraint mismatch at argument %d in "
8292 "template parameter list for %qD",
8293 i + 1, in_decl);
8294 inform (input_location, " expected %qD but got %qD",
8295 parm, arg);
8296 }
8297 val = error_mark_node;
8298 }
8299 }
8300 }
8301 else
8302 val = orig_arg;
8303 /* We only form one instance of each template specialization.
8304 Therefore, if we use a non-canonical variant (i.e., a
8305 typedef), any future messages referring to the type will use
8306 the typedef, which is confusing if those future uses do not
8307 themselves also use the typedef. */
8308 if (TYPE_P (val))
8309 val = canonicalize_type_argument (val, complain);
8310 }
8311 else
8312 {
8313 tree t = TREE_TYPE (parm);
8314
8315 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8316 > TMPL_ARGS_DEPTH (args))
8317 /* We don't have enough levels of args to do any substitution. This
8318 can happen in the context of -fnew-ttp-matching. */;
8319 else if (tree a = type_uses_auto (t))
8320 {
8321 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8322 if (t == error_mark_node)
8323 return error_mark_node;
8324 }
8325 else
8326 t = tsubst (t, args, complain, in_decl);
8327
8328 if (invalid_nontype_parm_type_p (t, complain))
8329 return error_mark_node;
8330
8331 if (t != TREE_TYPE (parm))
8332 t = canonicalize_type_argument (t, complain);
8333
8334 if (!type_dependent_expression_p (orig_arg)
8335 && !uses_template_parms (t))
8336 /* We used to call digest_init here. However, digest_init
8337 will report errors, which we don't want when complain
8338 is zero. More importantly, digest_init will try too
8339 hard to convert things: for example, `0' should not be
8340 converted to pointer type at this point according to
8341 the standard. Accepting this is not merely an
8342 extension, since deciding whether or not these
8343 conversions can occur is part of determining which
8344 function template to call, or whether a given explicit
8345 argument specification is valid. */
8346 val = convert_nontype_argument (t, orig_arg, complain);
8347 else
8348 {
8349 val = canonicalize_expr_argument (orig_arg, complain);
8350 val = maybe_convert_nontype_argument (t, val);
8351 }
8352
8353
8354 if (val == NULL_TREE)
8355 val = error_mark_node;
8356 else if (val == error_mark_node && (complain & tf_error))
8357 error_at (cp_expr_loc_or_input_loc (orig_arg),
8358 "could not convert template argument %qE from %qT to %qT",
8359 orig_arg, TREE_TYPE (orig_arg), t);
8360
8361 if (INDIRECT_REF_P (val))
8362 {
8363 /* Reject template arguments that are references to built-in
8364 functions with no library fallbacks. */
8365 const_tree inner = TREE_OPERAND (val, 0);
8366 const_tree innertype = TREE_TYPE (inner);
8367 if (innertype
8368 && TYPE_REF_P (innertype)
8369 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8370 && TREE_OPERAND_LENGTH (inner) > 0
8371 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8372 return error_mark_node;
8373 }
8374
8375 if (TREE_CODE (val) == SCOPE_REF)
8376 {
8377 /* Strip typedefs from the SCOPE_REF. */
8378 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8379 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8380 complain);
8381 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8382 QUALIFIED_NAME_IS_TEMPLATE (val));
8383 }
8384 }
8385
8386 return val;
8387 }
8388
8389 /* Coerces the remaining template arguments in INNER_ARGS (from
8390 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8391 Returns the coerced argument pack. PARM_IDX is the position of this
8392 parameter in the template parameter list. ARGS is the original
8393 template argument list. */
8394 static tree
8395 coerce_template_parameter_pack (tree parms,
8396 int parm_idx,
8397 tree args,
8398 tree inner_args,
8399 int arg_idx,
8400 tree new_args,
8401 int* lost,
8402 tree in_decl,
8403 tsubst_flags_t complain)
8404 {
8405 tree parm = TREE_VEC_ELT (parms, parm_idx);
8406 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8407 tree packed_args;
8408 tree argument_pack;
8409 tree packed_parms = NULL_TREE;
8410
8411 if (arg_idx > nargs)
8412 arg_idx = nargs;
8413
8414 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8415 {
8416 /* When the template parameter is a non-type template parameter pack
8417 or template template parameter pack whose type or template
8418 parameters use parameter packs, we know exactly how many arguments
8419 we are looking for. Build a vector of the instantiated decls for
8420 these template parameters in PACKED_PARMS. */
8421 /* We can't use make_pack_expansion here because it would interpret a
8422 _DECL as a use rather than a declaration. */
8423 tree decl = TREE_VALUE (parm);
8424 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8425 SET_PACK_EXPANSION_PATTERN (exp, decl);
8426 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8427 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8428
8429 TREE_VEC_LENGTH (args)--;
8430 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8431 TREE_VEC_LENGTH (args)++;
8432
8433 if (packed_parms == error_mark_node)
8434 return error_mark_node;
8435
8436 /* If we're doing a partial instantiation of a member template,
8437 verify that all of the types used for the non-type
8438 template parameter pack are, in fact, valid for non-type
8439 template parameters. */
8440 if (arg_idx < nargs
8441 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8442 {
8443 int j, len = TREE_VEC_LENGTH (packed_parms);
8444 for (j = 0; j < len; ++j)
8445 {
8446 tree t = TREE_VEC_ELT (packed_parms, j);
8447 if (TREE_CODE (t) == PARM_DECL
8448 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8449 return error_mark_node;
8450 }
8451 /* We don't know how many args we have yet, just
8452 use the unconverted ones for now. */
8453 return NULL_TREE;
8454 }
8455
8456 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8457 }
8458 /* Check if we have a placeholder pack, which indicates we're
8459 in the context of a introduction list. In that case we want
8460 to match this pack to the single placeholder. */
8461 else if (arg_idx < nargs
8462 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8463 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8464 {
8465 nargs = arg_idx + 1;
8466 packed_args = make_tree_vec (1);
8467 }
8468 else
8469 packed_args = make_tree_vec (nargs - arg_idx);
8470
8471 /* Convert the remaining arguments, which will be a part of the
8472 parameter pack "parm". */
8473 int first_pack_arg = arg_idx;
8474 for (; arg_idx < nargs; ++arg_idx)
8475 {
8476 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8477 tree actual_parm = TREE_VALUE (parm);
8478 int pack_idx = arg_idx - first_pack_arg;
8479
8480 if (packed_parms)
8481 {
8482 /* Once we've packed as many args as we have types, stop. */
8483 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8484 break;
8485 else if (PACK_EXPANSION_P (arg))
8486 /* We don't know how many args we have yet, just
8487 use the unconverted ones for now. */
8488 return NULL_TREE;
8489 else
8490 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8491 }
8492
8493 if (arg == error_mark_node)
8494 {
8495 if (complain & tf_error)
8496 error ("template argument %d is invalid", arg_idx + 1);
8497 }
8498 else
8499 arg = convert_template_argument (actual_parm,
8500 arg, new_args, complain, parm_idx,
8501 in_decl);
8502 if (arg == error_mark_node)
8503 (*lost)++;
8504 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8505 }
8506
8507 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8508 && TREE_VEC_LENGTH (packed_args) > 0)
8509 {
8510 if (complain & tf_error)
8511 error ("wrong number of template arguments (%d, should be %d)",
8512 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8513 return error_mark_node;
8514 }
8515
8516 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8517 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8518 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8519 else
8520 {
8521 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8522 TREE_CONSTANT (argument_pack) = 1;
8523 }
8524
8525 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8526 if (CHECKING_P)
8527 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8528 TREE_VEC_LENGTH (packed_args));
8529 return argument_pack;
8530 }
8531
8532 /* Returns the number of pack expansions in the template argument vector
8533 ARGS. */
8534
8535 static int
8536 pack_expansion_args_count (tree args)
8537 {
8538 int i;
8539 int count = 0;
8540 if (args)
8541 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8542 {
8543 tree elt = TREE_VEC_ELT (args, i);
8544 if (elt && PACK_EXPANSION_P (elt))
8545 ++count;
8546 }
8547 return count;
8548 }
8549
8550 /* Convert all template arguments to their appropriate types, and
8551 return a vector containing the innermost resulting template
8552 arguments. If any error occurs, return error_mark_node. Error and
8553 warning messages are issued under control of COMPLAIN.
8554
8555 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8556 for arguments not specified in ARGS. Otherwise, if
8557 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8558 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8559 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8560 ARGS. */
8561
8562 static tree
8563 coerce_template_parms (tree parms,
8564 tree args,
8565 tree in_decl,
8566 tsubst_flags_t complain,
8567 bool require_all_args,
8568 bool use_default_args)
8569 {
8570 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8571 tree orig_inner_args;
8572 tree inner_args;
8573 tree new_args;
8574 tree new_inner_args;
8575
8576 /* When used as a boolean value, indicates whether this is a
8577 variadic template parameter list. Since it's an int, we can also
8578 subtract it from nparms to get the number of non-variadic
8579 parameters. */
8580 int variadic_p = 0;
8581 int variadic_args_p = 0;
8582 int post_variadic_parms = 0;
8583
8584 /* Adjustment to nparms for fixed parameter packs. */
8585 int fixed_pack_adjust = 0;
8586 int fixed_packs = 0;
8587 int missing = 0;
8588
8589 /* Likewise for parameters with default arguments. */
8590 int default_p = 0;
8591
8592 if (args == error_mark_node)
8593 return error_mark_node;
8594
8595 nparms = TREE_VEC_LENGTH (parms);
8596
8597 /* Determine if there are any parameter packs or default arguments. */
8598 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8599 {
8600 tree parm = TREE_VEC_ELT (parms, parm_idx);
8601 if (variadic_p)
8602 ++post_variadic_parms;
8603 if (template_parameter_pack_p (TREE_VALUE (parm)))
8604 ++variadic_p;
8605 if (TREE_PURPOSE (parm))
8606 ++default_p;
8607 }
8608
8609 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8610 /* If there are no parameters that follow a parameter pack, we need to
8611 expand any argument packs so that we can deduce a parameter pack from
8612 some non-packed args followed by an argument pack, as in variadic85.C.
8613 If there are such parameters, we need to leave argument packs intact
8614 so the arguments are assigned properly. This can happen when dealing
8615 with a nested class inside a partial specialization of a class
8616 template, as in variadic92.C, or when deducing a template parameter pack
8617 from a sub-declarator, as in variadic114.C. */
8618 if (!post_variadic_parms)
8619 inner_args = expand_template_argument_pack (inner_args);
8620
8621 /* Count any pack expansion args. */
8622 variadic_args_p = pack_expansion_args_count (inner_args);
8623
8624 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8625 if ((nargs - variadic_args_p > nparms && !variadic_p)
8626 || (nargs < nparms - variadic_p
8627 && require_all_args
8628 && !variadic_args_p
8629 && (!use_default_args
8630 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8631 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8632 {
8633 bad_nargs:
8634 if (complain & tf_error)
8635 {
8636 if (variadic_p || default_p)
8637 {
8638 nparms -= variadic_p + default_p;
8639 error ("wrong number of template arguments "
8640 "(%d, should be at least %d)", nargs, nparms);
8641 }
8642 else
8643 error ("wrong number of template arguments "
8644 "(%d, should be %d)", nargs, nparms);
8645
8646 if (in_decl)
8647 inform (DECL_SOURCE_LOCATION (in_decl),
8648 "provided for %qD", in_decl);
8649 }
8650
8651 return error_mark_node;
8652 }
8653 /* We can't pass a pack expansion to a non-pack parameter of an alias
8654 template (DR 1430). */
8655 else if (in_decl
8656 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8657 || concept_definition_p (in_decl))
8658 && variadic_args_p
8659 && nargs - variadic_args_p < nparms - variadic_p)
8660 {
8661 if (complain & tf_error)
8662 {
8663 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8664 {
8665 tree arg = TREE_VEC_ELT (inner_args, i);
8666 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8667
8668 if (PACK_EXPANSION_P (arg)
8669 && !template_parameter_pack_p (parm))
8670 {
8671 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8672 error_at (location_of (arg),
8673 "pack expansion argument for non-pack parameter "
8674 "%qD of alias template %qD", parm, in_decl);
8675 else
8676 error_at (location_of (arg),
8677 "pack expansion argument for non-pack parameter "
8678 "%qD of concept %qD", parm, in_decl);
8679 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8680 goto found;
8681 }
8682 }
8683 gcc_unreachable ();
8684 found:;
8685 }
8686 return error_mark_node;
8687 }
8688
8689 /* We need to evaluate the template arguments, even though this
8690 template-id may be nested within a "sizeof". */
8691 cp_evaluated ev;
8692
8693 new_inner_args = make_tree_vec (nparms);
8694 new_args = add_outermost_template_args (args, new_inner_args);
8695 int pack_adjust = 0;
8696 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8697 {
8698 tree arg;
8699 tree parm;
8700
8701 /* Get the Ith template parameter. */
8702 parm = TREE_VEC_ELT (parms, parm_idx);
8703
8704 if (parm == error_mark_node)
8705 {
8706 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8707 continue;
8708 }
8709
8710 /* Calculate the next argument. */
8711 if (arg_idx < nargs)
8712 arg = TREE_VEC_ELT (inner_args, arg_idx);
8713 else
8714 arg = NULL_TREE;
8715
8716 if (template_parameter_pack_p (TREE_VALUE (parm))
8717 && (arg || require_all_args || !(complain & tf_partial))
8718 && !(arg && ARGUMENT_PACK_P (arg)))
8719 {
8720 /* Some arguments will be placed in the
8721 template parameter pack PARM. */
8722 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8723 inner_args, arg_idx,
8724 new_args, &lost,
8725 in_decl, complain);
8726
8727 if (arg == NULL_TREE)
8728 {
8729 /* We don't know how many args we have yet, just use the
8730 unconverted (and still packed) ones for now. */
8731 new_inner_args = orig_inner_args;
8732 arg_idx = nargs;
8733 break;
8734 }
8735
8736 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8737
8738 /* Store this argument. */
8739 if (arg == error_mark_node)
8740 {
8741 lost++;
8742 /* We are done with all of the arguments. */
8743 arg_idx = nargs;
8744 break;
8745 }
8746 else
8747 {
8748 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8749 arg_idx += pack_adjust;
8750 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8751 {
8752 ++fixed_packs;
8753 fixed_pack_adjust += pack_adjust;
8754 }
8755 }
8756
8757 continue;
8758 }
8759 else if (arg)
8760 {
8761 if (PACK_EXPANSION_P (arg))
8762 {
8763 /* "If every valid specialization of a variadic template
8764 requires an empty template parameter pack, the template is
8765 ill-formed, no diagnostic required." So check that the
8766 pattern works with this parameter. */
8767 tree pattern = PACK_EXPANSION_PATTERN (arg);
8768 tree conv = convert_template_argument (TREE_VALUE (parm),
8769 pattern, new_args,
8770 complain, parm_idx,
8771 in_decl);
8772 if (conv == error_mark_node)
8773 {
8774 if (complain & tf_error)
8775 inform (input_location, "so any instantiation with a "
8776 "non-empty parameter pack would be ill-formed");
8777 ++lost;
8778 }
8779 else if (TYPE_P (conv) && !TYPE_P (pattern))
8780 /* Recover from missing typename. */
8781 TREE_VEC_ELT (inner_args, arg_idx)
8782 = make_pack_expansion (conv, complain);
8783
8784 /* We don't know how many args we have yet, just
8785 use the unconverted ones for now. */
8786 new_inner_args = inner_args;
8787 arg_idx = nargs;
8788 break;
8789 }
8790 }
8791 else if (require_all_args)
8792 {
8793 /* There must be a default arg in this case. */
8794 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8795 complain, in_decl);
8796 /* The position of the first default template argument,
8797 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8798 Record that. */
8799 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8801 arg_idx - pack_adjust);
8802 }
8803 else
8804 break;
8805
8806 if (arg == error_mark_node)
8807 {
8808 if (complain & tf_error)
8809 error ("template argument %d is invalid", arg_idx + 1);
8810 }
8811 else if (!arg)
8812 {
8813 /* This can occur if there was an error in the template
8814 parameter list itself (which we would already have
8815 reported) that we are trying to recover from, e.g., a class
8816 template with a parameter list such as
8817 template<typename..., typename> (cpp0x/variadic150.C). */
8818 ++lost;
8819
8820 /* This can also happen with a fixed parameter pack (71834). */
8821 if (arg_idx >= nargs)
8822 ++missing;
8823 }
8824 else
8825 arg = convert_template_argument (TREE_VALUE (parm),
8826 arg, new_args, complain,
8827 parm_idx, in_decl);
8828
8829 if (arg == error_mark_node)
8830 lost++;
8831
8832 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8833 }
8834
8835 if (missing || arg_idx < nargs - variadic_args_p)
8836 {
8837 /* If we had fixed parameter packs, we didn't know how many arguments we
8838 actually needed earlier; now we do. */
8839 nparms += fixed_pack_adjust;
8840 variadic_p -= fixed_packs;
8841 goto bad_nargs;
8842 }
8843
8844 if (arg_idx < nargs)
8845 {
8846 /* We had some pack expansion arguments that will only work if the packs
8847 are empty, but wait until instantiation time to complain.
8848 See variadic-ttp3.C. */
8849
8850 /* Except that we can't provide empty packs to alias templates or
8851 concepts when there are no corresponding parameters. Basically,
8852 we can get here with this:
8853
8854 template<typename T> concept C = true;
8855
8856 template<typename... Args>
8857 requires C<Args...>
8858 void f();
8859
8860 When parsing C<Args...>, we try to form a concept check of
8861 C<?, Args...>. Without the extra check for substituting an empty
8862 pack past the last parameter, we can accept the check as valid.
8863
8864 FIXME: This may be valid for alias templates (but I doubt it).
8865
8866 FIXME: The error could be better also. */
8867 if (in_decl && concept_definition_p (in_decl))
8868 {
8869 if (complain & tf_error)
8870 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8871 "too many arguments");
8872 return error_mark_node;
8873 }
8874
8875 int len = nparms + (nargs - arg_idx);
8876 tree args = make_tree_vec (len);
8877 int i = 0;
8878 for (; i < nparms; ++i)
8879 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8880 for (; i < len; ++i, ++arg_idx)
8881 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8882 arg_idx - pack_adjust);
8883 new_inner_args = args;
8884 }
8885
8886 if (lost)
8887 {
8888 gcc_assert (!(complain & tf_error) || seen_error ());
8889 return error_mark_node;
8890 }
8891
8892 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8893 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8894 TREE_VEC_LENGTH (new_inner_args));
8895
8896 return new_inner_args;
8897 }
8898
8899 /* Convert all template arguments to their appropriate types, and
8900 return a vector containing the innermost resulting template
8901 arguments. If any error occurs, return error_mark_node. Error and
8902 warning messages are not issued.
8903
8904 Note that no function argument deduction is performed, and default
8905 arguments are used to fill in unspecified arguments. */
8906 tree
8907 coerce_template_parms (tree parms, tree args, tree in_decl)
8908 {
8909 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8910 }
8911
8912 /* Convert all template arguments to their appropriate type, and
8913 instantiate default arguments as needed. This returns a vector
8914 containing the innermost resulting template arguments, or
8915 error_mark_node if unsuccessful. */
8916 tree
8917 coerce_template_parms (tree parms, tree args, tree in_decl,
8918 tsubst_flags_t complain)
8919 {
8920 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8921 }
8922
8923 /* Like coerce_template_parms. If PARMS represents all template
8924 parameters levels, this function returns a vector of vectors
8925 representing all the resulting argument levels. Note that in this
8926 case, only the innermost arguments are coerced because the
8927 outermost ones are supposed to have been coerced already.
8928
8929 Otherwise, if PARMS represents only (the innermost) vector of
8930 parameters, this function returns a vector containing just the
8931 innermost resulting arguments. */
8932
8933 static tree
8934 coerce_innermost_template_parms (tree parms,
8935 tree args,
8936 tree in_decl,
8937 tsubst_flags_t complain,
8938 bool require_all_args,
8939 bool use_default_args)
8940 {
8941 int parms_depth = TMPL_PARMS_DEPTH (parms);
8942 int args_depth = TMPL_ARGS_DEPTH (args);
8943 tree coerced_args;
8944
8945 if (parms_depth > 1)
8946 {
8947 coerced_args = make_tree_vec (parms_depth);
8948 tree level;
8949 int cur_depth;
8950
8951 for (level = parms, cur_depth = parms_depth;
8952 parms_depth > 0 && level != NULL_TREE;
8953 level = TREE_CHAIN (level), --cur_depth)
8954 {
8955 tree l;
8956 if (cur_depth == args_depth)
8957 l = coerce_template_parms (TREE_VALUE (level),
8958 args, in_decl, complain,
8959 require_all_args,
8960 use_default_args);
8961 else
8962 l = TMPL_ARGS_LEVEL (args, cur_depth);
8963
8964 if (l == error_mark_node)
8965 return error_mark_node;
8966
8967 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8968 }
8969 }
8970 else
8971 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8972 args, in_decl, complain,
8973 require_all_args,
8974 use_default_args);
8975 return coerced_args;
8976 }
8977
8978 /* Returns 1 if template args OT and NT are equivalent. */
8979
8980 int
8981 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8982 {
8983 if (nt == ot)
8984 return 1;
8985 if (nt == NULL_TREE || ot == NULL_TREE)
8986 return false;
8987 if (nt == any_targ_node || ot == any_targ_node)
8988 return true;
8989
8990 if (TREE_CODE (nt) == TREE_VEC)
8991 /* For member templates */
8992 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8993 else if (PACK_EXPANSION_P (ot))
8994 return (PACK_EXPANSION_P (nt)
8995 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8996 PACK_EXPANSION_PATTERN (nt))
8997 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8998 PACK_EXPANSION_EXTRA_ARGS (nt)));
8999 else if (ARGUMENT_PACK_P (ot))
9000 {
9001 int i, len;
9002 tree opack, npack;
9003
9004 if (!ARGUMENT_PACK_P (nt))
9005 return 0;
9006
9007 opack = ARGUMENT_PACK_ARGS (ot);
9008 npack = ARGUMENT_PACK_ARGS (nt);
9009 len = TREE_VEC_LENGTH (opack);
9010 if (TREE_VEC_LENGTH (npack) != len)
9011 return 0;
9012 for (i = 0; i < len; ++i)
9013 if (!template_args_equal (TREE_VEC_ELT (opack, i),
9014 TREE_VEC_ELT (npack, i)))
9015 return 0;
9016 return 1;
9017 }
9018 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9019 gcc_unreachable ();
9020 else if (TYPE_P (nt))
9021 {
9022 if (!TYPE_P (ot))
9023 return false;
9024 /* Don't treat an alias template specialization with dependent
9025 arguments as equivalent to its underlying type when used as a
9026 template argument; we need them to be distinct so that we
9027 substitute into the specialization arguments at instantiation
9028 time. And aliases can't be equivalent without being ==, so
9029 we don't need to look any deeper.
9030
9031 During partial ordering, however, we need to treat them normally so
9032 that we can order uses of the same alias with different
9033 cv-qualification (79960). */
9034 if (!partial_order
9035 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
9036 return false;
9037 else
9038 return same_type_p (ot, nt);
9039 }
9040 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9041 return 0;
9042 else
9043 {
9044 /* Try to treat a template non-type argument that has been converted
9045 to the parameter type as equivalent to one that hasn't yet. */
9046 for (enum tree_code code1 = TREE_CODE (ot);
9047 CONVERT_EXPR_CODE_P (code1)
9048 || code1 == NON_LVALUE_EXPR;
9049 code1 = TREE_CODE (ot))
9050 ot = TREE_OPERAND (ot, 0);
9051 for (enum tree_code code2 = TREE_CODE (nt);
9052 CONVERT_EXPR_CODE_P (code2)
9053 || code2 == NON_LVALUE_EXPR;
9054 code2 = TREE_CODE (nt))
9055 nt = TREE_OPERAND (nt, 0);
9056
9057 return cp_tree_equal (ot, nt);
9058 }
9059 }
9060
9061 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9062 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9063 NEWARG_PTR with the offending arguments if they are non-NULL. */
9064
9065 int
9066 comp_template_args (tree oldargs, tree newargs,
9067 tree *oldarg_ptr, tree *newarg_ptr,
9068 bool partial_order)
9069 {
9070 int i;
9071
9072 if (oldargs == newargs)
9073 return 1;
9074
9075 if (!oldargs || !newargs)
9076 return 0;
9077
9078 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9079 return 0;
9080
9081 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9082 {
9083 tree nt = TREE_VEC_ELT (newargs, i);
9084 tree ot = TREE_VEC_ELT (oldargs, i);
9085
9086 if (! template_args_equal (ot, nt, partial_order))
9087 {
9088 if (oldarg_ptr != NULL)
9089 *oldarg_ptr = ot;
9090 if (newarg_ptr != NULL)
9091 *newarg_ptr = nt;
9092 return 0;
9093 }
9094 }
9095 return 1;
9096 }
9097
9098 inline bool
9099 comp_template_args_porder (tree oargs, tree nargs)
9100 {
9101 return comp_template_args (oargs, nargs, NULL, NULL, true);
9102 }
9103
9104 /* Implement a freelist interface for objects of type T.
9105
9106 Head is a separate object, rather than a regular member, so that we
9107 can define it as a GTY deletable pointer, which is highly
9108 desirable. A data member could be declared that way, but then the
9109 containing object would implicitly get GTY((user)), which would
9110 prevent us from instantiating freelists as global objects.
9111 Although this way we can create freelist global objects, they're
9112 such thin wrappers that instantiating temporaries at every use
9113 loses nothing and saves permanent storage for the freelist object.
9114
9115 Member functions next, anew, poison and reinit have default
9116 implementations that work for most of the types we're interested
9117 in, but if they don't work for some type, they should be explicitly
9118 specialized. See the comments before them for requirements, and
9119 the example specializations for the tree_list_freelist. */
9120 template <typename T>
9121 class freelist
9122 {
9123 /* Return the next object in a chain. We could just do type
9124 punning, but if we access the object with its underlying type, we
9125 avoid strict-aliasing trouble. This needs only work between
9126 poison and reinit. */
9127 static T *&next (T *obj) { return obj->next; }
9128
9129 /* Return a newly allocated, uninitialized or minimally-initialized
9130 object of type T. Any initialization performed by anew should
9131 either remain across the life of the object and the execution of
9132 poison, or be redone by reinit. */
9133 static T *anew () { return ggc_alloc<T> (); }
9134
9135 /* Optionally scribble all over the bits holding the object, so that
9136 they become (mostly?) uninitialized memory. This is called while
9137 preparing to make the object part of the free list. */
9138 static void poison (T *obj) {
9139 T *p ATTRIBUTE_UNUSED = obj;
9140 T **q ATTRIBUTE_UNUSED = &next (obj);
9141
9142 #ifdef ENABLE_GC_CHECKING
9143 /* Poison the data, to indicate the data is garbage. */
9144 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9145 memset (p, 0xa5, sizeof (*p));
9146 #endif
9147 /* Let valgrind know the object is free. */
9148 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9149
9150 /* Let valgrind know the next portion of the object is available,
9151 but uninitialized. */
9152 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9153 }
9154
9155 /* Bring an object that underwent at least one lifecycle after anew
9156 and before the most recent free and poison, back to a usable
9157 state, reinitializing whatever is needed for it to be
9158 functionally equivalent to an object just allocated and returned
9159 by anew. This may poison or clear the next field, used by
9160 freelist housekeeping after poison was called. */
9161 static void reinit (T *obj) {
9162 T **q ATTRIBUTE_UNUSED = &next (obj);
9163
9164 #ifdef ENABLE_GC_CHECKING
9165 memset (q, 0xa5, sizeof (*q));
9166 #endif
9167 /* Let valgrind know the entire object is available, but
9168 uninitialized. */
9169 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9170 }
9171
9172 /* Reference a GTY-deletable pointer that points to the first object
9173 in the free list proper. */
9174 T *&head;
9175 public:
9176 /* Construct a freelist object chaining objects off of HEAD. */
9177 freelist (T *&head) : head(head) {}
9178
9179 /* Add OBJ to the free object list. The former head becomes OBJ's
9180 successor. */
9181 void free (T *obj)
9182 {
9183 poison (obj);
9184 next (obj) = head;
9185 head = obj;
9186 }
9187
9188 /* Take an object from the free list, if one is available, or
9189 allocate a new one. Objects taken from the free list should be
9190 regarded as filled with garbage, except for bits that are
9191 configured to be preserved across free and alloc. */
9192 T *alloc ()
9193 {
9194 if (head)
9195 {
9196 T *obj = head;
9197 head = next (head);
9198 reinit (obj);
9199 return obj;
9200 }
9201 else
9202 return anew ();
9203 }
9204 };
9205
9206 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9207 want to allocate a TREE_LIST using the usual interface, and ensure
9208 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9209 build_tree_list logic in reinit, so this could go out of sync. */
9210 template <>
9211 inline tree &
9212 freelist<tree_node>::next (tree obj)
9213 {
9214 return TREE_CHAIN (obj);
9215 }
9216 template <>
9217 inline tree
9218 freelist<tree_node>::anew ()
9219 {
9220 return build_tree_list (NULL, NULL);
9221 }
9222 template <>
9223 inline void
9224 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9225 {
9226 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9227 tree p ATTRIBUTE_UNUSED = obj;
9228 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9229 tree *q ATTRIBUTE_UNUSED = &next (obj);
9230
9231 #ifdef ENABLE_GC_CHECKING
9232 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9233
9234 /* Poison the data, to indicate the data is garbage. */
9235 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9236 memset (p, 0xa5, size);
9237 #endif
9238 /* Let valgrind know the object is free. */
9239 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9240 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9241 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9242 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9243
9244 #ifdef ENABLE_GC_CHECKING
9245 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9246 /* Keep TREE_CHAIN functional. */
9247 TREE_SET_CODE (obj, TREE_LIST);
9248 #else
9249 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9250 #endif
9251 }
9252 template <>
9253 inline void
9254 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9255 {
9256 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9257
9258 #ifdef ENABLE_GC_CHECKING
9259 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9260 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9261 memset (obj, 0, sizeof (tree_list));
9262 #endif
9263
9264 /* Let valgrind know the entire object is available, but
9265 uninitialized. */
9266 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9267
9268 #ifdef ENABLE_GC_CHECKING
9269 TREE_SET_CODE (obj, TREE_LIST);
9270 #else
9271 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9272 #endif
9273 }
9274
9275 /* Point to the first object in the TREE_LIST freelist. */
9276 static GTY((deletable)) tree tree_list_freelist_head;
9277 /* Return the/an actual TREE_LIST freelist. */
9278 static inline freelist<tree_node>
9279 tree_list_freelist ()
9280 {
9281 return tree_list_freelist_head;
9282 }
9283
9284 /* Point to the first object in the tinst_level freelist. */
9285 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9286 /* Return the/an actual tinst_level freelist. */
9287 static inline freelist<tinst_level>
9288 tinst_level_freelist ()
9289 {
9290 return tinst_level_freelist_head;
9291 }
9292
9293 /* Point to the first object in the pending_template freelist. */
9294 static GTY((deletable)) pending_template *pending_template_freelist_head;
9295 /* Return the/an actual pending_template freelist. */
9296 static inline freelist<pending_template>
9297 pending_template_freelist ()
9298 {
9299 return pending_template_freelist_head;
9300 }
9301
9302 /* Build the TREE_LIST object out of a split list, store it
9303 permanently, and return it. */
9304 tree
9305 tinst_level::to_list ()
9306 {
9307 gcc_assert (split_list_p ());
9308 tree ret = tree_list_freelist ().alloc ();
9309 TREE_PURPOSE (ret) = tldcl;
9310 TREE_VALUE (ret) = targs;
9311 tldcl = ret;
9312 targs = NULL;
9313 gcc_assert (tree_list_p ());
9314 return ret;
9315 }
9316
9317 const unsigned short tinst_level::refcount_infinity;
9318
9319 /* Increment OBJ's refcount unless it is already infinite. */
9320 static tinst_level *
9321 inc_refcount_use (tinst_level *obj)
9322 {
9323 if (obj && obj->refcount != tinst_level::refcount_infinity)
9324 ++obj->refcount;
9325 return obj;
9326 }
9327
9328 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9329 void
9330 tinst_level::free (tinst_level *obj)
9331 {
9332 if (obj->tree_list_p ())
9333 tree_list_freelist ().free (obj->get_node ());
9334 tinst_level_freelist ().free (obj);
9335 }
9336
9337 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9338 OBJ's DECL and OBJ, and start over with the tinst_level object that
9339 used to be referenced by OBJ's NEXT. */
9340 static void
9341 dec_refcount_use (tinst_level *obj)
9342 {
9343 while (obj
9344 && obj->refcount != tinst_level::refcount_infinity
9345 && !--obj->refcount)
9346 {
9347 tinst_level *next = obj->next;
9348 tinst_level::free (obj);
9349 obj = next;
9350 }
9351 }
9352
9353 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9354 and of the former PTR. Omitting the second argument is equivalent
9355 to passing (T*)NULL; this is allowed because passing the
9356 zero-valued integral constant NULL confuses type deduction and/or
9357 overload resolution. */
9358 template <typename T>
9359 static void
9360 set_refcount_ptr (T *& ptr, T *obj = NULL)
9361 {
9362 T *save = ptr;
9363 ptr = inc_refcount_use (obj);
9364 dec_refcount_use (save);
9365 }
9366
9367 static void
9368 add_pending_template (tree d)
9369 {
9370 tree ti = (TYPE_P (d)
9371 ? CLASSTYPE_TEMPLATE_INFO (d)
9372 : DECL_TEMPLATE_INFO (d));
9373 struct pending_template *pt;
9374 int level;
9375
9376 if (TI_PENDING_TEMPLATE_FLAG (ti))
9377 return;
9378
9379 /* We are called both from instantiate_decl, where we've already had a
9380 tinst_level pushed, and instantiate_template, where we haven't.
9381 Compensate. */
9382 gcc_assert (TREE_CODE (d) != TREE_LIST);
9383 level = !current_tinst_level
9384 || current_tinst_level->maybe_get_node () != d;
9385
9386 if (level)
9387 push_tinst_level (d);
9388
9389 pt = pending_template_freelist ().alloc ();
9390 pt->next = NULL;
9391 pt->tinst = NULL;
9392 set_refcount_ptr (pt->tinst, current_tinst_level);
9393 if (last_pending_template)
9394 last_pending_template->next = pt;
9395 else
9396 pending_templates = pt;
9397
9398 last_pending_template = pt;
9399
9400 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9401
9402 if (level)
9403 pop_tinst_level ();
9404 }
9405
9406
9407 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9408 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9409 documentation for TEMPLATE_ID_EXPR. */
9410
9411 tree
9412 lookup_template_function (tree fns, tree arglist)
9413 {
9414 if (fns == error_mark_node || arglist == error_mark_node)
9415 return error_mark_node;
9416
9417 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9418
9419 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9420 {
9421 error ("%q#D is not a function template", fns);
9422 return error_mark_node;
9423 }
9424
9425 if (BASELINK_P (fns))
9426 {
9427 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9428 unknown_type_node,
9429 BASELINK_FUNCTIONS (fns),
9430 arglist);
9431 return fns;
9432 }
9433
9434 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9435 }
9436
9437 /* Within the scope of a template class S<T>, the name S gets bound
9438 (in build_self_reference) to a TYPE_DECL for the class, not a
9439 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9440 or one of its enclosing classes, and that type is a template,
9441 return the associated TEMPLATE_DECL. Otherwise, the original
9442 DECL is returned.
9443
9444 Also handle the case when DECL is a TREE_LIST of ambiguous
9445 injected-class-names from different bases. */
9446
9447 tree
9448 maybe_get_template_decl_from_type_decl (tree decl)
9449 {
9450 if (decl == NULL_TREE)
9451 return decl;
9452
9453 /* DR 176: A lookup that finds an injected-class-name (10.2
9454 [class.member.lookup]) can result in an ambiguity in certain cases
9455 (for example, if it is found in more than one base class). If all of
9456 the injected-class-names that are found refer to specializations of
9457 the same class template, and if the name is followed by a
9458 template-argument-list, the reference refers to the class template
9459 itself and not a specialization thereof, and is not ambiguous. */
9460 if (TREE_CODE (decl) == TREE_LIST)
9461 {
9462 tree t, tmpl = NULL_TREE;
9463 for (t = decl; t; t = TREE_CHAIN (t))
9464 {
9465 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9466 if (!tmpl)
9467 tmpl = elt;
9468 else if (tmpl != elt)
9469 break;
9470 }
9471 if (tmpl && t == NULL_TREE)
9472 return tmpl;
9473 else
9474 return decl;
9475 }
9476
9477 return (decl != NULL_TREE
9478 && DECL_SELF_REFERENCE_P (decl)
9479 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9480 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9481 }
9482
9483 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9484 parameters, find the desired type.
9485
9486 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9487
9488 IN_DECL, if non-NULL, is the template declaration we are trying to
9489 instantiate.
9490
9491 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9492 the class we are looking up.
9493
9494 Issue error and warning messages under control of COMPLAIN.
9495
9496 If the template class is really a local class in a template
9497 function, then the FUNCTION_CONTEXT is the function in which it is
9498 being instantiated.
9499
9500 ??? Note that this function is currently called *twice* for each
9501 template-id: the first time from the parser, while creating the
9502 incomplete type (finish_template_type), and the second type during the
9503 real instantiation (instantiate_template_class). This is surely something
9504 that we want to avoid. It also causes some problems with argument
9505 coercion (see convert_nontype_argument for more information on this). */
9506
9507 static tree
9508 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9509 int entering_scope, tsubst_flags_t complain)
9510 {
9511 tree templ = NULL_TREE, parmlist;
9512 tree t;
9513 spec_entry **slot;
9514 spec_entry *entry;
9515 spec_entry elt;
9516 hashval_t hash;
9517
9518 if (identifier_p (d1))
9519 {
9520 tree value = innermost_non_namespace_value (d1);
9521 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9522 templ = value;
9523 else
9524 {
9525 if (context)
9526 push_decl_namespace (context);
9527 templ = lookup_name (d1);
9528 templ = maybe_get_template_decl_from_type_decl (templ);
9529 if (context)
9530 pop_decl_namespace ();
9531 }
9532 if (templ)
9533 context = DECL_CONTEXT (templ);
9534 }
9535 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9536 {
9537 tree type = TREE_TYPE (d1);
9538
9539 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9540 an implicit typename for the second A. Deal with it. */
9541 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9542 type = TREE_TYPE (type);
9543
9544 if (CLASSTYPE_TEMPLATE_INFO (type))
9545 {
9546 templ = CLASSTYPE_TI_TEMPLATE (type);
9547 d1 = DECL_NAME (templ);
9548 }
9549 }
9550 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9551 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9552 {
9553 templ = TYPE_TI_TEMPLATE (d1);
9554 d1 = DECL_NAME (templ);
9555 }
9556 else if (DECL_TYPE_TEMPLATE_P (d1))
9557 {
9558 templ = d1;
9559 d1 = DECL_NAME (templ);
9560 context = DECL_CONTEXT (templ);
9561 }
9562 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9563 {
9564 templ = d1;
9565 d1 = DECL_NAME (templ);
9566 }
9567
9568 /* Issue an error message if we didn't find a template. */
9569 if (! templ)
9570 {
9571 if (complain & tf_error)
9572 error ("%qT is not a template", d1);
9573 return error_mark_node;
9574 }
9575
9576 if (TREE_CODE (templ) != TEMPLATE_DECL
9577 /* Make sure it's a user visible template, if it was named by
9578 the user. */
9579 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9580 && !PRIMARY_TEMPLATE_P (templ)))
9581 {
9582 if (complain & tf_error)
9583 {
9584 error ("non-template type %qT used as a template", d1);
9585 if (in_decl)
9586 error ("for template declaration %q+D", in_decl);
9587 }
9588 return error_mark_node;
9589 }
9590
9591 complain &= ~tf_user;
9592
9593 /* An alias that just changes the name of a template is equivalent to the
9594 other template, so if any of the arguments are pack expansions, strip
9595 the alias to avoid problems with a pack expansion passed to a non-pack
9596 alias template parameter (DR 1430). */
9597 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9598 templ = get_underlying_template (templ);
9599
9600 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9601 {
9602 tree parm;
9603 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9604 if (arglist2 == error_mark_node
9605 || (!uses_template_parms (arglist2)
9606 && check_instantiated_args (templ, arglist2, complain)))
9607 return error_mark_node;
9608
9609 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9610 return parm;
9611 }
9612 else
9613 {
9614 tree template_type = TREE_TYPE (templ);
9615 tree gen_tmpl;
9616 tree type_decl;
9617 tree found = NULL_TREE;
9618 int arg_depth;
9619 int parm_depth;
9620 int is_dependent_type;
9621 int use_partial_inst_tmpl = false;
9622
9623 if (template_type == error_mark_node)
9624 /* An error occurred while building the template TEMPL, and a
9625 diagnostic has most certainly been emitted for that
9626 already. Let's propagate that error. */
9627 return error_mark_node;
9628
9629 gen_tmpl = most_general_template (templ);
9630 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9631 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9632 arg_depth = TMPL_ARGS_DEPTH (arglist);
9633
9634 if (arg_depth == 1 && parm_depth > 1)
9635 {
9636 /* We've been given an incomplete set of template arguments.
9637 For example, given:
9638
9639 template <class T> struct S1 {
9640 template <class U> struct S2 {};
9641 template <class U> struct S2<U*> {};
9642 };
9643
9644 we will be called with an ARGLIST of `U*', but the
9645 TEMPLATE will be `template <class T> template
9646 <class U> struct S1<T>::S2'. We must fill in the missing
9647 arguments. */
9648 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9649 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9650 arg_depth = TMPL_ARGS_DEPTH (arglist);
9651 }
9652
9653 /* Now we should have enough arguments. */
9654 gcc_assert (parm_depth == arg_depth);
9655
9656 /* From here on, we're only interested in the most general
9657 template. */
9658
9659 /* Calculate the BOUND_ARGS. These will be the args that are
9660 actually tsubst'd into the definition to create the
9661 instantiation. */
9662 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9663 complain,
9664 /*require_all_args=*/true,
9665 /*use_default_args=*/true);
9666
9667 if (arglist == error_mark_node)
9668 /* We were unable to bind the arguments. */
9669 return error_mark_node;
9670
9671 /* In the scope of a template class, explicit references to the
9672 template class refer to the type of the template, not any
9673 instantiation of it. For example, in:
9674
9675 template <class T> class C { void f(C<T>); }
9676
9677 the `C<T>' is just the same as `C'. Outside of the
9678 class, however, such a reference is an instantiation. */
9679 if (entering_scope
9680 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9681 || currently_open_class (template_type))
9682 {
9683 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9684
9685 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9686 return template_type;
9687 }
9688
9689 /* If we already have this specialization, return it. */
9690 elt.tmpl = gen_tmpl;
9691 elt.args = arglist;
9692 elt.spec = NULL_TREE;
9693 hash = spec_hasher::hash (&elt);
9694 entry = type_specializations->find_with_hash (&elt, hash);
9695
9696 if (entry)
9697 return entry->spec;
9698
9699 /* If the the template's constraints are not satisfied,
9700 then we cannot form a valid type.
9701
9702 Note that the check is deferred until after the hash
9703 lookup. This prevents redundant checks on previously
9704 instantiated specializations. */
9705 if (flag_concepts
9706 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9707 && !constraints_satisfied_p (gen_tmpl, arglist))
9708 {
9709 if (complain & tf_error)
9710 {
9711 auto_diagnostic_group d;
9712 error ("template constraint failure for %qD", gen_tmpl);
9713 diagnose_constraints (input_location, gen_tmpl, arglist);
9714 }
9715 return error_mark_node;
9716 }
9717
9718 is_dependent_type = uses_template_parms (arglist);
9719
9720 /* If the deduced arguments are invalid, then the binding
9721 failed. */
9722 if (!is_dependent_type
9723 && check_instantiated_args (gen_tmpl,
9724 INNERMOST_TEMPLATE_ARGS (arglist),
9725 complain))
9726 return error_mark_node;
9727
9728 if (!is_dependent_type
9729 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9730 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9731 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9732 {
9733 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9734 DECL_NAME (gen_tmpl),
9735 /*tag_scope=*/ts_global);
9736 return found;
9737 }
9738
9739 context = DECL_CONTEXT (gen_tmpl);
9740 if (context && TYPE_P (context))
9741 {
9742 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9743 context = complete_type (context);
9744 }
9745 else
9746 context = tsubst (context, arglist, complain, in_decl);
9747
9748 if (context == error_mark_node)
9749 return error_mark_node;
9750
9751 if (!context)
9752 context = global_namespace;
9753
9754 /* Create the type. */
9755 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9756 {
9757 /* The user referred to a specialization of an alias
9758 template represented by GEN_TMPL.
9759
9760 [temp.alias]/2 says:
9761
9762 When a template-id refers to the specialization of an
9763 alias template, it is equivalent to the associated
9764 type obtained by substitution of its
9765 template-arguments for the template-parameters in the
9766 type-id of the alias template. */
9767
9768 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9769 /* Note that the call above (by indirectly calling
9770 register_specialization in tsubst_decl) registers the
9771 TYPE_DECL representing the specialization of the alias
9772 template. So next time someone substitutes ARGLIST for
9773 the template parms into the alias template (GEN_TMPL),
9774 she'll get that TYPE_DECL back. */
9775
9776 if (t == error_mark_node)
9777 return t;
9778 }
9779 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9780 {
9781 if (!is_dependent_type)
9782 {
9783 set_current_access_from_decl (TYPE_NAME (template_type));
9784 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9785 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9786 arglist, complain, in_decl),
9787 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9788 arglist, complain, in_decl),
9789 SCOPED_ENUM_P (template_type), NULL);
9790
9791 if (t == error_mark_node)
9792 return t;
9793 }
9794 else
9795 {
9796 /* We don't want to call start_enum for this type, since
9797 the values for the enumeration constants may involve
9798 template parameters. And, no one should be interested
9799 in the enumeration constants for such a type. */
9800 t = cxx_make_type (ENUMERAL_TYPE);
9801 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9802 }
9803 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9804 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9805 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9806 }
9807 else if (CLASS_TYPE_P (template_type))
9808 {
9809 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9810 instantiated here. */
9811 gcc_assert (!LAMBDA_TYPE_P (template_type));
9812
9813 t = make_class_type (TREE_CODE (template_type));
9814 CLASSTYPE_DECLARED_CLASS (t)
9815 = CLASSTYPE_DECLARED_CLASS (template_type);
9816 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9817
9818 /* A local class. Make sure the decl gets registered properly. */
9819 if (context == current_function_decl)
9820 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9821 == error_mark_node)
9822 return error_mark_node;
9823
9824 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9825 /* This instantiation is another name for the primary
9826 template type. Set the TYPE_CANONICAL field
9827 appropriately. */
9828 TYPE_CANONICAL (t) = template_type;
9829 else if (any_template_arguments_need_structural_equality_p (arglist))
9830 /* Some of the template arguments require structural
9831 equality testing, so this template class requires
9832 structural equality testing. */
9833 SET_TYPE_STRUCTURAL_EQUALITY (t);
9834 }
9835 else
9836 gcc_unreachable ();
9837
9838 /* If we called start_enum or pushtag above, this information
9839 will already be set up. */
9840 if (!TYPE_NAME (t))
9841 {
9842 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9843
9844 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9845 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9846 DECL_SOURCE_LOCATION (type_decl)
9847 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9848 }
9849 else
9850 type_decl = TYPE_NAME (t);
9851
9852 if (CLASS_TYPE_P (template_type))
9853 {
9854 TREE_PRIVATE (type_decl)
9855 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9856 TREE_PROTECTED (type_decl)
9857 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9858 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9859 {
9860 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9861 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9862 }
9863 }
9864
9865 if (OVERLOAD_TYPE_P (t)
9866 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9867 {
9868 static const char *tags[] = {"abi_tag", "may_alias"};
9869
9870 for (unsigned ix = 0; ix != 2; ix++)
9871 {
9872 tree attributes
9873 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9874
9875 if (attributes)
9876 TYPE_ATTRIBUTES (t)
9877 = tree_cons (TREE_PURPOSE (attributes),
9878 TREE_VALUE (attributes),
9879 TYPE_ATTRIBUTES (t));
9880 }
9881 }
9882
9883 /* Let's consider the explicit specialization of a member
9884 of a class template specialization that is implicitly instantiated,
9885 e.g.:
9886 template<class T>
9887 struct S
9888 {
9889 template<class U> struct M {}; //#0
9890 };
9891
9892 template<>
9893 template<>
9894 struct S<int>::M<char> //#1
9895 {
9896 int i;
9897 };
9898 [temp.expl.spec]/4 says this is valid.
9899
9900 In this case, when we write:
9901 S<int>::M<char> m;
9902
9903 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9904 the one of #0.
9905
9906 When we encounter #1, we want to store the partial instantiation
9907 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9908
9909 For all cases other than this "explicit specialization of member of a
9910 class template", we just want to store the most general template into
9911 the CLASSTYPE_TI_TEMPLATE of M.
9912
9913 This case of "explicit specialization of member of a class template"
9914 only happens when:
9915 1/ the enclosing class is an instantiation of, and therefore not
9916 the same as, the context of the most general template, and
9917 2/ we aren't looking at the partial instantiation itself, i.e.
9918 the innermost arguments are not the same as the innermost parms of
9919 the most general template.
9920
9921 So it's only when 1/ and 2/ happens that we want to use the partial
9922 instantiation of the member template in lieu of its most general
9923 template. */
9924
9925 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9926 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9927 /* the enclosing class must be an instantiation... */
9928 && CLASS_TYPE_P (context)
9929 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9930 {
9931 TREE_VEC_LENGTH (arglist)--;
9932 ++processing_template_decl;
9933 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9934 tree partial_inst_args =
9935 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9936 arglist, complain, NULL_TREE);
9937 --processing_template_decl;
9938 TREE_VEC_LENGTH (arglist)++;
9939 if (partial_inst_args == error_mark_node)
9940 return error_mark_node;
9941 use_partial_inst_tmpl =
9942 /*...and we must not be looking at the partial instantiation
9943 itself. */
9944 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9945 partial_inst_args);
9946 }
9947
9948 if (!use_partial_inst_tmpl)
9949 /* This case is easy; there are no member templates involved. */
9950 found = gen_tmpl;
9951 else
9952 {
9953 /* This is a full instantiation of a member template. Find
9954 the partial instantiation of which this is an instance. */
9955
9956 /* Temporarily reduce by one the number of levels in the ARGLIST
9957 so as to avoid comparing the last set of arguments. */
9958 TREE_VEC_LENGTH (arglist)--;
9959 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9960 TREE_VEC_LENGTH (arglist)++;
9961 /* FOUND is either a proper class type, or an alias
9962 template specialization. In the later case, it's a
9963 TYPE_DECL, resulting from the substituting of arguments
9964 for parameters in the TYPE_DECL of the alias template
9965 done earlier. So be careful while getting the template
9966 of FOUND. */
9967 found = (TREE_CODE (found) == TEMPLATE_DECL
9968 ? found
9969 : (TREE_CODE (found) == TYPE_DECL
9970 ? DECL_TI_TEMPLATE (found)
9971 : CLASSTYPE_TI_TEMPLATE (found)));
9972
9973 if (DECL_CLASS_TEMPLATE_P (found)
9974 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9975 {
9976 /* If this partial instantiation is specialized, we want to
9977 use it for hash table lookup. */
9978 elt.tmpl = found;
9979 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9980 hash = spec_hasher::hash (&elt);
9981 }
9982 }
9983
9984 // Build template info for the new specialization.
9985 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9986
9987 elt.spec = t;
9988 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9989 gcc_checking_assert (*slot == NULL);
9990 entry = ggc_alloc<spec_entry> ();
9991 *entry = elt;
9992 *slot = entry;
9993
9994 /* Note this use of the partial instantiation so we can check it
9995 later in maybe_process_partial_specialization. */
9996 DECL_TEMPLATE_INSTANTIATIONS (found)
9997 = tree_cons (arglist, t,
9998 DECL_TEMPLATE_INSTANTIATIONS (found));
9999
10000 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
10001 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10002 /* Now that the type has been registered on the instantiations
10003 list, we set up the enumerators. Because the enumeration
10004 constants may involve the enumeration type itself, we make
10005 sure to register the type first, and then create the
10006 constants. That way, doing tsubst_expr for the enumeration
10007 constants won't result in recursive calls here; we'll find
10008 the instantiation and exit above. */
10009 tsubst_enum (template_type, t, arglist);
10010
10011 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10012 /* If the type makes use of template parameters, the
10013 code that generates debugging information will crash. */
10014 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10015
10016 /* Possibly limit visibility based on template args. */
10017 TREE_PUBLIC (type_decl) = 1;
10018 determine_visibility (type_decl);
10019
10020 inherit_targ_abi_tags (t);
10021
10022 return t;
10023 }
10024 }
10025
10026 /* Wrapper for lookup_template_class_1. */
10027
10028 tree
10029 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10030 int entering_scope, tsubst_flags_t complain)
10031 {
10032 tree ret;
10033 timevar_push (TV_TEMPLATE_INST);
10034 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10035 entering_scope, complain);
10036 timevar_pop (TV_TEMPLATE_INST);
10037 return ret;
10038 }
10039
10040 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10041
10042 tree
10043 lookup_template_variable (tree templ, tree arglist)
10044 {
10045 if (flag_concepts && variable_concept_p (templ))
10046 return build_concept_check (templ, arglist, tf_none);
10047
10048 /* The type of the expression is NULL_TREE since the template-id could refer
10049 to an explicit or partial specialization. */
10050 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10051 }
10052
10053 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10054
10055 tree
10056 finish_template_variable (tree var, tsubst_flags_t complain)
10057 {
10058 tree templ = TREE_OPERAND (var, 0);
10059 tree arglist = TREE_OPERAND (var, 1);
10060
10061 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10062 arglist = add_outermost_template_args (tmpl_args, arglist);
10063
10064 templ = most_general_template (templ);
10065 tree parms = DECL_TEMPLATE_PARMS (templ);
10066 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10067 /*req_all*/true,
10068 /*use_default*/true);
10069
10070 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10071 {
10072 if (complain & tf_error)
10073 {
10074 auto_diagnostic_group d;
10075 error ("use of invalid variable template %qE", var);
10076 diagnose_constraints (location_of (var), templ, arglist);
10077 }
10078 return error_mark_node;
10079 }
10080
10081 return instantiate_template (templ, arglist, complain);
10082 }
10083
10084 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10085 TARGS template args, and instantiate it if it's not dependent. */
10086
10087 tree
10088 lookup_and_finish_template_variable (tree templ, tree targs,
10089 tsubst_flags_t complain)
10090 {
10091 templ = lookup_template_variable (templ, targs);
10092 if (!any_dependent_template_arguments_p (targs))
10093 {
10094 templ = finish_template_variable (templ, complain);
10095 mark_used (templ);
10096 }
10097
10098 return convert_from_reference (templ);
10099 }
10100
10101 \f
10102 struct pair_fn_data
10103 {
10104 tree_fn_t fn;
10105 tree_fn_t any_fn;
10106 void *data;
10107 /* True when we should also visit template parameters that occur in
10108 non-deduced contexts. */
10109 bool include_nondeduced_p;
10110 hash_set<tree> *visited;
10111 };
10112
10113 /* Called from for_each_template_parm via walk_tree. */
10114
10115 static tree
10116 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10117 {
10118 tree t = *tp;
10119 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10120 tree_fn_t fn = pfd->fn;
10121 void *data = pfd->data;
10122 tree result = NULL_TREE;
10123
10124 #define WALK_SUBTREE(NODE) \
10125 do \
10126 { \
10127 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10128 pfd->include_nondeduced_p, \
10129 pfd->any_fn); \
10130 if (result) goto out; \
10131 } \
10132 while (0)
10133
10134 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10135 return t;
10136
10137 if (TYPE_P (t)
10138 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10139 WALK_SUBTREE (TYPE_CONTEXT (t));
10140
10141 switch (TREE_CODE (t))
10142 {
10143 case RECORD_TYPE:
10144 if (TYPE_PTRMEMFUNC_P (t))
10145 break;
10146 /* Fall through. */
10147
10148 case UNION_TYPE:
10149 case ENUMERAL_TYPE:
10150 if (!TYPE_TEMPLATE_INFO (t))
10151 *walk_subtrees = 0;
10152 else
10153 WALK_SUBTREE (TYPE_TI_ARGS (t));
10154 break;
10155
10156 case INTEGER_TYPE:
10157 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10158 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10159 break;
10160
10161 case METHOD_TYPE:
10162 /* Since we're not going to walk subtrees, we have to do this
10163 explicitly here. */
10164 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10165 /* Fall through. */
10166
10167 case FUNCTION_TYPE:
10168 /* Check the return type. */
10169 WALK_SUBTREE (TREE_TYPE (t));
10170
10171 /* Check the parameter types. Since default arguments are not
10172 instantiated until they are needed, the TYPE_ARG_TYPES may
10173 contain expressions that involve template parameters. But,
10174 no-one should be looking at them yet. And, once they're
10175 instantiated, they don't contain template parameters, so
10176 there's no point in looking at them then, either. */
10177 {
10178 tree parm;
10179
10180 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10181 WALK_SUBTREE (TREE_VALUE (parm));
10182
10183 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10184 want walk_tree walking into them itself. */
10185 *walk_subtrees = 0;
10186 }
10187
10188 if (flag_noexcept_type)
10189 {
10190 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10191 if (spec)
10192 WALK_SUBTREE (TREE_PURPOSE (spec));
10193 }
10194 break;
10195
10196 case TYPEOF_TYPE:
10197 case DECLTYPE_TYPE:
10198 case UNDERLYING_TYPE:
10199 if (pfd->include_nondeduced_p
10200 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10201 pfd->visited,
10202 pfd->include_nondeduced_p,
10203 pfd->any_fn))
10204 return error_mark_node;
10205 *walk_subtrees = false;
10206 break;
10207
10208 case FUNCTION_DECL:
10209 case VAR_DECL:
10210 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10211 WALK_SUBTREE (DECL_TI_ARGS (t));
10212 /* Fall through. */
10213
10214 case PARM_DECL:
10215 case CONST_DECL:
10216 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10217 WALK_SUBTREE (DECL_INITIAL (t));
10218 if (DECL_CONTEXT (t)
10219 && pfd->include_nondeduced_p)
10220 WALK_SUBTREE (DECL_CONTEXT (t));
10221 break;
10222
10223 case BOUND_TEMPLATE_TEMPLATE_PARM:
10224 /* Record template parameters such as `T' inside `TT<T>'. */
10225 WALK_SUBTREE (TYPE_TI_ARGS (t));
10226 /* Fall through. */
10227
10228 case TEMPLATE_TEMPLATE_PARM:
10229 case TEMPLATE_TYPE_PARM:
10230 case TEMPLATE_PARM_INDEX:
10231 if (fn && (*fn)(t, data))
10232 return t;
10233 else if (!fn)
10234 return t;
10235 break;
10236
10237 case TEMPLATE_DECL:
10238 /* A template template parameter is encountered. */
10239 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10240 WALK_SUBTREE (TREE_TYPE (t));
10241
10242 /* Already substituted template template parameter */
10243 *walk_subtrees = 0;
10244 break;
10245
10246 case TYPENAME_TYPE:
10247 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10248 partial instantiation. */
10249 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10250 break;
10251
10252 case CONSTRUCTOR:
10253 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10254 && pfd->include_nondeduced_p)
10255 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10256 break;
10257
10258 case INDIRECT_REF:
10259 case COMPONENT_REF:
10260 /* If there's no type, then this thing must be some expression
10261 involving template parameters. */
10262 if (!fn && !TREE_TYPE (t))
10263 return error_mark_node;
10264 break;
10265
10266 case MODOP_EXPR:
10267 case CAST_EXPR:
10268 case IMPLICIT_CONV_EXPR:
10269 case REINTERPRET_CAST_EXPR:
10270 case CONST_CAST_EXPR:
10271 case STATIC_CAST_EXPR:
10272 case DYNAMIC_CAST_EXPR:
10273 case ARROW_EXPR:
10274 case DOTSTAR_EXPR:
10275 case TYPEID_EXPR:
10276 case PSEUDO_DTOR_EXPR:
10277 if (!fn)
10278 return error_mark_node;
10279 break;
10280
10281 case SCOPE_REF:
10282 if (pfd->include_nondeduced_p)
10283 WALK_SUBTREE (TREE_OPERAND (t, 0));
10284 break;
10285
10286 case REQUIRES_EXPR:
10287 {
10288 if (!fn)
10289 return error_mark_node;
10290
10291 /* Recursively walk the type of each constraint variable. */
10292 tree p = TREE_OPERAND (t, 0);
10293 while (p)
10294 {
10295 WALK_SUBTREE (TREE_TYPE (p));
10296 p = TREE_CHAIN (p);
10297 }
10298 }
10299 break;
10300
10301 default:
10302 break;
10303 }
10304
10305 #undef WALK_SUBTREE
10306
10307 /* We didn't find any template parameters we liked. */
10308 out:
10309 return result;
10310 }
10311
10312 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10313 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10314 call FN with the parameter and the DATA.
10315 If FN returns nonzero, the iteration is terminated, and
10316 for_each_template_parm returns 1. Otherwise, the iteration
10317 continues. If FN never returns a nonzero value, the value
10318 returned by for_each_template_parm is 0. If FN is NULL, it is
10319 considered to be the function which always returns 1.
10320
10321 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10322 parameters that occur in non-deduced contexts. When false, only
10323 visits those template parameters that can be deduced. */
10324
10325 static tree
10326 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10327 hash_set<tree> *visited,
10328 bool include_nondeduced_p,
10329 tree_fn_t any_fn)
10330 {
10331 struct pair_fn_data pfd;
10332 tree result;
10333
10334 /* Set up. */
10335 pfd.fn = fn;
10336 pfd.any_fn = any_fn;
10337 pfd.data = data;
10338 pfd.include_nondeduced_p = include_nondeduced_p;
10339
10340 /* Walk the tree. (Conceptually, we would like to walk without
10341 duplicates, but for_each_template_parm_r recursively calls
10342 for_each_template_parm, so we would need to reorganize a fair
10343 bit to use walk_tree_without_duplicates, so we keep our own
10344 visited list.) */
10345 if (visited)
10346 pfd.visited = visited;
10347 else
10348 pfd.visited = new hash_set<tree>;
10349 result = cp_walk_tree (&t,
10350 for_each_template_parm_r,
10351 &pfd,
10352 pfd.visited);
10353
10354 /* Clean up. */
10355 if (!visited)
10356 {
10357 delete pfd.visited;
10358 pfd.visited = 0;
10359 }
10360
10361 return result;
10362 }
10363
10364 struct find_template_parameter_info
10365 {
10366 explicit find_template_parameter_info (int d)
10367 : max_depth (d)
10368 {}
10369
10370 hash_set<tree> visited;
10371 hash_set<tree> parms;
10372 int max_depth;
10373 };
10374
10375 /* Appends the declaration of T to the list in DATA. */
10376
10377 static int
10378 keep_template_parm (tree t, void* data)
10379 {
10380 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10381
10382 /* Template parameters declared within the expression are not part of
10383 the parameter mapping. For example, in this concept:
10384
10385 template<typename T>
10386 concept C = requires { <expr> } -> same_as<int>;
10387
10388 the return specifier same_as<int> declares a new decltype parameter
10389 that must not be part of the parameter mapping. The same is true
10390 for generic lambda parameters, lambda template parameters, etc. */
10391 int level;
10392 int index;
10393 template_parm_level_and_index (t, &level, &index);
10394 if (level > ftpi->max_depth)
10395 return 0;
10396
10397 /* Arguments like const T yield parameters like const T. This means that
10398 a template-id like X<T, const T> would yield two distinct parameters:
10399 T and const T. Adjust types to their unqualified versions. */
10400 if (TYPE_P (t))
10401 t = TYPE_MAIN_VARIANT (t);
10402 ftpi->parms.add (t);
10403
10404 return 0;
10405 }
10406
10407 /* Ensure that we recursively examine certain terms that are not normally
10408 visited in for_each_template_parm_r. */
10409
10410 static int
10411 any_template_parm_r (tree t, void *data)
10412 {
10413 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10414
10415 #define WALK_SUBTREE(NODE) \
10416 do \
10417 { \
10418 for_each_template_parm (NODE, keep_template_parm, data, \
10419 &ftpi->visited, true, \
10420 any_template_parm_r); \
10421 } \
10422 while (0)
10423
10424 switch (TREE_CODE (t))
10425 {
10426 case RECORD_TYPE:
10427 case UNION_TYPE:
10428 case ENUMERAL_TYPE:
10429 /* Search for template parameters in type aliases. */
10430 if (tree ats = alias_template_specialization_p (t, nt_opaque))
10431 {
10432 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (ats);
10433 WALK_SUBTREE (TI_ARGS (tinfo));
10434 }
10435 break;
10436
10437 case TEMPLATE_TYPE_PARM:
10438 /* Type constraints of a placeholder type may contain parameters. */
10439 if (is_auto (t))
10440 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10441 WALK_SUBTREE (constr);
10442 break;
10443
10444 case TEMPLATE_ID_EXPR:
10445 /* Search through references to variable templates. */
10446 WALK_SUBTREE (TREE_OPERAND (t, 0));
10447 WALK_SUBTREE (TREE_OPERAND (t, 1));
10448 break;
10449
10450 case CONSTRUCTOR:
10451 if (TREE_TYPE (t))
10452 WALK_SUBTREE (TREE_TYPE (t));
10453 break;
10454
10455 case PARM_DECL:
10456 /* A parameter or constraint variable may also depend on a template
10457 parameter without explicitly naming it. */
10458 WALK_SUBTREE (TREE_TYPE (t));
10459 break;
10460
10461 default:
10462 break;
10463 }
10464
10465 /* Keep walking. */
10466 return 0;
10467 }
10468
10469 /* Returns a list of unique template parameters found within T. */
10470
10471 tree
10472 find_template_parameters (tree t, int depth)
10473 {
10474 find_template_parameter_info ftpi (depth);
10475 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10476 /*include_nondeduced*/true, any_template_parm_r);
10477 tree list = NULL_TREE;
10478 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10479 iter != ftpi.parms.end(); ++iter)
10480 list = tree_cons (NULL_TREE, *iter, list);
10481 return list;
10482 }
10483
10484 /* Returns true if T depends on any template parameter. */
10485
10486 int
10487 uses_template_parms (tree t)
10488 {
10489 if (t == NULL_TREE)
10490 return false;
10491
10492 bool dependent_p;
10493 int saved_processing_template_decl;
10494
10495 saved_processing_template_decl = processing_template_decl;
10496 if (!saved_processing_template_decl)
10497 processing_template_decl = 1;
10498 if (TYPE_P (t))
10499 dependent_p = dependent_type_p (t);
10500 else if (TREE_CODE (t) == TREE_VEC)
10501 dependent_p = any_dependent_template_arguments_p (t);
10502 else if (TREE_CODE (t) == TREE_LIST)
10503 dependent_p = (uses_template_parms (TREE_VALUE (t))
10504 || uses_template_parms (TREE_CHAIN (t)));
10505 else if (TREE_CODE (t) == TYPE_DECL)
10506 dependent_p = dependent_type_p (TREE_TYPE (t));
10507 else if (DECL_P (t)
10508 || EXPR_P (t)
10509 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10510 || TREE_CODE (t) == OVERLOAD
10511 || BASELINK_P (t)
10512 || identifier_p (t)
10513 || TREE_CODE (t) == TRAIT_EXPR
10514 || TREE_CODE (t) == CONSTRUCTOR
10515 || CONSTANT_CLASS_P (t))
10516 dependent_p = (type_dependent_expression_p (t)
10517 || value_dependent_expression_p (t));
10518 else
10519 {
10520 gcc_assert (t == error_mark_node);
10521 dependent_p = false;
10522 }
10523
10524 processing_template_decl = saved_processing_template_decl;
10525
10526 return dependent_p;
10527 }
10528
10529 /* Returns true iff current_function_decl is an incompletely instantiated
10530 template. Useful instead of processing_template_decl because the latter
10531 is set to 0 during instantiate_non_dependent_expr. */
10532
10533 bool
10534 in_template_function (void)
10535 {
10536 tree fn = current_function_decl;
10537 bool ret;
10538 ++processing_template_decl;
10539 ret = (fn && DECL_LANG_SPECIFIC (fn)
10540 && DECL_TEMPLATE_INFO (fn)
10541 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10542 --processing_template_decl;
10543 return ret;
10544 }
10545
10546 /* Returns true if T depends on any template parameter with level LEVEL. */
10547
10548 bool
10549 uses_template_parms_level (tree t, int level)
10550 {
10551 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10552 /*include_nondeduced_p=*/true);
10553 }
10554
10555 /* Returns true if the signature of DECL depends on any template parameter from
10556 its enclosing class. */
10557
10558 bool
10559 uses_outer_template_parms (tree decl)
10560 {
10561 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10562 if (depth == 0)
10563 return false;
10564 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10565 &depth, NULL, /*include_nondeduced_p=*/true))
10566 return true;
10567 if (PRIMARY_TEMPLATE_P (decl)
10568 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10569 (DECL_TEMPLATE_PARMS (decl)),
10570 template_parm_outer_level,
10571 &depth, NULL, /*include_nondeduced_p=*/true))
10572 return true;
10573 tree ci = get_constraints (decl);
10574 if (ci)
10575 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10576 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10577 &depth, NULL, /*nondeduced*/true))
10578 return true;
10579 return false;
10580 }
10581
10582 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10583 ill-formed translation unit, i.e. a variable or function that isn't
10584 usable in a constant expression. */
10585
10586 static inline bool
10587 neglectable_inst_p (tree d)
10588 {
10589 return (d && DECL_P (d)
10590 && !undeduced_auto_decl (d)
10591 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10592 : decl_maybe_constant_var_p (d)));
10593 }
10594
10595 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10596 neglectable and instantiated from within an erroneous instantiation. */
10597
10598 static bool
10599 limit_bad_template_recursion (tree decl)
10600 {
10601 struct tinst_level *lev = current_tinst_level;
10602 int errs = errorcount + sorrycount;
10603 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10604 return false;
10605
10606 for (; lev; lev = lev->next)
10607 if (neglectable_inst_p (lev->maybe_get_node ()))
10608 break;
10609
10610 return (lev && errs > lev->errors);
10611 }
10612
10613 static int tinst_depth;
10614 extern int max_tinst_depth;
10615 int depth_reached;
10616
10617 static GTY(()) struct tinst_level *last_error_tinst_level;
10618
10619 /* We're starting to instantiate D; record the template instantiation context
10620 at LOC for diagnostics and to restore it later. */
10621
10622 static bool
10623 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10624 {
10625 struct tinst_level *new_level;
10626
10627 if (tinst_depth >= max_tinst_depth)
10628 {
10629 /* Tell error.c not to try to instantiate any templates. */
10630 at_eof = 2;
10631 fatal_error (input_location,
10632 "template instantiation depth exceeds maximum of %d"
10633 " (use %<-ftemplate-depth=%> to increase the maximum)",
10634 max_tinst_depth);
10635 return false;
10636 }
10637
10638 /* If the current instantiation caused problems, don't let it instantiate
10639 anything else. Do allow deduction substitution and decls usable in
10640 constant expressions. */
10641 if (!targs && limit_bad_template_recursion (tldcl))
10642 return false;
10643
10644 /* When not -quiet, dump template instantiations other than functions, since
10645 announce_function will take care of those. */
10646 if (!quiet_flag && !targs
10647 && TREE_CODE (tldcl) != TREE_LIST
10648 && TREE_CODE (tldcl) != FUNCTION_DECL)
10649 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10650
10651 new_level = tinst_level_freelist ().alloc ();
10652 new_level->tldcl = tldcl;
10653 new_level->targs = targs;
10654 new_level->locus = loc;
10655 new_level->errors = errorcount + sorrycount;
10656 new_level->next = NULL;
10657 new_level->refcount = 0;
10658 set_refcount_ptr (new_level->next, current_tinst_level);
10659 set_refcount_ptr (current_tinst_level, new_level);
10660
10661 ++tinst_depth;
10662 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10663 depth_reached = tinst_depth;
10664
10665 return true;
10666 }
10667
10668 /* We're starting substitution of TMPL<ARGS>; record the template
10669 substitution context for diagnostics and to restore it later. */
10670
10671 static bool
10672 push_tinst_level (tree tmpl, tree args)
10673 {
10674 return push_tinst_level_loc (tmpl, args, input_location);
10675 }
10676
10677 /* We're starting to instantiate D; record INPUT_LOCATION and the
10678 template instantiation context for diagnostics and to restore it
10679 later. */
10680
10681 bool
10682 push_tinst_level (tree d)
10683 {
10684 return push_tinst_level_loc (d, input_location);
10685 }
10686
10687 /* Likewise, but record LOC as the program location. */
10688
10689 bool
10690 push_tinst_level_loc (tree d, location_t loc)
10691 {
10692 gcc_assert (TREE_CODE (d) != TREE_LIST);
10693 return push_tinst_level_loc (d, NULL, loc);
10694 }
10695
10696 /* We're done instantiating this template; return to the instantiation
10697 context. */
10698
10699 void
10700 pop_tinst_level (void)
10701 {
10702 /* Restore the filename and line number stashed away when we started
10703 this instantiation. */
10704 input_location = current_tinst_level->locus;
10705 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10706 --tinst_depth;
10707 }
10708
10709 /* We're instantiating a deferred template; restore the template
10710 instantiation context in which the instantiation was requested, which
10711 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10712
10713 static tree
10714 reopen_tinst_level (struct tinst_level *level)
10715 {
10716 struct tinst_level *t;
10717
10718 tinst_depth = 0;
10719 for (t = level; t; t = t->next)
10720 ++tinst_depth;
10721
10722 set_refcount_ptr (current_tinst_level, level);
10723 pop_tinst_level ();
10724 if (current_tinst_level)
10725 current_tinst_level->errors = errorcount+sorrycount;
10726 return level->maybe_get_node ();
10727 }
10728
10729 /* Returns the TINST_LEVEL which gives the original instantiation
10730 context. */
10731
10732 struct tinst_level *
10733 outermost_tinst_level (void)
10734 {
10735 struct tinst_level *level = current_tinst_level;
10736 if (level)
10737 while (level->next)
10738 level = level->next;
10739 return level;
10740 }
10741
10742 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10743 vector of template arguments, as for tsubst.
10744
10745 Returns an appropriate tsubst'd friend declaration. */
10746
10747 static tree
10748 tsubst_friend_function (tree decl, tree args)
10749 {
10750 tree new_friend;
10751
10752 if (TREE_CODE (decl) == FUNCTION_DECL
10753 && DECL_TEMPLATE_INSTANTIATION (decl)
10754 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10755 /* This was a friend declared with an explicit template
10756 argument list, e.g.:
10757
10758 friend void f<>(T);
10759
10760 to indicate that f was a template instantiation, not a new
10761 function declaration. Now, we have to figure out what
10762 instantiation of what template. */
10763 {
10764 tree template_id, arglist, fns;
10765 tree new_args;
10766 tree tmpl;
10767 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10768
10769 /* Friend functions are looked up in the containing namespace scope.
10770 We must enter that scope, to avoid finding member functions of the
10771 current class with same name. */
10772 push_nested_namespace (ns);
10773 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10774 tf_warning_or_error, NULL_TREE,
10775 /*integral_constant_expression_p=*/false);
10776 pop_nested_namespace (ns);
10777 arglist = tsubst (DECL_TI_ARGS (decl), args,
10778 tf_warning_or_error, NULL_TREE);
10779 template_id = lookup_template_function (fns, arglist);
10780
10781 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10782 tmpl = determine_specialization (template_id, new_friend,
10783 &new_args,
10784 /*need_member_template=*/0,
10785 TREE_VEC_LENGTH (args),
10786 tsk_none);
10787 return instantiate_template (tmpl, new_args, tf_error);
10788 }
10789
10790 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10791
10792 /* The NEW_FRIEND will look like an instantiation, to the
10793 compiler, but is not an instantiation from the point of view of
10794 the language. For example, we might have had:
10795
10796 template <class T> struct S {
10797 template <class U> friend void f(T, U);
10798 };
10799
10800 Then, in S<int>, template <class U> void f(int, U) is not an
10801 instantiation of anything. */
10802 if (new_friend == error_mark_node)
10803 return error_mark_node;
10804
10805 DECL_USE_TEMPLATE (new_friend) = 0;
10806 if (TREE_CODE (decl) == TEMPLATE_DECL)
10807 {
10808 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10809 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10810 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10811
10812 /* Attach the template requirements to the new declaration
10813 for declaration matching. We need to rebuild the requirements
10814 so that parameter levels match. */
10815 if (tree ci = get_constraints (decl))
10816 {
10817 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10818 tree args = generic_targs_for (new_friend);
10819 tree treqs = tsubst_constraint (CI_TEMPLATE_REQS (ci), args,
10820 tf_warning_or_error, NULL_TREE);
10821 tree freqs = tsubst_constraint (CI_DECLARATOR_REQS (ci), args,
10822 tf_warning_or_error, NULL_TREE);
10823
10824 /* Update the constraints -- these won't really be valid for
10825 checking, but that's not what we need them for. These ensure
10826 that the declared function can find the friend during
10827 declaration matching. */
10828 tree new_ci = get_constraints (new_friend);
10829 CI_TEMPLATE_REQS (new_ci) = treqs;
10830 CI_DECLARATOR_REQS (new_ci) = freqs;
10831
10832 /* Also update the template parameter list. */
10833 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10834 }
10835 }
10836
10837 /* The mangled name for the NEW_FRIEND is incorrect. The function
10838 is not a template instantiation and should not be mangled like
10839 one. Therefore, we forget the mangling here; we'll recompute it
10840 later if we need it. */
10841 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10842 {
10843 SET_DECL_RTL (new_friend, NULL);
10844 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10845 }
10846
10847 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10848 {
10849 tree old_decl;
10850 tree new_friend_template_info;
10851 tree new_friend_result_template_info;
10852 tree ns;
10853 int new_friend_is_defn;
10854
10855 /* We must save some information from NEW_FRIEND before calling
10856 duplicate decls since that function will free NEW_FRIEND if
10857 possible. */
10858 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10859 new_friend_is_defn =
10860 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10861 (template_for_substitution (new_friend)))
10862 != NULL_TREE);
10863 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10864 {
10865 /* This declaration is a `primary' template. */
10866 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10867
10868 new_friend_result_template_info
10869 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10870 }
10871 else
10872 new_friend_result_template_info = NULL_TREE;
10873
10874 /* Inside pushdecl_namespace_level, we will push into the
10875 current namespace. However, the friend function should go
10876 into the namespace of the template. */
10877 ns = decl_namespace_context (new_friend);
10878 push_nested_namespace (ns);
10879 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10880 pop_nested_namespace (ns);
10881
10882 if (old_decl == error_mark_node)
10883 return error_mark_node;
10884
10885 if (old_decl != new_friend)
10886 {
10887 /* This new friend declaration matched an existing
10888 declaration. For example, given:
10889
10890 template <class T> void f(T);
10891 template <class U> class C {
10892 template <class T> friend void f(T) {}
10893 };
10894
10895 the friend declaration actually provides the definition
10896 of `f', once C has been instantiated for some type. So,
10897 old_decl will be the out-of-class template declaration,
10898 while new_friend is the in-class definition.
10899
10900 But, if `f' was called before this point, the
10901 instantiation of `f' will have DECL_TI_ARGS corresponding
10902 to `T' but not to `U', references to which might appear
10903 in the definition of `f'. Previously, the most general
10904 template for an instantiation of `f' was the out-of-class
10905 version; now it is the in-class version. Therefore, we
10906 run through all specialization of `f', adding to their
10907 DECL_TI_ARGS appropriately. In particular, they need a
10908 new set of outer arguments, corresponding to the
10909 arguments for this class instantiation.
10910
10911 The same situation can arise with something like this:
10912
10913 friend void f(int);
10914 template <class T> class C {
10915 friend void f(T) {}
10916 };
10917
10918 when `C<int>' is instantiated. Now, `f(int)' is defined
10919 in the class. */
10920
10921 if (!new_friend_is_defn)
10922 /* On the other hand, if the in-class declaration does
10923 *not* provide a definition, then we don't want to alter
10924 existing definitions. We can just leave everything
10925 alone. */
10926 ;
10927 else
10928 {
10929 tree new_template = TI_TEMPLATE (new_friend_template_info);
10930 tree new_args = TI_ARGS (new_friend_template_info);
10931
10932 /* Overwrite whatever template info was there before, if
10933 any, with the new template information pertaining to
10934 the declaration. */
10935 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10936
10937 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10938 {
10939 /* We should have called reregister_specialization in
10940 duplicate_decls. */
10941 gcc_assert (retrieve_specialization (new_template,
10942 new_args, 0)
10943 == old_decl);
10944
10945 /* Instantiate it if the global has already been used. */
10946 if (DECL_ODR_USED (old_decl))
10947 instantiate_decl (old_decl, /*defer_ok=*/true,
10948 /*expl_inst_class_mem_p=*/false);
10949 }
10950 else
10951 {
10952 tree t;
10953
10954 /* Indicate that the old function template is a partial
10955 instantiation. */
10956 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10957 = new_friend_result_template_info;
10958
10959 gcc_assert (new_template
10960 == most_general_template (new_template));
10961 gcc_assert (new_template != old_decl);
10962
10963 /* Reassign any specializations already in the hash table
10964 to the new more general template, and add the
10965 additional template args. */
10966 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10967 t != NULL_TREE;
10968 t = TREE_CHAIN (t))
10969 {
10970 tree spec = TREE_VALUE (t);
10971 spec_entry elt;
10972
10973 elt.tmpl = old_decl;
10974 elt.args = DECL_TI_ARGS (spec);
10975 elt.spec = NULL_TREE;
10976
10977 decl_specializations->remove_elt (&elt);
10978
10979 DECL_TI_ARGS (spec)
10980 = add_outermost_template_args (new_args,
10981 DECL_TI_ARGS (spec));
10982
10983 register_specialization
10984 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10985
10986 }
10987 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10988 }
10989 }
10990
10991 /* The information from NEW_FRIEND has been merged into OLD_DECL
10992 by duplicate_decls. */
10993 new_friend = old_decl;
10994 }
10995 }
10996 else
10997 {
10998 tree context = DECL_CONTEXT (new_friend);
10999 bool dependent_p;
11000
11001 /* In the code
11002 template <class T> class C {
11003 template <class U> friend void C1<U>::f (); // case 1
11004 friend void C2<T>::f (); // case 2
11005 };
11006 we only need to make sure CONTEXT is a complete type for
11007 case 2. To distinguish between the two cases, we note that
11008 CONTEXT of case 1 remains dependent type after tsubst while
11009 this isn't true for case 2. */
11010 ++processing_template_decl;
11011 dependent_p = dependent_type_p (context);
11012 --processing_template_decl;
11013
11014 if (!dependent_p
11015 && !complete_type_or_else (context, NULL_TREE))
11016 return error_mark_node;
11017
11018 if (COMPLETE_TYPE_P (context))
11019 {
11020 tree fn = new_friend;
11021 /* do_friend adds the TEMPLATE_DECL for any member friend
11022 template even if it isn't a member template, i.e.
11023 template <class T> friend A<T>::f();
11024 Look through it in that case. */
11025 if (TREE_CODE (fn) == TEMPLATE_DECL
11026 && !PRIMARY_TEMPLATE_P (fn))
11027 fn = DECL_TEMPLATE_RESULT (fn);
11028 /* Check to see that the declaration is really present, and,
11029 possibly obtain an improved declaration. */
11030 fn = check_classfn (context, fn, NULL_TREE);
11031
11032 if (fn)
11033 new_friend = fn;
11034 }
11035 }
11036
11037 return new_friend;
11038 }
11039
11040 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11041 template arguments, as for tsubst.
11042
11043 Returns an appropriate tsubst'd friend type or error_mark_node on
11044 failure. */
11045
11046 static tree
11047 tsubst_friend_class (tree friend_tmpl, tree args)
11048 {
11049 tree tmpl;
11050
11051 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11052 {
11053 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11054 return TREE_TYPE (tmpl);
11055 }
11056
11057 tree context = CP_DECL_CONTEXT (friend_tmpl);
11058 if (TREE_CODE (context) == NAMESPACE_DECL)
11059 push_nested_namespace (context);
11060 else
11061 {
11062 context = tsubst (context, args, tf_error, NULL_TREE);
11063 push_nested_class (context);
11064 }
11065
11066 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11067 /*non_class=*/false, /*block_p=*/false,
11068 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11069
11070 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11071 {
11072 /* The friend template has already been declared. Just
11073 check to see that the declarations match, and install any new
11074 default parameters. We must tsubst the default parameters,
11075 of course. We only need the innermost template parameters
11076 because that is all that redeclare_class_template will look
11077 at. */
11078 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11079 > TMPL_ARGS_DEPTH (args))
11080 {
11081 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11082 args, tf_warning_or_error);
11083 location_t saved_input_location = input_location;
11084 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11085 tree cons = get_constraints (tmpl);
11086 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11087 input_location = saved_input_location;
11088 }
11089 }
11090 else
11091 {
11092 /* The friend template has not already been declared. In this
11093 case, the instantiation of the template class will cause the
11094 injection of this template into the namespace scope. */
11095 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11096
11097 if (tmpl != error_mark_node)
11098 {
11099 /* The new TMPL is not an instantiation of anything, so we
11100 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11101 for the new type because that is supposed to be the
11102 corresponding template decl, i.e., TMPL. */
11103 DECL_USE_TEMPLATE (tmpl) = 0;
11104 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11105 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11106 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11107 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11108
11109 /* It is hidden. */
11110 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11111 DECL_ANTICIPATED (tmpl)
11112 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11113
11114 /* Inject this template into the enclosing namspace scope. */
11115 tmpl = pushdecl_namespace_level (tmpl, true);
11116 }
11117 }
11118
11119 if (TREE_CODE (context) == NAMESPACE_DECL)
11120 pop_nested_namespace (context);
11121 else
11122 pop_nested_class ();
11123
11124 return TREE_TYPE (tmpl);
11125 }
11126
11127 /* Returns zero if TYPE cannot be completed later due to circularity.
11128 Otherwise returns one. */
11129
11130 static int
11131 can_complete_type_without_circularity (tree type)
11132 {
11133 if (type == NULL_TREE || type == error_mark_node)
11134 return 0;
11135 else if (COMPLETE_TYPE_P (type))
11136 return 1;
11137 else if (TREE_CODE (type) == ARRAY_TYPE)
11138 return can_complete_type_without_circularity (TREE_TYPE (type));
11139 else if (CLASS_TYPE_P (type)
11140 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11141 return 0;
11142 else
11143 return 1;
11144 }
11145
11146 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11147 tsubst_flags_t, tree);
11148
11149 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11150 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11151
11152 static tree
11153 tsubst_attribute (tree t, tree *decl_p, tree args,
11154 tsubst_flags_t complain, tree in_decl)
11155 {
11156 gcc_assert (ATTR_IS_DEPENDENT (t));
11157
11158 tree val = TREE_VALUE (t);
11159 if (val == NULL_TREE)
11160 /* Nothing to do. */;
11161 else if ((flag_openmp || flag_openmp_simd)
11162 && is_attribute_p ("omp declare simd",
11163 get_attribute_name (t)))
11164 {
11165 tree clauses = TREE_VALUE (val);
11166 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11167 complain, in_decl);
11168 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11169 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11170 tree parms = DECL_ARGUMENTS (*decl_p);
11171 clauses
11172 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11173 if (clauses)
11174 val = build_tree_list (NULL_TREE, clauses);
11175 else
11176 val = NULL_TREE;
11177 }
11178 else if (flag_openmp
11179 && is_attribute_p ("omp declare variant base",
11180 get_attribute_name (t)))
11181 {
11182 ++cp_unevaluated_operand;
11183 tree varid
11184 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11185 in_decl, /*integral_constant_expression_p=*/false);
11186 --cp_unevaluated_operand;
11187 tree chain = TREE_CHAIN (val);
11188 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11189 tree ctx = copy_list (TREE_VALUE (val));
11190 tree simd = get_identifier ("simd");
11191 tree score = get_identifier (" score");
11192 tree condition = get_identifier ("condition");
11193 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11194 {
11195 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11196 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11197 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11198 {
11199 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11200 {
11201 tree clauses = TREE_VALUE (t2);
11202 clauses = tsubst_omp_clauses (clauses,
11203 C_ORT_OMP_DECLARE_SIMD, args,
11204 complain, in_decl);
11205 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11206 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11207 TREE_VALUE (t2) = clauses;
11208 }
11209 else
11210 {
11211 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11212 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11213 if (TREE_VALUE (t3))
11214 {
11215 bool allow_string
11216 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11217 && TREE_PURPOSE (t3) != score);
11218 tree v = TREE_VALUE (t3);
11219 if (TREE_CODE (v) == STRING_CST && allow_string)
11220 continue;
11221 v = tsubst_expr (v, args, complain, in_decl, true);
11222 v = fold_non_dependent_expr (v);
11223 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11224 || (TREE_PURPOSE (t3) == score
11225 ? TREE_CODE (v) != INTEGER_CST
11226 : !tree_fits_shwi_p (v)))
11227 {
11228 location_t loc
11229 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11230 match_loc);
11231 if (TREE_PURPOSE (t3) == score)
11232 error_at (loc, "score argument must be "
11233 "constant integer expression");
11234 else if (allow_string)
11235 error_at (loc, "property must be constant "
11236 "integer expression or string "
11237 "literal");
11238 else
11239 error_at (loc, "property must be constant "
11240 "integer expression");
11241 return NULL_TREE;
11242 }
11243 else if (TREE_PURPOSE (t3) == score
11244 && tree_int_cst_sgn (v) < 0)
11245 {
11246 location_t loc
11247 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11248 match_loc);
11249 error_at (loc, "score argument must be "
11250 "non-negative");
11251 return NULL_TREE;
11252 }
11253 TREE_VALUE (t3) = v;
11254 }
11255 }
11256 }
11257 }
11258 val = tree_cons (varid, ctx, chain);
11259 }
11260 /* If the first attribute argument is an identifier, don't
11261 pass it through tsubst. Attributes like mode, format,
11262 cleanup and several target specific attributes expect it
11263 unmodified. */
11264 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11265 {
11266 tree chain
11267 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11268 /*integral_constant_expression_p=*/false);
11269 if (chain != TREE_CHAIN (val))
11270 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11271 }
11272 else if (PACK_EXPANSION_P (val))
11273 {
11274 /* An attribute pack expansion. */
11275 tree purp = TREE_PURPOSE (t);
11276 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11277 if (pack == error_mark_node)
11278 return error_mark_node;
11279 int len = TREE_VEC_LENGTH (pack);
11280 tree list = NULL_TREE;
11281 tree *q = &list;
11282 for (int i = 0; i < len; ++i)
11283 {
11284 tree elt = TREE_VEC_ELT (pack, i);
11285 *q = build_tree_list (purp, elt);
11286 q = &TREE_CHAIN (*q);
11287 }
11288 return list;
11289 }
11290 else
11291 val = tsubst_expr (val, args, complain, in_decl,
11292 /*integral_constant_expression_p=*/false);
11293
11294 if (val != TREE_VALUE (t))
11295 return build_tree_list (TREE_PURPOSE (t), val);
11296 return t;
11297 }
11298
11299 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11300 unchanged or a new TREE_LIST chain. */
11301
11302 static tree
11303 tsubst_attributes (tree attributes, tree args,
11304 tsubst_flags_t complain, tree in_decl)
11305 {
11306 tree last_dep = NULL_TREE;
11307
11308 for (tree t = attributes; t; t = TREE_CHAIN (t))
11309 if (ATTR_IS_DEPENDENT (t))
11310 {
11311 last_dep = t;
11312 attributes = copy_list (attributes);
11313 break;
11314 }
11315
11316 if (last_dep)
11317 for (tree *p = &attributes; *p; )
11318 {
11319 tree t = *p;
11320 if (ATTR_IS_DEPENDENT (t))
11321 {
11322 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11323 if (subst != t)
11324 {
11325 *p = subst;
11326 while (*p)
11327 p = &TREE_CHAIN (*p);
11328 *p = TREE_CHAIN (t);
11329 continue;
11330 }
11331 }
11332 p = &TREE_CHAIN (*p);
11333 }
11334
11335 return attributes;
11336 }
11337
11338 /* Apply any attributes which had to be deferred until instantiation
11339 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11340 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11341
11342 static void
11343 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11344 tree args, tsubst_flags_t complain, tree in_decl)
11345 {
11346 tree last_dep = NULL_TREE;
11347 tree t;
11348 tree *p;
11349
11350 if (attributes == NULL_TREE)
11351 return;
11352
11353 if (DECL_P (*decl_p))
11354 {
11355 if (TREE_TYPE (*decl_p) == error_mark_node)
11356 return;
11357 p = &DECL_ATTRIBUTES (*decl_p);
11358 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11359 to our attributes parameter. */
11360 gcc_assert (*p == attributes);
11361 }
11362 else
11363 {
11364 p = &TYPE_ATTRIBUTES (*decl_p);
11365 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11366 lookup_template_class_1, and should be preserved. */
11367 gcc_assert (*p != attributes);
11368 while (*p)
11369 p = &TREE_CHAIN (*p);
11370 }
11371
11372 for (t = attributes; t; t = TREE_CHAIN (t))
11373 if (ATTR_IS_DEPENDENT (t))
11374 {
11375 last_dep = t;
11376 attributes = copy_list (attributes);
11377 break;
11378 }
11379
11380 *p = attributes;
11381 if (last_dep)
11382 {
11383 tree late_attrs = NULL_TREE;
11384 tree *q = &late_attrs;
11385
11386 for (; *p; )
11387 {
11388 t = *p;
11389 if (ATTR_IS_DEPENDENT (t))
11390 {
11391 *p = TREE_CHAIN (t);
11392 TREE_CHAIN (t) = NULL_TREE;
11393 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11394 while (*q)
11395 q = &TREE_CHAIN (*q);
11396 }
11397 else
11398 p = &TREE_CHAIN (t);
11399 }
11400
11401 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11402 }
11403 }
11404
11405 /* Perform (or defer) access check for typedefs that were referenced
11406 from within the template TMPL code.
11407 This is a subroutine of instantiate_decl and instantiate_class_template.
11408 TMPL is the template to consider and TARGS is the list of arguments of
11409 that template. */
11410
11411 static void
11412 perform_typedefs_access_check (tree tmpl, tree targs)
11413 {
11414 unsigned i;
11415 qualified_typedef_usage_t *iter;
11416
11417 if (!tmpl
11418 || (!CLASS_TYPE_P (tmpl)
11419 && TREE_CODE (tmpl) != FUNCTION_DECL))
11420 return;
11421
11422 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11423 {
11424 tree type_decl = iter->typedef_decl;
11425 tree type_scope = iter->context;
11426
11427 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11428 continue;
11429
11430 if (uses_template_parms (type_decl))
11431 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11432 if (uses_template_parms (type_scope))
11433 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11434
11435 /* Make access check error messages point to the location
11436 of the use of the typedef. */
11437 iloc_sentinel ils (iter->locus);
11438 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11439 type_decl, type_decl,
11440 tf_warning_or_error);
11441 }
11442 }
11443
11444 static tree
11445 instantiate_class_template_1 (tree type)
11446 {
11447 tree templ, args, pattern, t, member;
11448 tree typedecl;
11449 tree pbinfo;
11450 tree base_list;
11451 unsigned int saved_maximum_field_alignment;
11452 tree fn_context;
11453
11454 if (type == error_mark_node)
11455 return error_mark_node;
11456
11457 if (COMPLETE_OR_OPEN_TYPE_P (type)
11458 || uses_template_parms (type))
11459 return type;
11460
11461 /* Figure out which template is being instantiated. */
11462 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11463 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11464
11465 /* Mark the type as in the process of being defined. */
11466 TYPE_BEING_DEFINED (type) = 1;
11467
11468 /* We may be in the middle of deferred access check. Disable
11469 it now. */
11470 deferring_access_check_sentinel acs (dk_no_deferred);
11471
11472 /* Determine what specialization of the original template to
11473 instantiate. */
11474 t = most_specialized_partial_spec (type, tf_warning_or_error);
11475 if (t == error_mark_node)
11476 return error_mark_node;
11477 else if (t)
11478 {
11479 /* This TYPE is actually an instantiation of a partial
11480 specialization. We replace the innermost set of ARGS with
11481 the arguments appropriate for substitution. For example,
11482 given:
11483
11484 template <class T> struct S {};
11485 template <class T> struct S<T*> {};
11486
11487 and supposing that we are instantiating S<int*>, ARGS will
11488 presently be {int*} -- but we need {int}. */
11489 pattern = TREE_TYPE (t);
11490 args = TREE_PURPOSE (t);
11491 }
11492 else
11493 {
11494 pattern = TREE_TYPE (templ);
11495 args = CLASSTYPE_TI_ARGS (type);
11496 }
11497
11498 /* If the template we're instantiating is incomplete, then clearly
11499 there's nothing we can do. */
11500 if (!COMPLETE_TYPE_P (pattern))
11501 {
11502 /* We can try again later. */
11503 TYPE_BEING_DEFINED (type) = 0;
11504 return type;
11505 }
11506
11507 /* If we've recursively instantiated too many templates, stop. */
11508 if (! push_tinst_level (type))
11509 return type;
11510
11511 int saved_unevaluated_operand = cp_unevaluated_operand;
11512 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11513
11514 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11515 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11516 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11517 fn_context = error_mark_node;
11518 if (!fn_context)
11519 push_to_top_level ();
11520 else
11521 {
11522 cp_unevaluated_operand = 0;
11523 c_inhibit_evaluation_warnings = 0;
11524 }
11525 /* Use #pragma pack from the template context. */
11526 saved_maximum_field_alignment = maximum_field_alignment;
11527 maximum_field_alignment = TYPE_PRECISION (pattern);
11528
11529 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11530
11531 /* Set the input location to the most specialized template definition.
11532 This is needed if tsubsting causes an error. */
11533 typedecl = TYPE_MAIN_DECL (pattern);
11534 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11535 DECL_SOURCE_LOCATION (typedecl);
11536
11537 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11538 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11539 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11540 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11541 if (ANON_AGGR_TYPE_P (pattern))
11542 SET_ANON_AGGR_TYPE_P (type);
11543 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11544 {
11545 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11546 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11547 /* Adjust visibility for template arguments. */
11548 determine_visibility (TYPE_MAIN_DECL (type));
11549 }
11550 if (CLASS_TYPE_P (type))
11551 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11552
11553 pbinfo = TYPE_BINFO (pattern);
11554
11555 /* We should never instantiate a nested class before its enclosing
11556 class; we need to look up the nested class by name before we can
11557 instantiate it, and that lookup should instantiate the enclosing
11558 class. */
11559 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11560 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11561
11562 base_list = NULL_TREE;
11563 if (BINFO_N_BASE_BINFOS (pbinfo))
11564 {
11565 tree pbase_binfo;
11566 tree pushed_scope;
11567 int i;
11568
11569 /* We must enter the scope containing the type, as that is where
11570 the accessibility of types named in dependent bases are
11571 looked up from. */
11572 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11573
11574 /* Substitute into each of the bases to determine the actual
11575 basetypes. */
11576 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11577 {
11578 tree base;
11579 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11580 tree expanded_bases = NULL_TREE;
11581 int idx, len = 1;
11582
11583 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11584 {
11585 expanded_bases =
11586 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11587 args, tf_error, NULL_TREE);
11588 if (expanded_bases == error_mark_node)
11589 continue;
11590
11591 len = TREE_VEC_LENGTH (expanded_bases);
11592 }
11593
11594 for (idx = 0; idx < len; idx++)
11595 {
11596 if (expanded_bases)
11597 /* Extract the already-expanded base class. */
11598 base = TREE_VEC_ELT (expanded_bases, idx);
11599 else
11600 /* Substitute to figure out the base class. */
11601 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11602 NULL_TREE);
11603
11604 if (base == error_mark_node)
11605 continue;
11606
11607 base_list = tree_cons (access, base, base_list);
11608 if (BINFO_VIRTUAL_P (pbase_binfo))
11609 TREE_TYPE (base_list) = integer_type_node;
11610 }
11611 }
11612
11613 /* The list is now in reverse order; correct that. */
11614 base_list = nreverse (base_list);
11615
11616 if (pushed_scope)
11617 pop_scope (pushed_scope);
11618 }
11619 /* Now call xref_basetypes to set up all the base-class
11620 information. */
11621 xref_basetypes (type, base_list);
11622
11623 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11624 (int) ATTR_FLAG_TYPE_IN_PLACE,
11625 args, tf_error, NULL_TREE);
11626 fixup_attribute_variants (type);
11627
11628 /* Now that our base classes are set up, enter the scope of the
11629 class, so that name lookups into base classes, etc. will work
11630 correctly. This is precisely analogous to what we do in
11631 begin_class_definition when defining an ordinary non-template
11632 class, except we also need to push the enclosing classes. */
11633 push_nested_class (type);
11634
11635 /* Now members are processed in the order of declaration. */
11636 for (member = CLASSTYPE_DECL_LIST (pattern);
11637 member; member = TREE_CHAIN (member))
11638 {
11639 tree t = TREE_VALUE (member);
11640
11641 if (TREE_PURPOSE (member))
11642 {
11643 if (TYPE_P (t))
11644 {
11645 if (LAMBDA_TYPE_P (t))
11646 /* A closure type for a lambda in an NSDMI or default argument.
11647 Ignore it; it will be regenerated when needed. */
11648 continue;
11649
11650 /* Build new CLASSTYPE_NESTED_UTDS. */
11651
11652 tree newtag;
11653 bool class_template_p;
11654
11655 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11656 && TYPE_LANG_SPECIFIC (t)
11657 && CLASSTYPE_IS_TEMPLATE (t));
11658 /* If the member is a class template, then -- even after
11659 substitution -- there may be dependent types in the
11660 template argument list for the class. We increment
11661 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11662 that function will assume that no types are dependent
11663 when outside of a template. */
11664 if (class_template_p)
11665 ++processing_template_decl;
11666 newtag = tsubst (t, args, tf_error, NULL_TREE);
11667 if (class_template_p)
11668 --processing_template_decl;
11669 if (newtag == error_mark_node)
11670 continue;
11671
11672 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11673 {
11674 tree name = TYPE_IDENTIFIER (t);
11675
11676 if (class_template_p)
11677 /* Unfortunately, lookup_template_class sets
11678 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11679 instantiation (i.e., for the type of a member
11680 template class nested within a template class.)
11681 This behavior is required for
11682 maybe_process_partial_specialization to work
11683 correctly, but is not accurate in this case;
11684 the TAG is not an instantiation of anything.
11685 (The corresponding TEMPLATE_DECL is an
11686 instantiation, but the TYPE is not.) */
11687 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11688
11689 /* Now, we call pushtag to put this NEWTAG into the scope of
11690 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11691 pushtag calling push_template_decl. We don't have to do
11692 this for enums because it will already have been done in
11693 tsubst_enum. */
11694 if (name)
11695 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11696 pushtag (name, newtag, /*tag_scope=*/ts_current);
11697 }
11698 }
11699 else if (DECL_DECLARES_FUNCTION_P (t))
11700 {
11701 tree r;
11702
11703 if (TREE_CODE (t) == TEMPLATE_DECL)
11704 ++processing_template_decl;
11705 r = tsubst (t, args, tf_error, NULL_TREE);
11706 if (TREE_CODE (t) == TEMPLATE_DECL)
11707 --processing_template_decl;
11708 set_current_access_from_decl (r);
11709 finish_member_declaration (r);
11710 /* Instantiate members marked with attribute used. */
11711 if (r != error_mark_node && DECL_PRESERVE_P (r))
11712 mark_used (r);
11713 if (TREE_CODE (r) == FUNCTION_DECL
11714 && DECL_OMP_DECLARE_REDUCTION_P (r))
11715 cp_check_omp_declare_reduction (r);
11716 }
11717 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11718 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11719 /* A closure type for a lambda in an NSDMI or default argument.
11720 Ignore it; it will be regenerated when needed. */;
11721 else
11722 {
11723 /* Build new TYPE_FIELDS. */
11724 if (TREE_CODE (t) == STATIC_ASSERT)
11725 {
11726 tree condition;
11727
11728 ++c_inhibit_evaluation_warnings;
11729 condition =
11730 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11731 tf_warning_or_error, NULL_TREE,
11732 /*integral_constant_expression_p=*/true);
11733 --c_inhibit_evaluation_warnings;
11734
11735 finish_static_assert (condition,
11736 STATIC_ASSERT_MESSAGE (t),
11737 STATIC_ASSERT_SOURCE_LOCATION (t),
11738 /*member_p=*/true);
11739 }
11740 else if (TREE_CODE (t) != CONST_DECL)
11741 {
11742 tree r;
11743 tree vec = NULL_TREE;
11744 int len = 1;
11745
11746 /* The file and line for this declaration, to
11747 assist in error message reporting. Since we
11748 called push_tinst_level above, we don't need to
11749 restore these. */
11750 input_location = DECL_SOURCE_LOCATION (t);
11751
11752 if (TREE_CODE (t) == TEMPLATE_DECL)
11753 ++processing_template_decl;
11754 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11755 if (TREE_CODE (t) == TEMPLATE_DECL)
11756 --processing_template_decl;
11757
11758 if (TREE_CODE (r) == TREE_VEC)
11759 {
11760 /* A capture pack became multiple fields. */
11761 vec = r;
11762 len = TREE_VEC_LENGTH (vec);
11763 }
11764
11765 for (int i = 0; i < len; ++i)
11766 {
11767 if (vec)
11768 r = TREE_VEC_ELT (vec, i);
11769 if (VAR_P (r))
11770 {
11771 /* In [temp.inst]:
11772
11773 [t]he initialization (and any associated
11774 side-effects) of a static data member does
11775 not occur unless the static data member is
11776 itself used in a way that requires the
11777 definition of the static data member to
11778 exist.
11779
11780 Therefore, we do not substitute into the
11781 initialized for the static data member here. */
11782 finish_static_data_member_decl
11783 (r,
11784 /*init=*/NULL_TREE,
11785 /*init_const_expr_p=*/false,
11786 /*asmspec_tree=*/NULL_TREE,
11787 /*flags=*/0);
11788 /* Instantiate members marked with attribute used. */
11789 if (r != error_mark_node && DECL_PRESERVE_P (r))
11790 mark_used (r);
11791 }
11792 else if (TREE_CODE (r) == FIELD_DECL)
11793 {
11794 /* Determine whether R has a valid type and can be
11795 completed later. If R is invalid, then its type
11796 is replaced by error_mark_node. */
11797 tree rtype = TREE_TYPE (r);
11798 if (can_complete_type_without_circularity (rtype))
11799 complete_type (rtype);
11800
11801 if (!complete_or_array_type_p (rtype))
11802 {
11803 /* If R's type couldn't be completed and
11804 it isn't a flexible array member (whose
11805 type is incomplete by definition) give
11806 an error. */
11807 cxx_incomplete_type_error (r, rtype);
11808 TREE_TYPE (r) = error_mark_node;
11809 }
11810 else if (TREE_CODE (rtype) == ARRAY_TYPE
11811 && TYPE_DOMAIN (rtype) == NULL_TREE
11812 && (TREE_CODE (type) == UNION_TYPE
11813 || TREE_CODE (type) == QUAL_UNION_TYPE))
11814 {
11815 error ("flexible array member %qD in union", r);
11816 TREE_TYPE (r) = error_mark_node;
11817 }
11818 }
11819
11820 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11821 such a thing will already have been added to the field
11822 list by tsubst_enum in finish_member_declaration in the
11823 CLASSTYPE_NESTED_UTDS case above. */
11824 if (!(TREE_CODE (r) == TYPE_DECL
11825 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11826 && DECL_ARTIFICIAL (r)))
11827 {
11828 set_current_access_from_decl (r);
11829 finish_member_declaration (r);
11830 }
11831 }
11832 }
11833 }
11834 }
11835 else
11836 {
11837 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11838 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11839 {
11840 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11841
11842 tree friend_type = t;
11843 bool adjust_processing_template_decl = false;
11844
11845 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11846 {
11847 /* template <class T> friend class C; */
11848 friend_type = tsubst_friend_class (friend_type, args);
11849 adjust_processing_template_decl = true;
11850 }
11851 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11852 {
11853 /* template <class T> friend class C::D; */
11854 friend_type = tsubst (friend_type, args,
11855 tf_warning_or_error, NULL_TREE);
11856 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11857 friend_type = TREE_TYPE (friend_type);
11858 adjust_processing_template_decl = true;
11859 }
11860 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11861 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11862 {
11863 /* This could be either
11864
11865 friend class T::C;
11866
11867 when dependent_type_p is false or
11868
11869 template <class U> friend class T::C;
11870
11871 otherwise. */
11872 /* Bump processing_template_decl in case this is something like
11873 template <class T> friend struct A<T>::B. */
11874 ++processing_template_decl;
11875 friend_type = tsubst (friend_type, args,
11876 tf_warning_or_error, NULL_TREE);
11877 if (dependent_type_p (friend_type))
11878 adjust_processing_template_decl = true;
11879 --processing_template_decl;
11880 }
11881 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11882 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11883 && TYPE_HIDDEN_P (friend_type))
11884 {
11885 /* friend class C;
11886
11887 where C hasn't been declared yet. Let's lookup name
11888 from namespace scope directly, bypassing any name that
11889 come from dependent base class. */
11890 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11891
11892 /* The call to xref_tag_from_type does injection for friend
11893 classes. */
11894 push_nested_namespace (ns);
11895 friend_type =
11896 xref_tag_from_type (friend_type, NULL_TREE,
11897 /*tag_scope=*/ts_current);
11898 pop_nested_namespace (ns);
11899 }
11900 else if (uses_template_parms (friend_type))
11901 /* friend class C<T>; */
11902 friend_type = tsubst (friend_type, args,
11903 tf_warning_or_error, NULL_TREE);
11904 /* Otherwise it's
11905
11906 friend class C;
11907
11908 where C is already declared or
11909
11910 friend class C<int>;
11911
11912 We don't have to do anything in these cases. */
11913
11914 if (adjust_processing_template_decl)
11915 /* Trick make_friend_class into realizing that the friend
11916 we're adding is a template, not an ordinary class. It's
11917 important that we use make_friend_class since it will
11918 perform some error-checking and output cross-reference
11919 information. */
11920 ++processing_template_decl;
11921
11922 if (friend_type != error_mark_node)
11923 make_friend_class (type, friend_type, /*complain=*/false);
11924
11925 if (adjust_processing_template_decl)
11926 --processing_template_decl;
11927 }
11928 else
11929 {
11930 /* Build new DECL_FRIENDLIST. */
11931 tree r;
11932
11933 /* The file and line for this declaration, to
11934 assist in error message reporting. Since we
11935 called push_tinst_level above, we don't need to
11936 restore these. */
11937 input_location = DECL_SOURCE_LOCATION (t);
11938
11939 if (TREE_CODE (t) == TEMPLATE_DECL)
11940 {
11941 ++processing_template_decl;
11942 push_deferring_access_checks (dk_no_check);
11943 }
11944
11945 r = tsubst_friend_function (t, args);
11946 add_friend (type, r, /*complain=*/false);
11947 if (TREE_CODE (t) == TEMPLATE_DECL)
11948 {
11949 pop_deferring_access_checks ();
11950 --processing_template_decl;
11951 }
11952 }
11953 }
11954 }
11955
11956 if (fn_context)
11957 {
11958 /* Restore these before substituting into the lambda capture
11959 initializers. */
11960 cp_unevaluated_operand = saved_unevaluated_operand;
11961 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11962 }
11963
11964 /* Set the file and line number information to whatever is given for
11965 the class itself. This puts error messages involving generated
11966 implicit functions at a predictable point, and the same point
11967 that would be used for non-template classes. */
11968 input_location = DECL_SOURCE_LOCATION (typedecl);
11969
11970 unreverse_member_declarations (type);
11971 finish_struct_1 (type);
11972 TYPE_BEING_DEFINED (type) = 0;
11973
11974 /* We don't instantiate default arguments for member functions. 14.7.1:
11975
11976 The implicit instantiation of a class template specialization causes
11977 the implicit instantiation of the declarations, but not of the
11978 definitions or default arguments, of the class member functions,
11979 member classes, static data members and member templates.... */
11980
11981 /* Some typedefs referenced from within the template code need to be access
11982 checked at template instantiation time, i.e now. These types were
11983 added to the template at parsing time. Let's get those and perform
11984 the access checks then. */
11985 perform_typedefs_access_check (pattern, args);
11986 perform_deferred_access_checks (tf_warning_or_error);
11987 pop_nested_class ();
11988 maximum_field_alignment = saved_maximum_field_alignment;
11989 if (!fn_context)
11990 pop_from_top_level ();
11991 pop_tinst_level ();
11992
11993 /* The vtable for a template class can be emitted in any translation
11994 unit in which the class is instantiated. When there is no key
11995 method, however, finish_struct_1 will already have added TYPE to
11996 the keyed_classes. */
11997 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11998 vec_safe_push (keyed_classes, type);
11999
12000 return type;
12001 }
12002
12003 /* Wrapper for instantiate_class_template_1. */
12004
12005 tree
12006 instantiate_class_template (tree type)
12007 {
12008 tree ret;
12009 timevar_push (TV_TEMPLATE_INST);
12010 ret = instantiate_class_template_1 (type);
12011 timevar_pop (TV_TEMPLATE_INST);
12012 return ret;
12013 }
12014
12015 tree
12016 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12017 {
12018 tree r;
12019
12020 if (!t)
12021 r = t;
12022 else if (TYPE_P (t))
12023 r = tsubst (t, args, complain, in_decl);
12024 else
12025 {
12026 if (!(complain & tf_warning))
12027 ++c_inhibit_evaluation_warnings;
12028 r = tsubst_expr (t, args, complain, in_decl,
12029 /*integral_constant_expression_p=*/true);
12030 if (!(complain & tf_warning))
12031 --c_inhibit_evaluation_warnings;
12032 }
12033
12034 return r;
12035 }
12036
12037 /* Given a function parameter pack TMPL_PARM and some function parameters
12038 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12039 and set *SPEC_P to point at the next point in the list. */
12040
12041 tree
12042 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12043 {
12044 /* Collect all of the extra "packed" parameters into an
12045 argument pack. */
12046 tree parmvec;
12047 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12048 tree spec_parm = *spec_p;
12049 int i, len;
12050
12051 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12052 if (tmpl_parm
12053 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12054 break;
12055
12056 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12057 parmvec = make_tree_vec (len);
12058 spec_parm = *spec_p;
12059 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12060 {
12061 tree elt = spec_parm;
12062 if (DECL_PACK_P (elt))
12063 elt = make_pack_expansion (elt);
12064 TREE_VEC_ELT (parmvec, i) = elt;
12065 }
12066
12067 /* Build the argument packs. */
12068 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12069 *spec_p = spec_parm;
12070
12071 return argpack;
12072 }
12073
12074 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12075 NONTYPE_ARGUMENT_PACK. */
12076
12077 static tree
12078 make_fnparm_pack (tree spec_parm)
12079 {
12080 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12081 }
12082
12083 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12084 pack expansion with no extra args, 2 if it has extra args, or 0
12085 if it is not a pack expansion. */
12086
12087 static int
12088 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12089 {
12090 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12091 /* We're being called before this happens in tsubst_pack_expansion. */
12092 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12093 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12094 if (i >= TREE_VEC_LENGTH (vec))
12095 return 0;
12096 tree elt = TREE_VEC_ELT (vec, i);
12097 if (DECL_P (elt))
12098 /* A decl pack is itself an expansion. */
12099 elt = TREE_TYPE (elt);
12100 if (!PACK_EXPANSION_P (elt))
12101 return 0;
12102 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12103 return 2;
12104 return 1;
12105 }
12106
12107
12108 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12109
12110 static tree
12111 make_argument_pack_select (tree arg_pack, unsigned index)
12112 {
12113 tree aps = make_node (ARGUMENT_PACK_SELECT);
12114
12115 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12116 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12117
12118 return aps;
12119 }
12120
12121 /* This is a subroutine of tsubst_pack_expansion.
12122
12123 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12124 mechanism to store the (non complete list of) arguments of the
12125 substitution and return a non substituted pack expansion, in order
12126 to wait for when we have enough arguments to really perform the
12127 substitution. */
12128
12129 static bool
12130 use_pack_expansion_extra_args_p (tree parm_packs,
12131 int arg_pack_len,
12132 bool has_empty_arg)
12133 {
12134 /* If one pack has an expansion and another pack has a normal
12135 argument or if one pack has an empty argument and an another
12136 one hasn't then tsubst_pack_expansion cannot perform the
12137 substitution and need to fall back on the
12138 PACK_EXPANSION_EXTRA mechanism. */
12139 if (parm_packs == NULL_TREE)
12140 return false;
12141 else if (has_empty_arg)
12142 {
12143 /* If all the actual packs are pack expansions, we can still
12144 subsitute directly. */
12145 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12146 {
12147 tree a = TREE_VALUE (p);
12148 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12149 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12150 a = ARGUMENT_PACK_ARGS (a);
12151 if (TREE_VEC_LENGTH (a) == 1)
12152 a = TREE_VEC_ELT (a, 0);
12153 if (PACK_EXPANSION_P (a))
12154 continue;
12155 return true;
12156 }
12157 return false;
12158 }
12159
12160 bool has_expansion_arg = false;
12161 for (int i = 0 ; i < arg_pack_len; ++i)
12162 {
12163 bool has_non_expansion_arg = false;
12164 for (tree parm_pack = parm_packs;
12165 parm_pack;
12166 parm_pack = TREE_CHAIN (parm_pack))
12167 {
12168 tree arg = TREE_VALUE (parm_pack);
12169
12170 int exp = argument_pack_element_is_expansion_p (arg, i);
12171 if (exp == 2)
12172 /* We can't substitute a pack expansion with extra args into
12173 our pattern. */
12174 return true;
12175 else if (exp)
12176 has_expansion_arg = true;
12177 else
12178 has_non_expansion_arg = true;
12179 }
12180
12181 if (has_expansion_arg && has_non_expansion_arg)
12182 return true;
12183 }
12184 return false;
12185 }
12186
12187 /* [temp.variadic]/6 says that:
12188
12189 The instantiation of a pack expansion [...]
12190 produces a list E1,E2, ..., En, where N is the number of elements
12191 in the pack expansion parameters.
12192
12193 This subroutine of tsubst_pack_expansion produces one of these Ei.
12194
12195 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12196 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12197 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12198 INDEX is the index 'i' of the element Ei to produce. ARGS,
12199 COMPLAIN, and IN_DECL are the same parameters as for the
12200 tsubst_pack_expansion function.
12201
12202 The function returns the resulting Ei upon successful completion,
12203 or error_mark_node.
12204
12205 Note that this function possibly modifies the ARGS parameter, so
12206 it's the responsibility of the caller to restore it. */
12207
12208 static tree
12209 gen_elem_of_pack_expansion_instantiation (tree pattern,
12210 tree parm_packs,
12211 unsigned index,
12212 tree args /* This parm gets
12213 modified. */,
12214 tsubst_flags_t complain,
12215 tree in_decl)
12216 {
12217 tree t;
12218 bool ith_elem_is_expansion = false;
12219
12220 /* For each parameter pack, change the substitution of the parameter
12221 pack to the ith argument in its argument pack, then expand the
12222 pattern. */
12223 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12224 {
12225 tree parm = TREE_PURPOSE (pack);
12226 tree arg_pack = TREE_VALUE (pack);
12227 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12228
12229 ith_elem_is_expansion |=
12230 argument_pack_element_is_expansion_p (arg_pack, index);
12231
12232 /* Select the Ith argument from the pack. */
12233 if (TREE_CODE (parm) == PARM_DECL
12234 || VAR_P (parm)
12235 || TREE_CODE (parm) == FIELD_DECL)
12236 {
12237 if (index == 0)
12238 {
12239 aps = make_argument_pack_select (arg_pack, index);
12240 if (!mark_used (parm, complain) && !(complain & tf_error))
12241 return error_mark_node;
12242 register_local_specialization (aps, parm);
12243 }
12244 else
12245 aps = retrieve_local_specialization (parm);
12246 }
12247 else
12248 {
12249 int idx, level;
12250 template_parm_level_and_index (parm, &level, &idx);
12251
12252 if (index == 0)
12253 {
12254 aps = make_argument_pack_select (arg_pack, index);
12255 /* Update the corresponding argument. */
12256 TMPL_ARG (args, level, idx) = aps;
12257 }
12258 else
12259 /* Re-use the ARGUMENT_PACK_SELECT. */
12260 aps = TMPL_ARG (args, level, idx);
12261 }
12262 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12263 }
12264
12265 /* Substitute into the PATTERN with the (possibly altered)
12266 arguments. */
12267 if (pattern == in_decl)
12268 /* Expanding a fixed parameter pack from
12269 coerce_template_parameter_pack. */
12270 t = tsubst_decl (pattern, args, complain);
12271 else if (pattern == error_mark_node)
12272 t = error_mark_node;
12273 else if (!TYPE_P (pattern))
12274 t = tsubst_expr (pattern, args, complain, in_decl,
12275 /*integral_constant_expression_p=*/false);
12276 else
12277 t = tsubst (pattern, args, complain, in_decl);
12278
12279 /* If the Ith argument pack element is a pack expansion, then
12280 the Ith element resulting from the substituting is going to
12281 be a pack expansion as well. */
12282 if (ith_elem_is_expansion)
12283 t = make_pack_expansion (t, complain);
12284
12285 return t;
12286 }
12287
12288 /* When the unexpanded parameter pack in a fold expression expands to an empty
12289 sequence, the value of the expression is as follows; the program is
12290 ill-formed if the operator is not listed in this table.
12291
12292 && true
12293 || false
12294 , void() */
12295
12296 tree
12297 expand_empty_fold (tree t, tsubst_flags_t complain)
12298 {
12299 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12300 if (!FOLD_EXPR_MODIFY_P (t))
12301 switch (code)
12302 {
12303 case TRUTH_ANDIF_EXPR:
12304 return boolean_true_node;
12305 case TRUTH_ORIF_EXPR:
12306 return boolean_false_node;
12307 case COMPOUND_EXPR:
12308 return void_node;
12309 default:
12310 break;
12311 }
12312
12313 if (complain & tf_error)
12314 error_at (location_of (t),
12315 "fold of empty expansion over %O", code);
12316 return error_mark_node;
12317 }
12318
12319 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12320 form an expression that combines the two terms using the
12321 operator of T. */
12322
12323 static tree
12324 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12325 {
12326 tree op = FOLD_EXPR_OP (t);
12327 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12328
12329 // Handle compound assignment operators.
12330 if (FOLD_EXPR_MODIFY_P (t))
12331 return build_x_modify_expr (input_location, left, code, right, complain);
12332
12333 switch (code)
12334 {
12335 case COMPOUND_EXPR:
12336 return build_x_compound_expr (input_location, left, right, complain);
12337 default:
12338 return build_x_binary_op (input_location, code,
12339 left, TREE_CODE (left),
12340 right, TREE_CODE (right),
12341 /*overload=*/NULL,
12342 complain);
12343 }
12344 }
12345
12346 /* Substitute ARGS into the pack of a fold expression T. */
12347
12348 static inline tree
12349 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12350 {
12351 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12352 }
12353
12354 /* Substitute ARGS into the pack of a fold expression T. */
12355
12356 static inline tree
12357 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12358 {
12359 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12360 }
12361
12362 /* Expand a PACK of arguments into a grouped as left fold.
12363 Given a pack containing elements A0, A1, ..., An and an
12364 operator @, this builds the expression:
12365
12366 ((A0 @ A1) @ A2) ... @ An
12367
12368 Note that PACK must not be empty.
12369
12370 The operator is defined by the original fold expression T. */
12371
12372 static tree
12373 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12374 {
12375 tree left = TREE_VEC_ELT (pack, 0);
12376 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12377 {
12378 tree right = TREE_VEC_ELT (pack, i);
12379 left = fold_expression (t, left, right, complain);
12380 }
12381 return left;
12382 }
12383
12384 /* Substitute into a unary left fold expression. */
12385
12386 static tree
12387 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12388 tree in_decl)
12389 {
12390 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12391 if (pack == error_mark_node)
12392 return error_mark_node;
12393 if (PACK_EXPANSION_P (pack))
12394 {
12395 tree r = copy_node (t);
12396 FOLD_EXPR_PACK (r) = pack;
12397 return r;
12398 }
12399 if (TREE_VEC_LENGTH (pack) == 0)
12400 return expand_empty_fold (t, complain);
12401 else
12402 return expand_left_fold (t, pack, complain);
12403 }
12404
12405 /* Substitute into a binary left fold expression.
12406
12407 Do ths by building a single (non-empty) vector of argumnts and
12408 building the expression from those elements. */
12409
12410 static tree
12411 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12412 tree in_decl)
12413 {
12414 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12415 if (pack == error_mark_node)
12416 return error_mark_node;
12417 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12418 if (init == error_mark_node)
12419 return error_mark_node;
12420
12421 if (PACK_EXPANSION_P (pack))
12422 {
12423 tree r = copy_node (t);
12424 FOLD_EXPR_PACK (r) = pack;
12425 FOLD_EXPR_INIT (r) = init;
12426 return r;
12427 }
12428
12429 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12430 TREE_VEC_ELT (vec, 0) = init;
12431 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12432 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12433
12434 return expand_left_fold (t, vec, complain);
12435 }
12436
12437 /* Expand a PACK of arguments into a grouped as right fold.
12438 Given a pack containing elementns A0, A1, ..., and an
12439 operator @, this builds the expression:
12440
12441 A0@ ... (An-2 @ (An-1 @ An))
12442
12443 Note that PACK must not be empty.
12444
12445 The operator is defined by the original fold expression T. */
12446
12447 tree
12448 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12449 {
12450 // Build the expression.
12451 int n = TREE_VEC_LENGTH (pack);
12452 tree right = TREE_VEC_ELT (pack, n - 1);
12453 for (--n; n != 0; --n)
12454 {
12455 tree left = TREE_VEC_ELT (pack, n - 1);
12456 right = fold_expression (t, left, right, complain);
12457 }
12458 return right;
12459 }
12460
12461 /* Substitute into a unary right fold expression. */
12462
12463 static tree
12464 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12465 tree in_decl)
12466 {
12467 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12468 if (pack == error_mark_node)
12469 return error_mark_node;
12470 if (PACK_EXPANSION_P (pack))
12471 {
12472 tree r = copy_node (t);
12473 FOLD_EXPR_PACK (r) = pack;
12474 return r;
12475 }
12476 if (TREE_VEC_LENGTH (pack) == 0)
12477 return expand_empty_fold (t, complain);
12478 else
12479 return expand_right_fold (t, pack, complain);
12480 }
12481
12482 /* Substitute into a binary right fold expression.
12483
12484 Do ths by building a single (non-empty) vector of arguments and
12485 building the expression from those elements. */
12486
12487 static tree
12488 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12489 tree in_decl)
12490 {
12491 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12492 if (pack == error_mark_node)
12493 return error_mark_node;
12494 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12495 if (init == error_mark_node)
12496 return error_mark_node;
12497
12498 if (PACK_EXPANSION_P (pack))
12499 {
12500 tree r = copy_node (t);
12501 FOLD_EXPR_PACK (r) = pack;
12502 FOLD_EXPR_INIT (r) = init;
12503 return r;
12504 }
12505
12506 int n = TREE_VEC_LENGTH (pack);
12507 tree vec = make_tree_vec (n + 1);
12508 for (int i = 0; i < n; ++i)
12509 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12510 TREE_VEC_ELT (vec, n) = init;
12511
12512 return expand_right_fold (t, vec, complain);
12513 }
12514
12515 /* Walk through the pattern of a pack expansion, adding everything in
12516 local_specializations to a list. */
12517
12518 class el_data
12519 {
12520 public:
12521 hash_set<tree> internal;
12522 tree extra;
12523 tsubst_flags_t complain;
12524
12525 el_data (tsubst_flags_t c)
12526 : extra (NULL_TREE), complain (c) {}
12527 };
12528 static tree
12529 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12530 {
12531 el_data &data = *reinterpret_cast<el_data*>(data_);
12532 tree *extra = &data.extra;
12533 tsubst_flags_t complain = data.complain;
12534
12535 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12536 /* Remember local typedefs (85214). */
12537 tp = &TYPE_NAME (*tp);
12538
12539 if (TREE_CODE (*tp) == DECL_EXPR)
12540 data.internal.add (DECL_EXPR_DECL (*tp));
12541 else if (tree spec = retrieve_local_specialization (*tp))
12542 {
12543 if (data.internal.contains (*tp))
12544 /* Don't mess with variables declared within the pattern. */
12545 return NULL_TREE;
12546 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12547 {
12548 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12549 tree args = ARGUMENT_PACK_ARGS (spec);
12550 if (TREE_VEC_LENGTH (args) == 1)
12551 {
12552 tree elt = TREE_VEC_ELT (args, 0);
12553 if (PACK_EXPANSION_P (elt))
12554 elt = PACK_EXPANSION_PATTERN (elt);
12555 if (DECL_PACK_P (elt))
12556 spec = elt;
12557 }
12558 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12559 {
12560 /* Handle lambda capture here, since we aren't doing any
12561 substitution now, and so tsubst_copy won't call
12562 process_outer_var_ref. */
12563 tree args = ARGUMENT_PACK_ARGS (spec);
12564 int len = TREE_VEC_LENGTH (args);
12565 for (int i = 0; i < len; ++i)
12566 {
12567 tree arg = TREE_VEC_ELT (args, i);
12568 tree carg = arg;
12569 if (outer_automatic_var_p (arg))
12570 carg = process_outer_var_ref (arg, complain);
12571 if (carg != arg)
12572 {
12573 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12574 proxies. */
12575 if (i == 0)
12576 {
12577 spec = copy_node (spec);
12578 args = copy_node (args);
12579 SET_ARGUMENT_PACK_ARGS (spec, args);
12580 register_local_specialization (spec, *tp);
12581 }
12582 TREE_VEC_ELT (args, i) = carg;
12583 }
12584 }
12585 }
12586 }
12587 if (outer_automatic_var_p (spec))
12588 spec = process_outer_var_ref (spec, complain);
12589 *extra = tree_cons (*tp, spec, *extra);
12590 }
12591 return NULL_TREE;
12592 }
12593 static tree
12594 extract_local_specs (tree pattern, tsubst_flags_t complain)
12595 {
12596 el_data data (complain);
12597 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12598 return data.extra;
12599 }
12600
12601 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12602 for use in PACK_EXPANSION_EXTRA_ARGS. */
12603
12604 tree
12605 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12606 {
12607 tree extra = args;
12608 if (local_specializations)
12609 if (tree locals = extract_local_specs (pattern, complain))
12610 extra = tree_cons (NULL_TREE, extra, locals);
12611 return extra;
12612 }
12613
12614 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12615 normal template args to ARGS. */
12616
12617 tree
12618 add_extra_args (tree extra, tree args)
12619 {
12620 if (extra && TREE_CODE (extra) == TREE_LIST)
12621 {
12622 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12623 {
12624 /* The partial instantiation involved local declarations collected in
12625 extract_local_specs; map from the general template to our local
12626 context. */
12627 tree gen = TREE_PURPOSE (elt);
12628 tree inst = TREE_VALUE (elt);
12629 if (DECL_P (inst))
12630 if (tree local = retrieve_local_specialization (inst))
12631 inst = local;
12632 /* else inst is already a full instantiation of the pack. */
12633 register_local_specialization (inst, gen);
12634 }
12635 gcc_assert (!TREE_PURPOSE (extra));
12636 extra = TREE_VALUE (extra);
12637 }
12638 #if 1
12639 /* I think we should always be able to substitute dependent args into the
12640 pattern. If that turns out to be incorrect in some cases, enable the
12641 alternate code (and add complain/in_decl parms to this function). */
12642 gcc_checking_assert (!uses_template_parms (extra));
12643 #else
12644 if (!uses_template_parms (extra))
12645 {
12646 gcc_unreachable ();
12647 extra = tsubst_template_args (extra, args, complain, in_decl);
12648 args = add_outermost_template_args (args, extra);
12649 }
12650 else
12651 #endif
12652 args = add_to_template_args (extra, args);
12653 return args;
12654 }
12655
12656 /* Substitute ARGS into T, which is an pack expansion
12657 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12658 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12659 (if only a partial substitution could be performed) or
12660 ERROR_MARK_NODE if there was an error. */
12661 tree
12662 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12663 tree in_decl)
12664 {
12665 tree pattern;
12666 tree pack, packs = NULL_TREE;
12667 bool unsubstituted_packs = false;
12668 bool unsubstituted_fn_pack = false;
12669 int i, len = -1;
12670 tree result;
12671 hash_map<tree, tree> *saved_local_specializations = NULL;
12672 bool need_local_specializations = false;
12673 int levels;
12674
12675 gcc_assert (PACK_EXPANSION_P (t));
12676 pattern = PACK_EXPANSION_PATTERN (t);
12677
12678 /* Add in any args remembered from an earlier partial instantiation. */
12679 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12680
12681 levels = TMPL_ARGS_DEPTH (args);
12682
12683 /* Determine the argument packs that will instantiate the parameter
12684 packs used in the expansion expression. While we're at it,
12685 compute the number of arguments to be expanded and make sure it
12686 is consistent. */
12687 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12688 pack = TREE_CHAIN (pack))
12689 {
12690 tree parm_pack = TREE_VALUE (pack);
12691 tree arg_pack = NULL_TREE;
12692 tree orig_arg = NULL_TREE;
12693 int level = 0;
12694
12695 if (TREE_CODE (parm_pack) == BASES)
12696 {
12697 gcc_assert (parm_pack == pattern);
12698 if (BASES_DIRECT (parm_pack))
12699 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12700 args, complain,
12701 in_decl, false),
12702 complain);
12703 else
12704 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12705 args, complain, in_decl,
12706 false), complain);
12707 }
12708 else if (builtin_pack_call_p (parm_pack))
12709 {
12710 if (parm_pack != pattern)
12711 {
12712 if (complain & tf_error)
12713 sorry ("%qE is not the entire pattern of the pack expansion",
12714 parm_pack);
12715 return error_mark_node;
12716 }
12717 return expand_builtin_pack_call (parm_pack, args,
12718 complain, in_decl);
12719 }
12720 else if (TREE_CODE (parm_pack) == PARM_DECL)
12721 {
12722 /* We know we have correct local_specializations if this
12723 expansion is at function scope, or if we're dealing with a
12724 local parameter in a requires expression; for the latter,
12725 tsubst_requires_expr set it up appropriately. */
12726 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12727 arg_pack = retrieve_local_specialization (parm_pack);
12728 else
12729 /* We can't rely on local_specializations for a parameter
12730 name used later in a function declaration (such as in a
12731 late-specified return type). Even if it exists, it might
12732 have the wrong value for a recursive call. */
12733 need_local_specializations = true;
12734
12735 if (!arg_pack)
12736 {
12737 /* This parameter pack was used in an unevaluated context. Just
12738 make a dummy decl, since it's only used for its type. */
12739 ++cp_unevaluated_operand;
12740 arg_pack = tsubst_decl (parm_pack, args, complain);
12741 --cp_unevaluated_operand;
12742 if (arg_pack && DECL_PACK_P (arg_pack))
12743 /* Partial instantiation of the parm_pack, we can't build
12744 up an argument pack yet. */
12745 arg_pack = NULL_TREE;
12746 else
12747 arg_pack = make_fnparm_pack (arg_pack);
12748 }
12749 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12750 /* This argument pack isn't fully instantiated yet. We set this
12751 flag rather than clear arg_pack because we do want to do the
12752 optimization below, and we don't want to substitute directly
12753 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12754 where it isn't expected). */
12755 unsubstituted_fn_pack = true;
12756 }
12757 else if (is_capture_proxy (parm_pack))
12758 {
12759 arg_pack = retrieve_local_specialization (parm_pack);
12760 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12761 unsubstituted_fn_pack = true;
12762 }
12763 else
12764 {
12765 int idx;
12766 template_parm_level_and_index (parm_pack, &level, &idx);
12767 if (level <= levels)
12768 arg_pack = TMPL_ARG (args, level, idx);
12769 }
12770
12771 orig_arg = arg_pack;
12772 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12773 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12774
12775 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12776 /* This can only happen if we forget to expand an argument
12777 pack somewhere else. Just return an error, silently. */
12778 {
12779 result = make_tree_vec (1);
12780 TREE_VEC_ELT (result, 0) = error_mark_node;
12781 return result;
12782 }
12783
12784 if (arg_pack)
12785 {
12786 int my_len =
12787 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12788
12789 /* Don't bother trying to do a partial substitution with
12790 incomplete packs; we'll try again after deduction. */
12791 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12792 return t;
12793
12794 if (len < 0)
12795 len = my_len;
12796 else if (len != my_len
12797 && !unsubstituted_fn_pack)
12798 {
12799 if (!(complain & tf_error))
12800 /* Fail quietly. */;
12801 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12802 error ("mismatched argument pack lengths while expanding %qT",
12803 pattern);
12804 else
12805 error ("mismatched argument pack lengths while expanding %qE",
12806 pattern);
12807 return error_mark_node;
12808 }
12809
12810 /* Keep track of the parameter packs and their corresponding
12811 argument packs. */
12812 packs = tree_cons (parm_pack, arg_pack, packs);
12813 TREE_TYPE (packs) = orig_arg;
12814 }
12815 else
12816 {
12817 /* We can't substitute for this parameter pack. We use a flag as
12818 well as the missing_level counter because function parameter
12819 packs don't have a level. */
12820 if (!(processing_template_decl || is_auto (parm_pack)))
12821 {
12822 gcc_unreachable ();
12823 }
12824 gcc_assert (processing_template_decl || is_auto (parm_pack));
12825 unsubstituted_packs = true;
12826 }
12827 }
12828
12829 /* If the expansion is just T..., return the matching argument pack, unless
12830 we need to call convert_from_reference on all the elements. This is an
12831 important optimization; see c++/68422. */
12832 if (!unsubstituted_packs
12833 && TREE_PURPOSE (packs) == pattern)
12834 {
12835 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12836
12837 /* If the argument pack is a single pack expansion, pull it out. */
12838 if (TREE_VEC_LENGTH (args) == 1
12839 && pack_expansion_args_count (args))
12840 return TREE_VEC_ELT (args, 0);
12841
12842 /* Types need no adjustment, nor does sizeof..., and if we still have
12843 some pack expansion args we won't do anything yet. */
12844 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12845 || PACK_EXPANSION_SIZEOF_P (t)
12846 || pack_expansion_args_count (args))
12847 return args;
12848 /* Also optimize expression pack expansions if we can tell that the
12849 elements won't have reference type. */
12850 tree type = TREE_TYPE (pattern);
12851 if (type && !TYPE_REF_P (type)
12852 && !PACK_EXPANSION_P (type)
12853 && !WILDCARD_TYPE_P (type))
12854 return args;
12855 /* Otherwise use the normal path so we get convert_from_reference. */
12856 }
12857
12858 /* We cannot expand this expansion expression, because we don't have
12859 all of the argument packs we need. */
12860 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12861 {
12862 /* We got some full packs, but we can't substitute them in until we
12863 have values for all the packs. So remember these until then. */
12864
12865 t = make_pack_expansion (pattern, complain);
12866 PACK_EXPANSION_EXTRA_ARGS (t)
12867 = build_extra_args (pattern, args, complain);
12868 return t;
12869 }
12870 else if (unsubstituted_packs)
12871 {
12872 /* There were no real arguments, we're just replacing a parameter
12873 pack with another version of itself. Substitute into the
12874 pattern and return a PACK_EXPANSION_*. The caller will need to
12875 deal with that. */
12876 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12877 t = tsubst_expr (pattern, args, complain, in_decl,
12878 /*integral_constant_expression_p=*/false);
12879 else
12880 t = tsubst (pattern, args, complain, in_decl);
12881 t = make_pack_expansion (t, complain);
12882 return t;
12883 }
12884
12885 gcc_assert (len >= 0);
12886
12887 if (need_local_specializations)
12888 {
12889 /* We're in a late-specified return type, so create our own local
12890 specializations map; the current map is either NULL or (in the
12891 case of recursive unification) might have bindings that we don't
12892 want to use or alter. */
12893 saved_local_specializations = local_specializations;
12894 local_specializations = new hash_map<tree, tree>;
12895 }
12896
12897 /* For each argument in each argument pack, substitute into the
12898 pattern. */
12899 result = make_tree_vec (len);
12900 tree elem_args = copy_template_args (args);
12901 for (i = 0; i < len; ++i)
12902 {
12903 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12904 i,
12905 elem_args, complain,
12906 in_decl);
12907 TREE_VEC_ELT (result, i) = t;
12908 if (t == error_mark_node)
12909 {
12910 result = error_mark_node;
12911 break;
12912 }
12913 }
12914
12915 /* Update ARGS to restore the substitution from parameter packs to
12916 their argument packs. */
12917 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12918 {
12919 tree parm = TREE_PURPOSE (pack);
12920
12921 if (TREE_CODE (parm) == PARM_DECL
12922 || VAR_P (parm)
12923 || TREE_CODE (parm) == FIELD_DECL)
12924 register_local_specialization (TREE_TYPE (pack), parm);
12925 else
12926 {
12927 int idx, level;
12928
12929 if (TREE_VALUE (pack) == NULL_TREE)
12930 continue;
12931
12932 template_parm_level_and_index (parm, &level, &idx);
12933
12934 /* Update the corresponding argument. */
12935 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12936 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12937 TREE_TYPE (pack);
12938 else
12939 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12940 }
12941 }
12942
12943 if (need_local_specializations)
12944 {
12945 delete local_specializations;
12946 local_specializations = saved_local_specializations;
12947 }
12948
12949 /* If the dependent pack arguments were such that we end up with only a
12950 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12951 if (len == 1 && TREE_CODE (result) == TREE_VEC
12952 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12953 return TREE_VEC_ELT (result, 0);
12954
12955 return result;
12956 }
12957
12958 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12959 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12960 parameter packs; all parms generated from a function parameter pack will
12961 have the same DECL_PARM_INDEX. */
12962
12963 tree
12964 get_pattern_parm (tree parm, tree tmpl)
12965 {
12966 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12967 tree patparm;
12968
12969 if (DECL_ARTIFICIAL (parm))
12970 {
12971 for (patparm = DECL_ARGUMENTS (pattern);
12972 patparm; patparm = DECL_CHAIN (patparm))
12973 if (DECL_ARTIFICIAL (patparm)
12974 && DECL_NAME (parm) == DECL_NAME (patparm))
12975 break;
12976 }
12977 else
12978 {
12979 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12980 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12981 gcc_assert (DECL_PARM_INDEX (patparm)
12982 == DECL_PARM_INDEX (parm));
12983 }
12984
12985 return patparm;
12986 }
12987
12988 /* Make an argument pack out of the TREE_VEC VEC. */
12989
12990 static tree
12991 make_argument_pack (tree vec)
12992 {
12993 tree pack;
12994 tree elt = TREE_VEC_ELT (vec, 0);
12995 if (TYPE_P (elt))
12996 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12997 else
12998 {
12999 pack = make_node (NONTYPE_ARGUMENT_PACK);
13000 TREE_CONSTANT (pack) = 1;
13001 }
13002 SET_ARGUMENT_PACK_ARGS (pack, vec);
13003 return pack;
13004 }
13005
13006 /* Return an exact copy of template args T that can be modified
13007 independently. */
13008
13009 static tree
13010 copy_template_args (tree t)
13011 {
13012 if (t == error_mark_node)
13013 return t;
13014
13015 int len = TREE_VEC_LENGTH (t);
13016 tree new_vec = make_tree_vec (len);
13017
13018 for (int i = 0; i < len; ++i)
13019 {
13020 tree elt = TREE_VEC_ELT (t, i);
13021 if (elt && TREE_CODE (elt) == TREE_VEC)
13022 elt = copy_template_args (elt);
13023 TREE_VEC_ELT (new_vec, i) = elt;
13024 }
13025
13026 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13027 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13028
13029 return new_vec;
13030 }
13031
13032 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13033
13034 tree
13035 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13036 tree in_decl)
13037 {
13038 /* Substitute into each of the arguments. */
13039 tree new_arg = TYPE_P (orig_arg)
13040 ? cxx_make_type (TREE_CODE (orig_arg))
13041 : make_node (TREE_CODE (orig_arg));
13042
13043 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13044 args, complain, in_decl);
13045 if (pack_args == error_mark_node)
13046 new_arg = error_mark_node;
13047 else
13048 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13049
13050 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13051 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13052
13053 return new_arg;
13054 }
13055
13056 /* Substitute ARGS into the vector or list of template arguments T. */
13057
13058 tree
13059 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13060 {
13061 tree orig_t = t;
13062 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13063 tree *elts;
13064
13065 if (t == error_mark_node)
13066 return error_mark_node;
13067
13068 len = TREE_VEC_LENGTH (t);
13069 elts = XALLOCAVEC (tree, len);
13070
13071 for (i = 0; i < len; i++)
13072 {
13073 tree orig_arg = TREE_VEC_ELT (t, i);
13074 tree new_arg;
13075
13076 if (TREE_CODE (orig_arg) == TREE_VEC)
13077 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13078 else if (PACK_EXPANSION_P (orig_arg))
13079 {
13080 /* Substitute into an expansion expression. */
13081 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13082
13083 if (TREE_CODE (new_arg) == TREE_VEC)
13084 /* Add to the expanded length adjustment the number of
13085 expanded arguments. We subtract one from this
13086 measurement, because the argument pack expression
13087 itself is already counted as 1 in
13088 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13089 the argument pack is empty. */
13090 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13091 }
13092 else if (ARGUMENT_PACK_P (orig_arg))
13093 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13094 else
13095 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13096
13097 if (new_arg == error_mark_node)
13098 return error_mark_node;
13099
13100 elts[i] = new_arg;
13101 if (new_arg != orig_arg)
13102 need_new = 1;
13103 }
13104
13105 if (!need_new)
13106 return t;
13107
13108 /* Make space for the expanded arguments coming from template
13109 argument packs. */
13110 t = make_tree_vec (len + expanded_len_adjust);
13111 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13112 arguments for a member template.
13113 In that case each TREE_VEC in ORIG_T represents a level of template
13114 arguments, and ORIG_T won't carry any non defaulted argument count.
13115 It will rather be the nested TREE_VECs that will carry one.
13116 In other words, ORIG_T carries a non defaulted argument count only
13117 if it doesn't contain any nested TREE_VEC. */
13118 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13119 {
13120 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13121 count += expanded_len_adjust;
13122 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13123 }
13124 for (i = 0, out = 0; i < len; i++)
13125 {
13126 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13127 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13128 && TREE_CODE (elts[i]) == TREE_VEC)
13129 {
13130 int idx;
13131
13132 /* Now expand the template argument pack "in place". */
13133 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13134 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13135 }
13136 else
13137 {
13138 TREE_VEC_ELT (t, out) = elts[i];
13139 out++;
13140 }
13141 }
13142
13143 return t;
13144 }
13145
13146 /* Substitute ARGS into one level PARMS of template parameters. */
13147
13148 static tree
13149 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13150 {
13151 if (parms == error_mark_node)
13152 return error_mark_node;
13153
13154 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13155
13156 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13157 {
13158 tree tuple = TREE_VEC_ELT (parms, i);
13159
13160 if (tuple == error_mark_node)
13161 continue;
13162
13163 TREE_VEC_ELT (new_vec, i) =
13164 tsubst_template_parm (tuple, args, complain);
13165 }
13166
13167 return new_vec;
13168 }
13169
13170 /* Return the result of substituting ARGS into the template parameters
13171 given by PARMS. If there are m levels of ARGS and m + n levels of
13172 PARMS, then the result will contain n levels of PARMS. For
13173 example, if PARMS is `template <class T> template <class U>
13174 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13175 result will be `template <int*, double, class V>'. */
13176
13177 static tree
13178 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13179 {
13180 tree r = NULL_TREE;
13181 tree* new_parms;
13182
13183 /* When substituting into a template, we must set
13184 PROCESSING_TEMPLATE_DECL as the template parameters may be
13185 dependent if they are based on one-another, and the dependency
13186 predicates are short-circuit outside of templates. */
13187 ++processing_template_decl;
13188
13189 for (new_parms = &r;
13190 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13191 new_parms = &(TREE_CHAIN (*new_parms)),
13192 parms = TREE_CHAIN (parms))
13193 {
13194 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13195 args, complain);
13196 *new_parms =
13197 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13198 - TMPL_ARGS_DEPTH (args)),
13199 new_vec, NULL_TREE);
13200 }
13201
13202 --processing_template_decl;
13203
13204 return r;
13205 }
13206
13207 /* Return the result of substituting ARGS into one template parameter
13208 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13209 parameter and which TREE_PURPOSE is the default argument of the
13210 template parameter. */
13211
13212 static tree
13213 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13214 {
13215 tree default_value, parm_decl;
13216
13217 if (args == NULL_TREE
13218 || t == NULL_TREE
13219 || t == error_mark_node)
13220 return t;
13221
13222 gcc_assert (TREE_CODE (t) == TREE_LIST);
13223
13224 default_value = TREE_PURPOSE (t);
13225 parm_decl = TREE_VALUE (t);
13226 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13227
13228 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13229 if (TREE_CODE (parm_decl) == PARM_DECL
13230 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13231 parm_decl = error_mark_node;
13232 default_value = tsubst_template_arg (default_value, args,
13233 complain, NULL_TREE);
13234 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13235
13236 tree r = build_tree_list (default_value, parm_decl);
13237 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13238 return r;
13239 }
13240
13241 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13242 type T. If T is not an aggregate or enumeration type, it is
13243 handled as if by tsubst. IN_DECL is as for tsubst. If
13244 ENTERING_SCOPE is nonzero, T is the context for a template which
13245 we are presently tsubst'ing. Return the substituted value. */
13246
13247 static tree
13248 tsubst_aggr_type (tree t,
13249 tree args,
13250 tsubst_flags_t complain,
13251 tree in_decl,
13252 int entering_scope)
13253 {
13254 if (t == NULL_TREE)
13255 return NULL_TREE;
13256
13257 switch (TREE_CODE (t))
13258 {
13259 case RECORD_TYPE:
13260 if (TYPE_PTRMEMFUNC_P (t))
13261 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13262
13263 /* Fall through. */
13264 case ENUMERAL_TYPE:
13265 case UNION_TYPE:
13266 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13267 {
13268 tree argvec;
13269 tree context;
13270 tree r;
13271
13272 /* In "sizeof(X<I>)" we need to evaluate "I". */
13273 cp_evaluated ev;
13274
13275 /* First, determine the context for the type we are looking
13276 up. */
13277 context = TYPE_CONTEXT (t);
13278 if (context && TYPE_P (context))
13279 {
13280 context = tsubst_aggr_type (context, args, complain,
13281 in_decl, /*entering_scope=*/1);
13282 /* If context is a nested class inside a class template,
13283 it may still need to be instantiated (c++/33959). */
13284 context = complete_type (context);
13285 }
13286
13287 /* Then, figure out what arguments are appropriate for the
13288 type we are trying to find. For example, given:
13289
13290 template <class T> struct S;
13291 template <class T, class U> void f(T, U) { S<U> su; }
13292
13293 and supposing that we are instantiating f<int, double>,
13294 then our ARGS will be {int, double}, but, when looking up
13295 S we only want {double}. */
13296 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13297 complain, in_decl);
13298 if (argvec == error_mark_node)
13299 r = error_mark_node;
13300 else
13301 {
13302 r = lookup_template_class (t, argvec, in_decl, context,
13303 entering_scope, complain);
13304 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13305 }
13306
13307 return r;
13308 }
13309 else
13310 /* This is not a template type, so there's nothing to do. */
13311 return t;
13312
13313 default:
13314 return tsubst (t, args, complain, in_decl);
13315 }
13316 }
13317
13318 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13319
13320 /* Substitute into the default argument ARG (a default argument for
13321 FN), which has the indicated TYPE. */
13322
13323 tree
13324 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13325 tsubst_flags_t complain)
13326 {
13327 int errs = errorcount + sorrycount;
13328
13329 /* This can happen in invalid code. */
13330 if (TREE_CODE (arg) == DEFERRED_PARSE)
13331 return arg;
13332
13333 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13334 parm = chain_index (parmnum, parm);
13335 tree parmtype = TREE_TYPE (parm);
13336 if (DECL_BY_REFERENCE (parm))
13337 parmtype = TREE_TYPE (parmtype);
13338 if (parmtype == error_mark_node)
13339 return error_mark_node;
13340
13341 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13342
13343 tree *slot;
13344 if (defarg_inst && (slot = defarg_inst->get (parm)))
13345 return *slot;
13346
13347 /* This default argument came from a template. Instantiate the
13348 default argument here, not in tsubst. In the case of
13349 something like:
13350
13351 template <class T>
13352 struct S {
13353 static T t();
13354 void f(T = t());
13355 };
13356
13357 we must be careful to do name lookup in the scope of S<T>,
13358 rather than in the current class. */
13359 push_to_top_level ();
13360 push_access_scope (fn);
13361 push_deferring_access_checks (dk_no_deferred);
13362 start_lambda_scope (parm);
13363
13364 /* The default argument expression may cause implicitly defined
13365 member functions to be synthesized, which will result in garbage
13366 collection. We must treat this situation as if we were within
13367 the body of function so as to avoid collecting live data on the
13368 stack. */
13369 ++function_depth;
13370 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13371 complain, NULL_TREE,
13372 /*integral_constant_expression_p=*/false);
13373 --function_depth;
13374
13375 finish_lambda_scope ();
13376
13377 /* Make sure the default argument is reasonable. */
13378 arg = check_default_argument (type, arg, complain);
13379
13380 if (errorcount+sorrycount > errs
13381 && (complain & tf_warning_or_error))
13382 inform (input_location,
13383 " when instantiating default argument for call to %qD", fn);
13384
13385 pop_deferring_access_checks ();
13386 pop_access_scope (fn);
13387 pop_from_top_level ();
13388
13389 if (arg != error_mark_node && !cp_unevaluated_operand)
13390 {
13391 if (!defarg_inst)
13392 defarg_inst = decl_tree_cache_map::create_ggc (37);
13393 defarg_inst->put (parm, arg);
13394 }
13395
13396 return arg;
13397 }
13398
13399 /* Substitute into all the default arguments for FN. */
13400
13401 static void
13402 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13403 {
13404 tree arg;
13405 tree tmpl_args;
13406
13407 tmpl_args = DECL_TI_ARGS (fn);
13408
13409 /* If this function is not yet instantiated, we certainly don't need
13410 its default arguments. */
13411 if (uses_template_parms (tmpl_args))
13412 return;
13413 /* Don't do this again for clones. */
13414 if (DECL_CLONED_FUNCTION_P (fn))
13415 return;
13416
13417 int i = 0;
13418 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13419 arg;
13420 arg = TREE_CHAIN (arg), ++i)
13421 if (TREE_PURPOSE (arg))
13422 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13423 TREE_VALUE (arg),
13424 TREE_PURPOSE (arg),
13425 complain);
13426 }
13427
13428 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13429 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13430
13431 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13432
13433 void
13434 store_explicit_specifier (tree v, tree t)
13435 {
13436 if (!explicit_specifier_map)
13437 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13438 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13439 explicit_specifier_map->put (v, t);
13440 }
13441
13442 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13443
13444 static tree
13445 lookup_explicit_specifier (tree v)
13446 {
13447 return *explicit_specifier_map->get (v);
13448 }
13449
13450 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13451
13452 static tree
13453 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13454 tree lambda_fntype)
13455 {
13456 tree gen_tmpl, argvec;
13457 hashval_t hash = 0;
13458 tree in_decl = t;
13459
13460 /* Nobody should be tsubst'ing into non-template functions. */
13461 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13462
13463 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13464 {
13465 /* If T is not dependent, just return it. */
13466 if (!uses_template_parms (DECL_TI_ARGS (t))
13467 && !LAMBDA_FUNCTION_P (t))
13468 return t;
13469
13470 /* Calculate the most general template of which R is a
13471 specialization. */
13472 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13473
13474 /* We're substituting a lambda function under tsubst_lambda_expr but not
13475 directly from it; find the matching function we're already inside.
13476 But don't do this if T is a generic lambda with a single level of
13477 template parms, as in that case we're doing a normal instantiation. */
13478 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13479 && (!generic_lambda_fn_p (t)
13480 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13481 return enclosing_instantiation_of (t);
13482
13483 /* Calculate the complete set of arguments used to
13484 specialize R. */
13485 argvec = tsubst_template_args (DECL_TI_ARGS
13486 (DECL_TEMPLATE_RESULT
13487 (DECL_TI_TEMPLATE (t))),
13488 args, complain, in_decl);
13489 if (argvec == error_mark_node)
13490 return error_mark_node;
13491
13492 /* Check to see if we already have this specialization. */
13493 if (!lambda_fntype)
13494 {
13495 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13496 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13497 return spec;
13498 }
13499
13500 /* We can see more levels of arguments than parameters if
13501 there was a specialization of a member template, like
13502 this:
13503
13504 template <class T> struct S { template <class U> void f(); }
13505 template <> template <class U> void S<int>::f(U);
13506
13507 Here, we'll be substituting into the specialization,
13508 because that's where we can find the code we actually
13509 want to generate, but we'll have enough arguments for
13510 the most general template.
13511
13512 We also deal with the peculiar case:
13513
13514 template <class T> struct S {
13515 template <class U> friend void f();
13516 };
13517 template <class U> void f() {}
13518 template S<int>;
13519 template void f<double>();
13520
13521 Here, the ARGS for the instantiation of will be {int,
13522 double}. But, we only need as many ARGS as there are
13523 levels of template parameters in CODE_PATTERN. We are
13524 careful not to get fooled into reducing the ARGS in
13525 situations like:
13526
13527 template <class T> struct S { template <class U> void f(U); }
13528 template <class T> template <> void S<T>::f(int) {}
13529
13530 which we can spot because the pattern will be a
13531 specialization in this case. */
13532 int args_depth = TMPL_ARGS_DEPTH (args);
13533 int parms_depth =
13534 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13535
13536 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13537 args = get_innermost_template_args (args, parms_depth);
13538 }
13539 else
13540 {
13541 /* This special case arises when we have something like this:
13542
13543 template <class T> struct S {
13544 friend void f<int>(int, double);
13545 };
13546
13547 Here, the DECL_TI_TEMPLATE for the friend declaration
13548 will be an IDENTIFIER_NODE. We are being called from
13549 tsubst_friend_function, and we want only to create a
13550 new decl (R) with appropriate types so that we can call
13551 determine_specialization. */
13552 gen_tmpl = NULL_TREE;
13553 argvec = NULL_TREE;
13554 }
13555
13556 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13557 : NULL_TREE);
13558 tree ctx = closure ? closure : DECL_CONTEXT (t);
13559 bool member = ctx && TYPE_P (ctx);
13560
13561 if (member && !closure)
13562 ctx = tsubst_aggr_type (ctx, args,
13563 complain, t, /*entering_scope=*/1);
13564
13565 tree type = (lambda_fntype ? lambda_fntype
13566 : tsubst (TREE_TYPE (t), args,
13567 complain | tf_fndecl_type, in_decl));
13568 if (type == error_mark_node)
13569 return error_mark_node;
13570
13571 /* If we hit excessive deduction depth, the type is bogus even if
13572 it isn't error_mark_node, so don't build a decl. */
13573 if (excessive_deduction_depth)
13574 return error_mark_node;
13575
13576 /* We do NOT check for matching decls pushed separately at this
13577 point, as they may not represent instantiations of this
13578 template, and in any case are considered separate under the
13579 discrete model. */
13580 tree r = copy_decl (t);
13581 DECL_USE_TEMPLATE (r) = 0;
13582 TREE_TYPE (r) = type;
13583 /* Clear out the mangled name and RTL for the instantiation. */
13584 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13585 SET_DECL_RTL (r, NULL);
13586 /* Leave DECL_INITIAL set on deleted instantiations. */
13587 if (!DECL_DELETED_FN (r))
13588 DECL_INITIAL (r) = NULL_TREE;
13589 DECL_CONTEXT (r) = ctx;
13590
13591 /* Handle explicit(dependent-expr). */
13592 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13593 {
13594 tree spec = lookup_explicit_specifier (t);
13595 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13596 /*function_p=*/false,
13597 /*i_c_e_p=*/true);
13598 spec = build_explicit_specifier (spec, complain);
13599 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13600 }
13601
13602 /* OpenMP UDRs have the only argument a reference to the declared
13603 type. We want to diagnose if the declared type is a reference,
13604 which is invalid, but as references to references are usually
13605 quietly merged, diagnose it here. */
13606 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13607 {
13608 tree argtype
13609 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13610 argtype = tsubst (argtype, args, complain, in_decl);
13611 if (TYPE_REF_P (argtype))
13612 error_at (DECL_SOURCE_LOCATION (t),
13613 "reference type %qT in "
13614 "%<#pragma omp declare reduction%>", argtype);
13615 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13616 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13617 argtype);
13618 }
13619
13620 if (member && DECL_CONV_FN_P (r))
13621 /* Type-conversion operator. Reconstruct the name, in
13622 case it's the name of one of the template's parameters. */
13623 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13624
13625 tree parms = DECL_ARGUMENTS (t);
13626 if (closure)
13627 parms = DECL_CHAIN (parms);
13628 parms = tsubst (parms, args, complain, t);
13629 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13630 DECL_CONTEXT (parm) = r;
13631 if (closure)
13632 {
13633 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13634 DECL_CHAIN (tparm) = parms;
13635 parms = tparm;
13636 }
13637 DECL_ARGUMENTS (r) = parms;
13638 DECL_RESULT (r) = NULL_TREE;
13639
13640 TREE_STATIC (r) = 0;
13641 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13642 DECL_EXTERNAL (r) = 1;
13643 /* If this is an instantiation of a function with internal
13644 linkage, we already know what object file linkage will be
13645 assigned to the instantiation. */
13646 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13647 DECL_DEFER_OUTPUT (r) = 0;
13648 DECL_CHAIN (r) = NULL_TREE;
13649 DECL_PENDING_INLINE_INFO (r) = 0;
13650 DECL_PENDING_INLINE_P (r) = 0;
13651 DECL_SAVED_TREE (r) = NULL_TREE;
13652 DECL_STRUCT_FUNCTION (r) = NULL;
13653 TREE_USED (r) = 0;
13654 /* We'll re-clone as appropriate in instantiate_template. */
13655 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13656
13657 /* If we aren't complaining now, return on error before we register
13658 the specialization so that we'll complain eventually. */
13659 if ((complain & tf_error) == 0
13660 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13661 && !grok_op_properties (r, /*complain=*/false))
13662 return error_mark_node;
13663
13664 /* Associate the constraints directly with the instantiation. We
13665 don't substitute through the constraints; that's only done when
13666 they are checked. */
13667 if (tree ci = get_constraints (t))
13668 set_constraints (r, ci);
13669
13670 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13671 SET_DECL_FRIEND_CONTEXT (r,
13672 tsubst (DECL_FRIEND_CONTEXT (t),
13673 args, complain, in_decl));
13674
13675 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13676 this in the special friend case mentioned above where
13677 GEN_TMPL is NULL. */
13678 if (gen_tmpl && !closure)
13679 {
13680 DECL_TEMPLATE_INFO (r)
13681 = build_template_info (gen_tmpl, argvec);
13682 SET_DECL_IMPLICIT_INSTANTIATION (r);
13683
13684 tree new_r
13685 = register_specialization (r, gen_tmpl, argvec, false, hash);
13686 if (new_r != r)
13687 /* We instantiated this while substituting into
13688 the type earlier (template/friend54.C). */
13689 return new_r;
13690
13691 /* We're not supposed to instantiate default arguments
13692 until they are called, for a template. But, for a
13693 declaration like:
13694
13695 template <class T> void f ()
13696 { extern void g(int i = T()); }
13697
13698 we should do the substitution when the template is
13699 instantiated. We handle the member function case in
13700 instantiate_class_template since the default arguments
13701 might refer to other members of the class. */
13702 if (!member
13703 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13704 && !uses_template_parms (argvec))
13705 tsubst_default_arguments (r, complain);
13706 }
13707 else
13708 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13709
13710 /* Copy the list of befriending classes. */
13711 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13712 *friends;
13713 friends = &TREE_CHAIN (*friends))
13714 {
13715 *friends = copy_node (*friends);
13716 TREE_VALUE (*friends)
13717 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13718 }
13719
13720 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13721 {
13722 maybe_retrofit_in_chrg (r);
13723 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13724 return error_mark_node;
13725 /* If this is an instantiation of a member template, clone it.
13726 If it isn't, that'll be handled by
13727 clone_constructors_and_destructors. */
13728 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13729 clone_function_decl (r, /*update_methods=*/false);
13730 }
13731 else if ((complain & tf_error) != 0
13732 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13733 && !grok_op_properties (r, /*complain=*/true))
13734 return error_mark_node;
13735
13736 /* Possibly limit visibility based on template args. */
13737 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13738 if (DECL_VISIBILITY_SPECIFIED (t))
13739 {
13740 DECL_VISIBILITY_SPECIFIED (r) = 0;
13741 DECL_ATTRIBUTES (r)
13742 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13743 }
13744 determine_visibility (r);
13745 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13746 && !processing_template_decl)
13747 defaulted_late_check (r);
13748
13749 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13750 args, complain, in_decl);
13751 if (flag_openmp)
13752 if (tree attr = lookup_attribute ("omp declare variant base",
13753 DECL_ATTRIBUTES (r)))
13754 omp_declare_variant_finalize (r, attr);
13755
13756 return r;
13757 }
13758
13759 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13760
13761 static tree
13762 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13763 tree lambda_fntype)
13764 {
13765 /* We can get here when processing a member function template,
13766 member class template, or template template parameter. */
13767 tree decl = DECL_TEMPLATE_RESULT (t);
13768 tree in_decl = t;
13769 tree spec;
13770 tree tmpl_args;
13771 tree full_args;
13772 tree r;
13773 hashval_t hash = 0;
13774
13775 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13776 {
13777 /* Template template parameter is treated here. */
13778 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13779 if (new_type == error_mark_node)
13780 r = error_mark_node;
13781 /* If we get a real template back, return it. This can happen in
13782 the context of most_specialized_partial_spec. */
13783 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13784 r = new_type;
13785 else
13786 /* The new TEMPLATE_DECL was built in
13787 reduce_template_parm_level. */
13788 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13789 return r;
13790 }
13791
13792 if (!lambda_fntype)
13793 {
13794 /* We might already have an instance of this template.
13795 The ARGS are for the surrounding class type, so the
13796 full args contain the tsubst'd args for the context,
13797 plus the innermost args from the template decl. */
13798 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13799 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13800 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13801 /* Because this is a template, the arguments will still be
13802 dependent, even after substitution. If
13803 PROCESSING_TEMPLATE_DECL is not set, the dependency
13804 predicates will short-circuit. */
13805 ++processing_template_decl;
13806 full_args = tsubst_template_args (tmpl_args, args,
13807 complain, in_decl);
13808 --processing_template_decl;
13809 if (full_args == error_mark_node)
13810 return error_mark_node;
13811
13812 /* If this is a default template template argument,
13813 tsubst might not have changed anything. */
13814 if (full_args == tmpl_args)
13815 return t;
13816
13817 hash = hash_tmpl_and_args (t, full_args);
13818 spec = retrieve_specialization (t, full_args, hash);
13819 if (spec != NULL_TREE)
13820 {
13821 if (TYPE_P (spec))
13822 /* Type partial instantiations are stored as the type by
13823 lookup_template_class_1, not here as the template. */
13824 spec = CLASSTYPE_TI_TEMPLATE (spec);
13825 return spec;
13826 }
13827 }
13828
13829 /* Make a new template decl. It will be similar to the
13830 original, but will record the current template arguments.
13831 We also create a new function declaration, which is just
13832 like the old one, but points to this new template, rather
13833 than the old one. */
13834 r = copy_decl (t);
13835 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13836 DECL_CHAIN (r) = NULL_TREE;
13837
13838 // Build new template info linking to the original template decl.
13839 if (!lambda_fntype)
13840 {
13841 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13842 SET_DECL_IMPLICIT_INSTANTIATION (r);
13843 }
13844 else
13845 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13846
13847 /* The template parameters for this new template are all the
13848 template parameters for the old template, except the
13849 outermost level of parameters. */
13850 DECL_TEMPLATE_PARMS (r)
13851 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13852 complain);
13853
13854 if (TREE_CODE (decl) == TYPE_DECL
13855 && !TYPE_DECL_ALIAS_P (decl))
13856 {
13857 tree new_type;
13858 ++processing_template_decl;
13859 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13860 --processing_template_decl;
13861 if (new_type == error_mark_node)
13862 return error_mark_node;
13863
13864 TREE_TYPE (r) = new_type;
13865 /* For a partial specialization, we need to keep pointing to
13866 the primary template. */
13867 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13868 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13869 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13870 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13871 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13872 }
13873 else
13874 {
13875 tree new_decl;
13876 ++processing_template_decl;
13877 if (TREE_CODE (decl) == FUNCTION_DECL)
13878 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13879 else
13880 new_decl = tsubst (decl, args, complain, in_decl);
13881 --processing_template_decl;
13882 if (new_decl == error_mark_node)
13883 return error_mark_node;
13884
13885 DECL_TEMPLATE_RESULT (r) = new_decl;
13886 TREE_TYPE (r) = TREE_TYPE (new_decl);
13887 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13888 if (lambda_fntype)
13889 {
13890 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13891 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13892 }
13893 else
13894 {
13895 DECL_TI_TEMPLATE (new_decl) = r;
13896 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13897 }
13898 }
13899
13900 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13901 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13902
13903 if (PRIMARY_TEMPLATE_P (t))
13904 DECL_PRIMARY_TEMPLATE (r) = r;
13905
13906 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13907 && !lambda_fntype)
13908 /* Record this non-type partial instantiation. */
13909 register_specialization (r, t,
13910 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13911 false, hash);
13912
13913 return r;
13914 }
13915
13916 /* True if FN is the op() for a lambda in an uninstantiated template. */
13917
13918 bool
13919 lambda_fn_in_template_p (tree fn)
13920 {
13921 if (!fn || !LAMBDA_FUNCTION_P (fn))
13922 return false;
13923 tree closure = DECL_CONTEXT (fn);
13924 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13925 }
13926
13927 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13928 which the above is true. */
13929
13930 bool
13931 instantiated_lambda_fn_p (tree fn)
13932 {
13933 if (!fn || !LAMBDA_FUNCTION_P (fn))
13934 return false;
13935 tree closure = DECL_CONTEXT (fn);
13936 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13937 return LAMBDA_EXPR_INSTANTIATED (lam);
13938 }
13939
13940 /* We're instantiating a variable from template function TCTX. Return the
13941 corresponding current enclosing scope. This gets complicated because lambda
13942 functions in templates are regenerated rather than instantiated, but generic
13943 lambda functions are subsequently instantiated. */
13944
13945 static tree
13946 enclosing_instantiation_of (tree otctx)
13947 {
13948 tree tctx = otctx;
13949 tree fn = current_function_decl;
13950 int lambda_count = 0;
13951
13952 for (; tctx && (lambda_fn_in_template_p (tctx)
13953 || instantiated_lambda_fn_p (tctx));
13954 tctx = decl_function_context (tctx))
13955 ++lambda_count;
13956 for (; fn; fn = decl_function_context (fn))
13957 {
13958 tree ofn = fn;
13959 int flambda_count = 0;
13960 for (; fn && instantiated_lambda_fn_p (fn);
13961 fn = decl_function_context (fn))
13962 ++flambda_count;
13963 if ((fn && DECL_TEMPLATE_INFO (fn))
13964 ? most_general_template (fn) != most_general_template (tctx)
13965 : fn != tctx)
13966 continue;
13967 if (flambda_count != lambda_count)
13968 {
13969 gcc_assert (flambda_count > lambda_count);
13970 for (; flambda_count > lambda_count; --flambda_count)
13971 ofn = decl_function_context (ofn);
13972 }
13973 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13974 || DECL_CONV_FN_P (ofn));
13975 return ofn;
13976 }
13977 gcc_unreachable ();
13978 }
13979
13980 /* Substitute the ARGS into the T, which is a _DECL. Return the
13981 result of the substitution. Issue error and warning messages under
13982 control of COMPLAIN. */
13983
13984 static tree
13985 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13986 {
13987 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13988 location_t saved_loc;
13989 tree r = NULL_TREE;
13990 tree in_decl = t;
13991 hashval_t hash = 0;
13992
13993 /* Set the filename and linenumber to improve error-reporting. */
13994 saved_loc = input_location;
13995 input_location = DECL_SOURCE_LOCATION (t);
13996
13997 switch (TREE_CODE (t))
13998 {
13999 case TEMPLATE_DECL:
14000 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14001 break;
14002
14003 case FUNCTION_DECL:
14004 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14005 break;
14006
14007 case PARM_DECL:
14008 {
14009 tree type = NULL_TREE;
14010 int i, len = 1;
14011 tree expanded_types = NULL_TREE;
14012 tree prev_r = NULL_TREE;
14013 tree first_r = NULL_TREE;
14014
14015 if (DECL_PACK_P (t))
14016 {
14017 /* If there is a local specialization that isn't a
14018 parameter pack, it means that we're doing a "simple"
14019 substitution from inside tsubst_pack_expansion. Just
14020 return the local specialization (which will be a single
14021 parm). */
14022 tree spec = retrieve_local_specialization (t);
14023 if (spec
14024 && TREE_CODE (spec) == PARM_DECL
14025 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14026 RETURN (spec);
14027
14028 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14029 the parameters in this function parameter pack. */
14030 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14031 complain, in_decl);
14032 if (TREE_CODE (expanded_types) == TREE_VEC)
14033 {
14034 len = TREE_VEC_LENGTH (expanded_types);
14035
14036 /* Zero-length parameter packs are boring. Just substitute
14037 into the chain. */
14038 if (len == 0)
14039 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14040 TREE_CHAIN (t)));
14041 }
14042 else
14043 {
14044 /* All we did was update the type. Make a note of that. */
14045 type = expanded_types;
14046 expanded_types = NULL_TREE;
14047 }
14048 }
14049
14050 /* Loop through all of the parameters we'll build. When T is
14051 a function parameter pack, LEN is the number of expanded
14052 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14053 r = NULL_TREE;
14054 for (i = 0; i < len; ++i)
14055 {
14056 prev_r = r;
14057 r = copy_node (t);
14058 if (DECL_TEMPLATE_PARM_P (t))
14059 SET_DECL_TEMPLATE_PARM_P (r);
14060
14061 if (expanded_types)
14062 /* We're on the Ith parameter of the function parameter
14063 pack. */
14064 {
14065 /* Get the Ith type. */
14066 type = TREE_VEC_ELT (expanded_types, i);
14067
14068 /* Rename the parameter to include the index. */
14069 DECL_NAME (r)
14070 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14071 }
14072 else if (!type)
14073 /* We're dealing with a normal parameter. */
14074 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14075
14076 type = type_decays_to (type);
14077 TREE_TYPE (r) = type;
14078 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14079
14080 if (DECL_INITIAL (r))
14081 {
14082 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14083 DECL_INITIAL (r) = TREE_TYPE (r);
14084 else
14085 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14086 complain, in_decl);
14087 }
14088
14089 DECL_CONTEXT (r) = NULL_TREE;
14090
14091 if (!DECL_TEMPLATE_PARM_P (r))
14092 DECL_ARG_TYPE (r) = type_passed_as (type);
14093
14094 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14095 args, complain, in_decl);
14096
14097 /* Keep track of the first new parameter we
14098 generate. That's what will be returned to the
14099 caller. */
14100 if (!first_r)
14101 first_r = r;
14102
14103 /* Build a proper chain of parameters when substituting
14104 into a function parameter pack. */
14105 if (prev_r)
14106 DECL_CHAIN (prev_r) = r;
14107 }
14108
14109 /* If cp_unevaluated_operand is set, we're just looking for a
14110 single dummy parameter, so don't keep going. */
14111 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14112 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14113 complain, DECL_CHAIN (t));
14114
14115 /* FIRST_R contains the start of the chain we've built. */
14116 r = first_r;
14117 }
14118 break;
14119
14120 case FIELD_DECL:
14121 {
14122 tree type = NULL_TREE;
14123 tree vec = NULL_TREE;
14124 tree expanded_types = NULL_TREE;
14125 int len = 1;
14126
14127 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14128 {
14129 /* This field is a lambda capture pack. Return a TREE_VEC of
14130 the expanded fields to instantiate_class_template_1. */
14131 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14132 complain, in_decl);
14133 if (TREE_CODE (expanded_types) == TREE_VEC)
14134 {
14135 len = TREE_VEC_LENGTH (expanded_types);
14136 vec = make_tree_vec (len);
14137 }
14138 else
14139 {
14140 /* All we did was update the type. Make a note of that. */
14141 type = expanded_types;
14142 expanded_types = NULL_TREE;
14143 }
14144 }
14145
14146 for (int i = 0; i < len; ++i)
14147 {
14148 r = copy_decl (t);
14149 if (expanded_types)
14150 {
14151 type = TREE_VEC_ELT (expanded_types, i);
14152 DECL_NAME (r)
14153 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14154 }
14155 else if (!type)
14156 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14157
14158 if (type == error_mark_node)
14159 RETURN (error_mark_node);
14160 TREE_TYPE (r) = type;
14161 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14162
14163 if (DECL_C_BIT_FIELD (r))
14164 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14165 number of bits. */
14166 DECL_BIT_FIELD_REPRESENTATIVE (r)
14167 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14168 complain, in_decl,
14169 /*integral_constant_expression_p=*/true);
14170 if (DECL_INITIAL (t))
14171 {
14172 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14173 NSDMI in perform_member_init. Still set DECL_INITIAL
14174 so that we know there is one. */
14175 DECL_INITIAL (r) = void_node;
14176 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14177 retrofit_lang_decl (r);
14178 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14179 }
14180 /* We don't have to set DECL_CONTEXT here; it is set by
14181 finish_member_declaration. */
14182 DECL_CHAIN (r) = NULL_TREE;
14183
14184 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14185 args, complain, in_decl);
14186
14187 if (vec)
14188 TREE_VEC_ELT (vec, i) = r;
14189 }
14190
14191 if (vec)
14192 r = vec;
14193 }
14194 break;
14195
14196 case USING_DECL:
14197 /* We reach here only for member using decls. We also need to check
14198 uses_template_parms because DECL_DEPENDENT_P is not set for a
14199 using-declaration that designates a member of the current
14200 instantiation (c++/53549). */
14201 if (DECL_DEPENDENT_P (t)
14202 || uses_template_parms (USING_DECL_SCOPE (t)))
14203 {
14204 tree scope = USING_DECL_SCOPE (t);
14205 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14206 if (PACK_EXPANSION_P (scope))
14207 {
14208 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14209 int len = TREE_VEC_LENGTH (vec);
14210 r = make_tree_vec (len);
14211 for (int i = 0; i < len; ++i)
14212 {
14213 tree escope = TREE_VEC_ELT (vec, i);
14214 tree elt = do_class_using_decl (escope, name);
14215 if (!elt)
14216 {
14217 r = error_mark_node;
14218 break;
14219 }
14220 else
14221 {
14222 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14223 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14224 }
14225 TREE_VEC_ELT (r, i) = elt;
14226 }
14227 }
14228 else
14229 {
14230 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14231 complain, in_decl);
14232 r = do_class_using_decl (inst_scope, name);
14233 if (!r)
14234 r = error_mark_node;
14235 else
14236 {
14237 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14238 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14239 }
14240 }
14241 }
14242 else
14243 {
14244 r = copy_node (t);
14245 DECL_CHAIN (r) = NULL_TREE;
14246 }
14247 break;
14248
14249 case TYPE_DECL:
14250 case VAR_DECL:
14251 {
14252 tree argvec = NULL_TREE;
14253 tree gen_tmpl = NULL_TREE;
14254 tree spec;
14255 tree tmpl = NULL_TREE;
14256 tree ctx;
14257 tree type = NULL_TREE;
14258 bool local_p;
14259
14260 if (TREE_TYPE (t) == error_mark_node)
14261 RETURN (error_mark_node);
14262
14263 if (TREE_CODE (t) == TYPE_DECL
14264 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14265 {
14266 /* If this is the canonical decl, we don't have to
14267 mess with instantiations, and often we can't (for
14268 typename, template type parms and such). Note that
14269 TYPE_NAME is not correct for the above test if
14270 we've copied the type for a typedef. */
14271 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14272 if (type == error_mark_node)
14273 RETURN (error_mark_node);
14274 r = TYPE_NAME (type);
14275 break;
14276 }
14277
14278 /* Check to see if we already have the specialization we
14279 need. */
14280 spec = NULL_TREE;
14281 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14282 {
14283 /* T is a static data member or namespace-scope entity.
14284 We have to substitute into namespace-scope variables
14285 (not just variable templates) because of cases like:
14286
14287 template <class T> void f() { extern T t; }
14288
14289 where the entity referenced is not known until
14290 instantiation time. */
14291 local_p = false;
14292 ctx = DECL_CONTEXT (t);
14293 if (DECL_CLASS_SCOPE_P (t))
14294 {
14295 ctx = tsubst_aggr_type (ctx, args,
14296 complain,
14297 in_decl, /*entering_scope=*/1);
14298 /* If CTX is unchanged, then T is in fact the
14299 specialization we want. That situation occurs when
14300 referencing a static data member within in its own
14301 class. We can use pointer equality, rather than
14302 same_type_p, because DECL_CONTEXT is always
14303 canonical... */
14304 if (ctx == DECL_CONTEXT (t)
14305 /* ... unless T is a member template; in which
14306 case our caller can be willing to create a
14307 specialization of that template represented
14308 by T. */
14309 && !(DECL_TI_TEMPLATE (t)
14310 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14311 spec = t;
14312 }
14313
14314 if (!spec)
14315 {
14316 tmpl = DECL_TI_TEMPLATE (t);
14317 gen_tmpl = most_general_template (tmpl);
14318 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14319 if (argvec != error_mark_node)
14320 argvec = (coerce_innermost_template_parms
14321 (DECL_TEMPLATE_PARMS (gen_tmpl),
14322 argvec, t, complain,
14323 /*all*/true, /*defarg*/true));
14324 if (argvec == error_mark_node)
14325 RETURN (error_mark_node);
14326 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14327 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14328 }
14329 }
14330 else
14331 {
14332 /* A local variable. */
14333 local_p = true;
14334 /* Subsequent calls to pushdecl will fill this in. */
14335 ctx = NULL_TREE;
14336 /* Unless this is a reference to a static variable from an
14337 enclosing function, in which case we need to fill it in now. */
14338 if (TREE_STATIC (t))
14339 {
14340 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14341 if (fn != current_function_decl)
14342 ctx = fn;
14343 }
14344 spec = retrieve_local_specialization (t);
14345 }
14346 /* If we already have the specialization we need, there is
14347 nothing more to do. */
14348 if (spec)
14349 {
14350 r = spec;
14351 break;
14352 }
14353
14354 /* Create a new node for the specialization we need. */
14355 if (type == NULL_TREE)
14356 {
14357 if (is_typedef_decl (t))
14358 type = DECL_ORIGINAL_TYPE (t);
14359 else
14360 type = TREE_TYPE (t);
14361 if (VAR_P (t)
14362 && VAR_HAD_UNKNOWN_BOUND (t)
14363 && type != error_mark_node)
14364 type = strip_array_domain (type);
14365 tree sub_args = args;
14366 if (tree auto_node = type_uses_auto (type))
14367 {
14368 /* Mask off any template args past the variable's context so we
14369 don't replace the auto with an unrelated argument. */
14370 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14371 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14372 if (extra > 0)
14373 /* This should never happen with the new lambda instantiation
14374 model, but keep the handling just in case. */
14375 gcc_assert (!CHECKING_P),
14376 sub_args = strip_innermost_template_args (args, extra);
14377 }
14378 type = tsubst (type, sub_args, complain, in_decl);
14379 /* Substituting the type might have recursively instantiated this
14380 same alias (c++/86171). */
14381 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14382 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14383 {
14384 r = spec;
14385 break;
14386 }
14387 }
14388 r = copy_decl (t);
14389 if (VAR_P (r))
14390 {
14391 DECL_INITIALIZED_P (r) = 0;
14392 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14393 if (type == error_mark_node)
14394 RETURN (error_mark_node);
14395 if (TREE_CODE (type) == FUNCTION_TYPE)
14396 {
14397 /* It may seem that this case cannot occur, since:
14398
14399 typedef void f();
14400 void g() { f x; }
14401
14402 declares a function, not a variable. However:
14403
14404 typedef void f();
14405 template <typename T> void g() { T t; }
14406 template void g<f>();
14407
14408 is an attempt to declare a variable with function
14409 type. */
14410 error ("variable %qD has function type",
14411 /* R is not yet sufficiently initialized, so we
14412 just use its name. */
14413 DECL_NAME (r));
14414 RETURN (error_mark_node);
14415 }
14416 type = complete_type (type);
14417 /* Wait until cp_finish_decl to set this again, to handle
14418 circular dependency (template/instantiate6.C). */
14419 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14420 type = check_var_type (DECL_NAME (r), type,
14421 DECL_SOURCE_LOCATION (r));
14422 if (DECL_HAS_VALUE_EXPR_P (t))
14423 {
14424 tree ve = DECL_VALUE_EXPR (t);
14425 ve = tsubst_expr (ve, args, complain, in_decl,
14426 /*constant_expression_p=*/false);
14427 if (REFERENCE_REF_P (ve))
14428 {
14429 gcc_assert (TYPE_REF_P (type));
14430 ve = TREE_OPERAND (ve, 0);
14431 }
14432 SET_DECL_VALUE_EXPR (r, ve);
14433 }
14434 if (CP_DECL_THREAD_LOCAL_P (r)
14435 && !processing_template_decl)
14436 set_decl_tls_model (r, decl_default_tls_model (r));
14437 }
14438 else if (DECL_SELF_REFERENCE_P (t))
14439 SET_DECL_SELF_REFERENCE_P (r);
14440 TREE_TYPE (r) = type;
14441 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14442 DECL_CONTEXT (r) = ctx;
14443 /* Clear out the mangled name and RTL for the instantiation. */
14444 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14445 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14446 SET_DECL_RTL (r, NULL);
14447 /* The initializer must not be expanded until it is required;
14448 see [temp.inst]. */
14449 DECL_INITIAL (r) = NULL_TREE;
14450 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14451 if (VAR_P (r))
14452 {
14453 if (DECL_LANG_SPECIFIC (r))
14454 SET_DECL_DEPENDENT_INIT_P (r, false);
14455
14456 SET_DECL_MODE (r, VOIDmode);
14457
14458 /* Possibly limit visibility based on template args. */
14459 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14460 if (DECL_VISIBILITY_SPECIFIED (t))
14461 {
14462 DECL_VISIBILITY_SPECIFIED (r) = 0;
14463 DECL_ATTRIBUTES (r)
14464 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14465 }
14466 determine_visibility (r);
14467 }
14468
14469 if (!local_p)
14470 {
14471 /* A static data member declaration is always marked
14472 external when it is declared in-class, even if an
14473 initializer is present. We mimic the non-template
14474 processing here. */
14475 DECL_EXTERNAL (r) = 1;
14476 if (DECL_NAMESPACE_SCOPE_P (t))
14477 DECL_NOT_REALLY_EXTERN (r) = 1;
14478
14479 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14480 SET_DECL_IMPLICIT_INSTANTIATION (r);
14481 /* Remember whether we require constant initialization of
14482 a non-constant template variable. */
14483 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14484 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14485 if (!error_operand_p (r) || (complain & tf_error))
14486 register_specialization (r, gen_tmpl, argvec, false, hash);
14487 }
14488 else
14489 {
14490 if (DECL_LANG_SPECIFIC (r))
14491 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14492 if (!cp_unevaluated_operand)
14493 register_local_specialization (r, t);
14494 }
14495
14496 DECL_CHAIN (r) = NULL_TREE;
14497
14498 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14499 /*flags=*/0,
14500 args, complain, in_decl);
14501
14502 /* Preserve a typedef that names a type. */
14503 if (is_typedef_decl (r) && type != error_mark_node)
14504 {
14505 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14506 set_underlying_type (r);
14507 if (TYPE_DECL_ALIAS_P (r))
14508 /* An alias template specialization can be dependent
14509 even if its underlying type is not. */
14510 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14511 }
14512
14513 layout_decl (r, 0);
14514 }
14515 break;
14516
14517 default:
14518 gcc_unreachable ();
14519 }
14520 #undef RETURN
14521
14522 out:
14523 /* Restore the file and line information. */
14524 input_location = saved_loc;
14525
14526 return r;
14527 }
14528
14529 /* Substitute into the complete parameter type list PARMS. */
14530
14531 tree
14532 tsubst_function_parms (tree parms,
14533 tree args,
14534 tsubst_flags_t complain,
14535 tree in_decl)
14536 {
14537 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14538 }
14539
14540 /* Substitute into the ARG_TYPES of a function type.
14541 If END is a TREE_CHAIN, leave it and any following types
14542 un-substituted. */
14543
14544 static tree
14545 tsubst_arg_types (tree arg_types,
14546 tree args,
14547 tree end,
14548 tsubst_flags_t complain,
14549 tree in_decl)
14550 {
14551 tree remaining_arg_types;
14552 tree type = NULL_TREE;
14553 int i = 1;
14554 tree expanded_args = NULL_TREE;
14555 tree default_arg;
14556
14557 if (!arg_types || arg_types == void_list_node || arg_types == end)
14558 return arg_types;
14559
14560 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14561 args, end, complain, in_decl);
14562 if (remaining_arg_types == error_mark_node)
14563 return error_mark_node;
14564
14565 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14566 {
14567 /* For a pack expansion, perform substitution on the
14568 entire expression. Later on, we'll handle the arguments
14569 one-by-one. */
14570 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14571 args, complain, in_decl);
14572
14573 if (TREE_CODE (expanded_args) == TREE_VEC)
14574 /* So that we'll spin through the parameters, one by one. */
14575 i = TREE_VEC_LENGTH (expanded_args);
14576 else
14577 {
14578 /* We only partially substituted into the parameter
14579 pack. Our type is TYPE_PACK_EXPANSION. */
14580 type = expanded_args;
14581 expanded_args = NULL_TREE;
14582 }
14583 }
14584
14585 while (i > 0) {
14586 --i;
14587
14588 if (expanded_args)
14589 type = TREE_VEC_ELT (expanded_args, i);
14590 else if (!type)
14591 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14592
14593 if (type == error_mark_node)
14594 return error_mark_node;
14595 if (VOID_TYPE_P (type))
14596 {
14597 if (complain & tf_error)
14598 {
14599 error ("invalid parameter type %qT", type);
14600 if (in_decl)
14601 error ("in declaration %q+D", in_decl);
14602 }
14603 return error_mark_node;
14604 }
14605 /* DR 657. */
14606 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14607 return error_mark_node;
14608
14609 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14610 top-level qualifiers as required. */
14611 type = cv_unqualified (type_decays_to (type));
14612
14613 /* We do not substitute into default arguments here. The standard
14614 mandates that they be instantiated only when needed, which is
14615 done in build_over_call. */
14616 default_arg = TREE_PURPOSE (arg_types);
14617
14618 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14619 since the new op() won't have any associated template arguments for us
14620 to refer to later. */
14621 if (lambda_fn_in_template_p (in_decl))
14622 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14623 false/*fn*/, false/*constexpr*/);
14624
14625 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14626 {
14627 /* We've instantiated a template before its default arguments
14628 have been parsed. This can happen for a nested template
14629 class, and is not an error unless we require the default
14630 argument in a call of this function. */
14631 remaining_arg_types =
14632 tree_cons (default_arg, type, remaining_arg_types);
14633 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14634 remaining_arg_types);
14635 }
14636 else
14637 remaining_arg_types =
14638 hash_tree_cons (default_arg, type, remaining_arg_types);
14639 }
14640
14641 return remaining_arg_types;
14642 }
14643
14644 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14645 *not* handle the exception-specification for FNTYPE, because the
14646 initial substitution of explicitly provided template parameters
14647 during argument deduction forbids substitution into the
14648 exception-specification:
14649
14650 [temp.deduct]
14651
14652 All references in the function type of the function template to the
14653 corresponding template parameters are replaced by the specified tem-
14654 plate argument values. If a substitution in a template parameter or
14655 in the function type of the function template results in an invalid
14656 type, type deduction fails. [Note: The equivalent substitution in
14657 exception specifications is done only when the function is instanti-
14658 ated, at which point a program is ill-formed if the substitution
14659 results in an invalid type.] */
14660
14661 static tree
14662 tsubst_function_type (tree t,
14663 tree args,
14664 tsubst_flags_t complain,
14665 tree in_decl)
14666 {
14667 tree return_type;
14668 tree arg_types = NULL_TREE;
14669 tree fntype;
14670
14671 /* The TYPE_CONTEXT is not used for function/method types. */
14672 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14673
14674 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14675 failure. */
14676 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14677
14678 if (late_return_type_p)
14679 {
14680 /* Substitute the argument types. */
14681 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14682 complain, in_decl);
14683 if (arg_types == error_mark_node)
14684 return error_mark_node;
14685
14686 tree save_ccp = current_class_ptr;
14687 tree save_ccr = current_class_ref;
14688 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14689 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14690 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14691 if (do_inject)
14692 {
14693 /* DR 1207: 'this' is in scope in the trailing return type. */
14694 inject_this_parameter (this_type, cp_type_quals (this_type));
14695 }
14696
14697 /* Substitute the return type. */
14698 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14699
14700 if (do_inject)
14701 {
14702 current_class_ptr = save_ccp;
14703 current_class_ref = save_ccr;
14704 }
14705 }
14706 else
14707 /* Substitute the return type. */
14708 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14709
14710 if (return_type == error_mark_node)
14711 return error_mark_node;
14712 /* DR 486 clarifies that creation of a function type with an
14713 invalid return type is a deduction failure. */
14714 if (TREE_CODE (return_type) == ARRAY_TYPE
14715 || TREE_CODE (return_type) == FUNCTION_TYPE)
14716 {
14717 if (complain & tf_error)
14718 {
14719 if (TREE_CODE (return_type) == ARRAY_TYPE)
14720 error ("function returning an array");
14721 else
14722 error ("function returning a function");
14723 }
14724 return error_mark_node;
14725 }
14726 /* And DR 657. */
14727 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14728 return error_mark_node;
14729
14730 if (!late_return_type_p)
14731 {
14732 /* Substitute the argument types. */
14733 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14734 complain, in_decl);
14735 if (arg_types == error_mark_node)
14736 return error_mark_node;
14737 }
14738
14739 /* Construct a new type node and return it. */
14740 if (TREE_CODE (t) == FUNCTION_TYPE)
14741 {
14742 fntype = build_function_type (return_type, arg_types);
14743 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14744 }
14745 else
14746 {
14747 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14748 /* Don't pick up extra function qualifiers from the basetype. */
14749 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14750 if (! MAYBE_CLASS_TYPE_P (r))
14751 {
14752 /* [temp.deduct]
14753
14754 Type deduction may fail for any of the following
14755 reasons:
14756
14757 -- Attempting to create "pointer to member of T" when T
14758 is not a class type. */
14759 if (complain & tf_error)
14760 error ("creating pointer to member function of non-class type %qT",
14761 r);
14762 return error_mark_node;
14763 }
14764
14765 fntype = build_method_type_directly (r, return_type,
14766 TREE_CHAIN (arg_types));
14767 }
14768 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14769
14770 /* See comment above. */
14771 tree raises = NULL_TREE;
14772 cp_ref_qualifier rqual = type_memfn_rqual (t);
14773 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14774
14775 return fntype;
14776 }
14777
14778 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14779 ARGS into that specification, and return the substituted
14780 specification. If there is no specification, return NULL_TREE. */
14781
14782 static tree
14783 tsubst_exception_specification (tree fntype,
14784 tree args,
14785 tsubst_flags_t complain,
14786 tree in_decl,
14787 bool defer_ok)
14788 {
14789 tree specs;
14790 tree new_specs;
14791
14792 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14793 new_specs = NULL_TREE;
14794 if (specs && TREE_PURPOSE (specs))
14795 {
14796 /* A noexcept-specifier. */
14797 tree expr = TREE_PURPOSE (specs);
14798 if (TREE_CODE (expr) == INTEGER_CST)
14799 new_specs = expr;
14800 else if (defer_ok)
14801 {
14802 /* Defer instantiation of noexcept-specifiers to avoid
14803 excessive instantiations (c++/49107). */
14804 new_specs = make_node (DEFERRED_NOEXCEPT);
14805 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14806 {
14807 /* We already partially instantiated this member template,
14808 so combine the new args with the old. */
14809 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14810 = DEFERRED_NOEXCEPT_PATTERN (expr);
14811 DEFERRED_NOEXCEPT_ARGS (new_specs)
14812 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14813 }
14814 else
14815 {
14816 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14817 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14818 }
14819 }
14820 else
14821 {
14822 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14823 {
14824 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14825 args);
14826 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14827 }
14828 new_specs = tsubst_copy_and_build
14829 (expr, args, complain, in_decl, /*function_p=*/false,
14830 /*integral_constant_expression_p=*/true);
14831 }
14832 new_specs = build_noexcept_spec (new_specs, complain);
14833 }
14834 else if (specs)
14835 {
14836 if (! TREE_VALUE (specs))
14837 new_specs = specs;
14838 else
14839 while (specs)
14840 {
14841 tree spec;
14842 int i, len = 1;
14843 tree expanded_specs = NULL_TREE;
14844
14845 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14846 {
14847 /* Expand the pack expansion type. */
14848 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14849 args, complain,
14850 in_decl);
14851
14852 if (expanded_specs == error_mark_node)
14853 return error_mark_node;
14854 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14855 len = TREE_VEC_LENGTH (expanded_specs);
14856 else
14857 {
14858 /* We're substituting into a member template, so
14859 we got a TYPE_PACK_EXPANSION back. Add that
14860 expansion and move on. */
14861 gcc_assert (TREE_CODE (expanded_specs)
14862 == TYPE_PACK_EXPANSION);
14863 new_specs = add_exception_specifier (new_specs,
14864 expanded_specs,
14865 complain);
14866 specs = TREE_CHAIN (specs);
14867 continue;
14868 }
14869 }
14870
14871 for (i = 0; i < len; ++i)
14872 {
14873 if (expanded_specs)
14874 spec = TREE_VEC_ELT (expanded_specs, i);
14875 else
14876 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14877 if (spec == error_mark_node)
14878 return spec;
14879 new_specs = add_exception_specifier (new_specs, spec,
14880 complain);
14881 }
14882
14883 specs = TREE_CHAIN (specs);
14884 }
14885 }
14886 return new_specs;
14887 }
14888
14889 /* Take the tree structure T and replace template parameters used
14890 therein with the argument vector ARGS. IN_DECL is an associated
14891 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14892 Issue error and warning messages under control of COMPLAIN. Note
14893 that we must be relatively non-tolerant of extensions here, in
14894 order to preserve conformance; if we allow substitutions that
14895 should not be allowed, we may allow argument deductions that should
14896 not succeed, and therefore report ambiguous overload situations
14897 where there are none. In theory, we could allow the substitution,
14898 but indicate that it should have failed, and allow our caller to
14899 make sure that the right thing happens, but we don't try to do this
14900 yet.
14901
14902 This function is used for dealing with types, decls and the like;
14903 for expressions, use tsubst_expr or tsubst_copy. */
14904
14905 tree
14906 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14907 {
14908 enum tree_code code;
14909 tree type, r = NULL_TREE;
14910
14911 if (t == NULL_TREE || t == error_mark_node
14912 || t == integer_type_node
14913 || t == void_type_node
14914 || t == char_type_node
14915 || t == unknown_type_node
14916 || TREE_CODE (t) == NAMESPACE_DECL
14917 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14918 return t;
14919
14920 if (DECL_P (t))
14921 return tsubst_decl (t, args, complain);
14922
14923 if (args == NULL_TREE)
14924 return t;
14925
14926 code = TREE_CODE (t);
14927
14928 if (code == IDENTIFIER_NODE)
14929 type = IDENTIFIER_TYPE_VALUE (t);
14930 else
14931 type = TREE_TYPE (t);
14932
14933 gcc_assert (type != unknown_type_node);
14934
14935 /* Reuse typedefs. We need to do this to handle dependent attributes,
14936 such as attribute aligned. */
14937 if (TYPE_P (t)
14938 && typedef_variant_p (t))
14939 {
14940 tree decl = TYPE_NAME (t);
14941
14942 if (alias_template_specialization_p (t, nt_opaque))
14943 {
14944 /* DECL represents an alias template and we want to
14945 instantiate it. */
14946 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14947 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14948 r = instantiate_alias_template (tmpl, gen_args, complain);
14949 }
14950 else if (DECL_CLASS_SCOPE_P (decl)
14951 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14952 && uses_template_parms (DECL_CONTEXT (decl)))
14953 {
14954 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14955 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14956 r = retrieve_specialization (tmpl, gen_args, 0);
14957 }
14958 else if (DECL_FUNCTION_SCOPE_P (decl)
14959 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14960 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14961 r = retrieve_local_specialization (decl);
14962 else
14963 /* The typedef is from a non-template context. */
14964 return t;
14965
14966 if (r)
14967 {
14968 r = TREE_TYPE (r);
14969 r = cp_build_qualified_type_real
14970 (r, cp_type_quals (t) | cp_type_quals (r),
14971 complain | tf_ignore_bad_quals);
14972 return r;
14973 }
14974 else
14975 {
14976 /* We don't have an instantiation yet, so drop the typedef. */
14977 int quals = cp_type_quals (t);
14978 t = DECL_ORIGINAL_TYPE (decl);
14979 t = cp_build_qualified_type_real (t, quals,
14980 complain | tf_ignore_bad_quals);
14981 }
14982 }
14983
14984 bool fndecl_type = (complain & tf_fndecl_type);
14985 complain &= ~tf_fndecl_type;
14986
14987 if (type
14988 && code != TYPENAME_TYPE
14989 && code != TEMPLATE_TYPE_PARM
14990 && code != TEMPLATE_PARM_INDEX
14991 && code != IDENTIFIER_NODE
14992 && code != FUNCTION_TYPE
14993 && code != METHOD_TYPE)
14994 type = tsubst (type, args, complain, in_decl);
14995 if (type == error_mark_node)
14996 return error_mark_node;
14997
14998 switch (code)
14999 {
15000 case RECORD_TYPE:
15001 case UNION_TYPE:
15002 case ENUMERAL_TYPE:
15003 return tsubst_aggr_type (t, args, complain, in_decl,
15004 /*entering_scope=*/0);
15005
15006 case ERROR_MARK:
15007 case IDENTIFIER_NODE:
15008 case VOID_TYPE:
15009 case REAL_TYPE:
15010 case COMPLEX_TYPE:
15011 case VECTOR_TYPE:
15012 case BOOLEAN_TYPE:
15013 case NULLPTR_TYPE:
15014 case LANG_TYPE:
15015 return t;
15016
15017 case INTEGER_TYPE:
15018 if (t == integer_type_node)
15019 return t;
15020
15021 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15022 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15023 return t;
15024
15025 {
15026 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15027
15028 max = tsubst_expr (omax, args, complain, in_decl,
15029 /*integral_constant_expression_p=*/false);
15030
15031 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15032 needed. */
15033 if (TREE_CODE (max) == NOP_EXPR
15034 && TREE_SIDE_EFFECTS (omax)
15035 && !TREE_TYPE (max))
15036 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15037
15038 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15039 with TREE_SIDE_EFFECTS that indicates this is not an integral
15040 constant expression. */
15041 if (processing_template_decl
15042 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15043 {
15044 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15045 TREE_SIDE_EFFECTS (max) = 1;
15046 }
15047
15048 return compute_array_index_type (NULL_TREE, max, complain);
15049 }
15050
15051 case TEMPLATE_TYPE_PARM:
15052 case TEMPLATE_TEMPLATE_PARM:
15053 case BOUND_TEMPLATE_TEMPLATE_PARM:
15054 case TEMPLATE_PARM_INDEX:
15055 {
15056 int idx;
15057 int level;
15058 int levels;
15059 tree arg = NULL_TREE;
15060
15061 /* Early in template argument deduction substitution, we don't
15062 want to reduce the level of 'auto', or it will be confused
15063 with a normal template parm in subsequent deduction. */
15064 if (is_auto (t) && (complain & tf_partial))
15065 return t;
15066
15067 r = NULL_TREE;
15068
15069 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15070 template_parm_level_and_index (t, &level, &idx);
15071
15072 levels = TMPL_ARGS_DEPTH (args);
15073 if (level <= levels
15074 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15075 {
15076 arg = TMPL_ARG (args, level, idx);
15077
15078 /* See through ARGUMENT_PACK_SELECT arguments. */
15079 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15080 arg = argument_pack_select_arg (arg);
15081 }
15082
15083 if (arg == error_mark_node)
15084 return error_mark_node;
15085 else if (arg != NULL_TREE)
15086 {
15087 if (ARGUMENT_PACK_P (arg))
15088 /* If ARG is an argument pack, we don't actually want to
15089 perform a substitution here, because substitutions
15090 for argument packs are only done
15091 element-by-element. We can get to this point when
15092 substituting the type of a non-type template
15093 parameter pack, when that type actually contains
15094 template parameter packs from an outer template, e.g.,
15095
15096 template<typename... Types> struct A {
15097 template<Types... Values> struct B { };
15098 }; */
15099 return t;
15100
15101 if (code == TEMPLATE_TYPE_PARM)
15102 {
15103 int quals;
15104
15105 /* When building concept checks for the purpose of
15106 deducing placeholders, we can end up with wildcards
15107 where types are expected. Adjust this to the deduced
15108 value. */
15109 if (TREE_CODE (arg) == WILDCARD_DECL)
15110 arg = TREE_TYPE (TREE_TYPE (arg));
15111
15112 gcc_assert (TYPE_P (arg));
15113
15114 quals = cp_type_quals (arg) | cp_type_quals (t);
15115
15116 return cp_build_qualified_type_real
15117 (arg, quals, complain | tf_ignore_bad_quals);
15118 }
15119 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15120 {
15121 /* We are processing a type constructed from a
15122 template template parameter. */
15123 tree argvec = tsubst (TYPE_TI_ARGS (t),
15124 args, complain, in_decl);
15125 if (argvec == error_mark_node)
15126 return error_mark_node;
15127
15128 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15129 || TREE_CODE (arg) == TEMPLATE_DECL
15130 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15131
15132 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15133 /* Consider this code:
15134
15135 template <template <class> class Template>
15136 struct Internal {
15137 template <class Arg> using Bind = Template<Arg>;
15138 };
15139
15140 template <template <class> class Template, class Arg>
15141 using Instantiate = Template<Arg>; //#0
15142
15143 template <template <class> class Template,
15144 class Argument>
15145 using Bind =
15146 Instantiate<Internal<Template>::template Bind,
15147 Argument>; //#1
15148
15149 When #1 is parsed, the
15150 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15151 parameter `Template' in #0 matches the
15152 UNBOUND_CLASS_TEMPLATE representing the argument
15153 `Internal<Template>::template Bind'; We then want
15154 to assemble the type `Bind<Argument>' that can't
15155 be fully created right now, because
15156 `Internal<Template>' not being complete, the Bind
15157 template cannot be looked up in that context. So
15158 we need to "store" `Bind<Argument>' for later
15159 when the context of Bind becomes complete. Let's
15160 store that in a TYPENAME_TYPE. */
15161 return make_typename_type (TYPE_CONTEXT (arg),
15162 build_nt (TEMPLATE_ID_EXPR,
15163 TYPE_IDENTIFIER (arg),
15164 argvec),
15165 typename_type,
15166 complain);
15167
15168 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15169 are resolving nested-types in the signature of a
15170 member function templates. Otherwise ARG is a
15171 TEMPLATE_DECL and is the real template to be
15172 instantiated. */
15173 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15174 arg = TYPE_NAME (arg);
15175
15176 r = lookup_template_class (arg,
15177 argvec, in_decl,
15178 DECL_CONTEXT (arg),
15179 /*entering_scope=*/0,
15180 complain);
15181 return cp_build_qualified_type_real
15182 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15183 }
15184 else if (code == TEMPLATE_TEMPLATE_PARM)
15185 return arg;
15186 else
15187 /* TEMPLATE_PARM_INDEX. */
15188 return convert_from_reference (unshare_expr (arg));
15189 }
15190
15191 if (level == 1)
15192 /* This can happen during the attempted tsubst'ing in
15193 unify. This means that we don't yet have any information
15194 about the template parameter in question. */
15195 return t;
15196
15197 /* If we get here, we must have been looking at a parm for a
15198 more deeply nested template. Make a new version of this
15199 template parameter, but with a lower level. */
15200 switch (code)
15201 {
15202 case TEMPLATE_TYPE_PARM:
15203 case TEMPLATE_TEMPLATE_PARM:
15204 case BOUND_TEMPLATE_TEMPLATE_PARM:
15205 if (cp_type_quals (t))
15206 {
15207 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15208 r = cp_build_qualified_type_real
15209 (r, cp_type_quals (t),
15210 complain | (code == TEMPLATE_TYPE_PARM
15211 ? tf_ignore_bad_quals : 0));
15212 }
15213 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15214 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15215 && (r = (TEMPLATE_PARM_DESCENDANTS
15216 (TEMPLATE_TYPE_PARM_INDEX (t))))
15217 && (r = TREE_TYPE (r))
15218 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15219 /* Break infinite recursion when substituting the constraints
15220 of a constrained placeholder. */;
15221 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15222 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15223 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15224 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15225 r = TEMPLATE_PARM_DESCENDANTS (arg))
15226 && (TEMPLATE_PARM_LEVEL (r)
15227 == TEMPLATE_PARM_LEVEL (arg) - levels))
15228 /* Cache the simple case of lowering a type parameter. */
15229 r = TREE_TYPE (r);
15230 else
15231 {
15232 r = copy_type (t);
15233 TEMPLATE_TYPE_PARM_INDEX (r)
15234 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15235 r, levels, args, complain);
15236 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15237 TYPE_MAIN_VARIANT (r) = r;
15238 TYPE_POINTER_TO (r) = NULL_TREE;
15239 TYPE_REFERENCE_TO (r) = NULL_TREE;
15240
15241 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15242 {
15243 /* Propagate constraints on placeholders since they are
15244 only instantiated during satisfaction. */
15245 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15246 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15247 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15248 {
15249 pl = tsubst_copy (pl, args, complain, in_decl);
15250 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15251 }
15252 }
15253
15254 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15255 /* We have reduced the level of the template
15256 template parameter, but not the levels of its
15257 template parameters, so canonical_type_parameter
15258 will not be able to find the canonical template
15259 template parameter for this level. Thus, we
15260 require structural equality checking to compare
15261 TEMPLATE_TEMPLATE_PARMs. */
15262 SET_TYPE_STRUCTURAL_EQUALITY (r);
15263 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15264 SET_TYPE_STRUCTURAL_EQUALITY (r);
15265 else
15266 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15267
15268 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15269 {
15270 tree tinfo = TYPE_TEMPLATE_INFO (t);
15271 /* We might need to substitute into the types of non-type
15272 template parameters. */
15273 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15274 complain, in_decl);
15275 if (tmpl == error_mark_node)
15276 return error_mark_node;
15277 tree argvec = tsubst (TI_ARGS (tinfo), args,
15278 complain, in_decl);
15279 if (argvec == error_mark_node)
15280 return error_mark_node;
15281
15282 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15283 = build_template_info (tmpl, argvec);
15284 }
15285 }
15286 break;
15287
15288 case TEMPLATE_PARM_INDEX:
15289 /* OK, now substitute the type of the non-type parameter. We
15290 couldn't do it earlier because it might be an auto parameter,
15291 and we wouldn't need to if we had an argument. */
15292 type = tsubst (type, args, complain, in_decl);
15293 if (type == error_mark_node)
15294 return error_mark_node;
15295 r = reduce_template_parm_level (t, type, levels, args, complain);
15296 break;
15297
15298 default:
15299 gcc_unreachable ();
15300 }
15301
15302 return r;
15303 }
15304
15305 case TREE_LIST:
15306 {
15307 tree purpose, value, chain;
15308
15309 if (t == void_list_node)
15310 return t;
15311
15312 purpose = TREE_PURPOSE (t);
15313 if (purpose)
15314 {
15315 purpose = tsubst (purpose, args, complain, in_decl);
15316 if (purpose == error_mark_node)
15317 return error_mark_node;
15318 }
15319 value = TREE_VALUE (t);
15320 if (value)
15321 {
15322 value = tsubst (value, args, complain, in_decl);
15323 if (value == error_mark_node)
15324 return error_mark_node;
15325 }
15326 chain = TREE_CHAIN (t);
15327 if (chain && chain != void_type_node)
15328 {
15329 chain = tsubst (chain, args, complain, in_decl);
15330 if (chain == error_mark_node)
15331 return error_mark_node;
15332 }
15333 if (purpose == TREE_PURPOSE (t)
15334 && value == TREE_VALUE (t)
15335 && chain == TREE_CHAIN (t))
15336 return t;
15337 return hash_tree_cons (purpose, value, chain);
15338 }
15339
15340 case TREE_BINFO:
15341 /* We should never be tsubsting a binfo. */
15342 gcc_unreachable ();
15343
15344 case TREE_VEC:
15345 /* A vector of template arguments. */
15346 gcc_assert (!type);
15347 return tsubst_template_args (t, args, complain, in_decl);
15348
15349 case POINTER_TYPE:
15350 case REFERENCE_TYPE:
15351 {
15352 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15353 return t;
15354
15355 /* [temp.deduct]
15356
15357 Type deduction may fail for any of the following
15358 reasons:
15359
15360 -- Attempting to create a pointer to reference type.
15361 -- Attempting to create a reference to a reference type or
15362 a reference to void.
15363
15364 Core issue 106 says that creating a reference to a reference
15365 during instantiation is no longer a cause for failure. We
15366 only enforce this check in strict C++98 mode. */
15367 if ((TYPE_REF_P (type)
15368 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15369 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15370 {
15371 static location_t last_loc;
15372
15373 /* We keep track of the last time we issued this error
15374 message to avoid spewing a ton of messages during a
15375 single bad template instantiation. */
15376 if (complain & tf_error
15377 && last_loc != input_location)
15378 {
15379 if (VOID_TYPE_P (type))
15380 error ("forming reference to void");
15381 else if (code == POINTER_TYPE)
15382 error ("forming pointer to reference type %qT", type);
15383 else
15384 error ("forming reference to reference type %qT", type);
15385 last_loc = input_location;
15386 }
15387
15388 return error_mark_node;
15389 }
15390 else if (TREE_CODE (type) == FUNCTION_TYPE
15391 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15392 || type_memfn_rqual (type) != REF_QUAL_NONE))
15393 {
15394 if (complain & tf_error)
15395 {
15396 if (code == POINTER_TYPE)
15397 error ("forming pointer to qualified function type %qT",
15398 type);
15399 else
15400 error ("forming reference to qualified function type %qT",
15401 type);
15402 }
15403 return error_mark_node;
15404 }
15405 else if (code == POINTER_TYPE)
15406 {
15407 r = build_pointer_type (type);
15408 if (TREE_CODE (type) == METHOD_TYPE)
15409 r = build_ptrmemfunc_type (r);
15410 }
15411 else if (TYPE_REF_P (type))
15412 /* In C++0x, during template argument substitution, when there is an
15413 attempt to create a reference to a reference type, reference
15414 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15415
15416 "If a template-argument for a template-parameter T names a type
15417 that is a reference to a type A, an attempt to create the type
15418 'lvalue reference to cv T' creates the type 'lvalue reference to
15419 A,' while an attempt to create the type type rvalue reference to
15420 cv T' creates the type T"
15421 */
15422 r = cp_build_reference_type
15423 (TREE_TYPE (type),
15424 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15425 else
15426 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15427 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15428
15429 if (r != error_mark_node)
15430 /* Will this ever be needed for TYPE_..._TO values? */
15431 layout_type (r);
15432
15433 return r;
15434 }
15435 case OFFSET_TYPE:
15436 {
15437 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15438 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15439 {
15440 /* [temp.deduct]
15441
15442 Type deduction may fail for any of the following
15443 reasons:
15444
15445 -- Attempting to create "pointer to member of T" when T
15446 is not a class type. */
15447 if (complain & tf_error)
15448 error ("creating pointer to member of non-class type %qT", r);
15449 return error_mark_node;
15450 }
15451 if (TYPE_REF_P (type))
15452 {
15453 if (complain & tf_error)
15454 error ("creating pointer to member reference type %qT", type);
15455 return error_mark_node;
15456 }
15457 if (VOID_TYPE_P (type))
15458 {
15459 if (complain & tf_error)
15460 error ("creating pointer to member of type void");
15461 return error_mark_node;
15462 }
15463 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15464 if (TREE_CODE (type) == FUNCTION_TYPE)
15465 {
15466 /* The type of the implicit object parameter gets its
15467 cv-qualifiers from the FUNCTION_TYPE. */
15468 tree memptr;
15469 tree method_type
15470 = build_memfn_type (type, r, type_memfn_quals (type),
15471 type_memfn_rqual (type));
15472 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15473 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15474 complain);
15475 }
15476 else
15477 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15478 cp_type_quals (t),
15479 complain);
15480 }
15481 case FUNCTION_TYPE:
15482 case METHOD_TYPE:
15483 {
15484 tree fntype;
15485 tree specs;
15486 fntype = tsubst_function_type (t, args, complain, in_decl);
15487 if (fntype == error_mark_node)
15488 return error_mark_node;
15489
15490 /* Substitute the exception specification. */
15491 specs = tsubst_exception_specification (t, args, complain, in_decl,
15492 /*defer_ok*/fndecl_type);
15493 if (specs == error_mark_node)
15494 return error_mark_node;
15495 if (specs)
15496 fntype = build_exception_variant (fntype, specs);
15497 return fntype;
15498 }
15499 case ARRAY_TYPE:
15500 {
15501 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15502 if (domain == error_mark_node)
15503 return error_mark_node;
15504
15505 /* As an optimization, we avoid regenerating the array type if
15506 it will obviously be the same as T. */
15507 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15508 return t;
15509
15510 /* These checks should match the ones in create_array_type_for_decl.
15511
15512 [temp.deduct]
15513
15514 The deduction may fail for any of the following reasons:
15515
15516 -- Attempting to create an array with an element type that
15517 is void, a function type, or a reference type, or [DR337]
15518 an abstract class type. */
15519 if (VOID_TYPE_P (type)
15520 || TREE_CODE (type) == FUNCTION_TYPE
15521 || (TREE_CODE (type) == ARRAY_TYPE
15522 && TYPE_DOMAIN (type) == NULL_TREE)
15523 || TYPE_REF_P (type))
15524 {
15525 if (complain & tf_error)
15526 error ("creating array of %qT", type);
15527 return error_mark_node;
15528 }
15529
15530 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15531 return error_mark_node;
15532
15533 r = build_cplus_array_type (type, domain);
15534
15535 if (!valid_array_size_p (input_location, r, in_decl,
15536 (complain & tf_error)))
15537 return error_mark_node;
15538
15539 if (TYPE_USER_ALIGN (t))
15540 {
15541 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15542 TYPE_USER_ALIGN (r) = 1;
15543 }
15544
15545 return r;
15546 }
15547
15548 case TYPENAME_TYPE:
15549 {
15550 tree ctx = TYPE_CONTEXT (t);
15551 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15552 {
15553 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15554 if (ctx == error_mark_node
15555 || TREE_VEC_LENGTH (ctx) > 1)
15556 return error_mark_node;
15557 if (TREE_VEC_LENGTH (ctx) == 0)
15558 {
15559 if (complain & tf_error)
15560 error ("%qD is instantiated for an empty pack",
15561 TYPENAME_TYPE_FULLNAME (t));
15562 return error_mark_node;
15563 }
15564 ctx = TREE_VEC_ELT (ctx, 0);
15565 }
15566 else
15567 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15568 /*entering_scope=*/1);
15569 if (ctx == error_mark_node)
15570 return error_mark_node;
15571
15572 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15573 complain, in_decl);
15574 if (f == error_mark_node)
15575 return error_mark_node;
15576
15577 if (!MAYBE_CLASS_TYPE_P (ctx))
15578 {
15579 if (complain & tf_error)
15580 error ("%qT is not a class, struct, or union type", ctx);
15581 return error_mark_node;
15582 }
15583 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15584 {
15585 /* Normally, make_typename_type does not require that the CTX
15586 have complete type in order to allow things like:
15587
15588 template <class T> struct S { typename S<T>::X Y; };
15589
15590 But, such constructs have already been resolved by this
15591 point, so here CTX really should have complete type, unless
15592 it's a partial instantiation. */
15593 ctx = complete_type (ctx);
15594 if (!COMPLETE_TYPE_P (ctx))
15595 {
15596 if (complain & tf_error)
15597 cxx_incomplete_type_error (NULL_TREE, ctx);
15598 return error_mark_node;
15599 }
15600 }
15601
15602 f = make_typename_type (ctx, f, typename_type,
15603 complain | tf_keep_type_decl);
15604 if (f == error_mark_node)
15605 return f;
15606 if (TREE_CODE (f) == TYPE_DECL)
15607 {
15608 complain |= tf_ignore_bad_quals;
15609 f = TREE_TYPE (f);
15610 }
15611
15612 if (TREE_CODE (f) != TYPENAME_TYPE)
15613 {
15614 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15615 {
15616 if (complain & tf_error)
15617 error ("%qT resolves to %qT, which is not an enumeration type",
15618 t, f);
15619 else
15620 return error_mark_node;
15621 }
15622 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15623 {
15624 if (complain & tf_error)
15625 error ("%qT resolves to %qT, which is is not a class type",
15626 t, f);
15627 else
15628 return error_mark_node;
15629 }
15630 }
15631
15632 return cp_build_qualified_type_real
15633 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15634 }
15635
15636 case UNBOUND_CLASS_TEMPLATE:
15637 {
15638 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15639 in_decl, /*entering_scope=*/1);
15640 tree name = TYPE_IDENTIFIER (t);
15641 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15642
15643 if (ctx == error_mark_node || name == error_mark_node)
15644 return error_mark_node;
15645
15646 if (parm_list)
15647 parm_list = tsubst_template_parms (parm_list, args, complain);
15648 return make_unbound_class_template (ctx, name, parm_list, complain);
15649 }
15650
15651 case TYPEOF_TYPE:
15652 {
15653 tree type;
15654
15655 ++cp_unevaluated_operand;
15656 ++c_inhibit_evaluation_warnings;
15657
15658 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15659 complain, in_decl,
15660 /*integral_constant_expression_p=*/false);
15661
15662 --cp_unevaluated_operand;
15663 --c_inhibit_evaluation_warnings;
15664
15665 type = finish_typeof (type);
15666 return cp_build_qualified_type_real (type,
15667 cp_type_quals (t)
15668 | cp_type_quals (type),
15669 complain);
15670 }
15671
15672 case DECLTYPE_TYPE:
15673 {
15674 tree type;
15675
15676 ++cp_unevaluated_operand;
15677 ++c_inhibit_evaluation_warnings;
15678
15679 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15680 complain|tf_decltype, in_decl,
15681 /*function_p*/false,
15682 /*integral_constant_expression*/false);
15683
15684 --cp_unevaluated_operand;
15685 --c_inhibit_evaluation_warnings;
15686
15687 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15688 type = lambda_capture_field_type (type,
15689 false /*explicit_init*/,
15690 DECLTYPE_FOR_REF_CAPTURE (t));
15691 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15692 type = lambda_proxy_type (type);
15693 else
15694 {
15695 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15696 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15697 && EXPR_P (type))
15698 /* In a template ~id could be either a complement expression
15699 or an unqualified-id naming a destructor; if instantiating
15700 it produces an expression, it's not an id-expression or
15701 member access. */
15702 id = false;
15703 type = finish_decltype_type (type, id, complain);
15704 }
15705 return cp_build_qualified_type_real (type,
15706 cp_type_quals (t)
15707 | cp_type_quals (type),
15708 complain | tf_ignore_bad_quals);
15709 }
15710
15711 case UNDERLYING_TYPE:
15712 {
15713 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15714 complain, in_decl);
15715 return finish_underlying_type (type);
15716 }
15717
15718 case TYPE_ARGUMENT_PACK:
15719 case NONTYPE_ARGUMENT_PACK:
15720 {
15721 tree r;
15722
15723 if (code == NONTYPE_ARGUMENT_PACK)
15724 r = make_node (code);
15725 else
15726 r = cxx_make_type (code);
15727
15728 tree pack_args = ARGUMENT_PACK_ARGS (t);
15729 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15730 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15731
15732 return r;
15733 }
15734
15735 case VOID_CST:
15736 case INTEGER_CST:
15737 case REAL_CST:
15738 case STRING_CST:
15739 case PLUS_EXPR:
15740 case MINUS_EXPR:
15741 case NEGATE_EXPR:
15742 case NOP_EXPR:
15743 case INDIRECT_REF:
15744 case ADDR_EXPR:
15745 case CALL_EXPR:
15746 case ARRAY_REF:
15747 case SCOPE_REF:
15748 /* We should use one of the expression tsubsts for these codes. */
15749 gcc_unreachable ();
15750
15751 default:
15752 sorry ("use of %qs in template", get_tree_code_name (code));
15753 return error_mark_node;
15754 }
15755 }
15756
15757 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15758 expression on the left-hand side of the "." or "->" operator. We
15759 only do the lookup if we had a dependent BASELINK. Otherwise we
15760 adjust it onto the instantiated heirarchy. */
15761
15762 static tree
15763 tsubst_baselink (tree baselink, tree object_type,
15764 tree args, tsubst_flags_t complain, tree in_decl)
15765 {
15766 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15767 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15768 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15769
15770 tree optype = BASELINK_OPTYPE (baselink);
15771 optype = tsubst (optype, args, complain, in_decl);
15772
15773 tree template_args = NULL_TREE;
15774 bool template_id_p = false;
15775 tree fns = BASELINK_FUNCTIONS (baselink);
15776 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15777 {
15778 template_id_p = true;
15779 template_args = TREE_OPERAND (fns, 1);
15780 fns = TREE_OPERAND (fns, 0);
15781 if (template_args)
15782 template_args = tsubst_template_args (template_args, args,
15783 complain, in_decl);
15784 }
15785
15786 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15787 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15788 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15789
15790 if (dependent_p)
15791 {
15792 tree name = OVL_NAME (fns);
15793 if (IDENTIFIER_CONV_OP_P (name))
15794 name = make_conv_op_name (optype);
15795
15796 if (name == complete_dtor_identifier)
15797 /* Treat as-if non-dependent below. */
15798 dependent_p = false;
15799
15800 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15801 if (!baselink)
15802 {
15803 if ((complain & tf_error)
15804 && constructor_name_p (name, qualifying_scope))
15805 error ("cannot call constructor %<%T::%D%> directly",
15806 qualifying_scope, name);
15807 return error_mark_node;
15808 }
15809
15810 if (BASELINK_P (baselink))
15811 fns = BASELINK_FUNCTIONS (baselink);
15812 }
15813 else
15814 /* We're going to overwrite pieces below, make a duplicate. */
15815 baselink = copy_node (baselink);
15816
15817 /* If lookup found a single function, mark it as used at this point.
15818 (If lookup found multiple functions the one selected later by
15819 overload resolution will be marked as used at that point.) */
15820 if (!template_id_p && !really_overloaded_fn (fns))
15821 {
15822 tree fn = OVL_FIRST (fns);
15823 bool ok = mark_used (fn, complain);
15824 if (!ok && !(complain & tf_error))
15825 return error_mark_node;
15826 if (ok && BASELINK_P (baselink))
15827 /* We might have instantiated an auto function. */
15828 TREE_TYPE (baselink) = TREE_TYPE (fn);
15829 }
15830
15831 if (BASELINK_P (baselink))
15832 {
15833 /* Add back the template arguments, if present. */
15834 if (template_id_p)
15835 BASELINK_FUNCTIONS (baselink)
15836 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15837
15838 /* Update the conversion operator type. */
15839 BASELINK_OPTYPE (baselink) = optype;
15840 }
15841
15842 if (!object_type)
15843 object_type = current_class_type;
15844
15845 if (qualified_p || !dependent_p)
15846 {
15847 baselink = adjust_result_of_qualified_name_lookup (baselink,
15848 qualifying_scope,
15849 object_type);
15850 if (!qualified_p)
15851 /* We need to call adjust_result_of_qualified_name_lookup in case the
15852 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15853 so that we still get virtual function binding. */
15854 BASELINK_QUALIFIED_P (baselink) = false;
15855 }
15856
15857 return baselink;
15858 }
15859
15860 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15861 true if the qualified-id will be a postfix-expression in-and-of
15862 itself; false if more of the postfix-expression follows the
15863 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15864 of "&". */
15865
15866 static tree
15867 tsubst_qualified_id (tree qualified_id, tree args,
15868 tsubst_flags_t complain, tree in_decl,
15869 bool done, bool address_p)
15870 {
15871 tree expr;
15872 tree scope;
15873 tree name;
15874 bool is_template;
15875 tree template_args;
15876 location_t loc = UNKNOWN_LOCATION;
15877
15878 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15879
15880 /* Figure out what name to look up. */
15881 name = TREE_OPERAND (qualified_id, 1);
15882 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15883 {
15884 is_template = true;
15885 loc = EXPR_LOCATION (name);
15886 template_args = TREE_OPERAND (name, 1);
15887 if (template_args)
15888 template_args = tsubst_template_args (template_args, args,
15889 complain, in_decl);
15890 if (template_args == error_mark_node)
15891 return error_mark_node;
15892 name = TREE_OPERAND (name, 0);
15893 }
15894 else
15895 {
15896 is_template = false;
15897 template_args = NULL_TREE;
15898 }
15899
15900 /* Substitute into the qualifying scope. When there are no ARGS, we
15901 are just trying to simplify a non-dependent expression. In that
15902 case the qualifying scope may be dependent, and, in any case,
15903 substituting will not help. */
15904 scope = TREE_OPERAND (qualified_id, 0);
15905 if (args)
15906 {
15907 scope = tsubst (scope, args, complain, in_decl);
15908 expr = tsubst_copy (name, args, complain, in_decl);
15909 }
15910 else
15911 expr = name;
15912
15913 if (dependent_scope_p (scope))
15914 {
15915 if (is_template)
15916 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15917 tree r = build_qualified_name (NULL_TREE, scope, expr,
15918 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15919 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15920 return r;
15921 }
15922
15923 if (!BASELINK_P (name) && !DECL_P (expr))
15924 {
15925 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15926 {
15927 /* A BIT_NOT_EXPR is used to represent a destructor. */
15928 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15929 {
15930 error ("qualifying type %qT does not match destructor name ~%qT",
15931 scope, TREE_OPERAND (expr, 0));
15932 expr = error_mark_node;
15933 }
15934 else
15935 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15936 /*is_type_p=*/0, false);
15937 }
15938 else
15939 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15940 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15941 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15942 {
15943 if (complain & tf_error)
15944 {
15945 error ("dependent-name %qE is parsed as a non-type, but "
15946 "instantiation yields a type", qualified_id);
15947 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15948 }
15949 return error_mark_node;
15950 }
15951 }
15952
15953 if (DECL_P (expr))
15954 {
15955 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15956 scope);
15957 /* Remember that there was a reference to this entity. */
15958 if (!mark_used (expr, complain) && !(complain & tf_error))
15959 return error_mark_node;
15960 }
15961
15962 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15963 {
15964 if (complain & tf_error)
15965 qualified_name_lookup_error (scope,
15966 TREE_OPERAND (qualified_id, 1),
15967 expr, input_location);
15968 return error_mark_node;
15969 }
15970
15971 if (is_template)
15972 {
15973 /* We may be repeating a check already done during parsing, but
15974 if it was well-formed and passed then, it will pass again
15975 now, and if it didn't, we wouldn't have got here. The case
15976 we want to catch is when we couldn't tell then, and can now,
15977 namely when templ prior to substitution was an
15978 identifier. */
15979 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15980 return error_mark_node;
15981
15982 if (variable_template_p (expr))
15983 expr = lookup_and_finish_template_variable (expr, template_args,
15984 complain);
15985 else
15986 expr = lookup_template_function (expr, template_args);
15987 }
15988
15989 if (expr == error_mark_node && complain & tf_error)
15990 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15991 expr, input_location);
15992 else if (TYPE_P (scope))
15993 {
15994 expr = (adjust_result_of_qualified_name_lookup
15995 (expr, scope, current_nonlambda_class_type ()));
15996 expr = (finish_qualified_id_expr
15997 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15998 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15999 /*template_arg_p=*/false, complain));
16000 }
16001
16002 /* Expressions do not generally have reference type. */
16003 if (TREE_CODE (expr) != SCOPE_REF
16004 /* However, if we're about to form a pointer-to-member, we just
16005 want the referenced member referenced. */
16006 && TREE_CODE (expr) != OFFSET_REF)
16007 expr = convert_from_reference (expr);
16008
16009 if (REF_PARENTHESIZED_P (qualified_id))
16010 expr = force_paren_expr (expr);
16011
16012 return expr;
16013 }
16014
16015 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16016 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16017 for tsubst. */
16018
16019 static tree
16020 tsubst_init (tree init, tree decl, tree args,
16021 tsubst_flags_t complain, tree in_decl)
16022 {
16023 if (!init)
16024 return NULL_TREE;
16025
16026 init = tsubst_expr (init, args, complain, in_decl, false);
16027
16028 tree type = TREE_TYPE (decl);
16029
16030 if (!init && type != error_mark_node)
16031 {
16032 if (tree auto_node = type_uses_auto (type))
16033 {
16034 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16035 {
16036 if (complain & tf_error)
16037 error ("initializer for %q#D expands to an empty list "
16038 "of expressions", decl);
16039 return error_mark_node;
16040 }
16041 }
16042 else if (!dependent_type_p (type))
16043 {
16044 /* If we had an initializer but it
16045 instantiated to nothing,
16046 value-initialize the object. This will
16047 only occur when the initializer was a
16048 pack expansion where the parameter packs
16049 used in that expansion were of length
16050 zero. */
16051 init = build_value_init (type, complain);
16052 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16053 init = get_target_expr_sfinae (init, complain);
16054 if (TREE_CODE (init) == TARGET_EXPR)
16055 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16056 }
16057 }
16058
16059 return init;
16060 }
16061
16062 /* Like tsubst, but deals with expressions. This function just replaces
16063 template parms; to finish processing the resultant expression, use
16064 tsubst_copy_and_build or tsubst_expr. */
16065
16066 static tree
16067 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16068 {
16069 enum tree_code code;
16070 tree r;
16071
16072 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16073 return t;
16074
16075 code = TREE_CODE (t);
16076
16077 switch (code)
16078 {
16079 case PARM_DECL:
16080 r = retrieve_local_specialization (t);
16081
16082 if (r == NULL_TREE)
16083 {
16084 /* We get here for a use of 'this' in an NSDMI. */
16085 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16086 return current_class_ptr;
16087
16088 /* This can happen for a parameter name used later in a function
16089 declaration (such as in a late-specified return type). Just
16090 make a dummy decl, since it's only used for its type. */
16091 gcc_assert (cp_unevaluated_operand != 0);
16092 r = tsubst_decl (t, args, complain);
16093 /* Give it the template pattern as its context; its true context
16094 hasn't been instantiated yet and this is good enough for
16095 mangling. */
16096 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16097 }
16098
16099 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16100 r = argument_pack_select_arg (r);
16101 if (!mark_used (r, complain) && !(complain & tf_error))
16102 return error_mark_node;
16103 return r;
16104
16105 case CONST_DECL:
16106 {
16107 tree enum_type;
16108 tree v;
16109
16110 if (DECL_TEMPLATE_PARM_P (t))
16111 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16112 /* There is no need to substitute into namespace-scope
16113 enumerators. */
16114 if (DECL_NAMESPACE_SCOPE_P (t))
16115 return t;
16116 /* If ARGS is NULL, then T is known to be non-dependent. */
16117 if (args == NULL_TREE)
16118 return scalar_constant_value (t);
16119
16120 /* Unfortunately, we cannot just call lookup_name here.
16121 Consider:
16122
16123 template <int I> int f() {
16124 enum E { a = I };
16125 struct S { void g() { E e = a; } };
16126 };
16127
16128 When we instantiate f<7>::S::g(), say, lookup_name is not
16129 clever enough to find f<7>::a. */
16130 enum_type
16131 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16132 /*entering_scope=*/0);
16133
16134 for (v = TYPE_VALUES (enum_type);
16135 v != NULL_TREE;
16136 v = TREE_CHAIN (v))
16137 if (TREE_PURPOSE (v) == DECL_NAME (t))
16138 return TREE_VALUE (v);
16139
16140 /* We didn't find the name. That should never happen; if
16141 name-lookup found it during preliminary parsing, we
16142 should find it again here during instantiation. */
16143 gcc_unreachable ();
16144 }
16145 return t;
16146
16147 case FIELD_DECL:
16148 if (DECL_CONTEXT (t))
16149 {
16150 tree ctx;
16151
16152 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16153 /*entering_scope=*/1);
16154 if (ctx != DECL_CONTEXT (t))
16155 {
16156 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16157 if (!r)
16158 {
16159 if (complain & tf_error)
16160 error ("using invalid field %qD", t);
16161 return error_mark_node;
16162 }
16163 return r;
16164 }
16165 }
16166
16167 return t;
16168
16169 case VAR_DECL:
16170 case FUNCTION_DECL:
16171 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16172 r = tsubst (t, args, complain, in_decl);
16173 else if (local_variable_p (t)
16174 && uses_template_parms (DECL_CONTEXT (t)))
16175 {
16176 r = retrieve_local_specialization (t);
16177 if (r == NULL_TREE)
16178 {
16179 /* First try name lookup to find the instantiation. */
16180 r = lookup_name (DECL_NAME (t));
16181 if (r)
16182 {
16183 if (!VAR_P (r))
16184 {
16185 /* During error-recovery we may find a non-variable,
16186 even an OVERLOAD: just bail out and avoid ICEs and
16187 duplicate diagnostics (c++/62207). */
16188 gcc_assert (seen_error ());
16189 return error_mark_node;
16190 }
16191 if (!is_capture_proxy (r))
16192 {
16193 /* Make sure the one we found is the one we want. */
16194 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16195 if (ctx != DECL_CONTEXT (r))
16196 r = NULL_TREE;
16197 }
16198 }
16199
16200 if (r)
16201 /* OK */;
16202 else
16203 {
16204 /* This can happen for a variable used in a
16205 late-specified return type of a local lambda, or for a
16206 local static or constant. Building a new VAR_DECL
16207 should be OK in all those cases. */
16208 r = tsubst_decl (t, args, complain);
16209 if (local_specializations)
16210 /* Avoid infinite recursion (79640). */
16211 register_local_specialization (r, t);
16212 if (decl_maybe_constant_var_p (r))
16213 {
16214 /* We can't call cp_finish_decl, so handle the
16215 initializer by hand. */
16216 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16217 complain, in_decl);
16218 if (!processing_template_decl)
16219 init = maybe_constant_init (init);
16220 if (processing_template_decl
16221 ? potential_constant_expression (init)
16222 : reduced_constant_expression_p (init))
16223 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16224 = TREE_CONSTANT (r) = true;
16225 DECL_INITIAL (r) = init;
16226 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16227 TREE_TYPE (r)
16228 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16229 complain, adc_variable_type);
16230 }
16231 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16232 || decl_constant_var_p (r)
16233 || seen_error ());
16234 if (!processing_template_decl
16235 && !TREE_STATIC (r))
16236 r = process_outer_var_ref (r, complain);
16237 }
16238 /* Remember this for subsequent uses. */
16239 if (local_specializations)
16240 register_local_specialization (r, t);
16241 }
16242 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16243 r = argument_pack_select_arg (r);
16244 }
16245 else
16246 r = t;
16247 if (!mark_used (r, complain))
16248 return error_mark_node;
16249 return r;
16250
16251 case NAMESPACE_DECL:
16252 return t;
16253
16254 case OVERLOAD:
16255 return t;
16256
16257 case BASELINK:
16258 return tsubst_baselink (t, current_nonlambda_class_type (),
16259 args, complain, in_decl);
16260
16261 case TEMPLATE_DECL:
16262 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16263 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16264 args, complain, in_decl);
16265 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16266 return tsubst (t, args, complain, in_decl);
16267 else if (DECL_CLASS_SCOPE_P (t)
16268 && uses_template_parms (DECL_CONTEXT (t)))
16269 {
16270 /* Template template argument like the following example need
16271 special treatment:
16272
16273 template <template <class> class TT> struct C {};
16274 template <class T> struct D {
16275 template <class U> struct E {};
16276 C<E> c; // #1
16277 };
16278 D<int> d; // #2
16279
16280 We are processing the template argument `E' in #1 for
16281 the template instantiation #2. Originally, `E' is a
16282 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16283 have to substitute this with one having context `D<int>'. */
16284
16285 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16286 if (dependent_scope_p (context))
16287 {
16288 /* When rewriting a constructor into a deduction guide, a
16289 non-dependent name can become dependent, so memtmpl<args>
16290 becomes context::template memtmpl<args>. */
16291 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16292 return build_qualified_name (type, context, DECL_NAME (t),
16293 /*template*/true);
16294 }
16295 return lookup_field (context, DECL_NAME(t), 0, false);
16296 }
16297 else
16298 /* Ordinary template template argument. */
16299 return t;
16300
16301 case NON_LVALUE_EXPR:
16302 case VIEW_CONVERT_EXPR:
16303 {
16304 /* Handle location wrappers by substituting the wrapped node
16305 first, *then* reusing the resulting type. Doing the type
16306 first ensures that we handle template parameters and
16307 parameter pack expansions. */
16308 if (location_wrapper_p (t))
16309 {
16310 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16311 complain, in_decl);
16312 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16313 }
16314 tree op = TREE_OPERAND (t, 0);
16315 if (code == VIEW_CONVERT_EXPR
16316 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16317 {
16318 /* Wrapper to make a C++20 template parameter object const. */
16319 op = tsubst_copy (op, args, complain, in_decl);
16320 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16321 {
16322 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16323 return build1 (code, type, op);
16324 }
16325 else
16326 {
16327 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
16328 return op;
16329 }
16330 }
16331 /* We shouldn't see any other uses of these in templates. */
16332 gcc_unreachable ();
16333 }
16334
16335 case CAST_EXPR:
16336 case REINTERPRET_CAST_EXPR:
16337 case CONST_CAST_EXPR:
16338 case STATIC_CAST_EXPR:
16339 case DYNAMIC_CAST_EXPR:
16340 case IMPLICIT_CONV_EXPR:
16341 case CONVERT_EXPR:
16342 case NOP_EXPR:
16343 {
16344 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16345 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16346 return build1 (code, type, op0);
16347 }
16348
16349 case SIZEOF_EXPR:
16350 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16351 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16352 {
16353 tree expanded, op = TREE_OPERAND (t, 0);
16354 int len = 0;
16355
16356 if (SIZEOF_EXPR_TYPE_P (t))
16357 op = TREE_TYPE (op);
16358
16359 ++cp_unevaluated_operand;
16360 ++c_inhibit_evaluation_warnings;
16361 /* We only want to compute the number of arguments. */
16362 if (PACK_EXPANSION_P (op))
16363 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16364 else
16365 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16366 args, complain, in_decl);
16367 --cp_unevaluated_operand;
16368 --c_inhibit_evaluation_warnings;
16369
16370 if (TREE_CODE (expanded) == TREE_VEC)
16371 {
16372 len = TREE_VEC_LENGTH (expanded);
16373 /* Set TREE_USED for the benefit of -Wunused. */
16374 for (int i = 0; i < len; i++)
16375 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16376 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16377 }
16378
16379 if (expanded == error_mark_node)
16380 return error_mark_node;
16381 else if (PACK_EXPANSION_P (expanded)
16382 || (TREE_CODE (expanded) == TREE_VEC
16383 && pack_expansion_args_count (expanded)))
16384
16385 {
16386 if (PACK_EXPANSION_P (expanded))
16387 /* OK. */;
16388 else if (TREE_VEC_LENGTH (expanded) == 1)
16389 expanded = TREE_VEC_ELT (expanded, 0);
16390 else
16391 expanded = make_argument_pack (expanded);
16392
16393 if (TYPE_P (expanded))
16394 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
16395 false,
16396 complain & tf_error);
16397 else
16398 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
16399 complain & tf_error);
16400 }
16401 else
16402 return build_int_cst (size_type_node, len);
16403 }
16404 if (SIZEOF_EXPR_TYPE_P (t))
16405 {
16406 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16407 args, complain, in_decl);
16408 r = build1 (NOP_EXPR, r, error_mark_node);
16409 r = build1 (SIZEOF_EXPR,
16410 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16411 SIZEOF_EXPR_TYPE_P (r) = 1;
16412 return r;
16413 }
16414 /* Fall through */
16415
16416 case INDIRECT_REF:
16417 case NEGATE_EXPR:
16418 case TRUTH_NOT_EXPR:
16419 case BIT_NOT_EXPR:
16420 case ADDR_EXPR:
16421 case UNARY_PLUS_EXPR: /* Unary + */
16422 case ALIGNOF_EXPR:
16423 case AT_ENCODE_EXPR:
16424 case ARROW_EXPR:
16425 case THROW_EXPR:
16426 case TYPEID_EXPR:
16427 case REALPART_EXPR:
16428 case IMAGPART_EXPR:
16429 case PAREN_EXPR:
16430 {
16431 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16432 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16433 r = build1 (code, type, op0);
16434 if (code == ALIGNOF_EXPR)
16435 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16436 return r;
16437 }
16438
16439 case COMPONENT_REF:
16440 {
16441 tree object;
16442 tree name;
16443
16444 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16445 name = TREE_OPERAND (t, 1);
16446 if (TREE_CODE (name) == BIT_NOT_EXPR)
16447 {
16448 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16449 complain, in_decl);
16450 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16451 }
16452 else if (TREE_CODE (name) == SCOPE_REF
16453 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16454 {
16455 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16456 complain, in_decl);
16457 name = TREE_OPERAND (name, 1);
16458 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16459 complain, in_decl);
16460 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16461 name = build_qualified_name (/*type=*/NULL_TREE,
16462 base, name,
16463 /*template_p=*/false);
16464 }
16465 else if (BASELINK_P (name))
16466 name = tsubst_baselink (name,
16467 non_reference (TREE_TYPE (object)),
16468 args, complain,
16469 in_decl);
16470 else
16471 name = tsubst_copy (name, args, complain, in_decl);
16472 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16473 }
16474
16475 case PLUS_EXPR:
16476 case MINUS_EXPR:
16477 case MULT_EXPR:
16478 case TRUNC_DIV_EXPR:
16479 case CEIL_DIV_EXPR:
16480 case FLOOR_DIV_EXPR:
16481 case ROUND_DIV_EXPR:
16482 case EXACT_DIV_EXPR:
16483 case BIT_AND_EXPR:
16484 case BIT_IOR_EXPR:
16485 case BIT_XOR_EXPR:
16486 case TRUNC_MOD_EXPR:
16487 case FLOOR_MOD_EXPR:
16488 case TRUTH_ANDIF_EXPR:
16489 case TRUTH_ORIF_EXPR:
16490 case TRUTH_AND_EXPR:
16491 case TRUTH_OR_EXPR:
16492 case RSHIFT_EXPR:
16493 case LSHIFT_EXPR:
16494 case EQ_EXPR:
16495 case NE_EXPR:
16496 case MAX_EXPR:
16497 case MIN_EXPR:
16498 case LE_EXPR:
16499 case GE_EXPR:
16500 case LT_EXPR:
16501 case GT_EXPR:
16502 case COMPOUND_EXPR:
16503 case DOTSTAR_EXPR:
16504 case MEMBER_REF:
16505 case PREDECREMENT_EXPR:
16506 case PREINCREMENT_EXPR:
16507 case POSTDECREMENT_EXPR:
16508 case POSTINCREMENT_EXPR:
16509 {
16510 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16511 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16512 return build_nt (code, op0, op1);
16513 }
16514
16515 case SCOPE_REF:
16516 {
16517 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16518 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16519 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16520 QUALIFIED_NAME_IS_TEMPLATE (t));
16521 }
16522
16523 case ARRAY_REF:
16524 {
16525 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16526 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16527 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16528 }
16529
16530 case CALL_EXPR:
16531 {
16532 int n = VL_EXP_OPERAND_LENGTH (t);
16533 tree result = build_vl_exp (CALL_EXPR, n);
16534 int i;
16535 for (i = 0; i < n; i++)
16536 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16537 complain, in_decl);
16538 return result;
16539 }
16540
16541 case COND_EXPR:
16542 case MODOP_EXPR:
16543 case PSEUDO_DTOR_EXPR:
16544 case VEC_PERM_EXPR:
16545 {
16546 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16547 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16548 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16549 r = build_nt (code, op0, op1, op2);
16550 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16551 return r;
16552 }
16553
16554 case NEW_EXPR:
16555 {
16556 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16557 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16558 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16559 r = build_nt (code, op0, op1, op2);
16560 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16561 return r;
16562 }
16563
16564 case DELETE_EXPR:
16565 {
16566 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16567 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16568 r = build_nt (code, op0, op1);
16569 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16570 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16571 return r;
16572 }
16573
16574 case TEMPLATE_ID_EXPR:
16575 {
16576 /* Substituted template arguments */
16577 tree fn = TREE_OPERAND (t, 0);
16578 tree targs = TREE_OPERAND (t, 1);
16579
16580 fn = tsubst_copy (fn, args, complain, in_decl);
16581 if (targs)
16582 targs = tsubst_template_args (targs, args, complain, in_decl);
16583
16584 return lookup_template_function (fn, targs);
16585 }
16586
16587 case TREE_LIST:
16588 {
16589 tree purpose, value, chain;
16590
16591 if (t == void_list_node)
16592 return t;
16593
16594 purpose = TREE_PURPOSE (t);
16595 if (purpose)
16596 purpose = tsubst_copy (purpose, args, complain, in_decl);
16597 value = TREE_VALUE (t);
16598 if (value)
16599 value = tsubst_copy (value, args, complain, in_decl);
16600 chain = TREE_CHAIN (t);
16601 if (chain && chain != void_type_node)
16602 chain = tsubst_copy (chain, args, complain, in_decl);
16603 if (purpose == TREE_PURPOSE (t)
16604 && value == TREE_VALUE (t)
16605 && chain == TREE_CHAIN (t))
16606 return t;
16607 return tree_cons (purpose, value, chain);
16608 }
16609
16610 case RECORD_TYPE:
16611 case UNION_TYPE:
16612 case ENUMERAL_TYPE:
16613 case INTEGER_TYPE:
16614 case TEMPLATE_TYPE_PARM:
16615 case TEMPLATE_TEMPLATE_PARM:
16616 case BOUND_TEMPLATE_TEMPLATE_PARM:
16617 case TEMPLATE_PARM_INDEX:
16618 case POINTER_TYPE:
16619 case REFERENCE_TYPE:
16620 case OFFSET_TYPE:
16621 case FUNCTION_TYPE:
16622 case METHOD_TYPE:
16623 case ARRAY_TYPE:
16624 case TYPENAME_TYPE:
16625 case UNBOUND_CLASS_TEMPLATE:
16626 case TYPEOF_TYPE:
16627 case DECLTYPE_TYPE:
16628 case TYPE_DECL:
16629 return tsubst (t, args, complain, in_decl);
16630
16631 case USING_DECL:
16632 t = DECL_NAME (t);
16633 /* Fall through. */
16634 case IDENTIFIER_NODE:
16635 if (IDENTIFIER_CONV_OP_P (t))
16636 {
16637 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16638 return make_conv_op_name (new_type);
16639 }
16640 else
16641 return t;
16642
16643 case CONSTRUCTOR:
16644 /* This is handled by tsubst_copy_and_build. */
16645 gcc_unreachable ();
16646
16647 case VA_ARG_EXPR:
16648 {
16649 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16651 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16652 }
16653
16654 case CLEANUP_POINT_EXPR:
16655 /* We shouldn't have built any of these during initial template
16656 generation. Instead, they should be built during instantiation
16657 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16658 gcc_unreachable ();
16659
16660 case OFFSET_REF:
16661 {
16662 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16663 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16664 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16665 r = build2 (code, type, op0, op1);
16666 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16667 if (!mark_used (TREE_OPERAND (r, 1), complain)
16668 && !(complain & tf_error))
16669 return error_mark_node;
16670 return r;
16671 }
16672
16673 case EXPR_PACK_EXPANSION:
16674 error ("invalid use of pack expansion expression");
16675 return error_mark_node;
16676
16677 case NONTYPE_ARGUMENT_PACK:
16678 error ("use %<...%> to expand argument pack");
16679 return error_mark_node;
16680
16681 case VOID_CST:
16682 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16683 return t;
16684
16685 case INTEGER_CST:
16686 case REAL_CST:
16687 case STRING_CST:
16688 case COMPLEX_CST:
16689 {
16690 /* Instantiate any typedefs in the type. */
16691 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16692 r = fold_convert (type, t);
16693 gcc_assert (TREE_CODE (r) == code);
16694 return r;
16695 }
16696
16697 case PTRMEM_CST:
16698 /* These can sometimes show up in a partial instantiation, but never
16699 involve template parms. */
16700 gcc_assert (!uses_template_parms (t));
16701 return t;
16702
16703 case UNARY_LEFT_FOLD_EXPR:
16704 return tsubst_unary_left_fold (t, args, complain, in_decl);
16705 case UNARY_RIGHT_FOLD_EXPR:
16706 return tsubst_unary_right_fold (t, args, complain, in_decl);
16707 case BINARY_LEFT_FOLD_EXPR:
16708 return tsubst_binary_left_fold (t, args, complain, in_decl);
16709 case BINARY_RIGHT_FOLD_EXPR:
16710 return tsubst_binary_right_fold (t, args, complain, in_decl);
16711 case PREDICT_EXPR:
16712 return t;
16713
16714 case DEBUG_BEGIN_STMT:
16715 /* ??? There's no point in copying it for now, but maybe some
16716 day it will contain more information, such as a pointer back
16717 to the containing function, inlined copy or so. */
16718 return t;
16719
16720 default:
16721 /* We shouldn't get here, but keep going if !flag_checking. */
16722 if (flag_checking)
16723 gcc_unreachable ();
16724 return t;
16725 }
16726 }
16727
16728 /* Helper function for tsubst_omp_clauses, used for instantiation of
16729 OMP_CLAUSE_DECL of clauses. */
16730
16731 static tree
16732 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16733 tree in_decl, tree *iterator_cache)
16734 {
16735 if (decl == NULL_TREE)
16736 return NULL_TREE;
16737
16738 /* Handle OpenMP iterators. */
16739 if (TREE_CODE (decl) == TREE_LIST
16740 && TREE_PURPOSE (decl)
16741 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16742 {
16743 tree ret;
16744 if (iterator_cache[0] == TREE_PURPOSE (decl))
16745 ret = iterator_cache[1];
16746 else
16747 {
16748 tree *tp = &ret;
16749 begin_scope (sk_omp, NULL);
16750 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16751 {
16752 *tp = copy_node (it);
16753 TREE_VEC_ELT (*tp, 0)
16754 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16755 TREE_VEC_ELT (*tp, 1)
16756 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16757 /*integral_constant_expression_p=*/false);
16758 TREE_VEC_ELT (*tp, 2)
16759 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16760 /*integral_constant_expression_p=*/false);
16761 TREE_VEC_ELT (*tp, 3)
16762 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16763 /*integral_constant_expression_p=*/false);
16764 TREE_CHAIN (*tp) = NULL_TREE;
16765 tp = &TREE_CHAIN (*tp);
16766 }
16767 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16768 iterator_cache[0] = TREE_PURPOSE (decl);
16769 iterator_cache[1] = ret;
16770 }
16771 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16772 args, complain,
16773 in_decl, NULL));
16774 }
16775
16776 /* Handle an OpenMP array section represented as a TREE_LIST (or
16777 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16778 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16779 TREE_LIST. We can handle it exactly the same as an array section
16780 (purpose, value, and a chain), even though the nomenclature
16781 (low_bound, length, etc) is different. */
16782 if (TREE_CODE (decl) == TREE_LIST)
16783 {
16784 tree low_bound
16785 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16786 /*integral_constant_expression_p=*/false);
16787 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16788 /*integral_constant_expression_p=*/false);
16789 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16790 in_decl, NULL);
16791 if (TREE_PURPOSE (decl) == low_bound
16792 && TREE_VALUE (decl) == length
16793 && TREE_CHAIN (decl) == chain)
16794 return decl;
16795 tree ret = tree_cons (low_bound, length, chain);
16796 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16797 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16798 return ret;
16799 }
16800 tree ret = tsubst_expr (decl, args, complain, in_decl,
16801 /*integral_constant_expression_p=*/false);
16802 /* Undo convert_from_reference tsubst_expr could have called. */
16803 if (decl
16804 && REFERENCE_REF_P (ret)
16805 && !REFERENCE_REF_P (decl))
16806 ret = TREE_OPERAND (ret, 0);
16807 return ret;
16808 }
16809
16810 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16811
16812 static tree
16813 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16814 tree args, tsubst_flags_t complain, tree in_decl)
16815 {
16816 tree new_clauses = NULL_TREE, nc, oc;
16817 tree linear_no_step = NULL_TREE;
16818 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16819
16820 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16821 {
16822 nc = copy_node (oc);
16823 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16824 new_clauses = nc;
16825
16826 switch (OMP_CLAUSE_CODE (nc))
16827 {
16828 case OMP_CLAUSE_LASTPRIVATE:
16829 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16830 {
16831 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16832 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16833 in_decl, /*integral_constant_expression_p=*/false);
16834 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16835 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16836 }
16837 /* FALLTHRU */
16838 case OMP_CLAUSE_PRIVATE:
16839 case OMP_CLAUSE_SHARED:
16840 case OMP_CLAUSE_FIRSTPRIVATE:
16841 case OMP_CLAUSE_COPYIN:
16842 case OMP_CLAUSE_COPYPRIVATE:
16843 case OMP_CLAUSE_UNIFORM:
16844 case OMP_CLAUSE_DEPEND:
16845 case OMP_CLAUSE_FROM:
16846 case OMP_CLAUSE_TO:
16847 case OMP_CLAUSE_MAP:
16848 case OMP_CLAUSE_NONTEMPORAL:
16849 case OMP_CLAUSE_USE_DEVICE_PTR:
16850 case OMP_CLAUSE_USE_DEVICE_ADDR:
16851 case OMP_CLAUSE_IS_DEVICE_PTR:
16852 case OMP_CLAUSE_INCLUSIVE:
16853 case OMP_CLAUSE_EXCLUSIVE:
16854 OMP_CLAUSE_DECL (nc)
16855 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16856 in_decl, iterator_cache);
16857 break;
16858 case OMP_CLAUSE_TILE:
16859 case OMP_CLAUSE_IF:
16860 case OMP_CLAUSE_NUM_THREADS:
16861 case OMP_CLAUSE_SCHEDULE:
16862 case OMP_CLAUSE_COLLAPSE:
16863 case OMP_CLAUSE_FINAL:
16864 case OMP_CLAUSE_DEVICE:
16865 case OMP_CLAUSE_DIST_SCHEDULE:
16866 case OMP_CLAUSE_NUM_TEAMS:
16867 case OMP_CLAUSE_THREAD_LIMIT:
16868 case OMP_CLAUSE_SAFELEN:
16869 case OMP_CLAUSE_SIMDLEN:
16870 case OMP_CLAUSE_NUM_TASKS:
16871 case OMP_CLAUSE_GRAINSIZE:
16872 case OMP_CLAUSE_PRIORITY:
16873 case OMP_CLAUSE_ORDERED:
16874 case OMP_CLAUSE_HINT:
16875 case OMP_CLAUSE_NUM_GANGS:
16876 case OMP_CLAUSE_NUM_WORKERS:
16877 case OMP_CLAUSE_VECTOR_LENGTH:
16878 case OMP_CLAUSE_WORKER:
16879 case OMP_CLAUSE_VECTOR:
16880 case OMP_CLAUSE_ASYNC:
16881 case OMP_CLAUSE_WAIT:
16882 OMP_CLAUSE_OPERAND (nc, 0)
16883 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16884 in_decl, /*integral_constant_expression_p=*/false);
16885 break;
16886 case OMP_CLAUSE_REDUCTION:
16887 case OMP_CLAUSE_IN_REDUCTION:
16888 case OMP_CLAUSE_TASK_REDUCTION:
16889 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16890 {
16891 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16892 if (TREE_CODE (placeholder) == SCOPE_REF)
16893 {
16894 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16895 complain, in_decl);
16896 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16897 = build_qualified_name (NULL_TREE, scope,
16898 TREE_OPERAND (placeholder, 1),
16899 false);
16900 }
16901 else
16902 gcc_assert (identifier_p (placeholder));
16903 }
16904 OMP_CLAUSE_DECL (nc)
16905 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16906 in_decl, NULL);
16907 break;
16908 case OMP_CLAUSE_GANG:
16909 case OMP_CLAUSE_ALIGNED:
16910 OMP_CLAUSE_DECL (nc)
16911 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16912 in_decl, NULL);
16913 OMP_CLAUSE_OPERAND (nc, 1)
16914 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16915 in_decl, /*integral_constant_expression_p=*/false);
16916 break;
16917 case OMP_CLAUSE_LINEAR:
16918 OMP_CLAUSE_DECL (nc)
16919 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16920 in_decl, NULL);
16921 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16922 {
16923 gcc_assert (!linear_no_step);
16924 linear_no_step = nc;
16925 }
16926 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16927 OMP_CLAUSE_LINEAR_STEP (nc)
16928 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16929 complain, in_decl, NULL);
16930 else
16931 OMP_CLAUSE_LINEAR_STEP (nc)
16932 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16933 in_decl,
16934 /*integral_constant_expression_p=*/false);
16935 break;
16936 case OMP_CLAUSE_NOWAIT:
16937 case OMP_CLAUSE_DEFAULT:
16938 case OMP_CLAUSE_UNTIED:
16939 case OMP_CLAUSE_MERGEABLE:
16940 case OMP_CLAUSE_INBRANCH:
16941 case OMP_CLAUSE_NOTINBRANCH:
16942 case OMP_CLAUSE_PROC_BIND:
16943 case OMP_CLAUSE_FOR:
16944 case OMP_CLAUSE_PARALLEL:
16945 case OMP_CLAUSE_SECTIONS:
16946 case OMP_CLAUSE_TASKGROUP:
16947 case OMP_CLAUSE_NOGROUP:
16948 case OMP_CLAUSE_THREADS:
16949 case OMP_CLAUSE_SIMD:
16950 case OMP_CLAUSE_DEFAULTMAP:
16951 case OMP_CLAUSE_ORDER:
16952 case OMP_CLAUSE_BIND:
16953 case OMP_CLAUSE_INDEPENDENT:
16954 case OMP_CLAUSE_AUTO:
16955 case OMP_CLAUSE_SEQ:
16956 case OMP_CLAUSE_IF_PRESENT:
16957 case OMP_CLAUSE_FINALIZE:
16958 break;
16959 default:
16960 gcc_unreachable ();
16961 }
16962 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16963 switch (OMP_CLAUSE_CODE (nc))
16964 {
16965 case OMP_CLAUSE_SHARED:
16966 case OMP_CLAUSE_PRIVATE:
16967 case OMP_CLAUSE_FIRSTPRIVATE:
16968 case OMP_CLAUSE_LASTPRIVATE:
16969 case OMP_CLAUSE_COPYPRIVATE:
16970 case OMP_CLAUSE_LINEAR:
16971 case OMP_CLAUSE_REDUCTION:
16972 case OMP_CLAUSE_IN_REDUCTION:
16973 case OMP_CLAUSE_TASK_REDUCTION:
16974 case OMP_CLAUSE_USE_DEVICE_PTR:
16975 case OMP_CLAUSE_USE_DEVICE_ADDR:
16976 case OMP_CLAUSE_IS_DEVICE_PTR:
16977 case OMP_CLAUSE_INCLUSIVE:
16978 case OMP_CLAUSE_EXCLUSIVE:
16979 /* tsubst_expr on SCOPE_REF results in returning
16980 finish_non_static_data_member result. Undo that here. */
16981 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16982 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16983 == IDENTIFIER_NODE))
16984 {
16985 tree t = OMP_CLAUSE_DECL (nc);
16986 tree v = t;
16987 while (v)
16988 switch (TREE_CODE (v))
16989 {
16990 case COMPONENT_REF:
16991 case MEM_REF:
16992 case INDIRECT_REF:
16993 CASE_CONVERT:
16994 case POINTER_PLUS_EXPR:
16995 v = TREE_OPERAND (v, 0);
16996 continue;
16997 case PARM_DECL:
16998 if (DECL_CONTEXT (v) == current_function_decl
16999 && DECL_ARTIFICIAL (v)
17000 && DECL_NAME (v) == this_identifier)
17001 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17002 /* FALLTHRU */
17003 default:
17004 v = NULL_TREE;
17005 break;
17006 }
17007 }
17008 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17009 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17010 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17011 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17012 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17013 {
17014 tree decl = OMP_CLAUSE_DECL (nc);
17015 if (VAR_P (decl))
17016 {
17017 retrofit_lang_decl (decl);
17018 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17019 }
17020 }
17021 break;
17022 default:
17023 break;
17024 }
17025 }
17026
17027 new_clauses = nreverse (new_clauses);
17028 if (ort != C_ORT_OMP_DECLARE_SIMD)
17029 {
17030 new_clauses = finish_omp_clauses (new_clauses, ort);
17031 if (linear_no_step)
17032 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17033 if (nc == linear_no_step)
17034 {
17035 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17036 break;
17037 }
17038 }
17039 return new_clauses;
17040 }
17041
17042 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17043
17044 static tree
17045 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17046 tree in_decl)
17047 {
17048 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17049
17050 tree purpose, value, chain;
17051
17052 if (t == NULL)
17053 return t;
17054
17055 if (TREE_CODE (t) != TREE_LIST)
17056 return tsubst_copy_and_build (t, args, complain, in_decl,
17057 /*function_p=*/false,
17058 /*integral_constant_expression_p=*/false);
17059
17060 if (t == void_list_node)
17061 return t;
17062
17063 purpose = TREE_PURPOSE (t);
17064 if (purpose)
17065 purpose = RECUR (purpose);
17066 value = TREE_VALUE (t);
17067 if (value)
17068 {
17069 if (TREE_CODE (value) != LABEL_DECL)
17070 value = RECUR (value);
17071 else
17072 {
17073 value = lookup_label (DECL_NAME (value));
17074 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17075 TREE_USED (value) = 1;
17076 }
17077 }
17078 chain = TREE_CHAIN (t);
17079 if (chain && chain != void_type_node)
17080 chain = RECUR (chain);
17081 return tree_cons (purpose, value, chain);
17082 #undef RECUR
17083 }
17084
17085 /* Used to temporarily communicate the list of #pragma omp parallel
17086 clauses to #pragma omp for instantiation if they are combined
17087 together. */
17088
17089 static tree *omp_parallel_combined_clauses;
17090
17091 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17092 tree *, unsigned int *);
17093
17094 /* Substitute one OMP_FOR iterator. */
17095
17096 static bool
17097 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17098 tree initv, tree condv, tree incrv, tree *clauses,
17099 tree args, tsubst_flags_t complain, tree in_decl,
17100 bool integral_constant_expression_p)
17101 {
17102 #define RECUR(NODE) \
17103 tsubst_expr ((NODE), args, complain, in_decl, \
17104 integral_constant_expression_p)
17105 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17106 bool ret = false;
17107
17108 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17109 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17110
17111 decl = TREE_OPERAND (init, 0);
17112 init = TREE_OPERAND (init, 1);
17113 tree decl_expr = NULL_TREE;
17114 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17115 if (range_for)
17116 {
17117 bool decomp = false;
17118 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17119 {
17120 tree v = DECL_VALUE_EXPR (decl);
17121 if (TREE_CODE (v) == ARRAY_REF
17122 && VAR_P (TREE_OPERAND (v, 0))
17123 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17124 {
17125 tree decomp_first = NULL_TREE;
17126 unsigned decomp_cnt = 0;
17127 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17128 maybe_push_decl (d);
17129 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17130 in_decl, &decomp_first, &decomp_cnt);
17131 decomp = true;
17132 if (d == error_mark_node)
17133 decl = error_mark_node;
17134 else
17135 for (unsigned int i = 0; i < decomp_cnt; i++)
17136 {
17137 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17138 {
17139 tree v = build_nt (ARRAY_REF, d,
17140 size_int (decomp_cnt - i - 1),
17141 NULL_TREE, NULL_TREE);
17142 SET_DECL_VALUE_EXPR (decomp_first, v);
17143 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17144 }
17145 fit_decomposition_lang_decl (decomp_first, d);
17146 decomp_first = DECL_CHAIN (decomp_first);
17147 }
17148 }
17149 }
17150 decl = tsubst_decl (decl, args, complain);
17151 if (!decomp)
17152 maybe_push_decl (decl);
17153 }
17154 else if (init && TREE_CODE (init) == DECL_EXPR)
17155 {
17156 /* We need to jump through some hoops to handle declarations in the
17157 init-statement, since we might need to handle auto deduction,
17158 but we need to keep control of initialization. */
17159 decl_expr = init;
17160 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17161 decl = tsubst_decl (decl, args, complain);
17162 }
17163 else
17164 {
17165 if (TREE_CODE (decl) == SCOPE_REF)
17166 {
17167 decl = RECUR (decl);
17168 if (TREE_CODE (decl) == COMPONENT_REF)
17169 {
17170 tree v = decl;
17171 while (v)
17172 switch (TREE_CODE (v))
17173 {
17174 case COMPONENT_REF:
17175 case MEM_REF:
17176 case INDIRECT_REF:
17177 CASE_CONVERT:
17178 case POINTER_PLUS_EXPR:
17179 v = TREE_OPERAND (v, 0);
17180 continue;
17181 case PARM_DECL:
17182 if (DECL_CONTEXT (v) == current_function_decl
17183 && DECL_ARTIFICIAL (v)
17184 && DECL_NAME (v) == this_identifier)
17185 {
17186 decl = TREE_OPERAND (decl, 1);
17187 decl = omp_privatize_field (decl, false);
17188 }
17189 /* FALLTHRU */
17190 default:
17191 v = NULL_TREE;
17192 break;
17193 }
17194 }
17195 }
17196 else
17197 decl = RECUR (decl);
17198 }
17199 init = RECUR (init);
17200
17201 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17202 {
17203 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17204 if (TREE_CODE (o) == TREE_LIST)
17205 TREE_VEC_ELT (orig_declv, i)
17206 = tree_cons (RECUR (TREE_PURPOSE (o)),
17207 RECUR (TREE_VALUE (o)),
17208 NULL_TREE);
17209 else
17210 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17211 }
17212
17213 if (range_for)
17214 {
17215 tree this_pre_body = NULL_TREE;
17216 tree orig_init = NULL_TREE;
17217 tree orig_decl = NULL_TREE;
17218 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17219 orig_init, cond, incr);
17220 if (orig_decl)
17221 {
17222 if (orig_declv == NULL_TREE)
17223 orig_declv = copy_node (declv);
17224 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17225 ret = true;
17226 }
17227 else if (orig_declv)
17228 TREE_VEC_ELT (orig_declv, i) = decl;
17229 }
17230
17231 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17232 if (!range_for && auto_node && init)
17233 TREE_TYPE (decl)
17234 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17235
17236 gcc_assert (!type_dependent_expression_p (decl));
17237
17238 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17239 {
17240 if (decl_expr)
17241 {
17242 /* Declare the variable, but don't let that initialize it. */
17243 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17244 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17245 RECUR (decl_expr);
17246 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17247 }
17248
17249 if (!range_for)
17250 {
17251 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17252 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17253 if (TREE_CODE (incr) == MODIFY_EXPR)
17254 {
17255 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17256 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17257 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17258 NOP_EXPR, rhs, complain);
17259 }
17260 else
17261 incr = RECUR (incr);
17262 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17263 TREE_VEC_ELT (orig_declv, i) = decl;
17264 }
17265 TREE_VEC_ELT (declv, i) = decl;
17266 TREE_VEC_ELT (initv, i) = init;
17267 TREE_VEC_ELT (condv, i) = cond;
17268 TREE_VEC_ELT (incrv, i) = incr;
17269 return ret;
17270 }
17271
17272 if (decl_expr)
17273 {
17274 /* Declare and initialize the variable. */
17275 RECUR (decl_expr);
17276 init = NULL_TREE;
17277 }
17278 else if (init)
17279 {
17280 tree *pc;
17281 int j;
17282 for (j = ((omp_parallel_combined_clauses == NULL
17283 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17284 {
17285 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17286 {
17287 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17288 && OMP_CLAUSE_DECL (*pc) == decl)
17289 break;
17290 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17291 && OMP_CLAUSE_DECL (*pc) == decl)
17292 {
17293 if (j)
17294 break;
17295 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17296 tree c = *pc;
17297 *pc = OMP_CLAUSE_CHAIN (c);
17298 OMP_CLAUSE_CHAIN (c) = *clauses;
17299 *clauses = c;
17300 }
17301 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17302 && OMP_CLAUSE_DECL (*pc) == decl)
17303 {
17304 error ("iteration variable %qD should not be firstprivate",
17305 decl);
17306 *pc = OMP_CLAUSE_CHAIN (*pc);
17307 }
17308 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17309 && OMP_CLAUSE_DECL (*pc) == decl)
17310 {
17311 error ("iteration variable %qD should not be reduction",
17312 decl);
17313 *pc = OMP_CLAUSE_CHAIN (*pc);
17314 }
17315 else
17316 pc = &OMP_CLAUSE_CHAIN (*pc);
17317 }
17318 if (*pc)
17319 break;
17320 }
17321 if (*pc == NULL_TREE)
17322 {
17323 tree c = build_omp_clause (input_location,
17324 TREE_CODE (t) == OMP_LOOP
17325 ? OMP_CLAUSE_LASTPRIVATE
17326 : OMP_CLAUSE_PRIVATE);
17327 OMP_CLAUSE_DECL (c) = decl;
17328 c = finish_omp_clauses (c, C_ORT_OMP);
17329 if (c)
17330 {
17331 OMP_CLAUSE_CHAIN (c) = *clauses;
17332 *clauses = c;
17333 }
17334 }
17335 }
17336 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17337 if (COMPARISON_CLASS_P (cond))
17338 {
17339 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17340 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17341 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17342 }
17343 else
17344 cond = RECUR (cond);
17345 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17346 switch (TREE_CODE (incr))
17347 {
17348 case PREINCREMENT_EXPR:
17349 case PREDECREMENT_EXPR:
17350 case POSTINCREMENT_EXPR:
17351 case POSTDECREMENT_EXPR:
17352 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17353 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17354 break;
17355 case MODIFY_EXPR:
17356 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17357 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17358 {
17359 tree rhs = TREE_OPERAND (incr, 1);
17360 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17361 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17362 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17363 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17364 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17365 rhs0, rhs1));
17366 }
17367 else
17368 incr = RECUR (incr);
17369 break;
17370 case MODOP_EXPR:
17371 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17372 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17373 {
17374 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17375 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17376 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17377 TREE_TYPE (decl), lhs,
17378 RECUR (TREE_OPERAND (incr, 2))));
17379 }
17380 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17381 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17382 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17383 {
17384 tree rhs = TREE_OPERAND (incr, 2);
17385 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17386 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17387 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17388 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17389 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17390 rhs0, rhs1));
17391 }
17392 else
17393 incr = RECUR (incr);
17394 break;
17395 default:
17396 incr = RECUR (incr);
17397 break;
17398 }
17399
17400 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17401 TREE_VEC_ELT (orig_declv, i) = decl;
17402 TREE_VEC_ELT (declv, i) = decl;
17403 TREE_VEC_ELT (initv, i) = init;
17404 TREE_VEC_ELT (condv, i) = cond;
17405 TREE_VEC_ELT (incrv, i) = incr;
17406 return false;
17407 #undef RECUR
17408 }
17409
17410 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17411 of OMP_TARGET's body. */
17412
17413 static tree
17414 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17415 {
17416 *walk_subtrees = 0;
17417 switch (TREE_CODE (*tp))
17418 {
17419 case OMP_TEAMS:
17420 return *tp;
17421 case BIND_EXPR:
17422 case STATEMENT_LIST:
17423 *walk_subtrees = 1;
17424 break;
17425 default:
17426 break;
17427 }
17428 return NULL_TREE;
17429 }
17430
17431 /* Helper function for tsubst_expr. For decomposition declaration
17432 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17433 also the corresponding decls representing the identifiers
17434 of the decomposition declaration. Return DECL if successful
17435 or error_mark_node otherwise, set *FIRST to the first decl
17436 in the list chained through DECL_CHAIN and *CNT to the number
17437 of such decls. */
17438
17439 static tree
17440 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17441 tsubst_flags_t complain, tree in_decl, tree *first,
17442 unsigned int *cnt)
17443 {
17444 tree decl2, decl3, prev = decl;
17445 *cnt = 0;
17446 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17447 for (decl2 = DECL_CHAIN (pattern_decl);
17448 decl2
17449 && VAR_P (decl2)
17450 && DECL_DECOMPOSITION_P (decl2)
17451 && DECL_NAME (decl2);
17452 decl2 = DECL_CHAIN (decl2))
17453 {
17454 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17455 {
17456 gcc_assert (errorcount);
17457 return error_mark_node;
17458 }
17459 (*cnt)++;
17460 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17461 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17462 tree v = DECL_VALUE_EXPR (decl2);
17463 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17464 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17465 decl3 = tsubst (decl2, args, complain, in_decl);
17466 SET_DECL_VALUE_EXPR (decl2, v);
17467 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17468 if (VAR_P (decl3))
17469 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17470 else
17471 {
17472 gcc_assert (errorcount);
17473 decl = error_mark_node;
17474 continue;
17475 }
17476 maybe_push_decl (decl3);
17477 if (error_operand_p (decl3))
17478 decl = error_mark_node;
17479 else if (decl != error_mark_node
17480 && DECL_CHAIN (decl3) != prev
17481 && decl != prev)
17482 {
17483 gcc_assert (errorcount);
17484 decl = error_mark_node;
17485 }
17486 else
17487 prev = decl3;
17488 }
17489 *first = prev;
17490 return decl;
17491 }
17492
17493 /* Return the proper local_specialization for init-capture pack DECL. */
17494
17495 static tree
17496 lookup_init_capture_pack (tree decl)
17497 {
17498 /* We handle normal pack captures by forwarding to the specialization of the
17499 captured parameter. We can't do that for pack init-captures; we need them
17500 to have their own local_specialization. We created the individual
17501 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17502 when we process the DECL_EXPR for the pack init-capture in the template.
17503 So, how do we find them? We don't know the capture proxy pack when
17504 building the individual resulting proxies, and we don't know the
17505 individual proxies when instantiating the pack. What we have in common is
17506 the FIELD_DECL.
17507
17508 So...when we instantiate the FIELD_DECL, we stick the result in
17509 local_specializations. Then at the DECL_EXPR we look up that result, see
17510 how many elements it has, synthesize the names, and look them up. */
17511
17512 tree cname = DECL_NAME (decl);
17513 tree val = DECL_VALUE_EXPR (decl);
17514 tree field = TREE_OPERAND (val, 1);
17515 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17516 tree fpack = retrieve_local_specialization (field);
17517 if (fpack == error_mark_node)
17518 return error_mark_node;
17519
17520 int len = 1;
17521 tree vec = NULL_TREE;
17522 tree r = NULL_TREE;
17523 if (TREE_CODE (fpack) == TREE_VEC)
17524 {
17525 len = TREE_VEC_LENGTH (fpack);
17526 vec = make_tree_vec (len);
17527 r = make_node (NONTYPE_ARGUMENT_PACK);
17528 SET_ARGUMENT_PACK_ARGS (r, vec);
17529 }
17530 for (int i = 0; i < len; ++i)
17531 {
17532 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17533 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17534 if (vec)
17535 TREE_VEC_ELT (vec, i) = elt;
17536 else
17537 r = elt;
17538 }
17539 return r;
17540 }
17541
17542 /* Like tsubst_copy for expressions, etc. but also does semantic
17543 processing. */
17544
17545 tree
17546 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17547 bool integral_constant_expression_p)
17548 {
17549 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17550 #define RECUR(NODE) \
17551 tsubst_expr ((NODE), args, complain, in_decl, \
17552 integral_constant_expression_p)
17553
17554 tree stmt, tmp;
17555 tree r;
17556 location_t loc;
17557
17558 if (t == NULL_TREE || t == error_mark_node)
17559 return t;
17560
17561 loc = input_location;
17562 if (location_t eloc = cp_expr_location (t))
17563 input_location = eloc;
17564 if (STATEMENT_CODE_P (TREE_CODE (t)))
17565 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17566
17567 switch (TREE_CODE (t))
17568 {
17569 case STATEMENT_LIST:
17570 {
17571 tree_stmt_iterator i;
17572 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17573 RECUR (tsi_stmt (i));
17574 break;
17575 }
17576
17577 case CTOR_INITIALIZER:
17578 finish_mem_initializers (tsubst_initializer_list
17579 (TREE_OPERAND (t, 0), args));
17580 break;
17581
17582 case RETURN_EXPR:
17583 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17584 break;
17585
17586 case EXPR_STMT:
17587 tmp = RECUR (EXPR_STMT_EXPR (t));
17588 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17589 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17590 else
17591 finish_expr_stmt (tmp);
17592 break;
17593
17594 case USING_STMT:
17595 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17596 break;
17597
17598 case DECL_EXPR:
17599 {
17600 tree decl, pattern_decl;
17601 tree init;
17602
17603 pattern_decl = decl = DECL_EXPR_DECL (t);
17604 if (TREE_CODE (decl) == LABEL_DECL)
17605 finish_label_decl (DECL_NAME (decl));
17606 else if (TREE_CODE (decl) == USING_DECL)
17607 {
17608 tree scope = USING_DECL_SCOPE (decl);
17609 tree name = DECL_NAME (decl);
17610
17611 scope = tsubst (scope, args, complain, in_decl);
17612 finish_nonmember_using_decl (scope, name);
17613 }
17614 else if (is_capture_proxy (decl)
17615 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17616 {
17617 /* We're in tsubst_lambda_expr, we've already inserted a new
17618 capture proxy, so look it up and register it. */
17619 tree inst;
17620 if (!DECL_PACK_P (decl))
17621 {
17622 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17623 /*nonclass*/1, /*block_p=*/true,
17624 /*ns_only*/0, LOOKUP_HIDDEN);
17625 gcc_assert (inst != decl && is_capture_proxy (inst));
17626 }
17627 else if (is_normal_capture_proxy (decl))
17628 {
17629 inst = (retrieve_local_specialization
17630 (DECL_CAPTURED_VARIABLE (decl)));
17631 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17632 }
17633 else
17634 inst = lookup_init_capture_pack (decl);
17635
17636 register_local_specialization (inst, decl);
17637 break;
17638 }
17639 else if (DECL_PRETTY_FUNCTION_P (decl))
17640 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17641 DECL_NAME (decl),
17642 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17643 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17644 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17645 /* Don't copy the old closure; we'll create a new one in
17646 tsubst_lambda_expr. */
17647 break;
17648 else
17649 {
17650 init = DECL_INITIAL (decl);
17651 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17652 for local variables, so save if DECL was declared constinit. */
17653 const bool constinit_p
17654 = (VAR_P (decl)
17655 && DECL_LANG_SPECIFIC (decl)
17656 && DECL_TEMPLATE_INFO (decl)
17657 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17658 decl = tsubst (decl, args, complain, in_decl);
17659 if (decl != error_mark_node)
17660 {
17661 /* By marking the declaration as instantiated, we avoid
17662 trying to instantiate it. Since instantiate_decl can't
17663 handle local variables, and since we've already done
17664 all that needs to be done, that's the right thing to
17665 do. */
17666 if (VAR_P (decl))
17667 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17668 if (VAR_P (decl) && !DECL_NAME (decl)
17669 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17670 /* Anonymous aggregates are a special case. */
17671 finish_anon_union (decl);
17672 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17673 {
17674 DECL_CONTEXT (decl) = current_function_decl;
17675 if (DECL_NAME (decl) == this_identifier)
17676 {
17677 tree lam = DECL_CONTEXT (current_function_decl);
17678 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17679 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17680 }
17681 insert_capture_proxy (decl);
17682 }
17683 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17684 /* We already did a pushtag. */;
17685 else if (TREE_CODE (decl) == FUNCTION_DECL
17686 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17687 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17688 {
17689 DECL_CONTEXT (decl) = NULL_TREE;
17690 pushdecl (decl);
17691 DECL_CONTEXT (decl) = current_function_decl;
17692 cp_check_omp_declare_reduction (decl);
17693 }
17694 else
17695 {
17696 bool const_init = false;
17697 unsigned int cnt = 0;
17698 tree first = NULL_TREE, ndecl = error_mark_node;
17699 maybe_push_decl (decl);
17700
17701 if (VAR_P (decl)
17702 && DECL_DECOMPOSITION_P (decl)
17703 && TREE_TYPE (pattern_decl) != error_mark_node)
17704 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17705 complain, in_decl, &first,
17706 &cnt);
17707
17708 init = tsubst_init (init, decl, args, complain, in_decl);
17709
17710 if (VAR_P (decl))
17711 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17712 (pattern_decl));
17713
17714 if (ndecl != error_mark_node)
17715 cp_maybe_mangle_decomp (ndecl, first, cnt);
17716
17717 cp_finish_decl (decl, init, const_init, NULL_TREE,
17718 constinit_p ? LOOKUP_CONSTINIT : 0);
17719
17720 if (ndecl != error_mark_node)
17721 cp_finish_decomp (ndecl, first, cnt);
17722 }
17723 }
17724 }
17725
17726 break;
17727 }
17728
17729 case FOR_STMT:
17730 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17731 RECUR (FOR_INIT_STMT (t));
17732 finish_init_stmt (stmt);
17733 tmp = RECUR (FOR_COND (t));
17734 finish_for_cond (tmp, stmt, false, 0);
17735 tmp = RECUR (FOR_EXPR (t));
17736 finish_for_expr (tmp, stmt);
17737 {
17738 bool prev = note_iteration_stmt_body_start ();
17739 RECUR (FOR_BODY (t));
17740 note_iteration_stmt_body_end (prev);
17741 }
17742 finish_for_stmt (stmt);
17743 break;
17744
17745 case RANGE_FOR_STMT:
17746 {
17747 /* Construct another range_for, if this is not a final
17748 substitution (for inside inside a generic lambda of a
17749 template). Otherwise convert to a regular for. */
17750 tree decl, expr;
17751 stmt = (processing_template_decl
17752 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17753 : begin_for_stmt (NULL_TREE, NULL_TREE));
17754 RECUR (RANGE_FOR_INIT_STMT (t));
17755 decl = RANGE_FOR_DECL (t);
17756 decl = tsubst (decl, args, complain, in_decl);
17757 maybe_push_decl (decl);
17758 expr = RECUR (RANGE_FOR_EXPR (t));
17759
17760 tree decomp_first = NULL_TREE;
17761 unsigned decomp_cnt = 0;
17762 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17763 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17764 complain, in_decl,
17765 &decomp_first, &decomp_cnt);
17766
17767 if (processing_template_decl)
17768 {
17769 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17770 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17771 finish_range_for_decl (stmt, decl, expr);
17772 if (decomp_first && decl != error_mark_node)
17773 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17774 }
17775 else
17776 {
17777 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17778 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17779 stmt = cp_convert_range_for (stmt, decl, expr,
17780 decomp_first, decomp_cnt,
17781 RANGE_FOR_IVDEP (t), unroll);
17782 }
17783
17784 bool prev = note_iteration_stmt_body_start ();
17785 RECUR (RANGE_FOR_BODY (t));
17786 note_iteration_stmt_body_end (prev);
17787 finish_for_stmt (stmt);
17788 }
17789 break;
17790
17791 case WHILE_STMT:
17792 stmt = begin_while_stmt ();
17793 tmp = RECUR (WHILE_COND (t));
17794 finish_while_stmt_cond (tmp, stmt, false, 0);
17795 {
17796 bool prev = note_iteration_stmt_body_start ();
17797 RECUR (WHILE_BODY (t));
17798 note_iteration_stmt_body_end (prev);
17799 }
17800 finish_while_stmt (stmt);
17801 break;
17802
17803 case DO_STMT:
17804 stmt = begin_do_stmt ();
17805 {
17806 bool prev = note_iteration_stmt_body_start ();
17807 RECUR (DO_BODY (t));
17808 note_iteration_stmt_body_end (prev);
17809 }
17810 finish_do_body (stmt);
17811 tmp = RECUR (DO_COND (t));
17812 finish_do_stmt (tmp, stmt, false, 0);
17813 break;
17814
17815 case IF_STMT:
17816 stmt = begin_if_stmt ();
17817 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17818 if (IF_STMT_CONSTEXPR_P (t))
17819 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17820 tmp = RECUR (IF_COND (t));
17821 tmp = finish_if_stmt_cond (tmp, stmt);
17822 if (IF_STMT_CONSTEXPR_P (t)
17823 && instantiation_dependent_expression_p (tmp))
17824 {
17825 /* We're partially instantiating a generic lambda, but the condition
17826 of the constexpr if is still dependent. Don't substitute into the
17827 branches now, just remember the template arguments. */
17828 do_poplevel (IF_SCOPE (stmt));
17829 IF_COND (stmt) = IF_COND (t);
17830 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17831 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17832 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17833 add_stmt (stmt);
17834 break;
17835 }
17836 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17837 /* Don't instantiate the THEN_CLAUSE. */;
17838 else
17839 {
17840 tree folded = fold_non_dependent_expr (tmp, complain);
17841 bool inhibit = integer_zerop (folded);
17842 if (inhibit)
17843 ++c_inhibit_evaluation_warnings;
17844 RECUR (THEN_CLAUSE (t));
17845 if (inhibit)
17846 --c_inhibit_evaluation_warnings;
17847 }
17848 finish_then_clause (stmt);
17849
17850 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17851 /* Don't instantiate the ELSE_CLAUSE. */;
17852 else if (ELSE_CLAUSE (t))
17853 {
17854 tree folded = fold_non_dependent_expr (tmp, complain);
17855 bool inhibit = integer_nonzerop (folded);
17856 begin_else_clause (stmt);
17857 if (inhibit)
17858 ++c_inhibit_evaluation_warnings;
17859 RECUR (ELSE_CLAUSE (t));
17860 if (inhibit)
17861 --c_inhibit_evaluation_warnings;
17862 finish_else_clause (stmt);
17863 }
17864
17865 finish_if_stmt (stmt);
17866 break;
17867
17868 case BIND_EXPR:
17869 if (BIND_EXPR_BODY_BLOCK (t))
17870 stmt = begin_function_body ();
17871 else
17872 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17873 ? BCS_TRY_BLOCK : 0);
17874
17875 RECUR (BIND_EXPR_BODY (t));
17876
17877 if (BIND_EXPR_BODY_BLOCK (t))
17878 finish_function_body (stmt);
17879 else
17880 finish_compound_stmt (stmt);
17881 break;
17882
17883 case BREAK_STMT:
17884 finish_break_stmt ();
17885 break;
17886
17887 case CONTINUE_STMT:
17888 finish_continue_stmt ();
17889 break;
17890
17891 case SWITCH_STMT:
17892 stmt = begin_switch_stmt ();
17893 tmp = RECUR (SWITCH_STMT_COND (t));
17894 finish_switch_cond (tmp, stmt);
17895 RECUR (SWITCH_STMT_BODY (t));
17896 finish_switch_stmt (stmt);
17897 break;
17898
17899 case CASE_LABEL_EXPR:
17900 {
17901 tree decl = CASE_LABEL (t);
17902 tree low = RECUR (CASE_LOW (t));
17903 tree high = RECUR (CASE_HIGH (t));
17904 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17905 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17906 {
17907 tree label = CASE_LABEL (l);
17908 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17909 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17910 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17911 }
17912 }
17913 break;
17914
17915 case LABEL_EXPR:
17916 {
17917 tree decl = LABEL_EXPR_LABEL (t);
17918 tree label;
17919
17920 label = finish_label_stmt (DECL_NAME (decl));
17921 if (TREE_CODE (label) == LABEL_DECL)
17922 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17923 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17924 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17925 }
17926 break;
17927
17928 case GOTO_EXPR:
17929 tmp = GOTO_DESTINATION (t);
17930 if (TREE_CODE (tmp) != LABEL_DECL)
17931 /* Computed goto's must be tsubst'd into. On the other hand,
17932 non-computed gotos must not be; the identifier in question
17933 will have no binding. */
17934 tmp = RECUR (tmp);
17935 else
17936 tmp = DECL_NAME (tmp);
17937 finish_goto_stmt (tmp);
17938 break;
17939
17940 case ASM_EXPR:
17941 {
17942 tree string = RECUR (ASM_STRING (t));
17943 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17944 complain, in_decl);
17945 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17946 complain, in_decl);
17947 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17948 complain, in_decl);
17949 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17950 complain, in_decl);
17951 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17952 outputs, inputs, clobbers, labels,
17953 ASM_INLINE_P (t));
17954 tree asm_expr = tmp;
17955 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17956 asm_expr = TREE_OPERAND (asm_expr, 0);
17957 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17958 }
17959 break;
17960
17961 case TRY_BLOCK:
17962 if (CLEANUP_P (t))
17963 {
17964 stmt = begin_try_block ();
17965 RECUR (TRY_STMTS (t));
17966 finish_cleanup_try_block (stmt);
17967 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17968 }
17969 else
17970 {
17971 tree compound_stmt = NULL_TREE;
17972
17973 if (FN_TRY_BLOCK_P (t))
17974 stmt = begin_function_try_block (&compound_stmt);
17975 else
17976 stmt = begin_try_block ();
17977
17978 RECUR (TRY_STMTS (t));
17979
17980 if (FN_TRY_BLOCK_P (t))
17981 finish_function_try_block (stmt);
17982 else
17983 finish_try_block (stmt);
17984
17985 RECUR (TRY_HANDLERS (t));
17986 if (FN_TRY_BLOCK_P (t))
17987 finish_function_handler_sequence (stmt, compound_stmt);
17988 else
17989 finish_handler_sequence (stmt);
17990 }
17991 break;
17992
17993 case HANDLER:
17994 {
17995 tree decl = HANDLER_PARMS (t);
17996
17997 if (decl)
17998 {
17999 decl = tsubst (decl, args, complain, in_decl);
18000 /* Prevent instantiate_decl from trying to instantiate
18001 this variable. We've already done all that needs to be
18002 done. */
18003 if (decl != error_mark_node)
18004 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18005 }
18006 stmt = begin_handler ();
18007 finish_handler_parms (decl, stmt);
18008 RECUR (HANDLER_BODY (t));
18009 finish_handler (stmt);
18010 }
18011 break;
18012
18013 case TAG_DEFN:
18014 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18015 if (CLASS_TYPE_P (tmp))
18016 {
18017 /* Local classes are not independent templates; they are
18018 instantiated along with their containing function. And this
18019 way we don't have to deal with pushing out of one local class
18020 to instantiate a member of another local class. */
18021 /* Closures are handled by the LAMBDA_EXPR. */
18022 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18023 complete_type (tmp);
18024 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18025 if ((VAR_P (fld)
18026 || (TREE_CODE (fld) == FUNCTION_DECL
18027 && !DECL_ARTIFICIAL (fld)))
18028 && DECL_TEMPLATE_INSTANTIATION (fld))
18029 instantiate_decl (fld, /*defer_ok=*/false,
18030 /*expl_inst_class=*/false);
18031 }
18032 break;
18033
18034 case STATIC_ASSERT:
18035 {
18036 tree condition;
18037
18038 ++c_inhibit_evaluation_warnings;
18039 condition =
18040 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18041 args,
18042 complain, in_decl,
18043 /*integral_constant_expression_p=*/true);
18044 --c_inhibit_evaluation_warnings;
18045
18046 finish_static_assert (condition,
18047 STATIC_ASSERT_MESSAGE (t),
18048 STATIC_ASSERT_SOURCE_LOCATION (t),
18049 /*member_p=*/false);
18050 }
18051 break;
18052
18053 case OACC_KERNELS:
18054 case OACC_PARALLEL:
18055 case OACC_SERIAL:
18056 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18057 in_decl);
18058 stmt = begin_omp_parallel ();
18059 RECUR (OMP_BODY (t));
18060 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18061 break;
18062
18063 case OMP_PARALLEL:
18064 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18065 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18066 complain, in_decl);
18067 if (OMP_PARALLEL_COMBINED (t))
18068 omp_parallel_combined_clauses = &tmp;
18069 stmt = begin_omp_parallel ();
18070 RECUR (OMP_PARALLEL_BODY (t));
18071 gcc_assert (omp_parallel_combined_clauses == NULL);
18072 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18073 = OMP_PARALLEL_COMBINED (t);
18074 pop_omp_privatization_clauses (r);
18075 break;
18076
18077 case OMP_TASK:
18078 if (OMP_TASK_BODY (t) == NULL_TREE)
18079 {
18080 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18081 complain, in_decl);
18082 t = copy_node (t);
18083 OMP_TASK_CLAUSES (t) = tmp;
18084 add_stmt (t);
18085 break;
18086 }
18087 r = push_omp_privatization_clauses (false);
18088 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18089 complain, in_decl);
18090 stmt = begin_omp_task ();
18091 RECUR (OMP_TASK_BODY (t));
18092 finish_omp_task (tmp, stmt);
18093 pop_omp_privatization_clauses (r);
18094 break;
18095
18096 case OMP_FOR:
18097 case OMP_LOOP:
18098 case OMP_SIMD:
18099 case OMP_DISTRIBUTE:
18100 case OMP_TASKLOOP:
18101 case OACC_LOOP:
18102 {
18103 tree clauses, body, pre_body;
18104 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18105 tree orig_declv = NULL_TREE;
18106 tree incrv = NULL_TREE;
18107 enum c_omp_region_type ort = C_ORT_OMP;
18108 bool any_range_for = false;
18109 int i;
18110
18111 if (TREE_CODE (t) == OACC_LOOP)
18112 ort = C_ORT_ACC;
18113
18114 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18115 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18116 in_decl);
18117 if (OMP_FOR_INIT (t) != NULL_TREE)
18118 {
18119 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18120 if (OMP_FOR_ORIG_DECLS (t))
18121 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18122 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18123 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18124 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18125 }
18126
18127 keep_next_level (true);
18128 stmt = begin_omp_structured_block ();
18129
18130 pre_body = push_stmt_list ();
18131 RECUR (OMP_FOR_PRE_BODY (t));
18132 pre_body = pop_stmt_list (pre_body);
18133
18134 if (OMP_FOR_INIT (t) != NULL_TREE)
18135 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18136 any_range_for
18137 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18138 condv, incrv, &clauses, args,
18139 complain, in_decl,
18140 integral_constant_expression_p);
18141 omp_parallel_combined_clauses = NULL;
18142
18143 if (any_range_for)
18144 {
18145 gcc_assert (orig_declv);
18146 body = begin_omp_structured_block ();
18147 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18148 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18149 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18150 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18151 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18152 TREE_VEC_ELT (declv, i));
18153 }
18154 else
18155 body = push_stmt_list ();
18156 RECUR (OMP_FOR_BODY (t));
18157 if (any_range_for)
18158 body = finish_omp_structured_block (body);
18159 else
18160 body = pop_stmt_list (body);
18161
18162 if (OMP_FOR_INIT (t) != NULL_TREE)
18163 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18164 orig_declv, initv, condv, incrv, body, pre_body,
18165 NULL, clauses);
18166 else
18167 {
18168 t = make_node (TREE_CODE (t));
18169 TREE_TYPE (t) = void_type_node;
18170 OMP_FOR_BODY (t) = body;
18171 OMP_FOR_PRE_BODY (t) = pre_body;
18172 OMP_FOR_CLAUSES (t) = clauses;
18173 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18174 add_stmt (t);
18175 }
18176
18177 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18178 t));
18179 pop_omp_privatization_clauses (r);
18180 }
18181 break;
18182
18183 case OMP_SECTIONS:
18184 omp_parallel_combined_clauses = NULL;
18185 /* FALLTHRU */
18186 case OMP_SINGLE:
18187 case OMP_TEAMS:
18188 case OMP_CRITICAL:
18189 case OMP_TASKGROUP:
18190 case OMP_SCAN:
18191 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18192 && OMP_TEAMS_COMBINED (t));
18193 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18194 in_decl);
18195 if (TREE_CODE (t) == OMP_TEAMS)
18196 {
18197 keep_next_level (true);
18198 stmt = begin_omp_structured_block ();
18199 RECUR (OMP_BODY (t));
18200 stmt = finish_omp_structured_block (stmt);
18201 }
18202 else
18203 {
18204 stmt = push_stmt_list ();
18205 RECUR (OMP_BODY (t));
18206 stmt = pop_stmt_list (stmt);
18207 }
18208
18209 t = copy_node (t);
18210 OMP_BODY (t) = stmt;
18211 OMP_CLAUSES (t) = tmp;
18212 add_stmt (t);
18213 pop_omp_privatization_clauses (r);
18214 break;
18215
18216 case OMP_DEPOBJ:
18217 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18218 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18219 {
18220 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18221 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18222 {
18223 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18224 args, complain, in_decl);
18225 if (tmp == NULL_TREE)
18226 tmp = error_mark_node;
18227 }
18228 else
18229 {
18230 kind = (enum omp_clause_depend_kind)
18231 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18232 tmp = NULL_TREE;
18233 }
18234 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18235 }
18236 else
18237 finish_omp_depobj (EXPR_LOCATION (t), r,
18238 OMP_CLAUSE_DEPEND_SOURCE,
18239 OMP_DEPOBJ_CLAUSES (t));
18240 break;
18241
18242 case OACC_DATA:
18243 case OMP_TARGET_DATA:
18244 case OMP_TARGET:
18245 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18246 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18247 in_decl);
18248 keep_next_level (true);
18249 stmt = begin_omp_structured_block ();
18250
18251 RECUR (OMP_BODY (t));
18252 stmt = finish_omp_structured_block (stmt);
18253
18254 t = copy_node (t);
18255 OMP_BODY (t) = stmt;
18256 OMP_CLAUSES (t) = tmp;
18257 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18258 {
18259 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18260 if (teams)
18261 {
18262 /* For combined target teams, ensure the num_teams and
18263 thread_limit clause expressions are evaluated on the host,
18264 before entering the target construct. */
18265 tree c;
18266 for (c = OMP_TEAMS_CLAUSES (teams);
18267 c; c = OMP_CLAUSE_CHAIN (c))
18268 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18269 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18270 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18271 {
18272 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18273 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18274 if (expr == error_mark_node)
18275 continue;
18276 tmp = TARGET_EXPR_SLOT (expr);
18277 add_stmt (expr);
18278 OMP_CLAUSE_OPERAND (c, 0) = expr;
18279 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18280 OMP_CLAUSE_FIRSTPRIVATE);
18281 OMP_CLAUSE_DECL (tc) = tmp;
18282 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18283 OMP_TARGET_CLAUSES (t) = tc;
18284 }
18285 }
18286 }
18287 add_stmt (t);
18288 break;
18289
18290 case OACC_DECLARE:
18291 t = copy_node (t);
18292 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18293 complain, in_decl);
18294 OACC_DECLARE_CLAUSES (t) = tmp;
18295 add_stmt (t);
18296 break;
18297
18298 case OMP_TARGET_UPDATE:
18299 case OMP_TARGET_ENTER_DATA:
18300 case OMP_TARGET_EXIT_DATA:
18301 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18302 complain, in_decl);
18303 t = copy_node (t);
18304 OMP_STANDALONE_CLAUSES (t) = tmp;
18305 add_stmt (t);
18306 break;
18307
18308 case OACC_ENTER_DATA:
18309 case OACC_EXIT_DATA:
18310 case OACC_UPDATE:
18311 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18312 complain, in_decl);
18313 t = copy_node (t);
18314 OMP_STANDALONE_CLAUSES (t) = tmp;
18315 add_stmt (t);
18316 break;
18317
18318 case OMP_ORDERED:
18319 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18320 complain, in_decl);
18321 stmt = push_stmt_list ();
18322 RECUR (OMP_BODY (t));
18323 stmt = pop_stmt_list (stmt);
18324
18325 t = copy_node (t);
18326 OMP_BODY (t) = stmt;
18327 OMP_ORDERED_CLAUSES (t) = tmp;
18328 add_stmt (t);
18329 break;
18330
18331 case OMP_SECTION:
18332 case OMP_MASTER:
18333 stmt = push_stmt_list ();
18334 RECUR (OMP_BODY (t));
18335 stmt = pop_stmt_list (stmt);
18336
18337 t = copy_node (t);
18338 OMP_BODY (t) = stmt;
18339 add_stmt (t);
18340 break;
18341
18342 case OMP_ATOMIC:
18343 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18344 tmp = NULL_TREE;
18345 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18346 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18347 complain, in_decl);
18348 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18349 {
18350 tree op1 = TREE_OPERAND (t, 1);
18351 tree rhs1 = NULL_TREE;
18352 tree lhs, rhs;
18353 if (TREE_CODE (op1) == COMPOUND_EXPR)
18354 {
18355 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18356 op1 = TREE_OPERAND (op1, 1);
18357 }
18358 lhs = RECUR (TREE_OPERAND (op1, 0));
18359 rhs = RECUR (TREE_OPERAND (op1, 1));
18360 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18361 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18362 OMP_ATOMIC_MEMORY_ORDER (t));
18363 }
18364 else
18365 {
18366 tree op1 = TREE_OPERAND (t, 1);
18367 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18368 tree rhs1 = NULL_TREE;
18369 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18370 enum tree_code opcode = NOP_EXPR;
18371 if (code == OMP_ATOMIC_READ)
18372 {
18373 v = RECUR (TREE_OPERAND (op1, 0));
18374 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18375 }
18376 else if (code == OMP_ATOMIC_CAPTURE_OLD
18377 || code == OMP_ATOMIC_CAPTURE_NEW)
18378 {
18379 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18380 v = RECUR (TREE_OPERAND (op1, 0));
18381 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18382 if (TREE_CODE (op11) == COMPOUND_EXPR)
18383 {
18384 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18385 op11 = TREE_OPERAND (op11, 1);
18386 }
18387 lhs = RECUR (TREE_OPERAND (op11, 0));
18388 rhs = RECUR (TREE_OPERAND (op11, 1));
18389 opcode = TREE_CODE (op11);
18390 if (opcode == MODIFY_EXPR)
18391 opcode = NOP_EXPR;
18392 }
18393 else
18394 {
18395 code = OMP_ATOMIC;
18396 lhs = RECUR (TREE_OPERAND (op1, 0));
18397 rhs = RECUR (TREE_OPERAND (op1, 1));
18398 }
18399 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18400 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18401 }
18402 break;
18403
18404 case TRANSACTION_EXPR:
18405 {
18406 int flags = 0;
18407 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18408 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18409
18410 if (TRANSACTION_EXPR_IS_STMT (t))
18411 {
18412 tree body = TRANSACTION_EXPR_BODY (t);
18413 tree noex = NULL_TREE;
18414 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18415 {
18416 noex = MUST_NOT_THROW_COND (body);
18417 if (noex == NULL_TREE)
18418 noex = boolean_true_node;
18419 body = TREE_OPERAND (body, 0);
18420 }
18421 stmt = begin_transaction_stmt (input_location, NULL, flags);
18422 RECUR (body);
18423 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18424 }
18425 else
18426 {
18427 stmt = build_transaction_expr (EXPR_LOCATION (t),
18428 RECUR (TRANSACTION_EXPR_BODY (t)),
18429 flags, NULL_TREE);
18430 RETURN (stmt);
18431 }
18432 }
18433 break;
18434
18435 case MUST_NOT_THROW_EXPR:
18436 {
18437 tree op0 = RECUR (TREE_OPERAND (t, 0));
18438 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18439 RETURN (build_must_not_throw_expr (op0, cond));
18440 }
18441
18442 case EXPR_PACK_EXPANSION:
18443 error ("invalid use of pack expansion expression");
18444 RETURN (error_mark_node);
18445
18446 case NONTYPE_ARGUMENT_PACK:
18447 error ("use %<...%> to expand argument pack");
18448 RETURN (error_mark_node);
18449
18450 case COMPOUND_EXPR:
18451 tmp = RECUR (TREE_OPERAND (t, 0));
18452 if (tmp == NULL_TREE)
18453 /* If the first operand was a statement, we're done with it. */
18454 RETURN (RECUR (TREE_OPERAND (t, 1)));
18455 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18456 RECUR (TREE_OPERAND (t, 1)),
18457 complain));
18458
18459 case ANNOTATE_EXPR:
18460 tmp = RECUR (TREE_OPERAND (t, 0));
18461 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18462 TREE_TYPE (tmp), tmp,
18463 RECUR (TREE_OPERAND (t, 1)),
18464 RECUR (TREE_OPERAND (t, 2))));
18465
18466 case PREDICT_EXPR:
18467 RETURN (add_stmt (copy_node (t)));
18468
18469 default:
18470 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18471
18472 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18473 /*function_p=*/false,
18474 integral_constant_expression_p));
18475 }
18476
18477 RETURN (NULL_TREE);
18478 out:
18479 input_location = loc;
18480 return r;
18481 #undef RECUR
18482 #undef RETURN
18483 }
18484
18485 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18486 function. For description of the body see comment above
18487 cp_parser_omp_declare_reduction_exprs. */
18488
18489 static void
18490 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18491 {
18492 if (t == NULL_TREE || t == error_mark_node)
18493 return;
18494
18495 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18496
18497 tree_stmt_iterator tsi;
18498 int i;
18499 tree stmts[7];
18500 memset (stmts, 0, sizeof stmts);
18501 for (i = 0, tsi = tsi_start (t);
18502 i < 7 && !tsi_end_p (tsi);
18503 i++, tsi_next (&tsi))
18504 stmts[i] = tsi_stmt (tsi);
18505 gcc_assert (tsi_end_p (tsi));
18506
18507 if (i >= 3)
18508 {
18509 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18510 && TREE_CODE (stmts[1]) == DECL_EXPR);
18511 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18512 args, complain, in_decl);
18513 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18514 args, complain, in_decl);
18515 DECL_CONTEXT (omp_out) = current_function_decl;
18516 DECL_CONTEXT (omp_in) = current_function_decl;
18517 keep_next_level (true);
18518 tree block = begin_omp_structured_block ();
18519 tsubst_expr (stmts[2], args, complain, in_decl, false);
18520 block = finish_omp_structured_block (block);
18521 block = maybe_cleanup_point_expr_void (block);
18522 add_decl_expr (omp_out);
18523 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18524 TREE_NO_WARNING (omp_out) = 1;
18525 add_decl_expr (omp_in);
18526 finish_expr_stmt (block);
18527 }
18528 if (i >= 6)
18529 {
18530 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18531 && TREE_CODE (stmts[4]) == DECL_EXPR);
18532 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18533 args, complain, in_decl);
18534 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18535 args, complain, in_decl);
18536 DECL_CONTEXT (omp_priv) = current_function_decl;
18537 DECL_CONTEXT (omp_orig) = current_function_decl;
18538 keep_next_level (true);
18539 tree block = begin_omp_structured_block ();
18540 tsubst_expr (stmts[5], args, complain, in_decl, false);
18541 block = finish_omp_structured_block (block);
18542 block = maybe_cleanup_point_expr_void (block);
18543 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18544 add_decl_expr (omp_priv);
18545 add_decl_expr (omp_orig);
18546 finish_expr_stmt (block);
18547 if (i == 7)
18548 add_decl_expr (omp_orig);
18549 }
18550 }
18551
18552 /* T is a postfix-expression that is not being used in a function
18553 call. Return the substituted version of T. */
18554
18555 static tree
18556 tsubst_non_call_postfix_expression (tree t, tree args,
18557 tsubst_flags_t complain,
18558 tree in_decl)
18559 {
18560 if (TREE_CODE (t) == SCOPE_REF)
18561 t = tsubst_qualified_id (t, args, complain, in_decl,
18562 /*done=*/false, /*address_p=*/false);
18563 else
18564 t = tsubst_copy_and_build (t, args, complain, in_decl,
18565 /*function_p=*/false,
18566 /*integral_constant_expression_p=*/false);
18567
18568 return t;
18569 }
18570
18571 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18572 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18573 dependent init-capture. */
18574
18575 static void
18576 prepend_one_capture (tree field, tree init, tree &list,
18577 tsubst_flags_t complain)
18578 {
18579 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18580 {
18581 tree type = NULL_TREE;
18582 if (!init)
18583 {
18584 if (complain & tf_error)
18585 error ("empty initializer in lambda init-capture");
18586 init = error_mark_node;
18587 }
18588 else if (TREE_CODE (init) == TREE_LIST)
18589 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18590 if (!type)
18591 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18592 TREE_TYPE (field) = type;
18593 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18594 }
18595 list = tree_cons (field, init, list);
18596 }
18597
18598 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18599 instantiation context. Instantiating a pack expansion containing a lambda
18600 might result in multiple lambdas all based on the same lambda in the
18601 template. */
18602
18603 tree
18604 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18605 {
18606 tree oldfn = lambda_function (t);
18607 in_decl = oldfn;
18608
18609 tree r = build_lambda_expr ();
18610
18611 LAMBDA_EXPR_LOCATION (r)
18612 = LAMBDA_EXPR_LOCATION (t);
18613 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18614 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18615 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18616 LAMBDA_EXPR_INSTANTIATED (r) = true;
18617
18618 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18619 /* A lambda in a default argument outside a class gets no
18620 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18621 tsubst_default_argument calls start_lambda_scope, so we need to
18622 specifically ignore it here, and use the global scope. */
18623 record_null_lambda_scope (r);
18624 else
18625 record_lambda_scope (r);
18626
18627 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18628 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18629
18630 vec<tree,va_gc>* field_packs = NULL;
18631
18632 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18633 cap = TREE_CHAIN (cap))
18634 {
18635 tree ofield = TREE_PURPOSE (cap);
18636 if (PACK_EXPANSION_P (ofield))
18637 ofield = PACK_EXPANSION_PATTERN (ofield);
18638 tree field = tsubst_decl (ofield, args, complain);
18639
18640 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18641 {
18642 /* Remember these for when we've pushed local_specializations. */
18643 vec_safe_push (field_packs, ofield);
18644 vec_safe_push (field_packs, field);
18645 }
18646
18647 if (field == error_mark_node)
18648 return error_mark_node;
18649
18650 tree init = TREE_VALUE (cap);
18651 if (PACK_EXPANSION_P (init))
18652 init = tsubst_pack_expansion (init, args, complain, in_decl);
18653 else
18654 init = tsubst_copy_and_build (init, args, complain, in_decl,
18655 /*fn*/false, /*constexpr*/false);
18656
18657 if (TREE_CODE (field) == TREE_VEC)
18658 {
18659 int len = TREE_VEC_LENGTH (field);
18660 gcc_assert (TREE_CODE (init) == TREE_VEC
18661 && TREE_VEC_LENGTH (init) == len);
18662 for (int i = 0; i < len; ++i)
18663 prepend_one_capture (TREE_VEC_ELT (field, i),
18664 TREE_VEC_ELT (init, i),
18665 LAMBDA_EXPR_CAPTURE_LIST (r),
18666 complain);
18667 }
18668 else
18669 {
18670 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18671 complain);
18672
18673 if (id_equal (DECL_NAME (field), "__this"))
18674 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18675 }
18676 }
18677
18678 tree type = begin_lambda_type (r);
18679 if (type == error_mark_node)
18680 return error_mark_node;
18681
18682 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18683 determine_visibility (TYPE_NAME (type));
18684
18685 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18686
18687 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18688 ? DECL_TI_TEMPLATE (oldfn)
18689 : NULL_TREE);
18690
18691 tree fntype = static_fn_type (oldfn);
18692 if (oldtmpl)
18693 ++processing_template_decl;
18694 fntype = tsubst (fntype, args, complain, in_decl);
18695 if (oldtmpl)
18696 --processing_template_decl;
18697
18698 if (fntype == error_mark_node)
18699 r = error_mark_node;
18700 else
18701 {
18702 /* The body of a lambda-expression is not a subexpression of the
18703 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18704 which would be skipped if cp_unevaluated_operand. */
18705 cp_evaluated ev;
18706
18707 /* Fix the type of 'this'. */
18708 fntype = build_memfn_type (fntype, type,
18709 type_memfn_quals (fntype),
18710 type_memfn_rqual (fntype));
18711 tree fn, tmpl;
18712 if (oldtmpl)
18713 {
18714 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18715 fn = DECL_TEMPLATE_RESULT (tmpl);
18716 finish_member_declaration (tmpl);
18717 }
18718 else
18719 {
18720 tmpl = NULL_TREE;
18721 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18722 finish_member_declaration (fn);
18723 }
18724
18725 /* Let finish_function set this. */
18726 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18727
18728 bool nested = cfun;
18729 if (nested)
18730 push_function_context ();
18731 else
18732 /* Still increment function_depth so that we don't GC in the
18733 middle of an expression. */
18734 ++function_depth;
18735
18736 local_specialization_stack s (lss_copy);
18737
18738 tree body = start_lambda_function (fn, r);
18739
18740 /* Now record them for lookup_init_capture_pack. */
18741 int fplen = vec_safe_length (field_packs);
18742 for (int i = 0; i < fplen; )
18743 {
18744 tree pack = (*field_packs)[i++];
18745 tree inst = (*field_packs)[i++];
18746 register_local_specialization (inst, pack);
18747 }
18748 release_tree_vector (field_packs);
18749
18750 register_parameter_specializations (oldfn, fn);
18751
18752 if (oldtmpl)
18753 {
18754 /* We might not partially instantiate some parts of the function, so
18755 copy these flags from the original template. */
18756 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18757 current_function_returns_value = ol->returns_value;
18758 current_function_returns_null = ol->returns_null;
18759 current_function_returns_abnormally = ol->returns_abnormally;
18760 current_function_infinite_loop = ol->infinite_loop;
18761 }
18762
18763 /* [temp.deduct] A lambda-expression appearing in a function type or a
18764 template parameter is not considered part of the immediate context for
18765 the purposes of template argument deduction. */
18766 complain = tf_warning_or_error;
18767
18768 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18769 /*constexpr*/false);
18770
18771 finish_lambda_function (body);
18772
18773 if (nested)
18774 pop_function_context ();
18775 else
18776 --function_depth;
18777
18778 /* The capture list was built up in reverse order; fix that now. */
18779 LAMBDA_EXPR_CAPTURE_LIST (r)
18780 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18781
18782 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18783
18784 maybe_add_lambda_conv_op (type);
18785 }
18786
18787 finish_struct (type, /*attr*/NULL_TREE);
18788
18789 insert_pending_capture_proxies ();
18790
18791 return r;
18792 }
18793
18794 /* Like tsubst but deals with expressions and performs semantic
18795 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18796
18797 tree
18798 tsubst_copy_and_build (tree t,
18799 tree args,
18800 tsubst_flags_t complain,
18801 tree in_decl,
18802 bool function_p,
18803 bool integral_constant_expression_p)
18804 {
18805 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18806 #define RECUR(NODE) \
18807 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18808 /*function_p=*/false, \
18809 integral_constant_expression_p)
18810
18811 tree retval, op1;
18812 location_t loc;
18813
18814 if (t == NULL_TREE || t == error_mark_node)
18815 return t;
18816
18817 loc = input_location;
18818 if (location_t eloc = cp_expr_location (t))
18819 input_location = eloc;
18820
18821 /* N3276 decltype magic only applies to calls at the top level or on the
18822 right side of a comma. */
18823 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18824 complain &= ~tf_decltype;
18825
18826 switch (TREE_CODE (t))
18827 {
18828 case USING_DECL:
18829 t = DECL_NAME (t);
18830 /* Fall through. */
18831 case IDENTIFIER_NODE:
18832 {
18833 tree decl;
18834 cp_id_kind idk;
18835 bool non_integral_constant_expression_p;
18836 const char *error_msg;
18837
18838 if (IDENTIFIER_CONV_OP_P (t))
18839 {
18840 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18841 t = make_conv_op_name (new_type);
18842 }
18843
18844 /* Look up the name. */
18845 decl = lookup_name (t);
18846
18847 /* By convention, expressions use ERROR_MARK_NODE to indicate
18848 failure, not NULL_TREE. */
18849 if (decl == NULL_TREE)
18850 decl = error_mark_node;
18851
18852 decl = finish_id_expression (t, decl, NULL_TREE,
18853 &idk,
18854 integral_constant_expression_p,
18855 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18856 &non_integral_constant_expression_p,
18857 /*template_p=*/false,
18858 /*done=*/true,
18859 /*address_p=*/false,
18860 /*template_arg_p=*/false,
18861 &error_msg,
18862 input_location);
18863 if (error_msg)
18864 error (error_msg);
18865 if (!function_p && identifier_p (decl))
18866 {
18867 if (complain & tf_error)
18868 unqualified_name_lookup_error (decl);
18869 decl = error_mark_node;
18870 }
18871 RETURN (decl);
18872 }
18873
18874 case TEMPLATE_ID_EXPR:
18875 {
18876 tree object;
18877 tree templ = RECUR (TREE_OPERAND (t, 0));
18878 tree targs = TREE_OPERAND (t, 1);
18879
18880 if (targs)
18881 targs = tsubst_template_args (targs, args, complain, in_decl);
18882 if (targs == error_mark_node)
18883 RETURN (error_mark_node);
18884
18885 if (TREE_CODE (templ) == SCOPE_REF)
18886 {
18887 tree name = TREE_OPERAND (templ, 1);
18888 tree tid = lookup_template_function (name, targs);
18889 TREE_OPERAND (templ, 1) = tid;
18890 RETURN (templ);
18891 }
18892
18893 if (concept_definition_p (templ))
18894 {
18895 tree check = build_concept_check (templ, targs, complain);
18896 if (check == error_mark_node)
18897 RETURN (error_mark_node);
18898
18899 tree id = unpack_concept_check (check);
18900
18901 /* If we built a function concept check, return the underlying
18902 template-id. So we can evaluate it as a function call. */
18903 if (function_concept_p (TREE_OPERAND (id, 0)))
18904 RETURN (id);
18905
18906 RETURN (check);
18907 }
18908
18909 if (variable_template_p (templ))
18910 {
18911 tree r = lookup_and_finish_template_variable (templ, targs,
18912 complain);
18913 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
18914 RETURN (r);
18915 }
18916
18917 if (TREE_CODE (templ) == COMPONENT_REF)
18918 {
18919 object = TREE_OPERAND (templ, 0);
18920 templ = TREE_OPERAND (templ, 1);
18921 }
18922 else
18923 object = NULL_TREE;
18924 templ = lookup_template_function (templ, targs);
18925
18926 if (object)
18927 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18928 object, templ, NULL_TREE));
18929 else
18930 RETURN (baselink_for_fns (templ));
18931 }
18932
18933 case INDIRECT_REF:
18934 {
18935 tree r = RECUR (TREE_OPERAND (t, 0));
18936
18937 if (REFERENCE_REF_P (t))
18938 {
18939 /* A type conversion to reference type will be enclosed in
18940 such an indirect ref, but the substitution of the cast
18941 will have also added such an indirect ref. */
18942 r = convert_from_reference (r);
18943 }
18944 else
18945 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18946 complain|decltype_flag);
18947
18948 if (REF_PARENTHESIZED_P (t))
18949 r = force_paren_expr (r);
18950
18951 RETURN (r);
18952 }
18953
18954 case NOP_EXPR:
18955 {
18956 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18957 tree op0 = RECUR (TREE_OPERAND (t, 0));
18958 RETURN (build_nop (type, op0));
18959 }
18960
18961 case IMPLICIT_CONV_EXPR:
18962 {
18963 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18964 tree expr = RECUR (TREE_OPERAND (t, 0));
18965 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18966 {
18967 retval = copy_node (t);
18968 TREE_TYPE (retval) = type;
18969 TREE_OPERAND (retval, 0) = expr;
18970 RETURN (retval);
18971 }
18972 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18973 /* We'll pass this to convert_nontype_argument again, we don't need
18974 to actually perform any conversion here. */
18975 RETURN (expr);
18976 int flags = LOOKUP_IMPLICIT;
18977 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18978 flags = LOOKUP_NORMAL;
18979 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18980 flags |= LOOKUP_NO_NARROWING;
18981 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18982 flags));
18983 }
18984
18985 case CONVERT_EXPR:
18986 {
18987 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18988 tree op0 = RECUR (TREE_OPERAND (t, 0));
18989 if (op0 == error_mark_node)
18990 RETURN (error_mark_node);
18991 RETURN (build1 (CONVERT_EXPR, type, op0));
18992 }
18993
18994 case CAST_EXPR:
18995 case REINTERPRET_CAST_EXPR:
18996 case CONST_CAST_EXPR:
18997 case DYNAMIC_CAST_EXPR:
18998 case STATIC_CAST_EXPR:
18999 {
19000 tree type;
19001 tree op, r = NULL_TREE;
19002
19003 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19004 if (integral_constant_expression_p
19005 && !cast_valid_in_integral_constant_expression_p (type))
19006 {
19007 if (complain & tf_error)
19008 error ("a cast to a type other than an integral or "
19009 "enumeration type cannot appear in a constant-expression");
19010 RETURN (error_mark_node);
19011 }
19012
19013 op = RECUR (TREE_OPERAND (t, 0));
19014
19015 warning_sentinel s(warn_useless_cast);
19016 warning_sentinel s2(warn_ignored_qualifiers);
19017 switch (TREE_CODE (t))
19018 {
19019 case CAST_EXPR:
19020 r = build_functional_cast (type, op, complain);
19021 break;
19022 case REINTERPRET_CAST_EXPR:
19023 r = build_reinterpret_cast (type, op, complain);
19024 break;
19025 case CONST_CAST_EXPR:
19026 r = build_const_cast (type, op, complain);
19027 break;
19028 case DYNAMIC_CAST_EXPR:
19029 r = build_dynamic_cast (type, op, complain);
19030 break;
19031 case STATIC_CAST_EXPR:
19032 r = build_static_cast (type, op, complain);
19033 break;
19034 default:
19035 gcc_unreachable ();
19036 }
19037
19038 RETURN (r);
19039 }
19040
19041 case POSTDECREMENT_EXPR:
19042 case POSTINCREMENT_EXPR:
19043 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19044 args, complain, in_decl);
19045 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19046 complain|decltype_flag));
19047
19048 case PREDECREMENT_EXPR:
19049 case PREINCREMENT_EXPR:
19050 case NEGATE_EXPR:
19051 case BIT_NOT_EXPR:
19052 case ABS_EXPR:
19053 case TRUTH_NOT_EXPR:
19054 case UNARY_PLUS_EXPR: /* Unary + */
19055 case REALPART_EXPR:
19056 case IMAGPART_EXPR:
19057 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19058 RECUR (TREE_OPERAND (t, 0)),
19059 complain|decltype_flag));
19060
19061 case FIX_TRUNC_EXPR:
19062 gcc_unreachable ();
19063
19064 case ADDR_EXPR:
19065 op1 = TREE_OPERAND (t, 0);
19066 if (TREE_CODE (op1) == LABEL_DECL)
19067 RETURN (finish_label_address_expr (DECL_NAME (op1),
19068 EXPR_LOCATION (op1)));
19069 if (TREE_CODE (op1) == SCOPE_REF)
19070 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19071 /*done=*/true, /*address_p=*/true);
19072 else
19073 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19074 in_decl);
19075 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19076 complain|decltype_flag));
19077
19078 case PLUS_EXPR:
19079 case MINUS_EXPR:
19080 case MULT_EXPR:
19081 case TRUNC_DIV_EXPR:
19082 case CEIL_DIV_EXPR:
19083 case FLOOR_DIV_EXPR:
19084 case ROUND_DIV_EXPR:
19085 case EXACT_DIV_EXPR:
19086 case BIT_AND_EXPR:
19087 case BIT_IOR_EXPR:
19088 case BIT_XOR_EXPR:
19089 case TRUNC_MOD_EXPR:
19090 case FLOOR_MOD_EXPR:
19091 case TRUTH_ANDIF_EXPR:
19092 case TRUTH_ORIF_EXPR:
19093 case TRUTH_AND_EXPR:
19094 case TRUTH_OR_EXPR:
19095 case RSHIFT_EXPR:
19096 case LSHIFT_EXPR:
19097 case EQ_EXPR:
19098 case NE_EXPR:
19099 case MAX_EXPR:
19100 case MIN_EXPR:
19101 case LE_EXPR:
19102 case GE_EXPR:
19103 case LT_EXPR:
19104 case GT_EXPR:
19105 case SPACESHIP_EXPR:
19106 case MEMBER_REF:
19107 case DOTSTAR_EXPR:
19108 {
19109 warning_sentinel s1(warn_type_limits);
19110 warning_sentinel s2(warn_div_by_zero);
19111 warning_sentinel s3(warn_logical_op);
19112 warning_sentinel s4(warn_tautological_compare);
19113 tree op0 = RECUR (TREE_OPERAND (t, 0));
19114 tree op1 = RECUR (TREE_OPERAND (t, 1));
19115 tree r = build_x_binary_op
19116 (input_location, TREE_CODE (t),
19117 op0,
19118 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19119 ? ERROR_MARK
19120 : TREE_CODE (TREE_OPERAND (t, 0))),
19121 op1,
19122 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19123 ? ERROR_MARK
19124 : TREE_CODE (TREE_OPERAND (t, 1))),
19125 /*overload=*/NULL,
19126 complain|decltype_flag);
19127 if (EXPR_P (r) && TREE_NO_WARNING (t))
19128 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19129
19130 RETURN (r);
19131 }
19132
19133 case POINTER_PLUS_EXPR:
19134 {
19135 tree op0 = RECUR (TREE_OPERAND (t, 0));
19136 tree op1 = RECUR (TREE_OPERAND (t, 1));
19137 RETURN (fold_build_pointer_plus (op0, op1));
19138 }
19139
19140 case SCOPE_REF:
19141 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19142 /*address_p=*/false));
19143 case ARRAY_REF:
19144 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19145 args, complain, in_decl);
19146 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19147 RECUR (TREE_OPERAND (t, 1)),
19148 complain|decltype_flag));
19149
19150 case SIZEOF_EXPR:
19151 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19152 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19153 RETURN (tsubst_copy (t, args, complain, in_decl));
19154 /* Fall through */
19155
19156 case ALIGNOF_EXPR:
19157 {
19158 tree r;
19159
19160 op1 = TREE_OPERAND (t, 0);
19161 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19162 op1 = TREE_TYPE (op1);
19163 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19164 && ALIGNOF_EXPR_STD_P (t));
19165 if (!args)
19166 {
19167 /* When there are no ARGS, we are trying to evaluate a
19168 non-dependent expression from the parser. Trying to do
19169 the substitutions may not work. */
19170 if (!TYPE_P (op1))
19171 op1 = TREE_TYPE (op1);
19172 }
19173 else
19174 {
19175 ++cp_unevaluated_operand;
19176 ++c_inhibit_evaluation_warnings;
19177 if (TYPE_P (op1))
19178 op1 = tsubst (op1, args, complain, in_decl);
19179 else
19180 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19181 /*function_p=*/false,
19182 /*integral_constant_expression_p=*/
19183 false);
19184 --cp_unevaluated_operand;
19185 --c_inhibit_evaluation_warnings;
19186 }
19187 if (TYPE_P (op1))
19188 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
19189 complain & tf_error);
19190 else
19191 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
19192 complain & tf_error);
19193 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19194 {
19195 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19196 {
19197 if (!processing_template_decl && TYPE_P (op1))
19198 {
19199 r = build_min (SIZEOF_EXPR, size_type_node,
19200 build1 (NOP_EXPR, op1, error_mark_node));
19201 SIZEOF_EXPR_TYPE_P (r) = 1;
19202 }
19203 else
19204 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19205 TREE_SIDE_EFFECTS (r) = 0;
19206 TREE_READONLY (r) = 1;
19207 }
19208 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19209 }
19210 RETURN (r);
19211 }
19212
19213 case AT_ENCODE_EXPR:
19214 {
19215 op1 = TREE_OPERAND (t, 0);
19216 ++cp_unevaluated_operand;
19217 ++c_inhibit_evaluation_warnings;
19218 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19219 /*function_p=*/false,
19220 /*integral_constant_expression_p=*/false);
19221 --cp_unevaluated_operand;
19222 --c_inhibit_evaluation_warnings;
19223 RETURN (objc_build_encode_expr (op1));
19224 }
19225
19226 case NOEXCEPT_EXPR:
19227 op1 = TREE_OPERAND (t, 0);
19228 ++cp_unevaluated_operand;
19229 ++c_inhibit_evaluation_warnings;
19230 ++cp_noexcept_operand;
19231 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19232 /*function_p=*/false,
19233 /*integral_constant_expression_p=*/false);
19234 --cp_unevaluated_operand;
19235 --c_inhibit_evaluation_warnings;
19236 --cp_noexcept_operand;
19237 RETURN (finish_noexcept_expr (op1, complain));
19238
19239 case MODOP_EXPR:
19240 {
19241 warning_sentinel s(warn_div_by_zero);
19242 tree lhs = RECUR (TREE_OPERAND (t, 0));
19243 tree rhs = RECUR (TREE_OPERAND (t, 2));
19244 tree r = build_x_modify_expr
19245 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19246 complain|decltype_flag);
19247 /* TREE_NO_WARNING must be set if either the expression was
19248 parenthesized or it uses an operator such as >>= rather
19249 than plain assignment. In the former case, it was already
19250 set and must be copied. In the latter case,
19251 build_x_modify_expr sets it and it must not be reset
19252 here. */
19253 if (TREE_NO_WARNING (t))
19254 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19255
19256 RETURN (r);
19257 }
19258
19259 case ARROW_EXPR:
19260 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19261 args, complain, in_decl);
19262 /* Remember that there was a reference to this entity. */
19263 if (DECL_P (op1)
19264 && !mark_used (op1, complain) && !(complain & tf_error))
19265 RETURN (error_mark_node);
19266 RETURN (build_x_arrow (input_location, op1, complain));
19267
19268 case NEW_EXPR:
19269 {
19270 tree placement = RECUR (TREE_OPERAND (t, 0));
19271 tree init = RECUR (TREE_OPERAND (t, 3));
19272 vec<tree, va_gc> *placement_vec;
19273 vec<tree, va_gc> *init_vec;
19274 tree ret;
19275
19276 if (placement == NULL_TREE)
19277 placement_vec = NULL;
19278 else
19279 {
19280 placement_vec = make_tree_vector ();
19281 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19282 vec_safe_push (placement_vec, TREE_VALUE (placement));
19283 }
19284
19285 /* If there was an initializer in the original tree, but it
19286 instantiated to an empty list, then we should pass a
19287 non-NULL empty vector to tell build_new that it was an
19288 empty initializer() rather than no initializer. This can
19289 only happen when the initializer is a pack expansion whose
19290 parameter packs are of length zero. */
19291 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19292 init_vec = NULL;
19293 else
19294 {
19295 init_vec = make_tree_vector ();
19296 if (init == void_node)
19297 gcc_assert (init_vec != NULL);
19298 else
19299 {
19300 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19301 vec_safe_push (init_vec, TREE_VALUE (init));
19302 }
19303 }
19304
19305 /* Avoid passing an enclosing decl to valid_array_size_p. */
19306 in_decl = NULL_TREE;
19307
19308 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19309 tree op2 = RECUR (TREE_OPERAND (t, 2));
19310 ret = build_new (&placement_vec, op1, op2, &init_vec,
19311 NEW_EXPR_USE_GLOBAL (t),
19312 complain);
19313
19314 if (placement_vec != NULL)
19315 release_tree_vector (placement_vec);
19316 if (init_vec != NULL)
19317 release_tree_vector (init_vec);
19318
19319 RETURN (ret);
19320 }
19321
19322 case DELETE_EXPR:
19323 {
19324 tree op0 = RECUR (TREE_OPERAND (t, 0));
19325 tree op1 = RECUR (TREE_OPERAND (t, 1));
19326 RETURN (delete_sanity (op0, op1,
19327 DELETE_EXPR_USE_VEC (t),
19328 DELETE_EXPR_USE_GLOBAL (t),
19329 complain));
19330 }
19331
19332 case COMPOUND_EXPR:
19333 {
19334 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19335 complain & ~tf_decltype, in_decl,
19336 /*function_p=*/false,
19337 integral_constant_expression_p);
19338 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19339 op0,
19340 RECUR (TREE_OPERAND (t, 1)),
19341 complain|decltype_flag));
19342 }
19343
19344 case CALL_EXPR:
19345 {
19346 tree function;
19347 unsigned int nargs, i;
19348 bool qualified_p;
19349 bool koenig_p;
19350 tree ret;
19351
19352 function = CALL_EXPR_FN (t);
19353 /* Internal function with no arguments. */
19354 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19355 RETURN (t);
19356
19357 /* When we parsed the expression, we determined whether or
19358 not Koenig lookup should be performed. */
19359 koenig_p = KOENIG_LOOKUP_P (t);
19360 if (function == NULL_TREE)
19361 {
19362 koenig_p = false;
19363 qualified_p = false;
19364 }
19365 else if (TREE_CODE (function) == SCOPE_REF)
19366 {
19367 qualified_p = true;
19368 function = tsubst_qualified_id (function, args, complain, in_decl,
19369 /*done=*/false,
19370 /*address_p=*/false);
19371 }
19372 else if (koenig_p && identifier_p (function))
19373 {
19374 /* Do nothing; calling tsubst_copy_and_build on an identifier
19375 would incorrectly perform unqualified lookup again.
19376
19377 Note that we can also have an IDENTIFIER_NODE if the earlier
19378 unqualified lookup found a member function; in that case
19379 koenig_p will be false and we do want to do the lookup
19380 again to find the instantiated member function.
19381
19382 FIXME but doing that causes c++/15272, so we need to stop
19383 using IDENTIFIER_NODE in that situation. */
19384 qualified_p = false;
19385 }
19386 else
19387 {
19388 if (TREE_CODE (function) == COMPONENT_REF)
19389 {
19390 tree op = TREE_OPERAND (function, 1);
19391
19392 qualified_p = (TREE_CODE (op) == SCOPE_REF
19393 || (BASELINK_P (op)
19394 && BASELINK_QUALIFIED_P (op)));
19395 }
19396 else
19397 qualified_p = false;
19398
19399 if (TREE_CODE (function) == ADDR_EXPR
19400 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19401 /* Avoid error about taking the address of a constructor. */
19402 function = TREE_OPERAND (function, 0);
19403
19404 function = tsubst_copy_and_build (function, args, complain,
19405 in_decl,
19406 !qualified_p,
19407 integral_constant_expression_p);
19408
19409 if (BASELINK_P (function))
19410 qualified_p = true;
19411 }
19412
19413 nargs = call_expr_nargs (t);
19414 releasing_vec call_args;
19415 for (i = 0; i < nargs; ++i)
19416 {
19417 tree arg = CALL_EXPR_ARG (t, i);
19418
19419 if (!PACK_EXPANSION_P (arg))
19420 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19421 else
19422 {
19423 /* Expand the pack expansion and push each entry onto
19424 CALL_ARGS. */
19425 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19426 if (TREE_CODE (arg) == TREE_VEC)
19427 {
19428 unsigned int len, j;
19429
19430 len = TREE_VEC_LENGTH (arg);
19431 for (j = 0; j < len; ++j)
19432 {
19433 tree value = TREE_VEC_ELT (arg, j);
19434 if (value != NULL_TREE)
19435 value = convert_from_reference (value);
19436 vec_safe_push (call_args, value);
19437 }
19438 }
19439 else
19440 {
19441 /* A partial substitution. Add one entry. */
19442 vec_safe_push (call_args, arg);
19443 }
19444 }
19445 }
19446
19447 /* Stripped-down processing for a call in a thunk. Specifically, in
19448 the thunk template for a generic lambda. */
19449 if (CALL_FROM_THUNK_P (t))
19450 {
19451 /* Now that we've expanded any packs, the number of call args
19452 might be different. */
19453 unsigned int cargs = call_args->length ();
19454 tree thisarg = NULL_TREE;
19455 if (TREE_CODE (function) == COMPONENT_REF)
19456 {
19457 thisarg = TREE_OPERAND (function, 0);
19458 if (TREE_CODE (thisarg) == INDIRECT_REF)
19459 thisarg = TREE_OPERAND (thisarg, 0);
19460 function = TREE_OPERAND (function, 1);
19461 if (TREE_CODE (function) == BASELINK)
19462 function = BASELINK_FUNCTIONS (function);
19463 }
19464 /* We aren't going to do normal overload resolution, so force the
19465 template-id to resolve. */
19466 function = resolve_nondeduced_context (function, complain);
19467 for (unsigned i = 0; i < cargs; ++i)
19468 {
19469 /* In a thunk, pass through args directly, without any
19470 conversions. */
19471 tree arg = (*call_args)[i];
19472 while (TREE_CODE (arg) != PARM_DECL)
19473 arg = TREE_OPERAND (arg, 0);
19474 (*call_args)[i] = arg;
19475 }
19476 if (thisarg)
19477 {
19478 /* If there are no other args, just push 'this'. */
19479 if (cargs == 0)
19480 vec_safe_push (call_args, thisarg);
19481 else
19482 {
19483 /* Otherwise, shift the other args over to make room. */
19484 tree last = (*call_args)[cargs - 1];
19485 vec_safe_push (call_args, last);
19486 for (int i = cargs - 1; i > 0; --i)
19487 (*call_args)[i] = (*call_args)[i - 1];
19488 (*call_args)[0] = thisarg;
19489 }
19490 }
19491 ret = build_call_a (function, call_args->length (),
19492 call_args->address ());
19493 /* The thunk location is not interesting. */
19494 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19495 CALL_FROM_THUNK_P (ret) = true;
19496 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19497 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19498
19499 RETURN (ret);
19500 }
19501
19502 /* We do not perform argument-dependent lookup if normal
19503 lookup finds a non-function, in accordance with the
19504 expected resolution of DR 218. */
19505 if (koenig_p
19506 && ((is_overloaded_fn (function)
19507 /* If lookup found a member function, the Koenig lookup is
19508 not appropriate, even if an unqualified-name was used
19509 to denote the function. */
19510 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19511 || identifier_p (function))
19512 /* Only do this when substitution turns a dependent call
19513 into a non-dependent call. */
19514 && type_dependent_expression_p_push (t)
19515 && !any_type_dependent_arguments_p (call_args))
19516 function = perform_koenig_lookup (function, call_args, tf_none);
19517
19518 if (function != NULL_TREE
19519 && identifier_p (function)
19520 && !any_type_dependent_arguments_p (call_args))
19521 {
19522 if (koenig_p && (complain & tf_warning_or_error))
19523 {
19524 /* For backwards compatibility and good diagnostics, try
19525 the unqualified lookup again if we aren't in SFINAE
19526 context. */
19527 tree unq = (tsubst_copy_and_build
19528 (function, args, complain, in_decl, true,
19529 integral_constant_expression_p));
19530 if (unq == error_mark_node)
19531 RETURN (error_mark_node);
19532
19533 if (unq != function)
19534 {
19535 /* In a lambda fn, we have to be careful to not
19536 introduce new this captures. Legacy code can't
19537 be using lambdas anyway, so it's ok to be
19538 stricter. */
19539 bool in_lambda = (current_class_type
19540 && LAMBDA_TYPE_P (current_class_type));
19541 char const *const msg
19542 = G_("%qD was not declared in this scope, "
19543 "and no declarations were found by "
19544 "argument-dependent lookup at the point "
19545 "of instantiation");
19546
19547 bool diag = true;
19548 if (in_lambda)
19549 error_at (cp_expr_loc_or_input_loc (t),
19550 msg, function);
19551 else
19552 diag = permerror (cp_expr_loc_or_input_loc (t),
19553 msg, function);
19554 if (diag)
19555 {
19556 tree fn = unq;
19557
19558 if (INDIRECT_REF_P (fn))
19559 fn = TREE_OPERAND (fn, 0);
19560 if (is_overloaded_fn (fn))
19561 fn = get_first_fn (fn);
19562
19563 if (!DECL_P (fn))
19564 /* Can't say anything more. */;
19565 else if (DECL_CLASS_SCOPE_P (fn))
19566 {
19567 location_t loc = cp_expr_loc_or_input_loc (t);
19568 inform (loc,
19569 "declarations in dependent base %qT are "
19570 "not found by unqualified lookup",
19571 DECL_CLASS_CONTEXT (fn));
19572 if (current_class_ptr)
19573 inform (loc,
19574 "use %<this->%D%> instead", function);
19575 else
19576 inform (loc,
19577 "use %<%T::%D%> instead",
19578 current_class_name, function);
19579 }
19580 else
19581 inform (DECL_SOURCE_LOCATION (fn),
19582 "%qD declared here, later in the "
19583 "translation unit", fn);
19584 if (in_lambda)
19585 RETURN (error_mark_node);
19586 }
19587
19588 function = unq;
19589 }
19590 }
19591 if (identifier_p (function))
19592 {
19593 if (complain & tf_error)
19594 unqualified_name_lookup_error (function);
19595 RETURN (error_mark_node);
19596 }
19597 }
19598
19599 /* Remember that there was a reference to this entity. */
19600 if (function != NULL_TREE
19601 && DECL_P (function)
19602 && !mark_used (function, complain) && !(complain & tf_error))
19603 RETURN (error_mark_node);
19604
19605 /* Put back tf_decltype for the actual call. */
19606 complain |= decltype_flag;
19607
19608 if (function == NULL_TREE)
19609 switch (CALL_EXPR_IFN (t))
19610 {
19611 case IFN_LAUNDER:
19612 gcc_assert (nargs == 1);
19613 if (vec_safe_length (call_args) != 1)
19614 {
19615 error_at (cp_expr_loc_or_input_loc (t),
19616 "wrong number of arguments to "
19617 "%<__builtin_launder%>");
19618 ret = error_mark_node;
19619 }
19620 else
19621 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19622 (*call_args)[0], complain);
19623 break;
19624
19625 case IFN_VEC_CONVERT:
19626 gcc_assert (nargs == 1);
19627 if (vec_safe_length (call_args) != 1)
19628 {
19629 error_at (cp_expr_loc_or_input_loc (t),
19630 "wrong number of arguments to "
19631 "%<__builtin_convertvector%>");
19632 ret = error_mark_node;
19633 break;
19634 }
19635 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19636 tsubst (TREE_TYPE (t), args,
19637 complain, in_decl),
19638 complain);
19639 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19640 RETURN (ret);
19641 break;
19642
19643 default:
19644 /* Unsupported internal function with arguments. */
19645 gcc_unreachable ();
19646 }
19647 else if (TREE_CODE (function) == OFFSET_REF
19648 || TREE_CODE (function) == DOTSTAR_EXPR
19649 || TREE_CODE (function) == MEMBER_REF)
19650 ret = build_offset_ref_call_from_tree (function, &call_args,
19651 complain);
19652 else if (TREE_CODE (function) == COMPONENT_REF)
19653 {
19654 tree instance = TREE_OPERAND (function, 0);
19655 tree fn = TREE_OPERAND (function, 1);
19656
19657 if (processing_template_decl
19658 && (type_dependent_expression_p (instance)
19659 || (!BASELINK_P (fn)
19660 && TREE_CODE (fn) != FIELD_DECL)
19661 || type_dependent_expression_p (fn)
19662 || any_type_dependent_arguments_p (call_args)))
19663 ret = build_min_nt_call_vec (function, call_args);
19664 else if (!BASELINK_P (fn))
19665 ret = finish_call_expr (function, &call_args,
19666 /*disallow_virtual=*/false,
19667 /*koenig_p=*/false,
19668 complain);
19669 else
19670 ret = (build_new_method_call
19671 (instance, fn,
19672 &call_args, NULL_TREE,
19673 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19674 /*fn_p=*/NULL,
19675 complain));
19676 }
19677 else if (concept_check_p (function))
19678 {
19679 /* FUNCTION is a template-id referring to a concept definition. */
19680 tree id = unpack_concept_check (function);
19681 tree tmpl = TREE_OPERAND (id, 0);
19682 tree args = TREE_OPERAND (id, 1);
19683
19684 /* Calls to standard and variable concepts should have been
19685 previously diagnosed. */
19686 gcc_assert (function_concept_p (tmpl));
19687
19688 /* Ensure the result is wrapped as a call expression. */
19689 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19690 }
19691 else
19692 ret = finish_call_expr (function, &call_args,
19693 /*disallow_virtual=*/qualified_p,
19694 koenig_p,
19695 complain);
19696
19697 if (ret != error_mark_node)
19698 {
19699 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19700 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19701 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19702 if (op || ord || rev)
19703 {
19704 function = extract_call_expr (ret);
19705 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19706 CALL_EXPR_ORDERED_ARGS (function) = ord;
19707 CALL_EXPR_REVERSE_ARGS (function) = rev;
19708 }
19709 }
19710
19711 RETURN (ret);
19712 }
19713
19714 case COND_EXPR:
19715 {
19716 tree cond = RECUR (TREE_OPERAND (t, 0));
19717 cond = mark_rvalue_use (cond);
19718 tree folded_cond = fold_non_dependent_expr (cond, complain);
19719 tree exp1, exp2;
19720
19721 if (TREE_CODE (folded_cond) == INTEGER_CST)
19722 {
19723 if (integer_zerop (folded_cond))
19724 {
19725 ++c_inhibit_evaluation_warnings;
19726 exp1 = RECUR (TREE_OPERAND (t, 1));
19727 --c_inhibit_evaluation_warnings;
19728 exp2 = RECUR (TREE_OPERAND (t, 2));
19729 }
19730 else
19731 {
19732 exp1 = RECUR (TREE_OPERAND (t, 1));
19733 ++c_inhibit_evaluation_warnings;
19734 exp2 = RECUR (TREE_OPERAND (t, 2));
19735 --c_inhibit_evaluation_warnings;
19736 }
19737 cond = folded_cond;
19738 }
19739 else
19740 {
19741 exp1 = RECUR (TREE_OPERAND (t, 1));
19742 exp2 = RECUR (TREE_OPERAND (t, 2));
19743 }
19744
19745 warning_sentinel s(warn_duplicated_branches);
19746 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19747 cond, exp1, exp2, complain));
19748 }
19749
19750 case PSEUDO_DTOR_EXPR:
19751 {
19752 tree op0 = RECUR (TREE_OPERAND (t, 0));
19753 tree op1 = RECUR (TREE_OPERAND (t, 1));
19754 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19755 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19756 input_location));
19757 }
19758
19759 case TREE_LIST:
19760 {
19761 tree purpose, value, chain;
19762
19763 if (t == void_list_node)
19764 RETURN (t);
19765
19766 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19767 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19768 {
19769 /* We have pack expansions, so expand those and
19770 create a new list out of it. */
19771 tree purposevec = NULL_TREE;
19772 tree valuevec = NULL_TREE;
19773 tree chain;
19774 int i, len = -1;
19775
19776 /* Expand the argument expressions. */
19777 if (TREE_PURPOSE (t))
19778 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19779 complain, in_decl);
19780 if (TREE_VALUE (t))
19781 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19782 complain, in_decl);
19783
19784 /* Build the rest of the list. */
19785 chain = TREE_CHAIN (t);
19786 if (chain && chain != void_type_node)
19787 chain = RECUR (chain);
19788
19789 /* Determine the number of arguments. */
19790 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19791 {
19792 len = TREE_VEC_LENGTH (purposevec);
19793 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19794 }
19795 else if (TREE_CODE (valuevec) == TREE_VEC)
19796 len = TREE_VEC_LENGTH (valuevec);
19797 else
19798 {
19799 /* Since we only performed a partial substitution into
19800 the argument pack, we only RETURN (a single list
19801 node. */
19802 if (purposevec == TREE_PURPOSE (t)
19803 && valuevec == TREE_VALUE (t)
19804 && chain == TREE_CHAIN (t))
19805 RETURN (t);
19806
19807 RETURN (tree_cons (purposevec, valuevec, chain));
19808 }
19809
19810 /* Convert the argument vectors into a TREE_LIST */
19811 i = len;
19812 while (i > 0)
19813 {
19814 /* Grab the Ith values. */
19815 i--;
19816 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19817 : NULL_TREE;
19818 value
19819 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19820 : NULL_TREE;
19821
19822 /* Build the list (backwards). */
19823 chain = tree_cons (purpose, value, chain);
19824 }
19825
19826 RETURN (chain);
19827 }
19828
19829 purpose = TREE_PURPOSE (t);
19830 if (purpose)
19831 purpose = RECUR (purpose);
19832 value = TREE_VALUE (t);
19833 if (value)
19834 value = RECUR (value);
19835 chain = TREE_CHAIN (t);
19836 if (chain && chain != void_type_node)
19837 chain = RECUR (chain);
19838 if (purpose == TREE_PURPOSE (t)
19839 && value == TREE_VALUE (t)
19840 && chain == TREE_CHAIN (t))
19841 RETURN (t);
19842 RETURN (tree_cons (purpose, value, chain));
19843 }
19844
19845 case COMPONENT_REF:
19846 {
19847 tree object;
19848 tree object_type;
19849 tree member;
19850 tree r;
19851
19852 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19853 args, complain, in_decl);
19854 /* Remember that there was a reference to this entity. */
19855 if (DECL_P (object)
19856 && !mark_used (object, complain) && !(complain & tf_error))
19857 RETURN (error_mark_node);
19858 object_type = TREE_TYPE (object);
19859
19860 member = TREE_OPERAND (t, 1);
19861 if (BASELINK_P (member))
19862 member = tsubst_baselink (member,
19863 non_reference (TREE_TYPE (object)),
19864 args, complain, in_decl);
19865 else
19866 member = tsubst_copy (member, args, complain, in_decl);
19867 if (member == error_mark_node)
19868 RETURN (error_mark_node);
19869
19870 if (TREE_CODE (member) == FIELD_DECL)
19871 {
19872 r = finish_non_static_data_member (member, object, NULL_TREE);
19873 if (TREE_CODE (r) == COMPONENT_REF)
19874 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19875 RETURN (r);
19876 }
19877 else if (type_dependent_expression_p (object))
19878 /* We can't do much here. */;
19879 else if (!CLASS_TYPE_P (object_type))
19880 {
19881 if (scalarish_type_p (object_type))
19882 {
19883 tree s = NULL_TREE;
19884 tree dtor = member;
19885
19886 if (TREE_CODE (dtor) == SCOPE_REF)
19887 {
19888 s = TREE_OPERAND (dtor, 0);
19889 dtor = TREE_OPERAND (dtor, 1);
19890 }
19891 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19892 {
19893 dtor = TREE_OPERAND (dtor, 0);
19894 if (TYPE_P (dtor))
19895 RETURN (finish_pseudo_destructor_expr
19896 (object, s, dtor, input_location));
19897 }
19898 }
19899 }
19900 else if (TREE_CODE (member) == SCOPE_REF
19901 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19902 {
19903 /* Lookup the template functions now that we know what the
19904 scope is. */
19905 tree scope = TREE_OPERAND (member, 0);
19906 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19907 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19908 member = lookup_qualified_name (scope, tmpl,
19909 /*is_type_p=*/false,
19910 /*complain=*/false);
19911 if (BASELINK_P (member))
19912 {
19913 BASELINK_FUNCTIONS (member)
19914 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19915 args);
19916 member = (adjust_result_of_qualified_name_lookup
19917 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19918 object_type));
19919 }
19920 else
19921 {
19922 qualified_name_lookup_error (scope, tmpl, member,
19923 input_location);
19924 RETURN (error_mark_node);
19925 }
19926 }
19927 else if (TREE_CODE (member) == SCOPE_REF
19928 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19929 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19930 {
19931 if (complain & tf_error)
19932 {
19933 if (TYPE_P (TREE_OPERAND (member, 0)))
19934 error ("%qT is not a class or namespace",
19935 TREE_OPERAND (member, 0));
19936 else
19937 error ("%qD is not a class or namespace",
19938 TREE_OPERAND (member, 0));
19939 }
19940 RETURN (error_mark_node);
19941 }
19942
19943 r = finish_class_member_access_expr (object, member,
19944 /*template_p=*/false,
19945 complain);
19946 if (TREE_CODE (r) == COMPONENT_REF)
19947 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19948 RETURN (r);
19949 }
19950
19951 case THROW_EXPR:
19952 RETURN (build_throw
19953 (RECUR (TREE_OPERAND (t, 0))));
19954
19955 case CONSTRUCTOR:
19956 {
19957 vec<constructor_elt, va_gc> *n;
19958 constructor_elt *ce;
19959 unsigned HOST_WIDE_INT idx;
19960 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19961 bool process_index_p;
19962 int newlen;
19963 bool need_copy_p = false;
19964 tree r;
19965
19966 if (type == error_mark_node)
19967 RETURN (error_mark_node);
19968
19969 /* We do not want to process the index of aggregate
19970 initializers as they are identifier nodes which will be
19971 looked up by digest_init. */
19972 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19973
19974 if (null_member_pointer_value_p (t))
19975 {
19976 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19977 RETURN (t);
19978 }
19979
19980 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19981 newlen = vec_safe_length (n);
19982 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19983 {
19984 if (ce->index && process_index_p
19985 /* An identifier index is looked up in the type
19986 being initialized, not the current scope. */
19987 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19988 ce->index = RECUR (ce->index);
19989
19990 if (PACK_EXPANSION_P (ce->value))
19991 {
19992 /* Substitute into the pack expansion. */
19993 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19994 in_decl);
19995
19996 if (ce->value == error_mark_node
19997 || PACK_EXPANSION_P (ce->value))
19998 ;
19999 else if (TREE_VEC_LENGTH (ce->value) == 1)
20000 /* Just move the argument into place. */
20001 ce->value = TREE_VEC_ELT (ce->value, 0);
20002 else
20003 {
20004 /* Update the length of the final CONSTRUCTOR
20005 arguments vector, and note that we will need to
20006 copy.*/
20007 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20008 need_copy_p = true;
20009 }
20010 }
20011 else
20012 ce->value = RECUR (ce->value);
20013 }
20014
20015 if (need_copy_p)
20016 {
20017 vec<constructor_elt, va_gc> *old_n = n;
20018
20019 vec_alloc (n, newlen);
20020 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20021 {
20022 if (TREE_CODE (ce->value) == TREE_VEC)
20023 {
20024 int i, len = TREE_VEC_LENGTH (ce->value);
20025 for (i = 0; i < len; ++i)
20026 CONSTRUCTOR_APPEND_ELT (n, 0,
20027 TREE_VEC_ELT (ce->value, i));
20028 }
20029 else
20030 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20031 }
20032 }
20033
20034 r = build_constructor (init_list_type_node, n);
20035 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20036 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20037 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20038
20039 if (TREE_HAS_CONSTRUCTOR (t))
20040 {
20041 fcl_t cl = fcl_functional;
20042 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20043 cl = fcl_c99;
20044 RETURN (finish_compound_literal (type, r, complain, cl));
20045 }
20046
20047 TREE_TYPE (r) = type;
20048 RETURN (r);
20049 }
20050
20051 case TYPEID_EXPR:
20052 {
20053 tree operand_0 = TREE_OPERAND (t, 0);
20054 if (TYPE_P (operand_0))
20055 {
20056 operand_0 = tsubst (operand_0, args, complain, in_decl);
20057 RETURN (get_typeid (operand_0, complain));
20058 }
20059 else
20060 {
20061 operand_0 = RECUR (operand_0);
20062 RETURN (build_typeid (operand_0, complain));
20063 }
20064 }
20065
20066 case VAR_DECL:
20067 if (!args)
20068 RETURN (t);
20069 /* Fall through */
20070
20071 case PARM_DECL:
20072 {
20073 tree r = tsubst_copy (t, args, complain, in_decl);
20074 /* ??? We're doing a subset of finish_id_expression here. */
20075 if (tree wrap = maybe_get_tls_wrapper_call (r))
20076 /* Replace an evaluated use of the thread_local variable with
20077 a call to its wrapper. */
20078 r = wrap;
20079 else if (outer_automatic_var_p (r))
20080 r = process_outer_var_ref (r, complain);
20081
20082 if (!TYPE_REF_P (TREE_TYPE (t)))
20083 /* If the original type was a reference, we'll be wrapped in
20084 the appropriate INDIRECT_REF. */
20085 r = convert_from_reference (r);
20086 RETURN (r);
20087 }
20088
20089 case VA_ARG_EXPR:
20090 {
20091 tree op0 = RECUR (TREE_OPERAND (t, 0));
20092 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20093 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20094 }
20095
20096 case OFFSETOF_EXPR:
20097 {
20098 tree object_ptr
20099 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20100 in_decl, /*function_p=*/false,
20101 /*integral_constant_expression_p=*/false);
20102 RETURN (finish_offsetof (object_ptr,
20103 RECUR (TREE_OPERAND (t, 0)),
20104 EXPR_LOCATION (t)));
20105 }
20106
20107 case ADDRESSOF_EXPR:
20108 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20109 RECUR (TREE_OPERAND (t, 0)), complain));
20110
20111 case TRAIT_EXPR:
20112 {
20113 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20114 complain, in_decl);
20115
20116 tree type2 = TRAIT_EXPR_TYPE2 (t);
20117 if (type2 && TREE_CODE (type2) == TREE_LIST)
20118 type2 = RECUR (type2);
20119 else if (type2)
20120 type2 = tsubst (type2, args, complain, in_decl);
20121
20122 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20123 TRAIT_EXPR_KIND (t), type1, type2));
20124 }
20125
20126 case STMT_EXPR:
20127 {
20128 tree old_stmt_expr = cur_stmt_expr;
20129 tree stmt_expr = begin_stmt_expr ();
20130
20131 cur_stmt_expr = stmt_expr;
20132 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20133 integral_constant_expression_p);
20134 stmt_expr = finish_stmt_expr (stmt_expr, false);
20135 cur_stmt_expr = old_stmt_expr;
20136
20137 /* If the resulting list of expression statement is empty,
20138 fold it further into void_node. */
20139 if (empty_expr_stmt_p (stmt_expr))
20140 stmt_expr = void_node;
20141
20142 RETURN (stmt_expr);
20143 }
20144
20145 case LAMBDA_EXPR:
20146 {
20147 if (complain & tf_partial)
20148 {
20149 /* We don't have a full set of template arguments yet; don't touch
20150 the lambda at all. */
20151 gcc_assert (processing_template_decl);
20152 return t;
20153 }
20154 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20155
20156 RETURN (build_lambda_object (r));
20157 }
20158
20159 case TARGET_EXPR:
20160 /* We can get here for a constant initializer of non-dependent type.
20161 FIXME stop folding in cp_parser_initializer_clause. */
20162 {
20163 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20164 complain);
20165 RETURN (r);
20166 }
20167
20168 case TRANSACTION_EXPR:
20169 RETURN (tsubst_expr(t, args, complain, in_decl,
20170 integral_constant_expression_p));
20171
20172 case PAREN_EXPR:
20173 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20174
20175 case VEC_PERM_EXPR:
20176 {
20177 tree op0 = RECUR (TREE_OPERAND (t, 0));
20178 tree op1 = RECUR (TREE_OPERAND (t, 1));
20179 tree op2 = RECUR (TREE_OPERAND (t, 2));
20180 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20181 complain));
20182 }
20183
20184 case REQUIRES_EXPR:
20185 {
20186 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20187 if (r == error_mark_node && (complain & tf_error))
20188 tsubst_requires_expr (t, args, complain, in_decl);
20189 RETURN (r);
20190 }
20191
20192 case RANGE_EXPR:
20193 /* No need to substitute further, a RANGE_EXPR will always be built
20194 with constant operands. */
20195 RETURN (t);
20196
20197 case NON_LVALUE_EXPR:
20198 case VIEW_CONVERT_EXPR:
20199 if (location_wrapper_p (t))
20200 /* We need to do this here as well as in tsubst_copy so we get the
20201 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20202 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20203 EXPR_LOCATION (t)));
20204 /* fallthrough. */
20205
20206 default:
20207 /* Handle Objective-C++ constructs, if appropriate. */
20208 {
20209 tree subst
20210 = objcp_tsubst_copy_and_build (t, args, complain,
20211 in_decl, /*function_p=*/false);
20212 if (subst)
20213 RETURN (subst);
20214 }
20215 RETURN (tsubst_copy (t, args, complain, in_decl));
20216 }
20217
20218 #undef RECUR
20219 #undef RETURN
20220 out:
20221 input_location = loc;
20222 return retval;
20223 }
20224
20225 /* Verify that the instantiated ARGS are valid. For type arguments,
20226 make sure that the type's linkage is ok. For non-type arguments,
20227 make sure they are constants if they are integral or enumerations.
20228 Emit an error under control of COMPLAIN, and return TRUE on error. */
20229
20230 static bool
20231 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20232 {
20233 if (dependent_template_arg_p (t))
20234 return false;
20235 if (ARGUMENT_PACK_P (t))
20236 {
20237 tree vec = ARGUMENT_PACK_ARGS (t);
20238 int len = TREE_VEC_LENGTH (vec);
20239 bool result = false;
20240 int i;
20241
20242 for (i = 0; i < len; ++i)
20243 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20244 result = true;
20245 return result;
20246 }
20247 else if (TYPE_P (t))
20248 {
20249 /* [basic.link]: A name with no linkage (notably, the name
20250 of a class or enumeration declared in a local scope)
20251 shall not be used to declare an entity with linkage.
20252 This implies that names with no linkage cannot be used as
20253 template arguments
20254
20255 DR 757 relaxes this restriction for C++0x. */
20256 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20257 : no_linkage_check (t, /*relaxed_p=*/false));
20258
20259 if (nt)
20260 {
20261 /* DR 488 makes use of a type with no linkage cause
20262 type deduction to fail. */
20263 if (complain & tf_error)
20264 {
20265 if (TYPE_UNNAMED_P (nt))
20266 error ("%qT is/uses unnamed type", t);
20267 else
20268 error ("template argument for %qD uses local type %qT",
20269 tmpl, t);
20270 }
20271 return true;
20272 }
20273 /* In order to avoid all sorts of complications, we do not
20274 allow variably-modified types as template arguments. */
20275 else if (variably_modified_type_p (t, NULL_TREE))
20276 {
20277 if (complain & tf_error)
20278 error ("%qT is a variably modified type", t);
20279 return true;
20280 }
20281 }
20282 /* Class template and alias template arguments should be OK. */
20283 else if (DECL_TYPE_TEMPLATE_P (t))
20284 ;
20285 /* A non-type argument of integral or enumerated type must be a
20286 constant. */
20287 else if (TREE_TYPE (t)
20288 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20289 && !REFERENCE_REF_P (t)
20290 && !TREE_CONSTANT (t))
20291 {
20292 if (complain & tf_error)
20293 error ("integral expression %qE is not constant", t);
20294 return true;
20295 }
20296 return false;
20297 }
20298
20299 static bool
20300 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20301 {
20302 int ix, len = DECL_NTPARMS (tmpl);
20303 bool result = false;
20304
20305 for (ix = 0; ix != len; ix++)
20306 {
20307 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20308 result = true;
20309 }
20310 if (result && (complain & tf_error))
20311 error (" trying to instantiate %qD", tmpl);
20312 return result;
20313 }
20314
20315 /* We're out of SFINAE context now, so generate diagnostics for the access
20316 errors we saw earlier when instantiating D from TMPL and ARGS. */
20317
20318 static void
20319 recheck_decl_substitution (tree d, tree tmpl, tree args)
20320 {
20321 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20322 tree type = TREE_TYPE (pattern);
20323 location_t loc = input_location;
20324
20325 push_access_scope (d);
20326 push_deferring_access_checks (dk_no_deferred);
20327 input_location = DECL_SOURCE_LOCATION (pattern);
20328 tsubst (type, args, tf_warning_or_error, d);
20329 input_location = loc;
20330 pop_deferring_access_checks ();
20331 pop_access_scope (d);
20332 }
20333
20334 /* Instantiate the indicated variable, function, or alias template TMPL with
20335 the template arguments in TARG_PTR. */
20336
20337 static tree
20338 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20339 {
20340 tree targ_ptr = orig_args;
20341 tree fndecl;
20342 tree gen_tmpl;
20343 tree spec;
20344 bool access_ok = true;
20345
20346 if (tmpl == error_mark_node)
20347 return error_mark_node;
20348
20349 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20350
20351 /* If this function is a clone, handle it specially. */
20352 if (DECL_CLONED_FUNCTION_P (tmpl))
20353 {
20354 tree spec;
20355 tree clone;
20356
20357 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20358 DECL_CLONED_FUNCTION. */
20359 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20360 targ_ptr, complain);
20361 if (spec == error_mark_node)
20362 return error_mark_node;
20363
20364 /* Look for the clone. */
20365 FOR_EACH_CLONE (clone, spec)
20366 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20367 return clone;
20368 /* We should always have found the clone by now. */
20369 gcc_unreachable ();
20370 return NULL_TREE;
20371 }
20372
20373 if (targ_ptr == error_mark_node)
20374 return error_mark_node;
20375
20376 /* Check to see if we already have this specialization. */
20377 gen_tmpl = most_general_template (tmpl);
20378 if (TMPL_ARGS_DEPTH (targ_ptr)
20379 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20380 /* targ_ptr only has the innermost template args, so add the outer ones
20381 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20382 the case of a non-dependent call within a template definition). */
20383 targ_ptr = (add_outermost_template_args
20384 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20385 targ_ptr));
20386
20387 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20388 but it doesn't seem to be on the hot path. */
20389 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20390
20391 gcc_assert (tmpl == gen_tmpl
20392 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20393 == spec)
20394 || fndecl == NULL_TREE);
20395
20396 if (spec != NULL_TREE)
20397 {
20398 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20399 {
20400 if (complain & tf_error)
20401 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20402 return error_mark_node;
20403 }
20404 return spec;
20405 }
20406
20407 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20408 complain))
20409 return error_mark_node;
20410
20411 /* We are building a FUNCTION_DECL, during which the access of its
20412 parameters and return types have to be checked. However this
20413 FUNCTION_DECL which is the desired context for access checking
20414 is not built yet. We solve this chicken-and-egg problem by
20415 deferring all checks until we have the FUNCTION_DECL. */
20416 push_deferring_access_checks (dk_deferred);
20417
20418 /* Instantiation of the function happens in the context of the function
20419 template, not the context of the overload resolution we're doing. */
20420 push_to_top_level ();
20421 /* If there are dependent arguments, e.g. because we're doing partial
20422 ordering, make sure processing_template_decl stays set. */
20423 if (uses_template_parms (targ_ptr))
20424 ++processing_template_decl;
20425 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20426 {
20427 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20428 complain, gen_tmpl, true);
20429 push_nested_class (ctx);
20430 }
20431
20432 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20433
20434 fndecl = NULL_TREE;
20435 if (VAR_P (pattern))
20436 {
20437 /* We need to determine if we're using a partial or explicit
20438 specialization now, because the type of the variable could be
20439 different. */
20440 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20441 tree elt = most_specialized_partial_spec (tid, complain);
20442 if (elt == error_mark_node)
20443 pattern = error_mark_node;
20444 else if (elt)
20445 {
20446 tree partial_tmpl = TREE_VALUE (elt);
20447 tree partial_args = TREE_PURPOSE (elt);
20448 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20449 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20450 }
20451 }
20452
20453 /* Substitute template parameters to obtain the specialization. */
20454 if (fndecl == NULL_TREE)
20455 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20456 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20457 pop_nested_class ();
20458 pop_from_top_level ();
20459
20460 if (fndecl == error_mark_node)
20461 {
20462 pop_deferring_access_checks ();
20463 return error_mark_node;
20464 }
20465
20466 /* The DECL_TI_TEMPLATE should always be the immediate parent
20467 template, not the most general template. */
20468 DECL_TI_TEMPLATE (fndecl) = tmpl;
20469 DECL_TI_ARGS (fndecl) = targ_ptr;
20470
20471 /* Now we know the specialization, compute access previously
20472 deferred. Do no access control for inheriting constructors,
20473 as we already checked access for the inherited constructor. */
20474 if (!(flag_new_inheriting_ctors
20475 && DECL_INHERITED_CTOR (fndecl)))
20476 {
20477 push_access_scope (fndecl);
20478 if (!perform_deferred_access_checks (complain))
20479 access_ok = false;
20480 pop_access_scope (fndecl);
20481 }
20482 pop_deferring_access_checks ();
20483
20484 /* If we've just instantiated the main entry point for a function,
20485 instantiate all the alternate entry points as well. We do this
20486 by cloning the instantiation of the main entry point, not by
20487 instantiating the template clones. */
20488 if (tree chain = DECL_CHAIN (gen_tmpl))
20489 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20490 clone_function_decl (fndecl, /*update_methods=*/false);
20491
20492 if (!access_ok)
20493 {
20494 if (!(complain & tf_error))
20495 {
20496 /* Remember to reinstantiate when we're out of SFINAE so the user
20497 can see the errors. */
20498 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20499 }
20500 return error_mark_node;
20501 }
20502 return fndecl;
20503 }
20504
20505 /* Wrapper for instantiate_template_1. */
20506
20507 tree
20508 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20509 {
20510 tree ret;
20511 timevar_push (TV_TEMPLATE_INST);
20512 ret = instantiate_template_1 (tmpl, orig_args, complain);
20513 timevar_pop (TV_TEMPLATE_INST);
20514 return ret;
20515 }
20516
20517 /* Instantiate the alias template TMPL with ARGS. Also push a template
20518 instantiation level, which instantiate_template doesn't do because
20519 functions and variables have sufficient context established by the
20520 callers. */
20521
20522 static tree
20523 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20524 {
20525 if (tmpl == error_mark_node || args == error_mark_node)
20526 return error_mark_node;
20527
20528 args =
20529 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20530 args, tmpl, complain,
20531 /*require_all_args=*/true,
20532 /*use_default_args=*/true);
20533
20534 /* FIXME check for satisfaction in check_instantiated_args. */
20535 if (flag_concepts
20536 && !any_dependent_template_arguments_p (args)
20537 && !constraints_satisfied_p (tmpl, args))
20538 {
20539 if (complain & tf_error)
20540 {
20541 auto_diagnostic_group d;
20542 error ("template constraint failure for %qD", tmpl);
20543 diagnose_constraints (input_location, tmpl, args);
20544 }
20545 return error_mark_node;
20546 }
20547
20548 if (!push_tinst_level (tmpl, args))
20549 return error_mark_node;
20550 tree r = instantiate_template (tmpl, args, complain);
20551 pop_tinst_level ();
20552
20553 return r;
20554 }
20555
20556 /* PARM is a template parameter pack for FN. Returns true iff
20557 PARM is used in a deducible way in the argument list of FN. */
20558
20559 static bool
20560 pack_deducible_p (tree parm, tree fn)
20561 {
20562 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20563 for (; t; t = TREE_CHAIN (t))
20564 {
20565 tree type = TREE_VALUE (t);
20566 tree packs;
20567 if (!PACK_EXPANSION_P (type))
20568 continue;
20569 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20570 packs; packs = TREE_CHAIN (packs))
20571 if (template_args_equal (TREE_VALUE (packs), parm))
20572 {
20573 /* The template parameter pack is used in a function parameter
20574 pack. If this is the end of the parameter list, the
20575 template parameter pack is deducible. */
20576 if (TREE_CHAIN (t) == void_list_node)
20577 return true;
20578 else
20579 /* Otherwise, not. Well, it could be deduced from
20580 a non-pack parameter, but doing so would end up with
20581 a deduction mismatch, so don't bother. */
20582 return false;
20583 }
20584 }
20585 /* The template parameter pack isn't used in any function parameter
20586 packs, but it might be used deeper, e.g. tuple<Args...>. */
20587 return true;
20588 }
20589
20590 /* Subroutine of fn_type_unification: check non-dependent parms for
20591 convertibility. */
20592
20593 static int
20594 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20595 tree fn, unification_kind_t strict, int flags,
20596 struct conversion **convs, bool explain_p)
20597 {
20598 /* Non-constructor methods need to leave a conversion for 'this', which
20599 isn't included in nargs here. */
20600 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20601 && !DECL_CONSTRUCTOR_P (fn));
20602
20603 for (unsigned ia = 0;
20604 parms && parms != void_list_node && ia < nargs; )
20605 {
20606 tree parm = TREE_VALUE (parms);
20607
20608 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20609 && (!TREE_CHAIN (parms)
20610 || TREE_CHAIN (parms) == void_list_node))
20611 /* For a function parameter pack that occurs at the end of the
20612 parameter-declaration-list, the type A of each remaining
20613 argument of the call is compared with the type P of the
20614 declarator-id of the function parameter pack. */
20615 break;
20616
20617 parms = TREE_CHAIN (parms);
20618
20619 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20620 /* For a function parameter pack that does not occur at the
20621 end of the parameter-declaration-list, the type of the
20622 parameter pack is a non-deduced context. */
20623 continue;
20624
20625 if (!uses_template_parms (parm))
20626 {
20627 tree arg = args[ia];
20628 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20629 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20630
20631 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20632 conv_p, explain_p))
20633 return 1;
20634 }
20635
20636 ++ia;
20637 }
20638
20639 return 0;
20640 }
20641
20642 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20643 NARGS elements of the arguments that are being used when calling
20644 it. TARGS is a vector into which the deduced template arguments
20645 are placed.
20646
20647 Returns either a FUNCTION_DECL for the matching specialization of FN or
20648 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20649 true, diagnostics will be printed to explain why it failed.
20650
20651 If FN is a conversion operator, or we are trying to produce a specific
20652 specialization, RETURN_TYPE is the return type desired.
20653
20654 The EXPLICIT_TARGS are explicit template arguments provided via a
20655 template-id.
20656
20657 The parameter STRICT is one of:
20658
20659 DEDUCE_CALL:
20660 We are deducing arguments for a function call, as in
20661 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20662 deducing arguments for a call to the result of a conversion
20663 function template, as in [over.call.object].
20664
20665 DEDUCE_CONV:
20666 We are deducing arguments for a conversion function, as in
20667 [temp.deduct.conv].
20668
20669 DEDUCE_EXACT:
20670 We are deducing arguments when doing an explicit instantiation
20671 as in [temp.explicit], when determining an explicit specialization
20672 as in [temp.expl.spec], or when taking the address of a function
20673 template, as in [temp.deduct.funcaddr]. */
20674
20675 tree
20676 fn_type_unification (tree fn,
20677 tree explicit_targs,
20678 tree targs,
20679 const tree *args,
20680 unsigned int nargs,
20681 tree return_type,
20682 unification_kind_t strict,
20683 int flags,
20684 struct conversion **convs,
20685 bool explain_p,
20686 bool decltype_p)
20687 {
20688 tree parms;
20689 tree fntype;
20690 tree decl = NULL_TREE;
20691 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20692 bool ok;
20693 static int deduction_depth;
20694 /* type_unification_real will pass back any access checks from default
20695 template argument substitution. */
20696 vec<deferred_access_check, va_gc> *checks = NULL;
20697 /* We don't have all the template args yet. */
20698 bool incomplete = true;
20699
20700 tree orig_fn = fn;
20701 if (flag_new_inheriting_ctors)
20702 fn = strip_inheriting_ctors (fn);
20703
20704 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20705 tree r = error_mark_node;
20706
20707 tree full_targs = targs;
20708 if (TMPL_ARGS_DEPTH (targs)
20709 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20710 full_targs = (add_outermost_template_args
20711 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20712 targs));
20713
20714 if (decltype_p)
20715 complain |= tf_decltype;
20716
20717 /* In C++0x, it's possible to have a function template whose type depends
20718 on itself recursively. This is most obvious with decltype, but can also
20719 occur with enumeration scope (c++/48969). So we need to catch infinite
20720 recursion and reject the substitution at deduction time; this function
20721 will return error_mark_node for any repeated substitution.
20722
20723 This also catches excessive recursion such as when f<N> depends on
20724 f<N-1> across all integers, and returns error_mark_node for all the
20725 substitutions back up to the initial one.
20726
20727 This is, of course, not reentrant. */
20728 if (excessive_deduction_depth)
20729 return error_mark_node;
20730 ++deduction_depth;
20731
20732 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20733
20734 fntype = TREE_TYPE (fn);
20735 if (explicit_targs)
20736 {
20737 /* [temp.deduct]
20738
20739 The specified template arguments must match the template
20740 parameters in kind (i.e., type, nontype, template), and there
20741 must not be more arguments than there are parameters;
20742 otherwise type deduction fails.
20743
20744 Nontype arguments must match the types of the corresponding
20745 nontype template parameters, or must be convertible to the
20746 types of the corresponding nontype parameters as specified in
20747 _temp.arg.nontype_, otherwise type deduction fails.
20748
20749 All references in the function type of the function template
20750 to the corresponding template parameters are replaced by the
20751 specified template argument values. If a substitution in a
20752 template parameter or in the function type of the function
20753 template results in an invalid type, type deduction fails. */
20754 int i, len = TREE_VEC_LENGTH (tparms);
20755 location_t loc = input_location;
20756 incomplete = false;
20757
20758 if (explicit_targs == error_mark_node)
20759 goto fail;
20760
20761 if (TMPL_ARGS_DEPTH (explicit_targs)
20762 < TMPL_ARGS_DEPTH (full_targs))
20763 explicit_targs = add_outermost_template_args (full_targs,
20764 explicit_targs);
20765
20766 /* Adjust any explicit template arguments before entering the
20767 substitution context. */
20768 explicit_targs
20769 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20770 complain|tf_partial,
20771 /*require_all_args=*/false,
20772 /*use_default_args=*/false));
20773 if (explicit_targs == error_mark_node)
20774 goto fail;
20775
20776 /* Substitute the explicit args into the function type. This is
20777 necessary so that, for instance, explicitly declared function
20778 arguments can match null pointed constants. If we were given
20779 an incomplete set of explicit args, we must not do semantic
20780 processing during substitution as we could create partial
20781 instantiations. */
20782 for (i = 0; i < len; i++)
20783 {
20784 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20785 bool parameter_pack = false;
20786 tree targ = TREE_VEC_ELT (explicit_targs, i);
20787
20788 /* Dig out the actual parm. */
20789 if (TREE_CODE (parm) == TYPE_DECL
20790 || TREE_CODE (parm) == TEMPLATE_DECL)
20791 {
20792 parm = TREE_TYPE (parm);
20793 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20794 }
20795 else if (TREE_CODE (parm) == PARM_DECL)
20796 {
20797 parm = DECL_INITIAL (parm);
20798 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20799 }
20800
20801 if (targ == NULL_TREE)
20802 /* No explicit argument for this template parameter. */
20803 incomplete = true;
20804 else if (parameter_pack && pack_deducible_p (parm, fn))
20805 {
20806 /* Mark the argument pack as "incomplete". We could
20807 still deduce more arguments during unification.
20808 We remove this mark in type_unification_real. */
20809 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20810 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20811 = ARGUMENT_PACK_ARGS (targ);
20812
20813 /* We have some incomplete argument packs. */
20814 incomplete = true;
20815 }
20816 }
20817
20818 if (incomplete)
20819 {
20820 if (!push_tinst_level (fn, explicit_targs))
20821 {
20822 excessive_deduction_depth = true;
20823 goto fail;
20824 }
20825 ++processing_template_decl;
20826 input_location = DECL_SOURCE_LOCATION (fn);
20827 /* Ignore any access checks; we'll see them again in
20828 instantiate_template and they might have the wrong
20829 access path at this point. */
20830 push_deferring_access_checks (dk_deferred);
20831 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20832 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20833 pop_deferring_access_checks ();
20834 input_location = loc;
20835 --processing_template_decl;
20836 pop_tinst_level ();
20837
20838 if (fntype == error_mark_node)
20839 goto fail;
20840 }
20841
20842 /* Place the explicitly specified arguments in TARGS. */
20843 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20844 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20845 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20846 if (!incomplete && CHECKING_P
20847 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20848 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20849 (targs, NUM_TMPL_ARGS (explicit_targs));
20850 }
20851
20852 if (return_type && strict != DEDUCE_CALL)
20853 {
20854 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20855 new_args[0] = return_type;
20856 memcpy (new_args + 1, args, nargs * sizeof (tree));
20857 args = new_args;
20858 ++nargs;
20859 }
20860
20861 if (!incomplete)
20862 goto deduced;
20863
20864 /* Never do unification on the 'this' parameter. */
20865 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20866
20867 if (return_type && strict == DEDUCE_CALL)
20868 {
20869 /* We're deducing for a call to the result of a template conversion
20870 function. The parms we really want are in return_type. */
20871 if (INDIRECT_TYPE_P (return_type))
20872 return_type = TREE_TYPE (return_type);
20873 parms = TYPE_ARG_TYPES (return_type);
20874 }
20875 else if (return_type)
20876 {
20877 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20878 }
20879
20880 /* We allow incomplete unification without an error message here
20881 because the standard doesn't seem to explicitly prohibit it. Our
20882 callers must be ready to deal with unification failures in any
20883 event. */
20884
20885 /* If we aren't explaining yet, push tinst context so we can see where
20886 any errors (e.g. from class instantiations triggered by instantiation
20887 of default template arguments) come from. If we are explaining, this
20888 context is redundant. */
20889 if (!explain_p && !push_tinst_level (fn, targs))
20890 {
20891 excessive_deduction_depth = true;
20892 goto fail;
20893 }
20894
20895 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20896 full_targs, parms, args, nargs, /*subr=*/0,
20897 strict, &checks, explain_p);
20898 if (!explain_p)
20899 pop_tinst_level ();
20900 if (!ok)
20901 goto fail;
20902
20903 /* Now that we have bindings for all of the template arguments,
20904 ensure that the arguments deduced for the template template
20905 parameters have compatible template parameter lists. We cannot
20906 check this property before we have deduced all template
20907 arguments, because the template parameter types of a template
20908 template parameter might depend on prior template parameters
20909 deduced after the template template parameter. The following
20910 ill-formed example illustrates this issue:
20911
20912 template<typename T, template<T> class C> void f(C<5>, T);
20913
20914 template<int N> struct X {};
20915
20916 void g() {
20917 f(X<5>(), 5l); // error: template argument deduction fails
20918 }
20919
20920 The template parameter list of 'C' depends on the template type
20921 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20922 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20923 time that we deduce 'C'. */
20924 if (!template_template_parm_bindings_ok_p
20925 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20926 {
20927 unify_inconsistent_template_template_parameters (explain_p);
20928 goto fail;
20929 }
20930
20931 /* DR 1391: All parameters have args, now check non-dependent parms for
20932 convertibility. */
20933 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20934 convs, explain_p))
20935 goto fail;
20936
20937 deduced:
20938 /* All is well so far. Now, check:
20939
20940 [temp.deduct]
20941
20942 When all template arguments have been deduced, all uses of
20943 template parameters in nondeduced contexts are replaced with
20944 the corresponding deduced argument values. If the
20945 substitution results in an invalid type, as described above,
20946 type deduction fails. */
20947 if (!push_tinst_level (fn, targs))
20948 {
20949 excessive_deduction_depth = true;
20950 goto fail;
20951 }
20952
20953 /* Also collect access checks from the instantiation. */
20954 reopen_deferring_access_checks (checks);
20955
20956 decl = instantiate_template (fn, targs, complain);
20957
20958 checks = get_deferred_access_checks ();
20959 pop_deferring_access_checks ();
20960
20961 pop_tinst_level ();
20962
20963 if (decl == error_mark_node)
20964 goto fail;
20965
20966 /* Now perform any access checks encountered during substitution. */
20967 push_access_scope (decl);
20968 ok = perform_access_checks (checks, complain);
20969 pop_access_scope (decl);
20970 if (!ok)
20971 goto fail;
20972
20973 /* If we're looking for an exact match, check that what we got
20974 is indeed an exact match. It might not be if some template
20975 parameters are used in non-deduced contexts. But don't check
20976 for an exact match if we have dependent template arguments;
20977 in that case we're doing partial ordering, and we already know
20978 that we have two candidates that will provide the actual type. */
20979 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20980 {
20981 tree substed = TREE_TYPE (decl);
20982 unsigned int i;
20983
20984 tree sarg
20985 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20986 if (return_type)
20987 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20988 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20989 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20990 {
20991 unify_type_mismatch (explain_p, args[i],
20992 TREE_VALUE (sarg));
20993 goto fail;
20994 }
20995 }
20996
20997 /* After doing deduction with the inherited constructor, actually return an
20998 instantiation of the inheriting constructor. */
20999 if (orig_fn != fn)
21000 decl = instantiate_template (orig_fn, targs, complain);
21001
21002 r = decl;
21003
21004 fail:
21005 --deduction_depth;
21006 if (excessive_deduction_depth)
21007 {
21008 if (deduction_depth == 0)
21009 /* Reset once we're all the way out. */
21010 excessive_deduction_depth = false;
21011 }
21012
21013 return r;
21014 }
21015
21016 /* Adjust types before performing type deduction, as described in
21017 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21018 sections are symmetric. PARM is the type of a function parameter
21019 or the return type of the conversion function. ARG is the type of
21020 the argument passed to the call, or the type of the value
21021 initialized with the result of the conversion function.
21022 ARG_EXPR is the original argument expression, which may be null. */
21023
21024 static int
21025 maybe_adjust_types_for_deduction (unification_kind_t strict,
21026 tree* parm,
21027 tree* arg,
21028 tree arg_expr)
21029 {
21030 int result = 0;
21031
21032 switch (strict)
21033 {
21034 case DEDUCE_CALL:
21035 break;
21036
21037 case DEDUCE_CONV:
21038 /* Swap PARM and ARG throughout the remainder of this
21039 function; the handling is precisely symmetric since PARM
21040 will initialize ARG rather than vice versa. */
21041 std::swap (parm, arg);
21042 break;
21043
21044 case DEDUCE_EXACT:
21045 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21046 too, but here handle it by stripping the reference from PARM
21047 rather than by adding it to ARG. */
21048 if (TYPE_REF_P (*parm)
21049 && TYPE_REF_IS_RVALUE (*parm)
21050 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21051 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21052 && TYPE_REF_P (*arg)
21053 && !TYPE_REF_IS_RVALUE (*arg))
21054 *parm = TREE_TYPE (*parm);
21055 /* Nothing else to do in this case. */
21056 return 0;
21057
21058 default:
21059 gcc_unreachable ();
21060 }
21061
21062 if (!TYPE_REF_P (*parm))
21063 {
21064 /* [temp.deduct.call]
21065
21066 If P is not a reference type:
21067
21068 --If A is an array type, the pointer type produced by the
21069 array-to-pointer standard conversion (_conv.array_) is
21070 used in place of A for type deduction; otherwise,
21071
21072 --If A is a function type, the pointer type produced by
21073 the function-to-pointer standard conversion
21074 (_conv.func_) is used in place of A for type deduction;
21075 otherwise,
21076
21077 --If A is a cv-qualified type, the top level
21078 cv-qualifiers of A's type are ignored for type
21079 deduction. */
21080 if (TREE_CODE (*arg) == ARRAY_TYPE)
21081 *arg = build_pointer_type (TREE_TYPE (*arg));
21082 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21083 *arg = build_pointer_type (*arg);
21084 else
21085 *arg = TYPE_MAIN_VARIANT (*arg);
21086 }
21087
21088 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21089 reference to a cv-unqualified template parameter that does not represent a
21090 template parameter of a class template (during class template argument
21091 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21092 an lvalue, the type "lvalue reference to A" is used in place of A for type
21093 deduction. */
21094 if (TYPE_REF_P (*parm)
21095 && TYPE_REF_IS_RVALUE (*parm)
21096 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21097 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21098 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21099 && (arg_expr ? lvalue_p (arg_expr)
21100 /* try_one_overload doesn't provide an arg_expr, but
21101 functions are always lvalues. */
21102 : TREE_CODE (*arg) == FUNCTION_TYPE))
21103 *arg = build_reference_type (*arg);
21104
21105 /* [temp.deduct.call]
21106
21107 If P is a cv-qualified type, the top level cv-qualifiers
21108 of P's type are ignored for type deduction. If P is a
21109 reference type, the type referred to by P is used for
21110 type deduction. */
21111 *parm = TYPE_MAIN_VARIANT (*parm);
21112 if (TYPE_REF_P (*parm))
21113 {
21114 *parm = TREE_TYPE (*parm);
21115 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21116 }
21117
21118 /* DR 322. For conversion deduction, remove a reference type on parm
21119 too (which has been swapped into ARG). */
21120 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21121 *arg = TREE_TYPE (*arg);
21122
21123 return result;
21124 }
21125
21126 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21127 template which doesn't contain any deducible template parameters; check if
21128 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21129 unify_one_argument. */
21130
21131 static int
21132 check_non_deducible_conversion (tree parm, tree arg, int strict,
21133 int flags, struct conversion **conv_p,
21134 bool explain_p)
21135 {
21136 tree type;
21137
21138 if (!TYPE_P (arg))
21139 type = TREE_TYPE (arg);
21140 else
21141 type = arg;
21142
21143 if (same_type_p (parm, type))
21144 return unify_success (explain_p);
21145
21146 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21147 if (strict == DEDUCE_CONV)
21148 {
21149 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21150 return unify_success (explain_p);
21151 }
21152 else if (strict != DEDUCE_EXACT)
21153 {
21154 bool ok = false;
21155 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21156 if (conv_p)
21157 /* Avoid recalculating this in add_function_candidate. */
21158 ok = (*conv_p
21159 = good_conversion (parm, type, conv_arg, flags, complain));
21160 else
21161 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21162 if (ok)
21163 return unify_success (explain_p);
21164 }
21165
21166 if (strict == DEDUCE_EXACT)
21167 return unify_type_mismatch (explain_p, parm, arg);
21168 else
21169 return unify_arg_conversion (explain_p, parm, type, arg);
21170 }
21171
21172 static bool uses_deducible_template_parms (tree type);
21173
21174 /* Returns true iff the expression EXPR is one from which a template
21175 argument can be deduced. In other words, if it's an undecorated
21176 use of a template non-type parameter. */
21177
21178 static bool
21179 deducible_expression (tree expr)
21180 {
21181 /* Strip implicit conversions. */
21182 while (CONVERT_EXPR_P (expr))
21183 expr = TREE_OPERAND (expr, 0);
21184 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21185 }
21186
21187 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21188 deducible way; that is, if it has a max value of <PARM> - 1. */
21189
21190 static bool
21191 deducible_array_bound (tree domain)
21192 {
21193 if (domain == NULL_TREE)
21194 return false;
21195
21196 tree max = TYPE_MAX_VALUE (domain);
21197 if (TREE_CODE (max) != MINUS_EXPR)
21198 return false;
21199
21200 return deducible_expression (TREE_OPERAND (max, 0));
21201 }
21202
21203 /* Returns true iff the template arguments ARGS use a template parameter
21204 in a deducible way. */
21205
21206 static bool
21207 deducible_template_args (tree args)
21208 {
21209 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21210 {
21211 bool deducible;
21212 tree elt = TREE_VEC_ELT (args, i);
21213 if (ARGUMENT_PACK_P (elt))
21214 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21215 else
21216 {
21217 if (PACK_EXPANSION_P (elt))
21218 elt = PACK_EXPANSION_PATTERN (elt);
21219 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21220 deducible = true;
21221 else if (TYPE_P (elt))
21222 deducible = uses_deducible_template_parms (elt);
21223 else
21224 deducible = deducible_expression (elt);
21225 }
21226 if (deducible)
21227 return true;
21228 }
21229 return false;
21230 }
21231
21232 /* Returns true iff TYPE contains any deducible references to template
21233 parameters, as per 14.8.2.5. */
21234
21235 static bool
21236 uses_deducible_template_parms (tree type)
21237 {
21238 if (PACK_EXPANSION_P (type))
21239 type = PACK_EXPANSION_PATTERN (type);
21240
21241 /* T
21242 cv-list T
21243 TT<T>
21244 TT<i>
21245 TT<> */
21246 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21247 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21248 return true;
21249
21250 /* T*
21251 T&
21252 T&& */
21253 if (INDIRECT_TYPE_P (type))
21254 return uses_deducible_template_parms (TREE_TYPE (type));
21255
21256 /* T[integer-constant ]
21257 type [i] */
21258 if (TREE_CODE (type) == ARRAY_TYPE)
21259 return (uses_deducible_template_parms (TREE_TYPE (type))
21260 || deducible_array_bound (TYPE_DOMAIN (type)));
21261
21262 /* T type ::*
21263 type T::*
21264 T T::*
21265 T (type ::*)()
21266 type (T::*)()
21267 type (type ::*)(T)
21268 type (T::*)(T)
21269 T (type ::*)(T)
21270 T (T::*)()
21271 T (T::*)(T) */
21272 if (TYPE_PTRMEM_P (type))
21273 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21274 || (uses_deducible_template_parms
21275 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21276
21277 /* template-name <T> (where template-name refers to a class template)
21278 template-name <i> (where template-name refers to a class template) */
21279 if (CLASS_TYPE_P (type)
21280 && CLASSTYPE_TEMPLATE_INFO (type)
21281 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21282 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21283 (CLASSTYPE_TI_ARGS (type)));
21284
21285 /* type (T)
21286 T()
21287 T(T) */
21288 if (FUNC_OR_METHOD_TYPE_P (type))
21289 {
21290 if (uses_deducible_template_parms (TREE_TYPE (type)))
21291 return true;
21292 tree parm = TYPE_ARG_TYPES (type);
21293 if (TREE_CODE (type) == METHOD_TYPE)
21294 parm = TREE_CHAIN (parm);
21295 for (; parm; parm = TREE_CHAIN (parm))
21296 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21297 return true;
21298 }
21299
21300 return false;
21301 }
21302
21303 /* Subroutine of type_unification_real and unify_pack_expansion to
21304 handle unification of a single P/A pair. Parameters are as
21305 for those functions. */
21306
21307 static int
21308 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21309 int subr, unification_kind_t strict,
21310 bool explain_p)
21311 {
21312 tree arg_expr = NULL_TREE;
21313 int arg_strict;
21314
21315 if (arg == error_mark_node || parm == error_mark_node)
21316 return unify_invalid (explain_p);
21317 if (arg == unknown_type_node)
21318 /* We can't deduce anything from this, but we might get all the
21319 template args from other function args. */
21320 return unify_success (explain_p);
21321
21322 /* Implicit conversions (Clause 4) will be performed on a function
21323 argument to convert it to the type of the corresponding function
21324 parameter if the parameter type contains no template-parameters that
21325 participate in template argument deduction. */
21326 if (strict != DEDUCE_EXACT
21327 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21328 /* For function parameters with no deducible template parameters,
21329 just return. We'll check non-dependent conversions later. */
21330 return unify_success (explain_p);
21331
21332 switch (strict)
21333 {
21334 case DEDUCE_CALL:
21335 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21336 | UNIFY_ALLOW_MORE_CV_QUAL
21337 | UNIFY_ALLOW_DERIVED);
21338 break;
21339
21340 case DEDUCE_CONV:
21341 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21342 break;
21343
21344 case DEDUCE_EXACT:
21345 arg_strict = UNIFY_ALLOW_NONE;
21346 break;
21347
21348 default:
21349 gcc_unreachable ();
21350 }
21351
21352 /* We only do these transformations if this is the top-level
21353 parameter_type_list in a call or declaration matching; in other
21354 situations (nested function declarators, template argument lists) we
21355 won't be comparing a type to an expression, and we don't do any type
21356 adjustments. */
21357 if (!subr)
21358 {
21359 if (!TYPE_P (arg))
21360 {
21361 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21362 if (type_unknown_p (arg))
21363 {
21364 /* [temp.deduct.type] A template-argument can be
21365 deduced from a pointer to function or pointer
21366 to member function argument if the set of
21367 overloaded functions does not contain function
21368 templates and at most one of a set of
21369 overloaded functions provides a unique
21370 match. */
21371 resolve_overloaded_unification (tparms, targs, parm,
21372 arg, strict,
21373 arg_strict, explain_p);
21374 /* If a unique match was not found, this is a
21375 non-deduced context, so we still succeed. */
21376 return unify_success (explain_p);
21377 }
21378
21379 arg_expr = arg;
21380 arg = unlowered_expr_type (arg);
21381 if (arg == error_mark_node)
21382 return unify_invalid (explain_p);
21383 }
21384
21385 arg_strict |=
21386 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21387 }
21388 else
21389 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21390 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21391 return unify_template_argument_mismatch (explain_p, parm, arg);
21392
21393 /* For deduction from an init-list we need the actual list. */
21394 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21395 arg = arg_expr;
21396 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21397 }
21398
21399 /* for_each_template_parm callback that always returns 0. */
21400
21401 static int
21402 zero_r (tree, void *)
21403 {
21404 return 0;
21405 }
21406
21407 /* for_each_template_parm any_fn callback to handle deduction of a template
21408 type argument from the type of an array bound. */
21409
21410 static int
21411 array_deduction_r (tree t, void *data)
21412 {
21413 tree_pair_p d = (tree_pair_p)data;
21414 tree &tparms = d->purpose;
21415 tree &targs = d->value;
21416
21417 if (TREE_CODE (t) == ARRAY_TYPE)
21418 if (tree dom = TYPE_DOMAIN (t))
21419 if (tree max = TYPE_MAX_VALUE (dom))
21420 {
21421 if (TREE_CODE (max) == MINUS_EXPR)
21422 max = TREE_OPERAND (max, 0);
21423 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21424 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21425 UNIFY_ALLOW_NONE, /*explain*/false);
21426 }
21427
21428 /* Keep walking. */
21429 return 0;
21430 }
21431
21432 /* Try to deduce any not-yet-deduced template type arguments from the type of
21433 an array bound. This is handled separately from unify because 14.8.2.5 says
21434 "The type of a type parameter is only deduced from an array bound if it is
21435 not otherwise deduced." */
21436
21437 static void
21438 try_array_deduction (tree tparms, tree targs, tree parm)
21439 {
21440 tree_pair_s data = { tparms, targs };
21441 hash_set<tree> visited;
21442 for_each_template_parm (parm, zero_r, &data, &visited,
21443 /*nondeduced*/false, array_deduction_r);
21444 }
21445
21446 /* Most parms like fn_type_unification.
21447
21448 If SUBR is 1, we're being called recursively (to unify the
21449 arguments of a function or method parameter of a function
21450 template).
21451
21452 CHECKS is a pointer to a vector of access checks encountered while
21453 substituting default template arguments. */
21454
21455 static int
21456 type_unification_real (tree tparms,
21457 tree full_targs,
21458 tree xparms,
21459 const tree *xargs,
21460 unsigned int xnargs,
21461 int subr,
21462 unification_kind_t strict,
21463 vec<deferred_access_check, va_gc> **checks,
21464 bool explain_p)
21465 {
21466 tree parm, arg;
21467 int i;
21468 int ntparms = TREE_VEC_LENGTH (tparms);
21469 int saw_undeduced = 0;
21470 tree parms;
21471 const tree *args;
21472 unsigned int nargs;
21473 unsigned int ia;
21474
21475 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21476 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21477 gcc_assert (ntparms > 0);
21478
21479 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21480
21481 /* Reset the number of non-defaulted template arguments contained
21482 in TARGS. */
21483 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21484
21485 again:
21486 parms = xparms;
21487 args = xargs;
21488 nargs = xnargs;
21489
21490 ia = 0;
21491 while (parms && parms != void_list_node
21492 && ia < nargs)
21493 {
21494 parm = TREE_VALUE (parms);
21495
21496 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21497 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21498 /* For a function parameter pack that occurs at the end of the
21499 parameter-declaration-list, the type A of each remaining
21500 argument of the call is compared with the type P of the
21501 declarator-id of the function parameter pack. */
21502 break;
21503
21504 parms = TREE_CHAIN (parms);
21505
21506 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21507 /* For a function parameter pack that does not occur at the
21508 end of the parameter-declaration-list, the type of the
21509 parameter pack is a non-deduced context. */
21510 continue;
21511
21512 arg = args[ia];
21513 ++ia;
21514
21515 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21516 explain_p))
21517 return 1;
21518 }
21519
21520 if (parms
21521 && parms != void_list_node
21522 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21523 {
21524 /* Unify the remaining arguments with the pack expansion type. */
21525 tree argvec;
21526 tree parmvec = make_tree_vec (1);
21527
21528 /* Allocate a TREE_VEC and copy in all of the arguments */
21529 argvec = make_tree_vec (nargs - ia);
21530 for (i = 0; ia < nargs; ++ia, ++i)
21531 TREE_VEC_ELT (argvec, i) = args[ia];
21532
21533 /* Copy the parameter into parmvec. */
21534 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21535 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21536 /*subr=*/subr, explain_p))
21537 return 1;
21538
21539 /* Advance to the end of the list of parameters. */
21540 parms = TREE_CHAIN (parms);
21541 }
21542
21543 /* Fail if we've reached the end of the parm list, and more args
21544 are present, and the parm list isn't variadic. */
21545 if (ia < nargs && parms == void_list_node)
21546 return unify_too_many_arguments (explain_p, nargs, ia);
21547 /* Fail if parms are left and they don't have default values and
21548 they aren't all deduced as empty packs (c++/57397). This is
21549 consistent with sufficient_parms_p. */
21550 if (parms && parms != void_list_node
21551 && TREE_PURPOSE (parms) == NULL_TREE)
21552 {
21553 unsigned int count = nargs;
21554 tree p = parms;
21555 bool type_pack_p;
21556 do
21557 {
21558 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21559 if (!type_pack_p)
21560 count++;
21561 p = TREE_CHAIN (p);
21562 }
21563 while (p && p != void_list_node);
21564 if (count != nargs)
21565 return unify_too_few_arguments (explain_p, ia, count,
21566 type_pack_p);
21567 }
21568
21569 if (!subr)
21570 {
21571 tsubst_flags_t complain = (explain_p
21572 ? tf_warning_or_error
21573 : tf_none);
21574 bool tried_array_deduction = (cxx_dialect < cxx17);
21575
21576 for (i = 0; i < ntparms; i++)
21577 {
21578 tree targ = TREE_VEC_ELT (targs, i);
21579 tree tparm = TREE_VEC_ELT (tparms, i);
21580
21581 /* Clear the "incomplete" flags on all argument packs now so that
21582 substituting them into later default arguments works. */
21583 if (targ && ARGUMENT_PACK_P (targ))
21584 {
21585 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21586 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21587 }
21588
21589 if (targ || tparm == error_mark_node)
21590 continue;
21591 tparm = TREE_VALUE (tparm);
21592
21593 if (TREE_CODE (tparm) == TYPE_DECL
21594 && !tried_array_deduction)
21595 {
21596 try_array_deduction (tparms, targs, xparms);
21597 tried_array_deduction = true;
21598 if (TREE_VEC_ELT (targs, i))
21599 continue;
21600 }
21601
21602 /* If this is an undeduced nontype parameter that depends on
21603 a type parameter, try another pass; its type may have been
21604 deduced from a later argument than the one from which
21605 this parameter can be deduced. */
21606 if (TREE_CODE (tparm) == PARM_DECL
21607 && uses_template_parms (TREE_TYPE (tparm))
21608 && saw_undeduced < 2)
21609 {
21610 saw_undeduced = 1;
21611 continue;
21612 }
21613
21614 /* Core issue #226 (C++0x) [temp.deduct]:
21615
21616 If a template argument has not been deduced, its
21617 default template argument, if any, is used.
21618
21619 When we are in C++98 mode, TREE_PURPOSE will either
21620 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21621 to explicitly check cxx_dialect here. */
21622 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21623 /* OK, there is a default argument. Wait until after the
21624 conversion check to do substitution. */
21625 continue;
21626
21627 /* If the type parameter is a parameter pack, then it will
21628 be deduced to an empty parameter pack. */
21629 if (template_parameter_pack_p (tparm))
21630 {
21631 tree arg;
21632
21633 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21634 {
21635 arg = make_node (NONTYPE_ARGUMENT_PACK);
21636 TREE_CONSTANT (arg) = 1;
21637 }
21638 else
21639 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21640
21641 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21642
21643 TREE_VEC_ELT (targs, i) = arg;
21644 continue;
21645 }
21646
21647 return unify_parameter_deduction_failure (explain_p, tparm);
21648 }
21649
21650 /* Now substitute into the default template arguments. */
21651 for (i = 0; i < ntparms; i++)
21652 {
21653 tree targ = TREE_VEC_ELT (targs, i);
21654 tree tparm = TREE_VEC_ELT (tparms, i);
21655
21656 if (targ || tparm == error_mark_node)
21657 continue;
21658 tree parm = TREE_VALUE (tparm);
21659 tree arg = TREE_PURPOSE (tparm);
21660 reopen_deferring_access_checks (*checks);
21661 location_t save_loc = input_location;
21662 if (DECL_P (parm))
21663 input_location = DECL_SOURCE_LOCATION (parm);
21664
21665 if (saw_undeduced == 1
21666 && TREE_CODE (parm) == PARM_DECL
21667 && uses_template_parms (TREE_TYPE (parm)))
21668 {
21669 /* The type of this non-type parameter depends on undeduced
21670 parameters. Don't try to use its default argument yet,
21671 since we might deduce an argument for it on the next pass,
21672 but do check whether the arguments we already have cause
21673 substitution failure, so that that happens before we try
21674 later default arguments (78489). */
21675 ++processing_template_decl;
21676 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21677 NULL_TREE);
21678 --processing_template_decl;
21679 if (type == error_mark_node)
21680 arg = error_mark_node;
21681 else
21682 arg = NULL_TREE;
21683 }
21684 else
21685 {
21686 /* Even if the call is happening in template context, getting
21687 here means it's non-dependent, and a default argument is
21688 considered a separate definition under [temp.decls], so we can
21689 do this substitution without processing_template_decl. This
21690 is important if the default argument contains something that
21691 might be instantiation-dependent like access (87480). */
21692 processing_template_decl_sentinel s;
21693 tree substed = NULL_TREE;
21694 if (saw_undeduced == 1)
21695 {
21696 /* First instatiate in template context, in case we still
21697 depend on undeduced template parameters. */
21698 ++processing_template_decl;
21699 substed = tsubst_template_arg (arg, full_targs, complain,
21700 NULL_TREE);
21701 --processing_template_decl;
21702 if (substed != error_mark_node
21703 && !uses_template_parms (substed))
21704 /* We replaced all the tparms, substitute again out of
21705 template context. */
21706 substed = NULL_TREE;
21707 }
21708 if (!substed)
21709 substed = tsubst_template_arg (arg, full_targs, complain,
21710 NULL_TREE);
21711
21712 if (!uses_template_parms (substed))
21713 arg = convert_template_argument (parm, substed, full_targs,
21714 complain, i, NULL_TREE);
21715 else if (saw_undeduced == 1)
21716 arg = NULL_TREE;
21717 else
21718 arg = error_mark_node;
21719 }
21720
21721 input_location = save_loc;
21722 *checks = get_deferred_access_checks ();
21723 pop_deferring_access_checks ();
21724
21725 if (arg == error_mark_node)
21726 return 1;
21727 else if (arg)
21728 {
21729 TREE_VEC_ELT (targs, i) = arg;
21730 /* The position of the first default template argument,
21731 is also the number of non-defaulted arguments in TARGS.
21732 Record that. */
21733 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21734 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21735 }
21736 }
21737
21738 if (saw_undeduced++ == 1)
21739 goto again;
21740 }
21741
21742 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21743 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21744
21745 return unify_success (explain_p);
21746 }
21747
21748 /* Subroutine of type_unification_real. Args are like the variables
21749 at the call site. ARG is an overloaded function (or template-id);
21750 we try deducing template args from each of the overloads, and if
21751 only one succeeds, we go with that. Modifies TARGS and returns
21752 true on success. */
21753
21754 static bool
21755 resolve_overloaded_unification (tree tparms,
21756 tree targs,
21757 tree parm,
21758 tree arg,
21759 unification_kind_t strict,
21760 int sub_strict,
21761 bool explain_p)
21762 {
21763 tree tempargs = copy_node (targs);
21764 int good = 0;
21765 tree goodfn = NULL_TREE;
21766 bool addr_p;
21767
21768 if (TREE_CODE (arg) == ADDR_EXPR)
21769 {
21770 arg = TREE_OPERAND (arg, 0);
21771 addr_p = true;
21772 }
21773 else
21774 addr_p = false;
21775
21776 if (TREE_CODE (arg) == COMPONENT_REF)
21777 /* Handle `&x' where `x' is some static or non-static member
21778 function name. */
21779 arg = TREE_OPERAND (arg, 1);
21780
21781 if (TREE_CODE (arg) == OFFSET_REF)
21782 arg = TREE_OPERAND (arg, 1);
21783
21784 /* Strip baselink information. */
21785 if (BASELINK_P (arg))
21786 arg = BASELINK_FUNCTIONS (arg);
21787
21788 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21789 {
21790 /* If we got some explicit template args, we need to plug them into
21791 the affected templates before we try to unify, in case the
21792 explicit args will completely resolve the templates in question. */
21793
21794 int ok = 0;
21795 tree expl_subargs = TREE_OPERAND (arg, 1);
21796 arg = TREE_OPERAND (arg, 0);
21797
21798 for (lkp_iterator iter (arg); iter; ++iter)
21799 {
21800 tree fn = *iter;
21801 tree subargs, elem;
21802
21803 if (TREE_CODE (fn) != TEMPLATE_DECL)
21804 continue;
21805
21806 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21807 expl_subargs, NULL_TREE, tf_none,
21808 /*require_all_args=*/true,
21809 /*use_default_args=*/true);
21810 if (subargs != error_mark_node
21811 && !any_dependent_template_arguments_p (subargs))
21812 {
21813 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21814 if (try_one_overload (tparms, targs, tempargs, parm,
21815 elem, strict, sub_strict, addr_p, explain_p)
21816 && (!goodfn || !same_type_p (goodfn, elem)))
21817 {
21818 goodfn = elem;
21819 ++good;
21820 }
21821 }
21822 else if (subargs)
21823 ++ok;
21824 }
21825 /* If no templates (or more than one) are fully resolved by the
21826 explicit arguments, this template-id is a non-deduced context; it
21827 could still be OK if we deduce all template arguments for the
21828 enclosing call through other arguments. */
21829 if (good != 1)
21830 good = ok;
21831 }
21832 else if (!OVL_P (arg))
21833 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21834 -- but the deduction does not succeed because the expression is
21835 not just the function on its own. */
21836 return false;
21837 else
21838 for (lkp_iterator iter (arg); iter; ++iter)
21839 {
21840 tree fn = *iter;
21841 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21842 strict, sub_strict, addr_p, explain_p)
21843 && (!goodfn || !decls_match (goodfn, fn)))
21844 {
21845 goodfn = fn;
21846 ++good;
21847 }
21848 }
21849
21850 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21851 to function or pointer to member function argument if the set of
21852 overloaded functions does not contain function templates and at most
21853 one of a set of overloaded functions provides a unique match.
21854
21855 So if we found multiple possibilities, we return success but don't
21856 deduce anything. */
21857
21858 if (good == 1)
21859 {
21860 int i = TREE_VEC_LENGTH (targs);
21861 for (; i--; )
21862 if (TREE_VEC_ELT (tempargs, i))
21863 {
21864 tree old = TREE_VEC_ELT (targs, i);
21865 tree new_ = TREE_VEC_ELT (tempargs, i);
21866 if (new_ && old && ARGUMENT_PACK_P (old)
21867 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21868 /* Don't forget explicit template arguments in a pack. */
21869 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21870 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21871 TREE_VEC_ELT (targs, i) = new_;
21872 }
21873 }
21874 if (good)
21875 return true;
21876
21877 return false;
21878 }
21879
21880 /* Core DR 115: In contexts where deduction is done and fails, or in
21881 contexts where deduction is not done, if a template argument list is
21882 specified and it, along with any default template arguments, identifies
21883 a single function template specialization, then the template-id is an
21884 lvalue for the function template specialization. */
21885
21886 tree
21887 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21888 {
21889 tree expr, offset, baselink;
21890 bool addr;
21891
21892 if (!type_unknown_p (orig_expr))
21893 return orig_expr;
21894
21895 expr = orig_expr;
21896 addr = false;
21897 offset = NULL_TREE;
21898 baselink = NULL_TREE;
21899
21900 if (TREE_CODE (expr) == ADDR_EXPR)
21901 {
21902 expr = TREE_OPERAND (expr, 0);
21903 addr = true;
21904 }
21905 if (TREE_CODE (expr) == OFFSET_REF)
21906 {
21907 offset = expr;
21908 expr = TREE_OPERAND (expr, 1);
21909 }
21910 if (BASELINK_P (expr))
21911 {
21912 baselink = expr;
21913 expr = BASELINK_FUNCTIONS (expr);
21914 }
21915
21916 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21917 {
21918 int good = 0;
21919 tree goodfn = NULL_TREE;
21920
21921 /* If we got some explicit template args, we need to plug them into
21922 the affected templates before we try to unify, in case the
21923 explicit args will completely resolve the templates in question. */
21924
21925 tree expl_subargs = TREE_OPERAND (expr, 1);
21926 tree arg = TREE_OPERAND (expr, 0);
21927 tree badfn = NULL_TREE;
21928 tree badargs = NULL_TREE;
21929
21930 for (lkp_iterator iter (arg); iter; ++iter)
21931 {
21932 tree fn = *iter;
21933 tree subargs, elem;
21934
21935 if (TREE_CODE (fn) != TEMPLATE_DECL)
21936 continue;
21937
21938 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21939 expl_subargs, NULL_TREE, tf_none,
21940 /*require_all_args=*/true,
21941 /*use_default_args=*/true);
21942 if (subargs != error_mark_node
21943 && !any_dependent_template_arguments_p (subargs))
21944 {
21945 elem = instantiate_template (fn, subargs, tf_none);
21946 if (elem == error_mark_node)
21947 {
21948 badfn = fn;
21949 badargs = subargs;
21950 }
21951 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21952 {
21953 goodfn = elem;
21954 ++good;
21955 }
21956 }
21957 }
21958 if (good == 1)
21959 {
21960 mark_used (goodfn);
21961 expr = goodfn;
21962 if (baselink)
21963 expr = build_baselink (BASELINK_BINFO (baselink),
21964 BASELINK_ACCESS_BINFO (baselink),
21965 expr, BASELINK_OPTYPE (baselink));
21966 if (offset)
21967 {
21968 tree base
21969 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21970 expr = build_offset_ref (base, expr, addr, complain);
21971 }
21972 if (addr)
21973 expr = cp_build_addr_expr (expr, complain);
21974 return expr;
21975 }
21976 else if (good == 0 && badargs && (complain & tf_error))
21977 /* There were no good options and at least one bad one, so let the
21978 user know what the problem is. */
21979 instantiate_template (badfn, badargs, complain);
21980 }
21981 return orig_expr;
21982 }
21983
21984 /* As above, but error out if the expression remains overloaded. */
21985
21986 tree
21987 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21988 {
21989 exp = resolve_nondeduced_context (exp, complain);
21990 if (type_unknown_p (exp))
21991 {
21992 if (complain & tf_error)
21993 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21994 return error_mark_node;
21995 }
21996 return exp;
21997 }
21998
21999 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22000 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22001 different overloads deduce different arguments for a given parm.
22002 ADDR_P is true if the expression for which deduction is being
22003 performed was of the form "& fn" rather than simply "fn".
22004
22005 Returns 1 on success. */
22006
22007 static int
22008 try_one_overload (tree tparms,
22009 tree orig_targs,
22010 tree targs,
22011 tree parm,
22012 tree arg,
22013 unification_kind_t strict,
22014 int sub_strict,
22015 bool addr_p,
22016 bool explain_p)
22017 {
22018 int nargs;
22019 tree tempargs;
22020 int i;
22021
22022 if (arg == error_mark_node)
22023 return 0;
22024
22025 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22026 to function or pointer to member function argument if the set of
22027 overloaded functions does not contain function templates and at most
22028 one of a set of overloaded functions provides a unique match.
22029
22030 So if this is a template, just return success. */
22031
22032 if (uses_template_parms (arg))
22033 return 1;
22034
22035 if (TREE_CODE (arg) == METHOD_TYPE)
22036 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22037 else if (addr_p)
22038 arg = build_pointer_type (arg);
22039
22040 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22041
22042 /* We don't copy orig_targs for this because if we have already deduced
22043 some template args from previous args, unify would complain when we
22044 try to deduce a template parameter for the same argument, even though
22045 there isn't really a conflict. */
22046 nargs = TREE_VEC_LENGTH (targs);
22047 tempargs = make_tree_vec (nargs);
22048
22049 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22050 return 0;
22051
22052 /* First make sure we didn't deduce anything that conflicts with
22053 explicitly specified args. */
22054 for (i = nargs; i--; )
22055 {
22056 tree elt = TREE_VEC_ELT (tempargs, i);
22057 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22058
22059 if (!elt)
22060 /*NOP*/;
22061 else if (uses_template_parms (elt))
22062 /* Since we're unifying against ourselves, we will fill in
22063 template args used in the function parm list with our own
22064 template parms. Discard them. */
22065 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22066 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22067 {
22068 /* Check that the argument at each index of the deduced argument pack
22069 is equivalent to the corresponding explicitly specified argument.
22070 We may have deduced more arguments than were explicitly specified,
22071 and that's OK. */
22072
22073 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22074 that's wrong if we deduce the same argument pack from multiple
22075 function arguments: it's only incomplete the first time. */
22076
22077 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22078 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22079
22080 if (TREE_VEC_LENGTH (deduced_pack)
22081 < TREE_VEC_LENGTH (explicit_pack))
22082 return 0;
22083
22084 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22085 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22086 TREE_VEC_ELT (deduced_pack, j)))
22087 return 0;
22088 }
22089 else if (oldelt && !template_args_equal (oldelt, elt))
22090 return 0;
22091 }
22092
22093 for (i = nargs; i--; )
22094 {
22095 tree elt = TREE_VEC_ELT (tempargs, i);
22096
22097 if (elt)
22098 TREE_VEC_ELT (targs, i) = elt;
22099 }
22100
22101 return 1;
22102 }
22103
22104 /* PARM is a template class (perhaps with unbound template
22105 parameters). ARG is a fully instantiated type. If ARG can be
22106 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22107 TARGS are as for unify. */
22108
22109 static tree
22110 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22111 bool explain_p)
22112 {
22113 tree copy_of_targs;
22114
22115 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22116 return NULL_TREE;
22117 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22118 /* Matches anything. */;
22119 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22120 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22121 return NULL_TREE;
22122
22123 /* We need to make a new template argument vector for the call to
22124 unify. If we used TARGS, we'd clutter it up with the result of
22125 the attempted unification, even if this class didn't work out.
22126 We also don't want to commit ourselves to all the unifications
22127 we've already done, since unification is supposed to be done on
22128 an argument-by-argument basis. In other words, consider the
22129 following pathological case:
22130
22131 template <int I, int J, int K>
22132 struct S {};
22133
22134 template <int I, int J>
22135 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22136
22137 template <int I, int J, int K>
22138 void f(S<I, J, K>, S<I, I, I>);
22139
22140 void g() {
22141 S<0, 0, 0> s0;
22142 S<0, 1, 2> s2;
22143
22144 f(s0, s2);
22145 }
22146
22147 Now, by the time we consider the unification involving `s2', we
22148 already know that we must have `f<0, 0, 0>'. But, even though
22149 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22150 because there are two ways to unify base classes of S<0, 1, 2>
22151 with S<I, I, I>. If we kept the already deduced knowledge, we
22152 would reject the possibility I=1. */
22153 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22154
22155 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22156 {
22157 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22158 return NULL_TREE;
22159 return arg;
22160 }
22161
22162 /* If unification failed, we're done. */
22163 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22164 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22165 return NULL_TREE;
22166
22167 return arg;
22168 }
22169
22170 /* Given a template type PARM and a class type ARG, find the unique
22171 base type in ARG that is an instance of PARM. We do not examine
22172 ARG itself; only its base-classes. If there is not exactly one
22173 appropriate base class, return NULL_TREE. PARM may be the type of
22174 a partial specialization, as well as a plain template type. Used
22175 by unify. */
22176
22177 static enum template_base_result
22178 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22179 bool explain_p, tree *result)
22180 {
22181 tree rval = NULL_TREE;
22182 tree binfo;
22183
22184 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22185
22186 binfo = TYPE_BINFO (complete_type (arg));
22187 if (!binfo)
22188 {
22189 /* The type could not be completed. */
22190 *result = NULL_TREE;
22191 return tbr_incomplete_type;
22192 }
22193
22194 /* Walk in inheritance graph order. The search order is not
22195 important, and this avoids multiple walks of virtual bases. */
22196 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22197 {
22198 tree r = try_class_unification (tparms, targs, parm,
22199 BINFO_TYPE (binfo), explain_p);
22200
22201 if (r)
22202 {
22203 /* If there is more than one satisfactory baseclass, then:
22204
22205 [temp.deduct.call]
22206
22207 If they yield more than one possible deduced A, the type
22208 deduction fails.
22209
22210 applies. */
22211 if (rval && !same_type_p (r, rval))
22212 {
22213 *result = NULL_TREE;
22214 return tbr_ambiguous_baseclass;
22215 }
22216
22217 rval = r;
22218 }
22219 }
22220
22221 *result = rval;
22222 return tbr_success;
22223 }
22224
22225 /* Returns the level of DECL, which declares a template parameter. */
22226
22227 static int
22228 template_decl_level (tree decl)
22229 {
22230 switch (TREE_CODE (decl))
22231 {
22232 case TYPE_DECL:
22233 case TEMPLATE_DECL:
22234 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22235
22236 case PARM_DECL:
22237 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22238
22239 default:
22240 gcc_unreachable ();
22241 }
22242 return 0;
22243 }
22244
22245 /* Decide whether ARG can be unified with PARM, considering only the
22246 cv-qualifiers of each type, given STRICT as documented for unify.
22247 Returns nonzero iff the unification is OK on that basis. */
22248
22249 static int
22250 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22251 {
22252 int arg_quals = cp_type_quals (arg);
22253 int parm_quals = cp_type_quals (parm);
22254
22255 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22256 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22257 {
22258 /* Although a CVR qualifier is ignored when being applied to a
22259 substituted template parameter ([8.3.2]/1 for example), that
22260 does not allow us to unify "const T" with "int&" because both
22261 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22262 It is ok when we're allowing additional CV qualifiers
22263 at the outer level [14.8.2.1]/3,1st bullet. */
22264 if ((TYPE_REF_P (arg)
22265 || FUNC_OR_METHOD_TYPE_P (arg))
22266 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22267 return 0;
22268
22269 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22270 && (parm_quals & TYPE_QUAL_RESTRICT))
22271 return 0;
22272 }
22273
22274 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22275 && (arg_quals & parm_quals) != parm_quals)
22276 return 0;
22277
22278 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22279 && (parm_quals & arg_quals) != arg_quals)
22280 return 0;
22281
22282 return 1;
22283 }
22284
22285 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22286 void
22287 template_parm_level_and_index (tree parm, int* level, int* index)
22288 {
22289 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22290 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22291 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22292 {
22293 *index = TEMPLATE_TYPE_IDX (parm);
22294 *level = TEMPLATE_TYPE_LEVEL (parm);
22295 }
22296 else
22297 {
22298 *index = TEMPLATE_PARM_IDX (parm);
22299 *level = TEMPLATE_PARM_LEVEL (parm);
22300 }
22301 }
22302
22303 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22304 do { \
22305 if (unify (TP, TA, P, A, S, EP)) \
22306 return 1; \
22307 } while (0)
22308
22309 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22310 expansion at the end of PACKED_PARMS. Returns 0 if the type
22311 deduction succeeds, 1 otherwise. STRICT is the same as in
22312 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22313 function call argument list. We'll need to adjust the arguments to make them
22314 types. SUBR tells us if this is from a recursive call to
22315 type_unification_real, or for comparing two template argument
22316 lists. */
22317
22318 static int
22319 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22320 tree packed_args, unification_kind_t strict,
22321 bool subr, bool explain_p)
22322 {
22323 tree parm
22324 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22325 tree pattern = PACK_EXPANSION_PATTERN (parm);
22326 tree pack, packs = NULL_TREE;
22327 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22328
22329 /* Add in any args remembered from an earlier partial instantiation. */
22330 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22331 int levels = TMPL_ARGS_DEPTH (targs);
22332
22333 packed_args = expand_template_argument_pack (packed_args);
22334
22335 int len = TREE_VEC_LENGTH (packed_args);
22336
22337 /* Determine the parameter packs we will be deducing from the
22338 pattern, and record their current deductions. */
22339 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22340 pack; pack = TREE_CHAIN (pack))
22341 {
22342 tree parm_pack = TREE_VALUE (pack);
22343 int idx, level;
22344
22345 /* Only template parameter packs can be deduced, not e.g. function
22346 parameter packs or __bases or __integer_pack. */
22347 if (!TEMPLATE_PARM_P (parm_pack))
22348 continue;
22349
22350 /* Determine the index and level of this parameter pack. */
22351 template_parm_level_and_index (parm_pack, &level, &idx);
22352 if (level < levels)
22353 continue;
22354
22355 /* Keep track of the parameter packs and their corresponding
22356 argument packs. */
22357 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22358 TREE_TYPE (packs) = make_tree_vec (len - start);
22359 }
22360
22361 /* Loop through all of the arguments that have not yet been
22362 unified and unify each with the pattern. */
22363 for (i = start; i < len; i++)
22364 {
22365 tree parm;
22366 bool any_explicit = false;
22367 tree arg = TREE_VEC_ELT (packed_args, i);
22368
22369 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22370 or the element of its argument pack at the current index if
22371 this argument was explicitly specified. */
22372 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22373 {
22374 int idx, level;
22375 tree arg, pargs;
22376 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22377
22378 arg = NULL_TREE;
22379 if (TREE_VALUE (pack)
22380 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22381 && (i - start < TREE_VEC_LENGTH (pargs)))
22382 {
22383 any_explicit = true;
22384 arg = TREE_VEC_ELT (pargs, i - start);
22385 }
22386 TMPL_ARG (targs, level, idx) = arg;
22387 }
22388
22389 /* If we had explicit template arguments, substitute them into the
22390 pattern before deduction. */
22391 if (any_explicit)
22392 {
22393 /* Some arguments might still be unspecified or dependent. */
22394 bool dependent;
22395 ++processing_template_decl;
22396 dependent = any_dependent_template_arguments_p (targs);
22397 if (!dependent)
22398 --processing_template_decl;
22399 parm = tsubst (pattern, targs,
22400 explain_p ? tf_warning_or_error : tf_none,
22401 NULL_TREE);
22402 if (dependent)
22403 --processing_template_decl;
22404 if (parm == error_mark_node)
22405 return 1;
22406 }
22407 else
22408 parm = pattern;
22409
22410 /* Unify the pattern with the current argument. */
22411 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22412 explain_p))
22413 return 1;
22414
22415 /* For each parameter pack, collect the deduced value. */
22416 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22417 {
22418 int idx, level;
22419 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22420
22421 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22422 TMPL_ARG (targs, level, idx);
22423 }
22424 }
22425
22426 /* Verify that the results of unification with the parameter packs
22427 produce results consistent with what we've seen before, and make
22428 the deduced argument packs available. */
22429 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22430 {
22431 tree old_pack = TREE_VALUE (pack);
22432 tree new_args = TREE_TYPE (pack);
22433 int i, len = TREE_VEC_LENGTH (new_args);
22434 int idx, level;
22435 bool nondeduced_p = false;
22436
22437 /* By default keep the original deduced argument pack.
22438 If necessary, more specific code is going to update the
22439 resulting deduced argument later down in this function. */
22440 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22441 TMPL_ARG (targs, level, idx) = old_pack;
22442
22443 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22444 actually deduce anything. */
22445 for (i = 0; i < len && !nondeduced_p; ++i)
22446 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22447 nondeduced_p = true;
22448 if (nondeduced_p)
22449 continue;
22450
22451 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22452 {
22453 /* If we had fewer function args than explicit template args,
22454 just use the explicits. */
22455 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22456 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22457 if (len < explicit_len)
22458 new_args = explicit_args;
22459 }
22460
22461 if (!old_pack)
22462 {
22463 tree result;
22464 /* Build the deduced *_ARGUMENT_PACK. */
22465 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22466 {
22467 result = make_node (NONTYPE_ARGUMENT_PACK);
22468 TREE_CONSTANT (result) = 1;
22469 }
22470 else
22471 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22472
22473 SET_ARGUMENT_PACK_ARGS (result, new_args);
22474
22475 /* Note the deduced argument packs for this parameter
22476 pack. */
22477 TMPL_ARG (targs, level, idx) = result;
22478 }
22479 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22480 && (ARGUMENT_PACK_ARGS (old_pack)
22481 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22482 {
22483 /* We only had the explicitly-provided arguments before, but
22484 now we have a complete set of arguments. */
22485 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22486
22487 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22488 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22489 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22490 }
22491 else
22492 {
22493 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22494 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22495
22496 if (!comp_template_args (old_args, new_args,
22497 &bad_old_arg, &bad_new_arg))
22498 /* Inconsistent unification of this parameter pack. */
22499 return unify_parameter_pack_inconsistent (explain_p,
22500 bad_old_arg,
22501 bad_new_arg);
22502 }
22503 }
22504
22505 return unify_success (explain_p);
22506 }
22507
22508 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22509 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22510 parameters and return value are as for unify. */
22511
22512 static int
22513 unify_array_domain (tree tparms, tree targs,
22514 tree parm_dom, tree arg_dom,
22515 bool explain_p)
22516 {
22517 tree parm_max;
22518 tree arg_max;
22519 bool parm_cst;
22520 bool arg_cst;
22521
22522 /* Our representation of array types uses "N - 1" as the
22523 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22524 not an integer constant. We cannot unify arbitrarily
22525 complex expressions, so we eliminate the MINUS_EXPRs
22526 here. */
22527 parm_max = TYPE_MAX_VALUE (parm_dom);
22528 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22529 if (!parm_cst)
22530 {
22531 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22532 parm_max = TREE_OPERAND (parm_max, 0);
22533 }
22534 arg_max = TYPE_MAX_VALUE (arg_dom);
22535 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22536 if (!arg_cst)
22537 {
22538 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22539 trying to unify the type of a variable with the type
22540 of a template parameter. For example:
22541
22542 template <unsigned int N>
22543 void f (char (&) [N]);
22544 int g();
22545 void h(int i) {
22546 char a[g(i)];
22547 f(a);
22548 }
22549
22550 Here, the type of the ARG will be "int [g(i)]", and
22551 may be a SAVE_EXPR, etc. */
22552 if (TREE_CODE (arg_max) != MINUS_EXPR)
22553 return unify_vla_arg (explain_p, arg_dom);
22554 arg_max = TREE_OPERAND (arg_max, 0);
22555 }
22556
22557 /* If only one of the bounds used a MINUS_EXPR, compensate
22558 by adding one to the other bound. */
22559 if (parm_cst && !arg_cst)
22560 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22561 integer_type_node,
22562 parm_max,
22563 integer_one_node);
22564 else if (arg_cst && !parm_cst)
22565 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22566 integer_type_node,
22567 arg_max,
22568 integer_one_node);
22569
22570 return unify (tparms, targs, parm_max, arg_max,
22571 UNIFY_ALLOW_INTEGER, explain_p);
22572 }
22573
22574 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22575
22576 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22577
22578 static pa_kind_t
22579 pa_kind (tree t)
22580 {
22581 if (PACK_EXPANSION_P (t))
22582 t = PACK_EXPANSION_PATTERN (t);
22583 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22584 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22585 || DECL_TYPE_TEMPLATE_P (t))
22586 return pa_tmpl;
22587 else if (TYPE_P (t))
22588 return pa_type;
22589 else
22590 return pa_expr;
22591 }
22592
22593 /* Deduce the value of template parameters. TPARMS is the (innermost)
22594 set of template parameters to a template. TARGS is the bindings
22595 for those template parameters, as determined thus far; TARGS may
22596 include template arguments for outer levels of template parameters
22597 as well. PARM is a parameter to a template function, or a
22598 subcomponent of that parameter; ARG is the corresponding argument.
22599 This function attempts to match PARM with ARG in a manner
22600 consistent with the existing assignments in TARGS. If more values
22601 are deduced, then TARGS is updated.
22602
22603 Returns 0 if the type deduction succeeds, 1 otherwise. The
22604 parameter STRICT is a bitwise or of the following flags:
22605
22606 UNIFY_ALLOW_NONE:
22607 Require an exact match between PARM and ARG.
22608 UNIFY_ALLOW_MORE_CV_QUAL:
22609 Allow the deduced ARG to be more cv-qualified (by qualification
22610 conversion) than ARG.
22611 UNIFY_ALLOW_LESS_CV_QUAL:
22612 Allow the deduced ARG to be less cv-qualified than ARG.
22613 UNIFY_ALLOW_DERIVED:
22614 Allow the deduced ARG to be a template base class of ARG,
22615 or a pointer to a template base class of the type pointed to by
22616 ARG.
22617 UNIFY_ALLOW_INTEGER:
22618 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22619 case for more information.
22620 UNIFY_ALLOW_OUTER_LEVEL:
22621 This is the outermost level of a deduction. Used to determine validity
22622 of qualification conversions. A valid qualification conversion must
22623 have const qualified pointers leading up to the inner type which
22624 requires additional CV quals, except at the outer level, where const
22625 is not required [conv.qual]. It would be normal to set this flag in
22626 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22627 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22628 This is the outermost level of a deduction, and PARM can be more CV
22629 qualified at this point.
22630 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22631 This is the outermost level of a deduction, and PARM can be less CV
22632 qualified at this point. */
22633
22634 static int
22635 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22636 bool explain_p)
22637 {
22638 int idx;
22639 tree targ;
22640 tree tparm;
22641 int strict_in = strict;
22642 tsubst_flags_t complain = (explain_p
22643 ? tf_warning_or_error
22644 : tf_none);
22645
22646 /* I don't think this will do the right thing with respect to types.
22647 But the only case I've seen it in so far has been array bounds, where
22648 signedness is the only information lost, and I think that will be
22649 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22650 finish_id_expression_1, and are also OK. */
22651 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22652 parm = TREE_OPERAND (parm, 0);
22653
22654 if (arg == error_mark_node)
22655 return unify_invalid (explain_p);
22656 if (arg == unknown_type_node
22657 || arg == init_list_type_node)
22658 /* We can't deduce anything from this, but we might get all the
22659 template args from other function args. */
22660 return unify_success (explain_p);
22661
22662 if (parm == any_targ_node || arg == any_targ_node)
22663 return unify_success (explain_p);
22664
22665 /* If PARM uses template parameters, then we can't bail out here,
22666 even if ARG == PARM, since we won't record unifications for the
22667 template parameters. We might need them if we're trying to
22668 figure out which of two things is more specialized. */
22669 if (arg == parm && !uses_template_parms (parm))
22670 return unify_success (explain_p);
22671
22672 /* Handle init lists early, so the rest of the function can assume
22673 we're dealing with a type. */
22674 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22675 {
22676 tree elt, elttype;
22677 unsigned i;
22678 tree orig_parm = parm;
22679
22680 if (!is_std_init_list (parm)
22681 && TREE_CODE (parm) != ARRAY_TYPE)
22682 /* We can only deduce from an initializer list argument if the
22683 parameter is std::initializer_list or an array; otherwise this
22684 is a non-deduced context. */
22685 return unify_success (explain_p);
22686
22687 if (TREE_CODE (parm) == ARRAY_TYPE)
22688 elttype = TREE_TYPE (parm);
22689 else
22690 {
22691 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22692 /* Deduction is defined in terms of a single type, so just punt
22693 on the (bizarre) std::initializer_list<T...>. */
22694 if (PACK_EXPANSION_P (elttype))
22695 return unify_success (explain_p);
22696 }
22697
22698 if (strict != DEDUCE_EXACT
22699 && TYPE_P (elttype)
22700 && !uses_deducible_template_parms (elttype))
22701 /* If ELTTYPE has no deducible template parms, skip deduction from
22702 the list elements. */;
22703 else
22704 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22705 {
22706 int elt_strict = strict;
22707
22708 if (elt == error_mark_node)
22709 return unify_invalid (explain_p);
22710
22711 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22712 {
22713 tree type = TREE_TYPE (elt);
22714 if (type == error_mark_node)
22715 return unify_invalid (explain_p);
22716 /* It should only be possible to get here for a call. */
22717 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22718 elt_strict |= maybe_adjust_types_for_deduction
22719 (DEDUCE_CALL, &elttype, &type, elt);
22720 elt = type;
22721 }
22722
22723 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22724 explain_p);
22725 }
22726
22727 if (TREE_CODE (parm) == ARRAY_TYPE
22728 && deducible_array_bound (TYPE_DOMAIN (parm)))
22729 {
22730 /* Also deduce from the length of the initializer list. */
22731 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22732 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22733 if (idx == error_mark_node)
22734 return unify_invalid (explain_p);
22735 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22736 idx, explain_p);
22737 }
22738
22739 /* If the std::initializer_list<T> deduction worked, replace the
22740 deduced A with std::initializer_list<A>. */
22741 if (orig_parm != parm)
22742 {
22743 idx = TEMPLATE_TYPE_IDX (orig_parm);
22744 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22745 targ = listify (targ);
22746 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22747 }
22748 return unify_success (explain_p);
22749 }
22750
22751 /* If parm and arg aren't the same kind of thing (template, type, or
22752 expression), fail early. */
22753 if (pa_kind (parm) != pa_kind (arg))
22754 return unify_invalid (explain_p);
22755
22756 /* Immediately reject some pairs that won't unify because of
22757 cv-qualification mismatches. */
22758 if (TREE_CODE (arg) == TREE_CODE (parm)
22759 && TYPE_P (arg)
22760 /* It is the elements of the array which hold the cv quals of an array
22761 type, and the elements might be template type parms. We'll check
22762 when we recurse. */
22763 && TREE_CODE (arg) != ARRAY_TYPE
22764 /* We check the cv-qualifiers when unifying with template type
22765 parameters below. We want to allow ARG `const T' to unify with
22766 PARM `T' for example, when computing which of two templates
22767 is more specialized, for example. */
22768 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22769 && !check_cv_quals_for_unify (strict_in, arg, parm))
22770 return unify_cv_qual_mismatch (explain_p, parm, arg);
22771
22772 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22773 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22774 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22775 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22776 strict &= ~UNIFY_ALLOW_DERIVED;
22777 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22778 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22779
22780 switch (TREE_CODE (parm))
22781 {
22782 case TYPENAME_TYPE:
22783 case SCOPE_REF:
22784 case UNBOUND_CLASS_TEMPLATE:
22785 /* In a type which contains a nested-name-specifier, template
22786 argument values cannot be deduced for template parameters used
22787 within the nested-name-specifier. */
22788 return unify_success (explain_p);
22789
22790 case TEMPLATE_TYPE_PARM:
22791 case TEMPLATE_TEMPLATE_PARM:
22792 case BOUND_TEMPLATE_TEMPLATE_PARM:
22793 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22794 if (error_operand_p (tparm))
22795 return unify_invalid (explain_p);
22796
22797 if (TEMPLATE_TYPE_LEVEL (parm)
22798 != template_decl_level (tparm))
22799 /* The PARM is not one we're trying to unify. Just check
22800 to see if it matches ARG. */
22801 {
22802 if (TREE_CODE (arg) == TREE_CODE (parm)
22803 && (is_auto (parm) ? is_auto (arg)
22804 : same_type_p (parm, arg)))
22805 return unify_success (explain_p);
22806 else
22807 return unify_type_mismatch (explain_p, parm, arg);
22808 }
22809 idx = TEMPLATE_TYPE_IDX (parm);
22810 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22811 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22812 if (error_operand_p (tparm))
22813 return unify_invalid (explain_p);
22814
22815 /* Check for mixed types and values. */
22816 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22817 && TREE_CODE (tparm) != TYPE_DECL)
22818 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22819 && TREE_CODE (tparm) != TEMPLATE_DECL))
22820 gcc_unreachable ();
22821
22822 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22823 {
22824 if ((strict_in & UNIFY_ALLOW_DERIVED)
22825 && CLASS_TYPE_P (arg))
22826 {
22827 /* First try to match ARG directly. */
22828 tree t = try_class_unification (tparms, targs, parm, arg,
22829 explain_p);
22830 if (!t)
22831 {
22832 /* Otherwise, look for a suitable base of ARG, as below. */
22833 enum template_base_result r;
22834 r = get_template_base (tparms, targs, parm, arg,
22835 explain_p, &t);
22836 if (!t)
22837 return unify_no_common_base (explain_p, r, parm, arg);
22838 arg = t;
22839 }
22840 }
22841 /* ARG must be constructed from a template class or a template
22842 template parameter. */
22843 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22844 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22845 return unify_template_deduction_failure (explain_p, parm, arg);
22846
22847 /* Deduce arguments T, i from TT<T> or TT<i>. */
22848 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22849 return 1;
22850
22851 arg = TYPE_TI_TEMPLATE (arg);
22852
22853 /* Fall through to deduce template name. */
22854 }
22855
22856 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22857 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22858 {
22859 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22860
22861 /* Simple cases: Value already set, does match or doesn't. */
22862 if (targ != NULL_TREE && template_args_equal (targ, arg))
22863 return unify_success (explain_p);
22864 else if (targ)
22865 return unify_inconsistency (explain_p, parm, targ, arg);
22866 }
22867 else
22868 {
22869 /* If PARM is `const T' and ARG is only `int', we don't have
22870 a match unless we are allowing additional qualification.
22871 If ARG is `const int' and PARM is just `T' that's OK;
22872 that binds `const int' to `T'. */
22873 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22874 arg, parm))
22875 return unify_cv_qual_mismatch (explain_p, parm, arg);
22876
22877 /* Consider the case where ARG is `const volatile int' and
22878 PARM is `const T'. Then, T should be `volatile int'. */
22879 arg = cp_build_qualified_type_real
22880 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22881 if (arg == error_mark_node)
22882 return unify_invalid (explain_p);
22883
22884 /* Simple cases: Value already set, does match or doesn't. */
22885 if (targ != NULL_TREE && same_type_p (targ, arg))
22886 return unify_success (explain_p);
22887 else if (targ)
22888 return unify_inconsistency (explain_p, parm, targ, arg);
22889
22890 /* Make sure that ARG is not a variable-sized array. (Note
22891 that were talking about variable-sized arrays (like
22892 `int[n]'), rather than arrays of unknown size (like
22893 `int[]').) We'll get very confused by such a type since
22894 the bound of the array is not constant, and therefore
22895 not mangleable. Besides, such types are not allowed in
22896 ISO C++, so we can do as we please here. We do allow
22897 them for 'auto' deduction, since that isn't ABI-exposed. */
22898 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22899 return unify_vla_arg (explain_p, arg);
22900
22901 /* Strip typedefs as in convert_template_argument. */
22902 arg = canonicalize_type_argument (arg, tf_none);
22903 }
22904
22905 /* If ARG is a parameter pack or an expansion, we cannot unify
22906 against it unless PARM is also a parameter pack. */
22907 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22908 && !template_parameter_pack_p (parm))
22909 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22910
22911 /* If the argument deduction results is a METHOD_TYPE,
22912 then there is a problem.
22913 METHOD_TYPE doesn't map to any real C++ type the result of
22914 the deduction cannot be of that type. */
22915 if (TREE_CODE (arg) == METHOD_TYPE)
22916 return unify_method_type_error (explain_p, arg);
22917
22918 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22919 return unify_success (explain_p);
22920
22921 case TEMPLATE_PARM_INDEX:
22922 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22923 if (error_operand_p (tparm))
22924 return unify_invalid (explain_p);
22925
22926 if (TEMPLATE_PARM_LEVEL (parm)
22927 != template_decl_level (tparm))
22928 {
22929 /* The PARM is not one we're trying to unify. Just check
22930 to see if it matches ARG. */
22931 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22932 && cp_tree_equal (parm, arg));
22933 if (result)
22934 unify_expression_unequal (explain_p, parm, arg);
22935 return result;
22936 }
22937
22938 idx = TEMPLATE_PARM_IDX (parm);
22939 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22940
22941 if (targ)
22942 {
22943 if ((strict & UNIFY_ALLOW_INTEGER)
22944 && TREE_TYPE (targ) && TREE_TYPE (arg)
22945 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22946 /* We're deducing from an array bound, the type doesn't matter. */
22947 arg = fold_convert (TREE_TYPE (targ), arg);
22948 int x = !cp_tree_equal (targ, arg);
22949 if (x)
22950 unify_inconsistency (explain_p, parm, targ, arg);
22951 return x;
22952 }
22953
22954 /* [temp.deduct.type] If, in the declaration of a function template
22955 with a non-type template-parameter, the non-type
22956 template-parameter is used in an expression in the function
22957 parameter-list and, if the corresponding template-argument is
22958 deduced, the template-argument type shall match the type of the
22959 template-parameter exactly, except that a template-argument
22960 deduced from an array bound may be of any integral type.
22961 The non-type parameter might use already deduced type parameters. */
22962 tparm = TREE_TYPE (parm);
22963 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22964 /* We don't have enough levels of args to do any substitution. This
22965 can happen in the context of -fnew-ttp-matching. */;
22966 else
22967 {
22968 ++processing_template_decl;
22969 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22970 --processing_template_decl;
22971
22972 if (tree a = type_uses_auto (tparm))
22973 {
22974 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22975 if (tparm == error_mark_node)
22976 return 1;
22977 }
22978 }
22979
22980 if (!TREE_TYPE (arg))
22981 /* Template-parameter dependent expression. Just accept it for now.
22982 It will later be processed in convert_template_argument. */
22983 ;
22984 else if (same_type_ignoring_top_level_qualifiers_p
22985 (non_reference (TREE_TYPE (arg)),
22986 non_reference (tparm)))
22987 /* OK. Ignore top-level quals here because a class-type template
22988 parameter object is const. */;
22989 else if ((strict & UNIFY_ALLOW_INTEGER)
22990 && CP_INTEGRAL_TYPE_P (tparm))
22991 /* Convert the ARG to the type of PARM; the deduced non-type
22992 template argument must exactly match the types of the
22993 corresponding parameter. */
22994 arg = fold (build_nop (tparm, arg));
22995 else if (uses_template_parms (tparm))
22996 {
22997 /* We haven't deduced the type of this parameter yet. */
22998 if (cxx_dialect >= cxx17
22999 /* We deduce from array bounds in try_array_deduction. */
23000 && !(strict & UNIFY_ALLOW_INTEGER))
23001 {
23002 /* Deduce it from the non-type argument. */
23003 tree atype = TREE_TYPE (arg);
23004 RECUR_AND_CHECK_FAILURE (tparms, targs,
23005 tparm, atype,
23006 UNIFY_ALLOW_NONE, explain_p);
23007 }
23008 else
23009 /* Try again later. */
23010 return unify_success (explain_p);
23011 }
23012 else
23013 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23014
23015 /* If ARG is a parameter pack or an expansion, we cannot unify
23016 against it unless PARM is also a parameter pack. */
23017 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23018 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23019 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23020
23021 {
23022 bool removed_attr = false;
23023 arg = strip_typedefs_expr (arg, &removed_attr);
23024 }
23025 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23026 return unify_success (explain_p);
23027
23028 case PTRMEM_CST:
23029 {
23030 /* A pointer-to-member constant can be unified only with
23031 another constant. */
23032 if (TREE_CODE (arg) != PTRMEM_CST)
23033 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23034
23035 /* Just unify the class member. It would be useless (and possibly
23036 wrong, depending on the strict flags) to unify also
23037 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23038 arg refer to the same variable, even if through different
23039 classes. For instance:
23040
23041 struct A { int x; };
23042 struct B : A { };
23043
23044 Unification of &A::x and &B::x must succeed. */
23045 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23046 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23047 }
23048
23049 case POINTER_TYPE:
23050 {
23051 if (!TYPE_PTR_P (arg))
23052 return unify_type_mismatch (explain_p, parm, arg);
23053
23054 /* [temp.deduct.call]
23055
23056 A can be another pointer or pointer to member type that can
23057 be converted to the deduced A via a qualification
23058 conversion (_conv.qual_).
23059
23060 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23061 This will allow for additional cv-qualification of the
23062 pointed-to types if appropriate. */
23063
23064 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23065 /* The derived-to-base conversion only persists through one
23066 level of pointers. */
23067 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23068
23069 return unify (tparms, targs, TREE_TYPE (parm),
23070 TREE_TYPE (arg), strict, explain_p);
23071 }
23072
23073 case REFERENCE_TYPE:
23074 if (!TYPE_REF_P (arg))
23075 return unify_type_mismatch (explain_p, parm, arg);
23076 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23077 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23078
23079 case ARRAY_TYPE:
23080 if (TREE_CODE (arg) != ARRAY_TYPE)
23081 return unify_type_mismatch (explain_p, parm, arg);
23082 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23083 != (TYPE_DOMAIN (arg) == NULL_TREE))
23084 return unify_type_mismatch (explain_p, parm, arg);
23085 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23086 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23087 if (TYPE_DOMAIN (parm) != NULL_TREE)
23088 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23089 TYPE_DOMAIN (arg), explain_p);
23090 return unify_success (explain_p);
23091
23092 case REAL_TYPE:
23093 case COMPLEX_TYPE:
23094 case VECTOR_TYPE:
23095 case INTEGER_TYPE:
23096 case BOOLEAN_TYPE:
23097 case ENUMERAL_TYPE:
23098 case VOID_TYPE:
23099 case NULLPTR_TYPE:
23100 if (TREE_CODE (arg) != TREE_CODE (parm))
23101 return unify_type_mismatch (explain_p, parm, arg);
23102
23103 /* We have already checked cv-qualification at the top of the
23104 function. */
23105 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23106 return unify_type_mismatch (explain_p, parm, arg);
23107
23108 /* As far as unification is concerned, this wins. Later checks
23109 will invalidate it if necessary. */
23110 return unify_success (explain_p);
23111
23112 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23113 /* Type INTEGER_CST can come from ordinary constant template args. */
23114 case INTEGER_CST:
23115 while (CONVERT_EXPR_P (arg))
23116 arg = TREE_OPERAND (arg, 0);
23117
23118 if (TREE_CODE (arg) != INTEGER_CST)
23119 return unify_template_argument_mismatch (explain_p, parm, arg);
23120 return (tree_int_cst_equal (parm, arg)
23121 ? unify_success (explain_p)
23122 : unify_template_argument_mismatch (explain_p, parm, arg));
23123
23124 case TREE_VEC:
23125 {
23126 int i, len, argslen;
23127 int parm_variadic_p = 0;
23128
23129 if (TREE_CODE (arg) != TREE_VEC)
23130 return unify_template_argument_mismatch (explain_p, parm, arg);
23131
23132 len = TREE_VEC_LENGTH (parm);
23133 argslen = TREE_VEC_LENGTH (arg);
23134
23135 /* Check for pack expansions in the parameters. */
23136 for (i = 0; i < len; ++i)
23137 {
23138 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23139 {
23140 if (i == len - 1)
23141 /* We can unify against something with a trailing
23142 parameter pack. */
23143 parm_variadic_p = 1;
23144 else
23145 /* [temp.deduct.type]/9: If the template argument list of
23146 P contains a pack expansion that is not the last
23147 template argument, the entire template argument list
23148 is a non-deduced context. */
23149 return unify_success (explain_p);
23150 }
23151 }
23152
23153 /* If we don't have enough arguments to satisfy the parameters
23154 (not counting the pack expression at the end), or we have
23155 too many arguments for a parameter list that doesn't end in
23156 a pack expression, we can't unify. */
23157 if (parm_variadic_p
23158 ? argslen < len - parm_variadic_p
23159 : argslen != len)
23160 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23161
23162 /* Unify all of the parameters that precede the (optional)
23163 pack expression. */
23164 for (i = 0; i < len - parm_variadic_p; ++i)
23165 {
23166 RECUR_AND_CHECK_FAILURE (tparms, targs,
23167 TREE_VEC_ELT (parm, i),
23168 TREE_VEC_ELT (arg, i),
23169 UNIFY_ALLOW_NONE, explain_p);
23170 }
23171 if (parm_variadic_p)
23172 return unify_pack_expansion (tparms, targs, parm, arg,
23173 DEDUCE_EXACT,
23174 /*subr=*/true, explain_p);
23175 return unify_success (explain_p);
23176 }
23177
23178 case RECORD_TYPE:
23179 case UNION_TYPE:
23180 if (TREE_CODE (arg) != TREE_CODE (parm))
23181 return unify_type_mismatch (explain_p, parm, arg);
23182
23183 if (TYPE_PTRMEMFUNC_P (parm))
23184 {
23185 if (!TYPE_PTRMEMFUNC_P (arg))
23186 return unify_type_mismatch (explain_p, parm, arg);
23187
23188 return unify (tparms, targs,
23189 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23190 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23191 strict, explain_p);
23192 }
23193 else if (TYPE_PTRMEMFUNC_P (arg))
23194 return unify_type_mismatch (explain_p, parm, arg);
23195
23196 if (CLASSTYPE_TEMPLATE_INFO (parm))
23197 {
23198 tree t = NULL_TREE;
23199
23200 if (strict_in & UNIFY_ALLOW_DERIVED)
23201 {
23202 /* First, we try to unify the PARM and ARG directly. */
23203 t = try_class_unification (tparms, targs,
23204 parm, arg, explain_p);
23205
23206 if (!t)
23207 {
23208 /* Fallback to the special case allowed in
23209 [temp.deduct.call]:
23210
23211 If P is a class, and P has the form
23212 template-id, then A can be a derived class of
23213 the deduced A. Likewise, if P is a pointer to
23214 a class of the form template-id, A can be a
23215 pointer to a derived class pointed to by the
23216 deduced A. */
23217 enum template_base_result r;
23218 r = get_template_base (tparms, targs, parm, arg,
23219 explain_p, &t);
23220
23221 if (!t)
23222 {
23223 /* Don't give the derived diagnostic if we're
23224 already dealing with the same template. */
23225 bool same_template
23226 = (CLASSTYPE_TEMPLATE_INFO (arg)
23227 && (CLASSTYPE_TI_TEMPLATE (parm)
23228 == CLASSTYPE_TI_TEMPLATE (arg)));
23229 return unify_no_common_base (explain_p && !same_template,
23230 r, parm, arg);
23231 }
23232 }
23233 }
23234 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23235 && (CLASSTYPE_TI_TEMPLATE (parm)
23236 == CLASSTYPE_TI_TEMPLATE (arg)))
23237 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23238 Then, we should unify `int' and `U'. */
23239 t = arg;
23240 else
23241 /* There's no chance of unification succeeding. */
23242 return unify_type_mismatch (explain_p, parm, arg);
23243
23244 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23245 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23246 }
23247 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23248 return unify_type_mismatch (explain_p, parm, arg);
23249 return unify_success (explain_p);
23250
23251 case METHOD_TYPE:
23252 case FUNCTION_TYPE:
23253 {
23254 unsigned int nargs;
23255 tree *args;
23256 tree a;
23257 unsigned int i;
23258
23259 if (TREE_CODE (arg) != TREE_CODE (parm))
23260 return unify_type_mismatch (explain_p, parm, arg);
23261
23262 /* CV qualifications for methods can never be deduced, they must
23263 match exactly. We need to check them explicitly here,
23264 because type_unification_real treats them as any other
23265 cv-qualified parameter. */
23266 if (TREE_CODE (parm) == METHOD_TYPE
23267 && (!check_cv_quals_for_unify
23268 (UNIFY_ALLOW_NONE,
23269 class_of_this_parm (arg),
23270 class_of_this_parm (parm))))
23271 return unify_cv_qual_mismatch (explain_p, parm, arg);
23272 if (TREE_CODE (arg) == FUNCTION_TYPE
23273 && type_memfn_quals (parm) != type_memfn_quals (arg))
23274 return unify_cv_qual_mismatch (explain_p, parm, arg);
23275 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23276 return unify_type_mismatch (explain_p, parm, arg);
23277
23278 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23279 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23280
23281 nargs = list_length (TYPE_ARG_TYPES (arg));
23282 args = XALLOCAVEC (tree, nargs);
23283 for (a = TYPE_ARG_TYPES (arg), i = 0;
23284 a != NULL_TREE && a != void_list_node;
23285 a = TREE_CHAIN (a), ++i)
23286 args[i] = TREE_VALUE (a);
23287 nargs = i;
23288
23289 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23290 args, nargs, 1, DEDUCE_EXACT,
23291 NULL, explain_p))
23292 return 1;
23293
23294 if (flag_noexcept_type)
23295 {
23296 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23297 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23298 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23299 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23300 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23301 && uses_template_parms (TREE_PURPOSE (pspec)))
23302 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23303 TREE_PURPOSE (aspec),
23304 UNIFY_ALLOW_NONE, explain_p);
23305 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23306 return unify_type_mismatch (explain_p, parm, arg);
23307 }
23308
23309 return 0;
23310 }
23311
23312 case OFFSET_TYPE:
23313 /* Unify a pointer to member with a pointer to member function, which
23314 deduces the type of the member as a function type. */
23315 if (TYPE_PTRMEMFUNC_P (arg))
23316 {
23317 /* Check top-level cv qualifiers */
23318 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23319 return unify_cv_qual_mismatch (explain_p, parm, arg);
23320
23321 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23322 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23323 UNIFY_ALLOW_NONE, explain_p);
23324
23325 /* Determine the type of the function we are unifying against. */
23326 tree fntype = static_fn_type (arg);
23327
23328 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23329 }
23330
23331 if (TREE_CODE (arg) != OFFSET_TYPE)
23332 return unify_type_mismatch (explain_p, parm, arg);
23333 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23334 TYPE_OFFSET_BASETYPE (arg),
23335 UNIFY_ALLOW_NONE, explain_p);
23336 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23337 strict, explain_p);
23338
23339 case CONST_DECL:
23340 if (DECL_TEMPLATE_PARM_P (parm))
23341 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23342 if (arg != scalar_constant_value (parm))
23343 return unify_template_argument_mismatch (explain_p, parm, arg);
23344 return unify_success (explain_p);
23345
23346 case FIELD_DECL:
23347 case TEMPLATE_DECL:
23348 /* Matched cases are handled by the ARG == PARM test above. */
23349 return unify_template_argument_mismatch (explain_p, parm, arg);
23350
23351 case VAR_DECL:
23352 /* We might get a variable as a non-type template argument in parm if the
23353 corresponding parameter is type-dependent. Make any necessary
23354 adjustments based on whether arg is a reference. */
23355 if (CONSTANT_CLASS_P (arg))
23356 parm = fold_non_dependent_expr (parm, complain);
23357 else if (REFERENCE_REF_P (arg))
23358 {
23359 tree sub = TREE_OPERAND (arg, 0);
23360 STRIP_NOPS (sub);
23361 if (TREE_CODE (sub) == ADDR_EXPR)
23362 arg = TREE_OPERAND (sub, 0);
23363 }
23364 /* Now use the normal expression code to check whether they match. */
23365 goto expr;
23366
23367 case TYPE_ARGUMENT_PACK:
23368 case NONTYPE_ARGUMENT_PACK:
23369 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23370 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23371
23372 case TYPEOF_TYPE:
23373 case DECLTYPE_TYPE:
23374 case UNDERLYING_TYPE:
23375 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23376 or UNDERLYING_TYPE nodes. */
23377 return unify_success (explain_p);
23378
23379 case ERROR_MARK:
23380 /* Unification fails if we hit an error node. */
23381 return unify_invalid (explain_p);
23382
23383 case INDIRECT_REF:
23384 if (REFERENCE_REF_P (parm))
23385 {
23386 bool pexp = PACK_EXPANSION_P (arg);
23387 if (pexp)
23388 arg = PACK_EXPANSION_PATTERN (arg);
23389 if (REFERENCE_REF_P (arg))
23390 arg = TREE_OPERAND (arg, 0);
23391 if (pexp)
23392 arg = make_pack_expansion (arg, complain);
23393 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23394 strict, explain_p);
23395 }
23396 /* FALLTHRU */
23397
23398 default:
23399 /* An unresolved overload is a nondeduced context. */
23400 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23401 return unify_success (explain_p);
23402 gcc_assert (EXPR_P (parm)
23403 || COMPOUND_LITERAL_P (parm)
23404 || TREE_CODE (parm) == TRAIT_EXPR);
23405 expr:
23406 /* We must be looking at an expression. This can happen with
23407 something like:
23408
23409 template <int I>
23410 void foo(S<I>, S<I + 2>);
23411
23412 or
23413
23414 template<typename T>
23415 void foo(A<T, T{}>);
23416
23417 This is a "non-deduced context":
23418
23419 [deduct.type]
23420
23421 The non-deduced contexts are:
23422
23423 --A non-type template argument or an array bound in which
23424 a subexpression references a template parameter.
23425
23426 In these cases, we assume deduction succeeded, but don't
23427 actually infer any unifications. */
23428
23429 if (!uses_template_parms (parm)
23430 && !template_args_equal (parm, arg))
23431 return unify_expression_unequal (explain_p, parm, arg);
23432 else
23433 return unify_success (explain_p);
23434 }
23435 }
23436 #undef RECUR_AND_CHECK_FAILURE
23437 \f
23438 /* Note that DECL can be defined in this translation unit, if
23439 required. */
23440
23441 static void
23442 mark_definable (tree decl)
23443 {
23444 tree clone;
23445 DECL_NOT_REALLY_EXTERN (decl) = 1;
23446 FOR_EACH_CLONE (clone, decl)
23447 DECL_NOT_REALLY_EXTERN (clone) = 1;
23448 }
23449
23450 /* Called if RESULT is explicitly instantiated, or is a member of an
23451 explicitly instantiated class. */
23452
23453 void
23454 mark_decl_instantiated (tree result, int extern_p)
23455 {
23456 SET_DECL_EXPLICIT_INSTANTIATION (result);
23457
23458 /* If this entity has already been written out, it's too late to
23459 make any modifications. */
23460 if (TREE_ASM_WRITTEN (result))
23461 return;
23462
23463 /* For anonymous namespace we don't need to do anything. */
23464 if (decl_anon_ns_mem_p (result))
23465 {
23466 gcc_assert (!TREE_PUBLIC (result));
23467 return;
23468 }
23469
23470 if (TREE_CODE (result) != FUNCTION_DECL)
23471 /* The TREE_PUBLIC flag for function declarations will have been
23472 set correctly by tsubst. */
23473 TREE_PUBLIC (result) = 1;
23474
23475 /* This might have been set by an earlier implicit instantiation. */
23476 DECL_COMDAT (result) = 0;
23477
23478 if (extern_p)
23479 DECL_NOT_REALLY_EXTERN (result) = 0;
23480 else
23481 {
23482 mark_definable (result);
23483 mark_needed (result);
23484 /* Always make artificials weak. */
23485 if (DECL_ARTIFICIAL (result) && flag_weak)
23486 comdat_linkage (result);
23487 /* For WIN32 we also want to put explicit instantiations in
23488 linkonce sections. */
23489 else if (TREE_PUBLIC (result))
23490 maybe_make_one_only (result);
23491 if (TREE_CODE (result) == FUNCTION_DECL
23492 && DECL_TEMPLATE_INSTANTIATED (result))
23493 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23494 since start_preparsed_function wouldn't have if we had an earlier
23495 extern explicit instantiation. */
23496 DECL_EXTERNAL (result) = 0;
23497 }
23498
23499 /* If EXTERN_P, then this function will not be emitted -- unless
23500 followed by an explicit instantiation, at which point its linkage
23501 will be adjusted. If !EXTERN_P, then this function will be
23502 emitted here. In neither circumstance do we want
23503 import_export_decl to adjust the linkage. */
23504 DECL_INTERFACE_KNOWN (result) = 1;
23505 }
23506
23507 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23508 important template arguments. If any are missing, we check whether
23509 they're important by using error_mark_node for substituting into any
23510 args that were used for partial ordering (the ones between ARGS and END)
23511 and seeing if it bubbles up. */
23512
23513 static bool
23514 check_undeduced_parms (tree targs, tree args, tree end)
23515 {
23516 bool found = false;
23517 int i;
23518 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23519 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23520 {
23521 found = true;
23522 TREE_VEC_ELT (targs, i) = error_mark_node;
23523 }
23524 if (found)
23525 {
23526 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23527 if (substed == error_mark_node)
23528 return true;
23529 }
23530 return false;
23531 }
23532
23533 /* Given two function templates PAT1 and PAT2, return:
23534
23535 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23536 -1 if PAT2 is more specialized than PAT1.
23537 0 if neither is more specialized.
23538
23539 LEN indicates the number of parameters we should consider
23540 (defaulted parameters should not be considered).
23541
23542 The 1998 std underspecified function template partial ordering, and
23543 DR214 addresses the issue. We take pairs of arguments, one from
23544 each of the templates, and deduce them against each other. One of
23545 the templates will be more specialized if all the *other*
23546 template's arguments deduce against its arguments and at least one
23547 of its arguments *does* *not* deduce against the other template's
23548 corresponding argument. Deduction is done as for class templates.
23549 The arguments used in deduction have reference and top level cv
23550 qualifiers removed. Iff both arguments were originally reference
23551 types *and* deduction succeeds in both directions, an lvalue reference
23552 wins against an rvalue reference and otherwise the template
23553 with the more cv-qualified argument wins for that pairing (if
23554 neither is more cv-qualified, they both are equal). Unlike regular
23555 deduction, after all the arguments have been deduced in this way,
23556 we do *not* verify the deduced template argument values can be
23557 substituted into non-deduced contexts.
23558
23559 The logic can be a bit confusing here, because we look at deduce1 and
23560 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23561 can find template arguments for pat1 to make arg1 look like arg2, that
23562 means that arg2 is at least as specialized as arg1. */
23563
23564 int
23565 more_specialized_fn (tree pat1, tree pat2, int len)
23566 {
23567 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23568 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23569 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23570 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23571 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23572 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23573 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23574 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23575 tree origs1, origs2;
23576 bool lose1 = false;
23577 bool lose2 = false;
23578
23579 /* Remove the this parameter from non-static member functions. If
23580 one is a non-static member function and the other is not a static
23581 member function, remove the first parameter from that function
23582 also. This situation occurs for operator functions where we
23583 locate both a member function (with this pointer) and non-member
23584 operator (with explicit first operand). */
23585 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23586 {
23587 len--; /* LEN is the number of significant arguments for DECL1 */
23588 args1 = TREE_CHAIN (args1);
23589 if (!DECL_STATIC_FUNCTION_P (decl2))
23590 args2 = TREE_CHAIN (args2);
23591 }
23592 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23593 {
23594 args2 = TREE_CHAIN (args2);
23595 if (!DECL_STATIC_FUNCTION_P (decl1))
23596 {
23597 len--;
23598 args1 = TREE_CHAIN (args1);
23599 }
23600 }
23601
23602 /* If only one is a conversion operator, they are unordered. */
23603 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23604 return 0;
23605
23606 /* Consider the return type for a conversion function */
23607 if (DECL_CONV_FN_P (decl1))
23608 {
23609 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23610 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23611 len++;
23612 }
23613
23614 processing_template_decl++;
23615
23616 origs1 = args1;
23617 origs2 = args2;
23618
23619 while (len--
23620 /* Stop when an ellipsis is seen. */
23621 && args1 != NULL_TREE && args2 != NULL_TREE)
23622 {
23623 tree arg1 = TREE_VALUE (args1);
23624 tree arg2 = TREE_VALUE (args2);
23625 int deduce1, deduce2;
23626 int quals1 = -1;
23627 int quals2 = -1;
23628 int ref1 = 0;
23629 int ref2 = 0;
23630
23631 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23632 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23633 {
23634 /* When both arguments are pack expansions, we need only
23635 unify the patterns themselves. */
23636 arg1 = PACK_EXPANSION_PATTERN (arg1);
23637 arg2 = PACK_EXPANSION_PATTERN (arg2);
23638
23639 /* This is the last comparison we need to do. */
23640 len = 0;
23641 }
23642
23643 /* DR 1847: If a particular P contains no template-parameters that
23644 participate in template argument deduction, that P is not used to
23645 determine the ordering. */
23646 if (!uses_deducible_template_parms (arg1)
23647 && !uses_deducible_template_parms (arg2))
23648 goto next;
23649
23650 if (TYPE_REF_P (arg1))
23651 {
23652 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23653 arg1 = TREE_TYPE (arg1);
23654 quals1 = cp_type_quals (arg1);
23655 }
23656
23657 if (TYPE_REF_P (arg2))
23658 {
23659 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23660 arg2 = TREE_TYPE (arg2);
23661 quals2 = cp_type_quals (arg2);
23662 }
23663
23664 arg1 = TYPE_MAIN_VARIANT (arg1);
23665 arg2 = TYPE_MAIN_VARIANT (arg2);
23666
23667 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23668 {
23669 int i, len2 = remaining_arguments (args2);
23670 tree parmvec = make_tree_vec (1);
23671 tree argvec = make_tree_vec (len2);
23672 tree ta = args2;
23673
23674 /* Setup the parameter vector, which contains only ARG1. */
23675 TREE_VEC_ELT (parmvec, 0) = arg1;
23676
23677 /* Setup the argument vector, which contains the remaining
23678 arguments. */
23679 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23680 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23681
23682 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23683 argvec, DEDUCE_EXACT,
23684 /*subr=*/true, /*explain_p=*/false)
23685 == 0);
23686
23687 /* We cannot deduce in the other direction, because ARG1 is
23688 a pack expansion but ARG2 is not. */
23689 deduce2 = 0;
23690 }
23691 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23692 {
23693 int i, len1 = remaining_arguments (args1);
23694 tree parmvec = make_tree_vec (1);
23695 tree argvec = make_tree_vec (len1);
23696 tree ta = args1;
23697
23698 /* Setup the parameter vector, which contains only ARG1. */
23699 TREE_VEC_ELT (parmvec, 0) = arg2;
23700
23701 /* Setup the argument vector, which contains the remaining
23702 arguments. */
23703 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23704 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23705
23706 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23707 argvec, DEDUCE_EXACT,
23708 /*subr=*/true, /*explain_p=*/false)
23709 == 0);
23710
23711 /* We cannot deduce in the other direction, because ARG2 is
23712 a pack expansion but ARG1 is not.*/
23713 deduce1 = 0;
23714 }
23715
23716 else
23717 {
23718 /* The normal case, where neither argument is a pack
23719 expansion. */
23720 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23721 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23722 == 0);
23723 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23724 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23725 == 0);
23726 }
23727
23728 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23729 arg2, then arg2 is not as specialized as arg1. */
23730 if (!deduce1)
23731 lose2 = true;
23732 if (!deduce2)
23733 lose1 = true;
23734
23735 /* "If, for a given type, deduction succeeds in both directions
23736 (i.e., the types are identical after the transformations above)
23737 and both P and A were reference types (before being replaced with
23738 the type referred to above):
23739 - if the type from the argument template was an lvalue reference and
23740 the type from the parameter template was not, the argument type is
23741 considered to be more specialized than the other; otherwise,
23742 - if the type from the argument template is more cv-qualified
23743 than the type from the parameter template (as described above),
23744 the argument type is considered to be more specialized than the other;
23745 otherwise,
23746 - neither type is more specialized than the other." */
23747
23748 if (deduce1 && deduce2)
23749 {
23750 if (ref1 && ref2 && ref1 != ref2)
23751 {
23752 if (ref1 > ref2)
23753 lose1 = true;
23754 else
23755 lose2 = true;
23756 }
23757 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23758 {
23759 if ((quals1 & quals2) == quals2)
23760 lose2 = true;
23761 if ((quals1 & quals2) == quals1)
23762 lose1 = true;
23763 }
23764 }
23765
23766 if (lose1 && lose2)
23767 /* We've failed to deduce something in either direction.
23768 These must be unordered. */
23769 break;
23770
23771 next:
23772
23773 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23774 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23775 /* We have already processed all of the arguments in our
23776 handing of the pack expansion type. */
23777 len = 0;
23778
23779 args1 = TREE_CHAIN (args1);
23780 args2 = TREE_CHAIN (args2);
23781 }
23782
23783 /* "In most cases, all template parameters must have values in order for
23784 deduction to succeed, but for partial ordering purposes a template
23785 parameter may remain without a value provided it is not used in the
23786 types being used for partial ordering."
23787
23788 Thus, if we are missing any of the targs1 we need to substitute into
23789 origs1, then pat2 is not as specialized as pat1. This can happen when
23790 there is a nondeduced context. */
23791 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23792 lose2 = true;
23793 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23794 lose1 = true;
23795
23796 processing_template_decl--;
23797
23798 /* If both deductions succeed, the partial ordering selects the more
23799 constrained template. */
23800 if (!lose1 && !lose2)
23801 {
23802 int winner = more_constrained (decl1, decl2);
23803 if (winner > 0)
23804 lose2 = true;
23805 else if (winner < 0)
23806 lose1 = true;
23807 }
23808
23809 /* All things being equal, if the next argument is a pack expansion
23810 for one function but not for the other, prefer the
23811 non-variadic function. FIXME this is bogus; see c++/41958. */
23812 if (lose1 == lose2
23813 && args1 && TREE_VALUE (args1)
23814 && args2 && TREE_VALUE (args2))
23815 {
23816 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23817 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23818 }
23819
23820 if (lose1 == lose2)
23821 return 0;
23822 else if (!lose1)
23823 return 1;
23824 else
23825 return -1;
23826 }
23827
23828 /* Determine which of two partial specializations of TMPL is more
23829 specialized.
23830
23831 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23832 to the first partial specialization. The TREE_PURPOSE is the
23833 innermost set of template parameters for the partial
23834 specialization. PAT2 is similar, but for the second template.
23835
23836 Return 1 if the first partial specialization is more specialized;
23837 -1 if the second is more specialized; 0 if neither is more
23838 specialized.
23839
23840 See [temp.class.order] for information about determining which of
23841 two templates is more specialized. */
23842
23843 static int
23844 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23845 {
23846 tree targs;
23847 int winner = 0;
23848 bool any_deductions = false;
23849
23850 tree tmpl1 = TREE_VALUE (pat1);
23851 tree tmpl2 = TREE_VALUE (pat2);
23852 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23853 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23854
23855 /* Just like what happens for functions, if we are ordering between
23856 different template specializations, we may encounter dependent
23857 types in the arguments, and we need our dependency check functions
23858 to behave correctly. */
23859 ++processing_template_decl;
23860 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23861 if (targs)
23862 {
23863 --winner;
23864 any_deductions = true;
23865 }
23866
23867 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23868 if (targs)
23869 {
23870 ++winner;
23871 any_deductions = true;
23872 }
23873 --processing_template_decl;
23874
23875 /* If both deductions succeed, the partial ordering selects the more
23876 constrained template. */
23877 if (!winner && any_deductions)
23878 winner = more_constrained (tmpl1, tmpl2);
23879
23880 /* In the case of a tie where at least one of the templates
23881 has a parameter pack at the end, the template with the most
23882 non-packed parameters wins. */
23883 if (winner == 0
23884 && any_deductions
23885 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23886 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23887 {
23888 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23889 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23890 int len1 = TREE_VEC_LENGTH (args1);
23891 int len2 = TREE_VEC_LENGTH (args2);
23892
23893 /* We don't count the pack expansion at the end. */
23894 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23895 --len1;
23896 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23897 --len2;
23898
23899 if (len1 > len2)
23900 return 1;
23901 else if (len1 < len2)
23902 return -1;
23903 }
23904
23905 return winner;
23906 }
23907
23908 /* Return the template arguments that will produce the function signature
23909 DECL from the function template FN, with the explicit template
23910 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23911 also match. Return NULL_TREE if no satisfactory arguments could be
23912 found. */
23913
23914 static tree
23915 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23916 {
23917 int ntparms = DECL_NTPARMS (fn);
23918 tree targs = make_tree_vec (ntparms);
23919 tree decl_type = TREE_TYPE (decl);
23920 tree decl_arg_types;
23921 tree *args;
23922 unsigned int nargs, ix;
23923 tree arg;
23924
23925 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23926
23927 /* Never do unification on the 'this' parameter. */
23928 decl_arg_types = skip_artificial_parms_for (decl,
23929 TYPE_ARG_TYPES (decl_type));
23930
23931 nargs = list_length (decl_arg_types);
23932 args = XALLOCAVEC (tree, nargs);
23933 for (arg = decl_arg_types, ix = 0;
23934 arg != NULL_TREE && arg != void_list_node;
23935 arg = TREE_CHAIN (arg), ++ix)
23936 args[ix] = TREE_VALUE (arg);
23937
23938 if (fn_type_unification (fn, explicit_args, targs,
23939 args, ix,
23940 (check_rettype || DECL_CONV_FN_P (fn)
23941 ? TREE_TYPE (decl_type) : NULL_TREE),
23942 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23943 /*explain_p=*/false,
23944 /*decltype*/false)
23945 == error_mark_node)
23946 return NULL_TREE;
23947
23948 return targs;
23949 }
23950
23951 /* Return the innermost template arguments that, when applied to a partial
23952 specialization SPEC_TMPL of TMPL, yield the ARGS.
23953
23954 For example, suppose we have:
23955
23956 template <class T, class U> struct S {};
23957 template <class T> struct S<T*, int> {};
23958
23959 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23960 partial specialization and the ARGS will be {double*, int}. The resulting
23961 vector will be {double}, indicating that `T' is bound to `double'. */
23962
23963 static tree
23964 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23965 {
23966 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23967 tree spec_args
23968 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23969 int i, ntparms = TREE_VEC_LENGTH (tparms);
23970 tree deduced_args;
23971 tree innermost_deduced_args;
23972
23973 innermost_deduced_args = make_tree_vec (ntparms);
23974 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23975 {
23976 deduced_args = copy_node (args);
23977 SET_TMPL_ARGS_LEVEL (deduced_args,
23978 TMPL_ARGS_DEPTH (deduced_args),
23979 innermost_deduced_args);
23980 }
23981 else
23982 deduced_args = innermost_deduced_args;
23983
23984 bool tried_array_deduction = (cxx_dialect < cxx17);
23985 again:
23986 if (unify (tparms, deduced_args,
23987 INNERMOST_TEMPLATE_ARGS (spec_args),
23988 INNERMOST_TEMPLATE_ARGS (args),
23989 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23990 return NULL_TREE;
23991
23992 for (i = 0; i < ntparms; ++i)
23993 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23994 {
23995 if (!tried_array_deduction)
23996 {
23997 try_array_deduction (tparms, innermost_deduced_args,
23998 INNERMOST_TEMPLATE_ARGS (spec_args));
23999 tried_array_deduction = true;
24000 if (TREE_VEC_ELT (innermost_deduced_args, i))
24001 goto again;
24002 }
24003 return NULL_TREE;
24004 }
24005
24006 if (!push_tinst_level (spec_tmpl, deduced_args))
24007 {
24008 excessive_deduction_depth = true;
24009 return NULL_TREE;
24010 }
24011
24012 /* Verify that nondeduced template arguments agree with the type
24013 obtained from argument deduction.
24014
24015 For example:
24016
24017 struct A { typedef int X; };
24018 template <class T, class U> struct C {};
24019 template <class T> struct C<T, typename T::X> {};
24020
24021 Then with the instantiation `C<A, int>', we can deduce that
24022 `T' is `A' but unify () does not check whether `typename T::X'
24023 is `int'. */
24024 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24025
24026 if (spec_args != error_mark_node)
24027 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24028 INNERMOST_TEMPLATE_ARGS (spec_args),
24029 tmpl, tf_none, false, false);
24030
24031 pop_tinst_level ();
24032
24033 if (spec_args == error_mark_node
24034 /* We only need to check the innermost arguments; the other
24035 arguments will always agree. */
24036 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24037 INNERMOST_TEMPLATE_ARGS (args)))
24038 return NULL_TREE;
24039
24040 /* Now that we have bindings for all of the template arguments,
24041 ensure that the arguments deduced for the template template
24042 parameters have compatible template parameter lists. See the use
24043 of template_template_parm_bindings_ok_p in fn_type_unification
24044 for more information. */
24045 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24046 return NULL_TREE;
24047
24048 return deduced_args;
24049 }
24050
24051 // Compare two function templates T1 and T2 by deducing bindings
24052 // from one against the other. If both deductions succeed, compare
24053 // constraints to see which is more constrained.
24054 static int
24055 more_specialized_inst (tree t1, tree t2)
24056 {
24057 int fate = 0;
24058 int count = 0;
24059
24060 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24061 {
24062 --fate;
24063 ++count;
24064 }
24065
24066 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24067 {
24068 ++fate;
24069 ++count;
24070 }
24071
24072 // If both deductions succeed, then one may be more constrained.
24073 if (count == 2 && fate == 0)
24074 fate = more_constrained (t1, t2);
24075
24076 return fate;
24077 }
24078
24079 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24080 Return the TREE_LIST node with the most specialized template, if
24081 any. If there is no most specialized template, the error_mark_node
24082 is returned.
24083
24084 Note that this function does not look at, or modify, the
24085 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24086 returned is one of the elements of INSTANTIATIONS, callers may
24087 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24088 and retrieve it from the value returned. */
24089
24090 tree
24091 most_specialized_instantiation (tree templates)
24092 {
24093 tree fn, champ;
24094
24095 ++processing_template_decl;
24096
24097 champ = templates;
24098 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24099 {
24100 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24101 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24102 if (fate == -1)
24103 champ = fn;
24104 else if (!fate)
24105 {
24106 /* Equally specialized, move to next function. If there
24107 is no next function, nothing's most specialized. */
24108 fn = TREE_CHAIN (fn);
24109 champ = fn;
24110 if (!fn)
24111 break;
24112 }
24113 }
24114
24115 if (champ)
24116 /* Now verify that champ is better than everything earlier in the
24117 instantiation list. */
24118 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24119 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24120 {
24121 champ = NULL_TREE;
24122 break;
24123 }
24124 }
24125
24126 processing_template_decl--;
24127
24128 if (!champ)
24129 return error_mark_node;
24130
24131 return champ;
24132 }
24133
24134 /* If DECL is a specialization of some template, return the most
24135 general such template. Otherwise, returns NULL_TREE.
24136
24137 For example, given:
24138
24139 template <class T> struct S { template <class U> void f(U); };
24140
24141 if TMPL is `template <class U> void S<int>::f(U)' this will return
24142 the full template. This function will not trace past partial
24143 specializations, however. For example, given in addition:
24144
24145 template <class T> struct S<T*> { template <class U> void f(U); };
24146
24147 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24148 `template <class T> template <class U> S<T*>::f(U)'. */
24149
24150 tree
24151 most_general_template (tree decl)
24152 {
24153 if (TREE_CODE (decl) != TEMPLATE_DECL)
24154 {
24155 if (tree tinfo = get_template_info (decl))
24156 decl = TI_TEMPLATE (tinfo);
24157 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24158 template friend, or a FIELD_DECL for a capture pack. */
24159 if (TREE_CODE (decl) != TEMPLATE_DECL)
24160 return NULL_TREE;
24161 }
24162
24163 /* Look for more and more general templates. */
24164 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24165 {
24166 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24167 (See cp-tree.h for details.) */
24168 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24169 break;
24170
24171 if (CLASS_TYPE_P (TREE_TYPE (decl))
24172 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24173 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24174 break;
24175
24176 /* Stop if we run into an explicitly specialized class template. */
24177 if (!DECL_NAMESPACE_SCOPE_P (decl)
24178 && DECL_CONTEXT (decl)
24179 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24180 break;
24181
24182 decl = DECL_TI_TEMPLATE (decl);
24183 }
24184
24185 return decl;
24186 }
24187
24188 /* Return the most specialized of the template partial specializations
24189 which can produce TARGET, a specialization of some class or variable
24190 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24191 a TEMPLATE_DECL node corresponding to the partial specialization, while
24192 the TREE_PURPOSE is the set of template arguments that must be
24193 substituted into the template pattern in order to generate TARGET.
24194
24195 If the choice of partial specialization is ambiguous, a diagnostic
24196 is issued, and the error_mark_node is returned. If there are no
24197 partial specializations matching TARGET, then NULL_TREE is
24198 returned, indicating that the primary template should be used. */
24199
24200 static tree
24201 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24202 {
24203 tree list = NULL_TREE;
24204 tree t;
24205 tree champ;
24206 int fate;
24207 bool ambiguous_p;
24208 tree outer_args = NULL_TREE;
24209 tree tmpl, args;
24210
24211 if (TYPE_P (target))
24212 {
24213 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24214 tmpl = TI_TEMPLATE (tinfo);
24215 args = TI_ARGS (tinfo);
24216 }
24217 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24218 {
24219 tmpl = TREE_OPERAND (target, 0);
24220 args = TREE_OPERAND (target, 1);
24221 }
24222 else if (VAR_P (target))
24223 {
24224 tree tinfo = DECL_TEMPLATE_INFO (target);
24225 tmpl = TI_TEMPLATE (tinfo);
24226 args = TI_ARGS (tinfo);
24227 }
24228 else
24229 gcc_unreachable ();
24230
24231 tree main_tmpl = most_general_template (tmpl);
24232
24233 /* For determining which partial specialization to use, only the
24234 innermost args are interesting. */
24235 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24236 {
24237 outer_args = strip_innermost_template_args (args, 1);
24238 args = INNERMOST_TEMPLATE_ARGS (args);
24239 }
24240
24241 /* The caller hasn't called push_to_top_level yet, but we need
24242 get_partial_spec_bindings to be done in non-template context so that we'll
24243 fully resolve everything. */
24244 processing_template_decl_sentinel ptds;
24245
24246 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24247 {
24248 tree spec_args;
24249 tree spec_tmpl = TREE_VALUE (t);
24250
24251 if (outer_args)
24252 {
24253 /* Substitute in the template args from the enclosing class. */
24254 ++processing_template_decl;
24255 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24256 --processing_template_decl;
24257 }
24258
24259 if (spec_tmpl == error_mark_node)
24260 return error_mark_node;
24261
24262 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24263 if (spec_args)
24264 {
24265 if (outer_args)
24266 spec_args = add_to_template_args (outer_args, spec_args);
24267
24268 /* Keep the candidate only if the constraints are satisfied,
24269 or if we're not compiling with concepts. */
24270 if (!flag_concepts
24271 || constraints_satisfied_p (spec_tmpl, spec_args))
24272 {
24273 list = tree_cons (spec_args, TREE_VALUE (t), list);
24274 TREE_TYPE (list) = TREE_TYPE (t);
24275 }
24276 }
24277 }
24278
24279 if (! list)
24280 return NULL_TREE;
24281
24282 ambiguous_p = false;
24283 t = list;
24284 champ = t;
24285 t = TREE_CHAIN (t);
24286 for (; t; t = TREE_CHAIN (t))
24287 {
24288 fate = more_specialized_partial_spec (tmpl, champ, t);
24289 if (fate == 1)
24290 ;
24291 else
24292 {
24293 if (fate == 0)
24294 {
24295 t = TREE_CHAIN (t);
24296 if (! t)
24297 {
24298 ambiguous_p = true;
24299 break;
24300 }
24301 }
24302 champ = t;
24303 }
24304 }
24305
24306 if (!ambiguous_p)
24307 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24308 {
24309 fate = more_specialized_partial_spec (tmpl, champ, t);
24310 if (fate != 1)
24311 {
24312 ambiguous_p = true;
24313 break;
24314 }
24315 }
24316
24317 if (ambiguous_p)
24318 {
24319 const char *str;
24320 char *spaces = NULL;
24321 if (!(complain & tf_error))
24322 return error_mark_node;
24323 if (TYPE_P (target))
24324 error ("ambiguous template instantiation for %q#T", target);
24325 else
24326 error ("ambiguous template instantiation for %q#D", target);
24327 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24328 for (t = list; t; t = TREE_CHAIN (t))
24329 {
24330 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24331 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24332 "%s %#qS", spaces ? spaces : str, subst);
24333 spaces = spaces ? spaces : get_spaces (str);
24334 }
24335 free (spaces);
24336 return error_mark_node;
24337 }
24338
24339 return champ;
24340 }
24341
24342 /* Explicitly instantiate DECL. */
24343
24344 void
24345 do_decl_instantiation (tree decl, tree storage)
24346 {
24347 tree result = NULL_TREE;
24348 int extern_p = 0;
24349
24350 if (!decl || decl == error_mark_node)
24351 /* An error occurred, for which grokdeclarator has already issued
24352 an appropriate message. */
24353 return;
24354 else if (! DECL_LANG_SPECIFIC (decl))
24355 {
24356 error ("explicit instantiation of non-template %q#D", decl);
24357 return;
24358 }
24359 else if (DECL_DECLARED_CONCEPT_P (decl))
24360 {
24361 if (VAR_P (decl))
24362 error ("explicit instantiation of variable concept %q#D", decl);
24363 else
24364 error ("explicit instantiation of function concept %q#D", decl);
24365 return;
24366 }
24367
24368 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24369 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24370
24371 if (VAR_P (decl) && !var_templ)
24372 {
24373 /* There is an asymmetry here in the way VAR_DECLs and
24374 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24375 the latter, the DECL we get back will be marked as a
24376 template instantiation, and the appropriate
24377 DECL_TEMPLATE_INFO will be set up. This does not happen for
24378 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24379 should handle VAR_DECLs as it currently handles
24380 FUNCTION_DECLs. */
24381 if (!DECL_CLASS_SCOPE_P (decl))
24382 {
24383 error ("%qD is not a static data member of a class template", decl);
24384 return;
24385 }
24386 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24387 if (!result || !VAR_P (result))
24388 {
24389 error ("no matching template for %qD found", decl);
24390 return;
24391 }
24392 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24393 {
24394 error ("type %qT for explicit instantiation %qD does not match "
24395 "declared type %qT", TREE_TYPE (result), decl,
24396 TREE_TYPE (decl));
24397 return;
24398 }
24399 }
24400 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24401 {
24402 error ("explicit instantiation of %q#D", decl);
24403 return;
24404 }
24405 else
24406 result = decl;
24407
24408 /* Check for various error cases. Note that if the explicit
24409 instantiation is valid the RESULT will currently be marked as an
24410 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24411 until we get here. */
24412
24413 if (DECL_TEMPLATE_SPECIALIZATION (result))
24414 {
24415 /* DR 259 [temp.spec].
24416
24417 Both an explicit instantiation and a declaration of an explicit
24418 specialization shall not appear in a program unless the explicit
24419 instantiation follows a declaration of the explicit specialization.
24420
24421 For a given set of template parameters, if an explicit
24422 instantiation of a template appears after a declaration of an
24423 explicit specialization for that template, the explicit
24424 instantiation has no effect. */
24425 return;
24426 }
24427 else if (DECL_EXPLICIT_INSTANTIATION (result))
24428 {
24429 /* [temp.spec]
24430
24431 No program shall explicitly instantiate any template more
24432 than once.
24433
24434 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24435 the first instantiation was `extern' and the second is not,
24436 and EXTERN_P for the opposite case. */
24437 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24438 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24439 /* If an "extern" explicit instantiation follows an ordinary
24440 explicit instantiation, the template is instantiated. */
24441 if (extern_p)
24442 return;
24443 }
24444 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24445 {
24446 error ("no matching template for %qD found", result);
24447 return;
24448 }
24449 else if (!DECL_TEMPLATE_INFO (result))
24450 {
24451 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24452 return;
24453 }
24454
24455 if (storage == NULL_TREE)
24456 ;
24457 else if (storage == ridpointers[(int) RID_EXTERN])
24458 {
24459 if (cxx_dialect == cxx98)
24460 pedwarn (input_location, OPT_Wpedantic,
24461 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24462 "instantiations");
24463 extern_p = 1;
24464 }
24465 else
24466 error ("storage class %qD applied to template instantiation", storage);
24467
24468 check_explicit_instantiation_namespace (result);
24469 mark_decl_instantiated (result, extern_p);
24470 if (! extern_p)
24471 instantiate_decl (result, /*defer_ok=*/true,
24472 /*expl_inst_class_mem_p=*/false);
24473 }
24474
24475 static void
24476 mark_class_instantiated (tree t, int extern_p)
24477 {
24478 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24479 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24480 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24481 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24482 if (! extern_p)
24483 {
24484 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24485 rest_of_type_compilation (t, 1);
24486 }
24487 }
24488
24489 /* Called from do_type_instantiation through binding_table_foreach to
24490 do recursive instantiation for the type bound in ENTRY. */
24491 static void
24492 bt_instantiate_type_proc (binding_entry entry, void *data)
24493 {
24494 tree storage = *(tree *) data;
24495
24496 if (MAYBE_CLASS_TYPE_P (entry->type)
24497 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24498 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24499 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24500 }
24501
24502 /* Perform an explicit instantiation of template class T. STORAGE, if
24503 non-null, is the RID for extern, inline or static. COMPLAIN is
24504 nonzero if this is called from the parser, zero if called recursively,
24505 since the standard is unclear (as detailed below). */
24506
24507 void
24508 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24509 {
24510 int extern_p = 0;
24511 int nomem_p = 0;
24512 int static_p = 0;
24513 int previous_instantiation_extern_p = 0;
24514
24515 if (TREE_CODE (t) == TYPE_DECL)
24516 t = TREE_TYPE (t);
24517
24518 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24519 {
24520 tree tmpl =
24521 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24522 if (tmpl)
24523 error ("explicit instantiation of non-class template %qD", tmpl);
24524 else
24525 error ("explicit instantiation of non-template type %qT", t);
24526 return;
24527 }
24528
24529 complete_type (t);
24530
24531 if (!COMPLETE_TYPE_P (t))
24532 {
24533 if (complain & tf_error)
24534 error ("explicit instantiation of %q#T before definition of template",
24535 t);
24536 return;
24537 }
24538
24539 if (storage != NULL_TREE)
24540 {
24541 if (storage == ridpointers[(int) RID_EXTERN])
24542 {
24543 if (cxx_dialect == cxx98)
24544 pedwarn (input_location, OPT_Wpedantic,
24545 "ISO C++ 1998 forbids the use of %<extern%> on "
24546 "explicit instantiations");
24547 }
24548 else
24549 pedwarn (input_location, OPT_Wpedantic,
24550 "ISO C++ forbids the use of %qE"
24551 " on explicit instantiations", storage);
24552
24553 if (storage == ridpointers[(int) RID_INLINE])
24554 nomem_p = 1;
24555 else if (storage == ridpointers[(int) RID_EXTERN])
24556 extern_p = 1;
24557 else if (storage == ridpointers[(int) RID_STATIC])
24558 static_p = 1;
24559 else
24560 {
24561 error ("storage class %qD applied to template instantiation",
24562 storage);
24563 extern_p = 0;
24564 }
24565 }
24566
24567 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24568 {
24569 /* DR 259 [temp.spec].
24570
24571 Both an explicit instantiation and a declaration of an explicit
24572 specialization shall not appear in a program unless the explicit
24573 instantiation follows a declaration of the explicit specialization.
24574
24575 For a given set of template parameters, if an explicit
24576 instantiation of a template appears after a declaration of an
24577 explicit specialization for that template, the explicit
24578 instantiation has no effect. */
24579 return;
24580 }
24581 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24582 {
24583 /* [temp.spec]
24584
24585 No program shall explicitly instantiate any template more
24586 than once.
24587
24588 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24589 instantiation was `extern'. If EXTERN_P then the second is.
24590 These cases are OK. */
24591 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24592
24593 if (!previous_instantiation_extern_p && !extern_p
24594 && (complain & tf_error))
24595 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24596
24597 /* If we've already instantiated the template, just return now. */
24598 if (!CLASSTYPE_INTERFACE_ONLY (t))
24599 return;
24600 }
24601
24602 check_explicit_instantiation_namespace (TYPE_NAME (t));
24603 mark_class_instantiated (t, extern_p);
24604
24605 if (nomem_p)
24606 return;
24607
24608 /* In contrast to implicit instantiation, where only the
24609 declarations, and not the definitions, of members are
24610 instantiated, we have here:
24611
24612 [temp.explicit]
24613
24614 The explicit instantiation of a class template specialization
24615 implies the instantiation of all of its members not
24616 previously explicitly specialized in the translation unit
24617 containing the explicit instantiation.
24618
24619 Of course, we can't instantiate member template classes, since we
24620 don't have any arguments for them. Note that the standard is
24621 unclear on whether the instantiation of the members are
24622 *explicit* instantiations or not. However, the most natural
24623 interpretation is that it should be an explicit
24624 instantiation. */
24625 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24626 if ((VAR_P (fld)
24627 || (TREE_CODE (fld) == FUNCTION_DECL
24628 && !static_p
24629 && user_provided_p (fld)))
24630 && DECL_TEMPLATE_INSTANTIATION (fld))
24631 {
24632 mark_decl_instantiated (fld, extern_p);
24633 if (! extern_p)
24634 instantiate_decl (fld, /*defer_ok=*/true,
24635 /*expl_inst_class_mem_p=*/true);
24636 }
24637
24638 if (CLASSTYPE_NESTED_UTDS (t))
24639 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24640 bt_instantiate_type_proc, &storage);
24641 }
24642
24643 /* Given a function DECL, which is a specialization of TMPL, modify
24644 DECL to be a re-instantiation of TMPL with the same template
24645 arguments. TMPL should be the template into which tsubst'ing
24646 should occur for DECL, not the most general template.
24647
24648 One reason for doing this is a scenario like this:
24649
24650 template <class T>
24651 void f(const T&, int i);
24652
24653 void g() { f(3, 7); }
24654
24655 template <class T>
24656 void f(const T& t, const int i) { }
24657
24658 Note that when the template is first instantiated, with
24659 instantiate_template, the resulting DECL will have no name for the
24660 first parameter, and the wrong type for the second. So, when we go
24661 to instantiate the DECL, we regenerate it. */
24662
24663 static void
24664 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24665 {
24666 /* The arguments used to instantiate DECL, from the most general
24667 template. */
24668 tree code_pattern;
24669
24670 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24671
24672 /* Make sure that we can see identifiers, and compute access
24673 correctly. */
24674 push_access_scope (decl);
24675
24676 if (TREE_CODE (decl) == FUNCTION_DECL)
24677 {
24678 tree decl_parm;
24679 tree pattern_parm;
24680 tree specs;
24681 int args_depth;
24682 int parms_depth;
24683
24684 args_depth = TMPL_ARGS_DEPTH (args);
24685 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24686 if (args_depth > parms_depth)
24687 args = get_innermost_template_args (args, parms_depth);
24688
24689 /* Instantiate a dynamic exception-specification. noexcept will be
24690 handled below. */
24691 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24692 if (TREE_VALUE (raises))
24693 {
24694 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24695 args, tf_error, NULL_TREE,
24696 /*defer_ok*/false);
24697 if (specs && specs != error_mark_node)
24698 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24699 specs);
24700 }
24701
24702 /* Merge parameter declarations. */
24703 decl_parm = skip_artificial_parms_for (decl,
24704 DECL_ARGUMENTS (decl));
24705 pattern_parm
24706 = skip_artificial_parms_for (code_pattern,
24707 DECL_ARGUMENTS (code_pattern));
24708 while (decl_parm && !DECL_PACK_P (pattern_parm))
24709 {
24710 tree parm_type;
24711 tree attributes;
24712
24713 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24714 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24715 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24716 NULL_TREE);
24717 parm_type = type_decays_to (parm_type);
24718 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24719 TREE_TYPE (decl_parm) = parm_type;
24720 attributes = DECL_ATTRIBUTES (pattern_parm);
24721 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24722 {
24723 DECL_ATTRIBUTES (decl_parm) = attributes;
24724 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24725 }
24726 decl_parm = DECL_CHAIN (decl_parm);
24727 pattern_parm = DECL_CHAIN (pattern_parm);
24728 }
24729 /* Merge any parameters that match with the function parameter
24730 pack. */
24731 if (pattern_parm && DECL_PACK_P (pattern_parm))
24732 {
24733 int i, len;
24734 tree expanded_types;
24735 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24736 the parameters in this function parameter pack. */
24737 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24738 args, tf_error, NULL_TREE);
24739 len = TREE_VEC_LENGTH (expanded_types);
24740 for (i = 0; i < len; i++)
24741 {
24742 tree parm_type;
24743 tree attributes;
24744
24745 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24746 /* Rename the parameter to include the index. */
24747 DECL_NAME (decl_parm) =
24748 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24749 parm_type = TREE_VEC_ELT (expanded_types, i);
24750 parm_type = type_decays_to (parm_type);
24751 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24752 TREE_TYPE (decl_parm) = parm_type;
24753 attributes = DECL_ATTRIBUTES (pattern_parm);
24754 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24755 {
24756 DECL_ATTRIBUTES (decl_parm) = attributes;
24757 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24758 }
24759 decl_parm = DECL_CHAIN (decl_parm);
24760 }
24761 }
24762 /* Merge additional specifiers from the CODE_PATTERN. */
24763 if (DECL_DECLARED_INLINE_P (code_pattern)
24764 && !DECL_DECLARED_INLINE_P (decl))
24765 DECL_DECLARED_INLINE_P (decl) = 1;
24766
24767 maybe_instantiate_noexcept (decl, tf_error);
24768 }
24769 else if (VAR_P (decl))
24770 {
24771 start_lambda_scope (decl);
24772 DECL_INITIAL (decl) =
24773 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24774 tf_error, DECL_TI_TEMPLATE (decl));
24775 finish_lambda_scope ();
24776 if (VAR_HAD_UNKNOWN_BOUND (decl))
24777 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24778 tf_error, DECL_TI_TEMPLATE (decl));
24779 }
24780 else
24781 gcc_unreachable ();
24782
24783 pop_access_scope (decl);
24784 }
24785
24786 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24787 substituted to get DECL. */
24788
24789 tree
24790 template_for_substitution (tree decl)
24791 {
24792 tree tmpl = DECL_TI_TEMPLATE (decl);
24793
24794 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24795 for the instantiation. This is not always the most general
24796 template. Consider, for example:
24797
24798 template <class T>
24799 struct S { template <class U> void f();
24800 template <> void f<int>(); };
24801
24802 and an instantiation of S<double>::f<int>. We want TD to be the
24803 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24804 while (/* An instantiation cannot have a definition, so we need a
24805 more general template. */
24806 DECL_TEMPLATE_INSTANTIATION (tmpl)
24807 /* We must also deal with friend templates. Given:
24808
24809 template <class T> struct S {
24810 template <class U> friend void f() {};
24811 };
24812
24813 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24814 so far as the language is concerned, but that's still
24815 where we get the pattern for the instantiation from. On
24816 other hand, if the definition comes outside the class, say:
24817
24818 template <class T> struct S {
24819 template <class U> friend void f();
24820 };
24821 template <class U> friend void f() {}
24822
24823 we don't need to look any further. That's what the check for
24824 DECL_INITIAL is for. */
24825 || (TREE_CODE (decl) == FUNCTION_DECL
24826 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24827 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24828 {
24829 /* The present template, TD, should not be a definition. If it
24830 were a definition, we should be using it! Note that we
24831 cannot restructure the loop to just keep going until we find
24832 a template with a definition, since that might go too far if
24833 a specialization was declared, but not defined. */
24834
24835 /* Fetch the more general template. */
24836 tmpl = DECL_TI_TEMPLATE (tmpl);
24837 }
24838
24839 return tmpl;
24840 }
24841
24842 /* Returns true if we need to instantiate this template instance even if we
24843 know we aren't going to emit it. */
24844
24845 bool
24846 always_instantiate_p (tree decl)
24847 {
24848 /* We always instantiate inline functions so that we can inline them. An
24849 explicit instantiation declaration prohibits implicit instantiation of
24850 non-inline functions. With high levels of optimization, we would
24851 normally inline non-inline functions -- but we're not allowed to do
24852 that for "extern template" functions. Therefore, we check
24853 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24854 return ((TREE_CODE (decl) == FUNCTION_DECL
24855 && (DECL_DECLARED_INLINE_P (decl)
24856 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24857 /* And we need to instantiate static data members so that
24858 their initializers are available in integral constant
24859 expressions. */
24860 || (VAR_P (decl)
24861 && decl_maybe_constant_var_p (decl)));
24862 }
24863
24864 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24865 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24866 error, true otherwise. */
24867
24868 bool
24869 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24870 {
24871 tree fntype, spec, noex, clone;
24872
24873 /* Don't instantiate a noexcept-specification from template context. */
24874 if (processing_template_decl
24875 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24876 return true;
24877
24878 if (DECL_CLONED_FUNCTION_P (fn))
24879 fn = DECL_CLONED_FUNCTION (fn);
24880
24881 tree orig_fn = NULL_TREE;
24882 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24883 its FUNCTION_DECL for the rest of this function -- push_access_scope
24884 doesn't accept TEMPLATE_DECLs. */
24885 if (DECL_FUNCTION_TEMPLATE_P (fn))
24886 {
24887 orig_fn = fn;
24888 fn = DECL_TEMPLATE_RESULT (fn);
24889 }
24890
24891 fntype = TREE_TYPE (fn);
24892 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24893
24894 if (!spec || !TREE_PURPOSE (spec))
24895 return true;
24896
24897 noex = TREE_PURPOSE (spec);
24898
24899 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24900 {
24901 static hash_set<tree>* fns = new hash_set<tree>;
24902 bool added = false;
24903 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24904 {
24905 spec = get_defaulted_eh_spec (fn, complain);
24906 if (spec == error_mark_node)
24907 /* This might have failed because of an unparsed DMI, so
24908 let's try again later. */
24909 return false;
24910 }
24911 else if (!(added = !fns->add (fn)))
24912 {
24913 /* If hash_set::add returns true, the element was already there. */
24914 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24915 DECL_SOURCE_LOCATION (fn));
24916 error_at (loc,
24917 "exception specification of %qD depends on itself",
24918 fn);
24919 spec = noexcept_false_spec;
24920 }
24921 else if (push_tinst_level (fn))
24922 {
24923 push_to_top_level ();
24924 push_access_scope (fn);
24925 push_deferring_access_checks (dk_no_deferred);
24926 input_location = DECL_SOURCE_LOCATION (fn);
24927
24928 /* If needed, set current_class_ptr for the benefit of
24929 tsubst_copy/PARM_DECL. */
24930 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24931 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24932 {
24933 tree this_parm = DECL_ARGUMENTS (tdecl);
24934 current_class_ptr = NULL_TREE;
24935 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24936 current_class_ptr = this_parm;
24937 }
24938
24939 /* If this function is represented by a TEMPLATE_DECL, then
24940 the deferred noexcept-specification might still contain
24941 dependent types, even after substitution. And we need the
24942 dependency check functions to work in build_noexcept_spec. */
24943 if (orig_fn)
24944 ++processing_template_decl;
24945
24946 /* Do deferred instantiation of the noexcept-specifier. */
24947 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24948 DEFERRED_NOEXCEPT_ARGS (noex),
24949 tf_warning_or_error, fn,
24950 /*function_p=*/false,
24951 /*i_c_e_p=*/true);
24952
24953 /* Build up the noexcept-specification. */
24954 spec = build_noexcept_spec (noex, tf_warning_or_error);
24955
24956 if (orig_fn)
24957 --processing_template_decl;
24958
24959 pop_deferring_access_checks ();
24960 pop_access_scope (fn);
24961 pop_tinst_level ();
24962 pop_from_top_level ();
24963 }
24964 else
24965 spec = noexcept_false_spec;
24966
24967 if (added)
24968 fns->remove (fn);
24969
24970 if (spec == error_mark_node)
24971 {
24972 /* This failed with a hard error, so let's go with false. */
24973 gcc_assert (seen_error ());
24974 spec = noexcept_false_spec;
24975 }
24976
24977 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24978 if (orig_fn)
24979 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24980
24981 FOR_EACH_CLONE (clone, fn)
24982 {
24983 if (TREE_TYPE (clone) == fntype)
24984 TREE_TYPE (clone) = TREE_TYPE (fn);
24985 else
24986 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24987 }
24988 }
24989
24990 return true;
24991 }
24992
24993 /* We're starting to process the function INST, an instantiation of PATTERN;
24994 add their parameters to local_specializations. */
24995
24996 static void
24997 register_parameter_specializations (tree pattern, tree inst)
24998 {
24999 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25000 tree spec_parm = DECL_ARGUMENTS (inst);
25001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25002 {
25003 register_local_specialization (spec_parm, tmpl_parm);
25004 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25005 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25006 }
25007 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25008 {
25009 if (!DECL_PACK_P (tmpl_parm))
25010 {
25011 register_local_specialization (spec_parm, tmpl_parm);
25012 spec_parm = DECL_CHAIN (spec_parm);
25013 }
25014 else
25015 {
25016 /* Register the (value) argument pack as a specialization of
25017 TMPL_PARM, then move on. */
25018 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25019 register_local_specialization (argpack, tmpl_parm);
25020 }
25021 }
25022 gcc_assert (!spec_parm);
25023 }
25024
25025 /* Produce the definition of D, a _DECL generated from a template. If
25026 DEFER_OK is true, then we don't have to actually do the
25027 instantiation now; we just have to do it sometime. Normally it is
25028 an error if this is an explicit instantiation but D is undefined.
25029 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25030 instantiated class template. */
25031
25032 tree
25033 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25034 {
25035 tree tmpl = DECL_TI_TEMPLATE (d);
25036 tree gen_args;
25037 tree args;
25038 tree td;
25039 tree code_pattern;
25040 tree spec;
25041 tree gen_tmpl;
25042 bool pattern_defined;
25043 location_t saved_loc = input_location;
25044 int saved_unevaluated_operand = cp_unevaluated_operand;
25045 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25046 bool external_p;
25047 bool deleted_p;
25048
25049 /* This function should only be used to instantiate templates for
25050 functions and static member variables. */
25051 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25052
25053 /* A concept is never instantiated. */
25054 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25055
25056 /* Variables are never deferred; if instantiation is required, they
25057 are instantiated right away. That allows for better code in the
25058 case that an expression refers to the value of the variable --
25059 if the variable has a constant value the referring expression can
25060 take advantage of that fact. */
25061 if (VAR_P (d))
25062 defer_ok = false;
25063
25064 /* Don't instantiate cloned functions. Instead, instantiate the
25065 functions they cloned. */
25066 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25067 d = DECL_CLONED_FUNCTION (d);
25068
25069 if (DECL_TEMPLATE_INSTANTIATED (d)
25070 || (TREE_CODE (d) == FUNCTION_DECL
25071 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25072 || DECL_TEMPLATE_SPECIALIZATION (d))
25073 /* D has already been instantiated or explicitly specialized, so
25074 there's nothing for us to do here.
25075
25076 It might seem reasonable to check whether or not D is an explicit
25077 instantiation, and, if so, stop here. But when an explicit
25078 instantiation is deferred until the end of the compilation,
25079 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25080 the instantiation. */
25081 return d;
25082
25083 /* Check to see whether we know that this template will be
25084 instantiated in some other file, as with "extern template"
25085 extension. */
25086 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25087
25088 /* In general, we do not instantiate such templates. */
25089 if (external_p && !always_instantiate_p (d))
25090 return d;
25091
25092 gen_tmpl = most_general_template (tmpl);
25093 gen_args = DECL_TI_ARGS (d);
25094
25095 if (tmpl != gen_tmpl)
25096 /* We should already have the extra args. */
25097 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25098 == TMPL_ARGS_DEPTH (gen_args));
25099 /* And what's in the hash table should match D. */
25100 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25101 || spec == NULL_TREE);
25102
25103 /* This needs to happen before any tsubsting. */
25104 if (! push_tinst_level (d))
25105 return d;
25106
25107 timevar_push (TV_TEMPLATE_INST);
25108
25109 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25110 for the instantiation. */
25111 td = template_for_substitution (d);
25112 args = gen_args;
25113
25114 if (VAR_P (d))
25115 {
25116 /* Look up an explicit specialization, if any. */
25117 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25118 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25119 if (elt && elt != error_mark_node)
25120 {
25121 td = TREE_VALUE (elt);
25122 args = TREE_PURPOSE (elt);
25123 }
25124 }
25125
25126 code_pattern = DECL_TEMPLATE_RESULT (td);
25127
25128 /* We should never be trying to instantiate a member of a class
25129 template or partial specialization. */
25130 gcc_assert (d != code_pattern);
25131
25132 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25133 || DECL_TEMPLATE_SPECIALIZATION (td))
25134 /* In the case of a friend template whose definition is provided
25135 outside the class, we may have too many arguments. Drop the
25136 ones we don't need. The same is true for specializations. */
25137 args = get_innermost_template_args
25138 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25139
25140 if (TREE_CODE (d) == FUNCTION_DECL)
25141 {
25142 deleted_p = DECL_DELETED_FN (code_pattern);
25143 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25144 && DECL_INITIAL (code_pattern) != error_mark_node)
25145 || DECL_DEFAULTED_FN (code_pattern)
25146 || deleted_p);
25147 }
25148 else
25149 {
25150 deleted_p = false;
25151 if (DECL_CLASS_SCOPE_P (code_pattern))
25152 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25153 else
25154 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25155 }
25156
25157 /* We may be in the middle of deferred access check. Disable it now. */
25158 push_deferring_access_checks (dk_no_deferred);
25159
25160 /* Unless an explicit instantiation directive has already determined
25161 the linkage of D, remember that a definition is available for
25162 this entity. */
25163 if (pattern_defined
25164 && !DECL_INTERFACE_KNOWN (d)
25165 && !DECL_NOT_REALLY_EXTERN (d))
25166 mark_definable (d);
25167
25168 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25169 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25170 input_location = DECL_SOURCE_LOCATION (d);
25171
25172 /* If D is a member of an explicitly instantiated class template,
25173 and no definition is available, treat it like an implicit
25174 instantiation. */
25175 if (!pattern_defined && expl_inst_class_mem_p
25176 && DECL_EXPLICIT_INSTANTIATION (d))
25177 {
25178 /* Leave linkage flags alone on instantiations with anonymous
25179 visibility. */
25180 if (TREE_PUBLIC (d))
25181 {
25182 DECL_NOT_REALLY_EXTERN (d) = 0;
25183 DECL_INTERFACE_KNOWN (d) = 0;
25184 }
25185 SET_DECL_IMPLICIT_INSTANTIATION (d);
25186 }
25187
25188 /* Defer all other templates, unless we have been explicitly
25189 forbidden from doing so. */
25190 if (/* If there is no definition, we cannot instantiate the
25191 template. */
25192 ! pattern_defined
25193 /* If it's OK to postpone instantiation, do so. */
25194 || defer_ok
25195 /* If this is a static data member that will be defined
25196 elsewhere, we don't want to instantiate the entire data
25197 member, but we do want to instantiate the initializer so that
25198 we can substitute that elsewhere. */
25199 || (external_p && VAR_P (d))
25200 /* Handle here a deleted function too, avoid generating
25201 its body (c++/61080). */
25202 || deleted_p)
25203 {
25204 /* The definition of the static data member is now required so
25205 we must substitute the initializer. */
25206 if (VAR_P (d)
25207 && !DECL_INITIAL (d)
25208 && DECL_INITIAL (code_pattern))
25209 {
25210 tree ns;
25211 tree init;
25212 bool const_init = false;
25213 bool enter_context = DECL_CLASS_SCOPE_P (d);
25214
25215 ns = decl_namespace_context (d);
25216 push_nested_namespace (ns);
25217 if (enter_context)
25218 push_nested_class (DECL_CONTEXT (d));
25219 init = tsubst_expr (DECL_INITIAL (code_pattern),
25220 args,
25221 tf_warning_or_error, NULL_TREE,
25222 /*integral_constant_expression_p=*/false);
25223 /* If instantiating the initializer involved instantiating this
25224 again, don't call cp_finish_decl twice. */
25225 if (!DECL_INITIAL (d))
25226 {
25227 /* Make sure the initializer is still constant, in case of
25228 circular dependency (template/instantiate6.C). */
25229 const_init
25230 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25231 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25232 /*asmspec_tree=*/NULL_TREE,
25233 LOOKUP_ONLYCONVERTING);
25234 }
25235 if (enter_context)
25236 pop_nested_class ();
25237 pop_nested_namespace (ns);
25238 }
25239
25240 /* We restore the source position here because it's used by
25241 add_pending_template. */
25242 input_location = saved_loc;
25243
25244 if (at_eof && !pattern_defined
25245 && DECL_EXPLICIT_INSTANTIATION (d)
25246 && DECL_NOT_REALLY_EXTERN (d))
25247 /* [temp.explicit]
25248
25249 The definition of a non-exported function template, a
25250 non-exported member function template, or a non-exported
25251 member function or static data member of a class template
25252 shall be present in every translation unit in which it is
25253 explicitly instantiated. */
25254 permerror (input_location, "explicit instantiation of %qD "
25255 "but no definition available", d);
25256
25257 /* If we're in unevaluated context, we just wanted to get the
25258 constant value; this isn't an odr use, so don't queue
25259 a full instantiation. */
25260 if (cp_unevaluated_operand != 0)
25261 goto out;
25262 /* ??? Historically, we have instantiated inline functions, even
25263 when marked as "extern template". */
25264 if (!(external_p && VAR_P (d)))
25265 add_pending_template (d);
25266 goto out;
25267 }
25268
25269 bool push_to_top, nested;
25270 tree fn_context;
25271 fn_context = decl_function_context (d);
25272 if (LAMBDA_FUNCTION_P (d))
25273 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25274 fn_context = NULL_TREE;
25275 nested = current_function_decl != NULL_TREE;
25276 push_to_top = !(nested && fn_context == current_function_decl);
25277
25278 vec<tree> omp_privatization_save;
25279 if (nested)
25280 save_omp_privatization_clauses (omp_privatization_save);
25281
25282 if (push_to_top)
25283 push_to_top_level ();
25284 else
25285 {
25286 gcc_assert (!processing_template_decl);
25287 push_function_context ();
25288 cp_unevaluated_operand = 0;
25289 c_inhibit_evaluation_warnings = 0;
25290 }
25291
25292 /* Mark D as instantiated so that recursive calls to
25293 instantiate_decl do not try to instantiate it again. */
25294 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25295
25296 /* Regenerate the declaration in case the template has been modified
25297 by a subsequent redeclaration. */
25298 regenerate_decl_from_template (d, td, args);
25299
25300 /* We already set the file and line above. Reset them now in case
25301 they changed as a result of calling regenerate_decl_from_template. */
25302 input_location = DECL_SOURCE_LOCATION (d);
25303
25304 if (VAR_P (d))
25305 {
25306 tree init;
25307 bool const_init = false;
25308
25309 /* Clear out DECL_RTL; whatever was there before may not be right
25310 since we've reset the type of the declaration. */
25311 SET_DECL_RTL (d, NULL);
25312 DECL_IN_AGGR_P (d) = 0;
25313
25314 /* The initializer is placed in DECL_INITIAL by
25315 regenerate_decl_from_template so we don't need to
25316 push/pop_access_scope again here. Pull it out so that
25317 cp_finish_decl can process it. */
25318 init = DECL_INITIAL (d);
25319 DECL_INITIAL (d) = NULL_TREE;
25320 DECL_INITIALIZED_P (d) = 0;
25321
25322 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25323 initializer. That function will defer actual emission until
25324 we have a chance to determine linkage. */
25325 DECL_EXTERNAL (d) = 0;
25326
25327 /* Enter the scope of D so that access-checking works correctly. */
25328 bool enter_context = DECL_CLASS_SCOPE_P (d);
25329 if (enter_context)
25330 push_nested_class (DECL_CONTEXT (d));
25331
25332 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25333 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25334 ? LOOKUP_CONSTINIT : 0);
25335 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25336
25337 if (enter_context)
25338 pop_nested_class ();
25339
25340 if (variable_template_p (gen_tmpl))
25341 note_variable_template_instantiation (d);
25342 }
25343 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25344 synthesize_method (d);
25345 else if (TREE_CODE (d) == FUNCTION_DECL)
25346 {
25347 /* Set up the list of local specializations. */
25348 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25349 tree block = NULL_TREE;
25350
25351 /* Set up context. */
25352 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25353 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25354 block = push_stmt_list ();
25355 else
25356 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25357
25358 /* Some typedefs referenced from within the template code need to be
25359 access checked at template instantiation time, i.e now. These
25360 types were added to the template at parsing time. Let's get those
25361 and perform the access checks then. */
25362 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25363 args);
25364
25365 /* Create substitution entries for the parameters. */
25366 register_parameter_specializations (code_pattern, d);
25367
25368 /* Substitute into the body of the function. */
25369 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25370 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25371 tf_warning_or_error, tmpl);
25372 else
25373 {
25374 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25375 tf_warning_or_error, tmpl,
25376 /*integral_constant_expression_p=*/false);
25377
25378 /* Set the current input_location to the end of the function
25379 so that finish_function knows where we are. */
25380 input_location
25381 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25382
25383 /* Remember if we saw an infinite loop in the template. */
25384 current_function_infinite_loop
25385 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25386 }
25387
25388 /* Finish the function. */
25389 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25390 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25391 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25392 else
25393 {
25394 d = finish_function (/*inline_p=*/false);
25395 expand_or_defer_fn (d);
25396 }
25397
25398 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25399 cp_check_omp_declare_reduction (d);
25400 }
25401
25402 /* We're not deferring instantiation any more. */
25403 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25404
25405 if (push_to_top)
25406 pop_from_top_level ();
25407 else
25408 pop_function_context ();
25409
25410 if (nested)
25411 restore_omp_privatization_clauses (omp_privatization_save);
25412
25413 out:
25414 pop_deferring_access_checks ();
25415 timevar_pop (TV_TEMPLATE_INST);
25416 pop_tinst_level ();
25417 input_location = saved_loc;
25418 cp_unevaluated_operand = saved_unevaluated_operand;
25419 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25420
25421 return d;
25422 }
25423
25424 /* Run through the list of templates that we wish we could
25425 instantiate, and instantiate any we can. RETRIES is the
25426 number of times we retry pending template instantiation. */
25427
25428 void
25429 instantiate_pending_templates (int retries)
25430 {
25431 int reconsider;
25432 location_t saved_loc = input_location;
25433
25434 /* Instantiating templates may trigger vtable generation. This in turn
25435 may require further template instantiations. We place a limit here
25436 to avoid infinite loop. */
25437 if (pending_templates && retries >= max_tinst_depth)
25438 {
25439 tree decl = pending_templates->tinst->maybe_get_node ();
25440
25441 fatal_error (input_location,
25442 "template instantiation depth exceeds maximum of %d"
25443 " instantiating %q+D, possibly from virtual table generation"
25444 " (use %<-ftemplate-depth=%> to increase the maximum)",
25445 max_tinst_depth, decl);
25446 if (TREE_CODE (decl) == FUNCTION_DECL)
25447 /* Pretend that we defined it. */
25448 DECL_INITIAL (decl) = error_mark_node;
25449 return;
25450 }
25451
25452 do
25453 {
25454 struct pending_template **t = &pending_templates;
25455 struct pending_template *last = NULL;
25456 reconsider = 0;
25457 while (*t)
25458 {
25459 tree instantiation = reopen_tinst_level ((*t)->tinst);
25460 bool complete = false;
25461
25462 if (TYPE_P (instantiation))
25463 {
25464 if (!COMPLETE_TYPE_P (instantiation))
25465 {
25466 instantiate_class_template (instantiation);
25467 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25468 for (tree fld = TYPE_FIELDS (instantiation);
25469 fld; fld = TREE_CHAIN (fld))
25470 if ((VAR_P (fld)
25471 || (TREE_CODE (fld) == FUNCTION_DECL
25472 && !DECL_ARTIFICIAL (fld)))
25473 && DECL_TEMPLATE_INSTANTIATION (fld))
25474 instantiate_decl (fld,
25475 /*defer_ok=*/false,
25476 /*expl_inst_class_mem_p=*/false);
25477
25478 if (COMPLETE_TYPE_P (instantiation))
25479 reconsider = 1;
25480 }
25481
25482 complete = COMPLETE_TYPE_P (instantiation);
25483 }
25484 else
25485 {
25486 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25487 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25488 {
25489 instantiation
25490 = instantiate_decl (instantiation,
25491 /*defer_ok=*/false,
25492 /*expl_inst_class_mem_p=*/false);
25493 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25494 reconsider = 1;
25495 }
25496
25497 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25498 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25499 }
25500
25501 if (complete)
25502 {
25503 /* If INSTANTIATION has been instantiated, then we don't
25504 need to consider it again in the future. */
25505 struct pending_template *drop = *t;
25506 *t = (*t)->next;
25507 set_refcount_ptr (drop->tinst);
25508 pending_template_freelist ().free (drop);
25509 }
25510 else
25511 {
25512 last = *t;
25513 t = &(*t)->next;
25514 }
25515 tinst_depth = 0;
25516 set_refcount_ptr (current_tinst_level);
25517 }
25518 last_pending_template = last;
25519 }
25520 while (reconsider);
25521
25522 input_location = saved_loc;
25523 }
25524
25525 /* Substitute ARGVEC into T, which is a list of initializers for
25526 either base class or a non-static data member. The TREE_PURPOSEs
25527 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25528 instantiate_decl. */
25529
25530 static tree
25531 tsubst_initializer_list (tree t, tree argvec)
25532 {
25533 tree inits = NULL_TREE;
25534 tree target_ctor = error_mark_node;
25535
25536 for (; t; t = TREE_CHAIN (t))
25537 {
25538 tree decl;
25539 tree init;
25540 tree expanded_bases = NULL_TREE;
25541 tree expanded_arguments = NULL_TREE;
25542 int i, len = 1;
25543
25544 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25545 {
25546 tree expr;
25547 tree arg;
25548
25549 /* Expand the base class expansion type into separate base
25550 classes. */
25551 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25552 tf_warning_or_error,
25553 NULL_TREE);
25554 if (expanded_bases == error_mark_node)
25555 continue;
25556
25557 /* We'll be building separate TREE_LISTs of arguments for
25558 each base. */
25559 len = TREE_VEC_LENGTH (expanded_bases);
25560 expanded_arguments = make_tree_vec (len);
25561 for (i = 0; i < len; i++)
25562 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25563
25564 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25565 expand each argument in the TREE_VALUE of t. */
25566 expr = make_node (EXPR_PACK_EXPANSION);
25567 PACK_EXPANSION_LOCAL_P (expr) = true;
25568 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25569 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25570
25571 if (TREE_VALUE (t) == void_type_node)
25572 /* VOID_TYPE_NODE is used to indicate
25573 value-initialization. */
25574 {
25575 for (i = 0; i < len; i++)
25576 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25577 }
25578 else
25579 {
25580 /* Substitute parameter packs into each argument in the
25581 TREE_LIST. */
25582 in_base_initializer = 1;
25583 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25584 {
25585 tree expanded_exprs;
25586
25587 /* Expand the argument. */
25588 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25589 expanded_exprs
25590 = tsubst_pack_expansion (expr, argvec,
25591 tf_warning_or_error,
25592 NULL_TREE);
25593 if (expanded_exprs == error_mark_node)
25594 continue;
25595
25596 /* Prepend each of the expanded expressions to the
25597 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25598 for (i = 0; i < len; i++)
25599 {
25600 TREE_VEC_ELT (expanded_arguments, i) =
25601 tree_cons (NULL_TREE,
25602 TREE_VEC_ELT (expanded_exprs, i),
25603 TREE_VEC_ELT (expanded_arguments, i));
25604 }
25605 }
25606 in_base_initializer = 0;
25607
25608 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25609 since we built them backwards. */
25610 for (i = 0; i < len; i++)
25611 {
25612 TREE_VEC_ELT (expanded_arguments, i) =
25613 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25614 }
25615 }
25616 }
25617
25618 for (i = 0; i < len; ++i)
25619 {
25620 if (expanded_bases)
25621 {
25622 decl = TREE_VEC_ELT (expanded_bases, i);
25623 decl = expand_member_init (decl);
25624 init = TREE_VEC_ELT (expanded_arguments, i);
25625 }
25626 else
25627 {
25628 tree tmp;
25629 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25630 tf_warning_or_error, NULL_TREE);
25631
25632 decl = expand_member_init (decl);
25633 if (decl && !DECL_P (decl))
25634 in_base_initializer = 1;
25635
25636 init = TREE_VALUE (t);
25637 tmp = init;
25638 if (init != void_type_node)
25639 init = tsubst_expr (init, argvec,
25640 tf_warning_or_error, NULL_TREE,
25641 /*integral_constant_expression_p=*/false);
25642 if (init == NULL_TREE && tmp != NULL_TREE)
25643 /* If we had an initializer but it instantiated to nothing,
25644 value-initialize the object. This will only occur when
25645 the initializer was a pack expansion where the parameter
25646 packs used in that expansion were of length zero. */
25647 init = void_type_node;
25648 in_base_initializer = 0;
25649 }
25650
25651 if (target_ctor != error_mark_node
25652 && init != error_mark_node)
25653 {
25654 error ("mem-initializer for %qD follows constructor delegation",
25655 decl);
25656 return inits;
25657 }
25658 /* Look for a target constructor. */
25659 if (init != error_mark_node
25660 && decl && CLASS_TYPE_P (decl)
25661 && same_type_p (decl, current_class_type))
25662 {
25663 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25664 if (inits)
25665 {
25666 error ("constructor delegation follows mem-initializer for %qD",
25667 TREE_PURPOSE (inits));
25668 continue;
25669 }
25670 target_ctor = init;
25671 }
25672
25673 if (decl)
25674 {
25675 init = build_tree_list (decl, init);
25676 TREE_CHAIN (init) = inits;
25677 inits = init;
25678 }
25679 }
25680 }
25681 return inits;
25682 }
25683
25684 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25685
25686 static void
25687 set_current_access_from_decl (tree decl)
25688 {
25689 if (TREE_PRIVATE (decl))
25690 current_access_specifier = access_private_node;
25691 else if (TREE_PROTECTED (decl))
25692 current_access_specifier = access_protected_node;
25693 else
25694 current_access_specifier = access_public_node;
25695 }
25696
25697 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25698 is the instantiation (which should have been created with
25699 start_enum) and ARGS are the template arguments to use. */
25700
25701 static void
25702 tsubst_enum (tree tag, tree newtag, tree args)
25703 {
25704 tree e;
25705
25706 if (SCOPED_ENUM_P (newtag))
25707 begin_scope (sk_scoped_enum, newtag);
25708
25709 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25710 {
25711 tree value;
25712 tree decl;
25713
25714 decl = TREE_VALUE (e);
25715 /* Note that in a template enum, the TREE_VALUE is the
25716 CONST_DECL, not the corresponding INTEGER_CST. */
25717 value = tsubst_expr (DECL_INITIAL (decl),
25718 args, tf_warning_or_error, NULL_TREE,
25719 /*integral_constant_expression_p=*/true);
25720
25721 /* Give this enumeration constant the correct access. */
25722 set_current_access_from_decl (decl);
25723
25724 /* Actually build the enumerator itself. Here we're assuming that
25725 enumerators can't have dependent attributes. */
25726 build_enumerator (DECL_NAME (decl), value, newtag,
25727 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25728 }
25729
25730 if (SCOPED_ENUM_P (newtag))
25731 finish_scope ();
25732
25733 finish_enum_value_list (newtag);
25734 finish_enum (newtag);
25735
25736 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25737 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25738 }
25739
25740 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25741 its type -- but without substituting the innermost set of template
25742 arguments. So, innermost set of template parameters will appear in
25743 the type. */
25744
25745 tree
25746 get_mostly_instantiated_function_type (tree decl)
25747 {
25748 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25749 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25750 }
25751
25752 /* Return truthvalue if we're processing a template different from
25753 the last one involved in diagnostics. */
25754 bool
25755 problematic_instantiation_changed (void)
25756 {
25757 return current_tinst_level != last_error_tinst_level;
25758 }
25759
25760 /* Remember current template involved in diagnostics. */
25761 void
25762 record_last_problematic_instantiation (void)
25763 {
25764 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25765 }
25766
25767 struct tinst_level *
25768 current_instantiation (void)
25769 {
25770 return current_tinst_level;
25771 }
25772
25773 /* Return TRUE if current_function_decl is being instantiated, false
25774 otherwise. */
25775
25776 bool
25777 instantiating_current_function_p (void)
25778 {
25779 return (current_instantiation ()
25780 && (current_instantiation ()->maybe_get_node ()
25781 == current_function_decl));
25782 }
25783
25784 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25785 type. Return false for ok, true for disallowed. Issue error and
25786 inform messages under control of COMPLAIN. */
25787
25788 static bool
25789 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25790 {
25791 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25792 return false;
25793 else if (TYPE_PTR_P (type))
25794 return false;
25795 else if (TYPE_REF_P (type)
25796 && !TYPE_REF_IS_RVALUE (type))
25797 return false;
25798 else if (TYPE_PTRMEM_P (type))
25799 return false;
25800 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25801 return false;
25802 else if (TREE_CODE (type) == TYPENAME_TYPE)
25803 return false;
25804 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25805 return false;
25806 else if (TREE_CODE (type) == NULLPTR_TYPE)
25807 return false;
25808 /* A bound template template parm could later be instantiated to have a valid
25809 nontype parm type via an alias template. */
25810 else if (cxx_dialect >= cxx11
25811 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25812 return false;
25813 else if (CLASS_TYPE_P (type))
25814 {
25815 if (cxx_dialect < cxx2a)
25816 {
25817 if (complain & tf_error)
25818 error ("non-type template parameters of class type only available "
25819 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25820 return true;
25821 }
25822 if (dependent_type_p (type))
25823 return false;
25824 if (!complete_type_or_else (type, NULL_TREE))
25825 return true;
25826 if (!structural_type_p (type))
25827 {
25828 auto_diagnostic_group d;
25829 if (complain & tf_error)
25830 error ("%qT is not a valid type for a template non-type parameter "
25831 "because it is not structural", type);
25832 structural_type_p (type, true);
25833 return true;
25834 }
25835 return false;
25836 }
25837
25838 if (complain & tf_error)
25839 {
25840 if (type == error_mark_node)
25841 inform (input_location, "invalid template non-type parameter");
25842 else
25843 error ("%q#T is not a valid type for a template non-type parameter",
25844 type);
25845 }
25846 return true;
25847 }
25848
25849 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25850 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25851
25852 static bool
25853 dependent_type_p_r (tree type)
25854 {
25855 tree scope;
25856
25857 /* [temp.dep.type]
25858
25859 A type is dependent if it is:
25860
25861 -- a template parameter. Template template parameters are types
25862 for us (since TYPE_P holds true for them) so we handle
25863 them here. */
25864 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25865 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25866 return true;
25867 /* -- a qualified-id with a nested-name-specifier which contains a
25868 class-name that names a dependent type or whose unqualified-id
25869 names a dependent type. */
25870 if (TREE_CODE (type) == TYPENAME_TYPE)
25871 return true;
25872
25873 /* An alias template specialization can be dependent even if the
25874 resulting type is not. */
25875 if (dependent_alias_template_spec_p (type, nt_transparent))
25876 return true;
25877
25878 /* -- a cv-qualified type where the cv-unqualified type is
25879 dependent.
25880 No code is necessary for this bullet; the code below handles
25881 cv-qualified types, and we don't want to strip aliases with
25882 TYPE_MAIN_VARIANT because of DR 1558. */
25883 /* -- a compound type constructed from any dependent type. */
25884 if (TYPE_PTRMEM_P (type))
25885 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25886 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25887 (type)));
25888 else if (INDIRECT_TYPE_P (type))
25889 return dependent_type_p (TREE_TYPE (type));
25890 else if (FUNC_OR_METHOD_TYPE_P (type))
25891 {
25892 tree arg_type;
25893
25894 if (dependent_type_p (TREE_TYPE (type)))
25895 return true;
25896 for (arg_type = TYPE_ARG_TYPES (type);
25897 arg_type;
25898 arg_type = TREE_CHAIN (arg_type))
25899 if (dependent_type_p (TREE_VALUE (arg_type)))
25900 return true;
25901 if (cxx_dialect >= cxx17)
25902 /* A value-dependent noexcept-specifier makes the type dependent. */
25903 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25904 if (tree noex = TREE_PURPOSE (spec))
25905 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25906 affect overload resolution and treating it as dependent breaks
25907 things. Same for an unparsed noexcept expression. */
25908 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25909 && TREE_CODE (noex) != DEFERRED_PARSE
25910 && value_dependent_expression_p (noex))
25911 return true;
25912 return false;
25913 }
25914 /* -- an array type constructed from any dependent type or whose
25915 size is specified by a constant expression that is
25916 value-dependent.
25917
25918 We checked for type- and value-dependence of the bounds in
25919 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25920 if (TREE_CODE (type) == ARRAY_TYPE)
25921 {
25922 if (TYPE_DOMAIN (type)
25923 && dependent_type_p (TYPE_DOMAIN (type)))
25924 return true;
25925 return dependent_type_p (TREE_TYPE (type));
25926 }
25927
25928 /* -- a template-id in which either the template name is a template
25929 parameter ... */
25930 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25931 return true;
25932 /* ... or any of the template arguments is a dependent type or
25933 an expression that is type-dependent or value-dependent. */
25934 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25935 && (any_dependent_template_arguments_p
25936 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25937 return true;
25938
25939 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25940 dependent; if the argument of the `typeof' expression is not
25941 type-dependent, then it should already been have resolved. */
25942 if (TREE_CODE (type) == TYPEOF_TYPE
25943 || TREE_CODE (type) == DECLTYPE_TYPE
25944 || TREE_CODE (type) == UNDERLYING_TYPE)
25945 return true;
25946
25947 /* A template argument pack is dependent if any of its packed
25948 arguments are. */
25949 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25950 {
25951 tree args = ARGUMENT_PACK_ARGS (type);
25952 int i, len = TREE_VEC_LENGTH (args);
25953 for (i = 0; i < len; ++i)
25954 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25955 return true;
25956 }
25957
25958 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25959 be template parameters. */
25960 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25961 return true;
25962
25963 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25964 return true;
25965
25966 /* The standard does not specifically mention types that are local
25967 to template functions or local classes, but they should be
25968 considered dependent too. For example:
25969
25970 template <int I> void f() {
25971 enum E { a = I };
25972 S<sizeof (E)> s;
25973 }
25974
25975 The size of `E' cannot be known until the value of `I' has been
25976 determined. Therefore, `E' must be considered dependent. */
25977 scope = TYPE_CONTEXT (type);
25978 if (scope && TYPE_P (scope))
25979 return dependent_type_p (scope);
25980 /* Don't use type_dependent_expression_p here, as it can lead
25981 to infinite recursion trying to determine whether a lambda
25982 nested in a lambda is dependent (c++/47687). */
25983 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25984 && DECL_LANG_SPECIFIC (scope)
25985 && DECL_TEMPLATE_INFO (scope)
25986 && (any_dependent_template_arguments_p
25987 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25988 return true;
25989
25990 /* Other types are non-dependent. */
25991 return false;
25992 }
25993
25994 /* Returns TRUE if TYPE is dependent, in the sense of
25995 [temp.dep.type]. Note that a NULL type is considered dependent. */
25996
25997 bool
25998 dependent_type_p (tree type)
25999 {
26000 /* If there are no template parameters in scope, then there can't be
26001 any dependent types. */
26002 if (!processing_template_decl)
26003 {
26004 /* If we are not processing a template, then nobody should be
26005 providing us with a dependent type. */
26006 gcc_assert (type);
26007 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26008 return false;
26009 }
26010
26011 /* If the type is NULL, we have not computed a type for the entity
26012 in question; in that case, the type is dependent. */
26013 if (!type)
26014 return true;
26015
26016 /* Erroneous types can be considered non-dependent. */
26017 if (type == error_mark_node)
26018 return false;
26019
26020 /* Getting here with global_type_node means we improperly called this
26021 function on the TREE_TYPE of an IDENTIFIER_NODE. */
26022 gcc_checking_assert (type != global_type_node);
26023
26024 /* If we have not already computed the appropriate value for TYPE,
26025 do so now. */
26026 if (!TYPE_DEPENDENT_P_VALID (type))
26027 {
26028 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26029 TYPE_DEPENDENT_P_VALID (type) = 1;
26030 }
26031
26032 return TYPE_DEPENDENT_P (type);
26033 }
26034
26035 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26036 lookup. In other words, a dependent type that is not the current
26037 instantiation. */
26038
26039 bool
26040 dependent_scope_p (tree scope)
26041 {
26042 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26043 && !currently_open_class (scope));
26044 }
26045
26046 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26047 an unknown base of 'this' (and is therefore instantiation-dependent). */
26048
26049 static bool
26050 unknown_base_ref_p (tree t)
26051 {
26052 if (!current_class_ptr)
26053 return false;
26054
26055 tree mem = TREE_OPERAND (t, 1);
26056 if (shared_member_p (mem))
26057 return false;
26058
26059 tree cur = current_nonlambda_class_type ();
26060 if (!any_dependent_bases_p (cur))
26061 return false;
26062
26063 tree ctx = TREE_OPERAND (t, 0);
26064 if (DERIVED_FROM_P (ctx, cur))
26065 return false;
26066
26067 return true;
26068 }
26069
26070 /* T is a SCOPE_REF; return whether we need to consider it
26071 instantiation-dependent so that we can check access at instantiation
26072 time even though we know which member it resolves to. */
26073
26074 static bool
26075 instantiation_dependent_scope_ref_p (tree t)
26076 {
26077 if (DECL_P (TREE_OPERAND (t, 1))
26078 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26079 && !unknown_base_ref_p (t)
26080 && accessible_in_template_p (TREE_OPERAND (t, 0),
26081 TREE_OPERAND (t, 1)))
26082 return false;
26083 else
26084 return true;
26085 }
26086
26087 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26088 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26089 expression. */
26090
26091 /* Note that this predicate is not appropriate for general expressions;
26092 only constant expressions (that satisfy potential_constant_expression)
26093 can be tested for value dependence. */
26094
26095 bool
26096 value_dependent_expression_p (tree expression)
26097 {
26098 if (!processing_template_decl || expression == NULL_TREE)
26099 return false;
26100
26101 /* A type-dependent expression is also value-dependent. */
26102 if (type_dependent_expression_p (expression))
26103 return true;
26104
26105 switch (TREE_CODE (expression))
26106 {
26107 case BASELINK:
26108 /* A dependent member function of the current instantiation. */
26109 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26110
26111 case FUNCTION_DECL:
26112 /* A dependent member function of the current instantiation. */
26113 if (DECL_CLASS_SCOPE_P (expression)
26114 && dependent_type_p (DECL_CONTEXT (expression)))
26115 return true;
26116 break;
26117
26118 case IDENTIFIER_NODE:
26119 /* A name that has not been looked up -- must be dependent. */
26120 return true;
26121
26122 case TEMPLATE_PARM_INDEX:
26123 /* A non-type template parm. */
26124 return true;
26125
26126 case CONST_DECL:
26127 /* A non-type template parm. */
26128 if (DECL_TEMPLATE_PARM_P (expression))
26129 return true;
26130 return value_dependent_expression_p (DECL_INITIAL (expression));
26131
26132 case VAR_DECL:
26133 /* A constant with literal type and is initialized
26134 with an expression that is value-dependent. */
26135 if (DECL_DEPENDENT_INIT_P (expression)
26136 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26137 || TYPE_REF_P (TREE_TYPE (expression)))
26138 return true;
26139 if (DECL_HAS_VALUE_EXPR_P (expression))
26140 {
26141 tree value_expr = DECL_VALUE_EXPR (expression);
26142 if (value_dependent_expression_p (value_expr)
26143 /* __PRETTY_FUNCTION__ inside a template function is dependent
26144 on the name of the function. */
26145 || (DECL_PRETTY_FUNCTION_P (expression)
26146 /* It might be used in a template, but not a template
26147 function, in which case its DECL_VALUE_EXPR will be
26148 "top level". */
26149 && value_expr == error_mark_node))
26150 return true;
26151 }
26152 return false;
26153
26154 case DYNAMIC_CAST_EXPR:
26155 case STATIC_CAST_EXPR:
26156 case CONST_CAST_EXPR:
26157 case REINTERPRET_CAST_EXPR:
26158 case CAST_EXPR:
26159 case IMPLICIT_CONV_EXPR:
26160 /* These expressions are value-dependent if the type to which
26161 the cast occurs is dependent or the expression being casted
26162 is value-dependent. */
26163 {
26164 tree type = TREE_TYPE (expression);
26165
26166 if (dependent_type_p (type))
26167 return true;
26168
26169 /* A functional cast has a list of operands. */
26170 expression = TREE_OPERAND (expression, 0);
26171 if (!expression)
26172 {
26173 /* If there are no operands, it must be an expression such
26174 as "int()". This should not happen for aggregate types
26175 because it would form non-constant expressions. */
26176 gcc_assert (cxx_dialect >= cxx11
26177 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26178
26179 return false;
26180 }
26181
26182 if (TREE_CODE (expression) == TREE_LIST)
26183 return any_value_dependent_elements_p (expression);
26184
26185 return value_dependent_expression_p (expression);
26186 }
26187
26188 case SIZEOF_EXPR:
26189 if (SIZEOF_EXPR_TYPE_P (expression))
26190 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26191 /* FALLTHRU */
26192 case ALIGNOF_EXPR:
26193 case TYPEID_EXPR:
26194 /* A `sizeof' expression is value-dependent if the operand is
26195 type-dependent or is a pack expansion. */
26196 expression = TREE_OPERAND (expression, 0);
26197 if (PACK_EXPANSION_P (expression))
26198 return true;
26199 else if (TYPE_P (expression))
26200 return dependent_type_p (expression);
26201 return instantiation_dependent_uneval_expression_p (expression);
26202
26203 case AT_ENCODE_EXPR:
26204 /* An 'encode' expression is value-dependent if the operand is
26205 type-dependent. */
26206 expression = TREE_OPERAND (expression, 0);
26207 return dependent_type_p (expression);
26208
26209 case NOEXCEPT_EXPR:
26210 expression = TREE_OPERAND (expression, 0);
26211 return instantiation_dependent_uneval_expression_p (expression);
26212
26213 case SCOPE_REF:
26214 /* All instantiation-dependent expressions should also be considered
26215 value-dependent. */
26216 return instantiation_dependent_scope_ref_p (expression);
26217
26218 case COMPONENT_REF:
26219 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26220 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26221
26222 case NONTYPE_ARGUMENT_PACK:
26223 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26224 is value-dependent. */
26225 {
26226 tree values = ARGUMENT_PACK_ARGS (expression);
26227 int i, len = TREE_VEC_LENGTH (values);
26228
26229 for (i = 0; i < len; ++i)
26230 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26231 return true;
26232
26233 return false;
26234 }
26235
26236 case TRAIT_EXPR:
26237 {
26238 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26239
26240 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26241 return true;
26242
26243 if (!type2)
26244 return false;
26245
26246 if (TREE_CODE (type2) != TREE_LIST)
26247 return dependent_type_p (type2);
26248
26249 for (; type2; type2 = TREE_CHAIN (type2))
26250 if (dependent_type_p (TREE_VALUE (type2)))
26251 return true;
26252
26253 return false;
26254 }
26255
26256 case MODOP_EXPR:
26257 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26258 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26259
26260 case ARRAY_REF:
26261 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26262 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26263
26264 case ADDR_EXPR:
26265 {
26266 tree op = TREE_OPERAND (expression, 0);
26267 return (value_dependent_expression_p (op)
26268 || has_value_dependent_address (op));
26269 }
26270
26271 case REQUIRES_EXPR:
26272 /* Treat all requires-expressions as value-dependent so
26273 we don't try to fold them. */
26274 return true;
26275
26276 case TYPE_REQ:
26277 return dependent_type_p (TREE_OPERAND (expression, 0));
26278
26279 case CALL_EXPR:
26280 {
26281 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26282 return true;
26283 tree fn = get_callee_fndecl (expression);
26284 int i, nargs;
26285 nargs = call_expr_nargs (expression);
26286 for (i = 0; i < nargs; ++i)
26287 {
26288 tree op = CALL_EXPR_ARG (expression, i);
26289 /* In a call to a constexpr member function, look through the
26290 implicit ADDR_EXPR on the object argument so that it doesn't
26291 cause the call to be considered value-dependent. We also
26292 look through it in potential_constant_expression. */
26293 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26294 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26295 && TREE_CODE (op) == ADDR_EXPR)
26296 op = TREE_OPERAND (op, 0);
26297 if (value_dependent_expression_p (op))
26298 return true;
26299 }
26300 return false;
26301 }
26302
26303 case TEMPLATE_ID_EXPR:
26304 return concept_definition_p (TREE_OPERAND (expression, 0));
26305
26306 case CONSTRUCTOR:
26307 {
26308 unsigned ix;
26309 tree val;
26310 if (dependent_type_p (TREE_TYPE (expression)))
26311 return true;
26312 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26313 if (value_dependent_expression_p (val))
26314 return true;
26315 return false;
26316 }
26317
26318 case STMT_EXPR:
26319 /* Treat a GNU statement expression as dependent to avoid crashing
26320 under instantiate_non_dependent_expr; it can't be constant. */
26321 return true;
26322
26323 default:
26324 /* A constant expression is value-dependent if any subexpression is
26325 value-dependent. */
26326 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26327 {
26328 case tcc_reference:
26329 case tcc_unary:
26330 case tcc_comparison:
26331 case tcc_binary:
26332 case tcc_expression:
26333 case tcc_vl_exp:
26334 {
26335 int i, len = cp_tree_operand_length (expression);
26336
26337 for (i = 0; i < len; i++)
26338 {
26339 tree t = TREE_OPERAND (expression, i);
26340
26341 /* In some cases, some of the operands may be missing.
26342 (For example, in the case of PREDECREMENT_EXPR, the
26343 amount to increment by may be missing.) That doesn't
26344 make the expression dependent. */
26345 if (t && value_dependent_expression_p (t))
26346 return true;
26347 }
26348 }
26349 break;
26350 default:
26351 break;
26352 }
26353 break;
26354 }
26355
26356 /* The expression is not value-dependent. */
26357 return false;
26358 }
26359
26360 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26361 [temp.dep.expr]. Note that an expression with no type is
26362 considered dependent. Other parts of the compiler arrange for an
26363 expression with type-dependent subexpressions to have no type, so
26364 this function doesn't have to be fully recursive. */
26365
26366 bool
26367 type_dependent_expression_p (tree expression)
26368 {
26369 if (!processing_template_decl)
26370 return false;
26371
26372 if (expression == NULL_TREE || expression == error_mark_node)
26373 return false;
26374
26375 STRIP_ANY_LOCATION_WRAPPER (expression);
26376
26377 /* An unresolved name is always dependent. */
26378 if (identifier_p (expression)
26379 || TREE_CODE (expression) == USING_DECL
26380 || TREE_CODE (expression) == WILDCARD_DECL)
26381 return true;
26382
26383 /* A lambda-expression in template context is dependent. dependent_type_p is
26384 true for a lambda in the scope of a class or function template, but that
26385 doesn't cover all template contexts, like a default template argument. */
26386 if (TREE_CODE (expression) == LAMBDA_EXPR)
26387 return true;
26388
26389 /* A fold expression is type-dependent. */
26390 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26391 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26392 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26393 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26394 return true;
26395
26396 /* Some expression forms are never type-dependent. */
26397 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26398 || TREE_CODE (expression) == SIZEOF_EXPR
26399 || TREE_CODE (expression) == ALIGNOF_EXPR
26400 || TREE_CODE (expression) == AT_ENCODE_EXPR
26401 || TREE_CODE (expression) == NOEXCEPT_EXPR
26402 || TREE_CODE (expression) == TRAIT_EXPR
26403 || TREE_CODE (expression) == TYPEID_EXPR
26404 || TREE_CODE (expression) == DELETE_EXPR
26405 || TREE_CODE (expression) == VEC_DELETE_EXPR
26406 || TREE_CODE (expression) == THROW_EXPR
26407 || TREE_CODE (expression) == REQUIRES_EXPR)
26408 return false;
26409
26410 /* The types of these expressions depends only on the type to which
26411 the cast occurs. */
26412 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26413 || TREE_CODE (expression) == STATIC_CAST_EXPR
26414 || TREE_CODE (expression) == CONST_CAST_EXPR
26415 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26416 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26417 || TREE_CODE (expression) == CAST_EXPR)
26418 return dependent_type_p (TREE_TYPE (expression));
26419
26420 /* The types of these expressions depends only on the type created
26421 by the expression. */
26422 if (TREE_CODE (expression) == NEW_EXPR
26423 || TREE_CODE (expression) == VEC_NEW_EXPR)
26424 {
26425 /* For NEW_EXPR tree nodes created inside a template, either
26426 the object type itself or a TREE_LIST may appear as the
26427 operand 1. */
26428 tree type = TREE_OPERAND (expression, 1);
26429 if (TREE_CODE (type) == TREE_LIST)
26430 /* This is an array type. We need to check array dimensions
26431 as well. */
26432 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26433 || value_dependent_expression_p
26434 (TREE_OPERAND (TREE_VALUE (type), 1));
26435 else
26436 return dependent_type_p (type);
26437 }
26438
26439 if (TREE_CODE (expression) == SCOPE_REF)
26440 {
26441 tree scope = TREE_OPERAND (expression, 0);
26442 tree name = TREE_OPERAND (expression, 1);
26443
26444 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26445 contains an identifier associated by name lookup with one or more
26446 declarations declared with a dependent type, or...a
26447 nested-name-specifier or qualified-id that names a member of an
26448 unknown specialization. */
26449 return (type_dependent_expression_p (name)
26450 || dependent_scope_p (scope));
26451 }
26452
26453 if (TREE_CODE (expression) == TEMPLATE_DECL
26454 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26455 return uses_outer_template_parms (expression);
26456
26457 if (TREE_CODE (expression) == STMT_EXPR)
26458 expression = stmt_expr_value_expr (expression);
26459
26460 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26461 {
26462 tree elt;
26463 unsigned i;
26464
26465 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26466 {
26467 if (type_dependent_expression_p (elt))
26468 return true;
26469 }
26470 return false;
26471 }
26472
26473 /* A static data member of the current instantiation with incomplete
26474 array type is type-dependent, as the definition and specializations
26475 can have different bounds. */
26476 if (VAR_P (expression)
26477 && DECL_CLASS_SCOPE_P (expression)
26478 && dependent_type_p (DECL_CONTEXT (expression))
26479 && VAR_HAD_UNKNOWN_BOUND (expression))
26480 return true;
26481
26482 /* An array of unknown bound depending on a variadic parameter, eg:
26483
26484 template<typename... Args>
26485 void foo (Args... args)
26486 {
26487 int arr[] = { args... };
26488 }
26489
26490 template<int... vals>
26491 void bar ()
26492 {
26493 int arr[] = { vals... };
26494 }
26495
26496 If the array has no length and has an initializer, it must be that
26497 we couldn't determine its length in cp_complete_array_type because
26498 it is dependent. */
26499 if (VAR_P (expression)
26500 && TREE_TYPE (expression) != NULL_TREE
26501 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26502 && !TYPE_DOMAIN (TREE_TYPE (expression))
26503 && DECL_INITIAL (expression))
26504 return true;
26505
26506 /* A function or variable template-id is type-dependent if it has any
26507 dependent template arguments. */
26508 if (VAR_OR_FUNCTION_DECL_P (expression)
26509 && DECL_LANG_SPECIFIC (expression)
26510 && DECL_TEMPLATE_INFO (expression))
26511 {
26512 /* Consider the innermost template arguments, since those are the ones
26513 that come from the template-id; the template arguments for the
26514 enclosing class do not make it type-dependent unless they are used in
26515 the type of the decl. */
26516 if (instantiates_primary_template_p (expression)
26517 && (any_dependent_template_arguments_p
26518 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26519 return true;
26520 }
26521
26522 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26523 type-dependent. Checking this is important for functions with auto return
26524 type, which looks like a dependent type. */
26525 if (TREE_CODE (expression) == FUNCTION_DECL
26526 && !(DECL_CLASS_SCOPE_P (expression)
26527 && dependent_type_p (DECL_CONTEXT (expression)))
26528 && !(DECL_LANG_SPECIFIC (expression)
26529 && DECL_FRIEND_P (expression)
26530 && (!DECL_FRIEND_CONTEXT (expression)
26531 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26532 && !DECL_LOCAL_FUNCTION_P (expression))
26533 {
26534 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26535 || undeduced_auto_decl (expression));
26536 return false;
26537 }
26538
26539 /* Always dependent, on the number of arguments if nothing else. */
26540 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26541 return true;
26542
26543 if (TREE_TYPE (expression) == unknown_type_node)
26544 {
26545 if (TREE_CODE (expression) == ADDR_EXPR)
26546 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26547 if (TREE_CODE (expression) == COMPONENT_REF
26548 || TREE_CODE (expression) == OFFSET_REF)
26549 {
26550 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26551 return true;
26552 expression = TREE_OPERAND (expression, 1);
26553 if (identifier_p (expression))
26554 return false;
26555 }
26556 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26557 if (TREE_CODE (expression) == SCOPE_REF)
26558 return false;
26559
26560 if (BASELINK_P (expression))
26561 {
26562 if (BASELINK_OPTYPE (expression)
26563 && dependent_type_p (BASELINK_OPTYPE (expression)))
26564 return true;
26565 expression = BASELINK_FUNCTIONS (expression);
26566 }
26567
26568 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26569 {
26570 if (any_dependent_template_arguments_p
26571 (TREE_OPERAND (expression, 1)))
26572 return true;
26573 expression = TREE_OPERAND (expression, 0);
26574 if (identifier_p (expression))
26575 return true;
26576 }
26577
26578 gcc_assert (OVL_P (expression));
26579
26580 for (lkp_iterator iter (expression); iter; ++iter)
26581 if (type_dependent_expression_p (*iter))
26582 return true;
26583
26584 return false;
26585 }
26586
26587 /* The type of a non-type template parm declared with a placeholder type
26588 depends on the corresponding template argument, even though
26589 placeholders are not normally considered dependent. */
26590 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26591 && is_auto (TREE_TYPE (expression)))
26592 return true;
26593
26594 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26595
26596 /* Dependent type attributes might not have made it from the decl to
26597 the type yet. */
26598 if (DECL_P (expression)
26599 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26600 return true;
26601
26602 return (dependent_type_p (TREE_TYPE (expression)));
26603 }
26604
26605 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26606 type-dependent if the expression refers to a member of the current
26607 instantiation and the type of the referenced member is dependent, or the
26608 class member access expression refers to a member of an unknown
26609 specialization.
26610
26611 This function returns true if the OBJECT in such a class member access
26612 expression is of an unknown specialization. */
26613
26614 bool
26615 type_dependent_object_expression_p (tree object)
26616 {
26617 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26618 dependent. */
26619 if (TREE_CODE (object) == IDENTIFIER_NODE)
26620 return true;
26621 tree scope = TREE_TYPE (object);
26622 return (!scope || dependent_scope_p (scope));
26623 }
26624
26625 /* walk_tree callback function for instantiation_dependent_expression_p,
26626 below. Returns non-zero if a dependent subexpression is found. */
26627
26628 static tree
26629 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26630 void * /*data*/)
26631 {
26632 if (TYPE_P (*tp))
26633 {
26634 /* We don't have to worry about decltype currently because decltype
26635 of an instantiation-dependent expr is a dependent type. This
26636 might change depending on the resolution of DR 1172. */
26637 *walk_subtrees = false;
26638 return NULL_TREE;
26639 }
26640 enum tree_code code = TREE_CODE (*tp);
26641 switch (code)
26642 {
26643 /* Don't treat an argument list as dependent just because it has no
26644 TREE_TYPE. */
26645 case TREE_LIST:
26646 case TREE_VEC:
26647 case NONTYPE_ARGUMENT_PACK:
26648 return NULL_TREE;
26649
26650 case TEMPLATE_PARM_INDEX:
26651 if (dependent_type_p (TREE_TYPE (*tp)))
26652 return *tp;
26653 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26654 return *tp;
26655 /* We'll check value-dependence separately. */
26656 return NULL_TREE;
26657
26658 /* Handle expressions with type operands. */
26659 case SIZEOF_EXPR:
26660 case ALIGNOF_EXPR:
26661 case TYPEID_EXPR:
26662 case AT_ENCODE_EXPR:
26663 {
26664 tree op = TREE_OPERAND (*tp, 0);
26665 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26666 op = TREE_TYPE (op);
26667 if (TYPE_P (op))
26668 {
26669 if (dependent_type_p (op))
26670 return *tp;
26671 else
26672 {
26673 *walk_subtrees = false;
26674 return NULL_TREE;
26675 }
26676 }
26677 break;
26678 }
26679
26680 case COMPONENT_REF:
26681 if (identifier_p (TREE_OPERAND (*tp, 1)))
26682 /* In a template, finish_class_member_access_expr creates a
26683 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26684 type-dependent, so that we can check access control at
26685 instantiation time (PR 42277). See also Core issue 1273. */
26686 return *tp;
26687 break;
26688
26689 case SCOPE_REF:
26690 if (instantiation_dependent_scope_ref_p (*tp))
26691 return *tp;
26692 else
26693 break;
26694
26695 /* Treat statement-expressions as dependent. */
26696 case BIND_EXPR:
26697 return *tp;
26698
26699 /* Treat requires-expressions as dependent. */
26700 case REQUIRES_EXPR:
26701 return *tp;
26702
26703 case CALL_EXPR:
26704 /* Treat concept checks as dependent. */
26705 if (concept_check_p (*tp))
26706 return *tp;
26707 break;
26708
26709 case TEMPLATE_ID_EXPR:
26710 /* Treat concept checks as dependent. */
26711 if (concept_check_p (*tp))
26712 return *tp;
26713 break;
26714
26715 case CONSTRUCTOR:
26716 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26717 return *tp;
26718 break;
26719
26720 default:
26721 break;
26722 }
26723
26724 if (type_dependent_expression_p (*tp))
26725 return *tp;
26726 else
26727 return NULL_TREE;
26728 }
26729
26730 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26731 sense defined by the ABI:
26732
26733 "An expression is instantiation-dependent if it is type-dependent
26734 or value-dependent, or it has a subexpression that is type-dependent
26735 or value-dependent."
26736
26737 Except don't actually check value-dependence for unevaluated expressions,
26738 because in sizeof(i) we don't care about the value of i. Checking
26739 type-dependence will in turn check value-dependence of array bounds/template
26740 arguments as needed. */
26741
26742 bool
26743 instantiation_dependent_uneval_expression_p (tree expression)
26744 {
26745 tree result;
26746
26747 if (!processing_template_decl)
26748 return false;
26749
26750 if (expression == error_mark_node)
26751 return false;
26752
26753 result = cp_walk_tree_without_duplicates (&expression,
26754 instantiation_dependent_r, NULL);
26755 return result != NULL_TREE;
26756 }
26757
26758 /* As above, but also check value-dependence of the expression as a whole. */
26759
26760 bool
26761 instantiation_dependent_expression_p (tree expression)
26762 {
26763 return (instantiation_dependent_uneval_expression_p (expression)
26764 || value_dependent_expression_p (expression));
26765 }
26766
26767 /* Like type_dependent_expression_p, but it also works while not processing
26768 a template definition, i.e. during substitution or mangling. */
26769
26770 bool
26771 type_dependent_expression_p_push (tree expr)
26772 {
26773 bool b;
26774 ++processing_template_decl;
26775 b = type_dependent_expression_p (expr);
26776 --processing_template_decl;
26777 return b;
26778 }
26779
26780 /* Returns TRUE if ARGS contains a type-dependent expression. */
26781
26782 bool
26783 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26784 {
26785 unsigned int i;
26786 tree arg;
26787
26788 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26789 {
26790 if (type_dependent_expression_p (arg))
26791 return true;
26792 }
26793 return false;
26794 }
26795
26796 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26797 expressions) contains any type-dependent expressions. */
26798
26799 bool
26800 any_type_dependent_elements_p (const_tree list)
26801 {
26802 for (; list; list = TREE_CHAIN (list))
26803 if (type_dependent_expression_p (TREE_VALUE (list)))
26804 return true;
26805
26806 return false;
26807 }
26808
26809 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26810 expressions) contains any value-dependent expressions. */
26811
26812 bool
26813 any_value_dependent_elements_p (const_tree list)
26814 {
26815 for (; list; list = TREE_CHAIN (list))
26816 if (value_dependent_expression_p (TREE_VALUE (list)))
26817 return true;
26818
26819 return false;
26820 }
26821
26822 /* Returns TRUE if the ARG (a template argument) is dependent. */
26823
26824 bool
26825 dependent_template_arg_p (tree arg)
26826 {
26827 if (!processing_template_decl)
26828 return false;
26829
26830 /* Assume a template argument that was wrongly written by the user
26831 is dependent. This is consistent with what
26832 any_dependent_template_arguments_p [that calls this function]
26833 does. */
26834 if (!arg || arg == error_mark_node)
26835 return true;
26836
26837 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26838 arg = argument_pack_select_arg (arg);
26839
26840 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26841 return true;
26842 if (TREE_CODE (arg) == TEMPLATE_DECL)
26843 {
26844 if (DECL_TEMPLATE_PARM_P (arg))
26845 return true;
26846 /* A member template of a dependent class is not necessarily
26847 type-dependent, but it is a dependent template argument because it
26848 will be a member of an unknown specialization to that template. */
26849 tree scope = CP_DECL_CONTEXT (arg);
26850 return TYPE_P (scope) && dependent_type_p (scope);
26851 }
26852 else if (ARGUMENT_PACK_P (arg))
26853 {
26854 tree args = ARGUMENT_PACK_ARGS (arg);
26855 int i, len = TREE_VEC_LENGTH (args);
26856 for (i = 0; i < len; ++i)
26857 {
26858 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26859 return true;
26860 }
26861
26862 return false;
26863 }
26864 else if (TYPE_P (arg))
26865 return dependent_type_p (arg);
26866 else
26867 return (type_dependent_expression_p (arg)
26868 || value_dependent_expression_p (arg));
26869 }
26870
26871 /* Returns true if ARGS (a collection of template arguments) contains
26872 any types that require structural equality testing. */
26873
26874 bool
26875 any_template_arguments_need_structural_equality_p (tree args)
26876 {
26877 int i;
26878 int j;
26879
26880 if (!args)
26881 return false;
26882 if (args == error_mark_node)
26883 return true;
26884
26885 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26886 {
26887 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26888 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26889 {
26890 tree arg = TREE_VEC_ELT (level, j);
26891 tree packed_args = NULL_TREE;
26892 int k, len = 1;
26893
26894 if (ARGUMENT_PACK_P (arg))
26895 {
26896 /* Look inside the argument pack. */
26897 packed_args = ARGUMENT_PACK_ARGS (arg);
26898 len = TREE_VEC_LENGTH (packed_args);
26899 }
26900
26901 for (k = 0; k < len; ++k)
26902 {
26903 if (packed_args)
26904 arg = TREE_VEC_ELT (packed_args, k);
26905
26906 if (error_operand_p (arg))
26907 return true;
26908 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26909 continue;
26910 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26911 return true;
26912 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26913 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26914 return true;
26915 }
26916 }
26917 }
26918
26919 return false;
26920 }
26921
26922 /* Returns true if ARGS (a collection of template arguments) contains
26923 any dependent arguments. */
26924
26925 bool
26926 any_dependent_template_arguments_p (const_tree args)
26927 {
26928 int i;
26929 int j;
26930
26931 if (!args)
26932 return false;
26933 if (args == error_mark_node)
26934 return true;
26935
26936 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26937 {
26938 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26939 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26940 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26941 return true;
26942 }
26943
26944 return false;
26945 }
26946
26947 /* Returns true if ARGS contains any errors. */
26948
26949 bool
26950 any_erroneous_template_args_p (const_tree args)
26951 {
26952 int i;
26953 int j;
26954
26955 if (args == error_mark_node)
26956 return true;
26957
26958 if (args && TREE_CODE (args) != TREE_VEC)
26959 {
26960 if (tree ti = get_template_info (args))
26961 args = TI_ARGS (ti);
26962 else
26963 args = NULL_TREE;
26964 }
26965
26966 if (!args)
26967 return false;
26968
26969 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26970 {
26971 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26972 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26973 if (error_operand_p (TREE_VEC_ELT (level, j)))
26974 return true;
26975 }
26976
26977 return false;
26978 }
26979
26980 /* Returns TRUE if the template TMPL is type-dependent. */
26981
26982 bool
26983 dependent_template_p (tree tmpl)
26984 {
26985 if (TREE_CODE (tmpl) == OVERLOAD)
26986 {
26987 for (lkp_iterator iter (tmpl); iter; ++iter)
26988 if (dependent_template_p (*iter))
26989 return true;
26990 return false;
26991 }
26992
26993 /* Template template parameters are dependent. */
26994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26995 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26996 return true;
26997 /* So are names that have not been looked up. */
26998 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26999 return true;
27000 return false;
27001 }
27002
27003 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27004
27005 bool
27006 dependent_template_id_p (tree tmpl, tree args)
27007 {
27008 return (dependent_template_p (tmpl)
27009 || any_dependent_template_arguments_p (args));
27010 }
27011
27012 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27013 are dependent. */
27014
27015 bool
27016 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27017 {
27018 int i;
27019
27020 if (!processing_template_decl)
27021 return false;
27022
27023 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27024 {
27025 tree decl = TREE_VEC_ELT (declv, i);
27026 tree init = TREE_VEC_ELT (initv, i);
27027 tree cond = TREE_VEC_ELT (condv, i);
27028 tree incr = TREE_VEC_ELT (incrv, i);
27029
27030 if (type_dependent_expression_p (decl)
27031 || TREE_CODE (decl) == SCOPE_REF)
27032 return true;
27033
27034 if (init && type_dependent_expression_p (init))
27035 return true;
27036
27037 if (cond == global_namespace)
27038 return true;
27039
27040 if (type_dependent_expression_p (cond))
27041 return true;
27042
27043 if (COMPARISON_CLASS_P (cond)
27044 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27045 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27046 return true;
27047
27048 if (TREE_CODE (incr) == MODOP_EXPR)
27049 {
27050 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27051 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27052 return true;
27053 }
27054 else if (type_dependent_expression_p (incr))
27055 return true;
27056 else if (TREE_CODE (incr) == MODIFY_EXPR)
27057 {
27058 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27059 return true;
27060 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27061 {
27062 tree t = TREE_OPERAND (incr, 1);
27063 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27064 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27065 return true;
27066
27067 /* If this loop has a class iterator with != comparison
27068 with increment other than i++/++i/i--/--i, make sure the
27069 increment is constant. */
27070 if (CLASS_TYPE_P (TREE_TYPE (decl))
27071 && TREE_CODE (cond) == NE_EXPR)
27072 {
27073 if (TREE_OPERAND (t, 0) == decl)
27074 t = TREE_OPERAND (t, 1);
27075 else
27076 t = TREE_OPERAND (t, 0);
27077 if (TREE_CODE (t) != INTEGER_CST)
27078 return true;
27079 }
27080 }
27081 }
27082 }
27083
27084 return false;
27085 }
27086
27087 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27088 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27089 no such TYPE can be found. Note that this function peers inside
27090 uninstantiated templates and therefore should be used only in
27091 extremely limited situations. ONLY_CURRENT_P restricts this
27092 peering to the currently open classes hierarchy (which is required
27093 when comparing types). */
27094
27095 tree
27096 resolve_typename_type (tree type, bool only_current_p)
27097 {
27098 tree scope;
27099 tree name;
27100 tree decl;
27101 int quals;
27102 tree pushed_scope;
27103 tree result;
27104
27105 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27106
27107 scope = TYPE_CONTEXT (type);
27108 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27109 gcc_checking_assert (uses_template_parms (scope));
27110
27111 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27112 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27113 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27114 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27115 identifier of the TYPENAME_TYPE anymore.
27116 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27117 TYPENAME_TYPE instead, we avoid messing up with a possible
27118 typedef variant case. */
27119 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27120
27121 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27122 it first before we can figure out what NAME refers to. */
27123 if (TREE_CODE (scope) == TYPENAME_TYPE)
27124 {
27125 if (TYPENAME_IS_RESOLVING_P (scope))
27126 /* Given a class template A with a dependent base with nested type C,
27127 typedef typename A::C::C C will land us here, as trying to resolve
27128 the initial A::C leads to the local C typedef, which leads back to
27129 A::C::C. So we break the recursion now. */
27130 return type;
27131 else
27132 scope = resolve_typename_type (scope, only_current_p);
27133 }
27134 /* If we don't know what SCOPE refers to, then we cannot resolve the
27135 TYPENAME_TYPE. */
27136 if (!CLASS_TYPE_P (scope))
27137 return type;
27138 /* If this is a typedef, we don't want to look inside (c++/11987). */
27139 if (typedef_variant_p (type))
27140 return type;
27141 /* If SCOPE isn't the template itself, it will not have a valid
27142 TYPE_FIELDS list. */
27143 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27144 /* scope is either the template itself or a compatible instantiation
27145 like X<T>, so look up the name in the original template. */
27146 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27147 /* If scope has no fields, it can't be a current instantiation. Check this
27148 before currently_open_class to avoid infinite recursion (71515). */
27149 if (!TYPE_FIELDS (scope))
27150 return type;
27151 /* If the SCOPE is not the current instantiation, there's no reason
27152 to look inside it. */
27153 if (only_current_p && !currently_open_class (scope))
27154 return type;
27155 /* Enter the SCOPE so that name lookup will be resolved as if we
27156 were in the class definition. In particular, SCOPE will no
27157 longer be considered a dependent type. */
27158 pushed_scope = push_scope (scope);
27159 /* Look up the declaration. */
27160 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27161 tf_warning_or_error);
27162
27163 result = NULL_TREE;
27164
27165 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27166 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27167 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27168 if (!decl)
27169 /*nop*/;
27170 else if (identifier_p (fullname)
27171 && TREE_CODE (decl) == TYPE_DECL)
27172 {
27173 result = TREE_TYPE (decl);
27174 if (result == error_mark_node)
27175 result = NULL_TREE;
27176 }
27177 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27178 && DECL_CLASS_TEMPLATE_P (decl))
27179 {
27180 /* Obtain the template and the arguments. */
27181 tree tmpl = TREE_OPERAND (fullname, 0);
27182 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27183 {
27184 /* We get here with a plain identifier because a previous tentative
27185 parse of the nested-name-specifier as part of a ptr-operator saw
27186 ::template X<A>. The use of ::template is necessary in a
27187 ptr-operator, but wrong in a declarator-id.
27188
27189 [temp.names]: In a qualified-id of a declarator-id, the keyword
27190 template shall not appear at the top level. */
27191 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27192 "keyword %<template%> not allowed in declarator-id");
27193 tmpl = decl;
27194 }
27195 tree args = TREE_OPERAND (fullname, 1);
27196 /* Instantiate the template. */
27197 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27198 /*entering_scope=*/true,
27199 tf_error | tf_user);
27200 if (result == error_mark_node)
27201 result = NULL_TREE;
27202 }
27203
27204 /* Leave the SCOPE. */
27205 if (pushed_scope)
27206 pop_scope (pushed_scope);
27207
27208 /* If we failed to resolve it, return the original typename. */
27209 if (!result)
27210 return type;
27211
27212 /* If lookup found a typename type, resolve that too. */
27213 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27214 {
27215 /* Ill-formed programs can cause infinite recursion here, so we
27216 must catch that. */
27217 TYPENAME_IS_RESOLVING_P (result) = 1;
27218 result = resolve_typename_type (result, only_current_p);
27219 TYPENAME_IS_RESOLVING_P (result) = 0;
27220 }
27221
27222 /* Qualify the resulting type. */
27223 quals = cp_type_quals (type);
27224 if (quals)
27225 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27226
27227 return result;
27228 }
27229
27230 /* EXPR is an expression which is not type-dependent. Return a proxy
27231 for EXPR that can be used to compute the types of larger
27232 expressions containing EXPR. */
27233
27234 tree
27235 build_non_dependent_expr (tree expr)
27236 {
27237 tree orig_expr = expr;
27238 tree inner_expr;
27239
27240 /* When checking, try to get a constant value for all non-dependent
27241 expressions in order to expose bugs in *_dependent_expression_p
27242 and constexpr. This can affect code generation, see PR70704, so
27243 only do this for -fchecking=2. */
27244 if (flag_checking > 1
27245 && cxx_dialect >= cxx11
27246 /* Don't do this during nsdmi parsing as it can lead to
27247 unexpected recursive instantiations. */
27248 && !parsing_nsdmi ()
27249 /* Don't do this during concept processing either and for
27250 the same reason. */
27251 && !processing_constraint_expression_p ())
27252 fold_non_dependent_expr (expr, tf_none);
27253
27254 STRIP_ANY_LOCATION_WRAPPER (expr);
27255
27256 /* Preserve OVERLOADs; the functions must be available to resolve
27257 types. */
27258 inner_expr = expr;
27259 if (TREE_CODE (inner_expr) == STMT_EXPR)
27260 inner_expr = stmt_expr_value_expr (inner_expr);
27261 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27262 inner_expr = TREE_OPERAND (inner_expr, 0);
27263 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27264 inner_expr = TREE_OPERAND (inner_expr, 1);
27265 if (is_overloaded_fn (inner_expr)
27266 || TREE_CODE (inner_expr) == OFFSET_REF)
27267 return orig_expr;
27268 /* There is no need to return a proxy for a variable or enumerator. */
27269 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27270 return orig_expr;
27271 /* Preserve string constants; conversions from string constants to
27272 "char *" are allowed, even though normally a "const char *"
27273 cannot be used to initialize a "char *". */
27274 if (TREE_CODE (expr) == STRING_CST)
27275 return orig_expr;
27276 /* Preserve void and arithmetic constants, as an optimization -- there is no
27277 reason to create a new node. */
27278 if (TREE_CODE (expr) == VOID_CST
27279 || TREE_CODE (expr) == INTEGER_CST
27280 || TREE_CODE (expr) == REAL_CST)
27281 return orig_expr;
27282 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27283 There is at least one place where we want to know that a
27284 particular expression is a throw-expression: when checking a ?:
27285 expression, there are special rules if the second or third
27286 argument is a throw-expression. */
27287 if (TREE_CODE (expr) == THROW_EXPR)
27288 return orig_expr;
27289
27290 /* Don't wrap an initializer list, we need to be able to look inside. */
27291 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27292 return orig_expr;
27293
27294 /* Don't wrap a dummy object, we need to be able to test for it. */
27295 if (is_dummy_object (expr))
27296 return orig_expr;
27297
27298 if (TREE_CODE (expr) == COND_EXPR)
27299 return build3 (COND_EXPR,
27300 TREE_TYPE (expr),
27301 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27302 (TREE_OPERAND (expr, 1)
27303 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27304 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27305 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27306 if (TREE_CODE (expr) == COMPOUND_EXPR
27307 && !COMPOUND_EXPR_OVERLOADED (expr))
27308 return build2 (COMPOUND_EXPR,
27309 TREE_TYPE (expr),
27310 TREE_OPERAND (expr, 0),
27311 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27312
27313 /* If the type is unknown, it can't really be non-dependent */
27314 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27315
27316 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27317 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27318 TREE_TYPE (expr), expr);
27319 }
27320
27321 /* ARGS is a vector of expressions as arguments to a function call.
27322 Replace the arguments with equivalent non-dependent expressions.
27323 This modifies ARGS in place. */
27324
27325 void
27326 make_args_non_dependent (vec<tree, va_gc> *args)
27327 {
27328 unsigned int ix;
27329 tree arg;
27330
27331 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27332 {
27333 tree newarg = build_non_dependent_expr (arg);
27334 if (newarg != arg)
27335 (*args)[ix] = newarg;
27336 }
27337 }
27338
27339 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27340 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27341 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27342
27343 static tree
27344 make_auto_1 (tree name, bool set_canonical)
27345 {
27346 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27347 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27348 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27349 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27350 (0, processing_template_decl + 1, processing_template_decl + 1,
27351 TYPE_NAME (au), NULL_TREE);
27352 if (set_canonical)
27353 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27354 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27355 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27356
27357 return au;
27358 }
27359
27360 tree
27361 make_decltype_auto (void)
27362 {
27363 return make_auto_1 (decltype_auto_identifier, true);
27364 }
27365
27366 tree
27367 make_auto (void)
27368 {
27369 return make_auto_1 (auto_identifier, true);
27370 }
27371
27372 /* Return a C++17 deduction placeholder for class template TMPL. */
27373
27374 tree
27375 make_template_placeholder (tree tmpl)
27376 {
27377 tree t = make_auto_1 (auto_identifier, false);
27378 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27379 /* Our canonical type depends on the placeholder. */
27380 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27381 return t;
27382 }
27383
27384 /* True iff T is a C++17 class template deduction placeholder. */
27385
27386 bool
27387 template_placeholder_p (tree t)
27388 {
27389 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27390 }
27391
27392 /* Make a "constrained auto" type-specifier. This is an auto or
27393 decltype(auto) type with constraints that must be associated after
27394 deduction. The constraint is formed from the given concept CON
27395 and its optional sequence of template arguments ARGS.
27396
27397 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27398
27399 static tree
27400 make_constrained_placeholder_type (tree type, tree con, tree args)
27401 {
27402 /* Build the constraint. */
27403 tree tmpl = DECL_TI_TEMPLATE (con);
27404 tree expr = tmpl;
27405 if (TREE_CODE (con) == FUNCTION_DECL)
27406 expr = ovl_make (tmpl);
27407 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27408
27409 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27410
27411 /* Our canonical type depends on the constraint. */
27412 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27413
27414 /* Attach the constraint to the type declaration. */
27415 return TYPE_NAME (type);
27416 }
27417
27418 /* Make a "constrained auto" type-specifier. */
27419
27420 tree
27421 make_constrained_auto (tree con, tree args)
27422 {
27423 tree type = make_auto_1 (auto_identifier, false);
27424 return make_constrained_placeholder_type (type, con, args);
27425 }
27426
27427 /* Make a "constrained decltype(auto)" type-specifier. */
27428
27429 tree
27430 make_constrained_decltype_auto (tree con, tree args)
27431 {
27432 tree type = make_auto_1 (decltype_auto_identifier, false);
27433 /* FIXME: I don't know why this isn't done in make_auto_1. */
27434 AUTO_IS_DECLTYPE (type) = true;
27435 return make_constrained_placeholder_type (type, con, args);
27436 }
27437
27438 /* Build and return a concept definition. Like other templates, the
27439 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27440 the TEMPLATE_DECL. */
27441
27442 tree
27443 finish_concept_definition (cp_expr id, tree init)
27444 {
27445 gcc_assert (identifier_p (id));
27446 gcc_assert (processing_template_decl);
27447
27448 location_t loc = id.get_location();
27449
27450 /* A concept-definition shall not have associated constraints. */
27451 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27452 {
27453 error_at (loc, "a concept cannot be constrained");
27454 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27455 }
27456
27457 /* A concept-definition shall appear in namespace scope. Templates
27458 aren't allowed in block scope, so we only need to check for class
27459 scope. */
27460 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27461 {
27462 error_at (loc, "concept %qE not in namespace scope", *id);
27463 return error_mark_node;
27464 }
27465
27466 /* Initially build the concept declaration; it's type is bool. */
27467 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27468 DECL_CONTEXT (decl) = current_scope ();
27469 DECL_INITIAL (decl) = init;
27470
27471 /* Push the enclosing template. */
27472 return push_template_decl (decl);
27473 }
27474
27475 /* Given type ARG, return std::initializer_list<ARG>. */
27476
27477 static tree
27478 listify (tree arg)
27479 {
27480 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27481
27482 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27483 {
27484 gcc_rich_location richloc (input_location);
27485 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27486 error_at (&richloc,
27487 "deducing from brace-enclosed initializer list"
27488 " requires %<#include <initializer_list>%>");
27489
27490 return error_mark_node;
27491 }
27492 tree argvec = make_tree_vec (1);
27493 TREE_VEC_ELT (argvec, 0) = arg;
27494
27495 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27496 NULL_TREE, 0, tf_warning_or_error);
27497 }
27498
27499 /* Replace auto in TYPE with std::initializer_list<auto>. */
27500
27501 static tree
27502 listify_autos (tree type, tree auto_node)
27503 {
27504 tree init_auto = listify (strip_top_quals (auto_node));
27505 tree argvec = make_tree_vec (1);
27506 TREE_VEC_ELT (argvec, 0) = init_auto;
27507 if (processing_template_decl)
27508 argvec = add_to_template_args (current_template_args (), argvec);
27509 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27510 }
27511
27512 /* Hash traits for hashing possibly constrained 'auto'
27513 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27514
27515 struct auto_hash : default_hash_traits<tree>
27516 {
27517 static inline hashval_t hash (tree);
27518 static inline bool equal (tree, tree);
27519 };
27520
27521 /* Hash the 'auto' T. */
27522
27523 inline hashval_t
27524 auto_hash::hash (tree t)
27525 {
27526 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27527 /* Matching constrained-type-specifiers denote the same template
27528 parameter, so hash the constraint. */
27529 return hash_placeholder_constraint (c);
27530 else
27531 /* But unconstrained autos are all separate, so just hash the pointer. */
27532 return iterative_hash_object (t, 0);
27533 }
27534
27535 /* Compare two 'auto's. */
27536
27537 inline bool
27538 auto_hash::equal (tree t1, tree t2)
27539 {
27540 if (t1 == t2)
27541 return true;
27542
27543 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27544 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27545
27546 /* Two unconstrained autos are distinct. */
27547 if (!c1 || !c2)
27548 return false;
27549
27550 return equivalent_placeholder_constraints (c1, c2);
27551 }
27552
27553 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27554 constrained) auto, add it to the vector. */
27555
27556 static int
27557 extract_autos_r (tree t, void *data)
27558 {
27559 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27560 if (is_auto (t))
27561 {
27562 /* All the autos were built with index 0; fix that up now. */
27563 tree *p = hash.find_slot (t, INSERT);
27564 unsigned idx;
27565 if (*p)
27566 /* If this is a repeated constrained-type-specifier, use the index we
27567 chose before. */
27568 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27569 else
27570 {
27571 /* Otherwise this is new, so use the current count. */
27572 *p = t;
27573 idx = hash.elements () - 1;
27574 }
27575 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27576 }
27577
27578 /* Always keep walking. */
27579 return 0;
27580 }
27581
27582 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27583 says they can appear anywhere in the type. */
27584
27585 static tree
27586 extract_autos (tree type)
27587 {
27588 hash_set<tree> visited;
27589 hash_table<auto_hash> hash (2);
27590
27591 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27592
27593 tree tree_vec = make_tree_vec (hash.elements());
27594 for (hash_table<auto_hash>::iterator iter = hash.begin();
27595 iter != hash.end(); ++iter)
27596 {
27597 tree elt = *iter;
27598 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27599 TREE_VEC_ELT (tree_vec, i)
27600 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27601 }
27602
27603 return tree_vec;
27604 }
27605
27606 /* The stem for deduction guide names. */
27607 const char *const dguide_base = "__dguide_";
27608
27609 /* Return the name for a deduction guide for class template TMPL. */
27610
27611 tree
27612 dguide_name (tree tmpl)
27613 {
27614 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27615 tree tname = TYPE_IDENTIFIER (type);
27616 char *buf = (char *) alloca (1 + strlen (dguide_base)
27617 + IDENTIFIER_LENGTH (tname));
27618 memcpy (buf, dguide_base, strlen (dguide_base));
27619 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27620 IDENTIFIER_LENGTH (tname) + 1);
27621 tree dname = get_identifier (buf);
27622 TREE_TYPE (dname) = type;
27623 return dname;
27624 }
27625
27626 /* True if NAME is the name of a deduction guide. */
27627
27628 bool
27629 dguide_name_p (tree name)
27630 {
27631 return (TREE_CODE (name) == IDENTIFIER_NODE
27632 && TREE_TYPE (name)
27633 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27634 strlen (dguide_base)));
27635 }
27636
27637 /* True if FN is a deduction guide. */
27638
27639 bool
27640 deduction_guide_p (const_tree fn)
27641 {
27642 if (DECL_P (fn))
27643 if (tree name = DECL_NAME (fn))
27644 return dguide_name_p (name);
27645 return false;
27646 }
27647
27648 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27649
27650 bool
27651 copy_guide_p (const_tree fn)
27652 {
27653 gcc_assert (deduction_guide_p (fn));
27654 if (!DECL_ARTIFICIAL (fn))
27655 return false;
27656 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27657 return (TREE_CHAIN (parms) == void_list_node
27658 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27659 }
27660
27661 /* True if FN is a guide generated from a constructor template. */
27662
27663 bool
27664 template_guide_p (const_tree fn)
27665 {
27666 gcc_assert (deduction_guide_p (fn));
27667 if (!DECL_ARTIFICIAL (fn))
27668 return false;
27669 tree tmpl = DECL_TI_TEMPLATE (fn);
27670 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27671 return PRIMARY_TEMPLATE_P (org);
27672 return false;
27673 }
27674
27675 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27676 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27677 template parameter types. Note that the handling of template template
27678 parameters relies on current_template_parms being set appropriately for the
27679 new template. */
27680
27681 static tree
27682 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27683 tree tsubst_args, tsubst_flags_t complain)
27684 {
27685 if (olddecl == error_mark_node)
27686 return error_mark_node;
27687
27688 tree oldidx = get_template_parm_index (olddecl);
27689
27690 tree newtype;
27691 if (TREE_CODE (olddecl) == TYPE_DECL
27692 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27693 {
27694 tree oldtype = TREE_TYPE (olddecl);
27695 newtype = cxx_make_type (TREE_CODE (oldtype));
27696 TYPE_MAIN_VARIANT (newtype) = newtype;
27697 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27698 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27699 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27700 }
27701 else
27702 {
27703 newtype = TREE_TYPE (olddecl);
27704 if (type_uses_auto (newtype))
27705 {
27706 // Substitute once to fix references to other template parameters.
27707 newtype = tsubst (newtype, tsubst_args,
27708 complain|tf_partial, NULL_TREE);
27709 // Now substitute again to reduce the level of the auto.
27710 newtype = tsubst (newtype, current_template_args (),
27711 complain, NULL_TREE);
27712 }
27713 else
27714 newtype = tsubst (newtype, tsubst_args,
27715 complain, NULL_TREE);
27716 }
27717
27718 tree newdecl
27719 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27720 DECL_NAME (olddecl), newtype);
27721 SET_DECL_TEMPLATE_PARM_P (newdecl);
27722
27723 tree newidx;
27724 if (TREE_CODE (olddecl) == TYPE_DECL
27725 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27726 {
27727 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27728 = build_template_parm_index (index, level, level,
27729 newdecl, newtype);
27730 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27731 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27732 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27733 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27734
27735 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27736 {
27737 DECL_TEMPLATE_RESULT (newdecl)
27738 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27739 DECL_NAME (olddecl), newtype);
27740 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27741 // First create a copy (ttargs) of tsubst_args with an
27742 // additional level for the template template parameter's own
27743 // template parameters (ttparms).
27744 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27745 (DECL_TEMPLATE_PARMS (olddecl)));
27746 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27747 tree ttargs = make_tree_vec (depth + 1);
27748 for (int i = 0; i < depth; ++i)
27749 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27750 TREE_VEC_ELT (ttargs, depth)
27751 = template_parms_level_to_args (ttparms);
27752 // Substitute ttargs into ttparms to fix references to
27753 // other template parameters.
27754 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27755 complain|tf_partial);
27756 // Now substitute again with args based on tparms, to reduce
27757 // the level of the ttparms.
27758 ttargs = current_template_args ();
27759 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27760 complain);
27761 // Finally, tack the adjusted parms onto tparms.
27762 ttparms = tree_cons (size_int (depth), ttparms,
27763 current_template_parms);
27764 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27765 }
27766 }
27767 else
27768 {
27769 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27770 tree newconst
27771 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27772 TREE_CODE (oldconst),
27773 DECL_NAME (oldconst), newtype);
27774 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27775 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27776 SET_DECL_TEMPLATE_PARM_P (newconst);
27777 newidx = build_template_parm_index (index, level, level,
27778 newconst, newtype);
27779 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27780 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27781 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27782 }
27783
27784 return newdecl;
27785 }
27786
27787 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
27788 template parameter. */
27789
27790 static tree
27791 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
27792 tree targs, unsigned targs_index, tsubst_flags_t complain)
27793 {
27794 tree olddecl = TREE_VALUE (oldelt);
27795 tree newdecl = rewrite_template_parm (olddecl, index, level,
27796 targs, complain);
27797 if (newdecl == error_mark_node)
27798 return error_mark_node;
27799 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27800 targs, complain, NULL_TREE);
27801 tree list = build_tree_list (newdef, newdecl);
27802 TEMPLATE_PARM_CONSTRAINTS (list)
27803 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27804 targs, complain, NULL_TREE);
27805 int depth = TMPL_ARGS_DEPTH (targs);
27806 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
27807 return list;
27808 }
27809
27810 /* Returns a C++17 class deduction guide template based on the constructor
27811 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27812 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27813 aggregate initialization guide. */
27814
27815 static tree
27816 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27817 {
27818 tree tparms, targs, fparms, fargs, ci;
27819 bool memtmpl = false;
27820 bool explicit_p;
27821 location_t loc;
27822 tree fn_tmpl = NULL_TREE;
27823
27824 if (outer_args)
27825 {
27826 ++processing_template_decl;
27827 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
27828 --processing_template_decl;
27829 }
27830
27831 if (!DECL_DECLARES_FUNCTION_P (ctor))
27832 {
27833 if (TYPE_P (ctor))
27834 {
27835 bool copy_p = TYPE_REF_P (ctor);
27836 if (copy_p)
27837 fparms = tree_cons (NULL_TREE, type, void_list_node);
27838 else
27839 fparms = void_list_node;
27840 }
27841 else if (TREE_CODE (ctor) == TREE_LIST)
27842 fparms = ctor;
27843 else
27844 gcc_unreachable ();
27845
27846 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27847 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27848 targs = CLASSTYPE_TI_ARGS (type);
27849 ci = NULL_TREE;
27850 fargs = NULL_TREE;
27851 loc = DECL_SOURCE_LOCATION (ctmpl);
27852 explicit_p = false;
27853 }
27854 else
27855 {
27856 ++processing_template_decl;
27857 bool ok = true;
27858
27859 fn_tmpl
27860 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27861 : DECL_TI_TEMPLATE (ctor));
27862 if (outer_args)
27863 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27864 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27865
27866 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27867 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27868 fully specialized args for the enclosing class. Strip those off, as
27869 the deduction guide won't have those template parameters. */
27870 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27871 TMPL_PARMS_DEPTH (tparms));
27872 /* Discard the 'this' parameter. */
27873 fparms = FUNCTION_ARG_CHAIN (ctor);
27874 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27875 ci = get_constraints (ctor);
27876 loc = DECL_SOURCE_LOCATION (ctor);
27877 explicit_p = DECL_NONCONVERTING_P (ctor);
27878
27879 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27880 {
27881 memtmpl = true;
27882
27883 /* For a member template constructor, we need to flatten the two
27884 template parameter lists into one, and then adjust the function
27885 signature accordingly. This gets...complicated. */
27886 tree save_parms = current_template_parms;
27887
27888 /* For a member template we should have two levels of parms/args, one
27889 for the class and one for the constructor. We stripped
27890 specialized args for further enclosing classes above. */
27891 const int depth = 2;
27892 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27893
27894 /* Template args for translating references to the two-level template
27895 parameters into references to the one-level template parameters we
27896 are creating. */
27897 tree tsubst_args = copy_node (targs);
27898 TMPL_ARGS_LEVEL (tsubst_args, depth)
27899 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27900
27901 /* Template parms for the constructor template. */
27902 tree ftparms = TREE_VALUE (tparms);
27903 unsigned flen = TREE_VEC_LENGTH (ftparms);
27904 /* Template parms for the class template. */
27905 tparms = TREE_CHAIN (tparms);
27906 tree ctparms = TREE_VALUE (tparms);
27907 unsigned clen = TREE_VEC_LENGTH (ctparms);
27908 /* Template parms for the deduction guide start as a copy of the
27909 template parms for the class. We set current_template_parms for
27910 lookup_template_class_1. */
27911 current_template_parms = tparms = copy_node (tparms);
27912 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27913 for (unsigned i = 0; i < clen; ++i)
27914 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27915
27916 /* Now we need to rewrite the constructor parms to append them to the
27917 class parms. */
27918 for (unsigned i = 0; i < flen; ++i)
27919 {
27920 unsigned index = i + clen;
27921 unsigned level = 1;
27922 tree oldelt = TREE_VEC_ELT (ftparms, i);
27923 tree newelt
27924 = rewrite_tparm_list (oldelt, index, level,
27925 tsubst_args, i, complain);
27926 if (newelt == error_mark_node)
27927 ok = false;
27928 TREE_VEC_ELT (new_vec, index) = newelt;
27929 }
27930
27931 /* Now we have a final set of template parms to substitute into the
27932 function signature. */
27933 targs = template_parms_to_args (tparms);
27934 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27935 complain, ctor);
27936 if (fparms == error_mark_node)
27937 ok = false;
27938 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27939 if (ci)
27940 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27941
27942 current_template_parms = save_parms;
27943 }
27944
27945 --processing_template_decl;
27946 if (!ok)
27947 return error_mark_node;
27948 }
27949
27950 if (!memtmpl)
27951 {
27952 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27953 tparms = copy_node (tparms);
27954 INNERMOST_TEMPLATE_PARMS (tparms)
27955 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27956 }
27957
27958 tree fntype = build_function_type (type, fparms);
27959 tree ded_fn = build_lang_decl_loc (loc,
27960 FUNCTION_DECL,
27961 dguide_name (type), fntype);
27962 DECL_ARGUMENTS (ded_fn) = fargs;
27963 DECL_ARTIFICIAL (ded_fn) = true;
27964 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27965 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27966 DECL_ARTIFICIAL (ded_tmpl) = true;
27967 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27968 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27969 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27970 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27971 if (DECL_P (ctor))
27972 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27973 if (ci)
27974 set_constraints (ded_tmpl, ci);
27975
27976 return ded_tmpl;
27977 }
27978
27979 /* Add to LIST the member types for the reshaped initializer CTOR. */
27980
27981 static tree
27982 collect_ctor_idx_types (tree ctor, tree list)
27983 {
27984 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
27985 tree idx, val; unsigned i;
27986 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
27987 {
27988 if (BRACE_ENCLOSED_INITIALIZER_P (val)
27989 && CONSTRUCTOR_NELTS (val))
27990 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
27991 if (TREE_CODE (subidx) == FIELD_DECL)
27992 {
27993 list = collect_ctor_idx_types (val, list);
27994 continue;
27995 }
27996 tree ftype = finish_decltype_type (idx, true, tf_none);
27997 list = tree_cons (NULL_TREE, ftype, list);
27998 }
27999
28000 return list;
28001 }
28002
28003 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28004
28005 static bool
28006 is_spec_or_derived (tree etype, tree tmpl)
28007 {
28008 if (!etype || !CLASS_TYPE_P (etype))
28009 return false;
28010
28011 tree type = TREE_TYPE (tmpl);
28012 tree tparms = (INNERMOST_TEMPLATE_PARMS
28013 (DECL_TEMPLATE_PARMS (tmpl)));
28014 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28015 int err = unify (tparms, targs, type, etype,
28016 UNIFY_ALLOW_DERIVED, /*explain*/false);
28017 ggc_free (targs);
28018 return !err;
28019 }
28020
28021 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28022 INIT. */
28023
28024 static tree
28025 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28026 {
28027 if (cxx_dialect < cxx2a)
28028 return NULL_TREE;
28029
28030 if (init == NULL_TREE)
28031 return NULL_TREE;
28032
28033 tree type = TREE_TYPE (tmpl);
28034 if (!CP_AGGREGATE_TYPE_P (type))
28035 return NULL_TREE;
28036
28037 /* No aggregate candidate for copy-initialization. */
28038 if (args->length() == 1)
28039 {
28040 tree val = (*args)[0];
28041 if (is_spec_or_derived (tmpl, TREE_TYPE (val)))
28042 return NULL_TREE;
28043 }
28044
28045 /* If we encounter a problem, we just won't add the candidate. */
28046 tsubst_flags_t complain = tf_none;
28047
28048 tree parms = NULL_TREE;
28049 if (TREE_CODE (init) == CONSTRUCTOR)
28050 {
28051 init = reshape_init (type, init, complain);
28052 if (init == error_mark_node)
28053 return NULL_TREE;
28054 parms = collect_ctor_idx_types (init, parms);
28055 }
28056 else if (TREE_CODE (init) == TREE_LIST)
28057 {
28058 int len = list_length (init);
28059 for (tree field = TYPE_FIELDS (type);
28060 len;
28061 --len, field = DECL_CHAIN (field))
28062 {
28063 field = next_initializable_field (field);
28064 if (!field)
28065 return NULL_TREE;
28066 tree ftype = finish_decltype_type (field, true, complain);
28067 parms = tree_cons (NULL_TREE, ftype, parms);
28068 }
28069 }
28070 else
28071 /* Aggregate initialization doesn't apply to an initializer expression. */
28072 return NULL_TREE;
28073
28074 if (parms)
28075 {
28076 tree last = parms;
28077 parms = nreverse (parms);
28078 TREE_CHAIN (last) = void_list_node;
28079 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28080 return guide;
28081 }
28082
28083 return NULL_TREE;
28084 }
28085
28086 /* UGUIDES are the deduction guides for the underlying template of alias
28087 template TMPL; adjust them to be deduction guides for TMPL. */
28088
28089 static tree
28090 alias_ctad_tweaks (tree tmpl, tree uguides)
28091 {
28092 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28093 class type (9.2.8.2) where the template-name names an alias template A,
28094 the defining-type-id of A must be of the form
28095
28096 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28097
28098 as specified in 9.2.8.2. The guides of A are the set of functions or
28099 function templates formed as follows. For each function or function
28100 template f in the guides of the template named by the simple-template-id
28101 of the defining-type-id, the template arguments of the return type of f
28102 are deduced from the defining-type-id of A according to the process in
28103 13.10.2.5 with the exception that deduction does not fail if not all
28104 template arguments are deduced. Let g denote the result of substituting
28105 these deductions into f. If substitution succeeds, form a function or
28106 function template f' with the following properties and add it to the set
28107 of guides of A:
28108
28109 * The function type of f' is the function type of g.
28110
28111 * If f is a function template, f' is a function template whose template
28112 parameter list consists of all the template parameters of A (including
28113 their default template arguments) that appear in the above deductions or
28114 (recursively) in their default template arguments, followed by the
28115 template parameters of f that were not deduced (including their default
28116 template arguments), otherwise f' is not a function template.
28117
28118 * The associated constraints (13.5.2) are the conjunction of the
28119 associated constraints of g and a constraint that is satisfied if and only
28120 if the arguments of A are deducible (see below) from the return type.
28121
28122 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
28123 be so as well.
28124
28125 * If f was generated from a deduction-guide (12.4.1.8), then f' is
28126 considered to be so as well.
28127
28128 * The explicit-specifier of f' is the explicit-specifier of g (if
28129 any). */
28130
28131 /* This implementation differs from the above in two significant ways:
28132
28133 1) We include all template parameters of A, not just some.
28134 2) The added constraint is same_type instead of deducible.
28135
28136 I believe that while it's probably possible to construct a testcase that
28137 behaves differently with this simplification, it should have the same
28138 effect for real uses. Including all template parameters means that we
28139 deduce all parameters of A when resolving the call, so when we're in the
28140 constraint we don't need to deduce them again, we can just check whether
28141 the deduction produced the desired result. */
28142
28143 tsubst_flags_t complain = tf_warning_or_error;
28144 tree atype = TREE_TYPE (tmpl);
28145 tree aguides = NULL_TREE;
28146 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
28147 unsigned natparms = TREE_VEC_LENGTH (atparms);
28148 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28149 for (ovl_iterator iter (uguides); iter; ++iter)
28150 {
28151 tree f = *iter;
28152 tree in_decl = f;
28153 location_t loc = DECL_SOURCE_LOCATION (f);
28154 tree ret = TREE_TYPE (TREE_TYPE (f));
28155 tree fprime = f;
28156 if (TREE_CODE (f) == TEMPLATE_DECL)
28157 {
28158 processing_template_decl_sentinel ptds (/*reset*/false);
28159 ++processing_template_decl;
28160
28161 /* Deduce template arguments for f from the type-id of A. */
28162 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
28163 unsigned len = TREE_VEC_LENGTH (ftparms);
28164 tree targs = make_tree_vec (len);
28165 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
28166 gcc_assert (!err);
28167
28168 /* The number of parms for f' is the number of parms for A plus
28169 non-deduced parms of f. */
28170 unsigned ndlen = 0;
28171 unsigned j;
28172 for (unsigned i = 0; i < len; ++i)
28173 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28174 ++ndlen;
28175 tree gtparms = make_tree_vec (natparms + ndlen);
28176
28177 /* First copy over the parms of A. */
28178 for (j = 0; j < natparms; ++j)
28179 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
28180 /* Now rewrite the non-deduced parms of f. */
28181 for (unsigned i = 0; ndlen && i < len; ++i)
28182 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28183 {
28184 --ndlen;
28185 unsigned index = j++;
28186 unsigned level = 1;
28187 tree oldlist = TREE_VEC_ELT (ftparms, i);
28188 tree list = rewrite_tparm_list (oldlist, index, level,
28189 targs, i, complain);
28190 TREE_VEC_ELT (gtparms, index) = list;
28191 }
28192 gtparms = build_tree_list (size_one_node, gtparms);
28193
28194 /* Substitute the deduced arguments plus the rewritten template
28195 parameters into f to get g. This covers the type, copyness,
28196 guideness, and explicit-specifier. */
28197 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
28198 if (g == error_mark_node)
28199 return error_mark_node;
28200 DECL_USE_TEMPLATE (g) = 0;
28201 fprime = build_template_decl (g, gtparms, false);
28202 DECL_TEMPLATE_RESULT (fprime) = g;
28203 TREE_TYPE (fprime) = TREE_TYPE (g);
28204 tree gtargs = template_parms_to_args (gtparms);
28205 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
28206 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
28207
28208 /* Substitute the associated constraints. */
28209 tree ci = get_constraints (f);
28210 if (ci)
28211 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
28212 if (ci == error_mark_node)
28213 return error_mark_node;
28214
28215 /* Add a constraint that the return type matches the instantiation of
28216 A with the same template arguments. */
28217 ret = TREE_TYPE (TREE_TYPE (fprime));
28218 if (!same_type_p (atype, ret)
28219 /* FIXME this should mean they don't compare as equivalent. */
28220 || dependent_alias_template_spec_p (atype, nt_opaque))
28221 {
28222 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
28223 ci = append_constraint (ci, same);
28224 }
28225
28226 if (ci)
28227 set_constraints (fprime, ci);
28228 }
28229 else
28230 {
28231 /* For a non-template deduction guide, if the arguments of A aren't
28232 deducible from the return type, don't add the candidate. */
28233 tree targs = make_tree_vec (natparms);
28234 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
28235 for (unsigned i = 0; !err && i < natparms; ++i)
28236 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
28237 err = true;
28238 if (err)
28239 continue;
28240 }
28241
28242 aguides = lookup_add (fprime, aguides);
28243 }
28244
28245 return aguides;
28246 }
28247
28248 /* Return artificial deduction guides built from the constructors of class
28249 template TMPL. */
28250
28251 static tree
28252 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28253 {
28254 tree type = TREE_TYPE (tmpl);
28255 tree outer_args = NULL_TREE;
28256 if (DECL_CLASS_SCOPE_P (tmpl)
28257 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28258 {
28259 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28260 type = TREE_TYPE (most_general_template (tmpl));
28261 }
28262
28263 tree cands = NULL_TREE;
28264
28265 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28266 {
28267 /* Skip inherited constructors. */
28268 if (iter.using_p ())
28269 continue;
28270
28271 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28272 cands = lookup_add (guide, cands);
28273 }
28274
28275 /* Add implicit default constructor deduction guide. */
28276 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
28277 {
28278 tree guide = build_deduction_guide (type, type, outer_args,
28279 complain);
28280 cands = lookup_add (guide, cands);
28281 }
28282
28283 /* Add copy guide. */
28284 {
28285 tree gtype = build_reference_type (type);
28286 tree guide = build_deduction_guide (type, gtype, outer_args,
28287 complain);
28288 cands = lookup_add (guide, cands);
28289 }
28290
28291 return cands;
28292 }
28293
28294 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
28295
28296 /* Return the non-aggregate deduction guides for deducible template TMPL. The
28297 aggregate candidate is added separately because it depends on the
28298 initializer. */
28299
28300 static tree
28301 deduction_guides_for (tree tmpl, tsubst_flags_t complain)
28302 {
28303 tree guides = NULL_TREE;
28304 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28305 {
28306 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28307 tree tinfo = get_template_info (under);
28308 guides = deduction_guides_for (TI_TEMPLATE (tinfo), complain);
28309 }
28310 else
28311 {
28312 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
28313 dguide_name (tmpl),
28314 /*type*/false, /*complain*/false,
28315 /*hidden*/false);
28316 if (guides == error_mark_node)
28317 guides = NULL_TREE;
28318 }
28319
28320 /* Cache the deduction guides for a template. We also remember the result of
28321 lookup, and rebuild everything if it changes; should be very rare. */
28322 tree_pair_p cache = NULL;
28323 if (tree_pair_p &r
28324 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
28325 {
28326 cache = r;
28327 if (cache->purpose == guides)
28328 return cache->value;
28329 }
28330 else
28331 {
28332 r = cache = ggc_cleared_alloc<tree_pair_s> ();
28333 cache->purpose = guides;
28334 }
28335
28336 tree cands = NULL_TREE;
28337 if (DECL_ALIAS_TEMPLATE_P (tmpl))
28338 cands = alias_ctad_tweaks (tmpl, guides);
28339 else
28340 {
28341 cands = ctor_deduction_guides_for (tmpl, complain);
28342 for (ovl_iterator it (guides); it; ++it)
28343 cands = lookup_add (*it, cands);
28344 }
28345
28346 cache->value = cands;
28347 return cands;
28348 }
28349
28350 /* Return whether TMPL is a (class template argument-) deducible template. */
28351
28352 bool
28353 ctad_template_p (tree tmpl)
28354 {
28355 /* A deducible template is either a class template or is an alias template
28356 whose defining-type-id is of the form
28357
28358 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28359
28360 where the nested-name-specifier (if any) is non-dependent and the
28361 template-name of the simple-template-id names a deducible template. */
28362
28363 if (DECL_CLASS_TEMPLATE_P (tmpl)
28364 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28365 return true;
28366 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
28367 return false;
28368 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
28369 if (tree tinfo = get_template_info (orig))
28370 return ctad_template_p (TI_TEMPLATE (tinfo));
28371 return false;
28372 }
28373
28374 /* Deduce template arguments for the class template placeholder PTYPE for
28375 template TMPL based on the initializer INIT, and return the resulting
28376 type. */
28377
28378 static tree
28379 do_class_deduction (tree ptype, tree tmpl, tree init,
28380 int flags, tsubst_flags_t complain)
28381 {
28382 /* We should have handled this in the caller. */
28383 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28384 return ptype;
28385
28386 /* Look through alias templates that just rename another template. */
28387 tmpl = get_underlying_template (tmpl);
28388 if (!ctad_template_p (tmpl))
28389 {
28390 if (complain & tf_error)
28391 error ("non-deducible template %qT used without template arguments", tmpl);
28392 return error_mark_node;
28393 }
28394 else if (cxx_dialect < cxx2a && DECL_ALIAS_TEMPLATE_P (tmpl))
28395 {
28396 /* This doesn't affect conforming C++17 code, so just pedwarn. */
28397 if (complain & tf_warning_or_error)
28398 pedwarn (input_location, 0, "alias template deduction only available "
28399 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
28400 }
28401
28402 if (init && TREE_TYPE (init) == ptype)
28403 /* Using the template parm as its own argument. */
28404 return ptype;
28405
28406 tree type = TREE_TYPE (tmpl);
28407
28408 bool try_list_ctor = false;
28409
28410 releasing_vec rv_args = NULL;
28411 vec<tree,va_gc> *&args = *&rv_args;
28412 if (init == NULL_TREE)
28413 args = make_tree_vector ();
28414 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28415 {
28416 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
28417 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
28418 {
28419 /* As an exception, the first phase in 16.3.1.7 (considering the
28420 initializer list as a single argument) is omitted if the
28421 initializer list consists of a single expression of type cv U,
28422 where U is a specialization of C or a class derived from a
28423 specialization of C. */
28424 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28425 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
28426 try_list_ctor = false;
28427 }
28428 if (try_list_ctor || is_std_init_list (type))
28429 args = make_tree_vector_single (init);
28430 else
28431 args = make_tree_vector_from_ctor (init);
28432 }
28433 else if (TREE_CODE (init) == TREE_LIST)
28434 args = make_tree_vector_from_list (init);
28435 else
28436 args = make_tree_vector_single (init);
28437
28438 /* Do this now to avoid problems with erroneous args later on. */
28439 args = resolve_args (args, complain);
28440 if (args == NULL)
28441 return error_mark_node;
28442
28443 tree cands = deduction_guides_for (tmpl, complain);
28444 if (cands == error_mark_node)
28445 return error_mark_node;
28446
28447 /* Prune explicit deduction guides in copy-initialization context. */
28448 bool elided = false;
28449 if (flags & LOOKUP_ONLYCONVERTING)
28450 {
28451 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28452 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28453 elided = true;
28454
28455 if (elided)
28456 {
28457 /* Found a nonconverting guide, prune the candidates. */
28458 tree pruned = NULL_TREE;
28459 for (lkp_iterator iter (cands); iter; ++iter)
28460 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28461 pruned = lookup_add (*iter, pruned);
28462
28463 cands = pruned;
28464 }
28465 }
28466
28467 if (tree guide = maybe_aggr_guide (tmpl, init, args))
28468 cands = lookup_add (guide, cands);
28469
28470 tree call = error_mark_node;
28471
28472 /* If this is list-initialization and the class has a list constructor, first
28473 try deducing from the list as a single argument, as [over.match.list]. */
28474 tree list_cands = NULL_TREE;
28475 if (try_list_ctor && cands)
28476 for (lkp_iterator iter (cands); iter; ++iter)
28477 {
28478 tree dg = *iter;
28479 if (is_list_ctor (dg))
28480 list_cands = lookup_add (dg, list_cands);
28481 }
28482 if (list_cands)
28483 {
28484 ++cp_unevaluated_operand;
28485 call = build_new_function_call (list_cands, &args, tf_decltype);
28486 --cp_unevaluated_operand;
28487
28488 if (call == error_mark_node)
28489 {
28490 /* That didn't work, now try treating the list as a sequence of
28491 arguments. */
28492 release_tree_vector (args);
28493 args = make_tree_vector_from_ctor (init);
28494 }
28495 }
28496
28497 if (elided && !cands)
28498 {
28499 error ("cannot deduce template arguments for copy-initialization"
28500 " of %qT, as it has no non-explicit deduction guides or "
28501 "user-declared constructors", type);
28502 return error_mark_node;
28503 }
28504 else if (!cands && call == error_mark_node)
28505 {
28506 error ("cannot deduce template arguments of %qT, as it has no viable "
28507 "deduction guides", type);
28508 return error_mark_node;
28509 }
28510
28511 if (call == error_mark_node)
28512 {
28513 ++cp_unevaluated_operand;
28514 call = build_new_function_call (cands, &args, tf_decltype);
28515 --cp_unevaluated_operand;
28516 }
28517
28518 if (call == error_mark_node
28519 && (complain & tf_warning_or_error))
28520 {
28521 error ("class template argument deduction failed:");
28522
28523 ++cp_unevaluated_operand;
28524 call = build_new_function_call (cands, &args, complain | tf_decltype);
28525 --cp_unevaluated_operand;
28526
28527 if (elided)
28528 inform (input_location, "explicit deduction guides not considered "
28529 "for copy-initialization");
28530 }
28531
28532 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28533 }
28534
28535 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28536 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28537 The CONTEXT determines the context in which auto deduction is performed
28538 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28539 OUTER_TARGS are used during template argument deduction
28540 (context == adc_unify) to properly substitute the result, and is ignored
28541 in other contexts.
28542
28543 For partial-concept-ids, extra args may be appended to the list of deduced
28544 template arguments prior to determining constraint satisfaction. */
28545
28546 tree
28547 do_auto_deduction (tree type, tree init, tree auto_node,
28548 tsubst_flags_t complain, auto_deduction_context context,
28549 tree outer_targs, int flags)
28550 {
28551 tree targs;
28552
28553 if (init == error_mark_node)
28554 return error_mark_node;
28555
28556 if (init && type_dependent_expression_p (init)
28557 && context != adc_unify)
28558 /* Defining a subset of type-dependent expressions that we can deduce
28559 from ahead of time isn't worth the trouble. */
28560 return type;
28561
28562 /* Similarly, we can't deduce from another undeduced decl. */
28563 if (init && undeduced_auto_decl (init))
28564 return type;
28565
28566 /* We may be doing a partial substitution, but we still want to replace
28567 auto_node. */
28568 complain &= ~tf_partial;
28569
28570 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28571 /* C++17 class template argument deduction. */
28572 return do_class_deduction (type, tmpl, init, flags, complain);
28573
28574 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28575 /* Nothing we can do with this, even in deduction context. */
28576 return type;
28577
28578 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28579 with either a new invented type template parameter U or, if the
28580 initializer is a braced-init-list (8.5.4), with
28581 std::initializer_list<U>. */
28582 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28583 {
28584 if (!DIRECT_LIST_INIT_P (init))
28585 type = listify_autos (type, auto_node);
28586 else if (CONSTRUCTOR_NELTS (init) == 1)
28587 init = CONSTRUCTOR_ELT (init, 0)->value;
28588 else
28589 {
28590 if (complain & tf_warning_or_error)
28591 {
28592 if (permerror (input_location, "direct-list-initialization of "
28593 "%<auto%> requires exactly one element"))
28594 inform (input_location,
28595 "for deduction to %<std::initializer_list%>, use copy-"
28596 "list-initialization (i.e. add %<=%> before the %<{%>)");
28597 }
28598 type = listify_autos (type, auto_node);
28599 }
28600 }
28601
28602 if (type == error_mark_node)
28603 return error_mark_node;
28604
28605 init = resolve_nondeduced_context (init, complain);
28606
28607 if (context == adc_decomp_type
28608 && auto_node == type
28609 && init != error_mark_node
28610 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28611 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28612 and initializer has array type, deduce cv-qualified array type. */
28613 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28614 complain);
28615 else if (AUTO_IS_DECLTYPE (auto_node))
28616 {
28617 tree stripped_init = tree_strip_any_location_wrapper (init);
28618 bool id = (DECL_P (stripped_init)
28619 || ((TREE_CODE (init) == COMPONENT_REF
28620 || TREE_CODE (init) == SCOPE_REF)
28621 && !REF_PARENTHESIZED_P (init)));
28622 targs = make_tree_vec (1);
28623 TREE_VEC_ELT (targs, 0)
28624 = finish_decltype_type (init, id, tf_warning_or_error);
28625 if (type != auto_node)
28626 {
28627 if (complain & tf_error)
28628 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28629 return error_mark_node;
28630 }
28631 }
28632 else
28633 {
28634 if (error_operand_p (init))
28635 return error_mark_node;
28636
28637 tree parms = build_tree_list (NULL_TREE, type);
28638 tree tparms;
28639
28640 if (flag_concepts)
28641 tparms = extract_autos (type);
28642 else
28643 {
28644 tparms = make_tree_vec (1);
28645 TREE_VEC_ELT (tparms, 0)
28646 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28647 }
28648
28649 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28650 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28651 DEDUCE_CALL,
28652 NULL, /*explain_p=*/false);
28653 if (val > 0)
28654 {
28655 if (processing_template_decl)
28656 /* Try again at instantiation time. */
28657 return type;
28658 if (type && type != error_mark_node
28659 && (complain & tf_error))
28660 /* If type is error_mark_node a diagnostic must have been
28661 emitted by now. Also, having a mention to '<type error>'
28662 in the diagnostic is not really useful to the user. */
28663 {
28664 if (cfun
28665 && FNDECL_USED_AUTO (current_function_decl)
28666 && (auto_node
28667 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28668 && LAMBDA_FUNCTION_P (current_function_decl))
28669 error ("unable to deduce lambda return type from %qE", init);
28670 else
28671 error ("unable to deduce %qT from %qE", type, init);
28672 type_unification_real (tparms, targs, parms, &init, 1, 0,
28673 DEDUCE_CALL,
28674 NULL, /*explain_p=*/true);
28675 }
28676 return error_mark_node;
28677 }
28678 }
28679
28680 /* Check any placeholder constraints against the deduced type. */
28681 if (flag_concepts && !processing_template_decl)
28682 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28683 {
28684 /* Use the deduced type to check the associated constraints. If we
28685 have a partial-concept-id, rebuild the argument list so that
28686 we check using the extra arguments. */
28687 check = unpack_concept_check (check);
28688 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28689 tree cdecl = TREE_OPERAND (check, 0);
28690 if (OVL_P (cdecl))
28691 cdecl = OVL_FIRST (cdecl);
28692 tree cargs = TREE_OPERAND (check, 1);
28693 if (TREE_VEC_LENGTH (cargs) > 1)
28694 {
28695 cargs = copy_node (cargs);
28696 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28697 }
28698 else
28699 cargs = targs;
28700
28701 /* Rebuild the check using the deduced arguments. */
28702 check = build_concept_check (cdecl, cargs, tf_none);
28703
28704 if (!constraints_satisfied_p (check))
28705 {
28706 if (complain & tf_warning_or_error)
28707 {
28708 auto_diagnostic_group d;
28709 switch (context)
28710 {
28711 case adc_unspecified:
28712 case adc_unify:
28713 error("placeholder constraints not satisfied");
28714 break;
28715 case adc_variable_type:
28716 case adc_decomp_type:
28717 error ("deduced initializer does not satisfy "
28718 "placeholder constraints");
28719 break;
28720 case adc_return_type:
28721 error ("deduced return type does not satisfy "
28722 "placeholder constraints");
28723 break;
28724 case adc_requirement:
28725 error ("deduced expression type does not satisfy "
28726 "placeholder constraints");
28727 break;
28728 }
28729 diagnose_constraints (input_location, check, targs);
28730 }
28731 return error_mark_node;
28732 }
28733 }
28734
28735 if (processing_template_decl && context != adc_unify)
28736 outer_targs = current_template_args ();
28737 targs = add_to_template_args (outer_targs, targs);
28738 return tsubst (type, targs, complain, NULL_TREE);
28739 }
28740
28741 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28742 result. */
28743
28744 tree
28745 splice_late_return_type (tree type, tree late_return_type)
28746 {
28747 if (is_auto (type))
28748 {
28749 if (late_return_type)
28750 return late_return_type;
28751
28752 tree idx = get_template_parm_index (type);
28753 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28754 /* In an abbreviated function template we didn't know we were dealing
28755 with a function template when we saw the auto return type, so update
28756 it to have the correct level. */
28757 return make_auto_1 (TYPE_IDENTIFIER (type), true);
28758 }
28759 return type;
28760 }
28761
28762 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28763 'decltype(auto)' or a deduced class template. */
28764
28765 bool
28766 is_auto (const_tree type)
28767 {
28768 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28769 && (TYPE_IDENTIFIER (type) == auto_identifier
28770 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28771 return true;
28772 else
28773 return false;
28774 }
28775
28776 /* for_each_template_parm callback for type_uses_auto. */
28777
28778 int
28779 is_auto_r (tree tp, void */*data*/)
28780 {
28781 return is_auto (tp);
28782 }
28783
28784 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28785 a use of `auto'. Returns NULL_TREE otherwise. */
28786
28787 tree
28788 type_uses_auto (tree type)
28789 {
28790 if (type == NULL_TREE)
28791 return NULL_TREE;
28792 else if (flag_concepts)
28793 {
28794 /* The Concepts TS allows multiple autos in one type-specifier; just
28795 return the first one we find, do_auto_deduction will collect all of
28796 them. */
28797 if (uses_template_parms (type))
28798 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28799 /*visited*/NULL, /*nondeduced*/false);
28800 else
28801 return NULL_TREE;
28802 }
28803 else
28804 return find_type_usage (type, is_auto);
28805 }
28806
28807 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28808 concepts are enabled, auto is acceptable in template arguments, but
28809 only when TEMPL identifies a template class. Return TRUE if any
28810 such errors were reported. */
28811
28812 bool
28813 check_auto_in_tmpl_args (tree tmpl, tree args)
28814 {
28815 /* If there were previous errors, nevermind. */
28816 if (!args || TREE_CODE (args) != TREE_VEC)
28817 return false;
28818
28819 /* If TMPL is an identifier, we're parsing and we can't tell yet
28820 whether TMPL is supposed to be a type, a function or a variable.
28821 We'll only be able to tell during template substitution, so we
28822 expect to be called again then. If concepts are enabled and we
28823 know we have a type, we're ok. */
28824 if (flag_concepts
28825 && (identifier_p (tmpl)
28826 || (DECL_P (tmpl)
28827 && (DECL_TYPE_TEMPLATE_P (tmpl)
28828 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
28829 return false;
28830
28831 /* Quickly search for any occurrences of auto; usually there won't
28832 be any, and then we'll avoid allocating the vector. */
28833 if (!type_uses_auto (args))
28834 return false;
28835
28836 bool errors = false;
28837
28838 tree vec = extract_autos (args);
28839 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
28840 {
28841 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
28842 error_at (DECL_SOURCE_LOCATION (xauto),
28843 "invalid use of %qT in template argument", xauto);
28844 errors = true;
28845 }
28846
28847 return errors;
28848 }
28849
28850 /* For a given template T, return the vector of typedefs referenced
28851 in T for which access check is needed at T instantiation time.
28852 T is either a FUNCTION_DECL or a RECORD_TYPE.
28853 Those typedefs were added to T by the function
28854 append_type_to_template_for_access_check. */
28855
28856 vec<qualified_typedef_usage_t, va_gc> *
28857 get_types_needing_access_check (tree t)
28858 {
28859 tree ti;
28860 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
28861
28862 if (!t || t == error_mark_node)
28863 return NULL;
28864
28865 if (!(ti = get_template_info (t)))
28866 return NULL;
28867
28868 if (CLASS_TYPE_P (t)
28869 || TREE_CODE (t) == FUNCTION_DECL)
28870 {
28871 if (!TI_TEMPLATE (ti))
28872 return NULL;
28873
28874 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
28875 }
28876
28877 return result;
28878 }
28879
28880 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28881 tied to T. That list of typedefs will be access checked at
28882 T instantiation time.
28883 T is either a FUNCTION_DECL or a RECORD_TYPE.
28884 TYPE_DECL is a TYPE_DECL node representing a typedef.
28885 SCOPE is the scope through which TYPE_DECL is accessed.
28886 LOCATION is the location of the usage point of TYPE_DECL.
28887
28888 This function is a subroutine of
28889 append_type_to_template_for_access_check. */
28890
28891 static void
28892 append_type_to_template_for_access_check_1 (tree t,
28893 tree type_decl,
28894 tree scope,
28895 location_t location)
28896 {
28897 qualified_typedef_usage_t typedef_usage;
28898 tree ti;
28899
28900 if (!t || t == error_mark_node)
28901 return;
28902
28903 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
28904 || CLASS_TYPE_P (t))
28905 && type_decl
28906 && TREE_CODE (type_decl) == TYPE_DECL
28907 && scope);
28908
28909 if (!(ti = get_template_info (t)))
28910 return;
28911
28912 gcc_assert (TI_TEMPLATE (ti));
28913
28914 typedef_usage.typedef_decl = type_decl;
28915 typedef_usage.context = scope;
28916 typedef_usage.locus = location;
28917
28918 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
28919 }
28920
28921 /* Append TYPE_DECL to the template TEMPL.
28922 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28923 At TEMPL instanciation time, TYPE_DECL will be checked to see
28924 if it can be accessed through SCOPE.
28925 LOCATION is the location of the usage point of TYPE_DECL.
28926
28927 e.g. consider the following code snippet:
28928
28929 class C
28930 {
28931 typedef int myint;
28932 };
28933
28934 template<class U> struct S
28935 {
28936 C::myint mi; // <-- usage point of the typedef C::myint
28937 };
28938
28939 S<char> s;
28940
28941 At S<char> instantiation time, we need to check the access of C::myint
28942 In other words, we need to check the access of the myint typedef through
28943 the C scope. For that purpose, this function will add the myint typedef
28944 and the scope C through which its being accessed to a list of typedefs
28945 tied to the template S. That list will be walked at template instantiation
28946 time and access check performed on each typedefs it contains.
28947 Note that this particular code snippet should yield an error because
28948 myint is private to C. */
28949
28950 void
28951 append_type_to_template_for_access_check (tree templ,
28952 tree type_decl,
28953 tree scope,
28954 location_t location)
28955 {
28956 qualified_typedef_usage_t *iter;
28957 unsigned i;
28958
28959 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
28960
28961 /* Make sure we don't append the type to the template twice. */
28962 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
28963 if (iter->typedef_decl == type_decl && scope == iter->context)
28964 return;
28965
28966 append_type_to_template_for_access_check_1 (templ, type_decl,
28967 scope, location);
28968 }
28969
28970 /* Recursively walk over && expressions searching for EXPR. Return a reference
28971 to that expression. */
28972
28973 static tree *find_template_requirement (tree *t, tree key)
28974 {
28975 if (*t == key)
28976 return t;
28977 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
28978 {
28979 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
28980 return p;
28981 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
28982 return p;
28983 }
28984 return 0;
28985 }
28986
28987 /* Convert the generic type parameters in PARM that match the types given in the
28988 range [START_IDX, END_IDX) from the current_template_parms into generic type
28989 packs. */
28990
28991 tree
28992 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
28993 {
28994 tree current = current_template_parms;
28995 int depth = TMPL_PARMS_DEPTH (current);
28996 current = INNERMOST_TEMPLATE_PARMS (current);
28997 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
28998
28999 for (int i = 0; i < start_idx; ++i)
29000 TREE_VEC_ELT (replacement, i)
29001 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29002
29003 for (int i = start_idx; i < end_idx; ++i)
29004 {
29005 /* Create a distinct parameter pack type from the current parm and add it
29006 to the replacement args to tsubst below into the generic function
29007 parameter. */
29008 tree node = TREE_VEC_ELT (current, i);
29009 tree o = TREE_TYPE (TREE_VALUE (node));
29010 tree t = copy_type (o);
29011 TEMPLATE_TYPE_PARM_INDEX (t)
29012 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29013 t, 0, 0, tf_none);
29014 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29015 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29016 TYPE_MAIN_VARIANT (t) = t;
29017 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29018 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29019 TREE_VEC_ELT (replacement, i) = t;
29020
29021 /* Replace the current template parameter with new pack. */
29022 TREE_VALUE (node) = TREE_CHAIN (t);
29023
29024 /* Surgically adjust the associated constraint of adjusted parameter
29025 and it's corresponding contribution to the current template
29026 requirements. */
29027 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29028 {
29029 tree id = unpack_concept_check (constr);
29030 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
29031 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29032 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29033
29034 /* If there was a constraint, we also need to replace that in
29035 the template requirements, which we've already built. */
29036 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29037 reqs = find_template_requirement (reqs, constr);
29038 *reqs = fold;
29039 }
29040 }
29041
29042 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29043 TREE_VEC_ELT (replacement, i)
29044 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29045
29046 /* If there are more levels then build up the replacement with the outer
29047 template parms. */
29048 if (depth > 1)
29049 replacement = add_to_template_args (template_parms_to_args
29050 (TREE_CHAIN (current_template_parms)),
29051 replacement);
29052
29053 return tsubst (parm, replacement, tf_none, NULL_TREE);
29054 }
29055
29056 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29057 0..N-1. */
29058
29059 void
29060 declare_integer_pack (void)
29061 {
29062 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29063 build_function_type_list (integer_type_node,
29064 integer_type_node,
29065 NULL_TREE),
29066 NULL_TREE, ECF_CONST);
29067 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29068 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29069 CP_BUILT_IN_INTEGER_PACK);
29070 }
29071
29072 /* Set up the hash tables for template instantiations. */
29073
29074 void
29075 init_template_processing (void)
29076 {
29077 /* FIXME: enable sanitization (PR87847) */
29078 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29079 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
29080
29081 if (cxx_dialect >= cxx11)
29082 declare_integer_pack ();
29083 }
29084
29085 /* Print stats about the template hash tables for -fstats. */
29086
29087 void
29088 print_template_statistics (void)
29089 {
29090 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
29091 "%f collisions\n", (long) decl_specializations->size (),
29092 (long) decl_specializations->elements (),
29093 decl_specializations->collisions ());
29094 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
29095 "%f collisions\n", (long) type_specializations->size (),
29096 (long) type_specializations->elements (),
29097 type_specializations->collisions ());
29098 }
29099
29100 #if CHECKING_P
29101
29102 namespace selftest {
29103
29104 /* Verify that build_non_dependent_expr () works, for various expressions,
29105 and that location wrappers don't affect the results. */
29106
29107 static void
29108 test_build_non_dependent_expr ()
29109 {
29110 location_t loc = BUILTINS_LOCATION;
29111
29112 /* Verify constants, without and with location wrappers. */
29113 tree int_cst = build_int_cst (integer_type_node, 42);
29114 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
29115
29116 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
29117 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
29118 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
29119
29120 tree string_lit = build_string (4, "foo");
29121 TREE_TYPE (string_lit) = char_array_type_node;
29122 string_lit = fix_string_type (string_lit);
29123 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
29124
29125 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
29126 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
29127 ASSERT_EQ (wrapped_string_lit,
29128 build_non_dependent_expr (wrapped_string_lit));
29129 }
29130
29131 /* Verify that type_dependent_expression_p () works correctly, even
29132 in the presence of location wrapper nodes. */
29133
29134 static void
29135 test_type_dependent_expression_p ()
29136 {
29137 location_t loc = BUILTINS_LOCATION;
29138
29139 tree name = get_identifier ("foo");
29140
29141 /* If no templates are involved, nothing is type-dependent. */
29142 gcc_assert (!processing_template_decl);
29143 ASSERT_FALSE (type_dependent_expression_p (name));
29144
29145 ++processing_template_decl;
29146
29147 /* Within a template, an unresolved name is always type-dependent. */
29148 ASSERT_TRUE (type_dependent_expression_p (name));
29149
29150 /* Ensure it copes with NULL_TREE and errors. */
29151 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
29152 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
29153
29154 /* A USING_DECL in a template should be type-dependent, even if wrapped
29155 with a location wrapper (PR c++/83799). */
29156 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
29157 TREE_TYPE (using_decl) = integer_type_node;
29158 ASSERT_TRUE (type_dependent_expression_p (using_decl));
29159 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
29160 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
29161 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
29162
29163 --processing_template_decl;
29164 }
29165
29166 /* Run all of the selftests within this file. */
29167
29168 void
29169 cp_pt_c_tests ()
29170 {
29171 test_build_non_dependent_expr ();
29172 test_type_dependent_expression_p ();
29173 }
29174
29175 } // namespace selftest
29176
29177 #endif /* #if CHECKING_P */
29178
29179 #include "gt-cp-pt.h"