]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Suppress diagnostics substituting into a requires-expression (PR c++/92403).
[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 tsubst_attributes (tree, tree, tsubst_flags_t, tree);
219 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
220 static tree make_argument_pack (tree);
221 static void register_parameter_specializations (tree, tree);
222 static tree enclosing_instantiation_of (tree tctx);
223
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 alias template (needed by instantiate_decl). */
228
229 void
230 push_access_scope (tree t)
231 {
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
233 || TREE_CODE (t) == TYPE_DECL);
234
235 if (DECL_FRIEND_CONTEXT (t))
236 push_nested_class (DECL_FRIEND_CONTEXT (t));
237 else if (DECL_CLASS_SCOPE_P (t))
238 push_nested_class (DECL_CONTEXT (t));
239 else
240 push_to_top_level ();
241
242 if (TREE_CODE (t) == FUNCTION_DECL)
243 {
244 vec_safe_push (saved_access_scope, current_function_decl);
245 current_function_decl = t;
246 }
247 }
248
249 /* Restore the scope set up by push_access_scope. T is the node we
250 are processing. */
251
252 void
253 pop_access_scope (tree t)
254 {
255 if (TREE_CODE (t) == FUNCTION_DECL)
256 current_function_decl = saved_access_scope->pop();
257
258 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
259 pop_nested_class ();
260 else
261 pop_from_top_level ();
262 }
263
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
268
269 tree
270 finish_member_template_decl (tree decl)
271 {
272 if (decl == error_mark_node)
273 return error_mark_node;
274
275 gcc_assert (DECL_P (decl));
276
277 if (TREE_CODE (decl) == TYPE_DECL)
278 {
279 tree type;
280
281 type = TREE_TYPE (decl);
282 if (type == error_mark_node)
283 return error_mark_node;
284 if (MAYBE_CLASS_TYPE_P (type)
285 && CLASSTYPE_TEMPLATE_INFO (type)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
287 {
288 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
289 check_member_template (tmpl);
290 return tmpl;
291 }
292 return NULL_TREE;
293 }
294 else if (TREE_CODE (decl) == FIELD_DECL)
295 error_at (DECL_SOURCE_LOCATION (decl),
296 "data member %qD cannot be a member template", decl);
297 else if (DECL_TEMPLATE_INFO (decl))
298 {
299 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
300 {
301 check_member_template (DECL_TI_TEMPLATE (decl));
302 return DECL_TI_TEMPLATE (decl);
303 }
304 else
305 return decl;
306 }
307 else
308 error_at (DECL_SOURCE_LOCATION (decl),
309 "invalid member template declaration %qD", decl);
310
311 return error_mark_node;
312 }
313
314 /* Create a template info node. */
315
316 tree
317 build_template_info (tree template_decl, tree template_args)
318 {
319 tree result = make_node (TEMPLATE_INFO);
320 TI_TEMPLATE (result) = template_decl;
321 TI_ARGS (result) = template_args;
322 return result;
323 }
324
325 /* Return the template info node corresponding to T, whatever T is. */
326
327 tree
328 get_template_info (const_tree t)
329 {
330 tree tinfo = NULL_TREE;
331
332 if (!t || t == error_mark_node)
333 return NULL;
334
335 if (TREE_CODE (t) == NAMESPACE_DECL
336 || TREE_CODE (t) == PARM_DECL)
337 return NULL;
338
339 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
340 tinfo = DECL_TEMPLATE_INFO (t);
341
342 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
343 t = TREE_TYPE (t);
344
345 if (OVERLOAD_TYPE_P (t))
346 tinfo = TYPE_TEMPLATE_INFO (t);
347 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
348 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
349
350 return tinfo;
351 }
352
353 /* Returns the template nesting level of the indicated class TYPE.
354
355 For example, in:
356 template <class T>
357 struct A
358 {
359 template <class U>
360 struct B {};
361 };
362
363 A<T>::B<U> has depth two, while A<T> has depth one.
364 Both A<T>::B<int> and A<int>::B<U> have depth one, if
365 they are instantiations, not specializations.
366
367 This function is guaranteed to return 0 if passed NULL_TREE so
368 that, for example, `template_class_depth (current_class_type)' is
369 always safe. */
370
371 int
372 template_class_depth (tree type)
373 {
374 int depth;
375
376 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
377 {
378 tree tinfo = get_template_info (type);
379
380 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
381 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
382 ++depth;
383
384 if (DECL_P (type))
385 type = CP_DECL_CONTEXT (type);
386 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
387 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
388 else
389 type = CP_TYPE_CONTEXT (type);
390 }
391
392 return depth;
393 }
394
395 /* Return TRUE if NODE instantiates a template that has arguments of
396 its own, be it directly a primary template or indirectly through a
397 partial specializations. */
398 static bool
399 instantiates_primary_template_p (tree node)
400 {
401 tree tinfo = get_template_info (node);
402 if (!tinfo)
403 return false;
404
405 tree tmpl = TI_TEMPLATE (tinfo);
406 if (PRIMARY_TEMPLATE_P (tmpl))
407 return true;
408
409 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
410 return false;
411
412 /* So now we know we have a specialization, but it could be a full
413 or a partial specialization. To tell which, compare the depth of
414 its template arguments with those of its context. */
415
416 tree ctxt = DECL_CONTEXT (tmpl);
417 tree ctinfo = get_template_info (ctxt);
418 if (!ctinfo)
419 return true;
420
421 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
422 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
423 }
424
425 /* Subroutine of maybe_begin_member_template_processing.
426 Returns true if processing DECL needs us to push template parms. */
427
428 static bool
429 inline_needs_template_parms (tree decl, bool nsdmi)
430 {
431 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
432 return false;
433
434 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
435 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
436 }
437
438 /* Subroutine of maybe_begin_member_template_processing.
439 Push the template parms in PARMS, starting from LEVELS steps into the
440 chain, and ending at the beginning, since template parms are listed
441 innermost first. */
442
443 static void
444 push_inline_template_parms_recursive (tree parmlist, int levels)
445 {
446 tree parms = TREE_VALUE (parmlist);
447 int i;
448
449 if (levels > 1)
450 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
451
452 ++processing_template_decl;
453 current_template_parms
454 = tree_cons (size_int (processing_template_decl),
455 parms, current_template_parms);
456 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
457
458 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
459 NULL);
460 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
461 {
462 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
463
464 if (error_operand_p (parm))
465 continue;
466
467 gcc_assert (DECL_P (parm));
468
469 switch (TREE_CODE (parm))
470 {
471 case TYPE_DECL:
472 case TEMPLATE_DECL:
473 pushdecl (parm);
474 break;
475
476 case PARM_DECL:
477 /* Push the CONST_DECL. */
478 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
479 break;
480
481 default:
482 gcc_unreachable ();
483 }
484 }
485 }
486
487 /* Restore the template parameter context for a member template, a
488 friend template defined in a class definition, or a non-template
489 member of template class. */
490
491 void
492 maybe_begin_member_template_processing (tree decl)
493 {
494 tree parms;
495 int levels = 0;
496 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
497
498 if (nsdmi)
499 {
500 tree ctx = DECL_CONTEXT (decl);
501 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
502 /* Disregard full specializations (c++/60999). */
503 && uses_template_parms (ctx)
504 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
505 }
506
507 if (inline_needs_template_parms (decl, nsdmi))
508 {
509 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
510 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
511
512 if (DECL_TEMPLATE_SPECIALIZATION (decl))
513 {
514 --levels;
515 parms = TREE_CHAIN (parms);
516 }
517
518 push_inline_template_parms_recursive (parms, levels);
519 }
520
521 /* Remember how many levels of template parameters we pushed so that
522 we can pop them later. */
523 inline_parm_levels.safe_push (levels);
524 }
525
526 /* Undo the effects of maybe_begin_member_template_processing. */
527
528 void
529 maybe_end_member_template_processing (void)
530 {
531 int i;
532 int last;
533
534 if (inline_parm_levels.length () == 0)
535 return;
536
537 last = inline_parm_levels.pop ();
538 for (i = 0; i < last; ++i)
539 {
540 --processing_template_decl;
541 current_template_parms = TREE_CHAIN (current_template_parms);
542 poplevel (0, 0, 0);
543 }
544 }
545
546 /* Return a new template argument vector which contains all of ARGS,
547 but has as its innermost set of arguments the EXTRA_ARGS. */
548
549 static tree
550 add_to_template_args (tree args, tree extra_args)
551 {
552 tree new_args;
553 int extra_depth;
554 int i;
555 int j;
556
557 if (args == NULL_TREE || extra_args == error_mark_node)
558 return extra_args;
559
560 extra_depth = TMPL_ARGS_DEPTH (extra_args);
561 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
562
563 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
564 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
565
566 for (j = 1; j <= extra_depth; ++j, ++i)
567 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
568
569 return new_args;
570 }
571
572 /* Like add_to_template_args, but only the outermost ARGS are added to
573 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
574 (EXTRA_ARGS) levels are added. This function is used to combine
575 the template arguments from a partial instantiation with the
576 template arguments used to attain the full instantiation from the
577 partial instantiation. */
578
579 tree
580 add_outermost_template_args (tree args, tree extra_args)
581 {
582 tree new_args;
583
584 /* If there are more levels of EXTRA_ARGS than there are ARGS,
585 something very fishy is going on. */
586 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
587
588 /* If *all* the new arguments will be the EXTRA_ARGS, just return
589 them. */
590 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
591 return extra_args;
592
593 /* For the moment, we make ARGS look like it contains fewer levels. */
594 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
595
596 new_args = add_to_template_args (args, extra_args);
597
598 /* Now, we restore ARGS to its full dimensions. */
599 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
600
601 return new_args;
602 }
603
604 /* Return the N levels of innermost template arguments from the ARGS. */
605
606 tree
607 get_innermost_template_args (tree args, int n)
608 {
609 tree new_args;
610 int extra_levels;
611 int i;
612
613 gcc_assert (n >= 0);
614
615 /* If N is 1, just return the innermost set of template arguments. */
616 if (n == 1)
617 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
618
619 /* If we're not removing anything, just return the arguments we were
620 given. */
621 extra_levels = TMPL_ARGS_DEPTH (args) - n;
622 gcc_assert (extra_levels >= 0);
623 if (extra_levels == 0)
624 return args;
625
626 /* Make a new set of arguments, not containing the outer arguments. */
627 new_args = make_tree_vec (n);
628 for (i = 1; i <= n; ++i)
629 SET_TMPL_ARGS_LEVEL (new_args, i,
630 TMPL_ARGS_LEVEL (args, i + extra_levels));
631
632 return new_args;
633 }
634
635 /* The inverse of get_innermost_template_args: Return all but the innermost
636 EXTRA_LEVELS levels of template arguments from the ARGS. */
637
638 static tree
639 strip_innermost_template_args (tree args, int extra_levels)
640 {
641 tree new_args;
642 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
643 int i;
644
645 gcc_assert (n >= 0);
646
647 /* If N is 1, just return the outermost set of template arguments. */
648 if (n == 1)
649 return TMPL_ARGS_LEVEL (args, 1);
650
651 /* If we're not removing anything, just return the arguments we were
652 given. */
653 gcc_assert (extra_levels >= 0);
654 if (extra_levels == 0)
655 return args;
656
657 /* Make a new set of arguments, not containing the inner arguments. */
658 new_args = make_tree_vec (n);
659 for (i = 1; i <= n; ++i)
660 SET_TMPL_ARGS_LEVEL (new_args, i,
661 TMPL_ARGS_LEVEL (args, i));
662
663 return new_args;
664 }
665
666 /* We've got a template header coming up; push to a new level for storing
667 the parms. */
668
669 void
670 begin_template_parm_list (void)
671 {
672 /* We use a non-tag-transparent scope here, which causes pushtag to
673 put tags in this scope, rather than in the enclosing class or
674 namespace scope. This is the right thing, since we want
675 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
676 global template class, push_template_decl handles putting the
677 TEMPLATE_DECL into top-level scope. For a nested template class,
678 e.g.:
679
680 template <class T> struct S1 {
681 template <class T> struct S2 {};
682 };
683
684 pushtag contains special code to insert the TEMPLATE_DECL for S2
685 at the right scope. */
686 begin_scope (sk_template_parms, NULL);
687 ++processing_template_decl;
688 ++processing_template_parmlist;
689 note_template_header (0);
690
691 /* Add a dummy parameter level while we process the parameter list. */
692 current_template_parms
693 = tree_cons (size_int (processing_template_decl),
694 make_tree_vec (0),
695 current_template_parms);
696 }
697
698 /* This routine is called when a specialization is declared. If it is
699 invalid to declare a specialization here, an error is reported and
700 false is returned, otherwise this routine will return true. */
701
702 static bool
703 check_specialization_scope (void)
704 {
705 tree scope = current_scope ();
706
707 /* [temp.expl.spec]
708
709 An explicit specialization shall be declared in the namespace of
710 which the template is a member, or, for member templates, in the
711 namespace of which the enclosing class or enclosing class
712 template is a member. An explicit specialization of a member
713 function, member class or static data member of a class template
714 shall be declared in the namespace of which the class template
715 is a member. */
716 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
717 {
718 error ("explicit specialization in non-namespace scope %qD", scope);
719 return false;
720 }
721
722 /* [temp.expl.spec]
723
724 In an explicit specialization declaration for a member of a class
725 template or a member template that appears in namespace scope,
726 the member template and some of its enclosing class templates may
727 remain unspecialized, except that the declaration shall not
728 explicitly specialize a class member template if its enclosing
729 class templates are not explicitly specialized as well. */
730 if (current_template_parms)
731 {
732 error ("enclosing class templates are not explicitly specialized");
733 return false;
734 }
735
736 return true;
737 }
738
739 /* We've just seen template <>. */
740
741 bool
742 begin_specialization (void)
743 {
744 begin_scope (sk_template_spec, NULL);
745 note_template_header (1);
746 return check_specialization_scope ();
747 }
748
749 /* Called at then end of processing a declaration preceded by
750 template<>. */
751
752 void
753 end_specialization (void)
754 {
755 finish_scope ();
756 reset_specialization ();
757 }
758
759 /* Any template <>'s that we have seen thus far are not referring to a
760 function specialization. */
761
762 void
763 reset_specialization (void)
764 {
765 processing_specialization = 0;
766 template_header_count = 0;
767 }
768
769 /* We've just seen a template header. If SPECIALIZATION is nonzero,
770 it was of the form template <>. */
771
772 static void
773 note_template_header (int specialization)
774 {
775 processing_specialization = specialization;
776 template_header_count++;
777 }
778
779 /* We're beginning an explicit instantiation. */
780
781 void
782 begin_explicit_instantiation (void)
783 {
784 gcc_assert (!processing_explicit_instantiation);
785 processing_explicit_instantiation = true;
786 }
787
788
789 void
790 end_explicit_instantiation (void)
791 {
792 gcc_assert (processing_explicit_instantiation);
793 processing_explicit_instantiation = false;
794 }
795
796 /* An explicit specialization or partial specialization of TMPL is being
797 declared. Check that the namespace in which the specialization is
798 occurring is permissible. Returns false iff it is invalid to
799 specialize TMPL in the current namespace. */
800
801 static bool
802 check_specialization_namespace (tree tmpl)
803 {
804 tree tpl_ns = decl_namespace_context (tmpl);
805
806 /* [tmpl.expl.spec]
807
808 An explicit specialization shall be declared in a namespace enclosing the
809 specialized template. An explicit specialization whose declarator-id is
810 not qualified shall be declared in the nearest enclosing namespace of the
811 template, or, if the namespace is inline (7.3.1), any namespace from its
812 enclosing namespace set. */
813 if (current_scope() != DECL_CONTEXT (tmpl)
814 && !at_namespace_scope_p ())
815 {
816 error ("specialization of %qD must appear at namespace scope", tmpl);
817 return false;
818 }
819
820 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
821 /* Same or enclosing namespace. */
822 return true;
823 else
824 {
825 auto_diagnostic_group d;
826 if (permerror (input_location,
827 "specialization of %qD in different namespace", tmpl))
828 inform (DECL_SOURCE_LOCATION (tmpl),
829 " from definition of %q#D", tmpl);
830 return false;
831 }
832 }
833
834 /* SPEC is an explicit instantiation. Check that it is valid to
835 perform this explicit instantiation in the current namespace. */
836
837 static void
838 check_explicit_instantiation_namespace (tree spec)
839 {
840 tree ns;
841
842 /* DR 275: An explicit instantiation shall appear in an enclosing
843 namespace of its template. */
844 ns = decl_namespace_context (spec);
845 if (!is_nested_namespace (current_namespace, ns))
846 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
847 "(which does not enclose namespace %qD)",
848 spec, current_namespace, ns);
849 }
850
851 // Returns the type of a template specialization only if that
852 // specialization needs to be defined. Otherwise (e.g., if the type has
853 // already been defined), the function returns NULL_TREE.
854 static tree
855 maybe_new_partial_specialization (tree type)
856 {
857 // An implicit instantiation of an incomplete type implies
858 // the definition of a new class template.
859 //
860 // template<typename T>
861 // struct S;
862 //
863 // template<typename T>
864 // struct S<T*>;
865 //
866 // Here, S<T*> is an implicit instantiation of S whose type
867 // is incomplete.
868 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
869 return type;
870
871 // It can also be the case that TYPE is a completed specialization.
872 // Continuing the previous example, suppose we also declare:
873 //
874 // template<typename T>
875 // requires Integral<T>
876 // struct S<T*>;
877 //
878 // Here, S<T*> refers to the specialization S<T*> defined
879 // above. However, we need to differentiate definitions because
880 // we intend to define a new partial specialization. In this case,
881 // we rely on the fact that the constraints are different for
882 // this declaration than that above.
883 //
884 // Note that we also get here for injected class names and
885 // late-parsed template definitions. We must ensure that we
886 // do not create new type declarations for those cases.
887 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
888 {
889 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
890 tree args = CLASSTYPE_TI_ARGS (type);
891
892 // If there are no template parameters, this cannot be a new
893 // partial template specializtion?
894 if (!current_template_parms)
895 return NULL_TREE;
896
897 // The injected-class-name is not a new partial specialization.
898 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
899 return NULL_TREE;
900
901 // If the constraints are not the same as those of the primary
902 // then, we can probably create a new specialization.
903 tree type_constr = current_template_constraints ();
904
905 if (type == TREE_TYPE (tmpl))
906 {
907 tree main_constr = get_constraints (tmpl);
908 if (equivalent_constraints (type_constr, main_constr))
909 return NULL_TREE;
910 }
911
912 // Also, if there's a pre-existing specialization with matching
913 // constraints, then this also isn't new.
914 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
915 while (specs)
916 {
917 tree spec_tmpl = TREE_VALUE (specs);
918 tree spec_args = TREE_PURPOSE (specs);
919 tree spec_constr = get_constraints (spec_tmpl);
920 if (comp_template_args (args, spec_args)
921 && equivalent_constraints (type_constr, spec_constr))
922 return NULL_TREE;
923 specs = TREE_CHAIN (specs);
924 }
925
926 // Create a new type node (and corresponding type decl)
927 // for the newly declared specialization.
928 tree t = make_class_type (TREE_CODE (type));
929 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
930 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
931
932 /* We only need a separate type node for storing the definition of this
933 partial specialization; uses of S<T*> are unconstrained, so all are
934 equivalent. So keep TYPE_CANONICAL the same. */
935 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
936
937 // Build the corresponding type decl.
938 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
939 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
940 DECL_SOURCE_LOCATION (d) = input_location;
941
942 return t;
943 }
944
945 return NULL_TREE;
946 }
947
948 /* The TYPE is being declared. If it is a template type, that means it
949 is a partial specialization. Do appropriate error-checking. */
950
951 tree
952 maybe_process_partial_specialization (tree type)
953 {
954 tree context;
955
956 if (type == error_mark_node)
957 return error_mark_node;
958
959 /* A lambda that appears in specialization context is not itself a
960 specialization. */
961 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
962 return type;
963
964 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
965 {
966 error ("name of class shadows template template parameter %qD",
967 TYPE_NAME (type));
968 return error_mark_node;
969 }
970
971 context = TYPE_CONTEXT (type);
972
973 if (TYPE_ALIAS_P (type))
974 {
975 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
976
977 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
978 error ("specialization of alias template %qD",
979 TI_TEMPLATE (tinfo));
980 else
981 error ("explicit specialization of non-template %qT", type);
982 return error_mark_node;
983 }
984 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
985 {
986 /* This is for ordinary explicit specialization and partial
987 specialization of a template class such as:
988
989 template <> class C<int>;
990
991 or:
992
993 template <class T> class C<T*>;
994
995 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
996
997 if (tree t = maybe_new_partial_specialization (type))
998 {
999 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1000 && !at_namespace_scope_p ())
1001 return error_mark_node;
1002 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1003 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1004 if (processing_template_decl)
1005 {
1006 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1007 if (decl == error_mark_node)
1008 return error_mark_node;
1009 return TREE_TYPE (decl);
1010 }
1011 }
1012 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1013 error ("specialization of %qT after instantiation", type);
1014 else if (errorcount && !processing_specialization
1015 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1016 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1017 /* Trying to define a specialization either without a template<> header
1018 or in an inappropriate place. We've already given an error, so just
1019 bail now so we don't actually define the specialization. */
1020 return error_mark_node;
1021 }
1022 else if (CLASS_TYPE_P (type)
1023 && !CLASSTYPE_USE_TEMPLATE (type)
1024 && CLASSTYPE_TEMPLATE_INFO (type)
1025 && context && CLASS_TYPE_P (context)
1026 && CLASSTYPE_TEMPLATE_INFO (context))
1027 {
1028 /* This is for an explicit specialization of member class
1029 template according to [temp.expl.spec/18]:
1030
1031 template <> template <class U> class C<int>::D;
1032
1033 The context `C<int>' must be an implicit instantiation.
1034 Otherwise this is just a member class template declared
1035 earlier like:
1036
1037 template <> class C<int> { template <class U> class D; };
1038 template <> template <class U> class C<int>::D;
1039
1040 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1041 while in the second case, `C<int>::D' is a primary template
1042 and `C<T>::D' may not exist. */
1043
1044 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1045 && !COMPLETE_TYPE_P (type))
1046 {
1047 tree t;
1048 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1049
1050 if (current_namespace
1051 != decl_namespace_context (tmpl))
1052 {
1053 if (permerror (input_location,
1054 "specialization of %qD in different namespace",
1055 type))
1056 inform (DECL_SOURCE_LOCATION (tmpl),
1057 "from definition of %q#D", tmpl);
1058 }
1059
1060 /* Check for invalid specialization after instantiation:
1061
1062 template <> template <> class C<int>::D<int>;
1063 template <> template <class U> class C<int>::D; */
1064
1065 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1066 t; t = TREE_CHAIN (t))
1067 {
1068 tree inst = TREE_VALUE (t);
1069 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1070 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1071 {
1072 /* We already have a full specialization of this partial
1073 instantiation, or a full specialization has been
1074 looked up but not instantiated. Reassign it to the
1075 new member specialization template. */
1076 spec_entry elt;
1077 spec_entry *entry;
1078
1079 elt.tmpl = most_general_template (tmpl);
1080 elt.args = CLASSTYPE_TI_ARGS (inst);
1081 elt.spec = inst;
1082
1083 type_specializations->remove_elt (&elt);
1084
1085 elt.tmpl = tmpl;
1086 CLASSTYPE_TI_ARGS (inst)
1087 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1088
1089 spec_entry **slot
1090 = type_specializations->find_slot (&elt, INSERT);
1091 entry = ggc_alloc<spec_entry> ();
1092 *entry = elt;
1093 *slot = entry;
1094 }
1095 else
1096 /* But if we've had an implicit instantiation, that's a
1097 problem ([temp.expl.spec]/6). */
1098 error ("specialization %qT after instantiation %qT",
1099 type, inst);
1100 }
1101
1102 /* Mark TYPE as a specialization. And as a result, we only
1103 have one level of template argument for the innermost
1104 class template. */
1105 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1106 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1107 CLASSTYPE_TI_ARGS (type)
1108 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1109 }
1110 }
1111 else if (processing_specialization)
1112 {
1113 /* Someday C++0x may allow for enum template specialization. */
1114 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1115 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1116 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1117 "of %qD not allowed by ISO C++", type);
1118 else
1119 {
1120 error ("explicit specialization of non-template %qT", type);
1121 return error_mark_node;
1122 }
1123 }
1124
1125 return type;
1126 }
1127
1128 /* Returns nonzero if we can optimize the retrieval of specializations
1129 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1130 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1131
1132 static inline bool
1133 optimize_specialization_lookup_p (tree tmpl)
1134 {
1135 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1136 && DECL_CLASS_SCOPE_P (tmpl)
1137 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1138 parameter. */
1139 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1140 /* The optimized lookup depends on the fact that the
1141 template arguments for the member function template apply
1142 purely to the containing class, which is not true if the
1143 containing class is an explicit or partial
1144 specialization. */
1145 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1146 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1147 && !DECL_CONV_FN_P (tmpl)
1148 /* It is possible to have a template that is not a member
1149 template and is not a member of a template class:
1150
1151 template <typename T>
1152 struct S { friend A::f(); };
1153
1154 Here, the friend function is a template, but the context does
1155 not have template information. The optimized lookup relies
1156 on having ARGS be the template arguments for both the class
1157 and the function template. */
1158 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1159 }
1160
1161 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1162 gone through coerce_template_parms by now. */
1163
1164 static void
1165 verify_unstripped_args_1 (tree inner)
1166 {
1167 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1168 {
1169 tree arg = TREE_VEC_ELT (inner, i);
1170 if (TREE_CODE (arg) == TEMPLATE_DECL)
1171 /* OK */;
1172 else if (TYPE_P (arg))
1173 gcc_assert (strip_typedefs (arg, NULL) == arg);
1174 else if (ARGUMENT_PACK_P (arg))
1175 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1176 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1177 /* Allow typedefs on the type of a non-type argument, since a
1178 parameter can have them. */;
1179 else
1180 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1181 }
1182 }
1183
1184 static void
1185 verify_unstripped_args (tree args)
1186 {
1187 ++processing_template_decl;
1188 if (!any_dependent_template_arguments_p (args))
1189 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1190 --processing_template_decl;
1191 }
1192
1193 /* Retrieve the specialization (in the sense of [temp.spec] - a
1194 specialization is either an instantiation or an explicit
1195 specialization) of TMPL for the given template ARGS. If there is
1196 no such specialization, return NULL_TREE. The ARGS are a vector of
1197 arguments, or a vector of vectors of arguments, in the case of
1198 templates with more than one level of parameters.
1199
1200 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1201 then we search for a partial specialization matching ARGS. This
1202 parameter is ignored if TMPL is not a class template.
1203
1204 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1205 result is a NONTYPE_ARGUMENT_PACK. */
1206
1207 static tree
1208 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1209 {
1210 if (tmpl == NULL_TREE)
1211 return NULL_TREE;
1212
1213 if (args == error_mark_node)
1214 return NULL_TREE;
1215
1216 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1217 || TREE_CODE (tmpl) == FIELD_DECL);
1218
1219 /* There should be as many levels of arguments as there are
1220 levels of parameters. */
1221 gcc_assert (TMPL_ARGS_DEPTH (args)
1222 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1223 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1224 : template_class_depth (DECL_CONTEXT (tmpl))));
1225
1226 if (flag_checking)
1227 verify_unstripped_args (args);
1228
1229 /* Lambda functions in templates aren't instantiated normally, but through
1230 tsubst_lambda_expr. */
1231 if (lambda_fn_in_template_p (tmpl))
1232 return NULL_TREE;
1233
1234 if (optimize_specialization_lookup_p (tmpl))
1235 {
1236 /* The template arguments actually apply to the containing
1237 class. Find the class specialization with those
1238 arguments. */
1239 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1240 tree class_specialization
1241 = retrieve_specialization (class_template, args, 0);
1242 if (!class_specialization)
1243 return NULL_TREE;
1244
1245 /* Find the instance of TMPL. */
1246 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1247 for (ovl_iterator iter (fns); iter; ++iter)
1248 {
1249 tree fn = *iter;
1250 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1251 /* using-declarations can add base methods to the method vec,
1252 and we don't want those here. */
1253 && DECL_CONTEXT (fn) == class_specialization)
1254 return fn;
1255 }
1256 return NULL_TREE;
1257 }
1258 else
1259 {
1260 spec_entry *found;
1261 spec_entry elt;
1262 hash_table<spec_hasher> *specializations;
1263
1264 elt.tmpl = tmpl;
1265 elt.args = args;
1266 elt.spec = NULL_TREE;
1267
1268 if (DECL_CLASS_TEMPLATE_P (tmpl))
1269 specializations = type_specializations;
1270 else
1271 specializations = decl_specializations;
1272
1273 if (hash == 0)
1274 hash = spec_hasher::hash (&elt);
1275 found = specializations->find_with_hash (&elt, hash);
1276 if (found)
1277 return found->spec;
1278 }
1279
1280 return NULL_TREE;
1281 }
1282
1283 /* Like retrieve_specialization, but for local declarations. */
1284
1285 tree
1286 retrieve_local_specialization (tree tmpl)
1287 {
1288 if (local_specializations == NULL)
1289 return NULL_TREE;
1290
1291 tree *slot = local_specializations->get (tmpl);
1292 return slot ? *slot : NULL_TREE;
1293 }
1294
1295 /* Returns nonzero iff DECL is a specialization of TMPL. */
1296
1297 int
1298 is_specialization_of (tree decl, tree tmpl)
1299 {
1300 tree t;
1301
1302 if (TREE_CODE (decl) == FUNCTION_DECL)
1303 {
1304 for (t = decl;
1305 t != NULL_TREE;
1306 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1307 if (t == tmpl)
1308 return 1;
1309 }
1310 else
1311 {
1312 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1313
1314 for (t = TREE_TYPE (decl);
1315 t != NULL_TREE;
1316 t = CLASSTYPE_USE_TEMPLATE (t)
1317 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1318 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1319 return 1;
1320 }
1321
1322 return 0;
1323 }
1324
1325 /* Returns nonzero iff DECL is a specialization of friend declaration
1326 FRIEND_DECL according to [temp.friend]. */
1327
1328 bool
1329 is_specialization_of_friend (tree decl, tree friend_decl)
1330 {
1331 bool need_template = true;
1332 int template_depth;
1333
1334 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1335 || TREE_CODE (decl) == TYPE_DECL);
1336
1337 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1338 of a template class, we want to check if DECL is a specialization
1339 if this. */
1340 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1341 && DECL_TEMPLATE_INFO (friend_decl)
1342 && !DECL_USE_TEMPLATE (friend_decl))
1343 {
1344 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1345 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1346 need_template = false;
1347 }
1348 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1349 && !PRIMARY_TEMPLATE_P (friend_decl))
1350 need_template = false;
1351
1352 /* There is nothing to do if this is not a template friend. */
1353 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1354 return false;
1355
1356 if (is_specialization_of (decl, friend_decl))
1357 return true;
1358
1359 /* [temp.friend/6]
1360 A member of a class template may be declared to be a friend of a
1361 non-template class. In this case, the corresponding member of
1362 every specialization of the class template is a friend of the
1363 class granting friendship.
1364
1365 For example, given a template friend declaration
1366
1367 template <class T> friend void A<T>::f();
1368
1369 the member function below is considered a friend
1370
1371 template <> struct A<int> {
1372 void f();
1373 };
1374
1375 For this type of template friend, TEMPLATE_DEPTH below will be
1376 nonzero. To determine if DECL is a friend of FRIEND, we first
1377 check if the enclosing class is a specialization of another. */
1378
1379 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1380 if (template_depth
1381 && DECL_CLASS_SCOPE_P (decl)
1382 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1383 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1384 {
1385 /* Next, we check the members themselves. In order to handle
1386 a few tricky cases, such as when FRIEND_DECL's are
1387
1388 template <class T> friend void A<T>::g(T t);
1389 template <class T> template <T t> friend void A<T>::h();
1390
1391 and DECL's are
1392
1393 void A<int>::g(int);
1394 template <int> void A<int>::h();
1395
1396 we need to figure out ARGS, the template arguments from
1397 the context of DECL. This is required for template substitution
1398 of `T' in the function parameter of `g' and template parameter
1399 of `h' in the above examples. Here ARGS corresponds to `int'. */
1400
1401 tree context = DECL_CONTEXT (decl);
1402 tree args = NULL_TREE;
1403 int current_depth = 0;
1404
1405 while (current_depth < template_depth)
1406 {
1407 if (CLASSTYPE_TEMPLATE_INFO (context))
1408 {
1409 if (current_depth == 0)
1410 args = TYPE_TI_ARGS (context);
1411 else
1412 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1413 current_depth++;
1414 }
1415 context = TYPE_CONTEXT (context);
1416 }
1417
1418 if (TREE_CODE (decl) == FUNCTION_DECL)
1419 {
1420 bool is_template;
1421 tree friend_type;
1422 tree decl_type;
1423 tree friend_args_type;
1424 tree decl_args_type;
1425
1426 /* Make sure that both DECL and FRIEND_DECL are templates or
1427 non-templates. */
1428 is_template = DECL_TEMPLATE_INFO (decl)
1429 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1430 if (need_template ^ is_template)
1431 return false;
1432 else if (is_template)
1433 {
1434 /* If both are templates, check template parameter list. */
1435 tree friend_parms
1436 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1437 args, tf_none);
1438 if (!comp_template_parms
1439 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1440 friend_parms))
1441 return false;
1442
1443 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1444 }
1445 else
1446 decl_type = TREE_TYPE (decl);
1447
1448 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1449 tf_none, NULL_TREE);
1450 if (friend_type == error_mark_node)
1451 return false;
1452
1453 /* Check if return types match. */
1454 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1455 return false;
1456
1457 /* Check if function parameter types match, ignoring the
1458 `this' parameter. */
1459 friend_args_type = TYPE_ARG_TYPES (friend_type);
1460 decl_args_type = TYPE_ARG_TYPES (decl_type);
1461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1462 friend_args_type = TREE_CHAIN (friend_args_type);
1463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1464 decl_args_type = TREE_CHAIN (decl_args_type);
1465
1466 return compparms (decl_args_type, friend_args_type);
1467 }
1468 else
1469 {
1470 /* DECL is a TYPE_DECL */
1471 bool is_template;
1472 tree decl_type = TREE_TYPE (decl);
1473
1474 /* Make sure that both DECL and FRIEND_DECL are templates or
1475 non-templates. */
1476 is_template
1477 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1478 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1479
1480 if (need_template ^ is_template)
1481 return false;
1482 else if (is_template)
1483 {
1484 tree friend_parms;
1485 /* If both are templates, check the name of the two
1486 TEMPLATE_DECL's first because is_friend didn't. */
1487 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1488 != DECL_NAME (friend_decl))
1489 return false;
1490
1491 /* Now check template parameter list. */
1492 friend_parms
1493 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1494 args, tf_none);
1495 return comp_template_parms
1496 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1497 friend_parms);
1498 }
1499 else
1500 return (DECL_NAME (decl)
1501 == DECL_NAME (friend_decl));
1502 }
1503 }
1504 return false;
1505 }
1506
1507 /* Register the specialization SPEC as a specialization of TMPL with
1508 the indicated ARGS. IS_FRIEND indicates whether the specialization
1509 is actually just a friend declaration. ATTRLIST is the list of
1510 attributes that the specialization is declared with or NULL when
1511 it isn't. Returns SPEC, or an equivalent prior declaration, if
1512 available.
1513
1514 We also store instantiations of field packs in the hash table, even
1515 though they are not themselves templates, to make lookup easier. */
1516
1517 static tree
1518 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1519 hashval_t hash)
1520 {
1521 tree fn;
1522 spec_entry **slot = NULL;
1523 spec_entry elt;
1524
1525 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1526 || (TREE_CODE (tmpl) == FIELD_DECL
1527 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1528
1529 if (TREE_CODE (spec) == FUNCTION_DECL
1530 && uses_template_parms (DECL_TI_ARGS (spec)))
1531 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1532 register it; we want the corresponding TEMPLATE_DECL instead.
1533 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1534 the more obvious `uses_template_parms (spec)' to avoid problems
1535 with default function arguments. In particular, given
1536 something like this:
1537
1538 template <class T> void f(T t1, T t = T())
1539
1540 the default argument expression is not substituted for in an
1541 instantiation unless and until it is actually needed. */
1542 return spec;
1543
1544 if (optimize_specialization_lookup_p (tmpl))
1545 /* We don't put these specializations in the hash table, but we might
1546 want to give an error about a mismatch. */
1547 fn = retrieve_specialization (tmpl, args, 0);
1548 else
1549 {
1550 elt.tmpl = tmpl;
1551 elt.args = args;
1552 elt.spec = spec;
1553
1554 if (hash == 0)
1555 hash = spec_hasher::hash (&elt);
1556
1557 slot =
1558 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1559 if (*slot)
1560 fn = ((spec_entry *) *slot)->spec;
1561 else
1562 fn = NULL_TREE;
1563 }
1564
1565 /* We can sometimes try to re-register a specialization that we've
1566 already got. In particular, regenerate_decl_from_template calls
1567 duplicate_decls which will update the specialization list. But,
1568 we'll still get called again here anyhow. It's more convenient
1569 to simply allow this than to try to prevent it. */
1570 if (fn == spec)
1571 return spec;
1572 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1573 {
1574 if (DECL_TEMPLATE_INSTANTIATION (fn))
1575 {
1576 if (DECL_ODR_USED (fn)
1577 || DECL_EXPLICIT_INSTANTIATION (fn))
1578 {
1579 error ("specialization of %qD after instantiation",
1580 fn);
1581 return error_mark_node;
1582 }
1583 else
1584 {
1585 tree clone;
1586 /* This situation should occur only if the first
1587 specialization is an implicit instantiation, the
1588 second is an explicit specialization, and the
1589 implicit instantiation has not yet been used. That
1590 situation can occur if we have implicitly
1591 instantiated a member function and then specialized
1592 it later.
1593
1594 We can also wind up here if a friend declaration that
1595 looked like an instantiation turns out to be a
1596 specialization:
1597
1598 template <class T> void foo(T);
1599 class S { friend void foo<>(int) };
1600 template <> void foo(int);
1601
1602 We transform the existing DECL in place so that any
1603 pointers to it become pointers to the updated
1604 declaration.
1605
1606 If there was a definition for the template, but not
1607 for the specialization, we want this to look as if
1608 there were no definition, and vice versa. */
1609 DECL_INITIAL (fn) = NULL_TREE;
1610 duplicate_decls (spec, fn, is_friend);
1611 /* The call to duplicate_decls will have applied
1612 [temp.expl.spec]:
1613
1614 An explicit specialization of a function template
1615 is inline only if it is explicitly declared to be,
1616 and independently of whether its function template
1617 is.
1618
1619 to the primary function; now copy the inline bits to
1620 the various clones. */
1621 FOR_EACH_CLONE (clone, fn)
1622 {
1623 DECL_DECLARED_INLINE_P (clone)
1624 = DECL_DECLARED_INLINE_P (fn);
1625 DECL_SOURCE_LOCATION (clone)
1626 = DECL_SOURCE_LOCATION (fn);
1627 DECL_DELETED_FN (clone)
1628 = DECL_DELETED_FN (fn);
1629 }
1630 check_specialization_namespace (tmpl);
1631
1632 return fn;
1633 }
1634 }
1635 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1636 {
1637 tree dd = duplicate_decls (spec, fn, is_friend);
1638 if (dd == error_mark_node)
1639 /* We've already complained in duplicate_decls. */
1640 return error_mark_node;
1641
1642 if (dd == NULL_TREE && DECL_INITIAL (spec))
1643 /* Dup decl failed, but this is a new definition. Set the
1644 line number so any errors match this new
1645 definition. */
1646 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1647
1648 return fn;
1649 }
1650 }
1651 else if (fn)
1652 return duplicate_decls (spec, fn, is_friend);
1653
1654 /* A specialization must be declared in the same namespace as the
1655 template it is specializing. */
1656 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1657 && !check_specialization_namespace (tmpl))
1658 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1659
1660 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1661 {
1662 spec_entry *entry = ggc_alloc<spec_entry> ();
1663 gcc_assert (tmpl && args && spec);
1664 *entry = elt;
1665 *slot = entry;
1666 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1667 && PRIMARY_TEMPLATE_P (tmpl)
1668 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1669 || variable_template_p (tmpl))
1670 /* If TMPL is a forward declaration of a template function, keep a list
1671 of all specializations in case we need to reassign them to a friend
1672 template later in tsubst_friend_function.
1673
1674 Also keep a list of all variable template instantiations so that
1675 process_partial_specialization can check whether a later partial
1676 specialization would have used it. */
1677 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1678 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1679 }
1680
1681 return spec;
1682 }
1683
1684 /* Returns true iff two spec_entry nodes are equivalent. */
1685
1686 int comparing_specializations;
1687
1688 bool
1689 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1690 {
1691 int equal;
1692
1693 ++comparing_specializations;
1694 equal = (e1->tmpl == e2->tmpl
1695 && comp_template_args (e1->args, e2->args));
1696 if (equal && flag_concepts
1697 /* tmpl could be a FIELD_DECL for a capture pack. */
1698 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1699 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1700 && uses_template_parms (e1->args))
1701 {
1702 /* Partial specializations of a variable template can be distinguished by
1703 constraints. */
1704 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1705 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1706 equal = equivalent_constraints (c1, c2);
1707 }
1708 --comparing_specializations;
1709
1710 return equal;
1711 }
1712
1713 /* Returns a hash for a template TMPL and template arguments ARGS. */
1714
1715 static hashval_t
1716 hash_tmpl_and_args (tree tmpl, tree args)
1717 {
1718 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1719 return iterative_hash_template_arg (args, val);
1720 }
1721
1722 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1723 ignoring SPEC. */
1724
1725 hashval_t
1726 spec_hasher::hash (spec_entry *e)
1727 {
1728 return hash_tmpl_and_args (e->tmpl, e->args);
1729 }
1730
1731 /* Recursively calculate a hash value for a template argument ARG, for use
1732 in the hash tables of template specializations. */
1733
1734 hashval_t
1735 iterative_hash_template_arg (tree arg, hashval_t val)
1736 {
1737 unsigned HOST_WIDE_INT i;
1738 enum tree_code code;
1739 char tclass;
1740
1741 if (arg == NULL_TREE)
1742 return iterative_hash_object (arg, val);
1743
1744 if (!TYPE_P (arg))
1745 STRIP_NOPS (arg);
1746
1747 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1748 gcc_unreachable ();
1749
1750 code = TREE_CODE (arg);
1751 tclass = TREE_CODE_CLASS (code);
1752
1753 val = iterative_hash_object (code, val);
1754
1755 switch (code)
1756 {
1757 case ERROR_MARK:
1758 return val;
1759
1760 case IDENTIFIER_NODE:
1761 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1762
1763 case TREE_VEC:
1764 {
1765 int i, len = TREE_VEC_LENGTH (arg);
1766 for (i = 0; i < len; ++i)
1767 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1768 return val;
1769 }
1770
1771 case TYPE_PACK_EXPANSION:
1772 case EXPR_PACK_EXPANSION:
1773 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1774 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1775
1776 case TYPE_ARGUMENT_PACK:
1777 case NONTYPE_ARGUMENT_PACK:
1778 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1779
1780 case TREE_LIST:
1781 for (; arg; arg = TREE_CHAIN (arg))
1782 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1783 return val;
1784
1785 case OVERLOAD:
1786 for (lkp_iterator iter (arg); iter; ++iter)
1787 val = iterative_hash_template_arg (*iter, val);
1788 return val;
1789
1790 case CONSTRUCTOR:
1791 {
1792 tree field, value;
1793 iterative_hash_template_arg (TREE_TYPE (arg), val);
1794 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1795 {
1796 val = iterative_hash_template_arg (field, val);
1797 val = iterative_hash_template_arg (value, val);
1798 }
1799 return val;
1800 }
1801
1802 case PARM_DECL:
1803 if (!DECL_ARTIFICIAL (arg))
1804 {
1805 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1806 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1807 }
1808 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1809
1810 case TARGET_EXPR:
1811 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1812
1813 case PTRMEM_CST:
1814 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1815 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1816
1817 case TEMPLATE_PARM_INDEX:
1818 val = iterative_hash_template_arg
1819 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1820 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1821 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1822
1823 case TRAIT_EXPR:
1824 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1825 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1826 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1827
1828 case BASELINK:
1829 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1830 val);
1831 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1832 val);
1833
1834 case MODOP_EXPR:
1835 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1836 code = TREE_CODE (TREE_OPERAND (arg, 1));
1837 val = iterative_hash_object (code, val);
1838 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1839
1840 case LAMBDA_EXPR:
1841 /* [temp.over.link] Two lambda-expressions are never considered
1842 equivalent.
1843
1844 So just hash the closure type. */
1845 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1846
1847 case CAST_EXPR:
1848 case IMPLICIT_CONV_EXPR:
1849 case STATIC_CAST_EXPR:
1850 case REINTERPRET_CAST_EXPR:
1851 case CONST_CAST_EXPR:
1852 case DYNAMIC_CAST_EXPR:
1853 case NEW_EXPR:
1854 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1855 /* Now hash operands as usual. */
1856 break;
1857
1858 case CALL_EXPR:
1859 {
1860 tree fn = CALL_EXPR_FN (arg);
1861 if (tree name = dependent_name (fn))
1862 {
1863 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1864 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1865 fn = name;
1866 }
1867 val = iterative_hash_template_arg (fn, val);
1868 call_expr_arg_iterator ai;
1869 for (tree x = first_call_expr_arg (arg, &ai); x;
1870 x = next_call_expr_arg (&ai))
1871 val = iterative_hash_template_arg (x, val);
1872 return val;
1873 }
1874
1875 default:
1876 break;
1877 }
1878
1879 switch (tclass)
1880 {
1881 case tcc_type:
1882 if (alias_template_specialization_p (arg))
1883 {
1884 // We want an alias specialization that survived strip_typedefs
1885 // to hash differently from its TYPE_CANONICAL, to avoid hash
1886 // collisions that compare as different in template_args_equal.
1887 // These could be dependent specializations that strip_typedefs
1888 // left alone, or untouched specializations because
1889 // coerce_template_parms returns the unconverted template
1890 // arguments if it sees incomplete argument packs.
1891 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1892 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1893 }
1894 if (TYPE_CANONICAL (arg))
1895 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1896 val);
1897 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1898 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1899 /* Otherwise just compare the types during lookup. */
1900 return val;
1901
1902 case tcc_declaration:
1903 case tcc_constant:
1904 return iterative_hash_expr (arg, val);
1905
1906 default:
1907 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1908 {
1909 unsigned n = cp_tree_operand_length (arg);
1910 for (i = 0; i < n; ++i)
1911 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1912 return val;
1913 }
1914 }
1915 gcc_unreachable ();
1916 return 0;
1917 }
1918
1919 /* Unregister the specialization SPEC as a specialization of TMPL.
1920 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1921 if the SPEC was listed as a specialization of TMPL.
1922
1923 Note that SPEC has been ggc_freed, so we can't look inside it. */
1924
1925 bool
1926 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1927 {
1928 spec_entry *entry;
1929 spec_entry elt;
1930
1931 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1932 elt.args = TI_ARGS (tinfo);
1933 elt.spec = NULL_TREE;
1934
1935 entry = decl_specializations->find (&elt);
1936 if (entry != NULL)
1937 {
1938 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1939 gcc_assert (new_spec != NULL_TREE);
1940 entry->spec = new_spec;
1941 return 1;
1942 }
1943
1944 return 0;
1945 }
1946
1947 /* Like register_specialization, but for local declarations. We are
1948 registering SPEC, an instantiation of TMPL. */
1949
1950 void
1951 register_local_specialization (tree spec, tree tmpl)
1952 {
1953 gcc_assert (tmpl != spec);
1954 local_specializations->put (tmpl, spec);
1955 }
1956
1957 /* TYPE is a class type. Returns true if TYPE is an explicitly
1958 specialized class. */
1959
1960 bool
1961 explicit_class_specialization_p (tree type)
1962 {
1963 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1964 return false;
1965 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1966 }
1967
1968 /* Print the list of functions at FNS, going through all the overloads
1969 for each element of the list. Alternatively, FNS cannot be a
1970 TREE_LIST, in which case it will be printed together with all the
1971 overloads.
1972
1973 MORE and *STR should respectively be FALSE and NULL when the function
1974 is called from the outside. They are used internally on recursive
1975 calls. print_candidates manages the two parameters and leaves NULL
1976 in *STR when it ends. */
1977
1978 static void
1979 print_candidates_1 (tree fns, char **str, bool more = false)
1980 {
1981 if (TREE_CODE (fns) == TREE_LIST)
1982 for (; fns; fns = TREE_CHAIN (fns))
1983 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1984 else
1985 for (lkp_iterator iter (fns); iter;)
1986 {
1987 tree cand = *iter;
1988 ++iter;
1989
1990 const char *pfx = *str;
1991 if (!pfx)
1992 {
1993 if (more || iter)
1994 pfx = _("candidates are:");
1995 else
1996 pfx = _("candidate is:");
1997 *str = get_spaces (pfx);
1998 }
1999 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2000 }
2001 }
2002
2003 /* Print the list of candidate FNS in an error message. FNS can also
2004 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2005
2006 void
2007 print_candidates (tree fns)
2008 {
2009 char *str = NULL;
2010 print_candidates_1 (fns, &str);
2011 free (str);
2012 }
2013
2014 /* Get a (possibly) constrained template declaration for the
2015 purpose of ordering candidates. */
2016 static tree
2017 get_template_for_ordering (tree list)
2018 {
2019 gcc_assert (TREE_CODE (list) == TREE_LIST);
2020 tree f = TREE_VALUE (list);
2021 if (tree ti = DECL_TEMPLATE_INFO (f))
2022 return TI_TEMPLATE (ti);
2023 return f;
2024 }
2025
2026 /* Among candidates having the same signature, return the
2027 most constrained or NULL_TREE if there is no best candidate.
2028 If the signatures of candidates vary (e.g., template
2029 specialization vs. member function), then there can be no
2030 most constrained.
2031
2032 Note that we don't compare constraints on the functions
2033 themselves, but rather those of their templates. */
2034 static tree
2035 most_constrained_function (tree candidates)
2036 {
2037 // Try to find the best candidate in a first pass.
2038 tree champ = candidates;
2039 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2040 {
2041 int winner = more_constrained (get_template_for_ordering (champ),
2042 get_template_for_ordering (c));
2043 if (winner == -1)
2044 champ = c; // The candidate is more constrained
2045 else if (winner == 0)
2046 return NULL_TREE; // Neither is more constrained
2047 }
2048
2049 // Verify that the champ is better than previous candidates.
2050 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2051 if (!more_constrained (get_template_for_ordering (champ),
2052 get_template_for_ordering (c)))
2053 return NULL_TREE;
2054 }
2055
2056 return champ;
2057 }
2058
2059
2060 /* Returns the template (one of the functions given by TEMPLATE_ID)
2061 which can be specialized to match the indicated DECL with the
2062 explicit template args given in TEMPLATE_ID. The DECL may be
2063 NULL_TREE if none is available. In that case, the functions in
2064 TEMPLATE_ID are non-members.
2065
2066 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2067 specialization of a member template.
2068
2069 The TEMPLATE_COUNT is the number of references to qualifying
2070 template classes that appeared in the name of the function. See
2071 check_explicit_specialization for a more accurate description.
2072
2073 TSK indicates what kind of template declaration (if any) is being
2074 declared. TSK_TEMPLATE indicates that the declaration given by
2075 DECL, though a FUNCTION_DECL, has template parameters, and is
2076 therefore a template function.
2077
2078 The template args (those explicitly specified and those deduced)
2079 are output in a newly created vector *TARGS_OUT.
2080
2081 If it is impossible to determine the result, an error message is
2082 issued. The error_mark_node is returned to indicate failure. */
2083
2084 static tree
2085 determine_specialization (tree template_id,
2086 tree decl,
2087 tree* targs_out,
2088 int need_member_template,
2089 int template_count,
2090 tmpl_spec_kind tsk)
2091 {
2092 tree fns;
2093 tree targs;
2094 tree explicit_targs;
2095 tree candidates = NULL_TREE;
2096
2097 /* A TREE_LIST of templates of which DECL may be a specialization.
2098 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2099 corresponding TREE_PURPOSE is the set of template arguments that,
2100 when used to instantiate the template, would produce a function
2101 with the signature of DECL. */
2102 tree templates = NULL_TREE;
2103 int header_count;
2104 cp_binding_level *b;
2105
2106 *targs_out = NULL_TREE;
2107
2108 if (template_id == error_mark_node || decl == error_mark_node)
2109 return error_mark_node;
2110
2111 /* We shouldn't be specializing a member template of an
2112 unspecialized class template; we already gave an error in
2113 check_specialization_scope, now avoid crashing. */
2114 if (!VAR_P (decl)
2115 && template_count && DECL_CLASS_SCOPE_P (decl)
2116 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2117 {
2118 gcc_assert (errorcount);
2119 return error_mark_node;
2120 }
2121
2122 fns = TREE_OPERAND (template_id, 0);
2123 explicit_targs = TREE_OPERAND (template_id, 1);
2124
2125 if (fns == error_mark_node)
2126 return error_mark_node;
2127
2128 /* Check for baselinks. */
2129 if (BASELINK_P (fns))
2130 fns = BASELINK_FUNCTIONS (fns);
2131
2132 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2133 {
2134 error_at (DECL_SOURCE_LOCATION (decl),
2135 "%qD is not a function template", fns);
2136 return error_mark_node;
2137 }
2138 else if (VAR_P (decl) && !variable_template_p (fns))
2139 {
2140 error ("%qD is not a variable template", fns);
2141 return error_mark_node;
2142 }
2143
2144 /* Count the number of template headers specified for this
2145 specialization. */
2146 header_count = 0;
2147 for (b = current_binding_level;
2148 b->kind == sk_template_parms;
2149 b = b->level_chain)
2150 ++header_count;
2151
2152 tree orig_fns = fns;
2153
2154 if (variable_template_p (fns))
2155 {
2156 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2157 targs = coerce_template_parms (parms, explicit_targs, fns,
2158 tf_warning_or_error,
2159 /*req_all*/true, /*use_defarg*/true);
2160 if (targs != error_mark_node)
2161 templates = tree_cons (targs, fns, templates);
2162 }
2163 else for (lkp_iterator iter (fns); iter; ++iter)
2164 {
2165 tree fn = *iter;
2166
2167 if (TREE_CODE (fn) == TEMPLATE_DECL)
2168 {
2169 tree decl_arg_types;
2170 tree fn_arg_types;
2171 tree insttype;
2172
2173 /* In case of explicit specialization, we need to check if
2174 the number of template headers appearing in the specialization
2175 is correct. This is usually done in check_explicit_specialization,
2176 but the check done there cannot be exhaustive when specializing
2177 member functions. Consider the following code:
2178
2179 template <> void A<int>::f(int);
2180 template <> template <> void A<int>::f(int);
2181
2182 Assuming that A<int> is not itself an explicit specialization
2183 already, the first line specializes "f" which is a non-template
2184 member function, whilst the second line specializes "f" which
2185 is a template member function. So both lines are syntactically
2186 correct, and check_explicit_specialization does not reject
2187 them.
2188
2189 Here, we can do better, as we are matching the specialization
2190 against the declarations. We count the number of template
2191 headers, and we check if they match TEMPLATE_COUNT + 1
2192 (TEMPLATE_COUNT is the number of qualifying template classes,
2193 plus there must be another header for the member template
2194 itself).
2195
2196 Notice that if header_count is zero, this is not a
2197 specialization but rather a template instantiation, so there
2198 is no check we can perform here. */
2199 if (header_count && header_count != template_count + 1)
2200 continue;
2201
2202 /* Check that the number of template arguments at the
2203 innermost level for DECL is the same as for FN. */
2204 if (current_binding_level->kind == sk_template_parms
2205 && !current_binding_level->explicit_spec_p
2206 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2207 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2208 (current_template_parms))))
2209 continue;
2210
2211 /* DECL might be a specialization of FN. */
2212 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2213 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2214
2215 /* For a non-static member function, we need to make sure
2216 that the const qualification is the same. Since
2217 get_bindings does not try to merge the "this" parameter,
2218 we must do the comparison explicitly. */
2219 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2220 {
2221 if (!same_type_p (TREE_VALUE (fn_arg_types),
2222 TREE_VALUE (decl_arg_types)))
2223 continue;
2224
2225 /* And the ref-qualification. */
2226 if (type_memfn_rqual (TREE_TYPE (decl))
2227 != type_memfn_rqual (TREE_TYPE (fn)))
2228 continue;
2229 }
2230
2231 /* Skip the "this" parameter and, for constructors of
2232 classes with virtual bases, the VTT parameter. A
2233 full specialization of a constructor will have a VTT
2234 parameter, but a template never will. */
2235 decl_arg_types
2236 = skip_artificial_parms_for (decl, decl_arg_types);
2237 fn_arg_types
2238 = skip_artificial_parms_for (fn, fn_arg_types);
2239
2240 /* Function templates cannot be specializations; there are
2241 no partial specializations of functions. Therefore, if
2242 the type of DECL does not match FN, there is no
2243 match.
2244
2245 Note that it should never be the case that we have both
2246 candidates added here, and for regular member functions
2247 below. */
2248 if (tsk == tsk_template)
2249 {
2250 if (compparms (fn_arg_types, decl_arg_types))
2251 candidates = tree_cons (NULL_TREE, fn, candidates);
2252 continue;
2253 }
2254
2255 /* See whether this function might be a specialization of this
2256 template. Suppress access control because we might be trying
2257 to make this specialization a friend, and we have already done
2258 access control for the declaration of the specialization. */
2259 push_deferring_access_checks (dk_no_check);
2260 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2261 pop_deferring_access_checks ();
2262
2263 if (!targs)
2264 /* We cannot deduce template arguments that when used to
2265 specialize TMPL will produce DECL. */
2266 continue;
2267
2268 if (uses_template_parms (targs))
2269 /* We deduced something involving 'auto', which isn't a valid
2270 template argument. */
2271 continue;
2272
2273 /* Remove, from the set of candidates, all those functions
2274 whose constraints are not satisfied. */
2275 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2276 continue;
2277
2278 // Then, try to form the new function type.
2279 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2280 if (insttype == error_mark_node)
2281 continue;
2282 fn_arg_types
2283 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2284 if (!compparms (fn_arg_types, decl_arg_types))
2285 continue;
2286
2287 /* Save this template, and the arguments deduced. */
2288 templates = tree_cons (targs, fn, templates);
2289 }
2290 else if (need_member_template)
2291 /* FN is an ordinary member function, and we need a
2292 specialization of a member template. */
2293 ;
2294 else if (TREE_CODE (fn) != FUNCTION_DECL)
2295 /* We can get IDENTIFIER_NODEs here in certain erroneous
2296 cases. */
2297 ;
2298 else if (!DECL_FUNCTION_MEMBER_P (fn))
2299 /* This is just an ordinary non-member function. Nothing can
2300 be a specialization of that. */
2301 ;
2302 else if (DECL_ARTIFICIAL (fn))
2303 /* Cannot specialize functions that are created implicitly. */
2304 ;
2305 else
2306 {
2307 tree decl_arg_types;
2308
2309 /* This is an ordinary member function. However, since
2310 we're here, we can assume its enclosing class is a
2311 template class. For example,
2312
2313 template <typename T> struct S { void f(); };
2314 template <> void S<int>::f() {}
2315
2316 Here, S<int>::f is a non-template, but S<int> is a
2317 template class. If FN has the same type as DECL, we
2318 might be in business. */
2319
2320 if (!DECL_TEMPLATE_INFO (fn))
2321 /* Its enclosing class is an explicit specialization
2322 of a template class. This is not a candidate. */
2323 continue;
2324
2325 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2326 TREE_TYPE (TREE_TYPE (fn))))
2327 /* The return types differ. */
2328 continue;
2329
2330 /* Adjust the type of DECL in case FN is a static member. */
2331 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2332 if (DECL_STATIC_FUNCTION_P (fn)
2333 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2334 decl_arg_types = TREE_CHAIN (decl_arg_types);
2335
2336 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2337 decl_arg_types))
2338 continue;
2339
2340 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2341 && (type_memfn_rqual (TREE_TYPE (decl))
2342 != type_memfn_rqual (TREE_TYPE (fn))))
2343 continue;
2344
2345 // If the deduced arguments do not satisfy the constraints,
2346 // this is not a candidate.
2347 if (flag_concepts && !constraints_satisfied_p (fn))
2348 continue;
2349
2350 // Add the candidate.
2351 candidates = tree_cons (NULL_TREE, fn, candidates);
2352 }
2353 }
2354
2355 if (templates && TREE_CHAIN (templates))
2356 {
2357 /* We have:
2358
2359 [temp.expl.spec]
2360
2361 It is possible for a specialization with a given function
2362 signature to be instantiated from more than one function
2363 template. In such cases, explicit specification of the
2364 template arguments must be used to uniquely identify the
2365 function template specialization being specialized.
2366
2367 Note that here, there's no suggestion that we're supposed to
2368 determine which of the candidate templates is most
2369 specialized. However, we, also have:
2370
2371 [temp.func.order]
2372
2373 Partial ordering of overloaded function template
2374 declarations is used in the following contexts to select
2375 the function template to which a function template
2376 specialization refers:
2377
2378 -- when an explicit specialization refers to a function
2379 template.
2380
2381 So, we do use the partial ordering rules, at least for now.
2382 This extension can only serve to make invalid programs valid,
2383 so it's safe. And, there is strong anecdotal evidence that
2384 the committee intended the partial ordering rules to apply;
2385 the EDG front end has that behavior, and John Spicer claims
2386 that the committee simply forgot to delete the wording in
2387 [temp.expl.spec]. */
2388 tree tmpl = most_specialized_instantiation (templates);
2389 if (tmpl != error_mark_node)
2390 {
2391 templates = tmpl;
2392 TREE_CHAIN (templates) = NULL_TREE;
2393 }
2394 }
2395
2396 // Concepts allows multiple declarations of member functions
2397 // with the same signature. Like above, we need to rely on
2398 // on the partial ordering of those candidates to determine which
2399 // is the best.
2400 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2401 {
2402 if (tree cand = most_constrained_function (candidates))
2403 {
2404 candidates = cand;
2405 TREE_CHAIN (cand) = NULL_TREE;
2406 }
2407 }
2408
2409 if (templates == NULL_TREE && candidates == NULL_TREE)
2410 {
2411 error ("template-id %qD for %q+D does not match any template "
2412 "declaration", template_id, decl);
2413 if (header_count && header_count != template_count + 1)
2414 inform (DECL_SOURCE_LOCATION (decl),
2415 "saw %d %<template<>%>, need %d for "
2416 "specializing a member function template",
2417 header_count, template_count + 1);
2418 else
2419 print_candidates (orig_fns);
2420 return error_mark_node;
2421 }
2422 else if ((templates && TREE_CHAIN (templates))
2423 || (candidates && TREE_CHAIN (candidates))
2424 || (templates && candidates))
2425 {
2426 error ("ambiguous template specialization %qD for %q+D",
2427 template_id, decl);
2428 candidates = chainon (candidates, templates);
2429 print_candidates (candidates);
2430 return error_mark_node;
2431 }
2432
2433 /* We have one, and exactly one, match. */
2434 if (candidates)
2435 {
2436 tree fn = TREE_VALUE (candidates);
2437 *targs_out = copy_node (DECL_TI_ARGS (fn));
2438
2439 /* Propagate the candidate's constraints to the declaration. */
2440 set_constraints (decl, get_constraints (fn));
2441
2442 /* DECL is a re-declaration or partial instantiation of a template
2443 function. */
2444 if (TREE_CODE (fn) == TEMPLATE_DECL)
2445 return fn;
2446 /* It was a specialization of an ordinary member function in a
2447 template class. */
2448 return DECL_TI_TEMPLATE (fn);
2449 }
2450
2451 /* It was a specialization of a template. */
2452 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2453 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2454 {
2455 *targs_out = copy_node (targs);
2456 SET_TMPL_ARGS_LEVEL (*targs_out,
2457 TMPL_ARGS_DEPTH (*targs_out),
2458 TREE_PURPOSE (templates));
2459 }
2460 else
2461 *targs_out = TREE_PURPOSE (templates);
2462 return TREE_VALUE (templates);
2463 }
2464
2465 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2466 but with the default argument values filled in from those in the
2467 TMPL_TYPES. */
2468
2469 static tree
2470 copy_default_args_to_explicit_spec_1 (tree spec_types,
2471 tree tmpl_types)
2472 {
2473 tree new_spec_types;
2474
2475 if (!spec_types)
2476 return NULL_TREE;
2477
2478 if (spec_types == void_list_node)
2479 return void_list_node;
2480
2481 /* Substitute into the rest of the list. */
2482 new_spec_types =
2483 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2484 TREE_CHAIN (tmpl_types));
2485
2486 /* Add the default argument for this parameter. */
2487 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2488 TREE_VALUE (spec_types),
2489 new_spec_types);
2490 }
2491
2492 /* DECL is an explicit specialization. Replicate default arguments
2493 from the template it specializes. (That way, code like:
2494
2495 template <class T> void f(T = 3);
2496 template <> void f(double);
2497 void g () { f (); }
2498
2499 works, as required.) An alternative approach would be to look up
2500 the correct default arguments at the call-site, but this approach
2501 is consistent with how implicit instantiations are handled. */
2502
2503 static void
2504 copy_default_args_to_explicit_spec (tree decl)
2505 {
2506 tree tmpl;
2507 tree spec_types;
2508 tree tmpl_types;
2509 tree new_spec_types;
2510 tree old_type;
2511 tree new_type;
2512 tree t;
2513 tree object_type = NULL_TREE;
2514 tree in_charge = NULL_TREE;
2515 tree vtt = NULL_TREE;
2516
2517 /* See if there's anything we need to do. */
2518 tmpl = DECL_TI_TEMPLATE (decl);
2519 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2520 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2521 if (TREE_PURPOSE (t))
2522 break;
2523 if (!t)
2524 return;
2525
2526 old_type = TREE_TYPE (decl);
2527 spec_types = TYPE_ARG_TYPES (old_type);
2528
2529 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2530 {
2531 /* Remove the this pointer, but remember the object's type for
2532 CV quals. */
2533 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2534 spec_types = TREE_CHAIN (spec_types);
2535 tmpl_types = TREE_CHAIN (tmpl_types);
2536
2537 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2538 {
2539 /* DECL may contain more parameters than TMPL due to the extra
2540 in-charge parameter in constructors and destructors. */
2541 in_charge = spec_types;
2542 spec_types = TREE_CHAIN (spec_types);
2543 }
2544 if (DECL_HAS_VTT_PARM_P (decl))
2545 {
2546 vtt = spec_types;
2547 spec_types = TREE_CHAIN (spec_types);
2548 }
2549 }
2550
2551 /* Compute the merged default arguments. */
2552 new_spec_types =
2553 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2554
2555 /* Compute the new FUNCTION_TYPE. */
2556 if (object_type)
2557 {
2558 if (vtt)
2559 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2560 TREE_VALUE (vtt),
2561 new_spec_types);
2562
2563 if (in_charge)
2564 /* Put the in-charge parameter back. */
2565 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2566 TREE_VALUE (in_charge),
2567 new_spec_types);
2568
2569 new_type = build_method_type_directly (object_type,
2570 TREE_TYPE (old_type),
2571 new_spec_types);
2572 }
2573 else
2574 new_type = build_function_type (TREE_TYPE (old_type),
2575 new_spec_types);
2576 new_type = cp_build_type_attribute_variant (new_type,
2577 TYPE_ATTRIBUTES (old_type));
2578 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2579
2580 TREE_TYPE (decl) = new_type;
2581 }
2582
2583 /* Return the number of template headers we expect to see for a definition
2584 or specialization of CTYPE or one of its non-template members. */
2585
2586 int
2587 num_template_headers_for_class (tree ctype)
2588 {
2589 int num_templates = 0;
2590
2591 while (ctype && CLASS_TYPE_P (ctype))
2592 {
2593 /* You're supposed to have one `template <...>' for every
2594 template class, but you don't need one for a full
2595 specialization. For example:
2596
2597 template <class T> struct S{};
2598 template <> struct S<int> { void f(); };
2599 void S<int>::f () {}
2600
2601 is correct; there shouldn't be a `template <>' for the
2602 definition of `S<int>::f'. */
2603 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2604 /* If CTYPE does not have template information of any
2605 kind, then it is not a template, nor is it nested
2606 within a template. */
2607 break;
2608 if (explicit_class_specialization_p (ctype))
2609 break;
2610 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2611 ++num_templates;
2612
2613 ctype = TYPE_CONTEXT (ctype);
2614 }
2615
2616 return num_templates;
2617 }
2618
2619 /* Do a simple sanity check on the template headers that precede the
2620 variable declaration DECL. */
2621
2622 void
2623 check_template_variable (tree decl)
2624 {
2625 tree ctx = CP_DECL_CONTEXT (decl);
2626 int wanted = num_template_headers_for_class (ctx);
2627 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2628 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2629 {
2630 if (cxx_dialect < cxx14)
2631 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2632 "variable templates only available with "
2633 "%<-std=c++14%> or %<-std=gnu++14%>");
2634
2635 // Namespace-scope variable templates should have a template header.
2636 ++wanted;
2637 }
2638 if (template_header_count > wanted)
2639 {
2640 auto_diagnostic_group d;
2641 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2642 "too many template headers for %qD "
2643 "(should be %d)",
2644 decl, wanted);
2645 if (warned && CLASS_TYPE_P (ctx)
2646 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2647 inform (DECL_SOURCE_LOCATION (decl),
2648 "members of an explicitly specialized class are defined "
2649 "without a template header");
2650 }
2651 }
2652
2653 /* An explicit specialization whose declarator-id or class-head-name is not
2654 qualified shall be declared in the nearest enclosing namespace of the
2655 template, or, if the namespace is inline (7.3.1), any namespace from its
2656 enclosing namespace set.
2657
2658 If the name declared in the explicit instantiation is an unqualified name,
2659 the explicit instantiation shall appear in the namespace where its template
2660 is declared or, if that namespace is inline (7.3.1), any namespace from its
2661 enclosing namespace set. */
2662
2663 void
2664 check_unqualified_spec_or_inst (tree t, location_t loc)
2665 {
2666 tree tmpl = most_general_template (t);
2667 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2668 && !is_nested_namespace (current_namespace,
2669 CP_DECL_CONTEXT (tmpl), true))
2670 {
2671 if (processing_specialization)
2672 permerror (loc, "explicit specialization of %qD outside its "
2673 "namespace must use a nested-name-specifier", tmpl);
2674 else if (processing_explicit_instantiation
2675 && cxx_dialect >= cxx11)
2676 /* This was allowed in C++98, so only pedwarn. */
2677 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2678 "outside its namespace must use a nested-name-"
2679 "specifier", tmpl);
2680 }
2681 }
2682
2683 /* Warn for a template specialization SPEC that is missing some of a set
2684 of function or type attributes that the template TEMPL is declared with.
2685 ATTRLIST is a list of additional attributes that SPEC should be taken
2686 to ultimately be declared with. */
2687
2688 static void
2689 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2690 {
2691 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2692 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2693
2694 /* Avoid warning if the difference between the primary and
2695 the specialization is not in one of the attributes below. */
2696 const char* const blacklist[] = {
2697 "alloc_align", "alloc_size", "assume_aligned", "format",
2698 "format_arg", "malloc", "nonnull", NULL
2699 };
2700
2701 /* Put together a list of the black listed attributes that the primary
2702 template is declared with that the specialization is not, in case
2703 it's not apparent from the most recent declaration of the primary. */
2704 pretty_printer str;
2705 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2706 blacklist, &str);
2707
2708 if (!nattrs)
2709 return;
2710
2711 auto_diagnostic_group d;
2712 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2713 "explicit specialization %q#D may be missing attributes",
2714 spec))
2715 inform (DECL_SOURCE_LOCATION (tmpl),
2716 nattrs > 1
2717 ? G_("missing primary template attributes %s")
2718 : G_("missing primary template attribute %s"),
2719 pp_formatted_text (&str));
2720 }
2721
2722 /* Check to see if the function just declared, as indicated in
2723 DECLARATOR, and in DECL, is a specialization of a function
2724 template. We may also discover that the declaration is an explicit
2725 instantiation at this point.
2726
2727 Returns DECL, or an equivalent declaration that should be used
2728 instead if all goes well. Issues an error message if something is
2729 amiss. Returns error_mark_node if the error is not easily
2730 recoverable.
2731
2732 FLAGS is a bitmask consisting of the following flags:
2733
2734 2: The function has a definition.
2735 4: The function is a friend.
2736
2737 The TEMPLATE_COUNT is the number of references to qualifying
2738 template classes that appeared in the name of the function. For
2739 example, in
2740
2741 template <class T> struct S { void f(); };
2742 void S<int>::f();
2743
2744 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2745 classes are not counted in the TEMPLATE_COUNT, so that in
2746
2747 template <class T> struct S {};
2748 template <> struct S<int> { void f(); }
2749 template <> void S<int>::f();
2750
2751 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2752 invalid; there should be no template <>.)
2753
2754 If the function is a specialization, it is marked as such via
2755 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2756 is set up correctly, and it is added to the list of specializations
2757 for that template. */
2758
2759 tree
2760 check_explicit_specialization (tree declarator,
2761 tree decl,
2762 int template_count,
2763 int flags,
2764 tree attrlist)
2765 {
2766 int have_def = flags & 2;
2767 int is_friend = flags & 4;
2768 bool is_concept = flags & 8;
2769 int specialization = 0;
2770 int explicit_instantiation = 0;
2771 int member_specialization = 0;
2772 tree ctype = DECL_CLASS_CONTEXT (decl);
2773 tree dname = DECL_NAME (decl);
2774 tmpl_spec_kind tsk;
2775
2776 if (is_friend)
2777 {
2778 if (!processing_specialization)
2779 tsk = tsk_none;
2780 else
2781 tsk = tsk_excessive_parms;
2782 }
2783 else
2784 tsk = current_tmpl_spec_kind (template_count);
2785
2786 switch (tsk)
2787 {
2788 case tsk_none:
2789 if (processing_specialization && !VAR_P (decl))
2790 {
2791 specialization = 1;
2792 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2793 }
2794 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2795 {
2796 if (is_friend)
2797 /* This could be something like:
2798
2799 template <class T> void f(T);
2800 class S { friend void f<>(int); } */
2801 specialization = 1;
2802 else
2803 {
2804 /* This case handles bogus declarations like template <>
2805 template <class T> void f<int>(); */
2806
2807 error_at (cp_expr_loc_or_input_loc (declarator),
2808 "template-id %qE in declaration of primary template",
2809 declarator);
2810 return decl;
2811 }
2812 }
2813 break;
2814
2815 case tsk_invalid_member_spec:
2816 /* The error has already been reported in
2817 check_specialization_scope. */
2818 return error_mark_node;
2819
2820 case tsk_invalid_expl_inst:
2821 error ("template parameter list used in explicit instantiation");
2822
2823 /* Fall through. */
2824
2825 case tsk_expl_inst:
2826 if (have_def)
2827 error ("definition provided for explicit instantiation");
2828
2829 explicit_instantiation = 1;
2830 break;
2831
2832 case tsk_excessive_parms:
2833 case tsk_insufficient_parms:
2834 if (tsk == tsk_excessive_parms)
2835 error ("too many template parameter lists in declaration of %qD",
2836 decl);
2837 else if (template_header_count)
2838 error("too few template parameter lists in declaration of %qD", decl);
2839 else
2840 error("explicit specialization of %qD must be introduced by "
2841 "%<template <>%>", decl);
2842
2843 /* Fall through. */
2844 case tsk_expl_spec:
2845 if (is_concept)
2846 error ("explicit specialization declared %<concept%>");
2847
2848 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2849 /* In cases like template<> constexpr bool v = true;
2850 We'll give an error in check_template_variable. */
2851 break;
2852
2853 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2854 if (ctype)
2855 member_specialization = 1;
2856 else
2857 specialization = 1;
2858 break;
2859
2860 case tsk_template:
2861 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2862 {
2863 /* This case handles bogus declarations like template <>
2864 template <class T> void f<int>(); */
2865
2866 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2867 error_at (cp_expr_loc_or_input_loc (declarator),
2868 "template-id %qE in declaration of primary template",
2869 declarator);
2870 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2871 {
2872 /* Partial specialization of variable template. */
2873 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2874 specialization = 1;
2875 goto ok;
2876 }
2877 else if (cxx_dialect < cxx14)
2878 error_at (cp_expr_loc_or_input_loc (declarator),
2879 "non-type partial specialization %qE "
2880 "is not allowed", declarator);
2881 else
2882 error_at (cp_expr_loc_or_input_loc (declarator),
2883 "non-class, non-variable partial specialization %qE "
2884 "is not allowed", declarator);
2885 return decl;
2886 ok:;
2887 }
2888
2889 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2890 /* This is a specialization of a member template, without
2891 specialization the containing class. Something like:
2892
2893 template <class T> struct S {
2894 template <class U> void f (U);
2895 };
2896 template <> template <class U> void S<int>::f(U) {}
2897
2898 That's a specialization -- but of the entire template. */
2899 specialization = 1;
2900 break;
2901
2902 default:
2903 gcc_unreachable ();
2904 }
2905
2906 if ((specialization || member_specialization)
2907 /* This doesn't apply to variable templates. */
2908 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2909 {
2910 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2911 for (; t; t = TREE_CHAIN (t))
2912 if (TREE_PURPOSE (t))
2913 {
2914 permerror (input_location,
2915 "default argument specified in explicit specialization");
2916 break;
2917 }
2918 }
2919
2920 if (specialization || member_specialization || explicit_instantiation)
2921 {
2922 tree tmpl = NULL_TREE;
2923 tree targs = NULL_TREE;
2924 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2925
2926 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2927 if (!was_template_id)
2928 {
2929 tree fns;
2930
2931 gcc_assert (identifier_p (declarator));
2932 if (ctype)
2933 fns = dname;
2934 else
2935 {
2936 /* If there is no class context, the explicit instantiation
2937 must be at namespace scope. */
2938 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2939
2940 /* Find the namespace binding, using the declaration
2941 context. */
2942 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2943 false, true);
2944 if (fns == error_mark_node)
2945 /* If lookup fails, look for a friend declaration so we can
2946 give a better diagnostic. */
2947 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2948 /*type*/false, /*complain*/true,
2949 /*hidden*/true);
2950
2951 if (fns == error_mark_node || !is_overloaded_fn (fns))
2952 {
2953 error ("%qD is not a template function", dname);
2954 fns = error_mark_node;
2955 }
2956 }
2957
2958 declarator = lookup_template_function (fns, NULL_TREE);
2959 }
2960
2961 if (declarator == error_mark_node)
2962 return error_mark_node;
2963
2964 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2965 {
2966 if (!explicit_instantiation)
2967 /* A specialization in class scope. This is invalid,
2968 but the error will already have been flagged by
2969 check_specialization_scope. */
2970 return error_mark_node;
2971 else
2972 {
2973 /* It's not valid to write an explicit instantiation in
2974 class scope, e.g.:
2975
2976 class C { template void f(); }
2977
2978 This case is caught by the parser. However, on
2979 something like:
2980
2981 template class C { void f(); };
2982
2983 (which is invalid) we can get here. The error will be
2984 issued later. */
2985 ;
2986 }
2987
2988 return decl;
2989 }
2990 else if (ctype != NULL_TREE
2991 && (identifier_p (TREE_OPERAND (declarator, 0))))
2992 {
2993 // We'll match variable templates in start_decl.
2994 if (VAR_P (decl))
2995 return decl;
2996
2997 /* Find the list of functions in ctype that have the same
2998 name as the declared function. */
2999 tree name = TREE_OPERAND (declarator, 0);
3000
3001 if (constructor_name_p (name, ctype))
3002 {
3003 if (DECL_CONSTRUCTOR_P (decl)
3004 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3005 : !CLASSTYPE_DESTRUCTOR (ctype))
3006 {
3007 /* From [temp.expl.spec]:
3008
3009 If such an explicit specialization for the member
3010 of a class template names an implicitly-declared
3011 special member function (clause _special_), the
3012 program is ill-formed.
3013
3014 Similar language is found in [temp.explicit]. */
3015 error ("specialization of implicitly-declared special member function");
3016 return error_mark_node;
3017 }
3018
3019 name = DECL_NAME (decl);
3020 }
3021
3022 /* For a type-conversion operator, We might be looking for
3023 `operator int' which will be a specialization of
3024 `operator T'. Grab all the conversion operators, and
3025 then select from them. */
3026 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3027 ? conv_op_identifier : name);
3028
3029 if (fns == NULL_TREE)
3030 {
3031 error ("no member function %qD declared in %qT", name, ctype);
3032 return error_mark_node;
3033 }
3034 else
3035 TREE_OPERAND (declarator, 0) = fns;
3036 }
3037
3038 /* Figure out what exactly is being specialized at this point.
3039 Note that for an explicit instantiation, even one for a
3040 member function, we cannot tell a priori whether the
3041 instantiation is for a member template, or just a member
3042 function of a template class. Even if a member template is
3043 being instantiated, the member template arguments may be
3044 elided if they can be deduced from the rest of the
3045 declaration. */
3046 tmpl = determine_specialization (declarator, decl,
3047 &targs,
3048 member_specialization,
3049 template_count,
3050 tsk);
3051
3052 if (!tmpl || tmpl == error_mark_node)
3053 /* We couldn't figure out what this declaration was
3054 specializing. */
3055 return error_mark_node;
3056 else
3057 {
3058 if (TREE_CODE (decl) == FUNCTION_DECL
3059 && DECL_HIDDEN_FRIEND_P (tmpl))
3060 {
3061 auto_diagnostic_group d;
3062 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3063 "friend declaration %qD is not visible to "
3064 "explicit specialization", tmpl))
3065 inform (DECL_SOURCE_LOCATION (tmpl),
3066 "friend declaration here");
3067 }
3068 else if (!ctype && !is_friend
3069 && CP_DECL_CONTEXT (decl) == current_namespace)
3070 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3071
3072 tree gen_tmpl = most_general_template (tmpl);
3073
3074 if (explicit_instantiation)
3075 {
3076 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3077 is done by do_decl_instantiation later. */
3078
3079 int arg_depth = TMPL_ARGS_DEPTH (targs);
3080 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3081
3082 if (arg_depth > parm_depth)
3083 {
3084 /* If TMPL is not the most general template (for
3085 example, if TMPL is a friend template that is
3086 injected into namespace scope), then there will
3087 be too many levels of TARGS. Remove some of them
3088 here. */
3089 int i;
3090 tree new_targs;
3091
3092 new_targs = make_tree_vec (parm_depth);
3093 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3094 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3095 = TREE_VEC_ELT (targs, i);
3096 targs = new_targs;
3097 }
3098
3099 return instantiate_template (tmpl, targs, tf_error);
3100 }
3101
3102 /* If we thought that the DECL was a member function, but it
3103 turns out to be specializing a static member function,
3104 make DECL a static member function as well. */
3105 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3106 && DECL_STATIC_FUNCTION_P (tmpl)
3107 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3108 revert_static_member_fn (decl);
3109
3110 /* If this is a specialization of a member template of a
3111 template class, we want to return the TEMPLATE_DECL, not
3112 the specialization of it. */
3113 if (tsk == tsk_template && !was_template_id)
3114 {
3115 tree result = DECL_TEMPLATE_RESULT (tmpl);
3116 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3117 DECL_INITIAL (result) = NULL_TREE;
3118 if (have_def)
3119 {
3120 tree parm;
3121 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3122 DECL_SOURCE_LOCATION (result)
3123 = DECL_SOURCE_LOCATION (decl);
3124 /* We want to use the argument list specified in the
3125 definition, not in the original declaration. */
3126 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3127 for (parm = DECL_ARGUMENTS (result); parm;
3128 parm = DECL_CHAIN (parm))
3129 DECL_CONTEXT (parm) = result;
3130 }
3131 return register_specialization (tmpl, gen_tmpl, targs,
3132 is_friend, 0);
3133 }
3134
3135 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3136 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3137
3138 if (was_template_id)
3139 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3140
3141 /* Inherit default function arguments from the template
3142 DECL is specializing. */
3143 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3144 copy_default_args_to_explicit_spec (decl);
3145
3146 /* This specialization has the same protection as the
3147 template it specializes. */
3148 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3149 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3150
3151 /* 7.1.1-1 [dcl.stc]
3152
3153 A storage-class-specifier shall not be specified in an
3154 explicit specialization...
3155
3156 The parser rejects these, so unless action is taken here,
3157 explicit function specializations will always appear with
3158 global linkage.
3159
3160 The action recommended by the C++ CWG in response to C++
3161 defect report 605 is to make the storage class and linkage
3162 of the explicit specialization match the templated function:
3163
3164 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3165 */
3166 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3167 {
3168 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3169 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3170
3171 /* A concept cannot be specialized. */
3172 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3173 {
3174 error ("explicit specialization of function concept %qD",
3175 gen_tmpl);
3176 return error_mark_node;
3177 }
3178
3179 /* This specialization has the same linkage and visibility as
3180 the function template it specializes. */
3181 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3182 if (! TREE_PUBLIC (decl))
3183 {
3184 DECL_INTERFACE_KNOWN (decl) = 1;
3185 DECL_NOT_REALLY_EXTERN (decl) = 1;
3186 }
3187 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3188 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3189 {
3190 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3191 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3192 }
3193 }
3194
3195 /* If DECL is a friend declaration, declared using an
3196 unqualified name, the namespace associated with DECL may
3197 have been set incorrectly. For example, in:
3198
3199 template <typename T> void f(T);
3200 namespace N {
3201 struct S { friend void f<int>(int); }
3202 }
3203
3204 we will have set the DECL_CONTEXT for the friend
3205 declaration to N, rather than to the global namespace. */
3206 if (DECL_NAMESPACE_SCOPE_P (decl))
3207 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3208
3209 if (is_friend && !have_def)
3210 /* This is not really a declaration of a specialization.
3211 It's just the name of an instantiation. But, it's not
3212 a request for an instantiation, either. */
3213 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3214 else if (TREE_CODE (decl) == FUNCTION_DECL)
3215 /* A specialization is not necessarily COMDAT. */
3216 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3217 && DECL_DECLARED_INLINE_P (decl));
3218 else if (VAR_P (decl))
3219 DECL_COMDAT (decl) = false;
3220
3221 /* If this is a full specialization, register it so that we can find
3222 it again. Partial specializations will be registered in
3223 process_partial_specialization. */
3224 if (!processing_template_decl)
3225 {
3226 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3227
3228 decl = register_specialization (decl, gen_tmpl, targs,
3229 is_friend, 0);
3230 }
3231
3232
3233 /* A 'structor should already have clones. */
3234 gcc_assert (decl == error_mark_node
3235 || variable_template_p (tmpl)
3236 || !(DECL_CONSTRUCTOR_P (decl)
3237 || DECL_DESTRUCTOR_P (decl))
3238 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3239 }
3240 }
3241
3242 return decl;
3243 }
3244
3245 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3246 parameters. These are represented in the same format used for
3247 DECL_TEMPLATE_PARMS. */
3248
3249 int
3250 comp_template_parms (const_tree parms1, const_tree parms2)
3251 {
3252 const_tree p1;
3253 const_tree p2;
3254
3255 if (parms1 == parms2)
3256 return 1;
3257
3258 for (p1 = parms1, p2 = parms2;
3259 p1 != NULL_TREE && p2 != NULL_TREE;
3260 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3261 {
3262 tree t1 = TREE_VALUE (p1);
3263 tree t2 = TREE_VALUE (p2);
3264 int i;
3265
3266 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3267 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3268
3269 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3270 return 0;
3271
3272 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3273 {
3274 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3275 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3276
3277 /* If either of the template parameters are invalid, assume
3278 they match for the sake of error recovery. */
3279 if (error_operand_p (parm1) || error_operand_p (parm2))
3280 return 1;
3281
3282 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3283 return 0;
3284
3285 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3286 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3287 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3288 continue;
3289 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3290 return 0;
3291 }
3292 }
3293
3294 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3295 /* One set of parameters has more parameters lists than the
3296 other. */
3297 return 0;
3298
3299 return 1;
3300 }
3301
3302 /* Returns true if two template parameters are declared with
3303 equivalent constraints. */
3304
3305 static bool
3306 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3307 {
3308 tree req1 = TREE_TYPE (parm1);
3309 tree req2 = TREE_TYPE (parm2);
3310 if (!req1 != !req2)
3311 return false;
3312 if (req1)
3313 return cp_tree_equal (req1, req2);
3314 return true;
3315 }
3316
3317 /* Returns true when two template parameters are equivalent. */
3318
3319 static bool
3320 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3321 {
3322 tree decl1 = TREE_VALUE (parm1);
3323 tree decl2 = TREE_VALUE (parm2);
3324
3325 /* If either of the template parameters are invalid, assume
3326 they match for the sake of error recovery. */
3327 if (error_operand_p (decl1) || error_operand_p (decl2))
3328 return true;
3329
3330 /* ... they declare parameters of the same kind. */
3331 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3332 return false;
3333
3334 /* ... one parameter was introduced by a parameter declaration, then
3335 both are. This case arises as a result of eagerly rewriting declarations
3336 during parsing. */
3337 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3338 return false;
3339
3340 /* ... if either declares a pack, they both do. */
3341 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3342 return false;
3343
3344 if (TREE_CODE (decl1) == PARM_DECL)
3345 {
3346 /* ... if they declare non-type parameters, the types are equivalent. */
3347 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3348 return false;
3349 }
3350 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3351 {
3352 /* ... if they declare template template parameters, their template
3353 parameter lists are equivalent. */
3354 if (!template_heads_equivalent_p (decl1, decl2))
3355 return false;
3356 }
3357
3358 /* ... if they are declared with a qualified-concept name, they both
3359 are, and those names are equivalent. */
3360 return template_parameter_constraints_equivalent_p (parm1, parm2);
3361 }
3362
3363 /* Returns true if two template parameters lists are equivalent.
3364 Two template parameter lists are equivalent if they have the
3365 same length and their corresponding parameters are equivalent.
3366
3367 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3368 data structure returned by DECL_TEMPLATE_PARMS.
3369
3370 This is generally the same implementation as comp_template_parms
3371 except that it also the concept names and arguments used to
3372 introduce parameters. */
3373
3374 static bool
3375 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3376 {
3377 if (parms1 == parms2)
3378 return true;
3379
3380 const_tree p1 = parms1;
3381 const_tree p2 = parms2;
3382 while (p1 != NULL_TREE && p2 != NULL_TREE)
3383 {
3384 tree list1 = TREE_VALUE (p1);
3385 tree list2 = TREE_VALUE (p2);
3386
3387 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3388 return 0;
3389
3390 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3391 {
3392 tree parm1 = TREE_VEC_ELT (list1, i);
3393 tree parm2 = TREE_VEC_ELT (list2, i);
3394 if (!template_parameters_equivalent_p (parm1, parm2))
3395 return false;
3396 }
3397
3398 p1 = TREE_CHAIN (p1);
3399 p2 = TREE_CHAIN (p2);
3400 }
3401
3402 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3403 return false;
3404
3405 return true;
3406 }
3407
3408 /* Return true if the requires-clause of the template parameter lists are
3409 equivalent and false otherwise. */
3410 static bool
3411 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3412 {
3413 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3414 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3415 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3416 return false;
3417 if (!cp_tree_equal (req1, req2))
3418 return false;
3419 return true;
3420 }
3421
3422 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3423 Two template heads are equivalent if their template parameter
3424 lists are equivalent and their requires clauses are equivalent.
3425
3426 In pre-C++20, this is equivalent to calling comp_template_parms
3427 for the template parameters of TMPL1 and TMPL2. */
3428
3429 bool
3430 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3431 {
3432 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3433 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3434
3435 /* Don't change the matching rules for pre-C++20. */
3436 if (cxx_dialect < cxx2a)
3437 return comp_template_parms (parms1, parms2);
3438
3439 /* ... have the same number of template parameters, and their
3440 corresponding parameters are equivalent. */
3441 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3442 return false;
3443
3444 /* ... if either has a requires-clause, they both do and their
3445 corresponding constraint-expressions are equivalent. */
3446 return template_requirements_equivalent_p (parms1, parms2);
3447 }
3448
3449 /* Determine whether PARM is a parameter pack. */
3450
3451 bool
3452 template_parameter_pack_p (const_tree parm)
3453 {
3454 /* Determine if we have a non-type template parameter pack. */
3455 if (TREE_CODE (parm) == PARM_DECL)
3456 return (DECL_TEMPLATE_PARM_P (parm)
3457 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3458 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3459 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3460
3461 /* If this is a list of template parameters, we could get a
3462 TYPE_DECL or a TEMPLATE_DECL. */
3463 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3464 parm = TREE_TYPE (parm);
3465
3466 /* Otherwise it must be a type template parameter. */
3467 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3468 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3469 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3470 }
3471
3472 /* Determine if T is a function parameter pack. */
3473
3474 bool
3475 function_parameter_pack_p (const_tree t)
3476 {
3477 if (t && TREE_CODE (t) == PARM_DECL)
3478 return DECL_PACK_P (t);
3479 return false;
3480 }
3481
3482 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3483 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3484
3485 tree
3486 get_function_template_decl (const_tree primary_func_tmpl_inst)
3487 {
3488 if (! primary_func_tmpl_inst
3489 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3490 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3491 return NULL;
3492
3493 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3494 }
3495
3496 /* Return true iff the function parameter PARAM_DECL was expanded
3497 from the function parameter pack PACK. */
3498
3499 bool
3500 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3501 {
3502 if (DECL_ARTIFICIAL (param_decl)
3503 || !function_parameter_pack_p (pack))
3504 return false;
3505
3506 /* The parameter pack and its pack arguments have the same
3507 DECL_PARM_INDEX. */
3508 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3509 }
3510
3511 /* Determine whether ARGS describes a variadic template args list,
3512 i.e., one that is terminated by a template argument pack. */
3513
3514 static bool
3515 template_args_variadic_p (tree args)
3516 {
3517 int nargs;
3518 tree last_parm;
3519
3520 if (args == NULL_TREE)
3521 return false;
3522
3523 args = INNERMOST_TEMPLATE_ARGS (args);
3524 nargs = TREE_VEC_LENGTH (args);
3525
3526 if (nargs == 0)
3527 return false;
3528
3529 last_parm = TREE_VEC_ELT (args, nargs - 1);
3530
3531 return ARGUMENT_PACK_P (last_parm);
3532 }
3533
3534 /* Generate a new name for the parameter pack name NAME (an
3535 IDENTIFIER_NODE) that incorporates its */
3536
3537 static tree
3538 make_ith_pack_parameter_name (tree name, int i)
3539 {
3540 /* Munge the name to include the parameter index. */
3541 #define NUMBUF_LEN 128
3542 char numbuf[NUMBUF_LEN];
3543 char* newname;
3544 int newname_len;
3545
3546 if (name == NULL_TREE)
3547 return name;
3548 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3549 newname_len = IDENTIFIER_LENGTH (name)
3550 + strlen (numbuf) + 2;
3551 newname = (char*)alloca (newname_len);
3552 snprintf (newname, newname_len,
3553 "%s#%i", IDENTIFIER_POINTER (name), i);
3554 return get_identifier (newname);
3555 }
3556
3557 /* Return true if T is a primary function, class or alias template
3558 specialization, not including the template pattern. */
3559
3560 bool
3561 primary_template_specialization_p (const_tree t)
3562 {
3563 if (!t)
3564 return false;
3565
3566 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3567 return (DECL_LANG_SPECIFIC (t)
3568 && DECL_USE_TEMPLATE (t)
3569 && DECL_TEMPLATE_INFO (t)
3570 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3571 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3572 return (CLASSTYPE_TEMPLATE_INFO (t)
3573 && CLASSTYPE_USE_TEMPLATE (t)
3574 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3575 else if (alias_template_specialization_p (t))
3576 return true;
3577 return false;
3578 }
3579
3580 /* Return true if PARM is a template template parameter. */
3581
3582 bool
3583 template_template_parameter_p (const_tree parm)
3584 {
3585 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3586 }
3587
3588 /* Return true iff PARM is a DECL representing a type template
3589 parameter. */
3590
3591 bool
3592 template_type_parameter_p (const_tree parm)
3593 {
3594 return (parm
3595 && (TREE_CODE (parm) == TYPE_DECL
3596 || TREE_CODE (parm) == TEMPLATE_DECL)
3597 && DECL_TEMPLATE_PARM_P (parm));
3598 }
3599
3600 /* Return the template parameters of T if T is a
3601 primary template instantiation, NULL otherwise. */
3602
3603 tree
3604 get_primary_template_innermost_parameters (const_tree t)
3605 {
3606 tree parms = NULL, template_info = NULL;
3607
3608 if ((template_info = get_template_info (t))
3609 && primary_template_specialization_p (t))
3610 parms = INNERMOST_TEMPLATE_PARMS
3611 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3612
3613 return parms;
3614 }
3615
3616 /* Return the template parameters of the LEVELth level from the full list
3617 of template parameters PARMS. */
3618
3619 tree
3620 get_template_parms_at_level (tree parms, int level)
3621 {
3622 tree p;
3623 if (!parms
3624 || TREE_CODE (parms) != TREE_LIST
3625 || level > TMPL_PARMS_DEPTH (parms))
3626 return NULL_TREE;
3627
3628 for (p = parms; p; p = TREE_CHAIN (p))
3629 if (TMPL_PARMS_DEPTH (p) == level)
3630 return p;
3631
3632 return NULL_TREE;
3633 }
3634
3635 /* Returns the template arguments of T if T is a template instantiation,
3636 NULL otherwise. */
3637
3638 tree
3639 get_template_innermost_arguments (const_tree t)
3640 {
3641 tree args = NULL, template_info = NULL;
3642
3643 if ((template_info = get_template_info (t))
3644 && TI_ARGS (template_info))
3645 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3646
3647 return args;
3648 }
3649
3650 /* Return the argument pack elements of T if T is a template argument pack,
3651 NULL otherwise. */
3652
3653 tree
3654 get_template_argument_pack_elems (const_tree t)
3655 {
3656 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3657 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3658 return NULL;
3659
3660 return ARGUMENT_PACK_ARGS (t);
3661 }
3662
3663 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3664 ARGUMENT_PACK_SELECT represents. */
3665
3666 static tree
3667 argument_pack_select_arg (tree t)
3668 {
3669 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3670 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3671
3672 /* If the selected argument is an expansion E, that most likely means we were
3673 called from gen_elem_of_pack_expansion_instantiation during the
3674 substituting of an argument pack (of which the Ith element is a pack
3675 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3676 In this case, the Ith element resulting from this substituting is going to
3677 be a pack expansion, which pattern is the pattern of E. Let's return the
3678 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3679 resulting pack expansion from it. */
3680 if (PACK_EXPANSION_P (arg))
3681 {
3682 /* Make sure we aren't throwing away arg info. */
3683 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3684 arg = PACK_EXPANSION_PATTERN (arg);
3685 }
3686
3687 return arg;
3688 }
3689
3690
3691 /* True iff FN is a function representing a built-in variadic parameter
3692 pack. */
3693
3694 bool
3695 builtin_pack_fn_p (tree fn)
3696 {
3697 if (!fn
3698 || TREE_CODE (fn) != FUNCTION_DECL
3699 || !DECL_IS_BUILTIN (fn))
3700 return false;
3701
3702 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3703 return true;
3704
3705 return false;
3706 }
3707
3708 /* True iff CALL is a call to a function representing a built-in variadic
3709 parameter pack. */
3710
3711 static bool
3712 builtin_pack_call_p (tree call)
3713 {
3714 if (TREE_CODE (call) != CALL_EXPR)
3715 return false;
3716 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3717 }
3718
3719 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3720
3721 static tree
3722 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3723 tree in_decl)
3724 {
3725 tree ohi = CALL_EXPR_ARG (call, 0);
3726 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3727 false/*fn*/, true/*int_cst*/);
3728
3729 if (value_dependent_expression_p (hi))
3730 {
3731 if (hi != ohi)
3732 {
3733 call = copy_node (call);
3734 CALL_EXPR_ARG (call, 0) = hi;
3735 }
3736 tree ex = make_pack_expansion (call, complain);
3737 tree vec = make_tree_vec (1);
3738 TREE_VEC_ELT (vec, 0) = ex;
3739 return vec;
3740 }
3741 else
3742 {
3743 hi = cxx_constant_value (hi);
3744 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3745
3746 /* Calculate the largest value of len that won't make the size of the vec
3747 overflow an int. The compiler will exceed resource limits long before
3748 this, but it seems a decent place to diagnose. */
3749 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3750
3751 if (len < 0 || len > max)
3752 {
3753 if ((complain & tf_error)
3754 && hi != error_mark_node)
3755 error ("argument to %<__integer_pack%> must be between 0 and %d",
3756 max);
3757 return error_mark_node;
3758 }
3759
3760 tree vec = make_tree_vec (len);
3761
3762 for (int i = 0; i < len; ++i)
3763 TREE_VEC_ELT (vec, i) = size_int (i);
3764
3765 return vec;
3766 }
3767 }
3768
3769 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3770 CALL. */
3771
3772 static tree
3773 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3774 tree in_decl)
3775 {
3776 if (!builtin_pack_call_p (call))
3777 return NULL_TREE;
3778
3779 tree fn = CALL_EXPR_FN (call);
3780
3781 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3782 return expand_integer_pack (call, args, complain, in_decl);
3783
3784 return NULL_TREE;
3785 }
3786
3787 /* Structure used to track the progress of find_parameter_packs_r. */
3788 struct find_parameter_pack_data
3789 {
3790 /* TREE_LIST that will contain all of the parameter packs found by
3791 the traversal. */
3792 tree* parameter_packs;
3793
3794 /* Set of AST nodes that have been visited by the traversal. */
3795 hash_set<tree> *visited;
3796
3797 /* True iff we're making a type pack expansion. */
3798 bool type_pack_expansion_p;
3799 };
3800
3801 /* Identifies all of the argument packs that occur in a template
3802 argument and appends them to the TREE_LIST inside DATA, which is a
3803 find_parameter_pack_data structure. This is a subroutine of
3804 make_pack_expansion and uses_parameter_packs. */
3805 static tree
3806 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3807 {
3808 tree t = *tp;
3809 struct find_parameter_pack_data* ppd =
3810 (struct find_parameter_pack_data*)data;
3811 bool parameter_pack_p = false;
3812
3813 /* Handle type aliases/typedefs. */
3814 if (TYPE_ALIAS_P (t))
3815 {
3816 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3817 cp_walk_tree (&TI_ARGS (tinfo),
3818 &find_parameter_packs_r,
3819 ppd, ppd->visited);
3820 *walk_subtrees = 0;
3821 return NULL_TREE;
3822 }
3823
3824 /* Identify whether this is a parameter pack or not. */
3825 switch (TREE_CODE (t))
3826 {
3827 case TEMPLATE_PARM_INDEX:
3828 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3829 parameter_pack_p = true;
3830 break;
3831
3832 case TEMPLATE_TYPE_PARM:
3833 t = TYPE_MAIN_VARIANT (t);
3834 /* FALLTHRU */
3835 case TEMPLATE_TEMPLATE_PARM:
3836 /* If the placeholder appears in the decl-specifier-seq of a function
3837 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3838 is a pack expansion, the invented template parameter is a template
3839 parameter pack. */
3840 if (ppd->type_pack_expansion_p && is_auto (t))
3841 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3842 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3843 parameter_pack_p = true;
3844 break;
3845
3846 case FIELD_DECL:
3847 case PARM_DECL:
3848 if (DECL_PACK_P (t))
3849 {
3850 /* We don't want to walk into the type of a PARM_DECL,
3851 because we don't want to see the type parameter pack. */
3852 *walk_subtrees = 0;
3853 parameter_pack_p = true;
3854 }
3855 break;
3856
3857 case VAR_DECL:
3858 if (DECL_PACK_P (t))
3859 {
3860 /* We don't want to walk into the type of a variadic capture proxy,
3861 because we don't want to see the type parameter pack. */
3862 *walk_subtrees = 0;
3863 parameter_pack_p = true;
3864 }
3865 else if (variable_template_specialization_p (t))
3866 {
3867 cp_walk_tree (&DECL_TI_ARGS (t),
3868 find_parameter_packs_r,
3869 ppd, ppd->visited);
3870 *walk_subtrees = 0;
3871 }
3872 break;
3873
3874 case CALL_EXPR:
3875 if (builtin_pack_call_p (t))
3876 parameter_pack_p = true;
3877 break;
3878
3879 case BASES:
3880 parameter_pack_p = true;
3881 break;
3882 default:
3883 /* Not a parameter pack. */
3884 break;
3885 }
3886
3887 if (parameter_pack_p)
3888 {
3889 /* Add this parameter pack to the list. */
3890 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3891 }
3892
3893 if (TYPE_P (t))
3894 cp_walk_tree (&TYPE_CONTEXT (t),
3895 &find_parameter_packs_r, ppd, ppd->visited);
3896
3897 /* This switch statement will return immediately if we don't find a
3898 parameter pack. */
3899 switch (TREE_CODE (t))
3900 {
3901 case TEMPLATE_PARM_INDEX:
3902 return NULL_TREE;
3903
3904 case BOUND_TEMPLATE_TEMPLATE_PARM:
3905 /* Check the template itself. */
3906 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3907 &find_parameter_packs_r, ppd, ppd->visited);
3908 /* Check the template arguments. */
3909 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3910 ppd->visited);
3911 *walk_subtrees = 0;
3912 return NULL_TREE;
3913
3914 case TEMPLATE_TYPE_PARM:
3915 case TEMPLATE_TEMPLATE_PARM:
3916 return NULL_TREE;
3917
3918 case PARM_DECL:
3919 return NULL_TREE;
3920
3921 case DECL_EXPR:
3922 /* Ignore the declaration of a capture proxy for a parameter pack. */
3923 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3924 *walk_subtrees = 0;
3925 return NULL_TREE;
3926
3927 case RECORD_TYPE:
3928 if (TYPE_PTRMEMFUNC_P (t))
3929 return NULL_TREE;
3930 /* Fall through. */
3931
3932 case UNION_TYPE:
3933 case ENUMERAL_TYPE:
3934 if (TYPE_TEMPLATE_INFO (t))
3935 cp_walk_tree (&TYPE_TI_ARGS (t),
3936 &find_parameter_packs_r, ppd, ppd->visited);
3937
3938 *walk_subtrees = 0;
3939 return NULL_TREE;
3940
3941 case TEMPLATE_DECL:
3942 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3943 return NULL_TREE;
3944 gcc_fallthrough();
3945
3946 case CONSTRUCTOR:
3947 cp_walk_tree (&TREE_TYPE (t),
3948 &find_parameter_packs_r, ppd, ppd->visited);
3949 return NULL_TREE;
3950
3951 case TYPENAME_TYPE:
3952 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3953 ppd, ppd->visited);
3954 *walk_subtrees = 0;
3955 return NULL_TREE;
3956
3957 case TYPE_PACK_EXPANSION:
3958 case EXPR_PACK_EXPANSION:
3959 *walk_subtrees = 0;
3960 return NULL_TREE;
3961
3962 case INTEGER_TYPE:
3963 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3964 ppd, ppd->visited);
3965 *walk_subtrees = 0;
3966 return NULL_TREE;
3967
3968 case IDENTIFIER_NODE:
3969 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3970 ppd->visited);
3971 *walk_subtrees = 0;
3972 return NULL_TREE;
3973
3974 case LAMBDA_EXPR:
3975 {
3976 /* Look at explicit captures. */
3977 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3978 cap; cap = TREE_CHAIN (cap))
3979 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3980 ppd->visited);
3981 /* Since we defer implicit capture, look in the parms and body. */
3982 tree fn = lambda_function (t);
3983 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3984 ppd->visited);
3985 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3986 ppd->visited);
3987 *walk_subtrees = 0;
3988 return NULL_TREE;
3989 }
3990
3991 case DECLTYPE_TYPE:
3992 {
3993 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3994 type_pack_expansion_p to false so that any placeholders
3995 within the expression don't get marked as parameter packs. */
3996 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3997 ppd->type_pack_expansion_p = false;
3998 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3999 ppd, ppd->visited);
4000 ppd->type_pack_expansion_p = type_pack_expansion_p;
4001 *walk_subtrees = 0;
4002 return NULL_TREE;
4003 }
4004
4005 case IF_STMT:
4006 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4007 ppd, ppd->visited);
4008 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4009 ppd, ppd->visited);
4010 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4011 ppd, ppd->visited);
4012 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4013 *walk_subtrees = 0;
4014 return NULL_TREE;
4015
4016 default:
4017 return NULL_TREE;
4018 }
4019
4020 return NULL_TREE;
4021 }
4022
4023 /* Determines if the expression or type T uses any parameter packs. */
4024 tree
4025 uses_parameter_packs (tree t)
4026 {
4027 tree parameter_packs = NULL_TREE;
4028 struct find_parameter_pack_data ppd;
4029 ppd.parameter_packs = &parameter_packs;
4030 ppd.visited = new hash_set<tree>;
4031 ppd.type_pack_expansion_p = false;
4032 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4033 delete ppd.visited;
4034 return parameter_packs;
4035 }
4036
4037 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4038 representation a base-class initializer into a parameter pack
4039 expansion. If all goes well, the resulting node will be an
4040 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4041 respectively. */
4042 tree
4043 make_pack_expansion (tree arg, tsubst_flags_t complain)
4044 {
4045 tree result;
4046 tree parameter_packs = NULL_TREE;
4047 bool for_types = false;
4048 struct find_parameter_pack_data ppd;
4049
4050 if (!arg || arg == error_mark_node)
4051 return arg;
4052
4053 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4054 {
4055 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4056 class initializer. In this case, the TREE_PURPOSE will be a
4057 _TYPE node (representing the base class expansion we're
4058 initializing) and the TREE_VALUE will be a TREE_LIST
4059 containing the initialization arguments.
4060
4061 The resulting expansion looks somewhat different from most
4062 expansions. Rather than returning just one _EXPANSION, we
4063 return a TREE_LIST whose TREE_PURPOSE is a
4064 TYPE_PACK_EXPANSION containing the bases that will be
4065 initialized. The TREE_VALUE will be identical to the
4066 original TREE_VALUE, which is a list of arguments that will
4067 be passed to each base. We do not introduce any new pack
4068 expansion nodes into the TREE_VALUE (although it is possible
4069 that some already exist), because the TREE_PURPOSE and
4070 TREE_VALUE all need to be expanded together with the same
4071 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4072 resulting TREE_PURPOSE will mention the parameter packs in
4073 both the bases and the arguments to the bases. */
4074 tree purpose;
4075 tree value;
4076 tree parameter_packs = NULL_TREE;
4077
4078 /* Determine which parameter packs will be used by the base
4079 class expansion. */
4080 ppd.visited = new hash_set<tree>;
4081 ppd.parameter_packs = &parameter_packs;
4082 ppd.type_pack_expansion_p = false;
4083 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4084 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4085 &ppd, ppd.visited);
4086
4087 if (parameter_packs == NULL_TREE)
4088 {
4089 if (complain & tf_error)
4090 error ("base initializer expansion %qT contains no parameter packs",
4091 arg);
4092 delete ppd.visited;
4093 return error_mark_node;
4094 }
4095
4096 if (TREE_VALUE (arg) != void_type_node)
4097 {
4098 /* Collect the sets of parameter packs used in each of the
4099 initialization arguments. */
4100 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4101 {
4102 /* Determine which parameter packs will be expanded in this
4103 argument. */
4104 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4105 &ppd, ppd.visited);
4106 }
4107 }
4108
4109 delete ppd.visited;
4110
4111 /* Create the pack expansion type for the base type. */
4112 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4113 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4114 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4115 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4116
4117 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4118 they will rarely be compared to anything. */
4119 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4120
4121 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4122 }
4123
4124 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4125 for_types = true;
4126
4127 /* Build the PACK_EXPANSION_* node. */
4128 result = for_types
4129 ? cxx_make_type (TYPE_PACK_EXPANSION)
4130 : make_node (EXPR_PACK_EXPANSION);
4131 SET_PACK_EXPANSION_PATTERN (result, arg);
4132 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4133 {
4134 /* Propagate type and const-expression information. */
4135 TREE_TYPE (result) = TREE_TYPE (arg);
4136 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4137 /* Mark this read now, since the expansion might be length 0. */
4138 mark_exp_read (arg);
4139 }
4140 else
4141 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4142 they will rarely be compared to anything. */
4143 SET_TYPE_STRUCTURAL_EQUALITY (result);
4144
4145 /* Determine which parameter packs will be expanded. */
4146 ppd.parameter_packs = &parameter_packs;
4147 ppd.visited = new hash_set<tree>;
4148 ppd.type_pack_expansion_p = TYPE_P (arg);
4149 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4150 delete ppd.visited;
4151
4152 /* Make sure we found some parameter packs. */
4153 if (parameter_packs == NULL_TREE)
4154 {
4155 if (complain & tf_error)
4156 {
4157 if (TYPE_P (arg))
4158 error ("expansion pattern %qT contains no parameter packs", arg);
4159 else
4160 error ("expansion pattern %qE contains no parameter packs", arg);
4161 }
4162 return error_mark_node;
4163 }
4164 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4165
4166 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4167
4168 return result;
4169 }
4170
4171 /* Checks T for any "bare" parameter packs, which have not yet been
4172 expanded, and issues an error if any are found. This operation can
4173 only be done on full expressions or types (e.g., an expression
4174 statement, "if" condition, etc.), because we could have expressions like:
4175
4176 foo(f(g(h(args)))...)
4177
4178 where "args" is a parameter pack. check_for_bare_parameter_packs
4179 should not be called for the subexpressions args, h(args),
4180 g(h(args)), or f(g(h(args))), because we would produce erroneous
4181 error messages.
4182
4183 Returns TRUE and emits an error if there were bare parameter packs,
4184 returns FALSE otherwise. */
4185 bool
4186 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4187 {
4188 tree parameter_packs = NULL_TREE;
4189 struct find_parameter_pack_data ppd;
4190
4191 if (!processing_template_decl || !t || t == error_mark_node)
4192 return false;
4193
4194 /* A lambda might use a parameter pack from the containing context. */
4195 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4196 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4197 return false;
4198
4199 if (TREE_CODE (t) == TYPE_DECL)
4200 t = TREE_TYPE (t);
4201
4202 ppd.parameter_packs = &parameter_packs;
4203 ppd.visited = new hash_set<tree>;
4204 ppd.type_pack_expansion_p = false;
4205 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4206 delete ppd.visited;
4207
4208 if (parameter_packs)
4209 {
4210 if (loc == UNKNOWN_LOCATION)
4211 loc = cp_expr_loc_or_input_loc (t);
4212 error_at (loc, "parameter packs not expanded with %<...%>:");
4213 while (parameter_packs)
4214 {
4215 tree pack = TREE_VALUE (parameter_packs);
4216 tree name = NULL_TREE;
4217
4218 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4219 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4220 name = TYPE_NAME (pack);
4221 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4222 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4223 else if (TREE_CODE (pack) == CALL_EXPR)
4224 name = DECL_NAME (CALL_EXPR_FN (pack));
4225 else
4226 name = DECL_NAME (pack);
4227
4228 if (name)
4229 inform (loc, " %qD", name);
4230 else
4231 inform (loc, " %s", "<anonymous>");
4232
4233 parameter_packs = TREE_CHAIN (parameter_packs);
4234 }
4235
4236 return true;
4237 }
4238
4239 return false;
4240 }
4241
4242 /* Expand any parameter packs that occur in the template arguments in
4243 ARGS. */
4244 tree
4245 expand_template_argument_pack (tree args)
4246 {
4247 if (args == error_mark_node)
4248 return error_mark_node;
4249
4250 tree result_args = NULL_TREE;
4251 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4252 int num_result_args = -1;
4253 int non_default_args_count = -1;
4254
4255 /* First, determine if we need to expand anything, and the number of
4256 slots we'll need. */
4257 for (in_arg = 0; in_arg < nargs; ++in_arg)
4258 {
4259 tree arg = TREE_VEC_ELT (args, in_arg);
4260 if (arg == NULL_TREE)
4261 return args;
4262 if (ARGUMENT_PACK_P (arg))
4263 {
4264 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4265 if (num_result_args < 0)
4266 num_result_args = in_arg + num_packed;
4267 else
4268 num_result_args += num_packed;
4269 }
4270 else
4271 {
4272 if (num_result_args >= 0)
4273 num_result_args++;
4274 }
4275 }
4276
4277 /* If no expansion is necessary, we're done. */
4278 if (num_result_args < 0)
4279 return args;
4280
4281 /* Expand arguments. */
4282 result_args = make_tree_vec (num_result_args);
4283 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4284 non_default_args_count =
4285 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4286 for (in_arg = 0; in_arg < nargs; ++in_arg)
4287 {
4288 tree arg = TREE_VEC_ELT (args, in_arg);
4289 if (ARGUMENT_PACK_P (arg))
4290 {
4291 tree packed = ARGUMENT_PACK_ARGS (arg);
4292 int i, num_packed = TREE_VEC_LENGTH (packed);
4293 for (i = 0; i < num_packed; ++i, ++out_arg)
4294 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4295 if (non_default_args_count > 0)
4296 non_default_args_count += num_packed - 1;
4297 }
4298 else
4299 {
4300 TREE_VEC_ELT (result_args, out_arg) = arg;
4301 ++out_arg;
4302 }
4303 }
4304 if (non_default_args_count >= 0)
4305 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4306 return result_args;
4307 }
4308
4309 /* Checks if DECL shadows a template parameter.
4310
4311 [temp.local]: A template-parameter shall not be redeclared within its
4312 scope (including nested scopes).
4313
4314 Emits an error and returns TRUE if the DECL shadows a parameter,
4315 returns FALSE otherwise. */
4316
4317 bool
4318 check_template_shadow (tree decl)
4319 {
4320 tree olddecl;
4321
4322 /* If we're not in a template, we can't possibly shadow a template
4323 parameter. */
4324 if (!current_template_parms)
4325 return true;
4326
4327 /* Figure out what we're shadowing. */
4328 decl = OVL_FIRST (decl);
4329 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4330
4331 /* If there's no previous binding for this name, we're not shadowing
4332 anything, let alone a template parameter. */
4333 if (!olddecl)
4334 return true;
4335
4336 /* If we're not shadowing a template parameter, we're done. Note
4337 that OLDDECL might be an OVERLOAD (or perhaps even an
4338 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4339 node. */
4340 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4341 return true;
4342
4343 /* We check for decl != olddecl to avoid bogus errors for using a
4344 name inside a class. We check TPFI to avoid duplicate errors for
4345 inline member templates. */
4346 if (decl == olddecl
4347 || (DECL_TEMPLATE_PARM_P (decl)
4348 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4349 return true;
4350
4351 /* Don't complain about the injected class name, as we've already
4352 complained about the class itself. */
4353 if (DECL_SELF_REFERENCE_P (decl))
4354 return false;
4355
4356 if (DECL_TEMPLATE_PARM_P (decl))
4357 error ("declaration of template parameter %q+D shadows "
4358 "template parameter", decl);
4359 else
4360 error ("declaration of %q+#D shadows template parameter", decl);
4361 inform (DECL_SOURCE_LOCATION (olddecl),
4362 "template parameter %qD declared here", olddecl);
4363 return false;
4364 }
4365
4366 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4367 ORIG_LEVEL, DECL, and TYPE. */
4368
4369 static tree
4370 build_template_parm_index (int index,
4371 int level,
4372 int orig_level,
4373 tree decl,
4374 tree type)
4375 {
4376 tree t = make_node (TEMPLATE_PARM_INDEX);
4377 TEMPLATE_PARM_IDX (t) = index;
4378 TEMPLATE_PARM_LEVEL (t) = level;
4379 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4380 TEMPLATE_PARM_DECL (t) = decl;
4381 TREE_TYPE (t) = type;
4382 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4383 TREE_READONLY (t) = TREE_READONLY (decl);
4384
4385 return t;
4386 }
4387
4388 /* Find the canonical type parameter for the given template type
4389 parameter. Returns the canonical type parameter, which may be TYPE
4390 if no such parameter existed. */
4391
4392 static tree
4393 canonical_type_parameter (tree type)
4394 {
4395 tree list;
4396 int idx = TEMPLATE_TYPE_IDX (type);
4397 if (!canonical_template_parms)
4398 vec_alloc (canonical_template_parms, idx + 1);
4399
4400 if (canonical_template_parms->length () <= (unsigned) idx)
4401 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4402
4403 list = (*canonical_template_parms)[idx];
4404 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4405 list = TREE_CHAIN (list);
4406
4407 if (list)
4408 return TREE_VALUE (list);
4409 else
4410 {
4411 (*canonical_template_parms)[idx]
4412 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4413 return type;
4414 }
4415 }
4416
4417 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4418 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4419 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4420 new one is created. */
4421
4422 static tree
4423 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4424 tsubst_flags_t complain)
4425 {
4426 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4427 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4428 != TEMPLATE_PARM_LEVEL (index) - levels)
4429 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4430 {
4431 tree orig_decl = TEMPLATE_PARM_DECL (index);
4432
4433 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4434 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4435 type);
4436 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4437 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4438 DECL_ARTIFICIAL (decl) = 1;
4439 SET_DECL_TEMPLATE_PARM_P (decl);
4440
4441 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4442 TEMPLATE_PARM_LEVEL (index) - levels,
4443 TEMPLATE_PARM_ORIG_LEVEL (index),
4444 decl, type);
4445 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4446 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4447 = TEMPLATE_PARM_PARAMETER_PACK (index);
4448
4449 /* Template template parameters need this. */
4450 tree inner = decl;
4451 if (TREE_CODE (decl) == TEMPLATE_DECL)
4452 {
4453 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4454 TYPE_DECL, DECL_NAME (decl), type);
4455 DECL_TEMPLATE_RESULT (decl) = inner;
4456 DECL_ARTIFICIAL (inner) = true;
4457 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4458 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4459 }
4460
4461 /* Attach the TPI to the decl. */
4462 if (TREE_CODE (inner) == TYPE_DECL)
4463 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4464 else
4465 DECL_INITIAL (decl) = tpi;
4466 }
4467
4468 return TEMPLATE_PARM_DESCENDANTS (index);
4469 }
4470
4471 /* Process information from new template parameter PARM and append it
4472 to the LIST being built. This new parameter is a non-type
4473 parameter iff IS_NON_TYPE is true. This new parameter is a
4474 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4475 is in PARM_LOC. */
4476
4477 tree
4478 process_template_parm (tree list, location_t parm_loc, tree parm,
4479 bool is_non_type, bool is_parameter_pack)
4480 {
4481 tree decl = 0;
4482 int idx = 0;
4483
4484 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4485 tree defval = TREE_PURPOSE (parm);
4486 tree constr = TREE_TYPE (parm);
4487
4488 if (list)
4489 {
4490 tree p = tree_last (list);
4491
4492 if (p && TREE_VALUE (p) != error_mark_node)
4493 {
4494 p = TREE_VALUE (p);
4495 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4496 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4497 else
4498 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4499 }
4500
4501 ++idx;
4502 }
4503
4504 if (is_non_type)
4505 {
4506 parm = TREE_VALUE (parm);
4507
4508 SET_DECL_TEMPLATE_PARM_P (parm);
4509
4510 if (TREE_TYPE (parm) != error_mark_node)
4511 {
4512 /* [temp.param]
4513
4514 The top-level cv-qualifiers on the template-parameter are
4515 ignored when determining its type. */
4516 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4517 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4518 TREE_TYPE (parm) = error_mark_node;
4519 else if (uses_parameter_packs (TREE_TYPE (parm))
4520 && !is_parameter_pack
4521 /* If we're in a nested template parameter list, the template
4522 template parameter could be a parameter pack. */
4523 && processing_template_parmlist == 1)
4524 {
4525 /* This template parameter is not a parameter pack, but it
4526 should be. Complain about "bare" parameter packs. */
4527 check_for_bare_parameter_packs (TREE_TYPE (parm));
4528
4529 /* Recover by calling this a parameter pack. */
4530 is_parameter_pack = true;
4531 }
4532 }
4533
4534 /* A template parameter is not modifiable. */
4535 TREE_CONSTANT (parm) = 1;
4536 TREE_READONLY (parm) = 1;
4537 decl = build_decl (parm_loc,
4538 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4539 TREE_CONSTANT (decl) = 1;
4540 TREE_READONLY (decl) = 1;
4541 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4542 = build_template_parm_index (idx, processing_template_decl,
4543 processing_template_decl,
4544 decl, TREE_TYPE (parm));
4545
4546 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4547 = is_parameter_pack;
4548 }
4549 else
4550 {
4551 tree t;
4552 parm = TREE_VALUE (TREE_VALUE (parm));
4553
4554 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4555 {
4556 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4557 /* This is for distinguishing between real templates and template
4558 template parameters */
4559 TREE_TYPE (parm) = t;
4560 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4561 decl = parm;
4562 }
4563 else
4564 {
4565 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4566 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4567 decl = build_decl (parm_loc,
4568 TYPE_DECL, parm, t);
4569 }
4570
4571 TYPE_NAME (t) = decl;
4572 TYPE_STUB_DECL (t) = decl;
4573 parm = decl;
4574 TEMPLATE_TYPE_PARM_INDEX (t)
4575 = build_template_parm_index (idx, processing_template_decl,
4576 processing_template_decl,
4577 decl, TREE_TYPE (parm));
4578 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4579 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4580 }
4581 DECL_ARTIFICIAL (decl) = 1;
4582 SET_DECL_TEMPLATE_PARM_P (decl);
4583
4584 /* Build requirements for the type/template parameter.
4585 This must be done after SET_DECL_TEMPLATE_PARM_P or
4586 process_template_parm could fail. */
4587 tree reqs = finish_shorthand_constraint (parm, constr);
4588
4589 decl = pushdecl (decl);
4590 if (!is_non_type)
4591 parm = decl;
4592
4593 /* Build the parameter node linking the parameter declaration,
4594 its default argument (if any), and its constraints (if any). */
4595 parm = build_tree_list (defval, parm);
4596 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4597
4598 return chainon (list, parm);
4599 }
4600
4601 /* The end of a template parameter list has been reached. Process the
4602 tree list into a parameter vector, converting each parameter into a more
4603 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4604 as PARM_DECLs. */
4605
4606 tree
4607 end_template_parm_list (tree parms)
4608 {
4609 int nparms;
4610 tree parm, next;
4611 tree saved_parmlist = make_tree_vec (list_length (parms));
4612
4613 /* Pop the dummy parameter level and add the real one. */
4614 current_template_parms = TREE_CHAIN (current_template_parms);
4615
4616 current_template_parms
4617 = tree_cons (size_int (processing_template_decl),
4618 saved_parmlist, current_template_parms);
4619
4620 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4621 {
4622 next = TREE_CHAIN (parm);
4623 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4624 TREE_CHAIN (parm) = NULL_TREE;
4625 }
4626
4627 --processing_template_parmlist;
4628
4629 return saved_parmlist;
4630 }
4631
4632 // Explicitly indicate the end of the template parameter list. We assume
4633 // that the current template parameters have been constructed and/or
4634 // managed explicitly, as when creating new template template parameters
4635 // from a shorthand constraint.
4636 void
4637 end_template_parm_list ()
4638 {
4639 --processing_template_parmlist;
4640 }
4641
4642 /* end_template_decl is called after a template declaration is seen. */
4643
4644 void
4645 end_template_decl (void)
4646 {
4647 reset_specialization ();
4648
4649 if (! processing_template_decl)
4650 return;
4651
4652 /* This matches the pushlevel in begin_template_parm_list. */
4653 finish_scope ();
4654
4655 --processing_template_decl;
4656 current_template_parms = TREE_CHAIN (current_template_parms);
4657 }
4658
4659 /* Takes a TREE_LIST representing a template parameter and convert it
4660 into an argument suitable to be passed to the type substitution
4661 functions. Note that If the TREE_LIST contains an error_mark
4662 node, the returned argument is error_mark_node. */
4663
4664 tree
4665 template_parm_to_arg (tree t)
4666 {
4667
4668 if (t == NULL_TREE
4669 || TREE_CODE (t) != TREE_LIST)
4670 return t;
4671
4672 if (error_operand_p (TREE_VALUE (t)))
4673 return error_mark_node;
4674
4675 t = TREE_VALUE (t);
4676
4677 if (TREE_CODE (t) == TYPE_DECL
4678 || TREE_CODE (t) == TEMPLATE_DECL)
4679 {
4680 t = TREE_TYPE (t);
4681
4682 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4683 {
4684 /* Turn this argument into a TYPE_ARGUMENT_PACK
4685 with a single element, which expands T. */
4686 tree vec = make_tree_vec (1);
4687 if (CHECKING_P)
4688 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4689
4690 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4691
4692 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4693 SET_ARGUMENT_PACK_ARGS (t, vec);
4694 }
4695 }
4696 else
4697 {
4698 t = DECL_INITIAL (t);
4699
4700 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4701 {
4702 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4703 with a single element, which expands T. */
4704 tree vec = make_tree_vec (1);
4705 if (CHECKING_P)
4706 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4707
4708 t = convert_from_reference (t);
4709 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4710
4711 t = make_node (NONTYPE_ARGUMENT_PACK);
4712 SET_ARGUMENT_PACK_ARGS (t, vec);
4713 }
4714 else
4715 t = convert_from_reference (t);
4716 }
4717 return t;
4718 }
4719
4720 /* Given a single level of template parameters (a TREE_VEC), return it
4721 as a set of template arguments. */
4722
4723 tree
4724 template_parms_level_to_args (tree parms)
4725 {
4726 tree a = copy_node (parms);
4727 TREE_TYPE (a) = NULL_TREE;
4728 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4729 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4730
4731 if (CHECKING_P)
4732 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4733
4734 return a;
4735 }
4736
4737 /* Given a set of template parameters, return them as a set of template
4738 arguments. The template parameters are represented as a TREE_VEC, in
4739 the form documented in cp-tree.h for template arguments. */
4740
4741 tree
4742 template_parms_to_args (tree parms)
4743 {
4744 tree header;
4745 tree args = NULL_TREE;
4746 int length = TMPL_PARMS_DEPTH (parms);
4747 int l = length;
4748
4749 /* If there is only one level of template parameters, we do not
4750 create a TREE_VEC of TREE_VECs. Instead, we return a single
4751 TREE_VEC containing the arguments. */
4752 if (length > 1)
4753 args = make_tree_vec (length);
4754
4755 for (header = parms; header; header = TREE_CHAIN (header))
4756 {
4757 tree a = template_parms_level_to_args (TREE_VALUE (header));
4758
4759 if (length > 1)
4760 TREE_VEC_ELT (args, --l) = a;
4761 else
4762 args = a;
4763 }
4764
4765 return args;
4766 }
4767
4768 /* Within the declaration of a template, return the currently active
4769 template parameters as an argument TREE_VEC. */
4770
4771 static tree
4772 current_template_args (void)
4773 {
4774 return template_parms_to_args (current_template_parms);
4775 }
4776
4777 /* Return the fully generic arguments for of TMPL, i.e. what
4778 current_template_args would be while parsing it. */
4779
4780 tree
4781 generic_targs_for (tree tmpl)
4782 {
4783 if (tmpl == NULL_TREE)
4784 return NULL_TREE;
4785 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4786 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4787 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4788 template parameter, it has no TEMPLATE_INFO; for a partial
4789 specialization, it has the arguments for the primary template, and we
4790 want the arguments for the partial specialization. */;
4791 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4792 if (tree ti = get_template_info (result))
4793 return TI_ARGS (ti);
4794 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4795 }
4796
4797 /* Update the declared TYPE by doing any lookups which were thought to be
4798 dependent, but are not now that we know the SCOPE of the declarator. */
4799
4800 tree
4801 maybe_update_decl_type (tree orig_type, tree scope)
4802 {
4803 tree type = orig_type;
4804
4805 if (type == NULL_TREE)
4806 return type;
4807
4808 if (TREE_CODE (orig_type) == TYPE_DECL)
4809 type = TREE_TYPE (type);
4810
4811 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4812 && dependent_type_p (type)
4813 /* Don't bother building up the args in this case. */
4814 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4815 {
4816 /* tsubst in the args corresponding to the template parameters,
4817 including auto if present. Most things will be unchanged, but
4818 make_typename_type and tsubst_qualified_id will resolve
4819 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4820 tree args = current_template_args ();
4821 tree auto_node = type_uses_auto (type);
4822 tree pushed;
4823 if (auto_node)
4824 {
4825 tree auto_vec = make_tree_vec (1);
4826 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4827 args = add_to_template_args (args, auto_vec);
4828 }
4829 pushed = push_scope (scope);
4830 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4831 if (pushed)
4832 pop_scope (scope);
4833 }
4834
4835 if (type == error_mark_node)
4836 return orig_type;
4837
4838 if (TREE_CODE (orig_type) == TYPE_DECL)
4839 {
4840 if (same_type_p (type, TREE_TYPE (orig_type)))
4841 type = orig_type;
4842 else
4843 type = TYPE_NAME (type);
4844 }
4845 return type;
4846 }
4847
4848 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4849 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4850 the new template is a member template. */
4851
4852 static tree
4853 build_template_decl (tree decl, tree parms, bool member_template_p)
4854 {
4855 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4856 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4857 DECL_TEMPLATE_PARMS (tmpl) = parms;
4858 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4859 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4860 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4861
4862 return tmpl;
4863 }
4864
4865 struct template_parm_data
4866 {
4867 /* The level of the template parameters we are currently
4868 processing. */
4869 int level;
4870
4871 /* The index of the specialization argument we are currently
4872 processing. */
4873 int current_arg;
4874
4875 /* An array whose size is the number of template parameters. The
4876 elements are nonzero if the parameter has been used in any one
4877 of the arguments processed so far. */
4878 int* parms;
4879
4880 /* An array whose size is the number of template arguments. The
4881 elements are nonzero if the argument makes use of template
4882 parameters of this level. */
4883 int* arg_uses_template_parms;
4884 };
4885
4886 /* Subroutine of push_template_decl used to see if each template
4887 parameter in a partial specialization is used in the explicit
4888 argument list. If T is of the LEVEL given in DATA (which is
4889 treated as a template_parm_data*), then DATA->PARMS is marked
4890 appropriately. */
4891
4892 static int
4893 mark_template_parm (tree t, void* data)
4894 {
4895 int level;
4896 int idx;
4897 struct template_parm_data* tpd = (struct template_parm_data*) data;
4898
4899 template_parm_level_and_index (t, &level, &idx);
4900
4901 if (level == tpd->level)
4902 {
4903 tpd->parms[idx] = 1;
4904 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4905 }
4906
4907 /* In C++17 the type of a non-type argument is a deduced context. */
4908 if (cxx_dialect >= cxx17
4909 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4910 for_each_template_parm (TREE_TYPE (t),
4911 &mark_template_parm,
4912 data,
4913 NULL,
4914 /*include_nondeduced_p=*/false);
4915
4916 /* Return zero so that for_each_template_parm will continue the
4917 traversal of the tree; we want to mark *every* template parm. */
4918 return 0;
4919 }
4920
4921 /* Process the partial specialization DECL. */
4922
4923 static tree
4924 process_partial_specialization (tree decl)
4925 {
4926 tree type = TREE_TYPE (decl);
4927 tree tinfo = get_template_info (decl);
4928 tree maintmpl = TI_TEMPLATE (tinfo);
4929 tree specargs = TI_ARGS (tinfo);
4930 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4931 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4932 tree inner_parms;
4933 tree inst;
4934 int nargs = TREE_VEC_LENGTH (inner_args);
4935 int ntparms;
4936 int i;
4937 bool did_error_intro = false;
4938 struct template_parm_data tpd;
4939 struct template_parm_data tpd2;
4940
4941 gcc_assert (current_template_parms);
4942
4943 /* A concept cannot be specialized. */
4944 if (flag_concepts && variable_concept_p (maintmpl))
4945 {
4946 error ("specialization of variable concept %q#D", maintmpl);
4947 return error_mark_node;
4948 }
4949
4950 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4951 ntparms = TREE_VEC_LENGTH (inner_parms);
4952
4953 /* We check that each of the template parameters given in the
4954 partial specialization is used in the argument list to the
4955 specialization. For example:
4956
4957 template <class T> struct S;
4958 template <class T> struct S<T*>;
4959
4960 The second declaration is OK because `T*' uses the template
4961 parameter T, whereas
4962
4963 template <class T> struct S<int>;
4964
4965 is no good. Even trickier is:
4966
4967 template <class T>
4968 struct S1
4969 {
4970 template <class U>
4971 struct S2;
4972 template <class U>
4973 struct S2<T>;
4974 };
4975
4976 The S2<T> declaration is actually invalid; it is a
4977 full-specialization. Of course,
4978
4979 template <class U>
4980 struct S2<T (*)(U)>;
4981
4982 or some such would have been OK. */
4983 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4984 tpd.parms = XALLOCAVEC (int, ntparms);
4985 memset (tpd.parms, 0, sizeof (int) * ntparms);
4986
4987 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4988 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4989 for (i = 0; i < nargs; ++i)
4990 {
4991 tpd.current_arg = i;
4992 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4993 &mark_template_parm,
4994 &tpd,
4995 NULL,
4996 /*include_nondeduced_p=*/false);
4997 }
4998 for (i = 0; i < ntparms; ++i)
4999 if (tpd.parms[i] == 0)
5000 {
5001 /* One of the template parms was not used in a deduced context in the
5002 specialization. */
5003 if (!did_error_intro)
5004 {
5005 error ("template parameters not deducible in "
5006 "partial specialization:");
5007 did_error_intro = true;
5008 }
5009
5010 inform (input_location, " %qD",
5011 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5012 }
5013
5014 if (did_error_intro)
5015 return error_mark_node;
5016
5017 /* [temp.class.spec]
5018
5019 The argument list of the specialization shall not be identical to
5020 the implicit argument list of the primary template. */
5021 tree main_args
5022 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5023 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5024 && (!flag_concepts
5025 || !strictly_subsumes (current_template_constraints (),
5026 inner_args, maintmpl)))
5027 {
5028 if (!flag_concepts)
5029 error ("partial specialization %q+D does not specialize "
5030 "any template arguments; to define the primary template, "
5031 "remove the template argument list", decl);
5032 else
5033 error ("partial specialization %q+D does not specialize any "
5034 "template arguments and is not more constrained than "
5035 "the primary template; to define the primary template, "
5036 "remove the template argument list", decl);
5037 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5038 }
5039
5040 /* A partial specialization that replaces multiple parameters of the
5041 primary template with a pack expansion is less specialized for those
5042 parameters. */
5043 if (nargs < DECL_NTPARMS (maintmpl))
5044 {
5045 error ("partial specialization is not more specialized than the "
5046 "primary template because it replaces multiple parameters "
5047 "with a pack expansion");
5048 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5049 /* Avoid crash in process_partial_specialization. */
5050 return decl;
5051 }
5052
5053 /* If we aren't in a dependent class, we can actually try deduction. */
5054 else if (tpd.level == 1
5055 /* FIXME we should be able to handle a partial specialization of a
5056 partial instantiation, but currently we can't (c++/41727). */
5057 && TMPL_ARGS_DEPTH (specargs) == 1
5058 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5059 {
5060 auto_diagnostic_group d;
5061 if (permerror (input_location, "partial specialization %qD is not "
5062 "more specialized than", decl))
5063 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5064 maintmpl);
5065 }
5066
5067 /* [temp.class.spec]
5068
5069 A partially specialized non-type argument expression shall not
5070 involve template parameters of the partial specialization except
5071 when the argument expression is a simple identifier.
5072
5073 The type of a template parameter corresponding to a specialized
5074 non-type argument shall not be dependent on a parameter of the
5075 specialization.
5076
5077 Also, we verify that pack expansions only occur at the
5078 end of the argument list. */
5079 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
5080 tpd2.parms = 0;
5081 for (i = 0; i < nargs; ++i)
5082 {
5083 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5084 tree arg = TREE_VEC_ELT (inner_args, i);
5085 tree packed_args = NULL_TREE;
5086 int j, len = 1;
5087
5088 if (ARGUMENT_PACK_P (arg))
5089 {
5090 /* Extract the arguments from the argument pack. We'll be
5091 iterating over these in the following loop. */
5092 packed_args = ARGUMENT_PACK_ARGS (arg);
5093 len = TREE_VEC_LENGTH (packed_args);
5094 }
5095
5096 for (j = 0; j < len; j++)
5097 {
5098 if (packed_args)
5099 /* Get the Jth argument in the parameter pack. */
5100 arg = TREE_VEC_ELT (packed_args, j);
5101
5102 if (PACK_EXPANSION_P (arg))
5103 {
5104 /* Pack expansions must come at the end of the
5105 argument list. */
5106 if ((packed_args && j < len - 1)
5107 || (!packed_args && i < nargs - 1))
5108 {
5109 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5110 error ("parameter pack argument %qE must be at the "
5111 "end of the template argument list", arg);
5112 else
5113 error ("parameter pack argument %qT must be at the "
5114 "end of the template argument list", arg);
5115 }
5116 }
5117
5118 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5119 /* We only care about the pattern. */
5120 arg = PACK_EXPANSION_PATTERN (arg);
5121
5122 if (/* These first two lines are the `non-type' bit. */
5123 !TYPE_P (arg)
5124 && TREE_CODE (arg) != TEMPLATE_DECL
5125 /* This next two lines are the `argument expression is not just a
5126 simple identifier' condition and also the `specialized
5127 non-type argument' bit. */
5128 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5129 && !((REFERENCE_REF_P (arg)
5130 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5131 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5132 {
5133 if ((!packed_args && tpd.arg_uses_template_parms[i])
5134 || (packed_args && uses_template_parms (arg)))
5135 error_at (cp_expr_loc_or_input_loc (arg),
5136 "template argument %qE involves template "
5137 "parameter(s)", arg);
5138 else
5139 {
5140 /* Look at the corresponding template parameter,
5141 marking which template parameters its type depends
5142 upon. */
5143 tree type = TREE_TYPE (parm);
5144
5145 if (!tpd2.parms)
5146 {
5147 /* We haven't yet initialized TPD2. Do so now. */
5148 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5149 /* The number of parameters here is the number in the
5150 main template, which, as checked in the assertion
5151 above, is NARGS. */
5152 tpd2.parms = XALLOCAVEC (int, nargs);
5153 tpd2.level =
5154 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5155 }
5156
5157 /* Mark the template parameters. But this time, we're
5158 looking for the template parameters of the main
5159 template, not in the specialization. */
5160 tpd2.current_arg = i;
5161 tpd2.arg_uses_template_parms[i] = 0;
5162 memset (tpd2.parms, 0, sizeof (int) * nargs);
5163 for_each_template_parm (type,
5164 &mark_template_parm,
5165 &tpd2,
5166 NULL,
5167 /*include_nondeduced_p=*/false);
5168
5169 if (tpd2.arg_uses_template_parms [i])
5170 {
5171 /* The type depended on some template parameters.
5172 If they are fully specialized in the
5173 specialization, that's OK. */
5174 int j;
5175 int count = 0;
5176 for (j = 0; j < nargs; ++j)
5177 if (tpd2.parms[j] != 0
5178 && tpd.arg_uses_template_parms [j])
5179 ++count;
5180 if (count != 0)
5181 error_n (input_location, count,
5182 "type %qT of template argument %qE depends "
5183 "on a template parameter",
5184 "type %qT of template argument %qE depends "
5185 "on template parameters",
5186 type,
5187 arg);
5188 }
5189 }
5190 }
5191 }
5192 }
5193
5194 /* We should only get here once. */
5195 if (TREE_CODE (decl) == TYPE_DECL)
5196 gcc_assert (!COMPLETE_TYPE_P (type));
5197
5198 // Build the template decl.
5199 tree tmpl = build_template_decl (decl, current_template_parms,
5200 DECL_MEMBER_TEMPLATE_P (maintmpl));
5201 TREE_TYPE (tmpl) = type;
5202 DECL_TEMPLATE_RESULT (tmpl) = decl;
5203 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5204 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5205 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5206
5207 /* Give template template parms a DECL_CONTEXT of the template
5208 for which they are a parameter. */
5209 for (i = 0; i < ntparms; ++i)
5210 {
5211 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5212 if (TREE_CODE (parm) == TEMPLATE_DECL)
5213 DECL_CONTEXT (parm) = tmpl;
5214 }
5215
5216 if (VAR_P (decl))
5217 /* We didn't register this in check_explicit_specialization so we could
5218 wait until the constraints were set. */
5219 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5220 else
5221 associate_classtype_constraints (type);
5222
5223 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5224 = tree_cons (specargs, tmpl,
5225 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5226 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5227
5228 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5229 inst = TREE_CHAIN (inst))
5230 {
5231 tree instance = TREE_VALUE (inst);
5232 if (TYPE_P (instance)
5233 ? (COMPLETE_TYPE_P (instance)
5234 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5235 : DECL_TEMPLATE_INSTANTIATION (instance))
5236 {
5237 tree spec = most_specialized_partial_spec (instance, tf_none);
5238 tree inst_decl = (DECL_P (instance)
5239 ? instance : TYPE_NAME (instance));
5240 if (!spec)
5241 /* OK */;
5242 else if (spec == error_mark_node)
5243 permerror (input_location,
5244 "declaration of %qD ambiguates earlier template "
5245 "instantiation for %qD", decl, inst_decl);
5246 else if (TREE_VALUE (spec) == tmpl)
5247 permerror (input_location,
5248 "partial specialization of %qD after instantiation "
5249 "of %qD", decl, inst_decl);
5250 }
5251 }
5252
5253 return decl;
5254 }
5255
5256 /* PARM is a template parameter of some form; return the corresponding
5257 TEMPLATE_PARM_INDEX. */
5258
5259 static tree
5260 get_template_parm_index (tree parm)
5261 {
5262 if (TREE_CODE (parm) == PARM_DECL
5263 || TREE_CODE (parm) == CONST_DECL)
5264 parm = DECL_INITIAL (parm);
5265 else if (TREE_CODE (parm) == TYPE_DECL
5266 || TREE_CODE (parm) == TEMPLATE_DECL)
5267 parm = TREE_TYPE (parm);
5268 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5269 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5270 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5271 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5272 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5273 return parm;
5274 }
5275
5276 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5277 parameter packs used by the template parameter PARM. */
5278
5279 static void
5280 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5281 {
5282 /* A type parm can't refer to another parm. */
5283 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5284 return;
5285 else if (TREE_CODE (parm) == PARM_DECL)
5286 {
5287 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5288 ppd, ppd->visited);
5289 return;
5290 }
5291
5292 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5293
5294 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5295 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5296 {
5297 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5298 if (template_parameter_pack_p (p))
5299 /* Any packs in the type are expanded by this parameter. */;
5300 else
5301 fixed_parameter_pack_p_1 (p, ppd);
5302 }
5303 }
5304
5305 /* PARM is a template parameter pack. Return any parameter packs used in
5306 its type or the type of any of its template parameters. If there are
5307 any such packs, it will be instantiated into a fixed template parameter
5308 list by partial instantiation rather than be fully deduced. */
5309
5310 tree
5311 fixed_parameter_pack_p (tree parm)
5312 {
5313 /* This can only be true in a member template. */
5314 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5315 return NULL_TREE;
5316 /* This can only be true for a parameter pack. */
5317 if (!template_parameter_pack_p (parm))
5318 return NULL_TREE;
5319 /* A type parm can't refer to another parm. */
5320 if (TREE_CODE (parm) == TYPE_DECL)
5321 return NULL_TREE;
5322
5323 tree parameter_packs = NULL_TREE;
5324 struct find_parameter_pack_data ppd;
5325 ppd.parameter_packs = &parameter_packs;
5326 ppd.visited = new hash_set<tree>;
5327 ppd.type_pack_expansion_p = false;
5328
5329 fixed_parameter_pack_p_1 (parm, &ppd);
5330
5331 delete ppd.visited;
5332 return parameter_packs;
5333 }
5334
5335 /* Check that a template declaration's use of default arguments and
5336 parameter packs is not invalid. Here, PARMS are the template
5337 parameters. IS_PRIMARY is true if DECL is the thing declared by
5338 a primary template. IS_PARTIAL is true if DECL is a partial
5339 specialization.
5340
5341 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5342 function template declaration or a friend class template
5343 declaration. In the function case, 1 indicates a declaration, 2
5344 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5345 emitted for extraneous default arguments.
5346
5347 Returns TRUE if there were no errors found, FALSE otherwise. */
5348
5349 bool
5350 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5351 bool is_partial, int is_friend_decl)
5352 {
5353 const char *msg;
5354 int last_level_to_check;
5355 tree parm_level;
5356 bool no_errors = true;
5357
5358 /* [temp.param]
5359
5360 A default template-argument shall not be specified in a
5361 function template declaration or a function template definition, nor
5362 in the template-parameter-list of the definition of a member of a
5363 class template. */
5364
5365 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5366 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5367 /* You can't have a function template declaration in a local
5368 scope, nor you can you define a member of a class template in a
5369 local scope. */
5370 return true;
5371
5372 if ((TREE_CODE (decl) == TYPE_DECL
5373 && TREE_TYPE (decl)
5374 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5375 || (TREE_CODE (decl) == FUNCTION_DECL
5376 && LAMBDA_FUNCTION_P (decl)))
5377 /* A lambda doesn't have an explicit declaration; don't complain
5378 about the parms of the enclosing class. */
5379 return true;
5380
5381 if (current_class_type
5382 && !TYPE_BEING_DEFINED (current_class_type)
5383 && DECL_LANG_SPECIFIC (decl)
5384 && DECL_DECLARES_FUNCTION_P (decl)
5385 /* If this is either a friend defined in the scope of the class
5386 or a member function. */
5387 && (DECL_FUNCTION_MEMBER_P (decl)
5388 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5389 : DECL_FRIEND_CONTEXT (decl)
5390 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5391 : false)
5392 /* And, if it was a member function, it really was defined in
5393 the scope of the class. */
5394 && (!DECL_FUNCTION_MEMBER_P (decl)
5395 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5396 /* We already checked these parameters when the template was
5397 declared, so there's no need to do it again now. This function
5398 was defined in class scope, but we're processing its body now
5399 that the class is complete. */
5400 return true;
5401
5402 /* Core issue 226 (C++0x only): the following only applies to class
5403 templates. */
5404 if (is_primary
5405 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5406 {
5407 /* [temp.param]
5408
5409 If a template-parameter has a default template-argument, all
5410 subsequent template-parameters shall have a default
5411 template-argument supplied. */
5412 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5413 {
5414 tree inner_parms = TREE_VALUE (parm_level);
5415 int ntparms = TREE_VEC_LENGTH (inner_parms);
5416 int seen_def_arg_p = 0;
5417 int i;
5418
5419 for (i = 0; i < ntparms; ++i)
5420 {
5421 tree parm = TREE_VEC_ELT (inner_parms, i);
5422
5423 if (parm == error_mark_node)
5424 continue;
5425
5426 if (TREE_PURPOSE (parm))
5427 seen_def_arg_p = 1;
5428 else if (seen_def_arg_p
5429 && !template_parameter_pack_p (TREE_VALUE (parm)))
5430 {
5431 error ("no default argument for %qD", TREE_VALUE (parm));
5432 /* For better subsequent error-recovery, we indicate that
5433 there should have been a default argument. */
5434 TREE_PURPOSE (parm) = error_mark_node;
5435 no_errors = false;
5436 }
5437 else if (!is_partial
5438 && !is_friend_decl
5439 /* Don't complain about an enclosing partial
5440 specialization. */
5441 && parm_level == parms
5442 && TREE_CODE (decl) == TYPE_DECL
5443 && i < ntparms - 1
5444 && template_parameter_pack_p (TREE_VALUE (parm))
5445 /* A fixed parameter pack will be partially
5446 instantiated into a fixed length list. */
5447 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5448 {
5449 /* A primary class template can only have one
5450 parameter pack, at the end of the template
5451 parameter list. */
5452
5453 error ("parameter pack %q+D must be at the end of the"
5454 " template parameter list", TREE_VALUE (parm));
5455
5456 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5457 = error_mark_node;
5458 no_errors = false;
5459 }
5460 }
5461 }
5462 }
5463
5464 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5465 || is_partial
5466 || !is_primary
5467 || is_friend_decl)
5468 /* For an ordinary class template, default template arguments are
5469 allowed at the innermost level, e.g.:
5470 template <class T = int>
5471 struct S {};
5472 but, in a partial specialization, they're not allowed even
5473 there, as we have in [temp.class.spec]:
5474
5475 The template parameter list of a specialization shall not
5476 contain default template argument values.
5477
5478 So, for a partial specialization, or for a function template
5479 (in C++98/C++03), we look at all of them. */
5480 ;
5481 else
5482 /* But, for a primary class template that is not a partial
5483 specialization we look at all template parameters except the
5484 innermost ones. */
5485 parms = TREE_CHAIN (parms);
5486
5487 /* Figure out what error message to issue. */
5488 if (is_friend_decl == 2)
5489 msg = G_("default template arguments may not be used in function template "
5490 "friend re-declaration");
5491 else if (is_friend_decl)
5492 msg = G_("default template arguments may not be used in template "
5493 "friend declarations");
5494 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5495 msg = G_("default template arguments may not be used in function templates "
5496 "without %<-std=c++11%> or %<-std=gnu++11%>");
5497 else if (is_partial)
5498 msg = G_("default template arguments may not be used in "
5499 "partial specializations");
5500 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5501 msg = G_("default argument for template parameter for class enclosing %qD");
5502 else
5503 /* Per [temp.param]/9, "A default template-argument shall not be
5504 specified in the template-parameter-lists of the definition of
5505 a member of a class template that appears outside of the member's
5506 class.", thus if we aren't handling a member of a class template
5507 there is no need to examine the parameters. */
5508 return true;
5509
5510 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5511 /* If we're inside a class definition, there's no need to
5512 examine the parameters to the class itself. On the one
5513 hand, they will be checked when the class is defined, and,
5514 on the other, default arguments are valid in things like:
5515 template <class T = double>
5516 struct S { template <class U> void f(U); };
5517 Here the default argument for `S' has no bearing on the
5518 declaration of `f'. */
5519 last_level_to_check = template_class_depth (current_class_type) + 1;
5520 else
5521 /* Check everything. */
5522 last_level_to_check = 0;
5523
5524 for (parm_level = parms;
5525 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5526 parm_level = TREE_CHAIN (parm_level))
5527 {
5528 tree inner_parms = TREE_VALUE (parm_level);
5529 int i;
5530 int ntparms;
5531
5532 ntparms = TREE_VEC_LENGTH (inner_parms);
5533 for (i = 0; i < ntparms; ++i)
5534 {
5535 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5536 continue;
5537
5538 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5539 {
5540 if (msg)
5541 {
5542 no_errors = false;
5543 if (is_friend_decl == 2)
5544 return no_errors;
5545
5546 error (msg, decl);
5547 msg = 0;
5548 }
5549
5550 /* Clear out the default argument so that we are not
5551 confused later. */
5552 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5553 }
5554 }
5555
5556 /* At this point, if we're still interested in issuing messages,
5557 they must apply to classes surrounding the object declared. */
5558 if (msg)
5559 msg = G_("default argument for template parameter for class "
5560 "enclosing %qD");
5561 }
5562
5563 return no_errors;
5564 }
5565
5566 /* Worker for push_template_decl_real, called via
5567 for_each_template_parm. DATA is really an int, indicating the
5568 level of the parameters we are interested in. If T is a template
5569 parameter of that level, return nonzero. */
5570
5571 static int
5572 template_parm_this_level_p (tree t, void* data)
5573 {
5574 int this_level = *(int *)data;
5575 int level;
5576
5577 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5578 level = TEMPLATE_PARM_LEVEL (t);
5579 else
5580 level = TEMPLATE_TYPE_LEVEL (t);
5581 return level == this_level;
5582 }
5583
5584 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5585 DATA is really an int, indicating the innermost outer level of parameters.
5586 If T is a template parameter of that level or further out, return
5587 nonzero. */
5588
5589 static int
5590 template_parm_outer_level (tree t, void *data)
5591 {
5592 int this_level = *(int *)data;
5593 int level;
5594
5595 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5596 level = TEMPLATE_PARM_LEVEL (t);
5597 else
5598 level = TEMPLATE_TYPE_LEVEL (t);
5599 return level <= this_level;
5600 }
5601
5602 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5603 parameters given by current_template_args, or reuses a
5604 previously existing one, if appropriate. Returns the DECL, or an
5605 equivalent one, if it is replaced via a call to duplicate_decls.
5606
5607 If IS_FRIEND is true, DECL is a friend declaration. */
5608
5609 tree
5610 push_template_decl_real (tree decl, bool is_friend)
5611 {
5612 tree tmpl;
5613 tree args;
5614 tree info;
5615 tree ctx;
5616 bool is_primary;
5617 bool is_partial;
5618 int new_template_p = 0;
5619 /* True if the template is a member template, in the sense of
5620 [temp.mem]. */
5621 bool member_template_p = false;
5622
5623 if (decl == error_mark_node || !current_template_parms)
5624 return error_mark_node;
5625
5626 /* See if this is a partial specialization. */
5627 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5628 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5629 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5630 || (VAR_P (decl)
5631 && DECL_LANG_SPECIFIC (decl)
5632 && DECL_TEMPLATE_SPECIALIZATION (decl)
5633 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5634
5635 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5636 is_friend = true;
5637
5638 if (is_friend)
5639 /* For a friend, we want the context of the friend, not
5640 the type of which it is a friend. */
5641 ctx = CP_DECL_CONTEXT (decl);
5642 else if (CP_DECL_CONTEXT (decl)
5643 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5644 /* In the case of a virtual function, we want the class in which
5645 it is defined. */
5646 ctx = CP_DECL_CONTEXT (decl);
5647 else
5648 /* Otherwise, if we're currently defining some class, the DECL
5649 is assumed to be a member of the class. */
5650 ctx = current_scope ();
5651
5652 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5653 ctx = NULL_TREE;
5654
5655 if (!DECL_CONTEXT (decl))
5656 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5657
5658 /* See if this is a primary template. */
5659 if (is_friend && ctx
5660 && uses_template_parms_level (ctx, processing_template_decl))
5661 /* A friend template that specifies a class context, i.e.
5662 template <typename T> friend void A<T>::f();
5663 is not primary. */
5664 is_primary = false;
5665 else if (TREE_CODE (decl) == TYPE_DECL
5666 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5667 is_primary = false;
5668 else
5669 is_primary = template_parm_scope_p ();
5670
5671 if (is_primary)
5672 {
5673 warning (OPT_Wtemplates, "template %qD declared", decl);
5674
5675 if (DECL_CLASS_SCOPE_P (decl))
5676 member_template_p = true;
5677 if (TREE_CODE (decl) == TYPE_DECL
5678 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5679 {
5680 error ("template class without a name");
5681 return error_mark_node;
5682 }
5683 else if (TREE_CODE (decl) == FUNCTION_DECL)
5684 {
5685 if (member_template_p)
5686 {
5687 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5688 error ("member template %qD may not have virt-specifiers", decl);
5689 }
5690 if (DECL_DESTRUCTOR_P (decl))
5691 {
5692 /* [temp.mem]
5693
5694 A destructor shall not be a member template. */
5695 error_at (DECL_SOURCE_LOCATION (decl),
5696 "destructor %qD declared as member template", decl);
5697 return error_mark_node;
5698 }
5699 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5700 && (!prototype_p (TREE_TYPE (decl))
5701 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5702 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5703 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5704 == void_list_node)))
5705 {
5706 /* [basic.stc.dynamic.allocation]
5707
5708 An allocation function can be a function
5709 template. ... Template allocation functions shall
5710 have two or more parameters. */
5711 error ("invalid template declaration of %qD", decl);
5712 return error_mark_node;
5713 }
5714 }
5715 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5716 && CLASS_TYPE_P (TREE_TYPE (decl)))
5717 {
5718 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5719 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5720 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5721 {
5722 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5723 if (TREE_CODE (t) == TYPE_DECL)
5724 t = TREE_TYPE (t);
5725 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5726 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5727 }
5728 }
5729 else if (TREE_CODE (decl) == TYPE_DECL
5730 && TYPE_DECL_ALIAS_P (decl))
5731 /* alias-declaration */
5732 gcc_assert (!DECL_ARTIFICIAL (decl));
5733 else if (VAR_P (decl))
5734 /* C++14 variable template. */;
5735 else if (TREE_CODE (decl) == CONCEPT_DECL)
5736 /* C++2a concept definitions. */;
5737 else
5738 {
5739 error ("template declaration of %q#D", decl);
5740 return error_mark_node;
5741 }
5742 }
5743
5744 /* Check to see that the rules regarding the use of default
5745 arguments are not being violated. We check args for a friend
5746 functions when we know whether it's a definition, introducing
5747 declaration or re-declaration. */
5748 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5749 check_default_tmpl_args (decl, current_template_parms,
5750 is_primary, is_partial, is_friend);
5751
5752 /* Ensure that there are no parameter packs in the type of this
5753 declaration that have not been expanded. */
5754 if (TREE_CODE (decl) == FUNCTION_DECL)
5755 {
5756 /* Check each of the arguments individually to see if there are
5757 any bare parameter packs. */
5758 tree type = TREE_TYPE (decl);
5759 tree arg = DECL_ARGUMENTS (decl);
5760 tree argtype = TYPE_ARG_TYPES (type);
5761
5762 while (arg && argtype)
5763 {
5764 if (!DECL_PACK_P (arg)
5765 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5766 {
5767 /* This is a PARM_DECL that contains unexpanded parameter
5768 packs. We have already complained about this in the
5769 check_for_bare_parameter_packs call, so just replace
5770 these types with ERROR_MARK_NODE. */
5771 TREE_TYPE (arg) = error_mark_node;
5772 TREE_VALUE (argtype) = error_mark_node;
5773 }
5774
5775 arg = DECL_CHAIN (arg);
5776 argtype = TREE_CHAIN (argtype);
5777 }
5778
5779 /* Check for bare parameter packs in the return type and the
5780 exception specifiers. */
5781 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5782 /* Errors were already issued, set return type to int
5783 as the frontend doesn't expect error_mark_node as
5784 the return type. */
5785 TREE_TYPE (type) = integer_type_node;
5786 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5787 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5788 }
5789 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5790 && TYPE_DECL_ALIAS_P (decl))
5791 ? DECL_ORIGINAL_TYPE (decl)
5792 : TREE_TYPE (decl)))
5793 {
5794 TREE_TYPE (decl) = error_mark_node;
5795 return error_mark_node;
5796 }
5797
5798 if (is_partial)
5799 return process_partial_specialization (decl);
5800
5801 args = current_template_args ();
5802
5803 if (!ctx
5804 || TREE_CODE (ctx) == FUNCTION_DECL
5805 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5806 || (TREE_CODE (decl) == TYPE_DECL
5807 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5808 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5809 {
5810 if (DECL_LANG_SPECIFIC (decl)
5811 && DECL_TEMPLATE_INFO (decl)
5812 && DECL_TI_TEMPLATE (decl))
5813 tmpl = DECL_TI_TEMPLATE (decl);
5814 /* If DECL is a TYPE_DECL for a class-template, then there won't
5815 be DECL_LANG_SPECIFIC. The information equivalent to
5816 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5817 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5818 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5819 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5820 {
5821 /* Since a template declaration already existed for this
5822 class-type, we must be redeclaring it here. Make sure
5823 that the redeclaration is valid. */
5824 redeclare_class_template (TREE_TYPE (decl),
5825 current_template_parms,
5826 current_template_constraints ());
5827 /* We don't need to create a new TEMPLATE_DECL; just use the
5828 one we already had. */
5829 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5830 }
5831 else
5832 {
5833 tmpl = build_template_decl (decl, current_template_parms,
5834 member_template_p);
5835 new_template_p = 1;
5836
5837 if (DECL_LANG_SPECIFIC (decl)
5838 && DECL_TEMPLATE_SPECIALIZATION (decl))
5839 {
5840 /* A specialization of a member template of a template
5841 class. */
5842 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5843 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5844 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5845 }
5846 }
5847 }
5848 else
5849 {
5850 tree a, t, current, parms;
5851 int i;
5852 tree tinfo = get_template_info (decl);
5853
5854 if (!tinfo)
5855 {
5856 error ("template definition of non-template %q#D", decl);
5857 return error_mark_node;
5858 }
5859
5860 tmpl = TI_TEMPLATE (tinfo);
5861
5862 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5863 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5864 && DECL_TEMPLATE_SPECIALIZATION (decl)
5865 && DECL_MEMBER_TEMPLATE_P (tmpl))
5866 {
5867 tree new_tmpl;
5868
5869 /* The declaration is a specialization of a member
5870 template, declared outside the class. Therefore, the
5871 innermost template arguments will be NULL, so we
5872 replace them with the arguments determined by the
5873 earlier call to check_explicit_specialization. */
5874 args = DECL_TI_ARGS (decl);
5875
5876 new_tmpl
5877 = build_template_decl (decl, current_template_parms,
5878 member_template_p);
5879 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5880 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5881 DECL_TI_TEMPLATE (decl) = new_tmpl;
5882 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5883 DECL_TEMPLATE_INFO (new_tmpl)
5884 = build_template_info (tmpl, args);
5885
5886 register_specialization (new_tmpl,
5887 most_general_template (tmpl),
5888 args,
5889 is_friend, 0);
5890 return decl;
5891 }
5892
5893 /* Make sure the template headers we got make sense. */
5894
5895 parms = DECL_TEMPLATE_PARMS (tmpl);
5896 i = TMPL_PARMS_DEPTH (parms);
5897 if (TMPL_ARGS_DEPTH (args) != i)
5898 {
5899 error ("expected %d levels of template parms for %q#D, got %d",
5900 i, decl, TMPL_ARGS_DEPTH (args));
5901 DECL_INTERFACE_KNOWN (decl) = 1;
5902 return error_mark_node;
5903 }
5904 else
5905 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5906 {
5907 a = TMPL_ARGS_LEVEL (args, i);
5908 t = INNERMOST_TEMPLATE_PARMS (parms);
5909
5910 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5911 {
5912 if (current == decl)
5913 error ("got %d template parameters for %q#D",
5914 TREE_VEC_LENGTH (a), decl);
5915 else
5916 error ("got %d template parameters for %q#T",
5917 TREE_VEC_LENGTH (a), current);
5918 error (" but %d required", TREE_VEC_LENGTH (t));
5919 /* Avoid crash in import_export_decl. */
5920 DECL_INTERFACE_KNOWN (decl) = 1;
5921 return error_mark_node;
5922 }
5923
5924 if (current == decl)
5925 current = ctx;
5926 else if (current == NULL_TREE)
5927 /* Can happen in erroneous input. */
5928 break;
5929 else
5930 current = get_containing_scope (current);
5931 }
5932
5933 /* Check that the parms are used in the appropriate qualifying scopes
5934 in the declarator. */
5935 if (!comp_template_args
5936 (TI_ARGS (tinfo),
5937 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5938 {
5939 error ("template arguments to %qD do not match original "
5940 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5941 if (!uses_template_parms (TI_ARGS (tinfo)))
5942 inform (input_location, "use %<template<>%> for"
5943 " an explicit specialization");
5944 /* Avoid crash in import_export_decl. */
5945 DECL_INTERFACE_KNOWN (decl) = 1;
5946 return error_mark_node;
5947 }
5948 }
5949
5950 DECL_TEMPLATE_RESULT (tmpl) = decl;
5951 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5952
5953 /* Push template declarations for global functions and types. Note
5954 that we do not try to push a global template friend declared in a
5955 template class; such a thing may well depend on the template
5956 parameters of the class. */
5957 if (new_template_p && !ctx
5958 && !(is_friend && template_class_depth (current_class_type) > 0))
5959 {
5960 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5961 if (tmpl == error_mark_node)
5962 return error_mark_node;
5963
5964 /* Hide template friend classes that haven't been declared yet. */
5965 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5966 {
5967 DECL_ANTICIPATED (tmpl) = 1;
5968 DECL_FRIEND_P (tmpl) = 1;
5969 }
5970 }
5971
5972 if (is_primary)
5973 {
5974 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5975
5976 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5977
5978 /* Give template template parms a DECL_CONTEXT of the template
5979 for which they are a parameter. */
5980 parms = INNERMOST_TEMPLATE_PARMS (parms);
5981 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5982 {
5983 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5984 if (TREE_CODE (parm) == TEMPLATE_DECL)
5985 DECL_CONTEXT (parm) = tmpl;
5986 }
5987
5988 if (TREE_CODE (decl) == TYPE_DECL
5989 && TYPE_DECL_ALIAS_P (decl)
5990 && complex_alias_template_p (tmpl))
5991 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5992 }
5993
5994 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5995 back to its most general template. If TMPL is a specialization,
5996 ARGS may only have the innermost set of arguments. Add the missing
5997 argument levels if necessary. */
5998 if (DECL_TEMPLATE_INFO (tmpl))
5999 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6000
6001 info = build_template_info (tmpl, args);
6002
6003 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6004 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6005 else
6006 {
6007 if (is_primary)
6008 retrofit_lang_decl (decl);
6009 if (DECL_LANG_SPECIFIC (decl))
6010 DECL_TEMPLATE_INFO (decl) = info;
6011 }
6012
6013 if (flag_implicit_templates
6014 && !is_friend
6015 && TREE_PUBLIC (decl)
6016 && VAR_OR_FUNCTION_DECL_P (decl))
6017 /* Set DECL_COMDAT on template instantiations; if we force
6018 them to be emitted by explicit instantiation,
6019 mark_needed will tell cgraph to do the right thing. */
6020 DECL_COMDAT (decl) = true;
6021
6022 return DECL_TEMPLATE_RESULT (tmpl);
6023 }
6024
6025 tree
6026 push_template_decl (tree decl)
6027 {
6028 return push_template_decl_real (decl, false);
6029 }
6030
6031 /* FN is an inheriting constructor that inherits from the constructor
6032 template INHERITED; turn FN into a constructor template with a matching
6033 template header. */
6034
6035 tree
6036 add_inherited_template_parms (tree fn, tree inherited)
6037 {
6038 tree inner_parms
6039 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6040 inner_parms = copy_node (inner_parms);
6041 tree parms
6042 = tree_cons (size_int (processing_template_decl + 1),
6043 inner_parms, current_template_parms);
6044 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6045 tree args = template_parms_to_args (parms);
6046 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6047 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6048 DECL_TEMPLATE_RESULT (tmpl) = fn;
6049 DECL_ARTIFICIAL (tmpl) = true;
6050 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6051 return tmpl;
6052 }
6053
6054 /* Called when a class template TYPE is redeclared with the indicated
6055 template PARMS, e.g.:
6056
6057 template <class T> struct S;
6058 template <class T> struct S {}; */
6059
6060 bool
6061 redeclare_class_template (tree type, tree parms, tree cons)
6062 {
6063 tree tmpl;
6064 tree tmpl_parms;
6065 int i;
6066
6067 if (!TYPE_TEMPLATE_INFO (type))
6068 {
6069 error ("%qT is not a template type", type);
6070 return false;
6071 }
6072
6073 tmpl = TYPE_TI_TEMPLATE (type);
6074 if (!PRIMARY_TEMPLATE_P (tmpl))
6075 /* The type is nested in some template class. Nothing to worry
6076 about here; there are no new template parameters for the nested
6077 type. */
6078 return true;
6079
6080 if (!parms)
6081 {
6082 error ("template specifiers not specified in declaration of %qD",
6083 tmpl);
6084 return false;
6085 }
6086
6087 parms = INNERMOST_TEMPLATE_PARMS (parms);
6088 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6089
6090 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6091 {
6092 error_n (input_location, TREE_VEC_LENGTH (parms),
6093 "redeclared with %d template parameter",
6094 "redeclared with %d template parameters",
6095 TREE_VEC_LENGTH (parms));
6096 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6097 "previous declaration %qD used %d template parameter",
6098 "previous declaration %qD used %d template parameters",
6099 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6100 return false;
6101 }
6102
6103 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6104 {
6105 tree tmpl_parm;
6106 tree parm;
6107 tree tmpl_default;
6108 tree parm_default;
6109
6110 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6111 || TREE_VEC_ELT (parms, i) == error_mark_node)
6112 continue;
6113
6114 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6115 if (error_operand_p (tmpl_parm))
6116 return false;
6117
6118 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6119 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6120 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6121
6122 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6123 TEMPLATE_DECL. */
6124 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6125 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6126 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6127 || (TREE_CODE (tmpl_parm) != PARM_DECL
6128 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6129 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6130 || (TREE_CODE (tmpl_parm) == PARM_DECL
6131 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6132 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6133 {
6134 error ("template parameter %q+#D", tmpl_parm);
6135 error ("redeclared here as %q#D", parm);
6136 return false;
6137 }
6138
6139 /* The parameters can be declared to introduce different
6140 constraints. */
6141 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6142 tree p2 = TREE_VEC_ELT (parms, i);
6143 if (!template_parameter_constraints_equivalent_p (p1, p2))
6144 {
6145 error ("declaration of template parameter %q+#D with different "
6146 "constraints", parm);
6147 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6148 "original declaration appeared here");
6149 return false;
6150 }
6151
6152 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6153 {
6154 /* We have in [temp.param]:
6155
6156 A template-parameter may not be given default arguments
6157 by two different declarations in the same scope. */
6158 error_at (input_location, "redefinition of default argument for %q#D", parm);
6159 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6160 "original definition appeared here");
6161 return false;
6162 }
6163
6164 if (parm_default != NULL_TREE)
6165 /* Update the previous template parameters (which are the ones
6166 that will really count) with the new default value. */
6167 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6168 else if (tmpl_default != NULL_TREE)
6169 /* Update the new parameters, too; they'll be used as the
6170 parameters for any members. */
6171 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6172
6173 /* Give each template template parm in this redeclaration a
6174 DECL_CONTEXT of the template for which they are a parameter. */
6175 if (TREE_CODE (parm) == TEMPLATE_DECL)
6176 {
6177 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6178 DECL_CONTEXT (parm) = tmpl;
6179 }
6180
6181 if (TREE_CODE (parm) == TYPE_DECL)
6182 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6183 }
6184
6185 tree ci = get_constraints (tmpl);
6186 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6187 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6188
6189 /* Two classes with different constraints declare different entities. */
6190 if (!cp_tree_equal (req1, req2))
6191 {
6192 error_at (input_location, "redeclaration %q#D with different "
6193 "constraints", tmpl);
6194 inform (DECL_SOURCE_LOCATION (tmpl),
6195 "original declaration appeared here");
6196 return false;
6197 }
6198
6199 return true;
6200 }
6201
6202 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6203 to be used when the caller has already checked
6204 (processing_template_decl
6205 && !instantiation_dependent_expression_p (expr)
6206 && potential_constant_expression (expr))
6207 and cleared processing_template_decl. */
6208
6209 tree
6210 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6211 {
6212 return tsubst_copy_and_build (expr,
6213 /*args=*/NULL_TREE,
6214 complain,
6215 /*in_decl=*/NULL_TREE,
6216 /*function_p=*/false,
6217 /*integral_constant_expression_p=*/true);
6218 }
6219
6220 /* Simplify EXPR if it is a non-dependent expression. Returns the
6221 (possibly simplified) expression. */
6222
6223 tree
6224 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6225 {
6226 if (expr == NULL_TREE)
6227 return NULL_TREE;
6228
6229 /* If we're in a template, but EXPR isn't value dependent, simplify
6230 it. We're supposed to treat:
6231
6232 template <typename T> void f(T[1 + 1]);
6233 template <typename T> void f(T[2]);
6234
6235 as two declarations of the same function, for example. */
6236 if (processing_template_decl
6237 && is_nondependent_constant_expression (expr))
6238 {
6239 processing_template_decl_sentinel s;
6240 expr = instantiate_non_dependent_expr_internal (expr, complain);
6241 }
6242 return expr;
6243 }
6244
6245 tree
6246 instantiate_non_dependent_expr (tree expr)
6247 {
6248 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6249 }
6250
6251 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6252 an uninstantiated expression. */
6253
6254 tree
6255 instantiate_non_dependent_or_null (tree expr)
6256 {
6257 if (expr == NULL_TREE)
6258 return NULL_TREE;
6259 if (processing_template_decl)
6260 {
6261 if (!is_nondependent_constant_expression (expr))
6262 expr = NULL_TREE;
6263 else
6264 {
6265 processing_template_decl_sentinel s;
6266 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6267 }
6268 }
6269 return expr;
6270 }
6271
6272 /* True iff T is a specialization of a variable template. */
6273
6274 bool
6275 variable_template_specialization_p (tree t)
6276 {
6277 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6278 return false;
6279 tree tmpl = DECL_TI_TEMPLATE (t);
6280 return variable_template_p (tmpl);
6281 }
6282
6283 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6284 template declaration, or a TYPE_DECL for an alias declaration. */
6285
6286 bool
6287 alias_type_or_template_p (tree t)
6288 {
6289 if (t == NULL_TREE)
6290 return false;
6291 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6292 || (TYPE_P (t)
6293 && TYPE_NAME (t)
6294 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6295 || DECL_ALIAS_TEMPLATE_P (t));
6296 }
6297
6298 /* Return TRUE iff T is a specialization of an alias template. */
6299
6300 bool
6301 alias_template_specialization_p (const_tree t)
6302 {
6303 /* It's an alias template specialization if it's an alias and its
6304 TYPE_NAME is a specialization of a primary template. */
6305 if (TYPE_ALIAS_P (t))
6306 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6307 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6308
6309 return false;
6310 }
6311
6312 /* An alias template is complex from a SFINAE perspective if a template-id
6313 using that alias can be ill-formed when the expansion is not, as with
6314 the void_t template. We determine this by checking whether the
6315 expansion for the alias template uses all its template parameters. */
6316
6317 struct uses_all_template_parms_data
6318 {
6319 int level;
6320 bool *seen;
6321 };
6322
6323 static int
6324 uses_all_template_parms_r (tree t, void *data_)
6325 {
6326 struct uses_all_template_parms_data &data
6327 = *(struct uses_all_template_parms_data*)data_;
6328 tree idx = get_template_parm_index (t);
6329
6330 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6331 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6332 return 0;
6333 }
6334
6335 static bool
6336 complex_alias_template_p (const_tree tmpl)
6337 {
6338 struct uses_all_template_parms_data data;
6339 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6340 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6341 data.level = TMPL_PARMS_DEPTH (parms);
6342 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6343 data.seen = XALLOCAVEC (bool, len);
6344 for (int i = 0; i < len; ++i)
6345 data.seen[i] = false;
6346
6347 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6348 for (int i = 0; i < len; ++i)
6349 if (!data.seen[i])
6350 return true;
6351 return false;
6352 }
6353
6354 /* Return TRUE iff T is a specialization of a complex alias template with
6355 dependent template-arguments. */
6356
6357 bool
6358 dependent_alias_template_spec_p (const_tree t)
6359 {
6360 if (!alias_template_specialization_p (t))
6361 return false;
6362
6363 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6364 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6365 return false;
6366
6367 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6368 if (!any_dependent_template_arguments_p (args))
6369 return false;
6370
6371 return true;
6372 }
6373
6374 /* Return the number of innermost template parameters in TMPL. */
6375
6376 static int
6377 num_innermost_template_parms (tree tmpl)
6378 {
6379 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6380 return TREE_VEC_LENGTH (parms);
6381 }
6382
6383 /* Return either TMPL or another template that it is equivalent to under DR
6384 1286: An alias that just changes the name of a template is equivalent to
6385 the other template. */
6386
6387 static tree
6388 get_underlying_template (tree tmpl)
6389 {
6390 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6391 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6392 {
6393 /* Determine if the alias is equivalent to an underlying template. */
6394 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6395 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6396 if (!tinfo)
6397 break;
6398
6399 tree underlying = TI_TEMPLATE (tinfo);
6400 if (!PRIMARY_TEMPLATE_P (underlying)
6401 || (num_innermost_template_parms (tmpl)
6402 != num_innermost_template_parms (underlying)))
6403 break;
6404
6405 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6406 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6407 break;
6408
6409 /* Alias is equivalent. Strip it and repeat. */
6410 tmpl = underlying;
6411 }
6412
6413 return tmpl;
6414 }
6415
6416 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6417 must be a reference-to-function or a pointer-to-function type, as specified
6418 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6419 and check that the resulting function has external linkage. */
6420
6421 static tree
6422 convert_nontype_argument_function (tree type, tree expr,
6423 tsubst_flags_t complain)
6424 {
6425 tree fns = expr;
6426 tree fn, fn_no_ptr;
6427 linkage_kind linkage;
6428
6429 fn = instantiate_type (type, fns, tf_none);
6430 if (fn == error_mark_node)
6431 return error_mark_node;
6432
6433 if (value_dependent_expression_p (fn))
6434 goto accept;
6435
6436 fn_no_ptr = strip_fnptr_conv (fn);
6437 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6438 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6439 if (BASELINK_P (fn_no_ptr))
6440 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6441
6442 /* [temp.arg.nontype]/1
6443
6444 A template-argument for a non-type, non-template template-parameter
6445 shall be one of:
6446 [...]
6447 -- the address of an object or function with external [C++11: or
6448 internal] linkage. */
6449
6450 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6451 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6452 {
6453 if (complain & tf_error)
6454 {
6455 location_t loc = cp_expr_loc_or_input_loc (expr);
6456 error_at (loc, "%qE is not a valid template argument for type %qT",
6457 expr, type);
6458 if (TYPE_PTR_P (type))
6459 inform (loc, "it must be the address of a function "
6460 "with external linkage");
6461 else
6462 inform (loc, "it must be the name of a function with "
6463 "external linkage");
6464 }
6465 return NULL_TREE;
6466 }
6467
6468 linkage = decl_linkage (fn_no_ptr);
6469 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6470 {
6471 if (complain & tf_error)
6472 {
6473 location_t loc = cp_expr_loc_or_input_loc (expr);
6474 if (cxx_dialect >= cxx11)
6475 error_at (loc, "%qE is not a valid template argument for type "
6476 "%qT because %qD has no linkage",
6477 expr, type, fn_no_ptr);
6478 else
6479 error_at (loc, "%qE is not a valid template argument for type "
6480 "%qT because %qD does not have external linkage",
6481 expr, type, fn_no_ptr);
6482 }
6483 return NULL_TREE;
6484 }
6485
6486 accept:
6487 if (TYPE_REF_P (type))
6488 {
6489 if (REFERENCE_REF_P (fn))
6490 fn = TREE_OPERAND (fn, 0);
6491 else
6492 fn = build_address (fn);
6493 }
6494 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6495 fn = build_nop (type, fn);
6496
6497 return fn;
6498 }
6499
6500 /* Subroutine of convert_nontype_argument.
6501 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6502 Emit an error otherwise. */
6503
6504 static bool
6505 check_valid_ptrmem_cst_expr (tree type, tree expr,
6506 tsubst_flags_t complain)
6507 {
6508 tree orig_expr = expr;
6509 STRIP_NOPS (expr);
6510 if (null_ptr_cst_p (expr))
6511 return true;
6512 if (TREE_CODE (expr) == PTRMEM_CST
6513 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6514 PTRMEM_CST_CLASS (expr)))
6515 return true;
6516 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6517 return true;
6518 if (processing_template_decl
6519 && TREE_CODE (expr) == ADDR_EXPR
6520 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6521 return true;
6522 if (complain & tf_error)
6523 {
6524 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6525 error_at (loc, "%qE is not a valid template argument for type %qT",
6526 orig_expr, type);
6527 if (TREE_CODE (expr) != PTRMEM_CST)
6528 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6529 else
6530 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6531 }
6532 return false;
6533 }
6534
6535 /* Returns TRUE iff the address of OP is value-dependent.
6536
6537 14.6.2.4 [temp.dep.temp]:
6538 A non-integral non-type template-argument is dependent if its type is
6539 dependent or it has either of the following forms
6540 qualified-id
6541 & qualified-id
6542 and contains a nested-name-specifier which specifies a class-name that
6543 names a dependent type.
6544
6545 We generalize this to just say that the address of a member of a
6546 dependent class is value-dependent; the above doesn't cover the
6547 address of a static data member named with an unqualified-id. */
6548
6549 static bool
6550 has_value_dependent_address (tree op)
6551 {
6552 STRIP_ANY_LOCATION_WRAPPER (op);
6553
6554 /* We could use get_inner_reference here, but there's no need;
6555 this is only relevant for template non-type arguments, which
6556 can only be expressed as &id-expression. */
6557 if (DECL_P (op))
6558 {
6559 tree ctx = CP_DECL_CONTEXT (op);
6560 if (TYPE_P (ctx) && dependent_type_p (ctx))
6561 return true;
6562 }
6563
6564 return false;
6565 }
6566
6567 /* The next set of functions are used for providing helpful explanatory
6568 diagnostics for failed overload resolution. Their messages should be
6569 indented by two spaces for consistency with the messages in
6570 call.c */
6571
6572 static int
6573 unify_success (bool /*explain_p*/)
6574 {
6575 return 0;
6576 }
6577
6578 /* Other failure functions should call this one, to provide a single function
6579 for setting a breakpoint on. */
6580
6581 static int
6582 unify_invalid (bool /*explain_p*/)
6583 {
6584 return 1;
6585 }
6586
6587 static int
6588 unify_parameter_deduction_failure (bool explain_p, tree parm)
6589 {
6590 if (explain_p)
6591 inform (input_location,
6592 " couldn%'t deduce template parameter %qD", parm);
6593 return unify_invalid (explain_p);
6594 }
6595
6596 static int
6597 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6598 {
6599 if (explain_p)
6600 inform (input_location,
6601 " types %qT and %qT have incompatible cv-qualifiers",
6602 parm, arg);
6603 return unify_invalid (explain_p);
6604 }
6605
6606 static int
6607 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6608 {
6609 if (explain_p)
6610 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6611 return unify_invalid (explain_p);
6612 }
6613
6614 static int
6615 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6616 {
6617 if (explain_p)
6618 inform (input_location,
6619 " template parameter %qD is not a parameter pack, but "
6620 "argument %qD is",
6621 parm, arg);
6622 return unify_invalid (explain_p);
6623 }
6624
6625 static int
6626 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6627 {
6628 if (explain_p)
6629 inform (input_location,
6630 " template argument %qE does not match "
6631 "pointer-to-member constant %qE",
6632 arg, parm);
6633 return unify_invalid (explain_p);
6634 }
6635
6636 static int
6637 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6638 {
6639 if (explain_p)
6640 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6641 return unify_invalid (explain_p);
6642 }
6643
6644 static int
6645 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6646 {
6647 if (explain_p)
6648 inform (input_location,
6649 " inconsistent parameter pack deduction with %qT and %qT",
6650 old_arg, new_arg);
6651 return unify_invalid (explain_p);
6652 }
6653
6654 static int
6655 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6656 {
6657 if (explain_p)
6658 {
6659 if (TYPE_P (parm))
6660 inform (input_location,
6661 " deduced conflicting types for parameter %qT (%qT and %qT)",
6662 parm, first, second);
6663 else
6664 inform (input_location,
6665 " deduced conflicting values for non-type parameter "
6666 "%qE (%qE and %qE)", parm, first, second);
6667 }
6668 return unify_invalid (explain_p);
6669 }
6670
6671 static int
6672 unify_vla_arg (bool explain_p, tree arg)
6673 {
6674 if (explain_p)
6675 inform (input_location,
6676 " variable-sized array type %qT is not "
6677 "a valid template argument",
6678 arg);
6679 return unify_invalid (explain_p);
6680 }
6681
6682 static int
6683 unify_method_type_error (bool explain_p, tree arg)
6684 {
6685 if (explain_p)
6686 inform (input_location,
6687 " member function type %qT is not a valid template argument",
6688 arg);
6689 return unify_invalid (explain_p);
6690 }
6691
6692 static int
6693 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6694 {
6695 if (explain_p)
6696 {
6697 if (least_p)
6698 inform_n (input_location, wanted,
6699 " candidate expects at least %d argument, %d provided",
6700 " candidate expects at least %d arguments, %d provided",
6701 wanted, have);
6702 else
6703 inform_n (input_location, wanted,
6704 " candidate expects %d argument, %d provided",
6705 " candidate expects %d arguments, %d provided",
6706 wanted, have);
6707 }
6708 return unify_invalid (explain_p);
6709 }
6710
6711 static int
6712 unify_too_many_arguments (bool explain_p, int have, int wanted)
6713 {
6714 return unify_arity (explain_p, have, wanted);
6715 }
6716
6717 static int
6718 unify_too_few_arguments (bool explain_p, int have, int wanted,
6719 bool least_p = false)
6720 {
6721 return unify_arity (explain_p, have, wanted, least_p);
6722 }
6723
6724 static int
6725 unify_arg_conversion (bool explain_p, tree to_type,
6726 tree from_type, tree arg)
6727 {
6728 if (explain_p)
6729 inform (cp_expr_loc_or_input_loc (arg),
6730 " cannot convert %qE (type %qT) to type %qT",
6731 arg, from_type, to_type);
6732 return unify_invalid (explain_p);
6733 }
6734
6735 static int
6736 unify_no_common_base (bool explain_p, enum template_base_result r,
6737 tree parm, tree arg)
6738 {
6739 if (explain_p)
6740 switch (r)
6741 {
6742 case tbr_ambiguous_baseclass:
6743 inform (input_location, " %qT is an ambiguous base class of %qT",
6744 parm, arg);
6745 break;
6746 default:
6747 inform (input_location, " %qT is not derived from %qT", arg, parm);
6748 break;
6749 }
6750 return unify_invalid (explain_p);
6751 }
6752
6753 static int
6754 unify_inconsistent_template_template_parameters (bool explain_p)
6755 {
6756 if (explain_p)
6757 inform (input_location,
6758 " template parameters of a template template argument are "
6759 "inconsistent with other deduced template arguments");
6760 return unify_invalid (explain_p);
6761 }
6762
6763 static int
6764 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6765 {
6766 if (explain_p)
6767 inform (input_location,
6768 " cannot deduce a template for %qT from non-template type %qT",
6769 parm, arg);
6770 return unify_invalid (explain_p);
6771 }
6772
6773 static int
6774 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6775 {
6776 if (explain_p)
6777 inform (input_location,
6778 " template argument %qE does not match %qE", arg, parm);
6779 return unify_invalid (explain_p);
6780 }
6781
6782 /* True if T is a C++20 template parameter object to store the argument for a
6783 template parameter of class type. */
6784
6785 bool
6786 template_parm_object_p (const_tree t)
6787 {
6788 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6789 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6790 }
6791
6792 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6793 argument for TYPE, points to an unsuitable object. */
6794
6795 static bool
6796 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6797 {
6798 switch (TREE_CODE (expr))
6799 {
6800 CASE_CONVERT:
6801 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6802 complain);
6803
6804 case TARGET_EXPR:
6805 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6806 complain);
6807
6808 case CONSTRUCTOR:
6809 {
6810 unsigned i; tree elt;
6811 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6812 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6813 return true;
6814 }
6815 break;
6816
6817 case ADDR_EXPR:
6818 {
6819 tree decl = TREE_OPERAND (expr, 0);
6820
6821 if (!VAR_P (decl))
6822 {
6823 if (complain & tf_error)
6824 error_at (cp_expr_loc_or_input_loc (expr),
6825 "%qE is not a valid template argument of type %qT "
6826 "because %qE is not a variable", expr, type, decl);
6827 return true;
6828 }
6829 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6830 {
6831 if (complain & tf_error)
6832 error_at (cp_expr_loc_or_input_loc (expr),
6833 "%qE is not a valid template argument of type %qT "
6834 "in C++98 because %qD does not have external linkage",
6835 expr, type, decl);
6836 return true;
6837 }
6838 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6839 && decl_linkage (decl) == lk_none)
6840 {
6841 if (complain & tf_error)
6842 error_at (cp_expr_loc_or_input_loc (expr),
6843 "%qE is not a valid template argument of type %qT "
6844 "because %qD has no linkage", expr, type, decl);
6845 return true;
6846 }
6847 /* C++17: For a non-type template-parameter of reference or pointer
6848 type, the value of the constant expression shall not refer to (or
6849 for a pointer type, shall not be the address of):
6850 * a subobject (4.5),
6851 * a temporary object (15.2),
6852 * a string literal (5.13.5),
6853 * the result of a typeid expression (8.2.8), or
6854 * a predefined __func__ variable (11.4.1). */
6855 else if (DECL_ARTIFICIAL (decl))
6856 {
6857 if (complain & tf_error)
6858 error ("the address of %qD is not a valid template argument",
6859 decl);
6860 return true;
6861 }
6862 else if (!same_type_ignoring_top_level_qualifiers_p
6863 (strip_array_types (TREE_TYPE (type)),
6864 strip_array_types (TREE_TYPE (decl))))
6865 {
6866 if (complain & tf_error)
6867 error ("the address of the %qT subobject of %qD is not a "
6868 "valid template argument", TREE_TYPE (type), decl);
6869 return true;
6870 }
6871 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6872 {
6873 if (complain & tf_error)
6874 error ("the address of %qD is not a valid template argument "
6875 "because it does not have static storage duration",
6876 decl);
6877 return true;
6878 }
6879 }
6880 break;
6881
6882 default:
6883 if (!INDIRECT_TYPE_P (type))
6884 /* We're only concerned about pointers and references here. */;
6885 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6886 /* Null pointer values are OK in C++11. */;
6887 else
6888 {
6889 if (VAR_P (expr))
6890 {
6891 if (complain & tf_error)
6892 error ("%qD is not a valid template argument "
6893 "because %qD is a variable, not the address of "
6894 "a variable", expr, expr);
6895 return true;
6896 }
6897 else
6898 {
6899 if (complain & tf_error)
6900 error ("%qE is not a valid template argument for %qT "
6901 "because it is not the address of a variable",
6902 expr, type);
6903 return true;
6904 }
6905 }
6906 }
6907 return false;
6908
6909 }
6910
6911 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6912 template argument EXPR. */
6913
6914 static tree
6915 get_template_parm_object (tree expr, tsubst_flags_t complain)
6916 {
6917 if (TREE_CODE (expr) == TARGET_EXPR)
6918 expr = TARGET_EXPR_INITIAL (expr);
6919
6920 if (!TREE_CONSTANT (expr))
6921 {
6922 if ((complain & tf_error)
6923 && require_rvalue_constant_expression (expr))
6924 cxx_constant_value (expr);
6925 return error_mark_node;
6926 }
6927 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6928 return error_mark_node;
6929
6930 tree name = mangle_template_parm_object (expr);
6931 tree decl = get_global_binding (name);
6932 if (decl)
6933 return decl;
6934
6935 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6936 decl = create_temporary_var (type);
6937 TREE_STATIC (decl) = true;
6938 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6939 TREE_READONLY (decl) = true;
6940 DECL_NAME (decl) = name;
6941 SET_DECL_ASSEMBLER_NAME (decl, name);
6942 DECL_CONTEXT (decl) = global_namespace;
6943 comdat_linkage (decl);
6944 pushdecl_top_level_and_finish (decl, expr);
6945 return decl;
6946 }
6947
6948 /* Attempt to convert the non-type template parameter EXPR to the
6949 indicated TYPE. If the conversion is successful, return the
6950 converted value. If the conversion is unsuccessful, return
6951 NULL_TREE if we issued an error message, or error_mark_node if we
6952 did not. We issue error messages for out-and-out bad template
6953 parameters, but not simply because the conversion failed, since we
6954 might be just trying to do argument deduction. Both TYPE and EXPR
6955 must be non-dependent.
6956
6957 The conversion follows the special rules described in
6958 [temp.arg.nontype], and it is much more strict than an implicit
6959 conversion.
6960
6961 This function is called twice for each template argument (see
6962 lookup_template_class for a more accurate description of this
6963 problem). This means that we need to handle expressions which
6964 are not valid in a C++ source, but can be created from the
6965 first call (for instance, casts to perform conversions). These
6966 hacks can go away after we fix the double coercion problem. */
6967
6968 static tree
6969 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6970 {
6971 tree expr_type;
6972 location_t loc = cp_expr_loc_or_input_loc (expr);
6973
6974 /* Detect immediately string literals as invalid non-type argument.
6975 This special-case is not needed for correctness (we would easily
6976 catch this later), but only to provide better diagnostic for this
6977 common user mistake. As suggested by DR 100, we do not mention
6978 linkage issues in the diagnostic as this is not the point. */
6979 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6980 {
6981 if (complain & tf_error)
6982 error ("%qE is not a valid template argument for type %qT "
6983 "because string literals can never be used in this context",
6984 expr, type);
6985 return NULL_TREE;
6986 }
6987
6988 /* Add the ADDR_EXPR now for the benefit of
6989 value_dependent_expression_p. */
6990 if (TYPE_PTROBV_P (type)
6991 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6992 {
6993 expr = decay_conversion (expr, complain);
6994 if (expr == error_mark_node)
6995 return error_mark_node;
6996 }
6997
6998 /* If we are in a template, EXPR may be non-dependent, but still
6999 have a syntactic, rather than semantic, form. For example, EXPR
7000 might be a SCOPE_REF, rather than the VAR_DECL to which the
7001 SCOPE_REF refers. Preserving the qualifying scope is necessary
7002 so that access checking can be performed when the template is
7003 instantiated -- but here we need the resolved form so that we can
7004 convert the argument. */
7005 bool non_dep = false;
7006 if (TYPE_REF_OBJ_P (type)
7007 && has_value_dependent_address (expr))
7008 /* If we want the address and it's value-dependent, don't fold. */;
7009 else if (processing_template_decl
7010 && is_nondependent_constant_expression (expr))
7011 non_dep = true;
7012 if (error_operand_p (expr))
7013 return error_mark_node;
7014 expr_type = TREE_TYPE (expr);
7015
7016 /* If the argument is non-dependent, perform any conversions in
7017 non-dependent context as well. */
7018 processing_template_decl_sentinel s (non_dep);
7019 if (non_dep)
7020 expr = instantiate_non_dependent_expr_internal (expr, complain);
7021
7022 if (value_dependent_expression_p (expr))
7023 expr = canonicalize_expr_argument (expr, complain);
7024
7025 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7026 to a non-type argument of "nullptr". */
7027 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7028 expr = fold_simple (convert (type, expr));
7029
7030 /* In C++11, integral or enumeration non-type template arguments can be
7031 arbitrary constant expressions. Pointer and pointer to
7032 member arguments can be general constant expressions that evaluate
7033 to a null value, but otherwise still need to be of a specific form. */
7034 if (cxx_dialect >= cxx11)
7035 {
7036 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7037 /* A PTRMEM_CST is already constant, and a valid template
7038 argument for a parameter of pointer to member type, we just want
7039 to leave it in that form rather than lower it to a
7040 CONSTRUCTOR. */;
7041 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7042 || cxx_dialect >= cxx17)
7043 {
7044 /* Calling build_converted_constant_expr might create a call to
7045 a conversion function with a value-dependent argument, which
7046 could invoke taking the address of a temporary representing
7047 the result of the conversion. */
7048 if (COMPOUND_LITERAL_P (expr)
7049 && CONSTRUCTOR_IS_DEPENDENT (expr)
7050 && MAYBE_CLASS_TYPE_P (expr_type)
7051 && TYPE_HAS_CONVERSION (expr_type))
7052 {
7053 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
7054 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7055 return expr;
7056 }
7057 /* C++17: A template-argument for a non-type template-parameter shall
7058 be a converted constant expression (8.20) of the type of the
7059 template-parameter. */
7060 expr = build_converted_constant_expr (type, expr, complain);
7061 if (expr == error_mark_node)
7062 /* Make sure we return NULL_TREE only if we have really issued
7063 an error, as described above. */
7064 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7065 expr = maybe_constant_value (expr, NULL_TREE,
7066 /*manifestly_const_eval=*/true);
7067 expr = convert_from_reference (expr);
7068 }
7069 else if (TYPE_PTR_OR_PTRMEM_P (type))
7070 {
7071 tree folded = maybe_constant_value (expr, NULL_TREE,
7072 /*manifestly_const_eval=*/true);
7073 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7074 : null_member_pointer_value_p (folded))
7075 expr = folded;
7076 }
7077 }
7078
7079 if (TYPE_REF_P (type))
7080 expr = mark_lvalue_use (expr);
7081 else
7082 expr = mark_rvalue_use (expr);
7083
7084 /* HACK: Due to double coercion, we can get a
7085 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7086 which is the tree that we built on the first call (see
7087 below when coercing to reference to object or to reference to
7088 function). We just strip everything and get to the arg.
7089 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7090 for examples. */
7091 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7092 {
7093 tree probe_type, probe = expr;
7094 if (REFERENCE_REF_P (probe))
7095 probe = TREE_OPERAND (probe, 0);
7096 probe_type = TREE_TYPE (probe);
7097 if (TREE_CODE (probe) == NOP_EXPR)
7098 {
7099 /* ??? Maybe we could use convert_from_reference here, but we
7100 would need to relax its constraints because the NOP_EXPR
7101 could actually change the type to something more cv-qualified,
7102 and this is not folded by convert_from_reference. */
7103 tree addr = TREE_OPERAND (probe, 0);
7104 if (TYPE_REF_P (probe_type)
7105 && TREE_CODE (addr) == ADDR_EXPR
7106 && TYPE_PTR_P (TREE_TYPE (addr))
7107 && (same_type_ignoring_top_level_qualifiers_p
7108 (TREE_TYPE (probe_type),
7109 TREE_TYPE (TREE_TYPE (addr)))))
7110 {
7111 expr = TREE_OPERAND (addr, 0);
7112 expr_type = TREE_TYPE (probe_type);
7113 }
7114 }
7115 }
7116
7117 /* [temp.arg.nontype]/5, bullet 1
7118
7119 For a non-type template-parameter of integral or enumeration type,
7120 integral promotions (_conv.prom_) and integral conversions
7121 (_conv.integral_) are applied. */
7122 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7123 {
7124 if (cxx_dialect < cxx11)
7125 {
7126 tree t = build_converted_constant_expr (type, expr, complain);
7127 t = maybe_constant_value (t);
7128 if (t != error_mark_node)
7129 expr = t;
7130 }
7131
7132 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7133 return error_mark_node;
7134
7135 /* Notice that there are constant expressions like '4 % 0' which
7136 do not fold into integer constants. */
7137 if (TREE_CODE (expr) != INTEGER_CST
7138 && !value_dependent_expression_p (expr))
7139 {
7140 if (complain & tf_error)
7141 {
7142 int errs = errorcount, warns = warningcount + werrorcount;
7143 if (!require_potential_constant_expression (expr))
7144 expr = error_mark_node;
7145 else
7146 expr = cxx_constant_value (expr);
7147 if (errorcount > errs || warningcount + werrorcount > warns)
7148 inform (loc, "in template argument for type %qT", type);
7149 if (expr == error_mark_node)
7150 return NULL_TREE;
7151 /* else cxx_constant_value complained but gave us
7152 a real constant, so go ahead. */
7153 if (TREE_CODE (expr) != INTEGER_CST)
7154 {
7155 /* Some assemble time constant expressions like
7156 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7157 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7158 as we can emit them into .rodata initializers of
7159 variables, yet they can't fold into an INTEGER_CST at
7160 compile time. Refuse them here. */
7161 gcc_checking_assert (reduced_constant_expression_p (expr));
7162 error_at (loc, "template argument %qE for type %qT not "
7163 "a constant integer", expr, type);
7164 return NULL_TREE;
7165 }
7166 }
7167 else
7168 return NULL_TREE;
7169 }
7170
7171 /* Avoid typedef problems. */
7172 if (TREE_TYPE (expr) != type)
7173 expr = fold_convert (type, expr);
7174 }
7175 /* [temp.arg.nontype]/5, bullet 2
7176
7177 For a non-type template-parameter of type pointer to object,
7178 qualification conversions (_conv.qual_) and the array-to-pointer
7179 conversion (_conv.array_) are applied. */
7180 else if (TYPE_PTROBV_P (type))
7181 {
7182 tree decayed = expr;
7183
7184 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7185 decay_conversion or an explicit cast. If it's a problematic cast,
7186 we'll complain about it below. */
7187 if (TREE_CODE (expr) == NOP_EXPR)
7188 {
7189 tree probe = expr;
7190 STRIP_NOPS (probe);
7191 if (TREE_CODE (probe) == ADDR_EXPR
7192 && TYPE_PTR_P (TREE_TYPE (probe)))
7193 {
7194 expr = probe;
7195 expr_type = TREE_TYPE (expr);
7196 }
7197 }
7198
7199 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7200
7201 A template-argument for a non-type, non-template template-parameter
7202 shall be one of: [...]
7203
7204 -- the name of a non-type template-parameter;
7205 -- the address of an object or function with external linkage, [...]
7206 expressed as "& id-expression" where the & is optional if the name
7207 refers to a function or array, or if the corresponding
7208 template-parameter is a reference.
7209
7210 Here, we do not care about functions, as they are invalid anyway
7211 for a parameter of type pointer-to-object. */
7212
7213 if (value_dependent_expression_p (expr))
7214 /* Non-type template parameters are OK. */
7215 ;
7216 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7217 /* Null pointer values are OK in C++11. */;
7218 else if (TREE_CODE (expr) != ADDR_EXPR
7219 && !INDIRECT_TYPE_P (expr_type))
7220 /* Other values, like integer constants, might be valid
7221 non-type arguments of some other type. */
7222 return error_mark_node;
7223 else if (invalid_tparm_referent_p (type, expr, complain))
7224 return NULL_TREE;
7225
7226 expr = decayed;
7227
7228 expr = perform_qualification_conversions (type, expr);
7229 if (expr == error_mark_node)
7230 return error_mark_node;
7231 }
7232 /* [temp.arg.nontype]/5, bullet 3
7233
7234 For a non-type template-parameter of type reference to object, no
7235 conversions apply. The type referred to by the reference may be more
7236 cv-qualified than the (otherwise identical) type of the
7237 template-argument. The template-parameter is bound directly to the
7238 template-argument, which must be an lvalue. */
7239 else if (TYPE_REF_OBJ_P (type))
7240 {
7241 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7242 expr_type))
7243 return error_mark_node;
7244
7245 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7246 {
7247 if (complain & tf_error)
7248 error ("%qE is not a valid template argument for type %qT "
7249 "because of conflicts in cv-qualification", expr, type);
7250 return NULL_TREE;
7251 }
7252
7253 if (!lvalue_p (expr))
7254 {
7255 if (complain & tf_error)
7256 error ("%qE is not a valid template argument for type %qT "
7257 "because it is not an lvalue", expr, type);
7258 return NULL_TREE;
7259 }
7260
7261 /* [temp.arg.nontype]/1
7262
7263 A template-argument for a non-type, non-template template-parameter
7264 shall be one of: [...]
7265
7266 -- the address of an object or function with external linkage. */
7267 if (INDIRECT_REF_P (expr)
7268 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7269 {
7270 expr = TREE_OPERAND (expr, 0);
7271 if (DECL_P (expr))
7272 {
7273 if (complain & tf_error)
7274 error ("%q#D is not a valid template argument for type %qT "
7275 "because a reference variable does not have a constant "
7276 "address", expr, type);
7277 return NULL_TREE;
7278 }
7279 }
7280
7281 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7282 && value_dependent_expression_p (expr))
7283 /* OK, dependent reference. We don't want to ask whether a DECL is
7284 itself value-dependent, since what we want here is its address. */;
7285 else
7286 {
7287 expr = build_address (expr);
7288
7289 if (invalid_tparm_referent_p (type, expr, complain))
7290 return NULL_TREE;
7291 }
7292
7293 if (!same_type_p (type, TREE_TYPE (expr)))
7294 expr = build_nop (type, expr);
7295 }
7296 /* [temp.arg.nontype]/5, bullet 4
7297
7298 For a non-type template-parameter of type pointer to function, only
7299 the function-to-pointer conversion (_conv.func_) is applied. If the
7300 template-argument represents a set of overloaded functions (or a
7301 pointer to such), the matching function is selected from the set
7302 (_over.over_). */
7303 else if (TYPE_PTRFN_P (type))
7304 {
7305 /* If the argument is a template-id, we might not have enough
7306 context information to decay the pointer. */
7307 if (!type_unknown_p (expr_type))
7308 {
7309 expr = decay_conversion (expr, complain);
7310 if (expr == error_mark_node)
7311 return error_mark_node;
7312 }
7313
7314 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7315 /* Null pointer values are OK in C++11. */
7316 return perform_qualification_conversions (type, expr);
7317
7318 expr = convert_nontype_argument_function (type, expr, complain);
7319 if (!expr || expr == error_mark_node)
7320 return expr;
7321 }
7322 /* [temp.arg.nontype]/5, bullet 5
7323
7324 For a non-type template-parameter of type reference to function, no
7325 conversions apply. If the template-argument represents a set of
7326 overloaded functions, the matching function is selected from the set
7327 (_over.over_). */
7328 else if (TYPE_REFFN_P (type))
7329 {
7330 if (TREE_CODE (expr) == ADDR_EXPR)
7331 {
7332 if (complain & tf_error)
7333 {
7334 error ("%qE is not a valid template argument for type %qT "
7335 "because it is a pointer", expr, type);
7336 inform (input_location, "try using %qE instead",
7337 TREE_OPERAND (expr, 0));
7338 }
7339 return NULL_TREE;
7340 }
7341
7342 expr = convert_nontype_argument_function (type, expr, complain);
7343 if (!expr || expr == error_mark_node)
7344 return expr;
7345 }
7346 /* [temp.arg.nontype]/5, bullet 6
7347
7348 For a non-type template-parameter of type pointer to member function,
7349 no conversions apply. If the template-argument represents a set of
7350 overloaded member functions, the matching member function is selected
7351 from the set (_over.over_). */
7352 else if (TYPE_PTRMEMFUNC_P (type))
7353 {
7354 expr = instantiate_type (type, expr, tf_none);
7355 if (expr == error_mark_node)
7356 return error_mark_node;
7357
7358 /* [temp.arg.nontype] bullet 1 says the pointer to member
7359 expression must be a pointer-to-member constant. */
7360 if (!value_dependent_expression_p (expr)
7361 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7362 return NULL_TREE;
7363
7364 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7365 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7366 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7367 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7368 }
7369 /* [temp.arg.nontype]/5, bullet 7
7370
7371 For a non-type template-parameter of type pointer to data member,
7372 qualification conversions (_conv.qual_) are applied. */
7373 else if (TYPE_PTRDATAMEM_P (type))
7374 {
7375 /* [temp.arg.nontype] bullet 1 says the pointer to member
7376 expression must be a pointer-to-member constant. */
7377 if (!value_dependent_expression_p (expr)
7378 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7379 return NULL_TREE;
7380
7381 expr = perform_qualification_conversions (type, expr);
7382 if (expr == error_mark_node)
7383 return expr;
7384 }
7385 else if (NULLPTR_TYPE_P (type))
7386 {
7387 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7388 {
7389 if (complain & tf_error)
7390 error ("%qE is not a valid template argument for type %qT "
7391 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7392 return NULL_TREE;
7393 }
7394 return expr;
7395 }
7396 else if (CLASS_TYPE_P (type))
7397 {
7398 /* Replace the argument with a reference to the corresponding template
7399 parameter object. */
7400 if (!value_dependent_expression_p (expr))
7401 expr = get_template_parm_object (expr, complain);
7402 if (expr == error_mark_node)
7403 return NULL_TREE;
7404 }
7405 /* A template non-type parameter must be one of the above. */
7406 else
7407 gcc_unreachable ();
7408
7409 /* Sanity check: did we actually convert the argument to the
7410 right type? */
7411 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7412 (type, TREE_TYPE (expr)));
7413 return convert_from_reference (expr);
7414 }
7415
7416 /* Subroutine of coerce_template_template_parms, which returns 1 if
7417 PARM_PARM and ARG_PARM match using the rule for the template
7418 parameters of template template parameters. Both PARM and ARG are
7419 template parameters; the rest of the arguments are the same as for
7420 coerce_template_template_parms.
7421 */
7422 static int
7423 coerce_template_template_parm (tree parm,
7424 tree arg,
7425 tsubst_flags_t complain,
7426 tree in_decl,
7427 tree outer_args)
7428 {
7429 if (arg == NULL_TREE || error_operand_p (arg)
7430 || parm == NULL_TREE || error_operand_p (parm))
7431 return 0;
7432
7433 if (TREE_CODE (arg) != TREE_CODE (parm))
7434 return 0;
7435
7436 switch (TREE_CODE (parm))
7437 {
7438 case TEMPLATE_DECL:
7439 /* We encounter instantiations of templates like
7440 template <template <template <class> class> class TT>
7441 class C; */
7442 {
7443 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7444 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7445
7446 if (!coerce_template_template_parms
7447 (parmparm, argparm, complain, in_decl, outer_args))
7448 return 0;
7449 }
7450 /* Fall through. */
7451
7452 case TYPE_DECL:
7453 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7454 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7455 /* Argument is a parameter pack but parameter is not. */
7456 return 0;
7457 break;
7458
7459 case PARM_DECL:
7460 /* The tsubst call is used to handle cases such as
7461
7462 template <int> class C {};
7463 template <class T, template <T> class TT> class D {};
7464 D<int, C> d;
7465
7466 i.e. the parameter list of TT depends on earlier parameters. */
7467 if (!uses_template_parms (TREE_TYPE (arg)))
7468 {
7469 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7470 if (!uses_template_parms (t)
7471 && !same_type_p (t, TREE_TYPE (arg)))
7472 return 0;
7473 }
7474
7475 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7476 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7477 /* Argument is a parameter pack but parameter is not. */
7478 return 0;
7479
7480 break;
7481
7482 default:
7483 gcc_unreachable ();
7484 }
7485
7486 return 1;
7487 }
7488
7489 /* Coerce template argument list ARGLIST for use with template
7490 template-parameter TEMPL. */
7491
7492 static tree
7493 coerce_template_args_for_ttp (tree templ, tree arglist,
7494 tsubst_flags_t complain)
7495 {
7496 /* Consider an example where a template template parameter declared as
7497
7498 template <class T, class U = std::allocator<T> > class TT
7499
7500 The template parameter level of T and U are one level larger than
7501 of TT. To proper process the default argument of U, say when an
7502 instantiation `TT<int>' is seen, we need to build the full
7503 arguments containing {int} as the innermost level. Outer levels,
7504 available when not appearing as default template argument, can be
7505 obtained from the arguments of the enclosing template.
7506
7507 Suppose that TT is later substituted with std::vector. The above
7508 instantiation is `TT<int, std::allocator<T> >' with TT at
7509 level 1, and T at level 2, while the template arguments at level 1
7510 becomes {std::vector} and the inner level 2 is {int}. */
7511
7512 tree outer = DECL_CONTEXT (templ);
7513 if (outer)
7514 outer = generic_targs_for (outer);
7515 else if (current_template_parms)
7516 {
7517 /* This is an argument of the current template, so we haven't set
7518 DECL_CONTEXT yet. */
7519 tree relevant_template_parms;
7520
7521 /* Parameter levels that are greater than the level of the given
7522 template template parm are irrelevant. */
7523 relevant_template_parms = current_template_parms;
7524 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7525 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7526 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7527
7528 outer = template_parms_to_args (relevant_template_parms);
7529 }
7530
7531 if (outer)
7532 arglist = add_to_template_args (outer, arglist);
7533
7534 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7535 return coerce_template_parms (parmlist, arglist, templ,
7536 complain,
7537 /*require_all_args=*/true,
7538 /*use_default_args=*/true);
7539 }
7540
7541 /* A cache of template template parameters with match-all default
7542 arguments. */
7543 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7544
7545 /* T is a bound template template-parameter. Copy its arguments into default
7546 arguments of the template template-parameter's template parameters. */
7547
7548 static tree
7549 add_defaults_to_ttp (tree otmpl)
7550 {
7551 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7552 return *c;
7553
7554 tree ntmpl = copy_node (otmpl);
7555
7556 tree ntype = copy_node (TREE_TYPE (otmpl));
7557 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7558 TYPE_MAIN_VARIANT (ntype) = ntype;
7559 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7560 TYPE_NAME (ntype) = ntmpl;
7561 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7562
7563 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7564 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7565 TEMPLATE_PARM_DECL (idx) = ntmpl;
7566 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7567
7568 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7569 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7570 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7571 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7572 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7573 {
7574 tree o = TREE_VEC_ELT (vec, i);
7575 if (!template_parameter_pack_p (TREE_VALUE (o)))
7576 {
7577 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7578 TREE_PURPOSE (n) = any_targ_node;
7579 }
7580 }
7581
7582 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7583 return ntmpl;
7584 }
7585
7586 /* ARG is a bound potential template template-argument, and PARGS is a list
7587 of arguments for the corresponding template template-parameter. Adjust
7588 PARGS as appropriate for application to ARG's template, and if ARG is a
7589 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7590 arguments to the template template parameter. */
7591
7592 static tree
7593 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7594 {
7595 ++processing_template_decl;
7596 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7597 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7598 {
7599 /* When comparing two template template-parameters in partial ordering,
7600 rewrite the one currently being used as an argument to have default
7601 arguments for all parameters. */
7602 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7603 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7604 if (pargs != error_mark_node)
7605 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7606 TYPE_TI_ARGS (arg));
7607 }
7608 else
7609 {
7610 tree aparms
7611 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7612 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7613 /*require_all*/true,
7614 /*use_default*/true);
7615 }
7616 --processing_template_decl;
7617 return pargs;
7618 }
7619
7620 /* Subroutine of unify for the case when PARM is a
7621 BOUND_TEMPLATE_TEMPLATE_PARM. */
7622
7623 static int
7624 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7625 bool explain_p)
7626 {
7627 tree parmvec = TYPE_TI_ARGS (parm);
7628 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7629
7630 /* The template template parm might be variadic and the argument
7631 not, so flatten both argument lists. */
7632 parmvec = expand_template_argument_pack (parmvec);
7633 argvec = expand_template_argument_pack (argvec);
7634
7635 if (flag_new_ttp)
7636 {
7637 /* In keeping with P0522R0, adjust P's template arguments
7638 to apply to A's template; then flatten it again. */
7639 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7640 nparmvec = expand_template_argument_pack (nparmvec);
7641
7642 if (unify (tparms, targs, nparmvec, argvec,
7643 UNIFY_ALLOW_NONE, explain_p))
7644 return 1;
7645
7646 /* If the P0522 adjustment eliminated a pack expansion, deduce
7647 empty packs. */
7648 if (flag_new_ttp
7649 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7650 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7651 DEDUCE_EXACT, /*sub*/true, explain_p))
7652 return 1;
7653 }
7654 else
7655 {
7656 /* Deduce arguments T, i from TT<T> or TT<i>.
7657 We check each element of PARMVEC and ARGVEC individually
7658 rather than the whole TREE_VEC since they can have
7659 different number of elements, which is allowed under N2555. */
7660
7661 int len = TREE_VEC_LENGTH (parmvec);
7662
7663 /* Check if the parameters end in a pack, making them
7664 variadic. */
7665 int parm_variadic_p = 0;
7666 if (len > 0
7667 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7668 parm_variadic_p = 1;
7669
7670 for (int i = 0; i < len - parm_variadic_p; ++i)
7671 /* If the template argument list of P contains a pack
7672 expansion that is not the last template argument, the
7673 entire template argument list is a non-deduced
7674 context. */
7675 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7676 return unify_success (explain_p);
7677
7678 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7679 return unify_too_few_arguments (explain_p,
7680 TREE_VEC_LENGTH (argvec), len);
7681
7682 for (int i = 0; i < len - parm_variadic_p; ++i)
7683 if (unify (tparms, targs,
7684 TREE_VEC_ELT (parmvec, i),
7685 TREE_VEC_ELT (argvec, i),
7686 UNIFY_ALLOW_NONE, explain_p))
7687 return 1;
7688
7689 if (parm_variadic_p
7690 && unify_pack_expansion (tparms, targs,
7691 parmvec, argvec,
7692 DEDUCE_EXACT,
7693 /*subr=*/true, explain_p))
7694 return 1;
7695 }
7696
7697 return 0;
7698 }
7699
7700 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7701 template template parameters. Both PARM_PARMS and ARG_PARMS are
7702 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7703 or PARM_DECL.
7704
7705 Consider the example:
7706 template <class T> class A;
7707 template<template <class U> class TT> class B;
7708
7709 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7710 the parameters to A, and OUTER_ARGS contains A. */
7711
7712 static int
7713 coerce_template_template_parms (tree parm_parms,
7714 tree arg_parms,
7715 tsubst_flags_t complain,
7716 tree in_decl,
7717 tree outer_args)
7718 {
7719 int nparms, nargs, i;
7720 tree parm, arg;
7721 int variadic_p = 0;
7722
7723 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7724 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7725
7726 nparms = TREE_VEC_LENGTH (parm_parms);
7727 nargs = TREE_VEC_LENGTH (arg_parms);
7728
7729 if (flag_new_ttp)
7730 {
7731 /* P0522R0: A template template-parameter P is at least as specialized as
7732 a template template-argument A if, given the following rewrite to two
7733 function templates, the function template corresponding to P is at
7734 least as specialized as the function template corresponding to A
7735 according to the partial ordering rules for function templates
7736 ([temp.func.order]). Given an invented class template X with the
7737 template parameter list of A (including default arguments):
7738
7739 * Each of the two function templates has the same template parameters,
7740 respectively, as P or A.
7741
7742 * Each function template has a single function parameter whose type is
7743 a specialization of X with template arguments corresponding to the
7744 template parameters from the respective function template where, for
7745 each template parameter PP in the template parameter list of the
7746 function template, a corresponding template argument AA is formed. If
7747 PP declares a parameter pack, then AA is the pack expansion
7748 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7749
7750 If the rewrite produces an invalid type, then P is not at least as
7751 specialized as A. */
7752
7753 /* So coerce P's args to apply to A's parms, and then deduce between A's
7754 args and the converted args. If that succeeds, A is at least as
7755 specialized as P, so they match.*/
7756 tree pargs = template_parms_level_to_args (parm_parms);
7757 pargs = add_outermost_template_args (outer_args, pargs);
7758 ++processing_template_decl;
7759 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7760 /*require_all*/true, /*use_default*/true);
7761 --processing_template_decl;
7762 if (pargs != error_mark_node)
7763 {
7764 tree targs = make_tree_vec (nargs);
7765 tree aargs = template_parms_level_to_args (arg_parms);
7766 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7767 /*explain*/false))
7768 return 1;
7769 }
7770 }
7771
7772 /* Determine whether we have a parameter pack at the end of the
7773 template template parameter's template parameter list. */
7774 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7775 {
7776 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7777
7778 if (error_operand_p (parm))
7779 return 0;
7780
7781 switch (TREE_CODE (parm))
7782 {
7783 case TEMPLATE_DECL:
7784 case TYPE_DECL:
7785 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7786 variadic_p = 1;
7787 break;
7788
7789 case PARM_DECL:
7790 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7791 variadic_p = 1;
7792 break;
7793
7794 default:
7795 gcc_unreachable ();
7796 }
7797 }
7798
7799 if (nargs != nparms
7800 && !(variadic_p && nargs >= nparms - 1))
7801 return 0;
7802
7803 /* Check all of the template parameters except the parameter pack at
7804 the end (if any). */
7805 for (i = 0; i < nparms - variadic_p; ++i)
7806 {
7807 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7808 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7809 continue;
7810
7811 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7812 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7813
7814 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7815 outer_args))
7816 return 0;
7817
7818 }
7819
7820 if (variadic_p)
7821 {
7822 /* Check each of the template parameters in the template
7823 argument against the template parameter pack at the end of
7824 the template template parameter. */
7825 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7826 return 0;
7827
7828 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7829
7830 for (; i < nargs; ++i)
7831 {
7832 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7833 continue;
7834
7835 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7836
7837 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7838 outer_args))
7839 return 0;
7840 }
7841 }
7842
7843 return 1;
7844 }
7845
7846 /* Verifies that the deduced template arguments (in TARGS) for the
7847 template template parameters (in TPARMS) represent valid bindings,
7848 by comparing the template parameter list of each template argument
7849 to the template parameter list of its corresponding template
7850 template parameter, in accordance with DR150. This
7851 routine can only be called after all template arguments have been
7852 deduced. It will return TRUE if all of the template template
7853 parameter bindings are okay, FALSE otherwise. */
7854 bool
7855 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7856 {
7857 int i, ntparms = TREE_VEC_LENGTH (tparms);
7858 bool ret = true;
7859
7860 /* We're dealing with template parms in this process. */
7861 ++processing_template_decl;
7862
7863 targs = INNERMOST_TEMPLATE_ARGS (targs);
7864
7865 for (i = 0; i < ntparms; ++i)
7866 {
7867 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7868 tree targ = TREE_VEC_ELT (targs, i);
7869
7870 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7871 {
7872 tree packed_args = NULL_TREE;
7873 int idx, len = 1;
7874
7875 if (ARGUMENT_PACK_P (targ))
7876 {
7877 /* Look inside the argument pack. */
7878 packed_args = ARGUMENT_PACK_ARGS (targ);
7879 len = TREE_VEC_LENGTH (packed_args);
7880 }
7881
7882 for (idx = 0; idx < len; ++idx)
7883 {
7884 tree targ_parms = NULL_TREE;
7885
7886 if (packed_args)
7887 /* Extract the next argument from the argument
7888 pack. */
7889 targ = TREE_VEC_ELT (packed_args, idx);
7890
7891 if (PACK_EXPANSION_P (targ))
7892 /* Look at the pattern of the pack expansion. */
7893 targ = PACK_EXPANSION_PATTERN (targ);
7894
7895 /* Extract the template parameters from the template
7896 argument. */
7897 if (TREE_CODE (targ) == TEMPLATE_DECL)
7898 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7899 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7900 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7901
7902 /* Verify that we can coerce the template template
7903 parameters from the template argument to the template
7904 parameter. This requires an exact match. */
7905 if (targ_parms
7906 && !coerce_template_template_parms
7907 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7908 targ_parms,
7909 tf_none,
7910 tparm,
7911 targs))
7912 {
7913 ret = false;
7914 goto out;
7915 }
7916 }
7917 }
7918 }
7919
7920 out:
7921
7922 --processing_template_decl;
7923 return ret;
7924 }
7925
7926 /* Since type attributes aren't mangled, we need to strip them from
7927 template type arguments. */
7928
7929 static tree
7930 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7931 {
7932 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7933 return arg;
7934 bool removed_attributes = false;
7935 tree canon = strip_typedefs (arg, &removed_attributes);
7936 if (removed_attributes
7937 && (complain & tf_warning))
7938 warning (OPT_Wignored_attributes,
7939 "ignoring attributes on template argument %qT", arg);
7940 return canon;
7941 }
7942
7943 /* And from inside dependent non-type arguments like sizeof(Type). */
7944
7945 static tree
7946 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7947 {
7948 if (!arg || arg == error_mark_node)
7949 return arg;
7950 bool removed_attributes = false;
7951 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7952 if (removed_attributes
7953 && (complain & tf_warning))
7954 warning (OPT_Wignored_attributes,
7955 "ignoring attributes in template argument %qE", arg);
7956 return canon;
7957 }
7958
7959 // A template declaration can be substituted for a constrained
7960 // template template parameter only when the argument is more
7961 // constrained than the parameter.
7962 static bool
7963 is_compatible_template_arg (tree parm, tree arg)
7964 {
7965 tree parm_cons = get_constraints (parm);
7966
7967 /* For now, allow constrained template template arguments
7968 and unconstrained template template parameters. */
7969 if (parm_cons == NULL_TREE)
7970 return true;
7971
7972 /* If the template parameter is constrained, we need to rewrite its
7973 constraints in terms of the ARG's template parameters. This ensures
7974 that all of the template parameter types will have the same depth.
7975
7976 Note that this is only valid when coerce_template_template_parm is
7977 true for the innermost template parameters of PARM and ARG. In other
7978 words, because coercion is successful, this conversion will be valid. */
7979 tree new_args = NULL_TREE;
7980 if (parm_cons)
7981 {
7982 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7983 new_args = template_parms_level_to_args (aparms);
7984 parm_cons = tsubst_constraint_info (parm_cons, new_args,
7985 tf_none, NULL_TREE);
7986 if (parm_cons == error_mark_node)
7987 return false;
7988 }
7989
7990 return weakly_subsumes (parm_cons, new_args, arg);
7991 }
7992
7993 // Convert a placeholder argument into a binding to the original
7994 // parameter. The original parameter is saved as the TREE_TYPE of
7995 // ARG.
7996 static inline tree
7997 convert_wildcard_argument (tree parm, tree arg)
7998 {
7999 TREE_TYPE (arg) = parm;
8000 return arg;
8001 }
8002
8003 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8004 because one of them is dependent. But we need to represent the
8005 conversion for the benefit of cp_tree_equal. */
8006
8007 static tree
8008 maybe_convert_nontype_argument (tree type, tree arg)
8009 {
8010 /* Auto parms get no conversion. */
8011 if (type_uses_auto (type))
8012 return arg;
8013 /* We don't need or want to add this conversion now if we're going to use the
8014 argument for deduction. */
8015 if (value_dependent_expression_p (arg))
8016 return arg;
8017
8018 type = cv_unqualified (type);
8019 tree argtype = TREE_TYPE (arg);
8020 if (same_type_p (type, argtype))
8021 return arg;
8022
8023 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8024 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8025 return arg;
8026 }
8027
8028 /* Convert the indicated template ARG as necessary to match the
8029 indicated template PARM. Returns the converted ARG, or
8030 error_mark_node if the conversion was unsuccessful. Error and
8031 warning messages are issued under control of COMPLAIN. This
8032 conversion is for the Ith parameter in the parameter list. ARGS is
8033 the full set of template arguments deduced so far. */
8034
8035 static tree
8036 convert_template_argument (tree parm,
8037 tree arg,
8038 tree args,
8039 tsubst_flags_t complain,
8040 int i,
8041 tree in_decl)
8042 {
8043 tree orig_arg;
8044 tree val;
8045 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8046
8047 if (parm == error_mark_node || error_operand_p (arg))
8048 return error_mark_node;
8049
8050 /* Trivially convert placeholders. */
8051 if (TREE_CODE (arg) == WILDCARD_DECL)
8052 return convert_wildcard_argument (parm, arg);
8053
8054 if (arg == any_targ_node)
8055 return arg;
8056
8057 if (TREE_CODE (arg) == TREE_LIST
8058 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8059 {
8060 /* The template argument was the name of some
8061 member function. That's usually
8062 invalid, but static members are OK. In any
8063 case, grab the underlying fields/functions
8064 and issue an error later if required. */
8065 TREE_TYPE (arg) = unknown_type_node;
8066 }
8067
8068 orig_arg = arg;
8069
8070 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8071 requires_type = (TREE_CODE (parm) == TYPE_DECL
8072 || requires_tmpl_type);
8073
8074 /* When determining whether an argument pack expansion is a template,
8075 look at the pattern. */
8076 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8077 arg = PACK_EXPANSION_PATTERN (arg);
8078
8079 /* Deal with an injected-class-name used as a template template arg. */
8080 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8081 {
8082 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8083 if (TREE_CODE (t) == TEMPLATE_DECL)
8084 {
8085 if (cxx_dialect >= cxx11)
8086 /* OK under DR 1004. */;
8087 else if (complain & tf_warning_or_error)
8088 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8089 " used as template template argument", TYPE_NAME (arg));
8090 else if (flag_pedantic_errors)
8091 t = arg;
8092
8093 arg = t;
8094 }
8095 }
8096
8097 is_tmpl_type =
8098 ((TREE_CODE (arg) == TEMPLATE_DECL
8099 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8100 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8101 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8102 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8103
8104 if (is_tmpl_type
8105 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8106 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8107 arg = TYPE_STUB_DECL (arg);
8108
8109 is_type = TYPE_P (arg) || is_tmpl_type;
8110
8111 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8112 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8113 {
8114 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8115 {
8116 if (complain & tf_error)
8117 error ("invalid use of destructor %qE as a type", orig_arg);
8118 return error_mark_node;
8119 }
8120
8121 permerror (input_location,
8122 "to refer to a type member of a template parameter, "
8123 "use %<typename %E%>", orig_arg);
8124
8125 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8126 TREE_OPERAND (arg, 1),
8127 typename_type,
8128 complain);
8129 arg = orig_arg;
8130 is_type = 1;
8131 }
8132 if (is_type != requires_type)
8133 {
8134 if (in_decl)
8135 {
8136 if (complain & tf_error)
8137 {
8138 error ("type/value mismatch at argument %d in template "
8139 "parameter list for %qD",
8140 i + 1, in_decl);
8141 if (is_type)
8142 {
8143 /* The template argument is a type, but we're expecting
8144 an expression. */
8145 inform (input_location,
8146 " expected a constant of type %qT, got %qT",
8147 TREE_TYPE (parm),
8148 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8149 /* [temp.arg]/2: "In a template-argument, an ambiguity
8150 between a type-id and an expression is resolved to a
8151 type-id, regardless of the form of the corresponding
8152 template-parameter." So give the user a clue. */
8153 if (TREE_CODE (arg) == FUNCTION_TYPE)
8154 inform (input_location, " ambiguous template argument "
8155 "for non-type template parameter is treated as "
8156 "function type");
8157 }
8158 else if (requires_tmpl_type)
8159 inform (input_location,
8160 " expected a class template, got %qE", orig_arg);
8161 else
8162 inform (input_location,
8163 " expected a type, got %qE", orig_arg);
8164 }
8165 }
8166 return error_mark_node;
8167 }
8168 if (is_tmpl_type ^ requires_tmpl_type)
8169 {
8170 if (in_decl && (complain & tf_error))
8171 {
8172 error ("type/value mismatch at argument %d in template "
8173 "parameter list for %qD",
8174 i + 1, in_decl);
8175 if (is_tmpl_type)
8176 inform (input_location,
8177 " expected a type, got %qT", DECL_NAME (arg));
8178 else
8179 inform (input_location,
8180 " expected a class template, got %qT", orig_arg);
8181 }
8182 return error_mark_node;
8183 }
8184
8185 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8186 /* We already did the appropriate conversion when packing args. */
8187 val = orig_arg;
8188 else if (is_type)
8189 {
8190 if (requires_tmpl_type)
8191 {
8192 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8193 /* The number of argument required is not known yet.
8194 Just accept it for now. */
8195 val = orig_arg;
8196 else
8197 {
8198 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8199 tree argparm;
8200
8201 /* Strip alias templates that are equivalent to another
8202 template. */
8203 arg = get_underlying_template (arg);
8204 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8205
8206 if (coerce_template_template_parms (parmparm, argparm,
8207 complain, in_decl,
8208 args))
8209 {
8210 val = arg;
8211
8212 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8213 TEMPLATE_DECL. */
8214 if (val != error_mark_node)
8215 {
8216 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8217 val = TREE_TYPE (val);
8218 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8219 val = make_pack_expansion (val, complain);
8220 }
8221 }
8222 else
8223 {
8224 if (in_decl && (complain & tf_error))
8225 {
8226 error ("type/value mismatch at argument %d in "
8227 "template parameter list for %qD",
8228 i + 1, in_decl);
8229 inform (input_location,
8230 " expected a template of type %qD, got %qT",
8231 parm, orig_arg);
8232 }
8233
8234 val = error_mark_node;
8235 }
8236
8237 // Check that the constraints are compatible before allowing the
8238 // substitution.
8239 if (val != error_mark_node)
8240 if (!is_compatible_template_arg (parm, arg))
8241 {
8242 if (in_decl && (complain & tf_error))
8243 {
8244 error ("constraint mismatch at argument %d in "
8245 "template parameter list for %qD",
8246 i + 1, in_decl);
8247 inform (input_location, " expected %qD but got %qD",
8248 parm, arg);
8249 }
8250 val = error_mark_node;
8251 }
8252 }
8253 }
8254 else
8255 val = orig_arg;
8256 /* We only form one instance of each template specialization.
8257 Therefore, if we use a non-canonical variant (i.e., a
8258 typedef), any future messages referring to the type will use
8259 the typedef, which is confusing if those future uses do not
8260 themselves also use the typedef. */
8261 if (TYPE_P (val))
8262 val = canonicalize_type_argument (val, complain);
8263 }
8264 else
8265 {
8266 tree t = TREE_TYPE (parm);
8267
8268 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8269 > TMPL_ARGS_DEPTH (args))
8270 /* We don't have enough levels of args to do any substitution. This
8271 can happen in the context of -fnew-ttp-matching. */;
8272 else if (tree a = type_uses_auto (t))
8273 {
8274 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8275 if (t == error_mark_node)
8276 return error_mark_node;
8277 }
8278 else
8279 t = tsubst (t, args, complain, in_decl);
8280
8281 if (invalid_nontype_parm_type_p (t, complain))
8282 return error_mark_node;
8283
8284 if (t != TREE_TYPE (parm))
8285 t = canonicalize_type_argument (t, complain);
8286
8287 if (!type_dependent_expression_p (orig_arg)
8288 && !uses_template_parms (t))
8289 /* We used to call digest_init here. However, digest_init
8290 will report errors, which we don't want when complain
8291 is zero. More importantly, digest_init will try too
8292 hard to convert things: for example, `0' should not be
8293 converted to pointer type at this point according to
8294 the standard. Accepting this is not merely an
8295 extension, since deciding whether or not these
8296 conversions can occur is part of determining which
8297 function template to call, or whether a given explicit
8298 argument specification is valid. */
8299 val = convert_nontype_argument (t, orig_arg, complain);
8300 else
8301 {
8302 val = canonicalize_expr_argument (orig_arg, complain);
8303 val = maybe_convert_nontype_argument (t, val);
8304 }
8305
8306
8307 if (val == NULL_TREE)
8308 val = error_mark_node;
8309 else if (val == error_mark_node && (complain & tf_error))
8310 error_at (cp_expr_loc_or_input_loc (orig_arg),
8311 "could not convert template argument %qE from %qT to %qT",
8312 orig_arg, TREE_TYPE (orig_arg), t);
8313
8314 if (INDIRECT_REF_P (val))
8315 {
8316 /* Reject template arguments that are references to built-in
8317 functions with no library fallbacks. */
8318 const_tree inner = TREE_OPERAND (val, 0);
8319 const_tree innertype = TREE_TYPE (inner);
8320 if (innertype
8321 && TYPE_REF_P (innertype)
8322 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8323 && TREE_OPERAND_LENGTH (inner) > 0
8324 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8325 return error_mark_node;
8326 }
8327
8328 if (TREE_CODE (val) == SCOPE_REF)
8329 {
8330 /* Strip typedefs from the SCOPE_REF. */
8331 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8332 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8333 complain);
8334 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8335 QUALIFIED_NAME_IS_TEMPLATE (val));
8336 }
8337 }
8338
8339 return val;
8340 }
8341
8342 /* Coerces the remaining template arguments in INNER_ARGS (from
8343 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8344 Returns the coerced argument pack. PARM_IDX is the position of this
8345 parameter in the template parameter list. ARGS is the original
8346 template argument list. */
8347 static tree
8348 coerce_template_parameter_pack (tree parms,
8349 int parm_idx,
8350 tree args,
8351 tree inner_args,
8352 int arg_idx,
8353 tree new_args,
8354 int* lost,
8355 tree in_decl,
8356 tsubst_flags_t complain)
8357 {
8358 tree parm = TREE_VEC_ELT (parms, parm_idx);
8359 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8360 tree packed_args;
8361 tree argument_pack;
8362 tree packed_parms = NULL_TREE;
8363
8364 if (arg_idx > nargs)
8365 arg_idx = nargs;
8366
8367 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8368 {
8369 /* When the template parameter is a non-type template parameter pack
8370 or template template parameter pack whose type or template
8371 parameters use parameter packs, we know exactly how many arguments
8372 we are looking for. Build a vector of the instantiated decls for
8373 these template parameters in PACKED_PARMS. */
8374 /* We can't use make_pack_expansion here because it would interpret a
8375 _DECL as a use rather than a declaration. */
8376 tree decl = TREE_VALUE (parm);
8377 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8378 SET_PACK_EXPANSION_PATTERN (exp, decl);
8379 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8380 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8381
8382 TREE_VEC_LENGTH (args)--;
8383 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8384 TREE_VEC_LENGTH (args)++;
8385
8386 if (packed_parms == error_mark_node)
8387 return error_mark_node;
8388
8389 /* If we're doing a partial instantiation of a member template,
8390 verify that all of the types used for the non-type
8391 template parameter pack are, in fact, valid for non-type
8392 template parameters. */
8393 if (arg_idx < nargs
8394 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8395 {
8396 int j, len = TREE_VEC_LENGTH (packed_parms);
8397 for (j = 0; j < len; ++j)
8398 {
8399 tree t = TREE_VEC_ELT (packed_parms, j);
8400 if (TREE_CODE (t) == PARM_DECL
8401 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8402 return error_mark_node;
8403 }
8404 /* We don't know how many args we have yet, just
8405 use the unconverted ones for now. */
8406 return NULL_TREE;
8407 }
8408
8409 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8410 }
8411 /* Check if we have a placeholder pack, which indicates we're
8412 in the context of a introduction list. In that case we want
8413 to match this pack to the single placeholder. */
8414 else if (arg_idx < nargs
8415 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8416 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8417 {
8418 nargs = arg_idx + 1;
8419 packed_args = make_tree_vec (1);
8420 }
8421 else
8422 packed_args = make_tree_vec (nargs - arg_idx);
8423
8424 /* Convert the remaining arguments, which will be a part of the
8425 parameter pack "parm". */
8426 int first_pack_arg = arg_idx;
8427 for (; arg_idx < nargs; ++arg_idx)
8428 {
8429 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8430 tree actual_parm = TREE_VALUE (parm);
8431 int pack_idx = arg_idx - first_pack_arg;
8432
8433 if (packed_parms)
8434 {
8435 /* Once we've packed as many args as we have types, stop. */
8436 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8437 break;
8438 else if (PACK_EXPANSION_P (arg))
8439 /* We don't know how many args we have yet, just
8440 use the unconverted ones for now. */
8441 return NULL_TREE;
8442 else
8443 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8444 }
8445
8446 if (arg == error_mark_node)
8447 {
8448 if (complain & tf_error)
8449 error ("template argument %d is invalid", arg_idx + 1);
8450 }
8451 else
8452 arg = convert_template_argument (actual_parm,
8453 arg, new_args, complain, parm_idx,
8454 in_decl);
8455 if (arg == error_mark_node)
8456 (*lost)++;
8457 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8458 }
8459
8460 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8461 && TREE_VEC_LENGTH (packed_args) > 0)
8462 {
8463 if (complain & tf_error)
8464 error ("wrong number of template arguments (%d, should be %d)",
8465 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8466 return error_mark_node;
8467 }
8468
8469 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8470 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8471 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8472 else
8473 {
8474 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8475 TREE_CONSTANT (argument_pack) = 1;
8476 }
8477
8478 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8479 if (CHECKING_P)
8480 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8481 TREE_VEC_LENGTH (packed_args));
8482 return argument_pack;
8483 }
8484
8485 /* Returns the number of pack expansions in the template argument vector
8486 ARGS. */
8487
8488 static int
8489 pack_expansion_args_count (tree args)
8490 {
8491 int i;
8492 int count = 0;
8493 if (args)
8494 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8495 {
8496 tree elt = TREE_VEC_ELT (args, i);
8497 if (elt && PACK_EXPANSION_P (elt))
8498 ++count;
8499 }
8500 return count;
8501 }
8502
8503 /* Convert all template arguments to their appropriate types, and
8504 return a vector containing the innermost resulting template
8505 arguments. If any error occurs, return error_mark_node. Error and
8506 warning messages are issued under control of COMPLAIN.
8507
8508 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8509 for arguments not specified in ARGS. Otherwise, if
8510 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8511 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8512 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8513 ARGS. */
8514
8515 static tree
8516 coerce_template_parms (tree parms,
8517 tree args,
8518 tree in_decl,
8519 tsubst_flags_t complain,
8520 bool require_all_args,
8521 bool use_default_args)
8522 {
8523 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8524 tree orig_inner_args;
8525 tree inner_args;
8526 tree new_args;
8527 tree new_inner_args;
8528
8529 /* When used as a boolean value, indicates whether this is a
8530 variadic template parameter list. Since it's an int, we can also
8531 subtract it from nparms to get the number of non-variadic
8532 parameters. */
8533 int variadic_p = 0;
8534 int variadic_args_p = 0;
8535 int post_variadic_parms = 0;
8536
8537 /* Adjustment to nparms for fixed parameter packs. */
8538 int fixed_pack_adjust = 0;
8539 int fixed_packs = 0;
8540 int missing = 0;
8541
8542 /* Likewise for parameters with default arguments. */
8543 int default_p = 0;
8544
8545 if (args == error_mark_node)
8546 return error_mark_node;
8547
8548 nparms = TREE_VEC_LENGTH (parms);
8549
8550 /* Determine if there are any parameter packs or default arguments. */
8551 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8552 {
8553 tree parm = TREE_VEC_ELT (parms, parm_idx);
8554 if (variadic_p)
8555 ++post_variadic_parms;
8556 if (template_parameter_pack_p (TREE_VALUE (parm)))
8557 ++variadic_p;
8558 if (TREE_PURPOSE (parm))
8559 ++default_p;
8560 }
8561
8562 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8563 /* If there are no parameters that follow a parameter pack, we need to
8564 expand any argument packs so that we can deduce a parameter pack from
8565 some non-packed args followed by an argument pack, as in variadic85.C.
8566 If there are such parameters, we need to leave argument packs intact
8567 so the arguments are assigned properly. This can happen when dealing
8568 with a nested class inside a partial specialization of a class
8569 template, as in variadic92.C, or when deducing a template parameter pack
8570 from a sub-declarator, as in variadic114.C. */
8571 if (!post_variadic_parms)
8572 inner_args = expand_template_argument_pack (inner_args);
8573
8574 /* Count any pack expansion args. */
8575 variadic_args_p = pack_expansion_args_count (inner_args);
8576
8577 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8578 if ((nargs - variadic_args_p > nparms && !variadic_p)
8579 || (nargs < nparms - variadic_p
8580 && require_all_args
8581 && !variadic_args_p
8582 && (!use_default_args
8583 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8584 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8585 {
8586 bad_nargs:
8587 if (complain & tf_error)
8588 {
8589 if (variadic_p || default_p)
8590 {
8591 nparms -= variadic_p + default_p;
8592 error ("wrong number of template arguments "
8593 "(%d, should be at least %d)", nargs, nparms);
8594 }
8595 else
8596 error ("wrong number of template arguments "
8597 "(%d, should be %d)", nargs, nparms);
8598
8599 if (in_decl)
8600 inform (DECL_SOURCE_LOCATION (in_decl),
8601 "provided for %qD", in_decl);
8602 }
8603
8604 return error_mark_node;
8605 }
8606 /* We can't pass a pack expansion to a non-pack parameter of an alias
8607 template (DR 1430). */
8608 else if (in_decl
8609 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8610 || concept_definition_p (in_decl))
8611 && variadic_args_p
8612 && nargs - variadic_args_p < nparms - variadic_p)
8613 {
8614 if (complain & tf_error)
8615 {
8616 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8617 {
8618 tree arg = TREE_VEC_ELT (inner_args, i);
8619 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8620
8621 if (PACK_EXPANSION_P (arg)
8622 && !template_parameter_pack_p (parm))
8623 {
8624 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8625 error_at (location_of (arg),
8626 "pack expansion argument for non-pack parameter "
8627 "%qD of alias template %qD", parm, in_decl);
8628 else
8629 error_at (location_of (arg),
8630 "pack expansion argument for non-pack parameter "
8631 "%qD of concept %qD", parm, in_decl);
8632 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8633 goto found;
8634 }
8635 }
8636 gcc_unreachable ();
8637 found:;
8638 }
8639 return error_mark_node;
8640 }
8641
8642 /* We need to evaluate the template arguments, even though this
8643 template-id may be nested within a "sizeof". */
8644 cp_evaluated ev;
8645
8646 new_inner_args = make_tree_vec (nparms);
8647 new_args = add_outermost_template_args (args, new_inner_args);
8648 int pack_adjust = 0;
8649 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8650 {
8651 tree arg;
8652 tree parm;
8653
8654 /* Get the Ith template parameter. */
8655 parm = TREE_VEC_ELT (parms, parm_idx);
8656
8657 if (parm == error_mark_node)
8658 {
8659 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8660 continue;
8661 }
8662
8663 /* Calculate the next argument. */
8664 if (arg_idx < nargs)
8665 arg = TREE_VEC_ELT (inner_args, arg_idx);
8666 else
8667 arg = NULL_TREE;
8668
8669 if (template_parameter_pack_p (TREE_VALUE (parm))
8670 && (arg || require_all_args || !(complain & tf_partial))
8671 && !(arg && ARGUMENT_PACK_P (arg)))
8672 {
8673 /* Some arguments will be placed in the
8674 template parameter pack PARM. */
8675 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8676 inner_args, arg_idx,
8677 new_args, &lost,
8678 in_decl, complain);
8679
8680 if (arg == NULL_TREE)
8681 {
8682 /* We don't know how many args we have yet, just use the
8683 unconverted (and still packed) ones for now. */
8684 new_inner_args = orig_inner_args;
8685 arg_idx = nargs;
8686 break;
8687 }
8688
8689 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8690
8691 /* Store this argument. */
8692 if (arg == error_mark_node)
8693 {
8694 lost++;
8695 /* We are done with all of the arguments. */
8696 arg_idx = nargs;
8697 break;
8698 }
8699 else
8700 {
8701 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8702 arg_idx += pack_adjust;
8703 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8704 {
8705 ++fixed_packs;
8706 fixed_pack_adjust += pack_adjust;
8707 }
8708 }
8709
8710 continue;
8711 }
8712 else if (arg)
8713 {
8714 if (PACK_EXPANSION_P (arg))
8715 {
8716 /* "If every valid specialization of a variadic template
8717 requires an empty template parameter pack, the template is
8718 ill-formed, no diagnostic required." So check that the
8719 pattern works with this parameter. */
8720 tree pattern = PACK_EXPANSION_PATTERN (arg);
8721 tree conv = convert_template_argument (TREE_VALUE (parm),
8722 pattern, new_args,
8723 complain, parm_idx,
8724 in_decl);
8725 if (conv == error_mark_node)
8726 {
8727 if (complain & tf_error)
8728 inform (input_location, "so any instantiation with a "
8729 "non-empty parameter pack would be ill-formed");
8730 ++lost;
8731 }
8732 else if (TYPE_P (conv) && !TYPE_P (pattern))
8733 /* Recover from missing typename. */
8734 TREE_VEC_ELT (inner_args, arg_idx)
8735 = make_pack_expansion (conv, complain);
8736
8737 /* We don't know how many args we have yet, just
8738 use the unconverted ones for now. */
8739 new_inner_args = inner_args;
8740 arg_idx = nargs;
8741 break;
8742 }
8743 }
8744 else if (require_all_args)
8745 {
8746 /* There must be a default arg in this case. */
8747 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8748 complain, in_decl);
8749 /* The position of the first default template argument,
8750 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8751 Record that. */
8752 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8753 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8754 arg_idx - pack_adjust);
8755 }
8756 else
8757 break;
8758
8759 if (arg == error_mark_node)
8760 {
8761 if (complain & tf_error)
8762 error ("template argument %d is invalid", arg_idx + 1);
8763 }
8764 else if (!arg)
8765 {
8766 /* This can occur if there was an error in the template
8767 parameter list itself (which we would already have
8768 reported) that we are trying to recover from, e.g., a class
8769 template with a parameter list such as
8770 template<typename..., typename> (cpp0x/variadic150.C). */
8771 ++lost;
8772
8773 /* This can also happen with a fixed parameter pack (71834). */
8774 if (arg_idx >= nargs)
8775 ++missing;
8776 }
8777 else
8778 arg = convert_template_argument (TREE_VALUE (parm),
8779 arg, new_args, complain,
8780 parm_idx, in_decl);
8781
8782 if (arg == error_mark_node)
8783 lost++;
8784
8785 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8786 }
8787
8788 if (missing || arg_idx < nargs - variadic_args_p)
8789 {
8790 /* If we had fixed parameter packs, we didn't know how many arguments we
8791 actually needed earlier; now we do. */
8792 nparms += fixed_pack_adjust;
8793 variadic_p -= fixed_packs;
8794 goto bad_nargs;
8795 }
8796
8797 if (arg_idx < nargs)
8798 {
8799 /* We had some pack expansion arguments that will only work if the packs
8800 are empty, but wait until instantiation time to complain.
8801 See variadic-ttp3.C. */
8802
8803 /* Except that we can't provide empty packs to alias templates or
8804 concepts when there are no corresponding parameters. Basically,
8805 we can get here with this:
8806
8807 template<typename T> concept C = true;
8808
8809 template<typename... Args>
8810 requires C<Args...>
8811 void f();
8812
8813 When parsing C<Args...>, we try to form a concept check of
8814 C<?, Args...>. Without the extra check for substituting an empty
8815 pack past the last parameter, we can accept the check as valid.
8816
8817 FIXME: This may be valid for alias templates (but I doubt it).
8818
8819 FIXME: The error could be better also. */
8820 if (in_decl && concept_definition_p (in_decl))
8821 {
8822 if (complain & tf_error)
8823 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8824 "too many arguments");
8825 return error_mark_node;
8826 }
8827
8828 int len = nparms + (nargs - arg_idx);
8829 tree args = make_tree_vec (len);
8830 int i = 0;
8831 for (; i < nparms; ++i)
8832 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8833 for (; i < len; ++i, ++arg_idx)
8834 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8835 arg_idx - pack_adjust);
8836 new_inner_args = args;
8837 }
8838
8839 if (lost)
8840 {
8841 gcc_assert (!(complain & tf_error) || seen_error ());
8842 return error_mark_node;
8843 }
8844
8845 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8846 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8847 TREE_VEC_LENGTH (new_inner_args));
8848
8849 return new_inner_args;
8850 }
8851
8852 /* Convert all template arguments to their appropriate types, and
8853 return a vector containing the innermost resulting template
8854 arguments. If any error occurs, return error_mark_node. Error and
8855 warning messages are not issued.
8856
8857 Note that no function argument deduction is performed, and default
8858 arguments are used to fill in unspecified arguments. */
8859 tree
8860 coerce_template_parms (tree parms, tree args, tree in_decl)
8861 {
8862 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8863 }
8864
8865 /* Convert all template arguments to their appropriate type, and
8866 instantiate default arguments as needed. This returns a vector
8867 containing the innermost resulting template arguments, or
8868 error_mark_node if unsuccessful. */
8869 tree
8870 coerce_template_parms (tree parms, tree args, tree in_decl,
8871 tsubst_flags_t complain)
8872 {
8873 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8874 }
8875
8876 /* Like coerce_template_parms. If PARMS represents all template
8877 parameters levels, this function returns a vector of vectors
8878 representing all the resulting argument levels. Note that in this
8879 case, only the innermost arguments are coerced because the
8880 outermost ones are supposed to have been coerced already.
8881
8882 Otherwise, if PARMS represents only (the innermost) vector of
8883 parameters, this function returns a vector containing just the
8884 innermost resulting arguments. */
8885
8886 static tree
8887 coerce_innermost_template_parms (tree parms,
8888 tree args,
8889 tree in_decl,
8890 tsubst_flags_t complain,
8891 bool require_all_args,
8892 bool use_default_args)
8893 {
8894 int parms_depth = TMPL_PARMS_DEPTH (parms);
8895 int args_depth = TMPL_ARGS_DEPTH (args);
8896 tree coerced_args;
8897
8898 if (parms_depth > 1)
8899 {
8900 coerced_args = make_tree_vec (parms_depth);
8901 tree level;
8902 int cur_depth;
8903
8904 for (level = parms, cur_depth = parms_depth;
8905 parms_depth > 0 && level != NULL_TREE;
8906 level = TREE_CHAIN (level), --cur_depth)
8907 {
8908 tree l;
8909 if (cur_depth == args_depth)
8910 l = coerce_template_parms (TREE_VALUE (level),
8911 args, in_decl, complain,
8912 require_all_args,
8913 use_default_args);
8914 else
8915 l = TMPL_ARGS_LEVEL (args, cur_depth);
8916
8917 if (l == error_mark_node)
8918 return error_mark_node;
8919
8920 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8921 }
8922 }
8923 else
8924 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8925 args, in_decl, complain,
8926 require_all_args,
8927 use_default_args);
8928 return coerced_args;
8929 }
8930
8931 /* Returns 1 if template args OT and NT are equivalent. */
8932
8933 int
8934 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8935 {
8936 if (nt == ot)
8937 return 1;
8938 if (nt == NULL_TREE || ot == NULL_TREE)
8939 return false;
8940 if (nt == any_targ_node || ot == any_targ_node)
8941 return true;
8942
8943 if (TREE_CODE (nt) == TREE_VEC)
8944 /* For member templates */
8945 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8946 else if (PACK_EXPANSION_P (ot))
8947 return (PACK_EXPANSION_P (nt)
8948 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8949 PACK_EXPANSION_PATTERN (nt))
8950 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8951 PACK_EXPANSION_EXTRA_ARGS (nt)));
8952 else if (ARGUMENT_PACK_P (ot))
8953 {
8954 int i, len;
8955 tree opack, npack;
8956
8957 if (!ARGUMENT_PACK_P (nt))
8958 return 0;
8959
8960 opack = ARGUMENT_PACK_ARGS (ot);
8961 npack = ARGUMENT_PACK_ARGS (nt);
8962 len = TREE_VEC_LENGTH (opack);
8963 if (TREE_VEC_LENGTH (npack) != len)
8964 return 0;
8965 for (i = 0; i < len; ++i)
8966 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8967 TREE_VEC_ELT (npack, i)))
8968 return 0;
8969 return 1;
8970 }
8971 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8972 gcc_unreachable ();
8973 else if (TYPE_P (nt))
8974 {
8975 if (!TYPE_P (ot))
8976 return false;
8977 /* Don't treat an alias template specialization with dependent
8978 arguments as equivalent to its underlying type when used as a
8979 template argument; we need them to be distinct so that we
8980 substitute into the specialization arguments at instantiation
8981 time. And aliases can't be equivalent without being ==, so
8982 we don't need to look any deeper.
8983
8984 During partial ordering, however, we need to treat them normally so
8985 that we can order uses of the same alias with different
8986 cv-qualification (79960). */
8987 if (!partial_order
8988 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8989 return false;
8990 else
8991 return same_type_p (ot, nt);
8992 }
8993 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8994 return 0;
8995 else
8996 {
8997 /* Try to treat a template non-type argument that has been converted
8998 to the parameter type as equivalent to one that hasn't yet. */
8999 for (enum tree_code code1 = TREE_CODE (ot);
9000 CONVERT_EXPR_CODE_P (code1)
9001 || code1 == NON_LVALUE_EXPR;
9002 code1 = TREE_CODE (ot))
9003 ot = TREE_OPERAND (ot, 0);
9004 for (enum tree_code code2 = TREE_CODE (nt);
9005 CONVERT_EXPR_CODE_P (code2)
9006 || code2 == NON_LVALUE_EXPR;
9007 code2 = TREE_CODE (nt))
9008 nt = TREE_OPERAND (nt, 0);
9009
9010 return cp_tree_equal (ot, nt);
9011 }
9012 }
9013
9014 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9015 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9016 NEWARG_PTR with the offending arguments if they are non-NULL. */
9017
9018 int
9019 comp_template_args (tree oldargs, tree newargs,
9020 tree *oldarg_ptr, tree *newarg_ptr,
9021 bool partial_order)
9022 {
9023 int i;
9024
9025 if (oldargs == newargs)
9026 return 1;
9027
9028 if (!oldargs || !newargs)
9029 return 0;
9030
9031 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9032 return 0;
9033
9034 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9035 {
9036 tree nt = TREE_VEC_ELT (newargs, i);
9037 tree ot = TREE_VEC_ELT (oldargs, i);
9038
9039 if (! template_args_equal (ot, nt, partial_order))
9040 {
9041 if (oldarg_ptr != NULL)
9042 *oldarg_ptr = ot;
9043 if (newarg_ptr != NULL)
9044 *newarg_ptr = nt;
9045 return 0;
9046 }
9047 }
9048 return 1;
9049 }
9050
9051 inline bool
9052 comp_template_args_porder (tree oargs, tree nargs)
9053 {
9054 return comp_template_args (oargs, nargs, NULL, NULL, true);
9055 }
9056
9057 /* Implement a freelist interface for objects of type T.
9058
9059 Head is a separate object, rather than a regular member, so that we
9060 can define it as a GTY deletable pointer, which is highly
9061 desirable. A data member could be declared that way, but then the
9062 containing object would implicitly get GTY((user)), which would
9063 prevent us from instantiating freelists as global objects.
9064 Although this way we can create freelist global objects, they're
9065 such thin wrappers that instantiating temporaries at every use
9066 loses nothing and saves permanent storage for the freelist object.
9067
9068 Member functions next, anew, poison and reinit have default
9069 implementations that work for most of the types we're interested
9070 in, but if they don't work for some type, they should be explicitly
9071 specialized. See the comments before them for requirements, and
9072 the example specializations for the tree_list_freelist. */
9073 template <typename T>
9074 class freelist
9075 {
9076 /* Return the next object in a chain. We could just do type
9077 punning, but if we access the object with its underlying type, we
9078 avoid strict-aliasing trouble. This needs only work between
9079 poison and reinit. */
9080 static T *&next (T *obj) { return obj->next; }
9081
9082 /* Return a newly allocated, uninitialized or minimally-initialized
9083 object of type T. Any initialization performed by anew should
9084 either remain across the life of the object and the execution of
9085 poison, or be redone by reinit. */
9086 static T *anew () { return ggc_alloc<T> (); }
9087
9088 /* Optionally scribble all over the bits holding the object, so that
9089 they become (mostly?) uninitialized memory. This is called while
9090 preparing to make the object part of the free list. */
9091 static void poison (T *obj) {
9092 T *p ATTRIBUTE_UNUSED = obj;
9093 T **q ATTRIBUTE_UNUSED = &next (obj);
9094
9095 #ifdef ENABLE_GC_CHECKING
9096 /* Poison the data, to indicate the data is garbage. */
9097 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9098 memset (p, 0xa5, sizeof (*p));
9099 #endif
9100 /* Let valgrind know the object is free. */
9101 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9102
9103 /* Let valgrind know the next portion of the object is available,
9104 but uninitialized. */
9105 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9106 }
9107
9108 /* Bring an object that underwent at least one lifecycle after anew
9109 and before the most recent free and poison, back to a usable
9110 state, reinitializing whatever is needed for it to be
9111 functionally equivalent to an object just allocated and returned
9112 by anew. This may poison or clear the next field, used by
9113 freelist housekeeping after poison was called. */
9114 static void reinit (T *obj) {
9115 T **q ATTRIBUTE_UNUSED = &next (obj);
9116
9117 #ifdef ENABLE_GC_CHECKING
9118 memset (q, 0xa5, sizeof (*q));
9119 #endif
9120 /* Let valgrind know the entire object is available, but
9121 uninitialized. */
9122 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9123 }
9124
9125 /* Reference a GTY-deletable pointer that points to the first object
9126 in the free list proper. */
9127 T *&head;
9128 public:
9129 /* Construct a freelist object chaining objects off of HEAD. */
9130 freelist (T *&head) : head(head) {}
9131
9132 /* Add OBJ to the free object list. The former head becomes OBJ's
9133 successor. */
9134 void free (T *obj)
9135 {
9136 poison (obj);
9137 next (obj) = head;
9138 head = obj;
9139 }
9140
9141 /* Take an object from the free list, if one is available, or
9142 allocate a new one. Objects taken from the free list should be
9143 regarded as filled with garbage, except for bits that are
9144 configured to be preserved across free and alloc. */
9145 T *alloc ()
9146 {
9147 if (head)
9148 {
9149 T *obj = head;
9150 head = next (head);
9151 reinit (obj);
9152 return obj;
9153 }
9154 else
9155 return anew ();
9156 }
9157 };
9158
9159 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9160 want to allocate a TREE_LIST using the usual interface, and ensure
9161 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9162 build_tree_list logic in reinit, so this could go out of sync. */
9163 template <>
9164 inline tree &
9165 freelist<tree_node>::next (tree obj)
9166 {
9167 return TREE_CHAIN (obj);
9168 }
9169 template <>
9170 inline tree
9171 freelist<tree_node>::anew ()
9172 {
9173 return build_tree_list (NULL, NULL);
9174 }
9175 template <>
9176 inline void
9177 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9178 {
9179 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9180 tree p ATTRIBUTE_UNUSED = obj;
9181 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9182 tree *q ATTRIBUTE_UNUSED = &next (obj);
9183
9184 #ifdef ENABLE_GC_CHECKING
9185 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9186
9187 /* Poison the data, to indicate the data is garbage. */
9188 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9189 memset (p, 0xa5, size);
9190 #endif
9191 /* Let valgrind know the object is free. */
9192 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9193 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9194 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9195 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9196
9197 #ifdef ENABLE_GC_CHECKING
9198 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9199 /* Keep TREE_CHAIN functional. */
9200 TREE_SET_CODE (obj, TREE_LIST);
9201 #else
9202 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9203 #endif
9204 }
9205 template <>
9206 inline void
9207 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9208 {
9209 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9210
9211 #ifdef ENABLE_GC_CHECKING
9212 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9213 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9214 memset (obj, 0, sizeof (tree_list));
9215 #endif
9216
9217 /* Let valgrind know the entire object is available, but
9218 uninitialized. */
9219 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9220
9221 #ifdef ENABLE_GC_CHECKING
9222 TREE_SET_CODE (obj, TREE_LIST);
9223 #else
9224 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9225 #endif
9226 }
9227
9228 /* Point to the first object in the TREE_LIST freelist. */
9229 static GTY((deletable)) tree tree_list_freelist_head;
9230 /* Return the/an actual TREE_LIST freelist. */
9231 static inline freelist<tree_node>
9232 tree_list_freelist ()
9233 {
9234 return tree_list_freelist_head;
9235 }
9236
9237 /* Point to the first object in the tinst_level freelist. */
9238 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9239 /* Return the/an actual tinst_level freelist. */
9240 static inline freelist<tinst_level>
9241 tinst_level_freelist ()
9242 {
9243 return tinst_level_freelist_head;
9244 }
9245
9246 /* Point to the first object in the pending_template freelist. */
9247 static GTY((deletable)) pending_template *pending_template_freelist_head;
9248 /* Return the/an actual pending_template freelist. */
9249 static inline freelist<pending_template>
9250 pending_template_freelist ()
9251 {
9252 return pending_template_freelist_head;
9253 }
9254
9255 /* Build the TREE_LIST object out of a split list, store it
9256 permanently, and return it. */
9257 tree
9258 tinst_level::to_list ()
9259 {
9260 gcc_assert (split_list_p ());
9261 tree ret = tree_list_freelist ().alloc ();
9262 TREE_PURPOSE (ret) = tldcl;
9263 TREE_VALUE (ret) = targs;
9264 tldcl = ret;
9265 targs = NULL;
9266 gcc_assert (tree_list_p ());
9267 return ret;
9268 }
9269
9270 const unsigned short tinst_level::refcount_infinity;
9271
9272 /* Increment OBJ's refcount unless it is already infinite. */
9273 static tinst_level *
9274 inc_refcount_use (tinst_level *obj)
9275 {
9276 if (obj && obj->refcount != tinst_level::refcount_infinity)
9277 ++obj->refcount;
9278 return obj;
9279 }
9280
9281 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9282 void
9283 tinst_level::free (tinst_level *obj)
9284 {
9285 if (obj->tree_list_p ())
9286 tree_list_freelist ().free (obj->get_node ());
9287 tinst_level_freelist ().free (obj);
9288 }
9289
9290 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9291 OBJ's DECL and OBJ, and start over with the tinst_level object that
9292 used to be referenced by OBJ's NEXT. */
9293 static void
9294 dec_refcount_use (tinst_level *obj)
9295 {
9296 while (obj
9297 && obj->refcount != tinst_level::refcount_infinity
9298 && !--obj->refcount)
9299 {
9300 tinst_level *next = obj->next;
9301 tinst_level::free (obj);
9302 obj = next;
9303 }
9304 }
9305
9306 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9307 and of the former PTR. Omitting the second argument is equivalent
9308 to passing (T*)NULL; this is allowed because passing the
9309 zero-valued integral constant NULL confuses type deduction and/or
9310 overload resolution. */
9311 template <typename T>
9312 static void
9313 set_refcount_ptr (T *& ptr, T *obj = NULL)
9314 {
9315 T *save = ptr;
9316 ptr = inc_refcount_use (obj);
9317 dec_refcount_use (save);
9318 }
9319
9320 static void
9321 add_pending_template (tree d)
9322 {
9323 tree ti = (TYPE_P (d)
9324 ? CLASSTYPE_TEMPLATE_INFO (d)
9325 : DECL_TEMPLATE_INFO (d));
9326 struct pending_template *pt;
9327 int level;
9328
9329 if (TI_PENDING_TEMPLATE_FLAG (ti))
9330 return;
9331
9332 /* We are called both from instantiate_decl, where we've already had a
9333 tinst_level pushed, and instantiate_template, where we haven't.
9334 Compensate. */
9335 gcc_assert (TREE_CODE (d) != TREE_LIST);
9336 level = !current_tinst_level
9337 || current_tinst_level->maybe_get_node () != d;
9338
9339 if (level)
9340 push_tinst_level (d);
9341
9342 pt = pending_template_freelist ().alloc ();
9343 pt->next = NULL;
9344 pt->tinst = NULL;
9345 set_refcount_ptr (pt->tinst, current_tinst_level);
9346 if (last_pending_template)
9347 last_pending_template->next = pt;
9348 else
9349 pending_templates = pt;
9350
9351 last_pending_template = pt;
9352
9353 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9354
9355 if (level)
9356 pop_tinst_level ();
9357 }
9358
9359
9360 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9361 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9362 documentation for TEMPLATE_ID_EXPR. */
9363
9364 tree
9365 lookup_template_function (tree fns, tree arglist)
9366 {
9367 if (fns == error_mark_node || arglist == error_mark_node)
9368 return error_mark_node;
9369
9370 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9371
9372 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9373 {
9374 error ("%q#D is not a function template", fns);
9375 return error_mark_node;
9376 }
9377
9378 if (BASELINK_P (fns))
9379 {
9380 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9381 unknown_type_node,
9382 BASELINK_FUNCTIONS (fns),
9383 arglist);
9384 return fns;
9385 }
9386
9387 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9388 }
9389
9390 /* Within the scope of a template class S<T>, the name S gets bound
9391 (in build_self_reference) to a TYPE_DECL for the class, not a
9392 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9393 or one of its enclosing classes, and that type is a template,
9394 return the associated TEMPLATE_DECL. Otherwise, the original
9395 DECL is returned.
9396
9397 Also handle the case when DECL is a TREE_LIST of ambiguous
9398 injected-class-names from different bases. */
9399
9400 tree
9401 maybe_get_template_decl_from_type_decl (tree decl)
9402 {
9403 if (decl == NULL_TREE)
9404 return decl;
9405
9406 /* DR 176: A lookup that finds an injected-class-name (10.2
9407 [class.member.lookup]) can result in an ambiguity in certain cases
9408 (for example, if it is found in more than one base class). If all of
9409 the injected-class-names that are found refer to specializations of
9410 the same class template, and if the name is followed by a
9411 template-argument-list, the reference refers to the class template
9412 itself and not a specialization thereof, and is not ambiguous. */
9413 if (TREE_CODE (decl) == TREE_LIST)
9414 {
9415 tree t, tmpl = NULL_TREE;
9416 for (t = decl; t; t = TREE_CHAIN (t))
9417 {
9418 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9419 if (!tmpl)
9420 tmpl = elt;
9421 else if (tmpl != elt)
9422 break;
9423 }
9424 if (tmpl && t == NULL_TREE)
9425 return tmpl;
9426 else
9427 return decl;
9428 }
9429
9430 return (decl != NULL_TREE
9431 && DECL_SELF_REFERENCE_P (decl)
9432 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9433 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9434 }
9435
9436 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9437 parameters, find the desired type.
9438
9439 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9440
9441 IN_DECL, if non-NULL, is the template declaration we are trying to
9442 instantiate.
9443
9444 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9445 the class we are looking up.
9446
9447 Issue error and warning messages under control of COMPLAIN.
9448
9449 If the template class is really a local class in a template
9450 function, then the FUNCTION_CONTEXT is the function in which it is
9451 being instantiated.
9452
9453 ??? Note that this function is currently called *twice* for each
9454 template-id: the first time from the parser, while creating the
9455 incomplete type (finish_template_type), and the second type during the
9456 real instantiation (instantiate_template_class). This is surely something
9457 that we want to avoid. It also causes some problems with argument
9458 coercion (see convert_nontype_argument for more information on this). */
9459
9460 static tree
9461 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9462 int entering_scope, tsubst_flags_t complain)
9463 {
9464 tree templ = NULL_TREE, parmlist;
9465 tree t;
9466 spec_entry **slot;
9467 spec_entry *entry;
9468 spec_entry elt;
9469 hashval_t hash;
9470
9471 if (identifier_p (d1))
9472 {
9473 tree value = innermost_non_namespace_value (d1);
9474 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9475 templ = value;
9476 else
9477 {
9478 if (context)
9479 push_decl_namespace (context);
9480 templ = lookup_name (d1);
9481 templ = maybe_get_template_decl_from_type_decl (templ);
9482 if (context)
9483 pop_decl_namespace ();
9484 }
9485 if (templ)
9486 context = DECL_CONTEXT (templ);
9487 }
9488 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9489 {
9490 tree type = TREE_TYPE (d1);
9491
9492 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9493 an implicit typename for the second A. Deal with it. */
9494 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9495 type = TREE_TYPE (type);
9496
9497 if (CLASSTYPE_TEMPLATE_INFO (type))
9498 {
9499 templ = CLASSTYPE_TI_TEMPLATE (type);
9500 d1 = DECL_NAME (templ);
9501 }
9502 }
9503 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9504 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9505 {
9506 templ = TYPE_TI_TEMPLATE (d1);
9507 d1 = DECL_NAME (templ);
9508 }
9509 else if (DECL_TYPE_TEMPLATE_P (d1))
9510 {
9511 templ = d1;
9512 d1 = DECL_NAME (templ);
9513 context = DECL_CONTEXT (templ);
9514 }
9515 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9516 {
9517 templ = d1;
9518 d1 = DECL_NAME (templ);
9519 }
9520
9521 /* Issue an error message if we didn't find a template. */
9522 if (! templ)
9523 {
9524 if (complain & tf_error)
9525 error ("%qT is not a template", d1);
9526 return error_mark_node;
9527 }
9528
9529 if (TREE_CODE (templ) != TEMPLATE_DECL
9530 /* Make sure it's a user visible template, if it was named by
9531 the user. */
9532 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9533 && !PRIMARY_TEMPLATE_P (templ)))
9534 {
9535 if (complain & tf_error)
9536 {
9537 error ("non-template type %qT used as a template", d1);
9538 if (in_decl)
9539 error ("for template declaration %q+D", in_decl);
9540 }
9541 return error_mark_node;
9542 }
9543
9544 complain &= ~tf_user;
9545
9546 /* An alias that just changes the name of a template is equivalent to the
9547 other template, so if any of the arguments are pack expansions, strip
9548 the alias to avoid problems with a pack expansion passed to a non-pack
9549 alias template parameter (DR 1430). */
9550 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9551 templ = get_underlying_template (templ);
9552
9553 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9554 {
9555 tree parm;
9556 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9557 if (arglist2 == error_mark_node
9558 || (!uses_template_parms (arglist2)
9559 && check_instantiated_args (templ, arglist2, complain)))
9560 return error_mark_node;
9561
9562 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9563 return parm;
9564 }
9565 else
9566 {
9567 tree template_type = TREE_TYPE (templ);
9568 tree gen_tmpl;
9569 tree type_decl;
9570 tree found = NULL_TREE;
9571 int arg_depth;
9572 int parm_depth;
9573 int is_dependent_type;
9574 int use_partial_inst_tmpl = false;
9575
9576 if (template_type == error_mark_node)
9577 /* An error occurred while building the template TEMPL, and a
9578 diagnostic has most certainly been emitted for that
9579 already. Let's propagate that error. */
9580 return error_mark_node;
9581
9582 gen_tmpl = most_general_template (templ);
9583 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9584 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9585 arg_depth = TMPL_ARGS_DEPTH (arglist);
9586
9587 if (arg_depth == 1 && parm_depth > 1)
9588 {
9589 /* We've been given an incomplete set of template arguments.
9590 For example, given:
9591
9592 template <class T> struct S1 {
9593 template <class U> struct S2 {};
9594 template <class U> struct S2<U*> {};
9595 };
9596
9597 we will be called with an ARGLIST of `U*', but the
9598 TEMPLATE will be `template <class T> template
9599 <class U> struct S1<T>::S2'. We must fill in the missing
9600 arguments. */
9601 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9602 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9603 arg_depth = TMPL_ARGS_DEPTH (arglist);
9604 }
9605
9606 /* Now we should have enough arguments. */
9607 gcc_assert (parm_depth == arg_depth);
9608
9609 /* From here on, we're only interested in the most general
9610 template. */
9611
9612 /* Calculate the BOUND_ARGS. These will be the args that are
9613 actually tsubst'd into the definition to create the
9614 instantiation. */
9615 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9616 complain,
9617 /*require_all_args=*/true,
9618 /*use_default_args=*/true);
9619
9620 if (arglist == error_mark_node)
9621 /* We were unable to bind the arguments. */
9622 return error_mark_node;
9623
9624 /* In the scope of a template class, explicit references to the
9625 template class refer to the type of the template, not any
9626 instantiation of it. For example, in:
9627
9628 template <class T> class C { void f(C<T>); }
9629
9630 the `C<T>' is just the same as `C'. Outside of the
9631 class, however, such a reference is an instantiation. */
9632 if (entering_scope
9633 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9634 || currently_open_class (template_type))
9635 {
9636 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9637
9638 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9639 return template_type;
9640 }
9641
9642 /* If we already have this specialization, return it. */
9643 elt.tmpl = gen_tmpl;
9644 elt.args = arglist;
9645 elt.spec = NULL_TREE;
9646 hash = spec_hasher::hash (&elt);
9647 entry = type_specializations->find_with_hash (&elt, hash);
9648
9649 if (entry)
9650 return entry->spec;
9651
9652 /* If the the template's constraints are not satisfied,
9653 then we cannot form a valid type.
9654
9655 Note that the check is deferred until after the hash
9656 lookup. This prevents redundant checks on previously
9657 instantiated specializations. */
9658 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9659 {
9660 if (complain & tf_error)
9661 {
9662 auto_diagnostic_group d;
9663 error ("template constraint failure for %qD", gen_tmpl);
9664 diagnose_constraints (input_location, gen_tmpl, arglist);
9665 }
9666 return error_mark_node;
9667 }
9668
9669 is_dependent_type = uses_template_parms (arglist);
9670
9671 /* If the deduced arguments are invalid, then the binding
9672 failed. */
9673 if (!is_dependent_type
9674 && check_instantiated_args (gen_tmpl,
9675 INNERMOST_TEMPLATE_ARGS (arglist),
9676 complain))
9677 return error_mark_node;
9678
9679 if (!is_dependent_type
9680 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9681 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9682 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9683 {
9684 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9685 DECL_NAME (gen_tmpl),
9686 /*tag_scope=*/ts_global);
9687 return found;
9688 }
9689
9690 context = DECL_CONTEXT (gen_tmpl);
9691 if (context && TYPE_P (context))
9692 {
9693 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9694 context = complete_type (context);
9695 }
9696 else
9697 context = tsubst (context, arglist, complain, in_decl);
9698
9699 if (context == error_mark_node)
9700 return error_mark_node;
9701
9702 if (!context)
9703 context = global_namespace;
9704
9705 /* Create the type. */
9706 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9707 {
9708 /* The user referred to a specialization of an alias
9709 template represented by GEN_TMPL.
9710
9711 [temp.alias]/2 says:
9712
9713 When a template-id refers to the specialization of an
9714 alias template, it is equivalent to the associated
9715 type obtained by substitution of its
9716 template-arguments for the template-parameters in the
9717 type-id of the alias template. */
9718
9719 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9720 /* Note that the call above (by indirectly calling
9721 register_specialization in tsubst_decl) registers the
9722 TYPE_DECL representing the specialization of the alias
9723 template. So next time someone substitutes ARGLIST for
9724 the template parms into the alias template (GEN_TMPL),
9725 she'll get that TYPE_DECL back. */
9726
9727 if (t == error_mark_node)
9728 return t;
9729 }
9730 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9731 {
9732 if (!is_dependent_type)
9733 {
9734 set_current_access_from_decl (TYPE_NAME (template_type));
9735 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9736 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9737 arglist, complain, in_decl),
9738 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9739 arglist, complain, in_decl),
9740 SCOPED_ENUM_P (template_type), NULL);
9741
9742 if (t == error_mark_node)
9743 return t;
9744 }
9745 else
9746 {
9747 /* We don't want to call start_enum for this type, since
9748 the values for the enumeration constants may involve
9749 template parameters. And, no one should be interested
9750 in the enumeration constants for such a type. */
9751 t = cxx_make_type (ENUMERAL_TYPE);
9752 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9753 }
9754 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9755 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9756 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9757 }
9758 else if (CLASS_TYPE_P (template_type))
9759 {
9760 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9761 instantiated here. */
9762 gcc_assert (!LAMBDA_TYPE_P (template_type));
9763
9764 t = make_class_type (TREE_CODE (template_type));
9765 CLASSTYPE_DECLARED_CLASS (t)
9766 = CLASSTYPE_DECLARED_CLASS (template_type);
9767 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9768
9769 /* A local class. Make sure the decl gets registered properly. */
9770 if (context == current_function_decl)
9771 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9772 == error_mark_node)
9773 return error_mark_node;
9774
9775 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9776 /* This instantiation is another name for the primary
9777 template type. Set the TYPE_CANONICAL field
9778 appropriately. */
9779 TYPE_CANONICAL (t) = template_type;
9780 else if (any_template_arguments_need_structural_equality_p (arglist))
9781 /* Some of the template arguments require structural
9782 equality testing, so this template class requires
9783 structural equality testing. */
9784 SET_TYPE_STRUCTURAL_EQUALITY (t);
9785 }
9786 else
9787 gcc_unreachable ();
9788
9789 /* If we called start_enum or pushtag above, this information
9790 will already be set up. */
9791 if (!TYPE_NAME (t))
9792 {
9793 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9794
9795 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9796 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9797 DECL_SOURCE_LOCATION (type_decl)
9798 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9799 }
9800 else
9801 type_decl = TYPE_NAME (t);
9802
9803 if (CLASS_TYPE_P (template_type))
9804 {
9805 TREE_PRIVATE (type_decl)
9806 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9807 TREE_PROTECTED (type_decl)
9808 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9809 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9810 {
9811 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9812 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9813 }
9814 }
9815
9816 if (OVERLOAD_TYPE_P (t)
9817 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9818 {
9819 static const char *tags[] = {"abi_tag", "may_alias"};
9820
9821 for (unsigned ix = 0; ix != 2; ix++)
9822 {
9823 tree attributes
9824 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9825
9826 if (attributes)
9827 TYPE_ATTRIBUTES (t)
9828 = tree_cons (TREE_PURPOSE (attributes),
9829 TREE_VALUE (attributes),
9830 TYPE_ATTRIBUTES (t));
9831 }
9832 }
9833
9834 /* Let's consider the explicit specialization of a member
9835 of a class template specialization that is implicitly instantiated,
9836 e.g.:
9837 template<class T>
9838 struct S
9839 {
9840 template<class U> struct M {}; //#0
9841 };
9842
9843 template<>
9844 template<>
9845 struct S<int>::M<char> //#1
9846 {
9847 int i;
9848 };
9849 [temp.expl.spec]/4 says this is valid.
9850
9851 In this case, when we write:
9852 S<int>::M<char> m;
9853
9854 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9855 the one of #0.
9856
9857 When we encounter #1, we want to store the partial instantiation
9858 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9859
9860 For all cases other than this "explicit specialization of member of a
9861 class template", we just want to store the most general template into
9862 the CLASSTYPE_TI_TEMPLATE of M.
9863
9864 This case of "explicit specialization of member of a class template"
9865 only happens when:
9866 1/ the enclosing class is an instantiation of, and therefore not
9867 the same as, the context of the most general template, and
9868 2/ we aren't looking at the partial instantiation itself, i.e.
9869 the innermost arguments are not the same as the innermost parms of
9870 the most general template.
9871
9872 So it's only when 1/ and 2/ happens that we want to use the partial
9873 instantiation of the member template in lieu of its most general
9874 template. */
9875
9876 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9877 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9878 /* the enclosing class must be an instantiation... */
9879 && CLASS_TYPE_P (context)
9880 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9881 {
9882 TREE_VEC_LENGTH (arglist)--;
9883 ++processing_template_decl;
9884 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9885 tree partial_inst_args =
9886 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9887 arglist, complain, NULL_TREE);
9888 --processing_template_decl;
9889 TREE_VEC_LENGTH (arglist)++;
9890 if (partial_inst_args == error_mark_node)
9891 return error_mark_node;
9892 use_partial_inst_tmpl =
9893 /*...and we must not be looking at the partial instantiation
9894 itself. */
9895 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9896 partial_inst_args);
9897 }
9898
9899 if (!use_partial_inst_tmpl)
9900 /* This case is easy; there are no member templates involved. */
9901 found = gen_tmpl;
9902 else
9903 {
9904 /* This is a full instantiation of a member template. Find
9905 the partial instantiation of which this is an instance. */
9906
9907 /* Temporarily reduce by one the number of levels in the ARGLIST
9908 so as to avoid comparing the last set of arguments. */
9909 TREE_VEC_LENGTH (arglist)--;
9910 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9911 TREE_VEC_LENGTH (arglist)++;
9912 /* FOUND is either a proper class type, or an alias
9913 template specialization. In the later case, it's a
9914 TYPE_DECL, resulting from the substituting of arguments
9915 for parameters in the TYPE_DECL of the alias template
9916 done earlier. So be careful while getting the template
9917 of FOUND. */
9918 found = (TREE_CODE (found) == TEMPLATE_DECL
9919 ? found
9920 : (TREE_CODE (found) == TYPE_DECL
9921 ? DECL_TI_TEMPLATE (found)
9922 : CLASSTYPE_TI_TEMPLATE (found)));
9923
9924 if (DECL_CLASS_TEMPLATE_P (found)
9925 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9926 {
9927 /* If this partial instantiation is specialized, we want to
9928 use it for hash table lookup. */
9929 elt.tmpl = found;
9930 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9931 hash = spec_hasher::hash (&elt);
9932 }
9933 }
9934
9935 // Build template info for the new specialization.
9936 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9937
9938 elt.spec = t;
9939 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9940 gcc_checking_assert (*slot == NULL);
9941 entry = ggc_alloc<spec_entry> ();
9942 *entry = elt;
9943 *slot = entry;
9944
9945 /* Note this use of the partial instantiation so we can check it
9946 later in maybe_process_partial_specialization. */
9947 DECL_TEMPLATE_INSTANTIATIONS (found)
9948 = tree_cons (arglist, t,
9949 DECL_TEMPLATE_INSTANTIATIONS (found));
9950
9951 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9952 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9953 /* Now that the type has been registered on the instantiations
9954 list, we set up the enumerators. Because the enumeration
9955 constants may involve the enumeration type itself, we make
9956 sure to register the type first, and then create the
9957 constants. That way, doing tsubst_expr for the enumeration
9958 constants won't result in recursive calls here; we'll find
9959 the instantiation and exit above. */
9960 tsubst_enum (template_type, t, arglist);
9961
9962 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9963 /* If the type makes use of template parameters, the
9964 code that generates debugging information will crash. */
9965 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9966
9967 /* Possibly limit visibility based on template args. */
9968 TREE_PUBLIC (type_decl) = 1;
9969 determine_visibility (type_decl);
9970
9971 inherit_targ_abi_tags (t);
9972
9973 return t;
9974 }
9975 }
9976
9977 /* Wrapper for lookup_template_class_1. */
9978
9979 tree
9980 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9981 int entering_scope, tsubst_flags_t complain)
9982 {
9983 tree ret;
9984 timevar_push (TV_TEMPLATE_INST);
9985 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9986 entering_scope, complain);
9987 timevar_pop (TV_TEMPLATE_INST);
9988 return ret;
9989 }
9990
9991 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9992
9993 tree
9994 lookup_template_variable (tree templ, tree arglist)
9995 {
9996 if (flag_concepts && variable_concept_p (templ))
9997 return build_concept_check (templ, arglist, tf_none);
9998
9999 /* The type of the expression is NULL_TREE since the template-id could refer
10000 to an explicit or partial specialization. */
10001 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10002 }
10003
10004 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10005
10006 tree
10007 finish_template_variable (tree var, tsubst_flags_t complain)
10008 {
10009 tree templ = TREE_OPERAND (var, 0);
10010 tree arglist = TREE_OPERAND (var, 1);
10011
10012 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10013 arglist = add_outermost_template_args (tmpl_args, arglist);
10014
10015 templ = most_general_template (templ);
10016 tree parms = DECL_TEMPLATE_PARMS (templ);
10017 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10018 /*req_all*/true,
10019 /*use_default*/true);
10020
10021 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10022 {
10023 if (complain & tf_error)
10024 {
10025 auto_diagnostic_group d;
10026 error ("use of invalid variable template %qE", var);
10027 diagnose_constraints (location_of (var), templ, arglist);
10028 }
10029 return error_mark_node;
10030 }
10031
10032 return instantiate_template (templ, arglist, complain);
10033 }
10034
10035 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10036 TARGS template args, and instantiate it if it's not dependent. */
10037
10038 tree
10039 lookup_and_finish_template_variable (tree templ, tree targs,
10040 tsubst_flags_t complain)
10041 {
10042 templ = lookup_template_variable (templ, targs);
10043 if (!any_dependent_template_arguments_p (targs))
10044 {
10045 templ = finish_template_variable (templ, complain);
10046 mark_used (templ);
10047 }
10048
10049 return convert_from_reference (templ);
10050 }
10051
10052 \f
10053 struct pair_fn_data
10054 {
10055 tree_fn_t fn;
10056 tree_fn_t any_fn;
10057 void *data;
10058 /* True when we should also visit template parameters that occur in
10059 non-deduced contexts. */
10060 bool include_nondeduced_p;
10061 hash_set<tree> *visited;
10062 };
10063
10064 /* Called from for_each_template_parm via walk_tree. */
10065
10066 static tree
10067 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10068 {
10069 tree t = *tp;
10070 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10071 tree_fn_t fn = pfd->fn;
10072 void *data = pfd->data;
10073 tree result = NULL_TREE;
10074
10075 #define WALK_SUBTREE(NODE) \
10076 do \
10077 { \
10078 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10079 pfd->include_nondeduced_p, \
10080 pfd->any_fn); \
10081 if (result) goto out; \
10082 } \
10083 while (0)
10084
10085 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10086 return t;
10087
10088 if (TYPE_P (t)
10089 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10090 WALK_SUBTREE (TYPE_CONTEXT (t));
10091
10092 switch (TREE_CODE (t))
10093 {
10094 case RECORD_TYPE:
10095 if (TYPE_PTRMEMFUNC_P (t))
10096 break;
10097 /* Fall through. */
10098
10099 case UNION_TYPE:
10100 case ENUMERAL_TYPE:
10101 if (!TYPE_TEMPLATE_INFO (t))
10102 *walk_subtrees = 0;
10103 else
10104 WALK_SUBTREE (TYPE_TI_ARGS (t));
10105 break;
10106
10107 case INTEGER_TYPE:
10108 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10109 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10110 break;
10111
10112 case METHOD_TYPE:
10113 /* Since we're not going to walk subtrees, we have to do this
10114 explicitly here. */
10115 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10116 /* Fall through. */
10117
10118 case FUNCTION_TYPE:
10119 /* Check the return type. */
10120 WALK_SUBTREE (TREE_TYPE (t));
10121
10122 /* Check the parameter types. Since default arguments are not
10123 instantiated until they are needed, the TYPE_ARG_TYPES may
10124 contain expressions that involve template parameters. But,
10125 no-one should be looking at them yet. And, once they're
10126 instantiated, they don't contain template parameters, so
10127 there's no point in looking at them then, either. */
10128 {
10129 tree parm;
10130
10131 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10132 WALK_SUBTREE (TREE_VALUE (parm));
10133
10134 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10135 want walk_tree walking into them itself. */
10136 *walk_subtrees = 0;
10137 }
10138
10139 if (flag_noexcept_type)
10140 {
10141 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10142 if (spec)
10143 WALK_SUBTREE (TREE_PURPOSE (spec));
10144 }
10145 break;
10146
10147 case TYPEOF_TYPE:
10148 case DECLTYPE_TYPE:
10149 case UNDERLYING_TYPE:
10150 if (pfd->include_nondeduced_p
10151 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10152 pfd->visited,
10153 pfd->include_nondeduced_p,
10154 pfd->any_fn))
10155 return error_mark_node;
10156 *walk_subtrees = false;
10157 break;
10158
10159 case FUNCTION_DECL:
10160 case VAR_DECL:
10161 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10162 WALK_SUBTREE (DECL_TI_ARGS (t));
10163 /* Fall through. */
10164
10165 case PARM_DECL:
10166 case CONST_DECL:
10167 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10168 WALK_SUBTREE (DECL_INITIAL (t));
10169 if (DECL_CONTEXT (t)
10170 && pfd->include_nondeduced_p)
10171 WALK_SUBTREE (DECL_CONTEXT (t));
10172 break;
10173
10174 case BOUND_TEMPLATE_TEMPLATE_PARM:
10175 /* Record template parameters such as `T' inside `TT<T>'. */
10176 WALK_SUBTREE (TYPE_TI_ARGS (t));
10177 /* Fall through. */
10178
10179 case TEMPLATE_TEMPLATE_PARM:
10180 case TEMPLATE_TYPE_PARM:
10181 case TEMPLATE_PARM_INDEX:
10182 if (fn && (*fn)(t, data))
10183 return t;
10184 else if (!fn)
10185 return t;
10186 break;
10187
10188 case TEMPLATE_DECL:
10189 /* A template template parameter is encountered. */
10190 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10191 WALK_SUBTREE (TREE_TYPE (t));
10192
10193 /* Already substituted template template parameter */
10194 *walk_subtrees = 0;
10195 break;
10196
10197 case TYPENAME_TYPE:
10198 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10199 partial instantiation. */
10200 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10201 break;
10202
10203 case CONSTRUCTOR:
10204 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10205 && pfd->include_nondeduced_p)
10206 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10207 break;
10208
10209 case INDIRECT_REF:
10210 case COMPONENT_REF:
10211 /* If there's no type, then this thing must be some expression
10212 involving template parameters. */
10213 if (!fn && !TREE_TYPE (t))
10214 return error_mark_node;
10215 break;
10216
10217 case MODOP_EXPR:
10218 case CAST_EXPR:
10219 case IMPLICIT_CONV_EXPR:
10220 case REINTERPRET_CAST_EXPR:
10221 case CONST_CAST_EXPR:
10222 case STATIC_CAST_EXPR:
10223 case DYNAMIC_CAST_EXPR:
10224 case ARROW_EXPR:
10225 case DOTSTAR_EXPR:
10226 case TYPEID_EXPR:
10227 case PSEUDO_DTOR_EXPR:
10228 if (!fn)
10229 return error_mark_node;
10230 break;
10231
10232 case SCOPE_REF:
10233 if (pfd->include_nondeduced_p)
10234 WALK_SUBTREE (TREE_OPERAND (t, 0));
10235 break;
10236
10237 case REQUIRES_EXPR:
10238 {
10239 if (!fn)
10240 return error_mark_node;
10241
10242 /* Recursively walk the type of each constraint variable. */
10243 tree p = TREE_OPERAND (t, 0);
10244 while (p)
10245 {
10246 WALK_SUBTREE (TREE_TYPE (p));
10247 p = TREE_CHAIN (p);
10248 }
10249 }
10250 break;
10251
10252 default:
10253 break;
10254 }
10255
10256 #undef WALK_SUBTREE
10257
10258 /* We didn't find any template parameters we liked. */
10259 out:
10260 return result;
10261 }
10262
10263 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10264 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10265 call FN with the parameter and the DATA.
10266 If FN returns nonzero, the iteration is terminated, and
10267 for_each_template_parm returns 1. Otherwise, the iteration
10268 continues. If FN never returns a nonzero value, the value
10269 returned by for_each_template_parm is 0. If FN is NULL, it is
10270 considered to be the function which always returns 1.
10271
10272 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10273 parameters that occur in non-deduced contexts. When false, only
10274 visits those template parameters that can be deduced. */
10275
10276 static tree
10277 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10278 hash_set<tree> *visited,
10279 bool include_nondeduced_p,
10280 tree_fn_t any_fn)
10281 {
10282 struct pair_fn_data pfd;
10283 tree result;
10284
10285 /* Set up. */
10286 pfd.fn = fn;
10287 pfd.any_fn = any_fn;
10288 pfd.data = data;
10289 pfd.include_nondeduced_p = include_nondeduced_p;
10290
10291 /* Walk the tree. (Conceptually, we would like to walk without
10292 duplicates, but for_each_template_parm_r recursively calls
10293 for_each_template_parm, so we would need to reorganize a fair
10294 bit to use walk_tree_without_duplicates, so we keep our own
10295 visited list.) */
10296 if (visited)
10297 pfd.visited = visited;
10298 else
10299 pfd.visited = new hash_set<tree>;
10300 result = cp_walk_tree (&t,
10301 for_each_template_parm_r,
10302 &pfd,
10303 pfd.visited);
10304
10305 /* Clean up. */
10306 if (!visited)
10307 {
10308 delete pfd.visited;
10309 pfd.visited = 0;
10310 }
10311
10312 return result;
10313 }
10314
10315 struct find_template_parameter_info
10316 {
10317 explicit find_template_parameter_info (int d)
10318 : max_depth (d)
10319 {}
10320
10321 hash_set<tree> visited;
10322 hash_set<tree> parms;
10323 int max_depth;
10324 };
10325
10326 /* Appends the declaration of T to the list in DATA. */
10327
10328 static int
10329 keep_template_parm (tree t, void* data)
10330 {
10331 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10332
10333 /* Template parameters declared within the expression are not part of
10334 the parameter mapping. For example, in this concept:
10335
10336 template<typename T>
10337 concept C = requires { <expr> } -> same_as<int>;
10338
10339 the return specifier same_as<int> declares a new decltype parameter
10340 that must not be part of the parameter mapping. The same is true
10341 for generic lambda parameters, lambda template parameters, etc. */
10342 int level;
10343 int index;
10344 template_parm_level_and_index (t, &level, &index);
10345 if (level > ftpi->max_depth)
10346 return 0;
10347
10348 /* Arguments like const T yield parameters like const T. This means that
10349 a template-id like X<T, const T> would yield two distinct parameters:
10350 T and const T. Adjust types to their unqualified versions. */
10351 if (TYPE_P (t))
10352 t = TYPE_MAIN_VARIANT (t);
10353 ftpi->parms.add (t);
10354
10355 return 0;
10356 }
10357
10358 /* Ensure that we recursively examine certain terms that are not normally
10359 visited in for_each_template_parm_r. */
10360
10361 static int
10362 any_template_parm_r (tree t, void *data)
10363 {
10364 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10365
10366 #define WALK_SUBTREE(NODE) \
10367 do \
10368 { \
10369 for_each_template_parm (NODE, keep_template_parm, data, \
10370 &ftpi->visited, true, \
10371 any_template_parm_r); \
10372 } \
10373 while (0)
10374
10375 switch (TREE_CODE (t))
10376 {
10377 case RECORD_TYPE:
10378 case UNION_TYPE:
10379 case ENUMERAL_TYPE:
10380 /* Search for template parameters in type aliases. */
10381 if (alias_template_specialization_p (t))
10382 {
10383 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
10384 WALK_SUBTREE (TI_ARGS (tinfo));
10385 }
10386 break;
10387
10388 case TEMPLATE_TYPE_PARM:
10389 /* Type constraints of a placeholder type may contain parameters. */
10390 if (is_auto (t))
10391 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10392 WALK_SUBTREE (constr);
10393 break;
10394
10395 case TEMPLATE_ID_EXPR:
10396 /* Search through references to variable templates. */
10397 WALK_SUBTREE (TREE_OPERAND (t, 0));
10398 WALK_SUBTREE (TREE_OPERAND (t, 1));
10399 break;
10400
10401 case CONSTRUCTOR:
10402 if (TREE_TYPE (t))
10403 WALK_SUBTREE (TREE_TYPE (t));
10404 break;
10405
10406 case PARM_DECL:
10407 /* A parameter or constraint variable may also depend on a template
10408 parameter without explicitly naming it. */
10409 WALK_SUBTREE (TREE_TYPE (t));
10410 break;
10411
10412 default:
10413 break;
10414 }
10415
10416 /* Keep walking. */
10417 return 0;
10418 }
10419
10420 /* Returns a list of unique template parameters found within T. */
10421
10422 tree
10423 find_template_parameters (tree t, int depth)
10424 {
10425 find_template_parameter_info ftpi (depth);
10426 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10427 /*include_nondeduced*/true, any_template_parm_r);
10428 tree list = NULL_TREE;
10429 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10430 iter != ftpi.parms.end(); ++iter)
10431 list = tree_cons (NULL_TREE, *iter, list);
10432 return list;
10433 }
10434
10435 /* Returns true if T depends on any template parameter. */
10436
10437 int
10438 uses_template_parms (tree t)
10439 {
10440 if (t == NULL_TREE)
10441 return false;
10442
10443 bool dependent_p;
10444 int saved_processing_template_decl;
10445
10446 saved_processing_template_decl = processing_template_decl;
10447 if (!saved_processing_template_decl)
10448 processing_template_decl = 1;
10449 if (TYPE_P (t))
10450 dependent_p = dependent_type_p (t);
10451 else if (TREE_CODE (t) == TREE_VEC)
10452 dependent_p = any_dependent_template_arguments_p (t);
10453 else if (TREE_CODE (t) == TREE_LIST)
10454 dependent_p = (uses_template_parms (TREE_VALUE (t))
10455 || uses_template_parms (TREE_CHAIN (t)));
10456 else if (TREE_CODE (t) == TYPE_DECL)
10457 dependent_p = dependent_type_p (TREE_TYPE (t));
10458 else if (DECL_P (t)
10459 || EXPR_P (t)
10460 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10461 || TREE_CODE (t) == OVERLOAD
10462 || BASELINK_P (t)
10463 || identifier_p (t)
10464 || TREE_CODE (t) == TRAIT_EXPR
10465 || TREE_CODE (t) == CONSTRUCTOR
10466 || CONSTANT_CLASS_P (t))
10467 dependent_p = (type_dependent_expression_p (t)
10468 || value_dependent_expression_p (t));
10469 else
10470 {
10471 gcc_assert (t == error_mark_node);
10472 dependent_p = false;
10473 }
10474
10475 processing_template_decl = saved_processing_template_decl;
10476
10477 return dependent_p;
10478 }
10479
10480 /* Returns true iff current_function_decl is an incompletely instantiated
10481 template. Useful instead of processing_template_decl because the latter
10482 is set to 0 during instantiate_non_dependent_expr. */
10483
10484 bool
10485 in_template_function (void)
10486 {
10487 tree fn = current_function_decl;
10488 bool ret;
10489 ++processing_template_decl;
10490 ret = (fn && DECL_LANG_SPECIFIC (fn)
10491 && DECL_TEMPLATE_INFO (fn)
10492 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10493 --processing_template_decl;
10494 return ret;
10495 }
10496
10497 /* Returns true if T depends on any template parameter with level LEVEL. */
10498
10499 bool
10500 uses_template_parms_level (tree t, int level)
10501 {
10502 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10503 /*include_nondeduced_p=*/true);
10504 }
10505
10506 /* Returns true if the signature of DECL depends on any template parameter from
10507 its enclosing class. */
10508
10509 bool
10510 uses_outer_template_parms (tree decl)
10511 {
10512 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10513 if (depth == 0)
10514 return false;
10515 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10516 &depth, NULL, /*include_nondeduced_p=*/true))
10517 return true;
10518 if (PRIMARY_TEMPLATE_P (decl)
10519 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10520 (DECL_TEMPLATE_PARMS (decl)),
10521 template_parm_outer_level,
10522 &depth, NULL, /*include_nondeduced_p=*/true))
10523 return true;
10524 tree ci = get_constraints (decl);
10525 if (ci)
10526 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10527 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10528 &depth, NULL, /*nondeduced*/true))
10529 return true;
10530 return false;
10531 }
10532
10533 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10534 ill-formed translation unit, i.e. a variable or function that isn't
10535 usable in a constant expression. */
10536
10537 static inline bool
10538 neglectable_inst_p (tree d)
10539 {
10540 return (d && DECL_P (d)
10541 && !undeduced_auto_decl (d)
10542 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10543 : decl_maybe_constant_var_p (d)));
10544 }
10545
10546 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10547 neglectable and instantiated from within an erroneous instantiation. */
10548
10549 static bool
10550 limit_bad_template_recursion (tree decl)
10551 {
10552 struct tinst_level *lev = current_tinst_level;
10553 int errs = errorcount + sorrycount;
10554 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10555 return false;
10556
10557 for (; lev; lev = lev->next)
10558 if (neglectable_inst_p (lev->maybe_get_node ()))
10559 break;
10560
10561 return (lev && errs > lev->errors);
10562 }
10563
10564 static int tinst_depth;
10565 extern int max_tinst_depth;
10566 int depth_reached;
10567
10568 static GTY(()) struct tinst_level *last_error_tinst_level;
10569
10570 /* We're starting to instantiate D; record the template instantiation context
10571 at LOC for diagnostics and to restore it later. */
10572
10573 static bool
10574 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10575 {
10576 struct tinst_level *new_level;
10577
10578 if (tinst_depth >= max_tinst_depth)
10579 {
10580 /* Tell error.c not to try to instantiate any templates. */
10581 at_eof = 2;
10582 fatal_error (input_location,
10583 "template instantiation depth exceeds maximum of %d"
10584 " (use %<-ftemplate-depth=%> to increase the maximum)",
10585 max_tinst_depth);
10586 return false;
10587 }
10588
10589 /* If the current instantiation caused problems, don't let it instantiate
10590 anything else. Do allow deduction substitution and decls usable in
10591 constant expressions. */
10592 if (!targs && limit_bad_template_recursion (tldcl))
10593 return false;
10594
10595 /* When not -quiet, dump template instantiations other than functions, since
10596 announce_function will take care of those. */
10597 if (!quiet_flag && !targs
10598 && TREE_CODE (tldcl) != TREE_LIST
10599 && TREE_CODE (tldcl) != FUNCTION_DECL)
10600 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10601
10602 new_level = tinst_level_freelist ().alloc ();
10603 new_level->tldcl = tldcl;
10604 new_level->targs = targs;
10605 new_level->locus = loc;
10606 new_level->errors = errorcount + sorrycount;
10607 new_level->next = NULL;
10608 new_level->refcount = 0;
10609 set_refcount_ptr (new_level->next, current_tinst_level);
10610 set_refcount_ptr (current_tinst_level, new_level);
10611
10612 ++tinst_depth;
10613 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10614 depth_reached = tinst_depth;
10615
10616 return true;
10617 }
10618
10619 /* We're starting substitution of TMPL<ARGS>; record the template
10620 substitution context for diagnostics and to restore it later. */
10621
10622 static bool
10623 push_tinst_level (tree tmpl, tree args)
10624 {
10625 return push_tinst_level_loc (tmpl, args, input_location);
10626 }
10627
10628 /* We're starting to instantiate D; record INPUT_LOCATION and the
10629 template instantiation context for diagnostics and to restore it
10630 later. */
10631
10632 bool
10633 push_tinst_level (tree d)
10634 {
10635 return push_tinst_level_loc (d, input_location);
10636 }
10637
10638 /* Likewise, but record LOC as the program location. */
10639
10640 bool
10641 push_tinst_level_loc (tree d, location_t loc)
10642 {
10643 gcc_assert (TREE_CODE (d) != TREE_LIST);
10644 return push_tinst_level_loc (d, NULL, loc);
10645 }
10646
10647 /* We're done instantiating this template; return to the instantiation
10648 context. */
10649
10650 void
10651 pop_tinst_level (void)
10652 {
10653 /* Restore the filename and line number stashed away when we started
10654 this instantiation. */
10655 input_location = current_tinst_level->locus;
10656 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10657 --tinst_depth;
10658 }
10659
10660 /* We're instantiating a deferred template; restore the template
10661 instantiation context in which the instantiation was requested, which
10662 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10663
10664 static tree
10665 reopen_tinst_level (struct tinst_level *level)
10666 {
10667 struct tinst_level *t;
10668
10669 tinst_depth = 0;
10670 for (t = level; t; t = t->next)
10671 ++tinst_depth;
10672
10673 set_refcount_ptr (current_tinst_level, level);
10674 pop_tinst_level ();
10675 if (current_tinst_level)
10676 current_tinst_level->errors = errorcount+sorrycount;
10677 return level->maybe_get_node ();
10678 }
10679
10680 /* Returns the TINST_LEVEL which gives the original instantiation
10681 context. */
10682
10683 struct tinst_level *
10684 outermost_tinst_level (void)
10685 {
10686 struct tinst_level *level = current_tinst_level;
10687 if (level)
10688 while (level->next)
10689 level = level->next;
10690 return level;
10691 }
10692
10693 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10694 vector of template arguments, as for tsubst.
10695
10696 Returns an appropriate tsubst'd friend declaration. */
10697
10698 static tree
10699 tsubst_friend_function (tree decl, tree args)
10700 {
10701 tree new_friend;
10702
10703 if (TREE_CODE (decl) == FUNCTION_DECL
10704 && DECL_TEMPLATE_INSTANTIATION (decl)
10705 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10706 /* This was a friend declared with an explicit template
10707 argument list, e.g.:
10708
10709 friend void f<>(T);
10710
10711 to indicate that f was a template instantiation, not a new
10712 function declaration. Now, we have to figure out what
10713 instantiation of what template. */
10714 {
10715 tree template_id, arglist, fns;
10716 tree new_args;
10717 tree tmpl;
10718 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10719
10720 /* Friend functions are looked up in the containing namespace scope.
10721 We must enter that scope, to avoid finding member functions of the
10722 current class with same name. */
10723 push_nested_namespace (ns);
10724 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10725 tf_warning_or_error, NULL_TREE,
10726 /*integral_constant_expression_p=*/false);
10727 pop_nested_namespace (ns);
10728 arglist = tsubst (DECL_TI_ARGS (decl), args,
10729 tf_warning_or_error, NULL_TREE);
10730 template_id = lookup_template_function (fns, arglist);
10731
10732 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10733 tmpl = determine_specialization (template_id, new_friend,
10734 &new_args,
10735 /*need_member_template=*/0,
10736 TREE_VEC_LENGTH (args),
10737 tsk_none);
10738 return instantiate_template (tmpl, new_args, tf_error);
10739 }
10740
10741 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10742
10743 /* The NEW_FRIEND will look like an instantiation, to the
10744 compiler, but is not an instantiation from the point of view of
10745 the language. For example, we might have had:
10746
10747 template <class T> struct S {
10748 template <class U> friend void f(T, U);
10749 };
10750
10751 Then, in S<int>, template <class U> void f(int, U) is not an
10752 instantiation of anything. */
10753 if (new_friend == error_mark_node)
10754 return error_mark_node;
10755
10756 DECL_USE_TEMPLATE (new_friend) = 0;
10757 if (TREE_CODE (decl) == TEMPLATE_DECL)
10758 {
10759 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10760 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10761 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10762
10763 /* Attach the template requirements to the new declaration
10764 for declaration matching. We need to rebuild the requirements
10765 so that parameter levels match. */
10766 if (tree ci = get_constraints (decl))
10767 {
10768 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10769 tree args = generic_targs_for (new_friend);
10770 tree treqs = tsubst_constraint (CI_TEMPLATE_REQS (ci), args,
10771 tf_warning_or_error, NULL_TREE);
10772 tree freqs = tsubst_constraint (CI_DECLARATOR_REQS (ci), args,
10773 tf_warning_or_error, NULL_TREE);
10774
10775 /* Update the constraints -- these won't really be valid for
10776 checking, but that's not what we need them for. These ensure
10777 that the declared function can find the friend during
10778 declaration matching. */
10779 tree new_ci = get_constraints (new_friend);
10780 CI_TEMPLATE_REQS (new_ci) = treqs;
10781 CI_DECLARATOR_REQS (new_ci) = freqs;
10782
10783 /* Also update the template parameter list. */
10784 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10785 }
10786 }
10787
10788 /* The mangled name for the NEW_FRIEND is incorrect. The function
10789 is not a template instantiation and should not be mangled like
10790 one. Therefore, we forget the mangling here; we'll recompute it
10791 later if we need it. */
10792 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10793 {
10794 SET_DECL_RTL (new_friend, NULL);
10795 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10796 }
10797
10798 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10799 {
10800 tree old_decl;
10801 tree new_friend_template_info;
10802 tree new_friend_result_template_info;
10803 tree ns;
10804 int new_friend_is_defn;
10805
10806 /* We must save some information from NEW_FRIEND before calling
10807 duplicate decls since that function will free NEW_FRIEND if
10808 possible. */
10809 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10810 new_friend_is_defn =
10811 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10812 (template_for_substitution (new_friend)))
10813 != NULL_TREE);
10814 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10815 {
10816 /* This declaration is a `primary' template. */
10817 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10818
10819 new_friend_result_template_info
10820 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10821 }
10822 else
10823 new_friend_result_template_info = NULL_TREE;
10824
10825 /* Inside pushdecl_namespace_level, we will push into the
10826 current namespace. However, the friend function should go
10827 into the namespace of the template. */
10828 ns = decl_namespace_context (new_friend);
10829 push_nested_namespace (ns);
10830 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10831 pop_nested_namespace (ns);
10832
10833 if (old_decl == error_mark_node)
10834 return error_mark_node;
10835
10836 if (old_decl != new_friend)
10837 {
10838 /* This new friend declaration matched an existing
10839 declaration. For example, given:
10840
10841 template <class T> void f(T);
10842 template <class U> class C {
10843 template <class T> friend void f(T) {}
10844 };
10845
10846 the friend declaration actually provides the definition
10847 of `f', once C has been instantiated for some type. So,
10848 old_decl will be the out-of-class template declaration,
10849 while new_friend is the in-class definition.
10850
10851 But, if `f' was called before this point, the
10852 instantiation of `f' will have DECL_TI_ARGS corresponding
10853 to `T' but not to `U', references to which might appear
10854 in the definition of `f'. Previously, the most general
10855 template for an instantiation of `f' was the out-of-class
10856 version; now it is the in-class version. Therefore, we
10857 run through all specialization of `f', adding to their
10858 DECL_TI_ARGS appropriately. In particular, they need a
10859 new set of outer arguments, corresponding to the
10860 arguments for this class instantiation.
10861
10862 The same situation can arise with something like this:
10863
10864 friend void f(int);
10865 template <class T> class C {
10866 friend void f(T) {}
10867 };
10868
10869 when `C<int>' is instantiated. Now, `f(int)' is defined
10870 in the class. */
10871
10872 if (!new_friend_is_defn)
10873 /* On the other hand, if the in-class declaration does
10874 *not* provide a definition, then we don't want to alter
10875 existing definitions. We can just leave everything
10876 alone. */
10877 ;
10878 else
10879 {
10880 tree new_template = TI_TEMPLATE (new_friend_template_info);
10881 tree new_args = TI_ARGS (new_friend_template_info);
10882
10883 /* Overwrite whatever template info was there before, if
10884 any, with the new template information pertaining to
10885 the declaration. */
10886 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10887
10888 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10889 {
10890 /* We should have called reregister_specialization in
10891 duplicate_decls. */
10892 gcc_assert (retrieve_specialization (new_template,
10893 new_args, 0)
10894 == old_decl);
10895
10896 /* Instantiate it if the global has already been used. */
10897 if (DECL_ODR_USED (old_decl))
10898 instantiate_decl (old_decl, /*defer_ok=*/true,
10899 /*expl_inst_class_mem_p=*/false);
10900 }
10901 else
10902 {
10903 tree t;
10904
10905 /* Indicate that the old function template is a partial
10906 instantiation. */
10907 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10908 = new_friend_result_template_info;
10909
10910 gcc_assert (new_template
10911 == most_general_template (new_template));
10912 gcc_assert (new_template != old_decl);
10913
10914 /* Reassign any specializations already in the hash table
10915 to the new more general template, and add the
10916 additional template args. */
10917 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10918 t != NULL_TREE;
10919 t = TREE_CHAIN (t))
10920 {
10921 tree spec = TREE_VALUE (t);
10922 spec_entry elt;
10923
10924 elt.tmpl = old_decl;
10925 elt.args = DECL_TI_ARGS (spec);
10926 elt.spec = NULL_TREE;
10927
10928 decl_specializations->remove_elt (&elt);
10929
10930 DECL_TI_ARGS (spec)
10931 = add_outermost_template_args (new_args,
10932 DECL_TI_ARGS (spec));
10933
10934 register_specialization
10935 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10936
10937 }
10938 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10939 }
10940 }
10941
10942 /* The information from NEW_FRIEND has been merged into OLD_DECL
10943 by duplicate_decls. */
10944 new_friend = old_decl;
10945 }
10946 }
10947 else
10948 {
10949 tree context = DECL_CONTEXT (new_friend);
10950 bool dependent_p;
10951
10952 /* In the code
10953 template <class T> class C {
10954 template <class U> friend void C1<U>::f (); // case 1
10955 friend void C2<T>::f (); // case 2
10956 };
10957 we only need to make sure CONTEXT is a complete type for
10958 case 2. To distinguish between the two cases, we note that
10959 CONTEXT of case 1 remains dependent type after tsubst while
10960 this isn't true for case 2. */
10961 ++processing_template_decl;
10962 dependent_p = dependent_type_p (context);
10963 --processing_template_decl;
10964
10965 if (!dependent_p
10966 && !complete_type_or_else (context, NULL_TREE))
10967 return error_mark_node;
10968
10969 if (COMPLETE_TYPE_P (context))
10970 {
10971 tree fn = new_friend;
10972 /* do_friend adds the TEMPLATE_DECL for any member friend
10973 template even if it isn't a member template, i.e.
10974 template <class T> friend A<T>::f();
10975 Look through it in that case. */
10976 if (TREE_CODE (fn) == TEMPLATE_DECL
10977 && !PRIMARY_TEMPLATE_P (fn))
10978 fn = DECL_TEMPLATE_RESULT (fn);
10979 /* Check to see that the declaration is really present, and,
10980 possibly obtain an improved declaration. */
10981 fn = check_classfn (context, fn, NULL_TREE);
10982
10983 if (fn)
10984 new_friend = fn;
10985 }
10986 }
10987
10988 return new_friend;
10989 }
10990
10991 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10992 template arguments, as for tsubst.
10993
10994 Returns an appropriate tsubst'd friend type or error_mark_node on
10995 failure. */
10996
10997 static tree
10998 tsubst_friend_class (tree friend_tmpl, tree args)
10999 {
11000 tree tmpl;
11001
11002 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11003 {
11004 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11005 return TREE_TYPE (tmpl);
11006 }
11007
11008 tree context = CP_DECL_CONTEXT (friend_tmpl);
11009 if (TREE_CODE (context) == NAMESPACE_DECL)
11010 push_nested_namespace (context);
11011 else
11012 {
11013 context = tsubst (context, args, tf_error, NULL_TREE);
11014 push_nested_class (context);
11015 }
11016
11017 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11018 /*non_class=*/false, /*block_p=*/false,
11019 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11020
11021 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11022 {
11023 /* The friend template has already been declared. Just
11024 check to see that the declarations match, and install any new
11025 default parameters. We must tsubst the default parameters,
11026 of course. We only need the innermost template parameters
11027 because that is all that redeclare_class_template will look
11028 at. */
11029 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11030 > TMPL_ARGS_DEPTH (args))
11031 {
11032 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11033 args, tf_warning_or_error);
11034 location_t saved_input_location = input_location;
11035 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11036 tree cons = get_constraints (tmpl);
11037 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11038 input_location = saved_input_location;
11039 }
11040 }
11041 else
11042 {
11043 /* The friend template has not already been declared. In this
11044 case, the instantiation of the template class will cause the
11045 injection of this template into the namespace scope. */
11046 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11047
11048 if (tmpl != error_mark_node)
11049 {
11050 /* The new TMPL is not an instantiation of anything, so we
11051 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11052 for the new type because that is supposed to be the
11053 corresponding template decl, i.e., TMPL. */
11054 DECL_USE_TEMPLATE (tmpl) = 0;
11055 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11056 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11057 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11058 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11059
11060 /* It is hidden. */
11061 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11062 DECL_ANTICIPATED (tmpl)
11063 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11064
11065 /* Inject this template into the enclosing namspace scope. */
11066 tmpl = pushdecl_namespace_level (tmpl, true);
11067 }
11068 }
11069
11070 if (TREE_CODE (context) == NAMESPACE_DECL)
11071 pop_nested_namespace (context);
11072 else
11073 pop_nested_class ();
11074
11075 return TREE_TYPE (tmpl);
11076 }
11077
11078 /* Returns zero if TYPE cannot be completed later due to circularity.
11079 Otherwise returns one. */
11080
11081 static int
11082 can_complete_type_without_circularity (tree type)
11083 {
11084 if (type == NULL_TREE || type == error_mark_node)
11085 return 0;
11086 else if (COMPLETE_TYPE_P (type))
11087 return 1;
11088 else if (TREE_CODE (type) == ARRAY_TYPE)
11089 return can_complete_type_without_circularity (TREE_TYPE (type));
11090 else if (CLASS_TYPE_P (type)
11091 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11092 return 0;
11093 else
11094 return 1;
11095 }
11096
11097 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11098 tsubst_flags_t, tree);
11099
11100 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11101 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11102
11103 static tree
11104 tsubst_attribute (tree t, tree *decl_p, tree args,
11105 tsubst_flags_t complain, tree in_decl)
11106 {
11107 gcc_assert (ATTR_IS_DEPENDENT (t));
11108
11109 tree val = TREE_VALUE (t);
11110 if (val == NULL_TREE)
11111 /* Nothing to do. */;
11112 else if ((flag_openmp || flag_openmp_simd)
11113 && is_attribute_p ("omp declare simd",
11114 get_attribute_name (t)))
11115 {
11116 tree clauses = TREE_VALUE (val);
11117 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11118 complain, in_decl);
11119 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11120 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11121 tree parms = DECL_ARGUMENTS (*decl_p);
11122 clauses
11123 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11124 if (clauses)
11125 val = build_tree_list (NULL_TREE, clauses);
11126 else
11127 val = NULL_TREE;
11128 }
11129 else if (flag_openmp
11130 && is_attribute_p ("omp declare variant base",
11131 get_attribute_name (t)))
11132 {
11133 ++cp_unevaluated_operand;
11134 tree varid
11135 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11136 in_decl, /*integral_constant_expression_p=*/false);
11137 --cp_unevaluated_operand;
11138 tree chain = TREE_CHAIN (val);
11139 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11140 tree ctx = copy_list (TREE_VALUE (val));
11141 tree simd = get_identifier ("simd");
11142 tree score = get_identifier (" score");
11143 tree condition = get_identifier ("condition");
11144 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11145 {
11146 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11147 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11148 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11149 {
11150 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11151 {
11152 tree clauses = TREE_VALUE (t2);
11153 clauses = tsubst_omp_clauses (clauses,
11154 C_ORT_OMP_DECLARE_SIMD, args,
11155 complain, in_decl);
11156 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11157 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11158 TREE_VALUE (t2) = clauses;
11159 }
11160 else
11161 {
11162 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11163 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11164 if (TREE_VALUE (t3))
11165 {
11166 bool allow_string
11167 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11168 && TREE_PURPOSE (t3) != score);
11169 tree v = TREE_VALUE (t3);
11170 if (TREE_CODE (v) == STRING_CST && allow_string)
11171 continue;
11172 v = tsubst_expr (v, args, complain, in_decl, true);
11173 v = fold_non_dependent_expr (v);
11174 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11175 || (TREE_PURPOSE (t3) == score
11176 ? TREE_CODE (v) != INTEGER_CST
11177 : !tree_fits_shwi_p (v)))
11178 {
11179 location_t loc
11180 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11181 match_loc);
11182 if (TREE_PURPOSE (t3) == score)
11183 error_at (loc, "score argument must be "
11184 "constant integer expression");
11185 else if (allow_string)
11186 error_at (loc, "property must be constant "
11187 "integer expression or string "
11188 "literal");
11189 else
11190 error_at (loc, "property must be constant "
11191 "integer expression");
11192 return NULL_TREE;
11193 }
11194 else if (TREE_PURPOSE (t3) == score
11195 && tree_int_cst_sgn (v) < 0)
11196 {
11197 location_t loc
11198 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11199 match_loc);
11200 error_at (loc, "score argument must be "
11201 "non-negative");
11202 return NULL_TREE;
11203 }
11204 TREE_VALUE (t3) = v;
11205 }
11206 }
11207 }
11208 }
11209 val = tree_cons (varid, ctx, chain);
11210 }
11211 /* If the first attribute argument is an identifier, don't
11212 pass it through tsubst. Attributes like mode, format,
11213 cleanup and several target specific attributes expect it
11214 unmodified. */
11215 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11216 {
11217 tree chain
11218 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11219 /*integral_constant_expression_p=*/false);
11220 if (chain != TREE_CHAIN (val))
11221 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11222 }
11223 else if (PACK_EXPANSION_P (val))
11224 {
11225 /* An attribute pack expansion. */
11226 tree purp = TREE_PURPOSE (t);
11227 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11228 if (pack == error_mark_node)
11229 return error_mark_node;
11230 int len = TREE_VEC_LENGTH (pack);
11231 tree list = NULL_TREE;
11232 tree *q = &list;
11233 for (int i = 0; i < len; ++i)
11234 {
11235 tree elt = TREE_VEC_ELT (pack, i);
11236 *q = build_tree_list (purp, elt);
11237 q = &TREE_CHAIN (*q);
11238 }
11239 return list;
11240 }
11241 else
11242 val = tsubst_expr (val, args, complain, in_decl,
11243 /*integral_constant_expression_p=*/false);
11244
11245 if (val != TREE_VALUE (t))
11246 return build_tree_list (TREE_PURPOSE (t), val);
11247 return t;
11248 }
11249
11250 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11251 unchanged or a new TREE_LIST chain. */
11252
11253 static tree
11254 tsubst_attributes (tree attributes, tree args,
11255 tsubst_flags_t complain, tree in_decl)
11256 {
11257 tree last_dep = NULL_TREE;
11258
11259 for (tree t = attributes; t; t = TREE_CHAIN (t))
11260 if (ATTR_IS_DEPENDENT (t))
11261 {
11262 last_dep = t;
11263 attributes = copy_list (attributes);
11264 break;
11265 }
11266
11267 if (last_dep)
11268 for (tree *p = &attributes; *p; )
11269 {
11270 tree t = *p;
11271 if (ATTR_IS_DEPENDENT (t))
11272 {
11273 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11274 if (subst != t)
11275 {
11276 *p = subst;
11277 while (*p)
11278 p = &TREE_CHAIN (*p);
11279 *p = TREE_CHAIN (t);
11280 continue;
11281 }
11282 }
11283 p = &TREE_CHAIN (*p);
11284 }
11285
11286 return attributes;
11287 }
11288
11289 /* Apply any attributes which had to be deferred until instantiation
11290 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11291 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11292
11293 static void
11294 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11295 tree args, tsubst_flags_t complain, tree in_decl)
11296 {
11297 tree last_dep = NULL_TREE;
11298 tree t;
11299 tree *p;
11300
11301 if (attributes == NULL_TREE)
11302 return;
11303
11304 if (DECL_P (*decl_p))
11305 {
11306 if (TREE_TYPE (*decl_p) == error_mark_node)
11307 return;
11308 p = &DECL_ATTRIBUTES (*decl_p);
11309 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11310 to our attributes parameter. */
11311 gcc_assert (*p == attributes);
11312 }
11313 else
11314 {
11315 p = &TYPE_ATTRIBUTES (*decl_p);
11316 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11317 lookup_template_class_1, and should be preserved. */
11318 gcc_assert (*p != attributes);
11319 while (*p)
11320 p = &TREE_CHAIN (*p);
11321 }
11322
11323 for (t = attributes; t; t = TREE_CHAIN (t))
11324 if (ATTR_IS_DEPENDENT (t))
11325 {
11326 last_dep = t;
11327 attributes = copy_list (attributes);
11328 break;
11329 }
11330
11331 *p = attributes;
11332 if (last_dep)
11333 {
11334 tree late_attrs = NULL_TREE;
11335 tree *q = &late_attrs;
11336
11337 for (; *p; )
11338 {
11339 t = *p;
11340 if (ATTR_IS_DEPENDENT (t))
11341 {
11342 *p = TREE_CHAIN (t);
11343 TREE_CHAIN (t) = NULL_TREE;
11344 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11345 while (*q)
11346 q = &TREE_CHAIN (*q);
11347 }
11348 else
11349 p = &TREE_CHAIN (t);
11350 }
11351
11352 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11353 }
11354 }
11355
11356 /* Perform (or defer) access check for typedefs that were referenced
11357 from within the template TMPL code.
11358 This is a subroutine of instantiate_decl and instantiate_class_template.
11359 TMPL is the template to consider and TARGS is the list of arguments of
11360 that template. */
11361
11362 static void
11363 perform_typedefs_access_check (tree tmpl, tree targs)
11364 {
11365 unsigned i;
11366 qualified_typedef_usage_t *iter;
11367
11368 if (!tmpl
11369 || (!CLASS_TYPE_P (tmpl)
11370 && TREE_CODE (tmpl) != FUNCTION_DECL))
11371 return;
11372
11373 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11374 {
11375 tree type_decl = iter->typedef_decl;
11376 tree type_scope = iter->context;
11377
11378 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11379 continue;
11380
11381 if (uses_template_parms (type_decl))
11382 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11383 if (uses_template_parms (type_scope))
11384 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11385
11386 /* Make access check error messages point to the location
11387 of the use of the typedef. */
11388 iloc_sentinel ils (iter->locus);
11389 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11390 type_decl, type_decl,
11391 tf_warning_or_error);
11392 }
11393 }
11394
11395 static tree
11396 instantiate_class_template_1 (tree type)
11397 {
11398 tree templ, args, pattern, t, member;
11399 tree typedecl;
11400 tree pbinfo;
11401 tree base_list;
11402 unsigned int saved_maximum_field_alignment;
11403 tree fn_context;
11404
11405 if (type == error_mark_node)
11406 return error_mark_node;
11407
11408 if (COMPLETE_OR_OPEN_TYPE_P (type)
11409 || uses_template_parms (type))
11410 return type;
11411
11412 /* Figure out which template is being instantiated. */
11413 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11414 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11415
11416 /* Mark the type as in the process of being defined. */
11417 TYPE_BEING_DEFINED (type) = 1;
11418
11419 /* We may be in the middle of deferred access check. Disable
11420 it now. */
11421 deferring_access_check_sentinel acs (dk_no_deferred);
11422
11423 /* Determine what specialization of the original template to
11424 instantiate. */
11425 t = most_specialized_partial_spec (type, tf_warning_or_error);
11426 if (t == error_mark_node)
11427 return error_mark_node;
11428 else if (t)
11429 {
11430 /* This TYPE is actually an instantiation of a partial
11431 specialization. We replace the innermost set of ARGS with
11432 the arguments appropriate for substitution. For example,
11433 given:
11434
11435 template <class T> struct S {};
11436 template <class T> struct S<T*> {};
11437
11438 and supposing that we are instantiating S<int*>, ARGS will
11439 presently be {int*} -- but we need {int}. */
11440 pattern = TREE_TYPE (t);
11441 args = TREE_PURPOSE (t);
11442 }
11443 else
11444 {
11445 pattern = TREE_TYPE (templ);
11446 args = CLASSTYPE_TI_ARGS (type);
11447 }
11448
11449 /* If the template we're instantiating is incomplete, then clearly
11450 there's nothing we can do. */
11451 if (!COMPLETE_TYPE_P (pattern))
11452 {
11453 /* We can try again later. */
11454 TYPE_BEING_DEFINED (type) = 0;
11455 return type;
11456 }
11457
11458 /* If we've recursively instantiated too many templates, stop. */
11459 if (! push_tinst_level (type))
11460 return type;
11461
11462 int saved_unevaluated_operand = cp_unevaluated_operand;
11463 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11464
11465 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11466 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11467 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11468 fn_context = error_mark_node;
11469 if (!fn_context)
11470 push_to_top_level ();
11471 else
11472 {
11473 cp_unevaluated_operand = 0;
11474 c_inhibit_evaluation_warnings = 0;
11475 }
11476 /* Use #pragma pack from the template context. */
11477 saved_maximum_field_alignment = maximum_field_alignment;
11478 maximum_field_alignment = TYPE_PRECISION (pattern);
11479
11480 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11481
11482 /* Set the input location to the most specialized template definition.
11483 This is needed if tsubsting causes an error. */
11484 typedecl = TYPE_MAIN_DECL (pattern);
11485 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11486 DECL_SOURCE_LOCATION (typedecl);
11487
11488 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11489 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11490 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11491 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11492 if (ANON_AGGR_TYPE_P (pattern))
11493 SET_ANON_AGGR_TYPE_P (type);
11494 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11495 {
11496 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11497 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11498 /* Adjust visibility for template arguments. */
11499 determine_visibility (TYPE_MAIN_DECL (type));
11500 }
11501 if (CLASS_TYPE_P (type))
11502 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11503
11504 pbinfo = TYPE_BINFO (pattern);
11505
11506 /* We should never instantiate a nested class before its enclosing
11507 class; we need to look up the nested class by name before we can
11508 instantiate it, and that lookup should instantiate the enclosing
11509 class. */
11510 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11511 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11512
11513 base_list = NULL_TREE;
11514 if (BINFO_N_BASE_BINFOS (pbinfo))
11515 {
11516 tree pbase_binfo;
11517 tree pushed_scope;
11518 int i;
11519
11520 /* We must enter the scope containing the type, as that is where
11521 the accessibility of types named in dependent bases are
11522 looked up from. */
11523 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11524
11525 /* Substitute into each of the bases to determine the actual
11526 basetypes. */
11527 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11528 {
11529 tree base;
11530 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11531 tree expanded_bases = NULL_TREE;
11532 int idx, len = 1;
11533
11534 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11535 {
11536 expanded_bases =
11537 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11538 args, tf_error, NULL_TREE);
11539 if (expanded_bases == error_mark_node)
11540 continue;
11541
11542 len = TREE_VEC_LENGTH (expanded_bases);
11543 }
11544
11545 for (idx = 0; idx < len; idx++)
11546 {
11547 if (expanded_bases)
11548 /* Extract the already-expanded base class. */
11549 base = TREE_VEC_ELT (expanded_bases, idx);
11550 else
11551 /* Substitute to figure out the base class. */
11552 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11553 NULL_TREE);
11554
11555 if (base == error_mark_node)
11556 continue;
11557
11558 base_list = tree_cons (access, base, base_list);
11559 if (BINFO_VIRTUAL_P (pbase_binfo))
11560 TREE_TYPE (base_list) = integer_type_node;
11561 }
11562 }
11563
11564 /* The list is now in reverse order; correct that. */
11565 base_list = nreverse (base_list);
11566
11567 if (pushed_scope)
11568 pop_scope (pushed_scope);
11569 }
11570 /* Now call xref_basetypes to set up all the base-class
11571 information. */
11572 xref_basetypes (type, base_list);
11573
11574 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11575 (int) ATTR_FLAG_TYPE_IN_PLACE,
11576 args, tf_error, NULL_TREE);
11577 fixup_attribute_variants (type);
11578
11579 /* Now that our base classes are set up, enter the scope of the
11580 class, so that name lookups into base classes, etc. will work
11581 correctly. This is precisely analogous to what we do in
11582 begin_class_definition when defining an ordinary non-template
11583 class, except we also need to push the enclosing classes. */
11584 push_nested_class (type);
11585
11586 /* Now members are processed in the order of declaration. */
11587 for (member = CLASSTYPE_DECL_LIST (pattern);
11588 member; member = TREE_CHAIN (member))
11589 {
11590 tree t = TREE_VALUE (member);
11591
11592 if (TREE_PURPOSE (member))
11593 {
11594 if (TYPE_P (t))
11595 {
11596 if (LAMBDA_TYPE_P (t))
11597 /* A closure type for a lambda in an NSDMI or default argument.
11598 Ignore it; it will be regenerated when needed. */
11599 continue;
11600
11601 /* Build new CLASSTYPE_NESTED_UTDS. */
11602
11603 tree newtag;
11604 bool class_template_p;
11605
11606 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11607 && TYPE_LANG_SPECIFIC (t)
11608 && CLASSTYPE_IS_TEMPLATE (t));
11609 /* If the member is a class template, then -- even after
11610 substitution -- there may be dependent types in the
11611 template argument list for the class. We increment
11612 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11613 that function will assume that no types are dependent
11614 when outside of a template. */
11615 if (class_template_p)
11616 ++processing_template_decl;
11617 newtag = tsubst (t, args, tf_error, NULL_TREE);
11618 if (class_template_p)
11619 --processing_template_decl;
11620 if (newtag == error_mark_node)
11621 continue;
11622
11623 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11624 {
11625 tree name = TYPE_IDENTIFIER (t);
11626
11627 if (class_template_p)
11628 /* Unfortunately, lookup_template_class sets
11629 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11630 instantiation (i.e., for the type of a member
11631 template class nested within a template class.)
11632 This behavior is required for
11633 maybe_process_partial_specialization to work
11634 correctly, but is not accurate in this case;
11635 the TAG is not an instantiation of anything.
11636 (The corresponding TEMPLATE_DECL is an
11637 instantiation, but the TYPE is not.) */
11638 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11639
11640 /* Now, we call pushtag to put this NEWTAG into the scope of
11641 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11642 pushtag calling push_template_decl. We don't have to do
11643 this for enums because it will already have been done in
11644 tsubst_enum. */
11645 if (name)
11646 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11647 pushtag (name, newtag, /*tag_scope=*/ts_current);
11648 }
11649 }
11650 else if (DECL_DECLARES_FUNCTION_P (t))
11651 {
11652 tree r;
11653
11654 if (TREE_CODE (t) == TEMPLATE_DECL)
11655 ++processing_template_decl;
11656 r = tsubst (t, args, tf_error, NULL_TREE);
11657 if (TREE_CODE (t) == TEMPLATE_DECL)
11658 --processing_template_decl;
11659 set_current_access_from_decl (r);
11660 finish_member_declaration (r);
11661 /* Instantiate members marked with attribute used. */
11662 if (r != error_mark_node && DECL_PRESERVE_P (r))
11663 mark_used (r);
11664 if (TREE_CODE (r) == FUNCTION_DECL
11665 && DECL_OMP_DECLARE_REDUCTION_P (r))
11666 cp_check_omp_declare_reduction (r);
11667 }
11668 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11669 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11670 /* A closure type for a lambda in an NSDMI or default argument.
11671 Ignore it; it will be regenerated when needed. */;
11672 else
11673 {
11674 /* Build new TYPE_FIELDS. */
11675 if (TREE_CODE (t) == STATIC_ASSERT)
11676 {
11677 tree condition;
11678
11679 ++c_inhibit_evaluation_warnings;
11680 condition =
11681 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11682 tf_warning_or_error, NULL_TREE,
11683 /*integral_constant_expression_p=*/true);
11684 --c_inhibit_evaluation_warnings;
11685
11686 finish_static_assert (condition,
11687 STATIC_ASSERT_MESSAGE (t),
11688 STATIC_ASSERT_SOURCE_LOCATION (t),
11689 /*member_p=*/true);
11690 }
11691 else if (TREE_CODE (t) != CONST_DECL)
11692 {
11693 tree r;
11694 tree vec = NULL_TREE;
11695 int len = 1;
11696
11697 /* The file and line for this declaration, to
11698 assist in error message reporting. Since we
11699 called push_tinst_level above, we don't need to
11700 restore these. */
11701 input_location = DECL_SOURCE_LOCATION (t);
11702
11703 if (TREE_CODE (t) == TEMPLATE_DECL)
11704 ++processing_template_decl;
11705 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11706 if (TREE_CODE (t) == TEMPLATE_DECL)
11707 --processing_template_decl;
11708
11709 if (TREE_CODE (r) == TREE_VEC)
11710 {
11711 /* A capture pack became multiple fields. */
11712 vec = r;
11713 len = TREE_VEC_LENGTH (vec);
11714 }
11715
11716 for (int i = 0; i < len; ++i)
11717 {
11718 if (vec)
11719 r = TREE_VEC_ELT (vec, i);
11720 if (VAR_P (r))
11721 {
11722 /* In [temp.inst]:
11723
11724 [t]he initialization (and any associated
11725 side-effects) of a static data member does
11726 not occur unless the static data member is
11727 itself used in a way that requires the
11728 definition of the static data member to
11729 exist.
11730
11731 Therefore, we do not substitute into the
11732 initialized for the static data member here. */
11733 finish_static_data_member_decl
11734 (r,
11735 /*init=*/NULL_TREE,
11736 /*init_const_expr_p=*/false,
11737 /*asmspec_tree=*/NULL_TREE,
11738 /*flags=*/0);
11739 /* Instantiate members marked with attribute used. */
11740 if (r != error_mark_node && DECL_PRESERVE_P (r))
11741 mark_used (r);
11742 }
11743 else if (TREE_CODE (r) == FIELD_DECL)
11744 {
11745 /* Determine whether R has a valid type and can be
11746 completed later. If R is invalid, then its type
11747 is replaced by error_mark_node. */
11748 tree rtype = TREE_TYPE (r);
11749 if (can_complete_type_without_circularity (rtype))
11750 complete_type (rtype);
11751
11752 if (!complete_or_array_type_p (rtype))
11753 {
11754 /* If R's type couldn't be completed and
11755 it isn't a flexible array member (whose
11756 type is incomplete by definition) give
11757 an error. */
11758 cxx_incomplete_type_error (r, rtype);
11759 TREE_TYPE (r) = error_mark_node;
11760 }
11761 else if (TREE_CODE (rtype) == ARRAY_TYPE
11762 && TYPE_DOMAIN (rtype) == NULL_TREE
11763 && (TREE_CODE (type) == UNION_TYPE
11764 || TREE_CODE (type) == QUAL_UNION_TYPE))
11765 {
11766 error ("flexible array member %qD in union", r);
11767 TREE_TYPE (r) = error_mark_node;
11768 }
11769 }
11770
11771 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11772 such a thing will already have been added to the field
11773 list by tsubst_enum in finish_member_declaration in the
11774 CLASSTYPE_NESTED_UTDS case above. */
11775 if (!(TREE_CODE (r) == TYPE_DECL
11776 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11777 && DECL_ARTIFICIAL (r)))
11778 {
11779 set_current_access_from_decl (r);
11780 finish_member_declaration (r);
11781 }
11782 }
11783 }
11784 }
11785 }
11786 else
11787 {
11788 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11789 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11790 {
11791 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11792
11793 tree friend_type = t;
11794 bool adjust_processing_template_decl = false;
11795
11796 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11797 {
11798 /* template <class T> friend class C; */
11799 friend_type = tsubst_friend_class (friend_type, args);
11800 adjust_processing_template_decl = true;
11801 }
11802 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11803 {
11804 /* template <class T> friend class C::D; */
11805 friend_type = tsubst (friend_type, args,
11806 tf_warning_or_error, NULL_TREE);
11807 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11808 friend_type = TREE_TYPE (friend_type);
11809 adjust_processing_template_decl = true;
11810 }
11811 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11812 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11813 {
11814 /* This could be either
11815
11816 friend class T::C;
11817
11818 when dependent_type_p is false or
11819
11820 template <class U> friend class T::C;
11821
11822 otherwise. */
11823 /* Bump processing_template_decl in case this is something like
11824 template <class T> friend struct A<T>::B. */
11825 ++processing_template_decl;
11826 friend_type = tsubst (friend_type, args,
11827 tf_warning_or_error, NULL_TREE);
11828 if (dependent_type_p (friend_type))
11829 adjust_processing_template_decl = true;
11830 --processing_template_decl;
11831 }
11832 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11833 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11834 && TYPE_HIDDEN_P (friend_type))
11835 {
11836 /* friend class C;
11837
11838 where C hasn't been declared yet. Let's lookup name
11839 from namespace scope directly, bypassing any name that
11840 come from dependent base class. */
11841 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11842
11843 /* The call to xref_tag_from_type does injection for friend
11844 classes. */
11845 push_nested_namespace (ns);
11846 friend_type =
11847 xref_tag_from_type (friend_type, NULL_TREE,
11848 /*tag_scope=*/ts_current);
11849 pop_nested_namespace (ns);
11850 }
11851 else if (uses_template_parms (friend_type))
11852 /* friend class C<T>; */
11853 friend_type = tsubst (friend_type, args,
11854 tf_warning_or_error, NULL_TREE);
11855 /* Otherwise it's
11856
11857 friend class C;
11858
11859 where C is already declared or
11860
11861 friend class C<int>;
11862
11863 We don't have to do anything in these cases. */
11864
11865 if (adjust_processing_template_decl)
11866 /* Trick make_friend_class into realizing that the friend
11867 we're adding is a template, not an ordinary class. It's
11868 important that we use make_friend_class since it will
11869 perform some error-checking and output cross-reference
11870 information. */
11871 ++processing_template_decl;
11872
11873 if (friend_type != error_mark_node)
11874 make_friend_class (type, friend_type, /*complain=*/false);
11875
11876 if (adjust_processing_template_decl)
11877 --processing_template_decl;
11878 }
11879 else
11880 {
11881 /* Build new DECL_FRIENDLIST. */
11882 tree r;
11883
11884 /* The file and line for this declaration, to
11885 assist in error message reporting. Since we
11886 called push_tinst_level above, we don't need to
11887 restore these. */
11888 input_location = DECL_SOURCE_LOCATION (t);
11889
11890 if (TREE_CODE (t) == TEMPLATE_DECL)
11891 {
11892 ++processing_template_decl;
11893 push_deferring_access_checks (dk_no_check);
11894 }
11895
11896 r = tsubst_friend_function (t, args);
11897 add_friend (type, r, /*complain=*/false);
11898 if (TREE_CODE (t) == TEMPLATE_DECL)
11899 {
11900 pop_deferring_access_checks ();
11901 --processing_template_decl;
11902 }
11903 }
11904 }
11905 }
11906
11907 if (fn_context)
11908 {
11909 /* Restore these before substituting into the lambda capture
11910 initializers. */
11911 cp_unevaluated_operand = saved_unevaluated_operand;
11912 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11913 }
11914
11915 /* Set the file and line number information to whatever is given for
11916 the class itself. This puts error messages involving generated
11917 implicit functions at a predictable point, and the same point
11918 that would be used for non-template classes. */
11919 input_location = DECL_SOURCE_LOCATION (typedecl);
11920
11921 unreverse_member_declarations (type);
11922 finish_struct_1 (type);
11923 TYPE_BEING_DEFINED (type) = 0;
11924
11925 /* We don't instantiate default arguments for member functions. 14.7.1:
11926
11927 The implicit instantiation of a class template specialization causes
11928 the implicit instantiation of the declarations, but not of the
11929 definitions or default arguments, of the class member functions,
11930 member classes, static data members and member templates.... */
11931
11932 /* Some typedefs referenced from within the template code need to be access
11933 checked at template instantiation time, i.e now. These types were
11934 added to the template at parsing time. Let's get those and perform
11935 the access checks then. */
11936 perform_typedefs_access_check (pattern, args);
11937 perform_deferred_access_checks (tf_warning_or_error);
11938 pop_nested_class ();
11939 maximum_field_alignment = saved_maximum_field_alignment;
11940 if (!fn_context)
11941 pop_from_top_level ();
11942 pop_tinst_level ();
11943
11944 /* The vtable for a template class can be emitted in any translation
11945 unit in which the class is instantiated. When there is no key
11946 method, however, finish_struct_1 will already have added TYPE to
11947 the keyed_classes. */
11948 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11949 vec_safe_push (keyed_classes, type);
11950
11951 return type;
11952 }
11953
11954 /* Wrapper for instantiate_class_template_1. */
11955
11956 tree
11957 instantiate_class_template (tree type)
11958 {
11959 tree ret;
11960 timevar_push (TV_TEMPLATE_INST);
11961 ret = instantiate_class_template_1 (type);
11962 timevar_pop (TV_TEMPLATE_INST);
11963 return ret;
11964 }
11965
11966 tree
11967 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11968 {
11969 tree r;
11970
11971 if (!t)
11972 r = t;
11973 else if (TYPE_P (t))
11974 r = tsubst (t, args, complain, in_decl);
11975 else
11976 {
11977 if (!(complain & tf_warning))
11978 ++c_inhibit_evaluation_warnings;
11979 r = tsubst_expr (t, args, complain, in_decl,
11980 /*integral_constant_expression_p=*/true);
11981 if (!(complain & tf_warning))
11982 --c_inhibit_evaluation_warnings;
11983 }
11984
11985 return r;
11986 }
11987
11988 /* Given a function parameter pack TMPL_PARM and some function parameters
11989 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11990 and set *SPEC_P to point at the next point in the list. */
11991
11992 tree
11993 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11994 {
11995 /* Collect all of the extra "packed" parameters into an
11996 argument pack. */
11997 tree parmvec;
11998 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11999 tree spec_parm = *spec_p;
12000 int i, len;
12001
12002 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12003 if (tmpl_parm
12004 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12005 break;
12006
12007 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12008 parmvec = make_tree_vec (len);
12009 spec_parm = *spec_p;
12010 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12011 {
12012 tree elt = spec_parm;
12013 if (DECL_PACK_P (elt))
12014 elt = make_pack_expansion (elt);
12015 TREE_VEC_ELT (parmvec, i) = elt;
12016 }
12017
12018 /* Build the argument packs. */
12019 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12020 *spec_p = spec_parm;
12021
12022 return argpack;
12023 }
12024
12025 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12026 NONTYPE_ARGUMENT_PACK. */
12027
12028 static tree
12029 make_fnparm_pack (tree spec_parm)
12030 {
12031 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12032 }
12033
12034 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12035 pack expansion with no extra args, 2 if it has extra args, or 0
12036 if it is not a pack expansion. */
12037
12038 static int
12039 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12040 {
12041 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12042 /* We're being called before this happens in tsubst_pack_expansion. */
12043 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12044 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12045 if (i >= TREE_VEC_LENGTH (vec))
12046 return 0;
12047 tree elt = TREE_VEC_ELT (vec, i);
12048 if (DECL_P (elt))
12049 /* A decl pack is itself an expansion. */
12050 elt = TREE_TYPE (elt);
12051 if (!PACK_EXPANSION_P (elt))
12052 return 0;
12053 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12054 return 2;
12055 return 1;
12056 }
12057
12058
12059 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12060
12061 static tree
12062 make_argument_pack_select (tree arg_pack, unsigned index)
12063 {
12064 tree aps = make_node (ARGUMENT_PACK_SELECT);
12065
12066 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12067 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12068
12069 return aps;
12070 }
12071
12072 /* This is a subroutine of tsubst_pack_expansion.
12073
12074 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12075 mechanism to store the (non complete list of) arguments of the
12076 substitution and return a non substituted pack expansion, in order
12077 to wait for when we have enough arguments to really perform the
12078 substitution. */
12079
12080 static bool
12081 use_pack_expansion_extra_args_p (tree parm_packs,
12082 int arg_pack_len,
12083 bool has_empty_arg)
12084 {
12085 /* If one pack has an expansion and another pack has a normal
12086 argument or if one pack has an empty argument and an another
12087 one hasn't then tsubst_pack_expansion cannot perform the
12088 substitution and need to fall back on the
12089 PACK_EXPANSION_EXTRA mechanism. */
12090 if (parm_packs == NULL_TREE)
12091 return false;
12092 else if (has_empty_arg)
12093 {
12094 /* If all the actual packs are pack expansions, we can still
12095 subsitute directly. */
12096 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12097 {
12098 tree a = TREE_VALUE (p);
12099 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12100 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12101 a = ARGUMENT_PACK_ARGS (a);
12102 if (TREE_VEC_LENGTH (a) == 1)
12103 a = TREE_VEC_ELT (a, 0);
12104 if (PACK_EXPANSION_P (a))
12105 continue;
12106 return true;
12107 }
12108 return false;
12109 }
12110
12111 bool has_expansion_arg = false;
12112 for (int i = 0 ; i < arg_pack_len; ++i)
12113 {
12114 bool has_non_expansion_arg = false;
12115 for (tree parm_pack = parm_packs;
12116 parm_pack;
12117 parm_pack = TREE_CHAIN (parm_pack))
12118 {
12119 tree arg = TREE_VALUE (parm_pack);
12120
12121 int exp = argument_pack_element_is_expansion_p (arg, i);
12122 if (exp == 2)
12123 /* We can't substitute a pack expansion with extra args into
12124 our pattern. */
12125 return true;
12126 else if (exp)
12127 has_expansion_arg = true;
12128 else
12129 has_non_expansion_arg = true;
12130 }
12131
12132 if (has_expansion_arg && has_non_expansion_arg)
12133 return true;
12134 }
12135 return false;
12136 }
12137
12138 /* [temp.variadic]/6 says that:
12139
12140 The instantiation of a pack expansion [...]
12141 produces a list E1,E2, ..., En, where N is the number of elements
12142 in the pack expansion parameters.
12143
12144 This subroutine of tsubst_pack_expansion produces one of these Ei.
12145
12146 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12147 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12148 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12149 INDEX is the index 'i' of the element Ei to produce. ARGS,
12150 COMPLAIN, and IN_DECL are the same parameters as for the
12151 tsubst_pack_expansion function.
12152
12153 The function returns the resulting Ei upon successful completion,
12154 or error_mark_node.
12155
12156 Note that this function possibly modifies the ARGS parameter, so
12157 it's the responsibility of the caller to restore it. */
12158
12159 static tree
12160 gen_elem_of_pack_expansion_instantiation (tree pattern,
12161 tree parm_packs,
12162 unsigned index,
12163 tree args /* This parm gets
12164 modified. */,
12165 tsubst_flags_t complain,
12166 tree in_decl)
12167 {
12168 tree t;
12169 bool ith_elem_is_expansion = false;
12170
12171 /* For each parameter pack, change the substitution of the parameter
12172 pack to the ith argument in its argument pack, then expand the
12173 pattern. */
12174 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12175 {
12176 tree parm = TREE_PURPOSE (pack);
12177 tree arg_pack = TREE_VALUE (pack);
12178 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12179
12180 ith_elem_is_expansion |=
12181 argument_pack_element_is_expansion_p (arg_pack, index);
12182
12183 /* Select the Ith argument from the pack. */
12184 if (TREE_CODE (parm) == PARM_DECL
12185 || VAR_P (parm)
12186 || TREE_CODE (parm) == FIELD_DECL)
12187 {
12188 if (index == 0)
12189 {
12190 aps = make_argument_pack_select (arg_pack, index);
12191 if (!mark_used (parm, complain) && !(complain & tf_error))
12192 return error_mark_node;
12193 register_local_specialization (aps, parm);
12194 }
12195 else
12196 aps = retrieve_local_specialization (parm);
12197 }
12198 else
12199 {
12200 int idx, level;
12201 template_parm_level_and_index (parm, &level, &idx);
12202
12203 if (index == 0)
12204 {
12205 aps = make_argument_pack_select (arg_pack, index);
12206 /* Update the corresponding argument. */
12207 TMPL_ARG (args, level, idx) = aps;
12208 }
12209 else
12210 /* Re-use the ARGUMENT_PACK_SELECT. */
12211 aps = TMPL_ARG (args, level, idx);
12212 }
12213 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12214 }
12215
12216 /* Substitute into the PATTERN with the (possibly altered)
12217 arguments. */
12218 if (pattern == in_decl)
12219 /* Expanding a fixed parameter pack from
12220 coerce_template_parameter_pack. */
12221 t = tsubst_decl (pattern, args, complain);
12222 else if (pattern == error_mark_node)
12223 t = error_mark_node;
12224 else if (!TYPE_P (pattern))
12225 t = tsubst_expr (pattern, args, complain, in_decl,
12226 /*integral_constant_expression_p=*/false);
12227 else
12228 t = tsubst (pattern, args, complain, in_decl);
12229
12230 /* If the Ith argument pack element is a pack expansion, then
12231 the Ith element resulting from the substituting is going to
12232 be a pack expansion as well. */
12233 if (ith_elem_is_expansion)
12234 t = make_pack_expansion (t, complain);
12235
12236 return t;
12237 }
12238
12239 /* When the unexpanded parameter pack in a fold expression expands to an empty
12240 sequence, the value of the expression is as follows; the program is
12241 ill-formed if the operator is not listed in this table.
12242
12243 && true
12244 || false
12245 , void() */
12246
12247 tree
12248 expand_empty_fold (tree t, tsubst_flags_t complain)
12249 {
12250 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12251 if (!FOLD_EXPR_MODIFY_P (t))
12252 switch (code)
12253 {
12254 case TRUTH_ANDIF_EXPR:
12255 return boolean_true_node;
12256 case TRUTH_ORIF_EXPR:
12257 return boolean_false_node;
12258 case COMPOUND_EXPR:
12259 return void_node;
12260 default:
12261 break;
12262 }
12263
12264 if (complain & tf_error)
12265 error_at (location_of (t),
12266 "fold of empty expansion over %O", code);
12267 return error_mark_node;
12268 }
12269
12270 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12271 form an expression that combines the two terms using the
12272 operator of T. */
12273
12274 static tree
12275 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12276 {
12277 tree op = FOLD_EXPR_OP (t);
12278 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12279
12280 // Handle compound assignment operators.
12281 if (FOLD_EXPR_MODIFY_P (t))
12282 return build_x_modify_expr (input_location, left, code, right, complain);
12283
12284 switch (code)
12285 {
12286 case COMPOUND_EXPR:
12287 return build_x_compound_expr (input_location, left, right, complain);
12288 default:
12289 return build_x_binary_op (input_location, code,
12290 left, TREE_CODE (left),
12291 right, TREE_CODE (right),
12292 /*overload=*/NULL,
12293 complain);
12294 }
12295 }
12296
12297 /* Substitute ARGS into the pack of a fold expression T. */
12298
12299 static inline tree
12300 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12301 {
12302 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12303 }
12304
12305 /* Substitute ARGS into the pack of a fold expression T. */
12306
12307 static inline tree
12308 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12309 {
12310 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12311 }
12312
12313 /* Expand a PACK of arguments into a grouped as left fold.
12314 Given a pack containing elements A0, A1, ..., An and an
12315 operator @, this builds the expression:
12316
12317 ((A0 @ A1) @ A2) ... @ An
12318
12319 Note that PACK must not be empty.
12320
12321 The operator is defined by the original fold expression T. */
12322
12323 static tree
12324 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12325 {
12326 tree left = TREE_VEC_ELT (pack, 0);
12327 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12328 {
12329 tree right = TREE_VEC_ELT (pack, i);
12330 left = fold_expression (t, left, right, complain);
12331 }
12332 return left;
12333 }
12334
12335 /* Substitute into a unary left fold expression. */
12336
12337 static tree
12338 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12339 tree in_decl)
12340 {
12341 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12342 if (pack == error_mark_node)
12343 return error_mark_node;
12344 if (PACK_EXPANSION_P (pack))
12345 {
12346 tree r = copy_node (t);
12347 FOLD_EXPR_PACK (r) = pack;
12348 return r;
12349 }
12350 if (TREE_VEC_LENGTH (pack) == 0)
12351 return expand_empty_fold (t, complain);
12352 else
12353 return expand_left_fold (t, pack, complain);
12354 }
12355
12356 /* Substitute into a binary left fold expression.
12357
12358 Do ths by building a single (non-empty) vector of argumnts and
12359 building the expression from those elements. */
12360
12361 static tree
12362 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12363 tree in_decl)
12364 {
12365 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12366 if (pack == error_mark_node)
12367 return error_mark_node;
12368 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12369 if (init == error_mark_node)
12370 return error_mark_node;
12371
12372 if (PACK_EXPANSION_P (pack))
12373 {
12374 tree r = copy_node (t);
12375 FOLD_EXPR_PACK (r) = pack;
12376 FOLD_EXPR_INIT (r) = init;
12377 return r;
12378 }
12379
12380 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12381 TREE_VEC_ELT (vec, 0) = init;
12382 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12383 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12384
12385 return expand_left_fold (t, vec, complain);
12386 }
12387
12388 /* Expand a PACK of arguments into a grouped as right fold.
12389 Given a pack containing elementns A0, A1, ..., and an
12390 operator @, this builds the expression:
12391
12392 A0@ ... (An-2 @ (An-1 @ An))
12393
12394 Note that PACK must not be empty.
12395
12396 The operator is defined by the original fold expression T. */
12397
12398 tree
12399 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12400 {
12401 // Build the expression.
12402 int n = TREE_VEC_LENGTH (pack);
12403 tree right = TREE_VEC_ELT (pack, n - 1);
12404 for (--n; n != 0; --n)
12405 {
12406 tree left = TREE_VEC_ELT (pack, n - 1);
12407 right = fold_expression (t, left, right, complain);
12408 }
12409 return right;
12410 }
12411
12412 /* Substitute into a unary right fold expression. */
12413
12414 static tree
12415 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12416 tree in_decl)
12417 {
12418 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12419 if (pack == error_mark_node)
12420 return error_mark_node;
12421 if (PACK_EXPANSION_P (pack))
12422 {
12423 tree r = copy_node (t);
12424 FOLD_EXPR_PACK (r) = pack;
12425 return r;
12426 }
12427 if (TREE_VEC_LENGTH (pack) == 0)
12428 return expand_empty_fold (t, complain);
12429 else
12430 return expand_right_fold (t, pack, complain);
12431 }
12432
12433 /* Substitute into a binary right fold expression.
12434
12435 Do ths by building a single (non-empty) vector of arguments and
12436 building the expression from those elements. */
12437
12438 static tree
12439 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12440 tree in_decl)
12441 {
12442 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12443 if (pack == error_mark_node)
12444 return error_mark_node;
12445 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12446 if (init == error_mark_node)
12447 return error_mark_node;
12448
12449 if (PACK_EXPANSION_P (pack))
12450 {
12451 tree r = copy_node (t);
12452 FOLD_EXPR_PACK (r) = pack;
12453 FOLD_EXPR_INIT (r) = init;
12454 return r;
12455 }
12456
12457 int n = TREE_VEC_LENGTH (pack);
12458 tree vec = make_tree_vec (n + 1);
12459 for (int i = 0; i < n; ++i)
12460 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12461 TREE_VEC_ELT (vec, n) = init;
12462
12463 return expand_right_fold (t, vec, complain);
12464 }
12465
12466 /* Walk through the pattern of a pack expansion, adding everything in
12467 local_specializations to a list. */
12468
12469 class el_data
12470 {
12471 public:
12472 hash_set<tree> internal;
12473 tree extra;
12474 tsubst_flags_t complain;
12475
12476 el_data (tsubst_flags_t c)
12477 : extra (NULL_TREE), complain (c) {}
12478 };
12479 static tree
12480 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12481 {
12482 el_data &data = *reinterpret_cast<el_data*>(data_);
12483 tree *extra = &data.extra;
12484 tsubst_flags_t complain = data.complain;
12485
12486 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12487 /* Remember local typedefs (85214). */
12488 tp = &TYPE_NAME (*tp);
12489
12490 if (TREE_CODE (*tp) == DECL_EXPR)
12491 data.internal.add (DECL_EXPR_DECL (*tp));
12492 else if (tree spec = retrieve_local_specialization (*tp))
12493 {
12494 if (data.internal.contains (*tp))
12495 /* Don't mess with variables declared within the pattern. */
12496 return NULL_TREE;
12497 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12498 {
12499 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12500 tree args = ARGUMENT_PACK_ARGS (spec);
12501 if (TREE_VEC_LENGTH (args) == 1)
12502 {
12503 tree elt = TREE_VEC_ELT (args, 0);
12504 if (PACK_EXPANSION_P (elt))
12505 elt = PACK_EXPANSION_PATTERN (elt);
12506 if (DECL_PACK_P (elt))
12507 spec = elt;
12508 }
12509 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12510 {
12511 /* Handle lambda capture here, since we aren't doing any
12512 substitution now, and so tsubst_copy won't call
12513 process_outer_var_ref. */
12514 tree args = ARGUMENT_PACK_ARGS (spec);
12515 int len = TREE_VEC_LENGTH (args);
12516 for (int i = 0; i < len; ++i)
12517 {
12518 tree arg = TREE_VEC_ELT (args, i);
12519 tree carg = arg;
12520 if (outer_automatic_var_p (arg))
12521 carg = process_outer_var_ref (arg, complain);
12522 if (carg != arg)
12523 {
12524 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12525 proxies. */
12526 if (i == 0)
12527 {
12528 spec = copy_node (spec);
12529 args = copy_node (args);
12530 SET_ARGUMENT_PACK_ARGS (spec, args);
12531 register_local_specialization (spec, *tp);
12532 }
12533 TREE_VEC_ELT (args, i) = carg;
12534 }
12535 }
12536 }
12537 }
12538 if (outer_automatic_var_p (spec))
12539 spec = process_outer_var_ref (spec, complain);
12540 *extra = tree_cons (*tp, spec, *extra);
12541 }
12542 return NULL_TREE;
12543 }
12544 static tree
12545 extract_local_specs (tree pattern, tsubst_flags_t complain)
12546 {
12547 el_data data (complain);
12548 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12549 return data.extra;
12550 }
12551
12552 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12553 for use in PACK_EXPANSION_EXTRA_ARGS. */
12554
12555 tree
12556 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12557 {
12558 tree extra = args;
12559 if (local_specializations)
12560 if (tree locals = extract_local_specs (pattern, complain))
12561 extra = tree_cons (NULL_TREE, extra, locals);
12562 return extra;
12563 }
12564
12565 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12566 normal template args to ARGS. */
12567
12568 tree
12569 add_extra_args (tree extra, tree args)
12570 {
12571 if (extra && TREE_CODE (extra) == TREE_LIST)
12572 {
12573 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12574 {
12575 /* The partial instantiation involved local declarations collected in
12576 extract_local_specs; map from the general template to our local
12577 context. */
12578 tree gen = TREE_PURPOSE (elt);
12579 tree inst = TREE_VALUE (elt);
12580 if (DECL_P (inst))
12581 if (tree local = retrieve_local_specialization (inst))
12582 inst = local;
12583 /* else inst is already a full instantiation of the pack. */
12584 register_local_specialization (inst, gen);
12585 }
12586 gcc_assert (!TREE_PURPOSE (extra));
12587 extra = TREE_VALUE (extra);
12588 }
12589 #if 1
12590 /* I think we should always be able to substitute dependent args into the
12591 pattern. If that turns out to be incorrect in some cases, enable the
12592 alternate code (and add complain/in_decl parms to this function). */
12593 gcc_checking_assert (!uses_template_parms (extra));
12594 #else
12595 if (!uses_template_parms (extra))
12596 {
12597 gcc_unreachable ();
12598 extra = tsubst_template_args (extra, args, complain, in_decl);
12599 args = add_outermost_template_args (args, extra);
12600 }
12601 else
12602 #endif
12603 args = add_to_template_args (extra, args);
12604 return args;
12605 }
12606
12607 /* Substitute ARGS into T, which is an pack expansion
12608 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12609 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12610 (if only a partial substitution could be performed) or
12611 ERROR_MARK_NODE if there was an error. */
12612 tree
12613 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12614 tree in_decl)
12615 {
12616 tree pattern;
12617 tree pack, packs = NULL_TREE;
12618 bool unsubstituted_packs = false;
12619 bool unsubstituted_fn_pack = false;
12620 int i, len = -1;
12621 tree result;
12622 hash_map<tree, tree> *saved_local_specializations = NULL;
12623 bool need_local_specializations = false;
12624 int levels;
12625
12626 gcc_assert (PACK_EXPANSION_P (t));
12627 pattern = PACK_EXPANSION_PATTERN (t);
12628
12629 /* Add in any args remembered from an earlier partial instantiation. */
12630 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12631
12632 levels = TMPL_ARGS_DEPTH (args);
12633
12634 /* Determine the argument packs that will instantiate the parameter
12635 packs used in the expansion expression. While we're at it,
12636 compute the number of arguments to be expanded and make sure it
12637 is consistent. */
12638 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12639 pack = TREE_CHAIN (pack))
12640 {
12641 tree parm_pack = TREE_VALUE (pack);
12642 tree arg_pack = NULL_TREE;
12643 tree orig_arg = NULL_TREE;
12644 int level = 0;
12645
12646 if (TREE_CODE (parm_pack) == BASES)
12647 {
12648 gcc_assert (parm_pack == pattern);
12649 if (BASES_DIRECT (parm_pack))
12650 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12651 args, complain,
12652 in_decl, false),
12653 complain);
12654 else
12655 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12656 args, complain, in_decl,
12657 false), complain);
12658 }
12659 else if (builtin_pack_call_p (parm_pack))
12660 {
12661 if (parm_pack != pattern)
12662 {
12663 if (complain & tf_error)
12664 sorry ("%qE is not the entire pattern of the pack expansion",
12665 parm_pack);
12666 return error_mark_node;
12667 }
12668 return expand_builtin_pack_call (parm_pack, args,
12669 complain, in_decl);
12670 }
12671 else if (TREE_CODE (parm_pack) == PARM_DECL)
12672 {
12673 /* We know we have correct local_specializations if this
12674 expansion is at function scope, or if we're dealing with a
12675 local parameter in a requires expression; for the latter,
12676 tsubst_requires_expr set it up appropriately. */
12677 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12678 arg_pack = retrieve_local_specialization (parm_pack);
12679 else
12680 /* We can't rely on local_specializations for a parameter
12681 name used later in a function declaration (such as in a
12682 late-specified return type). Even if it exists, it might
12683 have the wrong value for a recursive call. */
12684 need_local_specializations = true;
12685
12686 if (!arg_pack)
12687 {
12688 /* This parameter pack was used in an unevaluated context. Just
12689 make a dummy decl, since it's only used for its type. */
12690 ++cp_unevaluated_operand;
12691 arg_pack = tsubst_decl (parm_pack, args, complain);
12692 --cp_unevaluated_operand;
12693 if (arg_pack && DECL_PACK_P (arg_pack))
12694 /* Partial instantiation of the parm_pack, we can't build
12695 up an argument pack yet. */
12696 arg_pack = NULL_TREE;
12697 else
12698 arg_pack = make_fnparm_pack (arg_pack);
12699 }
12700 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12701 /* This argument pack isn't fully instantiated yet. We set this
12702 flag rather than clear arg_pack because we do want to do the
12703 optimization below, and we don't want to substitute directly
12704 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12705 where it isn't expected). */
12706 unsubstituted_fn_pack = true;
12707 }
12708 else if (is_capture_proxy (parm_pack))
12709 {
12710 arg_pack = retrieve_local_specialization (parm_pack);
12711 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12712 unsubstituted_fn_pack = true;
12713 }
12714 else
12715 {
12716 int idx;
12717 template_parm_level_and_index (parm_pack, &level, &idx);
12718 if (level <= levels)
12719 arg_pack = TMPL_ARG (args, level, idx);
12720 }
12721
12722 orig_arg = arg_pack;
12723 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12724 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12725
12726 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12727 /* This can only happen if we forget to expand an argument
12728 pack somewhere else. Just return an error, silently. */
12729 {
12730 result = make_tree_vec (1);
12731 TREE_VEC_ELT (result, 0) = error_mark_node;
12732 return result;
12733 }
12734
12735 if (arg_pack)
12736 {
12737 int my_len =
12738 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12739
12740 /* Don't bother trying to do a partial substitution with
12741 incomplete packs; we'll try again after deduction. */
12742 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12743 return t;
12744
12745 if (len < 0)
12746 len = my_len;
12747 else if (len != my_len
12748 && !unsubstituted_fn_pack)
12749 {
12750 if (!(complain & tf_error))
12751 /* Fail quietly. */;
12752 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12753 error ("mismatched argument pack lengths while expanding %qT",
12754 pattern);
12755 else
12756 error ("mismatched argument pack lengths while expanding %qE",
12757 pattern);
12758 return error_mark_node;
12759 }
12760
12761 /* Keep track of the parameter packs and their corresponding
12762 argument packs. */
12763 packs = tree_cons (parm_pack, arg_pack, packs);
12764 TREE_TYPE (packs) = orig_arg;
12765 }
12766 else
12767 {
12768 /* We can't substitute for this parameter pack. We use a flag as
12769 well as the missing_level counter because function parameter
12770 packs don't have a level. */
12771 if (!(processing_template_decl || is_auto (parm_pack)))
12772 {
12773 gcc_unreachable ();
12774 }
12775 gcc_assert (processing_template_decl || is_auto (parm_pack));
12776 unsubstituted_packs = true;
12777 }
12778 }
12779
12780 /* If the expansion is just T..., return the matching argument pack, unless
12781 we need to call convert_from_reference on all the elements. This is an
12782 important optimization; see c++/68422. */
12783 if (!unsubstituted_packs
12784 && TREE_PURPOSE (packs) == pattern)
12785 {
12786 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12787
12788 /* If the argument pack is a single pack expansion, pull it out. */
12789 if (TREE_VEC_LENGTH (args) == 1
12790 && pack_expansion_args_count (args))
12791 return TREE_VEC_ELT (args, 0);
12792
12793 /* Types need no adjustment, nor does sizeof..., and if we still have
12794 some pack expansion args we won't do anything yet. */
12795 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12796 || PACK_EXPANSION_SIZEOF_P (t)
12797 || pack_expansion_args_count (args))
12798 return args;
12799 /* Also optimize expression pack expansions if we can tell that the
12800 elements won't have reference type. */
12801 tree type = TREE_TYPE (pattern);
12802 if (type && !TYPE_REF_P (type)
12803 && !PACK_EXPANSION_P (type)
12804 && !WILDCARD_TYPE_P (type))
12805 return args;
12806 /* Otherwise use the normal path so we get convert_from_reference. */
12807 }
12808
12809 /* We cannot expand this expansion expression, because we don't have
12810 all of the argument packs we need. */
12811 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12812 {
12813 /* We got some full packs, but we can't substitute them in until we
12814 have values for all the packs. So remember these until then. */
12815
12816 t = make_pack_expansion (pattern, complain);
12817 PACK_EXPANSION_EXTRA_ARGS (t)
12818 = build_extra_args (pattern, args, complain);
12819 return t;
12820 }
12821 else if (unsubstituted_packs)
12822 {
12823 /* There were no real arguments, we're just replacing a parameter
12824 pack with another version of itself. Substitute into the
12825 pattern and return a PACK_EXPANSION_*. The caller will need to
12826 deal with that. */
12827 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12828 t = tsubst_expr (pattern, args, complain, in_decl,
12829 /*integral_constant_expression_p=*/false);
12830 else
12831 t = tsubst (pattern, args, complain, in_decl);
12832 t = make_pack_expansion (t, complain);
12833 return t;
12834 }
12835
12836 gcc_assert (len >= 0);
12837
12838 if (need_local_specializations)
12839 {
12840 /* We're in a late-specified return type, so create our own local
12841 specializations map; the current map is either NULL or (in the
12842 case of recursive unification) might have bindings that we don't
12843 want to use or alter. */
12844 saved_local_specializations = local_specializations;
12845 local_specializations = new hash_map<tree, tree>;
12846 }
12847
12848 /* For each argument in each argument pack, substitute into the
12849 pattern. */
12850 result = make_tree_vec (len);
12851 tree elem_args = copy_template_args (args);
12852 for (i = 0; i < len; ++i)
12853 {
12854 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12855 i,
12856 elem_args, complain,
12857 in_decl);
12858 TREE_VEC_ELT (result, i) = t;
12859 if (t == error_mark_node)
12860 {
12861 result = error_mark_node;
12862 break;
12863 }
12864 }
12865
12866 /* Update ARGS to restore the substitution from parameter packs to
12867 their argument packs. */
12868 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12869 {
12870 tree parm = TREE_PURPOSE (pack);
12871
12872 if (TREE_CODE (parm) == PARM_DECL
12873 || VAR_P (parm)
12874 || TREE_CODE (parm) == FIELD_DECL)
12875 register_local_specialization (TREE_TYPE (pack), parm);
12876 else
12877 {
12878 int idx, level;
12879
12880 if (TREE_VALUE (pack) == NULL_TREE)
12881 continue;
12882
12883 template_parm_level_and_index (parm, &level, &idx);
12884
12885 /* Update the corresponding argument. */
12886 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12887 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12888 TREE_TYPE (pack);
12889 else
12890 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12891 }
12892 }
12893
12894 if (need_local_specializations)
12895 {
12896 delete local_specializations;
12897 local_specializations = saved_local_specializations;
12898 }
12899
12900 /* If the dependent pack arguments were such that we end up with only a
12901 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12902 if (len == 1 && TREE_CODE (result) == TREE_VEC
12903 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12904 return TREE_VEC_ELT (result, 0);
12905
12906 return result;
12907 }
12908
12909 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12910 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12911 parameter packs; all parms generated from a function parameter pack will
12912 have the same DECL_PARM_INDEX. */
12913
12914 tree
12915 get_pattern_parm (tree parm, tree tmpl)
12916 {
12917 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12918 tree patparm;
12919
12920 if (DECL_ARTIFICIAL (parm))
12921 {
12922 for (patparm = DECL_ARGUMENTS (pattern);
12923 patparm; patparm = DECL_CHAIN (patparm))
12924 if (DECL_ARTIFICIAL (patparm)
12925 && DECL_NAME (parm) == DECL_NAME (patparm))
12926 break;
12927 }
12928 else
12929 {
12930 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12931 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12932 gcc_assert (DECL_PARM_INDEX (patparm)
12933 == DECL_PARM_INDEX (parm));
12934 }
12935
12936 return patparm;
12937 }
12938
12939 /* Make an argument pack out of the TREE_VEC VEC. */
12940
12941 static tree
12942 make_argument_pack (tree vec)
12943 {
12944 tree pack;
12945 tree elt = TREE_VEC_ELT (vec, 0);
12946 if (TYPE_P (elt))
12947 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12948 else
12949 {
12950 pack = make_node (NONTYPE_ARGUMENT_PACK);
12951 TREE_CONSTANT (pack) = 1;
12952 }
12953 SET_ARGUMENT_PACK_ARGS (pack, vec);
12954 return pack;
12955 }
12956
12957 /* Return an exact copy of template args T that can be modified
12958 independently. */
12959
12960 static tree
12961 copy_template_args (tree t)
12962 {
12963 if (t == error_mark_node)
12964 return t;
12965
12966 int len = TREE_VEC_LENGTH (t);
12967 tree new_vec = make_tree_vec (len);
12968
12969 for (int i = 0; i < len; ++i)
12970 {
12971 tree elt = TREE_VEC_ELT (t, i);
12972 if (elt && TREE_CODE (elt) == TREE_VEC)
12973 elt = copy_template_args (elt);
12974 TREE_VEC_ELT (new_vec, i) = elt;
12975 }
12976
12977 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12978 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12979
12980 return new_vec;
12981 }
12982
12983 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
12984
12985 tree
12986 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
12987 tree in_decl)
12988 {
12989 /* Substitute into each of the arguments. */
12990 tree new_arg = TYPE_P (orig_arg)
12991 ? cxx_make_type (TREE_CODE (orig_arg))
12992 : make_node (TREE_CODE (orig_arg));
12993
12994 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12995 args, complain, in_decl);
12996 if (pack_args == error_mark_node)
12997 new_arg = error_mark_node;
12998 else
12999 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13000
13001 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13002 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13003
13004 return new_arg;
13005 }
13006
13007 /* Substitute ARGS into the vector or list of template arguments T. */
13008
13009 tree
13010 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13011 {
13012 tree orig_t = t;
13013 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13014 tree *elts;
13015
13016 if (t == error_mark_node)
13017 return error_mark_node;
13018
13019 len = TREE_VEC_LENGTH (t);
13020 elts = XALLOCAVEC (tree, len);
13021
13022 for (i = 0; i < len; i++)
13023 {
13024 tree orig_arg = TREE_VEC_ELT (t, i);
13025 tree new_arg;
13026
13027 if (TREE_CODE (orig_arg) == TREE_VEC)
13028 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13029 else if (PACK_EXPANSION_P (orig_arg))
13030 {
13031 /* Substitute into an expansion expression. */
13032 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13033
13034 if (TREE_CODE (new_arg) == TREE_VEC)
13035 /* Add to the expanded length adjustment the number of
13036 expanded arguments. We subtract one from this
13037 measurement, because the argument pack expression
13038 itself is already counted as 1 in
13039 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13040 the argument pack is empty. */
13041 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13042 }
13043 else if (ARGUMENT_PACK_P (orig_arg))
13044 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13045 else
13046 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13047
13048 if (new_arg == error_mark_node)
13049 return error_mark_node;
13050
13051 elts[i] = new_arg;
13052 if (new_arg != orig_arg)
13053 need_new = 1;
13054 }
13055
13056 if (!need_new)
13057 return t;
13058
13059 /* Make space for the expanded arguments coming from template
13060 argument packs. */
13061 t = make_tree_vec (len + expanded_len_adjust);
13062 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13063 arguments for a member template.
13064 In that case each TREE_VEC in ORIG_T represents a level of template
13065 arguments, and ORIG_T won't carry any non defaulted argument count.
13066 It will rather be the nested TREE_VECs that will carry one.
13067 In other words, ORIG_T carries a non defaulted argument count only
13068 if it doesn't contain any nested TREE_VEC. */
13069 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13070 {
13071 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13072 count += expanded_len_adjust;
13073 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13074 }
13075 for (i = 0, out = 0; i < len; i++)
13076 {
13077 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13078 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13079 && TREE_CODE (elts[i]) == TREE_VEC)
13080 {
13081 int idx;
13082
13083 /* Now expand the template argument pack "in place". */
13084 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13085 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13086 }
13087 else
13088 {
13089 TREE_VEC_ELT (t, out) = elts[i];
13090 out++;
13091 }
13092 }
13093
13094 return t;
13095 }
13096
13097 /* Substitute ARGS into one level PARMS of template parameters. */
13098
13099 static tree
13100 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13101 {
13102 if (parms == error_mark_node)
13103 return error_mark_node;
13104
13105 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13106
13107 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13108 {
13109 tree tuple = TREE_VEC_ELT (parms, i);
13110
13111 if (tuple == error_mark_node)
13112 continue;
13113
13114 TREE_VEC_ELT (new_vec, i) =
13115 tsubst_template_parm (tuple, args, complain);
13116 }
13117
13118 return new_vec;
13119 }
13120
13121 /* Return the result of substituting ARGS into the template parameters
13122 given by PARMS. If there are m levels of ARGS and m + n levels of
13123 PARMS, then the result will contain n levels of PARMS. For
13124 example, if PARMS is `template <class T> template <class U>
13125 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13126 result will be `template <int*, double, class V>'. */
13127
13128 static tree
13129 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13130 {
13131 tree r = NULL_TREE;
13132 tree* new_parms;
13133
13134 /* When substituting into a template, we must set
13135 PROCESSING_TEMPLATE_DECL as the template parameters may be
13136 dependent if they are based on one-another, and the dependency
13137 predicates are short-circuit outside of templates. */
13138 ++processing_template_decl;
13139
13140 for (new_parms = &r;
13141 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13142 new_parms = &(TREE_CHAIN (*new_parms)),
13143 parms = TREE_CHAIN (parms))
13144 {
13145 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13146 args, complain);
13147 *new_parms =
13148 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13149 - TMPL_ARGS_DEPTH (args)),
13150 new_vec, NULL_TREE);
13151 }
13152
13153 --processing_template_decl;
13154
13155 return r;
13156 }
13157
13158 /* Return the result of substituting ARGS into one template parameter
13159 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13160 parameter and which TREE_PURPOSE is the default argument of the
13161 template parameter. */
13162
13163 static tree
13164 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13165 {
13166 tree default_value, parm_decl;
13167
13168 if (args == NULL_TREE
13169 || t == NULL_TREE
13170 || t == error_mark_node)
13171 return t;
13172
13173 gcc_assert (TREE_CODE (t) == TREE_LIST);
13174
13175 default_value = TREE_PURPOSE (t);
13176 parm_decl = TREE_VALUE (t);
13177 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13178
13179 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13180 if (TREE_CODE (parm_decl) == PARM_DECL
13181 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13182 parm_decl = error_mark_node;
13183 default_value = tsubst_template_arg (default_value, args,
13184 complain, NULL_TREE);
13185 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13186
13187 tree r = build_tree_list (default_value, parm_decl);
13188 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13189 return r;
13190 }
13191
13192 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13193 type T. If T is not an aggregate or enumeration type, it is
13194 handled as if by tsubst. IN_DECL is as for tsubst. If
13195 ENTERING_SCOPE is nonzero, T is the context for a template which
13196 we are presently tsubst'ing. Return the substituted value. */
13197
13198 static tree
13199 tsubst_aggr_type (tree t,
13200 tree args,
13201 tsubst_flags_t complain,
13202 tree in_decl,
13203 int entering_scope)
13204 {
13205 if (t == NULL_TREE)
13206 return NULL_TREE;
13207
13208 switch (TREE_CODE (t))
13209 {
13210 case RECORD_TYPE:
13211 if (TYPE_PTRMEMFUNC_P (t))
13212 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13213
13214 /* Fall through. */
13215 case ENUMERAL_TYPE:
13216 case UNION_TYPE:
13217 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13218 {
13219 tree argvec;
13220 tree context;
13221 tree r;
13222
13223 /* In "sizeof(X<I>)" we need to evaluate "I". */
13224 cp_evaluated ev;
13225
13226 /* First, determine the context for the type we are looking
13227 up. */
13228 context = TYPE_CONTEXT (t);
13229 if (context && TYPE_P (context))
13230 {
13231 context = tsubst_aggr_type (context, args, complain,
13232 in_decl, /*entering_scope=*/1);
13233 /* If context is a nested class inside a class template,
13234 it may still need to be instantiated (c++/33959). */
13235 context = complete_type (context);
13236 }
13237
13238 /* Then, figure out what arguments are appropriate for the
13239 type we are trying to find. For example, given:
13240
13241 template <class T> struct S;
13242 template <class T, class U> void f(T, U) { S<U> su; }
13243
13244 and supposing that we are instantiating f<int, double>,
13245 then our ARGS will be {int, double}, but, when looking up
13246 S we only want {double}. */
13247 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13248 complain, in_decl);
13249 if (argvec == error_mark_node)
13250 r = error_mark_node;
13251 else
13252 {
13253 r = lookup_template_class (t, argvec, in_decl, context,
13254 entering_scope, complain);
13255 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13256 }
13257
13258 return r;
13259 }
13260 else
13261 /* This is not a template type, so there's nothing to do. */
13262 return t;
13263
13264 default:
13265 return tsubst (t, args, complain, in_decl);
13266 }
13267 }
13268
13269 static GTY((cache)) tree_cache_map *defarg_inst;
13270
13271 /* Substitute into the default argument ARG (a default argument for
13272 FN), which has the indicated TYPE. */
13273
13274 tree
13275 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13276 tsubst_flags_t complain)
13277 {
13278 int errs = errorcount + sorrycount;
13279
13280 /* This can happen in invalid code. */
13281 if (TREE_CODE (arg) == DEFERRED_PARSE)
13282 return arg;
13283
13284 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13285 parm = chain_index (parmnum, parm);
13286 tree parmtype = TREE_TYPE (parm);
13287 if (DECL_BY_REFERENCE (parm))
13288 parmtype = TREE_TYPE (parmtype);
13289 if (parmtype == error_mark_node)
13290 return error_mark_node;
13291
13292 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13293
13294 tree *slot;
13295 if (defarg_inst && (slot = defarg_inst->get (parm)))
13296 return *slot;
13297
13298 /* This default argument came from a template. Instantiate the
13299 default argument here, not in tsubst. In the case of
13300 something like:
13301
13302 template <class T>
13303 struct S {
13304 static T t();
13305 void f(T = t());
13306 };
13307
13308 we must be careful to do name lookup in the scope of S<T>,
13309 rather than in the current class. */
13310 push_to_top_level ();
13311 push_access_scope (fn);
13312 push_deferring_access_checks (dk_no_deferred);
13313 start_lambda_scope (parm);
13314
13315 /* The default argument expression may cause implicitly defined
13316 member functions to be synthesized, which will result in garbage
13317 collection. We must treat this situation as if we were within
13318 the body of function so as to avoid collecting live data on the
13319 stack. */
13320 ++function_depth;
13321 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13322 complain, NULL_TREE,
13323 /*integral_constant_expression_p=*/false);
13324 --function_depth;
13325
13326 finish_lambda_scope ();
13327
13328 /* Make sure the default argument is reasonable. */
13329 arg = check_default_argument (type, arg, complain);
13330
13331 if (errorcount+sorrycount > errs
13332 && (complain & tf_warning_or_error))
13333 inform (input_location,
13334 " when instantiating default argument for call to %qD", fn);
13335
13336 pop_deferring_access_checks ();
13337 pop_access_scope (fn);
13338 pop_from_top_level ();
13339
13340 if (arg != error_mark_node && !cp_unevaluated_operand)
13341 {
13342 if (!defarg_inst)
13343 defarg_inst = tree_cache_map::create_ggc (37);
13344 defarg_inst->put (parm, arg);
13345 }
13346
13347 return arg;
13348 }
13349
13350 /* Substitute into all the default arguments for FN. */
13351
13352 static void
13353 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13354 {
13355 tree arg;
13356 tree tmpl_args;
13357
13358 tmpl_args = DECL_TI_ARGS (fn);
13359
13360 /* If this function is not yet instantiated, we certainly don't need
13361 its default arguments. */
13362 if (uses_template_parms (tmpl_args))
13363 return;
13364 /* Don't do this again for clones. */
13365 if (DECL_CLONED_FUNCTION_P (fn))
13366 return;
13367
13368 int i = 0;
13369 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13370 arg;
13371 arg = TREE_CHAIN (arg), ++i)
13372 if (TREE_PURPOSE (arg))
13373 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13374 TREE_VALUE (arg),
13375 TREE_PURPOSE (arg),
13376 complain);
13377 }
13378
13379 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13380 static GTY((cache)) tree_cache_map *explicit_specifier_map;
13381
13382 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13383
13384 void
13385 store_explicit_specifier (tree v, tree t)
13386 {
13387 if (!explicit_specifier_map)
13388 explicit_specifier_map = tree_cache_map::create_ggc (37);
13389 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13390 explicit_specifier_map->put (v, t);
13391 }
13392
13393 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13394
13395 static tree
13396 lookup_explicit_specifier (tree v)
13397 {
13398 return *explicit_specifier_map->get (v);
13399 }
13400
13401 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13402
13403 static tree
13404 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13405 tree lambda_fntype)
13406 {
13407 tree gen_tmpl, argvec;
13408 hashval_t hash = 0;
13409 tree in_decl = t;
13410
13411 /* Nobody should be tsubst'ing into non-template functions. */
13412 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13413
13414 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13415 {
13416 /* If T is not dependent, just return it. */
13417 if (!uses_template_parms (DECL_TI_ARGS (t))
13418 && !LAMBDA_FUNCTION_P (t))
13419 return t;
13420
13421 /* Calculate the most general template of which R is a
13422 specialization. */
13423 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13424
13425 /* We're substituting a lambda function under tsubst_lambda_expr but not
13426 directly from it; find the matching function we're already inside.
13427 But don't do this if T is a generic lambda with a single level of
13428 template parms, as in that case we're doing a normal instantiation. */
13429 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13430 && (!generic_lambda_fn_p (t)
13431 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13432 return enclosing_instantiation_of (t);
13433
13434 /* Calculate the complete set of arguments used to
13435 specialize R. */
13436 argvec = tsubst_template_args (DECL_TI_ARGS
13437 (DECL_TEMPLATE_RESULT
13438 (DECL_TI_TEMPLATE (t))),
13439 args, complain, in_decl);
13440 if (argvec == error_mark_node)
13441 return error_mark_node;
13442
13443 /* Check to see if we already have this specialization. */
13444 if (!lambda_fntype)
13445 {
13446 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13447 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13448 return spec;
13449 }
13450
13451 /* We can see more levels of arguments than parameters if
13452 there was a specialization of a member template, like
13453 this:
13454
13455 template <class T> struct S { template <class U> void f(); }
13456 template <> template <class U> void S<int>::f(U);
13457
13458 Here, we'll be substituting into the specialization,
13459 because that's where we can find the code we actually
13460 want to generate, but we'll have enough arguments for
13461 the most general template.
13462
13463 We also deal with the peculiar case:
13464
13465 template <class T> struct S {
13466 template <class U> friend void f();
13467 };
13468 template <class U> void f() {}
13469 template S<int>;
13470 template void f<double>();
13471
13472 Here, the ARGS for the instantiation of will be {int,
13473 double}. But, we only need as many ARGS as there are
13474 levels of template parameters in CODE_PATTERN. We are
13475 careful not to get fooled into reducing the ARGS in
13476 situations like:
13477
13478 template <class T> struct S { template <class U> void f(U); }
13479 template <class T> template <> void S<T>::f(int) {}
13480
13481 which we can spot because the pattern will be a
13482 specialization in this case. */
13483 int args_depth = TMPL_ARGS_DEPTH (args);
13484 int parms_depth =
13485 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13486
13487 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13488 args = get_innermost_template_args (args, parms_depth);
13489 }
13490 else
13491 {
13492 /* This special case arises when we have something like this:
13493
13494 template <class T> struct S {
13495 friend void f<int>(int, double);
13496 };
13497
13498 Here, the DECL_TI_TEMPLATE for the friend declaration
13499 will be an IDENTIFIER_NODE. We are being called from
13500 tsubst_friend_function, and we want only to create a
13501 new decl (R) with appropriate types so that we can call
13502 determine_specialization. */
13503 gen_tmpl = NULL_TREE;
13504 argvec = NULL_TREE;
13505 }
13506
13507 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13508 : NULL_TREE);
13509 tree ctx = closure ? closure : DECL_CONTEXT (t);
13510 bool member = ctx && TYPE_P (ctx);
13511
13512 if (member && !closure)
13513 ctx = tsubst_aggr_type (ctx, args,
13514 complain, t, /*entering_scope=*/1);
13515
13516 tree type = (lambda_fntype ? lambda_fntype
13517 : tsubst (TREE_TYPE (t), args,
13518 complain | tf_fndecl_type, in_decl));
13519 if (type == error_mark_node)
13520 return error_mark_node;
13521
13522 /* If we hit excessive deduction depth, the type is bogus even if
13523 it isn't error_mark_node, so don't build a decl. */
13524 if (excessive_deduction_depth)
13525 return error_mark_node;
13526
13527 /* We do NOT check for matching decls pushed separately at this
13528 point, as they may not represent instantiations of this
13529 template, and in any case are considered separate under the
13530 discrete model. */
13531 tree r = copy_decl (t);
13532 DECL_USE_TEMPLATE (r) = 0;
13533 TREE_TYPE (r) = type;
13534 /* Clear out the mangled name and RTL for the instantiation. */
13535 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13536 SET_DECL_RTL (r, NULL);
13537 /* Leave DECL_INITIAL set on deleted instantiations. */
13538 if (!DECL_DELETED_FN (r))
13539 DECL_INITIAL (r) = NULL_TREE;
13540 DECL_CONTEXT (r) = ctx;
13541
13542 /* Handle explicit(dependent-expr). */
13543 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13544 {
13545 tree spec = lookup_explicit_specifier (t);
13546 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13547 /*function_p=*/false,
13548 /*i_c_e_p=*/true);
13549 spec = build_explicit_specifier (spec, complain);
13550 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13551 }
13552
13553 /* OpenMP UDRs have the only argument a reference to the declared
13554 type. We want to diagnose if the declared type is a reference,
13555 which is invalid, but as references to references are usually
13556 quietly merged, diagnose it here. */
13557 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13558 {
13559 tree argtype
13560 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13561 argtype = tsubst (argtype, args, complain, in_decl);
13562 if (TYPE_REF_P (argtype))
13563 error_at (DECL_SOURCE_LOCATION (t),
13564 "reference type %qT in "
13565 "%<#pragma omp declare reduction%>", argtype);
13566 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13567 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13568 argtype);
13569 }
13570
13571 if (member && DECL_CONV_FN_P (r))
13572 /* Type-conversion operator. Reconstruct the name, in
13573 case it's the name of one of the template's parameters. */
13574 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13575
13576 tree parms = DECL_ARGUMENTS (t);
13577 if (closure)
13578 parms = DECL_CHAIN (parms);
13579 parms = tsubst (parms, args, complain, t);
13580 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13581 DECL_CONTEXT (parm) = r;
13582 if (closure)
13583 {
13584 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13585 DECL_CHAIN (tparm) = parms;
13586 parms = tparm;
13587 }
13588 DECL_ARGUMENTS (r) = parms;
13589 DECL_RESULT (r) = NULL_TREE;
13590
13591 TREE_STATIC (r) = 0;
13592 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13593 DECL_EXTERNAL (r) = 1;
13594 /* If this is an instantiation of a function with internal
13595 linkage, we already know what object file linkage will be
13596 assigned to the instantiation. */
13597 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13598 DECL_DEFER_OUTPUT (r) = 0;
13599 DECL_CHAIN (r) = NULL_TREE;
13600 DECL_PENDING_INLINE_INFO (r) = 0;
13601 DECL_PENDING_INLINE_P (r) = 0;
13602 DECL_SAVED_TREE (r) = NULL_TREE;
13603 DECL_STRUCT_FUNCTION (r) = NULL;
13604 TREE_USED (r) = 0;
13605 /* We'll re-clone as appropriate in instantiate_template. */
13606 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13607
13608 /* If we aren't complaining now, return on error before we register
13609 the specialization so that we'll complain eventually. */
13610 if ((complain & tf_error) == 0
13611 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13612 && !grok_op_properties (r, /*complain=*/false))
13613 return error_mark_node;
13614
13615 /* Associate the constraints directly with the instantiation. We
13616 don't substitute through the constraints; that's only done when
13617 they are checked. */
13618 if (tree ci = get_constraints (t))
13619 set_constraints (r, ci);
13620
13621 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13622 SET_DECL_FRIEND_CONTEXT (r,
13623 tsubst (DECL_FRIEND_CONTEXT (t),
13624 args, complain, in_decl));
13625
13626 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13627 this in the special friend case mentioned above where
13628 GEN_TMPL is NULL. */
13629 if (gen_tmpl && !closure)
13630 {
13631 DECL_TEMPLATE_INFO (r)
13632 = build_template_info (gen_tmpl, argvec);
13633 SET_DECL_IMPLICIT_INSTANTIATION (r);
13634
13635 tree new_r
13636 = register_specialization (r, gen_tmpl, argvec, false, hash);
13637 if (new_r != r)
13638 /* We instantiated this while substituting into
13639 the type earlier (template/friend54.C). */
13640 return new_r;
13641
13642 /* We're not supposed to instantiate default arguments
13643 until they are called, for a template. But, for a
13644 declaration like:
13645
13646 template <class T> void f ()
13647 { extern void g(int i = T()); }
13648
13649 we should do the substitution when the template is
13650 instantiated. We handle the member function case in
13651 instantiate_class_template since the default arguments
13652 might refer to other members of the class. */
13653 if (!member
13654 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13655 && !uses_template_parms (argvec))
13656 tsubst_default_arguments (r, complain);
13657 }
13658 else
13659 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13660
13661 /* Copy the list of befriending classes. */
13662 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13663 *friends;
13664 friends = &TREE_CHAIN (*friends))
13665 {
13666 *friends = copy_node (*friends);
13667 TREE_VALUE (*friends)
13668 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13669 }
13670
13671 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13672 {
13673 maybe_retrofit_in_chrg (r);
13674 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13675 return error_mark_node;
13676 /* If this is an instantiation of a member template, clone it.
13677 If it isn't, that'll be handled by
13678 clone_constructors_and_destructors. */
13679 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13680 clone_function_decl (r, /*update_methods=*/false);
13681 }
13682 else if ((complain & tf_error) != 0
13683 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13684 && !grok_op_properties (r, /*complain=*/true))
13685 return error_mark_node;
13686
13687 /* Possibly limit visibility based on template args. */
13688 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13689 if (DECL_VISIBILITY_SPECIFIED (t))
13690 {
13691 DECL_VISIBILITY_SPECIFIED (r) = 0;
13692 DECL_ATTRIBUTES (r)
13693 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13694 }
13695 determine_visibility (r);
13696 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13697 && !processing_template_decl)
13698 defaulted_late_check (r);
13699
13700 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13701 args, complain, in_decl);
13702 if (flag_openmp)
13703 if (tree attr = lookup_attribute ("omp declare variant base",
13704 DECL_ATTRIBUTES (r)))
13705 omp_declare_variant_finalize (r, attr);
13706
13707 return r;
13708 }
13709
13710 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13711
13712 static tree
13713 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13714 tree lambda_fntype)
13715 {
13716 /* We can get here when processing a member function template,
13717 member class template, or template template parameter. */
13718 tree decl = DECL_TEMPLATE_RESULT (t);
13719 tree in_decl = t;
13720 tree spec;
13721 tree tmpl_args;
13722 tree full_args;
13723 tree r;
13724 hashval_t hash = 0;
13725
13726 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13727 {
13728 /* Template template parameter is treated here. */
13729 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13730 if (new_type == error_mark_node)
13731 r = error_mark_node;
13732 /* If we get a real template back, return it. This can happen in
13733 the context of most_specialized_partial_spec. */
13734 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13735 r = new_type;
13736 else
13737 /* The new TEMPLATE_DECL was built in
13738 reduce_template_parm_level. */
13739 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13740 return r;
13741 }
13742
13743 if (!lambda_fntype)
13744 {
13745 /* We might already have an instance of this template.
13746 The ARGS are for the surrounding class type, so the
13747 full args contain the tsubst'd args for the context,
13748 plus the innermost args from the template decl. */
13749 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13750 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13751 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13752 /* Because this is a template, the arguments will still be
13753 dependent, even after substitution. If
13754 PROCESSING_TEMPLATE_DECL is not set, the dependency
13755 predicates will short-circuit. */
13756 ++processing_template_decl;
13757 full_args = tsubst_template_args (tmpl_args, args,
13758 complain, in_decl);
13759 --processing_template_decl;
13760 if (full_args == error_mark_node)
13761 return error_mark_node;
13762
13763 /* If this is a default template template argument,
13764 tsubst might not have changed anything. */
13765 if (full_args == tmpl_args)
13766 return t;
13767
13768 hash = hash_tmpl_and_args (t, full_args);
13769 spec = retrieve_specialization (t, full_args, hash);
13770 if (spec != NULL_TREE)
13771 {
13772 if (TYPE_P (spec))
13773 /* Type partial instantiations are stored as the type by
13774 lookup_template_class_1, not here as the template. */
13775 spec = CLASSTYPE_TI_TEMPLATE (spec);
13776 return spec;
13777 }
13778 }
13779
13780 /* Make a new template decl. It will be similar to the
13781 original, but will record the current template arguments.
13782 We also create a new function declaration, which is just
13783 like the old one, but points to this new template, rather
13784 than the old one. */
13785 r = copy_decl (t);
13786 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13787 DECL_CHAIN (r) = NULL_TREE;
13788
13789 // Build new template info linking to the original template decl.
13790 if (!lambda_fntype)
13791 {
13792 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13793 SET_DECL_IMPLICIT_INSTANTIATION (r);
13794 }
13795 else
13796 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13797
13798 /* The template parameters for this new template are all the
13799 template parameters for the old template, except the
13800 outermost level of parameters. */
13801 DECL_TEMPLATE_PARMS (r)
13802 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13803 complain);
13804
13805 if (TREE_CODE (decl) == TYPE_DECL
13806 && !TYPE_DECL_ALIAS_P (decl))
13807 {
13808 tree new_type;
13809 ++processing_template_decl;
13810 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13811 --processing_template_decl;
13812 if (new_type == error_mark_node)
13813 return error_mark_node;
13814
13815 TREE_TYPE (r) = new_type;
13816 /* For a partial specialization, we need to keep pointing to
13817 the primary template. */
13818 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13819 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13820 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13821 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13822 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13823 }
13824 else
13825 {
13826 tree new_decl;
13827 ++processing_template_decl;
13828 if (TREE_CODE (decl) == FUNCTION_DECL)
13829 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13830 else
13831 new_decl = tsubst (decl, args, complain, in_decl);
13832 --processing_template_decl;
13833 if (new_decl == error_mark_node)
13834 return error_mark_node;
13835
13836 DECL_TEMPLATE_RESULT (r) = new_decl;
13837 TREE_TYPE (r) = TREE_TYPE (new_decl);
13838 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13839 if (lambda_fntype)
13840 {
13841 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13842 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13843 }
13844 else
13845 {
13846 DECL_TI_TEMPLATE (new_decl) = r;
13847 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13848 }
13849 }
13850
13851 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13852 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13853
13854 if (PRIMARY_TEMPLATE_P (t))
13855 DECL_PRIMARY_TEMPLATE (r) = r;
13856
13857 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13858 && !lambda_fntype)
13859 /* Record this non-type partial instantiation. */
13860 register_specialization (r, t,
13861 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13862 false, hash);
13863
13864 return r;
13865 }
13866
13867 /* True if FN is the op() for a lambda in an uninstantiated template. */
13868
13869 bool
13870 lambda_fn_in_template_p (tree fn)
13871 {
13872 if (!fn || !LAMBDA_FUNCTION_P (fn))
13873 return false;
13874 tree closure = DECL_CONTEXT (fn);
13875 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13876 }
13877
13878 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13879 which the above is true. */
13880
13881 bool
13882 instantiated_lambda_fn_p (tree fn)
13883 {
13884 if (!fn || !LAMBDA_FUNCTION_P (fn))
13885 return false;
13886 tree closure = DECL_CONTEXT (fn);
13887 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13888 return LAMBDA_EXPR_INSTANTIATED (lam);
13889 }
13890
13891 /* We're instantiating a variable from template function TCTX. Return the
13892 corresponding current enclosing scope. This gets complicated because lambda
13893 functions in templates are regenerated rather than instantiated, but generic
13894 lambda functions are subsequently instantiated. */
13895
13896 static tree
13897 enclosing_instantiation_of (tree otctx)
13898 {
13899 tree tctx = otctx;
13900 tree fn = current_function_decl;
13901 int lambda_count = 0;
13902
13903 for (; tctx && (lambda_fn_in_template_p (tctx)
13904 || instantiated_lambda_fn_p (tctx));
13905 tctx = decl_function_context (tctx))
13906 ++lambda_count;
13907 for (; fn; fn = decl_function_context (fn))
13908 {
13909 tree ofn = fn;
13910 int flambda_count = 0;
13911 for (; fn && instantiated_lambda_fn_p (fn);
13912 fn = decl_function_context (fn))
13913 ++flambda_count;
13914 if ((fn && DECL_TEMPLATE_INFO (fn))
13915 ? most_general_template (fn) != most_general_template (tctx)
13916 : fn != tctx)
13917 continue;
13918 if (flambda_count != lambda_count)
13919 {
13920 gcc_assert (flambda_count > lambda_count);
13921 for (; flambda_count > lambda_count; --flambda_count)
13922 ofn = decl_function_context (ofn);
13923 }
13924 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13925 || DECL_CONV_FN_P (ofn));
13926 return ofn;
13927 }
13928 gcc_unreachable ();
13929 }
13930
13931 /* Substitute the ARGS into the T, which is a _DECL. Return the
13932 result of the substitution. Issue error and warning messages under
13933 control of COMPLAIN. */
13934
13935 static tree
13936 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13937 {
13938 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13939 location_t saved_loc;
13940 tree r = NULL_TREE;
13941 tree in_decl = t;
13942 hashval_t hash = 0;
13943
13944 /* Set the filename and linenumber to improve error-reporting. */
13945 saved_loc = input_location;
13946 input_location = DECL_SOURCE_LOCATION (t);
13947
13948 switch (TREE_CODE (t))
13949 {
13950 case TEMPLATE_DECL:
13951 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13952 break;
13953
13954 case FUNCTION_DECL:
13955 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13956 break;
13957
13958 case PARM_DECL:
13959 {
13960 tree type = NULL_TREE;
13961 int i, len = 1;
13962 tree expanded_types = NULL_TREE;
13963 tree prev_r = NULL_TREE;
13964 tree first_r = NULL_TREE;
13965
13966 if (DECL_PACK_P (t))
13967 {
13968 /* If there is a local specialization that isn't a
13969 parameter pack, it means that we're doing a "simple"
13970 substitution from inside tsubst_pack_expansion. Just
13971 return the local specialization (which will be a single
13972 parm). */
13973 tree spec = retrieve_local_specialization (t);
13974 if (spec
13975 && TREE_CODE (spec) == PARM_DECL
13976 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13977 RETURN (spec);
13978
13979 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13980 the parameters in this function parameter pack. */
13981 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13982 complain, in_decl);
13983 if (TREE_CODE (expanded_types) == TREE_VEC)
13984 {
13985 len = TREE_VEC_LENGTH (expanded_types);
13986
13987 /* Zero-length parameter packs are boring. Just substitute
13988 into the chain. */
13989 if (len == 0)
13990 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13991 TREE_CHAIN (t)));
13992 }
13993 else
13994 {
13995 /* All we did was update the type. Make a note of that. */
13996 type = expanded_types;
13997 expanded_types = NULL_TREE;
13998 }
13999 }
14000
14001 /* Loop through all of the parameters we'll build. When T is
14002 a function parameter pack, LEN is the number of expanded
14003 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14004 r = NULL_TREE;
14005 for (i = 0; i < len; ++i)
14006 {
14007 prev_r = r;
14008 r = copy_node (t);
14009 if (DECL_TEMPLATE_PARM_P (t))
14010 SET_DECL_TEMPLATE_PARM_P (r);
14011
14012 if (expanded_types)
14013 /* We're on the Ith parameter of the function parameter
14014 pack. */
14015 {
14016 /* Get the Ith type. */
14017 type = TREE_VEC_ELT (expanded_types, i);
14018
14019 /* Rename the parameter to include the index. */
14020 DECL_NAME (r)
14021 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14022 }
14023 else if (!type)
14024 /* We're dealing with a normal parameter. */
14025 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14026
14027 type = type_decays_to (type);
14028 TREE_TYPE (r) = type;
14029 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14030
14031 if (DECL_INITIAL (r))
14032 {
14033 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14034 DECL_INITIAL (r) = TREE_TYPE (r);
14035 else
14036 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14037 complain, in_decl);
14038 }
14039
14040 DECL_CONTEXT (r) = NULL_TREE;
14041
14042 if (!DECL_TEMPLATE_PARM_P (r))
14043 DECL_ARG_TYPE (r) = type_passed_as (type);
14044
14045 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14046 args, complain, in_decl);
14047
14048 /* Keep track of the first new parameter we
14049 generate. That's what will be returned to the
14050 caller. */
14051 if (!first_r)
14052 first_r = r;
14053
14054 /* Build a proper chain of parameters when substituting
14055 into a function parameter pack. */
14056 if (prev_r)
14057 DECL_CHAIN (prev_r) = r;
14058 }
14059
14060 /* If cp_unevaluated_operand is set, we're just looking for a
14061 single dummy parameter, so don't keep going. */
14062 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14063 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14064 complain, DECL_CHAIN (t));
14065
14066 /* FIRST_R contains the start of the chain we've built. */
14067 r = first_r;
14068 }
14069 break;
14070
14071 case FIELD_DECL:
14072 {
14073 tree type = NULL_TREE;
14074 tree vec = NULL_TREE;
14075 tree expanded_types = NULL_TREE;
14076 int len = 1;
14077
14078 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14079 {
14080 /* This field is a lambda capture pack. Return a TREE_VEC of
14081 the expanded fields to instantiate_class_template_1. */
14082 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14083 complain, in_decl);
14084 if (TREE_CODE (expanded_types) == TREE_VEC)
14085 {
14086 len = TREE_VEC_LENGTH (expanded_types);
14087 vec = make_tree_vec (len);
14088 }
14089 else
14090 {
14091 /* All we did was update the type. Make a note of that. */
14092 type = expanded_types;
14093 expanded_types = NULL_TREE;
14094 }
14095 }
14096
14097 for (int i = 0; i < len; ++i)
14098 {
14099 r = copy_decl (t);
14100 if (expanded_types)
14101 {
14102 type = TREE_VEC_ELT (expanded_types, i);
14103 DECL_NAME (r)
14104 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14105 }
14106 else if (!type)
14107 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14108
14109 if (type == error_mark_node)
14110 RETURN (error_mark_node);
14111 TREE_TYPE (r) = type;
14112 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14113
14114 if (DECL_C_BIT_FIELD (r))
14115 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14116 number of bits. */
14117 DECL_BIT_FIELD_REPRESENTATIVE (r)
14118 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14119 complain, in_decl,
14120 /*integral_constant_expression_p=*/true);
14121 if (DECL_INITIAL (t))
14122 {
14123 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14124 NSDMI in perform_member_init. Still set DECL_INITIAL
14125 so that we know there is one. */
14126 DECL_INITIAL (r) = void_node;
14127 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14128 retrofit_lang_decl (r);
14129 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14130 }
14131 /* We don't have to set DECL_CONTEXT here; it is set by
14132 finish_member_declaration. */
14133 DECL_CHAIN (r) = NULL_TREE;
14134
14135 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14136 args, complain, in_decl);
14137
14138 if (vec)
14139 TREE_VEC_ELT (vec, i) = r;
14140 }
14141
14142 if (vec)
14143 r = vec;
14144 }
14145 break;
14146
14147 case USING_DECL:
14148 /* We reach here only for member using decls. We also need to check
14149 uses_template_parms because DECL_DEPENDENT_P is not set for a
14150 using-declaration that designates a member of the current
14151 instantiation (c++/53549). */
14152 if (DECL_DEPENDENT_P (t)
14153 || uses_template_parms (USING_DECL_SCOPE (t)))
14154 {
14155 tree scope = USING_DECL_SCOPE (t);
14156 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14157 if (PACK_EXPANSION_P (scope))
14158 {
14159 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14160 int len = TREE_VEC_LENGTH (vec);
14161 r = make_tree_vec (len);
14162 for (int i = 0; i < len; ++i)
14163 {
14164 tree escope = TREE_VEC_ELT (vec, i);
14165 tree elt = do_class_using_decl (escope, name);
14166 if (!elt)
14167 {
14168 r = error_mark_node;
14169 break;
14170 }
14171 else
14172 {
14173 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14174 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14175 }
14176 TREE_VEC_ELT (r, i) = elt;
14177 }
14178 }
14179 else
14180 {
14181 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14182 complain, in_decl);
14183 r = do_class_using_decl (inst_scope, name);
14184 if (!r)
14185 r = error_mark_node;
14186 else
14187 {
14188 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14189 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14190 }
14191 }
14192 }
14193 else
14194 {
14195 r = copy_node (t);
14196 DECL_CHAIN (r) = NULL_TREE;
14197 }
14198 break;
14199
14200 case TYPE_DECL:
14201 case VAR_DECL:
14202 {
14203 tree argvec = NULL_TREE;
14204 tree gen_tmpl = NULL_TREE;
14205 tree spec;
14206 tree tmpl = NULL_TREE;
14207 tree ctx;
14208 tree type = NULL_TREE;
14209 bool local_p;
14210
14211 if (TREE_TYPE (t) == error_mark_node)
14212 RETURN (error_mark_node);
14213
14214 if (TREE_CODE (t) == TYPE_DECL
14215 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14216 {
14217 /* If this is the canonical decl, we don't have to
14218 mess with instantiations, and often we can't (for
14219 typename, template type parms and such). Note that
14220 TYPE_NAME is not correct for the above test if
14221 we've copied the type for a typedef. */
14222 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14223 if (type == error_mark_node)
14224 RETURN (error_mark_node);
14225 r = TYPE_NAME (type);
14226 break;
14227 }
14228
14229 /* Check to see if we already have the specialization we
14230 need. */
14231 spec = NULL_TREE;
14232 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14233 {
14234 /* T is a static data member or namespace-scope entity.
14235 We have to substitute into namespace-scope variables
14236 (not just variable templates) because of cases like:
14237
14238 template <class T> void f() { extern T t; }
14239
14240 where the entity referenced is not known until
14241 instantiation time. */
14242 local_p = false;
14243 ctx = DECL_CONTEXT (t);
14244 if (DECL_CLASS_SCOPE_P (t))
14245 {
14246 ctx = tsubst_aggr_type (ctx, args,
14247 complain,
14248 in_decl, /*entering_scope=*/1);
14249 /* If CTX is unchanged, then T is in fact the
14250 specialization we want. That situation occurs when
14251 referencing a static data member within in its own
14252 class. We can use pointer equality, rather than
14253 same_type_p, because DECL_CONTEXT is always
14254 canonical... */
14255 if (ctx == DECL_CONTEXT (t)
14256 /* ... unless T is a member template; in which
14257 case our caller can be willing to create a
14258 specialization of that template represented
14259 by T. */
14260 && !(DECL_TI_TEMPLATE (t)
14261 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14262 spec = t;
14263 }
14264
14265 if (!spec)
14266 {
14267 tmpl = DECL_TI_TEMPLATE (t);
14268 gen_tmpl = most_general_template (tmpl);
14269 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14270 if (argvec != error_mark_node)
14271 argvec = (coerce_innermost_template_parms
14272 (DECL_TEMPLATE_PARMS (gen_tmpl),
14273 argvec, t, complain,
14274 /*all*/true, /*defarg*/true));
14275 if (argvec == error_mark_node)
14276 RETURN (error_mark_node);
14277 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14278 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14279 }
14280 }
14281 else
14282 {
14283 /* A local variable. */
14284 local_p = true;
14285 /* Subsequent calls to pushdecl will fill this in. */
14286 ctx = NULL_TREE;
14287 /* Unless this is a reference to a static variable from an
14288 enclosing function, in which case we need to fill it in now. */
14289 if (TREE_STATIC (t))
14290 {
14291 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14292 if (fn != current_function_decl)
14293 ctx = fn;
14294 }
14295 spec = retrieve_local_specialization (t);
14296 }
14297 /* If we already have the specialization we need, there is
14298 nothing more to do. */
14299 if (spec)
14300 {
14301 r = spec;
14302 break;
14303 }
14304
14305 /* Create a new node for the specialization we need. */
14306 if (type == NULL_TREE)
14307 {
14308 if (is_typedef_decl (t))
14309 type = DECL_ORIGINAL_TYPE (t);
14310 else
14311 type = TREE_TYPE (t);
14312 if (VAR_P (t)
14313 && VAR_HAD_UNKNOWN_BOUND (t)
14314 && type != error_mark_node)
14315 type = strip_array_domain (type);
14316 tree sub_args = args;
14317 if (tree auto_node = type_uses_auto (type))
14318 {
14319 /* Mask off any template args past the variable's context so we
14320 don't replace the auto with an unrelated argument. */
14321 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14322 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14323 if (extra > 0)
14324 /* This should never happen with the new lambda instantiation
14325 model, but keep the handling just in case. */
14326 gcc_assert (!CHECKING_P),
14327 sub_args = strip_innermost_template_args (args, extra);
14328 }
14329 type = tsubst (type, sub_args, complain, in_decl);
14330 /* Substituting the type might have recursively instantiated this
14331 same alias (c++/86171). */
14332 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14333 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14334 {
14335 r = spec;
14336 break;
14337 }
14338 }
14339 r = copy_decl (t);
14340 if (VAR_P (r))
14341 {
14342 DECL_INITIALIZED_P (r) = 0;
14343 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14344 if (type == error_mark_node)
14345 RETURN (error_mark_node);
14346 if (TREE_CODE (type) == FUNCTION_TYPE)
14347 {
14348 /* It may seem that this case cannot occur, since:
14349
14350 typedef void f();
14351 void g() { f x; }
14352
14353 declares a function, not a variable. However:
14354
14355 typedef void f();
14356 template <typename T> void g() { T t; }
14357 template void g<f>();
14358
14359 is an attempt to declare a variable with function
14360 type. */
14361 error ("variable %qD has function type",
14362 /* R is not yet sufficiently initialized, so we
14363 just use its name. */
14364 DECL_NAME (r));
14365 RETURN (error_mark_node);
14366 }
14367 type = complete_type (type);
14368 /* Wait until cp_finish_decl to set this again, to handle
14369 circular dependency (template/instantiate6.C). */
14370 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14371 type = check_var_type (DECL_NAME (r), type,
14372 DECL_SOURCE_LOCATION (r));
14373 if (DECL_HAS_VALUE_EXPR_P (t))
14374 {
14375 tree ve = DECL_VALUE_EXPR (t);
14376 ve = tsubst_expr (ve, args, complain, in_decl,
14377 /*constant_expression_p=*/false);
14378 if (REFERENCE_REF_P (ve))
14379 {
14380 gcc_assert (TYPE_REF_P (type));
14381 ve = TREE_OPERAND (ve, 0);
14382 }
14383 SET_DECL_VALUE_EXPR (r, ve);
14384 }
14385 if (CP_DECL_THREAD_LOCAL_P (r)
14386 && !processing_template_decl)
14387 set_decl_tls_model (r, decl_default_tls_model (r));
14388 }
14389 else if (DECL_SELF_REFERENCE_P (t))
14390 SET_DECL_SELF_REFERENCE_P (r);
14391 TREE_TYPE (r) = type;
14392 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14393 DECL_CONTEXT (r) = ctx;
14394 /* Clear out the mangled name and RTL for the instantiation. */
14395 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14396 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14397 SET_DECL_RTL (r, NULL);
14398 /* The initializer must not be expanded until it is required;
14399 see [temp.inst]. */
14400 DECL_INITIAL (r) = NULL_TREE;
14401 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14402 if (VAR_P (r))
14403 {
14404 if (DECL_LANG_SPECIFIC (r))
14405 SET_DECL_DEPENDENT_INIT_P (r, false);
14406
14407 SET_DECL_MODE (r, VOIDmode);
14408
14409 /* Possibly limit visibility based on template args. */
14410 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14411 if (DECL_VISIBILITY_SPECIFIED (t))
14412 {
14413 DECL_VISIBILITY_SPECIFIED (r) = 0;
14414 DECL_ATTRIBUTES (r)
14415 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14416 }
14417 determine_visibility (r);
14418 }
14419
14420 if (!local_p)
14421 {
14422 /* A static data member declaration is always marked
14423 external when it is declared in-class, even if an
14424 initializer is present. We mimic the non-template
14425 processing here. */
14426 DECL_EXTERNAL (r) = 1;
14427 if (DECL_NAMESPACE_SCOPE_P (t))
14428 DECL_NOT_REALLY_EXTERN (r) = 1;
14429
14430 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14431 SET_DECL_IMPLICIT_INSTANTIATION (r);
14432 /* Remember whether we require constant initialization of
14433 a non-constant template variable. */
14434 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14435 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14436 if (!error_operand_p (r) || (complain & tf_error))
14437 register_specialization (r, gen_tmpl, argvec, false, hash);
14438 }
14439 else
14440 {
14441 if (DECL_LANG_SPECIFIC (r))
14442 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14443 if (!cp_unevaluated_operand)
14444 register_local_specialization (r, t);
14445 }
14446
14447 DECL_CHAIN (r) = NULL_TREE;
14448
14449 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14450 /*flags=*/0,
14451 args, complain, in_decl);
14452
14453 /* Preserve a typedef that names a type. */
14454 if (is_typedef_decl (r) && type != error_mark_node)
14455 {
14456 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14457 set_underlying_type (r);
14458 if (TYPE_DECL_ALIAS_P (r))
14459 /* An alias template specialization can be dependent
14460 even if its underlying type is not. */
14461 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14462 }
14463
14464 layout_decl (r, 0);
14465 }
14466 break;
14467
14468 default:
14469 gcc_unreachable ();
14470 }
14471 #undef RETURN
14472
14473 out:
14474 /* Restore the file and line information. */
14475 input_location = saved_loc;
14476
14477 return r;
14478 }
14479
14480 /* Substitute into the complete parameter type list PARMS. */
14481
14482 tree
14483 tsubst_function_parms (tree parms,
14484 tree args,
14485 tsubst_flags_t complain,
14486 tree in_decl)
14487 {
14488 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14489 }
14490
14491 /* Substitute into the ARG_TYPES of a function type.
14492 If END is a TREE_CHAIN, leave it and any following types
14493 un-substituted. */
14494
14495 static tree
14496 tsubst_arg_types (tree arg_types,
14497 tree args,
14498 tree end,
14499 tsubst_flags_t complain,
14500 tree in_decl)
14501 {
14502 tree remaining_arg_types;
14503 tree type = NULL_TREE;
14504 int i = 1;
14505 tree expanded_args = NULL_TREE;
14506 tree default_arg;
14507
14508 if (!arg_types || arg_types == void_list_node || arg_types == end)
14509 return arg_types;
14510
14511 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14512 args, end, complain, in_decl);
14513 if (remaining_arg_types == error_mark_node)
14514 return error_mark_node;
14515
14516 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14517 {
14518 /* For a pack expansion, perform substitution on the
14519 entire expression. Later on, we'll handle the arguments
14520 one-by-one. */
14521 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14522 args, complain, in_decl);
14523
14524 if (TREE_CODE (expanded_args) == TREE_VEC)
14525 /* So that we'll spin through the parameters, one by one. */
14526 i = TREE_VEC_LENGTH (expanded_args);
14527 else
14528 {
14529 /* We only partially substituted into the parameter
14530 pack. Our type is TYPE_PACK_EXPANSION. */
14531 type = expanded_args;
14532 expanded_args = NULL_TREE;
14533 }
14534 }
14535
14536 while (i > 0) {
14537 --i;
14538
14539 if (expanded_args)
14540 type = TREE_VEC_ELT (expanded_args, i);
14541 else if (!type)
14542 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14543
14544 if (type == error_mark_node)
14545 return error_mark_node;
14546 if (VOID_TYPE_P (type))
14547 {
14548 if (complain & tf_error)
14549 {
14550 error ("invalid parameter type %qT", type);
14551 if (in_decl)
14552 error ("in declaration %q+D", in_decl);
14553 }
14554 return error_mark_node;
14555 }
14556 /* DR 657. */
14557 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14558 return error_mark_node;
14559
14560 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14561 top-level qualifiers as required. */
14562 type = cv_unqualified (type_decays_to (type));
14563
14564 /* We do not substitute into default arguments here. The standard
14565 mandates that they be instantiated only when needed, which is
14566 done in build_over_call. */
14567 default_arg = TREE_PURPOSE (arg_types);
14568
14569 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14570 since the new op() won't have any associated template arguments for us
14571 to refer to later. */
14572 if (lambda_fn_in_template_p (in_decl))
14573 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14574 false/*fn*/, false/*constexpr*/);
14575
14576 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14577 {
14578 /* We've instantiated a template before its default arguments
14579 have been parsed. This can happen for a nested template
14580 class, and is not an error unless we require the default
14581 argument in a call of this function. */
14582 remaining_arg_types =
14583 tree_cons (default_arg, type, remaining_arg_types);
14584 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14585 remaining_arg_types);
14586 }
14587 else
14588 remaining_arg_types =
14589 hash_tree_cons (default_arg, type, remaining_arg_types);
14590 }
14591
14592 return remaining_arg_types;
14593 }
14594
14595 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14596 *not* handle the exception-specification for FNTYPE, because the
14597 initial substitution of explicitly provided template parameters
14598 during argument deduction forbids substitution into the
14599 exception-specification:
14600
14601 [temp.deduct]
14602
14603 All references in the function type of the function template to the
14604 corresponding template parameters are replaced by the specified tem-
14605 plate argument values. If a substitution in a template parameter or
14606 in the function type of the function template results in an invalid
14607 type, type deduction fails. [Note: The equivalent substitution in
14608 exception specifications is done only when the function is instanti-
14609 ated, at which point a program is ill-formed if the substitution
14610 results in an invalid type.] */
14611
14612 static tree
14613 tsubst_function_type (tree t,
14614 tree args,
14615 tsubst_flags_t complain,
14616 tree in_decl)
14617 {
14618 tree return_type;
14619 tree arg_types = NULL_TREE;
14620 tree fntype;
14621
14622 /* The TYPE_CONTEXT is not used for function/method types. */
14623 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14624
14625 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14626 failure. */
14627 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14628
14629 if (late_return_type_p)
14630 {
14631 /* Substitute the argument types. */
14632 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14633 complain, in_decl);
14634 if (arg_types == error_mark_node)
14635 return error_mark_node;
14636
14637 tree save_ccp = current_class_ptr;
14638 tree save_ccr = current_class_ref;
14639 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14640 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14641 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14642 if (do_inject)
14643 {
14644 /* DR 1207: 'this' is in scope in the trailing return type. */
14645 inject_this_parameter (this_type, cp_type_quals (this_type));
14646 }
14647
14648 /* Substitute the return type. */
14649 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14650
14651 if (do_inject)
14652 {
14653 current_class_ptr = save_ccp;
14654 current_class_ref = save_ccr;
14655 }
14656 }
14657 else
14658 /* Substitute the return type. */
14659 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14660
14661 if (return_type == error_mark_node)
14662 return error_mark_node;
14663 /* DR 486 clarifies that creation of a function type with an
14664 invalid return type is a deduction failure. */
14665 if (TREE_CODE (return_type) == ARRAY_TYPE
14666 || TREE_CODE (return_type) == FUNCTION_TYPE)
14667 {
14668 if (complain & tf_error)
14669 {
14670 if (TREE_CODE (return_type) == ARRAY_TYPE)
14671 error ("function returning an array");
14672 else
14673 error ("function returning a function");
14674 }
14675 return error_mark_node;
14676 }
14677 /* And DR 657. */
14678 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14679 return error_mark_node;
14680
14681 if (!late_return_type_p)
14682 {
14683 /* Substitute the argument types. */
14684 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14685 complain, in_decl);
14686 if (arg_types == error_mark_node)
14687 return error_mark_node;
14688 }
14689
14690 /* Construct a new type node and return it. */
14691 if (TREE_CODE (t) == FUNCTION_TYPE)
14692 {
14693 fntype = build_function_type (return_type, arg_types);
14694 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14695 }
14696 else
14697 {
14698 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14699 /* Don't pick up extra function qualifiers from the basetype. */
14700 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14701 if (! MAYBE_CLASS_TYPE_P (r))
14702 {
14703 /* [temp.deduct]
14704
14705 Type deduction may fail for any of the following
14706 reasons:
14707
14708 -- Attempting to create "pointer to member of T" when T
14709 is not a class type. */
14710 if (complain & tf_error)
14711 error ("creating pointer to member function of non-class type %qT",
14712 r);
14713 return error_mark_node;
14714 }
14715
14716 fntype = build_method_type_directly (r, return_type,
14717 TREE_CHAIN (arg_types));
14718 }
14719 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14720
14721 /* See comment above. */
14722 tree raises = NULL_TREE;
14723 cp_ref_qualifier rqual = type_memfn_rqual (t);
14724 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14725
14726 return fntype;
14727 }
14728
14729 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14730 ARGS into that specification, and return the substituted
14731 specification. If there is no specification, return NULL_TREE. */
14732
14733 static tree
14734 tsubst_exception_specification (tree fntype,
14735 tree args,
14736 tsubst_flags_t complain,
14737 tree in_decl,
14738 bool defer_ok)
14739 {
14740 tree specs;
14741 tree new_specs;
14742
14743 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14744 new_specs = NULL_TREE;
14745 if (specs && TREE_PURPOSE (specs))
14746 {
14747 /* A noexcept-specifier. */
14748 tree expr = TREE_PURPOSE (specs);
14749 if (TREE_CODE (expr) == INTEGER_CST)
14750 new_specs = expr;
14751 else if (defer_ok)
14752 {
14753 /* Defer instantiation of noexcept-specifiers to avoid
14754 excessive instantiations (c++/49107). */
14755 new_specs = make_node (DEFERRED_NOEXCEPT);
14756 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14757 {
14758 /* We already partially instantiated this member template,
14759 so combine the new args with the old. */
14760 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14761 = DEFERRED_NOEXCEPT_PATTERN (expr);
14762 DEFERRED_NOEXCEPT_ARGS (new_specs)
14763 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14764 }
14765 else
14766 {
14767 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14768 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14769 }
14770 }
14771 else
14772 {
14773 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14774 {
14775 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14776 args);
14777 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14778 }
14779 new_specs = tsubst_copy_and_build
14780 (expr, args, complain, in_decl, /*function_p=*/false,
14781 /*integral_constant_expression_p=*/true);
14782 }
14783 new_specs = build_noexcept_spec (new_specs, complain);
14784 }
14785 else if (specs)
14786 {
14787 if (! TREE_VALUE (specs))
14788 new_specs = specs;
14789 else
14790 while (specs)
14791 {
14792 tree spec;
14793 int i, len = 1;
14794 tree expanded_specs = NULL_TREE;
14795
14796 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14797 {
14798 /* Expand the pack expansion type. */
14799 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14800 args, complain,
14801 in_decl);
14802
14803 if (expanded_specs == error_mark_node)
14804 return error_mark_node;
14805 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14806 len = TREE_VEC_LENGTH (expanded_specs);
14807 else
14808 {
14809 /* We're substituting into a member template, so
14810 we got a TYPE_PACK_EXPANSION back. Add that
14811 expansion and move on. */
14812 gcc_assert (TREE_CODE (expanded_specs)
14813 == TYPE_PACK_EXPANSION);
14814 new_specs = add_exception_specifier (new_specs,
14815 expanded_specs,
14816 complain);
14817 specs = TREE_CHAIN (specs);
14818 continue;
14819 }
14820 }
14821
14822 for (i = 0; i < len; ++i)
14823 {
14824 if (expanded_specs)
14825 spec = TREE_VEC_ELT (expanded_specs, i);
14826 else
14827 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14828 if (spec == error_mark_node)
14829 return spec;
14830 new_specs = add_exception_specifier (new_specs, spec,
14831 complain);
14832 }
14833
14834 specs = TREE_CHAIN (specs);
14835 }
14836 }
14837 return new_specs;
14838 }
14839
14840 /* Take the tree structure T and replace template parameters used
14841 therein with the argument vector ARGS. IN_DECL is an associated
14842 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14843 Issue error and warning messages under control of COMPLAIN. Note
14844 that we must be relatively non-tolerant of extensions here, in
14845 order to preserve conformance; if we allow substitutions that
14846 should not be allowed, we may allow argument deductions that should
14847 not succeed, and therefore report ambiguous overload situations
14848 where there are none. In theory, we could allow the substitution,
14849 but indicate that it should have failed, and allow our caller to
14850 make sure that the right thing happens, but we don't try to do this
14851 yet.
14852
14853 This function is used for dealing with types, decls and the like;
14854 for expressions, use tsubst_expr or tsubst_copy. */
14855
14856 tree
14857 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14858 {
14859 enum tree_code code;
14860 tree type, r = NULL_TREE;
14861
14862 if (t == NULL_TREE || t == error_mark_node
14863 || t == integer_type_node
14864 || t == void_type_node
14865 || t == char_type_node
14866 || t == unknown_type_node
14867 || TREE_CODE (t) == NAMESPACE_DECL
14868 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14869 return t;
14870
14871 if (DECL_P (t))
14872 return tsubst_decl (t, args, complain);
14873
14874 if (args == NULL_TREE)
14875 return t;
14876
14877 code = TREE_CODE (t);
14878
14879 if (code == IDENTIFIER_NODE)
14880 type = IDENTIFIER_TYPE_VALUE (t);
14881 else
14882 type = TREE_TYPE (t);
14883
14884 gcc_assert (type != unknown_type_node);
14885
14886 /* Reuse typedefs. We need to do this to handle dependent attributes,
14887 such as attribute aligned. */
14888 if (TYPE_P (t)
14889 && typedef_variant_p (t))
14890 {
14891 tree decl = TYPE_NAME (t);
14892
14893 if (alias_template_specialization_p (t))
14894 {
14895 /* DECL represents an alias template and we want to
14896 instantiate it. */
14897 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14898 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14899 r = instantiate_alias_template (tmpl, gen_args, complain);
14900 }
14901 else if (DECL_CLASS_SCOPE_P (decl)
14902 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14903 && uses_template_parms (DECL_CONTEXT (decl)))
14904 {
14905 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14906 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14907 r = retrieve_specialization (tmpl, gen_args, 0);
14908 }
14909 else if (DECL_FUNCTION_SCOPE_P (decl)
14910 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14911 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14912 r = retrieve_local_specialization (decl);
14913 else
14914 /* The typedef is from a non-template context. */
14915 return t;
14916
14917 if (r)
14918 {
14919 r = TREE_TYPE (r);
14920 r = cp_build_qualified_type_real
14921 (r, cp_type_quals (t) | cp_type_quals (r),
14922 complain | tf_ignore_bad_quals);
14923 return r;
14924 }
14925 else
14926 {
14927 /* We don't have an instantiation yet, so drop the typedef. */
14928 int quals = cp_type_quals (t);
14929 t = DECL_ORIGINAL_TYPE (decl);
14930 t = cp_build_qualified_type_real (t, quals,
14931 complain | tf_ignore_bad_quals);
14932 }
14933 }
14934
14935 bool fndecl_type = (complain & tf_fndecl_type);
14936 complain &= ~tf_fndecl_type;
14937
14938 if (type
14939 && code != TYPENAME_TYPE
14940 && code != TEMPLATE_TYPE_PARM
14941 && code != TEMPLATE_PARM_INDEX
14942 && code != IDENTIFIER_NODE
14943 && code != FUNCTION_TYPE
14944 && code != METHOD_TYPE)
14945 type = tsubst (type, args, complain, in_decl);
14946 if (type == error_mark_node)
14947 return error_mark_node;
14948
14949 switch (code)
14950 {
14951 case RECORD_TYPE:
14952 case UNION_TYPE:
14953 case ENUMERAL_TYPE:
14954 return tsubst_aggr_type (t, args, complain, in_decl,
14955 /*entering_scope=*/0);
14956
14957 case ERROR_MARK:
14958 case IDENTIFIER_NODE:
14959 case VOID_TYPE:
14960 case REAL_TYPE:
14961 case COMPLEX_TYPE:
14962 case VECTOR_TYPE:
14963 case BOOLEAN_TYPE:
14964 case NULLPTR_TYPE:
14965 case LANG_TYPE:
14966 return t;
14967
14968 case INTEGER_TYPE:
14969 if (t == integer_type_node)
14970 return t;
14971
14972 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14973 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14974 return t;
14975
14976 {
14977 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14978
14979 max = tsubst_expr (omax, args, complain, in_decl,
14980 /*integral_constant_expression_p=*/false);
14981
14982 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14983 needed. */
14984 if (TREE_CODE (max) == NOP_EXPR
14985 && TREE_SIDE_EFFECTS (omax)
14986 && !TREE_TYPE (max))
14987 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14988
14989 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14990 with TREE_SIDE_EFFECTS that indicates this is not an integral
14991 constant expression. */
14992 if (processing_template_decl
14993 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14994 {
14995 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14996 TREE_SIDE_EFFECTS (max) = 1;
14997 }
14998
14999 return compute_array_index_type (NULL_TREE, max, complain);
15000 }
15001
15002 case TEMPLATE_TYPE_PARM:
15003 case TEMPLATE_TEMPLATE_PARM:
15004 case BOUND_TEMPLATE_TEMPLATE_PARM:
15005 case TEMPLATE_PARM_INDEX:
15006 {
15007 int idx;
15008 int level;
15009 int levels;
15010 tree arg = NULL_TREE;
15011
15012 /* Early in template argument deduction substitution, we don't
15013 want to reduce the level of 'auto', or it will be confused
15014 with a normal template parm in subsequent deduction. */
15015 if (is_auto (t) && (complain & tf_partial))
15016 return t;
15017
15018 r = NULL_TREE;
15019
15020 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15021 template_parm_level_and_index (t, &level, &idx);
15022
15023 levels = TMPL_ARGS_DEPTH (args);
15024 if (level <= levels
15025 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15026 {
15027 arg = TMPL_ARG (args, level, idx);
15028
15029 /* See through ARGUMENT_PACK_SELECT arguments. */
15030 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15031 arg = argument_pack_select_arg (arg);
15032 }
15033
15034 if (arg == error_mark_node)
15035 return error_mark_node;
15036 else if (arg != NULL_TREE)
15037 {
15038 if (ARGUMENT_PACK_P (arg))
15039 /* If ARG is an argument pack, we don't actually want to
15040 perform a substitution here, because substitutions
15041 for argument packs are only done
15042 element-by-element. We can get to this point when
15043 substituting the type of a non-type template
15044 parameter pack, when that type actually contains
15045 template parameter packs from an outer template, e.g.,
15046
15047 template<typename... Types> struct A {
15048 template<Types... Values> struct B { };
15049 }; */
15050 return t;
15051
15052 if (code == TEMPLATE_TYPE_PARM)
15053 {
15054 int quals;
15055
15056 /* When building concept checks for the purpose of
15057 deducing placeholders, we can end up with wildcards
15058 where types are expected. Adjust this to the deduced
15059 value. */
15060 if (TREE_CODE (arg) == WILDCARD_DECL)
15061 arg = TREE_TYPE (TREE_TYPE (arg));
15062
15063 gcc_assert (TYPE_P (arg));
15064
15065 quals = cp_type_quals (arg) | cp_type_quals (t);
15066
15067 return cp_build_qualified_type_real
15068 (arg, quals, complain | tf_ignore_bad_quals);
15069 }
15070 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15071 {
15072 /* We are processing a type constructed from a
15073 template template parameter. */
15074 tree argvec = tsubst (TYPE_TI_ARGS (t),
15075 args, complain, in_decl);
15076 if (argvec == error_mark_node)
15077 return error_mark_node;
15078
15079 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15080 || TREE_CODE (arg) == TEMPLATE_DECL
15081 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15082
15083 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15084 /* Consider this code:
15085
15086 template <template <class> class Template>
15087 struct Internal {
15088 template <class Arg> using Bind = Template<Arg>;
15089 };
15090
15091 template <template <class> class Template, class Arg>
15092 using Instantiate = Template<Arg>; //#0
15093
15094 template <template <class> class Template,
15095 class Argument>
15096 using Bind =
15097 Instantiate<Internal<Template>::template Bind,
15098 Argument>; //#1
15099
15100 When #1 is parsed, the
15101 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15102 parameter `Template' in #0 matches the
15103 UNBOUND_CLASS_TEMPLATE representing the argument
15104 `Internal<Template>::template Bind'; We then want
15105 to assemble the type `Bind<Argument>' that can't
15106 be fully created right now, because
15107 `Internal<Template>' not being complete, the Bind
15108 template cannot be looked up in that context. So
15109 we need to "store" `Bind<Argument>' for later
15110 when the context of Bind becomes complete. Let's
15111 store that in a TYPENAME_TYPE. */
15112 return make_typename_type (TYPE_CONTEXT (arg),
15113 build_nt (TEMPLATE_ID_EXPR,
15114 TYPE_IDENTIFIER (arg),
15115 argvec),
15116 typename_type,
15117 complain);
15118
15119 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15120 are resolving nested-types in the signature of a
15121 member function templates. Otherwise ARG is a
15122 TEMPLATE_DECL and is the real template to be
15123 instantiated. */
15124 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15125 arg = TYPE_NAME (arg);
15126
15127 r = lookup_template_class (arg,
15128 argvec, in_decl,
15129 DECL_CONTEXT (arg),
15130 /*entering_scope=*/0,
15131 complain);
15132 return cp_build_qualified_type_real
15133 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15134 }
15135 else if (code == TEMPLATE_TEMPLATE_PARM)
15136 return arg;
15137 else
15138 /* TEMPLATE_PARM_INDEX. */
15139 return convert_from_reference (unshare_expr (arg));
15140 }
15141
15142 if (level == 1)
15143 /* This can happen during the attempted tsubst'ing in
15144 unify. This means that we don't yet have any information
15145 about the template parameter in question. */
15146 return t;
15147
15148 /* If we get here, we must have been looking at a parm for a
15149 more deeply nested template. Make a new version of this
15150 template parameter, but with a lower level. */
15151 switch (code)
15152 {
15153 case TEMPLATE_TYPE_PARM:
15154 case TEMPLATE_TEMPLATE_PARM:
15155 case BOUND_TEMPLATE_TEMPLATE_PARM:
15156 if (cp_type_quals (t))
15157 {
15158 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15159 r = cp_build_qualified_type_real
15160 (r, cp_type_quals (t),
15161 complain | (code == TEMPLATE_TYPE_PARM
15162 ? tf_ignore_bad_quals : 0));
15163 }
15164 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15165 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15166 && (r = (TEMPLATE_PARM_DESCENDANTS
15167 (TEMPLATE_TYPE_PARM_INDEX (t))))
15168 && (r = TREE_TYPE (r))
15169 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15170 /* Break infinite recursion when substituting the constraints
15171 of a constrained placeholder. */;
15172 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15173 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15174 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15175 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15176 r = TEMPLATE_PARM_DESCENDANTS (arg))
15177 && (TEMPLATE_PARM_LEVEL (r)
15178 == TEMPLATE_PARM_LEVEL (arg) - levels))
15179 /* Cache the simple case of lowering a type parameter. */
15180 r = TREE_TYPE (r);
15181 else
15182 {
15183 r = copy_type (t);
15184 TEMPLATE_TYPE_PARM_INDEX (r)
15185 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15186 r, levels, args, complain);
15187 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15188 TYPE_MAIN_VARIANT (r) = r;
15189 TYPE_POINTER_TO (r) = NULL_TREE;
15190 TYPE_REFERENCE_TO (r) = NULL_TREE;
15191
15192 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15193 {
15194 /* Propagate constraints on placeholders since they are
15195 only instantiated during satisfaction. */
15196 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15197 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15198 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15199 {
15200 pl = tsubst_copy (pl, args, complain, in_decl);
15201 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15202 }
15203 }
15204
15205 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15206 /* We have reduced the level of the template
15207 template parameter, but not the levels of its
15208 template parameters, so canonical_type_parameter
15209 will not be able to find the canonical template
15210 template parameter for this level. Thus, we
15211 require structural equality checking to compare
15212 TEMPLATE_TEMPLATE_PARMs. */
15213 SET_TYPE_STRUCTURAL_EQUALITY (r);
15214 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15215 SET_TYPE_STRUCTURAL_EQUALITY (r);
15216 else
15217 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15218
15219 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15220 {
15221 tree tinfo = TYPE_TEMPLATE_INFO (t);
15222 /* We might need to substitute into the types of non-type
15223 template parameters. */
15224 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15225 complain, in_decl);
15226 if (tmpl == error_mark_node)
15227 return error_mark_node;
15228 tree argvec = tsubst (TI_ARGS (tinfo), args,
15229 complain, in_decl);
15230 if (argvec == error_mark_node)
15231 return error_mark_node;
15232
15233 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15234 = build_template_info (tmpl, argvec);
15235 }
15236 }
15237 break;
15238
15239 case TEMPLATE_PARM_INDEX:
15240 /* OK, now substitute the type of the non-type parameter. We
15241 couldn't do it earlier because it might be an auto parameter,
15242 and we wouldn't need to if we had an argument. */
15243 type = tsubst (type, args, complain, in_decl);
15244 if (type == error_mark_node)
15245 return error_mark_node;
15246 r = reduce_template_parm_level (t, type, levels, args, complain);
15247 break;
15248
15249 default:
15250 gcc_unreachable ();
15251 }
15252
15253 return r;
15254 }
15255
15256 case TREE_LIST:
15257 {
15258 tree purpose, value, chain;
15259
15260 if (t == void_list_node)
15261 return t;
15262
15263 purpose = TREE_PURPOSE (t);
15264 if (purpose)
15265 {
15266 purpose = tsubst (purpose, args, complain, in_decl);
15267 if (purpose == error_mark_node)
15268 return error_mark_node;
15269 }
15270 value = TREE_VALUE (t);
15271 if (value)
15272 {
15273 value = tsubst (value, args, complain, in_decl);
15274 if (value == error_mark_node)
15275 return error_mark_node;
15276 }
15277 chain = TREE_CHAIN (t);
15278 if (chain && chain != void_type_node)
15279 {
15280 chain = tsubst (chain, args, complain, in_decl);
15281 if (chain == error_mark_node)
15282 return error_mark_node;
15283 }
15284 if (purpose == TREE_PURPOSE (t)
15285 && value == TREE_VALUE (t)
15286 && chain == TREE_CHAIN (t))
15287 return t;
15288 return hash_tree_cons (purpose, value, chain);
15289 }
15290
15291 case TREE_BINFO:
15292 /* We should never be tsubsting a binfo. */
15293 gcc_unreachable ();
15294
15295 case TREE_VEC:
15296 /* A vector of template arguments. */
15297 gcc_assert (!type);
15298 return tsubst_template_args (t, args, complain, in_decl);
15299
15300 case POINTER_TYPE:
15301 case REFERENCE_TYPE:
15302 {
15303 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15304 return t;
15305
15306 /* [temp.deduct]
15307
15308 Type deduction may fail for any of the following
15309 reasons:
15310
15311 -- Attempting to create a pointer to reference type.
15312 -- Attempting to create a reference to a reference type or
15313 a reference to void.
15314
15315 Core issue 106 says that creating a reference to a reference
15316 during instantiation is no longer a cause for failure. We
15317 only enforce this check in strict C++98 mode. */
15318 if ((TYPE_REF_P (type)
15319 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15320 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15321 {
15322 static location_t last_loc;
15323
15324 /* We keep track of the last time we issued this error
15325 message to avoid spewing a ton of messages during a
15326 single bad template instantiation. */
15327 if (complain & tf_error
15328 && last_loc != input_location)
15329 {
15330 if (VOID_TYPE_P (type))
15331 error ("forming reference to void");
15332 else if (code == POINTER_TYPE)
15333 error ("forming pointer to reference type %qT", type);
15334 else
15335 error ("forming reference to reference type %qT", type);
15336 last_loc = input_location;
15337 }
15338
15339 return error_mark_node;
15340 }
15341 else if (TREE_CODE (type) == FUNCTION_TYPE
15342 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15343 || type_memfn_rqual (type) != REF_QUAL_NONE))
15344 {
15345 if (complain & tf_error)
15346 {
15347 if (code == POINTER_TYPE)
15348 error ("forming pointer to qualified function type %qT",
15349 type);
15350 else
15351 error ("forming reference to qualified function type %qT",
15352 type);
15353 }
15354 return error_mark_node;
15355 }
15356 else if (code == POINTER_TYPE)
15357 {
15358 r = build_pointer_type (type);
15359 if (TREE_CODE (type) == METHOD_TYPE)
15360 r = build_ptrmemfunc_type (r);
15361 }
15362 else if (TYPE_REF_P (type))
15363 /* In C++0x, during template argument substitution, when there is an
15364 attempt to create a reference to a reference type, reference
15365 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15366
15367 "If a template-argument for a template-parameter T names a type
15368 that is a reference to a type A, an attempt to create the type
15369 'lvalue reference to cv T' creates the type 'lvalue reference to
15370 A,' while an attempt to create the type type rvalue reference to
15371 cv T' creates the type T"
15372 */
15373 r = cp_build_reference_type
15374 (TREE_TYPE (type),
15375 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15376 else
15377 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15378 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15379
15380 if (r != error_mark_node)
15381 /* Will this ever be needed for TYPE_..._TO values? */
15382 layout_type (r);
15383
15384 return r;
15385 }
15386 case OFFSET_TYPE:
15387 {
15388 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15389 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15390 {
15391 /* [temp.deduct]
15392
15393 Type deduction may fail for any of the following
15394 reasons:
15395
15396 -- Attempting to create "pointer to member of T" when T
15397 is not a class type. */
15398 if (complain & tf_error)
15399 error ("creating pointer to member of non-class type %qT", r);
15400 return error_mark_node;
15401 }
15402 if (TYPE_REF_P (type))
15403 {
15404 if (complain & tf_error)
15405 error ("creating pointer to member reference type %qT", type);
15406 return error_mark_node;
15407 }
15408 if (VOID_TYPE_P (type))
15409 {
15410 if (complain & tf_error)
15411 error ("creating pointer to member of type void");
15412 return error_mark_node;
15413 }
15414 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15415 if (TREE_CODE (type) == FUNCTION_TYPE)
15416 {
15417 /* The type of the implicit object parameter gets its
15418 cv-qualifiers from the FUNCTION_TYPE. */
15419 tree memptr;
15420 tree method_type
15421 = build_memfn_type (type, r, type_memfn_quals (type),
15422 type_memfn_rqual (type));
15423 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15424 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15425 complain);
15426 }
15427 else
15428 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15429 cp_type_quals (t),
15430 complain);
15431 }
15432 case FUNCTION_TYPE:
15433 case METHOD_TYPE:
15434 {
15435 tree fntype;
15436 tree specs;
15437 fntype = tsubst_function_type (t, args, complain, in_decl);
15438 if (fntype == error_mark_node)
15439 return error_mark_node;
15440
15441 /* Substitute the exception specification. */
15442 specs = tsubst_exception_specification (t, args, complain, in_decl,
15443 /*defer_ok*/fndecl_type);
15444 if (specs == error_mark_node)
15445 return error_mark_node;
15446 if (specs)
15447 fntype = build_exception_variant (fntype, specs);
15448 return fntype;
15449 }
15450 case ARRAY_TYPE:
15451 {
15452 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15453 if (domain == error_mark_node)
15454 return error_mark_node;
15455
15456 /* As an optimization, we avoid regenerating the array type if
15457 it will obviously be the same as T. */
15458 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15459 return t;
15460
15461 /* These checks should match the ones in create_array_type_for_decl.
15462
15463 [temp.deduct]
15464
15465 The deduction may fail for any of the following reasons:
15466
15467 -- Attempting to create an array with an element type that
15468 is void, a function type, or a reference type, or [DR337]
15469 an abstract class type. */
15470 if (VOID_TYPE_P (type)
15471 || TREE_CODE (type) == FUNCTION_TYPE
15472 || (TREE_CODE (type) == ARRAY_TYPE
15473 && TYPE_DOMAIN (type) == NULL_TREE)
15474 || TYPE_REF_P (type))
15475 {
15476 if (complain & tf_error)
15477 error ("creating array of %qT", type);
15478 return error_mark_node;
15479 }
15480
15481 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15482 return error_mark_node;
15483
15484 r = build_cplus_array_type (type, domain);
15485
15486 if (!valid_array_size_p (input_location, r, in_decl,
15487 (complain & tf_error)))
15488 return error_mark_node;
15489
15490 if (TYPE_USER_ALIGN (t))
15491 {
15492 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15493 TYPE_USER_ALIGN (r) = 1;
15494 }
15495
15496 return r;
15497 }
15498
15499 case TYPENAME_TYPE:
15500 {
15501 tree ctx = TYPE_CONTEXT (t);
15502 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15503 {
15504 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15505 if (ctx == error_mark_node
15506 || TREE_VEC_LENGTH (ctx) > 1)
15507 return error_mark_node;
15508 if (TREE_VEC_LENGTH (ctx) == 0)
15509 {
15510 if (complain & tf_error)
15511 error ("%qD is instantiated for an empty pack",
15512 TYPENAME_TYPE_FULLNAME (t));
15513 return error_mark_node;
15514 }
15515 ctx = TREE_VEC_ELT (ctx, 0);
15516 }
15517 else
15518 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15519 /*entering_scope=*/1);
15520 if (ctx == error_mark_node)
15521 return error_mark_node;
15522
15523 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15524 complain, in_decl);
15525 if (f == error_mark_node)
15526 return error_mark_node;
15527
15528 if (!MAYBE_CLASS_TYPE_P (ctx))
15529 {
15530 if (complain & tf_error)
15531 error ("%qT is not a class, struct, or union type", ctx);
15532 return error_mark_node;
15533 }
15534 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15535 {
15536 /* Normally, make_typename_type does not require that the CTX
15537 have complete type in order to allow things like:
15538
15539 template <class T> struct S { typename S<T>::X Y; };
15540
15541 But, such constructs have already been resolved by this
15542 point, so here CTX really should have complete type, unless
15543 it's a partial instantiation. */
15544 ctx = complete_type (ctx);
15545 if (!COMPLETE_TYPE_P (ctx))
15546 {
15547 if (complain & tf_error)
15548 cxx_incomplete_type_error (NULL_TREE, ctx);
15549 return error_mark_node;
15550 }
15551 }
15552
15553 f = make_typename_type (ctx, f, typename_type,
15554 complain | tf_keep_type_decl);
15555 if (f == error_mark_node)
15556 return f;
15557 if (TREE_CODE (f) == TYPE_DECL)
15558 {
15559 complain |= tf_ignore_bad_quals;
15560 f = TREE_TYPE (f);
15561 }
15562
15563 if (TREE_CODE (f) != TYPENAME_TYPE)
15564 {
15565 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15566 {
15567 if (complain & tf_error)
15568 error ("%qT resolves to %qT, which is not an enumeration type",
15569 t, f);
15570 else
15571 return error_mark_node;
15572 }
15573 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15574 {
15575 if (complain & tf_error)
15576 error ("%qT resolves to %qT, which is is not a class type",
15577 t, f);
15578 else
15579 return error_mark_node;
15580 }
15581 }
15582
15583 return cp_build_qualified_type_real
15584 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15585 }
15586
15587 case UNBOUND_CLASS_TEMPLATE:
15588 {
15589 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15590 in_decl, /*entering_scope=*/1);
15591 tree name = TYPE_IDENTIFIER (t);
15592 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15593
15594 if (ctx == error_mark_node || name == error_mark_node)
15595 return error_mark_node;
15596
15597 if (parm_list)
15598 parm_list = tsubst_template_parms (parm_list, args, complain);
15599 return make_unbound_class_template (ctx, name, parm_list, complain);
15600 }
15601
15602 case TYPEOF_TYPE:
15603 {
15604 tree type;
15605
15606 ++cp_unevaluated_operand;
15607 ++c_inhibit_evaluation_warnings;
15608
15609 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15610 complain, in_decl,
15611 /*integral_constant_expression_p=*/false);
15612
15613 --cp_unevaluated_operand;
15614 --c_inhibit_evaluation_warnings;
15615
15616 type = finish_typeof (type);
15617 return cp_build_qualified_type_real (type,
15618 cp_type_quals (t)
15619 | cp_type_quals (type),
15620 complain);
15621 }
15622
15623 case DECLTYPE_TYPE:
15624 {
15625 tree type;
15626
15627 ++cp_unevaluated_operand;
15628 ++c_inhibit_evaluation_warnings;
15629
15630 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15631 complain|tf_decltype, in_decl,
15632 /*function_p*/false,
15633 /*integral_constant_expression*/false);
15634
15635 --cp_unevaluated_operand;
15636 --c_inhibit_evaluation_warnings;
15637
15638 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15639 type = lambda_capture_field_type (type,
15640 false /*explicit_init*/,
15641 DECLTYPE_FOR_REF_CAPTURE (t));
15642 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15643 type = lambda_proxy_type (type);
15644 else
15645 {
15646 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15647 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15648 && EXPR_P (type))
15649 /* In a template ~id could be either a complement expression
15650 or an unqualified-id naming a destructor; if instantiating
15651 it produces an expression, it's not an id-expression or
15652 member access. */
15653 id = false;
15654 type = finish_decltype_type (type, id, complain);
15655 }
15656 return cp_build_qualified_type_real (type,
15657 cp_type_quals (t)
15658 | cp_type_quals (type),
15659 complain | tf_ignore_bad_quals);
15660 }
15661
15662 case UNDERLYING_TYPE:
15663 {
15664 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15665 complain, in_decl);
15666 return finish_underlying_type (type);
15667 }
15668
15669 case TYPE_ARGUMENT_PACK:
15670 case NONTYPE_ARGUMENT_PACK:
15671 {
15672 tree r;
15673
15674 if (code == NONTYPE_ARGUMENT_PACK)
15675 r = make_node (code);
15676 else
15677 r = cxx_make_type (code);
15678
15679 tree pack_args = ARGUMENT_PACK_ARGS (t);
15680 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15681 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15682
15683 return r;
15684 }
15685
15686 case VOID_CST:
15687 case INTEGER_CST:
15688 case REAL_CST:
15689 case STRING_CST:
15690 case PLUS_EXPR:
15691 case MINUS_EXPR:
15692 case NEGATE_EXPR:
15693 case NOP_EXPR:
15694 case INDIRECT_REF:
15695 case ADDR_EXPR:
15696 case CALL_EXPR:
15697 case ARRAY_REF:
15698 case SCOPE_REF:
15699 /* We should use one of the expression tsubsts for these codes. */
15700 gcc_unreachable ();
15701
15702 default:
15703 sorry ("use of %qs in template", get_tree_code_name (code));
15704 return error_mark_node;
15705 }
15706 }
15707
15708 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15709 expression on the left-hand side of the "." or "->" operator. We
15710 only do the lookup if we had a dependent BASELINK. Otherwise we
15711 adjust it onto the instantiated heirarchy. */
15712
15713 static tree
15714 tsubst_baselink (tree baselink, tree object_type,
15715 tree args, tsubst_flags_t complain, tree in_decl)
15716 {
15717 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15718 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15719 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15720
15721 tree optype = BASELINK_OPTYPE (baselink);
15722 optype = tsubst (optype, args, complain, in_decl);
15723
15724 tree template_args = NULL_TREE;
15725 bool template_id_p = false;
15726 tree fns = BASELINK_FUNCTIONS (baselink);
15727 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15728 {
15729 template_id_p = true;
15730 template_args = TREE_OPERAND (fns, 1);
15731 fns = TREE_OPERAND (fns, 0);
15732 if (template_args)
15733 template_args = tsubst_template_args (template_args, args,
15734 complain, in_decl);
15735 }
15736
15737 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15738 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15739 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15740
15741 if (dependent_p)
15742 {
15743 tree name = OVL_NAME (fns);
15744 if (IDENTIFIER_CONV_OP_P (name))
15745 name = make_conv_op_name (optype);
15746
15747 if (name == complete_dtor_identifier)
15748 /* Treat as-if non-dependent below. */
15749 dependent_p = false;
15750
15751 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15752 if (!baselink)
15753 {
15754 if ((complain & tf_error)
15755 && constructor_name_p (name, qualifying_scope))
15756 error ("cannot call constructor %<%T::%D%> directly",
15757 qualifying_scope, name);
15758 return error_mark_node;
15759 }
15760
15761 if (BASELINK_P (baselink))
15762 fns = BASELINK_FUNCTIONS (baselink);
15763 }
15764 else
15765 /* We're going to overwrite pieces below, make a duplicate. */
15766 baselink = copy_node (baselink);
15767
15768 /* If lookup found a single function, mark it as used at this point.
15769 (If lookup found multiple functions the one selected later by
15770 overload resolution will be marked as used at that point.) */
15771 if (!template_id_p && !really_overloaded_fn (fns))
15772 {
15773 tree fn = OVL_FIRST (fns);
15774 bool ok = mark_used (fn, complain);
15775 if (!ok && !(complain & tf_error))
15776 return error_mark_node;
15777 if (ok && BASELINK_P (baselink))
15778 /* We might have instantiated an auto function. */
15779 TREE_TYPE (baselink) = TREE_TYPE (fn);
15780 }
15781
15782 if (BASELINK_P (baselink))
15783 {
15784 /* Add back the template arguments, if present. */
15785 if (template_id_p)
15786 BASELINK_FUNCTIONS (baselink)
15787 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15788
15789 /* Update the conversion operator type. */
15790 BASELINK_OPTYPE (baselink) = optype;
15791 }
15792
15793 if (!object_type)
15794 object_type = current_class_type;
15795
15796 if (qualified_p || !dependent_p)
15797 {
15798 baselink = adjust_result_of_qualified_name_lookup (baselink,
15799 qualifying_scope,
15800 object_type);
15801 if (!qualified_p)
15802 /* We need to call adjust_result_of_qualified_name_lookup in case the
15803 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15804 so that we still get virtual function binding. */
15805 BASELINK_QUALIFIED_P (baselink) = false;
15806 }
15807
15808 return baselink;
15809 }
15810
15811 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15812 true if the qualified-id will be a postfix-expression in-and-of
15813 itself; false if more of the postfix-expression follows the
15814 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15815 of "&". */
15816
15817 static tree
15818 tsubst_qualified_id (tree qualified_id, tree args,
15819 tsubst_flags_t complain, tree in_decl,
15820 bool done, bool address_p)
15821 {
15822 tree expr;
15823 tree scope;
15824 tree name;
15825 bool is_template;
15826 tree template_args;
15827 location_t loc = UNKNOWN_LOCATION;
15828
15829 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15830
15831 /* Figure out what name to look up. */
15832 name = TREE_OPERAND (qualified_id, 1);
15833 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15834 {
15835 is_template = true;
15836 loc = EXPR_LOCATION (name);
15837 template_args = TREE_OPERAND (name, 1);
15838 if (template_args)
15839 template_args = tsubst_template_args (template_args, args,
15840 complain, in_decl);
15841 if (template_args == error_mark_node)
15842 return error_mark_node;
15843 name = TREE_OPERAND (name, 0);
15844 }
15845 else
15846 {
15847 is_template = false;
15848 template_args = NULL_TREE;
15849 }
15850
15851 /* Substitute into the qualifying scope. When there are no ARGS, we
15852 are just trying to simplify a non-dependent expression. In that
15853 case the qualifying scope may be dependent, and, in any case,
15854 substituting will not help. */
15855 scope = TREE_OPERAND (qualified_id, 0);
15856 if (args)
15857 {
15858 scope = tsubst (scope, args, complain, in_decl);
15859 expr = tsubst_copy (name, args, complain, in_decl);
15860 }
15861 else
15862 expr = name;
15863
15864 if (dependent_scope_p (scope))
15865 {
15866 if (is_template)
15867 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15868 tree r = build_qualified_name (NULL_TREE, scope, expr,
15869 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15870 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15871 return r;
15872 }
15873
15874 if (!BASELINK_P (name) && !DECL_P (expr))
15875 {
15876 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15877 {
15878 /* A BIT_NOT_EXPR is used to represent a destructor. */
15879 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15880 {
15881 error ("qualifying type %qT does not match destructor name ~%qT",
15882 scope, TREE_OPERAND (expr, 0));
15883 expr = error_mark_node;
15884 }
15885 else
15886 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15887 /*is_type_p=*/0, false);
15888 }
15889 else
15890 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15891 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15892 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15893 {
15894 if (complain & tf_error)
15895 {
15896 error ("dependent-name %qE is parsed as a non-type, but "
15897 "instantiation yields a type", qualified_id);
15898 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15899 }
15900 return error_mark_node;
15901 }
15902 }
15903
15904 if (DECL_P (expr))
15905 {
15906 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15907 scope);
15908 /* Remember that there was a reference to this entity. */
15909 if (!mark_used (expr, complain) && !(complain & tf_error))
15910 return error_mark_node;
15911 }
15912
15913 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15914 {
15915 if (complain & tf_error)
15916 qualified_name_lookup_error (scope,
15917 TREE_OPERAND (qualified_id, 1),
15918 expr, input_location);
15919 return error_mark_node;
15920 }
15921
15922 if (is_template)
15923 {
15924 /* We may be repeating a check already done during parsing, but
15925 if it was well-formed and passed then, it will pass again
15926 now, and if it didn't, we wouldn't have got here. The case
15927 we want to catch is when we couldn't tell then, and can now,
15928 namely when templ prior to substitution was an
15929 identifier. */
15930 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15931 return error_mark_node;
15932
15933 if (variable_template_p (expr))
15934 expr = lookup_and_finish_template_variable (expr, template_args,
15935 complain);
15936 else
15937 expr = lookup_template_function (expr, template_args);
15938 }
15939
15940 if (expr == error_mark_node && complain & tf_error)
15941 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15942 expr, input_location);
15943 else if (TYPE_P (scope))
15944 {
15945 expr = (adjust_result_of_qualified_name_lookup
15946 (expr, scope, current_nonlambda_class_type ()));
15947 expr = (finish_qualified_id_expr
15948 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15949 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15950 /*template_arg_p=*/false, complain));
15951 }
15952
15953 /* Expressions do not generally have reference type. */
15954 if (TREE_CODE (expr) != SCOPE_REF
15955 /* However, if we're about to form a pointer-to-member, we just
15956 want the referenced member referenced. */
15957 && TREE_CODE (expr) != OFFSET_REF)
15958 expr = convert_from_reference (expr);
15959
15960 if (REF_PARENTHESIZED_P (qualified_id))
15961 expr = force_paren_expr (expr);
15962
15963 return expr;
15964 }
15965
15966 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15967 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15968 for tsubst. */
15969
15970 static tree
15971 tsubst_init (tree init, tree decl, tree args,
15972 tsubst_flags_t complain, tree in_decl)
15973 {
15974 if (!init)
15975 return NULL_TREE;
15976
15977 init = tsubst_expr (init, args, complain, in_decl, false);
15978
15979 tree type = TREE_TYPE (decl);
15980
15981 if (!init && type != error_mark_node)
15982 {
15983 if (tree auto_node = type_uses_auto (type))
15984 {
15985 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15986 {
15987 if (complain & tf_error)
15988 error ("initializer for %q#D expands to an empty list "
15989 "of expressions", decl);
15990 return error_mark_node;
15991 }
15992 }
15993 else if (!dependent_type_p (type))
15994 {
15995 /* If we had an initializer but it
15996 instantiated to nothing,
15997 value-initialize the object. This will
15998 only occur when the initializer was a
15999 pack expansion where the parameter packs
16000 used in that expansion were of length
16001 zero. */
16002 init = build_value_init (type, complain);
16003 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16004 init = get_target_expr_sfinae (init, complain);
16005 if (TREE_CODE (init) == TARGET_EXPR)
16006 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16007 }
16008 }
16009
16010 return init;
16011 }
16012
16013 /* Like tsubst, but deals with expressions. This function just replaces
16014 template parms; to finish processing the resultant expression, use
16015 tsubst_copy_and_build or tsubst_expr. */
16016
16017 static tree
16018 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16019 {
16020 enum tree_code code;
16021 tree r;
16022
16023 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16024 return t;
16025
16026 code = TREE_CODE (t);
16027
16028 switch (code)
16029 {
16030 case PARM_DECL:
16031 r = retrieve_local_specialization (t);
16032
16033 if (r == NULL_TREE)
16034 {
16035 /* We get here for a use of 'this' in an NSDMI. */
16036 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16037 return current_class_ptr;
16038
16039 /* This can happen for a parameter name used later in a function
16040 declaration (such as in a late-specified return type). Just
16041 make a dummy decl, since it's only used for its type. */
16042 gcc_assert (cp_unevaluated_operand != 0);
16043 r = tsubst_decl (t, args, complain);
16044 /* Give it the template pattern as its context; its true context
16045 hasn't been instantiated yet and this is good enough for
16046 mangling. */
16047 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16048 }
16049
16050 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16051 r = argument_pack_select_arg (r);
16052 if (!mark_used (r, complain) && !(complain & tf_error))
16053 return error_mark_node;
16054 return r;
16055
16056 case CONST_DECL:
16057 {
16058 tree enum_type;
16059 tree v;
16060
16061 if (DECL_TEMPLATE_PARM_P (t))
16062 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16063 /* There is no need to substitute into namespace-scope
16064 enumerators. */
16065 if (DECL_NAMESPACE_SCOPE_P (t))
16066 return t;
16067 /* If ARGS is NULL, then T is known to be non-dependent. */
16068 if (args == NULL_TREE)
16069 return scalar_constant_value (t);
16070
16071 /* Unfortunately, we cannot just call lookup_name here.
16072 Consider:
16073
16074 template <int I> int f() {
16075 enum E { a = I };
16076 struct S { void g() { E e = a; } };
16077 };
16078
16079 When we instantiate f<7>::S::g(), say, lookup_name is not
16080 clever enough to find f<7>::a. */
16081 enum_type
16082 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16083 /*entering_scope=*/0);
16084
16085 for (v = TYPE_VALUES (enum_type);
16086 v != NULL_TREE;
16087 v = TREE_CHAIN (v))
16088 if (TREE_PURPOSE (v) == DECL_NAME (t))
16089 return TREE_VALUE (v);
16090
16091 /* We didn't find the name. That should never happen; if
16092 name-lookup found it during preliminary parsing, we
16093 should find it again here during instantiation. */
16094 gcc_unreachable ();
16095 }
16096 return t;
16097
16098 case FIELD_DECL:
16099 if (DECL_CONTEXT (t))
16100 {
16101 tree ctx;
16102
16103 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16104 /*entering_scope=*/1);
16105 if (ctx != DECL_CONTEXT (t))
16106 {
16107 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16108 if (!r)
16109 {
16110 if (complain & tf_error)
16111 error ("using invalid field %qD", t);
16112 return error_mark_node;
16113 }
16114 return r;
16115 }
16116 }
16117
16118 return t;
16119
16120 case VAR_DECL:
16121 case FUNCTION_DECL:
16122 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16123 r = tsubst (t, args, complain, in_decl);
16124 else if (local_variable_p (t)
16125 && uses_template_parms (DECL_CONTEXT (t)))
16126 {
16127 r = retrieve_local_specialization (t);
16128 if (r == NULL_TREE)
16129 {
16130 /* First try name lookup to find the instantiation. */
16131 r = lookup_name (DECL_NAME (t));
16132 if (r)
16133 {
16134 if (!VAR_P (r))
16135 {
16136 /* During error-recovery we may find a non-variable,
16137 even an OVERLOAD: just bail out and avoid ICEs and
16138 duplicate diagnostics (c++/62207). */
16139 gcc_assert (seen_error ());
16140 return error_mark_node;
16141 }
16142 if (!is_capture_proxy (r))
16143 {
16144 /* Make sure the one we found is the one we want. */
16145 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16146 if (ctx != DECL_CONTEXT (r))
16147 r = NULL_TREE;
16148 }
16149 }
16150
16151 if (r)
16152 /* OK */;
16153 else
16154 {
16155 /* This can happen for a variable used in a
16156 late-specified return type of a local lambda, or for a
16157 local static or constant. Building a new VAR_DECL
16158 should be OK in all those cases. */
16159 r = tsubst_decl (t, args, complain);
16160 if (local_specializations)
16161 /* Avoid infinite recursion (79640). */
16162 register_local_specialization (r, t);
16163 if (decl_maybe_constant_var_p (r))
16164 {
16165 /* We can't call cp_finish_decl, so handle the
16166 initializer by hand. */
16167 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16168 complain, in_decl);
16169 if (!processing_template_decl)
16170 init = maybe_constant_init (init);
16171 if (processing_template_decl
16172 ? potential_constant_expression (init)
16173 : reduced_constant_expression_p (init))
16174 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16175 = TREE_CONSTANT (r) = true;
16176 DECL_INITIAL (r) = init;
16177 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16178 TREE_TYPE (r)
16179 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16180 complain, adc_variable_type);
16181 }
16182 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16183 || decl_constant_var_p (r)
16184 || seen_error ());
16185 if (!processing_template_decl
16186 && !TREE_STATIC (r))
16187 r = process_outer_var_ref (r, complain);
16188 }
16189 /* Remember this for subsequent uses. */
16190 if (local_specializations)
16191 register_local_specialization (r, t);
16192 }
16193 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16194 r = argument_pack_select_arg (r);
16195 }
16196 else
16197 r = t;
16198 if (!mark_used (r, complain))
16199 return error_mark_node;
16200 return r;
16201
16202 case NAMESPACE_DECL:
16203 return t;
16204
16205 case OVERLOAD:
16206 return t;
16207
16208 case BASELINK:
16209 return tsubst_baselink (t, current_nonlambda_class_type (),
16210 args, complain, in_decl);
16211
16212 case TEMPLATE_DECL:
16213 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16214 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16215 args, complain, in_decl);
16216 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16217 return tsubst (t, args, complain, in_decl);
16218 else if (DECL_CLASS_SCOPE_P (t)
16219 && uses_template_parms (DECL_CONTEXT (t)))
16220 {
16221 /* Template template argument like the following example need
16222 special treatment:
16223
16224 template <template <class> class TT> struct C {};
16225 template <class T> struct D {
16226 template <class U> struct E {};
16227 C<E> c; // #1
16228 };
16229 D<int> d; // #2
16230
16231 We are processing the template argument `E' in #1 for
16232 the template instantiation #2. Originally, `E' is a
16233 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16234 have to substitute this with one having context `D<int>'. */
16235
16236 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16237 if (dependent_scope_p (context))
16238 {
16239 /* When rewriting a constructor into a deduction guide, a
16240 non-dependent name can become dependent, so memtmpl<args>
16241 becomes context::template memtmpl<args>. */
16242 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16243 return build_qualified_name (type, context, DECL_NAME (t),
16244 /*template*/true);
16245 }
16246 return lookup_field (context, DECL_NAME(t), 0, false);
16247 }
16248 else
16249 /* Ordinary template template argument. */
16250 return t;
16251
16252 case NON_LVALUE_EXPR:
16253 case VIEW_CONVERT_EXPR:
16254 {
16255 /* Handle location wrappers by substituting the wrapped node
16256 first, *then* reusing the resulting type. Doing the type
16257 first ensures that we handle template parameters and
16258 parameter pack expansions. */
16259 if (location_wrapper_p (t))
16260 {
16261 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16262 complain, in_decl);
16263 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16264 }
16265 tree op = TREE_OPERAND (t, 0);
16266 if (code == VIEW_CONVERT_EXPR
16267 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16268 {
16269 /* Wrapper to make a C++20 template parameter object const. */
16270 op = tsubst_copy (op, args, complain, in_decl);
16271 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16272 {
16273 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16274 return build1 (code, type, op);
16275 }
16276 else
16277 {
16278 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
16279 return op;
16280 }
16281 }
16282 /* We shouldn't see any other uses of these in templates. */
16283 gcc_unreachable ();
16284 }
16285
16286 case CAST_EXPR:
16287 case REINTERPRET_CAST_EXPR:
16288 case CONST_CAST_EXPR:
16289 case STATIC_CAST_EXPR:
16290 case DYNAMIC_CAST_EXPR:
16291 case IMPLICIT_CONV_EXPR:
16292 case CONVERT_EXPR:
16293 case NOP_EXPR:
16294 {
16295 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16296 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16297 return build1 (code, type, op0);
16298 }
16299
16300 case SIZEOF_EXPR:
16301 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16302 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16303 {
16304 tree expanded, op = TREE_OPERAND (t, 0);
16305 int len = 0;
16306
16307 if (SIZEOF_EXPR_TYPE_P (t))
16308 op = TREE_TYPE (op);
16309
16310 ++cp_unevaluated_operand;
16311 ++c_inhibit_evaluation_warnings;
16312 /* We only want to compute the number of arguments. */
16313 if (PACK_EXPANSION_P (op))
16314 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16315 else
16316 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16317 args, complain, in_decl);
16318 --cp_unevaluated_operand;
16319 --c_inhibit_evaluation_warnings;
16320
16321 if (TREE_CODE (expanded) == TREE_VEC)
16322 {
16323 len = TREE_VEC_LENGTH (expanded);
16324 /* Set TREE_USED for the benefit of -Wunused. */
16325 for (int i = 0; i < len; i++)
16326 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16327 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16328 }
16329
16330 if (expanded == error_mark_node)
16331 return error_mark_node;
16332 else if (PACK_EXPANSION_P (expanded)
16333 || (TREE_CODE (expanded) == TREE_VEC
16334 && pack_expansion_args_count (expanded)))
16335
16336 {
16337 if (PACK_EXPANSION_P (expanded))
16338 /* OK. */;
16339 else if (TREE_VEC_LENGTH (expanded) == 1)
16340 expanded = TREE_VEC_ELT (expanded, 0);
16341 else
16342 expanded = make_argument_pack (expanded);
16343
16344 if (TYPE_P (expanded))
16345 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
16346 false,
16347 complain & tf_error);
16348 else
16349 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
16350 complain & tf_error);
16351 }
16352 else
16353 return build_int_cst (size_type_node, len);
16354 }
16355 if (SIZEOF_EXPR_TYPE_P (t))
16356 {
16357 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16358 args, complain, in_decl);
16359 r = build1 (NOP_EXPR, r, error_mark_node);
16360 r = build1 (SIZEOF_EXPR,
16361 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16362 SIZEOF_EXPR_TYPE_P (r) = 1;
16363 return r;
16364 }
16365 /* Fall through */
16366
16367 case INDIRECT_REF:
16368 case NEGATE_EXPR:
16369 case TRUTH_NOT_EXPR:
16370 case BIT_NOT_EXPR:
16371 case ADDR_EXPR:
16372 case UNARY_PLUS_EXPR: /* Unary + */
16373 case ALIGNOF_EXPR:
16374 case AT_ENCODE_EXPR:
16375 case ARROW_EXPR:
16376 case THROW_EXPR:
16377 case TYPEID_EXPR:
16378 case REALPART_EXPR:
16379 case IMAGPART_EXPR:
16380 case PAREN_EXPR:
16381 {
16382 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16383 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16384 r = build1 (code, type, op0);
16385 if (code == ALIGNOF_EXPR)
16386 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16387 return r;
16388 }
16389
16390 case COMPONENT_REF:
16391 {
16392 tree object;
16393 tree name;
16394
16395 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16396 name = TREE_OPERAND (t, 1);
16397 if (TREE_CODE (name) == BIT_NOT_EXPR)
16398 {
16399 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16400 complain, in_decl);
16401 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16402 }
16403 else if (TREE_CODE (name) == SCOPE_REF
16404 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16405 {
16406 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16407 complain, in_decl);
16408 name = TREE_OPERAND (name, 1);
16409 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16410 complain, in_decl);
16411 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16412 name = build_qualified_name (/*type=*/NULL_TREE,
16413 base, name,
16414 /*template_p=*/false);
16415 }
16416 else if (BASELINK_P (name))
16417 name = tsubst_baselink (name,
16418 non_reference (TREE_TYPE (object)),
16419 args, complain,
16420 in_decl);
16421 else
16422 name = tsubst_copy (name, args, complain, in_decl);
16423 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16424 }
16425
16426 case PLUS_EXPR:
16427 case MINUS_EXPR:
16428 case MULT_EXPR:
16429 case TRUNC_DIV_EXPR:
16430 case CEIL_DIV_EXPR:
16431 case FLOOR_DIV_EXPR:
16432 case ROUND_DIV_EXPR:
16433 case EXACT_DIV_EXPR:
16434 case BIT_AND_EXPR:
16435 case BIT_IOR_EXPR:
16436 case BIT_XOR_EXPR:
16437 case TRUNC_MOD_EXPR:
16438 case FLOOR_MOD_EXPR:
16439 case TRUTH_ANDIF_EXPR:
16440 case TRUTH_ORIF_EXPR:
16441 case TRUTH_AND_EXPR:
16442 case TRUTH_OR_EXPR:
16443 case RSHIFT_EXPR:
16444 case LSHIFT_EXPR:
16445 case EQ_EXPR:
16446 case NE_EXPR:
16447 case MAX_EXPR:
16448 case MIN_EXPR:
16449 case LE_EXPR:
16450 case GE_EXPR:
16451 case LT_EXPR:
16452 case GT_EXPR:
16453 case COMPOUND_EXPR:
16454 case DOTSTAR_EXPR:
16455 case MEMBER_REF:
16456 case PREDECREMENT_EXPR:
16457 case PREINCREMENT_EXPR:
16458 case POSTDECREMENT_EXPR:
16459 case POSTINCREMENT_EXPR:
16460 {
16461 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16462 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16463 return build_nt (code, op0, op1);
16464 }
16465
16466 case SCOPE_REF:
16467 {
16468 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16469 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16470 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16471 QUALIFIED_NAME_IS_TEMPLATE (t));
16472 }
16473
16474 case ARRAY_REF:
16475 {
16476 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16477 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16478 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16479 }
16480
16481 case CALL_EXPR:
16482 {
16483 int n = VL_EXP_OPERAND_LENGTH (t);
16484 tree result = build_vl_exp (CALL_EXPR, n);
16485 int i;
16486 for (i = 0; i < n; i++)
16487 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16488 complain, in_decl);
16489 return result;
16490 }
16491
16492 case COND_EXPR:
16493 case MODOP_EXPR:
16494 case PSEUDO_DTOR_EXPR:
16495 case VEC_PERM_EXPR:
16496 {
16497 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16498 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16499 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16500 r = build_nt (code, op0, op1, op2);
16501 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16502 return r;
16503 }
16504
16505 case NEW_EXPR:
16506 {
16507 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16508 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16509 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16510 r = build_nt (code, op0, op1, op2);
16511 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16512 return r;
16513 }
16514
16515 case DELETE_EXPR:
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 r = build_nt (code, op0, op1);
16520 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16521 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16522 return r;
16523 }
16524
16525 case TEMPLATE_ID_EXPR:
16526 {
16527 /* Substituted template arguments */
16528 tree fn = TREE_OPERAND (t, 0);
16529 tree targs = TREE_OPERAND (t, 1);
16530
16531 fn = tsubst_copy (fn, args, complain, in_decl);
16532 if (targs)
16533 targs = tsubst_template_args (targs, args, complain, in_decl);
16534
16535 return lookup_template_function (fn, targs);
16536 }
16537
16538 case TREE_LIST:
16539 {
16540 tree purpose, value, chain;
16541
16542 if (t == void_list_node)
16543 return t;
16544
16545 purpose = TREE_PURPOSE (t);
16546 if (purpose)
16547 purpose = tsubst_copy (purpose, args, complain, in_decl);
16548 value = TREE_VALUE (t);
16549 if (value)
16550 value = tsubst_copy (value, args, complain, in_decl);
16551 chain = TREE_CHAIN (t);
16552 if (chain && chain != void_type_node)
16553 chain = tsubst_copy (chain, args, complain, in_decl);
16554 if (purpose == TREE_PURPOSE (t)
16555 && value == TREE_VALUE (t)
16556 && chain == TREE_CHAIN (t))
16557 return t;
16558 return tree_cons (purpose, value, chain);
16559 }
16560
16561 case RECORD_TYPE:
16562 case UNION_TYPE:
16563 case ENUMERAL_TYPE:
16564 case INTEGER_TYPE:
16565 case TEMPLATE_TYPE_PARM:
16566 case TEMPLATE_TEMPLATE_PARM:
16567 case BOUND_TEMPLATE_TEMPLATE_PARM:
16568 case TEMPLATE_PARM_INDEX:
16569 case POINTER_TYPE:
16570 case REFERENCE_TYPE:
16571 case OFFSET_TYPE:
16572 case FUNCTION_TYPE:
16573 case METHOD_TYPE:
16574 case ARRAY_TYPE:
16575 case TYPENAME_TYPE:
16576 case UNBOUND_CLASS_TEMPLATE:
16577 case TYPEOF_TYPE:
16578 case DECLTYPE_TYPE:
16579 case TYPE_DECL:
16580 return tsubst (t, args, complain, in_decl);
16581
16582 case USING_DECL:
16583 t = DECL_NAME (t);
16584 /* Fall through. */
16585 case IDENTIFIER_NODE:
16586 if (IDENTIFIER_CONV_OP_P (t))
16587 {
16588 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16589 return make_conv_op_name (new_type);
16590 }
16591 else
16592 return t;
16593
16594 case CONSTRUCTOR:
16595 /* This is handled by tsubst_copy_and_build. */
16596 gcc_unreachable ();
16597
16598 case VA_ARG_EXPR:
16599 {
16600 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16601 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16602 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16603 }
16604
16605 case CLEANUP_POINT_EXPR:
16606 /* We shouldn't have built any of these during initial template
16607 generation. Instead, they should be built during instantiation
16608 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16609 gcc_unreachable ();
16610
16611 case OFFSET_REF:
16612 {
16613 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16614 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16615 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16616 r = build2 (code, type, op0, op1);
16617 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16618 if (!mark_used (TREE_OPERAND (r, 1), complain)
16619 && !(complain & tf_error))
16620 return error_mark_node;
16621 return r;
16622 }
16623
16624 case EXPR_PACK_EXPANSION:
16625 error ("invalid use of pack expansion expression");
16626 return error_mark_node;
16627
16628 case NONTYPE_ARGUMENT_PACK:
16629 error ("use %<...%> to expand argument pack");
16630 return error_mark_node;
16631
16632 case VOID_CST:
16633 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16634 return t;
16635
16636 case INTEGER_CST:
16637 case REAL_CST:
16638 case STRING_CST:
16639 case COMPLEX_CST:
16640 {
16641 /* Instantiate any typedefs in the type. */
16642 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16643 r = fold_convert (type, t);
16644 gcc_assert (TREE_CODE (r) == code);
16645 return r;
16646 }
16647
16648 case PTRMEM_CST:
16649 /* These can sometimes show up in a partial instantiation, but never
16650 involve template parms. */
16651 gcc_assert (!uses_template_parms (t));
16652 return t;
16653
16654 case UNARY_LEFT_FOLD_EXPR:
16655 return tsubst_unary_left_fold (t, args, complain, in_decl);
16656 case UNARY_RIGHT_FOLD_EXPR:
16657 return tsubst_unary_right_fold (t, args, complain, in_decl);
16658 case BINARY_LEFT_FOLD_EXPR:
16659 return tsubst_binary_left_fold (t, args, complain, in_decl);
16660 case BINARY_RIGHT_FOLD_EXPR:
16661 return tsubst_binary_right_fold (t, args, complain, in_decl);
16662 case PREDICT_EXPR:
16663 return t;
16664
16665 case DEBUG_BEGIN_STMT:
16666 /* ??? There's no point in copying it for now, but maybe some
16667 day it will contain more information, such as a pointer back
16668 to the containing function, inlined copy or so. */
16669 return t;
16670
16671 default:
16672 /* We shouldn't get here, but keep going if !flag_checking. */
16673 if (flag_checking)
16674 gcc_unreachable ();
16675 return t;
16676 }
16677 }
16678
16679 /* Helper function for tsubst_omp_clauses, used for instantiation of
16680 OMP_CLAUSE_DECL of clauses. */
16681
16682 static tree
16683 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16684 tree in_decl, tree *iterator_cache)
16685 {
16686 if (decl == NULL_TREE)
16687 return NULL_TREE;
16688
16689 /* Handle OpenMP iterators. */
16690 if (TREE_CODE (decl) == TREE_LIST
16691 && TREE_PURPOSE (decl)
16692 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16693 {
16694 tree ret;
16695 if (iterator_cache[0] == TREE_PURPOSE (decl))
16696 ret = iterator_cache[1];
16697 else
16698 {
16699 tree *tp = &ret;
16700 begin_scope (sk_omp, NULL);
16701 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16702 {
16703 *tp = copy_node (it);
16704 TREE_VEC_ELT (*tp, 0)
16705 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16706 TREE_VEC_ELT (*tp, 1)
16707 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16708 /*integral_constant_expression_p=*/false);
16709 TREE_VEC_ELT (*tp, 2)
16710 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16711 /*integral_constant_expression_p=*/false);
16712 TREE_VEC_ELT (*tp, 3)
16713 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16714 /*integral_constant_expression_p=*/false);
16715 TREE_CHAIN (*tp) = NULL_TREE;
16716 tp = &TREE_CHAIN (*tp);
16717 }
16718 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16719 iterator_cache[0] = TREE_PURPOSE (decl);
16720 iterator_cache[1] = ret;
16721 }
16722 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16723 args, complain,
16724 in_decl, NULL));
16725 }
16726
16727 /* Handle an OpenMP array section represented as a TREE_LIST (or
16728 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16729 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16730 TREE_LIST. We can handle it exactly the same as an array section
16731 (purpose, value, and a chain), even though the nomenclature
16732 (low_bound, length, etc) is different. */
16733 if (TREE_CODE (decl) == TREE_LIST)
16734 {
16735 tree low_bound
16736 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16737 /*integral_constant_expression_p=*/false);
16738 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16739 /*integral_constant_expression_p=*/false);
16740 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16741 in_decl, NULL);
16742 if (TREE_PURPOSE (decl) == low_bound
16743 && TREE_VALUE (decl) == length
16744 && TREE_CHAIN (decl) == chain)
16745 return decl;
16746 tree ret = tree_cons (low_bound, length, chain);
16747 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16748 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16749 return ret;
16750 }
16751 tree ret = tsubst_expr (decl, args, complain, in_decl,
16752 /*integral_constant_expression_p=*/false);
16753 /* Undo convert_from_reference tsubst_expr could have called. */
16754 if (decl
16755 && REFERENCE_REF_P (ret)
16756 && !REFERENCE_REF_P (decl))
16757 ret = TREE_OPERAND (ret, 0);
16758 return ret;
16759 }
16760
16761 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16762
16763 static tree
16764 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16765 tree args, tsubst_flags_t complain, tree in_decl)
16766 {
16767 tree new_clauses = NULL_TREE, nc, oc;
16768 tree linear_no_step = NULL_TREE;
16769 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16770
16771 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16772 {
16773 nc = copy_node (oc);
16774 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16775 new_clauses = nc;
16776
16777 switch (OMP_CLAUSE_CODE (nc))
16778 {
16779 case OMP_CLAUSE_LASTPRIVATE:
16780 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16781 {
16782 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16783 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16784 in_decl, /*integral_constant_expression_p=*/false);
16785 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16786 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16787 }
16788 /* FALLTHRU */
16789 case OMP_CLAUSE_PRIVATE:
16790 case OMP_CLAUSE_SHARED:
16791 case OMP_CLAUSE_FIRSTPRIVATE:
16792 case OMP_CLAUSE_COPYIN:
16793 case OMP_CLAUSE_COPYPRIVATE:
16794 case OMP_CLAUSE_UNIFORM:
16795 case OMP_CLAUSE_DEPEND:
16796 case OMP_CLAUSE_FROM:
16797 case OMP_CLAUSE_TO:
16798 case OMP_CLAUSE_MAP:
16799 case OMP_CLAUSE_NONTEMPORAL:
16800 case OMP_CLAUSE_USE_DEVICE_PTR:
16801 case OMP_CLAUSE_USE_DEVICE_ADDR:
16802 case OMP_CLAUSE_IS_DEVICE_PTR:
16803 case OMP_CLAUSE_INCLUSIVE:
16804 case OMP_CLAUSE_EXCLUSIVE:
16805 OMP_CLAUSE_DECL (nc)
16806 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16807 in_decl, iterator_cache);
16808 break;
16809 case OMP_CLAUSE_TILE:
16810 case OMP_CLAUSE_IF:
16811 case OMP_CLAUSE_NUM_THREADS:
16812 case OMP_CLAUSE_SCHEDULE:
16813 case OMP_CLAUSE_COLLAPSE:
16814 case OMP_CLAUSE_FINAL:
16815 case OMP_CLAUSE_DEVICE:
16816 case OMP_CLAUSE_DIST_SCHEDULE:
16817 case OMP_CLAUSE_NUM_TEAMS:
16818 case OMP_CLAUSE_THREAD_LIMIT:
16819 case OMP_CLAUSE_SAFELEN:
16820 case OMP_CLAUSE_SIMDLEN:
16821 case OMP_CLAUSE_NUM_TASKS:
16822 case OMP_CLAUSE_GRAINSIZE:
16823 case OMP_CLAUSE_PRIORITY:
16824 case OMP_CLAUSE_ORDERED:
16825 case OMP_CLAUSE_HINT:
16826 case OMP_CLAUSE_NUM_GANGS:
16827 case OMP_CLAUSE_NUM_WORKERS:
16828 case OMP_CLAUSE_VECTOR_LENGTH:
16829 case OMP_CLAUSE_WORKER:
16830 case OMP_CLAUSE_VECTOR:
16831 case OMP_CLAUSE_ASYNC:
16832 case OMP_CLAUSE_WAIT:
16833 OMP_CLAUSE_OPERAND (nc, 0)
16834 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16835 in_decl, /*integral_constant_expression_p=*/false);
16836 break;
16837 case OMP_CLAUSE_REDUCTION:
16838 case OMP_CLAUSE_IN_REDUCTION:
16839 case OMP_CLAUSE_TASK_REDUCTION:
16840 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16841 {
16842 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16843 if (TREE_CODE (placeholder) == SCOPE_REF)
16844 {
16845 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16846 complain, in_decl);
16847 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16848 = build_qualified_name (NULL_TREE, scope,
16849 TREE_OPERAND (placeholder, 1),
16850 false);
16851 }
16852 else
16853 gcc_assert (identifier_p (placeholder));
16854 }
16855 OMP_CLAUSE_DECL (nc)
16856 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16857 in_decl, NULL);
16858 break;
16859 case OMP_CLAUSE_GANG:
16860 case OMP_CLAUSE_ALIGNED:
16861 OMP_CLAUSE_DECL (nc)
16862 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16863 in_decl, NULL);
16864 OMP_CLAUSE_OPERAND (nc, 1)
16865 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16866 in_decl, /*integral_constant_expression_p=*/false);
16867 break;
16868 case OMP_CLAUSE_LINEAR:
16869 OMP_CLAUSE_DECL (nc)
16870 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16871 in_decl, NULL);
16872 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16873 {
16874 gcc_assert (!linear_no_step);
16875 linear_no_step = nc;
16876 }
16877 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16878 OMP_CLAUSE_LINEAR_STEP (nc)
16879 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16880 complain, in_decl, NULL);
16881 else
16882 OMP_CLAUSE_LINEAR_STEP (nc)
16883 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16884 in_decl,
16885 /*integral_constant_expression_p=*/false);
16886 break;
16887 case OMP_CLAUSE_NOWAIT:
16888 case OMP_CLAUSE_DEFAULT:
16889 case OMP_CLAUSE_UNTIED:
16890 case OMP_CLAUSE_MERGEABLE:
16891 case OMP_CLAUSE_INBRANCH:
16892 case OMP_CLAUSE_NOTINBRANCH:
16893 case OMP_CLAUSE_PROC_BIND:
16894 case OMP_CLAUSE_FOR:
16895 case OMP_CLAUSE_PARALLEL:
16896 case OMP_CLAUSE_SECTIONS:
16897 case OMP_CLAUSE_TASKGROUP:
16898 case OMP_CLAUSE_NOGROUP:
16899 case OMP_CLAUSE_THREADS:
16900 case OMP_CLAUSE_SIMD:
16901 case OMP_CLAUSE_DEFAULTMAP:
16902 case OMP_CLAUSE_ORDER:
16903 case OMP_CLAUSE_BIND:
16904 case OMP_CLAUSE_INDEPENDENT:
16905 case OMP_CLAUSE_AUTO:
16906 case OMP_CLAUSE_SEQ:
16907 case OMP_CLAUSE_IF_PRESENT:
16908 case OMP_CLAUSE_FINALIZE:
16909 break;
16910 default:
16911 gcc_unreachable ();
16912 }
16913 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16914 switch (OMP_CLAUSE_CODE (nc))
16915 {
16916 case OMP_CLAUSE_SHARED:
16917 case OMP_CLAUSE_PRIVATE:
16918 case OMP_CLAUSE_FIRSTPRIVATE:
16919 case OMP_CLAUSE_LASTPRIVATE:
16920 case OMP_CLAUSE_COPYPRIVATE:
16921 case OMP_CLAUSE_LINEAR:
16922 case OMP_CLAUSE_REDUCTION:
16923 case OMP_CLAUSE_IN_REDUCTION:
16924 case OMP_CLAUSE_TASK_REDUCTION:
16925 case OMP_CLAUSE_USE_DEVICE_PTR:
16926 case OMP_CLAUSE_USE_DEVICE_ADDR:
16927 case OMP_CLAUSE_IS_DEVICE_PTR:
16928 case OMP_CLAUSE_INCLUSIVE:
16929 case OMP_CLAUSE_EXCLUSIVE:
16930 /* tsubst_expr on SCOPE_REF results in returning
16931 finish_non_static_data_member result. Undo that here. */
16932 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16933 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16934 == IDENTIFIER_NODE))
16935 {
16936 tree t = OMP_CLAUSE_DECL (nc);
16937 tree v = t;
16938 while (v)
16939 switch (TREE_CODE (v))
16940 {
16941 case COMPONENT_REF:
16942 case MEM_REF:
16943 case INDIRECT_REF:
16944 CASE_CONVERT:
16945 case POINTER_PLUS_EXPR:
16946 v = TREE_OPERAND (v, 0);
16947 continue;
16948 case PARM_DECL:
16949 if (DECL_CONTEXT (v) == current_function_decl
16950 && DECL_ARTIFICIAL (v)
16951 && DECL_NAME (v) == this_identifier)
16952 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16953 /* FALLTHRU */
16954 default:
16955 v = NULL_TREE;
16956 break;
16957 }
16958 }
16959 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16960 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16961 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16962 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16963 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16964 {
16965 tree decl = OMP_CLAUSE_DECL (nc);
16966 if (VAR_P (decl))
16967 {
16968 retrofit_lang_decl (decl);
16969 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16970 }
16971 }
16972 break;
16973 default:
16974 break;
16975 }
16976 }
16977
16978 new_clauses = nreverse (new_clauses);
16979 if (ort != C_ORT_OMP_DECLARE_SIMD)
16980 {
16981 new_clauses = finish_omp_clauses (new_clauses, ort);
16982 if (linear_no_step)
16983 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16984 if (nc == linear_no_step)
16985 {
16986 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16987 break;
16988 }
16989 }
16990 return new_clauses;
16991 }
16992
16993 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16994
16995 static tree
16996 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16997 tree in_decl)
16998 {
16999 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17000
17001 tree purpose, value, chain;
17002
17003 if (t == NULL)
17004 return t;
17005
17006 if (TREE_CODE (t) != TREE_LIST)
17007 return tsubst_copy_and_build (t, args, complain, in_decl,
17008 /*function_p=*/false,
17009 /*integral_constant_expression_p=*/false);
17010
17011 if (t == void_list_node)
17012 return t;
17013
17014 purpose = TREE_PURPOSE (t);
17015 if (purpose)
17016 purpose = RECUR (purpose);
17017 value = TREE_VALUE (t);
17018 if (value)
17019 {
17020 if (TREE_CODE (value) != LABEL_DECL)
17021 value = RECUR (value);
17022 else
17023 {
17024 value = lookup_label (DECL_NAME (value));
17025 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17026 TREE_USED (value) = 1;
17027 }
17028 }
17029 chain = TREE_CHAIN (t);
17030 if (chain && chain != void_type_node)
17031 chain = RECUR (chain);
17032 return tree_cons (purpose, value, chain);
17033 #undef RECUR
17034 }
17035
17036 /* Used to temporarily communicate the list of #pragma omp parallel
17037 clauses to #pragma omp for instantiation if they are combined
17038 together. */
17039
17040 static tree *omp_parallel_combined_clauses;
17041
17042 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17043 tree *, unsigned int *);
17044
17045 /* Substitute one OMP_FOR iterator. */
17046
17047 static bool
17048 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17049 tree initv, tree condv, tree incrv, tree *clauses,
17050 tree args, tsubst_flags_t complain, tree in_decl,
17051 bool integral_constant_expression_p)
17052 {
17053 #define RECUR(NODE) \
17054 tsubst_expr ((NODE), args, complain, in_decl, \
17055 integral_constant_expression_p)
17056 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17057 bool ret = false;
17058
17059 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17060 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17061
17062 decl = TREE_OPERAND (init, 0);
17063 init = TREE_OPERAND (init, 1);
17064 tree decl_expr = NULL_TREE;
17065 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17066 if (range_for)
17067 {
17068 bool decomp = false;
17069 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17070 {
17071 tree v = DECL_VALUE_EXPR (decl);
17072 if (TREE_CODE (v) == ARRAY_REF
17073 && VAR_P (TREE_OPERAND (v, 0))
17074 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17075 {
17076 tree decomp_first = NULL_TREE;
17077 unsigned decomp_cnt = 0;
17078 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17079 maybe_push_decl (d);
17080 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17081 in_decl, &decomp_first, &decomp_cnt);
17082 decomp = true;
17083 if (d == error_mark_node)
17084 decl = error_mark_node;
17085 else
17086 for (unsigned int i = 0; i < decomp_cnt; i++)
17087 {
17088 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17089 {
17090 tree v = build_nt (ARRAY_REF, d,
17091 size_int (decomp_cnt - i - 1),
17092 NULL_TREE, NULL_TREE);
17093 SET_DECL_VALUE_EXPR (decomp_first, v);
17094 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17095 }
17096 fit_decomposition_lang_decl (decomp_first, d);
17097 decomp_first = DECL_CHAIN (decomp_first);
17098 }
17099 }
17100 }
17101 decl = tsubst_decl (decl, args, complain);
17102 if (!decomp)
17103 maybe_push_decl (decl);
17104 }
17105 else if (init && TREE_CODE (init) == DECL_EXPR)
17106 {
17107 /* We need to jump through some hoops to handle declarations in the
17108 init-statement, since we might need to handle auto deduction,
17109 but we need to keep control of initialization. */
17110 decl_expr = init;
17111 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17112 decl = tsubst_decl (decl, args, complain);
17113 }
17114 else
17115 {
17116 if (TREE_CODE (decl) == SCOPE_REF)
17117 {
17118 decl = RECUR (decl);
17119 if (TREE_CODE (decl) == COMPONENT_REF)
17120 {
17121 tree v = decl;
17122 while (v)
17123 switch (TREE_CODE (v))
17124 {
17125 case COMPONENT_REF:
17126 case MEM_REF:
17127 case INDIRECT_REF:
17128 CASE_CONVERT:
17129 case POINTER_PLUS_EXPR:
17130 v = TREE_OPERAND (v, 0);
17131 continue;
17132 case PARM_DECL:
17133 if (DECL_CONTEXT (v) == current_function_decl
17134 && DECL_ARTIFICIAL (v)
17135 && DECL_NAME (v) == this_identifier)
17136 {
17137 decl = TREE_OPERAND (decl, 1);
17138 decl = omp_privatize_field (decl, false);
17139 }
17140 /* FALLTHRU */
17141 default:
17142 v = NULL_TREE;
17143 break;
17144 }
17145 }
17146 }
17147 else
17148 decl = RECUR (decl);
17149 }
17150 init = RECUR (init);
17151
17152 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17153 {
17154 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17155 if (TREE_CODE (o) == TREE_LIST)
17156 TREE_VEC_ELT (orig_declv, i)
17157 = tree_cons (RECUR (TREE_PURPOSE (o)),
17158 RECUR (TREE_VALUE (o)),
17159 NULL_TREE);
17160 else
17161 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17162 }
17163
17164 if (range_for)
17165 {
17166 tree this_pre_body = NULL_TREE;
17167 tree orig_init = NULL_TREE;
17168 tree orig_decl = NULL_TREE;
17169 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17170 orig_init, cond, incr);
17171 if (orig_decl)
17172 {
17173 if (orig_declv == NULL_TREE)
17174 orig_declv = copy_node (declv);
17175 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17176 ret = true;
17177 }
17178 else if (orig_declv)
17179 TREE_VEC_ELT (orig_declv, i) = decl;
17180 }
17181
17182 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17183 if (!range_for && auto_node && init)
17184 TREE_TYPE (decl)
17185 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17186
17187 gcc_assert (!type_dependent_expression_p (decl));
17188
17189 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17190 {
17191 if (decl_expr)
17192 {
17193 /* Declare the variable, but don't let that initialize it. */
17194 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17195 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17196 RECUR (decl_expr);
17197 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17198 }
17199
17200 if (!range_for)
17201 {
17202 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17203 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17204 if (TREE_CODE (incr) == MODIFY_EXPR)
17205 {
17206 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17207 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17208 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17209 NOP_EXPR, rhs, complain);
17210 }
17211 else
17212 incr = RECUR (incr);
17213 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17214 TREE_VEC_ELT (orig_declv, i) = decl;
17215 }
17216 TREE_VEC_ELT (declv, i) = decl;
17217 TREE_VEC_ELT (initv, i) = init;
17218 TREE_VEC_ELT (condv, i) = cond;
17219 TREE_VEC_ELT (incrv, i) = incr;
17220 return ret;
17221 }
17222
17223 if (decl_expr)
17224 {
17225 /* Declare and initialize the variable. */
17226 RECUR (decl_expr);
17227 init = NULL_TREE;
17228 }
17229 else if (init)
17230 {
17231 tree *pc;
17232 int j;
17233 for (j = ((omp_parallel_combined_clauses == NULL
17234 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17235 {
17236 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17237 {
17238 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17239 && OMP_CLAUSE_DECL (*pc) == decl)
17240 break;
17241 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17242 && OMP_CLAUSE_DECL (*pc) == decl)
17243 {
17244 if (j)
17245 break;
17246 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17247 tree c = *pc;
17248 *pc = OMP_CLAUSE_CHAIN (c);
17249 OMP_CLAUSE_CHAIN (c) = *clauses;
17250 *clauses = c;
17251 }
17252 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17253 && OMP_CLAUSE_DECL (*pc) == decl)
17254 {
17255 error ("iteration variable %qD should not be firstprivate",
17256 decl);
17257 *pc = OMP_CLAUSE_CHAIN (*pc);
17258 }
17259 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17260 && OMP_CLAUSE_DECL (*pc) == decl)
17261 {
17262 error ("iteration variable %qD should not be reduction",
17263 decl);
17264 *pc = OMP_CLAUSE_CHAIN (*pc);
17265 }
17266 else
17267 pc = &OMP_CLAUSE_CHAIN (*pc);
17268 }
17269 if (*pc)
17270 break;
17271 }
17272 if (*pc == NULL_TREE)
17273 {
17274 tree c = build_omp_clause (input_location,
17275 TREE_CODE (t) == OMP_LOOP
17276 ? OMP_CLAUSE_LASTPRIVATE
17277 : OMP_CLAUSE_PRIVATE);
17278 OMP_CLAUSE_DECL (c) = decl;
17279 c = finish_omp_clauses (c, C_ORT_OMP);
17280 if (c)
17281 {
17282 OMP_CLAUSE_CHAIN (c) = *clauses;
17283 *clauses = c;
17284 }
17285 }
17286 }
17287 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17288 if (COMPARISON_CLASS_P (cond))
17289 {
17290 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17291 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17292 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17293 }
17294 else
17295 cond = RECUR (cond);
17296 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17297 switch (TREE_CODE (incr))
17298 {
17299 case PREINCREMENT_EXPR:
17300 case PREDECREMENT_EXPR:
17301 case POSTINCREMENT_EXPR:
17302 case POSTDECREMENT_EXPR:
17303 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17304 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17305 break;
17306 case MODIFY_EXPR:
17307 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17308 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17309 {
17310 tree rhs = TREE_OPERAND (incr, 1);
17311 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17312 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17313 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17314 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17315 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17316 rhs0, rhs1));
17317 }
17318 else
17319 incr = RECUR (incr);
17320 break;
17321 case MODOP_EXPR:
17322 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17323 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17324 {
17325 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17326 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17327 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17328 TREE_TYPE (decl), lhs,
17329 RECUR (TREE_OPERAND (incr, 2))));
17330 }
17331 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17332 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17333 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17334 {
17335 tree rhs = TREE_OPERAND (incr, 2);
17336 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17337 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17338 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17339 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17340 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17341 rhs0, rhs1));
17342 }
17343 else
17344 incr = RECUR (incr);
17345 break;
17346 default:
17347 incr = RECUR (incr);
17348 break;
17349 }
17350
17351 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17352 TREE_VEC_ELT (orig_declv, i) = decl;
17353 TREE_VEC_ELT (declv, i) = decl;
17354 TREE_VEC_ELT (initv, i) = init;
17355 TREE_VEC_ELT (condv, i) = cond;
17356 TREE_VEC_ELT (incrv, i) = incr;
17357 return false;
17358 #undef RECUR
17359 }
17360
17361 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17362 of OMP_TARGET's body. */
17363
17364 static tree
17365 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17366 {
17367 *walk_subtrees = 0;
17368 switch (TREE_CODE (*tp))
17369 {
17370 case OMP_TEAMS:
17371 return *tp;
17372 case BIND_EXPR:
17373 case STATEMENT_LIST:
17374 *walk_subtrees = 1;
17375 break;
17376 default:
17377 break;
17378 }
17379 return NULL_TREE;
17380 }
17381
17382 /* Helper function for tsubst_expr. For decomposition declaration
17383 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17384 also the corresponding decls representing the identifiers
17385 of the decomposition declaration. Return DECL if successful
17386 or error_mark_node otherwise, set *FIRST to the first decl
17387 in the list chained through DECL_CHAIN and *CNT to the number
17388 of such decls. */
17389
17390 static tree
17391 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17392 tsubst_flags_t complain, tree in_decl, tree *first,
17393 unsigned int *cnt)
17394 {
17395 tree decl2, decl3, prev = decl;
17396 *cnt = 0;
17397 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17398 for (decl2 = DECL_CHAIN (pattern_decl);
17399 decl2
17400 && VAR_P (decl2)
17401 && DECL_DECOMPOSITION_P (decl2)
17402 && DECL_NAME (decl2);
17403 decl2 = DECL_CHAIN (decl2))
17404 {
17405 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17406 {
17407 gcc_assert (errorcount);
17408 return error_mark_node;
17409 }
17410 (*cnt)++;
17411 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17412 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17413 tree v = DECL_VALUE_EXPR (decl2);
17414 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17415 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17416 decl3 = tsubst (decl2, args, complain, in_decl);
17417 SET_DECL_VALUE_EXPR (decl2, v);
17418 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17419 if (VAR_P (decl3))
17420 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17421 else
17422 {
17423 gcc_assert (errorcount);
17424 decl = error_mark_node;
17425 continue;
17426 }
17427 maybe_push_decl (decl3);
17428 if (error_operand_p (decl3))
17429 decl = error_mark_node;
17430 else if (decl != error_mark_node
17431 && DECL_CHAIN (decl3) != prev
17432 && decl != prev)
17433 {
17434 gcc_assert (errorcount);
17435 decl = error_mark_node;
17436 }
17437 else
17438 prev = decl3;
17439 }
17440 *first = prev;
17441 return decl;
17442 }
17443
17444 /* Return the proper local_specialization for init-capture pack DECL. */
17445
17446 static tree
17447 lookup_init_capture_pack (tree decl)
17448 {
17449 /* We handle normal pack captures by forwarding to the specialization of the
17450 captured parameter. We can't do that for pack init-captures; we need them
17451 to have their own local_specialization. We created the individual
17452 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17453 when we process the DECL_EXPR for the pack init-capture in the template.
17454 So, how do we find them? We don't know the capture proxy pack when
17455 building the individual resulting proxies, and we don't know the
17456 individual proxies when instantiating the pack. What we have in common is
17457 the FIELD_DECL.
17458
17459 So...when we instantiate the FIELD_DECL, we stick the result in
17460 local_specializations. Then at the DECL_EXPR we look up that result, see
17461 how many elements it has, synthesize the names, and look them up. */
17462
17463 tree cname = DECL_NAME (decl);
17464 tree val = DECL_VALUE_EXPR (decl);
17465 tree field = TREE_OPERAND (val, 1);
17466 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17467 tree fpack = retrieve_local_specialization (field);
17468 if (fpack == error_mark_node)
17469 return error_mark_node;
17470
17471 int len = 1;
17472 tree vec = NULL_TREE;
17473 tree r = NULL_TREE;
17474 if (TREE_CODE (fpack) == TREE_VEC)
17475 {
17476 len = TREE_VEC_LENGTH (fpack);
17477 vec = make_tree_vec (len);
17478 r = make_node (NONTYPE_ARGUMENT_PACK);
17479 SET_ARGUMENT_PACK_ARGS (r, vec);
17480 }
17481 for (int i = 0; i < len; ++i)
17482 {
17483 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17484 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17485 if (vec)
17486 TREE_VEC_ELT (vec, i) = elt;
17487 else
17488 r = elt;
17489 }
17490 return r;
17491 }
17492
17493 /* Like tsubst_copy for expressions, etc. but also does semantic
17494 processing. */
17495
17496 tree
17497 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17498 bool integral_constant_expression_p)
17499 {
17500 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17501 #define RECUR(NODE) \
17502 tsubst_expr ((NODE), args, complain, in_decl, \
17503 integral_constant_expression_p)
17504
17505 tree stmt, tmp;
17506 tree r;
17507 location_t loc;
17508
17509 if (t == NULL_TREE || t == error_mark_node)
17510 return t;
17511
17512 loc = input_location;
17513 if (location_t eloc = cp_expr_location (t))
17514 input_location = eloc;
17515 if (STATEMENT_CODE_P (TREE_CODE (t)))
17516 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17517
17518 switch (TREE_CODE (t))
17519 {
17520 case STATEMENT_LIST:
17521 {
17522 tree_stmt_iterator i;
17523 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17524 RECUR (tsi_stmt (i));
17525 break;
17526 }
17527
17528 case CTOR_INITIALIZER:
17529 finish_mem_initializers (tsubst_initializer_list
17530 (TREE_OPERAND (t, 0), args));
17531 break;
17532
17533 case RETURN_EXPR:
17534 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17535 break;
17536
17537 case EXPR_STMT:
17538 tmp = RECUR (EXPR_STMT_EXPR (t));
17539 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17540 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17541 else
17542 finish_expr_stmt (tmp);
17543 break;
17544
17545 case USING_STMT:
17546 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17547 break;
17548
17549 case DECL_EXPR:
17550 {
17551 tree decl, pattern_decl;
17552 tree init;
17553
17554 pattern_decl = decl = DECL_EXPR_DECL (t);
17555 if (TREE_CODE (decl) == LABEL_DECL)
17556 finish_label_decl (DECL_NAME (decl));
17557 else if (TREE_CODE (decl) == USING_DECL)
17558 {
17559 tree scope = USING_DECL_SCOPE (decl);
17560 tree name = DECL_NAME (decl);
17561
17562 scope = tsubst (scope, args, complain, in_decl);
17563 finish_nonmember_using_decl (scope, name);
17564 }
17565 else if (is_capture_proxy (decl)
17566 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17567 {
17568 /* We're in tsubst_lambda_expr, we've already inserted a new
17569 capture proxy, so look it up and register it. */
17570 tree inst;
17571 if (!DECL_PACK_P (decl))
17572 {
17573 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17574 /*nonclass*/1, /*block_p=*/true,
17575 /*ns_only*/0, LOOKUP_HIDDEN);
17576 gcc_assert (inst != decl && is_capture_proxy (inst));
17577 }
17578 else if (is_normal_capture_proxy (decl))
17579 {
17580 inst = (retrieve_local_specialization
17581 (DECL_CAPTURED_VARIABLE (decl)));
17582 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17583 }
17584 else
17585 inst = lookup_init_capture_pack (decl);
17586
17587 register_local_specialization (inst, decl);
17588 break;
17589 }
17590 else if (DECL_PRETTY_FUNCTION_P (decl))
17591 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17592 DECL_NAME (decl),
17593 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17594 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17595 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17596 /* Don't copy the old closure; we'll create a new one in
17597 tsubst_lambda_expr. */
17598 break;
17599 else
17600 {
17601 init = DECL_INITIAL (decl);
17602 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17603 for local variables, so save if DECL was declared constinit. */
17604 const bool constinit_p
17605 = (VAR_P (decl)
17606 && DECL_LANG_SPECIFIC (decl)
17607 && DECL_TEMPLATE_INFO (decl)
17608 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17609 decl = tsubst (decl, args, complain, in_decl);
17610 if (decl != error_mark_node)
17611 {
17612 /* By marking the declaration as instantiated, we avoid
17613 trying to instantiate it. Since instantiate_decl can't
17614 handle local variables, and since we've already done
17615 all that needs to be done, that's the right thing to
17616 do. */
17617 if (VAR_P (decl))
17618 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17619 if (VAR_P (decl) && !DECL_NAME (decl)
17620 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17621 /* Anonymous aggregates are a special case. */
17622 finish_anon_union (decl);
17623 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17624 {
17625 DECL_CONTEXT (decl) = current_function_decl;
17626 if (DECL_NAME (decl) == this_identifier)
17627 {
17628 tree lam = DECL_CONTEXT (current_function_decl);
17629 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17630 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17631 }
17632 insert_capture_proxy (decl);
17633 }
17634 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17635 /* We already did a pushtag. */;
17636 else if (TREE_CODE (decl) == FUNCTION_DECL
17637 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17638 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17639 {
17640 DECL_CONTEXT (decl) = NULL_TREE;
17641 pushdecl (decl);
17642 DECL_CONTEXT (decl) = current_function_decl;
17643 cp_check_omp_declare_reduction (decl);
17644 }
17645 else
17646 {
17647 bool const_init = false;
17648 unsigned int cnt = 0;
17649 tree first = NULL_TREE, ndecl = error_mark_node;
17650 maybe_push_decl (decl);
17651
17652 if (VAR_P (decl)
17653 && DECL_DECOMPOSITION_P (decl)
17654 && TREE_TYPE (pattern_decl) != error_mark_node)
17655 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17656 complain, in_decl, &first,
17657 &cnt);
17658
17659 init = tsubst_init (init, decl, args, complain, in_decl);
17660
17661 if (VAR_P (decl))
17662 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17663 (pattern_decl));
17664
17665 if (ndecl != error_mark_node)
17666 cp_maybe_mangle_decomp (ndecl, first, cnt);
17667
17668 cp_finish_decl (decl, init, const_init, NULL_TREE,
17669 constinit_p ? LOOKUP_CONSTINIT : 0);
17670
17671 if (ndecl != error_mark_node)
17672 cp_finish_decomp (ndecl, first, cnt);
17673 }
17674 }
17675 }
17676
17677 break;
17678 }
17679
17680 case FOR_STMT:
17681 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17682 RECUR (FOR_INIT_STMT (t));
17683 finish_init_stmt (stmt);
17684 tmp = RECUR (FOR_COND (t));
17685 finish_for_cond (tmp, stmt, false, 0);
17686 tmp = RECUR (FOR_EXPR (t));
17687 finish_for_expr (tmp, stmt);
17688 {
17689 bool prev = note_iteration_stmt_body_start ();
17690 RECUR (FOR_BODY (t));
17691 note_iteration_stmt_body_end (prev);
17692 }
17693 finish_for_stmt (stmt);
17694 break;
17695
17696 case RANGE_FOR_STMT:
17697 {
17698 /* Construct another range_for, if this is not a final
17699 substitution (for inside inside a generic lambda of a
17700 template). Otherwise convert to a regular for. */
17701 tree decl, expr;
17702 stmt = (processing_template_decl
17703 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17704 : begin_for_stmt (NULL_TREE, NULL_TREE));
17705 RECUR (RANGE_FOR_INIT_STMT (t));
17706 decl = RANGE_FOR_DECL (t);
17707 decl = tsubst (decl, args, complain, in_decl);
17708 maybe_push_decl (decl);
17709 expr = RECUR (RANGE_FOR_EXPR (t));
17710
17711 tree decomp_first = NULL_TREE;
17712 unsigned decomp_cnt = 0;
17713 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17714 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17715 complain, in_decl,
17716 &decomp_first, &decomp_cnt);
17717
17718 if (processing_template_decl)
17719 {
17720 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17721 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17722 finish_range_for_decl (stmt, decl, expr);
17723 if (decomp_first && decl != error_mark_node)
17724 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17725 }
17726 else
17727 {
17728 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17729 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17730 stmt = cp_convert_range_for (stmt, decl, expr,
17731 decomp_first, decomp_cnt,
17732 RANGE_FOR_IVDEP (t), unroll);
17733 }
17734
17735 bool prev = note_iteration_stmt_body_start ();
17736 RECUR (RANGE_FOR_BODY (t));
17737 note_iteration_stmt_body_end (prev);
17738 finish_for_stmt (stmt);
17739 }
17740 break;
17741
17742 case WHILE_STMT:
17743 stmt = begin_while_stmt ();
17744 tmp = RECUR (WHILE_COND (t));
17745 finish_while_stmt_cond (tmp, stmt, false, 0);
17746 {
17747 bool prev = note_iteration_stmt_body_start ();
17748 RECUR (WHILE_BODY (t));
17749 note_iteration_stmt_body_end (prev);
17750 }
17751 finish_while_stmt (stmt);
17752 break;
17753
17754 case DO_STMT:
17755 stmt = begin_do_stmt ();
17756 {
17757 bool prev = note_iteration_stmt_body_start ();
17758 RECUR (DO_BODY (t));
17759 note_iteration_stmt_body_end (prev);
17760 }
17761 finish_do_body (stmt);
17762 tmp = RECUR (DO_COND (t));
17763 finish_do_stmt (tmp, stmt, false, 0);
17764 break;
17765
17766 case IF_STMT:
17767 stmt = begin_if_stmt ();
17768 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17769 if (IF_STMT_CONSTEXPR_P (t))
17770 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17771 tmp = RECUR (IF_COND (t));
17772 tmp = finish_if_stmt_cond (tmp, stmt);
17773 if (IF_STMT_CONSTEXPR_P (t)
17774 && instantiation_dependent_expression_p (tmp))
17775 {
17776 /* We're partially instantiating a generic lambda, but the condition
17777 of the constexpr if is still dependent. Don't substitute into the
17778 branches now, just remember the template arguments. */
17779 do_poplevel (IF_SCOPE (stmt));
17780 IF_COND (stmt) = IF_COND (t);
17781 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17782 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17783 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17784 add_stmt (stmt);
17785 break;
17786 }
17787 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17788 /* Don't instantiate the THEN_CLAUSE. */;
17789 else
17790 {
17791 tree folded = fold_non_dependent_expr (tmp, complain);
17792 bool inhibit = integer_zerop (folded);
17793 if (inhibit)
17794 ++c_inhibit_evaluation_warnings;
17795 RECUR (THEN_CLAUSE (t));
17796 if (inhibit)
17797 --c_inhibit_evaluation_warnings;
17798 }
17799 finish_then_clause (stmt);
17800
17801 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17802 /* Don't instantiate the ELSE_CLAUSE. */;
17803 else if (ELSE_CLAUSE (t))
17804 {
17805 tree folded = fold_non_dependent_expr (tmp, complain);
17806 bool inhibit = integer_nonzerop (folded);
17807 begin_else_clause (stmt);
17808 if (inhibit)
17809 ++c_inhibit_evaluation_warnings;
17810 RECUR (ELSE_CLAUSE (t));
17811 if (inhibit)
17812 --c_inhibit_evaluation_warnings;
17813 finish_else_clause (stmt);
17814 }
17815
17816 finish_if_stmt (stmt);
17817 break;
17818
17819 case BIND_EXPR:
17820 if (BIND_EXPR_BODY_BLOCK (t))
17821 stmt = begin_function_body ();
17822 else
17823 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17824 ? BCS_TRY_BLOCK : 0);
17825
17826 RECUR (BIND_EXPR_BODY (t));
17827
17828 if (BIND_EXPR_BODY_BLOCK (t))
17829 finish_function_body (stmt);
17830 else
17831 finish_compound_stmt (stmt);
17832 break;
17833
17834 case BREAK_STMT:
17835 finish_break_stmt ();
17836 break;
17837
17838 case CONTINUE_STMT:
17839 finish_continue_stmt ();
17840 break;
17841
17842 case SWITCH_STMT:
17843 stmt = begin_switch_stmt ();
17844 tmp = RECUR (SWITCH_STMT_COND (t));
17845 finish_switch_cond (tmp, stmt);
17846 RECUR (SWITCH_STMT_BODY (t));
17847 finish_switch_stmt (stmt);
17848 break;
17849
17850 case CASE_LABEL_EXPR:
17851 {
17852 tree decl = CASE_LABEL (t);
17853 tree low = RECUR (CASE_LOW (t));
17854 tree high = RECUR (CASE_HIGH (t));
17855 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17856 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17857 {
17858 tree label = CASE_LABEL (l);
17859 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17860 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17861 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17862 }
17863 }
17864 break;
17865
17866 case LABEL_EXPR:
17867 {
17868 tree decl = LABEL_EXPR_LABEL (t);
17869 tree label;
17870
17871 label = finish_label_stmt (DECL_NAME (decl));
17872 if (TREE_CODE (label) == LABEL_DECL)
17873 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17874 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17875 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17876 }
17877 break;
17878
17879 case GOTO_EXPR:
17880 tmp = GOTO_DESTINATION (t);
17881 if (TREE_CODE (tmp) != LABEL_DECL)
17882 /* Computed goto's must be tsubst'd into. On the other hand,
17883 non-computed gotos must not be; the identifier in question
17884 will have no binding. */
17885 tmp = RECUR (tmp);
17886 else
17887 tmp = DECL_NAME (tmp);
17888 finish_goto_stmt (tmp);
17889 break;
17890
17891 case ASM_EXPR:
17892 {
17893 tree string = RECUR (ASM_STRING (t));
17894 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17895 complain, in_decl);
17896 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17897 complain, in_decl);
17898 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17899 complain, in_decl);
17900 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17901 complain, in_decl);
17902 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17903 outputs, inputs, clobbers, labels,
17904 ASM_INLINE_P (t));
17905 tree asm_expr = tmp;
17906 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17907 asm_expr = TREE_OPERAND (asm_expr, 0);
17908 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17909 }
17910 break;
17911
17912 case TRY_BLOCK:
17913 if (CLEANUP_P (t))
17914 {
17915 stmt = begin_try_block ();
17916 RECUR (TRY_STMTS (t));
17917 finish_cleanup_try_block (stmt);
17918 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17919 }
17920 else
17921 {
17922 tree compound_stmt = NULL_TREE;
17923
17924 if (FN_TRY_BLOCK_P (t))
17925 stmt = begin_function_try_block (&compound_stmt);
17926 else
17927 stmt = begin_try_block ();
17928
17929 RECUR (TRY_STMTS (t));
17930
17931 if (FN_TRY_BLOCK_P (t))
17932 finish_function_try_block (stmt);
17933 else
17934 finish_try_block (stmt);
17935
17936 RECUR (TRY_HANDLERS (t));
17937 if (FN_TRY_BLOCK_P (t))
17938 finish_function_handler_sequence (stmt, compound_stmt);
17939 else
17940 finish_handler_sequence (stmt);
17941 }
17942 break;
17943
17944 case HANDLER:
17945 {
17946 tree decl = HANDLER_PARMS (t);
17947
17948 if (decl)
17949 {
17950 decl = tsubst (decl, args, complain, in_decl);
17951 /* Prevent instantiate_decl from trying to instantiate
17952 this variable. We've already done all that needs to be
17953 done. */
17954 if (decl != error_mark_node)
17955 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17956 }
17957 stmt = begin_handler ();
17958 finish_handler_parms (decl, stmt);
17959 RECUR (HANDLER_BODY (t));
17960 finish_handler (stmt);
17961 }
17962 break;
17963
17964 case TAG_DEFN:
17965 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17966 if (CLASS_TYPE_P (tmp))
17967 {
17968 /* Local classes are not independent templates; they are
17969 instantiated along with their containing function. And this
17970 way we don't have to deal with pushing out of one local class
17971 to instantiate a member of another local class. */
17972 /* Closures are handled by the LAMBDA_EXPR. */
17973 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17974 complete_type (tmp);
17975 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17976 if ((VAR_P (fld)
17977 || (TREE_CODE (fld) == FUNCTION_DECL
17978 && !DECL_ARTIFICIAL (fld)))
17979 && DECL_TEMPLATE_INSTANTIATION (fld))
17980 instantiate_decl (fld, /*defer_ok=*/false,
17981 /*expl_inst_class=*/false);
17982 }
17983 break;
17984
17985 case STATIC_ASSERT:
17986 {
17987 tree condition;
17988
17989 ++c_inhibit_evaluation_warnings;
17990 condition =
17991 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17992 args,
17993 complain, in_decl,
17994 /*integral_constant_expression_p=*/true);
17995 --c_inhibit_evaluation_warnings;
17996
17997 finish_static_assert (condition,
17998 STATIC_ASSERT_MESSAGE (t),
17999 STATIC_ASSERT_SOURCE_LOCATION (t),
18000 /*member_p=*/false);
18001 }
18002 break;
18003
18004 case OACC_KERNELS:
18005 case OACC_PARALLEL:
18006 case OACC_SERIAL:
18007 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18008 in_decl);
18009 stmt = begin_omp_parallel ();
18010 RECUR (OMP_BODY (t));
18011 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18012 break;
18013
18014 case OMP_PARALLEL:
18015 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18016 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18017 complain, in_decl);
18018 if (OMP_PARALLEL_COMBINED (t))
18019 omp_parallel_combined_clauses = &tmp;
18020 stmt = begin_omp_parallel ();
18021 RECUR (OMP_PARALLEL_BODY (t));
18022 gcc_assert (omp_parallel_combined_clauses == NULL);
18023 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18024 = OMP_PARALLEL_COMBINED (t);
18025 pop_omp_privatization_clauses (r);
18026 break;
18027
18028 case OMP_TASK:
18029 if (OMP_TASK_BODY (t) == NULL_TREE)
18030 {
18031 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18032 complain, in_decl);
18033 t = copy_node (t);
18034 OMP_TASK_CLAUSES (t) = tmp;
18035 add_stmt (t);
18036 break;
18037 }
18038 r = push_omp_privatization_clauses (false);
18039 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18040 complain, in_decl);
18041 stmt = begin_omp_task ();
18042 RECUR (OMP_TASK_BODY (t));
18043 finish_omp_task (tmp, stmt);
18044 pop_omp_privatization_clauses (r);
18045 break;
18046
18047 case OMP_FOR:
18048 case OMP_LOOP:
18049 case OMP_SIMD:
18050 case OMP_DISTRIBUTE:
18051 case OMP_TASKLOOP:
18052 case OACC_LOOP:
18053 {
18054 tree clauses, body, pre_body;
18055 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18056 tree orig_declv = NULL_TREE;
18057 tree incrv = NULL_TREE;
18058 enum c_omp_region_type ort = C_ORT_OMP;
18059 bool any_range_for = false;
18060 int i;
18061
18062 if (TREE_CODE (t) == OACC_LOOP)
18063 ort = C_ORT_ACC;
18064
18065 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18066 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18067 in_decl);
18068 if (OMP_FOR_INIT (t) != NULL_TREE)
18069 {
18070 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18071 if (OMP_FOR_ORIG_DECLS (t))
18072 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18073 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18074 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18075 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18076 }
18077
18078 keep_next_level (true);
18079 stmt = begin_omp_structured_block ();
18080
18081 pre_body = push_stmt_list ();
18082 RECUR (OMP_FOR_PRE_BODY (t));
18083 pre_body = pop_stmt_list (pre_body);
18084
18085 if (OMP_FOR_INIT (t) != NULL_TREE)
18086 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18087 any_range_for
18088 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18089 condv, incrv, &clauses, args,
18090 complain, in_decl,
18091 integral_constant_expression_p);
18092 omp_parallel_combined_clauses = NULL;
18093
18094 if (any_range_for)
18095 {
18096 gcc_assert (orig_declv);
18097 body = begin_omp_structured_block ();
18098 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18099 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18100 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18101 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18102 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18103 TREE_VEC_ELT (declv, i));
18104 }
18105 else
18106 body = push_stmt_list ();
18107 RECUR (OMP_FOR_BODY (t));
18108 if (any_range_for)
18109 body = finish_omp_structured_block (body);
18110 else
18111 body = pop_stmt_list (body);
18112
18113 if (OMP_FOR_INIT (t) != NULL_TREE)
18114 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18115 orig_declv, initv, condv, incrv, body, pre_body,
18116 NULL, clauses);
18117 else
18118 {
18119 t = make_node (TREE_CODE (t));
18120 TREE_TYPE (t) = void_type_node;
18121 OMP_FOR_BODY (t) = body;
18122 OMP_FOR_PRE_BODY (t) = pre_body;
18123 OMP_FOR_CLAUSES (t) = clauses;
18124 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18125 add_stmt (t);
18126 }
18127
18128 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18129 t));
18130 pop_omp_privatization_clauses (r);
18131 }
18132 break;
18133
18134 case OMP_SECTIONS:
18135 omp_parallel_combined_clauses = NULL;
18136 /* FALLTHRU */
18137 case OMP_SINGLE:
18138 case OMP_TEAMS:
18139 case OMP_CRITICAL:
18140 case OMP_TASKGROUP:
18141 case OMP_SCAN:
18142 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18143 && OMP_TEAMS_COMBINED (t));
18144 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18145 in_decl);
18146 if (TREE_CODE (t) == OMP_TEAMS)
18147 {
18148 keep_next_level (true);
18149 stmt = begin_omp_structured_block ();
18150 RECUR (OMP_BODY (t));
18151 stmt = finish_omp_structured_block (stmt);
18152 }
18153 else
18154 {
18155 stmt = push_stmt_list ();
18156 RECUR (OMP_BODY (t));
18157 stmt = pop_stmt_list (stmt);
18158 }
18159
18160 t = copy_node (t);
18161 OMP_BODY (t) = stmt;
18162 OMP_CLAUSES (t) = tmp;
18163 add_stmt (t);
18164 pop_omp_privatization_clauses (r);
18165 break;
18166
18167 case OMP_DEPOBJ:
18168 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18169 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18170 {
18171 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18172 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18173 {
18174 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18175 args, complain, in_decl);
18176 if (tmp == NULL_TREE)
18177 tmp = error_mark_node;
18178 }
18179 else
18180 {
18181 kind = (enum omp_clause_depend_kind)
18182 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18183 tmp = NULL_TREE;
18184 }
18185 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18186 }
18187 else
18188 finish_omp_depobj (EXPR_LOCATION (t), r,
18189 OMP_CLAUSE_DEPEND_SOURCE,
18190 OMP_DEPOBJ_CLAUSES (t));
18191 break;
18192
18193 case OACC_DATA:
18194 case OMP_TARGET_DATA:
18195 case OMP_TARGET:
18196 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18197 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18198 in_decl);
18199 keep_next_level (true);
18200 stmt = begin_omp_structured_block ();
18201
18202 RECUR (OMP_BODY (t));
18203 stmt = finish_omp_structured_block (stmt);
18204
18205 t = copy_node (t);
18206 OMP_BODY (t) = stmt;
18207 OMP_CLAUSES (t) = tmp;
18208 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18209 {
18210 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18211 if (teams)
18212 {
18213 /* For combined target teams, ensure the num_teams and
18214 thread_limit clause expressions are evaluated on the host,
18215 before entering the target construct. */
18216 tree c;
18217 for (c = OMP_TEAMS_CLAUSES (teams);
18218 c; c = OMP_CLAUSE_CHAIN (c))
18219 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18220 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18221 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18222 {
18223 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18224 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18225 if (expr == error_mark_node)
18226 continue;
18227 tmp = TARGET_EXPR_SLOT (expr);
18228 add_stmt (expr);
18229 OMP_CLAUSE_OPERAND (c, 0) = expr;
18230 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18231 OMP_CLAUSE_FIRSTPRIVATE);
18232 OMP_CLAUSE_DECL (tc) = tmp;
18233 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18234 OMP_TARGET_CLAUSES (t) = tc;
18235 }
18236 }
18237 }
18238 add_stmt (t);
18239 break;
18240
18241 case OACC_DECLARE:
18242 t = copy_node (t);
18243 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18244 complain, in_decl);
18245 OACC_DECLARE_CLAUSES (t) = tmp;
18246 add_stmt (t);
18247 break;
18248
18249 case OMP_TARGET_UPDATE:
18250 case OMP_TARGET_ENTER_DATA:
18251 case OMP_TARGET_EXIT_DATA:
18252 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18253 complain, in_decl);
18254 t = copy_node (t);
18255 OMP_STANDALONE_CLAUSES (t) = tmp;
18256 add_stmt (t);
18257 break;
18258
18259 case OACC_ENTER_DATA:
18260 case OACC_EXIT_DATA:
18261 case OACC_UPDATE:
18262 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18263 complain, in_decl);
18264 t = copy_node (t);
18265 OMP_STANDALONE_CLAUSES (t) = tmp;
18266 add_stmt (t);
18267 break;
18268
18269 case OMP_ORDERED:
18270 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18271 complain, in_decl);
18272 stmt = push_stmt_list ();
18273 RECUR (OMP_BODY (t));
18274 stmt = pop_stmt_list (stmt);
18275
18276 t = copy_node (t);
18277 OMP_BODY (t) = stmt;
18278 OMP_ORDERED_CLAUSES (t) = tmp;
18279 add_stmt (t);
18280 break;
18281
18282 case OMP_SECTION:
18283 case OMP_MASTER:
18284 stmt = push_stmt_list ();
18285 RECUR (OMP_BODY (t));
18286 stmt = pop_stmt_list (stmt);
18287
18288 t = copy_node (t);
18289 OMP_BODY (t) = stmt;
18290 add_stmt (t);
18291 break;
18292
18293 case OMP_ATOMIC:
18294 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18295 tmp = NULL_TREE;
18296 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18297 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18298 complain, in_decl);
18299 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18300 {
18301 tree op1 = TREE_OPERAND (t, 1);
18302 tree rhs1 = NULL_TREE;
18303 tree lhs, rhs;
18304 if (TREE_CODE (op1) == COMPOUND_EXPR)
18305 {
18306 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18307 op1 = TREE_OPERAND (op1, 1);
18308 }
18309 lhs = RECUR (TREE_OPERAND (op1, 0));
18310 rhs = RECUR (TREE_OPERAND (op1, 1));
18311 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18312 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18313 OMP_ATOMIC_MEMORY_ORDER (t));
18314 }
18315 else
18316 {
18317 tree op1 = TREE_OPERAND (t, 1);
18318 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18319 tree rhs1 = NULL_TREE;
18320 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18321 enum tree_code opcode = NOP_EXPR;
18322 if (code == OMP_ATOMIC_READ)
18323 {
18324 v = RECUR (TREE_OPERAND (op1, 0));
18325 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18326 }
18327 else if (code == OMP_ATOMIC_CAPTURE_OLD
18328 || code == OMP_ATOMIC_CAPTURE_NEW)
18329 {
18330 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18331 v = RECUR (TREE_OPERAND (op1, 0));
18332 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18333 if (TREE_CODE (op11) == COMPOUND_EXPR)
18334 {
18335 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18336 op11 = TREE_OPERAND (op11, 1);
18337 }
18338 lhs = RECUR (TREE_OPERAND (op11, 0));
18339 rhs = RECUR (TREE_OPERAND (op11, 1));
18340 opcode = TREE_CODE (op11);
18341 if (opcode == MODIFY_EXPR)
18342 opcode = NOP_EXPR;
18343 }
18344 else
18345 {
18346 code = OMP_ATOMIC;
18347 lhs = RECUR (TREE_OPERAND (op1, 0));
18348 rhs = RECUR (TREE_OPERAND (op1, 1));
18349 }
18350 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18351 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18352 }
18353 break;
18354
18355 case TRANSACTION_EXPR:
18356 {
18357 int flags = 0;
18358 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18359 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18360
18361 if (TRANSACTION_EXPR_IS_STMT (t))
18362 {
18363 tree body = TRANSACTION_EXPR_BODY (t);
18364 tree noex = NULL_TREE;
18365 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18366 {
18367 noex = MUST_NOT_THROW_COND (body);
18368 if (noex == NULL_TREE)
18369 noex = boolean_true_node;
18370 body = TREE_OPERAND (body, 0);
18371 }
18372 stmt = begin_transaction_stmt (input_location, NULL, flags);
18373 RECUR (body);
18374 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18375 }
18376 else
18377 {
18378 stmt = build_transaction_expr (EXPR_LOCATION (t),
18379 RECUR (TRANSACTION_EXPR_BODY (t)),
18380 flags, NULL_TREE);
18381 RETURN (stmt);
18382 }
18383 }
18384 break;
18385
18386 case MUST_NOT_THROW_EXPR:
18387 {
18388 tree op0 = RECUR (TREE_OPERAND (t, 0));
18389 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18390 RETURN (build_must_not_throw_expr (op0, cond));
18391 }
18392
18393 case EXPR_PACK_EXPANSION:
18394 error ("invalid use of pack expansion expression");
18395 RETURN (error_mark_node);
18396
18397 case NONTYPE_ARGUMENT_PACK:
18398 error ("use %<...%> to expand argument pack");
18399 RETURN (error_mark_node);
18400
18401 case COMPOUND_EXPR:
18402 tmp = RECUR (TREE_OPERAND (t, 0));
18403 if (tmp == NULL_TREE)
18404 /* If the first operand was a statement, we're done with it. */
18405 RETURN (RECUR (TREE_OPERAND (t, 1)));
18406 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18407 RECUR (TREE_OPERAND (t, 1)),
18408 complain));
18409
18410 case ANNOTATE_EXPR:
18411 tmp = RECUR (TREE_OPERAND (t, 0));
18412 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18413 TREE_TYPE (tmp), tmp,
18414 RECUR (TREE_OPERAND (t, 1)),
18415 RECUR (TREE_OPERAND (t, 2))));
18416
18417 case PREDICT_EXPR:
18418 RETURN (add_stmt (copy_node (t)));
18419
18420 default:
18421 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18422
18423 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18424 /*function_p=*/false,
18425 integral_constant_expression_p));
18426 }
18427
18428 RETURN (NULL_TREE);
18429 out:
18430 input_location = loc;
18431 return r;
18432 #undef RECUR
18433 #undef RETURN
18434 }
18435
18436 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18437 function. For description of the body see comment above
18438 cp_parser_omp_declare_reduction_exprs. */
18439
18440 static void
18441 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18442 {
18443 if (t == NULL_TREE || t == error_mark_node)
18444 return;
18445
18446 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18447
18448 tree_stmt_iterator tsi;
18449 int i;
18450 tree stmts[7];
18451 memset (stmts, 0, sizeof stmts);
18452 for (i = 0, tsi = tsi_start (t);
18453 i < 7 && !tsi_end_p (tsi);
18454 i++, tsi_next (&tsi))
18455 stmts[i] = tsi_stmt (tsi);
18456 gcc_assert (tsi_end_p (tsi));
18457
18458 if (i >= 3)
18459 {
18460 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18461 && TREE_CODE (stmts[1]) == DECL_EXPR);
18462 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18463 args, complain, in_decl);
18464 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18465 args, complain, in_decl);
18466 DECL_CONTEXT (omp_out) = current_function_decl;
18467 DECL_CONTEXT (omp_in) = current_function_decl;
18468 keep_next_level (true);
18469 tree block = begin_omp_structured_block ();
18470 tsubst_expr (stmts[2], args, complain, in_decl, false);
18471 block = finish_omp_structured_block (block);
18472 block = maybe_cleanup_point_expr_void (block);
18473 add_decl_expr (omp_out);
18474 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18475 TREE_NO_WARNING (omp_out) = 1;
18476 add_decl_expr (omp_in);
18477 finish_expr_stmt (block);
18478 }
18479 if (i >= 6)
18480 {
18481 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18482 && TREE_CODE (stmts[4]) == DECL_EXPR);
18483 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18484 args, complain, in_decl);
18485 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18486 args, complain, in_decl);
18487 DECL_CONTEXT (omp_priv) = current_function_decl;
18488 DECL_CONTEXT (omp_orig) = current_function_decl;
18489 keep_next_level (true);
18490 tree block = begin_omp_structured_block ();
18491 tsubst_expr (stmts[5], args, complain, in_decl, false);
18492 block = finish_omp_structured_block (block);
18493 block = maybe_cleanup_point_expr_void (block);
18494 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18495 add_decl_expr (omp_priv);
18496 add_decl_expr (omp_orig);
18497 finish_expr_stmt (block);
18498 if (i == 7)
18499 add_decl_expr (omp_orig);
18500 }
18501 }
18502
18503 /* T is a postfix-expression that is not being used in a function
18504 call. Return the substituted version of T. */
18505
18506 static tree
18507 tsubst_non_call_postfix_expression (tree t, tree args,
18508 tsubst_flags_t complain,
18509 tree in_decl)
18510 {
18511 if (TREE_CODE (t) == SCOPE_REF)
18512 t = tsubst_qualified_id (t, args, complain, in_decl,
18513 /*done=*/false, /*address_p=*/false);
18514 else
18515 t = tsubst_copy_and_build (t, args, complain, in_decl,
18516 /*function_p=*/false,
18517 /*integral_constant_expression_p=*/false);
18518
18519 return t;
18520 }
18521
18522 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18523 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18524 dependent init-capture. */
18525
18526 static void
18527 prepend_one_capture (tree field, tree init, tree &list,
18528 tsubst_flags_t complain)
18529 {
18530 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18531 {
18532 tree type = NULL_TREE;
18533 if (!init)
18534 {
18535 if (complain & tf_error)
18536 error ("empty initializer in lambda init-capture");
18537 init = error_mark_node;
18538 }
18539 else if (TREE_CODE (init) == TREE_LIST)
18540 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18541 if (!type)
18542 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18543 TREE_TYPE (field) = type;
18544 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18545 }
18546 list = tree_cons (field, init, list);
18547 }
18548
18549 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18550 instantiation context. Instantiating a pack expansion containing a lambda
18551 might result in multiple lambdas all based on the same lambda in the
18552 template. */
18553
18554 tree
18555 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18556 {
18557 tree oldfn = lambda_function (t);
18558 in_decl = oldfn;
18559
18560 tree r = build_lambda_expr ();
18561
18562 LAMBDA_EXPR_LOCATION (r)
18563 = LAMBDA_EXPR_LOCATION (t);
18564 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18565 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18566 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18567 LAMBDA_EXPR_INSTANTIATED (r) = true;
18568
18569 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18570 /* A lambda in a default argument outside a class gets no
18571 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18572 tsubst_default_argument calls start_lambda_scope, so we need to
18573 specifically ignore it here, and use the global scope. */
18574 record_null_lambda_scope (r);
18575 else
18576 record_lambda_scope (r);
18577
18578 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18579 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18580
18581 vec<tree,va_gc>* field_packs = NULL;
18582
18583 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18584 cap = TREE_CHAIN (cap))
18585 {
18586 tree ofield = TREE_PURPOSE (cap);
18587 if (PACK_EXPANSION_P (ofield))
18588 ofield = PACK_EXPANSION_PATTERN (ofield);
18589 tree field = tsubst_decl (ofield, args, complain);
18590
18591 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18592 {
18593 /* Remember these for when we've pushed local_specializations. */
18594 vec_safe_push (field_packs, ofield);
18595 vec_safe_push (field_packs, field);
18596 }
18597
18598 if (field == error_mark_node)
18599 return error_mark_node;
18600
18601 tree init = TREE_VALUE (cap);
18602 if (PACK_EXPANSION_P (init))
18603 init = tsubst_pack_expansion (init, args, complain, in_decl);
18604 else
18605 init = tsubst_copy_and_build (init, args, complain, in_decl,
18606 /*fn*/false, /*constexpr*/false);
18607
18608 if (TREE_CODE (field) == TREE_VEC)
18609 {
18610 int len = TREE_VEC_LENGTH (field);
18611 gcc_assert (TREE_CODE (init) == TREE_VEC
18612 && TREE_VEC_LENGTH (init) == len);
18613 for (int i = 0; i < len; ++i)
18614 prepend_one_capture (TREE_VEC_ELT (field, i),
18615 TREE_VEC_ELT (init, i),
18616 LAMBDA_EXPR_CAPTURE_LIST (r),
18617 complain);
18618 }
18619 else
18620 {
18621 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18622 complain);
18623
18624 if (id_equal (DECL_NAME (field), "__this"))
18625 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18626 }
18627 }
18628
18629 tree type = begin_lambda_type (r);
18630 if (type == error_mark_node)
18631 return error_mark_node;
18632
18633 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18634 determine_visibility (TYPE_NAME (type));
18635
18636 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18637
18638 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18639 ? DECL_TI_TEMPLATE (oldfn)
18640 : NULL_TREE);
18641
18642 tree fntype = static_fn_type (oldfn);
18643 if (oldtmpl)
18644 ++processing_template_decl;
18645 fntype = tsubst (fntype, args, complain, in_decl);
18646 if (oldtmpl)
18647 --processing_template_decl;
18648
18649 if (fntype == error_mark_node)
18650 r = error_mark_node;
18651 else
18652 {
18653 /* The body of a lambda-expression is not a subexpression of the
18654 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18655 which would be skipped if cp_unevaluated_operand. */
18656 cp_evaluated ev;
18657
18658 /* Fix the type of 'this'. */
18659 fntype = build_memfn_type (fntype, type,
18660 type_memfn_quals (fntype),
18661 type_memfn_rqual (fntype));
18662 tree fn, tmpl;
18663 if (oldtmpl)
18664 {
18665 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18666 fn = DECL_TEMPLATE_RESULT (tmpl);
18667 finish_member_declaration (tmpl);
18668 }
18669 else
18670 {
18671 tmpl = NULL_TREE;
18672 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18673 finish_member_declaration (fn);
18674 }
18675
18676 /* Let finish_function set this. */
18677 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18678
18679 bool nested = cfun;
18680 if (nested)
18681 push_function_context ();
18682 else
18683 /* Still increment function_depth so that we don't GC in the
18684 middle of an expression. */
18685 ++function_depth;
18686
18687 local_specialization_stack s (lss_copy);
18688
18689 tree body = start_lambda_function (fn, r);
18690
18691 /* Now record them for lookup_init_capture_pack. */
18692 int fplen = vec_safe_length (field_packs);
18693 for (int i = 0; i < fplen; )
18694 {
18695 tree pack = (*field_packs)[i++];
18696 tree inst = (*field_packs)[i++];
18697 register_local_specialization (inst, pack);
18698 }
18699 release_tree_vector (field_packs);
18700
18701 register_parameter_specializations (oldfn, fn);
18702
18703 if (oldtmpl)
18704 {
18705 /* We might not partially instantiate some parts of the function, so
18706 copy these flags from the original template. */
18707 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18708 current_function_returns_value = ol->returns_value;
18709 current_function_returns_null = ol->returns_null;
18710 current_function_returns_abnormally = ol->returns_abnormally;
18711 current_function_infinite_loop = ol->infinite_loop;
18712 }
18713
18714 /* [temp.deduct] A lambda-expression appearing in a function type or a
18715 template parameter is not considered part of the immediate context for
18716 the purposes of template argument deduction. */
18717 complain = tf_warning_or_error;
18718
18719 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18720 /*constexpr*/false);
18721
18722 finish_lambda_function (body);
18723
18724 if (nested)
18725 pop_function_context ();
18726 else
18727 --function_depth;
18728
18729 /* The capture list was built up in reverse order; fix that now. */
18730 LAMBDA_EXPR_CAPTURE_LIST (r)
18731 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18732
18733 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18734
18735 maybe_add_lambda_conv_op (type);
18736 }
18737
18738 finish_struct (type, /*attr*/NULL_TREE);
18739
18740 insert_pending_capture_proxies ();
18741
18742 return r;
18743 }
18744
18745 /* Like tsubst but deals with expressions and performs semantic
18746 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18747
18748 tree
18749 tsubst_copy_and_build (tree t,
18750 tree args,
18751 tsubst_flags_t complain,
18752 tree in_decl,
18753 bool function_p,
18754 bool integral_constant_expression_p)
18755 {
18756 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18757 #define RECUR(NODE) \
18758 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18759 /*function_p=*/false, \
18760 integral_constant_expression_p)
18761
18762 tree retval, op1;
18763 location_t loc;
18764
18765 if (t == NULL_TREE || t == error_mark_node)
18766 return t;
18767
18768 loc = input_location;
18769 if (location_t eloc = cp_expr_location (t))
18770 input_location = eloc;
18771
18772 /* N3276 decltype magic only applies to calls at the top level or on the
18773 right side of a comma. */
18774 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18775 complain &= ~tf_decltype;
18776
18777 switch (TREE_CODE (t))
18778 {
18779 case USING_DECL:
18780 t = DECL_NAME (t);
18781 /* Fall through. */
18782 case IDENTIFIER_NODE:
18783 {
18784 tree decl;
18785 cp_id_kind idk;
18786 bool non_integral_constant_expression_p;
18787 const char *error_msg;
18788
18789 if (IDENTIFIER_CONV_OP_P (t))
18790 {
18791 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18792 t = make_conv_op_name (new_type);
18793 }
18794
18795 /* Look up the name. */
18796 decl = lookup_name (t);
18797
18798 /* By convention, expressions use ERROR_MARK_NODE to indicate
18799 failure, not NULL_TREE. */
18800 if (decl == NULL_TREE)
18801 decl = error_mark_node;
18802
18803 decl = finish_id_expression (t, decl, NULL_TREE,
18804 &idk,
18805 integral_constant_expression_p,
18806 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18807 &non_integral_constant_expression_p,
18808 /*template_p=*/false,
18809 /*done=*/true,
18810 /*address_p=*/false,
18811 /*template_arg_p=*/false,
18812 &error_msg,
18813 input_location);
18814 if (error_msg)
18815 error (error_msg);
18816 if (!function_p && identifier_p (decl))
18817 {
18818 if (complain & tf_error)
18819 unqualified_name_lookup_error (decl);
18820 decl = error_mark_node;
18821 }
18822 RETURN (decl);
18823 }
18824
18825 case TEMPLATE_ID_EXPR:
18826 {
18827 tree object;
18828 tree templ = RECUR (TREE_OPERAND (t, 0));
18829 tree targs = TREE_OPERAND (t, 1);
18830
18831 if (targs)
18832 targs = tsubst_template_args (targs, args, complain, in_decl);
18833 if (targs == error_mark_node)
18834 RETURN (error_mark_node);
18835
18836 if (TREE_CODE (templ) == SCOPE_REF)
18837 {
18838 tree name = TREE_OPERAND (templ, 1);
18839 tree tid = lookup_template_function (name, targs);
18840 TREE_OPERAND (templ, 1) = tid;
18841 RETURN (templ);
18842 }
18843
18844 if (concept_definition_p (templ))
18845 {
18846 tree check = build_concept_check (templ, targs, complain);
18847 if (check == error_mark_node)
18848 RETURN (error_mark_node);
18849
18850 tree id = unpack_concept_check (check);
18851
18852 /* If we built a function concept check, return the underlying
18853 template-id. So we can evaluate it as a function call. */
18854 if (function_concept_p (TREE_OPERAND (id, 0)))
18855 RETURN (id);
18856
18857 /* Evaluate the concept, if needed. */
18858 tree args = TREE_OPERAND (id, 1);
18859 if (!uses_template_parms (args)
18860 && !processing_constraint_expression_p ())
18861 RETURN (evaluate_concept_check (check, complain));
18862
18863 RETURN (check);
18864 }
18865
18866 if (variable_template_p (templ))
18867 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18868
18869 if (TREE_CODE (templ) == COMPONENT_REF)
18870 {
18871 object = TREE_OPERAND (templ, 0);
18872 templ = TREE_OPERAND (templ, 1);
18873 }
18874 else
18875 object = NULL_TREE;
18876 templ = lookup_template_function (templ, targs);
18877
18878 if (object)
18879 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18880 object, templ, NULL_TREE));
18881 else
18882 RETURN (baselink_for_fns (templ));
18883 }
18884
18885 case INDIRECT_REF:
18886 {
18887 tree r = RECUR (TREE_OPERAND (t, 0));
18888
18889 if (REFERENCE_REF_P (t))
18890 {
18891 /* A type conversion to reference type will be enclosed in
18892 such an indirect ref, but the substitution of the cast
18893 will have also added such an indirect ref. */
18894 r = convert_from_reference (r);
18895 }
18896 else
18897 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18898 complain|decltype_flag);
18899
18900 if (REF_PARENTHESIZED_P (t))
18901 r = force_paren_expr (r);
18902
18903 RETURN (r);
18904 }
18905
18906 case NOP_EXPR:
18907 {
18908 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18909 tree op0 = RECUR (TREE_OPERAND (t, 0));
18910 RETURN (build_nop (type, op0));
18911 }
18912
18913 case IMPLICIT_CONV_EXPR:
18914 {
18915 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18916 tree expr = RECUR (TREE_OPERAND (t, 0));
18917 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18918 {
18919 retval = copy_node (t);
18920 TREE_TYPE (retval) = type;
18921 TREE_OPERAND (retval, 0) = expr;
18922 RETURN (retval);
18923 }
18924 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18925 /* We'll pass this to convert_nontype_argument again, we don't need
18926 to actually perform any conversion here. */
18927 RETURN (expr);
18928 int flags = LOOKUP_IMPLICIT;
18929 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18930 flags = LOOKUP_NORMAL;
18931 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18932 flags |= LOOKUP_NO_NARROWING;
18933 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18934 flags));
18935 }
18936
18937 case CONVERT_EXPR:
18938 {
18939 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18940 tree op0 = RECUR (TREE_OPERAND (t, 0));
18941 if (op0 == error_mark_node)
18942 RETURN (error_mark_node);
18943 RETURN (build1 (CONVERT_EXPR, type, op0));
18944 }
18945
18946 case CAST_EXPR:
18947 case REINTERPRET_CAST_EXPR:
18948 case CONST_CAST_EXPR:
18949 case DYNAMIC_CAST_EXPR:
18950 case STATIC_CAST_EXPR:
18951 {
18952 tree type;
18953 tree op, r = NULL_TREE;
18954
18955 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18956 if (integral_constant_expression_p
18957 && !cast_valid_in_integral_constant_expression_p (type))
18958 {
18959 if (complain & tf_error)
18960 error ("a cast to a type other than an integral or "
18961 "enumeration type cannot appear in a constant-expression");
18962 RETURN (error_mark_node);
18963 }
18964
18965 op = RECUR (TREE_OPERAND (t, 0));
18966
18967 warning_sentinel s(warn_useless_cast);
18968 warning_sentinel s2(warn_ignored_qualifiers);
18969 switch (TREE_CODE (t))
18970 {
18971 case CAST_EXPR:
18972 r = build_functional_cast (type, op, complain);
18973 break;
18974 case REINTERPRET_CAST_EXPR:
18975 r = build_reinterpret_cast (type, op, complain);
18976 break;
18977 case CONST_CAST_EXPR:
18978 r = build_const_cast (type, op, complain);
18979 break;
18980 case DYNAMIC_CAST_EXPR:
18981 r = build_dynamic_cast (type, op, complain);
18982 break;
18983 case STATIC_CAST_EXPR:
18984 r = build_static_cast (type, op, complain);
18985 break;
18986 default:
18987 gcc_unreachable ();
18988 }
18989
18990 RETURN (r);
18991 }
18992
18993 case POSTDECREMENT_EXPR:
18994 case POSTINCREMENT_EXPR:
18995 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18996 args, complain, in_decl);
18997 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18998 complain|decltype_flag));
18999
19000 case PREDECREMENT_EXPR:
19001 case PREINCREMENT_EXPR:
19002 case NEGATE_EXPR:
19003 case BIT_NOT_EXPR:
19004 case ABS_EXPR:
19005 case TRUTH_NOT_EXPR:
19006 case UNARY_PLUS_EXPR: /* Unary + */
19007 case REALPART_EXPR:
19008 case IMAGPART_EXPR:
19009 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19010 RECUR (TREE_OPERAND (t, 0)),
19011 complain|decltype_flag));
19012
19013 case FIX_TRUNC_EXPR:
19014 gcc_unreachable ();
19015
19016 case ADDR_EXPR:
19017 op1 = TREE_OPERAND (t, 0);
19018 if (TREE_CODE (op1) == LABEL_DECL)
19019 RETURN (finish_label_address_expr (DECL_NAME (op1),
19020 EXPR_LOCATION (op1)));
19021 if (TREE_CODE (op1) == SCOPE_REF)
19022 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19023 /*done=*/true, /*address_p=*/true);
19024 else
19025 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19026 in_decl);
19027 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19028 complain|decltype_flag));
19029
19030 case PLUS_EXPR:
19031 case MINUS_EXPR:
19032 case MULT_EXPR:
19033 case TRUNC_DIV_EXPR:
19034 case CEIL_DIV_EXPR:
19035 case FLOOR_DIV_EXPR:
19036 case ROUND_DIV_EXPR:
19037 case EXACT_DIV_EXPR:
19038 case BIT_AND_EXPR:
19039 case BIT_IOR_EXPR:
19040 case BIT_XOR_EXPR:
19041 case TRUNC_MOD_EXPR:
19042 case FLOOR_MOD_EXPR:
19043 case TRUTH_ANDIF_EXPR:
19044 case TRUTH_ORIF_EXPR:
19045 case TRUTH_AND_EXPR:
19046 case TRUTH_OR_EXPR:
19047 case RSHIFT_EXPR:
19048 case LSHIFT_EXPR:
19049 case EQ_EXPR:
19050 case NE_EXPR:
19051 case MAX_EXPR:
19052 case MIN_EXPR:
19053 case LE_EXPR:
19054 case GE_EXPR:
19055 case LT_EXPR:
19056 case GT_EXPR:
19057 case SPACESHIP_EXPR:
19058 case MEMBER_REF:
19059 case DOTSTAR_EXPR:
19060 {
19061 warning_sentinel s1(warn_type_limits);
19062 warning_sentinel s2(warn_div_by_zero);
19063 warning_sentinel s3(warn_logical_op);
19064 warning_sentinel s4(warn_tautological_compare);
19065 tree op0 = RECUR (TREE_OPERAND (t, 0));
19066 tree op1 = RECUR (TREE_OPERAND (t, 1));
19067 tree r = build_x_binary_op
19068 (input_location, TREE_CODE (t),
19069 op0,
19070 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19071 ? ERROR_MARK
19072 : TREE_CODE (TREE_OPERAND (t, 0))),
19073 op1,
19074 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19075 ? ERROR_MARK
19076 : TREE_CODE (TREE_OPERAND (t, 1))),
19077 /*overload=*/NULL,
19078 complain|decltype_flag);
19079 if (EXPR_P (r) && TREE_NO_WARNING (t))
19080 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19081
19082 RETURN (r);
19083 }
19084
19085 case POINTER_PLUS_EXPR:
19086 {
19087 tree op0 = RECUR (TREE_OPERAND (t, 0));
19088 tree op1 = RECUR (TREE_OPERAND (t, 1));
19089 RETURN (fold_build_pointer_plus (op0, op1));
19090 }
19091
19092 case SCOPE_REF:
19093 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19094 /*address_p=*/false));
19095 case ARRAY_REF:
19096 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19097 args, complain, in_decl);
19098 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19099 RECUR (TREE_OPERAND (t, 1)),
19100 complain|decltype_flag));
19101
19102 case SIZEOF_EXPR:
19103 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19104 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19105 RETURN (tsubst_copy (t, args, complain, in_decl));
19106 /* Fall through */
19107
19108 case ALIGNOF_EXPR:
19109 {
19110 tree r;
19111
19112 op1 = TREE_OPERAND (t, 0);
19113 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19114 op1 = TREE_TYPE (op1);
19115 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19116 && ALIGNOF_EXPR_STD_P (t));
19117 if (!args)
19118 {
19119 /* When there are no ARGS, we are trying to evaluate a
19120 non-dependent expression from the parser. Trying to do
19121 the substitutions may not work. */
19122 if (!TYPE_P (op1))
19123 op1 = TREE_TYPE (op1);
19124 }
19125 else
19126 {
19127 ++cp_unevaluated_operand;
19128 ++c_inhibit_evaluation_warnings;
19129 if (TYPE_P (op1))
19130 op1 = tsubst (op1, args, complain, in_decl);
19131 else
19132 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19133 /*function_p=*/false,
19134 /*integral_constant_expression_p=*/
19135 false);
19136 --cp_unevaluated_operand;
19137 --c_inhibit_evaluation_warnings;
19138 }
19139 if (TYPE_P (op1))
19140 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
19141 complain & tf_error);
19142 else
19143 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
19144 complain & tf_error);
19145 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19146 {
19147 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19148 {
19149 if (!processing_template_decl && TYPE_P (op1))
19150 {
19151 r = build_min (SIZEOF_EXPR, size_type_node,
19152 build1 (NOP_EXPR, op1, error_mark_node));
19153 SIZEOF_EXPR_TYPE_P (r) = 1;
19154 }
19155 else
19156 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19157 TREE_SIDE_EFFECTS (r) = 0;
19158 TREE_READONLY (r) = 1;
19159 }
19160 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19161 }
19162 RETURN (r);
19163 }
19164
19165 case AT_ENCODE_EXPR:
19166 {
19167 op1 = TREE_OPERAND (t, 0);
19168 ++cp_unevaluated_operand;
19169 ++c_inhibit_evaluation_warnings;
19170 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19171 /*function_p=*/false,
19172 /*integral_constant_expression_p=*/false);
19173 --cp_unevaluated_operand;
19174 --c_inhibit_evaluation_warnings;
19175 RETURN (objc_build_encode_expr (op1));
19176 }
19177
19178 case NOEXCEPT_EXPR:
19179 op1 = TREE_OPERAND (t, 0);
19180 ++cp_unevaluated_operand;
19181 ++c_inhibit_evaluation_warnings;
19182 ++cp_noexcept_operand;
19183 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19184 /*function_p=*/false,
19185 /*integral_constant_expression_p=*/false);
19186 --cp_unevaluated_operand;
19187 --c_inhibit_evaluation_warnings;
19188 --cp_noexcept_operand;
19189 RETURN (finish_noexcept_expr (op1, complain));
19190
19191 case MODOP_EXPR:
19192 {
19193 warning_sentinel s(warn_div_by_zero);
19194 tree lhs = RECUR (TREE_OPERAND (t, 0));
19195 tree rhs = RECUR (TREE_OPERAND (t, 2));
19196 tree r = build_x_modify_expr
19197 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19198 complain|decltype_flag);
19199 /* TREE_NO_WARNING must be set if either the expression was
19200 parenthesized or it uses an operator such as >>= rather
19201 than plain assignment. In the former case, it was already
19202 set and must be copied. In the latter case,
19203 build_x_modify_expr sets it and it must not be reset
19204 here. */
19205 if (TREE_NO_WARNING (t))
19206 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19207
19208 RETURN (r);
19209 }
19210
19211 case ARROW_EXPR:
19212 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19213 args, complain, in_decl);
19214 /* Remember that there was a reference to this entity. */
19215 if (DECL_P (op1)
19216 && !mark_used (op1, complain) && !(complain & tf_error))
19217 RETURN (error_mark_node);
19218 RETURN (build_x_arrow (input_location, op1, complain));
19219
19220 case NEW_EXPR:
19221 {
19222 tree placement = RECUR (TREE_OPERAND (t, 0));
19223 tree init = RECUR (TREE_OPERAND (t, 3));
19224 vec<tree, va_gc> *placement_vec;
19225 vec<tree, va_gc> *init_vec;
19226 tree ret;
19227
19228 if (placement == NULL_TREE)
19229 placement_vec = NULL;
19230 else
19231 {
19232 placement_vec = make_tree_vector ();
19233 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19234 vec_safe_push (placement_vec, TREE_VALUE (placement));
19235 }
19236
19237 /* If there was an initializer in the original tree, but it
19238 instantiated to an empty list, then we should pass a
19239 non-NULL empty vector to tell build_new that it was an
19240 empty initializer() rather than no initializer. This can
19241 only happen when the initializer is a pack expansion whose
19242 parameter packs are of length zero. */
19243 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19244 init_vec = NULL;
19245 else
19246 {
19247 init_vec = make_tree_vector ();
19248 if (init == void_node)
19249 gcc_assert (init_vec != NULL);
19250 else
19251 {
19252 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19253 vec_safe_push (init_vec, TREE_VALUE (init));
19254 }
19255 }
19256
19257 /* Avoid passing an enclosing decl to valid_array_size_p. */
19258 in_decl = NULL_TREE;
19259
19260 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19261 tree op2 = RECUR (TREE_OPERAND (t, 2));
19262 ret = build_new (&placement_vec, op1, op2, &init_vec,
19263 NEW_EXPR_USE_GLOBAL (t),
19264 complain);
19265
19266 if (placement_vec != NULL)
19267 release_tree_vector (placement_vec);
19268 if (init_vec != NULL)
19269 release_tree_vector (init_vec);
19270
19271 RETURN (ret);
19272 }
19273
19274 case DELETE_EXPR:
19275 {
19276 tree op0 = RECUR (TREE_OPERAND (t, 0));
19277 tree op1 = RECUR (TREE_OPERAND (t, 1));
19278 RETURN (delete_sanity (op0, op1,
19279 DELETE_EXPR_USE_VEC (t),
19280 DELETE_EXPR_USE_GLOBAL (t),
19281 complain));
19282 }
19283
19284 case COMPOUND_EXPR:
19285 {
19286 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19287 complain & ~tf_decltype, in_decl,
19288 /*function_p=*/false,
19289 integral_constant_expression_p);
19290 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19291 op0,
19292 RECUR (TREE_OPERAND (t, 1)),
19293 complain|decltype_flag));
19294 }
19295
19296 case CALL_EXPR:
19297 {
19298 tree function;
19299 unsigned int nargs, i;
19300 bool qualified_p;
19301 bool koenig_p;
19302 tree ret;
19303
19304 function = CALL_EXPR_FN (t);
19305 /* Internal function with no arguments. */
19306 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19307 RETURN (t);
19308
19309 /* When we parsed the expression, we determined whether or
19310 not Koenig lookup should be performed. */
19311 koenig_p = KOENIG_LOOKUP_P (t);
19312 if (function == NULL_TREE)
19313 {
19314 koenig_p = false;
19315 qualified_p = false;
19316 }
19317 else if (TREE_CODE (function) == SCOPE_REF)
19318 {
19319 qualified_p = true;
19320 function = tsubst_qualified_id (function, args, complain, in_decl,
19321 /*done=*/false,
19322 /*address_p=*/false);
19323 }
19324 else if (koenig_p && identifier_p (function))
19325 {
19326 /* Do nothing; calling tsubst_copy_and_build on an identifier
19327 would incorrectly perform unqualified lookup again.
19328
19329 Note that we can also have an IDENTIFIER_NODE if the earlier
19330 unqualified lookup found a member function; in that case
19331 koenig_p will be false and we do want to do the lookup
19332 again to find the instantiated member function.
19333
19334 FIXME but doing that causes c++/15272, so we need to stop
19335 using IDENTIFIER_NODE in that situation. */
19336 qualified_p = false;
19337 }
19338 else
19339 {
19340 if (TREE_CODE (function) == COMPONENT_REF)
19341 {
19342 tree op = TREE_OPERAND (function, 1);
19343
19344 qualified_p = (TREE_CODE (op) == SCOPE_REF
19345 || (BASELINK_P (op)
19346 && BASELINK_QUALIFIED_P (op)));
19347 }
19348 else
19349 qualified_p = false;
19350
19351 if (TREE_CODE (function) == ADDR_EXPR
19352 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19353 /* Avoid error about taking the address of a constructor. */
19354 function = TREE_OPERAND (function, 0);
19355
19356 function = tsubst_copy_and_build (function, args, complain,
19357 in_decl,
19358 !qualified_p,
19359 integral_constant_expression_p);
19360
19361 if (BASELINK_P (function))
19362 qualified_p = true;
19363 }
19364
19365 nargs = call_expr_nargs (t);
19366 releasing_vec call_args;
19367 for (i = 0; i < nargs; ++i)
19368 {
19369 tree arg = CALL_EXPR_ARG (t, i);
19370
19371 if (!PACK_EXPANSION_P (arg))
19372 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19373 else
19374 {
19375 /* Expand the pack expansion and push each entry onto
19376 CALL_ARGS. */
19377 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19378 if (TREE_CODE (arg) == TREE_VEC)
19379 {
19380 unsigned int len, j;
19381
19382 len = TREE_VEC_LENGTH (arg);
19383 for (j = 0; j < len; ++j)
19384 {
19385 tree value = TREE_VEC_ELT (arg, j);
19386 if (value != NULL_TREE)
19387 value = convert_from_reference (value);
19388 vec_safe_push (call_args, value);
19389 }
19390 }
19391 else
19392 {
19393 /* A partial substitution. Add one entry. */
19394 vec_safe_push (call_args, arg);
19395 }
19396 }
19397 }
19398
19399 /* Stripped-down processing for a call in a thunk. Specifically, in
19400 the thunk template for a generic lambda. */
19401 if (CALL_FROM_THUNK_P (t))
19402 {
19403 /* Now that we've expanded any packs, the number of call args
19404 might be different. */
19405 unsigned int cargs = call_args->length ();
19406 tree thisarg = NULL_TREE;
19407 if (TREE_CODE (function) == COMPONENT_REF)
19408 {
19409 thisarg = TREE_OPERAND (function, 0);
19410 if (TREE_CODE (thisarg) == INDIRECT_REF)
19411 thisarg = TREE_OPERAND (thisarg, 0);
19412 function = TREE_OPERAND (function, 1);
19413 if (TREE_CODE (function) == BASELINK)
19414 function = BASELINK_FUNCTIONS (function);
19415 }
19416 /* We aren't going to do normal overload resolution, so force the
19417 template-id to resolve. */
19418 function = resolve_nondeduced_context (function, complain);
19419 for (unsigned i = 0; i < cargs; ++i)
19420 {
19421 /* In a thunk, pass through args directly, without any
19422 conversions. */
19423 tree arg = (*call_args)[i];
19424 while (TREE_CODE (arg) != PARM_DECL)
19425 arg = TREE_OPERAND (arg, 0);
19426 (*call_args)[i] = arg;
19427 }
19428 if (thisarg)
19429 {
19430 /* If there are no other args, just push 'this'. */
19431 if (cargs == 0)
19432 vec_safe_push (call_args, thisarg);
19433 else
19434 {
19435 /* Otherwise, shift the other args over to make room. */
19436 tree last = (*call_args)[cargs - 1];
19437 vec_safe_push (call_args, last);
19438 for (int i = cargs - 1; i > 0; --i)
19439 (*call_args)[i] = (*call_args)[i - 1];
19440 (*call_args)[0] = thisarg;
19441 }
19442 }
19443 ret = build_call_a (function, call_args->length (),
19444 call_args->address ());
19445 /* The thunk location is not interesting. */
19446 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19447 CALL_FROM_THUNK_P (ret) = true;
19448 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19449 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19450
19451 RETURN (ret);
19452 }
19453
19454 /* We do not perform argument-dependent lookup if normal
19455 lookup finds a non-function, in accordance with the
19456 expected resolution of DR 218. */
19457 if (koenig_p
19458 && ((is_overloaded_fn (function)
19459 /* If lookup found a member function, the Koenig lookup is
19460 not appropriate, even if an unqualified-name was used
19461 to denote the function. */
19462 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19463 || identifier_p (function))
19464 /* Only do this when substitution turns a dependent call
19465 into a non-dependent call. */
19466 && type_dependent_expression_p_push (t)
19467 && !any_type_dependent_arguments_p (call_args))
19468 function = perform_koenig_lookup (function, call_args, tf_none);
19469
19470 if (function != NULL_TREE
19471 && identifier_p (function)
19472 && !any_type_dependent_arguments_p (call_args))
19473 {
19474 if (koenig_p && (complain & tf_warning_or_error))
19475 {
19476 /* For backwards compatibility and good diagnostics, try
19477 the unqualified lookup again if we aren't in SFINAE
19478 context. */
19479 tree unq = (tsubst_copy_and_build
19480 (function, args, complain, in_decl, true,
19481 integral_constant_expression_p));
19482 if (unq == error_mark_node)
19483 RETURN (error_mark_node);
19484
19485 if (unq != function)
19486 {
19487 /* In a lambda fn, we have to be careful to not
19488 introduce new this captures. Legacy code can't
19489 be using lambdas anyway, so it's ok to be
19490 stricter. */
19491 bool in_lambda = (current_class_type
19492 && LAMBDA_TYPE_P (current_class_type));
19493 char const *const msg
19494 = G_("%qD was not declared in this scope, "
19495 "and no declarations were found by "
19496 "argument-dependent lookup at the point "
19497 "of instantiation");
19498
19499 bool diag = true;
19500 if (in_lambda)
19501 error_at (cp_expr_loc_or_input_loc (t),
19502 msg, function);
19503 else
19504 diag = permerror (cp_expr_loc_or_input_loc (t),
19505 msg, function);
19506 if (diag)
19507 {
19508 tree fn = unq;
19509
19510 if (INDIRECT_REF_P (fn))
19511 fn = TREE_OPERAND (fn, 0);
19512 if (is_overloaded_fn (fn))
19513 fn = get_first_fn (fn);
19514
19515 if (!DECL_P (fn))
19516 /* Can't say anything more. */;
19517 else if (DECL_CLASS_SCOPE_P (fn))
19518 {
19519 location_t loc = cp_expr_loc_or_input_loc (t);
19520 inform (loc,
19521 "declarations in dependent base %qT are "
19522 "not found by unqualified lookup",
19523 DECL_CLASS_CONTEXT (fn));
19524 if (current_class_ptr)
19525 inform (loc,
19526 "use %<this->%D%> instead", function);
19527 else
19528 inform (loc,
19529 "use %<%T::%D%> instead",
19530 current_class_name, function);
19531 }
19532 else
19533 inform (DECL_SOURCE_LOCATION (fn),
19534 "%qD declared here, later in the "
19535 "translation unit", fn);
19536 if (in_lambda)
19537 RETURN (error_mark_node);
19538 }
19539
19540 function = unq;
19541 }
19542 }
19543 if (identifier_p (function))
19544 {
19545 if (complain & tf_error)
19546 unqualified_name_lookup_error (function);
19547 RETURN (error_mark_node);
19548 }
19549 }
19550
19551 /* Remember that there was a reference to this entity. */
19552 if (function != NULL_TREE
19553 && DECL_P (function)
19554 && !mark_used (function, complain) && !(complain & tf_error))
19555 RETURN (error_mark_node);
19556
19557 /* Put back tf_decltype for the actual call. */
19558 complain |= decltype_flag;
19559
19560 if (function == NULL_TREE)
19561 switch (CALL_EXPR_IFN (t))
19562 {
19563 case IFN_LAUNDER:
19564 gcc_assert (nargs == 1);
19565 if (vec_safe_length (call_args) != 1)
19566 {
19567 error_at (cp_expr_loc_or_input_loc (t),
19568 "wrong number of arguments to "
19569 "%<__builtin_launder%>");
19570 ret = error_mark_node;
19571 }
19572 else
19573 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19574 (*call_args)[0], complain);
19575 break;
19576
19577 case IFN_VEC_CONVERT:
19578 gcc_assert (nargs == 1);
19579 if (vec_safe_length (call_args) != 1)
19580 {
19581 error_at (cp_expr_loc_or_input_loc (t),
19582 "wrong number of arguments to "
19583 "%<__builtin_convertvector%>");
19584 ret = error_mark_node;
19585 break;
19586 }
19587 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19588 tsubst (TREE_TYPE (t), args,
19589 complain, in_decl),
19590 complain);
19591 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19592 RETURN (ret);
19593 break;
19594
19595 default:
19596 /* Unsupported internal function with arguments. */
19597 gcc_unreachable ();
19598 }
19599 else if (TREE_CODE (function) == OFFSET_REF
19600 || TREE_CODE (function) == DOTSTAR_EXPR
19601 || TREE_CODE (function) == MEMBER_REF)
19602 ret = build_offset_ref_call_from_tree (function, &call_args,
19603 complain);
19604 else if (TREE_CODE (function) == COMPONENT_REF)
19605 {
19606 tree instance = TREE_OPERAND (function, 0);
19607 tree fn = TREE_OPERAND (function, 1);
19608
19609 if (processing_template_decl
19610 && (type_dependent_expression_p (instance)
19611 || (!BASELINK_P (fn)
19612 && TREE_CODE (fn) != FIELD_DECL)
19613 || type_dependent_expression_p (fn)
19614 || any_type_dependent_arguments_p (call_args)))
19615 ret = build_min_nt_call_vec (function, call_args);
19616 else if (!BASELINK_P (fn))
19617 ret = finish_call_expr (function, &call_args,
19618 /*disallow_virtual=*/false,
19619 /*koenig_p=*/false,
19620 complain);
19621 else
19622 ret = (build_new_method_call
19623 (instance, fn,
19624 &call_args, NULL_TREE,
19625 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19626 /*fn_p=*/NULL,
19627 complain));
19628 }
19629 else if (concept_check_p (function))
19630 {
19631 /* FUNCTION is a template-id referring to a concept definition. */
19632 tree id = unpack_concept_check (function);
19633 tree tmpl = TREE_OPERAND (id, 0);
19634 tree args = TREE_OPERAND (id, 1);
19635
19636 /* Calls to standard and variable concepts should have been
19637 previously diagnosed. */
19638 gcc_assert (function_concept_p (tmpl));
19639
19640 /* Ensure the result is wrapped as a call expression. */
19641 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19642
19643 /* Possibly evaluate the check if it is non-dependent. */
19644 if (!uses_template_parms (args)
19645 && !processing_constraint_expression_p ())
19646 ret = evaluate_concept_check (ret, complain);
19647 }
19648 else
19649 ret = finish_call_expr (function, &call_args,
19650 /*disallow_virtual=*/qualified_p,
19651 koenig_p,
19652 complain);
19653
19654 if (ret != error_mark_node)
19655 {
19656 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19657 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19658 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19659 if (op || ord || rev)
19660 {
19661 function = extract_call_expr (ret);
19662 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19663 CALL_EXPR_ORDERED_ARGS (function) = ord;
19664 CALL_EXPR_REVERSE_ARGS (function) = rev;
19665 }
19666 }
19667
19668 RETURN (ret);
19669 }
19670
19671 case COND_EXPR:
19672 {
19673 tree cond = RECUR (TREE_OPERAND (t, 0));
19674 cond = mark_rvalue_use (cond);
19675 tree folded_cond = fold_non_dependent_expr (cond, complain);
19676 tree exp1, exp2;
19677
19678 if (TREE_CODE (folded_cond) == INTEGER_CST)
19679 {
19680 if (integer_zerop (folded_cond))
19681 {
19682 ++c_inhibit_evaluation_warnings;
19683 exp1 = RECUR (TREE_OPERAND (t, 1));
19684 --c_inhibit_evaluation_warnings;
19685 exp2 = RECUR (TREE_OPERAND (t, 2));
19686 }
19687 else
19688 {
19689 exp1 = RECUR (TREE_OPERAND (t, 1));
19690 ++c_inhibit_evaluation_warnings;
19691 exp2 = RECUR (TREE_OPERAND (t, 2));
19692 --c_inhibit_evaluation_warnings;
19693 }
19694 cond = folded_cond;
19695 }
19696 else
19697 {
19698 exp1 = RECUR (TREE_OPERAND (t, 1));
19699 exp2 = RECUR (TREE_OPERAND (t, 2));
19700 }
19701
19702 warning_sentinel s(warn_duplicated_branches);
19703 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19704 cond, exp1, exp2, complain));
19705 }
19706
19707 case PSEUDO_DTOR_EXPR:
19708 {
19709 tree op0 = RECUR (TREE_OPERAND (t, 0));
19710 tree op1 = RECUR (TREE_OPERAND (t, 1));
19711 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19712 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19713 input_location));
19714 }
19715
19716 case TREE_LIST:
19717 {
19718 tree purpose, value, chain;
19719
19720 if (t == void_list_node)
19721 RETURN (t);
19722
19723 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19724 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19725 {
19726 /* We have pack expansions, so expand those and
19727 create a new list out of it. */
19728 tree purposevec = NULL_TREE;
19729 tree valuevec = NULL_TREE;
19730 tree chain;
19731 int i, len = -1;
19732
19733 /* Expand the argument expressions. */
19734 if (TREE_PURPOSE (t))
19735 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19736 complain, in_decl);
19737 if (TREE_VALUE (t))
19738 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19739 complain, in_decl);
19740
19741 /* Build the rest of the list. */
19742 chain = TREE_CHAIN (t);
19743 if (chain && chain != void_type_node)
19744 chain = RECUR (chain);
19745
19746 /* Determine the number of arguments. */
19747 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19748 {
19749 len = TREE_VEC_LENGTH (purposevec);
19750 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19751 }
19752 else if (TREE_CODE (valuevec) == TREE_VEC)
19753 len = TREE_VEC_LENGTH (valuevec);
19754 else
19755 {
19756 /* Since we only performed a partial substitution into
19757 the argument pack, we only RETURN (a single list
19758 node. */
19759 if (purposevec == TREE_PURPOSE (t)
19760 && valuevec == TREE_VALUE (t)
19761 && chain == TREE_CHAIN (t))
19762 RETURN (t);
19763
19764 RETURN (tree_cons (purposevec, valuevec, chain));
19765 }
19766
19767 /* Convert the argument vectors into a TREE_LIST */
19768 i = len;
19769 while (i > 0)
19770 {
19771 /* Grab the Ith values. */
19772 i--;
19773 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19774 : NULL_TREE;
19775 value
19776 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19777 : NULL_TREE;
19778
19779 /* Build the list (backwards). */
19780 chain = tree_cons (purpose, value, chain);
19781 }
19782
19783 RETURN (chain);
19784 }
19785
19786 purpose = TREE_PURPOSE (t);
19787 if (purpose)
19788 purpose = RECUR (purpose);
19789 value = TREE_VALUE (t);
19790 if (value)
19791 value = RECUR (value);
19792 chain = TREE_CHAIN (t);
19793 if (chain && chain != void_type_node)
19794 chain = RECUR (chain);
19795 if (purpose == TREE_PURPOSE (t)
19796 && value == TREE_VALUE (t)
19797 && chain == TREE_CHAIN (t))
19798 RETURN (t);
19799 RETURN (tree_cons (purpose, value, chain));
19800 }
19801
19802 case COMPONENT_REF:
19803 {
19804 tree object;
19805 tree object_type;
19806 tree member;
19807 tree r;
19808
19809 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19810 args, complain, in_decl);
19811 /* Remember that there was a reference to this entity. */
19812 if (DECL_P (object)
19813 && !mark_used (object, complain) && !(complain & tf_error))
19814 RETURN (error_mark_node);
19815 object_type = TREE_TYPE (object);
19816
19817 member = TREE_OPERAND (t, 1);
19818 if (BASELINK_P (member))
19819 member = tsubst_baselink (member,
19820 non_reference (TREE_TYPE (object)),
19821 args, complain, in_decl);
19822 else
19823 member = tsubst_copy (member, args, complain, in_decl);
19824 if (member == error_mark_node)
19825 RETURN (error_mark_node);
19826
19827 if (TREE_CODE (member) == FIELD_DECL)
19828 {
19829 r = finish_non_static_data_member (member, object, NULL_TREE);
19830 if (TREE_CODE (r) == COMPONENT_REF)
19831 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19832 RETURN (r);
19833 }
19834 else if (type_dependent_expression_p (object))
19835 /* We can't do much here. */;
19836 else if (!CLASS_TYPE_P (object_type))
19837 {
19838 if (scalarish_type_p (object_type))
19839 {
19840 tree s = NULL_TREE;
19841 tree dtor = member;
19842
19843 if (TREE_CODE (dtor) == SCOPE_REF)
19844 {
19845 s = TREE_OPERAND (dtor, 0);
19846 dtor = TREE_OPERAND (dtor, 1);
19847 }
19848 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19849 {
19850 dtor = TREE_OPERAND (dtor, 0);
19851 if (TYPE_P (dtor))
19852 RETURN (finish_pseudo_destructor_expr
19853 (object, s, dtor, input_location));
19854 }
19855 }
19856 }
19857 else if (TREE_CODE (member) == SCOPE_REF
19858 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19859 {
19860 /* Lookup the template functions now that we know what the
19861 scope is. */
19862 tree scope = TREE_OPERAND (member, 0);
19863 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19864 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19865 member = lookup_qualified_name (scope, tmpl,
19866 /*is_type_p=*/false,
19867 /*complain=*/false);
19868 if (BASELINK_P (member))
19869 {
19870 BASELINK_FUNCTIONS (member)
19871 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19872 args);
19873 member = (adjust_result_of_qualified_name_lookup
19874 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19875 object_type));
19876 }
19877 else
19878 {
19879 qualified_name_lookup_error (scope, tmpl, member,
19880 input_location);
19881 RETURN (error_mark_node);
19882 }
19883 }
19884 else if (TREE_CODE (member) == SCOPE_REF
19885 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19886 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19887 {
19888 if (complain & tf_error)
19889 {
19890 if (TYPE_P (TREE_OPERAND (member, 0)))
19891 error ("%qT is not a class or namespace",
19892 TREE_OPERAND (member, 0));
19893 else
19894 error ("%qD is not a class or namespace",
19895 TREE_OPERAND (member, 0));
19896 }
19897 RETURN (error_mark_node);
19898 }
19899
19900 r = finish_class_member_access_expr (object, member,
19901 /*template_p=*/false,
19902 complain);
19903 if (TREE_CODE (r) == COMPONENT_REF)
19904 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19905 RETURN (r);
19906 }
19907
19908 case THROW_EXPR:
19909 RETURN (build_throw
19910 (RECUR (TREE_OPERAND (t, 0))));
19911
19912 case CONSTRUCTOR:
19913 {
19914 vec<constructor_elt, va_gc> *n;
19915 constructor_elt *ce;
19916 unsigned HOST_WIDE_INT idx;
19917 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19918 bool process_index_p;
19919 int newlen;
19920 bool need_copy_p = false;
19921 tree r;
19922
19923 if (type == error_mark_node)
19924 RETURN (error_mark_node);
19925
19926 /* We do not want to process the index of aggregate
19927 initializers as they are identifier nodes which will be
19928 looked up by digest_init. */
19929 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19930
19931 if (null_member_pointer_value_p (t))
19932 {
19933 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19934 RETURN (t);
19935 }
19936
19937 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19938 newlen = vec_safe_length (n);
19939 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19940 {
19941 if (ce->index && process_index_p
19942 /* An identifier index is looked up in the type
19943 being initialized, not the current scope. */
19944 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19945 ce->index = RECUR (ce->index);
19946
19947 if (PACK_EXPANSION_P (ce->value))
19948 {
19949 /* Substitute into the pack expansion. */
19950 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19951 in_decl);
19952
19953 if (ce->value == error_mark_node
19954 || PACK_EXPANSION_P (ce->value))
19955 ;
19956 else if (TREE_VEC_LENGTH (ce->value) == 1)
19957 /* Just move the argument into place. */
19958 ce->value = TREE_VEC_ELT (ce->value, 0);
19959 else
19960 {
19961 /* Update the length of the final CONSTRUCTOR
19962 arguments vector, and note that we will need to
19963 copy.*/
19964 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19965 need_copy_p = true;
19966 }
19967 }
19968 else
19969 ce->value = RECUR (ce->value);
19970 }
19971
19972 if (need_copy_p)
19973 {
19974 vec<constructor_elt, va_gc> *old_n = n;
19975
19976 vec_alloc (n, newlen);
19977 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19978 {
19979 if (TREE_CODE (ce->value) == TREE_VEC)
19980 {
19981 int i, len = TREE_VEC_LENGTH (ce->value);
19982 for (i = 0; i < len; ++i)
19983 CONSTRUCTOR_APPEND_ELT (n, 0,
19984 TREE_VEC_ELT (ce->value, i));
19985 }
19986 else
19987 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19988 }
19989 }
19990
19991 r = build_constructor (init_list_type_node, n);
19992 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19993 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19994 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19995
19996 if (TREE_HAS_CONSTRUCTOR (t))
19997 {
19998 fcl_t cl = fcl_functional;
19999 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20000 cl = fcl_c99;
20001 RETURN (finish_compound_literal (type, r, complain, cl));
20002 }
20003
20004 TREE_TYPE (r) = type;
20005 RETURN (r);
20006 }
20007
20008 case TYPEID_EXPR:
20009 {
20010 tree operand_0 = TREE_OPERAND (t, 0);
20011 if (TYPE_P (operand_0))
20012 {
20013 operand_0 = tsubst (operand_0, args, complain, in_decl);
20014 RETURN (get_typeid (operand_0, complain));
20015 }
20016 else
20017 {
20018 operand_0 = RECUR (operand_0);
20019 RETURN (build_typeid (operand_0, complain));
20020 }
20021 }
20022
20023 case VAR_DECL:
20024 if (!args)
20025 RETURN (t);
20026 /* Fall through */
20027
20028 case PARM_DECL:
20029 {
20030 tree r = tsubst_copy (t, args, complain, in_decl);
20031 /* ??? We're doing a subset of finish_id_expression here. */
20032 if (tree wrap = maybe_get_tls_wrapper_call (r))
20033 /* Replace an evaluated use of the thread_local variable with
20034 a call to its wrapper. */
20035 r = wrap;
20036 else if (outer_automatic_var_p (r))
20037 r = process_outer_var_ref (r, complain);
20038
20039 if (!TYPE_REF_P (TREE_TYPE (t)))
20040 /* If the original type was a reference, we'll be wrapped in
20041 the appropriate INDIRECT_REF. */
20042 r = convert_from_reference (r);
20043 RETURN (r);
20044 }
20045
20046 case VA_ARG_EXPR:
20047 {
20048 tree op0 = RECUR (TREE_OPERAND (t, 0));
20049 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20050 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20051 }
20052
20053 case OFFSETOF_EXPR:
20054 {
20055 tree object_ptr
20056 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20057 in_decl, /*function_p=*/false,
20058 /*integral_constant_expression_p=*/false);
20059 RETURN (finish_offsetof (object_ptr,
20060 RECUR (TREE_OPERAND (t, 0)),
20061 EXPR_LOCATION (t)));
20062 }
20063
20064 case ADDRESSOF_EXPR:
20065 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20066 RECUR (TREE_OPERAND (t, 0)), complain));
20067
20068 case TRAIT_EXPR:
20069 {
20070 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20071 complain, in_decl);
20072
20073 tree type2 = TRAIT_EXPR_TYPE2 (t);
20074 if (type2 && TREE_CODE (type2) == TREE_LIST)
20075 type2 = RECUR (type2);
20076 else if (type2)
20077 type2 = tsubst (type2, args, complain, in_decl);
20078
20079 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20080 TRAIT_EXPR_KIND (t), type1, type2));
20081 }
20082
20083 case STMT_EXPR:
20084 {
20085 tree old_stmt_expr = cur_stmt_expr;
20086 tree stmt_expr = begin_stmt_expr ();
20087
20088 cur_stmt_expr = stmt_expr;
20089 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20090 integral_constant_expression_p);
20091 stmt_expr = finish_stmt_expr (stmt_expr, false);
20092 cur_stmt_expr = old_stmt_expr;
20093
20094 /* If the resulting list of expression statement is empty,
20095 fold it further into void_node. */
20096 if (empty_expr_stmt_p (stmt_expr))
20097 stmt_expr = void_node;
20098
20099 RETURN (stmt_expr);
20100 }
20101
20102 case LAMBDA_EXPR:
20103 {
20104 if (complain & tf_partial)
20105 {
20106 /* We don't have a full set of template arguments yet; don't touch
20107 the lambda at all. */
20108 gcc_assert (processing_template_decl);
20109 return t;
20110 }
20111 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20112
20113 RETURN (build_lambda_object (r));
20114 }
20115
20116 case TARGET_EXPR:
20117 /* We can get here for a constant initializer of non-dependent type.
20118 FIXME stop folding in cp_parser_initializer_clause. */
20119 {
20120 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20121 complain);
20122 RETURN (r);
20123 }
20124
20125 case TRANSACTION_EXPR:
20126 RETURN (tsubst_expr(t, args, complain, in_decl,
20127 integral_constant_expression_p));
20128
20129 case PAREN_EXPR:
20130 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20131
20132 case VEC_PERM_EXPR:
20133 {
20134 tree op0 = RECUR (TREE_OPERAND (t, 0));
20135 tree op1 = RECUR (TREE_OPERAND (t, 1));
20136 tree op2 = RECUR (TREE_OPERAND (t, 2));
20137 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20138 complain));
20139 }
20140
20141 case REQUIRES_EXPR:
20142 {
20143 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20144 if (r == error_mark_node && (complain & tf_error))
20145 tsubst_requires_expr (t, args, complain, in_decl);
20146 RETURN (r);
20147 }
20148
20149 case RANGE_EXPR:
20150 /* No need to substitute further, a RANGE_EXPR will always be built
20151 with constant operands. */
20152 RETURN (t);
20153
20154 case NON_LVALUE_EXPR:
20155 case VIEW_CONVERT_EXPR:
20156 if (location_wrapper_p (t))
20157 /* We need to do this here as well as in tsubst_copy so we get the
20158 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20159 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20160 EXPR_LOCATION (t)));
20161 /* fallthrough. */
20162
20163 default:
20164 /* Handle Objective-C++ constructs, if appropriate. */
20165 {
20166 tree subst
20167 = objcp_tsubst_copy_and_build (t, args, complain,
20168 in_decl, /*function_p=*/false);
20169 if (subst)
20170 RETURN (subst);
20171 }
20172 RETURN (tsubst_copy (t, args, complain, in_decl));
20173 }
20174
20175 #undef RECUR
20176 #undef RETURN
20177 out:
20178 input_location = loc;
20179 return retval;
20180 }
20181
20182 /* Verify that the instantiated ARGS are valid. For type arguments,
20183 make sure that the type's linkage is ok. For non-type arguments,
20184 make sure they are constants if they are integral or enumerations.
20185 Emit an error under control of COMPLAIN, and return TRUE on error. */
20186
20187 static bool
20188 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20189 {
20190 if (dependent_template_arg_p (t))
20191 return false;
20192 if (ARGUMENT_PACK_P (t))
20193 {
20194 tree vec = ARGUMENT_PACK_ARGS (t);
20195 int len = TREE_VEC_LENGTH (vec);
20196 bool result = false;
20197 int i;
20198
20199 for (i = 0; i < len; ++i)
20200 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20201 result = true;
20202 return result;
20203 }
20204 else if (TYPE_P (t))
20205 {
20206 /* [basic.link]: A name with no linkage (notably, the name
20207 of a class or enumeration declared in a local scope)
20208 shall not be used to declare an entity with linkage.
20209 This implies that names with no linkage cannot be used as
20210 template arguments
20211
20212 DR 757 relaxes this restriction for C++0x. */
20213 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20214 : no_linkage_check (t, /*relaxed_p=*/false));
20215
20216 if (nt)
20217 {
20218 /* DR 488 makes use of a type with no linkage cause
20219 type deduction to fail. */
20220 if (complain & tf_error)
20221 {
20222 if (TYPE_UNNAMED_P (nt))
20223 error ("%qT is/uses unnamed type", t);
20224 else
20225 error ("template argument for %qD uses local type %qT",
20226 tmpl, t);
20227 }
20228 return true;
20229 }
20230 /* In order to avoid all sorts of complications, we do not
20231 allow variably-modified types as template arguments. */
20232 else if (variably_modified_type_p (t, NULL_TREE))
20233 {
20234 if (complain & tf_error)
20235 error ("%qT is a variably modified type", t);
20236 return true;
20237 }
20238 }
20239 /* Class template and alias template arguments should be OK. */
20240 else if (DECL_TYPE_TEMPLATE_P (t))
20241 ;
20242 /* A non-type argument of integral or enumerated type must be a
20243 constant. */
20244 else if (TREE_TYPE (t)
20245 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20246 && !REFERENCE_REF_P (t)
20247 && !TREE_CONSTANT (t))
20248 {
20249 if (complain & tf_error)
20250 error ("integral expression %qE is not constant", t);
20251 return true;
20252 }
20253 return false;
20254 }
20255
20256 static bool
20257 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20258 {
20259 int ix, len = DECL_NTPARMS (tmpl);
20260 bool result = false;
20261
20262 for (ix = 0; ix != len; ix++)
20263 {
20264 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20265 result = true;
20266 }
20267 if (result && (complain & tf_error))
20268 error (" trying to instantiate %qD", tmpl);
20269 return result;
20270 }
20271
20272 /* We're out of SFINAE context now, so generate diagnostics for the access
20273 errors we saw earlier when instantiating D from TMPL and ARGS. */
20274
20275 static void
20276 recheck_decl_substitution (tree d, tree tmpl, tree args)
20277 {
20278 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20279 tree type = TREE_TYPE (pattern);
20280 location_t loc = input_location;
20281
20282 push_access_scope (d);
20283 push_deferring_access_checks (dk_no_deferred);
20284 input_location = DECL_SOURCE_LOCATION (pattern);
20285 tsubst (type, args, tf_warning_or_error, d);
20286 input_location = loc;
20287 pop_deferring_access_checks ();
20288 pop_access_scope (d);
20289 }
20290
20291 /* Instantiate the indicated variable, function, or alias template TMPL with
20292 the template arguments in TARG_PTR. */
20293
20294 static tree
20295 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20296 {
20297 tree targ_ptr = orig_args;
20298 tree fndecl;
20299 tree gen_tmpl;
20300 tree spec;
20301 bool access_ok = true;
20302
20303 if (tmpl == error_mark_node)
20304 return error_mark_node;
20305
20306 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20307
20308 /* If this function is a clone, handle it specially. */
20309 if (DECL_CLONED_FUNCTION_P (tmpl))
20310 {
20311 tree spec;
20312 tree clone;
20313
20314 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20315 DECL_CLONED_FUNCTION. */
20316 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20317 targ_ptr, complain);
20318 if (spec == error_mark_node)
20319 return error_mark_node;
20320
20321 /* Look for the clone. */
20322 FOR_EACH_CLONE (clone, spec)
20323 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20324 return clone;
20325 /* We should always have found the clone by now. */
20326 gcc_unreachable ();
20327 return NULL_TREE;
20328 }
20329
20330 if (targ_ptr == error_mark_node)
20331 return error_mark_node;
20332
20333 /* Check to see if we already have this specialization. */
20334 gen_tmpl = most_general_template (tmpl);
20335 if (TMPL_ARGS_DEPTH (targ_ptr)
20336 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20337 /* targ_ptr only has the innermost template args, so add the outer ones
20338 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20339 the case of a non-dependent call within a template definition). */
20340 targ_ptr = (add_outermost_template_args
20341 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20342 targ_ptr));
20343
20344 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20345 but it doesn't seem to be on the hot path. */
20346 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20347
20348 gcc_assert (tmpl == gen_tmpl
20349 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20350 == spec)
20351 || fndecl == NULL_TREE);
20352
20353 if (spec != NULL_TREE)
20354 {
20355 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20356 {
20357 if (complain & tf_error)
20358 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20359 return error_mark_node;
20360 }
20361 return spec;
20362 }
20363
20364 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20365 complain))
20366 return error_mark_node;
20367
20368 /* We are building a FUNCTION_DECL, during which the access of its
20369 parameters and return types have to be checked. However this
20370 FUNCTION_DECL which is the desired context for access checking
20371 is not built yet. We solve this chicken-and-egg problem by
20372 deferring all checks until we have the FUNCTION_DECL. */
20373 push_deferring_access_checks (dk_deferred);
20374
20375 /* Instantiation of the function happens in the context of the function
20376 template, not the context of the overload resolution we're doing. */
20377 push_to_top_level ();
20378 /* If there are dependent arguments, e.g. because we're doing partial
20379 ordering, make sure processing_template_decl stays set. */
20380 if (uses_template_parms (targ_ptr))
20381 ++processing_template_decl;
20382 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20383 {
20384 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20385 complain, gen_tmpl, true);
20386 push_nested_class (ctx);
20387 }
20388
20389 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20390
20391 fndecl = NULL_TREE;
20392 if (VAR_P (pattern))
20393 {
20394 /* We need to determine if we're using a partial or explicit
20395 specialization now, because the type of the variable could be
20396 different. */
20397 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20398 tree elt = most_specialized_partial_spec (tid, complain);
20399 if (elt == error_mark_node)
20400 pattern = error_mark_node;
20401 else if (elt)
20402 {
20403 tree partial_tmpl = TREE_VALUE (elt);
20404 tree partial_args = TREE_PURPOSE (elt);
20405 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20406 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20407 }
20408 }
20409
20410 /* Substitute template parameters to obtain the specialization. */
20411 if (fndecl == NULL_TREE)
20412 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20413 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20414 pop_nested_class ();
20415 pop_from_top_level ();
20416
20417 if (fndecl == error_mark_node)
20418 {
20419 pop_deferring_access_checks ();
20420 return error_mark_node;
20421 }
20422
20423 /* The DECL_TI_TEMPLATE should always be the immediate parent
20424 template, not the most general template. */
20425 DECL_TI_TEMPLATE (fndecl) = tmpl;
20426 DECL_TI_ARGS (fndecl) = targ_ptr;
20427
20428 /* Now we know the specialization, compute access previously
20429 deferred. Do no access control for inheriting constructors,
20430 as we already checked access for the inherited constructor. */
20431 if (!(flag_new_inheriting_ctors
20432 && DECL_INHERITED_CTOR (fndecl)))
20433 {
20434 push_access_scope (fndecl);
20435 if (!perform_deferred_access_checks (complain))
20436 access_ok = false;
20437 pop_access_scope (fndecl);
20438 }
20439 pop_deferring_access_checks ();
20440
20441 /* If we've just instantiated the main entry point for a function,
20442 instantiate all the alternate entry points as well. We do this
20443 by cloning the instantiation of the main entry point, not by
20444 instantiating the template clones. */
20445 if (tree chain = DECL_CHAIN (gen_tmpl))
20446 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20447 clone_function_decl (fndecl, /*update_methods=*/false);
20448
20449 if (!access_ok)
20450 {
20451 if (!(complain & tf_error))
20452 {
20453 /* Remember to reinstantiate when we're out of SFINAE so the user
20454 can see the errors. */
20455 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20456 }
20457 return error_mark_node;
20458 }
20459 return fndecl;
20460 }
20461
20462 /* Wrapper for instantiate_template_1. */
20463
20464 tree
20465 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20466 {
20467 tree ret;
20468 timevar_push (TV_TEMPLATE_INST);
20469 ret = instantiate_template_1 (tmpl, orig_args, complain);
20470 timevar_pop (TV_TEMPLATE_INST);
20471 return ret;
20472 }
20473
20474 /* Instantiate the alias template TMPL with ARGS. Also push a template
20475 instantiation level, which instantiate_template doesn't do because
20476 functions and variables have sufficient context established by the
20477 callers. */
20478
20479 static tree
20480 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20481 {
20482 if (tmpl == error_mark_node || args == error_mark_node)
20483 return error_mark_node;
20484 if (!push_tinst_level (tmpl, args))
20485 return error_mark_node;
20486
20487 args =
20488 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20489 args, tmpl, complain,
20490 /*require_all_args=*/true,
20491 /*use_default_args=*/true);
20492
20493 tree r = instantiate_template (tmpl, args, complain);
20494 pop_tinst_level ();
20495
20496 return r;
20497 }
20498
20499 /* PARM is a template parameter pack for FN. Returns true iff
20500 PARM is used in a deducible way in the argument list of FN. */
20501
20502 static bool
20503 pack_deducible_p (tree parm, tree fn)
20504 {
20505 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20506 for (; t; t = TREE_CHAIN (t))
20507 {
20508 tree type = TREE_VALUE (t);
20509 tree packs;
20510 if (!PACK_EXPANSION_P (type))
20511 continue;
20512 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20513 packs; packs = TREE_CHAIN (packs))
20514 if (template_args_equal (TREE_VALUE (packs), parm))
20515 {
20516 /* The template parameter pack is used in a function parameter
20517 pack. If this is the end of the parameter list, the
20518 template parameter pack is deducible. */
20519 if (TREE_CHAIN (t) == void_list_node)
20520 return true;
20521 else
20522 /* Otherwise, not. Well, it could be deduced from
20523 a non-pack parameter, but doing so would end up with
20524 a deduction mismatch, so don't bother. */
20525 return false;
20526 }
20527 }
20528 /* The template parameter pack isn't used in any function parameter
20529 packs, but it might be used deeper, e.g. tuple<Args...>. */
20530 return true;
20531 }
20532
20533 /* Subroutine of fn_type_unification: check non-dependent parms for
20534 convertibility. */
20535
20536 static int
20537 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20538 tree fn, unification_kind_t strict, int flags,
20539 struct conversion **convs, bool explain_p)
20540 {
20541 /* Non-constructor methods need to leave a conversion for 'this', which
20542 isn't included in nargs here. */
20543 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20544 && !DECL_CONSTRUCTOR_P (fn));
20545
20546 for (unsigned ia = 0;
20547 parms && parms != void_list_node && ia < nargs; )
20548 {
20549 tree parm = TREE_VALUE (parms);
20550
20551 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20552 && (!TREE_CHAIN (parms)
20553 || TREE_CHAIN (parms) == void_list_node))
20554 /* For a function parameter pack that occurs at the end of the
20555 parameter-declaration-list, the type A of each remaining
20556 argument of the call is compared with the type P of the
20557 declarator-id of the function parameter pack. */
20558 break;
20559
20560 parms = TREE_CHAIN (parms);
20561
20562 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20563 /* For a function parameter pack that does not occur at the
20564 end of the parameter-declaration-list, the type of the
20565 parameter pack is a non-deduced context. */
20566 continue;
20567
20568 if (!uses_template_parms (parm))
20569 {
20570 tree arg = args[ia];
20571 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20572 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20573
20574 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20575 conv_p, explain_p))
20576 return 1;
20577 }
20578
20579 ++ia;
20580 }
20581
20582 return 0;
20583 }
20584
20585 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20586 NARGS elements of the arguments that are being used when calling
20587 it. TARGS is a vector into which the deduced template arguments
20588 are placed.
20589
20590 Returns either a FUNCTION_DECL for the matching specialization of FN or
20591 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20592 true, diagnostics will be printed to explain why it failed.
20593
20594 If FN is a conversion operator, or we are trying to produce a specific
20595 specialization, RETURN_TYPE is the return type desired.
20596
20597 The EXPLICIT_TARGS are explicit template arguments provided via a
20598 template-id.
20599
20600 The parameter STRICT is one of:
20601
20602 DEDUCE_CALL:
20603 We are deducing arguments for a function call, as in
20604 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20605 deducing arguments for a call to the result of a conversion
20606 function template, as in [over.call.object].
20607
20608 DEDUCE_CONV:
20609 We are deducing arguments for a conversion function, as in
20610 [temp.deduct.conv].
20611
20612 DEDUCE_EXACT:
20613 We are deducing arguments when doing an explicit instantiation
20614 as in [temp.explicit], when determining an explicit specialization
20615 as in [temp.expl.spec], or when taking the address of a function
20616 template, as in [temp.deduct.funcaddr]. */
20617
20618 tree
20619 fn_type_unification (tree fn,
20620 tree explicit_targs,
20621 tree targs,
20622 const tree *args,
20623 unsigned int nargs,
20624 tree return_type,
20625 unification_kind_t strict,
20626 int flags,
20627 struct conversion **convs,
20628 bool explain_p,
20629 bool decltype_p)
20630 {
20631 tree parms;
20632 tree fntype;
20633 tree decl = NULL_TREE;
20634 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20635 bool ok;
20636 static int deduction_depth;
20637 /* type_unification_real will pass back any access checks from default
20638 template argument substitution. */
20639 vec<deferred_access_check, va_gc> *checks = NULL;
20640 /* We don't have all the template args yet. */
20641 bool incomplete = true;
20642
20643 tree orig_fn = fn;
20644 if (flag_new_inheriting_ctors)
20645 fn = strip_inheriting_ctors (fn);
20646
20647 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20648 tree r = error_mark_node;
20649
20650 tree full_targs = targs;
20651 if (TMPL_ARGS_DEPTH (targs)
20652 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20653 full_targs = (add_outermost_template_args
20654 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20655 targs));
20656
20657 if (decltype_p)
20658 complain |= tf_decltype;
20659
20660 /* In C++0x, it's possible to have a function template whose type depends
20661 on itself recursively. This is most obvious with decltype, but can also
20662 occur with enumeration scope (c++/48969). So we need to catch infinite
20663 recursion and reject the substitution at deduction time; this function
20664 will return error_mark_node for any repeated substitution.
20665
20666 This also catches excessive recursion such as when f<N> depends on
20667 f<N-1> across all integers, and returns error_mark_node for all the
20668 substitutions back up to the initial one.
20669
20670 This is, of course, not reentrant. */
20671 if (excessive_deduction_depth)
20672 return error_mark_node;
20673 ++deduction_depth;
20674
20675 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20676
20677 fntype = TREE_TYPE (fn);
20678 if (explicit_targs)
20679 {
20680 /* [temp.deduct]
20681
20682 The specified template arguments must match the template
20683 parameters in kind (i.e., type, nontype, template), and there
20684 must not be more arguments than there are parameters;
20685 otherwise type deduction fails.
20686
20687 Nontype arguments must match the types of the corresponding
20688 nontype template parameters, or must be convertible to the
20689 types of the corresponding nontype parameters as specified in
20690 _temp.arg.nontype_, otherwise type deduction fails.
20691
20692 All references in the function type of the function template
20693 to the corresponding template parameters are replaced by the
20694 specified template argument values. If a substitution in a
20695 template parameter or in the function type of the function
20696 template results in an invalid type, type deduction fails. */
20697 int i, len = TREE_VEC_LENGTH (tparms);
20698 location_t loc = input_location;
20699 incomplete = false;
20700
20701 if (explicit_targs == error_mark_node)
20702 goto fail;
20703
20704 if (TMPL_ARGS_DEPTH (explicit_targs)
20705 < TMPL_ARGS_DEPTH (full_targs))
20706 explicit_targs = add_outermost_template_args (full_targs,
20707 explicit_targs);
20708
20709 /* Adjust any explicit template arguments before entering the
20710 substitution context. */
20711 explicit_targs
20712 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20713 complain|tf_partial,
20714 /*require_all_args=*/false,
20715 /*use_default_args=*/false));
20716 if (explicit_targs == error_mark_node)
20717 goto fail;
20718
20719 /* Substitute the explicit args into the function type. This is
20720 necessary so that, for instance, explicitly declared function
20721 arguments can match null pointed constants. If we were given
20722 an incomplete set of explicit args, we must not do semantic
20723 processing during substitution as we could create partial
20724 instantiations. */
20725 for (i = 0; i < len; i++)
20726 {
20727 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20728 bool parameter_pack = false;
20729 tree targ = TREE_VEC_ELT (explicit_targs, i);
20730
20731 /* Dig out the actual parm. */
20732 if (TREE_CODE (parm) == TYPE_DECL
20733 || TREE_CODE (parm) == TEMPLATE_DECL)
20734 {
20735 parm = TREE_TYPE (parm);
20736 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20737 }
20738 else if (TREE_CODE (parm) == PARM_DECL)
20739 {
20740 parm = DECL_INITIAL (parm);
20741 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20742 }
20743
20744 if (targ == NULL_TREE)
20745 /* No explicit argument for this template parameter. */
20746 incomplete = true;
20747 else if (parameter_pack && pack_deducible_p (parm, fn))
20748 {
20749 /* Mark the argument pack as "incomplete". We could
20750 still deduce more arguments during unification.
20751 We remove this mark in type_unification_real. */
20752 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20753 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20754 = ARGUMENT_PACK_ARGS (targ);
20755
20756 /* We have some incomplete argument packs. */
20757 incomplete = true;
20758 }
20759 }
20760
20761 if (incomplete)
20762 {
20763 if (!push_tinst_level (fn, explicit_targs))
20764 {
20765 excessive_deduction_depth = true;
20766 goto fail;
20767 }
20768 ++processing_template_decl;
20769 input_location = DECL_SOURCE_LOCATION (fn);
20770 /* Ignore any access checks; we'll see them again in
20771 instantiate_template and they might have the wrong
20772 access path at this point. */
20773 push_deferring_access_checks (dk_deferred);
20774 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20775 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20776 pop_deferring_access_checks ();
20777 input_location = loc;
20778 --processing_template_decl;
20779 pop_tinst_level ();
20780
20781 if (fntype == error_mark_node)
20782 goto fail;
20783 }
20784
20785 /* Place the explicitly specified arguments in TARGS. */
20786 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20787 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20788 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20789 if (!incomplete && CHECKING_P
20790 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20791 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20792 (targs, NUM_TMPL_ARGS (explicit_targs));
20793 }
20794
20795 if (return_type && strict != DEDUCE_CALL)
20796 {
20797 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20798 new_args[0] = return_type;
20799 memcpy (new_args + 1, args, nargs * sizeof (tree));
20800 args = new_args;
20801 ++nargs;
20802 }
20803
20804 if (!incomplete)
20805 goto deduced;
20806
20807 /* Never do unification on the 'this' parameter. */
20808 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20809
20810 if (return_type && strict == DEDUCE_CALL)
20811 {
20812 /* We're deducing for a call to the result of a template conversion
20813 function. The parms we really want are in return_type. */
20814 if (INDIRECT_TYPE_P (return_type))
20815 return_type = TREE_TYPE (return_type);
20816 parms = TYPE_ARG_TYPES (return_type);
20817 }
20818 else if (return_type)
20819 {
20820 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20821 }
20822
20823 /* We allow incomplete unification without an error message here
20824 because the standard doesn't seem to explicitly prohibit it. Our
20825 callers must be ready to deal with unification failures in any
20826 event. */
20827
20828 /* If we aren't explaining yet, push tinst context so we can see where
20829 any errors (e.g. from class instantiations triggered by instantiation
20830 of default template arguments) come from. If we are explaining, this
20831 context is redundant. */
20832 if (!explain_p && !push_tinst_level (fn, targs))
20833 {
20834 excessive_deduction_depth = true;
20835 goto fail;
20836 }
20837
20838 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20839 full_targs, parms, args, nargs, /*subr=*/0,
20840 strict, &checks, explain_p);
20841 if (!explain_p)
20842 pop_tinst_level ();
20843 if (!ok)
20844 goto fail;
20845
20846 /* Now that we have bindings for all of the template arguments,
20847 ensure that the arguments deduced for the template template
20848 parameters have compatible template parameter lists. We cannot
20849 check this property before we have deduced all template
20850 arguments, because the template parameter types of a template
20851 template parameter might depend on prior template parameters
20852 deduced after the template template parameter. The following
20853 ill-formed example illustrates this issue:
20854
20855 template<typename T, template<T> class C> void f(C<5>, T);
20856
20857 template<int N> struct X {};
20858
20859 void g() {
20860 f(X<5>(), 5l); // error: template argument deduction fails
20861 }
20862
20863 The template parameter list of 'C' depends on the template type
20864 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20865 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20866 time that we deduce 'C'. */
20867 if (!template_template_parm_bindings_ok_p
20868 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20869 {
20870 unify_inconsistent_template_template_parameters (explain_p);
20871 goto fail;
20872 }
20873
20874 /* DR 1391: All parameters have args, now check non-dependent parms for
20875 convertibility. */
20876 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20877 convs, explain_p))
20878 goto fail;
20879
20880 deduced:
20881 /* All is well so far. Now, check:
20882
20883 [temp.deduct]
20884
20885 When all template arguments have been deduced, all uses of
20886 template parameters in nondeduced contexts are replaced with
20887 the corresponding deduced argument values. If the
20888 substitution results in an invalid type, as described above,
20889 type deduction fails. */
20890 if (!push_tinst_level (fn, targs))
20891 {
20892 excessive_deduction_depth = true;
20893 goto fail;
20894 }
20895
20896 /* Also collect access checks from the instantiation. */
20897 reopen_deferring_access_checks (checks);
20898
20899 decl = instantiate_template (fn, targs, complain);
20900
20901 checks = get_deferred_access_checks ();
20902 pop_deferring_access_checks ();
20903
20904 pop_tinst_level ();
20905
20906 if (decl == error_mark_node)
20907 goto fail;
20908
20909 /* Now perform any access checks encountered during substitution. */
20910 push_access_scope (decl);
20911 ok = perform_access_checks (checks, complain);
20912 pop_access_scope (decl);
20913 if (!ok)
20914 goto fail;
20915
20916 /* If we're looking for an exact match, check that what we got
20917 is indeed an exact match. It might not be if some template
20918 parameters are used in non-deduced contexts. But don't check
20919 for an exact match if we have dependent template arguments;
20920 in that case we're doing partial ordering, and we already know
20921 that we have two candidates that will provide the actual type. */
20922 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20923 {
20924 tree substed = TREE_TYPE (decl);
20925 unsigned int i;
20926
20927 tree sarg
20928 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20929 if (return_type)
20930 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20931 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20932 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20933 {
20934 unify_type_mismatch (explain_p, args[i],
20935 TREE_VALUE (sarg));
20936 goto fail;
20937 }
20938 }
20939
20940 /* After doing deduction with the inherited constructor, actually return an
20941 instantiation of the inheriting constructor. */
20942 if (orig_fn != fn)
20943 decl = instantiate_template (orig_fn, targs, complain);
20944
20945 r = decl;
20946
20947 fail:
20948 --deduction_depth;
20949 if (excessive_deduction_depth)
20950 {
20951 if (deduction_depth == 0)
20952 /* Reset once we're all the way out. */
20953 excessive_deduction_depth = false;
20954 }
20955
20956 return r;
20957 }
20958
20959 /* Adjust types before performing type deduction, as described in
20960 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20961 sections are symmetric. PARM is the type of a function parameter
20962 or the return type of the conversion function. ARG is the type of
20963 the argument passed to the call, or the type of the value
20964 initialized with the result of the conversion function.
20965 ARG_EXPR is the original argument expression, which may be null. */
20966
20967 static int
20968 maybe_adjust_types_for_deduction (unification_kind_t strict,
20969 tree* parm,
20970 tree* arg,
20971 tree arg_expr)
20972 {
20973 int result = 0;
20974
20975 switch (strict)
20976 {
20977 case DEDUCE_CALL:
20978 break;
20979
20980 case DEDUCE_CONV:
20981 /* Swap PARM and ARG throughout the remainder of this
20982 function; the handling is precisely symmetric since PARM
20983 will initialize ARG rather than vice versa. */
20984 std::swap (parm, arg);
20985 break;
20986
20987 case DEDUCE_EXACT:
20988 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20989 too, but here handle it by stripping the reference from PARM
20990 rather than by adding it to ARG. */
20991 if (TYPE_REF_P (*parm)
20992 && TYPE_REF_IS_RVALUE (*parm)
20993 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20994 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20995 && TYPE_REF_P (*arg)
20996 && !TYPE_REF_IS_RVALUE (*arg))
20997 *parm = TREE_TYPE (*parm);
20998 /* Nothing else to do in this case. */
20999 return 0;
21000
21001 default:
21002 gcc_unreachable ();
21003 }
21004
21005 if (!TYPE_REF_P (*parm))
21006 {
21007 /* [temp.deduct.call]
21008
21009 If P is not a reference type:
21010
21011 --If A is an array type, the pointer type produced by the
21012 array-to-pointer standard conversion (_conv.array_) is
21013 used in place of A for type deduction; otherwise,
21014
21015 --If A is a function type, the pointer type produced by
21016 the function-to-pointer standard conversion
21017 (_conv.func_) is used in place of A for type deduction;
21018 otherwise,
21019
21020 --If A is a cv-qualified type, the top level
21021 cv-qualifiers of A's type are ignored for type
21022 deduction. */
21023 if (TREE_CODE (*arg) == ARRAY_TYPE)
21024 *arg = build_pointer_type (TREE_TYPE (*arg));
21025 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21026 *arg = build_pointer_type (*arg);
21027 else
21028 *arg = TYPE_MAIN_VARIANT (*arg);
21029 }
21030
21031 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21032 reference to a cv-unqualified template parameter that does not represent a
21033 template parameter of a class template (during class template argument
21034 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21035 an lvalue, the type "lvalue reference to A" is used in place of A for type
21036 deduction. */
21037 if (TYPE_REF_P (*parm)
21038 && TYPE_REF_IS_RVALUE (*parm)
21039 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21040 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21041 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21042 && (arg_expr ? lvalue_p (arg_expr)
21043 /* try_one_overload doesn't provide an arg_expr, but
21044 functions are always lvalues. */
21045 : TREE_CODE (*arg) == FUNCTION_TYPE))
21046 *arg = build_reference_type (*arg);
21047
21048 /* [temp.deduct.call]
21049
21050 If P is a cv-qualified type, the top level cv-qualifiers
21051 of P's type are ignored for type deduction. If P is a
21052 reference type, the type referred to by P is used for
21053 type deduction. */
21054 *parm = TYPE_MAIN_VARIANT (*parm);
21055 if (TYPE_REF_P (*parm))
21056 {
21057 *parm = TREE_TYPE (*parm);
21058 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21059 }
21060
21061 /* DR 322. For conversion deduction, remove a reference type on parm
21062 too (which has been swapped into ARG). */
21063 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21064 *arg = TREE_TYPE (*arg);
21065
21066 return result;
21067 }
21068
21069 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21070 template which doesn't contain any deducible template parameters; check if
21071 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21072 unify_one_argument. */
21073
21074 static int
21075 check_non_deducible_conversion (tree parm, tree arg, int strict,
21076 int flags, struct conversion **conv_p,
21077 bool explain_p)
21078 {
21079 tree type;
21080
21081 if (!TYPE_P (arg))
21082 type = TREE_TYPE (arg);
21083 else
21084 type = arg;
21085
21086 if (same_type_p (parm, type))
21087 return unify_success (explain_p);
21088
21089 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21090 if (strict == DEDUCE_CONV)
21091 {
21092 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21093 return unify_success (explain_p);
21094 }
21095 else if (strict != DEDUCE_EXACT)
21096 {
21097 bool ok = false;
21098 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21099 if (conv_p)
21100 /* Avoid recalculating this in add_function_candidate. */
21101 ok = (*conv_p
21102 = good_conversion (parm, type, conv_arg, flags, complain));
21103 else
21104 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21105 if (ok)
21106 return unify_success (explain_p);
21107 }
21108
21109 if (strict == DEDUCE_EXACT)
21110 return unify_type_mismatch (explain_p, parm, arg);
21111 else
21112 return unify_arg_conversion (explain_p, parm, type, arg);
21113 }
21114
21115 static bool uses_deducible_template_parms (tree type);
21116
21117 /* Returns true iff the expression EXPR is one from which a template
21118 argument can be deduced. In other words, if it's an undecorated
21119 use of a template non-type parameter. */
21120
21121 static bool
21122 deducible_expression (tree expr)
21123 {
21124 /* Strip implicit conversions. */
21125 while (CONVERT_EXPR_P (expr))
21126 expr = TREE_OPERAND (expr, 0);
21127 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21128 }
21129
21130 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21131 deducible way; that is, if it has a max value of <PARM> - 1. */
21132
21133 static bool
21134 deducible_array_bound (tree domain)
21135 {
21136 if (domain == NULL_TREE)
21137 return false;
21138
21139 tree max = TYPE_MAX_VALUE (domain);
21140 if (TREE_CODE (max) != MINUS_EXPR)
21141 return false;
21142
21143 return deducible_expression (TREE_OPERAND (max, 0));
21144 }
21145
21146 /* Returns true iff the template arguments ARGS use a template parameter
21147 in a deducible way. */
21148
21149 static bool
21150 deducible_template_args (tree args)
21151 {
21152 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21153 {
21154 bool deducible;
21155 tree elt = TREE_VEC_ELT (args, i);
21156 if (ARGUMENT_PACK_P (elt))
21157 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21158 else
21159 {
21160 if (PACK_EXPANSION_P (elt))
21161 elt = PACK_EXPANSION_PATTERN (elt);
21162 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21163 deducible = true;
21164 else if (TYPE_P (elt))
21165 deducible = uses_deducible_template_parms (elt);
21166 else
21167 deducible = deducible_expression (elt);
21168 }
21169 if (deducible)
21170 return true;
21171 }
21172 return false;
21173 }
21174
21175 /* Returns true iff TYPE contains any deducible references to template
21176 parameters, as per 14.8.2.5. */
21177
21178 static bool
21179 uses_deducible_template_parms (tree type)
21180 {
21181 if (PACK_EXPANSION_P (type))
21182 type = PACK_EXPANSION_PATTERN (type);
21183
21184 /* T
21185 cv-list T
21186 TT<T>
21187 TT<i>
21188 TT<> */
21189 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21190 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21191 return true;
21192
21193 /* T*
21194 T&
21195 T&& */
21196 if (INDIRECT_TYPE_P (type))
21197 return uses_deducible_template_parms (TREE_TYPE (type));
21198
21199 /* T[integer-constant ]
21200 type [i] */
21201 if (TREE_CODE (type) == ARRAY_TYPE)
21202 return (uses_deducible_template_parms (TREE_TYPE (type))
21203 || deducible_array_bound (TYPE_DOMAIN (type)));
21204
21205 /* T type ::*
21206 type T::*
21207 T T::*
21208 T (type ::*)()
21209 type (T::*)()
21210 type (type ::*)(T)
21211 type (T::*)(T)
21212 T (type ::*)(T)
21213 T (T::*)()
21214 T (T::*)(T) */
21215 if (TYPE_PTRMEM_P (type))
21216 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21217 || (uses_deducible_template_parms
21218 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21219
21220 /* template-name <T> (where template-name refers to a class template)
21221 template-name <i> (where template-name refers to a class template) */
21222 if (CLASS_TYPE_P (type)
21223 && CLASSTYPE_TEMPLATE_INFO (type)
21224 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21225 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21226 (CLASSTYPE_TI_ARGS (type)));
21227
21228 /* type (T)
21229 T()
21230 T(T) */
21231 if (FUNC_OR_METHOD_TYPE_P (type))
21232 {
21233 if (uses_deducible_template_parms (TREE_TYPE (type)))
21234 return true;
21235 tree parm = TYPE_ARG_TYPES (type);
21236 if (TREE_CODE (type) == METHOD_TYPE)
21237 parm = TREE_CHAIN (parm);
21238 for (; parm; parm = TREE_CHAIN (parm))
21239 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21240 return true;
21241 }
21242
21243 return false;
21244 }
21245
21246 /* Subroutine of type_unification_real and unify_pack_expansion to
21247 handle unification of a single P/A pair. Parameters are as
21248 for those functions. */
21249
21250 static int
21251 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21252 int subr, unification_kind_t strict,
21253 bool explain_p)
21254 {
21255 tree arg_expr = NULL_TREE;
21256 int arg_strict;
21257
21258 if (arg == error_mark_node || parm == error_mark_node)
21259 return unify_invalid (explain_p);
21260 if (arg == unknown_type_node)
21261 /* We can't deduce anything from this, but we might get all the
21262 template args from other function args. */
21263 return unify_success (explain_p);
21264
21265 /* Implicit conversions (Clause 4) will be performed on a function
21266 argument to convert it to the type of the corresponding function
21267 parameter if the parameter type contains no template-parameters that
21268 participate in template argument deduction. */
21269 if (strict != DEDUCE_EXACT
21270 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21271 /* For function parameters with no deducible template parameters,
21272 just return. We'll check non-dependent conversions later. */
21273 return unify_success (explain_p);
21274
21275 switch (strict)
21276 {
21277 case DEDUCE_CALL:
21278 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21279 | UNIFY_ALLOW_MORE_CV_QUAL
21280 | UNIFY_ALLOW_DERIVED);
21281 break;
21282
21283 case DEDUCE_CONV:
21284 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21285 break;
21286
21287 case DEDUCE_EXACT:
21288 arg_strict = UNIFY_ALLOW_NONE;
21289 break;
21290
21291 default:
21292 gcc_unreachable ();
21293 }
21294
21295 /* We only do these transformations if this is the top-level
21296 parameter_type_list in a call or declaration matching; in other
21297 situations (nested function declarators, template argument lists) we
21298 won't be comparing a type to an expression, and we don't do any type
21299 adjustments. */
21300 if (!subr)
21301 {
21302 if (!TYPE_P (arg))
21303 {
21304 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21305 if (type_unknown_p (arg))
21306 {
21307 /* [temp.deduct.type] A template-argument can be
21308 deduced from a pointer to function or pointer
21309 to member function argument if the set of
21310 overloaded functions does not contain function
21311 templates and at most one of a set of
21312 overloaded functions provides a unique
21313 match. */
21314 resolve_overloaded_unification (tparms, targs, parm,
21315 arg, strict,
21316 arg_strict, explain_p);
21317 /* If a unique match was not found, this is a
21318 non-deduced context, so we still succeed. */
21319 return unify_success (explain_p);
21320 }
21321
21322 arg_expr = arg;
21323 arg = unlowered_expr_type (arg);
21324 if (arg == error_mark_node)
21325 return unify_invalid (explain_p);
21326 }
21327
21328 arg_strict |=
21329 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21330 }
21331 else
21332 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21333 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21334 return unify_template_argument_mismatch (explain_p, parm, arg);
21335
21336 /* For deduction from an init-list we need the actual list. */
21337 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21338 arg = arg_expr;
21339 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21340 }
21341
21342 /* for_each_template_parm callback that always returns 0. */
21343
21344 static int
21345 zero_r (tree, void *)
21346 {
21347 return 0;
21348 }
21349
21350 /* for_each_template_parm any_fn callback to handle deduction of a template
21351 type argument from the type of an array bound. */
21352
21353 static int
21354 array_deduction_r (tree t, void *data)
21355 {
21356 tree_pair_p d = (tree_pair_p)data;
21357 tree &tparms = d->purpose;
21358 tree &targs = d->value;
21359
21360 if (TREE_CODE (t) == ARRAY_TYPE)
21361 if (tree dom = TYPE_DOMAIN (t))
21362 if (tree max = TYPE_MAX_VALUE (dom))
21363 {
21364 if (TREE_CODE (max) == MINUS_EXPR)
21365 max = TREE_OPERAND (max, 0);
21366 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21367 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21368 UNIFY_ALLOW_NONE, /*explain*/false);
21369 }
21370
21371 /* Keep walking. */
21372 return 0;
21373 }
21374
21375 /* Try to deduce any not-yet-deduced template type arguments from the type of
21376 an array bound. This is handled separately from unify because 14.8.2.5 says
21377 "The type of a type parameter is only deduced from an array bound if it is
21378 not otherwise deduced." */
21379
21380 static void
21381 try_array_deduction (tree tparms, tree targs, tree parm)
21382 {
21383 tree_pair_s data = { tparms, targs };
21384 hash_set<tree> visited;
21385 for_each_template_parm (parm, zero_r, &data, &visited,
21386 /*nondeduced*/false, array_deduction_r);
21387 }
21388
21389 /* Most parms like fn_type_unification.
21390
21391 If SUBR is 1, we're being called recursively (to unify the
21392 arguments of a function or method parameter of a function
21393 template).
21394
21395 CHECKS is a pointer to a vector of access checks encountered while
21396 substituting default template arguments. */
21397
21398 static int
21399 type_unification_real (tree tparms,
21400 tree full_targs,
21401 tree xparms,
21402 const tree *xargs,
21403 unsigned int xnargs,
21404 int subr,
21405 unification_kind_t strict,
21406 vec<deferred_access_check, va_gc> **checks,
21407 bool explain_p)
21408 {
21409 tree parm, arg;
21410 int i;
21411 int ntparms = TREE_VEC_LENGTH (tparms);
21412 int saw_undeduced = 0;
21413 tree parms;
21414 const tree *args;
21415 unsigned int nargs;
21416 unsigned int ia;
21417
21418 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21419 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21420 gcc_assert (ntparms > 0);
21421
21422 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21423
21424 /* Reset the number of non-defaulted template arguments contained
21425 in TARGS. */
21426 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21427
21428 again:
21429 parms = xparms;
21430 args = xargs;
21431 nargs = xnargs;
21432
21433 ia = 0;
21434 while (parms && parms != void_list_node
21435 && ia < nargs)
21436 {
21437 parm = TREE_VALUE (parms);
21438
21439 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21440 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21441 /* For a function parameter pack that occurs at the end of the
21442 parameter-declaration-list, the type A of each remaining
21443 argument of the call is compared with the type P of the
21444 declarator-id of the function parameter pack. */
21445 break;
21446
21447 parms = TREE_CHAIN (parms);
21448
21449 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21450 /* For a function parameter pack that does not occur at the
21451 end of the parameter-declaration-list, the type of the
21452 parameter pack is a non-deduced context. */
21453 continue;
21454
21455 arg = args[ia];
21456 ++ia;
21457
21458 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21459 explain_p))
21460 return 1;
21461 }
21462
21463 if (parms
21464 && parms != void_list_node
21465 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21466 {
21467 /* Unify the remaining arguments with the pack expansion type. */
21468 tree argvec;
21469 tree parmvec = make_tree_vec (1);
21470
21471 /* Allocate a TREE_VEC and copy in all of the arguments */
21472 argvec = make_tree_vec (nargs - ia);
21473 for (i = 0; ia < nargs; ++ia, ++i)
21474 TREE_VEC_ELT (argvec, i) = args[ia];
21475
21476 /* Copy the parameter into parmvec. */
21477 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21478 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21479 /*subr=*/subr, explain_p))
21480 return 1;
21481
21482 /* Advance to the end of the list of parameters. */
21483 parms = TREE_CHAIN (parms);
21484 }
21485
21486 /* Fail if we've reached the end of the parm list, and more args
21487 are present, and the parm list isn't variadic. */
21488 if (ia < nargs && parms == void_list_node)
21489 return unify_too_many_arguments (explain_p, nargs, ia);
21490 /* Fail if parms are left and they don't have default values and
21491 they aren't all deduced as empty packs (c++/57397). This is
21492 consistent with sufficient_parms_p. */
21493 if (parms && parms != void_list_node
21494 && TREE_PURPOSE (parms) == NULL_TREE)
21495 {
21496 unsigned int count = nargs;
21497 tree p = parms;
21498 bool type_pack_p;
21499 do
21500 {
21501 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21502 if (!type_pack_p)
21503 count++;
21504 p = TREE_CHAIN (p);
21505 }
21506 while (p && p != void_list_node);
21507 if (count != nargs)
21508 return unify_too_few_arguments (explain_p, ia, count,
21509 type_pack_p);
21510 }
21511
21512 if (!subr)
21513 {
21514 tsubst_flags_t complain = (explain_p
21515 ? tf_warning_or_error
21516 : tf_none);
21517 bool tried_array_deduction = (cxx_dialect < cxx17);
21518
21519 for (i = 0; i < ntparms; i++)
21520 {
21521 tree targ = TREE_VEC_ELT (targs, i);
21522 tree tparm = TREE_VEC_ELT (tparms, i);
21523
21524 /* Clear the "incomplete" flags on all argument packs now so that
21525 substituting them into later default arguments works. */
21526 if (targ && ARGUMENT_PACK_P (targ))
21527 {
21528 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21529 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21530 }
21531
21532 if (targ || tparm == error_mark_node)
21533 continue;
21534 tparm = TREE_VALUE (tparm);
21535
21536 if (TREE_CODE (tparm) == TYPE_DECL
21537 && !tried_array_deduction)
21538 {
21539 try_array_deduction (tparms, targs, xparms);
21540 tried_array_deduction = true;
21541 if (TREE_VEC_ELT (targs, i))
21542 continue;
21543 }
21544
21545 /* If this is an undeduced nontype parameter that depends on
21546 a type parameter, try another pass; its type may have been
21547 deduced from a later argument than the one from which
21548 this parameter can be deduced. */
21549 if (TREE_CODE (tparm) == PARM_DECL
21550 && uses_template_parms (TREE_TYPE (tparm))
21551 && saw_undeduced < 2)
21552 {
21553 saw_undeduced = 1;
21554 continue;
21555 }
21556
21557 /* Core issue #226 (C++0x) [temp.deduct]:
21558
21559 If a template argument has not been deduced, its
21560 default template argument, if any, is used.
21561
21562 When we are in C++98 mode, TREE_PURPOSE will either
21563 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21564 to explicitly check cxx_dialect here. */
21565 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21566 /* OK, there is a default argument. Wait until after the
21567 conversion check to do substitution. */
21568 continue;
21569
21570 /* If the type parameter is a parameter pack, then it will
21571 be deduced to an empty parameter pack. */
21572 if (template_parameter_pack_p (tparm))
21573 {
21574 tree arg;
21575
21576 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21577 {
21578 arg = make_node (NONTYPE_ARGUMENT_PACK);
21579 TREE_CONSTANT (arg) = 1;
21580 }
21581 else
21582 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21583
21584 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21585
21586 TREE_VEC_ELT (targs, i) = arg;
21587 continue;
21588 }
21589
21590 return unify_parameter_deduction_failure (explain_p, tparm);
21591 }
21592
21593 /* Now substitute into the default template arguments. */
21594 for (i = 0; i < ntparms; i++)
21595 {
21596 tree targ = TREE_VEC_ELT (targs, i);
21597 tree tparm = TREE_VEC_ELT (tparms, i);
21598
21599 if (targ || tparm == error_mark_node)
21600 continue;
21601 tree parm = TREE_VALUE (tparm);
21602 tree arg = TREE_PURPOSE (tparm);
21603 reopen_deferring_access_checks (*checks);
21604 location_t save_loc = input_location;
21605 if (DECL_P (parm))
21606 input_location = DECL_SOURCE_LOCATION (parm);
21607
21608 if (saw_undeduced == 1
21609 && TREE_CODE (parm) == PARM_DECL
21610 && uses_template_parms (TREE_TYPE (parm)))
21611 {
21612 /* The type of this non-type parameter depends on undeduced
21613 parameters. Don't try to use its default argument yet,
21614 since we might deduce an argument for it on the next pass,
21615 but do check whether the arguments we already have cause
21616 substitution failure, so that that happens before we try
21617 later default arguments (78489). */
21618 ++processing_template_decl;
21619 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21620 NULL_TREE);
21621 --processing_template_decl;
21622 if (type == error_mark_node)
21623 arg = error_mark_node;
21624 else
21625 arg = NULL_TREE;
21626 }
21627 else
21628 {
21629 /* Even if the call is happening in template context, getting
21630 here means it's non-dependent, and a default argument is
21631 considered a separate definition under [temp.decls], so we can
21632 do this substitution without processing_template_decl. This
21633 is important if the default argument contains something that
21634 might be instantiation-dependent like access (87480). */
21635 processing_template_decl_sentinel s;
21636 tree substed = NULL_TREE;
21637 if (saw_undeduced == 1)
21638 {
21639 /* First instatiate in template context, in case we still
21640 depend on undeduced template parameters. */
21641 ++processing_template_decl;
21642 substed = tsubst_template_arg (arg, full_targs, complain,
21643 NULL_TREE);
21644 --processing_template_decl;
21645 if (substed != error_mark_node
21646 && !uses_template_parms (substed))
21647 /* We replaced all the tparms, substitute again out of
21648 template context. */
21649 substed = NULL_TREE;
21650 }
21651 if (!substed)
21652 substed = tsubst_template_arg (arg, full_targs, complain,
21653 NULL_TREE);
21654
21655 if (!uses_template_parms (substed))
21656 arg = convert_template_argument (parm, substed, full_targs,
21657 complain, i, NULL_TREE);
21658 else if (saw_undeduced == 1)
21659 arg = NULL_TREE;
21660 else
21661 arg = error_mark_node;
21662 }
21663
21664 input_location = save_loc;
21665 *checks = get_deferred_access_checks ();
21666 pop_deferring_access_checks ();
21667
21668 if (arg == error_mark_node)
21669 return 1;
21670 else if (arg)
21671 {
21672 TREE_VEC_ELT (targs, i) = arg;
21673 /* The position of the first default template argument,
21674 is also the number of non-defaulted arguments in TARGS.
21675 Record that. */
21676 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21677 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21678 }
21679 }
21680
21681 if (saw_undeduced++ == 1)
21682 goto again;
21683 }
21684
21685 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21687
21688 return unify_success (explain_p);
21689 }
21690
21691 /* Subroutine of type_unification_real. Args are like the variables
21692 at the call site. ARG is an overloaded function (or template-id);
21693 we try deducing template args from each of the overloads, and if
21694 only one succeeds, we go with that. Modifies TARGS and returns
21695 true on success. */
21696
21697 static bool
21698 resolve_overloaded_unification (tree tparms,
21699 tree targs,
21700 tree parm,
21701 tree arg,
21702 unification_kind_t strict,
21703 int sub_strict,
21704 bool explain_p)
21705 {
21706 tree tempargs = copy_node (targs);
21707 int good = 0;
21708 tree goodfn = NULL_TREE;
21709 bool addr_p;
21710
21711 if (TREE_CODE (arg) == ADDR_EXPR)
21712 {
21713 arg = TREE_OPERAND (arg, 0);
21714 addr_p = true;
21715 }
21716 else
21717 addr_p = false;
21718
21719 if (TREE_CODE (arg) == COMPONENT_REF)
21720 /* Handle `&x' where `x' is some static or non-static member
21721 function name. */
21722 arg = TREE_OPERAND (arg, 1);
21723
21724 if (TREE_CODE (arg) == OFFSET_REF)
21725 arg = TREE_OPERAND (arg, 1);
21726
21727 /* Strip baselink information. */
21728 if (BASELINK_P (arg))
21729 arg = BASELINK_FUNCTIONS (arg);
21730
21731 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21732 {
21733 /* If we got some explicit template args, we need to plug them into
21734 the affected templates before we try to unify, in case the
21735 explicit args will completely resolve the templates in question. */
21736
21737 int ok = 0;
21738 tree expl_subargs = TREE_OPERAND (arg, 1);
21739 arg = TREE_OPERAND (arg, 0);
21740
21741 for (lkp_iterator iter (arg); iter; ++iter)
21742 {
21743 tree fn = *iter;
21744 tree subargs, elem;
21745
21746 if (TREE_CODE (fn) != TEMPLATE_DECL)
21747 continue;
21748
21749 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21750 expl_subargs, NULL_TREE, tf_none,
21751 /*require_all_args=*/true,
21752 /*use_default_args=*/true);
21753 if (subargs != error_mark_node
21754 && !any_dependent_template_arguments_p (subargs))
21755 {
21756 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21757 if (try_one_overload (tparms, targs, tempargs, parm,
21758 elem, strict, sub_strict, addr_p, explain_p)
21759 && (!goodfn || !same_type_p (goodfn, elem)))
21760 {
21761 goodfn = elem;
21762 ++good;
21763 }
21764 }
21765 else if (subargs)
21766 ++ok;
21767 }
21768 /* If no templates (or more than one) are fully resolved by the
21769 explicit arguments, this template-id is a non-deduced context; it
21770 could still be OK if we deduce all template arguments for the
21771 enclosing call through other arguments. */
21772 if (good != 1)
21773 good = ok;
21774 }
21775 else if (!OVL_P (arg))
21776 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21777 -- but the deduction does not succeed because the expression is
21778 not just the function on its own. */
21779 return false;
21780 else
21781 for (lkp_iterator iter (arg); iter; ++iter)
21782 {
21783 tree fn = *iter;
21784 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21785 strict, sub_strict, addr_p, explain_p)
21786 && (!goodfn || !decls_match (goodfn, fn)))
21787 {
21788 goodfn = fn;
21789 ++good;
21790 }
21791 }
21792
21793 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21794 to function or pointer to member function argument if the set of
21795 overloaded functions does not contain function templates and at most
21796 one of a set of overloaded functions provides a unique match.
21797
21798 So if we found multiple possibilities, we return success but don't
21799 deduce anything. */
21800
21801 if (good == 1)
21802 {
21803 int i = TREE_VEC_LENGTH (targs);
21804 for (; i--; )
21805 if (TREE_VEC_ELT (tempargs, i))
21806 {
21807 tree old = TREE_VEC_ELT (targs, i);
21808 tree new_ = TREE_VEC_ELT (tempargs, i);
21809 if (new_ && old && ARGUMENT_PACK_P (old)
21810 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21811 /* Don't forget explicit template arguments in a pack. */
21812 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21813 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21814 TREE_VEC_ELT (targs, i) = new_;
21815 }
21816 }
21817 if (good)
21818 return true;
21819
21820 return false;
21821 }
21822
21823 /* Core DR 115: In contexts where deduction is done and fails, or in
21824 contexts where deduction is not done, if a template argument list is
21825 specified and it, along with any default template arguments, identifies
21826 a single function template specialization, then the template-id is an
21827 lvalue for the function template specialization. */
21828
21829 tree
21830 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21831 {
21832 tree expr, offset, baselink;
21833 bool addr;
21834
21835 if (!type_unknown_p (orig_expr))
21836 return orig_expr;
21837
21838 expr = orig_expr;
21839 addr = false;
21840 offset = NULL_TREE;
21841 baselink = NULL_TREE;
21842
21843 if (TREE_CODE (expr) == ADDR_EXPR)
21844 {
21845 expr = TREE_OPERAND (expr, 0);
21846 addr = true;
21847 }
21848 if (TREE_CODE (expr) == OFFSET_REF)
21849 {
21850 offset = expr;
21851 expr = TREE_OPERAND (expr, 1);
21852 }
21853 if (BASELINK_P (expr))
21854 {
21855 baselink = expr;
21856 expr = BASELINK_FUNCTIONS (expr);
21857 }
21858
21859 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21860 {
21861 int good = 0;
21862 tree goodfn = NULL_TREE;
21863
21864 /* If we got some explicit template args, we need to plug them into
21865 the affected templates before we try to unify, in case the
21866 explicit args will completely resolve the templates in question. */
21867
21868 tree expl_subargs = TREE_OPERAND (expr, 1);
21869 tree arg = TREE_OPERAND (expr, 0);
21870 tree badfn = NULL_TREE;
21871 tree badargs = NULL_TREE;
21872
21873 for (lkp_iterator iter (arg); iter; ++iter)
21874 {
21875 tree fn = *iter;
21876 tree subargs, elem;
21877
21878 if (TREE_CODE (fn) != TEMPLATE_DECL)
21879 continue;
21880
21881 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21882 expl_subargs, NULL_TREE, tf_none,
21883 /*require_all_args=*/true,
21884 /*use_default_args=*/true);
21885 if (subargs != error_mark_node
21886 && !any_dependent_template_arguments_p (subargs))
21887 {
21888 elem = instantiate_template (fn, subargs, tf_none);
21889 if (elem == error_mark_node)
21890 {
21891 badfn = fn;
21892 badargs = subargs;
21893 }
21894 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21895 {
21896 goodfn = elem;
21897 ++good;
21898 }
21899 }
21900 }
21901 if (good == 1)
21902 {
21903 mark_used (goodfn);
21904 expr = goodfn;
21905 if (baselink)
21906 expr = build_baselink (BASELINK_BINFO (baselink),
21907 BASELINK_ACCESS_BINFO (baselink),
21908 expr, BASELINK_OPTYPE (baselink));
21909 if (offset)
21910 {
21911 tree base
21912 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21913 expr = build_offset_ref (base, expr, addr, complain);
21914 }
21915 if (addr)
21916 expr = cp_build_addr_expr (expr, complain);
21917 return expr;
21918 }
21919 else if (good == 0 && badargs && (complain & tf_error))
21920 /* There were no good options and at least one bad one, so let the
21921 user know what the problem is. */
21922 instantiate_template (badfn, badargs, complain);
21923 }
21924 return orig_expr;
21925 }
21926
21927 /* As above, but error out if the expression remains overloaded. */
21928
21929 tree
21930 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21931 {
21932 exp = resolve_nondeduced_context (exp, complain);
21933 if (type_unknown_p (exp))
21934 {
21935 if (complain & tf_error)
21936 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21937 return error_mark_node;
21938 }
21939 return exp;
21940 }
21941
21942 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21943 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21944 different overloads deduce different arguments for a given parm.
21945 ADDR_P is true if the expression for which deduction is being
21946 performed was of the form "& fn" rather than simply "fn".
21947
21948 Returns 1 on success. */
21949
21950 static int
21951 try_one_overload (tree tparms,
21952 tree orig_targs,
21953 tree targs,
21954 tree parm,
21955 tree arg,
21956 unification_kind_t strict,
21957 int sub_strict,
21958 bool addr_p,
21959 bool explain_p)
21960 {
21961 int nargs;
21962 tree tempargs;
21963 int i;
21964
21965 if (arg == error_mark_node)
21966 return 0;
21967
21968 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21969 to function or pointer to member function argument if the set of
21970 overloaded functions does not contain function templates and at most
21971 one of a set of overloaded functions provides a unique match.
21972
21973 So if this is a template, just return success. */
21974
21975 if (uses_template_parms (arg))
21976 return 1;
21977
21978 if (TREE_CODE (arg) == METHOD_TYPE)
21979 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21980 else if (addr_p)
21981 arg = build_pointer_type (arg);
21982
21983 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21984
21985 /* We don't copy orig_targs for this because if we have already deduced
21986 some template args from previous args, unify would complain when we
21987 try to deduce a template parameter for the same argument, even though
21988 there isn't really a conflict. */
21989 nargs = TREE_VEC_LENGTH (targs);
21990 tempargs = make_tree_vec (nargs);
21991
21992 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21993 return 0;
21994
21995 /* First make sure we didn't deduce anything that conflicts with
21996 explicitly specified args. */
21997 for (i = nargs; i--; )
21998 {
21999 tree elt = TREE_VEC_ELT (tempargs, i);
22000 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22001
22002 if (!elt)
22003 /*NOP*/;
22004 else if (uses_template_parms (elt))
22005 /* Since we're unifying against ourselves, we will fill in
22006 template args used in the function parm list with our own
22007 template parms. Discard them. */
22008 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22009 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22010 {
22011 /* Check that the argument at each index of the deduced argument pack
22012 is equivalent to the corresponding explicitly specified argument.
22013 We may have deduced more arguments than were explicitly specified,
22014 and that's OK. */
22015
22016 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22017 that's wrong if we deduce the same argument pack from multiple
22018 function arguments: it's only incomplete the first time. */
22019
22020 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22021 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22022
22023 if (TREE_VEC_LENGTH (deduced_pack)
22024 < TREE_VEC_LENGTH (explicit_pack))
22025 return 0;
22026
22027 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22028 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22029 TREE_VEC_ELT (deduced_pack, j)))
22030 return 0;
22031 }
22032 else if (oldelt && !template_args_equal (oldelt, elt))
22033 return 0;
22034 }
22035
22036 for (i = nargs; i--; )
22037 {
22038 tree elt = TREE_VEC_ELT (tempargs, i);
22039
22040 if (elt)
22041 TREE_VEC_ELT (targs, i) = elt;
22042 }
22043
22044 return 1;
22045 }
22046
22047 /* PARM is a template class (perhaps with unbound template
22048 parameters). ARG is a fully instantiated type. If ARG can be
22049 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22050 TARGS are as for unify. */
22051
22052 static tree
22053 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22054 bool explain_p)
22055 {
22056 tree copy_of_targs;
22057
22058 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22059 return NULL_TREE;
22060 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22061 /* Matches anything. */;
22062 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22063 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22064 return NULL_TREE;
22065
22066 /* We need to make a new template argument vector for the call to
22067 unify. If we used TARGS, we'd clutter it up with the result of
22068 the attempted unification, even if this class didn't work out.
22069 We also don't want to commit ourselves to all the unifications
22070 we've already done, since unification is supposed to be done on
22071 an argument-by-argument basis. In other words, consider the
22072 following pathological case:
22073
22074 template <int I, int J, int K>
22075 struct S {};
22076
22077 template <int I, int J>
22078 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22079
22080 template <int I, int J, int K>
22081 void f(S<I, J, K>, S<I, I, I>);
22082
22083 void g() {
22084 S<0, 0, 0> s0;
22085 S<0, 1, 2> s2;
22086
22087 f(s0, s2);
22088 }
22089
22090 Now, by the time we consider the unification involving `s2', we
22091 already know that we must have `f<0, 0, 0>'. But, even though
22092 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22093 because there are two ways to unify base classes of S<0, 1, 2>
22094 with S<I, I, I>. If we kept the already deduced knowledge, we
22095 would reject the possibility I=1. */
22096 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22097
22098 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22099 {
22100 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22101 return NULL_TREE;
22102 return arg;
22103 }
22104
22105 /* If unification failed, we're done. */
22106 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22107 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22108 return NULL_TREE;
22109
22110 return arg;
22111 }
22112
22113 /* Given a template type PARM and a class type ARG, find the unique
22114 base type in ARG that is an instance of PARM. We do not examine
22115 ARG itself; only its base-classes. If there is not exactly one
22116 appropriate base class, return NULL_TREE. PARM may be the type of
22117 a partial specialization, as well as a plain template type. Used
22118 by unify. */
22119
22120 static enum template_base_result
22121 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22122 bool explain_p, tree *result)
22123 {
22124 tree rval = NULL_TREE;
22125 tree binfo;
22126
22127 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22128
22129 binfo = TYPE_BINFO (complete_type (arg));
22130 if (!binfo)
22131 {
22132 /* The type could not be completed. */
22133 *result = NULL_TREE;
22134 return tbr_incomplete_type;
22135 }
22136
22137 /* Walk in inheritance graph order. The search order is not
22138 important, and this avoids multiple walks of virtual bases. */
22139 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22140 {
22141 tree r = try_class_unification (tparms, targs, parm,
22142 BINFO_TYPE (binfo), explain_p);
22143
22144 if (r)
22145 {
22146 /* If there is more than one satisfactory baseclass, then:
22147
22148 [temp.deduct.call]
22149
22150 If they yield more than one possible deduced A, the type
22151 deduction fails.
22152
22153 applies. */
22154 if (rval && !same_type_p (r, rval))
22155 {
22156 *result = NULL_TREE;
22157 return tbr_ambiguous_baseclass;
22158 }
22159
22160 rval = r;
22161 }
22162 }
22163
22164 *result = rval;
22165 return tbr_success;
22166 }
22167
22168 /* Returns the level of DECL, which declares a template parameter. */
22169
22170 static int
22171 template_decl_level (tree decl)
22172 {
22173 switch (TREE_CODE (decl))
22174 {
22175 case TYPE_DECL:
22176 case TEMPLATE_DECL:
22177 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22178
22179 case PARM_DECL:
22180 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22181
22182 default:
22183 gcc_unreachable ();
22184 }
22185 return 0;
22186 }
22187
22188 /* Decide whether ARG can be unified with PARM, considering only the
22189 cv-qualifiers of each type, given STRICT as documented for unify.
22190 Returns nonzero iff the unification is OK on that basis. */
22191
22192 static int
22193 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22194 {
22195 int arg_quals = cp_type_quals (arg);
22196 int parm_quals = cp_type_quals (parm);
22197
22198 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22199 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22200 {
22201 /* Although a CVR qualifier is ignored when being applied to a
22202 substituted template parameter ([8.3.2]/1 for example), that
22203 does not allow us to unify "const T" with "int&" because both
22204 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22205 It is ok when we're allowing additional CV qualifiers
22206 at the outer level [14.8.2.1]/3,1st bullet. */
22207 if ((TYPE_REF_P (arg)
22208 || FUNC_OR_METHOD_TYPE_P (arg))
22209 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22210 return 0;
22211
22212 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22213 && (parm_quals & TYPE_QUAL_RESTRICT))
22214 return 0;
22215 }
22216
22217 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22218 && (arg_quals & parm_quals) != parm_quals)
22219 return 0;
22220
22221 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22222 && (parm_quals & arg_quals) != arg_quals)
22223 return 0;
22224
22225 return 1;
22226 }
22227
22228 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22229 void
22230 template_parm_level_and_index (tree parm, int* level, int* index)
22231 {
22232 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22233 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22234 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22235 {
22236 *index = TEMPLATE_TYPE_IDX (parm);
22237 *level = TEMPLATE_TYPE_LEVEL (parm);
22238 }
22239 else
22240 {
22241 *index = TEMPLATE_PARM_IDX (parm);
22242 *level = TEMPLATE_PARM_LEVEL (parm);
22243 }
22244 }
22245
22246 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22247 do { \
22248 if (unify (TP, TA, P, A, S, EP)) \
22249 return 1; \
22250 } while (0)
22251
22252 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22253 expansion at the end of PACKED_PARMS. Returns 0 if the type
22254 deduction succeeds, 1 otherwise. STRICT is the same as in
22255 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22256 function call argument list. We'll need to adjust the arguments to make them
22257 types. SUBR tells us if this is from a recursive call to
22258 type_unification_real, or for comparing two template argument
22259 lists. */
22260
22261 static int
22262 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22263 tree packed_args, unification_kind_t strict,
22264 bool subr, bool explain_p)
22265 {
22266 tree parm
22267 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22268 tree pattern = PACK_EXPANSION_PATTERN (parm);
22269 tree pack, packs = NULL_TREE;
22270 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22271
22272 /* Add in any args remembered from an earlier partial instantiation. */
22273 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22274 int levels = TMPL_ARGS_DEPTH (targs);
22275
22276 packed_args = expand_template_argument_pack (packed_args);
22277
22278 int len = TREE_VEC_LENGTH (packed_args);
22279
22280 /* Determine the parameter packs we will be deducing from the
22281 pattern, and record their current deductions. */
22282 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22283 pack; pack = TREE_CHAIN (pack))
22284 {
22285 tree parm_pack = TREE_VALUE (pack);
22286 int idx, level;
22287
22288 /* Only template parameter packs can be deduced, not e.g. function
22289 parameter packs or __bases or __integer_pack. */
22290 if (!TEMPLATE_PARM_P (parm_pack))
22291 continue;
22292
22293 /* Determine the index and level of this parameter pack. */
22294 template_parm_level_and_index (parm_pack, &level, &idx);
22295 if (level < levels)
22296 continue;
22297
22298 /* Keep track of the parameter packs and their corresponding
22299 argument packs. */
22300 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22301 TREE_TYPE (packs) = make_tree_vec (len - start);
22302 }
22303
22304 /* Loop through all of the arguments that have not yet been
22305 unified and unify each with the pattern. */
22306 for (i = start; i < len; i++)
22307 {
22308 tree parm;
22309 bool any_explicit = false;
22310 tree arg = TREE_VEC_ELT (packed_args, i);
22311
22312 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22313 or the element of its argument pack at the current index if
22314 this argument was explicitly specified. */
22315 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22316 {
22317 int idx, level;
22318 tree arg, pargs;
22319 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22320
22321 arg = NULL_TREE;
22322 if (TREE_VALUE (pack)
22323 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22324 && (i - start < TREE_VEC_LENGTH (pargs)))
22325 {
22326 any_explicit = true;
22327 arg = TREE_VEC_ELT (pargs, i - start);
22328 }
22329 TMPL_ARG (targs, level, idx) = arg;
22330 }
22331
22332 /* If we had explicit template arguments, substitute them into the
22333 pattern before deduction. */
22334 if (any_explicit)
22335 {
22336 /* Some arguments might still be unspecified or dependent. */
22337 bool dependent;
22338 ++processing_template_decl;
22339 dependent = any_dependent_template_arguments_p (targs);
22340 if (!dependent)
22341 --processing_template_decl;
22342 parm = tsubst (pattern, targs,
22343 explain_p ? tf_warning_or_error : tf_none,
22344 NULL_TREE);
22345 if (dependent)
22346 --processing_template_decl;
22347 if (parm == error_mark_node)
22348 return 1;
22349 }
22350 else
22351 parm = pattern;
22352
22353 /* Unify the pattern with the current argument. */
22354 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22355 explain_p))
22356 return 1;
22357
22358 /* For each parameter pack, collect the deduced value. */
22359 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22360 {
22361 int idx, level;
22362 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22363
22364 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22365 TMPL_ARG (targs, level, idx);
22366 }
22367 }
22368
22369 /* Verify that the results of unification with the parameter packs
22370 produce results consistent with what we've seen before, and make
22371 the deduced argument packs available. */
22372 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22373 {
22374 tree old_pack = TREE_VALUE (pack);
22375 tree new_args = TREE_TYPE (pack);
22376 int i, len = TREE_VEC_LENGTH (new_args);
22377 int idx, level;
22378 bool nondeduced_p = false;
22379
22380 /* By default keep the original deduced argument pack.
22381 If necessary, more specific code is going to update the
22382 resulting deduced argument later down in this function. */
22383 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22384 TMPL_ARG (targs, level, idx) = old_pack;
22385
22386 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22387 actually deduce anything. */
22388 for (i = 0; i < len && !nondeduced_p; ++i)
22389 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22390 nondeduced_p = true;
22391 if (nondeduced_p)
22392 continue;
22393
22394 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22395 {
22396 /* If we had fewer function args than explicit template args,
22397 just use the explicits. */
22398 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22399 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22400 if (len < explicit_len)
22401 new_args = explicit_args;
22402 }
22403
22404 if (!old_pack)
22405 {
22406 tree result;
22407 /* Build the deduced *_ARGUMENT_PACK. */
22408 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22409 {
22410 result = make_node (NONTYPE_ARGUMENT_PACK);
22411 TREE_CONSTANT (result) = 1;
22412 }
22413 else
22414 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22415
22416 SET_ARGUMENT_PACK_ARGS (result, new_args);
22417
22418 /* Note the deduced argument packs for this parameter
22419 pack. */
22420 TMPL_ARG (targs, level, idx) = result;
22421 }
22422 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22423 && (ARGUMENT_PACK_ARGS (old_pack)
22424 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22425 {
22426 /* We only had the explicitly-provided arguments before, but
22427 now we have a complete set of arguments. */
22428 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22429
22430 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22431 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22432 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22433 }
22434 else
22435 {
22436 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22437 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22438
22439 if (!comp_template_args (old_args, new_args,
22440 &bad_old_arg, &bad_new_arg))
22441 /* Inconsistent unification of this parameter pack. */
22442 return unify_parameter_pack_inconsistent (explain_p,
22443 bad_old_arg,
22444 bad_new_arg);
22445 }
22446 }
22447
22448 return unify_success (explain_p);
22449 }
22450
22451 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22452 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22453 parameters and return value are as for unify. */
22454
22455 static int
22456 unify_array_domain (tree tparms, tree targs,
22457 tree parm_dom, tree arg_dom,
22458 bool explain_p)
22459 {
22460 tree parm_max;
22461 tree arg_max;
22462 bool parm_cst;
22463 bool arg_cst;
22464
22465 /* Our representation of array types uses "N - 1" as the
22466 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22467 not an integer constant. We cannot unify arbitrarily
22468 complex expressions, so we eliminate the MINUS_EXPRs
22469 here. */
22470 parm_max = TYPE_MAX_VALUE (parm_dom);
22471 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22472 if (!parm_cst)
22473 {
22474 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22475 parm_max = TREE_OPERAND (parm_max, 0);
22476 }
22477 arg_max = TYPE_MAX_VALUE (arg_dom);
22478 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22479 if (!arg_cst)
22480 {
22481 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22482 trying to unify the type of a variable with the type
22483 of a template parameter. For example:
22484
22485 template <unsigned int N>
22486 void f (char (&) [N]);
22487 int g();
22488 void h(int i) {
22489 char a[g(i)];
22490 f(a);
22491 }
22492
22493 Here, the type of the ARG will be "int [g(i)]", and
22494 may be a SAVE_EXPR, etc. */
22495 if (TREE_CODE (arg_max) != MINUS_EXPR)
22496 return unify_vla_arg (explain_p, arg_dom);
22497 arg_max = TREE_OPERAND (arg_max, 0);
22498 }
22499
22500 /* If only one of the bounds used a MINUS_EXPR, compensate
22501 by adding one to the other bound. */
22502 if (parm_cst && !arg_cst)
22503 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22504 integer_type_node,
22505 parm_max,
22506 integer_one_node);
22507 else if (arg_cst && !parm_cst)
22508 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22509 integer_type_node,
22510 arg_max,
22511 integer_one_node);
22512
22513 return unify (tparms, targs, parm_max, arg_max,
22514 UNIFY_ALLOW_INTEGER, explain_p);
22515 }
22516
22517 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22518
22519 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22520
22521 static pa_kind_t
22522 pa_kind (tree t)
22523 {
22524 if (PACK_EXPANSION_P (t))
22525 t = PACK_EXPANSION_PATTERN (t);
22526 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22527 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22528 || DECL_TYPE_TEMPLATE_P (t))
22529 return pa_tmpl;
22530 else if (TYPE_P (t))
22531 return pa_type;
22532 else
22533 return pa_expr;
22534 }
22535
22536 /* Deduce the value of template parameters. TPARMS is the (innermost)
22537 set of template parameters to a template. TARGS is the bindings
22538 for those template parameters, as determined thus far; TARGS may
22539 include template arguments for outer levels of template parameters
22540 as well. PARM is a parameter to a template function, or a
22541 subcomponent of that parameter; ARG is the corresponding argument.
22542 This function attempts to match PARM with ARG in a manner
22543 consistent with the existing assignments in TARGS. If more values
22544 are deduced, then TARGS is updated.
22545
22546 Returns 0 if the type deduction succeeds, 1 otherwise. The
22547 parameter STRICT is a bitwise or of the following flags:
22548
22549 UNIFY_ALLOW_NONE:
22550 Require an exact match between PARM and ARG.
22551 UNIFY_ALLOW_MORE_CV_QUAL:
22552 Allow the deduced ARG to be more cv-qualified (by qualification
22553 conversion) than ARG.
22554 UNIFY_ALLOW_LESS_CV_QUAL:
22555 Allow the deduced ARG to be less cv-qualified than ARG.
22556 UNIFY_ALLOW_DERIVED:
22557 Allow the deduced ARG to be a template base class of ARG,
22558 or a pointer to a template base class of the type pointed to by
22559 ARG.
22560 UNIFY_ALLOW_INTEGER:
22561 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22562 case for more information.
22563 UNIFY_ALLOW_OUTER_LEVEL:
22564 This is the outermost level of a deduction. Used to determine validity
22565 of qualification conversions. A valid qualification conversion must
22566 have const qualified pointers leading up to the inner type which
22567 requires additional CV quals, except at the outer level, where const
22568 is not required [conv.qual]. It would be normal to set this flag in
22569 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22570 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22571 This is the outermost level of a deduction, and PARM can be more CV
22572 qualified at this point.
22573 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22574 This is the outermost level of a deduction, and PARM can be less CV
22575 qualified at this point. */
22576
22577 static int
22578 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22579 bool explain_p)
22580 {
22581 int idx;
22582 tree targ;
22583 tree tparm;
22584 int strict_in = strict;
22585 tsubst_flags_t complain = (explain_p
22586 ? tf_warning_or_error
22587 : tf_none);
22588
22589 /* I don't think this will do the right thing with respect to types.
22590 But the only case I've seen it in so far has been array bounds, where
22591 signedness is the only information lost, and I think that will be
22592 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22593 finish_id_expression_1, and are also OK. */
22594 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22595 parm = TREE_OPERAND (parm, 0);
22596
22597 if (arg == error_mark_node)
22598 return unify_invalid (explain_p);
22599 if (arg == unknown_type_node
22600 || arg == init_list_type_node)
22601 /* We can't deduce anything from this, but we might get all the
22602 template args from other function args. */
22603 return unify_success (explain_p);
22604
22605 if (parm == any_targ_node || arg == any_targ_node)
22606 return unify_success (explain_p);
22607
22608 /* If PARM uses template parameters, then we can't bail out here,
22609 even if ARG == PARM, since we won't record unifications for the
22610 template parameters. We might need them if we're trying to
22611 figure out which of two things is more specialized. */
22612 if (arg == parm && !uses_template_parms (parm))
22613 return unify_success (explain_p);
22614
22615 /* Handle init lists early, so the rest of the function can assume
22616 we're dealing with a type. */
22617 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22618 {
22619 tree elt, elttype;
22620 unsigned i;
22621 tree orig_parm = parm;
22622
22623 if (!is_std_init_list (parm)
22624 && TREE_CODE (parm) != ARRAY_TYPE)
22625 /* We can only deduce from an initializer list argument if the
22626 parameter is std::initializer_list or an array; otherwise this
22627 is a non-deduced context. */
22628 return unify_success (explain_p);
22629
22630 if (TREE_CODE (parm) == ARRAY_TYPE)
22631 elttype = TREE_TYPE (parm);
22632 else
22633 {
22634 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22635 /* Deduction is defined in terms of a single type, so just punt
22636 on the (bizarre) std::initializer_list<T...>. */
22637 if (PACK_EXPANSION_P (elttype))
22638 return unify_success (explain_p);
22639 }
22640
22641 if (strict != DEDUCE_EXACT
22642 && TYPE_P (elttype)
22643 && !uses_deducible_template_parms (elttype))
22644 /* If ELTTYPE has no deducible template parms, skip deduction from
22645 the list elements. */;
22646 else
22647 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22648 {
22649 int elt_strict = strict;
22650
22651 if (elt == error_mark_node)
22652 return unify_invalid (explain_p);
22653
22654 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22655 {
22656 tree type = TREE_TYPE (elt);
22657 if (type == error_mark_node)
22658 return unify_invalid (explain_p);
22659 /* It should only be possible to get here for a call. */
22660 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22661 elt_strict |= maybe_adjust_types_for_deduction
22662 (DEDUCE_CALL, &elttype, &type, elt);
22663 elt = type;
22664 }
22665
22666 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22667 explain_p);
22668 }
22669
22670 if (TREE_CODE (parm) == ARRAY_TYPE
22671 && deducible_array_bound (TYPE_DOMAIN (parm)))
22672 {
22673 /* Also deduce from the length of the initializer list. */
22674 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22675 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22676 if (idx == error_mark_node)
22677 return unify_invalid (explain_p);
22678 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22679 idx, explain_p);
22680 }
22681
22682 /* If the std::initializer_list<T> deduction worked, replace the
22683 deduced A with std::initializer_list<A>. */
22684 if (orig_parm != parm)
22685 {
22686 idx = TEMPLATE_TYPE_IDX (orig_parm);
22687 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22688 targ = listify (targ);
22689 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22690 }
22691 return unify_success (explain_p);
22692 }
22693
22694 /* If parm and arg aren't the same kind of thing (template, type, or
22695 expression), fail early. */
22696 if (pa_kind (parm) != pa_kind (arg))
22697 return unify_invalid (explain_p);
22698
22699 /* Immediately reject some pairs that won't unify because of
22700 cv-qualification mismatches. */
22701 if (TREE_CODE (arg) == TREE_CODE (parm)
22702 && TYPE_P (arg)
22703 /* It is the elements of the array which hold the cv quals of an array
22704 type, and the elements might be template type parms. We'll check
22705 when we recurse. */
22706 && TREE_CODE (arg) != ARRAY_TYPE
22707 /* We check the cv-qualifiers when unifying with template type
22708 parameters below. We want to allow ARG `const T' to unify with
22709 PARM `T' for example, when computing which of two templates
22710 is more specialized, for example. */
22711 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22712 && !check_cv_quals_for_unify (strict_in, arg, parm))
22713 return unify_cv_qual_mismatch (explain_p, parm, arg);
22714
22715 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22716 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22717 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22718 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22719 strict &= ~UNIFY_ALLOW_DERIVED;
22720 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22721 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22722
22723 switch (TREE_CODE (parm))
22724 {
22725 case TYPENAME_TYPE:
22726 case SCOPE_REF:
22727 case UNBOUND_CLASS_TEMPLATE:
22728 /* In a type which contains a nested-name-specifier, template
22729 argument values cannot be deduced for template parameters used
22730 within the nested-name-specifier. */
22731 return unify_success (explain_p);
22732
22733 case TEMPLATE_TYPE_PARM:
22734 case TEMPLATE_TEMPLATE_PARM:
22735 case BOUND_TEMPLATE_TEMPLATE_PARM:
22736 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22737 if (error_operand_p (tparm))
22738 return unify_invalid (explain_p);
22739
22740 if (TEMPLATE_TYPE_LEVEL (parm)
22741 != template_decl_level (tparm))
22742 /* The PARM is not one we're trying to unify. Just check
22743 to see if it matches ARG. */
22744 {
22745 if (TREE_CODE (arg) == TREE_CODE (parm)
22746 && (is_auto (parm) ? is_auto (arg)
22747 : same_type_p (parm, arg)))
22748 return unify_success (explain_p);
22749 else
22750 return unify_type_mismatch (explain_p, parm, arg);
22751 }
22752 idx = TEMPLATE_TYPE_IDX (parm);
22753 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22754 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22755 if (error_operand_p (tparm))
22756 return unify_invalid (explain_p);
22757
22758 /* Check for mixed types and values. */
22759 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22760 && TREE_CODE (tparm) != TYPE_DECL)
22761 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22762 && TREE_CODE (tparm) != TEMPLATE_DECL))
22763 gcc_unreachable ();
22764
22765 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22766 {
22767 if ((strict_in & UNIFY_ALLOW_DERIVED)
22768 && CLASS_TYPE_P (arg))
22769 {
22770 /* First try to match ARG directly. */
22771 tree t = try_class_unification (tparms, targs, parm, arg,
22772 explain_p);
22773 if (!t)
22774 {
22775 /* Otherwise, look for a suitable base of ARG, as below. */
22776 enum template_base_result r;
22777 r = get_template_base (tparms, targs, parm, arg,
22778 explain_p, &t);
22779 if (!t)
22780 return unify_no_common_base (explain_p, r, parm, arg);
22781 arg = t;
22782 }
22783 }
22784 /* ARG must be constructed from a template class or a template
22785 template parameter. */
22786 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22787 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22788 return unify_template_deduction_failure (explain_p, parm, arg);
22789
22790 /* Deduce arguments T, i from TT<T> or TT<i>. */
22791 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22792 return 1;
22793
22794 arg = TYPE_TI_TEMPLATE (arg);
22795
22796 /* Fall through to deduce template name. */
22797 }
22798
22799 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22800 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22801 {
22802 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22803
22804 /* Simple cases: Value already set, does match or doesn't. */
22805 if (targ != NULL_TREE && template_args_equal (targ, arg))
22806 return unify_success (explain_p);
22807 else if (targ)
22808 return unify_inconsistency (explain_p, parm, targ, arg);
22809 }
22810 else
22811 {
22812 /* If PARM is `const T' and ARG is only `int', we don't have
22813 a match unless we are allowing additional qualification.
22814 If ARG is `const int' and PARM is just `T' that's OK;
22815 that binds `const int' to `T'. */
22816 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22817 arg, parm))
22818 return unify_cv_qual_mismatch (explain_p, parm, arg);
22819
22820 /* Consider the case where ARG is `const volatile int' and
22821 PARM is `const T'. Then, T should be `volatile int'. */
22822 arg = cp_build_qualified_type_real
22823 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22824 if (arg == error_mark_node)
22825 return unify_invalid (explain_p);
22826
22827 /* Simple cases: Value already set, does match or doesn't. */
22828 if (targ != NULL_TREE && same_type_p (targ, arg))
22829 return unify_success (explain_p);
22830 else if (targ)
22831 return unify_inconsistency (explain_p, parm, targ, arg);
22832
22833 /* Make sure that ARG is not a variable-sized array. (Note
22834 that were talking about variable-sized arrays (like
22835 `int[n]'), rather than arrays of unknown size (like
22836 `int[]').) We'll get very confused by such a type since
22837 the bound of the array is not constant, and therefore
22838 not mangleable. Besides, such types are not allowed in
22839 ISO C++, so we can do as we please here. We do allow
22840 them for 'auto' deduction, since that isn't ABI-exposed. */
22841 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22842 return unify_vla_arg (explain_p, arg);
22843
22844 /* Strip typedefs as in convert_template_argument. */
22845 arg = canonicalize_type_argument (arg, tf_none);
22846 }
22847
22848 /* If ARG is a parameter pack or an expansion, we cannot unify
22849 against it unless PARM is also a parameter pack. */
22850 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22851 && !template_parameter_pack_p (parm))
22852 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22853
22854 /* If the argument deduction results is a METHOD_TYPE,
22855 then there is a problem.
22856 METHOD_TYPE doesn't map to any real C++ type the result of
22857 the deduction cannot be of that type. */
22858 if (TREE_CODE (arg) == METHOD_TYPE)
22859 return unify_method_type_error (explain_p, arg);
22860
22861 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22862 return unify_success (explain_p);
22863
22864 case TEMPLATE_PARM_INDEX:
22865 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22866 if (error_operand_p (tparm))
22867 return unify_invalid (explain_p);
22868
22869 if (TEMPLATE_PARM_LEVEL (parm)
22870 != template_decl_level (tparm))
22871 {
22872 /* The PARM is not one we're trying to unify. Just check
22873 to see if it matches ARG. */
22874 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22875 && cp_tree_equal (parm, arg));
22876 if (result)
22877 unify_expression_unequal (explain_p, parm, arg);
22878 return result;
22879 }
22880
22881 idx = TEMPLATE_PARM_IDX (parm);
22882 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22883
22884 if (targ)
22885 {
22886 if ((strict & UNIFY_ALLOW_INTEGER)
22887 && TREE_TYPE (targ) && TREE_TYPE (arg)
22888 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22889 /* We're deducing from an array bound, the type doesn't matter. */
22890 arg = fold_convert (TREE_TYPE (targ), arg);
22891 int x = !cp_tree_equal (targ, arg);
22892 if (x)
22893 unify_inconsistency (explain_p, parm, targ, arg);
22894 return x;
22895 }
22896
22897 /* [temp.deduct.type] If, in the declaration of a function template
22898 with a non-type template-parameter, the non-type
22899 template-parameter is used in an expression in the function
22900 parameter-list and, if the corresponding template-argument is
22901 deduced, the template-argument type shall match the type of the
22902 template-parameter exactly, except that a template-argument
22903 deduced from an array bound may be of any integral type.
22904 The non-type parameter might use already deduced type parameters. */
22905 tparm = TREE_TYPE (parm);
22906 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22907 /* We don't have enough levels of args to do any substitution. This
22908 can happen in the context of -fnew-ttp-matching. */;
22909 else
22910 {
22911 ++processing_template_decl;
22912 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22913 --processing_template_decl;
22914
22915 if (tree a = type_uses_auto (tparm))
22916 {
22917 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22918 if (tparm == error_mark_node)
22919 return 1;
22920 }
22921 }
22922
22923 if (!TREE_TYPE (arg))
22924 /* Template-parameter dependent expression. Just accept it for now.
22925 It will later be processed in convert_template_argument. */
22926 ;
22927 else if (same_type_ignoring_top_level_qualifiers_p
22928 (non_reference (TREE_TYPE (arg)),
22929 non_reference (tparm)))
22930 /* OK. Ignore top-level quals here because a class-type template
22931 parameter object is const. */;
22932 else if ((strict & UNIFY_ALLOW_INTEGER)
22933 && CP_INTEGRAL_TYPE_P (tparm))
22934 /* Convert the ARG to the type of PARM; the deduced non-type
22935 template argument must exactly match the types of the
22936 corresponding parameter. */
22937 arg = fold (build_nop (tparm, arg));
22938 else if (uses_template_parms (tparm))
22939 {
22940 /* We haven't deduced the type of this parameter yet. */
22941 if (cxx_dialect >= cxx17
22942 /* We deduce from array bounds in try_array_deduction. */
22943 && !(strict & UNIFY_ALLOW_INTEGER))
22944 {
22945 /* Deduce it from the non-type argument. */
22946 tree atype = TREE_TYPE (arg);
22947 RECUR_AND_CHECK_FAILURE (tparms, targs,
22948 tparm, atype,
22949 UNIFY_ALLOW_NONE, explain_p);
22950 }
22951 else
22952 /* Try again later. */
22953 return unify_success (explain_p);
22954 }
22955 else
22956 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22957
22958 /* If ARG is a parameter pack or an expansion, we cannot unify
22959 against it unless PARM is also a parameter pack. */
22960 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22961 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22962 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22963
22964 {
22965 bool removed_attr = false;
22966 arg = strip_typedefs_expr (arg, &removed_attr);
22967 }
22968 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22969 return unify_success (explain_p);
22970
22971 case PTRMEM_CST:
22972 {
22973 /* A pointer-to-member constant can be unified only with
22974 another constant. */
22975 if (TREE_CODE (arg) != PTRMEM_CST)
22976 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22977
22978 /* Just unify the class member. It would be useless (and possibly
22979 wrong, depending on the strict flags) to unify also
22980 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22981 arg refer to the same variable, even if through different
22982 classes. For instance:
22983
22984 struct A { int x; };
22985 struct B : A { };
22986
22987 Unification of &A::x and &B::x must succeed. */
22988 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22989 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22990 }
22991
22992 case POINTER_TYPE:
22993 {
22994 if (!TYPE_PTR_P (arg))
22995 return unify_type_mismatch (explain_p, parm, arg);
22996
22997 /* [temp.deduct.call]
22998
22999 A can be another pointer or pointer to member type that can
23000 be converted to the deduced A via a qualification
23001 conversion (_conv.qual_).
23002
23003 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23004 This will allow for additional cv-qualification of the
23005 pointed-to types if appropriate. */
23006
23007 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23008 /* The derived-to-base conversion only persists through one
23009 level of pointers. */
23010 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23011
23012 return unify (tparms, targs, TREE_TYPE (parm),
23013 TREE_TYPE (arg), strict, explain_p);
23014 }
23015
23016 case REFERENCE_TYPE:
23017 if (!TYPE_REF_P (arg))
23018 return unify_type_mismatch (explain_p, parm, arg);
23019 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23020 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23021
23022 case ARRAY_TYPE:
23023 if (TREE_CODE (arg) != ARRAY_TYPE)
23024 return unify_type_mismatch (explain_p, parm, arg);
23025 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23026 != (TYPE_DOMAIN (arg) == NULL_TREE))
23027 return unify_type_mismatch (explain_p, parm, arg);
23028 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23029 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23030 if (TYPE_DOMAIN (parm) != NULL_TREE)
23031 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23032 TYPE_DOMAIN (arg), explain_p);
23033 return unify_success (explain_p);
23034
23035 case REAL_TYPE:
23036 case COMPLEX_TYPE:
23037 case VECTOR_TYPE:
23038 case INTEGER_TYPE:
23039 case BOOLEAN_TYPE:
23040 case ENUMERAL_TYPE:
23041 case VOID_TYPE:
23042 case NULLPTR_TYPE:
23043 if (TREE_CODE (arg) != TREE_CODE (parm))
23044 return unify_type_mismatch (explain_p, parm, arg);
23045
23046 /* We have already checked cv-qualification at the top of the
23047 function. */
23048 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23049 return unify_type_mismatch (explain_p, parm, arg);
23050
23051 /* As far as unification is concerned, this wins. Later checks
23052 will invalidate it if necessary. */
23053 return unify_success (explain_p);
23054
23055 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23056 /* Type INTEGER_CST can come from ordinary constant template args. */
23057 case INTEGER_CST:
23058 while (CONVERT_EXPR_P (arg))
23059 arg = TREE_OPERAND (arg, 0);
23060
23061 if (TREE_CODE (arg) != INTEGER_CST)
23062 return unify_template_argument_mismatch (explain_p, parm, arg);
23063 return (tree_int_cst_equal (parm, arg)
23064 ? unify_success (explain_p)
23065 : unify_template_argument_mismatch (explain_p, parm, arg));
23066
23067 case TREE_VEC:
23068 {
23069 int i, len, argslen;
23070 int parm_variadic_p = 0;
23071
23072 if (TREE_CODE (arg) != TREE_VEC)
23073 return unify_template_argument_mismatch (explain_p, parm, arg);
23074
23075 len = TREE_VEC_LENGTH (parm);
23076 argslen = TREE_VEC_LENGTH (arg);
23077
23078 /* Check for pack expansions in the parameters. */
23079 for (i = 0; i < len; ++i)
23080 {
23081 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23082 {
23083 if (i == len - 1)
23084 /* We can unify against something with a trailing
23085 parameter pack. */
23086 parm_variadic_p = 1;
23087 else
23088 /* [temp.deduct.type]/9: If the template argument list of
23089 P contains a pack expansion that is not the last
23090 template argument, the entire template argument list
23091 is a non-deduced context. */
23092 return unify_success (explain_p);
23093 }
23094 }
23095
23096 /* If we don't have enough arguments to satisfy the parameters
23097 (not counting the pack expression at the end), or we have
23098 too many arguments for a parameter list that doesn't end in
23099 a pack expression, we can't unify. */
23100 if (parm_variadic_p
23101 ? argslen < len - parm_variadic_p
23102 : argslen != len)
23103 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23104
23105 /* Unify all of the parameters that precede the (optional)
23106 pack expression. */
23107 for (i = 0; i < len - parm_variadic_p; ++i)
23108 {
23109 RECUR_AND_CHECK_FAILURE (tparms, targs,
23110 TREE_VEC_ELT (parm, i),
23111 TREE_VEC_ELT (arg, i),
23112 UNIFY_ALLOW_NONE, explain_p);
23113 }
23114 if (parm_variadic_p)
23115 return unify_pack_expansion (tparms, targs, parm, arg,
23116 DEDUCE_EXACT,
23117 /*subr=*/true, explain_p);
23118 return unify_success (explain_p);
23119 }
23120
23121 case RECORD_TYPE:
23122 case UNION_TYPE:
23123 if (TREE_CODE (arg) != TREE_CODE (parm))
23124 return unify_type_mismatch (explain_p, parm, arg);
23125
23126 if (TYPE_PTRMEMFUNC_P (parm))
23127 {
23128 if (!TYPE_PTRMEMFUNC_P (arg))
23129 return unify_type_mismatch (explain_p, parm, arg);
23130
23131 return unify (tparms, targs,
23132 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23133 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23134 strict, explain_p);
23135 }
23136 else if (TYPE_PTRMEMFUNC_P (arg))
23137 return unify_type_mismatch (explain_p, parm, arg);
23138
23139 if (CLASSTYPE_TEMPLATE_INFO (parm))
23140 {
23141 tree t = NULL_TREE;
23142
23143 if (strict_in & UNIFY_ALLOW_DERIVED)
23144 {
23145 /* First, we try to unify the PARM and ARG directly. */
23146 t = try_class_unification (tparms, targs,
23147 parm, arg, explain_p);
23148
23149 if (!t)
23150 {
23151 /* Fallback to the special case allowed in
23152 [temp.deduct.call]:
23153
23154 If P is a class, and P has the form
23155 template-id, then A can be a derived class of
23156 the deduced A. Likewise, if P is a pointer to
23157 a class of the form template-id, A can be a
23158 pointer to a derived class pointed to by the
23159 deduced A. */
23160 enum template_base_result r;
23161 r = get_template_base (tparms, targs, parm, arg,
23162 explain_p, &t);
23163
23164 if (!t)
23165 {
23166 /* Don't give the derived diagnostic if we're
23167 already dealing with the same template. */
23168 bool same_template
23169 = (CLASSTYPE_TEMPLATE_INFO (arg)
23170 && (CLASSTYPE_TI_TEMPLATE (parm)
23171 == CLASSTYPE_TI_TEMPLATE (arg)));
23172 return unify_no_common_base (explain_p && !same_template,
23173 r, parm, arg);
23174 }
23175 }
23176 }
23177 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23178 && (CLASSTYPE_TI_TEMPLATE (parm)
23179 == CLASSTYPE_TI_TEMPLATE (arg)))
23180 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23181 Then, we should unify `int' and `U'. */
23182 t = arg;
23183 else
23184 /* There's no chance of unification succeeding. */
23185 return unify_type_mismatch (explain_p, parm, arg);
23186
23187 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23188 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23189 }
23190 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23191 return unify_type_mismatch (explain_p, parm, arg);
23192 return unify_success (explain_p);
23193
23194 case METHOD_TYPE:
23195 case FUNCTION_TYPE:
23196 {
23197 unsigned int nargs;
23198 tree *args;
23199 tree a;
23200 unsigned int i;
23201
23202 if (TREE_CODE (arg) != TREE_CODE (parm))
23203 return unify_type_mismatch (explain_p, parm, arg);
23204
23205 /* CV qualifications for methods can never be deduced, they must
23206 match exactly. We need to check them explicitly here,
23207 because type_unification_real treats them as any other
23208 cv-qualified parameter. */
23209 if (TREE_CODE (parm) == METHOD_TYPE
23210 && (!check_cv_quals_for_unify
23211 (UNIFY_ALLOW_NONE,
23212 class_of_this_parm (arg),
23213 class_of_this_parm (parm))))
23214 return unify_cv_qual_mismatch (explain_p, parm, arg);
23215 if (TREE_CODE (arg) == FUNCTION_TYPE
23216 && type_memfn_quals (parm) != type_memfn_quals (arg))
23217 return unify_cv_qual_mismatch (explain_p, parm, arg);
23218 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23219 return unify_type_mismatch (explain_p, parm, arg);
23220
23221 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23222 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23223
23224 nargs = list_length (TYPE_ARG_TYPES (arg));
23225 args = XALLOCAVEC (tree, nargs);
23226 for (a = TYPE_ARG_TYPES (arg), i = 0;
23227 a != NULL_TREE && a != void_list_node;
23228 a = TREE_CHAIN (a), ++i)
23229 args[i] = TREE_VALUE (a);
23230 nargs = i;
23231
23232 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23233 args, nargs, 1, DEDUCE_EXACT,
23234 NULL, explain_p))
23235 return 1;
23236
23237 if (flag_noexcept_type)
23238 {
23239 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23240 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23241 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23242 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23243 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23244 && uses_template_parms (TREE_PURPOSE (pspec)))
23245 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23246 TREE_PURPOSE (aspec),
23247 UNIFY_ALLOW_NONE, explain_p);
23248 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23249 return unify_type_mismatch (explain_p, parm, arg);
23250 }
23251
23252 return 0;
23253 }
23254
23255 case OFFSET_TYPE:
23256 /* Unify a pointer to member with a pointer to member function, which
23257 deduces the type of the member as a function type. */
23258 if (TYPE_PTRMEMFUNC_P (arg))
23259 {
23260 /* Check top-level cv qualifiers */
23261 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23262 return unify_cv_qual_mismatch (explain_p, parm, arg);
23263
23264 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23265 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23266 UNIFY_ALLOW_NONE, explain_p);
23267
23268 /* Determine the type of the function we are unifying against. */
23269 tree fntype = static_fn_type (arg);
23270
23271 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23272 }
23273
23274 if (TREE_CODE (arg) != OFFSET_TYPE)
23275 return unify_type_mismatch (explain_p, parm, arg);
23276 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23277 TYPE_OFFSET_BASETYPE (arg),
23278 UNIFY_ALLOW_NONE, explain_p);
23279 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23280 strict, explain_p);
23281
23282 case CONST_DECL:
23283 if (DECL_TEMPLATE_PARM_P (parm))
23284 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23285 if (arg != scalar_constant_value (parm))
23286 return unify_template_argument_mismatch (explain_p, parm, arg);
23287 return unify_success (explain_p);
23288
23289 case FIELD_DECL:
23290 case TEMPLATE_DECL:
23291 /* Matched cases are handled by the ARG == PARM test above. */
23292 return unify_template_argument_mismatch (explain_p, parm, arg);
23293
23294 case VAR_DECL:
23295 /* We might get a variable as a non-type template argument in parm if the
23296 corresponding parameter is type-dependent. Make any necessary
23297 adjustments based on whether arg is a reference. */
23298 if (CONSTANT_CLASS_P (arg))
23299 parm = fold_non_dependent_expr (parm, complain);
23300 else if (REFERENCE_REF_P (arg))
23301 {
23302 tree sub = TREE_OPERAND (arg, 0);
23303 STRIP_NOPS (sub);
23304 if (TREE_CODE (sub) == ADDR_EXPR)
23305 arg = TREE_OPERAND (sub, 0);
23306 }
23307 /* Now use the normal expression code to check whether they match. */
23308 goto expr;
23309
23310 case TYPE_ARGUMENT_PACK:
23311 case NONTYPE_ARGUMENT_PACK:
23312 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23313 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23314
23315 case TYPEOF_TYPE:
23316 case DECLTYPE_TYPE:
23317 case UNDERLYING_TYPE:
23318 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23319 or UNDERLYING_TYPE nodes. */
23320 return unify_success (explain_p);
23321
23322 case ERROR_MARK:
23323 /* Unification fails if we hit an error node. */
23324 return unify_invalid (explain_p);
23325
23326 case INDIRECT_REF:
23327 if (REFERENCE_REF_P (parm))
23328 {
23329 bool pexp = PACK_EXPANSION_P (arg);
23330 if (pexp)
23331 arg = PACK_EXPANSION_PATTERN (arg);
23332 if (REFERENCE_REF_P (arg))
23333 arg = TREE_OPERAND (arg, 0);
23334 if (pexp)
23335 arg = make_pack_expansion (arg, complain);
23336 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23337 strict, explain_p);
23338 }
23339 /* FALLTHRU */
23340
23341 default:
23342 /* An unresolved overload is a nondeduced context. */
23343 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23344 return unify_success (explain_p);
23345 gcc_assert (EXPR_P (parm)
23346 || COMPOUND_LITERAL_P (parm)
23347 || TREE_CODE (parm) == TRAIT_EXPR);
23348 expr:
23349 /* We must be looking at an expression. This can happen with
23350 something like:
23351
23352 template <int I>
23353 void foo(S<I>, S<I + 2>);
23354
23355 or
23356
23357 template<typename T>
23358 void foo(A<T, T{}>);
23359
23360 This is a "non-deduced context":
23361
23362 [deduct.type]
23363
23364 The non-deduced contexts are:
23365
23366 --A non-type template argument or an array bound in which
23367 a subexpression references a template parameter.
23368
23369 In these cases, we assume deduction succeeded, but don't
23370 actually infer any unifications. */
23371
23372 if (!uses_template_parms (parm)
23373 && !template_args_equal (parm, arg))
23374 return unify_expression_unequal (explain_p, parm, arg);
23375 else
23376 return unify_success (explain_p);
23377 }
23378 }
23379 #undef RECUR_AND_CHECK_FAILURE
23380 \f
23381 /* Note that DECL can be defined in this translation unit, if
23382 required. */
23383
23384 static void
23385 mark_definable (tree decl)
23386 {
23387 tree clone;
23388 DECL_NOT_REALLY_EXTERN (decl) = 1;
23389 FOR_EACH_CLONE (clone, decl)
23390 DECL_NOT_REALLY_EXTERN (clone) = 1;
23391 }
23392
23393 /* Called if RESULT is explicitly instantiated, or is a member of an
23394 explicitly instantiated class. */
23395
23396 void
23397 mark_decl_instantiated (tree result, int extern_p)
23398 {
23399 SET_DECL_EXPLICIT_INSTANTIATION (result);
23400
23401 /* If this entity has already been written out, it's too late to
23402 make any modifications. */
23403 if (TREE_ASM_WRITTEN (result))
23404 return;
23405
23406 /* For anonymous namespace we don't need to do anything. */
23407 if (decl_anon_ns_mem_p (result))
23408 {
23409 gcc_assert (!TREE_PUBLIC (result));
23410 return;
23411 }
23412
23413 if (TREE_CODE (result) != FUNCTION_DECL)
23414 /* The TREE_PUBLIC flag for function declarations will have been
23415 set correctly by tsubst. */
23416 TREE_PUBLIC (result) = 1;
23417
23418 /* This might have been set by an earlier implicit instantiation. */
23419 DECL_COMDAT (result) = 0;
23420
23421 if (extern_p)
23422 DECL_NOT_REALLY_EXTERN (result) = 0;
23423 else
23424 {
23425 mark_definable (result);
23426 mark_needed (result);
23427 /* Always make artificials weak. */
23428 if (DECL_ARTIFICIAL (result) && flag_weak)
23429 comdat_linkage (result);
23430 /* For WIN32 we also want to put explicit instantiations in
23431 linkonce sections. */
23432 else if (TREE_PUBLIC (result))
23433 maybe_make_one_only (result);
23434 if (TREE_CODE (result) == FUNCTION_DECL
23435 && DECL_TEMPLATE_INSTANTIATED (result))
23436 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23437 since start_preparsed_function wouldn't have if we had an earlier
23438 extern explicit instantiation. */
23439 DECL_EXTERNAL (result) = 0;
23440 }
23441
23442 /* If EXTERN_P, then this function will not be emitted -- unless
23443 followed by an explicit instantiation, at which point its linkage
23444 will be adjusted. If !EXTERN_P, then this function will be
23445 emitted here. In neither circumstance do we want
23446 import_export_decl to adjust the linkage. */
23447 DECL_INTERFACE_KNOWN (result) = 1;
23448 }
23449
23450 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23451 important template arguments. If any are missing, we check whether
23452 they're important by using error_mark_node for substituting into any
23453 args that were used for partial ordering (the ones between ARGS and END)
23454 and seeing if it bubbles up. */
23455
23456 static bool
23457 check_undeduced_parms (tree targs, tree args, tree end)
23458 {
23459 bool found = false;
23460 int i;
23461 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23462 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23463 {
23464 found = true;
23465 TREE_VEC_ELT (targs, i) = error_mark_node;
23466 }
23467 if (found)
23468 {
23469 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23470 if (substed == error_mark_node)
23471 return true;
23472 }
23473 return false;
23474 }
23475
23476 /* Given two function templates PAT1 and PAT2, return:
23477
23478 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23479 -1 if PAT2 is more specialized than PAT1.
23480 0 if neither is more specialized.
23481
23482 LEN indicates the number of parameters we should consider
23483 (defaulted parameters should not be considered).
23484
23485 The 1998 std underspecified function template partial ordering, and
23486 DR214 addresses the issue. We take pairs of arguments, one from
23487 each of the templates, and deduce them against each other. One of
23488 the templates will be more specialized if all the *other*
23489 template's arguments deduce against its arguments and at least one
23490 of its arguments *does* *not* deduce against the other template's
23491 corresponding argument. Deduction is done as for class templates.
23492 The arguments used in deduction have reference and top level cv
23493 qualifiers removed. Iff both arguments were originally reference
23494 types *and* deduction succeeds in both directions, an lvalue reference
23495 wins against an rvalue reference and otherwise the template
23496 with the more cv-qualified argument wins for that pairing (if
23497 neither is more cv-qualified, they both are equal). Unlike regular
23498 deduction, after all the arguments have been deduced in this way,
23499 we do *not* verify the deduced template argument values can be
23500 substituted into non-deduced contexts.
23501
23502 The logic can be a bit confusing here, because we look at deduce1 and
23503 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23504 can find template arguments for pat1 to make arg1 look like arg2, that
23505 means that arg2 is at least as specialized as arg1. */
23506
23507 int
23508 more_specialized_fn (tree pat1, tree pat2, int len)
23509 {
23510 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23511 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23512 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23513 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23514 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23515 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23516 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23517 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23518 tree origs1, origs2;
23519 bool lose1 = false;
23520 bool lose2 = false;
23521
23522 /* Remove the this parameter from non-static member functions. If
23523 one is a non-static member function and the other is not a static
23524 member function, remove the first parameter from that function
23525 also. This situation occurs for operator functions where we
23526 locate both a member function (with this pointer) and non-member
23527 operator (with explicit first operand). */
23528 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23529 {
23530 len--; /* LEN is the number of significant arguments for DECL1 */
23531 args1 = TREE_CHAIN (args1);
23532 if (!DECL_STATIC_FUNCTION_P (decl2))
23533 args2 = TREE_CHAIN (args2);
23534 }
23535 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23536 {
23537 args2 = TREE_CHAIN (args2);
23538 if (!DECL_STATIC_FUNCTION_P (decl1))
23539 {
23540 len--;
23541 args1 = TREE_CHAIN (args1);
23542 }
23543 }
23544
23545 /* If only one is a conversion operator, they are unordered. */
23546 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23547 return 0;
23548
23549 /* Consider the return type for a conversion function */
23550 if (DECL_CONV_FN_P (decl1))
23551 {
23552 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23553 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23554 len++;
23555 }
23556
23557 processing_template_decl++;
23558
23559 origs1 = args1;
23560 origs2 = args2;
23561
23562 while (len--
23563 /* Stop when an ellipsis is seen. */
23564 && args1 != NULL_TREE && args2 != NULL_TREE)
23565 {
23566 tree arg1 = TREE_VALUE (args1);
23567 tree arg2 = TREE_VALUE (args2);
23568 int deduce1, deduce2;
23569 int quals1 = -1;
23570 int quals2 = -1;
23571 int ref1 = 0;
23572 int ref2 = 0;
23573
23574 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23575 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23576 {
23577 /* When both arguments are pack expansions, we need only
23578 unify the patterns themselves. */
23579 arg1 = PACK_EXPANSION_PATTERN (arg1);
23580 arg2 = PACK_EXPANSION_PATTERN (arg2);
23581
23582 /* This is the last comparison we need to do. */
23583 len = 0;
23584 }
23585
23586 /* DR 1847: If a particular P contains no template-parameters that
23587 participate in template argument deduction, that P is not used to
23588 determine the ordering. */
23589 if (!uses_deducible_template_parms (arg1)
23590 && !uses_deducible_template_parms (arg2))
23591 goto next;
23592
23593 if (TYPE_REF_P (arg1))
23594 {
23595 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23596 arg1 = TREE_TYPE (arg1);
23597 quals1 = cp_type_quals (arg1);
23598 }
23599
23600 if (TYPE_REF_P (arg2))
23601 {
23602 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23603 arg2 = TREE_TYPE (arg2);
23604 quals2 = cp_type_quals (arg2);
23605 }
23606
23607 arg1 = TYPE_MAIN_VARIANT (arg1);
23608 arg2 = TYPE_MAIN_VARIANT (arg2);
23609
23610 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23611 {
23612 int i, len2 = remaining_arguments (args2);
23613 tree parmvec = make_tree_vec (1);
23614 tree argvec = make_tree_vec (len2);
23615 tree ta = args2;
23616
23617 /* Setup the parameter vector, which contains only ARG1. */
23618 TREE_VEC_ELT (parmvec, 0) = arg1;
23619
23620 /* Setup the argument vector, which contains the remaining
23621 arguments. */
23622 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23623 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23624
23625 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23626 argvec, DEDUCE_EXACT,
23627 /*subr=*/true, /*explain_p=*/false)
23628 == 0);
23629
23630 /* We cannot deduce in the other direction, because ARG1 is
23631 a pack expansion but ARG2 is not. */
23632 deduce2 = 0;
23633 }
23634 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23635 {
23636 int i, len1 = remaining_arguments (args1);
23637 tree parmvec = make_tree_vec (1);
23638 tree argvec = make_tree_vec (len1);
23639 tree ta = args1;
23640
23641 /* Setup the parameter vector, which contains only ARG1. */
23642 TREE_VEC_ELT (parmvec, 0) = arg2;
23643
23644 /* Setup the argument vector, which contains the remaining
23645 arguments. */
23646 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23647 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23648
23649 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23650 argvec, DEDUCE_EXACT,
23651 /*subr=*/true, /*explain_p=*/false)
23652 == 0);
23653
23654 /* We cannot deduce in the other direction, because ARG2 is
23655 a pack expansion but ARG1 is not.*/
23656 deduce1 = 0;
23657 }
23658
23659 else
23660 {
23661 /* The normal case, where neither argument is a pack
23662 expansion. */
23663 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23664 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23665 == 0);
23666 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23667 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23668 == 0);
23669 }
23670
23671 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23672 arg2, then arg2 is not as specialized as arg1. */
23673 if (!deduce1)
23674 lose2 = true;
23675 if (!deduce2)
23676 lose1 = true;
23677
23678 /* "If, for a given type, deduction succeeds in both directions
23679 (i.e., the types are identical after the transformations above)
23680 and both P and A were reference types (before being replaced with
23681 the type referred to above):
23682 - if the type from the argument template was an lvalue reference and
23683 the type from the parameter template was not, the argument type is
23684 considered to be more specialized than the other; otherwise,
23685 - if the type from the argument template is more cv-qualified
23686 than the type from the parameter template (as described above),
23687 the argument type is considered to be more specialized than the other;
23688 otherwise,
23689 - neither type is more specialized than the other." */
23690
23691 if (deduce1 && deduce2)
23692 {
23693 if (ref1 && ref2 && ref1 != ref2)
23694 {
23695 if (ref1 > ref2)
23696 lose1 = true;
23697 else
23698 lose2 = true;
23699 }
23700 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23701 {
23702 if ((quals1 & quals2) == quals2)
23703 lose2 = true;
23704 if ((quals1 & quals2) == quals1)
23705 lose1 = true;
23706 }
23707 }
23708
23709 if (lose1 && lose2)
23710 /* We've failed to deduce something in either direction.
23711 These must be unordered. */
23712 break;
23713
23714 next:
23715
23716 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23717 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23718 /* We have already processed all of the arguments in our
23719 handing of the pack expansion type. */
23720 len = 0;
23721
23722 args1 = TREE_CHAIN (args1);
23723 args2 = TREE_CHAIN (args2);
23724 }
23725
23726 /* "In most cases, all template parameters must have values in order for
23727 deduction to succeed, but for partial ordering purposes a template
23728 parameter may remain without a value provided it is not used in the
23729 types being used for partial ordering."
23730
23731 Thus, if we are missing any of the targs1 we need to substitute into
23732 origs1, then pat2 is not as specialized as pat1. This can happen when
23733 there is a nondeduced context. */
23734 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23735 lose2 = true;
23736 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23737 lose1 = true;
23738
23739 processing_template_decl--;
23740
23741 /* If both deductions succeed, the partial ordering selects the more
23742 constrained template. */
23743 if (!lose1 && !lose2)
23744 {
23745 int winner = more_constrained (decl1, decl2);
23746 if (winner > 0)
23747 lose2 = true;
23748 else if (winner < 0)
23749 lose1 = true;
23750 }
23751
23752 /* All things being equal, if the next argument is a pack expansion
23753 for one function but not for the other, prefer the
23754 non-variadic function. FIXME this is bogus; see c++/41958. */
23755 if (lose1 == lose2
23756 && args1 && TREE_VALUE (args1)
23757 && args2 && TREE_VALUE (args2))
23758 {
23759 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23760 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23761 }
23762
23763 if (lose1 == lose2)
23764 return 0;
23765 else if (!lose1)
23766 return 1;
23767 else
23768 return -1;
23769 }
23770
23771 /* Determine which of two partial specializations of TMPL is more
23772 specialized.
23773
23774 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23775 to the first partial specialization. The TREE_PURPOSE is the
23776 innermost set of template parameters for the partial
23777 specialization. PAT2 is similar, but for the second template.
23778
23779 Return 1 if the first partial specialization is more specialized;
23780 -1 if the second is more specialized; 0 if neither is more
23781 specialized.
23782
23783 See [temp.class.order] for information about determining which of
23784 two templates is more specialized. */
23785
23786 static int
23787 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23788 {
23789 tree targs;
23790 int winner = 0;
23791 bool any_deductions = false;
23792
23793 tree tmpl1 = TREE_VALUE (pat1);
23794 tree tmpl2 = TREE_VALUE (pat2);
23795 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23796 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23797
23798 /* Just like what happens for functions, if we are ordering between
23799 different template specializations, we may encounter dependent
23800 types in the arguments, and we need our dependency check functions
23801 to behave correctly. */
23802 ++processing_template_decl;
23803 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23804 if (targs)
23805 {
23806 --winner;
23807 any_deductions = true;
23808 }
23809
23810 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23811 if (targs)
23812 {
23813 ++winner;
23814 any_deductions = true;
23815 }
23816 --processing_template_decl;
23817
23818 /* If both deductions succeed, the partial ordering selects the more
23819 constrained template. */
23820 if (!winner && any_deductions)
23821 winner = more_constrained (tmpl1, tmpl2);
23822
23823 /* In the case of a tie where at least one of the templates
23824 has a parameter pack at the end, the template with the most
23825 non-packed parameters wins. */
23826 if (winner == 0
23827 && any_deductions
23828 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23829 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23830 {
23831 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23832 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23833 int len1 = TREE_VEC_LENGTH (args1);
23834 int len2 = TREE_VEC_LENGTH (args2);
23835
23836 /* We don't count the pack expansion at the end. */
23837 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23838 --len1;
23839 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23840 --len2;
23841
23842 if (len1 > len2)
23843 return 1;
23844 else if (len1 < len2)
23845 return -1;
23846 }
23847
23848 return winner;
23849 }
23850
23851 /* Return the template arguments that will produce the function signature
23852 DECL from the function template FN, with the explicit template
23853 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23854 also match. Return NULL_TREE if no satisfactory arguments could be
23855 found. */
23856
23857 static tree
23858 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23859 {
23860 int ntparms = DECL_NTPARMS (fn);
23861 tree targs = make_tree_vec (ntparms);
23862 tree decl_type = TREE_TYPE (decl);
23863 tree decl_arg_types;
23864 tree *args;
23865 unsigned int nargs, ix;
23866 tree arg;
23867
23868 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23869
23870 /* Never do unification on the 'this' parameter. */
23871 decl_arg_types = skip_artificial_parms_for (decl,
23872 TYPE_ARG_TYPES (decl_type));
23873
23874 nargs = list_length (decl_arg_types);
23875 args = XALLOCAVEC (tree, nargs);
23876 for (arg = decl_arg_types, ix = 0;
23877 arg != NULL_TREE && arg != void_list_node;
23878 arg = TREE_CHAIN (arg), ++ix)
23879 args[ix] = TREE_VALUE (arg);
23880
23881 if (fn_type_unification (fn, explicit_args, targs,
23882 args, ix,
23883 (check_rettype || DECL_CONV_FN_P (fn)
23884 ? TREE_TYPE (decl_type) : NULL_TREE),
23885 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23886 /*explain_p=*/false,
23887 /*decltype*/false)
23888 == error_mark_node)
23889 return NULL_TREE;
23890
23891 return targs;
23892 }
23893
23894 /* Return the innermost template arguments that, when applied to a partial
23895 specialization SPEC_TMPL of TMPL, yield the ARGS.
23896
23897 For example, suppose we have:
23898
23899 template <class T, class U> struct S {};
23900 template <class T> struct S<T*, int> {};
23901
23902 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23903 partial specialization and the ARGS will be {double*, int}. The resulting
23904 vector will be {double}, indicating that `T' is bound to `double'. */
23905
23906 static tree
23907 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23908 {
23909 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23910 tree spec_args
23911 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23912 int i, ntparms = TREE_VEC_LENGTH (tparms);
23913 tree deduced_args;
23914 tree innermost_deduced_args;
23915
23916 innermost_deduced_args = make_tree_vec (ntparms);
23917 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23918 {
23919 deduced_args = copy_node (args);
23920 SET_TMPL_ARGS_LEVEL (deduced_args,
23921 TMPL_ARGS_DEPTH (deduced_args),
23922 innermost_deduced_args);
23923 }
23924 else
23925 deduced_args = innermost_deduced_args;
23926
23927 bool tried_array_deduction = (cxx_dialect < cxx17);
23928 again:
23929 if (unify (tparms, deduced_args,
23930 INNERMOST_TEMPLATE_ARGS (spec_args),
23931 INNERMOST_TEMPLATE_ARGS (args),
23932 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23933 return NULL_TREE;
23934
23935 for (i = 0; i < ntparms; ++i)
23936 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23937 {
23938 if (!tried_array_deduction)
23939 {
23940 try_array_deduction (tparms, innermost_deduced_args,
23941 INNERMOST_TEMPLATE_ARGS (spec_args));
23942 tried_array_deduction = true;
23943 if (TREE_VEC_ELT (innermost_deduced_args, i))
23944 goto again;
23945 }
23946 return NULL_TREE;
23947 }
23948
23949 if (!push_tinst_level (spec_tmpl, deduced_args))
23950 {
23951 excessive_deduction_depth = true;
23952 return NULL_TREE;
23953 }
23954
23955 /* Verify that nondeduced template arguments agree with the type
23956 obtained from argument deduction.
23957
23958 For example:
23959
23960 struct A { typedef int X; };
23961 template <class T, class U> struct C {};
23962 template <class T> struct C<T, typename T::X> {};
23963
23964 Then with the instantiation `C<A, int>', we can deduce that
23965 `T' is `A' but unify () does not check whether `typename T::X'
23966 is `int'. */
23967 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23968
23969 if (spec_args != error_mark_node)
23970 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23971 INNERMOST_TEMPLATE_ARGS (spec_args),
23972 tmpl, tf_none, false, false);
23973
23974 pop_tinst_level ();
23975
23976 if (spec_args == error_mark_node
23977 /* We only need to check the innermost arguments; the other
23978 arguments will always agree. */
23979 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23980 INNERMOST_TEMPLATE_ARGS (args)))
23981 return NULL_TREE;
23982
23983 /* Now that we have bindings for all of the template arguments,
23984 ensure that the arguments deduced for the template template
23985 parameters have compatible template parameter lists. See the use
23986 of template_template_parm_bindings_ok_p in fn_type_unification
23987 for more information. */
23988 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23989 return NULL_TREE;
23990
23991 return deduced_args;
23992 }
23993
23994 // Compare two function templates T1 and T2 by deducing bindings
23995 // from one against the other. If both deductions succeed, compare
23996 // constraints to see which is more constrained.
23997 static int
23998 more_specialized_inst (tree t1, tree t2)
23999 {
24000 int fate = 0;
24001 int count = 0;
24002
24003 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24004 {
24005 --fate;
24006 ++count;
24007 }
24008
24009 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24010 {
24011 ++fate;
24012 ++count;
24013 }
24014
24015 // If both deductions succeed, then one may be more constrained.
24016 if (count == 2 && fate == 0)
24017 fate = more_constrained (t1, t2);
24018
24019 return fate;
24020 }
24021
24022 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24023 Return the TREE_LIST node with the most specialized template, if
24024 any. If there is no most specialized template, the error_mark_node
24025 is returned.
24026
24027 Note that this function does not look at, or modify, the
24028 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24029 returned is one of the elements of INSTANTIATIONS, callers may
24030 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24031 and retrieve it from the value returned. */
24032
24033 tree
24034 most_specialized_instantiation (tree templates)
24035 {
24036 tree fn, champ;
24037
24038 ++processing_template_decl;
24039
24040 champ = templates;
24041 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24042 {
24043 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24044 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24045 if (fate == -1)
24046 champ = fn;
24047 else if (!fate)
24048 {
24049 /* Equally specialized, move to next function. If there
24050 is no next function, nothing's most specialized. */
24051 fn = TREE_CHAIN (fn);
24052 champ = fn;
24053 if (!fn)
24054 break;
24055 }
24056 }
24057
24058 if (champ)
24059 /* Now verify that champ is better than everything earlier in the
24060 instantiation list. */
24061 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24062 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24063 {
24064 champ = NULL_TREE;
24065 break;
24066 }
24067 }
24068
24069 processing_template_decl--;
24070
24071 if (!champ)
24072 return error_mark_node;
24073
24074 return champ;
24075 }
24076
24077 /* If DECL is a specialization of some template, return the most
24078 general such template. Otherwise, returns NULL_TREE.
24079
24080 For example, given:
24081
24082 template <class T> struct S { template <class U> void f(U); };
24083
24084 if TMPL is `template <class U> void S<int>::f(U)' this will return
24085 the full template. This function will not trace past partial
24086 specializations, however. For example, given in addition:
24087
24088 template <class T> struct S<T*> { template <class U> void f(U); };
24089
24090 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24091 `template <class T> template <class U> S<T*>::f(U)'. */
24092
24093 tree
24094 most_general_template (tree decl)
24095 {
24096 if (TREE_CODE (decl) != TEMPLATE_DECL)
24097 {
24098 if (tree tinfo = get_template_info (decl))
24099 decl = TI_TEMPLATE (tinfo);
24100 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24101 template friend, or a FIELD_DECL for a capture pack. */
24102 if (TREE_CODE (decl) != TEMPLATE_DECL)
24103 return NULL_TREE;
24104 }
24105
24106 /* Look for more and more general templates. */
24107 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24108 {
24109 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24110 (See cp-tree.h for details.) */
24111 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24112 break;
24113
24114 if (CLASS_TYPE_P (TREE_TYPE (decl))
24115 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24116 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24117 break;
24118
24119 /* Stop if we run into an explicitly specialized class template. */
24120 if (!DECL_NAMESPACE_SCOPE_P (decl)
24121 && DECL_CONTEXT (decl)
24122 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24123 break;
24124
24125 decl = DECL_TI_TEMPLATE (decl);
24126 }
24127
24128 return decl;
24129 }
24130
24131 /* Return the most specialized of the template partial specializations
24132 which can produce TARGET, a specialization of some class or variable
24133 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24134 a TEMPLATE_DECL node corresponding to the partial specialization, while
24135 the TREE_PURPOSE is the set of template arguments that must be
24136 substituted into the template pattern in order to generate TARGET.
24137
24138 If the choice of partial specialization is ambiguous, a diagnostic
24139 is issued, and the error_mark_node is returned. If there are no
24140 partial specializations matching TARGET, then NULL_TREE is
24141 returned, indicating that the primary template should be used. */
24142
24143 static tree
24144 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24145 {
24146 tree list = NULL_TREE;
24147 tree t;
24148 tree champ;
24149 int fate;
24150 bool ambiguous_p;
24151 tree outer_args = NULL_TREE;
24152 tree tmpl, args;
24153
24154 if (TYPE_P (target))
24155 {
24156 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24157 tmpl = TI_TEMPLATE (tinfo);
24158 args = TI_ARGS (tinfo);
24159 }
24160 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24161 {
24162 tmpl = TREE_OPERAND (target, 0);
24163 args = TREE_OPERAND (target, 1);
24164 }
24165 else if (VAR_P (target))
24166 {
24167 tree tinfo = DECL_TEMPLATE_INFO (target);
24168 tmpl = TI_TEMPLATE (tinfo);
24169 args = TI_ARGS (tinfo);
24170 }
24171 else
24172 gcc_unreachable ();
24173
24174 tree main_tmpl = most_general_template (tmpl);
24175
24176 /* For determining which partial specialization to use, only the
24177 innermost args are interesting. */
24178 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24179 {
24180 outer_args = strip_innermost_template_args (args, 1);
24181 args = INNERMOST_TEMPLATE_ARGS (args);
24182 }
24183
24184 /* The caller hasn't called push_to_top_level yet, but we need
24185 get_partial_spec_bindings to be done in non-template context so that we'll
24186 fully resolve everything. */
24187 processing_template_decl_sentinel ptds;
24188
24189 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24190 {
24191 tree spec_args;
24192 tree spec_tmpl = TREE_VALUE (t);
24193
24194 if (outer_args)
24195 {
24196 /* Substitute in the template args from the enclosing class. */
24197 ++processing_template_decl;
24198 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24199 --processing_template_decl;
24200 }
24201
24202 if (spec_tmpl == error_mark_node)
24203 return error_mark_node;
24204
24205 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24206 if (spec_args)
24207 {
24208 if (outer_args)
24209 spec_args = add_to_template_args (outer_args, spec_args);
24210
24211 /* Keep the candidate only if the constraints are satisfied,
24212 or if we're not compiling with concepts. */
24213 if (!flag_concepts
24214 || constraints_satisfied_p (spec_tmpl, spec_args))
24215 {
24216 list = tree_cons (spec_args, TREE_VALUE (t), list);
24217 TREE_TYPE (list) = TREE_TYPE (t);
24218 }
24219 }
24220 }
24221
24222 if (! list)
24223 return NULL_TREE;
24224
24225 ambiguous_p = false;
24226 t = list;
24227 champ = t;
24228 t = TREE_CHAIN (t);
24229 for (; t; t = TREE_CHAIN (t))
24230 {
24231 fate = more_specialized_partial_spec (tmpl, champ, t);
24232 if (fate == 1)
24233 ;
24234 else
24235 {
24236 if (fate == 0)
24237 {
24238 t = TREE_CHAIN (t);
24239 if (! t)
24240 {
24241 ambiguous_p = true;
24242 break;
24243 }
24244 }
24245 champ = t;
24246 }
24247 }
24248
24249 if (!ambiguous_p)
24250 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24251 {
24252 fate = more_specialized_partial_spec (tmpl, champ, t);
24253 if (fate != 1)
24254 {
24255 ambiguous_p = true;
24256 break;
24257 }
24258 }
24259
24260 if (ambiguous_p)
24261 {
24262 const char *str;
24263 char *spaces = NULL;
24264 if (!(complain & tf_error))
24265 return error_mark_node;
24266 if (TYPE_P (target))
24267 error ("ambiguous template instantiation for %q#T", target);
24268 else
24269 error ("ambiguous template instantiation for %q#D", target);
24270 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24271 for (t = list; t; t = TREE_CHAIN (t))
24272 {
24273 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24274 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24275 "%s %#qS", spaces ? spaces : str, subst);
24276 spaces = spaces ? spaces : get_spaces (str);
24277 }
24278 free (spaces);
24279 return error_mark_node;
24280 }
24281
24282 return champ;
24283 }
24284
24285 /* Explicitly instantiate DECL. */
24286
24287 void
24288 do_decl_instantiation (tree decl, tree storage)
24289 {
24290 tree result = NULL_TREE;
24291 int extern_p = 0;
24292
24293 if (!decl || decl == error_mark_node)
24294 /* An error occurred, for which grokdeclarator has already issued
24295 an appropriate message. */
24296 return;
24297 else if (! DECL_LANG_SPECIFIC (decl))
24298 {
24299 error ("explicit instantiation of non-template %q#D", decl);
24300 return;
24301 }
24302 else if (DECL_DECLARED_CONCEPT_P (decl))
24303 {
24304 if (VAR_P (decl))
24305 error ("explicit instantiation of variable concept %q#D", decl);
24306 else
24307 error ("explicit instantiation of function concept %q#D", decl);
24308 return;
24309 }
24310
24311 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24312 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24313
24314 if (VAR_P (decl) && !var_templ)
24315 {
24316 /* There is an asymmetry here in the way VAR_DECLs and
24317 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24318 the latter, the DECL we get back will be marked as a
24319 template instantiation, and the appropriate
24320 DECL_TEMPLATE_INFO will be set up. This does not happen for
24321 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24322 should handle VAR_DECLs as it currently handles
24323 FUNCTION_DECLs. */
24324 if (!DECL_CLASS_SCOPE_P (decl))
24325 {
24326 error ("%qD is not a static data member of a class template", decl);
24327 return;
24328 }
24329 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24330 if (!result || !VAR_P (result))
24331 {
24332 error ("no matching template for %qD found", decl);
24333 return;
24334 }
24335 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24336 {
24337 error ("type %qT for explicit instantiation %qD does not match "
24338 "declared type %qT", TREE_TYPE (result), decl,
24339 TREE_TYPE (decl));
24340 return;
24341 }
24342 }
24343 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24344 {
24345 error ("explicit instantiation of %q#D", decl);
24346 return;
24347 }
24348 else
24349 result = decl;
24350
24351 /* Check for various error cases. Note that if the explicit
24352 instantiation is valid the RESULT will currently be marked as an
24353 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24354 until we get here. */
24355
24356 if (DECL_TEMPLATE_SPECIALIZATION (result))
24357 {
24358 /* DR 259 [temp.spec].
24359
24360 Both an explicit instantiation and a declaration of an explicit
24361 specialization shall not appear in a program unless the explicit
24362 instantiation follows a declaration of the explicit specialization.
24363
24364 For a given set of template parameters, if an explicit
24365 instantiation of a template appears after a declaration of an
24366 explicit specialization for that template, the explicit
24367 instantiation has no effect. */
24368 return;
24369 }
24370 else if (DECL_EXPLICIT_INSTANTIATION (result))
24371 {
24372 /* [temp.spec]
24373
24374 No program shall explicitly instantiate any template more
24375 than once.
24376
24377 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24378 the first instantiation was `extern' and the second is not,
24379 and EXTERN_P for the opposite case. */
24380 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24381 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24382 /* If an "extern" explicit instantiation follows an ordinary
24383 explicit instantiation, the template is instantiated. */
24384 if (extern_p)
24385 return;
24386 }
24387 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24388 {
24389 error ("no matching template for %qD found", result);
24390 return;
24391 }
24392 else if (!DECL_TEMPLATE_INFO (result))
24393 {
24394 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24395 return;
24396 }
24397
24398 if (storage == NULL_TREE)
24399 ;
24400 else if (storage == ridpointers[(int) RID_EXTERN])
24401 {
24402 if (cxx_dialect == cxx98)
24403 pedwarn (input_location, OPT_Wpedantic,
24404 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24405 "instantiations");
24406 extern_p = 1;
24407 }
24408 else
24409 error ("storage class %qD applied to template instantiation", storage);
24410
24411 check_explicit_instantiation_namespace (result);
24412 mark_decl_instantiated (result, extern_p);
24413 if (! extern_p)
24414 instantiate_decl (result, /*defer_ok=*/true,
24415 /*expl_inst_class_mem_p=*/false);
24416 }
24417
24418 static void
24419 mark_class_instantiated (tree t, int extern_p)
24420 {
24421 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24422 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24423 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24424 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24425 if (! extern_p)
24426 {
24427 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24428 rest_of_type_compilation (t, 1);
24429 }
24430 }
24431
24432 /* Called from do_type_instantiation through binding_table_foreach to
24433 do recursive instantiation for the type bound in ENTRY. */
24434 static void
24435 bt_instantiate_type_proc (binding_entry entry, void *data)
24436 {
24437 tree storage = *(tree *) data;
24438
24439 if (MAYBE_CLASS_TYPE_P (entry->type)
24440 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24441 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24442 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24443 }
24444
24445 /* Perform an explicit instantiation of template class T. STORAGE, if
24446 non-null, is the RID for extern, inline or static. COMPLAIN is
24447 nonzero if this is called from the parser, zero if called recursively,
24448 since the standard is unclear (as detailed below). */
24449
24450 void
24451 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24452 {
24453 int extern_p = 0;
24454 int nomem_p = 0;
24455 int static_p = 0;
24456 int previous_instantiation_extern_p = 0;
24457
24458 if (TREE_CODE (t) == TYPE_DECL)
24459 t = TREE_TYPE (t);
24460
24461 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24462 {
24463 tree tmpl =
24464 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24465 if (tmpl)
24466 error ("explicit instantiation of non-class template %qD", tmpl);
24467 else
24468 error ("explicit instantiation of non-template type %qT", t);
24469 return;
24470 }
24471
24472 complete_type (t);
24473
24474 if (!COMPLETE_TYPE_P (t))
24475 {
24476 if (complain & tf_error)
24477 error ("explicit instantiation of %q#T before definition of template",
24478 t);
24479 return;
24480 }
24481
24482 if (storage != NULL_TREE)
24483 {
24484 if (storage == ridpointers[(int) RID_EXTERN])
24485 {
24486 if (cxx_dialect == cxx98)
24487 pedwarn (input_location, OPT_Wpedantic,
24488 "ISO C++ 1998 forbids the use of %<extern%> on "
24489 "explicit instantiations");
24490 }
24491 else
24492 pedwarn (input_location, OPT_Wpedantic,
24493 "ISO C++ forbids the use of %qE"
24494 " on explicit instantiations", storage);
24495
24496 if (storage == ridpointers[(int) RID_INLINE])
24497 nomem_p = 1;
24498 else if (storage == ridpointers[(int) RID_EXTERN])
24499 extern_p = 1;
24500 else if (storage == ridpointers[(int) RID_STATIC])
24501 static_p = 1;
24502 else
24503 {
24504 error ("storage class %qD applied to template instantiation",
24505 storage);
24506 extern_p = 0;
24507 }
24508 }
24509
24510 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24511 {
24512 /* DR 259 [temp.spec].
24513
24514 Both an explicit instantiation and a declaration of an explicit
24515 specialization shall not appear in a program unless the explicit
24516 instantiation follows a declaration of the explicit specialization.
24517
24518 For a given set of template parameters, if an explicit
24519 instantiation of a template appears after a declaration of an
24520 explicit specialization for that template, the explicit
24521 instantiation has no effect. */
24522 return;
24523 }
24524 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24525 {
24526 /* [temp.spec]
24527
24528 No program shall explicitly instantiate any template more
24529 than once.
24530
24531 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24532 instantiation was `extern'. If EXTERN_P then the second is.
24533 These cases are OK. */
24534 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24535
24536 if (!previous_instantiation_extern_p && !extern_p
24537 && (complain & tf_error))
24538 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24539
24540 /* If we've already instantiated the template, just return now. */
24541 if (!CLASSTYPE_INTERFACE_ONLY (t))
24542 return;
24543 }
24544
24545 check_explicit_instantiation_namespace (TYPE_NAME (t));
24546 mark_class_instantiated (t, extern_p);
24547
24548 if (nomem_p)
24549 return;
24550
24551 /* In contrast to implicit instantiation, where only the
24552 declarations, and not the definitions, of members are
24553 instantiated, we have here:
24554
24555 [temp.explicit]
24556
24557 The explicit instantiation of a class template specialization
24558 implies the instantiation of all of its members not
24559 previously explicitly specialized in the translation unit
24560 containing the explicit instantiation.
24561
24562 Of course, we can't instantiate member template classes, since we
24563 don't have any arguments for them. Note that the standard is
24564 unclear on whether the instantiation of the members are
24565 *explicit* instantiations or not. However, the most natural
24566 interpretation is that it should be an explicit
24567 instantiation. */
24568 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24569 if ((VAR_P (fld)
24570 || (TREE_CODE (fld) == FUNCTION_DECL
24571 && !static_p
24572 && user_provided_p (fld)))
24573 && DECL_TEMPLATE_INSTANTIATION (fld))
24574 {
24575 mark_decl_instantiated (fld, extern_p);
24576 if (! extern_p)
24577 instantiate_decl (fld, /*defer_ok=*/true,
24578 /*expl_inst_class_mem_p=*/true);
24579 }
24580
24581 if (CLASSTYPE_NESTED_UTDS (t))
24582 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24583 bt_instantiate_type_proc, &storage);
24584 }
24585
24586 /* Given a function DECL, which is a specialization of TMPL, modify
24587 DECL to be a re-instantiation of TMPL with the same template
24588 arguments. TMPL should be the template into which tsubst'ing
24589 should occur for DECL, not the most general template.
24590
24591 One reason for doing this is a scenario like this:
24592
24593 template <class T>
24594 void f(const T&, int i);
24595
24596 void g() { f(3, 7); }
24597
24598 template <class T>
24599 void f(const T& t, const int i) { }
24600
24601 Note that when the template is first instantiated, with
24602 instantiate_template, the resulting DECL will have no name for the
24603 first parameter, and the wrong type for the second. So, when we go
24604 to instantiate the DECL, we regenerate it. */
24605
24606 static void
24607 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24608 {
24609 /* The arguments used to instantiate DECL, from the most general
24610 template. */
24611 tree code_pattern;
24612
24613 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24614
24615 /* Make sure that we can see identifiers, and compute access
24616 correctly. */
24617 push_access_scope (decl);
24618
24619 if (TREE_CODE (decl) == FUNCTION_DECL)
24620 {
24621 tree decl_parm;
24622 tree pattern_parm;
24623 tree specs;
24624 int args_depth;
24625 int parms_depth;
24626
24627 args_depth = TMPL_ARGS_DEPTH (args);
24628 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24629 if (args_depth > parms_depth)
24630 args = get_innermost_template_args (args, parms_depth);
24631
24632 /* Instantiate a dynamic exception-specification. noexcept will be
24633 handled below. */
24634 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24635 if (TREE_VALUE (raises))
24636 {
24637 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24638 args, tf_error, NULL_TREE,
24639 /*defer_ok*/false);
24640 if (specs && specs != error_mark_node)
24641 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24642 specs);
24643 }
24644
24645 /* Merge parameter declarations. */
24646 decl_parm = skip_artificial_parms_for (decl,
24647 DECL_ARGUMENTS (decl));
24648 pattern_parm
24649 = skip_artificial_parms_for (code_pattern,
24650 DECL_ARGUMENTS (code_pattern));
24651 while (decl_parm && !DECL_PACK_P (pattern_parm))
24652 {
24653 tree parm_type;
24654 tree attributes;
24655
24656 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24657 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24658 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24659 NULL_TREE);
24660 parm_type = type_decays_to (parm_type);
24661 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24662 TREE_TYPE (decl_parm) = parm_type;
24663 attributes = DECL_ATTRIBUTES (pattern_parm);
24664 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24665 {
24666 DECL_ATTRIBUTES (decl_parm) = attributes;
24667 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24668 }
24669 decl_parm = DECL_CHAIN (decl_parm);
24670 pattern_parm = DECL_CHAIN (pattern_parm);
24671 }
24672 /* Merge any parameters that match with the function parameter
24673 pack. */
24674 if (pattern_parm && DECL_PACK_P (pattern_parm))
24675 {
24676 int i, len;
24677 tree expanded_types;
24678 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24679 the parameters in this function parameter pack. */
24680 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24681 args, tf_error, NULL_TREE);
24682 len = TREE_VEC_LENGTH (expanded_types);
24683 for (i = 0; i < len; i++)
24684 {
24685 tree parm_type;
24686 tree attributes;
24687
24688 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24689 /* Rename the parameter to include the index. */
24690 DECL_NAME (decl_parm) =
24691 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24692 parm_type = TREE_VEC_ELT (expanded_types, i);
24693 parm_type = type_decays_to (parm_type);
24694 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24695 TREE_TYPE (decl_parm) = parm_type;
24696 attributes = DECL_ATTRIBUTES (pattern_parm);
24697 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24698 {
24699 DECL_ATTRIBUTES (decl_parm) = attributes;
24700 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24701 }
24702 decl_parm = DECL_CHAIN (decl_parm);
24703 }
24704 }
24705 /* Merge additional specifiers from the CODE_PATTERN. */
24706 if (DECL_DECLARED_INLINE_P (code_pattern)
24707 && !DECL_DECLARED_INLINE_P (decl))
24708 DECL_DECLARED_INLINE_P (decl) = 1;
24709
24710 maybe_instantiate_noexcept (decl, tf_error);
24711 }
24712 else if (VAR_P (decl))
24713 {
24714 start_lambda_scope (decl);
24715 DECL_INITIAL (decl) =
24716 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24717 tf_error, DECL_TI_TEMPLATE (decl));
24718 finish_lambda_scope ();
24719 if (VAR_HAD_UNKNOWN_BOUND (decl))
24720 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24721 tf_error, DECL_TI_TEMPLATE (decl));
24722 }
24723 else
24724 gcc_unreachable ();
24725
24726 pop_access_scope (decl);
24727 }
24728
24729 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24730 substituted to get DECL. */
24731
24732 tree
24733 template_for_substitution (tree decl)
24734 {
24735 tree tmpl = DECL_TI_TEMPLATE (decl);
24736
24737 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24738 for the instantiation. This is not always the most general
24739 template. Consider, for example:
24740
24741 template <class T>
24742 struct S { template <class U> void f();
24743 template <> void f<int>(); };
24744
24745 and an instantiation of S<double>::f<int>. We want TD to be the
24746 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24747 while (/* An instantiation cannot have a definition, so we need a
24748 more general template. */
24749 DECL_TEMPLATE_INSTANTIATION (tmpl)
24750 /* We must also deal with friend templates. Given:
24751
24752 template <class T> struct S {
24753 template <class U> friend void f() {};
24754 };
24755
24756 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24757 so far as the language is concerned, but that's still
24758 where we get the pattern for the instantiation from. On
24759 other hand, if the definition comes outside the class, say:
24760
24761 template <class T> struct S {
24762 template <class U> friend void f();
24763 };
24764 template <class U> friend void f() {}
24765
24766 we don't need to look any further. That's what the check for
24767 DECL_INITIAL is for. */
24768 || (TREE_CODE (decl) == FUNCTION_DECL
24769 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24770 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24771 {
24772 /* The present template, TD, should not be a definition. If it
24773 were a definition, we should be using it! Note that we
24774 cannot restructure the loop to just keep going until we find
24775 a template with a definition, since that might go too far if
24776 a specialization was declared, but not defined. */
24777
24778 /* Fetch the more general template. */
24779 tmpl = DECL_TI_TEMPLATE (tmpl);
24780 }
24781
24782 return tmpl;
24783 }
24784
24785 /* Returns true if we need to instantiate this template instance even if we
24786 know we aren't going to emit it. */
24787
24788 bool
24789 always_instantiate_p (tree decl)
24790 {
24791 /* We always instantiate inline functions so that we can inline them. An
24792 explicit instantiation declaration prohibits implicit instantiation of
24793 non-inline functions. With high levels of optimization, we would
24794 normally inline non-inline functions -- but we're not allowed to do
24795 that for "extern template" functions. Therefore, we check
24796 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24797 return ((TREE_CODE (decl) == FUNCTION_DECL
24798 && (DECL_DECLARED_INLINE_P (decl)
24799 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24800 /* And we need to instantiate static data members so that
24801 their initializers are available in integral constant
24802 expressions. */
24803 || (VAR_P (decl)
24804 && decl_maybe_constant_var_p (decl)));
24805 }
24806
24807 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24808 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24809 error, true otherwise. */
24810
24811 bool
24812 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24813 {
24814 tree fntype, spec, noex, clone;
24815
24816 /* Don't instantiate a noexcept-specification from template context. */
24817 if (processing_template_decl
24818 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24819 return true;
24820
24821 if (DECL_CLONED_FUNCTION_P (fn))
24822 fn = DECL_CLONED_FUNCTION (fn);
24823
24824 tree orig_fn = NULL_TREE;
24825 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24826 its FUNCTION_DECL for the rest of this function -- push_access_scope
24827 doesn't accept TEMPLATE_DECLs. */
24828 if (DECL_FUNCTION_TEMPLATE_P (fn))
24829 {
24830 orig_fn = fn;
24831 fn = DECL_TEMPLATE_RESULT (fn);
24832 }
24833
24834 fntype = TREE_TYPE (fn);
24835 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24836
24837 if (!spec || !TREE_PURPOSE (spec))
24838 return true;
24839
24840 noex = TREE_PURPOSE (spec);
24841
24842 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24843 {
24844 static hash_set<tree>* fns = new hash_set<tree>;
24845 bool added = false;
24846 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24847 {
24848 spec = get_defaulted_eh_spec (fn, complain);
24849 if (spec == error_mark_node)
24850 /* This might have failed because of an unparsed DMI, so
24851 let's try again later. */
24852 return false;
24853 }
24854 else if (!(added = !fns->add (fn)))
24855 {
24856 /* If hash_set::add returns true, the element was already there. */
24857 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24858 DECL_SOURCE_LOCATION (fn));
24859 error_at (loc,
24860 "exception specification of %qD depends on itself",
24861 fn);
24862 spec = noexcept_false_spec;
24863 }
24864 else if (push_tinst_level (fn))
24865 {
24866 push_to_top_level ();
24867 push_access_scope (fn);
24868 push_deferring_access_checks (dk_no_deferred);
24869 input_location = DECL_SOURCE_LOCATION (fn);
24870
24871 /* If needed, set current_class_ptr for the benefit of
24872 tsubst_copy/PARM_DECL. */
24873 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24874 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24875 {
24876 tree this_parm = DECL_ARGUMENTS (tdecl);
24877 current_class_ptr = NULL_TREE;
24878 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24879 current_class_ptr = this_parm;
24880 }
24881
24882 /* If this function is represented by a TEMPLATE_DECL, then
24883 the deferred noexcept-specification might still contain
24884 dependent types, even after substitution. And we need the
24885 dependency check functions to work in build_noexcept_spec. */
24886 if (orig_fn)
24887 ++processing_template_decl;
24888
24889 /* Do deferred instantiation of the noexcept-specifier. */
24890 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24891 DEFERRED_NOEXCEPT_ARGS (noex),
24892 tf_warning_or_error, fn,
24893 /*function_p=*/false,
24894 /*i_c_e_p=*/true);
24895
24896 /* Build up the noexcept-specification. */
24897 spec = build_noexcept_spec (noex, tf_warning_or_error);
24898
24899 if (orig_fn)
24900 --processing_template_decl;
24901
24902 pop_deferring_access_checks ();
24903 pop_access_scope (fn);
24904 pop_tinst_level ();
24905 pop_from_top_level ();
24906 }
24907 else
24908 spec = noexcept_false_spec;
24909
24910 if (added)
24911 fns->remove (fn);
24912
24913 if (spec == error_mark_node)
24914 {
24915 /* This failed with a hard error, so let's go with false. */
24916 gcc_assert (seen_error ());
24917 spec = noexcept_false_spec;
24918 }
24919
24920 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24921 if (orig_fn)
24922 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24923
24924 FOR_EACH_CLONE (clone, fn)
24925 {
24926 if (TREE_TYPE (clone) == fntype)
24927 TREE_TYPE (clone) = TREE_TYPE (fn);
24928 else
24929 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24930 }
24931 }
24932
24933 return true;
24934 }
24935
24936 /* We're starting to process the function INST, an instantiation of PATTERN;
24937 add their parameters to local_specializations. */
24938
24939 static void
24940 register_parameter_specializations (tree pattern, tree inst)
24941 {
24942 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24943 tree spec_parm = DECL_ARGUMENTS (inst);
24944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24945 {
24946 register_local_specialization (spec_parm, tmpl_parm);
24947 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24948 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24949 }
24950 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24951 {
24952 if (!DECL_PACK_P (tmpl_parm))
24953 {
24954 register_local_specialization (spec_parm, tmpl_parm);
24955 spec_parm = DECL_CHAIN (spec_parm);
24956 }
24957 else
24958 {
24959 /* Register the (value) argument pack as a specialization of
24960 TMPL_PARM, then move on. */
24961 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24962 register_local_specialization (argpack, tmpl_parm);
24963 }
24964 }
24965 gcc_assert (!spec_parm);
24966 }
24967
24968 /* Produce the definition of D, a _DECL generated from a template. If
24969 DEFER_OK is true, then we don't have to actually do the
24970 instantiation now; we just have to do it sometime. Normally it is
24971 an error if this is an explicit instantiation but D is undefined.
24972 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24973 instantiated class template. */
24974
24975 tree
24976 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24977 {
24978 tree tmpl = DECL_TI_TEMPLATE (d);
24979 tree gen_args;
24980 tree args;
24981 tree td;
24982 tree code_pattern;
24983 tree spec;
24984 tree gen_tmpl;
24985 bool pattern_defined;
24986 location_t saved_loc = input_location;
24987 int saved_unevaluated_operand = cp_unevaluated_operand;
24988 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24989 bool external_p;
24990 bool deleted_p;
24991
24992 /* This function should only be used to instantiate templates for
24993 functions and static member variables. */
24994 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24995
24996 /* A concept is never instantiated. */
24997 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24998
24999 /* Variables are never deferred; if instantiation is required, they
25000 are instantiated right away. That allows for better code in the
25001 case that an expression refers to the value of the variable --
25002 if the variable has a constant value the referring expression can
25003 take advantage of that fact. */
25004 if (VAR_P (d))
25005 defer_ok = false;
25006
25007 /* Don't instantiate cloned functions. Instead, instantiate the
25008 functions they cloned. */
25009 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25010 d = DECL_CLONED_FUNCTION (d);
25011
25012 if (DECL_TEMPLATE_INSTANTIATED (d)
25013 || (TREE_CODE (d) == FUNCTION_DECL
25014 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25015 || DECL_TEMPLATE_SPECIALIZATION (d))
25016 /* D has already been instantiated or explicitly specialized, so
25017 there's nothing for us to do here.
25018
25019 It might seem reasonable to check whether or not D is an explicit
25020 instantiation, and, if so, stop here. But when an explicit
25021 instantiation is deferred until the end of the compilation,
25022 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25023 the instantiation. */
25024 return d;
25025
25026 /* Check to see whether we know that this template will be
25027 instantiated in some other file, as with "extern template"
25028 extension. */
25029 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25030
25031 /* In general, we do not instantiate such templates. */
25032 if (external_p && !always_instantiate_p (d))
25033 return d;
25034
25035 gen_tmpl = most_general_template (tmpl);
25036 gen_args = DECL_TI_ARGS (d);
25037
25038 if (tmpl != gen_tmpl)
25039 /* We should already have the extra args. */
25040 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25041 == TMPL_ARGS_DEPTH (gen_args));
25042 /* And what's in the hash table should match D. */
25043 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25044 || spec == NULL_TREE);
25045
25046 /* This needs to happen before any tsubsting. */
25047 if (! push_tinst_level (d))
25048 return d;
25049
25050 timevar_push (TV_TEMPLATE_INST);
25051
25052 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25053 for the instantiation. */
25054 td = template_for_substitution (d);
25055 args = gen_args;
25056
25057 if (VAR_P (d))
25058 {
25059 /* Look up an explicit specialization, if any. */
25060 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25061 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25062 if (elt && elt != error_mark_node)
25063 {
25064 td = TREE_VALUE (elt);
25065 args = TREE_PURPOSE (elt);
25066 }
25067 }
25068
25069 code_pattern = DECL_TEMPLATE_RESULT (td);
25070
25071 /* We should never be trying to instantiate a member of a class
25072 template or partial specialization. */
25073 gcc_assert (d != code_pattern);
25074
25075 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25076 || DECL_TEMPLATE_SPECIALIZATION (td))
25077 /* In the case of a friend template whose definition is provided
25078 outside the class, we may have too many arguments. Drop the
25079 ones we don't need. The same is true for specializations. */
25080 args = get_innermost_template_args
25081 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25082
25083 if (TREE_CODE (d) == FUNCTION_DECL)
25084 {
25085 deleted_p = DECL_DELETED_FN (code_pattern);
25086 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25087 && DECL_INITIAL (code_pattern) != error_mark_node)
25088 || DECL_DEFAULTED_FN (code_pattern)
25089 || deleted_p);
25090 }
25091 else
25092 {
25093 deleted_p = false;
25094 if (DECL_CLASS_SCOPE_P (code_pattern))
25095 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25096 else
25097 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25098 }
25099
25100 /* We may be in the middle of deferred access check. Disable it now. */
25101 push_deferring_access_checks (dk_no_deferred);
25102
25103 /* Unless an explicit instantiation directive has already determined
25104 the linkage of D, remember that a definition is available for
25105 this entity. */
25106 if (pattern_defined
25107 && !DECL_INTERFACE_KNOWN (d)
25108 && !DECL_NOT_REALLY_EXTERN (d))
25109 mark_definable (d);
25110
25111 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25112 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25113 input_location = DECL_SOURCE_LOCATION (d);
25114
25115 /* If D is a member of an explicitly instantiated class template,
25116 and no definition is available, treat it like an implicit
25117 instantiation. */
25118 if (!pattern_defined && expl_inst_class_mem_p
25119 && DECL_EXPLICIT_INSTANTIATION (d))
25120 {
25121 /* Leave linkage flags alone on instantiations with anonymous
25122 visibility. */
25123 if (TREE_PUBLIC (d))
25124 {
25125 DECL_NOT_REALLY_EXTERN (d) = 0;
25126 DECL_INTERFACE_KNOWN (d) = 0;
25127 }
25128 SET_DECL_IMPLICIT_INSTANTIATION (d);
25129 }
25130
25131 /* Defer all other templates, unless we have been explicitly
25132 forbidden from doing so. */
25133 if (/* If there is no definition, we cannot instantiate the
25134 template. */
25135 ! pattern_defined
25136 /* If it's OK to postpone instantiation, do so. */
25137 || defer_ok
25138 /* If this is a static data member that will be defined
25139 elsewhere, we don't want to instantiate the entire data
25140 member, but we do want to instantiate the initializer so that
25141 we can substitute that elsewhere. */
25142 || (external_p && VAR_P (d))
25143 /* Handle here a deleted function too, avoid generating
25144 its body (c++/61080). */
25145 || deleted_p)
25146 {
25147 /* The definition of the static data member is now required so
25148 we must substitute the initializer. */
25149 if (VAR_P (d)
25150 && !DECL_INITIAL (d)
25151 && DECL_INITIAL (code_pattern))
25152 {
25153 tree ns;
25154 tree init;
25155 bool const_init = false;
25156 bool enter_context = DECL_CLASS_SCOPE_P (d);
25157
25158 ns = decl_namespace_context (d);
25159 push_nested_namespace (ns);
25160 if (enter_context)
25161 push_nested_class (DECL_CONTEXT (d));
25162 init = tsubst_expr (DECL_INITIAL (code_pattern),
25163 args,
25164 tf_warning_or_error, NULL_TREE,
25165 /*integral_constant_expression_p=*/false);
25166 /* If instantiating the initializer involved instantiating this
25167 again, don't call cp_finish_decl twice. */
25168 if (!DECL_INITIAL (d))
25169 {
25170 /* Make sure the initializer is still constant, in case of
25171 circular dependency (template/instantiate6.C). */
25172 const_init
25173 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25174 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25175 /*asmspec_tree=*/NULL_TREE,
25176 LOOKUP_ONLYCONVERTING);
25177 }
25178 if (enter_context)
25179 pop_nested_class ();
25180 pop_nested_namespace (ns);
25181 }
25182
25183 /* We restore the source position here because it's used by
25184 add_pending_template. */
25185 input_location = saved_loc;
25186
25187 if (at_eof && !pattern_defined
25188 && DECL_EXPLICIT_INSTANTIATION (d)
25189 && DECL_NOT_REALLY_EXTERN (d))
25190 /* [temp.explicit]
25191
25192 The definition of a non-exported function template, a
25193 non-exported member function template, or a non-exported
25194 member function or static data member of a class template
25195 shall be present in every translation unit in which it is
25196 explicitly instantiated. */
25197 permerror (input_location, "explicit instantiation of %qD "
25198 "but no definition available", d);
25199
25200 /* If we're in unevaluated context, we just wanted to get the
25201 constant value; this isn't an odr use, so don't queue
25202 a full instantiation. */
25203 if (cp_unevaluated_operand != 0)
25204 goto out;
25205 /* ??? Historically, we have instantiated inline functions, even
25206 when marked as "extern template". */
25207 if (!(external_p && VAR_P (d)))
25208 add_pending_template (d);
25209 goto out;
25210 }
25211
25212 bool push_to_top, nested;
25213 tree fn_context;
25214 fn_context = decl_function_context (d);
25215 if (LAMBDA_FUNCTION_P (d))
25216 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25217 fn_context = NULL_TREE;
25218 nested = current_function_decl != NULL_TREE;
25219 push_to_top = !(nested && fn_context == current_function_decl);
25220
25221 vec<tree> omp_privatization_save;
25222 if (nested)
25223 save_omp_privatization_clauses (omp_privatization_save);
25224
25225 if (push_to_top)
25226 push_to_top_level ();
25227 else
25228 {
25229 gcc_assert (!processing_template_decl);
25230 push_function_context ();
25231 cp_unevaluated_operand = 0;
25232 c_inhibit_evaluation_warnings = 0;
25233 }
25234
25235 /* Mark D as instantiated so that recursive calls to
25236 instantiate_decl do not try to instantiate it again. */
25237 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25238
25239 /* Regenerate the declaration in case the template has been modified
25240 by a subsequent redeclaration. */
25241 regenerate_decl_from_template (d, td, args);
25242
25243 /* We already set the file and line above. Reset them now in case
25244 they changed as a result of calling regenerate_decl_from_template. */
25245 input_location = DECL_SOURCE_LOCATION (d);
25246
25247 if (VAR_P (d))
25248 {
25249 tree init;
25250 bool const_init = false;
25251
25252 /* Clear out DECL_RTL; whatever was there before may not be right
25253 since we've reset the type of the declaration. */
25254 SET_DECL_RTL (d, NULL);
25255 DECL_IN_AGGR_P (d) = 0;
25256
25257 /* The initializer is placed in DECL_INITIAL by
25258 regenerate_decl_from_template so we don't need to
25259 push/pop_access_scope again here. Pull it out so that
25260 cp_finish_decl can process it. */
25261 init = DECL_INITIAL (d);
25262 DECL_INITIAL (d) = NULL_TREE;
25263 DECL_INITIALIZED_P (d) = 0;
25264
25265 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25266 initializer. That function will defer actual emission until
25267 we have a chance to determine linkage. */
25268 DECL_EXTERNAL (d) = 0;
25269
25270 /* Enter the scope of D so that access-checking works correctly. */
25271 bool enter_context = DECL_CLASS_SCOPE_P (d);
25272 if (enter_context)
25273 push_nested_class (DECL_CONTEXT (d));
25274
25275 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25276 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25277 ? LOOKUP_CONSTINIT : 0);
25278 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25279
25280 if (enter_context)
25281 pop_nested_class ();
25282
25283 if (variable_template_p (gen_tmpl))
25284 note_variable_template_instantiation (d);
25285 }
25286 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25287 synthesize_method (d);
25288 else if (TREE_CODE (d) == FUNCTION_DECL)
25289 {
25290 /* Set up the list of local specializations. */
25291 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25292 tree block = NULL_TREE;
25293
25294 /* Set up context. */
25295 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25296 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25297 block = push_stmt_list ();
25298 else
25299 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25300
25301 /* Some typedefs referenced from within the template code need to be
25302 access checked at template instantiation time, i.e now. These
25303 types were added to the template at parsing time. Let's get those
25304 and perform the access checks then. */
25305 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25306 args);
25307
25308 /* Create substitution entries for the parameters. */
25309 register_parameter_specializations (code_pattern, d);
25310
25311 /* Substitute into the body of the function. */
25312 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25313 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25314 tf_warning_or_error, tmpl);
25315 else
25316 {
25317 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25318 tf_warning_or_error, tmpl,
25319 /*integral_constant_expression_p=*/false);
25320
25321 /* Set the current input_location to the end of the function
25322 so that finish_function knows where we are. */
25323 input_location
25324 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25325
25326 /* Remember if we saw an infinite loop in the template. */
25327 current_function_infinite_loop
25328 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25329 }
25330
25331 /* Finish the function. */
25332 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25333 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25334 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25335 else
25336 {
25337 d = finish_function (/*inline_p=*/false);
25338 expand_or_defer_fn (d);
25339 }
25340
25341 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25342 cp_check_omp_declare_reduction (d);
25343 }
25344
25345 /* We're not deferring instantiation any more. */
25346 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25347
25348 if (push_to_top)
25349 pop_from_top_level ();
25350 else
25351 pop_function_context ();
25352
25353 if (nested)
25354 restore_omp_privatization_clauses (omp_privatization_save);
25355
25356 out:
25357 pop_deferring_access_checks ();
25358 timevar_pop (TV_TEMPLATE_INST);
25359 pop_tinst_level ();
25360 input_location = saved_loc;
25361 cp_unevaluated_operand = saved_unevaluated_operand;
25362 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25363
25364 return d;
25365 }
25366
25367 /* Run through the list of templates that we wish we could
25368 instantiate, and instantiate any we can. RETRIES is the
25369 number of times we retry pending template instantiation. */
25370
25371 void
25372 instantiate_pending_templates (int retries)
25373 {
25374 int reconsider;
25375 location_t saved_loc = input_location;
25376
25377 /* Instantiating templates may trigger vtable generation. This in turn
25378 may require further template instantiations. We place a limit here
25379 to avoid infinite loop. */
25380 if (pending_templates && retries >= max_tinst_depth)
25381 {
25382 tree decl = pending_templates->tinst->maybe_get_node ();
25383
25384 fatal_error (input_location,
25385 "template instantiation depth exceeds maximum of %d"
25386 " instantiating %q+D, possibly from virtual table generation"
25387 " (use %<-ftemplate-depth=%> to increase the maximum)",
25388 max_tinst_depth, decl);
25389 if (TREE_CODE (decl) == FUNCTION_DECL)
25390 /* Pretend that we defined it. */
25391 DECL_INITIAL (decl) = error_mark_node;
25392 return;
25393 }
25394
25395 do
25396 {
25397 struct pending_template **t = &pending_templates;
25398 struct pending_template *last = NULL;
25399 reconsider = 0;
25400 while (*t)
25401 {
25402 tree instantiation = reopen_tinst_level ((*t)->tinst);
25403 bool complete = false;
25404
25405 if (TYPE_P (instantiation))
25406 {
25407 if (!COMPLETE_TYPE_P (instantiation))
25408 {
25409 instantiate_class_template (instantiation);
25410 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25411 for (tree fld = TYPE_FIELDS (instantiation);
25412 fld; fld = TREE_CHAIN (fld))
25413 if ((VAR_P (fld)
25414 || (TREE_CODE (fld) == FUNCTION_DECL
25415 && !DECL_ARTIFICIAL (fld)))
25416 && DECL_TEMPLATE_INSTANTIATION (fld))
25417 instantiate_decl (fld,
25418 /*defer_ok=*/false,
25419 /*expl_inst_class_mem_p=*/false);
25420
25421 if (COMPLETE_TYPE_P (instantiation))
25422 reconsider = 1;
25423 }
25424
25425 complete = COMPLETE_TYPE_P (instantiation);
25426 }
25427 else
25428 {
25429 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25430 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25431 {
25432 instantiation
25433 = instantiate_decl (instantiation,
25434 /*defer_ok=*/false,
25435 /*expl_inst_class_mem_p=*/false);
25436 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25437 reconsider = 1;
25438 }
25439
25440 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25441 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25442 }
25443
25444 if (complete)
25445 {
25446 /* If INSTANTIATION has been instantiated, then we don't
25447 need to consider it again in the future. */
25448 struct pending_template *drop = *t;
25449 *t = (*t)->next;
25450 set_refcount_ptr (drop->tinst);
25451 pending_template_freelist ().free (drop);
25452 }
25453 else
25454 {
25455 last = *t;
25456 t = &(*t)->next;
25457 }
25458 tinst_depth = 0;
25459 set_refcount_ptr (current_tinst_level);
25460 }
25461 last_pending_template = last;
25462 }
25463 while (reconsider);
25464
25465 input_location = saved_loc;
25466 }
25467
25468 /* Substitute ARGVEC into T, which is a list of initializers for
25469 either base class or a non-static data member. The TREE_PURPOSEs
25470 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25471 instantiate_decl. */
25472
25473 static tree
25474 tsubst_initializer_list (tree t, tree argvec)
25475 {
25476 tree inits = NULL_TREE;
25477 tree target_ctor = error_mark_node;
25478
25479 for (; t; t = TREE_CHAIN (t))
25480 {
25481 tree decl;
25482 tree init;
25483 tree expanded_bases = NULL_TREE;
25484 tree expanded_arguments = NULL_TREE;
25485 int i, len = 1;
25486
25487 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25488 {
25489 tree expr;
25490 tree arg;
25491
25492 /* Expand the base class expansion type into separate base
25493 classes. */
25494 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25495 tf_warning_or_error,
25496 NULL_TREE);
25497 if (expanded_bases == error_mark_node)
25498 continue;
25499
25500 /* We'll be building separate TREE_LISTs of arguments for
25501 each base. */
25502 len = TREE_VEC_LENGTH (expanded_bases);
25503 expanded_arguments = make_tree_vec (len);
25504 for (i = 0; i < len; i++)
25505 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25506
25507 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25508 expand each argument in the TREE_VALUE of t. */
25509 expr = make_node (EXPR_PACK_EXPANSION);
25510 PACK_EXPANSION_LOCAL_P (expr) = true;
25511 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25512 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25513
25514 if (TREE_VALUE (t) == void_type_node)
25515 /* VOID_TYPE_NODE is used to indicate
25516 value-initialization. */
25517 {
25518 for (i = 0; i < len; i++)
25519 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25520 }
25521 else
25522 {
25523 /* Substitute parameter packs into each argument in the
25524 TREE_LIST. */
25525 in_base_initializer = 1;
25526 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25527 {
25528 tree expanded_exprs;
25529
25530 /* Expand the argument. */
25531 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25532 expanded_exprs
25533 = tsubst_pack_expansion (expr, argvec,
25534 tf_warning_or_error,
25535 NULL_TREE);
25536 if (expanded_exprs == error_mark_node)
25537 continue;
25538
25539 /* Prepend each of the expanded expressions to the
25540 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25541 for (i = 0; i < len; i++)
25542 {
25543 TREE_VEC_ELT (expanded_arguments, i) =
25544 tree_cons (NULL_TREE,
25545 TREE_VEC_ELT (expanded_exprs, i),
25546 TREE_VEC_ELT (expanded_arguments, i));
25547 }
25548 }
25549 in_base_initializer = 0;
25550
25551 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25552 since we built them backwards. */
25553 for (i = 0; i < len; i++)
25554 {
25555 TREE_VEC_ELT (expanded_arguments, i) =
25556 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25557 }
25558 }
25559 }
25560
25561 for (i = 0; i < len; ++i)
25562 {
25563 if (expanded_bases)
25564 {
25565 decl = TREE_VEC_ELT (expanded_bases, i);
25566 decl = expand_member_init (decl);
25567 init = TREE_VEC_ELT (expanded_arguments, i);
25568 }
25569 else
25570 {
25571 tree tmp;
25572 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25573 tf_warning_or_error, NULL_TREE);
25574
25575 decl = expand_member_init (decl);
25576 if (decl && !DECL_P (decl))
25577 in_base_initializer = 1;
25578
25579 init = TREE_VALUE (t);
25580 tmp = init;
25581 if (init != void_type_node)
25582 init = tsubst_expr (init, argvec,
25583 tf_warning_or_error, NULL_TREE,
25584 /*integral_constant_expression_p=*/false);
25585 if (init == NULL_TREE && tmp != NULL_TREE)
25586 /* If we had an initializer but it instantiated to nothing,
25587 value-initialize the object. This will only occur when
25588 the initializer was a pack expansion where the parameter
25589 packs used in that expansion were of length zero. */
25590 init = void_type_node;
25591 in_base_initializer = 0;
25592 }
25593
25594 if (target_ctor != error_mark_node
25595 && init != error_mark_node)
25596 {
25597 error ("mem-initializer for %qD follows constructor delegation",
25598 decl);
25599 return inits;
25600 }
25601 /* Look for a target constructor. */
25602 if (init != error_mark_node
25603 && decl && CLASS_TYPE_P (decl)
25604 && same_type_p (decl, current_class_type))
25605 {
25606 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25607 if (inits)
25608 {
25609 error ("constructor delegation follows mem-initializer for %qD",
25610 TREE_PURPOSE (inits));
25611 continue;
25612 }
25613 target_ctor = init;
25614 }
25615
25616 if (decl)
25617 {
25618 init = build_tree_list (decl, init);
25619 TREE_CHAIN (init) = inits;
25620 inits = init;
25621 }
25622 }
25623 }
25624 return inits;
25625 }
25626
25627 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25628
25629 static void
25630 set_current_access_from_decl (tree decl)
25631 {
25632 if (TREE_PRIVATE (decl))
25633 current_access_specifier = access_private_node;
25634 else if (TREE_PROTECTED (decl))
25635 current_access_specifier = access_protected_node;
25636 else
25637 current_access_specifier = access_public_node;
25638 }
25639
25640 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25641 is the instantiation (which should have been created with
25642 start_enum) and ARGS are the template arguments to use. */
25643
25644 static void
25645 tsubst_enum (tree tag, tree newtag, tree args)
25646 {
25647 tree e;
25648
25649 if (SCOPED_ENUM_P (newtag))
25650 begin_scope (sk_scoped_enum, newtag);
25651
25652 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25653 {
25654 tree value;
25655 tree decl;
25656
25657 decl = TREE_VALUE (e);
25658 /* Note that in a template enum, the TREE_VALUE is the
25659 CONST_DECL, not the corresponding INTEGER_CST. */
25660 value = tsubst_expr (DECL_INITIAL (decl),
25661 args, tf_warning_or_error, NULL_TREE,
25662 /*integral_constant_expression_p=*/true);
25663
25664 /* Give this enumeration constant the correct access. */
25665 set_current_access_from_decl (decl);
25666
25667 /* Actually build the enumerator itself. Here we're assuming that
25668 enumerators can't have dependent attributes. */
25669 build_enumerator (DECL_NAME (decl), value, newtag,
25670 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25671 }
25672
25673 if (SCOPED_ENUM_P (newtag))
25674 finish_scope ();
25675
25676 finish_enum_value_list (newtag);
25677 finish_enum (newtag);
25678
25679 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25680 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25681 }
25682
25683 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25684 its type -- but without substituting the innermost set of template
25685 arguments. So, innermost set of template parameters will appear in
25686 the type. */
25687
25688 tree
25689 get_mostly_instantiated_function_type (tree decl)
25690 {
25691 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25692 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25693 }
25694
25695 /* Return truthvalue if we're processing a template different from
25696 the last one involved in diagnostics. */
25697 bool
25698 problematic_instantiation_changed (void)
25699 {
25700 return current_tinst_level != last_error_tinst_level;
25701 }
25702
25703 /* Remember current template involved in diagnostics. */
25704 void
25705 record_last_problematic_instantiation (void)
25706 {
25707 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25708 }
25709
25710 struct tinst_level *
25711 current_instantiation (void)
25712 {
25713 return current_tinst_level;
25714 }
25715
25716 /* Return TRUE if current_function_decl is being instantiated, false
25717 otherwise. */
25718
25719 bool
25720 instantiating_current_function_p (void)
25721 {
25722 return (current_instantiation ()
25723 && (current_instantiation ()->maybe_get_node ()
25724 == current_function_decl));
25725 }
25726
25727 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25728 type. Return false for ok, true for disallowed. Issue error and
25729 inform messages under control of COMPLAIN. */
25730
25731 static bool
25732 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25733 {
25734 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25735 return false;
25736 else if (TYPE_PTR_P (type))
25737 return false;
25738 else if (TYPE_REF_P (type)
25739 && !TYPE_REF_IS_RVALUE (type))
25740 return false;
25741 else if (TYPE_PTRMEM_P (type))
25742 return false;
25743 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25744 return false;
25745 else if (TREE_CODE (type) == TYPENAME_TYPE)
25746 return false;
25747 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25748 return false;
25749 else if (TREE_CODE (type) == NULLPTR_TYPE)
25750 return false;
25751 /* A bound template template parm could later be instantiated to have a valid
25752 nontype parm type via an alias template. */
25753 else if (cxx_dialect >= cxx11
25754 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25755 return false;
25756 else if (CLASS_TYPE_P (type))
25757 {
25758 if (cxx_dialect < cxx2a)
25759 {
25760 if (complain & tf_error)
25761 error ("non-type template parameters of class type only available "
25762 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25763 return true;
25764 }
25765 if (dependent_type_p (type))
25766 return false;
25767 if (!complete_type_or_else (type, NULL_TREE))
25768 return true;
25769 if (!structural_type_p (type))
25770 {
25771 auto_diagnostic_group d;
25772 if (complain & tf_error)
25773 error ("%qT is not a valid type for a template non-type parameter "
25774 "because it is not structural", type);
25775 structural_type_p (type, true);
25776 return true;
25777 }
25778 return false;
25779 }
25780
25781 if (complain & tf_error)
25782 {
25783 if (type == error_mark_node)
25784 inform (input_location, "invalid template non-type parameter");
25785 else
25786 error ("%q#T is not a valid type for a template non-type parameter",
25787 type);
25788 }
25789 return true;
25790 }
25791
25792 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25793 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25794
25795 static bool
25796 dependent_type_p_r (tree type)
25797 {
25798 tree scope;
25799
25800 /* [temp.dep.type]
25801
25802 A type is dependent if it is:
25803
25804 -- a template parameter. Template template parameters are types
25805 for us (since TYPE_P holds true for them) so we handle
25806 them here. */
25807 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25808 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25809 return true;
25810 /* -- a qualified-id with a nested-name-specifier which contains a
25811 class-name that names a dependent type or whose unqualified-id
25812 names a dependent type. */
25813 if (TREE_CODE (type) == TYPENAME_TYPE)
25814 return true;
25815
25816 /* An alias template specialization can be dependent even if the
25817 resulting type is not. */
25818 if (dependent_alias_template_spec_p (type))
25819 return true;
25820
25821 /* -- a cv-qualified type where the cv-unqualified type is
25822 dependent.
25823 No code is necessary for this bullet; the code below handles
25824 cv-qualified types, and we don't want to strip aliases with
25825 TYPE_MAIN_VARIANT because of DR 1558. */
25826 /* -- a compound type constructed from any dependent type. */
25827 if (TYPE_PTRMEM_P (type))
25828 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25829 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25830 (type)));
25831 else if (INDIRECT_TYPE_P (type))
25832 return dependent_type_p (TREE_TYPE (type));
25833 else if (FUNC_OR_METHOD_TYPE_P (type))
25834 {
25835 tree arg_type;
25836
25837 if (dependent_type_p (TREE_TYPE (type)))
25838 return true;
25839 for (arg_type = TYPE_ARG_TYPES (type);
25840 arg_type;
25841 arg_type = TREE_CHAIN (arg_type))
25842 if (dependent_type_p (TREE_VALUE (arg_type)))
25843 return true;
25844 if (cxx_dialect >= cxx17)
25845 /* A value-dependent noexcept-specifier makes the type dependent. */
25846 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25847 if (tree noex = TREE_PURPOSE (spec))
25848 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25849 affect overload resolution and treating it as dependent breaks
25850 things. Same for an unparsed noexcept expression. */
25851 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25852 && TREE_CODE (noex) != DEFERRED_PARSE
25853 && value_dependent_expression_p (noex))
25854 return true;
25855 return false;
25856 }
25857 /* -- an array type constructed from any dependent type or whose
25858 size is specified by a constant expression that is
25859 value-dependent.
25860
25861 We checked for type- and value-dependence of the bounds in
25862 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25863 if (TREE_CODE (type) == ARRAY_TYPE)
25864 {
25865 if (TYPE_DOMAIN (type)
25866 && dependent_type_p (TYPE_DOMAIN (type)))
25867 return true;
25868 return dependent_type_p (TREE_TYPE (type));
25869 }
25870
25871 /* -- a template-id in which either the template name is a template
25872 parameter ... */
25873 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25874 return true;
25875 /* ... or any of the template arguments is a dependent type or
25876 an expression that is type-dependent or value-dependent. */
25877 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25878 && (any_dependent_template_arguments_p
25879 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25880 return true;
25881
25882 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25883 dependent; if the argument of the `typeof' expression is not
25884 type-dependent, then it should already been have resolved. */
25885 if (TREE_CODE (type) == TYPEOF_TYPE
25886 || TREE_CODE (type) == DECLTYPE_TYPE
25887 || TREE_CODE (type) == UNDERLYING_TYPE)
25888 return true;
25889
25890 /* A template argument pack is dependent if any of its packed
25891 arguments are. */
25892 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25893 {
25894 tree args = ARGUMENT_PACK_ARGS (type);
25895 int i, len = TREE_VEC_LENGTH (args);
25896 for (i = 0; i < len; ++i)
25897 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25898 return true;
25899 }
25900
25901 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25902 be template parameters. */
25903 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25904 return true;
25905
25906 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25907 return true;
25908
25909 /* The standard does not specifically mention types that are local
25910 to template functions or local classes, but they should be
25911 considered dependent too. For example:
25912
25913 template <int I> void f() {
25914 enum E { a = I };
25915 S<sizeof (E)> s;
25916 }
25917
25918 The size of `E' cannot be known until the value of `I' has been
25919 determined. Therefore, `E' must be considered dependent. */
25920 scope = TYPE_CONTEXT (type);
25921 if (scope && TYPE_P (scope))
25922 return dependent_type_p (scope);
25923 /* Don't use type_dependent_expression_p here, as it can lead
25924 to infinite recursion trying to determine whether a lambda
25925 nested in a lambda is dependent (c++/47687). */
25926 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25927 && DECL_LANG_SPECIFIC (scope)
25928 && DECL_TEMPLATE_INFO (scope)
25929 && (any_dependent_template_arguments_p
25930 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25931 return true;
25932
25933 /* Other types are non-dependent. */
25934 return false;
25935 }
25936
25937 /* Returns TRUE if TYPE is dependent, in the sense of
25938 [temp.dep.type]. Note that a NULL type is considered dependent. */
25939
25940 bool
25941 dependent_type_p (tree type)
25942 {
25943 /* If there are no template parameters in scope, then there can't be
25944 any dependent types. */
25945 if (!processing_template_decl)
25946 {
25947 /* If we are not processing a template, then nobody should be
25948 providing us with a dependent type. */
25949 gcc_assert (type);
25950 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25951 return false;
25952 }
25953
25954 /* If the type is NULL, we have not computed a type for the entity
25955 in question; in that case, the type is dependent. */
25956 if (!type)
25957 return true;
25958
25959 /* Erroneous types can be considered non-dependent. */
25960 if (type == error_mark_node)
25961 return false;
25962
25963 /* Getting here with global_type_node means we improperly called this
25964 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25965 gcc_checking_assert (type != global_type_node);
25966
25967 /* If we have not already computed the appropriate value for TYPE,
25968 do so now. */
25969 if (!TYPE_DEPENDENT_P_VALID (type))
25970 {
25971 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25972 TYPE_DEPENDENT_P_VALID (type) = 1;
25973 }
25974
25975 return TYPE_DEPENDENT_P (type);
25976 }
25977
25978 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25979 lookup. In other words, a dependent type that is not the current
25980 instantiation. */
25981
25982 bool
25983 dependent_scope_p (tree scope)
25984 {
25985 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25986 && !currently_open_class (scope));
25987 }
25988
25989 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25990 an unknown base of 'this' (and is therefore instantiation-dependent). */
25991
25992 static bool
25993 unknown_base_ref_p (tree t)
25994 {
25995 if (!current_class_ptr)
25996 return false;
25997
25998 tree mem = TREE_OPERAND (t, 1);
25999 if (shared_member_p (mem))
26000 return false;
26001
26002 tree cur = current_nonlambda_class_type ();
26003 if (!any_dependent_bases_p (cur))
26004 return false;
26005
26006 tree ctx = TREE_OPERAND (t, 0);
26007 if (DERIVED_FROM_P (ctx, cur))
26008 return false;
26009
26010 return true;
26011 }
26012
26013 /* T is a SCOPE_REF; return whether we need to consider it
26014 instantiation-dependent so that we can check access at instantiation
26015 time even though we know which member it resolves to. */
26016
26017 static bool
26018 instantiation_dependent_scope_ref_p (tree t)
26019 {
26020 if (DECL_P (TREE_OPERAND (t, 1))
26021 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26022 && !unknown_base_ref_p (t)
26023 && accessible_in_template_p (TREE_OPERAND (t, 0),
26024 TREE_OPERAND (t, 1)))
26025 return false;
26026 else
26027 return true;
26028 }
26029
26030 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26031 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26032 expression. */
26033
26034 /* Note that this predicate is not appropriate for general expressions;
26035 only constant expressions (that satisfy potential_constant_expression)
26036 can be tested for value dependence. */
26037
26038 bool
26039 value_dependent_expression_p (tree expression)
26040 {
26041 if (!processing_template_decl || expression == NULL_TREE)
26042 return false;
26043
26044 /* A type-dependent expression is also value-dependent. */
26045 if (type_dependent_expression_p (expression))
26046 return true;
26047
26048 switch (TREE_CODE (expression))
26049 {
26050 case BASELINK:
26051 /* A dependent member function of the current instantiation. */
26052 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26053
26054 case FUNCTION_DECL:
26055 /* A dependent member function of the current instantiation. */
26056 if (DECL_CLASS_SCOPE_P (expression)
26057 && dependent_type_p (DECL_CONTEXT (expression)))
26058 return true;
26059 break;
26060
26061 case IDENTIFIER_NODE:
26062 /* A name that has not been looked up -- must be dependent. */
26063 return true;
26064
26065 case TEMPLATE_PARM_INDEX:
26066 /* A non-type template parm. */
26067 return true;
26068
26069 case CONST_DECL:
26070 /* A non-type template parm. */
26071 if (DECL_TEMPLATE_PARM_P (expression))
26072 return true;
26073 return value_dependent_expression_p (DECL_INITIAL (expression));
26074
26075 case VAR_DECL:
26076 /* A constant with literal type and is initialized
26077 with an expression that is value-dependent. */
26078 if (DECL_DEPENDENT_INIT_P (expression)
26079 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26080 || TYPE_REF_P (TREE_TYPE (expression)))
26081 return true;
26082 if (DECL_HAS_VALUE_EXPR_P (expression))
26083 {
26084 tree value_expr = DECL_VALUE_EXPR (expression);
26085 if (value_dependent_expression_p (value_expr)
26086 /* __PRETTY_FUNCTION__ inside a template function is dependent
26087 on the name of the function. */
26088 || (DECL_PRETTY_FUNCTION_P (expression)
26089 /* It might be used in a template, but not a template
26090 function, in which case its DECL_VALUE_EXPR will be
26091 "top level". */
26092 && value_expr == error_mark_node))
26093 return true;
26094 }
26095 return false;
26096
26097 case DYNAMIC_CAST_EXPR:
26098 case STATIC_CAST_EXPR:
26099 case CONST_CAST_EXPR:
26100 case REINTERPRET_CAST_EXPR:
26101 case CAST_EXPR:
26102 case IMPLICIT_CONV_EXPR:
26103 /* These expressions are value-dependent if the type to which
26104 the cast occurs is dependent or the expression being casted
26105 is value-dependent. */
26106 {
26107 tree type = TREE_TYPE (expression);
26108
26109 if (dependent_type_p (type))
26110 return true;
26111
26112 /* A functional cast has a list of operands. */
26113 expression = TREE_OPERAND (expression, 0);
26114 if (!expression)
26115 {
26116 /* If there are no operands, it must be an expression such
26117 as "int()". This should not happen for aggregate types
26118 because it would form non-constant expressions. */
26119 gcc_assert (cxx_dialect >= cxx11
26120 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26121
26122 return false;
26123 }
26124
26125 if (TREE_CODE (expression) == TREE_LIST)
26126 return any_value_dependent_elements_p (expression);
26127
26128 return value_dependent_expression_p (expression);
26129 }
26130
26131 case SIZEOF_EXPR:
26132 if (SIZEOF_EXPR_TYPE_P (expression))
26133 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26134 /* FALLTHRU */
26135 case ALIGNOF_EXPR:
26136 case TYPEID_EXPR:
26137 /* A `sizeof' expression is value-dependent if the operand is
26138 type-dependent or is a pack expansion. */
26139 expression = TREE_OPERAND (expression, 0);
26140 if (PACK_EXPANSION_P (expression))
26141 return true;
26142 else if (TYPE_P (expression))
26143 return dependent_type_p (expression);
26144 return instantiation_dependent_uneval_expression_p (expression);
26145
26146 case AT_ENCODE_EXPR:
26147 /* An 'encode' expression is value-dependent if the operand is
26148 type-dependent. */
26149 expression = TREE_OPERAND (expression, 0);
26150 return dependent_type_p (expression);
26151
26152 case NOEXCEPT_EXPR:
26153 expression = TREE_OPERAND (expression, 0);
26154 return instantiation_dependent_uneval_expression_p (expression);
26155
26156 case SCOPE_REF:
26157 /* All instantiation-dependent expressions should also be considered
26158 value-dependent. */
26159 return instantiation_dependent_scope_ref_p (expression);
26160
26161 case COMPONENT_REF:
26162 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26163 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26164
26165 case NONTYPE_ARGUMENT_PACK:
26166 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26167 is value-dependent. */
26168 {
26169 tree values = ARGUMENT_PACK_ARGS (expression);
26170 int i, len = TREE_VEC_LENGTH (values);
26171
26172 for (i = 0; i < len; ++i)
26173 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26174 return true;
26175
26176 return false;
26177 }
26178
26179 case TRAIT_EXPR:
26180 {
26181 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26182
26183 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26184 return true;
26185
26186 if (!type2)
26187 return false;
26188
26189 if (TREE_CODE (type2) != TREE_LIST)
26190 return dependent_type_p (type2);
26191
26192 for (; type2; type2 = TREE_CHAIN (type2))
26193 if (dependent_type_p (TREE_VALUE (type2)))
26194 return true;
26195
26196 return false;
26197 }
26198
26199 case MODOP_EXPR:
26200 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26201 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26202
26203 case ARRAY_REF:
26204 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26205 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26206
26207 case ADDR_EXPR:
26208 {
26209 tree op = TREE_OPERAND (expression, 0);
26210 return (value_dependent_expression_p (op)
26211 || has_value_dependent_address (op));
26212 }
26213
26214 case REQUIRES_EXPR:
26215 /* Treat all requires-expressions as value-dependent so
26216 we don't try to fold them. */
26217 return true;
26218
26219 case TYPE_REQ:
26220 return dependent_type_p (TREE_OPERAND (expression, 0));
26221
26222 case CALL_EXPR:
26223 {
26224 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26225 return true;
26226 tree fn = get_callee_fndecl (expression);
26227 int i, nargs;
26228 nargs = call_expr_nargs (expression);
26229 for (i = 0; i < nargs; ++i)
26230 {
26231 tree op = CALL_EXPR_ARG (expression, i);
26232 /* In a call to a constexpr member function, look through the
26233 implicit ADDR_EXPR on the object argument so that it doesn't
26234 cause the call to be considered value-dependent. We also
26235 look through it in potential_constant_expression. */
26236 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26237 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26238 && TREE_CODE (op) == ADDR_EXPR)
26239 op = TREE_OPERAND (op, 0);
26240 if (value_dependent_expression_p (op))
26241 return true;
26242 }
26243 return false;
26244 }
26245
26246 case TEMPLATE_ID_EXPR:
26247 return concept_definition_p (TREE_OPERAND (expression, 0));
26248
26249 case CONSTRUCTOR:
26250 {
26251 unsigned ix;
26252 tree val;
26253 if (dependent_type_p (TREE_TYPE (expression)))
26254 return true;
26255 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26256 if (value_dependent_expression_p (val))
26257 return true;
26258 return false;
26259 }
26260
26261 case STMT_EXPR:
26262 /* Treat a GNU statement expression as dependent to avoid crashing
26263 under instantiate_non_dependent_expr; it can't be constant. */
26264 return true;
26265
26266 default:
26267 /* A constant expression is value-dependent if any subexpression is
26268 value-dependent. */
26269 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26270 {
26271 case tcc_reference:
26272 case tcc_unary:
26273 case tcc_comparison:
26274 case tcc_binary:
26275 case tcc_expression:
26276 case tcc_vl_exp:
26277 {
26278 int i, len = cp_tree_operand_length (expression);
26279
26280 for (i = 0; i < len; i++)
26281 {
26282 tree t = TREE_OPERAND (expression, i);
26283
26284 /* In some cases, some of the operands may be missing.
26285 (For example, in the case of PREDECREMENT_EXPR, the
26286 amount to increment by may be missing.) That doesn't
26287 make the expression dependent. */
26288 if (t && value_dependent_expression_p (t))
26289 return true;
26290 }
26291 }
26292 break;
26293 default:
26294 break;
26295 }
26296 break;
26297 }
26298
26299 /* The expression is not value-dependent. */
26300 return false;
26301 }
26302
26303 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26304 [temp.dep.expr]. Note that an expression with no type is
26305 considered dependent. Other parts of the compiler arrange for an
26306 expression with type-dependent subexpressions to have no type, so
26307 this function doesn't have to be fully recursive. */
26308
26309 bool
26310 type_dependent_expression_p (tree expression)
26311 {
26312 if (!processing_template_decl)
26313 return false;
26314
26315 if (expression == NULL_TREE || expression == error_mark_node)
26316 return false;
26317
26318 STRIP_ANY_LOCATION_WRAPPER (expression);
26319
26320 /* An unresolved name is always dependent. */
26321 if (identifier_p (expression)
26322 || TREE_CODE (expression) == USING_DECL
26323 || TREE_CODE (expression) == WILDCARD_DECL)
26324 return true;
26325
26326 /* A lambda-expression in template context is dependent. dependent_type_p is
26327 true for a lambda in the scope of a class or function template, but that
26328 doesn't cover all template contexts, like a default template argument. */
26329 if (TREE_CODE (expression) == LAMBDA_EXPR)
26330 return true;
26331
26332 /* A fold expression is type-dependent. */
26333 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26334 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26335 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26336 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26337 return true;
26338
26339 /* Some expression forms are never type-dependent. */
26340 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26341 || TREE_CODE (expression) == SIZEOF_EXPR
26342 || TREE_CODE (expression) == ALIGNOF_EXPR
26343 || TREE_CODE (expression) == AT_ENCODE_EXPR
26344 || TREE_CODE (expression) == NOEXCEPT_EXPR
26345 || TREE_CODE (expression) == TRAIT_EXPR
26346 || TREE_CODE (expression) == TYPEID_EXPR
26347 || TREE_CODE (expression) == DELETE_EXPR
26348 || TREE_CODE (expression) == VEC_DELETE_EXPR
26349 || TREE_CODE (expression) == THROW_EXPR
26350 || TREE_CODE (expression) == REQUIRES_EXPR)
26351 return false;
26352
26353 /* The types of these expressions depends only on the type to which
26354 the cast occurs. */
26355 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26356 || TREE_CODE (expression) == STATIC_CAST_EXPR
26357 || TREE_CODE (expression) == CONST_CAST_EXPR
26358 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26359 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26360 || TREE_CODE (expression) == CAST_EXPR)
26361 return dependent_type_p (TREE_TYPE (expression));
26362
26363 /* The types of these expressions depends only on the type created
26364 by the expression. */
26365 if (TREE_CODE (expression) == NEW_EXPR
26366 || TREE_CODE (expression) == VEC_NEW_EXPR)
26367 {
26368 /* For NEW_EXPR tree nodes created inside a template, either
26369 the object type itself or a TREE_LIST may appear as the
26370 operand 1. */
26371 tree type = TREE_OPERAND (expression, 1);
26372 if (TREE_CODE (type) == TREE_LIST)
26373 /* This is an array type. We need to check array dimensions
26374 as well. */
26375 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26376 || value_dependent_expression_p
26377 (TREE_OPERAND (TREE_VALUE (type), 1));
26378 else
26379 return dependent_type_p (type);
26380 }
26381
26382 if (TREE_CODE (expression) == SCOPE_REF)
26383 {
26384 tree scope = TREE_OPERAND (expression, 0);
26385 tree name = TREE_OPERAND (expression, 1);
26386
26387 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26388 contains an identifier associated by name lookup with one or more
26389 declarations declared with a dependent type, or...a
26390 nested-name-specifier or qualified-id that names a member of an
26391 unknown specialization. */
26392 return (type_dependent_expression_p (name)
26393 || dependent_scope_p (scope));
26394 }
26395
26396 if (TREE_CODE (expression) == TEMPLATE_DECL
26397 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26398 return uses_outer_template_parms (expression);
26399
26400 if (TREE_CODE (expression) == STMT_EXPR)
26401 expression = stmt_expr_value_expr (expression);
26402
26403 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26404 {
26405 tree elt;
26406 unsigned i;
26407
26408 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26409 {
26410 if (type_dependent_expression_p (elt))
26411 return true;
26412 }
26413 return false;
26414 }
26415
26416 /* A static data member of the current instantiation with incomplete
26417 array type is type-dependent, as the definition and specializations
26418 can have different bounds. */
26419 if (VAR_P (expression)
26420 && DECL_CLASS_SCOPE_P (expression)
26421 && dependent_type_p (DECL_CONTEXT (expression))
26422 && VAR_HAD_UNKNOWN_BOUND (expression))
26423 return true;
26424
26425 /* An array of unknown bound depending on a variadic parameter, eg:
26426
26427 template<typename... Args>
26428 void foo (Args... args)
26429 {
26430 int arr[] = { args... };
26431 }
26432
26433 template<int... vals>
26434 void bar ()
26435 {
26436 int arr[] = { vals... };
26437 }
26438
26439 If the array has no length and has an initializer, it must be that
26440 we couldn't determine its length in cp_complete_array_type because
26441 it is dependent. */
26442 if (VAR_P (expression)
26443 && TREE_TYPE (expression) != NULL_TREE
26444 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26445 && !TYPE_DOMAIN (TREE_TYPE (expression))
26446 && DECL_INITIAL (expression))
26447 return true;
26448
26449 /* A function or variable template-id is type-dependent if it has any
26450 dependent template arguments. */
26451 if (VAR_OR_FUNCTION_DECL_P (expression)
26452 && DECL_LANG_SPECIFIC (expression)
26453 && DECL_TEMPLATE_INFO (expression))
26454 {
26455 /* Consider the innermost template arguments, since those are the ones
26456 that come from the template-id; the template arguments for the
26457 enclosing class do not make it type-dependent unless they are used in
26458 the type of the decl. */
26459 if (instantiates_primary_template_p (expression)
26460 && (any_dependent_template_arguments_p
26461 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26462 return true;
26463 }
26464
26465 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26466 type-dependent. Checking this is important for functions with auto return
26467 type, which looks like a dependent type. */
26468 if (TREE_CODE (expression) == FUNCTION_DECL
26469 && !(DECL_CLASS_SCOPE_P (expression)
26470 && dependent_type_p (DECL_CONTEXT (expression)))
26471 && !(DECL_LANG_SPECIFIC (expression)
26472 && DECL_FRIEND_P (expression)
26473 && (!DECL_FRIEND_CONTEXT (expression)
26474 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26475 && !DECL_LOCAL_FUNCTION_P (expression))
26476 {
26477 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26478 || undeduced_auto_decl (expression));
26479 return false;
26480 }
26481
26482 /* Always dependent, on the number of arguments if nothing else. */
26483 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26484 return true;
26485
26486 if (TREE_TYPE (expression) == unknown_type_node)
26487 {
26488 if (TREE_CODE (expression) == ADDR_EXPR)
26489 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26490 if (TREE_CODE (expression) == COMPONENT_REF
26491 || TREE_CODE (expression) == OFFSET_REF)
26492 {
26493 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26494 return true;
26495 expression = TREE_OPERAND (expression, 1);
26496 if (identifier_p (expression))
26497 return false;
26498 }
26499 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26500 if (TREE_CODE (expression) == SCOPE_REF)
26501 return false;
26502
26503 if (BASELINK_P (expression))
26504 {
26505 if (BASELINK_OPTYPE (expression)
26506 && dependent_type_p (BASELINK_OPTYPE (expression)))
26507 return true;
26508 expression = BASELINK_FUNCTIONS (expression);
26509 }
26510
26511 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26512 {
26513 if (any_dependent_template_arguments_p
26514 (TREE_OPERAND (expression, 1)))
26515 return true;
26516 expression = TREE_OPERAND (expression, 0);
26517 if (identifier_p (expression))
26518 return true;
26519 }
26520
26521 gcc_assert (OVL_P (expression));
26522
26523 for (lkp_iterator iter (expression); iter; ++iter)
26524 if (type_dependent_expression_p (*iter))
26525 return true;
26526
26527 return false;
26528 }
26529
26530 /* The type of a non-type template parm declared with a placeholder type
26531 depends on the corresponding template argument, even though
26532 placeholders are not normally considered dependent. */
26533 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26534 && is_auto (TREE_TYPE (expression)))
26535 return true;
26536
26537 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26538
26539 /* Dependent type attributes might not have made it from the decl to
26540 the type yet. */
26541 if (DECL_P (expression)
26542 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26543 return true;
26544
26545 return (dependent_type_p (TREE_TYPE (expression)));
26546 }
26547
26548 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26549 type-dependent if the expression refers to a member of the current
26550 instantiation and the type of the referenced member is dependent, or the
26551 class member access expression refers to a member of an unknown
26552 specialization.
26553
26554 This function returns true if the OBJECT in such a class member access
26555 expression is of an unknown specialization. */
26556
26557 bool
26558 type_dependent_object_expression_p (tree object)
26559 {
26560 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26561 dependent. */
26562 if (TREE_CODE (object) == IDENTIFIER_NODE)
26563 return true;
26564 tree scope = TREE_TYPE (object);
26565 return (!scope || dependent_scope_p (scope));
26566 }
26567
26568 /* walk_tree callback function for instantiation_dependent_expression_p,
26569 below. Returns non-zero if a dependent subexpression is found. */
26570
26571 static tree
26572 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26573 void * /*data*/)
26574 {
26575 if (TYPE_P (*tp))
26576 {
26577 /* We don't have to worry about decltype currently because decltype
26578 of an instantiation-dependent expr is a dependent type. This
26579 might change depending on the resolution of DR 1172. */
26580 *walk_subtrees = false;
26581 return NULL_TREE;
26582 }
26583 enum tree_code code = TREE_CODE (*tp);
26584 switch (code)
26585 {
26586 /* Don't treat an argument list as dependent just because it has no
26587 TREE_TYPE. */
26588 case TREE_LIST:
26589 case TREE_VEC:
26590 case NONTYPE_ARGUMENT_PACK:
26591 return NULL_TREE;
26592
26593 case TEMPLATE_PARM_INDEX:
26594 if (dependent_type_p (TREE_TYPE (*tp)))
26595 return *tp;
26596 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26597 return *tp;
26598 /* We'll check value-dependence separately. */
26599 return NULL_TREE;
26600
26601 /* Handle expressions with type operands. */
26602 case SIZEOF_EXPR:
26603 case ALIGNOF_EXPR:
26604 case TYPEID_EXPR:
26605 case AT_ENCODE_EXPR:
26606 {
26607 tree op = TREE_OPERAND (*tp, 0);
26608 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26609 op = TREE_TYPE (op);
26610 if (TYPE_P (op))
26611 {
26612 if (dependent_type_p (op))
26613 return *tp;
26614 else
26615 {
26616 *walk_subtrees = false;
26617 return NULL_TREE;
26618 }
26619 }
26620 break;
26621 }
26622
26623 case COMPONENT_REF:
26624 if (identifier_p (TREE_OPERAND (*tp, 1)))
26625 /* In a template, finish_class_member_access_expr creates a
26626 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26627 type-dependent, so that we can check access control at
26628 instantiation time (PR 42277). See also Core issue 1273. */
26629 return *tp;
26630 break;
26631
26632 case SCOPE_REF:
26633 if (instantiation_dependent_scope_ref_p (*tp))
26634 return *tp;
26635 else
26636 break;
26637
26638 /* Treat statement-expressions as dependent. */
26639 case BIND_EXPR:
26640 return *tp;
26641
26642 /* Treat requires-expressions as dependent. */
26643 case REQUIRES_EXPR:
26644 return *tp;
26645
26646 case CALL_EXPR:
26647 /* Treat concept checks as dependent. */
26648 if (concept_check_p (*tp))
26649 return *tp;
26650 break;
26651
26652 case TEMPLATE_ID_EXPR:
26653 /* Treat concept checks as dependent. */
26654 if (concept_check_p (*tp))
26655 return *tp;
26656 break;
26657
26658 case CONSTRUCTOR:
26659 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26660 return *tp;
26661 break;
26662
26663 default:
26664 break;
26665 }
26666
26667 if (type_dependent_expression_p (*tp))
26668 return *tp;
26669 else
26670 return NULL_TREE;
26671 }
26672
26673 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26674 sense defined by the ABI:
26675
26676 "An expression is instantiation-dependent if it is type-dependent
26677 or value-dependent, or it has a subexpression that is type-dependent
26678 or value-dependent."
26679
26680 Except don't actually check value-dependence for unevaluated expressions,
26681 because in sizeof(i) we don't care about the value of i. Checking
26682 type-dependence will in turn check value-dependence of array bounds/template
26683 arguments as needed. */
26684
26685 bool
26686 instantiation_dependent_uneval_expression_p (tree expression)
26687 {
26688 tree result;
26689
26690 if (!processing_template_decl)
26691 return false;
26692
26693 if (expression == error_mark_node)
26694 return false;
26695
26696 result = cp_walk_tree_without_duplicates (&expression,
26697 instantiation_dependent_r, NULL);
26698 return result != NULL_TREE;
26699 }
26700
26701 /* As above, but also check value-dependence of the expression as a whole. */
26702
26703 bool
26704 instantiation_dependent_expression_p (tree expression)
26705 {
26706 return (instantiation_dependent_uneval_expression_p (expression)
26707 || value_dependent_expression_p (expression));
26708 }
26709
26710 /* Like type_dependent_expression_p, but it also works while not processing
26711 a template definition, i.e. during substitution or mangling. */
26712
26713 bool
26714 type_dependent_expression_p_push (tree expr)
26715 {
26716 bool b;
26717 ++processing_template_decl;
26718 b = type_dependent_expression_p (expr);
26719 --processing_template_decl;
26720 return b;
26721 }
26722
26723 /* Returns TRUE if ARGS contains a type-dependent expression. */
26724
26725 bool
26726 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26727 {
26728 unsigned int i;
26729 tree arg;
26730
26731 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26732 {
26733 if (type_dependent_expression_p (arg))
26734 return true;
26735 }
26736 return false;
26737 }
26738
26739 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26740 expressions) contains any type-dependent expressions. */
26741
26742 bool
26743 any_type_dependent_elements_p (const_tree list)
26744 {
26745 for (; list; list = TREE_CHAIN (list))
26746 if (type_dependent_expression_p (TREE_VALUE (list)))
26747 return true;
26748
26749 return false;
26750 }
26751
26752 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26753 expressions) contains any value-dependent expressions. */
26754
26755 bool
26756 any_value_dependent_elements_p (const_tree list)
26757 {
26758 for (; list; list = TREE_CHAIN (list))
26759 if (value_dependent_expression_p (TREE_VALUE (list)))
26760 return true;
26761
26762 return false;
26763 }
26764
26765 /* Returns TRUE if the ARG (a template argument) is dependent. */
26766
26767 bool
26768 dependent_template_arg_p (tree arg)
26769 {
26770 if (!processing_template_decl)
26771 return false;
26772
26773 /* Assume a template argument that was wrongly written by the user
26774 is dependent. This is consistent with what
26775 any_dependent_template_arguments_p [that calls this function]
26776 does. */
26777 if (!arg || arg == error_mark_node)
26778 return true;
26779
26780 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26781 arg = argument_pack_select_arg (arg);
26782
26783 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26784 return true;
26785 if (TREE_CODE (arg) == TEMPLATE_DECL)
26786 {
26787 if (DECL_TEMPLATE_PARM_P (arg))
26788 return true;
26789 /* A member template of a dependent class is not necessarily
26790 type-dependent, but it is a dependent template argument because it
26791 will be a member of an unknown specialization to that template. */
26792 tree scope = CP_DECL_CONTEXT (arg);
26793 return TYPE_P (scope) && dependent_type_p (scope);
26794 }
26795 else if (ARGUMENT_PACK_P (arg))
26796 {
26797 tree args = ARGUMENT_PACK_ARGS (arg);
26798 int i, len = TREE_VEC_LENGTH (args);
26799 for (i = 0; i < len; ++i)
26800 {
26801 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26802 return true;
26803 }
26804
26805 return false;
26806 }
26807 else if (TYPE_P (arg))
26808 return dependent_type_p (arg);
26809 else
26810 return (type_dependent_expression_p (arg)
26811 || value_dependent_expression_p (arg));
26812 }
26813
26814 /* Returns true if ARGS (a collection of template arguments) contains
26815 any types that require structural equality testing. */
26816
26817 bool
26818 any_template_arguments_need_structural_equality_p (tree args)
26819 {
26820 int i;
26821 int j;
26822
26823 if (!args)
26824 return false;
26825 if (args == error_mark_node)
26826 return true;
26827
26828 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26829 {
26830 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26831 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26832 {
26833 tree arg = TREE_VEC_ELT (level, j);
26834 tree packed_args = NULL_TREE;
26835 int k, len = 1;
26836
26837 if (ARGUMENT_PACK_P (arg))
26838 {
26839 /* Look inside the argument pack. */
26840 packed_args = ARGUMENT_PACK_ARGS (arg);
26841 len = TREE_VEC_LENGTH (packed_args);
26842 }
26843
26844 for (k = 0; k < len; ++k)
26845 {
26846 if (packed_args)
26847 arg = TREE_VEC_ELT (packed_args, k);
26848
26849 if (error_operand_p (arg))
26850 return true;
26851 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26852 continue;
26853 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26854 return true;
26855 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26856 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26857 return true;
26858 }
26859 }
26860 }
26861
26862 return false;
26863 }
26864
26865 /* Returns true if ARGS (a collection of template arguments) contains
26866 any dependent arguments. */
26867
26868 bool
26869 any_dependent_template_arguments_p (const_tree args)
26870 {
26871 int i;
26872 int j;
26873
26874 if (!args)
26875 return false;
26876 if (args == error_mark_node)
26877 return true;
26878
26879 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26880 {
26881 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26882 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26883 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26884 return true;
26885 }
26886
26887 return false;
26888 }
26889
26890 /* Returns true if ARGS contains any errors. */
26891
26892 bool
26893 any_erroneous_template_args_p (const_tree args)
26894 {
26895 int i;
26896 int j;
26897
26898 if (args == error_mark_node)
26899 return true;
26900
26901 if (args && TREE_CODE (args) != TREE_VEC)
26902 {
26903 if (tree ti = get_template_info (args))
26904 args = TI_ARGS (ti);
26905 else
26906 args = NULL_TREE;
26907 }
26908
26909 if (!args)
26910 return false;
26911
26912 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26913 {
26914 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26915 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26916 if (error_operand_p (TREE_VEC_ELT (level, j)))
26917 return true;
26918 }
26919
26920 return false;
26921 }
26922
26923 /* Returns TRUE if the template TMPL is type-dependent. */
26924
26925 bool
26926 dependent_template_p (tree tmpl)
26927 {
26928 if (TREE_CODE (tmpl) == OVERLOAD)
26929 {
26930 for (lkp_iterator iter (tmpl); iter; ++iter)
26931 if (dependent_template_p (*iter))
26932 return true;
26933 return false;
26934 }
26935
26936 /* Template template parameters are dependent. */
26937 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26938 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26939 return true;
26940 /* So are names that have not been looked up. */
26941 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26942 return true;
26943 return false;
26944 }
26945
26946 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26947
26948 bool
26949 dependent_template_id_p (tree tmpl, tree args)
26950 {
26951 return (dependent_template_p (tmpl)
26952 || any_dependent_template_arguments_p (args));
26953 }
26954
26955 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26956 are dependent. */
26957
26958 bool
26959 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26960 {
26961 int i;
26962
26963 if (!processing_template_decl)
26964 return false;
26965
26966 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26967 {
26968 tree decl = TREE_VEC_ELT (declv, i);
26969 tree init = TREE_VEC_ELT (initv, i);
26970 tree cond = TREE_VEC_ELT (condv, i);
26971 tree incr = TREE_VEC_ELT (incrv, i);
26972
26973 if (type_dependent_expression_p (decl)
26974 || TREE_CODE (decl) == SCOPE_REF)
26975 return true;
26976
26977 if (init && type_dependent_expression_p (init))
26978 return true;
26979
26980 if (cond == global_namespace)
26981 return true;
26982
26983 if (type_dependent_expression_p (cond))
26984 return true;
26985
26986 if (COMPARISON_CLASS_P (cond)
26987 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26988 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26989 return true;
26990
26991 if (TREE_CODE (incr) == MODOP_EXPR)
26992 {
26993 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26994 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26995 return true;
26996 }
26997 else if (type_dependent_expression_p (incr))
26998 return true;
26999 else if (TREE_CODE (incr) == MODIFY_EXPR)
27000 {
27001 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27002 return true;
27003 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27004 {
27005 tree t = TREE_OPERAND (incr, 1);
27006 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27007 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27008 return true;
27009
27010 /* If this loop has a class iterator with != comparison
27011 with increment other than i++/++i/i--/--i, make sure the
27012 increment is constant. */
27013 if (CLASS_TYPE_P (TREE_TYPE (decl))
27014 && TREE_CODE (cond) == NE_EXPR)
27015 {
27016 if (TREE_OPERAND (t, 0) == decl)
27017 t = TREE_OPERAND (t, 1);
27018 else
27019 t = TREE_OPERAND (t, 0);
27020 if (TREE_CODE (t) != INTEGER_CST)
27021 return true;
27022 }
27023 }
27024 }
27025 }
27026
27027 return false;
27028 }
27029
27030 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27031 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27032 no such TYPE can be found. Note that this function peers inside
27033 uninstantiated templates and therefore should be used only in
27034 extremely limited situations. ONLY_CURRENT_P restricts this
27035 peering to the currently open classes hierarchy (which is required
27036 when comparing types). */
27037
27038 tree
27039 resolve_typename_type (tree type, bool only_current_p)
27040 {
27041 tree scope;
27042 tree name;
27043 tree decl;
27044 int quals;
27045 tree pushed_scope;
27046 tree result;
27047
27048 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27049
27050 scope = TYPE_CONTEXT (type);
27051 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27052 gcc_checking_assert (uses_template_parms (scope));
27053
27054 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27055 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27056 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27057 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27058 identifier of the TYPENAME_TYPE anymore.
27059 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27060 TYPENAME_TYPE instead, we avoid messing up with a possible
27061 typedef variant case. */
27062 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27063
27064 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27065 it first before we can figure out what NAME refers to. */
27066 if (TREE_CODE (scope) == TYPENAME_TYPE)
27067 {
27068 if (TYPENAME_IS_RESOLVING_P (scope))
27069 /* Given a class template A with a dependent base with nested type C,
27070 typedef typename A::C::C C will land us here, as trying to resolve
27071 the initial A::C leads to the local C typedef, which leads back to
27072 A::C::C. So we break the recursion now. */
27073 return type;
27074 else
27075 scope = resolve_typename_type (scope, only_current_p);
27076 }
27077 /* If we don't know what SCOPE refers to, then we cannot resolve the
27078 TYPENAME_TYPE. */
27079 if (!CLASS_TYPE_P (scope))
27080 return type;
27081 /* If this is a typedef, we don't want to look inside (c++/11987). */
27082 if (typedef_variant_p (type))
27083 return type;
27084 /* If SCOPE isn't the template itself, it will not have a valid
27085 TYPE_FIELDS list. */
27086 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27087 /* scope is either the template itself or a compatible instantiation
27088 like X<T>, so look up the name in the original template. */
27089 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27090 /* If scope has no fields, it can't be a current instantiation. Check this
27091 before currently_open_class to avoid infinite recursion (71515). */
27092 if (!TYPE_FIELDS (scope))
27093 return type;
27094 /* If the SCOPE is not the current instantiation, there's no reason
27095 to look inside it. */
27096 if (only_current_p && !currently_open_class (scope))
27097 return type;
27098 /* Enter the SCOPE so that name lookup will be resolved as if we
27099 were in the class definition. In particular, SCOPE will no
27100 longer be considered a dependent type. */
27101 pushed_scope = push_scope (scope);
27102 /* Look up the declaration. */
27103 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27104 tf_warning_or_error);
27105
27106 result = NULL_TREE;
27107
27108 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27109 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27110 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27111 if (!decl)
27112 /*nop*/;
27113 else if (identifier_p (fullname)
27114 && TREE_CODE (decl) == TYPE_DECL)
27115 {
27116 result = TREE_TYPE (decl);
27117 if (result == error_mark_node)
27118 result = NULL_TREE;
27119 }
27120 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27121 && DECL_CLASS_TEMPLATE_P (decl))
27122 {
27123 /* Obtain the template and the arguments. */
27124 tree tmpl = TREE_OPERAND (fullname, 0);
27125 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27126 {
27127 /* We get here with a plain identifier because a previous tentative
27128 parse of the nested-name-specifier as part of a ptr-operator saw
27129 ::template X<A>. The use of ::template is necessary in a
27130 ptr-operator, but wrong in a declarator-id.
27131
27132 [temp.names]: In a qualified-id of a declarator-id, the keyword
27133 template shall not appear at the top level. */
27134 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27135 "keyword %<template%> not allowed in declarator-id");
27136 tmpl = decl;
27137 }
27138 tree args = TREE_OPERAND (fullname, 1);
27139 /* Instantiate the template. */
27140 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27141 /*entering_scope=*/true,
27142 tf_error | tf_user);
27143 if (result == error_mark_node)
27144 result = NULL_TREE;
27145 }
27146
27147 /* Leave the SCOPE. */
27148 if (pushed_scope)
27149 pop_scope (pushed_scope);
27150
27151 /* If we failed to resolve it, return the original typename. */
27152 if (!result)
27153 return type;
27154
27155 /* If lookup found a typename type, resolve that too. */
27156 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27157 {
27158 /* Ill-formed programs can cause infinite recursion here, so we
27159 must catch that. */
27160 TYPENAME_IS_RESOLVING_P (result) = 1;
27161 result = resolve_typename_type (result, only_current_p);
27162 TYPENAME_IS_RESOLVING_P (result) = 0;
27163 }
27164
27165 /* Qualify the resulting type. */
27166 quals = cp_type_quals (type);
27167 if (quals)
27168 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27169
27170 return result;
27171 }
27172
27173 /* EXPR is an expression which is not type-dependent. Return a proxy
27174 for EXPR that can be used to compute the types of larger
27175 expressions containing EXPR. */
27176
27177 tree
27178 build_non_dependent_expr (tree expr)
27179 {
27180 tree orig_expr = expr;
27181 tree inner_expr;
27182
27183 /* When checking, try to get a constant value for all non-dependent
27184 expressions in order to expose bugs in *_dependent_expression_p
27185 and constexpr. This can affect code generation, see PR70704, so
27186 only do this for -fchecking=2. */
27187 if (flag_checking > 1
27188 && cxx_dialect >= cxx11
27189 /* Don't do this during nsdmi parsing as it can lead to
27190 unexpected recursive instantiations. */
27191 && !parsing_nsdmi ()
27192 /* Don't do this during concept processing either and for
27193 the same reason. */
27194 && !processing_constraint_expression_p ())
27195 fold_non_dependent_expr (expr, tf_none);
27196
27197 STRIP_ANY_LOCATION_WRAPPER (expr);
27198
27199 /* Preserve OVERLOADs; the functions must be available to resolve
27200 types. */
27201 inner_expr = expr;
27202 if (TREE_CODE (inner_expr) == STMT_EXPR)
27203 inner_expr = stmt_expr_value_expr (inner_expr);
27204 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27205 inner_expr = TREE_OPERAND (inner_expr, 0);
27206 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27207 inner_expr = TREE_OPERAND (inner_expr, 1);
27208 if (is_overloaded_fn (inner_expr)
27209 || TREE_CODE (inner_expr) == OFFSET_REF)
27210 return orig_expr;
27211 /* There is no need to return a proxy for a variable or enumerator. */
27212 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27213 return orig_expr;
27214 /* Preserve string constants; conversions from string constants to
27215 "char *" are allowed, even though normally a "const char *"
27216 cannot be used to initialize a "char *". */
27217 if (TREE_CODE (expr) == STRING_CST)
27218 return orig_expr;
27219 /* Preserve void and arithmetic constants, as an optimization -- there is no
27220 reason to create a new node. */
27221 if (TREE_CODE (expr) == VOID_CST
27222 || TREE_CODE (expr) == INTEGER_CST
27223 || TREE_CODE (expr) == REAL_CST)
27224 return orig_expr;
27225 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27226 There is at least one place where we want to know that a
27227 particular expression is a throw-expression: when checking a ?:
27228 expression, there are special rules if the second or third
27229 argument is a throw-expression. */
27230 if (TREE_CODE (expr) == THROW_EXPR)
27231 return orig_expr;
27232
27233 /* Don't wrap an initializer list, we need to be able to look inside. */
27234 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27235 return orig_expr;
27236
27237 /* Don't wrap a dummy object, we need to be able to test for it. */
27238 if (is_dummy_object (expr))
27239 return orig_expr;
27240
27241 if (TREE_CODE (expr) == COND_EXPR)
27242 return build3 (COND_EXPR,
27243 TREE_TYPE (expr),
27244 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27245 (TREE_OPERAND (expr, 1)
27246 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27247 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27248 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27249 if (TREE_CODE (expr) == COMPOUND_EXPR
27250 && !COMPOUND_EXPR_OVERLOADED (expr))
27251 return build2 (COMPOUND_EXPR,
27252 TREE_TYPE (expr),
27253 TREE_OPERAND (expr, 0),
27254 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27255
27256 /* If the type is unknown, it can't really be non-dependent */
27257 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27258
27259 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27260 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27261 TREE_TYPE (expr), expr);
27262 }
27263
27264 /* ARGS is a vector of expressions as arguments to a function call.
27265 Replace the arguments with equivalent non-dependent expressions.
27266 This modifies ARGS in place. */
27267
27268 void
27269 make_args_non_dependent (vec<tree, va_gc> *args)
27270 {
27271 unsigned int ix;
27272 tree arg;
27273
27274 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27275 {
27276 tree newarg = build_non_dependent_expr (arg);
27277 if (newarg != arg)
27278 (*args)[ix] = newarg;
27279 }
27280 }
27281
27282 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27283 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27284 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27285
27286 static tree
27287 make_auto_1 (tree name, bool set_canonical)
27288 {
27289 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27290 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27291 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27292 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27293 (0, processing_template_decl + 1, processing_template_decl + 1,
27294 TYPE_NAME (au), NULL_TREE);
27295 if (set_canonical)
27296 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27297 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27298 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27299
27300 return au;
27301 }
27302
27303 tree
27304 make_decltype_auto (void)
27305 {
27306 return make_auto_1 (decltype_auto_identifier, true);
27307 }
27308
27309 tree
27310 make_auto (void)
27311 {
27312 return make_auto_1 (auto_identifier, true);
27313 }
27314
27315 /* Return a C++17 deduction placeholder for class template TMPL. */
27316
27317 tree
27318 make_template_placeholder (tree tmpl)
27319 {
27320 tree t = make_auto_1 (auto_identifier, false);
27321 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27322 /* Our canonical type depends on the placeholder. */
27323 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27324 return t;
27325 }
27326
27327 /* True iff T is a C++17 class template deduction placeholder. */
27328
27329 bool
27330 template_placeholder_p (tree t)
27331 {
27332 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27333 }
27334
27335 /* Make a "constrained auto" type-specifier. This is an auto or
27336 decltype(auto) type with constraints that must be associated after
27337 deduction. The constraint is formed from the given concept CON
27338 and its optional sequence of template arguments ARGS.
27339
27340 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27341
27342 static tree
27343 make_constrained_placeholder_type (tree type, tree con, tree args)
27344 {
27345 /* Build the constraint. */
27346 tree tmpl = DECL_TI_TEMPLATE (con);
27347 tree expr = tmpl;
27348 if (TREE_CODE (con) == FUNCTION_DECL)
27349 expr = ovl_make (tmpl);
27350 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27351
27352 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27353
27354 /* Our canonical type depends on the constraint. */
27355 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27356
27357 /* Attach the constraint to the type declaration. */
27358 return TYPE_NAME (type);
27359 }
27360
27361 /* Make a "constrained auto" type-specifier. */
27362
27363 tree
27364 make_constrained_auto (tree con, tree args)
27365 {
27366 tree type = make_auto_1 (auto_identifier, false);
27367 return make_constrained_placeholder_type (type, con, args);
27368 }
27369
27370 /* Make a "constrained decltype(auto)" type-specifier. */
27371
27372 tree
27373 make_constrained_decltype_auto (tree con, tree args)
27374 {
27375 tree type = make_auto_1 (decltype_auto_identifier, false);
27376 /* FIXME: I don't know why this isn't done in make_auto_1. */
27377 AUTO_IS_DECLTYPE (type) = true;
27378 return make_constrained_placeholder_type (type, con, args);
27379 }
27380
27381 /* Build and return a concept definition. Like other templates, the
27382 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27383 the TEMPLATE_DECL. */
27384
27385 tree
27386 finish_concept_definition (cp_expr id, tree init)
27387 {
27388 gcc_assert (identifier_p (id));
27389 gcc_assert (processing_template_decl);
27390
27391 location_t loc = id.get_location();
27392
27393 /* A concept-definition shall not have associated constraints. */
27394 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27395 {
27396 error_at (loc, "a concept cannot be constrained");
27397 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27398 }
27399
27400 /* A concept-definition shall appear in namespace scope. Templates
27401 aren't allowed in block scope, so we only need to check for class
27402 scope. */
27403 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27404 {
27405 error_at (loc, "concept %qE not in namespace scope", *id);
27406 return error_mark_node;
27407 }
27408
27409 /* Initially build the concept declaration; it's type is bool. */
27410 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27411 DECL_CONTEXT (decl) = current_scope ();
27412 DECL_INITIAL (decl) = init;
27413
27414 /* Push the enclosing template. */
27415 return push_template_decl (decl);
27416 }
27417
27418 /* Given type ARG, return std::initializer_list<ARG>. */
27419
27420 static tree
27421 listify (tree arg)
27422 {
27423 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27424
27425 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27426 {
27427 gcc_rich_location richloc (input_location);
27428 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27429 error_at (&richloc,
27430 "deducing from brace-enclosed initializer list"
27431 " requires %<#include <initializer_list>%>");
27432
27433 return error_mark_node;
27434 }
27435 tree argvec = make_tree_vec (1);
27436 TREE_VEC_ELT (argvec, 0) = arg;
27437
27438 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27439 NULL_TREE, 0, tf_warning_or_error);
27440 }
27441
27442 /* Replace auto in TYPE with std::initializer_list<auto>. */
27443
27444 static tree
27445 listify_autos (tree type, tree auto_node)
27446 {
27447 tree init_auto = listify (strip_top_quals (auto_node));
27448 tree argvec = make_tree_vec (1);
27449 TREE_VEC_ELT (argvec, 0) = init_auto;
27450 if (processing_template_decl)
27451 argvec = add_to_template_args (current_template_args (), argvec);
27452 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27453 }
27454
27455 /* Hash traits for hashing possibly constrained 'auto'
27456 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27457
27458 struct auto_hash : default_hash_traits<tree>
27459 {
27460 static inline hashval_t hash (tree);
27461 static inline bool equal (tree, tree);
27462 };
27463
27464 /* Hash the 'auto' T. */
27465
27466 inline hashval_t
27467 auto_hash::hash (tree t)
27468 {
27469 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27470 /* Matching constrained-type-specifiers denote the same template
27471 parameter, so hash the constraint. */
27472 return hash_placeholder_constraint (c);
27473 else
27474 /* But unconstrained autos are all separate, so just hash the pointer. */
27475 return iterative_hash_object (t, 0);
27476 }
27477
27478 /* Compare two 'auto's. */
27479
27480 inline bool
27481 auto_hash::equal (tree t1, tree t2)
27482 {
27483 if (t1 == t2)
27484 return true;
27485
27486 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27487 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27488
27489 /* Two unconstrained autos are distinct. */
27490 if (!c1 || !c2)
27491 return false;
27492
27493 return equivalent_placeholder_constraints (c1, c2);
27494 }
27495
27496 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27497 constrained) auto, add it to the vector. */
27498
27499 static int
27500 extract_autos_r (tree t, void *data)
27501 {
27502 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27503 if (is_auto (t))
27504 {
27505 /* All the autos were built with index 0; fix that up now. */
27506 tree *p = hash.find_slot (t, INSERT);
27507 unsigned idx;
27508 if (*p)
27509 /* If this is a repeated constrained-type-specifier, use the index we
27510 chose before. */
27511 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27512 else
27513 {
27514 /* Otherwise this is new, so use the current count. */
27515 *p = t;
27516 idx = hash.elements () - 1;
27517 }
27518 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27519 }
27520
27521 /* Always keep walking. */
27522 return 0;
27523 }
27524
27525 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27526 says they can appear anywhere in the type. */
27527
27528 static tree
27529 extract_autos (tree type)
27530 {
27531 hash_set<tree> visited;
27532 hash_table<auto_hash> hash (2);
27533
27534 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27535
27536 tree tree_vec = make_tree_vec (hash.elements());
27537 for (hash_table<auto_hash>::iterator iter = hash.begin();
27538 iter != hash.end(); ++iter)
27539 {
27540 tree elt = *iter;
27541 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27542 TREE_VEC_ELT (tree_vec, i)
27543 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27544 }
27545
27546 return tree_vec;
27547 }
27548
27549 /* The stem for deduction guide names. */
27550 const char *const dguide_base = "__dguide_";
27551
27552 /* Return the name for a deduction guide for class template TMPL. */
27553
27554 tree
27555 dguide_name (tree tmpl)
27556 {
27557 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27558 tree tname = TYPE_IDENTIFIER (type);
27559 char *buf = (char *) alloca (1 + strlen (dguide_base)
27560 + IDENTIFIER_LENGTH (tname));
27561 memcpy (buf, dguide_base, strlen (dguide_base));
27562 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27563 IDENTIFIER_LENGTH (tname) + 1);
27564 tree dname = get_identifier (buf);
27565 TREE_TYPE (dname) = type;
27566 return dname;
27567 }
27568
27569 /* True if NAME is the name of a deduction guide. */
27570
27571 bool
27572 dguide_name_p (tree name)
27573 {
27574 return (TREE_CODE (name) == IDENTIFIER_NODE
27575 && TREE_TYPE (name)
27576 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27577 strlen (dguide_base)));
27578 }
27579
27580 /* True if FN is a deduction guide. */
27581
27582 bool
27583 deduction_guide_p (const_tree fn)
27584 {
27585 if (DECL_P (fn))
27586 if (tree name = DECL_NAME (fn))
27587 return dguide_name_p (name);
27588 return false;
27589 }
27590
27591 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27592
27593 bool
27594 copy_guide_p (const_tree fn)
27595 {
27596 gcc_assert (deduction_guide_p (fn));
27597 if (!DECL_ARTIFICIAL (fn))
27598 return false;
27599 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27600 return (TREE_CHAIN (parms) == void_list_node
27601 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27602 }
27603
27604 /* True if FN is a guide generated from a constructor template. */
27605
27606 bool
27607 template_guide_p (const_tree fn)
27608 {
27609 gcc_assert (deduction_guide_p (fn));
27610 if (!DECL_ARTIFICIAL (fn))
27611 return false;
27612 tree tmpl = DECL_TI_TEMPLATE (fn);
27613 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27614 return PRIMARY_TEMPLATE_P (org);
27615 return false;
27616 }
27617
27618 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27619 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27620 template parameter types. Note that the handling of template template
27621 parameters relies on current_template_parms being set appropriately for the
27622 new template. */
27623
27624 static tree
27625 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27626 tree tsubst_args, tsubst_flags_t complain)
27627 {
27628 if (olddecl == error_mark_node)
27629 return error_mark_node;
27630
27631 tree oldidx = get_template_parm_index (olddecl);
27632
27633 tree newtype;
27634 if (TREE_CODE (olddecl) == TYPE_DECL
27635 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27636 {
27637 tree oldtype = TREE_TYPE (olddecl);
27638 newtype = cxx_make_type (TREE_CODE (oldtype));
27639 TYPE_MAIN_VARIANT (newtype) = newtype;
27640 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27641 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27642 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27643 }
27644 else
27645 {
27646 newtype = TREE_TYPE (olddecl);
27647 if (type_uses_auto (newtype))
27648 {
27649 // Substitute once to fix references to other template parameters.
27650 newtype = tsubst (newtype, tsubst_args,
27651 complain|tf_partial, NULL_TREE);
27652 // Now substitute again to reduce the level of the auto.
27653 newtype = tsubst (newtype, current_template_args (),
27654 complain, NULL_TREE);
27655 }
27656 else
27657 newtype = tsubst (newtype, tsubst_args,
27658 complain, NULL_TREE);
27659 }
27660
27661 tree newdecl
27662 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27663 DECL_NAME (olddecl), newtype);
27664 SET_DECL_TEMPLATE_PARM_P (newdecl);
27665
27666 tree newidx;
27667 if (TREE_CODE (olddecl) == TYPE_DECL
27668 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27669 {
27670 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27671 = build_template_parm_index (index, level, level,
27672 newdecl, newtype);
27673 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27674 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27675 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27676 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27677
27678 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27679 {
27680 DECL_TEMPLATE_RESULT (newdecl)
27681 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27682 DECL_NAME (olddecl), newtype);
27683 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27684 // First create a copy (ttargs) of tsubst_args with an
27685 // additional level for the template template parameter's own
27686 // template parameters (ttparms).
27687 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27688 (DECL_TEMPLATE_PARMS (olddecl)));
27689 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27690 tree ttargs = make_tree_vec (depth + 1);
27691 for (int i = 0; i < depth; ++i)
27692 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27693 TREE_VEC_ELT (ttargs, depth)
27694 = template_parms_level_to_args (ttparms);
27695 // Substitute ttargs into ttparms to fix references to
27696 // other template parameters.
27697 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27698 complain|tf_partial);
27699 // Now substitute again with args based on tparms, to reduce
27700 // the level of the ttparms.
27701 ttargs = current_template_args ();
27702 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27703 complain);
27704 // Finally, tack the adjusted parms onto tparms.
27705 ttparms = tree_cons (size_int (depth), ttparms,
27706 current_template_parms);
27707 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27708 }
27709 }
27710 else
27711 {
27712 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27713 tree newconst
27714 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27715 TREE_CODE (oldconst),
27716 DECL_NAME (oldconst), newtype);
27717 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27718 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27719 SET_DECL_TEMPLATE_PARM_P (newconst);
27720 newidx = build_template_parm_index (index, level, level,
27721 newconst, newtype);
27722 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27723 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27724 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27725 }
27726
27727 return newdecl;
27728 }
27729
27730 /* Returns a C++17 class deduction guide template based on the constructor
27731 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27732 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27733 aggregate initialization guide. */
27734
27735 static tree
27736 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27737 {
27738 tree tparms, targs, fparms, fargs, ci;
27739 bool memtmpl = false;
27740 bool explicit_p;
27741 location_t loc;
27742 tree fn_tmpl = NULL_TREE;
27743
27744 if (outer_args)
27745 {
27746 ++processing_template_decl;
27747 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
27748 --processing_template_decl;
27749 }
27750
27751 if (!DECL_DECLARES_FUNCTION_P (ctor))
27752 {
27753 if (TYPE_P (ctor))
27754 {
27755 bool copy_p = TYPE_REF_P (ctor);
27756 if (copy_p)
27757 fparms = tree_cons (NULL_TREE, type, void_list_node);
27758 else
27759 fparms = void_list_node;
27760 }
27761 else if (TREE_CODE (ctor) == TREE_LIST)
27762 fparms = ctor;
27763 else
27764 gcc_unreachable ();
27765
27766 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27767 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27768 targs = CLASSTYPE_TI_ARGS (type);
27769 ci = NULL_TREE;
27770 fargs = NULL_TREE;
27771 loc = DECL_SOURCE_LOCATION (ctmpl);
27772 explicit_p = false;
27773 }
27774 else
27775 {
27776 ++processing_template_decl;
27777 bool ok = true;
27778
27779 fn_tmpl
27780 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27781 : DECL_TI_TEMPLATE (ctor));
27782 if (outer_args)
27783 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27784 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27785
27786 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27787 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27788 fully specialized args for the enclosing class. Strip those off, as
27789 the deduction guide won't have those template parameters. */
27790 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27791 TMPL_PARMS_DEPTH (tparms));
27792 /* Discard the 'this' parameter. */
27793 fparms = FUNCTION_ARG_CHAIN (ctor);
27794 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27795 ci = get_constraints (ctor);
27796 loc = DECL_SOURCE_LOCATION (ctor);
27797 explicit_p = DECL_NONCONVERTING_P (ctor);
27798
27799 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27800 {
27801 memtmpl = true;
27802
27803 /* For a member template constructor, we need to flatten the two
27804 template parameter lists into one, and then adjust the function
27805 signature accordingly. This gets...complicated. */
27806 tree save_parms = current_template_parms;
27807
27808 /* For a member template we should have two levels of parms/args, one
27809 for the class and one for the constructor. We stripped
27810 specialized args for further enclosing classes above. */
27811 const int depth = 2;
27812 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27813
27814 /* Template args for translating references to the two-level template
27815 parameters into references to the one-level template parameters we
27816 are creating. */
27817 tree tsubst_args = copy_node (targs);
27818 TMPL_ARGS_LEVEL (tsubst_args, depth)
27819 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27820
27821 /* Template parms for the constructor template. */
27822 tree ftparms = TREE_VALUE (tparms);
27823 unsigned flen = TREE_VEC_LENGTH (ftparms);
27824 /* Template parms for the class template. */
27825 tparms = TREE_CHAIN (tparms);
27826 tree ctparms = TREE_VALUE (tparms);
27827 unsigned clen = TREE_VEC_LENGTH (ctparms);
27828 /* Template parms for the deduction guide start as a copy of the
27829 template parms for the class. We set current_template_parms for
27830 lookup_template_class_1. */
27831 current_template_parms = tparms = copy_node (tparms);
27832 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27833 for (unsigned i = 0; i < clen; ++i)
27834 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27835
27836 /* Now we need to rewrite the constructor parms to append them to the
27837 class parms. */
27838 for (unsigned i = 0; i < flen; ++i)
27839 {
27840 unsigned index = i + clen;
27841 unsigned level = 1;
27842 tree oldelt = TREE_VEC_ELT (ftparms, i);
27843 tree olddecl = TREE_VALUE (oldelt);
27844 tree newdecl = rewrite_template_parm (olddecl, index, level,
27845 tsubst_args, complain);
27846 if (newdecl == error_mark_node)
27847 ok = false;
27848 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27849 tsubst_args, complain, ctor);
27850 tree list = build_tree_list (newdef, newdecl);
27851 TEMPLATE_PARM_CONSTRAINTS (list)
27852 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27853 tsubst_args, complain, ctor);
27854 TREE_VEC_ELT (new_vec, index) = list;
27855 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27856 }
27857
27858 /* Now we have a final set of template parms to substitute into the
27859 function signature. */
27860 targs = template_parms_to_args (tparms);
27861 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27862 complain, ctor);
27863 if (fparms == error_mark_node)
27864 ok = false;
27865 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27866 if (ci)
27867 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27868
27869 current_template_parms = save_parms;
27870 }
27871
27872 --processing_template_decl;
27873 if (!ok)
27874 return error_mark_node;
27875 }
27876
27877 if (!memtmpl)
27878 {
27879 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27880 tparms = copy_node (tparms);
27881 INNERMOST_TEMPLATE_PARMS (tparms)
27882 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27883 }
27884
27885 tree fntype = build_function_type (type, fparms);
27886 tree ded_fn = build_lang_decl_loc (loc,
27887 FUNCTION_DECL,
27888 dguide_name (type), fntype);
27889 DECL_ARGUMENTS (ded_fn) = fargs;
27890 DECL_ARTIFICIAL (ded_fn) = true;
27891 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27892 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27893 DECL_ARTIFICIAL (ded_tmpl) = true;
27894 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27895 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27896 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27897 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27898 if (DECL_P (ctor))
27899 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27900 if (ci)
27901 set_constraints (ded_tmpl, ci);
27902
27903 return ded_tmpl;
27904 }
27905
27906 /* Add to LIST the member types for the reshaped initializer CTOR. */
27907
27908 static tree
27909 collect_ctor_idx_types (tree ctor, tree list)
27910 {
27911 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
27912 tree idx, val; unsigned i;
27913 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
27914 {
27915 if (BRACE_ENCLOSED_INITIALIZER_P (val)
27916 && CONSTRUCTOR_NELTS (val))
27917 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
27918 if (TREE_CODE (subidx) == FIELD_DECL)
27919 {
27920 list = collect_ctor_idx_types (val, list);
27921 continue;
27922 }
27923 tree ftype = finish_decltype_type (idx, true, tf_none);
27924 list = tree_cons (NULL_TREE, ftype, list);
27925 }
27926
27927 return list;
27928 }
27929
27930 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
27931 INIT. */
27932
27933 static tree
27934 maybe_aggr_guide (tree type, tree init)
27935 {
27936 if (cxx_dialect < cxx2a)
27937 return NULL_TREE;
27938
27939 if (init == NULL_TREE)
27940 return NULL_TREE;
27941 if (!CP_AGGREGATE_TYPE_P (type))
27942 return NULL_TREE;
27943
27944 /* If we encounter a problem, we just won't add the candidate. */
27945 tsubst_flags_t complain = tf_none;
27946
27947 tree parms = NULL_TREE;
27948 if (TREE_CODE (init) == CONSTRUCTOR)
27949 {
27950 init = reshape_init (type, init, complain);
27951 if (init == error_mark_node)
27952 return NULL_TREE;
27953 parms = collect_ctor_idx_types (init, parms);
27954 }
27955 else if (TREE_CODE (init) == TREE_LIST)
27956 {
27957 int len = list_length (init);
27958 for (tree field = TYPE_FIELDS (type);
27959 len;
27960 --len, field = DECL_CHAIN (field))
27961 {
27962 field = next_initializable_field (field);
27963 if (!field)
27964 return NULL_TREE;
27965 tree ftype = finish_decltype_type (field, true, complain);
27966 parms = tree_cons (NULL_TREE, ftype, parms);
27967 }
27968 }
27969 else
27970 /* Aggregate initialization doesn't apply to an initializer expression. */
27971 return NULL_TREE;
27972
27973 if (parms)
27974 {
27975 tree last = parms;
27976 parms = nreverse (parms);
27977 TREE_CHAIN (last) = void_list_node;
27978 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
27979 return guide;
27980 }
27981
27982 return NULL_TREE;
27983 }
27984
27985 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
27986
27987 static bool
27988 is_spec_or_derived (tree etype, tree tmpl)
27989 {
27990 if (!etype || !CLASS_TYPE_P (etype))
27991 return false;
27992
27993 tree type = TREE_TYPE (tmpl);
27994 tree tparms = (INNERMOST_TEMPLATE_PARMS
27995 (DECL_TEMPLATE_PARMS (tmpl)));
27996 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27997 int err = unify (tparms, targs, type, etype,
27998 UNIFY_ALLOW_DERIVED, /*explain*/false);
27999 ggc_free (targs);
28000 return !err;
28001 }
28002
28003 /* Deduce template arguments for the class template placeholder PTYPE for
28004 template TMPL based on the initializer INIT, and return the resulting
28005 type. */
28006
28007 static tree
28008 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
28009 tsubst_flags_t complain)
28010 {
28011 if (!DECL_CLASS_TEMPLATE_P (tmpl))
28012 {
28013 /* We should have handled this in the caller. */
28014 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28015 return ptype;
28016 if (complain & tf_error)
28017 error ("non-class template %qT used without template arguments", tmpl);
28018 return error_mark_node;
28019 }
28020 if (init && TREE_TYPE (init) == ptype)
28021 /* Using the template parm as its own argument. */
28022 return ptype;
28023
28024 tree type = TREE_TYPE (tmpl);
28025
28026 bool try_list_ctor = false;
28027 bool copy_init = false;
28028
28029 releasing_vec rv_args = NULL;
28030 vec<tree,va_gc> *&args = *&rv_args;
28031 if (init == NULL_TREE)
28032 args = make_tree_vector ();
28033 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28034 {
28035 if (CONSTRUCTOR_NELTS (init) == 1)
28036 {
28037 /* As an exception, the first phase in 16.3.1.7 (considering the
28038 initializer list as a single argument) is omitted if the
28039 initializer list consists of a single expression of type cv U,
28040 where U is a specialization of C or a class derived from a
28041 specialization of C. */
28042 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28043 copy_init = is_spec_or_derived (TREE_TYPE (elt), tmpl);
28044 }
28045 try_list_ctor = !copy_init && TYPE_HAS_LIST_CTOR (type);
28046 if (try_list_ctor || is_std_init_list (type))
28047 args = make_tree_vector_single (init);
28048 else
28049 args = make_tree_vector_from_ctor (init);
28050 }
28051 else
28052 {
28053 if (TREE_CODE (init) == TREE_LIST)
28054 args = make_tree_vector_from_list (init);
28055 else
28056 args = make_tree_vector_single (init);
28057
28058 if (args->length() == 1)
28059 copy_init = is_spec_or_derived (TREE_TYPE ((*args)[0]), tmpl);
28060 }
28061
28062 tree dname = dguide_name (tmpl);
28063 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
28064 /*type*/false, /*complain*/false,
28065 /*hidden*/false);
28066 bool elided = false;
28067 if (cands == error_mark_node)
28068 cands = NULL_TREE;
28069
28070 /* Prune explicit deduction guides in copy-initialization context. */
28071 if (flags & LOOKUP_ONLYCONVERTING)
28072 {
28073 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28074 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28075 elided = true;
28076
28077 if (elided)
28078 {
28079 /* Found a nonconverting guide, prune the candidates. */
28080 tree pruned = NULL_TREE;
28081 for (lkp_iterator iter (cands); iter; ++iter)
28082 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28083 pruned = lookup_add (*iter, pruned);
28084
28085 cands = pruned;
28086 }
28087 }
28088
28089 tree outer_args = NULL_TREE;
28090 if (DECL_CLASS_SCOPE_P (tmpl)
28091 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28092 {
28093 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28094 type = TREE_TYPE (most_general_template (tmpl));
28095 }
28096
28097 bool saw_ctor = false;
28098 // FIXME cache artificial deduction guides
28099 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28100 {
28101 /* Skip inherited constructors. */
28102 if (iter.using_p ())
28103 continue;
28104
28105 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28106 if (guide == error_mark_node)
28107 return error_mark_node;
28108 if ((flags & LOOKUP_ONLYCONVERTING)
28109 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
28110 elided = true;
28111 else
28112 cands = lookup_add (guide, cands);
28113
28114 saw_ctor = true;
28115 }
28116
28117 if (!copy_init)
28118 if (tree guide = maybe_aggr_guide (type, init))
28119 cands = lookup_add (guide, cands);
28120
28121 tree call = error_mark_node;
28122
28123 /* If this is list-initialization and the class has a list constructor, first
28124 try deducing from the list as a single argument, as [over.match.list]. */
28125 tree list_cands = NULL_TREE;
28126 if (try_list_ctor && cands)
28127 for (lkp_iterator iter (cands); iter; ++iter)
28128 {
28129 tree dg = *iter;
28130 if (is_list_ctor (dg))
28131 list_cands = lookup_add (dg, list_cands);
28132 }
28133 if (list_cands)
28134 {
28135 ++cp_unevaluated_operand;
28136 call = build_new_function_call (list_cands, &args, tf_decltype);
28137 --cp_unevaluated_operand;
28138
28139 if (call == error_mark_node)
28140 {
28141 /* That didn't work, now try treating the list as a sequence of
28142 arguments. */
28143 release_tree_vector (args);
28144 args = make_tree_vector_from_ctor (init);
28145 }
28146 }
28147
28148 /* Maybe generate an implicit deduction guide. */
28149 if (call == error_mark_node && args->length () < 2)
28150 {
28151 tree gtype = NULL_TREE;
28152
28153 if (args->length () == 1)
28154 /* Generate a copy guide. */
28155 gtype = build_reference_type (type);
28156 else if (!saw_ctor)
28157 /* Generate a default guide. */
28158 gtype = type;
28159
28160 if (gtype)
28161 {
28162 tree guide = build_deduction_guide (type, gtype, outer_args,
28163 complain);
28164 if (guide == error_mark_node)
28165 return error_mark_node;
28166 cands = lookup_add (guide, cands);
28167 }
28168 }
28169
28170 if (elided && !cands)
28171 {
28172 error ("cannot deduce template arguments for copy-initialization"
28173 " of %qT, as it has no non-explicit deduction guides or "
28174 "user-declared constructors", type);
28175 return error_mark_node;
28176 }
28177 else if (!cands && call == error_mark_node)
28178 {
28179 error ("cannot deduce template arguments of %qT, as it has no viable "
28180 "deduction guides", type);
28181 return error_mark_node;
28182 }
28183
28184 if (call == error_mark_node)
28185 {
28186 ++cp_unevaluated_operand;
28187 call = build_new_function_call (cands, &args, tf_decltype);
28188 --cp_unevaluated_operand;
28189 }
28190
28191 if (call == error_mark_node && (complain & tf_warning_or_error))
28192 {
28193 error ("class template argument deduction failed:");
28194
28195 ++cp_unevaluated_operand;
28196 call = build_new_function_call (cands, &args, complain | tf_decltype);
28197 --cp_unevaluated_operand;
28198
28199 if (elided)
28200 inform (input_location, "explicit deduction guides not considered "
28201 "for copy-initialization");
28202 }
28203
28204 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28205 }
28206
28207 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28208 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28209 The CONTEXT determines the context in which auto deduction is performed
28210 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28211 OUTER_TARGS are used during template argument deduction
28212 (context == adc_unify) to properly substitute the result, and is ignored
28213 in other contexts.
28214
28215 For partial-concept-ids, extra args may be appended to the list of deduced
28216 template arguments prior to determining constraint satisfaction. */
28217
28218 tree
28219 do_auto_deduction (tree type, tree init, tree auto_node,
28220 tsubst_flags_t complain, auto_deduction_context context,
28221 tree outer_targs, int flags)
28222 {
28223 tree targs;
28224
28225 if (init == error_mark_node)
28226 return error_mark_node;
28227
28228 if (init && type_dependent_expression_p (init)
28229 && context != adc_unify)
28230 /* Defining a subset of type-dependent expressions that we can deduce
28231 from ahead of time isn't worth the trouble. */
28232 return type;
28233
28234 /* Similarly, we can't deduce from another undeduced decl. */
28235 if (init && undeduced_auto_decl (init))
28236 return type;
28237
28238 /* We may be doing a partial substitution, but we still want to replace
28239 auto_node. */
28240 complain &= ~tf_partial;
28241
28242 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28243 /* C++17 class template argument deduction. */
28244 return do_class_deduction (type, tmpl, init, flags, complain);
28245
28246 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28247 /* Nothing we can do with this, even in deduction context. */
28248 return type;
28249
28250 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28251 with either a new invented type template parameter U or, if the
28252 initializer is a braced-init-list (8.5.4), with
28253 std::initializer_list<U>. */
28254 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28255 {
28256 if (!DIRECT_LIST_INIT_P (init))
28257 type = listify_autos (type, auto_node);
28258 else if (CONSTRUCTOR_NELTS (init) == 1)
28259 init = CONSTRUCTOR_ELT (init, 0)->value;
28260 else
28261 {
28262 if (complain & tf_warning_or_error)
28263 {
28264 if (permerror (input_location, "direct-list-initialization of "
28265 "%<auto%> requires exactly one element"))
28266 inform (input_location,
28267 "for deduction to %<std::initializer_list%>, use copy-"
28268 "list-initialization (i.e. add %<=%> before the %<{%>)");
28269 }
28270 type = listify_autos (type, auto_node);
28271 }
28272 }
28273
28274 if (type == error_mark_node)
28275 return error_mark_node;
28276
28277 init = resolve_nondeduced_context (init, complain);
28278
28279 if (context == adc_decomp_type
28280 && auto_node == type
28281 && init != error_mark_node
28282 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28283 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28284 and initializer has array type, deduce cv-qualified array type. */
28285 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28286 complain);
28287 else if (AUTO_IS_DECLTYPE (auto_node))
28288 {
28289 tree stripped_init = tree_strip_any_location_wrapper (init);
28290 bool id = (DECL_P (stripped_init)
28291 || ((TREE_CODE (init) == COMPONENT_REF
28292 || TREE_CODE (init) == SCOPE_REF)
28293 && !REF_PARENTHESIZED_P (init)));
28294 targs = make_tree_vec (1);
28295 TREE_VEC_ELT (targs, 0)
28296 = finish_decltype_type (init, id, tf_warning_or_error);
28297 if (type != auto_node)
28298 {
28299 if (complain & tf_error)
28300 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28301 return error_mark_node;
28302 }
28303 }
28304 else
28305 {
28306 if (error_operand_p (init))
28307 return error_mark_node;
28308
28309 tree parms = build_tree_list (NULL_TREE, type);
28310 tree tparms;
28311
28312 if (flag_concepts)
28313 tparms = extract_autos (type);
28314 else
28315 {
28316 tparms = make_tree_vec (1);
28317 TREE_VEC_ELT (tparms, 0)
28318 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28319 }
28320
28321 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28322 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28323 DEDUCE_CALL,
28324 NULL, /*explain_p=*/false);
28325 if (val > 0)
28326 {
28327 if (processing_template_decl)
28328 /* Try again at instantiation time. */
28329 return type;
28330 if (type && type != error_mark_node
28331 && (complain & tf_error))
28332 /* If type is error_mark_node a diagnostic must have been
28333 emitted by now. Also, having a mention to '<type error>'
28334 in the diagnostic is not really useful to the user. */
28335 {
28336 if (cfun
28337 && FNDECL_USED_AUTO (current_function_decl)
28338 && (auto_node
28339 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28340 && LAMBDA_FUNCTION_P (current_function_decl))
28341 error ("unable to deduce lambda return type from %qE", init);
28342 else
28343 error ("unable to deduce %qT from %qE", type, init);
28344 type_unification_real (tparms, targs, parms, &init, 1, 0,
28345 DEDUCE_CALL,
28346 NULL, /*explain_p=*/true);
28347 }
28348 return error_mark_node;
28349 }
28350 }
28351
28352 /* Check any placeholder constraints against the deduced type. */
28353 if (flag_concepts && !processing_template_decl)
28354 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28355 {
28356 /* Use the deduced type to check the associated constraints. If we
28357 have a partial-concept-id, rebuild the argument list so that
28358 we check using the extra arguments. */
28359 check = unpack_concept_check (check);
28360 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28361 tree cdecl = TREE_OPERAND (check, 0);
28362 if (OVL_P (cdecl))
28363 cdecl = OVL_FIRST (cdecl);
28364 tree cargs = TREE_OPERAND (check, 1);
28365 if (TREE_VEC_LENGTH (cargs) > 1)
28366 {
28367 cargs = copy_node (cargs);
28368 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28369 }
28370 else
28371 cargs = targs;
28372
28373 /* Rebuild the check using the deduced arguments. */
28374 check = build_concept_check (cdecl, cargs, tf_none);
28375
28376 if (!constraints_satisfied_p (check))
28377 {
28378 if (complain & tf_warning_or_error)
28379 {
28380 auto_diagnostic_group d;
28381 switch (context)
28382 {
28383 case adc_unspecified:
28384 case adc_unify:
28385 error("placeholder constraints not satisfied");
28386 break;
28387 case adc_variable_type:
28388 case adc_decomp_type:
28389 error ("deduced initializer does not satisfy "
28390 "placeholder constraints");
28391 break;
28392 case adc_return_type:
28393 error ("deduced return type does not satisfy "
28394 "placeholder constraints");
28395 break;
28396 case adc_requirement:
28397 error ("deduced expression type does not satisfy "
28398 "placeholder constraints");
28399 break;
28400 }
28401 diagnose_constraints (input_location, check, targs);
28402 }
28403 return error_mark_node;
28404 }
28405 }
28406
28407 if (processing_template_decl && context != adc_unify)
28408 outer_targs = current_template_args ();
28409 targs = add_to_template_args (outer_targs, targs);
28410 return tsubst (type, targs, complain, NULL_TREE);
28411 }
28412
28413 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28414 result. */
28415
28416 tree
28417 splice_late_return_type (tree type, tree late_return_type)
28418 {
28419 if (is_auto (type))
28420 {
28421 if (late_return_type)
28422 return late_return_type;
28423
28424 tree idx = get_template_parm_index (type);
28425 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28426 /* In an abbreviated function template we didn't know we were dealing
28427 with a function template when we saw the auto return type, so update
28428 it to have the correct level. */
28429 return make_auto_1 (TYPE_IDENTIFIER (type), true);
28430 }
28431 return type;
28432 }
28433
28434 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28435 'decltype(auto)' or a deduced class template. */
28436
28437 bool
28438 is_auto (const_tree type)
28439 {
28440 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28441 && (TYPE_IDENTIFIER (type) == auto_identifier
28442 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28443 return true;
28444 else
28445 return false;
28446 }
28447
28448 /* for_each_template_parm callback for type_uses_auto. */
28449
28450 int
28451 is_auto_r (tree tp, void */*data*/)
28452 {
28453 return is_auto (tp);
28454 }
28455
28456 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28457 a use of `auto'. Returns NULL_TREE otherwise. */
28458
28459 tree
28460 type_uses_auto (tree type)
28461 {
28462 if (type == NULL_TREE)
28463 return NULL_TREE;
28464 else if (flag_concepts)
28465 {
28466 /* The Concepts TS allows multiple autos in one type-specifier; just
28467 return the first one we find, do_auto_deduction will collect all of
28468 them. */
28469 if (uses_template_parms (type))
28470 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28471 /*visited*/NULL, /*nondeduced*/false);
28472 else
28473 return NULL_TREE;
28474 }
28475 else
28476 return find_type_usage (type, is_auto);
28477 }
28478
28479 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28480 concepts are enabled, auto is acceptable in template arguments, but
28481 only when TEMPL identifies a template class. Return TRUE if any
28482 such errors were reported. */
28483
28484 bool
28485 check_auto_in_tmpl_args (tree tmpl, tree args)
28486 {
28487 /* If there were previous errors, nevermind. */
28488 if (!args || TREE_CODE (args) != TREE_VEC)
28489 return false;
28490
28491 /* If TMPL is an identifier, we're parsing and we can't tell yet
28492 whether TMPL is supposed to be a type, a function or a variable.
28493 We'll only be able to tell during template substitution, so we
28494 expect to be called again then. If concepts are enabled and we
28495 know we have a type, we're ok. */
28496 if (flag_concepts
28497 && (identifier_p (tmpl)
28498 || (DECL_P (tmpl)
28499 && (DECL_TYPE_TEMPLATE_P (tmpl)
28500 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
28501 return false;
28502
28503 /* Quickly search for any occurrences of auto; usually there won't
28504 be any, and then we'll avoid allocating the vector. */
28505 if (!type_uses_auto (args))
28506 return false;
28507
28508 bool errors = false;
28509
28510 tree vec = extract_autos (args);
28511 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
28512 {
28513 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
28514 error_at (DECL_SOURCE_LOCATION (xauto),
28515 "invalid use of %qT in template argument", xauto);
28516 errors = true;
28517 }
28518
28519 return errors;
28520 }
28521
28522 /* For a given template T, return the vector of typedefs referenced
28523 in T for which access check is needed at T instantiation time.
28524 T is either a FUNCTION_DECL or a RECORD_TYPE.
28525 Those typedefs were added to T by the function
28526 append_type_to_template_for_access_check. */
28527
28528 vec<qualified_typedef_usage_t, va_gc> *
28529 get_types_needing_access_check (tree t)
28530 {
28531 tree ti;
28532 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
28533
28534 if (!t || t == error_mark_node)
28535 return NULL;
28536
28537 if (!(ti = get_template_info (t)))
28538 return NULL;
28539
28540 if (CLASS_TYPE_P (t)
28541 || TREE_CODE (t) == FUNCTION_DECL)
28542 {
28543 if (!TI_TEMPLATE (ti))
28544 return NULL;
28545
28546 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
28547 }
28548
28549 return result;
28550 }
28551
28552 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28553 tied to T. That list of typedefs will be access checked at
28554 T instantiation time.
28555 T is either a FUNCTION_DECL or a RECORD_TYPE.
28556 TYPE_DECL is a TYPE_DECL node representing a typedef.
28557 SCOPE is the scope through which TYPE_DECL is accessed.
28558 LOCATION is the location of the usage point of TYPE_DECL.
28559
28560 This function is a subroutine of
28561 append_type_to_template_for_access_check. */
28562
28563 static void
28564 append_type_to_template_for_access_check_1 (tree t,
28565 tree type_decl,
28566 tree scope,
28567 location_t location)
28568 {
28569 qualified_typedef_usage_t typedef_usage;
28570 tree ti;
28571
28572 if (!t || t == error_mark_node)
28573 return;
28574
28575 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
28576 || CLASS_TYPE_P (t))
28577 && type_decl
28578 && TREE_CODE (type_decl) == TYPE_DECL
28579 && scope);
28580
28581 if (!(ti = get_template_info (t)))
28582 return;
28583
28584 gcc_assert (TI_TEMPLATE (ti));
28585
28586 typedef_usage.typedef_decl = type_decl;
28587 typedef_usage.context = scope;
28588 typedef_usage.locus = location;
28589
28590 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
28591 }
28592
28593 /* Append TYPE_DECL to the template TEMPL.
28594 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28595 At TEMPL instanciation time, TYPE_DECL will be checked to see
28596 if it can be accessed through SCOPE.
28597 LOCATION is the location of the usage point of TYPE_DECL.
28598
28599 e.g. consider the following code snippet:
28600
28601 class C
28602 {
28603 typedef int myint;
28604 };
28605
28606 template<class U> struct S
28607 {
28608 C::myint mi; // <-- usage point of the typedef C::myint
28609 };
28610
28611 S<char> s;
28612
28613 At S<char> instantiation time, we need to check the access of C::myint
28614 In other words, we need to check the access of the myint typedef through
28615 the C scope. For that purpose, this function will add the myint typedef
28616 and the scope C through which its being accessed to a list of typedefs
28617 tied to the template S. That list will be walked at template instantiation
28618 time and access check performed on each typedefs it contains.
28619 Note that this particular code snippet should yield an error because
28620 myint is private to C. */
28621
28622 void
28623 append_type_to_template_for_access_check (tree templ,
28624 tree type_decl,
28625 tree scope,
28626 location_t location)
28627 {
28628 qualified_typedef_usage_t *iter;
28629 unsigned i;
28630
28631 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
28632
28633 /* Make sure we don't append the type to the template twice. */
28634 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
28635 if (iter->typedef_decl == type_decl && scope == iter->context)
28636 return;
28637
28638 append_type_to_template_for_access_check_1 (templ, type_decl,
28639 scope, location);
28640 }
28641
28642 /* Recursively walk over && expressions searching for EXPR. Return a reference
28643 to that expression. */
28644
28645 static tree *find_template_requirement (tree *t, tree key)
28646 {
28647 if (*t == key)
28648 return t;
28649 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
28650 {
28651 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
28652 return p;
28653 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
28654 return p;
28655 }
28656 return 0;
28657 }
28658
28659 /* Convert the generic type parameters in PARM that match the types given in the
28660 range [START_IDX, END_IDX) from the current_template_parms into generic type
28661 packs. */
28662
28663 tree
28664 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
28665 {
28666 tree current = current_template_parms;
28667 int depth = TMPL_PARMS_DEPTH (current);
28668 current = INNERMOST_TEMPLATE_PARMS (current);
28669 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
28670
28671 for (int i = 0; i < start_idx; ++i)
28672 TREE_VEC_ELT (replacement, i)
28673 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28674
28675 for (int i = start_idx; i < end_idx; ++i)
28676 {
28677 /* Create a distinct parameter pack type from the current parm and add it
28678 to the replacement args to tsubst below into the generic function
28679 parameter. */
28680 tree node = TREE_VEC_ELT (current, i);
28681 tree o = TREE_TYPE (TREE_VALUE (node));
28682 tree t = copy_type (o);
28683 TEMPLATE_TYPE_PARM_INDEX (t)
28684 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
28685 t, 0, 0, tf_none);
28686 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
28687 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
28688 TYPE_MAIN_VARIANT (t) = t;
28689 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
28690 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28691 TREE_VEC_ELT (replacement, i) = t;
28692
28693 /* Replace the current template parameter with new pack. */
28694 TREE_VALUE (node) = TREE_CHAIN (t);
28695
28696 /* Surgically adjust the associated constraint of adjusted parameter
28697 and it's corresponding contribution to the current template
28698 requirements. */
28699 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
28700 {
28701 tree id = unpack_concept_check (constr);
28702 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
28703 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
28704 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
28705
28706 /* If there was a constraint, we also need to replace that in
28707 the template requirements, which we've already built. */
28708 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
28709 reqs = find_template_requirement (reqs, constr);
28710 *reqs = fold;
28711 }
28712 }
28713
28714 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
28715 TREE_VEC_ELT (replacement, i)
28716 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28717
28718 /* If there are more levels then build up the replacement with the outer
28719 template parms. */
28720 if (depth > 1)
28721 replacement = add_to_template_args (template_parms_to_args
28722 (TREE_CHAIN (current_template_parms)),
28723 replacement);
28724
28725 return tsubst (parm, replacement, tf_none, NULL_TREE);
28726 }
28727
28728 GTY(()) tree current_failed_constraint;
28729
28730 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28731 0..N-1. */
28732
28733 void
28734 declare_integer_pack (void)
28735 {
28736 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28737 build_function_type_list (integer_type_node,
28738 integer_type_node,
28739 NULL_TREE),
28740 NULL_TREE, ECF_CONST);
28741 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28742 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
28743 CP_BUILT_IN_INTEGER_PACK);
28744 }
28745
28746 /* Set up the hash tables for template instantiations. */
28747
28748 void
28749 init_template_processing (void)
28750 {
28751 /* FIXME: enable sanitization (PR87847) */
28752 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28753 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28754
28755 if (cxx_dialect >= cxx11)
28756 declare_integer_pack ();
28757 }
28758
28759 /* Print stats about the template hash tables for -fstats. */
28760
28761 void
28762 print_template_statistics (void)
28763 {
28764 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28765 "%f collisions\n", (long) decl_specializations->size (),
28766 (long) decl_specializations->elements (),
28767 decl_specializations->collisions ());
28768 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28769 "%f collisions\n", (long) type_specializations->size (),
28770 (long) type_specializations->elements (),
28771 type_specializations->collisions ());
28772 }
28773
28774 #if CHECKING_P
28775
28776 namespace selftest {
28777
28778 /* Verify that build_non_dependent_expr () works, for various expressions,
28779 and that location wrappers don't affect the results. */
28780
28781 static void
28782 test_build_non_dependent_expr ()
28783 {
28784 location_t loc = BUILTINS_LOCATION;
28785
28786 /* Verify constants, without and with location wrappers. */
28787 tree int_cst = build_int_cst (integer_type_node, 42);
28788 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28789
28790 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28791 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28792 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28793
28794 tree string_lit = build_string (4, "foo");
28795 TREE_TYPE (string_lit) = char_array_type_node;
28796 string_lit = fix_string_type (string_lit);
28797 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28798
28799 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28800 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28801 ASSERT_EQ (wrapped_string_lit,
28802 build_non_dependent_expr (wrapped_string_lit));
28803 }
28804
28805 /* Verify that type_dependent_expression_p () works correctly, even
28806 in the presence of location wrapper nodes. */
28807
28808 static void
28809 test_type_dependent_expression_p ()
28810 {
28811 location_t loc = BUILTINS_LOCATION;
28812
28813 tree name = get_identifier ("foo");
28814
28815 /* If no templates are involved, nothing is type-dependent. */
28816 gcc_assert (!processing_template_decl);
28817 ASSERT_FALSE (type_dependent_expression_p (name));
28818
28819 ++processing_template_decl;
28820
28821 /* Within a template, an unresolved name is always type-dependent. */
28822 ASSERT_TRUE (type_dependent_expression_p (name));
28823
28824 /* Ensure it copes with NULL_TREE and errors. */
28825 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28826 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28827
28828 /* A USING_DECL in a template should be type-dependent, even if wrapped
28829 with a location wrapper (PR c++/83799). */
28830 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28831 TREE_TYPE (using_decl) = integer_type_node;
28832 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28833 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28834 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28835 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28836
28837 --processing_template_decl;
28838 }
28839
28840 /* Run all of the selftests within this file. */
28841
28842 void
28843 cp_pt_c_tests ()
28844 {
28845 test_build_non_dependent_expr ();
28846 test_type_dependent_expression_p ();
28847 }
28848
28849 } // namespace selftest
28850
28851 #endif /* #if CHECKING_P */
28852
28853 #include "gt-cp-pt.h"