]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
re PR c++/89913 (ICE with invalid using declaration)
[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
855 static tree
856 maybe_new_partial_specialization (tree type)
857 {
858 /* An implicit instantiation of an incomplete type implies
859 the definition of a new class template.
860
861 template<typename T>
862 struct S;
863
864 template<typename T>
865 struct S<T*>;
866
867 Here, S<T*> is an implicit instantiation of S whose type
868 is incomplete. */
869 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
870 return type;
871
872 /* It can also be the case that TYPE is a completed specialization.
873 Continuing the previous example, suppose we also declare:
874
875 template<typename T>
876 requires Integral<T>
877 struct S<T*>;
878
879 Here, S<T*> refers to the specialization S<T*> defined
880 above. However, we need to differentiate definitions because
881 we intend to define a new partial specialization. In this case,
882 we rely on the fact that the constraints are different for
883 this declaration than that above.
884
885 Note that we also get here for injected class names and
886 late-parsed template definitions. We must ensure that we
887 do not create new type declarations for those cases. */
888 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
889 {
890 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
891 tree args = CLASSTYPE_TI_ARGS (type);
892
893 /* If there are no template parameters, this cannot be a new
894 partial template specialization? */
895 if (!current_template_parms)
896 return NULL_TREE;
897
898 /* The injected-class-name is not a new partial specialization. */
899 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
900 return NULL_TREE;
901
902 /* If the constraints are not the same as those of the primary
903 then, we can probably create a new specialization. */
904 tree type_constr = current_template_constraints ();
905
906 if (type == TREE_TYPE (tmpl))
907 {
908 tree main_constr = get_constraints (tmpl);
909 if (equivalent_constraints (type_constr, main_constr))
910 return NULL_TREE;
911 }
912
913 /* Also, if there's a pre-existing specialization with matching
914 constraints, then this also isn't new. */
915 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
916 while (specs)
917 {
918 tree spec_tmpl = TREE_VALUE (specs);
919 tree spec_args = TREE_PURPOSE (specs);
920 tree spec_constr = get_constraints (spec_tmpl);
921 if (comp_template_args (args, spec_args)
922 && equivalent_constraints (type_constr, spec_constr))
923 return NULL_TREE;
924 specs = TREE_CHAIN (specs);
925 }
926
927 /* Create a new type node (and corresponding type decl)
928 for the newly declared specialization. */
929 tree t = make_class_type (TREE_CODE (type));
930 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
931 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
932
933 /* We only need a separate type node for storing the definition of this
934 partial specialization; uses of S<T*> are unconstrained, so all are
935 equivalent. So keep TYPE_CANONICAL the same. */
936 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
937
938 /* Build the corresponding type decl. */
939 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
940 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
941 DECL_SOURCE_LOCATION (d) = input_location;
942 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
943 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
944
945 return t;
946 }
947
948 return NULL_TREE;
949 }
950
951 /* The TYPE is being declared. If it is a template type, that means it
952 is a partial specialization. Do appropriate error-checking. */
953
954 tree
955 maybe_process_partial_specialization (tree type)
956 {
957 tree context;
958
959 if (type == error_mark_node)
960 return error_mark_node;
961
962 /* A lambda that appears in specialization context is not itself a
963 specialization. */
964 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
965 return type;
966
967 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
968 {
969 error ("name of class shadows template template parameter %qD",
970 TYPE_NAME (type));
971 return error_mark_node;
972 }
973
974 context = TYPE_CONTEXT (type);
975
976 if (TYPE_ALIAS_P (type))
977 {
978 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
979
980 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
981 error ("specialization of alias template %qD",
982 TI_TEMPLATE (tinfo));
983 else
984 error ("explicit specialization of non-template %qT", type);
985 return error_mark_node;
986 }
987 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
988 {
989 /* This is for ordinary explicit specialization and partial
990 specialization of a template class such as:
991
992 template <> class C<int>;
993
994 or:
995
996 template <class T> class C<T*>;
997
998 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
999
1000 if (tree t = maybe_new_partial_specialization (type))
1001 {
1002 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1003 && !at_namespace_scope_p ())
1004 return error_mark_node;
1005 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1006 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1007 if (processing_template_decl)
1008 {
1009 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1010 if (decl == error_mark_node)
1011 return error_mark_node;
1012 return TREE_TYPE (decl);
1013 }
1014 }
1015 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1016 error ("specialization of %qT after instantiation", type);
1017 else if (errorcount && !processing_specialization
1018 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1019 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1020 /* Trying to define a specialization either without a template<> header
1021 or in an inappropriate place. We've already given an error, so just
1022 bail now so we don't actually define the specialization. */
1023 return error_mark_node;
1024 }
1025 else if (CLASS_TYPE_P (type)
1026 && !CLASSTYPE_USE_TEMPLATE (type)
1027 && CLASSTYPE_TEMPLATE_INFO (type)
1028 && context && CLASS_TYPE_P (context)
1029 && CLASSTYPE_TEMPLATE_INFO (context))
1030 {
1031 /* This is for an explicit specialization of member class
1032 template according to [temp.expl.spec/18]:
1033
1034 template <> template <class U> class C<int>::D;
1035
1036 The context `C<int>' must be an implicit instantiation.
1037 Otherwise this is just a member class template declared
1038 earlier like:
1039
1040 template <> class C<int> { template <class U> class D; };
1041 template <> template <class U> class C<int>::D;
1042
1043 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1044 while in the second case, `C<int>::D' is a primary template
1045 and `C<T>::D' may not exist. */
1046
1047 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1048 && !COMPLETE_TYPE_P (type))
1049 {
1050 tree t;
1051 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1052
1053 if (current_namespace
1054 != decl_namespace_context (tmpl))
1055 {
1056 if (permerror (input_location,
1057 "specialization of %qD in different namespace",
1058 type))
1059 inform (DECL_SOURCE_LOCATION (tmpl),
1060 "from definition of %q#D", tmpl);
1061 }
1062
1063 /* Check for invalid specialization after instantiation:
1064
1065 template <> template <> class C<int>::D<int>;
1066 template <> template <class U> class C<int>::D; */
1067
1068 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1069 t; t = TREE_CHAIN (t))
1070 {
1071 tree inst = TREE_VALUE (t);
1072 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1073 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1074 {
1075 /* We already have a full specialization of this partial
1076 instantiation, or a full specialization has been
1077 looked up but not instantiated. Reassign it to the
1078 new member specialization template. */
1079 spec_entry elt;
1080 spec_entry *entry;
1081
1082 elt.tmpl = most_general_template (tmpl);
1083 elt.args = CLASSTYPE_TI_ARGS (inst);
1084 elt.spec = inst;
1085
1086 type_specializations->remove_elt (&elt);
1087
1088 elt.tmpl = tmpl;
1089 CLASSTYPE_TI_ARGS (inst)
1090 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1091
1092 spec_entry **slot
1093 = type_specializations->find_slot (&elt, INSERT);
1094 entry = ggc_alloc<spec_entry> ();
1095 *entry = elt;
1096 *slot = entry;
1097 }
1098 else
1099 /* But if we've had an implicit instantiation, that's a
1100 problem ([temp.expl.spec]/6). */
1101 error ("specialization %qT after instantiation %qT",
1102 type, inst);
1103 }
1104
1105 /* Mark TYPE as a specialization. And as a result, we only
1106 have one level of template argument for the innermost
1107 class template. */
1108 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1109 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1110 CLASSTYPE_TI_ARGS (type)
1111 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1112 }
1113 }
1114 else if (processing_specialization)
1115 {
1116 /* Someday C++0x may allow for enum template specialization. */
1117 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1118 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1119 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1120 "of %qD not allowed by ISO C++", type);
1121 else
1122 {
1123 error ("explicit specialization of non-template %qT", type);
1124 return error_mark_node;
1125 }
1126 }
1127
1128 return type;
1129 }
1130
1131 /* Returns nonzero if we can optimize the retrieval of specializations
1132 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1133 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1134
1135 static inline bool
1136 optimize_specialization_lookup_p (tree tmpl)
1137 {
1138 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1139 && DECL_CLASS_SCOPE_P (tmpl)
1140 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1141 parameter. */
1142 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1143 /* The optimized lookup depends on the fact that the
1144 template arguments for the member function template apply
1145 purely to the containing class, which is not true if the
1146 containing class is an explicit or partial
1147 specialization. */
1148 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1149 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1150 && !DECL_CONV_FN_P (tmpl)
1151 /* It is possible to have a template that is not a member
1152 template and is not a member of a template class:
1153
1154 template <typename T>
1155 struct S { friend A::f(); };
1156
1157 Here, the friend function is a template, but the context does
1158 not have template information. The optimized lookup relies
1159 on having ARGS be the template arguments for both the class
1160 and the function template. */
1161 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1162 }
1163
1164 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1165 gone through coerce_template_parms by now. */
1166
1167 static void
1168 verify_unstripped_args_1 (tree inner)
1169 {
1170 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1171 {
1172 tree arg = TREE_VEC_ELT (inner, i);
1173 if (TREE_CODE (arg) == TEMPLATE_DECL)
1174 /* OK */;
1175 else if (TYPE_P (arg))
1176 gcc_assert (strip_typedefs (arg, NULL) == arg);
1177 else if (ARGUMENT_PACK_P (arg))
1178 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1179 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1180 /* Allow typedefs on the type of a non-type argument, since a
1181 parameter can have them. */;
1182 else
1183 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1184 }
1185 }
1186
1187 static void
1188 verify_unstripped_args (tree args)
1189 {
1190 ++processing_template_decl;
1191 if (!any_dependent_template_arguments_p (args))
1192 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1193 --processing_template_decl;
1194 }
1195
1196 /* Retrieve the specialization (in the sense of [temp.spec] - a
1197 specialization is either an instantiation or an explicit
1198 specialization) of TMPL for the given template ARGS. If there is
1199 no such specialization, return NULL_TREE. The ARGS are a vector of
1200 arguments, or a vector of vectors of arguments, in the case of
1201 templates with more than one level of parameters.
1202
1203 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1204 then we search for a partial specialization matching ARGS. This
1205 parameter is ignored if TMPL is not a class template.
1206
1207 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1208 result is a NONTYPE_ARGUMENT_PACK. */
1209
1210 static tree
1211 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1212 {
1213 if (tmpl == NULL_TREE)
1214 return NULL_TREE;
1215
1216 if (args == error_mark_node)
1217 return NULL_TREE;
1218
1219 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1220 || TREE_CODE (tmpl) == FIELD_DECL);
1221
1222 /* There should be as many levels of arguments as there are
1223 levels of parameters. */
1224 gcc_assert (TMPL_ARGS_DEPTH (args)
1225 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1226 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1227 : template_class_depth (DECL_CONTEXT (tmpl))));
1228
1229 if (flag_checking)
1230 verify_unstripped_args (args);
1231
1232 /* Lambda functions in templates aren't instantiated normally, but through
1233 tsubst_lambda_expr. */
1234 if (lambda_fn_in_template_p (tmpl))
1235 return NULL_TREE;
1236
1237 if (optimize_specialization_lookup_p (tmpl))
1238 {
1239 /* The template arguments actually apply to the containing
1240 class. Find the class specialization with those
1241 arguments. */
1242 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1243 tree class_specialization
1244 = retrieve_specialization (class_template, args, 0);
1245 if (!class_specialization)
1246 return NULL_TREE;
1247
1248 /* Find the instance of TMPL. */
1249 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1250 for (ovl_iterator iter (fns); iter; ++iter)
1251 {
1252 tree fn = *iter;
1253 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1254 /* using-declarations can add base methods to the method vec,
1255 and we don't want those here. */
1256 && DECL_CONTEXT (fn) == class_specialization)
1257 return fn;
1258 }
1259 return NULL_TREE;
1260 }
1261 else
1262 {
1263 spec_entry *found;
1264 spec_entry elt;
1265 hash_table<spec_hasher> *specializations;
1266
1267 elt.tmpl = tmpl;
1268 elt.args = args;
1269 elt.spec = NULL_TREE;
1270
1271 if (DECL_CLASS_TEMPLATE_P (tmpl))
1272 specializations = type_specializations;
1273 else
1274 specializations = decl_specializations;
1275
1276 if (hash == 0)
1277 hash = spec_hasher::hash (&elt);
1278 found = specializations->find_with_hash (&elt, hash);
1279 if (found)
1280 return found->spec;
1281 }
1282
1283 return NULL_TREE;
1284 }
1285
1286 /* Like retrieve_specialization, but for local declarations. */
1287
1288 tree
1289 retrieve_local_specialization (tree tmpl)
1290 {
1291 if (local_specializations == NULL)
1292 return NULL_TREE;
1293
1294 tree *slot = local_specializations->get (tmpl);
1295 return slot ? *slot : NULL_TREE;
1296 }
1297
1298 /* Returns nonzero iff DECL is a specialization of TMPL. */
1299
1300 int
1301 is_specialization_of (tree decl, tree tmpl)
1302 {
1303 tree t;
1304
1305 if (TREE_CODE (decl) == FUNCTION_DECL)
1306 {
1307 for (t = decl;
1308 t != NULL_TREE;
1309 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1310 if (t == tmpl)
1311 return 1;
1312 }
1313 else
1314 {
1315 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1316
1317 for (t = TREE_TYPE (decl);
1318 t != NULL_TREE;
1319 t = CLASSTYPE_USE_TEMPLATE (t)
1320 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1321 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1322 return 1;
1323 }
1324
1325 return 0;
1326 }
1327
1328 /* Returns nonzero iff DECL is a specialization of friend declaration
1329 FRIEND_DECL according to [temp.friend]. */
1330
1331 bool
1332 is_specialization_of_friend (tree decl, tree friend_decl)
1333 {
1334 bool need_template = true;
1335 int template_depth;
1336
1337 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1338 || TREE_CODE (decl) == TYPE_DECL);
1339
1340 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1341 of a template class, we want to check if DECL is a specialization
1342 if this. */
1343 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1344 && DECL_TEMPLATE_INFO (friend_decl)
1345 && !DECL_USE_TEMPLATE (friend_decl))
1346 {
1347 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1348 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1349 need_template = false;
1350 }
1351 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1352 && !PRIMARY_TEMPLATE_P (friend_decl))
1353 need_template = false;
1354
1355 /* There is nothing to do if this is not a template friend. */
1356 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1357 return false;
1358
1359 if (is_specialization_of (decl, friend_decl))
1360 return true;
1361
1362 /* [temp.friend/6]
1363 A member of a class template may be declared to be a friend of a
1364 non-template class. In this case, the corresponding member of
1365 every specialization of the class template is a friend of the
1366 class granting friendship.
1367
1368 For example, given a template friend declaration
1369
1370 template <class T> friend void A<T>::f();
1371
1372 the member function below is considered a friend
1373
1374 template <> struct A<int> {
1375 void f();
1376 };
1377
1378 For this type of template friend, TEMPLATE_DEPTH below will be
1379 nonzero. To determine if DECL is a friend of FRIEND, we first
1380 check if the enclosing class is a specialization of another. */
1381
1382 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1383 if (template_depth
1384 && DECL_CLASS_SCOPE_P (decl)
1385 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1386 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1387 {
1388 /* Next, we check the members themselves. In order to handle
1389 a few tricky cases, such as when FRIEND_DECL's are
1390
1391 template <class T> friend void A<T>::g(T t);
1392 template <class T> template <T t> friend void A<T>::h();
1393
1394 and DECL's are
1395
1396 void A<int>::g(int);
1397 template <int> void A<int>::h();
1398
1399 we need to figure out ARGS, the template arguments from
1400 the context of DECL. This is required for template substitution
1401 of `T' in the function parameter of `g' and template parameter
1402 of `h' in the above examples. Here ARGS corresponds to `int'. */
1403
1404 tree context = DECL_CONTEXT (decl);
1405 tree args = NULL_TREE;
1406 int current_depth = 0;
1407
1408 while (current_depth < template_depth)
1409 {
1410 if (CLASSTYPE_TEMPLATE_INFO (context))
1411 {
1412 if (current_depth == 0)
1413 args = TYPE_TI_ARGS (context);
1414 else
1415 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1416 current_depth++;
1417 }
1418 context = TYPE_CONTEXT (context);
1419 }
1420
1421 if (TREE_CODE (decl) == FUNCTION_DECL)
1422 {
1423 bool is_template;
1424 tree friend_type;
1425 tree decl_type;
1426 tree friend_args_type;
1427 tree decl_args_type;
1428
1429 /* Make sure that both DECL and FRIEND_DECL are templates or
1430 non-templates. */
1431 is_template = DECL_TEMPLATE_INFO (decl)
1432 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1433 if (need_template ^ is_template)
1434 return false;
1435 else if (is_template)
1436 {
1437 /* If both are templates, check template parameter list. */
1438 tree friend_parms
1439 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1440 args, tf_none);
1441 if (!comp_template_parms
1442 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1443 friend_parms))
1444 return false;
1445
1446 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1447 }
1448 else
1449 decl_type = TREE_TYPE (decl);
1450
1451 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1452 tf_none, NULL_TREE);
1453 if (friend_type == error_mark_node)
1454 return false;
1455
1456 /* Check if return types match. */
1457 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1458 return false;
1459
1460 /* Check if function parameter types match, ignoring the
1461 `this' parameter. */
1462 friend_args_type = TYPE_ARG_TYPES (friend_type);
1463 decl_args_type = TYPE_ARG_TYPES (decl_type);
1464 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1465 friend_args_type = TREE_CHAIN (friend_args_type);
1466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1467 decl_args_type = TREE_CHAIN (decl_args_type);
1468
1469 return compparms (decl_args_type, friend_args_type);
1470 }
1471 else
1472 {
1473 /* DECL is a TYPE_DECL */
1474 bool is_template;
1475 tree decl_type = TREE_TYPE (decl);
1476
1477 /* Make sure that both DECL and FRIEND_DECL are templates or
1478 non-templates. */
1479 is_template
1480 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1481 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1482
1483 if (need_template ^ is_template)
1484 return false;
1485 else if (is_template)
1486 {
1487 tree friend_parms;
1488 /* If both are templates, check the name of the two
1489 TEMPLATE_DECL's first because is_friend didn't. */
1490 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1491 != DECL_NAME (friend_decl))
1492 return false;
1493
1494 /* Now check template parameter list. */
1495 friend_parms
1496 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1497 args, tf_none);
1498 return comp_template_parms
1499 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1500 friend_parms);
1501 }
1502 else
1503 return (DECL_NAME (decl)
1504 == DECL_NAME (friend_decl));
1505 }
1506 }
1507 return false;
1508 }
1509
1510 /* Register the specialization SPEC as a specialization of TMPL with
1511 the indicated ARGS. IS_FRIEND indicates whether the specialization
1512 is actually just a friend declaration. ATTRLIST is the list of
1513 attributes that the specialization is declared with or NULL when
1514 it isn't. Returns SPEC, or an equivalent prior declaration, if
1515 available.
1516
1517 We also store instantiations of field packs in the hash table, even
1518 though they are not themselves templates, to make lookup easier. */
1519
1520 static tree
1521 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1522 hashval_t hash)
1523 {
1524 tree fn;
1525 spec_entry **slot = NULL;
1526 spec_entry elt;
1527
1528 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1529 || (TREE_CODE (tmpl) == FIELD_DECL
1530 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1531
1532 if (TREE_CODE (spec) == FUNCTION_DECL
1533 && uses_template_parms (DECL_TI_ARGS (spec)))
1534 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1535 register it; we want the corresponding TEMPLATE_DECL instead.
1536 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1537 the more obvious `uses_template_parms (spec)' to avoid problems
1538 with default function arguments. In particular, given
1539 something like this:
1540
1541 template <class T> void f(T t1, T t = T())
1542
1543 the default argument expression is not substituted for in an
1544 instantiation unless and until it is actually needed. */
1545 return spec;
1546
1547 if (optimize_specialization_lookup_p (tmpl))
1548 /* We don't put these specializations in the hash table, but we might
1549 want to give an error about a mismatch. */
1550 fn = retrieve_specialization (tmpl, args, 0);
1551 else
1552 {
1553 elt.tmpl = tmpl;
1554 elt.args = args;
1555 elt.spec = spec;
1556
1557 if (hash == 0)
1558 hash = spec_hasher::hash (&elt);
1559
1560 slot =
1561 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1562 if (*slot)
1563 fn = ((spec_entry *) *slot)->spec;
1564 else
1565 fn = NULL_TREE;
1566 }
1567
1568 /* We can sometimes try to re-register a specialization that we've
1569 already got. In particular, regenerate_decl_from_template calls
1570 duplicate_decls which will update the specialization list. But,
1571 we'll still get called again here anyhow. It's more convenient
1572 to simply allow this than to try to prevent it. */
1573 if (fn == spec)
1574 return spec;
1575 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1576 {
1577 if (DECL_TEMPLATE_INSTANTIATION (fn))
1578 {
1579 if (DECL_ODR_USED (fn)
1580 || DECL_EXPLICIT_INSTANTIATION (fn))
1581 {
1582 error ("specialization of %qD after instantiation",
1583 fn);
1584 return error_mark_node;
1585 }
1586 else
1587 {
1588 tree clone;
1589 /* This situation should occur only if the first
1590 specialization is an implicit instantiation, the
1591 second is an explicit specialization, and the
1592 implicit instantiation has not yet been used. That
1593 situation can occur if we have implicitly
1594 instantiated a member function and then specialized
1595 it later.
1596
1597 We can also wind up here if a friend declaration that
1598 looked like an instantiation turns out to be a
1599 specialization:
1600
1601 template <class T> void foo(T);
1602 class S { friend void foo<>(int) };
1603 template <> void foo(int);
1604
1605 We transform the existing DECL in place so that any
1606 pointers to it become pointers to the updated
1607 declaration.
1608
1609 If there was a definition for the template, but not
1610 for the specialization, we want this to look as if
1611 there were no definition, and vice versa. */
1612 DECL_INITIAL (fn) = NULL_TREE;
1613 duplicate_decls (spec, fn, is_friend);
1614 /* The call to duplicate_decls will have applied
1615 [temp.expl.spec]:
1616
1617 An explicit specialization of a function template
1618 is inline only if it is explicitly declared to be,
1619 and independently of whether its function template
1620 is.
1621
1622 to the primary function; now copy the inline bits to
1623 the various clones. */
1624 FOR_EACH_CLONE (clone, fn)
1625 {
1626 DECL_DECLARED_INLINE_P (clone)
1627 = DECL_DECLARED_INLINE_P (fn);
1628 DECL_SOURCE_LOCATION (clone)
1629 = DECL_SOURCE_LOCATION (fn);
1630 DECL_DELETED_FN (clone)
1631 = DECL_DELETED_FN (fn);
1632 }
1633 check_specialization_namespace (tmpl);
1634
1635 return fn;
1636 }
1637 }
1638 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1639 {
1640 tree dd = duplicate_decls (spec, fn, is_friend);
1641 if (dd == error_mark_node)
1642 /* We've already complained in duplicate_decls. */
1643 return error_mark_node;
1644
1645 if (dd == NULL_TREE && DECL_INITIAL (spec))
1646 /* Dup decl failed, but this is a new definition. Set the
1647 line number so any errors match this new
1648 definition. */
1649 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1650
1651 return fn;
1652 }
1653 }
1654 else if (fn)
1655 return duplicate_decls (spec, fn, is_friend);
1656
1657 /* A specialization must be declared in the same namespace as the
1658 template it is specializing. */
1659 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1660 && !check_specialization_namespace (tmpl))
1661 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1662
1663 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1664 {
1665 spec_entry *entry = ggc_alloc<spec_entry> ();
1666 gcc_assert (tmpl && args && spec);
1667 *entry = elt;
1668 *slot = entry;
1669 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1670 && PRIMARY_TEMPLATE_P (tmpl)
1671 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1672 || variable_template_p (tmpl))
1673 /* If TMPL is a forward declaration of a template function, keep a list
1674 of all specializations in case we need to reassign them to a friend
1675 template later in tsubst_friend_function.
1676
1677 Also keep a list of all variable template instantiations so that
1678 process_partial_specialization can check whether a later partial
1679 specialization would have used it. */
1680 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1681 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1682 }
1683
1684 return spec;
1685 }
1686
1687 /* Returns true iff two spec_entry nodes are equivalent. */
1688
1689 int comparing_specializations;
1690
1691 bool
1692 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1693 {
1694 int equal;
1695
1696 ++comparing_specializations;
1697 equal = (e1->tmpl == e2->tmpl
1698 && comp_template_args (e1->args, e2->args));
1699 if (equal && flag_concepts
1700 /* tmpl could be a FIELD_DECL for a capture pack. */
1701 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1702 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1703 && uses_template_parms (e1->args))
1704 {
1705 /* Partial specializations of a variable template can be distinguished by
1706 constraints. */
1707 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1708 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1709 equal = equivalent_constraints (c1, c2);
1710 }
1711 --comparing_specializations;
1712
1713 return equal;
1714 }
1715
1716 /* Returns a hash for a template TMPL and template arguments ARGS. */
1717
1718 static hashval_t
1719 hash_tmpl_and_args (tree tmpl, tree args)
1720 {
1721 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1722 return iterative_hash_template_arg (args, val);
1723 }
1724
1725 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1726 ignoring SPEC. */
1727
1728 hashval_t
1729 spec_hasher::hash (spec_entry *e)
1730 {
1731 return hash_tmpl_and_args (e->tmpl, e->args);
1732 }
1733
1734 /* Recursively calculate a hash value for a template argument ARG, for use
1735 in the hash tables of template specializations. */
1736
1737 hashval_t
1738 iterative_hash_template_arg (tree arg, hashval_t val)
1739 {
1740 unsigned HOST_WIDE_INT i;
1741 enum tree_code code;
1742 char tclass;
1743
1744 if (arg == NULL_TREE)
1745 return iterative_hash_object (arg, val);
1746
1747 if (!TYPE_P (arg))
1748 STRIP_NOPS (arg);
1749
1750 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1751 gcc_unreachable ();
1752
1753 code = TREE_CODE (arg);
1754 tclass = TREE_CODE_CLASS (code);
1755
1756 val = iterative_hash_object (code, val);
1757
1758 switch (code)
1759 {
1760 case ERROR_MARK:
1761 return val;
1762
1763 case IDENTIFIER_NODE:
1764 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1765
1766 case TREE_VEC:
1767 {
1768 int i, len = TREE_VEC_LENGTH (arg);
1769 for (i = 0; i < len; ++i)
1770 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1771 return val;
1772 }
1773
1774 case TYPE_PACK_EXPANSION:
1775 case EXPR_PACK_EXPANSION:
1776 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1777 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1778
1779 case TYPE_ARGUMENT_PACK:
1780 case NONTYPE_ARGUMENT_PACK:
1781 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1782
1783 case TREE_LIST:
1784 for (; arg; arg = TREE_CHAIN (arg))
1785 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1786 return val;
1787
1788 case OVERLOAD:
1789 for (lkp_iterator iter (arg); iter; ++iter)
1790 val = iterative_hash_template_arg (*iter, val);
1791 return val;
1792
1793 case CONSTRUCTOR:
1794 {
1795 tree field, value;
1796 iterative_hash_template_arg (TREE_TYPE (arg), val);
1797 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1798 {
1799 val = iterative_hash_template_arg (field, val);
1800 val = iterative_hash_template_arg (value, val);
1801 }
1802 return val;
1803 }
1804
1805 case PARM_DECL:
1806 if (!DECL_ARTIFICIAL (arg))
1807 {
1808 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1809 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1810 }
1811 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1812
1813 case TARGET_EXPR:
1814 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1815
1816 case PTRMEM_CST:
1817 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1818 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1819
1820 case TEMPLATE_PARM_INDEX:
1821 val = iterative_hash_template_arg
1822 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1823 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1824 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1825
1826 case TRAIT_EXPR:
1827 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1828 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1829 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1830
1831 case BASELINK:
1832 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1833 val);
1834 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1835 val);
1836
1837 case MODOP_EXPR:
1838 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1839 code = TREE_CODE (TREE_OPERAND (arg, 1));
1840 val = iterative_hash_object (code, val);
1841 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1842
1843 case LAMBDA_EXPR:
1844 /* [temp.over.link] Two lambda-expressions are never considered
1845 equivalent.
1846
1847 So just hash the closure type. */
1848 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1849
1850 case CAST_EXPR:
1851 case IMPLICIT_CONV_EXPR:
1852 case STATIC_CAST_EXPR:
1853 case REINTERPRET_CAST_EXPR:
1854 case CONST_CAST_EXPR:
1855 case DYNAMIC_CAST_EXPR:
1856 case NEW_EXPR:
1857 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1858 /* Now hash operands as usual. */
1859 break;
1860
1861 case CALL_EXPR:
1862 {
1863 tree fn = CALL_EXPR_FN (arg);
1864 if (tree name = dependent_name (fn))
1865 {
1866 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1867 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1868 fn = name;
1869 }
1870 val = iterative_hash_template_arg (fn, val);
1871 call_expr_arg_iterator ai;
1872 for (tree x = first_call_expr_arg (arg, &ai); x;
1873 x = next_call_expr_arg (&ai))
1874 val = iterative_hash_template_arg (x, val);
1875 return val;
1876 }
1877
1878 default:
1879 break;
1880 }
1881
1882 switch (tclass)
1883 {
1884 case tcc_type:
1885 if (alias_template_specialization_p (arg))
1886 {
1887 // We want an alias specialization that survived strip_typedefs
1888 // to hash differently from its TYPE_CANONICAL, to avoid hash
1889 // collisions that compare as different in template_args_equal.
1890 // These could be dependent specializations that strip_typedefs
1891 // left alone, or untouched specializations because
1892 // coerce_template_parms returns the unconverted template
1893 // arguments if it sees incomplete argument packs.
1894 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1895 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1896 }
1897 if (TYPE_CANONICAL (arg))
1898 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1899 val);
1900 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1901 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1902 /* Otherwise just compare the types during lookup. */
1903 return val;
1904
1905 case tcc_declaration:
1906 case tcc_constant:
1907 return iterative_hash_expr (arg, val);
1908
1909 default:
1910 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1911 {
1912 unsigned n = cp_tree_operand_length (arg);
1913 for (i = 0; i < n; ++i)
1914 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1915 return val;
1916 }
1917 }
1918 gcc_unreachable ();
1919 return 0;
1920 }
1921
1922 /* Unregister the specialization SPEC as a specialization of TMPL.
1923 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1924 if the SPEC was listed as a specialization of TMPL.
1925
1926 Note that SPEC has been ggc_freed, so we can't look inside it. */
1927
1928 bool
1929 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1930 {
1931 spec_entry *entry;
1932 spec_entry elt;
1933
1934 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1935 elt.args = TI_ARGS (tinfo);
1936 elt.spec = NULL_TREE;
1937
1938 entry = decl_specializations->find (&elt);
1939 if (entry != NULL)
1940 {
1941 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1942 gcc_assert (new_spec != NULL_TREE);
1943 entry->spec = new_spec;
1944 return 1;
1945 }
1946
1947 return 0;
1948 }
1949
1950 /* Like register_specialization, but for local declarations. We are
1951 registering SPEC, an instantiation of TMPL. */
1952
1953 void
1954 register_local_specialization (tree spec, tree tmpl)
1955 {
1956 gcc_assert (tmpl != spec);
1957 local_specializations->put (tmpl, spec);
1958 }
1959
1960 /* TYPE is a class type. Returns true if TYPE is an explicitly
1961 specialized class. */
1962
1963 bool
1964 explicit_class_specialization_p (tree type)
1965 {
1966 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1967 return false;
1968 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1969 }
1970
1971 /* Print the list of functions at FNS, going through all the overloads
1972 for each element of the list. Alternatively, FNS cannot be a
1973 TREE_LIST, in which case it will be printed together with all the
1974 overloads.
1975
1976 MORE and *STR should respectively be FALSE and NULL when the function
1977 is called from the outside. They are used internally on recursive
1978 calls. print_candidates manages the two parameters and leaves NULL
1979 in *STR when it ends. */
1980
1981 static void
1982 print_candidates_1 (tree fns, char **str, bool more = false)
1983 {
1984 if (TREE_CODE (fns) == TREE_LIST)
1985 for (; fns; fns = TREE_CHAIN (fns))
1986 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1987 else
1988 for (lkp_iterator iter (fns); iter;)
1989 {
1990 tree cand = *iter;
1991 ++iter;
1992
1993 const char *pfx = *str;
1994 if (!pfx)
1995 {
1996 if (more || iter)
1997 pfx = _("candidates are:");
1998 else
1999 pfx = _("candidate is:");
2000 *str = get_spaces (pfx);
2001 }
2002 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2003 }
2004 }
2005
2006 /* Print the list of candidate FNS in an error message. FNS can also
2007 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2008
2009 void
2010 print_candidates (tree fns)
2011 {
2012 char *str = NULL;
2013 print_candidates_1 (fns, &str);
2014 free (str);
2015 }
2016
2017 /* Get a (possibly) constrained template declaration for the
2018 purpose of ordering candidates. */
2019 static tree
2020 get_template_for_ordering (tree list)
2021 {
2022 gcc_assert (TREE_CODE (list) == TREE_LIST);
2023 tree f = TREE_VALUE (list);
2024 if (tree ti = DECL_TEMPLATE_INFO (f))
2025 return TI_TEMPLATE (ti);
2026 return f;
2027 }
2028
2029 /* Among candidates having the same signature, return the
2030 most constrained or NULL_TREE if there is no best candidate.
2031 If the signatures of candidates vary (e.g., template
2032 specialization vs. member function), then there can be no
2033 most constrained.
2034
2035 Note that we don't compare constraints on the functions
2036 themselves, but rather those of their templates. */
2037 static tree
2038 most_constrained_function (tree candidates)
2039 {
2040 // Try to find the best candidate in a first pass.
2041 tree champ = candidates;
2042 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2043 {
2044 int winner = more_constrained (get_template_for_ordering (champ),
2045 get_template_for_ordering (c));
2046 if (winner == -1)
2047 champ = c; // The candidate is more constrained
2048 else if (winner == 0)
2049 return NULL_TREE; // Neither is more constrained
2050 }
2051
2052 // Verify that the champ is better than previous candidates.
2053 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2054 if (!more_constrained (get_template_for_ordering (champ),
2055 get_template_for_ordering (c)))
2056 return NULL_TREE;
2057 }
2058
2059 return champ;
2060 }
2061
2062
2063 /* Returns the template (one of the functions given by TEMPLATE_ID)
2064 which can be specialized to match the indicated DECL with the
2065 explicit template args given in TEMPLATE_ID. The DECL may be
2066 NULL_TREE if none is available. In that case, the functions in
2067 TEMPLATE_ID are non-members.
2068
2069 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2070 specialization of a member template.
2071
2072 The TEMPLATE_COUNT is the number of references to qualifying
2073 template classes that appeared in the name of the function. See
2074 check_explicit_specialization for a more accurate description.
2075
2076 TSK indicates what kind of template declaration (if any) is being
2077 declared. TSK_TEMPLATE indicates that the declaration given by
2078 DECL, though a FUNCTION_DECL, has template parameters, and is
2079 therefore a template function.
2080
2081 The template args (those explicitly specified and those deduced)
2082 are output in a newly created vector *TARGS_OUT.
2083
2084 If it is impossible to determine the result, an error message is
2085 issued. The error_mark_node is returned to indicate failure. */
2086
2087 static tree
2088 determine_specialization (tree template_id,
2089 tree decl,
2090 tree* targs_out,
2091 int need_member_template,
2092 int template_count,
2093 tmpl_spec_kind tsk)
2094 {
2095 tree fns;
2096 tree targs;
2097 tree explicit_targs;
2098 tree candidates = NULL_TREE;
2099
2100 /* A TREE_LIST of templates of which DECL may be a specialization.
2101 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2102 corresponding TREE_PURPOSE is the set of template arguments that,
2103 when used to instantiate the template, would produce a function
2104 with the signature of DECL. */
2105 tree templates = NULL_TREE;
2106 int header_count;
2107 cp_binding_level *b;
2108
2109 *targs_out = NULL_TREE;
2110
2111 if (template_id == error_mark_node || decl == error_mark_node)
2112 return error_mark_node;
2113
2114 /* We shouldn't be specializing a member template of an
2115 unspecialized class template; we already gave an error in
2116 check_specialization_scope, now avoid crashing. */
2117 if (!VAR_P (decl)
2118 && template_count && DECL_CLASS_SCOPE_P (decl)
2119 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2120 {
2121 gcc_assert (errorcount);
2122 return error_mark_node;
2123 }
2124
2125 fns = TREE_OPERAND (template_id, 0);
2126 explicit_targs = TREE_OPERAND (template_id, 1);
2127
2128 if (fns == error_mark_node)
2129 return error_mark_node;
2130
2131 /* Check for baselinks. */
2132 if (BASELINK_P (fns))
2133 fns = BASELINK_FUNCTIONS (fns);
2134
2135 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2136 {
2137 error_at (DECL_SOURCE_LOCATION (decl),
2138 "%qD is not a function template", fns);
2139 return error_mark_node;
2140 }
2141 else if (VAR_P (decl) && !variable_template_p (fns))
2142 {
2143 error ("%qD is not a variable template", fns);
2144 return error_mark_node;
2145 }
2146
2147 /* Count the number of template headers specified for this
2148 specialization. */
2149 header_count = 0;
2150 for (b = current_binding_level;
2151 b->kind == sk_template_parms;
2152 b = b->level_chain)
2153 ++header_count;
2154
2155 tree orig_fns = fns;
2156
2157 if (variable_template_p (fns))
2158 {
2159 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2160 targs = coerce_template_parms (parms, explicit_targs, fns,
2161 tf_warning_or_error,
2162 /*req_all*/true, /*use_defarg*/true);
2163 if (targs != error_mark_node)
2164 templates = tree_cons (targs, fns, templates);
2165 }
2166 else for (lkp_iterator iter (fns); iter; ++iter)
2167 {
2168 tree fn = *iter;
2169
2170 if (TREE_CODE (fn) == TEMPLATE_DECL)
2171 {
2172 tree decl_arg_types;
2173 tree fn_arg_types;
2174 tree insttype;
2175
2176 /* In case of explicit specialization, we need to check if
2177 the number of template headers appearing in the specialization
2178 is correct. This is usually done in check_explicit_specialization,
2179 but the check done there cannot be exhaustive when specializing
2180 member functions. Consider the following code:
2181
2182 template <> void A<int>::f(int);
2183 template <> template <> void A<int>::f(int);
2184
2185 Assuming that A<int> is not itself an explicit specialization
2186 already, the first line specializes "f" which is a non-template
2187 member function, whilst the second line specializes "f" which
2188 is a template member function. So both lines are syntactically
2189 correct, and check_explicit_specialization does not reject
2190 them.
2191
2192 Here, we can do better, as we are matching the specialization
2193 against the declarations. We count the number of template
2194 headers, and we check if they match TEMPLATE_COUNT + 1
2195 (TEMPLATE_COUNT is the number of qualifying template classes,
2196 plus there must be another header for the member template
2197 itself).
2198
2199 Notice that if header_count is zero, this is not a
2200 specialization but rather a template instantiation, so there
2201 is no check we can perform here. */
2202 if (header_count && header_count != template_count + 1)
2203 continue;
2204
2205 /* Check that the number of template arguments at the
2206 innermost level for DECL is the same as for FN. */
2207 if (current_binding_level->kind == sk_template_parms
2208 && !current_binding_level->explicit_spec_p
2209 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2210 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2211 (current_template_parms))))
2212 continue;
2213
2214 /* DECL might be a specialization of FN. */
2215 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2216 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2217
2218 /* For a non-static member function, we need to make sure
2219 that the const qualification is the same. Since
2220 get_bindings does not try to merge the "this" parameter,
2221 we must do the comparison explicitly. */
2222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2223 {
2224 if (!same_type_p (TREE_VALUE (fn_arg_types),
2225 TREE_VALUE (decl_arg_types)))
2226 continue;
2227
2228 /* And the ref-qualification. */
2229 if (type_memfn_rqual (TREE_TYPE (decl))
2230 != type_memfn_rqual (TREE_TYPE (fn)))
2231 continue;
2232 }
2233
2234 /* Skip the "this" parameter and, for constructors of
2235 classes with virtual bases, the VTT parameter. A
2236 full specialization of a constructor will have a VTT
2237 parameter, but a template never will. */
2238 decl_arg_types
2239 = skip_artificial_parms_for (decl, decl_arg_types);
2240 fn_arg_types
2241 = skip_artificial_parms_for (fn, fn_arg_types);
2242
2243 /* Function templates cannot be specializations; there are
2244 no partial specializations of functions. Therefore, if
2245 the type of DECL does not match FN, there is no
2246 match.
2247
2248 Note that it should never be the case that we have both
2249 candidates added here, and for regular member functions
2250 below. */
2251 if (tsk == tsk_template)
2252 {
2253 if (compparms (fn_arg_types, decl_arg_types))
2254 candidates = tree_cons (NULL_TREE, fn, candidates);
2255 continue;
2256 }
2257
2258 /* See whether this function might be a specialization of this
2259 template. Suppress access control because we might be trying
2260 to make this specialization a friend, and we have already done
2261 access control for the declaration of the specialization. */
2262 push_deferring_access_checks (dk_no_check);
2263 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2264 pop_deferring_access_checks ();
2265
2266 if (!targs)
2267 /* We cannot deduce template arguments that when used to
2268 specialize TMPL will produce DECL. */
2269 continue;
2270
2271 if (uses_template_parms (targs))
2272 /* We deduced something involving 'auto', which isn't a valid
2273 template argument. */
2274 continue;
2275
2276 /* Remove, from the set of candidates, all those functions
2277 whose constraints are not satisfied. */
2278 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2279 continue;
2280
2281 // Then, try to form the new function type.
2282 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2283 if (insttype == error_mark_node)
2284 continue;
2285 fn_arg_types
2286 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2287 if (!compparms (fn_arg_types, decl_arg_types))
2288 continue;
2289
2290 /* Save this template, and the arguments deduced. */
2291 templates = tree_cons (targs, fn, templates);
2292 }
2293 else if (need_member_template)
2294 /* FN is an ordinary member function, and we need a
2295 specialization of a member template. */
2296 ;
2297 else if (TREE_CODE (fn) != FUNCTION_DECL)
2298 /* We can get IDENTIFIER_NODEs here in certain erroneous
2299 cases. */
2300 ;
2301 else if (!DECL_FUNCTION_MEMBER_P (fn))
2302 /* This is just an ordinary non-member function. Nothing can
2303 be a specialization of that. */
2304 ;
2305 else if (DECL_ARTIFICIAL (fn))
2306 /* Cannot specialize functions that are created implicitly. */
2307 ;
2308 else
2309 {
2310 tree decl_arg_types;
2311
2312 /* This is an ordinary member function. However, since
2313 we're here, we can assume its enclosing class is a
2314 template class. For example,
2315
2316 template <typename T> struct S { void f(); };
2317 template <> void S<int>::f() {}
2318
2319 Here, S<int>::f is a non-template, but S<int> is a
2320 template class. If FN has the same type as DECL, we
2321 might be in business. */
2322
2323 if (!DECL_TEMPLATE_INFO (fn))
2324 /* Its enclosing class is an explicit specialization
2325 of a template class. This is not a candidate. */
2326 continue;
2327
2328 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2329 TREE_TYPE (TREE_TYPE (fn))))
2330 /* The return types differ. */
2331 continue;
2332
2333 /* Adjust the type of DECL in case FN is a static member. */
2334 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2335 if (DECL_STATIC_FUNCTION_P (fn)
2336 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2337 decl_arg_types = TREE_CHAIN (decl_arg_types);
2338
2339 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2340 decl_arg_types))
2341 continue;
2342
2343 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2344 && (type_memfn_rqual (TREE_TYPE (decl))
2345 != type_memfn_rqual (TREE_TYPE (fn))))
2346 continue;
2347
2348 // If the deduced arguments do not satisfy the constraints,
2349 // this is not a candidate.
2350 if (flag_concepts && !constraints_satisfied_p (fn))
2351 continue;
2352
2353 // Add the candidate.
2354 candidates = tree_cons (NULL_TREE, fn, candidates);
2355 }
2356 }
2357
2358 if (templates && TREE_CHAIN (templates))
2359 {
2360 /* We have:
2361
2362 [temp.expl.spec]
2363
2364 It is possible for a specialization with a given function
2365 signature to be instantiated from more than one function
2366 template. In such cases, explicit specification of the
2367 template arguments must be used to uniquely identify the
2368 function template specialization being specialized.
2369
2370 Note that here, there's no suggestion that we're supposed to
2371 determine which of the candidate templates is most
2372 specialized. However, we, also have:
2373
2374 [temp.func.order]
2375
2376 Partial ordering of overloaded function template
2377 declarations is used in the following contexts to select
2378 the function template to which a function template
2379 specialization refers:
2380
2381 -- when an explicit specialization refers to a function
2382 template.
2383
2384 So, we do use the partial ordering rules, at least for now.
2385 This extension can only serve to make invalid programs valid,
2386 so it's safe. And, there is strong anecdotal evidence that
2387 the committee intended the partial ordering rules to apply;
2388 the EDG front end has that behavior, and John Spicer claims
2389 that the committee simply forgot to delete the wording in
2390 [temp.expl.spec]. */
2391 tree tmpl = most_specialized_instantiation (templates);
2392 if (tmpl != error_mark_node)
2393 {
2394 templates = tmpl;
2395 TREE_CHAIN (templates) = NULL_TREE;
2396 }
2397 }
2398
2399 // Concepts allows multiple declarations of member functions
2400 // with the same signature. Like above, we need to rely on
2401 // on the partial ordering of those candidates to determine which
2402 // is the best.
2403 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2404 {
2405 if (tree cand = most_constrained_function (candidates))
2406 {
2407 candidates = cand;
2408 TREE_CHAIN (cand) = NULL_TREE;
2409 }
2410 }
2411
2412 if (templates == NULL_TREE && candidates == NULL_TREE)
2413 {
2414 error ("template-id %qD for %q+D does not match any template "
2415 "declaration", template_id, decl);
2416 if (header_count && header_count != template_count + 1)
2417 inform (DECL_SOURCE_LOCATION (decl),
2418 "saw %d %<template<>%>, need %d for "
2419 "specializing a member function template",
2420 header_count, template_count + 1);
2421 else
2422 print_candidates (orig_fns);
2423 return error_mark_node;
2424 }
2425 else if ((templates && TREE_CHAIN (templates))
2426 || (candidates && TREE_CHAIN (candidates))
2427 || (templates && candidates))
2428 {
2429 error ("ambiguous template specialization %qD for %q+D",
2430 template_id, decl);
2431 candidates = chainon (candidates, templates);
2432 print_candidates (candidates);
2433 return error_mark_node;
2434 }
2435
2436 /* We have one, and exactly one, match. */
2437 if (candidates)
2438 {
2439 tree fn = TREE_VALUE (candidates);
2440 *targs_out = copy_node (DECL_TI_ARGS (fn));
2441
2442 /* Propagate the candidate's constraints to the declaration. */
2443 set_constraints (decl, get_constraints (fn));
2444
2445 /* DECL is a re-declaration or partial instantiation of a template
2446 function. */
2447 if (TREE_CODE (fn) == TEMPLATE_DECL)
2448 return fn;
2449 /* It was a specialization of an ordinary member function in a
2450 template class. */
2451 return DECL_TI_TEMPLATE (fn);
2452 }
2453
2454 /* It was a specialization of a template. */
2455 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2456 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2457 {
2458 *targs_out = copy_node (targs);
2459 SET_TMPL_ARGS_LEVEL (*targs_out,
2460 TMPL_ARGS_DEPTH (*targs_out),
2461 TREE_PURPOSE (templates));
2462 }
2463 else
2464 *targs_out = TREE_PURPOSE (templates);
2465 return TREE_VALUE (templates);
2466 }
2467
2468 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2469 but with the default argument values filled in from those in the
2470 TMPL_TYPES. */
2471
2472 static tree
2473 copy_default_args_to_explicit_spec_1 (tree spec_types,
2474 tree tmpl_types)
2475 {
2476 tree new_spec_types;
2477
2478 if (!spec_types)
2479 return NULL_TREE;
2480
2481 if (spec_types == void_list_node)
2482 return void_list_node;
2483
2484 /* Substitute into the rest of the list. */
2485 new_spec_types =
2486 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2487 TREE_CHAIN (tmpl_types));
2488
2489 /* Add the default argument for this parameter. */
2490 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2491 TREE_VALUE (spec_types),
2492 new_spec_types);
2493 }
2494
2495 /* DECL is an explicit specialization. Replicate default arguments
2496 from the template it specializes. (That way, code like:
2497
2498 template <class T> void f(T = 3);
2499 template <> void f(double);
2500 void g () { f (); }
2501
2502 works, as required.) An alternative approach would be to look up
2503 the correct default arguments at the call-site, but this approach
2504 is consistent with how implicit instantiations are handled. */
2505
2506 static void
2507 copy_default_args_to_explicit_spec (tree decl)
2508 {
2509 tree tmpl;
2510 tree spec_types;
2511 tree tmpl_types;
2512 tree new_spec_types;
2513 tree old_type;
2514 tree new_type;
2515 tree t;
2516 tree object_type = NULL_TREE;
2517 tree in_charge = NULL_TREE;
2518 tree vtt = NULL_TREE;
2519
2520 /* See if there's anything we need to do. */
2521 tmpl = DECL_TI_TEMPLATE (decl);
2522 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2523 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2524 if (TREE_PURPOSE (t))
2525 break;
2526 if (!t)
2527 return;
2528
2529 old_type = TREE_TYPE (decl);
2530 spec_types = TYPE_ARG_TYPES (old_type);
2531
2532 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2533 {
2534 /* Remove the this pointer, but remember the object's type for
2535 CV quals. */
2536 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2537 spec_types = TREE_CHAIN (spec_types);
2538 tmpl_types = TREE_CHAIN (tmpl_types);
2539
2540 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2541 {
2542 /* DECL may contain more parameters than TMPL due to the extra
2543 in-charge parameter in constructors and destructors. */
2544 in_charge = spec_types;
2545 spec_types = TREE_CHAIN (spec_types);
2546 }
2547 if (DECL_HAS_VTT_PARM_P (decl))
2548 {
2549 vtt = spec_types;
2550 spec_types = TREE_CHAIN (spec_types);
2551 }
2552 }
2553
2554 /* Compute the merged default arguments. */
2555 new_spec_types =
2556 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2557
2558 /* Compute the new FUNCTION_TYPE. */
2559 if (object_type)
2560 {
2561 if (vtt)
2562 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2563 TREE_VALUE (vtt),
2564 new_spec_types);
2565
2566 if (in_charge)
2567 /* Put the in-charge parameter back. */
2568 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2569 TREE_VALUE (in_charge),
2570 new_spec_types);
2571
2572 new_type = build_method_type_directly (object_type,
2573 TREE_TYPE (old_type),
2574 new_spec_types);
2575 }
2576 else
2577 new_type = build_function_type (TREE_TYPE (old_type),
2578 new_spec_types);
2579 new_type = cp_build_type_attribute_variant (new_type,
2580 TYPE_ATTRIBUTES (old_type));
2581 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2582
2583 TREE_TYPE (decl) = new_type;
2584 }
2585
2586 /* Return the number of template headers we expect to see for a definition
2587 or specialization of CTYPE or one of its non-template members. */
2588
2589 int
2590 num_template_headers_for_class (tree ctype)
2591 {
2592 int num_templates = 0;
2593
2594 while (ctype && CLASS_TYPE_P (ctype))
2595 {
2596 /* You're supposed to have one `template <...>' for every
2597 template class, but you don't need one for a full
2598 specialization. For example:
2599
2600 template <class T> struct S{};
2601 template <> struct S<int> { void f(); };
2602 void S<int>::f () {}
2603
2604 is correct; there shouldn't be a `template <>' for the
2605 definition of `S<int>::f'. */
2606 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2607 /* If CTYPE does not have template information of any
2608 kind, then it is not a template, nor is it nested
2609 within a template. */
2610 break;
2611 if (explicit_class_specialization_p (ctype))
2612 break;
2613 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2614 ++num_templates;
2615
2616 ctype = TYPE_CONTEXT (ctype);
2617 }
2618
2619 return num_templates;
2620 }
2621
2622 /* Do a simple sanity check on the template headers that precede the
2623 variable declaration DECL. */
2624
2625 void
2626 check_template_variable (tree decl)
2627 {
2628 tree ctx = CP_DECL_CONTEXT (decl);
2629 int wanted = num_template_headers_for_class (ctx);
2630 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2631 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2632 {
2633 if (cxx_dialect < cxx14)
2634 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2635 "variable templates only available with "
2636 "%<-std=c++14%> or %<-std=gnu++14%>");
2637
2638 // Namespace-scope variable templates should have a template header.
2639 ++wanted;
2640 }
2641 if (template_header_count > wanted)
2642 {
2643 auto_diagnostic_group d;
2644 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2645 "too many template headers for %qD "
2646 "(should be %d)",
2647 decl, wanted);
2648 if (warned && CLASS_TYPE_P (ctx)
2649 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2650 inform (DECL_SOURCE_LOCATION (decl),
2651 "members of an explicitly specialized class are defined "
2652 "without a template header");
2653 }
2654 }
2655
2656 /* An explicit specialization whose declarator-id or class-head-name is not
2657 qualified shall be declared in the nearest enclosing namespace of the
2658 template, or, if the namespace is inline (7.3.1), any namespace from its
2659 enclosing namespace set.
2660
2661 If the name declared in the explicit instantiation is an unqualified name,
2662 the explicit instantiation shall appear in the namespace where its template
2663 is declared or, if that namespace is inline (7.3.1), any namespace from its
2664 enclosing namespace set. */
2665
2666 void
2667 check_unqualified_spec_or_inst (tree t, location_t loc)
2668 {
2669 tree tmpl = most_general_template (t);
2670 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2671 && !is_nested_namespace (current_namespace,
2672 CP_DECL_CONTEXT (tmpl), true))
2673 {
2674 if (processing_specialization)
2675 permerror (loc, "explicit specialization of %qD outside its "
2676 "namespace must use a nested-name-specifier", tmpl);
2677 else if (processing_explicit_instantiation
2678 && cxx_dialect >= cxx11)
2679 /* This was allowed in C++98, so only pedwarn. */
2680 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2681 "outside its namespace must use a nested-name-"
2682 "specifier", tmpl);
2683 }
2684 }
2685
2686 /* Warn for a template specialization SPEC that is missing some of a set
2687 of function or type attributes that the template TEMPL is declared with.
2688 ATTRLIST is a list of additional attributes that SPEC should be taken
2689 to ultimately be declared with. */
2690
2691 static void
2692 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2693 {
2694 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2695 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2696
2697 /* Avoid warning if the difference between the primary and
2698 the specialization is not in one of the attributes below. */
2699 const char* const blacklist[] = {
2700 "alloc_align", "alloc_size", "assume_aligned", "format",
2701 "format_arg", "malloc", "nonnull", NULL
2702 };
2703
2704 /* Put together a list of the black listed attributes that the primary
2705 template is declared with that the specialization is not, in case
2706 it's not apparent from the most recent declaration of the primary. */
2707 pretty_printer str;
2708 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2709 blacklist, &str);
2710
2711 if (!nattrs)
2712 return;
2713
2714 auto_diagnostic_group d;
2715 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2716 "explicit specialization %q#D may be missing attributes",
2717 spec))
2718 inform (DECL_SOURCE_LOCATION (tmpl),
2719 nattrs > 1
2720 ? G_("missing primary template attributes %s")
2721 : G_("missing primary template attribute %s"),
2722 pp_formatted_text (&str));
2723 }
2724
2725 /* Check to see if the function just declared, as indicated in
2726 DECLARATOR, and in DECL, is a specialization of a function
2727 template. We may also discover that the declaration is an explicit
2728 instantiation at this point.
2729
2730 Returns DECL, or an equivalent declaration that should be used
2731 instead if all goes well. Issues an error message if something is
2732 amiss. Returns error_mark_node if the error is not easily
2733 recoverable.
2734
2735 FLAGS is a bitmask consisting of the following flags:
2736
2737 2: The function has a definition.
2738 4: The function is a friend.
2739
2740 The TEMPLATE_COUNT is the number of references to qualifying
2741 template classes that appeared in the name of the function. For
2742 example, in
2743
2744 template <class T> struct S { void f(); };
2745 void S<int>::f();
2746
2747 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2748 classes are not counted in the TEMPLATE_COUNT, so that in
2749
2750 template <class T> struct S {};
2751 template <> struct S<int> { void f(); }
2752 template <> void S<int>::f();
2753
2754 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2755 invalid; there should be no template <>.)
2756
2757 If the function is a specialization, it is marked as such via
2758 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2759 is set up correctly, and it is added to the list of specializations
2760 for that template. */
2761
2762 tree
2763 check_explicit_specialization (tree declarator,
2764 tree decl,
2765 int template_count,
2766 int flags,
2767 tree attrlist)
2768 {
2769 int have_def = flags & 2;
2770 int is_friend = flags & 4;
2771 bool is_concept = flags & 8;
2772 int specialization = 0;
2773 int explicit_instantiation = 0;
2774 int member_specialization = 0;
2775 tree ctype = DECL_CLASS_CONTEXT (decl);
2776 tree dname = DECL_NAME (decl);
2777 tmpl_spec_kind tsk;
2778
2779 if (is_friend)
2780 {
2781 if (!processing_specialization)
2782 tsk = tsk_none;
2783 else
2784 tsk = tsk_excessive_parms;
2785 }
2786 else
2787 tsk = current_tmpl_spec_kind (template_count);
2788
2789 switch (tsk)
2790 {
2791 case tsk_none:
2792 if (processing_specialization && !VAR_P (decl))
2793 {
2794 specialization = 1;
2795 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2796 }
2797 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2798 {
2799 if (is_friend)
2800 /* This could be something like:
2801
2802 template <class T> void f(T);
2803 class S { friend void f<>(int); } */
2804 specialization = 1;
2805 else
2806 {
2807 /* This case handles bogus declarations like template <>
2808 template <class T> void f<int>(); */
2809
2810 error_at (cp_expr_loc_or_input_loc (declarator),
2811 "template-id %qE in declaration of primary template",
2812 declarator);
2813 return decl;
2814 }
2815 }
2816 break;
2817
2818 case tsk_invalid_member_spec:
2819 /* The error has already been reported in
2820 check_specialization_scope. */
2821 return error_mark_node;
2822
2823 case tsk_invalid_expl_inst:
2824 error ("template parameter list used in explicit instantiation");
2825
2826 /* Fall through. */
2827
2828 case tsk_expl_inst:
2829 if (have_def)
2830 error ("definition provided for explicit instantiation");
2831
2832 explicit_instantiation = 1;
2833 break;
2834
2835 case tsk_excessive_parms:
2836 case tsk_insufficient_parms:
2837 if (tsk == tsk_excessive_parms)
2838 error ("too many template parameter lists in declaration of %qD",
2839 decl);
2840 else if (template_header_count)
2841 error("too few template parameter lists in declaration of %qD", decl);
2842 else
2843 error("explicit specialization of %qD must be introduced by "
2844 "%<template <>%>", decl);
2845
2846 /* Fall through. */
2847 case tsk_expl_spec:
2848 if (is_concept)
2849 error ("explicit specialization declared %<concept%>");
2850
2851 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2852 /* In cases like template<> constexpr bool v = true;
2853 We'll give an error in check_template_variable. */
2854 break;
2855
2856 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2857 if (ctype)
2858 member_specialization = 1;
2859 else
2860 specialization = 1;
2861 break;
2862
2863 case tsk_template:
2864 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2865 {
2866 /* This case handles bogus declarations like template <>
2867 template <class T> void f<int>(); */
2868
2869 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2870 error_at (cp_expr_loc_or_input_loc (declarator),
2871 "template-id %qE in declaration of primary template",
2872 declarator);
2873 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2874 {
2875 /* Partial specialization of variable template. */
2876 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2877 specialization = 1;
2878 goto ok;
2879 }
2880 else if (cxx_dialect < cxx14)
2881 error_at (cp_expr_loc_or_input_loc (declarator),
2882 "non-type partial specialization %qE "
2883 "is not allowed", declarator);
2884 else
2885 error_at (cp_expr_loc_or_input_loc (declarator),
2886 "non-class, non-variable partial specialization %qE "
2887 "is not allowed", declarator);
2888 return decl;
2889 ok:;
2890 }
2891
2892 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2893 /* This is a specialization of a member template, without
2894 specialization the containing class. Something like:
2895
2896 template <class T> struct S {
2897 template <class U> void f (U);
2898 };
2899 template <> template <class U> void S<int>::f(U) {}
2900
2901 That's a specialization -- but of the entire template. */
2902 specialization = 1;
2903 break;
2904
2905 default:
2906 gcc_unreachable ();
2907 }
2908
2909 if ((specialization || member_specialization)
2910 /* This doesn't apply to variable templates. */
2911 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2912 {
2913 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2914 for (; t; t = TREE_CHAIN (t))
2915 if (TREE_PURPOSE (t))
2916 {
2917 permerror (input_location,
2918 "default argument specified in explicit specialization");
2919 break;
2920 }
2921 }
2922
2923 if (specialization || member_specialization || explicit_instantiation)
2924 {
2925 tree tmpl = NULL_TREE;
2926 tree targs = NULL_TREE;
2927 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2928
2929 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2930 if (!was_template_id)
2931 {
2932 tree fns;
2933
2934 gcc_assert (identifier_p (declarator));
2935 if (ctype)
2936 fns = dname;
2937 else
2938 {
2939 /* If there is no class context, the explicit instantiation
2940 must be at namespace scope. */
2941 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2942
2943 /* Find the namespace binding, using the declaration
2944 context. */
2945 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2946 false, true);
2947 if (fns == error_mark_node)
2948 /* If lookup fails, look for a friend declaration so we can
2949 give a better diagnostic. */
2950 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2951 /*type*/false, /*complain*/true,
2952 /*hidden*/true);
2953
2954 if (fns == error_mark_node || !is_overloaded_fn (fns))
2955 {
2956 error ("%qD is not a template function", dname);
2957 fns = error_mark_node;
2958 }
2959 }
2960
2961 declarator = lookup_template_function (fns, NULL_TREE);
2962 }
2963
2964 if (declarator == error_mark_node)
2965 return error_mark_node;
2966
2967 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2968 {
2969 if (!explicit_instantiation)
2970 /* A specialization in class scope. This is invalid,
2971 but the error will already have been flagged by
2972 check_specialization_scope. */
2973 return error_mark_node;
2974 else
2975 {
2976 /* It's not valid to write an explicit instantiation in
2977 class scope, e.g.:
2978
2979 class C { template void f(); }
2980
2981 This case is caught by the parser. However, on
2982 something like:
2983
2984 template class C { void f(); };
2985
2986 (which is invalid) we can get here. The error will be
2987 issued later. */
2988 ;
2989 }
2990
2991 return decl;
2992 }
2993 else if (ctype != NULL_TREE
2994 && (identifier_p (TREE_OPERAND (declarator, 0))))
2995 {
2996 // We'll match variable templates in start_decl.
2997 if (VAR_P (decl))
2998 return decl;
2999
3000 /* Find the list of functions in ctype that have the same
3001 name as the declared function. */
3002 tree name = TREE_OPERAND (declarator, 0);
3003
3004 if (constructor_name_p (name, ctype))
3005 {
3006 if (DECL_CONSTRUCTOR_P (decl)
3007 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3008 : !CLASSTYPE_DESTRUCTOR (ctype))
3009 {
3010 /* From [temp.expl.spec]:
3011
3012 If such an explicit specialization for the member
3013 of a class template names an implicitly-declared
3014 special member function (clause _special_), the
3015 program is ill-formed.
3016
3017 Similar language is found in [temp.explicit]. */
3018 error ("specialization of implicitly-declared special member function");
3019 return error_mark_node;
3020 }
3021
3022 name = DECL_NAME (decl);
3023 }
3024
3025 /* For a type-conversion operator, We might be looking for
3026 `operator int' which will be a specialization of
3027 `operator T'. Grab all the conversion operators, and
3028 then select from them. */
3029 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3030 ? conv_op_identifier : name);
3031
3032 if (fns == NULL_TREE)
3033 {
3034 error ("no member function %qD declared in %qT", name, ctype);
3035 return error_mark_node;
3036 }
3037 else
3038 TREE_OPERAND (declarator, 0) = fns;
3039 }
3040
3041 /* Figure out what exactly is being specialized at this point.
3042 Note that for an explicit instantiation, even one for a
3043 member function, we cannot tell a priori whether the
3044 instantiation is for a member template, or just a member
3045 function of a template class. Even if a member template is
3046 being instantiated, the member template arguments may be
3047 elided if they can be deduced from the rest of the
3048 declaration. */
3049 tmpl = determine_specialization (declarator, decl,
3050 &targs,
3051 member_specialization,
3052 template_count,
3053 tsk);
3054
3055 if (!tmpl || tmpl == error_mark_node)
3056 /* We couldn't figure out what this declaration was
3057 specializing. */
3058 return error_mark_node;
3059 else
3060 {
3061 if (TREE_CODE (decl) == FUNCTION_DECL
3062 && DECL_HIDDEN_FRIEND_P (tmpl))
3063 {
3064 auto_diagnostic_group d;
3065 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3066 "friend declaration %qD is not visible to "
3067 "explicit specialization", tmpl))
3068 inform (DECL_SOURCE_LOCATION (tmpl),
3069 "friend declaration here");
3070 }
3071 else if (!ctype && !is_friend
3072 && CP_DECL_CONTEXT (decl) == current_namespace)
3073 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3074
3075 tree gen_tmpl = most_general_template (tmpl);
3076
3077 if (explicit_instantiation)
3078 {
3079 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3080 is done by do_decl_instantiation later. */
3081
3082 int arg_depth = TMPL_ARGS_DEPTH (targs);
3083 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3084
3085 if (arg_depth > parm_depth)
3086 {
3087 /* If TMPL is not the most general template (for
3088 example, if TMPL is a friend template that is
3089 injected into namespace scope), then there will
3090 be too many levels of TARGS. Remove some of them
3091 here. */
3092 int i;
3093 tree new_targs;
3094
3095 new_targs = make_tree_vec (parm_depth);
3096 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3097 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3098 = TREE_VEC_ELT (targs, i);
3099 targs = new_targs;
3100 }
3101
3102 return instantiate_template (tmpl, targs, tf_error);
3103 }
3104
3105 /* If we thought that the DECL was a member function, but it
3106 turns out to be specializing a static member function,
3107 make DECL a static member function as well. */
3108 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3109 && DECL_STATIC_FUNCTION_P (tmpl)
3110 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3111 revert_static_member_fn (decl);
3112
3113 /* If this is a specialization of a member template of a
3114 template class, we want to return the TEMPLATE_DECL, not
3115 the specialization of it. */
3116 if (tsk == tsk_template && !was_template_id)
3117 {
3118 tree result = DECL_TEMPLATE_RESULT (tmpl);
3119 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3120 DECL_INITIAL (result) = NULL_TREE;
3121 if (have_def)
3122 {
3123 tree parm;
3124 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3125 DECL_SOURCE_LOCATION (result)
3126 = DECL_SOURCE_LOCATION (decl);
3127 /* We want to use the argument list specified in the
3128 definition, not in the original declaration. */
3129 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3130 for (parm = DECL_ARGUMENTS (result); parm;
3131 parm = DECL_CHAIN (parm))
3132 DECL_CONTEXT (parm) = result;
3133 }
3134 return register_specialization (tmpl, gen_tmpl, targs,
3135 is_friend, 0);
3136 }
3137
3138 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3139 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3140
3141 if (was_template_id)
3142 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3143
3144 /* Inherit default function arguments from the template
3145 DECL is specializing. */
3146 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3147 copy_default_args_to_explicit_spec (decl);
3148
3149 /* This specialization has the same protection as the
3150 template it specializes. */
3151 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3152 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3153
3154 /* 7.1.1-1 [dcl.stc]
3155
3156 A storage-class-specifier shall not be specified in an
3157 explicit specialization...
3158
3159 The parser rejects these, so unless action is taken here,
3160 explicit function specializations will always appear with
3161 global linkage.
3162
3163 The action recommended by the C++ CWG in response to C++
3164 defect report 605 is to make the storage class and linkage
3165 of the explicit specialization match the templated function:
3166
3167 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3168 */
3169 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3170 {
3171 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3172 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3173
3174 /* A concept cannot be specialized. */
3175 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3176 {
3177 error ("explicit specialization of function concept %qD",
3178 gen_tmpl);
3179 return error_mark_node;
3180 }
3181
3182 /* This specialization has the same linkage and visibility as
3183 the function template it specializes. */
3184 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3185 if (! TREE_PUBLIC (decl))
3186 {
3187 DECL_INTERFACE_KNOWN (decl) = 1;
3188 DECL_NOT_REALLY_EXTERN (decl) = 1;
3189 }
3190 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3191 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3192 {
3193 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3194 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3195 }
3196 }
3197
3198 /* If DECL is a friend declaration, declared using an
3199 unqualified name, the namespace associated with DECL may
3200 have been set incorrectly. For example, in:
3201
3202 template <typename T> void f(T);
3203 namespace N {
3204 struct S { friend void f<int>(int); }
3205 }
3206
3207 we will have set the DECL_CONTEXT for the friend
3208 declaration to N, rather than to the global namespace. */
3209 if (DECL_NAMESPACE_SCOPE_P (decl))
3210 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3211
3212 if (is_friend && !have_def)
3213 /* This is not really a declaration of a specialization.
3214 It's just the name of an instantiation. But, it's not
3215 a request for an instantiation, either. */
3216 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3217 else if (TREE_CODE (decl) == FUNCTION_DECL)
3218 /* A specialization is not necessarily COMDAT. */
3219 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3220 && DECL_DECLARED_INLINE_P (decl));
3221 else if (VAR_P (decl))
3222 DECL_COMDAT (decl) = false;
3223
3224 /* If this is a full specialization, register it so that we can find
3225 it again. Partial specializations will be registered in
3226 process_partial_specialization. */
3227 if (!processing_template_decl)
3228 {
3229 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3230
3231 decl = register_specialization (decl, gen_tmpl, targs,
3232 is_friend, 0);
3233 }
3234
3235
3236 /* A 'structor should already have clones. */
3237 gcc_assert (decl == error_mark_node
3238 || variable_template_p (tmpl)
3239 || !(DECL_CONSTRUCTOR_P (decl)
3240 || DECL_DESTRUCTOR_P (decl))
3241 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3242 }
3243 }
3244
3245 return decl;
3246 }
3247
3248 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3249 parameters. These are represented in the same format used for
3250 DECL_TEMPLATE_PARMS. */
3251
3252 int
3253 comp_template_parms (const_tree parms1, const_tree parms2)
3254 {
3255 const_tree p1;
3256 const_tree p2;
3257
3258 if (parms1 == parms2)
3259 return 1;
3260
3261 for (p1 = parms1, p2 = parms2;
3262 p1 != NULL_TREE && p2 != NULL_TREE;
3263 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3264 {
3265 tree t1 = TREE_VALUE (p1);
3266 tree t2 = TREE_VALUE (p2);
3267 int i;
3268
3269 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3270 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3271
3272 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3273 return 0;
3274
3275 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3276 {
3277 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3278 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3279
3280 /* If either of the template parameters are invalid, assume
3281 they match for the sake of error recovery. */
3282 if (error_operand_p (parm1) || error_operand_p (parm2))
3283 return 1;
3284
3285 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3286 return 0;
3287
3288 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3289 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3290 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3291 continue;
3292 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3293 return 0;
3294 }
3295 }
3296
3297 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3298 /* One set of parameters has more parameters lists than the
3299 other. */
3300 return 0;
3301
3302 return 1;
3303 }
3304
3305 /* Returns true if two template parameters are declared with
3306 equivalent constraints. */
3307
3308 static bool
3309 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3310 {
3311 tree req1 = TREE_TYPE (parm1);
3312 tree req2 = TREE_TYPE (parm2);
3313 if (!req1 != !req2)
3314 return false;
3315 if (req1)
3316 return cp_tree_equal (req1, req2);
3317 return true;
3318 }
3319
3320 /* Returns true when two template parameters are equivalent. */
3321
3322 static bool
3323 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3324 {
3325 tree decl1 = TREE_VALUE (parm1);
3326 tree decl2 = TREE_VALUE (parm2);
3327
3328 /* If either of the template parameters are invalid, assume
3329 they match for the sake of error recovery. */
3330 if (error_operand_p (decl1) || error_operand_p (decl2))
3331 return true;
3332
3333 /* ... they declare parameters of the same kind. */
3334 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3335 return false;
3336
3337 /* ... one parameter was introduced by a parameter declaration, then
3338 both are. This case arises as a result of eagerly rewriting declarations
3339 during parsing. */
3340 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3341 return false;
3342
3343 /* ... if either declares a pack, they both do. */
3344 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3345 return false;
3346
3347 if (TREE_CODE (decl1) == PARM_DECL)
3348 {
3349 /* ... if they declare non-type parameters, the types are equivalent. */
3350 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3351 return false;
3352 }
3353 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3354 {
3355 /* ... if they declare template template parameters, their template
3356 parameter lists are equivalent. */
3357 if (!template_heads_equivalent_p (decl1, decl2))
3358 return false;
3359 }
3360
3361 /* ... if they are declared with a qualified-concept name, they both
3362 are, and those names are equivalent. */
3363 return template_parameter_constraints_equivalent_p (parm1, parm2);
3364 }
3365
3366 /* Returns true if two template parameters lists are equivalent.
3367 Two template parameter lists are equivalent if they have the
3368 same length and their corresponding parameters are equivalent.
3369
3370 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3371 data structure returned by DECL_TEMPLATE_PARMS.
3372
3373 This is generally the same implementation as comp_template_parms
3374 except that it also the concept names and arguments used to
3375 introduce parameters. */
3376
3377 static bool
3378 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3379 {
3380 if (parms1 == parms2)
3381 return true;
3382
3383 const_tree p1 = parms1;
3384 const_tree p2 = parms2;
3385 while (p1 != NULL_TREE && p2 != NULL_TREE)
3386 {
3387 tree list1 = TREE_VALUE (p1);
3388 tree list2 = TREE_VALUE (p2);
3389
3390 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3391 return 0;
3392
3393 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3394 {
3395 tree parm1 = TREE_VEC_ELT (list1, i);
3396 tree parm2 = TREE_VEC_ELT (list2, i);
3397 if (!template_parameters_equivalent_p (parm1, parm2))
3398 return false;
3399 }
3400
3401 p1 = TREE_CHAIN (p1);
3402 p2 = TREE_CHAIN (p2);
3403 }
3404
3405 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3406 return false;
3407
3408 return true;
3409 }
3410
3411 /* Return true if the requires-clause of the template parameter lists are
3412 equivalent and false otherwise. */
3413 static bool
3414 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3415 {
3416 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3417 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3418 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3419 return false;
3420 if (!cp_tree_equal (req1, req2))
3421 return false;
3422 return true;
3423 }
3424
3425 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3426 Two template heads are equivalent if their template parameter
3427 lists are equivalent and their requires clauses are equivalent.
3428
3429 In pre-C++20, this is equivalent to calling comp_template_parms
3430 for the template parameters of TMPL1 and TMPL2. */
3431
3432 bool
3433 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3434 {
3435 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3436 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3437
3438 /* Don't change the matching rules for pre-C++20. */
3439 if (cxx_dialect < cxx2a)
3440 return comp_template_parms (parms1, parms2);
3441
3442 /* ... have the same number of template parameters, and their
3443 corresponding parameters are equivalent. */
3444 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3445 return false;
3446
3447 /* ... if either has a requires-clause, they both do and their
3448 corresponding constraint-expressions are equivalent. */
3449 return template_requirements_equivalent_p (parms1, parms2);
3450 }
3451
3452 /* Determine whether PARM is a parameter pack. */
3453
3454 bool
3455 template_parameter_pack_p (const_tree parm)
3456 {
3457 /* Determine if we have a non-type template parameter pack. */
3458 if (TREE_CODE (parm) == PARM_DECL)
3459 return (DECL_TEMPLATE_PARM_P (parm)
3460 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3461 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3462 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3463
3464 /* If this is a list of template parameters, we could get a
3465 TYPE_DECL or a TEMPLATE_DECL. */
3466 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3467 parm = TREE_TYPE (parm);
3468
3469 /* Otherwise it must be a type template parameter. */
3470 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3471 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3472 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3473 }
3474
3475 /* Determine if T is a function parameter pack. */
3476
3477 bool
3478 function_parameter_pack_p (const_tree t)
3479 {
3480 if (t && TREE_CODE (t) == PARM_DECL)
3481 return DECL_PACK_P (t);
3482 return false;
3483 }
3484
3485 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3486 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3487
3488 tree
3489 get_function_template_decl (const_tree primary_func_tmpl_inst)
3490 {
3491 if (! primary_func_tmpl_inst
3492 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3493 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3494 return NULL;
3495
3496 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3497 }
3498
3499 /* Return true iff the function parameter PARAM_DECL was expanded
3500 from the function parameter pack PACK. */
3501
3502 bool
3503 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3504 {
3505 if (DECL_ARTIFICIAL (param_decl)
3506 || !function_parameter_pack_p (pack))
3507 return false;
3508
3509 /* The parameter pack and its pack arguments have the same
3510 DECL_PARM_INDEX. */
3511 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3512 }
3513
3514 /* Determine whether ARGS describes a variadic template args list,
3515 i.e., one that is terminated by a template argument pack. */
3516
3517 static bool
3518 template_args_variadic_p (tree args)
3519 {
3520 int nargs;
3521 tree last_parm;
3522
3523 if (args == NULL_TREE)
3524 return false;
3525
3526 args = INNERMOST_TEMPLATE_ARGS (args);
3527 nargs = TREE_VEC_LENGTH (args);
3528
3529 if (nargs == 0)
3530 return false;
3531
3532 last_parm = TREE_VEC_ELT (args, nargs - 1);
3533
3534 return ARGUMENT_PACK_P (last_parm);
3535 }
3536
3537 /* Generate a new name for the parameter pack name NAME (an
3538 IDENTIFIER_NODE) that incorporates its */
3539
3540 static tree
3541 make_ith_pack_parameter_name (tree name, int i)
3542 {
3543 /* Munge the name to include the parameter index. */
3544 #define NUMBUF_LEN 128
3545 char numbuf[NUMBUF_LEN];
3546 char* newname;
3547 int newname_len;
3548
3549 if (name == NULL_TREE)
3550 return name;
3551 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3552 newname_len = IDENTIFIER_LENGTH (name)
3553 + strlen (numbuf) + 2;
3554 newname = (char*)alloca (newname_len);
3555 snprintf (newname, newname_len,
3556 "%s#%i", IDENTIFIER_POINTER (name), i);
3557 return get_identifier (newname);
3558 }
3559
3560 /* Return true if T is a primary function, class or alias template
3561 specialization, not including the template pattern. */
3562
3563 bool
3564 primary_template_specialization_p (const_tree t)
3565 {
3566 if (!t)
3567 return false;
3568
3569 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3570 return (DECL_LANG_SPECIFIC (t)
3571 && DECL_USE_TEMPLATE (t)
3572 && DECL_TEMPLATE_INFO (t)
3573 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3574 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3575 return (CLASSTYPE_TEMPLATE_INFO (t)
3576 && CLASSTYPE_USE_TEMPLATE (t)
3577 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3578 else if (alias_template_specialization_p (t))
3579 return true;
3580 return false;
3581 }
3582
3583 /* Return true if PARM is a template template parameter. */
3584
3585 bool
3586 template_template_parameter_p (const_tree parm)
3587 {
3588 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3589 }
3590
3591 /* Return true iff PARM is a DECL representing a type template
3592 parameter. */
3593
3594 bool
3595 template_type_parameter_p (const_tree parm)
3596 {
3597 return (parm
3598 && (TREE_CODE (parm) == TYPE_DECL
3599 || TREE_CODE (parm) == TEMPLATE_DECL)
3600 && DECL_TEMPLATE_PARM_P (parm));
3601 }
3602
3603 /* Return the template parameters of T if T is a
3604 primary template instantiation, NULL otherwise. */
3605
3606 tree
3607 get_primary_template_innermost_parameters (const_tree t)
3608 {
3609 tree parms = NULL, template_info = NULL;
3610
3611 if ((template_info = get_template_info (t))
3612 && primary_template_specialization_p (t))
3613 parms = INNERMOST_TEMPLATE_PARMS
3614 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3615
3616 return parms;
3617 }
3618
3619 /* Return the template parameters of the LEVELth level from the full list
3620 of template parameters PARMS. */
3621
3622 tree
3623 get_template_parms_at_level (tree parms, int level)
3624 {
3625 tree p;
3626 if (!parms
3627 || TREE_CODE (parms) != TREE_LIST
3628 || level > TMPL_PARMS_DEPTH (parms))
3629 return NULL_TREE;
3630
3631 for (p = parms; p; p = TREE_CHAIN (p))
3632 if (TMPL_PARMS_DEPTH (p) == level)
3633 return p;
3634
3635 return NULL_TREE;
3636 }
3637
3638 /* Returns the template arguments of T if T is a template instantiation,
3639 NULL otherwise. */
3640
3641 tree
3642 get_template_innermost_arguments (const_tree t)
3643 {
3644 tree args = NULL, template_info = NULL;
3645
3646 if ((template_info = get_template_info (t))
3647 && TI_ARGS (template_info))
3648 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3649
3650 return args;
3651 }
3652
3653 /* Return the argument pack elements of T if T is a template argument pack,
3654 NULL otherwise. */
3655
3656 tree
3657 get_template_argument_pack_elems (const_tree t)
3658 {
3659 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3660 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3661 return NULL;
3662
3663 return ARGUMENT_PACK_ARGS (t);
3664 }
3665
3666 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3667 ARGUMENT_PACK_SELECT represents. */
3668
3669 static tree
3670 argument_pack_select_arg (tree t)
3671 {
3672 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3673 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3674
3675 /* If the selected argument is an expansion E, that most likely means we were
3676 called from gen_elem_of_pack_expansion_instantiation during the
3677 substituting of an argument pack (of which the Ith element is a pack
3678 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3679 In this case, the Ith element resulting from this substituting is going to
3680 be a pack expansion, which pattern is the pattern of E. Let's return the
3681 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3682 resulting pack expansion from it. */
3683 if (PACK_EXPANSION_P (arg))
3684 {
3685 /* Make sure we aren't throwing away arg info. */
3686 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3687 arg = PACK_EXPANSION_PATTERN (arg);
3688 }
3689
3690 return arg;
3691 }
3692
3693
3694 /* True iff FN is a function representing a built-in variadic parameter
3695 pack. */
3696
3697 bool
3698 builtin_pack_fn_p (tree fn)
3699 {
3700 if (!fn
3701 || TREE_CODE (fn) != FUNCTION_DECL
3702 || !DECL_IS_BUILTIN (fn))
3703 return false;
3704
3705 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3706 return true;
3707
3708 return false;
3709 }
3710
3711 /* True iff CALL is a call to a function representing a built-in variadic
3712 parameter pack. */
3713
3714 static bool
3715 builtin_pack_call_p (tree call)
3716 {
3717 if (TREE_CODE (call) != CALL_EXPR)
3718 return false;
3719 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3720 }
3721
3722 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3723
3724 static tree
3725 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3726 tree in_decl)
3727 {
3728 tree ohi = CALL_EXPR_ARG (call, 0);
3729 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3730 false/*fn*/, true/*int_cst*/);
3731
3732 if (value_dependent_expression_p (hi))
3733 {
3734 if (hi != ohi)
3735 {
3736 call = copy_node (call);
3737 CALL_EXPR_ARG (call, 0) = hi;
3738 }
3739 tree ex = make_pack_expansion (call, complain);
3740 tree vec = make_tree_vec (1);
3741 TREE_VEC_ELT (vec, 0) = ex;
3742 return vec;
3743 }
3744 else
3745 {
3746 hi = cxx_constant_value (hi);
3747 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3748
3749 /* Calculate the largest value of len that won't make the size of the vec
3750 overflow an int. The compiler will exceed resource limits long before
3751 this, but it seems a decent place to diagnose. */
3752 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3753
3754 if (len < 0 || len > max)
3755 {
3756 if ((complain & tf_error)
3757 && hi != error_mark_node)
3758 error ("argument to %<__integer_pack%> must be between 0 and %d",
3759 max);
3760 return error_mark_node;
3761 }
3762
3763 tree vec = make_tree_vec (len);
3764
3765 for (int i = 0; i < len; ++i)
3766 TREE_VEC_ELT (vec, i) = size_int (i);
3767
3768 return vec;
3769 }
3770 }
3771
3772 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3773 CALL. */
3774
3775 static tree
3776 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3777 tree in_decl)
3778 {
3779 if (!builtin_pack_call_p (call))
3780 return NULL_TREE;
3781
3782 tree fn = CALL_EXPR_FN (call);
3783
3784 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3785 return expand_integer_pack (call, args, complain, in_decl);
3786
3787 return NULL_TREE;
3788 }
3789
3790 /* Structure used to track the progress of find_parameter_packs_r. */
3791 struct find_parameter_pack_data
3792 {
3793 /* TREE_LIST that will contain all of the parameter packs found by
3794 the traversal. */
3795 tree* parameter_packs;
3796
3797 /* Set of AST nodes that have been visited by the traversal. */
3798 hash_set<tree> *visited;
3799
3800 /* True iff we're making a type pack expansion. */
3801 bool type_pack_expansion_p;
3802 };
3803
3804 /* Identifies all of the argument packs that occur in a template
3805 argument and appends them to the TREE_LIST inside DATA, which is a
3806 find_parameter_pack_data structure. This is a subroutine of
3807 make_pack_expansion and uses_parameter_packs. */
3808 static tree
3809 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3810 {
3811 tree t = *tp;
3812 struct find_parameter_pack_data* ppd =
3813 (struct find_parameter_pack_data*)data;
3814 bool parameter_pack_p = false;
3815
3816 /* Handle type aliases/typedefs. */
3817 if (TYPE_ALIAS_P (t))
3818 {
3819 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3820 cp_walk_tree (&TI_ARGS (tinfo),
3821 &find_parameter_packs_r,
3822 ppd, ppd->visited);
3823 *walk_subtrees = 0;
3824 return NULL_TREE;
3825 }
3826
3827 /* Identify whether this is a parameter pack or not. */
3828 switch (TREE_CODE (t))
3829 {
3830 case TEMPLATE_PARM_INDEX:
3831 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3832 parameter_pack_p = true;
3833 break;
3834
3835 case TEMPLATE_TYPE_PARM:
3836 t = TYPE_MAIN_VARIANT (t);
3837 /* FALLTHRU */
3838 case TEMPLATE_TEMPLATE_PARM:
3839 /* If the placeholder appears in the decl-specifier-seq of a function
3840 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3841 is a pack expansion, the invented template parameter is a template
3842 parameter pack. */
3843 if (ppd->type_pack_expansion_p && is_auto (t))
3844 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3845 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3846 parameter_pack_p = true;
3847 break;
3848
3849 case FIELD_DECL:
3850 case PARM_DECL:
3851 if (DECL_PACK_P (t))
3852 {
3853 /* We don't want to walk into the type of a PARM_DECL,
3854 because we don't want to see the type parameter pack. */
3855 *walk_subtrees = 0;
3856 parameter_pack_p = true;
3857 }
3858 break;
3859
3860 case VAR_DECL:
3861 if (DECL_PACK_P (t))
3862 {
3863 /* We don't want to walk into the type of a variadic capture proxy,
3864 because we don't want to see the type parameter pack. */
3865 *walk_subtrees = 0;
3866 parameter_pack_p = true;
3867 }
3868 else if (variable_template_specialization_p (t))
3869 {
3870 cp_walk_tree (&DECL_TI_ARGS (t),
3871 find_parameter_packs_r,
3872 ppd, ppd->visited);
3873 *walk_subtrees = 0;
3874 }
3875 break;
3876
3877 case CALL_EXPR:
3878 if (builtin_pack_call_p (t))
3879 parameter_pack_p = true;
3880 break;
3881
3882 case BASES:
3883 parameter_pack_p = true;
3884 break;
3885 default:
3886 /* Not a parameter pack. */
3887 break;
3888 }
3889
3890 if (parameter_pack_p)
3891 {
3892 /* Add this parameter pack to the list. */
3893 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3894 }
3895
3896 if (TYPE_P (t))
3897 cp_walk_tree (&TYPE_CONTEXT (t),
3898 &find_parameter_packs_r, ppd, ppd->visited);
3899
3900 /* This switch statement will return immediately if we don't find a
3901 parameter pack. */
3902 switch (TREE_CODE (t))
3903 {
3904 case TEMPLATE_PARM_INDEX:
3905 return NULL_TREE;
3906
3907 case BOUND_TEMPLATE_TEMPLATE_PARM:
3908 /* Check the template itself. */
3909 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3910 &find_parameter_packs_r, ppd, ppd->visited);
3911 /* Check the template arguments. */
3912 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3913 ppd->visited);
3914 *walk_subtrees = 0;
3915 return NULL_TREE;
3916
3917 case TEMPLATE_TYPE_PARM:
3918 case TEMPLATE_TEMPLATE_PARM:
3919 return NULL_TREE;
3920
3921 case PARM_DECL:
3922 return NULL_TREE;
3923
3924 case DECL_EXPR:
3925 /* Ignore the declaration of a capture proxy for a parameter pack. */
3926 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3927 *walk_subtrees = 0;
3928 return NULL_TREE;
3929
3930 case RECORD_TYPE:
3931 if (TYPE_PTRMEMFUNC_P (t))
3932 return NULL_TREE;
3933 /* Fall through. */
3934
3935 case UNION_TYPE:
3936 case ENUMERAL_TYPE:
3937 if (TYPE_TEMPLATE_INFO (t))
3938 cp_walk_tree (&TYPE_TI_ARGS (t),
3939 &find_parameter_packs_r, ppd, ppd->visited);
3940
3941 *walk_subtrees = 0;
3942 return NULL_TREE;
3943
3944 case TEMPLATE_DECL:
3945 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3946 return NULL_TREE;
3947 gcc_fallthrough();
3948
3949 case CONSTRUCTOR:
3950 cp_walk_tree (&TREE_TYPE (t),
3951 &find_parameter_packs_r, ppd, ppd->visited);
3952 return NULL_TREE;
3953
3954 case TYPENAME_TYPE:
3955 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3956 ppd, ppd->visited);
3957 *walk_subtrees = 0;
3958 return NULL_TREE;
3959
3960 case TYPE_PACK_EXPANSION:
3961 case EXPR_PACK_EXPANSION:
3962 *walk_subtrees = 0;
3963 return NULL_TREE;
3964
3965 case INTEGER_TYPE:
3966 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3967 ppd, ppd->visited);
3968 *walk_subtrees = 0;
3969 return NULL_TREE;
3970
3971 case IDENTIFIER_NODE:
3972 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3973 ppd->visited);
3974 *walk_subtrees = 0;
3975 return NULL_TREE;
3976
3977 case LAMBDA_EXPR:
3978 {
3979 /* Look at explicit captures. */
3980 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3981 cap; cap = TREE_CHAIN (cap))
3982 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3983 ppd->visited);
3984 /* Since we defer implicit capture, look in the parms and body. */
3985 tree fn = lambda_function (t);
3986 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3987 ppd->visited);
3988 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3989 ppd->visited);
3990 *walk_subtrees = 0;
3991 return NULL_TREE;
3992 }
3993
3994 case DECLTYPE_TYPE:
3995 {
3996 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3997 type_pack_expansion_p to false so that any placeholders
3998 within the expression don't get marked as parameter packs. */
3999 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4000 ppd->type_pack_expansion_p = false;
4001 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4002 ppd, ppd->visited);
4003 ppd->type_pack_expansion_p = type_pack_expansion_p;
4004 *walk_subtrees = 0;
4005 return NULL_TREE;
4006 }
4007
4008 case IF_STMT:
4009 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4010 ppd, ppd->visited);
4011 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4012 ppd, ppd->visited);
4013 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4014 ppd, ppd->visited);
4015 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4016 *walk_subtrees = 0;
4017 return NULL_TREE;
4018
4019 default:
4020 return NULL_TREE;
4021 }
4022
4023 return NULL_TREE;
4024 }
4025
4026 /* Determines if the expression or type T uses any parameter packs. */
4027 tree
4028 uses_parameter_packs (tree t)
4029 {
4030 tree parameter_packs = NULL_TREE;
4031 struct find_parameter_pack_data ppd;
4032 ppd.parameter_packs = &parameter_packs;
4033 ppd.visited = new hash_set<tree>;
4034 ppd.type_pack_expansion_p = false;
4035 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4036 delete ppd.visited;
4037 return parameter_packs;
4038 }
4039
4040 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4041 representation a base-class initializer into a parameter pack
4042 expansion. If all goes well, the resulting node will be an
4043 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4044 respectively. */
4045 tree
4046 make_pack_expansion (tree arg, tsubst_flags_t complain)
4047 {
4048 tree result;
4049 tree parameter_packs = NULL_TREE;
4050 bool for_types = false;
4051 struct find_parameter_pack_data ppd;
4052
4053 if (!arg || arg == error_mark_node)
4054 return arg;
4055
4056 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4057 {
4058 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4059 class initializer. In this case, the TREE_PURPOSE will be a
4060 _TYPE node (representing the base class expansion we're
4061 initializing) and the TREE_VALUE will be a TREE_LIST
4062 containing the initialization arguments.
4063
4064 The resulting expansion looks somewhat different from most
4065 expansions. Rather than returning just one _EXPANSION, we
4066 return a TREE_LIST whose TREE_PURPOSE is a
4067 TYPE_PACK_EXPANSION containing the bases that will be
4068 initialized. The TREE_VALUE will be identical to the
4069 original TREE_VALUE, which is a list of arguments that will
4070 be passed to each base. We do not introduce any new pack
4071 expansion nodes into the TREE_VALUE (although it is possible
4072 that some already exist), because the TREE_PURPOSE and
4073 TREE_VALUE all need to be expanded together with the same
4074 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4075 resulting TREE_PURPOSE will mention the parameter packs in
4076 both the bases and the arguments to the bases. */
4077 tree purpose;
4078 tree value;
4079 tree parameter_packs = NULL_TREE;
4080
4081 /* Determine which parameter packs will be used by the base
4082 class expansion. */
4083 ppd.visited = new hash_set<tree>;
4084 ppd.parameter_packs = &parameter_packs;
4085 ppd.type_pack_expansion_p = false;
4086 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4087 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4088 &ppd, ppd.visited);
4089
4090 if (parameter_packs == NULL_TREE)
4091 {
4092 if (complain & tf_error)
4093 error ("base initializer expansion %qT contains no parameter packs",
4094 arg);
4095 delete ppd.visited;
4096 return error_mark_node;
4097 }
4098
4099 if (TREE_VALUE (arg) != void_type_node)
4100 {
4101 /* Collect the sets of parameter packs used in each of the
4102 initialization arguments. */
4103 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4104 {
4105 /* Determine which parameter packs will be expanded in this
4106 argument. */
4107 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4108 &ppd, ppd.visited);
4109 }
4110 }
4111
4112 delete ppd.visited;
4113
4114 /* Create the pack expansion type for the base type. */
4115 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4116 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4117 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4118 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4119
4120 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4121 they will rarely be compared to anything. */
4122 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4123
4124 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4125 }
4126
4127 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4128 for_types = true;
4129
4130 /* Build the PACK_EXPANSION_* node. */
4131 result = for_types
4132 ? cxx_make_type (TYPE_PACK_EXPANSION)
4133 : make_node (EXPR_PACK_EXPANSION);
4134 SET_PACK_EXPANSION_PATTERN (result, arg);
4135 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4136 {
4137 /* Propagate type and const-expression information. */
4138 TREE_TYPE (result) = TREE_TYPE (arg);
4139 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4140 /* Mark this read now, since the expansion might be length 0. */
4141 mark_exp_read (arg);
4142 }
4143 else
4144 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4145 they will rarely be compared to anything. */
4146 SET_TYPE_STRUCTURAL_EQUALITY (result);
4147
4148 /* Determine which parameter packs will be expanded. */
4149 ppd.parameter_packs = &parameter_packs;
4150 ppd.visited = new hash_set<tree>;
4151 ppd.type_pack_expansion_p = TYPE_P (arg);
4152 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4153 delete ppd.visited;
4154
4155 /* Make sure we found some parameter packs. */
4156 if (parameter_packs == NULL_TREE)
4157 {
4158 if (complain & tf_error)
4159 {
4160 if (TYPE_P (arg))
4161 error ("expansion pattern %qT contains no parameter packs", arg);
4162 else
4163 error ("expansion pattern %qE contains no parameter packs", arg);
4164 }
4165 return error_mark_node;
4166 }
4167 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4168
4169 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4170
4171 return result;
4172 }
4173
4174 /* Checks T for any "bare" parameter packs, which have not yet been
4175 expanded, and issues an error if any are found. This operation can
4176 only be done on full expressions or types (e.g., an expression
4177 statement, "if" condition, etc.), because we could have expressions like:
4178
4179 foo(f(g(h(args)))...)
4180
4181 where "args" is a parameter pack. check_for_bare_parameter_packs
4182 should not be called for the subexpressions args, h(args),
4183 g(h(args)), or f(g(h(args))), because we would produce erroneous
4184 error messages.
4185
4186 Returns TRUE and emits an error if there were bare parameter packs,
4187 returns FALSE otherwise. */
4188 bool
4189 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4190 {
4191 tree parameter_packs = NULL_TREE;
4192 struct find_parameter_pack_data ppd;
4193
4194 if (!processing_template_decl || !t || t == error_mark_node)
4195 return false;
4196
4197 /* A lambda might use a parameter pack from the containing context. */
4198 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4199 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4200 return false;
4201
4202 if (TREE_CODE (t) == TYPE_DECL)
4203 t = TREE_TYPE (t);
4204
4205 ppd.parameter_packs = &parameter_packs;
4206 ppd.visited = new hash_set<tree>;
4207 ppd.type_pack_expansion_p = false;
4208 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4209 delete ppd.visited;
4210
4211 if (parameter_packs)
4212 {
4213 if (loc == UNKNOWN_LOCATION)
4214 loc = cp_expr_loc_or_input_loc (t);
4215 error_at (loc, "parameter packs not expanded with %<...%>:");
4216 while (parameter_packs)
4217 {
4218 tree pack = TREE_VALUE (parameter_packs);
4219 tree name = NULL_TREE;
4220
4221 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4222 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4223 name = TYPE_NAME (pack);
4224 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4225 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4226 else if (TREE_CODE (pack) == CALL_EXPR)
4227 name = DECL_NAME (CALL_EXPR_FN (pack));
4228 else
4229 name = DECL_NAME (pack);
4230
4231 if (name)
4232 inform (loc, " %qD", name);
4233 else
4234 inform (loc, " %s", "<anonymous>");
4235
4236 parameter_packs = TREE_CHAIN (parameter_packs);
4237 }
4238
4239 return true;
4240 }
4241
4242 return false;
4243 }
4244
4245 /* Expand any parameter packs that occur in the template arguments in
4246 ARGS. */
4247 tree
4248 expand_template_argument_pack (tree args)
4249 {
4250 if (args == error_mark_node)
4251 return error_mark_node;
4252
4253 tree result_args = NULL_TREE;
4254 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4255 int num_result_args = -1;
4256 int non_default_args_count = -1;
4257
4258 /* First, determine if we need to expand anything, and the number of
4259 slots we'll need. */
4260 for (in_arg = 0; in_arg < nargs; ++in_arg)
4261 {
4262 tree arg = TREE_VEC_ELT (args, in_arg);
4263 if (arg == NULL_TREE)
4264 return args;
4265 if (ARGUMENT_PACK_P (arg))
4266 {
4267 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4268 if (num_result_args < 0)
4269 num_result_args = in_arg + num_packed;
4270 else
4271 num_result_args += num_packed;
4272 }
4273 else
4274 {
4275 if (num_result_args >= 0)
4276 num_result_args++;
4277 }
4278 }
4279
4280 /* If no expansion is necessary, we're done. */
4281 if (num_result_args < 0)
4282 return args;
4283
4284 /* Expand arguments. */
4285 result_args = make_tree_vec (num_result_args);
4286 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4287 non_default_args_count =
4288 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4289 for (in_arg = 0; in_arg < nargs; ++in_arg)
4290 {
4291 tree arg = TREE_VEC_ELT (args, in_arg);
4292 if (ARGUMENT_PACK_P (arg))
4293 {
4294 tree packed = ARGUMENT_PACK_ARGS (arg);
4295 int i, num_packed = TREE_VEC_LENGTH (packed);
4296 for (i = 0; i < num_packed; ++i, ++out_arg)
4297 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4298 if (non_default_args_count > 0)
4299 non_default_args_count += num_packed - 1;
4300 }
4301 else
4302 {
4303 TREE_VEC_ELT (result_args, out_arg) = arg;
4304 ++out_arg;
4305 }
4306 }
4307 if (non_default_args_count >= 0)
4308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4309 return result_args;
4310 }
4311
4312 /* Checks if DECL shadows a template parameter.
4313
4314 [temp.local]: A template-parameter shall not be redeclared within its
4315 scope (including nested scopes).
4316
4317 Emits an error and returns TRUE if the DECL shadows a parameter,
4318 returns FALSE otherwise. */
4319
4320 bool
4321 check_template_shadow (tree decl)
4322 {
4323 tree olddecl;
4324
4325 /* If we're not in a template, we can't possibly shadow a template
4326 parameter. */
4327 if (!current_template_parms)
4328 return true;
4329
4330 /* Figure out what we're shadowing. */
4331 decl = OVL_FIRST (decl);
4332 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4333
4334 /* If there's no previous binding for this name, we're not shadowing
4335 anything, let alone a template parameter. */
4336 if (!olddecl)
4337 return true;
4338
4339 /* If we're not shadowing a template parameter, we're done. Note
4340 that OLDDECL might be an OVERLOAD (or perhaps even an
4341 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4342 node. */
4343 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4344 return true;
4345
4346 /* We check for decl != olddecl to avoid bogus errors for using a
4347 name inside a class. We check TPFI to avoid duplicate errors for
4348 inline member templates. */
4349 if (decl == olddecl
4350 || (DECL_TEMPLATE_PARM_P (decl)
4351 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4352 return true;
4353
4354 /* Don't complain about the injected class name, as we've already
4355 complained about the class itself. */
4356 if (DECL_SELF_REFERENCE_P (decl))
4357 return false;
4358
4359 if (DECL_TEMPLATE_PARM_P (decl))
4360 error ("declaration of template parameter %q+D shadows "
4361 "template parameter", decl);
4362 else
4363 error ("declaration of %q+#D shadows template parameter", decl);
4364 inform (DECL_SOURCE_LOCATION (olddecl),
4365 "template parameter %qD declared here", olddecl);
4366 return false;
4367 }
4368
4369 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4370 ORIG_LEVEL, DECL, and TYPE. */
4371
4372 static tree
4373 build_template_parm_index (int index,
4374 int level,
4375 int orig_level,
4376 tree decl,
4377 tree type)
4378 {
4379 tree t = make_node (TEMPLATE_PARM_INDEX);
4380 TEMPLATE_PARM_IDX (t) = index;
4381 TEMPLATE_PARM_LEVEL (t) = level;
4382 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4383 TEMPLATE_PARM_DECL (t) = decl;
4384 TREE_TYPE (t) = type;
4385 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4386 TREE_READONLY (t) = TREE_READONLY (decl);
4387
4388 return t;
4389 }
4390
4391 /* Find the canonical type parameter for the given template type
4392 parameter. Returns the canonical type parameter, which may be TYPE
4393 if no such parameter existed. */
4394
4395 static tree
4396 canonical_type_parameter (tree type)
4397 {
4398 tree list;
4399 int idx = TEMPLATE_TYPE_IDX (type);
4400 if (!canonical_template_parms)
4401 vec_alloc (canonical_template_parms, idx + 1);
4402
4403 if (canonical_template_parms->length () <= (unsigned) idx)
4404 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4405
4406 list = (*canonical_template_parms)[idx];
4407 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4408 list = TREE_CHAIN (list);
4409
4410 if (list)
4411 return TREE_VALUE (list);
4412 else
4413 {
4414 (*canonical_template_parms)[idx]
4415 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4416 return type;
4417 }
4418 }
4419
4420 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4421 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4422 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4423 new one is created. */
4424
4425 static tree
4426 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4427 tsubst_flags_t complain)
4428 {
4429 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4430 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4431 != TEMPLATE_PARM_LEVEL (index) - levels)
4432 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4433 {
4434 tree orig_decl = TEMPLATE_PARM_DECL (index);
4435
4436 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4437 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4438 type);
4439 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4440 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4441 DECL_ARTIFICIAL (decl) = 1;
4442 SET_DECL_TEMPLATE_PARM_P (decl);
4443
4444 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4445 TEMPLATE_PARM_LEVEL (index) - levels,
4446 TEMPLATE_PARM_ORIG_LEVEL (index),
4447 decl, type);
4448 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4449 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4450 = TEMPLATE_PARM_PARAMETER_PACK (index);
4451
4452 /* Template template parameters need this. */
4453 tree inner = decl;
4454 if (TREE_CODE (decl) == TEMPLATE_DECL)
4455 {
4456 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4457 TYPE_DECL, DECL_NAME (decl), type);
4458 DECL_TEMPLATE_RESULT (decl) = inner;
4459 DECL_ARTIFICIAL (inner) = true;
4460 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4461 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4462 }
4463
4464 /* Attach the TPI to the decl. */
4465 if (TREE_CODE (inner) == TYPE_DECL)
4466 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4467 else
4468 DECL_INITIAL (decl) = tpi;
4469 }
4470
4471 return TEMPLATE_PARM_DESCENDANTS (index);
4472 }
4473
4474 /* Process information from new template parameter PARM and append it
4475 to the LIST being built. This new parameter is a non-type
4476 parameter iff IS_NON_TYPE is true. This new parameter is a
4477 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4478 is in PARM_LOC. */
4479
4480 tree
4481 process_template_parm (tree list, location_t parm_loc, tree parm,
4482 bool is_non_type, bool is_parameter_pack)
4483 {
4484 tree decl = 0;
4485 int idx = 0;
4486
4487 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4488 tree defval = TREE_PURPOSE (parm);
4489 tree constr = TREE_TYPE (parm);
4490
4491 if (list)
4492 {
4493 tree p = tree_last (list);
4494
4495 if (p && TREE_VALUE (p) != error_mark_node)
4496 {
4497 p = TREE_VALUE (p);
4498 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4499 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4500 else
4501 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4502 }
4503
4504 ++idx;
4505 }
4506
4507 if (is_non_type)
4508 {
4509 parm = TREE_VALUE (parm);
4510
4511 SET_DECL_TEMPLATE_PARM_P (parm);
4512
4513 if (TREE_TYPE (parm) != error_mark_node)
4514 {
4515 /* [temp.param]
4516
4517 The top-level cv-qualifiers on the template-parameter are
4518 ignored when determining its type. */
4519 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4520 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4521 TREE_TYPE (parm) = error_mark_node;
4522 else if (uses_parameter_packs (TREE_TYPE (parm))
4523 && !is_parameter_pack
4524 /* If we're in a nested template parameter list, the template
4525 template parameter could be a parameter pack. */
4526 && processing_template_parmlist == 1)
4527 {
4528 /* This template parameter is not a parameter pack, but it
4529 should be. Complain about "bare" parameter packs. */
4530 check_for_bare_parameter_packs (TREE_TYPE (parm));
4531
4532 /* Recover by calling this a parameter pack. */
4533 is_parameter_pack = true;
4534 }
4535 }
4536
4537 /* A template parameter is not modifiable. */
4538 TREE_CONSTANT (parm) = 1;
4539 TREE_READONLY (parm) = 1;
4540 decl = build_decl (parm_loc,
4541 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4542 TREE_CONSTANT (decl) = 1;
4543 TREE_READONLY (decl) = 1;
4544 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4545 = build_template_parm_index (idx, processing_template_decl,
4546 processing_template_decl,
4547 decl, TREE_TYPE (parm));
4548
4549 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4550 = is_parameter_pack;
4551 }
4552 else
4553 {
4554 tree t;
4555 parm = TREE_VALUE (TREE_VALUE (parm));
4556
4557 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4558 {
4559 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4560 /* This is for distinguishing between real templates and template
4561 template parameters */
4562 TREE_TYPE (parm) = t;
4563 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4564 decl = parm;
4565 }
4566 else
4567 {
4568 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4569 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4570 decl = build_decl (parm_loc,
4571 TYPE_DECL, parm, t);
4572 }
4573
4574 TYPE_NAME (t) = decl;
4575 TYPE_STUB_DECL (t) = decl;
4576 parm = decl;
4577 TEMPLATE_TYPE_PARM_INDEX (t)
4578 = build_template_parm_index (idx, processing_template_decl,
4579 processing_template_decl,
4580 decl, TREE_TYPE (parm));
4581 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4582 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4583 }
4584 DECL_ARTIFICIAL (decl) = 1;
4585 SET_DECL_TEMPLATE_PARM_P (decl);
4586
4587 /* Build requirements for the type/template parameter.
4588 This must be done after SET_DECL_TEMPLATE_PARM_P or
4589 process_template_parm could fail. */
4590 tree reqs = finish_shorthand_constraint (parm, constr);
4591
4592 decl = pushdecl (decl);
4593 if (!is_non_type)
4594 parm = decl;
4595
4596 /* Build the parameter node linking the parameter declaration,
4597 its default argument (if any), and its constraints (if any). */
4598 parm = build_tree_list (defval, parm);
4599 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4600
4601 return chainon (list, parm);
4602 }
4603
4604 /* The end of a template parameter list has been reached. Process the
4605 tree list into a parameter vector, converting each parameter into a more
4606 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4607 as PARM_DECLs. */
4608
4609 tree
4610 end_template_parm_list (tree parms)
4611 {
4612 int nparms;
4613 tree parm, next;
4614 tree saved_parmlist = make_tree_vec (list_length (parms));
4615
4616 /* Pop the dummy parameter level and add the real one. */
4617 current_template_parms = TREE_CHAIN (current_template_parms);
4618
4619 current_template_parms
4620 = tree_cons (size_int (processing_template_decl),
4621 saved_parmlist, current_template_parms);
4622
4623 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4624 {
4625 next = TREE_CHAIN (parm);
4626 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4627 TREE_CHAIN (parm) = NULL_TREE;
4628 }
4629
4630 --processing_template_parmlist;
4631
4632 return saved_parmlist;
4633 }
4634
4635 // Explicitly indicate the end of the template parameter list. We assume
4636 // that the current template parameters have been constructed and/or
4637 // managed explicitly, as when creating new template template parameters
4638 // from a shorthand constraint.
4639 void
4640 end_template_parm_list ()
4641 {
4642 --processing_template_parmlist;
4643 }
4644
4645 /* end_template_decl is called after a template declaration is seen. */
4646
4647 void
4648 end_template_decl (void)
4649 {
4650 reset_specialization ();
4651
4652 if (! processing_template_decl)
4653 return;
4654
4655 /* This matches the pushlevel in begin_template_parm_list. */
4656 finish_scope ();
4657
4658 --processing_template_decl;
4659 current_template_parms = TREE_CHAIN (current_template_parms);
4660 }
4661
4662 /* Takes a TREE_LIST representing a template parameter and convert it
4663 into an argument suitable to be passed to the type substitution
4664 functions. Note that If the TREE_LIST contains an error_mark
4665 node, the returned argument is error_mark_node. */
4666
4667 tree
4668 template_parm_to_arg (tree t)
4669 {
4670
4671 if (t == NULL_TREE
4672 || TREE_CODE (t) != TREE_LIST)
4673 return t;
4674
4675 if (error_operand_p (TREE_VALUE (t)))
4676 return error_mark_node;
4677
4678 t = TREE_VALUE (t);
4679
4680 if (TREE_CODE (t) == TYPE_DECL
4681 || TREE_CODE (t) == TEMPLATE_DECL)
4682 {
4683 t = TREE_TYPE (t);
4684
4685 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4686 {
4687 /* Turn this argument into a TYPE_ARGUMENT_PACK
4688 with a single element, which expands T. */
4689 tree vec = make_tree_vec (1);
4690 if (CHECKING_P)
4691 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4692
4693 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4694
4695 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4696 SET_ARGUMENT_PACK_ARGS (t, vec);
4697 }
4698 }
4699 else
4700 {
4701 t = DECL_INITIAL (t);
4702
4703 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4704 {
4705 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4706 with a single element, which expands T. */
4707 tree vec = make_tree_vec (1);
4708 if (CHECKING_P)
4709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4710
4711 t = convert_from_reference (t);
4712 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4713
4714 t = make_node (NONTYPE_ARGUMENT_PACK);
4715 SET_ARGUMENT_PACK_ARGS (t, vec);
4716 }
4717 else
4718 t = convert_from_reference (t);
4719 }
4720 return t;
4721 }
4722
4723 /* Given a single level of template parameters (a TREE_VEC), return it
4724 as a set of template arguments. */
4725
4726 tree
4727 template_parms_level_to_args (tree parms)
4728 {
4729 tree a = copy_node (parms);
4730 TREE_TYPE (a) = NULL_TREE;
4731 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4732 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4733
4734 if (CHECKING_P)
4735 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4736
4737 return a;
4738 }
4739
4740 /* Given a set of template parameters, return them as a set of template
4741 arguments. The template parameters are represented as a TREE_VEC, in
4742 the form documented in cp-tree.h for template arguments. */
4743
4744 tree
4745 template_parms_to_args (tree parms)
4746 {
4747 tree header;
4748 tree args = NULL_TREE;
4749 int length = TMPL_PARMS_DEPTH (parms);
4750 int l = length;
4751
4752 /* If there is only one level of template parameters, we do not
4753 create a TREE_VEC of TREE_VECs. Instead, we return a single
4754 TREE_VEC containing the arguments. */
4755 if (length > 1)
4756 args = make_tree_vec (length);
4757
4758 for (header = parms; header; header = TREE_CHAIN (header))
4759 {
4760 tree a = template_parms_level_to_args (TREE_VALUE (header));
4761
4762 if (length > 1)
4763 TREE_VEC_ELT (args, --l) = a;
4764 else
4765 args = a;
4766 }
4767
4768 return args;
4769 }
4770
4771 /* Within the declaration of a template, return the currently active
4772 template parameters as an argument TREE_VEC. */
4773
4774 static tree
4775 current_template_args (void)
4776 {
4777 return template_parms_to_args (current_template_parms);
4778 }
4779
4780 /* Return the fully generic arguments for of TMPL, i.e. what
4781 current_template_args would be while parsing it. */
4782
4783 tree
4784 generic_targs_for (tree tmpl)
4785 {
4786 if (tmpl == NULL_TREE)
4787 return NULL_TREE;
4788 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4789 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4790 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4791 template parameter, it has no TEMPLATE_INFO; for a partial
4792 specialization, it has the arguments for the primary template, and we
4793 want the arguments for the partial specialization. */;
4794 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4795 if (tree ti = get_template_info (result))
4796 return TI_ARGS (ti);
4797 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4798 }
4799
4800 /* Update the declared TYPE by doing any lookups which were thought to be
4801 dependent, but are not now that we know the SCOPE of the declarator. */
4802
4803 tree
4804 maybe_update_decl_type (tree orig_type, tree scope)
4805 {
4806 tree type = orig_type;
4807
4808 if (type == NULL_TREE)
4809 return type;
4810
4811 if (TREE_CODE (orig_type) == TYPE_DECL)
4812 type = TREE_TYPE (type);
4813
4814 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4815 && dependent_type_p (type)
4816 /* Don't bother building up the args in this case. */
4817 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4818 {
4819 /* tsubst in the args corresponding to the template parameters,
4820 including auto if present. Most things will be unchanged, but
4821 make_typename_type and tsubst_qualified_id will resolve
4822 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4823 tree args = current_template_args ();
4824 tree auto_node = type_uses_auto (type);
4825 tree pushed;
4826 if (auto_node)
4827 {
4828 tree auto_vec = make_tree_vec (1);
4829 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4830 args = add_to_template_args (args, auto_vec);
4831 }
4832 pushed = push_scope (scope);
4833 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4834 if (pushed)
4835 pop_scope (scope);
4836 }
4837
4838 if (type == error_mark_node)
4839 return orig_type;
4840
4841 if (TREE_CODE (orig_type) == TYPE_DECL)
4842 {
4843 if (same_type_p (type, TREE_TYPE (orig_type)))
4844 type = orig_type;
4845 else
4846 type = TYPE_NAME (type);
4847 }
4848 return type;
4849 }
4850
4851 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4852 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4853 the new template is a member template. */
4854
4855 static tree
4856 build_template_decl (tree decl, tree parms, bool member_template_p)
4857 {
4858 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4859 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4860 DECL_TEMPLATE_PARMS (tmpl) = parms;
4861 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4862 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4863 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4864
4865 return tmpl;
4866 }
4867
4868 struct template_parm_data
4869 {
4870 /* The level of the template parameters we are currently
4871 processing. */
4872 int level;
4873
4874 /* The index of the specialization argument we are currently
4875 processing. */
4876 int current_arg;
4877
4878 /* An array whose size is the number of template parameters. The
4879 elements are nonzero if the parameter has been used in any one
4880 of the arguments processed so far. */
4881 int* parms;
4882
4883 /* An array whose size is the number of template arguments. The
4884 elements are nonzero if the argument makes use of template
4885 parameters of this level. */
4886 int* arg_uses_template_parms;
4887 };
4888
4889 /* Subroutine of push_template_decl used to see if each template
4890 parameter in a partial specialization is used in the explicit
4891 argument list. If T is of the LEVEL given in DATA (which is
4892 treated as a template_parm_data*), then DATA->PARMS is marked
4893 appropriately. */
4894
4895 static int
4896 mark_template_parm (tree t, void* data)
4897 {
4898 int level;
4899 int idx;
4900 struct template_parm_data* tpd = (struct template_parm_data*) data;
4901
4902 template_parm_level_and_index (t, &level, &idx);
4903
4904 if (level == tpd->level)
4905 {
4906 tpd->parms[idx] = 1;
4907 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4908 }
4909
4910 /* In C++17 the type of a non-type argument is a deduced context. */
4911 if (cxx_dialect >= cxx17
4912 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4913 for_each_template_parm (TREE_TYPE (t),
4914 &mark_template_parm,
4915 data,
4916 NULL,
4917 /*include_nondeduced_p=*/false);
4918
4919 /* Return zero so that for_each_template_parm will continue the
4920 traversal of the tree; we want to mark *every* template parm. */
4921 return 0;
4922 }
4923
4924 /* Process the partial specialization DECL. */
4925
4926 static tree
4927 process_partial_specialization (tree decl)
4928 {
4929 tree type = TREE_TYPE (decl);
4930 tree tinfo = get_template_info (decl);
4931 tree maintmpl = TI_TEMPLATE (tinfo);
4932 tree specargs = TI_ARGS (tinfo);
4933 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4934 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4935 tree inner_parms;
4936 tree inst;
4937 int nargs = TREE_VEC_LENGTH (inner_args);
4938 int ntparms;
4939 int i;
4940 bool did_error_intro = false;
4941 struct template_parm_data tpd;
4942 struct template_parm_data tpd2;
4943
4944 gcc_assert (current_template_parms);
4945
4946 /* A concept cannot be specialized. */
4947 if (flag_concepts && variable_concept_p (maintmpl))
4948 {
4949 error ("specialization of variable concept %q#D", maintmpl);
4950 return error_mark_node;
4951 }
4952
4953 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4954 ntparms = TREE_VEC_LENGTH (inner_parms);
4955
4956 /* We check that each of the template parameters given in the
4957 partial specialization is used in the argument list to the
4958 specialization. For example:
4959
4960 template <class T> struct S;
4961 template <class T> struct S<T*>;
4962
4963 The second declaration is OK because `T*' uses the template
4964 parameter T, whereas
4965
4966 template <class T> struct S<int>;
4967
4968 is no good. Even trickier is:
4969
4970 template <class T>
4971 struct S1
4972 {
4973 template <class U>
4974 struct S2;
4975 template <class U>
4976 struct S2<T>;
4977 };
4978
4979 The S2<T> declaration is actually invalid; it is a
4980 full-specialization. Of course,
4981
4982 template <class U>
4983 struct S2<T (*)(U)>;
4984
4985 or some such would have been OK. */
4986 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4987 tpd.parms = XALLOCAVEC (int, ntparms);
4988 memset (tpd.parms, 0, sizeof (int) * ntparms);
4989
4990 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4991 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4992 for (i = 0; i < nargs; ++i)
4993 {
4994 tpd.current_arg = i;
4995 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4996 &mark_template_parm,
4997 &tpd,
4998 NULL,
4999 /*include_nondeduced_p=*/false);
5000 }
5001 for (i = 0; i < ntparms; ++i)
5002 if (tpd.parms[i] == 0)
5003 {
5004 /* One of the template parms was not used in a deduced context in the
5005 specialization. */
5006 if (!did_error_intro)
5007 {
5008 error ("template parameters not deducible in "
5009 "partial specialization:");
5010 did_error_intro = true;
5011 }
5012
5013 inform (input_location, " %qD",
5014 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5015 }
5016
5017 if (did_error_intro)
5018 return error_mark_node;
5019
5020 /* [temp.class.spec]
5021
5022 The argument list of the specialization shall not be identical to
5023 the implicit argument list of the primary template. */
5024 tree main_args
5025 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5026 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5027 && (!flag_concepts
5028 || !strictly_subsumes (current_template_constraints (),
5029 inner_args, maintmpl)))
5030 {
5031 if (!flag_concepts)
5032 error ("partial specialization %q+D does not specialize "
5033 "any template arguments; to define the primary template, "
5034 "remove the template argument list", decl);
5035 else
5036 error ("partial specialization %q+D does not specialize any "
5037 "template arguments and is not more constrained than "
5038 "the primary template; to define the primary template, "
5039 "remove the template argument list", decl);
5040 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5041 }
5042
5043 /* A partial specialization that replaces multiple parameters of the
5044 primary template with a pack expansion is less specialized for those
5045 parameters. */
5046 if (nargs < DECL_NTPARMS (maintmpl))
5047 {
5048 error ("partial specialization is not more specialized than the "
5049 "primary template because it replaces multiple parameters "
5050 "with a pack expansion");
5051 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5052 /* Avoid crash in process_partial_specialization. */
5053 return decl;
5054 }
5055
5056 /* If we aren't in a dependent class, we can actually try deduction. */
5057 else if (tpd.level == 1
5058 /* FIXME we should be able to handle a partial specialization of a
5059 partial instantiation, but currently we can't (c++/41727). */
5060 && TMPL_ARGS_DEPTH (specargs) == 1
5061 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5062 {
5063 auto_diagnostic_group d;
5064 if (permerror (input_location, "partial specialization %qD is not "
5065 "more specialized than", decl))
5066 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5067 maintmpl);
5068 }
5069
5070 /* [temp.class.spec]
5071
5072 A partially specialized non-type argument expression shall not
5073 involve template parameters of the partial specialization except
5074 when the argument expression is a simple identifier.
5075
5076 The type of a template parameter corresponding to a specialized
5077 non-type argument shall not be dependent on a parameter of the
5078 specialization.
5079
5080 Also, we verify that pack expansions only occur at the
5081 end of the argument list. */
5082 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
5083 tpd2.parms = 0;
5084 for (i = 0; i < nargs; ++i)
5085 {
5086 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5087 tree arg = TREE_VEC_ELT (inner_args, i);
5088 tree packed_args = NULL_TREE;
5089 int j, len = 1;
5090
5091 if (ARGUMENT_PACK_P (arg))
5092 {
5093 /* Extract the arguments from the argument pack. We'll be
5094 iterating over these in the following loop. */
5095 packed_args = ARGUMENT_PACK_ARGS (arg);
5096 len = TREE_VEC_LENGTH (packed_args);
5097 }
5098
5099 for (j = 0; j < len; j++)
5100 {
5101 if (packed_args)
5102 /* Get the Jth argument in the parameter pack. */
5103 arg = TREE_VEC_ELT (packed_args, j);
5104
5105 if (PACK_EXPANSION_P (arg))
5106 {
5107 /* Pack expansions must come at the end of the
5108 argument list. */
5109 if ((packed_args && j < len - 1)
5110 || (!packed_args && i < nargs - 1))
5111 {
5112 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5113 error ("parameter pack argument %qE must be at the "
5114 "end of the template argument list", arg);
5115 else
5116 error ("parameter pack argument %qT must be at the "
5117 "end of the template argument list", arg);
5118 }
5119 }
5120
5121 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5122 /* We only care about the pattern. */
5123 arg = PACK_EXPANSION_PATTERN (arg);
5124
5125 if (/* These first two lines are the `non-type' bit. */
5126 !TYPE_P (arg)
5127 && TREE_CODE (arg) != TEMPLATE_DECL
5128 /* This next two lines are the `argument expression is not just a
5129 simple identifier' condition and also the `specialized
5130 non-type argument' bit. */
5131 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5132 && !((REFERENCE_REF_P (arg)
5133 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5134 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5135 {
5136 if ((!packed_args && tpd.arg_uses_template_parms[i])
5137 || (packed_args && uses_template_parms (arg)))
5138 error_at (cp_expr_loc_or_input_loc (arg),
5139 "template argument %qE involves template "
5140 "parameter(s)", arg);
5141 else
5142 {
5143 /* Look at the corresponding template parameter,
5144 marking which template parameters its type depends
5145 upon. */
5146 tree type = TREE_TYPE (parm);
5147
5148 if (!tpd2.parms)
5149 {
5150 /* We haven't yet initialized TPD2. Do so now. */
5151 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5152 /* The number of parameters here is the number in the
5153 main template, which, as checked in the assertion
5154 above, is NARGS. */
5155 tpd2.parms = XALLOCAVEC (int, nargs);
5156 tpd2.level =
5157 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5158 }
5159
5160 /* Mark the template parameters. But this time, we're
5161 looking for the template parameters of the main
5162 template, not in the specialization. */
5163 tpd2.current_arg = i;
5164 tpd2.arg_uses_template_parms[i] = 0;
5165 memset (tpd2.parms, 0, sizeof (int) * nargs);
5166 for_each_template_parm (type,
5167 &mark_template_parm,
5168 &tpd2,
5169 NULL,
5170 /*include_nondeduced_p=*/false);
5171
5172 if (tpd2.arg_uses_template_parms [i])
5173 {
5174 /* The type depended on some template parameters.
5175 If they are fully specialized in the
5176 specialization, that's OK. */
5177 int j;
5178 int count = 0;
5179 for (j = 0; j < nargs; ++j)
5180 if (tpd2.parms[j] != 0
5181 && tpd.arg_uses_template_parms [j])
5182 ++count;
5183 if (count != 0)
5184 error_n (input_location, count,
5185 "type %qT of template argument %qE depends "
5186 "on a template parameter",
5187 "type %qT of template argument %qE depends "
5188 "on template parameters",
5189 type,
5190 arg);
5191 }
5192 }
5193 }
5194 }
5195 }
5196
5197 /* We should only get here once. */
5198 if (TREE_CODE (decl) == TYPE_DECL)
5199 gcc_assert (!COMPLETE_TYPE_P (type));
5200
5201 // Build the template decl.
5202 tree tmpl = build_template_decl (decl, current_template_parms,
5203 DECL_MEMBER_TEMPLATE_P (maintmpl));
5204 TREE_TYPE (tmpl) = type;
5205 DECL_TEMPLATE_RESULT (tmpl) = decl;
5206 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5207 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5208 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5209
5210 /* Give template template parms a DECL_CONTEXT of the template
5211 for which they are a parameter. */
5212 for (i = 0; i < ntparms; ++i)
5213 {
5214 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5215 if (TREE_CODE (parm) == TEMPLATE_DECL)
5216 DECL_CONTEXT (parm) = tmpl;
5217 }
5218
5219 if (VAR_P (decl))
5220 /* We didn't register this in check_explicit_specialization so we could
5221 wait until the constraints were set. */
5222 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5223 else
5224 associate_classtype_constraints (type);
5225
5226 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5227 = tree_cons (specargs, tmpl,
5228 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5229 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5230
5231 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5232 inst = TREE_CHAIN (inst))
5233 {
5234 tree instance = TREE_VALUE (inst);
5235 if (TYPE_P (instance)
5236 ? (COMPLETE_TYPE_P (instance)
5237 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5238 : DECL_TEMPLATE_INSTANTIATION (instance))
5239 {
5240 tree spec = most_specialized_partial_spec (instance, tf_none);
5241 tree inst_decl = (DECL_P (instance)
5242 ? instance : TYPE_NAME (instance));
5243 if (!spec)
5244 /* OK */;
5245 else if (spec == error_mark_node)
5246 permerror (input_location,
5247 "declaration of %qD ambiguates earlier template "
5248 "instantiation for %qD", decl, inst_decl);
5249 else if (TREE_VALUE (spec) == tmpl)
5250 permerror (input_location,
5251 "partial specialization of %qD after instantiation "
5252 "of %qD", decl, inst_decl);
5253 }
5254 }
5255
5256 return decl;
5257 }
5258
5259 /* PARM is a template parameter of some form; return the corresponding
5260 TEMPLATE_PARM_INDEX. */
5261
5262 static tree
5263 get_template_parm_index (tree parm)
5264 {
5265 if (TREE_CODE (parm) == PARM_DECL
5266 || TREE_CODE (parm) == CONST_DECL)
5267 parm = DECL_INITIAL (parm);
5268 else if (TREE_CODE (parm) == TYPE_DECL
5269 || TREE_CODE (parm) == TEMPLATE_DECL)
5270 parm = TREE_TYPE (parm);
5271 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5272 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5273 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5274 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5275 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5276 return parm;
5277 }
5278
5279 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5280 parameter packs used by the template parameter PARM. */
5281
5282 static void
5283 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5284 {
5285 /* A type parm can't refer to another parm. */
5286 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5287 return;
5288 else if (TREE_CODE (parm) == PARM_DECL)
5289 {
5290 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5291 ppd, ppd->visited);
5292 return;
5293 }
5294
5295 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5296
5297 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5298 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5299 {
5300 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5301 if (template_parameter_pack_p (p))
5302 /* Any packs in the type are expanded by this parameter. */;
5303 else
5304 fixed_parameter_pack_p_1 (p, ppd);
5305 }
5306 }
5307
5308 /* PARM is a template parameter pack. Return any parameter packs used in
5309 its type or the type of any of its template parameters. If there are
5310 any such packs, it will be instantiated into a fixed template parameter
5311 list by partial instantiation rather than be fully deduced. */
5312
5313 tree
5314 fixed_parameter_pack_p (tree parm)
5315 {
5316 /* This can only be true in a member template. */
5317 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5318 return NULL_TREE;
5319 /* This can only be true for a parameter pack. */
5320 if (!template_parameter_pack_p (parm))
5321 return NULL_TREE;
5322 /* A type parm can't refer to another parm. */
5323 if (TREE_CODE (parm) == TYPE_DECL)
5324 return NULL_TREE;
5325
5326 tree parameter_packs = NULL_TREE;
5327 struct find_parameter_pack_data ppd;
5328 ppd.parameter_packs = &parameter_packs;
5329 ppd.visited = new hash_set<tree>;
5330 ppd.type_pack_expansion_p = false;
5331
5332 fixed_parameter_pack_p_1 (parm, &ppd);
5333
5334 delete ppd.visited;
5335 return parameter_packs;
5336 }
5337
5338 /* Check that a template declaration's use of default arguments and
5339 parameter packs is not invalid. Here, PARMS are the template
5340 parameters. IS_PRIMARY is true if DECL is the thing declared by
5341 a primary template. IS_PARTIAL is true if DECL is a partial
5342 specialization.
5343
5344 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5345 function template declaration or a friend class template
5346 declaration. In the function case, 1 indicates a declaration, 2
5347 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5348 emitted for extraneous default arguments.
5349
5350 Returns TRUE if there were no errors found, FALSE otherwise. */
5351
5352 bool
5353 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5354 bool is_partial, int is_friend_decl)
5355 {
5356 const char *msg;
5357 int last_level_to_check;
5358 tree parm_level;
5359 bool no_errors = true;
5360
5361 /* [temp.param]
5362
5363 A default template-argument shall not be specified in a
5364 function template declaration or a function template definition, nor
5365 in the template-parameter-list of the definition of a member of a
5366 class template. */
5367
5368 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5369 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5370 /* You can't have a function template declaration in a local
5371 scope, nor you can you define a member of a class template in a
5372 local scope. */
5373 return true;
5374
5375 if ((TREE_CODE (decl) == TYPE_DECL
5376 && TREE_TYPE (decl)
5377 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5378 || (TREE_CODE (decl) == FUNCTION_DECL
5379 && LAMBDA_FUNCTION_P (decl)))
5380 /* A lambda doesn't have an explicit declaration; don't complain
5381 about the parms of the enclosing class. */
5382 return true;
5383
5384 if (current_class_type
5385 && !TYPE_BEING_DEFINED (current_class_type)
5386 && DECL_LANG_SPECIFIC (decl)
5387 && DECL_DECLARES_FUNCTION_P (decl)
5388 /* If this is either a friend defined in the scope of the class
5389 or a member function. */
5390 && (DECL_FUNCTION_MEMBER_P (decl)
5391 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5392 : DECL_FRIEND_CONTEXT (decl)
5393 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5394 : false)
5395 /* And, if it was a member function, it really was defined in
5396 the scope of the class. */
5397 && (!DECL_FUNCTION_MEMBER_P (decl)
5398 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5399 /* We already checked these parameters when the template was
5400 declared, so there's no need to do it again now. This function
5401 was defined in class scope, but we're processing its body now
5402 that the class is complete. */
5403 return true;
5404
5405 /* Core issue 226 (C++0x only): the following only applies to class
5406 templates. */
5407 if (is_primary
5408 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5409 {
5410 /* [temp.param]
5411
5412 If a template-parameter has a default template-argument, all
5413 subsequent template-parameters shall have a default
5414 template-argument supplied. */
5415 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5416 {
5417 tree inner_parms = TREE_VALUE (parm_level);
5418 int ntparms = TREE_VEC_LENGTH (inner_parms);
5419 int seen_def_arg_p = 0;
5420 int i;
5421
5422 for (i = 0; i < ntparms; ++i)
5423 {
5424 tree parm = TREE_VEC_ELT (inner_parms, i);
5425
5426 if (parm == error_mark_node)
5427 continue;
5428
5429 if (TREE_PURPOSE (parm))
5430 seen_def_arg_p = 1;
5431 else if (seen_def_arg_p
5432 && !template_parameter_pack_p (TREE_VALUE (parm)))
5433 {
5434 error ("no default argument for %qD", TREE_VALUE (parm));
5435 /* For better subsequent error-recovery, we indicate that
5436 there should have been a default argument. */
5437 TREE_PURPOSE (parm) = error_mark_node;
5438 no_errors = false;
5439 }
5440 else if (!is_partial
5441 && !is_friend_decl
5442 /* Don't complain about an enclosing partial
5443 specialization. */
5444 && parm_level == parms
5445 && TREE_CODE (decl) == TYPE_DECL
5446 && i < ntparms - 1
5447 && template_parameter_pack_p (TREE_VALUE (parm))
5448 /* A fixed parameter pack will be partially
5449 instantiated into a fixed length list. */
5450 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5451 {
5452 /* A primary class template can only have one
5453 parameter pack, at the end of the template
5454 parameter list. */
5455
5456 error ("parameter pack %q+D must be at the end of the"
5457 " template parameter list", TREE_VALUE (parm));
5458
5459 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5460 = error_mark_node;
5461 no_errors = false;
5462 }
5463 }
5464 }
5465 }
5466
5467 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5468 || is_partial
5469 || !is_primary
5470 || is_friend_decl)
5471 /* For an ordinary class template, default template arguments are
5472 allowed at the innermost level, e.g.:
5473 template <class T = int>
5474 struct S {};
5475 but, in a partial specialization, they're not allowed even
5476 there, as we have in [temp.class.spec]:
5477
5478 The template parameter list of a specialization shall not
5479 contain default template argument values.
5480
5481 So, for a partial specialization, or for a function template
5482 (in C++98/C++03), we look at all of them. */
5483 ;
5484 else
5485 /* But, for a primary class template that is not a partial
5486 specialization we look at all template parameters except the
5487 innermost ones. */
5488 parms = TREE_CHAIN (parms);
5489
5490 /* Figure out what error message to issue. */
5491 if (is_friend_decl == 2)
5492 msg = G_("default template arguments may not be used in function template "
5493 "friend re-declaration");
5494 else if (is_friend_decl)
5495 msg = G_("default template arguments may not be used in template "
5496 "friend declarations");
5497 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5498 msg = G_("default template arguments may not be used in function templates "
5499 "without %<-std=c++11%> or %<-std=gnu++11%>");
5500 else if (is_partial)
5501 msg = G_("default template arguments may not be used in "
5502 "partial specializations");
5503 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5504 msg = G_("default argument for template parameter for class enclosing %qD");
5505 else
5506 /* Per [temp.param]/9, "A default template-argument shall not be
5507 specified in the template-parameter-lists of the definition of
5508 a member of a class template that appears outside of the member's
5509 class.", thus if we aren't handling a member of a class template
5510 there is no need to examine the parameters. */
5511 return true;
5512
5513 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5514 /* If we're inside a class definition, there's no need to
5515 examine the parameters to the class itself. On the one
5516 hand, they will be checked when the class is defined, and,
5517 on the other, default arguments are valid in things like:
5518 template <class T = double>
5519 struct S { template <class U> void f(U); };
5520 Here the default argument for `S' has no bearing on the
5521 declaration of `f'. */
5522 last_level_to_check = template_class_depth (current_class_type) + 1;
5523 else
5524 /* Check everything. */
5525 last_level_to_check = 0;
5526
5527 for (parm_level = parms;
5528 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5529 parm_level = TREE_CHAIN (parm_level))
5530 {
5531 tree inner_parms = TREE_VALUE (parm_level);
5532 int i;
5533 int ntparms;
5534
5535 ntparms = TREE_VEC_LENGTH (inner_parms);
5536 for (i = 0; i < ntparms; ++i)
5537 {
5538 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5539 continue;
5540
5541 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5542 {
5543 if (msg)
5544 {
5545 no_errors = false;
5546 if (is_friend_decl == 2)
5547 return no_errors;
5548
5549 error (msg, decl);
5550 msg = 0;
5551 }
5552
5553 /* Clear out the default argument so that we are not
5554 confused later. */
5555 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5556 }
5557 }
5558
5559 /* At this point, if we're still interested in issuing messages,
5560 they must apply to classes surrounding the object declared. */
5561 if (msg)
5562 msg = G_("default argument for template parameter for class "
5563 "enclosing %qD");
5564 }
5565
5566 return no_errors;
5567 }
5568
5569 /* Worker for push_template_decl_real, called via
5570 for_each_template_parm. DATA is really an int, indicating the
5571 level of the parameters we are interested in. If T is a template
5572 parameter of that level, return nonzero. */
5573
5574 static int
5575 template_parm_this_level_p (tree t, void* data)
5576 {
5577 int this_level = *(int *)data;
5578 int level;
5579
5580 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5581 level = TEMPLATE_PARM_LEVEL (t);
5582 else
5583 level = TEMPLATE_TYPE_LEVEL (t);
5584 return level == this_level;
5585 }
5586
5587 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5588 DATA is really an int, indicating the innermost outer level of parameters.
5589 If T is a template parameter of that level or further out, return
5590 nonzero. */
5591
5592 static int
5593 template_parm_outer_level (tree t, void *data)
5594 {
5595 int this_level = *(int *)data;
5596 int level;
5597
5598 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5599 level = TEMPLATE_PARM_LEVEL (t);
5600 else
5601 level = TEMPLATE_TYPE_LEVEL (t);
5602 return level <= this_level;
5603 }
5604
5605 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5606 parameters given by current_template_args, or reuses a
5607 previously existing one, if appropriate. Returns the DECL, or an
5608 equivalent one, if it is replaced via a call to duplicate_decls.
5609
5610 If IS_FRIEND is true, DECL is a friend declaration. */
5611
5612 tree
5613 push_template_decl_real (tree decl, bool is_friend)
5614 {
5615 tree tmpl;
5616 tree args;
5617 tree info;
5618 tree ctx;
5619 bool is_primary;
5620 bool is_partial;
5621 int new_template_p = 0;
5622 /* True if the template is a member template, in the sense of
5623 [temp.mem]. */
5624 bool member_template_p = false;
5625
5626 if (decl == error_mark_node || !current_template_parms)
5627 return error_mark_node;
5628
5629 /* See if this is a partial specialization. */
5630 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5631 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5632 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5633 || (VAR_P (decl)
5634 && DECL_LANG_SPECIFIC (decl)
5635 && DECL_TEMPLATE_SPECIALIZATION (decl)
5636 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5637
5638 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5639 is_friend = true;
5640
5641 if (is_friend)
5642 /* For a friend, we want the context of the friend, not
5643 the type of which it is a friend. */
5644 ctx = CP_DECL_CONTEXT (decl);
5645 else if (CP_DECL_CONTEXT (decl)
5646 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5647 /* In the case of a virtual function, we want the class in which
5648 it is defined. */
5649 ctx = CP_DECL_CONTEXT (decl);
5650 else
5651 /* Otherwise, if we're currently defining some class, the DECL
5652 is assumed to be a member of the class. */
5653 ctx = current_scope ();
5654
5655 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5656 ctx = NULL_TREE;
5657
5658 if (!DECL_CONTEXT (decl))
5659 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5660
5661 /* See if this is a primary template. */
5662 if (is_friend && ctx
5663 && uses_template_parms_level (ctx, processing_template_decl))
5664 /* A friend template that specifies a class context, i.e.
5665 template <typename T> friend void A<T>::f();
5666 is not primary. */
5667 is_primary = false;
5668 else if (TREE_CODE (decl) == TYPE_DECL
5669 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5670 is_primary = false;
5671 else
5672 is_primary = template_parm_scope_p ();
5673
5674 if (is_primary)
5675 {
5676 warning (OPT_Wtemplates, "template %qD declared", decl);
5677
5678 if (DECL_CLASS_SCOPE_P (decl))
5679 member_template_p = true;
5680 if (TREE_CODE (decl) == TYPE_DECL
5681 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5682 {
5683 error ("template class without a name");
5684 return error_mark_node;
5685 }
5686 else if (TREE_CODE (decl) == FUNCTION_DECL)
5687 {
5688 if (member_template_p)
5689 {
5690 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5691 error ("member template %qD may not have virt-specifiers", decl);
5692 }
5693 if (DECL_DESTRUCTOR_P (decl))
5694 {
5695 /* [temp.mem]
5696
5697 A destructor shall not be a member template. */
5698 error_at (DECL_SOURCE_LOCATION (decl),
5699 "destructor %qD declared as member template", decl);
5700 return error_mark_node;
5701 }
5702 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5703 && (!prototype_p (TREE_TYPE (decl))
5704 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5705 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5706 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5707 == void_list_node)))
5708 {
5709 /* [basic.stc.dynamic.allocation]
5710
5711 An allocation function can be a function
5712 template. ... Template allocation functions shall
5713 have two or more parameters. */
5714 error ("invalid template declaration of %qD", decl);
5715 return error_mark_node;
5716 }
5717 }
5718 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5719 && CLASS_TYPE_P (TREE_TYPE (decl)))
5720 {
5721 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5722 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5723 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5724 {
5725 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5726 if (TREE_CODE (t) == TYPE_DECL)
5727 t = TREE_TYPE (t);
5728 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5729 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5730 }
5731 }
5732 else if (TREE_CODE (decl) == TYPE_DECL
5733 && TYPE_DECL_ALIAS_P (decl))
5734 /* alias-declaration */
5735 gcc_assert (!DECL_ARTIFICIAL (decl));
5736 else if (VAR_P (decl))
5737 /* C++14 variable template. */;
5738 else if (TREE_CODE (decl) == CONCEPT_DECL)
5739 /* C++2a concept definitions. */;
5740 else
5741 {
5742 error ("template declaration of %q#D", decl);
5743 return error_mark_node;
5744 }
5745 }
5746
5747 /* Check to see that the rules regarding the use of default
5748 arguments are not being violated. We check args for a friend
5749 functions when we know whether it's a definition, introducing
5750 declaration or re-declaration. */
5751 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5752 check_default_tmpl_args (decl, current_template_parms,
5753 is_primary, is_partial, is_friend);
5754
5755 /* Ensure that there are no parameter packs in the type of this
5756 declaration that have not been expanded. */
5757 if (TREE_CODE (decl) == FUNCTION_DECL)
5758 {
5759 /* Check each of the arguments individually to see if there are
5760 any bare parameter packs. */
5761 tree type = TREE_TYPE (decl);
5762 tree arg = DECL_ARGUMENTS (decl);
5763 tree argtype = TYPE_ARG_TYPES (type);
5764
5765 while (arg && argtype)
5766 {
5767 if (!DECL_PACK_P (arg)
5768 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5769 {
5770 /* This is a PARM_DECL that contains unexpanded parameter
5771 packs. We have already complained about this in the
5772 check_for_bare_parameter_packs call, so just replace
5773 these types with ERROR_MARK_NODE. */
5774 TREE_TYPE (arg) = error_mark_node;
5775 TREE_VALUE (argtype) = error_mark_node;
5776 }
5777
5778 arg = DECL_CHAIN (arg);
5779 argtype = TREE_CHAIN (argtype);
5780 }
5781
5782 /* Check for bare parameter packs in the return type and the
5783 exception specifiers. */
5784 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5785 /* Errors were already issued, set return type to int
5786 as the frontend doesn't expect error_mark_node as
5787 the return type. */
5788 TREE_TYPE (type) = integer_type_node;
5789 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5790 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5791 }
5792 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5793 && TYPE_DECL_ALIAS_P (decl))
5794 ? DECL_ORIGINAL_TYPE (decl)
5795 : TREE_TYPE (decl)))
5796 {
5797 TREE_TYPE (decl) = error_mark_node;
5798 return error_mark_node;
5799 }
5800
5801 if (is_partial)
5802 return process_partial_specialization (decl);
5803
5804 args = current_template_args ();
5805
5806 if (!ctx
5807 || TREE_CODE (ctx) == FUNCTION_DECL
5808 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5809 || (TREE_CODE (decl) == TYPE_DECL
5810 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5811 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5812 {
5813 if (DECL_LANG_SPECIFIC (decl)
5814 && DECL_TEMPLATE_INFO (decl)
5815 && DECL_TI_TEMPLATE (decl))
5816 tmpl = DECL_TI_TEMPLATE (decl);
5817 /* If DECL is a TYPE_DECL for a class-template, then there won't
5818 be DECL_LANG_SPECIFIC. The information equivalent to
5819 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5820 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5821 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5822 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5823 {
5824 /* Since a template declaration already existed for this
5825 class-type, we must be redeclaring it here. Make sure
5826 that the redeclaration is valid. */
5827 redeclare_class_template (TREE_TYPE (decl),
5828 current_template_parms,
5829 current_template_constraints ());
5830 /* We don't need to create a new TEMPLATE_DECL; just use the
5831 one we already had. */
5832 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5833 }
5834 else
5835 {
5836 tmpl = build_template_decl (decl, current_template_parms,
5837 member_template_p);
5838 new_template_p = 1;
5839
5840 if (DECL_LANG_SPECIFIC (decl)
5841 && DECL_TEMPLATE_SPECIALIZATION (decl))
5842 {
5843 /* A specialization of a member template of a template
5844 class. */
5845 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5846 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5847 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5848 }
5849 }
5850 }
5851 else
5852 {
5853 tree a, t, current, parms;
5854 int i;
5855 tree tinfo = get_template_info (decl);
5856
5857 if (!tinfo)
5858 {
5859 error ("template definition of non-template %q#D", decl);
5860 return error_mark_node;
5861 }
5862
5863 tmpl = TI_TEMPLATE (tinfo);
5864
5865 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5866 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5867 && DECL_TEMPLATE_SPECIALIZATION (decl)
5868 && DECL_MEMBER_TEMPLATE_P (tmpl))
5869 {
5870 tree new_tmpl;
5871
5872 /* The declaration is a specialization of a member
5873 template, declared outside the class. Therefore, the
5874 innermost template arguments will be NULL, so we
5875 replace them with the arguments determined by the
5876 earlier call to check_explicit_specialization. */
5877 args = DECL_TI_ARGS (decl);
5878
5879 new_tmpl
5880 = build_template_decl (decl, current_template_parms,
5881 member_template_p);
5882 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5883 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5884 DECL_TI_TEMPLATE (decl) = new_tmpl;
5885 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5886 DECL_TEMPLATE_INFO (new_tmpl)
5887 = build_template_info (tmpl, args);
5888
5889 register_specialization (new_tmpl,
5890 most_general_template (tmpl),
5891 args,
5892 is_friend, 0);
5893 return decl;
5894 }
5895
5896 /* Make sure the template headers we got make sense. */
5897
5898 parms = DECL_TEMPLATE_PARMS (tmpl);
5899 i = TMPL_PARMS_DEPTH (parms);
5900 if (TMPL_ARGS_DEPTH (args) != i)
5901 {
5902 error ("expected %d levels of template parms for %q#D, got %d",
5903 i, decl, TMPL_ARGS_DEPTH (args));
5904 DECL_INTERFACE_KNOWN (decl) = 1;
5905 return error_mark_node;
5906 }
5907 else
5908 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5909 {
5910 a = TMPL_ARGS_LEVEL (args, i);
5911 t = INNERMOST_TEMPLATE_PARMS (parms);
5912
5913 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5914 {
5915 if (current == decl)
5916 error ("got %d template parameters for %q#D",
5917 TREE_VEC_LENGTH (a), decl);
5918 else
5919 error ("got %d template parameters for %q#T",
5920 TREE_VEC_LENGTH (a), current);
5921 error (" but %d required", TREE_VEC_LENGTH (t));
5922 /* Avoid crash in import_export_decl. */
5923 DECL_INTERFACE_KNOWN (decl) = 1;
5924 return error_mark_node;
5925 }
5926
5927 if (current == decl)
5928 current = ctx;
5929 else if (current == NULL_TREE)
5930 /* Can happen in erroneous input. */
5931 break;
5932 else
5933 current = get_containing_scope (current);
5934 }
5935
5936 /* Check that the parms are used in the appropriate qualifying scopes
5937 in the declarator. */
5938 if (!comp_template_args
5939 (TI_ARGS (tinfo),
5940 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5941 {
5942 error ("template arguments to %qD do not match original "
5943 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5944 if (!uses_template_parms (TI_ARGS (tinfo)))
5945 inform (input_location, "use %<template<>%> for"
5946 " an explicit specialization");
5947 /* Avoid crash in import_export_decl. */
5948 DECL_INTERFACE_KNOWN (decl) = 1;
5949 return error_mark_node;
5950 }
5951 }
5952
5953 DECL_TEMPLATE_RESULT (tmpl) = decl;
5954 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5955
5956 /* Push template declarations for global functions and types. Note
5957 that we do not try to push a global template friend declared in a
5958 template class; such a thing may well depend on the template
5959 parameters of the class. */
5960 if (new_template_p && !ctx
5961 && !(is_friend && template_class_depth (current_class_type) > 0))
5962 {
5963 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5964 if (tmpl == error_mark_node)
5965 return error_mark_node;
5966
5967 /* Hide template friend classes that haven't been declared yet. */
5968 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5969 {
5970 DECL_ANTICIPATED (tmpl) = 1;
5971 DECL_FRIEND_P (tmpl) = 1;
5972 }
5973 }
5974
5975 if (is_primary)
5976 {
5977 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5978
5979 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5980
5981 /* Give template template parms a DECL_CONTEXT of the template
5982 for which they are a parameter. */
5983 parms = INNERMOST_TEMPLATE_PARMS (parms);
5984 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5985 {
5986 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5987 if (TREE_CODE (parm) == TEMPLATE_DECL)
5988 DECL_CONTEXT (parm) = tmpl;
5989 }
5990
5991 if (TREE_CODE (decl) == TYPE_DECL
5992 && TYPE_DECL_ALIAS_P (decl)
5993 && complex_alias_template_p (tmpl))
5994 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5995 }
5996
5997 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5998 back to its most general template. If TMPL is a specialization,
5999 ARGS may only have the innermost set of arguments. Add the missing
6000 argument levels if necessary. */
6001 if (DECL_TEMPLATE_INFO (tmpl))
6002 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6003
6004 info = build_template_info (tmpl, args);
6005
6006 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6007 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6008 else
6009 {
6010 if (is_primary)
6011 retrofit_lang_decl (decl);
6012 if (DECL_LANG_SPECIFIC (decl))
6013 DECL_TEMPLATE_INFO (decl) = info;
6014 }
6015
6016 if (flag_implicit_templates
6017 && !is_friend
6018 && TREE_PUBLIC (decl)
6019 && VAR_OR_FUNCTION_DECL_P (decl))
6020 /* Set DECL_COMDAT on template instantiations; if we force
6021 them to be emitted by explicit instantiation,
6022 mark_needed will tell cgraph to do the right thing. */
6023 DECL_COMDAT (decl) = true;
6024
6025 return DECL_TEMPLATE_RESULT (tmpl);
6026 }
6027
6028 tree
6029 push_template_decl (tree decl)
6030 {
6031 return push_template_decl_real (decl, false);
6032 }
6033
6034 /* FN is an inheriting constructor that inherits from the constructor
6035 template INHERITED; turn FN into a constructor template with a matching
6036 template header. */
6037
6038 tree
6039 add_inherited_template_parms (tree fn, tree inherited)
6040 {
6041 tree inner_parms
6042 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6043 inner_parms = copy_node (inner_parms);
6044 tree parms
6045 = tree_cons (size_int (processing_template_decl + 1),
6046 inner_parms, current_template_parms);
6047 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6048 tree args = template_parms_to_args (parms);
6049 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6050 TREE_TYPE (tmpl) = TREE_TYPE (fn);
6051 DECL_TEMPLATE_RESULT (tmpl) = fn;
6052 DECL_ARTIFICIAL (tmpl) = true;
6053 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6054 return tmpl;
6055 }
6056
6057 /* Called when a class template TYPE is redeclared with the indicated
6058 template PARMS, e.g.:
6059
6060 template <class T> struct S;
6061 template <class T> struct S {}; */
6062
6063 bool
6064 redeclare_class_template (tree type, tree parms, tree cons)
6065 {
6066 tree tmpl;
6067 tree tmpl_parms;
6068 int i;
6069
6070 if (!TYPE_TEMPLATE_INFO (type))
6071 {
6072 error ("%qT is not a template type", type);
6073 return false;
6074 }
6075
6076 tmpl = TYPE_TI_TEMPLATE (type);
6077 if (!PRIMARY_TEMPLATE_P (tmpl))
6078 /* The type is nested in some template class. Nothing to worry
6079 about here; there are no new template parameters for the nested
6080 type. */
6081 return true;
6082
6083 if (!parms)
6084 {
6085 error ("template specifiers not specified in declaration of %qD",
6086 tmpl);
6087 return false;
6088 }
6089
6090 parms = INNERMOST_TEMPLATE_PARMS (parms);
6091 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6092
6093 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6094 {
6095 error_n (input_location, TREE_VEC_LENGTH (parms),
6096 "redeclared with %d template parameter",
6097 "redeclared with %d template parameters",
6098 TREE_VEC_LENGTH (parms));
6099 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6100 "previous declaration %qD used %d template parameter",
6101 "previous declaration %qD used %d template parameters",
6102 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6103 return false;
6104 }
6105
6106 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6107 {
6108 tree tmpl_parm;
6109 tree parm;
6110 tree tmpl_default;
6111 tree parm_default;
6112
6113 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6114 || TREE_VEC_ELT (parms, i) == error_mark_node)
6115 continue;
6116
6117 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6118 if (error_operand_p (tmpl_parm))
6119 return false;
6120
6121 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6122 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6123 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6124
6125 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6126 TEMPLATE_DECL. */
6127 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6128 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6129 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6130 || (TREE_CODE (tmpl_parm) != PARM_DECL
6131 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6132 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6133 || (TREE_CODE (tmpl_parm) == PARM_DECL
6134 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6135 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6136 {
6137 error ("template parameter %q+#D", tmpl_parm);
6138 error ("redeclared here as %q#D", parm);
6139 return false;
6140 }
6141
6142 /* The parameters can be declared to introduce different
6143 constraints. */
6144 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6145 tree p2 = TREE_VEC_ELT (parms, i);
6146 if (!template_parameter_constraints_equivalent_p (p1, p2))
6147 {
6148 error ("declaration of template parameter %q+#D with different "
6149 "constraints", parm);
6150 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6151 "original declaration appeared here");
6152 return false;
6153 }
6154
6155 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6156 {
6157 /* We have in [temp.param]:
6158
6159 A template-parameter may not be given default arguments
6160 by two different declarations in the same scope. */
6161 error_at (input_location, "redefinition of default argument for %q#D", parm);
6162 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6163 "original definition appeared here");
6164 return false;
6165 }
6166
6167 if (parm_default != NULL_TREE)
6168 /* Update the previous template parameters (which are the ones
6169 that will really count) with the new default value. */
6170 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6171 else if (tmpl_default != NULL_TREE)
6172 /* Update the new parameters, too; they'll be used as the
6173 parameters for any members. */
6174 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6175
6176 /* Give each template template parm in this redeclaration a
6177 DECL_CONTEXT of the template for which they are a parameter. */
6178 if (TREE_CODE (parm) == TEMPLATE_DECL)
6179 {
6180 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6181 DECL_CONTEXT (parm) = tmpl;
6182 }
6183
6184 if (TREE_CODE (parm) == TYPE_DECL)
6185 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6186 }
6187
6188 tree ci = get_constraints (tmpl);
6189 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6190 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6191
6192 /* Two classes with different constraints declare different entities. */
6193 if (!cp_tree_equal (req1, req2))
6194 {
6195 error_at (input_location, "redeclaration %q#D with different "
6196 "constraints", tmpl);
6197 inform (DECL_SOURCE_LOCATION (tmpl),
6198 "original declaration appeared here");
6199 return false;
6200 }
6201
6202 return true;
6203 }
6204
6205 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6206 to be used when the caller has already checked
6207 (processing_template_decl
6208 && !instantiation_dependent_expression_p (expr)
6209 && potential_constant_expression (expr))
6210 and cleared processing_template_decl. */
6211
6212 tree
6213 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6214 {
6215 return tsubst_copy_and_build (expr,
6216 /*args=*/NULL_TREE,
6217 complain,
6218 /*in_decl=*/NULL_TREE,
6219 /*function_p=*/false,
6220 /*integral_constant_expression_p=*/true);
6221 }
6222
6223 /* Simplify EXPR if it is a non-dependent expression. Returns the
6224 (possibly simplified) expression. */
6225
6226 tree
6227 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6228 {
6229 if (expr == NULL_TREE)
6230 return NULL_TREE;
6231
6232 /* If we're in a template, but EXPR isn't value dependent, simplify
6233 it. We're supposed to treat:
6234
6235 template <typename T> void f(T[1 + 1]);
6236 template <typename T> void f(T[2]);
6237
6238 as two declarations of the same function, for example. */
6239 if (processing_template_decl
6240 && is_nondependent_constant_expression (expr))
6241 {
6242 processing_template_decl_sentinel s;
6243 expr = instantiate_non_dependent_expr_internal (expr, complain);
6244 }
6245 return expr;
6246 }
6247
6248 tree
6249 instantiate_non_dependent_expr (tree expr)
6250 {
6251 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6252 }
6253
6254 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6255 an uninstantiated expression. */
6256
6257 tree
6258 instantiate_non_dependent_or_null (tree expr)
6259 {
6260 if (expr == NULL_TREE)
6261 return NULL_TREE;
6262 if (processing_template_decl)
6263 {
6264 if (!is_nondependent_constant_expression (expr))
6265 expr = NULL_TREE;
6266 else
6267 {
6268 processing_template_decl_sentinel s;
6269 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6270 }
6271 }
6272 return expr;
6273 }
6274
6275 /* True iff T is a specialization of a variable template. */
6276
6277 bool
6278 variable_template_specialization_p (tree t)
6279 {
6280 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6281 return false;
6282 tree tmpl = DECL_TI_TEMPLATE (t);
6283 return variable_template_p (tmpl);
6284 }
6285
6286 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6287 template declaration, or a TYPE_DECL for an alias declaration. */
6288
6289 bool
6290 alias_type_or_template_p (tree t)
6291 {
6292 if (t == NULL_TREE)
6293 return false;
6294 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6295 || (TYPE_P (t)
6296 && TYPE_NAME (t)
6297 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6298 || DECL_ALIAS_TEMPLATE_P (t));
6299 }
6300
6301 /* Return TRUE iff T is a specialization of an alias template. */
6302
6303 bool
6304 alias_template_specialization_p (const_tree t)
6305 {
6306 /* It's an alias template specialization if it's an alias and its
6307 TYPE_NAME is a specialization of a primary template. */
6308 if (TYPE_ALIAS_P (t))
6309 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6310 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6311
6312 return false;
6313 }
6314
6315 /* An alias template is complex from a SFINAE perspective if a template-id
6316 using that alias can be ill-formed when the expansion is not, as with
6317 the void_t template. We determine this by checking whether the
6318 expansion for the alias template uses all its template parameters. */
6319
6320 struct uses_all_template_parms_data
6321 {
6322 int level;
6323 bool *seen;
6324 };
6325
6326 static int
6327 uses_all_template_parms_r (tree t, void *data_)
6328 {
6329 struct uses_all_template_parms_data &data
6330 = *(struct uses_all_template_parms_data*)data_;
6331 tree idx = get_template_parm_index (t);
6332
6333 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6334 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6335 return 0;
6336 }
6337
6338 static bool
6339 complex_alias_template_p (const_tree tmpl)
6340 {
6341 struct uses_all_template_parms_data data;
6342 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6343 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6344 data.level = TMPL_PARMS_DEPTH (parms);
6345 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6346 data.seen = XALLOCAVEC (bool, len);
6347 for (int i = 0; i < len; ++i)
6348 data.seen[i] = false;
6349
6350 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6351 for (int i = 0; i < len; ++i)
6352 if (!data.seen[i])
6353 return true;
6354 return false;
6355 }
6356
6357 /* Return TRUE iff T is a specialization of a complex alias template with
6358 dependent template-arguments. */
6359
6360 bool
6361 dependent_alias_template_spec_p (const_tree t)
6362 {
6363 if (!alias_template_specialization_p (t))
6364 return false;
6365
6366 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6367 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6368 return false;
6369
6370 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6371 if (!any_dependent_template_arguments_p (args))
6372 return false;
6373
6374 return true;
6375 }
6376
6377 /* Return the number of innermost template parameters in TMPL. */
6378
6379 static int
6380 num_innermost_template_parms (tree tmpl)
6381 {
6382 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6383 return TREE_VEC_LENGTH (parms);
6384 }
6385
6386 /* Return either TMPL or another template that it is equivalent to under DR
6387 1286: An alias that just changes the name of a template is equivalent to
6388 the other template. */
6389
6390 static tree
6391 get_underlying_template (tree tmpl)
6392 {
6393 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6394 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6395 {
6396 /* Determine if the alias is equivalent to an underlying template. */
6397 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6398 /* The underlying type may have been ill-formed. Don't proceed. */
6399 if (!orig_type)
6400 break;
6401 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6402 if (!tinfo)
6403 break;
6404
6405 tree underlying = TI_TEMPLATE (tinfo);
6406 if (!PRIMARY_TEMPLATE_P (underlying)
6407 || (num_innermost_template_parms (tmpl)
6408 != num_innermost_template_parms (underlying)))
6409 break;
6410
6411 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6412 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6413 break;
6414
6415 /* Alias is equivalent. Strip it and repeat. */
6416 tmpl = underlying;
6417 }
6418
6419 return tmpl;
6420 }
6421
6422 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6423 must be a reference-to-function or a pointer-to-function type, as specified
6424 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6425 and check that the resulting function has external linkage. */
6426
6427 static tree
6428 convert_nontype_argument_function (tree type, tree expr,
6429 tsubst_flags_t complain)
6430 {
6431 tree fns = expr;
6432 tree fn, fn_no_ptr;
6433 linkage_kind linkage;
6434
6435 fn = instantiate_type (type, fns, tf_none);
6436 if (fn == error_mark_node)
6437 return error_mark_node;
6438
6439 if (value_dependent_expression_p (fn))
6440 goto accept;
6441
6442 fn_no_ptr = strip_fnptr_conv (fn);
6443 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6444 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6445 if (BASELINK_P (fn_no_ptr))
6446 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6447
6448 /* [temp.arg.nontype]/1
6449
6450 A template-argument for a non-type, non-template template-parameter
6451 shall be one of:
6452 [...]
6453 -- the address of an object or function with external [C++11: or
6454 internal] linkage. */
6455
6456 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6457 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6458 {
6459 if (complain & tf_error)
6460 {
6461 location_t loc = cp_expr_loc_or_input_loc (expr);
6462 error_at (loc, "%qE is not a valid template argument for type %qT",
6463 expr, type);
6464 if (TYPE_PTR_P (type))
6465 inform (loc, "it must be the address of a function "
6466 "with external linkage");
6467 else
6468 inform (loc, "it must be the name of a function with "
6469 "external linkage");
6470 }
6471 return NULL_TREE;
6472 }
6473
6474 linkage = decl_linkage (fn_no_ptr);
6475 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6476 {
6477 if (complain & tf_error)
6478 {
6479 location_t loc = cp_expr_loc_or_input_loc (expr);
6480 if (cxx_dialect >= cxx11)
6481 error_at (loc, "%qE is not a valid template argument for type "
6482 "%qT because %qD has no linkage",
6483 expr, type, fn_no_ptr);
6484 else
6485 error_at (loc, "%qE is not a valid template argument for type "
6486 "%qT because %qD does not have external linkage",
6487 expr, type, fn_no_ptr);
6488 }
6489 return NULL_TREE;
6490 }
6491
6492 accept:
6493 if (TYPE_REF_P (type))
6494 {
6495 if (REFERENCE_REF_P (fn))
6496 fn = TREE_OPERAND (fn, 0);
6497 else
6498 fn = build_address (fn);
6499 }
6500 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6501 fn = build_nop (type, fn);
6502
6503 return fn;
6504 }
6505
6506 /* Subroutine of convert_nontype_argument.
6507 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6508 Emit an error otherwise. */
6509
6510 static bool
6511 check_valid_ptrmem_cst_expr (tree type, tree expr,
6512 tsubst_flags_t complain)
6513 {
6514 tree orig_expr = expr;
6515 STRIP_NOPS (expr);
6516 if (null_ptr_cst_p (expr))
6517 return true;
6518 if (TREE_CODE (expr) == PTRMEM_CST
6519 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6520 PTRMEM_CST_CLASS (expr)))
6521 return true;
6522 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6523 return true;
6524 if (processing_template_decl
6525 && TREE_CODE (expr) == ADDR_EXPR
6526 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6527 return true;
6528 if (complain & tf_error)
6529 {
6530 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6531 error_at (loc, "%qE is not a valid template argument for type %qT",
6532 orig_expr, type);
6533 if (TREE_CODE (expr) != PTRMEM_CST)
6534 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6535 else
6536 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6537 }
6538 return false;
6539 }
6540
6541 /* Returns TRUE iff the address of OP is value-dependent.
6542
6543 14.6.2.4 [temp.dep.temp]:
6544 A non-integral non-type template-argument is dependent if its type is
6545 dependent or it has either of the following forms
6546 qualified-id
6547 & qualified-id
6548 and contains a nested-name-specifier which specifies a class-name that
6549 names a dependent type.
6550
6551 We generalize this to just say that the address of a member of a
6552 dependent class is value-dependent; the above doesn't cover the
6553 address of a static data member named with an unqualified-id. */
6554
6555 static bool
6556 has_value_dependent_address (tree op)
6557 {
6558 STRIP_ANY_LOCATION_WRAPPER (op);
6559
6560 /* We could use get_inner_reference here, but there's no need;
6561 this is only relevant for template non-type arguments, which
6562 can only be expressed as &id-expression. */
6563 if (DECL_P (op))
6564 {
6565 tree ctx = CP_DECL_CONTEXT (op);
6566 if (TYPE_P (ctx) && dependent_type_p (ctx))
6567 return true;
6568 }
6569
6570 return false;
6571 }
6572
6573 /* The next set of functions are used for providing helpful explanatory
6574 diagnostics for failed overload resolution. Their messages should be
6575 indented by two spaces for consistency with the messages in
6576 call.c */
6577
6578 static int
6579 unify_success (bool /*explain_p*/)
6580 {
6581 return 0;
6582 }
6583
6584 /* Other failure functions should call this one, to provide a single function
6585 for setting a breakpoint on. */
6586
6587 static int
6588 unify_invalid (bool /*explain_p*/)
6589 {
6590 return 1;
6591 }
6592
6593 static int
6594 unify_parameter_deduction_failure (bool explain_p, tree parm)
6595 {
6596 if (explain_p)
6597 inform (input_location,
6598 " couldn%'t deduce template parameter %qD", parm);
6599 return unify_invalid (explain_p);
6600 }
6601
6602 static int
6603 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6604 {
6605 if (explain_p)
6606 inform (input_location,
6607 " types %qT and %qT have incompatible cv-qualifiers",
6608 parm, arg);
6609 return unify_invalid (explain_p);
6610 }
6611
6612 static int
6613 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6614 {
6615 if (explain_p)
6616 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6617 return unify_invalid (explain_p);
6618 }
6619
6620 static int
6621 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6622 {
6623 if (explain_p)
6624 inform (input_location,
6625 " template parameter %qD is not a parameter pack, but "
6626 "argument %qD is",
6627 parm, arg);
6628 return unify_invalid (explain_p);
6629 }
6630
6631 static int
6632 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6633 {
6634 if (explain_p)
6635 inform (input_location,
6636 " template argument %qE does not match "
6637 "pointer-to-member constant %qE",
6638 arg, parm);
6639 return unify_invalid (explain_p);
6640 }
6641
6642 static int
6643 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6644 {
6645 if (explain_p)
6646 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6647 return unify_invalid (explain_p);
6648 }
6649
6650 static int
6651 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6652 {
6653 if (explain_p)
6654 inform (input_location,
6655 " inconsistent parameter pack deduction with %qT and %qT",
6656 old_arg, new_arg);
6657 return unify_invalid (explain_p);
6658 }
6659
6660 static int
6661 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6662 {
6663 if (explain_p)
6664 {
6665 if (TYPE_P (parm))
6666 inform (input_location,
6667 " deduced conflicting types for parameter %qT (%qT and %qT)",
6668 parm, first, second);
6669 else
6670 inform (input_location,
6671 " deduced conflicting values for non-type parameter "
6672 "%qE (%qE and %qE)", parm, first, second);
6673 }
6674 return unify_invalid (explain_p);
6675 }
6676
6677 static int
6678 unify_vla_arg (bool explain_p, tree arg)
6679 {
6680 if (explain_p)
6681 inform (input_location,
6682 " variable-sized array type %qT is not "
6683 "a valid template argument",
6684 arg);
6685 return unify_invalid (explain_p);
6686 }
6687
6688 static int
6689 unify_method_type_error (bool explain_p, tree arg)
6690 {
6691 if (explain_p)
6692 inform (input_location,
6693 " member function type %qT is not a valid template argument",
6694 arg);
6695 return unify_invalid (explain_p);
6696 }
6697
6698 static int
6699 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6700 {
6701 if (explain_p)
6702 {
6703 if (least_p)
6704 inform_n (input_location, wanted,
6705 " candidate expects at least %d argument, %d provided",
6706 " candidate expects at least %d arguments, %d provided",
6707 wanted, have);
6708 else
6709 inform_n (input_location, wanted,
6710 " candidate expects %d argument, %d provided",
6711 " candidate expects %d arguments, %d provided",
6712 wanted, have);
6713 }
6714 return unify_invalid (explain_p);
6715 }
6716
6717 static int
6718 unify_too_many_arguments (bool explain_p, int have, int wanted)
6719 {
6720 return unify_arity (explain_p, have, wanted);
6721 }
6722
6723 static int
6724 unify_too_few_arguments (bool explain_p, int have, int wanted,
6725 bool least_p = false)
6726 {
6727 return unify_arity (explain_p, have, wanted, least_p);
6728 }
6729
6730 static int
6731 unify_arg_conversion (bool explain_p, tree to_type,
6732 tree from_type, tree arg)
6733 {
6734 if (explain_p)
6735 inform (cp_expr_loc_or_input_loc (arg),
6736 " cannot convert %qE (type %qT) to type %qT",
6737 arg, from_type, to_type);
6738 return unify_invalid (explain_p);
6739 }
6740
6741 static int
6742 unify_no_common_base (bool explain_p, enum template_base_result r,
6743 tree parm, tree arg)
6744 {
6745 if (explain_p)
6746 switch (r)
6747 {
6748 case tbr_ambiguous_baseclass:
6749 inform (input_location, " %qT is an ambiguous base class of %qT",
6750 parm, arg);
6751 break;
6752 default:
6753 inform (input_location, " %qT is not derived from %qT", arg, parm);
6754 break;
6755 }
6756 return unify_invalid (explain_p);
6757 }
6758
6759 static int
6760 unify_inconsistent_template_template_parameters (bool explain_p)
6761 {
6762 if (explain_p)
6763 inform (input_location,
6764 " template parameters of a template template argument are "
6765 "inconsistent with other deduced template arguments");
6766 return unify_invalid (explain_p);
6767 }
6768
6769 static int
6770 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6771 {
6772 if (explain_p)
6773 inform (input_location,
6774 " cannot deduce a template for %qT from non-template type %qT",
6775 parm, arg);
6776 return unify_invalid (explain_p);
6777 }
6778
6779 static int
6780 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6781 {
6782 if (explain_p)
6783 inform (input_location,
6784 " template argument %qE does not match %qE", arg, parm);
6785 return unify_invalid (explain_p);
6786 }
6787
6788 /* True if T is a C++20 template parameter object to store the argument for a
6789 template parameter of class type. */
6790
6791 bool
6792 template_parm_object_p (const_tree t)
6793 {
6794 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6795 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6796 }
6797
6798 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6799 argument for TYPE, points to an unsuitable object. */
6800
6801 static bool
6802 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6803 {
6804 switch (TREE_CODE (expr))
6805 {
6806 CASE_CONVERT:
6807 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6808 complain);
6809
6810 case TARGET_EXPR:
6811 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6812 complain);
6813
6814 case CONSTRUCTOR:
6815 {
6816 unsigned i; tree elt;
6817 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6818 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6819 return true;
6820 }
6821 break;
6822
6823 case ADDR_EXPR:
6824 {
6825 tree decl = TREE_OPERAND (expr, 0);
6826
6827 if (!VAR_P (decl))
6828 {
6829 if (complain & tf_error)
6830 error_at (cp_expr_loc_or_input_loc (expr),
6831 "%qE is not a valid template argument of type %qT "
6832 "because %qE is not a variable", expr, type, decl);
6833 return true;
6834 }
6835 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6836 {
6837 if (complain & tf_error)
6838 error_at (cp_expr_loc_or_input_loc (expr),
6839 "%qE is not a valid template argument of type %qT "
6840 "in C++98 because %qD does not have external linkage",
6841 expr, type, decl);
6842 return true;
6843 }
6844 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6845 && decl_linkage (decl) == lk_none)
6846 {
6847 if (complain & tf_error)
6848 error_at (cp_expr_loc_or_input_loc (expr),
6849 "%qE is not a valid template argument of type %qT "
6850 "because %qD has no linkage", expr, type, decl);
6851 return true;
6852 }
6853 /* C++17: For a non-type template-parameter of reference or pointer
6854 type, the value of the constant expression shall not refer to (or
6855 for a pointer type, shall not be the address of):
6856 * a subobject (4.5),
6857 * a temporary object (15.2),
6858 * a string literal (5.13.5),
6859 * the result of a typeid expression (8.2.8), or
6860 * a predefined __func__ variable (11.4.1). */
6861 else if (DECL_ARTIFICIAL (decl))
6862 {
6863 if (complain & tf_error)
6864 error ("the address of %qD is not a valid template argument",
6865 decl);
6866 return true;
6867 }
6868 else if (!same_type_ignoring_top_level_qualifiers_p
6869 (strip_array_types (TREE_TYPE (type)),
6870 strip_array_types (TREE_TYPE (decl))))
6871 {
6872 if (complain & tf_error)
6873 error ("the address of the %qT subobject of %qD is not a "
6874 "valid template argument", TREE_TYPE (type), decl);
6875 return true;
6876 }
6877 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6878 {
6879 if (complain & tf_error)
6880 error ("the address of %qD is not a valid template argument "
6881 "because it does not have static storage duration",
6882 decl);
6883 return true;
6884 }
6885 }
6886 break;
6887
6888 default:
6889 if (!INDIRECT_TYPE_P (type))
6890 /* We're only concerned about pointers and references here. */;
6891 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6892 /* Null pointer values are OK in C++11. */;
6893 else
6894 {
6895 if (VAR_P (expr))
6896 {
6897 if (complain & tf_error)
6898 error ("%qD is not a valid template argument "
6899 "because %qD is a variable, not the address of "
6900 "a variable", expr, expr);
6901 return true;
6902 }
6903 else
6904 {
6905 if (complain & tf_error)
6906 error ("%qE is not a valid template argument for %qT "
6907 "because it is not the address of a variable",
6908 expr, type);
6909 return true;
6910 }
6911 }
6912 }
6913 return false;
6914
6915 }
6916
6917 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6918 template argument EXPR. */
6919
6920 static tree
6921 get_template_parm_object (tree expr, tsubst_flags_t complain)
6922 {
6923 if (TREE_CODE (expr) == TARGET_EXPR)
6924 expr = TARGET_EXPR_INITIAL (expr);
6925
6926 if (!TREE_CONSTANT (expr))
6927 {
6928 if ((complain & tf_error)
6929 && require_rvalue_constant_expression (expr))
6930 cxx_constant_value (expr);
6931 return error_mark_node;
6932 }
6933 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6934 return error_mark_node;
6935
6936 tree name = mangle_template_parm_object (expr);
6937 tree decl = get_global_binding (name);
6938 if (decl)
6939 return decl;
6940
6941 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6942 decl = create_temporary_var (type);
6943 TREE_STATIC (decl) = true;
6944 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6945 TREE_READONLY (decl) = true;
6946 DECL_NAME (decl) = name;
6947 SET_DECL_ASSEMBLER_NAME (decl, name);
6948 DECL_CONTEXT (decl) = global_namespace;
6949 comdat_linkage (decl);
6950 pushdecl_top_level_and_finish (decl, expr);
6951 return decl;
6952 }
6953
6954 /* Attempt to convert the non-type template parameter EXPR to the
6955 indicated TYPE. If the conversion is successful, return the
6956 converted value. If the conversion is unsuccessful, return
6957 NULL_TREE if we issued an error message, or error_mark_node if we
6958 did not. We issue error messages for out-and-out bad template
6959 parameters, but not simply because the conversion failed, since we
6960 might be just trying to do argument deduction. Both TYPE and EXPR
6961 must be non-dependent.
6962
6963 The conversion follows the special rules described in
6964 [temp.arg.nontype], and it is much more strict than an implicit
6965 conversion.
6966
6967 This function is called twice for each template argument (see
6968 lookup_template_class for a more accurate description of this
6969 problem). This means that we need to handle expressions which
6970 are not valid in a C++ source, but can be created from the
6971 first call (for instance, casts to perform conversions). These
6972 hacks can go away after we fix the double coercion problem. */
6973
6974 static tree
6975 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6976 {
6977 tree expr_type;
6978 location_t loc = cp_expr_loc_or_input_loc (expr);
6979
6980 /* Detect immediately string literals as invalid non-type argument.
6981 This special-case is not needed for correctness (we would easily
6982 catch this later), but only to provide better diagnostic for this
6983 common user mistake. As suggested by DR 100, we do not mention
6984 linkage issues in the diagnostic as this is not the point. */
6985 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6986 {
6987 if (complain & tf_error)
6988 error ("%qE is not a valid template argument for type %qT "
6989 "because string literals can never be used in this context",
6990 expr, type);
6991 return NULL_TREE;
6992 }
6993
6994 /* Add the ADDR_EXPR now for the benefit of
6995 value_dependent_expression_p. */
6996 if (TYPE_PTROBV_P (type)
6997 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6998 {
6999 expr = decay_conversion (expr, complain);
7000 if (expr == error_mark_node)
7001 return error_mark_node;
7002 }
7003
7004 /* If we are in a template, EXPR may be non-dependent, but still
7005 have a syntactic, rather than semantic, form. For example, EXPR
7006 might be a SCOPE_REF, rather than the VAR_DECL to which the
7007 SCOPE_REF refers. Preserving the qualifying scope is necessary
7008 so that access checking can be performed when the template is
7009 instantiated -- but here we need the resolved form so that we can
7010 convert the argument. */
7011 bool non_dep = false;
7012 if (TYPE_REF_OBJ_P (type)
7013 && has_value_dependent_address (expr))
7014 /* If we want the address and it's value-dependent, don't fold. */;
7015 else if (processing_template_decl
7016 && is_nondependent_constant_expression (expr))
7017 non_dep = true;
7018 if (error_operand_p (expr))
7019 return error_mark_node;
7020 expr_type = TREE_TYPE (expr);
7021
7022 /* If the argument is non-dependent, perform any conversions in
7023 non-dependent context as well. */
7024 processing_template_decl_sentinel s (non_dep);
7025 if (non_dep)
7026 expr = instantiate_non_dependent_expr_internal (expr, complain);
7027
7028 if (value_dependent_expression_p (expr))
7029 expr = canonicalize_expr_argument (expr, complain);
7030
7031 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7032 to a non-type argument of "nullptr". */
7033 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7034 expr = fold_simple (convert (type, expr));
7035
7036 /* In C++11, integral or enumeration non-type template arguments can be
7037 arbitrary constant expressions. Pointer and pointer to
7038 member arguments can be general constant expressions that evaluate
7039 to a null value, but otherwise still need to be of a specific form. */
7040 if (cxx_dialect >= cxx11)
7041 {
7042 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7043 /* A PTRMEM_CST is already constant, and a valid template
7044 argument for a parameter of pointer to member type, we just want
7045 to leave it in that form rather than lower it to a
7046 CONSTRUCTOR. */;
7047 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7048 || cxx_dialect >= cxx17)
7049 {
7050 /* Calling build_converted_constant_expr might create a call to
7051 a conversion function with a value-dependent argument, which
7052 could invoke taking the address of a temporary representing
7053 the result of the conversion. */
7054 if (COMPOUND_LITERAL_P (expr)
7055 && CONSTRUCTOR_IS_DEPENDENT (expr)
7056 && MAYBE_CLASS_TYPE_P (expr_type)
7057 && TYPE_HAS_CONVERSION (expr_type))
7058 {
7059 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
7060 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7061 return expr;
7062 }
7063 /* C++17: A template-argument for a non-type template-parameter shall
7064 be a converted constant expression (8.20) of the type of the
7065 template-parameter. */
7066 expr = build_converted_constant_expr (type, expr, complain);
7067 if (expr == error_mark_node)
7068 /* Make sure we return NULL_TREE only if we have really issued
7069 an error, as described above. */
7070 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7071 expr = maybe_constant_value (expr, NULL_TREE,
7072 /*manifestly_const_eval=*/true);
7073 expr = convert_from_reference (expr);
7074 }
7075 else if (TYPE_PTR_OR_PTRMEM_P (type))
7076 {
7077 tree folded = maybe_constant_value (expr, NULL_TREE,
7078 /*manifestly_const_eval=*/true);
7079 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7080 : null_member_pointer_value_p (folded))
7081 expr = folded;
7082 }
7083 }
7084
7085 if (TYPE_REF_P (type))
7086 expr = mark_lvalue_use (expr);
7087 else
7088 expr = mark_rvalue_use (expr);
7089
7090 /* HACK: Due to double coercion, we can get a
7091 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7092 which is the tree that we built on the first call (see
7093 below when coercing to reference to object or to reference to
7094 function). We just strip everything and get to the arg.
7095 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7096 for examples. */
7097 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7098 {
7099 tree probe_type, probe = expr;
7100 if (REFERENCE_REF_P (probe))
7101 probe = TREE_OPERAND (probe, 0);
7102 probe_type = TREE_TYPE (probe);
7103 if (TREE_CODE (probe) == NOP_EXPR)
7104 {
7105 /* ??? Maybe we could use convert_from_reference here, but we
7106 would need to relax its constraints because the NOP_EXPR
7107 could actually change the type to something more cv-qualified,
7108 and this is not folded by convert_from_reference. */
7109 tree addr = TREE_OPERAND (probe, 0);
7110 if (TYPE_REF_P (probe_type)
7111 && TREE_CODE (addr) == ADDR_EXPR
7112 && TYPE_PTR_P (TREE_TYPE (addr))
7113 && (same_type_ignoring_top_level_qualifiers_p
7114 (TREE_TYPE (probe_type),
7115 TREE_TYPE (TREE_TYPE (addr)))))
7116 {
7117 expr = TREE_OPERAND (addr, 0);
7118 expr_type = TREE_TYPE (probe_type);
7119 }
7120 }
7121 }
7122
7123 /* [temp.arg.nontype]/5, bullet 1
7124
7125 For a non-type template-parameter of integral or enumeration type,
7126 integral promotions (_conv.prom_) and integral conversions
7127 (_conv.integral_) are applied. */
7128 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7129 {
7130 if (cxx_dialect < cxx11)
7131 {
7132 tree t = build_converted_constant_expr (type, expr, complain);
7133 t = maybe_constant_value (t);
7134 if (t != error_mark_node)
7135 expr = t;
7136 }
7137
7138 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7139 return error_mark_node;
7140
7141 /* Notice that there are constant expressions like '4 % 0' which
7142 do not fold into integer constants. */
7143 if (TREE_CODE (expr) != INTEGER_CST
7144 && !value_dependent_expression_p (expr))
7145 {
7146 if (complain & tf_error)
7147 {
7148 int errs = errorcount, warns = warningcount + werrorcount;
7149 if (!require_potential_constant_expression (expr))
7150 expr = error_mark_node;
7151 else
7152 expr = cxx_constant_value (expr);
7153 if (errorcount > errs || warningcount + werrorcount > warns)
7154 inform (loc, "in template argument for type %qT", type);
7155 if (expr == error_mark_node)
7156 return NULL_TREE;
7157 /* else cxx_constant_value complained but gave us
7158 a real constant, so go ahead. */
7159 if (TREE_CODE (expr) != INTEGER_CST)
7160 {
7161 /* Some assemble time constant expressions like
7162 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7163 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7164 as we can emit them into .rodata initializers of
7165 variables, yet they can't fold into an INTEGER_CST at
7166 compile time. Refuse them here. */
7167 gcc_checking_assert (reduced_constant_expression_p (expr));
7168 error_at (loc, "template argument %qE for type %qT not "
7169 "a constant integer", expr, type);
7170 return NULL_TREE;
7171 }
7172 }
7173 else
7174 return NULL_TREE;
7175 }
7176
7177 /* Avoid typedef problems. */
7178 if (TREE_TYPE (expr) != type)
7179 expr = fold_convert (type, expr);
7180 }
7181 /* [temp.arg.nontype]/5, bullet 2
7182
7183 For a non-type template-parameter of type pointer to object,
7184 qualification conversions (_conv.qual_) and the array-to-pointer
7185 conversion (_conv.array_) are applied. */
7186 else if (TYPE_PTROBV_P (type))
7187 {
7188 tree decayed = expr;
7189
7190 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7191 decay_conversion or an explicit cast. If it's a problematic cast,
7192 we'll complain about it below. */
7193 if (TREE_CODE (expr) == NOP_EXPR)
7194 {
7195 tree probe = expr;
7196 STRIP_NOPS (probe);
7197 if (TREE_CODE (probe) == ADDR_EXPR
7198 && TYPE_PTR_P (TREE_TYPE (probe)))
7199 {
7200 expr = probe;
7201 expr_type = TREE_TYPE (expr);
7202 }
7203 }
7204
7205 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7206
7207 A template-argument for a non-type, non-template template-parameter
7208 shall be one of: [...]
7209
7210 -- the name of a non-type template-parameter;
7211 -- the address of an object or function with external linkage, [...]
7212 expressed as "& id-expression" where the & is optional if the name
7213 refers to a function or array, or if the corresponding
7214 template-parameter is a reference.
7215
7216 Here, we do not care about functions, as they are invalid anyway
7217 for a parameter of type pointer-to-object. */
7218
7219 if (value_dependent_expression_p (expr))
7220 /* Non-type template parameters are OK. */
7221 ;
7222 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7223 /* Null pointer values are OK in C++11. */;
7224 else if (TREE_CODE (expr) != ADDR_EXPR
7225 && !INDIRECT_TYPE_P (expr_type))
7226 /* Other values, like integer constants, might be valid
7227 non-type arguments of some other type. */
7228 return error_mark_node;
7229 else if (invalid_tparm_referent_p (type, expr, complain))
7230 return NULL_TREE;
7231
7232 expr = decayed;
7233
7234 expr = perform_qualification_conversions (type, expr);
7235 if (expr == error_mark_node)
7236 return error_mark_node;
7237 }
7238 /* [temp.arg.nontype]/5, bullet 3
7239
7240 For a non-type template-parameter of type reference to object, no
7241 conversions apply. The type referred to by the reference may be more
7242 cv-qualified than the (otherwise identical) type of the
7243 template-argument. The template-parameter is bound directly to the
7244 template-argument, which must be an lvalue. */
7245 else if (TYPE_REF_OBJ_P (type))
7246 {
7247 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7248 expr_type))
7249 return error_mark_node;
7250
7251 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7252 {
7253 if (complain & tf_error)
7254 error ("%qE is not a valid template argument for type %qT "
7255 "because of conflicts in cv-qualification", expr, type);
7256 return NULL_TREE;
7257 }
7258
7259 if (!lvalue_p (expr))
7260 {
7261 if (complain & tf_error)
7262 error ("%qE is not a valid template argument for type %qT "
7263 "because it is not an lvalue", expr, type);
7264 return NULL_TREE;
7265 }
7266
7267 /* [temp.arg.nontype]/1
7268
7269 A template-argument for a non-type, non-template template-parameter
7270 shall be one of: [...]
7271
7272 -- the address of an object or function with external linkage. */
7273 if (INDIRECT_REF_P (expr)
7274 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7275 {
7276 expr = TREE_OPERAND (expr, 0);
7277 if (DECL_P (expr))
7278 {
7279 if (complain & tf_error)
7280 error ("%q#D is not a valid template argument for type %qT "
7281 "because a reference variable does not have a constant "
7282 "address", expr, type);
7283 return NULL_TREE;
7284 }
7285 }
7286
7287 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7288 && value_dependent_expression_p (expr))
7289 /* OK, dependent reference. We don't want to ask whether a DECL is
7290 itself value-dependent, since what we want here is its address. */;
7291 else
7292 {
7293 expr = build_address (expr);
7294
7295 if (invalid_tparm_referent_p (type, expr, complain))
7296 return NULL_TREE;
7297 }
7298
7299 if (!same_type_p (type, TREE_TYPE (expr)))
7300 expr = build_nop (type, expr);
7301 }
7302 /* [temp.arg.nontype]/5, bullet 4
7303
7304 For a non-type template-parameter of type pointer to function, only
7305 the function-to-pointer conversion (_conv.func_) is applied. If the
7306 template-argument represents a set of overloaded functions (or a
7307 pointer to such), the matching function is selected from the set
7308 (_over.over_). */
7309 else if (TYPE_PTRFN_P (type))
7310 {
7311 /* If the argument is a template-id, we might not have enough
7312 context information to decay the pointer. */
7313 if (!type_unknown_p (expr_type))
7314 {
7315 expr = decay_conversion (expr, complain);
7316 if (expr == error_mark_node)
7317 return error_mark_node;
7318 }
7319
7320 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7321 /* Null pointer values are OK in C++11. */
7322 return perform_qualification_conversions (type, expr);
7323
7324 expr = convert_nontype_argument_function (type, expr, complain);
7325 if (!expr || expr == error_mark_node)
7326 return expr;
7327 }
7328 /* [temp.arg.nontype]/5, bullet 5
7329
7330 For a non-type template-parameter of type reference to function, no
7331 conversions apply. If the template-argument represents a set of
7332 overloaded functions, the matching function is selected from the set
7333 (_over.over_). */
7334 else if (TYPE_REFFN_P (type))
7335 {
7336 if (TREE_CODE (expr) == ADDR_EXPR)
7337 {
7338 if (complain & tf_error)
7339 {
7340 error ("%qE is not a valid template argument for type %qT "
7341 "because it is a pointer", expr, type);
7342 inform (input_location, "try using %qE instead",
7343 TREE_OPERAND (expr, 0));
7344 }
7345 return NULL_TREE;
7346 }
7347
7348 expr = convert_nontype_argument_function (type, expr, complain);
7349 if (!expr || expr == error_mark_node)
7350 return expr;
7351 }
7352 /* [temp.arg.nontype]/5, bullet 6
7353
7354 For a non-type template-parameter of type pointer to member function,
7355 no conversions apply. If the template-argument represents a set of
7356 overloaded member functions, the matching member function is selected
7357 from the set (_over.over_). */
7358 else if (TYPE_PTRMEMFUNC_P (type))
7359 {
7360 expr = instantiate_type (type, expr, tf_none);
7361 if (expr == error_mark_node)
7362 return error_mark_node;
7363
7364 /* [temp.arg.nontype] bullet 1 says the pointer to member
7365 expression must be a pointer-to-member constant. */
7366 if (!value_dependent_expression_p (expr)
7367 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7368 return NULL_TREE;
7369
7370 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7371 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7372 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7373 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7374 }
7375 /* [temp.arg.nontype]/5, bullet 7
7376
7377 For a non-type template-parameter of type pointer to data member,
7378 qualification conversions (_conv.qual_) are applied. */
7379 else if (TYPE_PTRDATAMEM_P (type))
7380 {
7381 /* [temp.arg.nontype] bullet 1 says the pointer to member
7382 expression must be a pointer-to-member constant. */
7383 if (!value_dependent_expression_p (expr)
7384 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7385 return NULL_TREE;
7386
7387 expr = perform_qualification_conversions (type, expr);
7388 if (expr == error_mark_node)
7389 return expr;
7390 }
7391 else if (NULLPTR_TYPE_P (type))
7392 {
7393 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7394 {
7395 if (complain & tf_error)
7396 error ("%qE is not a valid template argument for type %qT "
7397 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7398 return NULL_TREE;
7399 }
7400 return expr;
7401 }
7402 else if (CLASS_TYPE_P (type))
7403 {
7404 /* Replace the argument with a reference to the corresponding template
7405 parameter object. */
7406 if (!value_dependent_expression_p (expr))
7407 expr = get_template_parm_object (expr, complain);
7408 if (expr == error_mark_node)
7409 return NULL_TREE;
7410 }
7411 /* A template non-type parameter must be one of the above. */
7412 else
7413 gcc_unreachable ();
7414
7415 /* Sanity check: did we actually convert the argument to the
7416 right type? */
7417 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7418 (type, TREE_TYPE (expr)));
7419 return convert_from_reference (expr);
7420 }
7421
7422 /* Subroutine of coerce_template_template_parms, which returns 1 if
7423 PARM_PARM and ARG_PARM match using the rule for the template
7424 parameters of template template parameters. Both PARM and ARG are
7425 template parameters; the rest of the arguments are the same as for
7426 coerce_template_template_parms.
7427 */
7428 static int
7429 coerce_template_template_parm (tree parm,
7430 tree arg,
7431 tsubst_flags_t complain,
7432 tree in_decl,
7433 tree outer_args)
7434 {
7435 if (arg == NULL_TREE || error_operand_p (arg)
7436 || parm == NULL_TREE || error_operand_p (parm))
7437 return 0;
7438
7439 if (TREE_CODE (arg) != TREE_CODE (parm))
7440 return 0;
7441
7442 switch (TREE_CODE (parm))
7443 {
7444 case TEMPLATE_DECL:
7445 /* We encounter instantiations of templates like
7446 template <template <template <class> class> class TT>
7447 class C; */
7448 {
7449 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7450 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7451
7452 if (!coerce_template_template_parms
7453 (parmparm, argparm, complain, in_decl, outer_args))
7454 return 0;
7455 }
7456 /* Fall through. */
7457
7458 case TYPE_DECL:
7459 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7460 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7461 /* Argument is a parameter pack but parameter is not. */
7462 return 0;
7463 break;
7464
7465 case PARM_DECL:
7466 /* The tsubst call is used to handle cases such as
7467
7468 template <int> class C {};
7469 template <class T, template <T> class TT> class D {};
7470 D<int, C> d;
7471
7472 i.e. the parameter list of TT depends on earlier parameters. */
7473 if (!uses_template_parms (TREE_TYPE (arg)))
7474 {
7475 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7476 if (!uses_template_parms (t)
7477 && !same_type_p (t, TREE_TYPE (arg)))
7478 return 0;
7479 }
7480
7481 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7482 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7483 /* Argument is a parameter pack but parameter is not. */
7484 return 0;
7485
7486 break;
7487
7488 default:
7489 gcc_unreachable ();
7490 }
7491
7492 return 1;
7493 }
7494
7495 /* Coerce template argument list ARGLIST for use with template
7496 template-parameter TEMPL. */
7497
7498 static tree
7499 coerce_template_args_for_ttp (tree templ, tree arglist,
7500 tsubst_flags_t complain)
7501 {
7502 /* Consider an example where a template template parameter declared as
7503
7504 template <class T, class U = std::allocator<T> > class TT
7505
7506 The template parameter level of T and U are one level larger than
7507 of TT. To proper process the default argument of U, say when an
7508 instantiation `TT<int>' is seen, we need to build the full
7509 arguments containing {int} as the innermost level. Outer levels,
7510 available when not appearing as default template argument, can be
7511 obtained from the arguments of the enclosing template.
7512
7513 Suppose that TT is later substituted with std::vector. The above
7514 instantiation is `TT<int, std::allocator<T> >' with TT at
7515 level 1, and T at level 2, while the template arguments at level 1
7516 becomes {std::vector} and the inner level 2 is {int}. */
7517
7518 tree outer = DECL_CONTEXT (templ);
7519 if (outer)
7520 outer = generic_targs_for (outer);
7521 else if (current_template_parms)
7522 {
7523 /* This is an argument of the current template, so we haven't set
7524 DECL_CONTEXT yet. */
7525 tree relevant_template_parms;
7526
7527 /* Parameter levels that are greater than the level of the given
7528 template template parm are irrelevant. */
7529 relevant_template_parms = current_template_parms;
7530 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7531 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7532 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7533
7534 outer = template_parms_to_args (relevant_template_parms);
7535 }
7536
7537 if (outer)
7538 arglist = add_to_template_args (outer, arglist);
7539
7540 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7541 return coerce_template_parms (parmlist, arglist, templ,
7542 complain,
7543 /*require_all_args=*/true,
7544 /*use_default_args=*/true);
7545 }
7546
7547 /* A cache of template template parameters with match-all default
7548 arguments. */
7549 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7550
7551 /* T is a bound template template-parameter. Copy its arguments into default
7552 arguments of the template template-parameter's template parameters. */
7553
7554 static tree
7555 add_defaults_to_ttp (tree otmpl)
7556 {
7557 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7558 return *c;
7559
7560 tree ntmpl = copy_node (otmpl);
7561
7562 tree ntype = copy_node (TREE_TYPE (otmpl));
7563 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7564 TYPE_MAIN_VARIANT (ntype) = ntype;
7565 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7566 TYPE_NAME (ntype) = ntmpl;
7567 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7568
7569 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7570 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7571 TEMPLATE_PARM_DECL (idx) = ntmpl;
7572 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7573
7574 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7575 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7576 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7577 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7578 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7579 {
7580 tree o = TREE_VEC_ELT (vec, i);
7581 if (!template_parameter_pack_p (TREE_VALUE (o)))
7582 {
7583 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7584 TREE_PURPOSE (n) = any_targ_node;
7585 }
7586 }
7587
7588 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7589 return ntmpl;
7590 }
7591
7592 /* ARG is a bound potential template template-argument, and PARGS is a list
7593 of arguments for the corresponding template template-parameter. Adjust
7594 PARGS as appropriate for application to ARG's template, and if ARG is a
7595 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7596 arguments to the template template parameter. */
7597
7598 static tree
7599 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7600 {
7601 ++processing_template_decl;
7602 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7603 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7604 {
7605 /* When comparing two template template-parameters in partial ordering,
7606 rewrite the one currently being used as an argument to have default
7607 arguments for all parameters. */
7608 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7609 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7610 if (pargs != error_mark_node)
7611 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7612 TYPE_TI_ARGS (arg));
7613 }
7614 else
7615 {
7616 tree aparms
7617 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7618 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7619 /*require_all*/true,
7620 /*use_default*/true);
7621 }
7622 --processing_template_decl;
7623 return pargs;
7624 }
7625
7626 /* Subroutine of unify for the case when PARM is a
7627 BOUND_TEMPLATE_TEMPLATE_PARM. */
7628
7629 static int
7630 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7631 bool explain_p)
7632 {
7633 tree parmvec = TYPE_TI_ARGS (parm);
7634 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7635
7636 /* The template template parm might be variadic and the argument
7637 not, so flatten both argument lists. */
7638 parmvec = expand_template_argument_pack (parmvec);
7639 argvec = expand_template_argument_pack (argvec);
7640
7641 if (flag_new_ttp)
7642 {
7643 /* In keeping with P0522R0, adjust P's template arguments
7644 to apply to A's template; then flatten it again. */
7645 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7646 nparmvec = expand_template_argument_pack (nparmvec);
7647
7648 if (unify (tparms, targs, nparmvec, argvec,
7649 UNIFY_ALLOW_NONE, explain_p))
7650 return 1;
7651
7652 /* If the P0522 adjustment eliminated a pack expansion, deduce
7653 empty packs. */
7654 if (flag_new_ttp
7655 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7656 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7657 DEDUCE_EXACT, /*sub*/true, explain_p))
7658 return 1;
7659 }
7660 else
7661 {
7662 /* Deduce arguments T, i from TT<T> or TT<i>.
7663 We check each element of PARMVEC and ARGVEC individually
7664 rather than the whole TREE_VEC since they can have
7665 different number of elements, which is allowed under N2555. */
7666
7667 int len = TREE_VEC_LENGTH (parmvec);
7668
7669 /* Check if the parameters end in a pack, making them
7670 variadic. */
7671 int parm_variadic_p = 0;
7672 if (len > 0
7673 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7674 parm_variadic_p = 1;
7675
7676 for (int i = 0; i < len - parm_variadic_p; ++i)
7677 /* If the template argument list of P contains a pack
7678 expansion that is not the last template argument, the
7679 entire template argument list is a non-deduced
7680 context. */
7681 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7682 return unify_success (explain_p);
7683
7684 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7685 return unify_too_few_arguments (explain_p,
7686 TREE_VEC_LENGTH (argvec), len);
7687
7688 for (int i = 0; i < len - parm_variadic_p; ++i)
7689 if (unify (tparms, targs,
7690 TREE_VEC_ELT (parmvec, i),
7691 TREE_VEC_ELT (argvec, i),
7692 UNIFY_ALLOW_NONE, explain_p))
7693 return 1;
7694
7695 if (parm_variadic_p
7696 && unify_pack_expansion (tparms, targs,
7697 parmvec, argvec,
7698 DEDUCE_EXACT,
7699 /*subr=*/true, explain_p))
7700 return 1;
7701 }
7702
7703 return 0;
7704 }
7705
7706 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7707 template template parameters. Both PARM_PARMS and ARG_PARMS are
7708 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7709 or PARM_DECL.
7710
7711 Consider the example:
7712 template <class T> class A;
7713 template<template <class U> class TT> class B;
7714
7715 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7716 the parameters to A, and OUTER_ARGS contains A. */
7717
7718 static int
7719 coerce_template_template_parms (tree parm_parms,
7720 tree arg_parms,
7721 tsubst_flags_t complain,
7722 tree in_decl,
7723 tree outer_args)
7724 {
7725 int nparms, nargs, i;
7726 tree parm, arg;
7727 int variadic_p = 0;
7728
7729 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7730 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7731
7732 nparms = TREE_VEC_LENGTH (parm_parms);
7733 nargs = TREE_VEC_LENGTH (arg_parms);
7734
7735 if (flag_new_ttp)
7736 {
7737 /* P0522R0: A template template-parameter P is at least as specialized as
7738 a template template-argument A if, given the following rewrite to two
7739 function templates, the function template corresponding to P is at
7740 least as specialized as the function template corresponding to A
7741 according to the partial ordering rules for function templates
7742 ([temp.func.order]). Given an invented class template X with the
7743 template parameter list of A (including default arguments):
7744
7745 * Each of the two function templates has the same template parameters,
7746 respectively, as P or A.
7747
7748 * Each function template has a single function parameter whose type is
7749 a specialization of X with template arguments corresponding to the
7750 template parameters from the respective function template where, for
7751 each template parameter PP in the template parameter list of the
7752 function template, a corresponding template argument AA is formed. If
7753 PP declares a parameter pack, then AA is the pack expansion
7754 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7755
7756 If the rewrite produces an invalid type, then P is not at least as
7757 specialized as A. */
7758
7759 /* So coerce P's args to apply to A's parms, and then deduce between A's
7760 args and the converted args. If that succeeds, A is at least as
7761 specialized as P, so they match.*/
7762 tree pargs = template_parms_level_to_args (parm_parms);
7763 pargs = add_outermost_template_args (outer_args, pargs);
7764 ++processing_template_decl;
7765 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7766 /*require_all*/true, /*use_default*/true);
7767 --processing_template_decl;
7768 if (pargs != error_mark_node)
7769 {
7770 tree targs = make_tree_vec (nargs);
7771 tree aargs = template_parms_level_to_args (arg_parms);
7772 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7773 /*explain*/false))
7774 return 1;
7775 }
7776 }
7777
7778 /* Determine whether we have a parameter pack at the end of the
7779 template template parameter's template parameter list. */
7780 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7781 {
7782 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7783
7784 if (error_operand_p (parm))
7785 return 0;
7786
7787 switch (TREE_CODE (parm))
7788 {
7789 case TEMPLATE_DECL:
7790 case TYPE_DECL:
7791 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7792 variadic_p = 1;
7793 break;
7794
7795 case PARM_DECL:
7796 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7797 variadic_p = 1;
7798 break;
7799
7800 default:
7801 gcc_unreachable ();
7802 }
7803 }
7804
7805 if (nargs != nparms
7806 && !(variadic_p && nargs >= nparms - 1))
7807 return 0;
7808
7809 /* Check all of the template parameters except the parameter pack at
7810 the end (if any). */
7811 for (i = 0; i < nparms - variadic_p; ++i)
7812 {
7813 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7814 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7815 continue;
7816
7817 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7818 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7819
7820 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7821 outer_args))
7822 return 0;
7823
7824 }
7825
7826 if (variadic_p)
7827 {
7828 /* Check each of the template parameters in the template
7829 argument against the template parameter pack at the end of
7830 the template template parameter. */
7831 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7832 return 0;
7833
7834 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7835
7836 for (; i < nargs; ++i)
7837 {
7838 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7839 continue;
7840
7841 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7842
7843 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7844 outer_args))
7845 return 0;
7846 }
7847 }
7848
7849 return 1;
7850 }
7851
7852 /* Verifies that the deduced template arguments (in TARGS) for the
7853 template template parameters (in TPARMS) represent valid bindings,
7854 by comparing the template parameter list of each template argument
7855 to the template parameter list of its corresponding template
7856 template parameter, in accordance with DR150. This
7857 routine can only be called after all template arguments have been
7858 deduced. It will return TRUE if all of the template template
7859 parameter bindings are okay, FALSE otherwise. */
7860 bool
7861 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7862 {
7863 int i, ntparms = TREE_VEC_LENGTH (tparms);
7864 bool ret = true;
7865
7866 /* We're dealing with template parms in this process. */
7867 ++processing_template_decl;
7868
7869 targs = INNERMOST_TEMPLATE_ARGS (targs);
7870
7871 for (i = 0; i < ntparms; ++i)
7872 {
7873 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7874 tree targ = TREE_VEC_ELT (targs, i);
7875
7876 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7877 {
7878 tree packed_args = NULL_TREE;
7879 int idx, len = 1;
7880
7881 if (ARGUMENT_PACK_P (targ))
7882 {
7883 /* Look inside the argument pack. */
7884 packed_args = ARGUMENT_PACK_ARGS (targ);
7885 len = TREE_VEC_LENGTH (packed_args);
7886 }
7887
7888 for (idx = 0; idx < len; ++idx)
7889 {
7890 tree targ_parms = NULL_TREE;
7891
7892 if (packed_args)
7893 /* Extract the next argument from the argument
7894 pack. */
7895 targ = TREE_VEC_ELT (packed_args, idx);
7896
7897 if (PACK_EXPANSION_P (targ))
7898 /* Look at the pattern of the pack expansion. */
7899 targ = PACK_EXPANSION_PATTERN (targ);
7900
7901 /* Extract the template parameters from the template
7902 argument. */
7903 if (TREE_CODE (targ) == TEMPLATE_DECL)
7904 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7905 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7906 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7907
7908 /* Verify that we can coerce the template template
7909 parameters from the template argument to the template
7910 parameter. This requires an exact match. */
7911 if (targ_parms
7912 && !coerce_template_template_parms
7913 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7914 targ_parms,
7915 tf_none,
7916 tparm,
7917 targs))
7918 {
7919 ret = false;
7920 goto out;
7921 }
7922 }
7923 }
7924 }
7925
7926 out:
7927
7928 --processing_template_decl;
7929 return ret;
7930 }
7931
7932 /* Since type attributes aren't mangled, we need to strip them from
7933 template type arguments. */
7934
7935 static tree
7936 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7937 {
7938 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7939 return arg;
7940 bool removed_attributes = false;
7941 tree canon = strip_typedefs (arg, &removed_attributes);
7942 if (removed_attributes
7943 && (complain & tf_warning))
7944 warning (OPT_Wignored_attributes,
7945 "ignoring attributes on template argument %qT", arg);
7946 return canon;
7947 }
7948
7949 /* And from inside dependent non-type arguments like sizeof(Type). */
7950
7951 static tree
7952 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7953 {
7954 if (!arg || arg == error_mark_node)
7955 return arg;
7956 bool removed_attributes = false;
7957 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7958 if (removed_attributes
7959 && (complain & tf_warning))
7960 warning (OPT_Wignored_attributes,
7961 "ignoring attributes in template argument %qE", arg);
7962 return canon;
7963 }
7964
7965 // A template declaration can be substituted for a constrained
7966 // template template parameter only when the argument is more
7967 // constrained than the parameter.
7968 static bool
7969 is_compatible_template_arg (tree parm, tree arg)
7970 {
7971 tree parm_cons = get_constraints (parm);
7972
7973 /* For now, allow constrained template template arguments
7974 and unconstrained template template parameters. */
7975 if (parm_cons == NULL_TREE)
7976 return true;
7977
7978 /* If the template parameter is constrained, we need to rewrite its
7979 constraints in terms of the ARG's template parameters. This ensures
7980 that all of the template parameter types will have the same depth.
7981
7982 Note that this is only valid when coerce_template_template_parm is
7983 true for the innermost template parameters of PARM and ARG. In other
7984 words, because coercion is successful, this conversion will be valid. */
7985 tree new_args = NULL_TREE;
7986 if (parm_cons)
7987 {
7988 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7989 new_args = template_parms_level_to_args (aparms);
7990 parm_cons = tsubst_constraint_info (parm_cons, new_args,
7991 tf_none, NULL_TREE);
7992 if (parm_cons == error_mark_node)
7993 return false;
7994 }
7995
7996 return weakly_subsumes (parm_cons, new_args, arg);
7997 }
7998
7999 // Convert a placeholder argument into a binding to the original
8000 // parameter. The original parameter is saved as the TREE_TYPE of
8001 // ARG.
8002 static inline tree
8003 convert_wildcard_argument (tree parm, tree arg)
8004 {
8005 TREE_TYPE (arg) = parm;
8006 return arg;
8007 }
8008
8009 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8010 because one of them is dependent. But we need to represent the
8011 conversion for the benefit of cp_tree_equal. */
8012
8013 static tree
8014 maybe_convert_nontype_argument (tree type, tree arg)
8015 {
8016 /* Auto parms get no conversion. */
8017 if (type_uses_auto (type))
8018 return arg;
8019 /* We don't need or want to add this conversion now if we're going to use the
8020 argument for deduction. */
8021 if (value_dependent_expression_p (arg))
8022 return arg;
8023
8024 type = cv_unqualified (type);
8025 tree argtype = TREE_TYPE (arg);
8026 if (same_type_p (type, argtype))
8027 return arg;
8028
8029 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8030 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8031 return arg;
8032 }
8033
8034 /* Convert the indicated template ARG as necessary to match the
8035 indicated template PARM. Returns the converted ARG, or
8036 error_mark_node if the conversion was unsuccessful. Error and
8037 warning messages are issued under control of COMPLAIN. This
8038 conversion is for the Ith parameter in the parameter list. ARGS is
8039 the full set of template arguments deduced so far. */
8040
8041 static tree
8042 convert_template_argument (tree parm,
8043 tree arg,
8044 tree args,
8045 tsubst_flags_t complain,
8046 int i,
8047 tree in_decl)
8048 {
8049 tree orig_arg;
8050 tree val;
8051 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8052
8053 if (parm == error_mark_node || error_operand_p (arg))
8054 return error_mark_node;
8055
8056 /* Trivially convert placeholders. */
8057 if (TREE_CODE (arg) == WILDCARD_DECL)
8058 return convert_wildcard_argument (parm, arg);
8059
8060 if (arg == any_targ_node)
8061 return arg;
8062
8063 if (TREE_CODE (arg) == TREE_LIST
8064 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8065 {
8066 /* The template argument was the name of some
8067 member function. That's usually
8068 invalid, but static members are OK. In any
8069 case, grab the underlying fields/functions
8070 and issue an error later if required. */
8071 TREE_TYPE (arg) = unknown_type_node;
8072 }
8073
8074 orig_arg = arg;
8075
8076 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8077 requires_type = (TREE_CODE (parm) == TYPE_DECL
8078 || requires_tmpl_type);
8079
8080 /* When determining whether an argument pack expansion is a template,
8081 look at the pattern. */
8082 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8083 arg = PACK_EXPANSION_PATTERN (arg);
8084
8085 /* Deal with an injected-class-name used as a template template arg. */
8086 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8087 {
8088 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8089 if (TREE_CODE (t) == TEMPLATE_DECL)
8090 {
8091 if (cxx_dialect >= cxx11)
8092 /* OK under DR 1004. */;
8093 else if (complain & tf_warning_or_error)
8094 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8095 " used as template template argument", TYPE_NAME (arg));
8096 else if (flag_pedantic_errors)
8097 t = arg;
8098
8099 arg = t;
8100 }
8101 }
8102
8103 is_tmpl_type =
8104 ((TREE_CODE (arg) == TEMPLATE_DECL
8105 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8106 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8107 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8108 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8109
8110 if (is_tmpl_type
8111 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8112 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8113 arg = TYPE_STUB_DECL (arg);
8114
8115 is_type = TYPE_P (arg) || is_tmpl_type;
8116
8117 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8118 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8119 {
8120 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8121 {
8122 if (complain & tf_error)
8123 error ("invalid use of destructor %qE as a type", orig_arg);
8124 return error_mark_node;
8125 }
8126
8127 permerror (input_location,
8128 "to refer to a type member of a template parameter, "
8129 "use %<typename %E%>", orig_arg);
8130
8131 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8132 TREE_OPERAND (arg, 1),
8133 typename_type,
8134 complain);
8135 arg = orig_arg;
8136 is_type = 1;
8137 }
8138 if (is_type != requires_type)
8139 {
8140 if (in_decl)
8141 {
8142 if (complain & tf_error)
8143 {
8144 error ("type/value mismatch at argument %d in template "
8145 "parameter list for %qD",
8146 i + 1, in_decl);
8147 if (is_type)
8148 {
8149 /* The template argument is a type, but we're expecting
8150 an expression. */
8151 inform (input_location,
8152 " expected a constant of type %qT, got %qT",
8153 TREE_TYPE (parm),
8154 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8155 /* [temp.arg]/2: "In a template-argument, an ambiguity
8156 between a type-id and an expression is resolved to a
8157 type-id, regardless of the form of the corresponding
8158 template-parameter." So give the user a clue. */
8159 if (TREE_CODE (arg) == FUNCTION_TYPE)
8160 inform (input_location, " ambiguous template argument "
8161 "for non-type template parameter is treated as "
8162 "function type");
8163 }
8164 else if (requires_tmpl_type)
8165 inform (input_location,
8166 " expected a class template, got %qE", orig_arg);
8167 else
8168 inform (input_location,
8169 " expected a type, got %qE", orig_arg);
8170 }
8171 }
8172 return error_mark_node;
8173 }
8174 if (is_tmpl_type ^ requires_tmpl_type)
8175 {
8176 if (in_decl && (complain & tf_error))
8177 {
8178 error ("type/value mismatch at argument %d in template "
8179 "parameter list for %qD",
8180 i + 1, in_decl);
8181 if (is_tmpl_type)
8182 inform (input_location,
8183 " expected a type, got %qT", DECL_NAME (arg));
8184 else
8185 inform (input_location,
8186 " expected a class template, got %qT", orig_arg);
8187 }
8188 return error_mark_node;
8189 }
8190
8191 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8192 /* We already did the appropriate conversion when packing args. */
8193 val = orig_arg;
8194 else if (is_type)
8195 {
8196 if (requires_tmpl_type)
8197 {
8198 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8199 /* The number of argument required is not known yet.
8200 Just accept it for now. */
8201 val = orig_arg;
8202 else
8203 {
8204 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8205 tree argparm;
8206
8207 /* Strip alias templates that are equivalent to another
8208 template. */
8209 arg = get_underlying_template (arg);
8210 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8211
8212 if (coerce_template_template_parms (parmparm, argparm,
8213 complain, in_decl,
8214 args))
8215 {
8216 val = arg;
8217
8218 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8219 TEMPLATE_DECL. */
8220 if (val != error_mark_node)
8221 {
8222 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8223 val = TREE_TYPE (val);
8224 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8225 val = make_pack_expansion (val, complain);
8226 }
8227 }
8228 else
8229 {
8230 if (in_decl && (complain & tf_error))
8231 {
8232 error ("type/value mismatch at argument %d in "
8233 "template parameter list for %qD",
8234 i + 1, in_decl);
8235 inform (input_location,
8236 " expected a template of type %qD, got %qT",
8237 parm, orig_arg);
8238 }
8239
8240 val = error_mark_node;
8241 }
8242
8243 // Check that the constraints are compatible before allowing the
8244 // substitution.
8245 if (val != error_mark_node)
8246 if (!is_compatible_template_arg (parm, arg))
8247 {
8248 if (in_decl && (complain & tf_error))
8249 {
8250 error ("constraint mismatch at argument %d in "
8251 "template parameter list for %qD",
8252 i + 1, in_decl);
8253 inform (input_location, " expected %qD but got %qD",
8254 parm, arg);
8255 }
8256 val = error_mark_node;
8257 }
8258 }
8259 }
8260 else
8261 val = orig_arg;
8262 /* We only form one instance of each template specialization.
8263 Therefore, if we use a non-canonical variant (i.e., a
8264 typedef), any future messages referring to the type will use
8265 the typedef, which is confusing if those future uses do not
8266 themselves also use the typedef. */
8267 if (TYPE_P (val))
8268 val = canonicalize_type_argument (val, complain);
8269 }
8270 else
8271 {
8272 tree t = TREE_TYPE (parm);
8273
8274 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8275 > TMPL_ARGS_DEPTH (args))
8276 /* We don't have enough levels of args to do any substitution. This
8277 can happen in the context of -fnew-ttp-matching. */;
8278 else if (tree a = type_uses_auto (t))
8279 {
8280 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8281 if (t == error_mark_node)
8282 return error_mark_node;
8283 }
8284 else
8285 t = tsubst (t, args, complain, in_decl);
8286
8287 if (invalid_nontype_parm_type_p (t, complain))
8288 return error_mark_node;
8289
8290 if (t != TREE_TYPE (parm))
8291 t = canonicalize_type_argument (t, complain);
8292
8293 if (!type_dependent_expression_p (orig_arg)
8294 && !uses_template_parms (t))
8295 /* We used to call digest_init here. However, digest_init
8296 will report errors, which we don't want when complain
8297 is zero. More importantly, digest_init will try too
8298 hard to convert things: for example, `0' should not be
8299 converted to pointer type at this point according to
8300 the standard. Accepting this is not merely an
8301 extension, since deciding whether or not these
8302 conversions can occur is part of determining which
8303 function template to call, or whether a given explicit
8304 argument specification is valid. */
8305 val = convert_nontype_argument (t, orig_arg, complain);
8306 else
8307 {
8308 val = canonicalize_expr_argument (orig_arg, complain);
8309 val = maybe_convert_nontype_argument (t, val);
8310 }
8311
8312
8313 if (val == NULL_TREE)
8314 val = error_mark_node;
8315 else if (val == error_mark_node && (complain & tf_error))
8316 error_at (cp_expr_loc_or_input_loc (orig_arg),
8317 "could not convert template argument %qE from %qT to %qT",
8318 orig_arg, TREE_TYPE (orig_arg), t);
8319
8320 if (INDIRECT_REF_P (val))
8321 {
8322 /* Reject template arguments that are references to built-in
8323 functions with no library fallbacks. */
8324 const_tree inner = TREE_OPERAND (val, 0);
8325 const_tree innertype = TREE_TYPE (inner);
8326 if (innertype
8327 && TYPE_REF_P (innertype)
8328 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8329 && TREE_OPERAND_LENGTH (inner) > 0
8330 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8331 return error_mark_node;
8332 }
8333
8334 if (TREE_CODE (val) == SCOPE_REF)
8335 {
8336 /* Strip typedefs from the SCOPE_REF. */
8337 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8338 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8339 complain);
8340 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8341 QUALIFIED_NAME_IS_TEMPLATE (val));
8342 }
8343 }
8344
8345 return val;
8346 }
8347
8348 /* Coerces the remaining template arguments in INNER_ARGS (from
8349 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8350 Returns the coerced argument pack. PARM_IDX is the position of this
8351 parameter in the template parameter list. ARGS is the original
8352 template argument list. */
8353 static tree
8354 coerce_template_parameter_pack (tree parms,
8355 int parm_idx,
8356 tree args,
8357 tree inner_args,
8358 int arg_idx,
8359 tree new_args,
8360 int* lost,
8361 tree in_decl,
8362 tsubst_flags_t complain)
8363 {
8364 tree parm = TREE_VEC_ELT (parms, parm_idx);
8365 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8366 tree packed_args;
8367 tree argument_pack;
8368 tree packed_parms = NULL_TREE;
8369
8370 if (arg_idx > nargs)
8371 arg_idx = nargs;
8372
8373 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8374 {
8375 /* When the template parameter is a non-type template parameter pack
8376 or template template parameter pack whose type or template
8377 parameters use parameter packs, we know exactly how many arguments
8378 we are looking for. Build a vector of the instantiated decls for
8379 these template parameters in PACKED_PARMS. */
8380 /* We can't use make_pack_expansion here because it would interpret a
8381 _DECL as a use rather than a declaration. */
8382 tree decl = TREE_VALUE (parm);
8383 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8384 SET_PACK_EXPANSION_PATTERN (exp, decl);
8385 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8386 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8387
8388 TREE_VEC_LENGTH (args)--;
8389 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8390 TREE_VEC_LENGTH (args)++;
8391
8392 if (packed_parms == error_mark_node)
8393 return error_mark_node;
8394
8395 /* If we're doing a partial instantiation of a member template,
8396 verify that all of the types used for the non-type
8397 template parameter pack are, in fact, valid for non-type
8398 template parameters. */
8399 if (arg_idx < nargs
8400 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8401 {
8402 int j, len = TREE_VEC_LENGTH (packed_parms);
8403 for (j = 0; j < len; ++j)
8404 {
8405 tree t = TREE_VEC_ELT (packed_parms, j);
8406 if (TREE_CODE (t) == PARM_DECL
8407 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8408 return error_mark_node;
8409 }
8410 /* We don't know how many args we have yet, just
8411 use the unconverted ones for now. */
8412 return NULL_TREE;
8413 }
8414
8415 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8416 }
8417 /* Check if we have a placeholder pack, which indicates we're
8418 in the context of a introduction list. In that case we want
8419 to match this pack to the single placeholder. */
8420 else if (arg_idx < nargs
8421 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8422 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8423 {
8424 nargs = arg_idx + 1;
8425 packed_args = make_tree_vec (1);
8426 }
8427 else
8428 packed_args = make_tree_vec (nargs - arg_idx);
8429
8430 /* Convert the remaining arguments, which will be a part of the
8431 parameter pack "parm". */
8432 int first_pack_arg = arg_idx;
8433 for (; arg_idx < nargs; ++arg_idx)
8434 {
8435 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8436 tree actual_parm = TREE_VALUE (parm);
8437 int pack_idx = arg_idx - first_pack_arg;
8438
8439 if (packed_parms)
8440 {
8441 /* Once we've packed as many args as we have types, stop. */
8442 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8443 break;
8444 else if (PACK_EXPANSION_P (arg))
8445 /* We don't know how many args we have yet, just
8446 use the unconverted ones for now. */
8447 return NULL_TREE;
8448 else
8449 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8450 }
8451
8452 if (arg == error_mark_node)
8453 {
8454 if (complain & tf_error)
8455 error ("template argument %d is invalid", arg_idx + 1);
8456 }
8457 else
8458 arg = convert_template_argument (actual_parm,
8459 arg, new_args, complain, parm_idx,
8460 in_decl);
8461 if (arg == error_mark_node)
8462 (*lost)++;
8463 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8464 }
8465
8466 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8467 && TREE_VEC_LENGTH (packed_args) > 0)
8468 {
8469 if (complain & tf_error)
8470 error ("wrong number of template arguments (%d, should be %d)",
8471 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8472 return error_mark_node;
8473 }
8474
8475 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8476 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8477 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8478 else
8479 {
8480 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8481 TREE_CONSTANT (argument_pack) = 1;
8482 }
8483
8484 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8485 if (CHECKING_P)
8486 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8487 TREE_VEC_LENGTH (packed_args));
8488 return argument_pack;
8489 }
8490
8491 /* Returns the number of pack expansions in the template argument vector
8492 ARGS. */
8493
8494 static int
8495 pack_expansion_args_count (tree args)
8496 {
8497 int i;
8498 int count = 0;
8499 if (args)
8500 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8501 {
8502 tree elt = TREE_VEC_ELT (args, i);
8503 if (elt && PACK_EXPANSION_P (elt))
8504 ++count;
8505 }
8506 return count;
8507 }
8508
8509 /* Convert all template arguments to their appropriate types, and
8510 return a vector containing the innermost resulting template
8511 arguments. If any error occurs, return error_mark_node. Error and
8512 warning messages are issued under control of COMPLAIN.
8513
8514 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8515 for arguments not specified in ARGS. Otherwise, if
8516 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8517 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8518 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8519 ARGS. */
8520
8521 static tree
8522 coerce_template_parms (tree parms,
8523 tree args,
8524 tree in_decl,
8525 tsubst_flags_t complain,
8526 bool require_all_args,
8527 bool use_default_args)
8528 {
8529 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8530 tree orig_inner_args;
8531 tree inner_args;
8532 tree new_args;
8533 tree new_inner_args;
8534
8535 /* When used as a boolean value, indicates whether this is a
8536 variadic template parameter list. Since it's an int, we can also
8537 subtract it from nparms to get the number of non-variadic
8538 parameters. */
8539 int variadic_p = 0;
8540 int variadic_args_p = 0;
8541 int post_variadic_parms = 0;
8542
8543 /* Adjustment to nparms for fixed parameter packs. */
8544 int fixed_pack_adjust = 0;
8545 int fixed_packs = 0;
8546 int missing = 0;
8547
8548 /* Likewise for parameters with default arguments. */
8549 int default_p = 0;
8550
8551 if (args == error_mark_node)
8552 return error_mark_node;
8553
8554 nparms = TREE_VEC_LENGTH (parms);
8555
8556 /* Determine if there are any parameter packs or default arguments. */
8557 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8558 {
8559 tree parm = TREE_VEC_ELT (parms, parm_idx);
8560 if (variadic_p)
8561 ++post_variadic_parms;
8562 if (template_parameter_pack_p (TREE_VALUE (parm)))
8563 ++variadic_p;
8564 if (TREE_PURPOSE (parm))
8565 ++default_p;
8566 }
8567
8568 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8569 /* If there are no parameters that follow a parameter pack, we need to
8570 expand any argument packs so that we can deduce a parameter pack from
8571 some non-packed args followed by an argument pack, as in variadic85.C.
8572 If there are such parameters, we need to leave argument packs intact
8573 so the arguments are assigned properly. This can happen when dealing
8574 with a nested class inside a partial specialization of a class
8575 template, as in variadic92.C, or when deducing a template parameter pack
8576 from a sub-declarator, as in variadic114.C. */
8577 if (!post_variadic_parms)
8578 inner_args = expand_template_argument_pack (inner_args);
8579
8580 /* Count any pack expansion args. */
8581 variadic_args_p = pack_expansion_args_count (inner_args);
8582
8583 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8584 if ((nargs - variadic_args_p > nparms && !variadic_p)
8585 || (nargs < nparms - variadic_p
8586 && require_all_args
8587 && !variadic_args_p
8588 && (!use_default_args
8589 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8590 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8591 {
8592 bad_nargs:
8593 if (complain & tf_error)
8594 {
8595 if (variadic_p || default_p)
8596 {
8597 nparms -= variadic_p + default_p;
8598 error ("wrong number of template arguments "
8599 "(%d, should be at least %d)", nargs, nparms);
8600 }
8601 else
8602 error ("wrong number of template arguments "
8603 "(%d, should be %d)", nargs, nparms);
8604
8605 if (in_decl)
8606 inform (DECL_SOURCE_LOCATION (in_decl),
8607 "provided for %qD", in_decl);
8608 }
8609
8610 return error_mark_node;
8611 }
8612 /* We can't pass a pack expansion to a non-pack parameter of an alias
8613 template (DR 1430). */
8614 else if (in_decl
8615 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8616 || concept_definition_p (in_decl))
8617 && variadic_args_p
8618 && nargs - variadic_args_p < nparms - variadic_p)
8619 {
8620 if (complain & tf_error)
8621 {
8622 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8623 {
8624 tree arg = TREE_VEC_ELT (inner_args, i);
8625 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8626
8627 if (PACK_EXPANSION_P (arg)
8628 && !template_parameter_pack_p (parm))
8629 {
8630 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8631 error_at (location_of (arg),
8632 "pack expansion argument for non-pack parameter "
8633 "%qD of alias template %qD", parm, in_decl);
8634 else
8635 error_at (location_of (arg),
8636 "pack expansion argument for non-pack parameter "
8637 "%qD of concept %qD", parm, in_decl);
8638 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8639 goto found;
8640 }
8641 }
8642 gcc_unreachable ();
8643 found:;
8644 }
8645 return error_mark_node;
8646 }
8647
8648 /* We need to evaluate the template arguments, even though this
8649 template-id may be nested within a "sizeof". */
8650 cp_evaluated ev;
8651
8652 new_inner_args = make_tree_vec (nparms);
8653 new_args = add_outermost_template_args (args, new_inner_args);
8654 int pack_adjust = 0;
8655 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8656 {
8657 tree arg;
8658 tree parm;
8659
8660 /* Get the Ith template parameter. */
8661 parm = TREE_VEC_ELT (parms, parm_idx);
8662
8663 if (parm == error_mark_node)
8664 {
8665 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8666 continue;
8667 }
8668
8669 /* Calculate the next argument. */
8670 if (arg_idx < nargs)
8671 arg = TREE_VEC_ELT (inner_args, arg_idx);
8672 else
8673 arg = NULL_TREE;
8674
8675 if (template_parameter_pack_p (TREE_VALUE (parm))
8676 && (arg || require_all_args || !(complain & tf_partial))
8677 && !(arg && ARGUMENT_PACK_P (arg)))
8678 {
8679 /* Some arguments will be placed in the
8680 template parameter pack PARM. */
8681 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8682 inner_args, arg_idx,
8683 new_args, &lost,
8684 in_decl, complain);
8685
8686 if (arg == NULL_TREE)
8687 {
8688 /* We don't know how many args we have yet, just use the
8689 unconverted (and still packed) ones for now. */
8690 new_inner_args = orig_inner_args;
8691 arg_idx = nargs;
8692 break;
8693 }
8694
8695 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8696
8697 /* Store this argument. */
8698 if (arg == error_mark_node)
8699 {
8700 lost++;
8701 /* We are done with all of the arguments. */
8702 arg_idx = nargs;
8703 break;
8704 }
8705 else
8706 {
8707 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8708 arg_idx += pack_adjust;
8709 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8710 {
8711 ++fixed_packs;
8712 fixed_pack_adjust += pack_adjust;
8713 }
8714 }
8715
8716 continue;
8717 }
8718 else if (arg)
8719 {
8720 if (PACK_EXPANSION_P (arg))
8721 {
8722 /* "If every valid specialization of a variadic template
8723 requires an empty template parameter pack, the template is
8724 ill-formed, no diagnostic required." So check that the
8725 pattern works with this parameter. */
8726 tree pattern = PACK_EXPANSION_PATTERN (arg);
8727 tree conv = convert_template_argument (TREE_VALUE (parm),
8728 pattern, new_args,
8729 complain, parm_idx,
8730 in_decl);
8731 if (conv == error_mark_node)
8732 {
8733 if (complain & tf_error)
8734 inform (input_location, "so any instantiation with a "
8735 "non-empty parameter pack would be ill-formed");
8736 ++lost;
8737 }
8738 else if (TYPE_P (conv) && !TYPE_P (pattern))
8739 /* Recover from missing typename. */
8740 TREE_VEC_ELT (inner_args, arg_idx)
8741 = make_pack_expansion (conv, complain);
8742
8743 /* We don't know how many args we have yet, just
8744 use the unconverted ones for now. */
8745 new_inner_args = inner_args;
8746 arg_idx = nargs;
8747 break;
8748 }
8749 }
8750 else if (require_all_args)
8751 {
8752 /* There must be a default arg in this case. */
8753 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8754 complain, in_decl);
8755 /* The position of the first default template argument,
8756 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8757 Record that. */
8758 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8759 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8760 arg_idx - pack_adjust);
8761 }
8762 else
8763 break;
8764
8765 if (arg == error_mark_node)
8766 {
8767 if (complain & tf_error)
8768 error ("template argument %d is invalid", arg_idx + 1);
8769 }
8770 else if (!arg)
8771 {
8772 /* This can occur if there was an error in the template
8773 parameter list itself (which we would already have
8774 reported) that we are trying to recover from, e.g., a class
8775 template with a parameter list such as
8776 template<typename..., typename> (cpp0x/variadic150.C). */
8777 ++lost;
8778
8779 /* This can also happen with a fixed parameter pack (71834). */
8780 if (arg_idx >= nargs)
8781 ++missing;
8782 }
8783 else
8784 arg = convert_template_argument (TREE_VALUE (parm),
8785 arg, new_args, complain,
8786 parm_idx, in_decl);
8787
8788 if (arg == error_mark_node)
8789 lost++;
8790
8791 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8792 }
8793
8794 if (missing || arg_idx < nargs - variadic_args_p)
8795 {
8796 /* If we had fixed parameter packs, we didn't know how many arguments we
8797 actually needed earlier; now we do. */
8798 nparms += fixed_pack_adjust;
8799 variadic_p -= fixed_packs;
8800 goto bad_nargs;
8801 }
8802
8803 if (arg_idx < nargs)
8804 {
8805 /* We had some pack expansion arguments that will only work if the packs
8806 are empty, but wait until instantiation time to complain.
8807 See variadic-ttp3.C. */
8808
8809 /* Except that we can't provide empty packs to alias templates or
8810 concepts when there are no corresponding parameters. Basically,
8811 we can get here with this:
8812
8813 template<typename T> concept C = true;
8814
8815 template<typename... Args>
8816 requires C<Args...>
8817 void f();
8818
8819 When parsing C<Args...>, we try to form a concept check of
8820 C<?, Args...>. Without the extra check for substituting an empty
8821 pack past the last parameter, we can accept the check as valid.
8822
8823 FIXME: This may be valid for alias templates (but I doubt it).
8824
8825 FIXME: The error could be better also. */
8826 if (in_decl && concept_definition_p (in_decl))
8827 {
8828 if (complain & tf_error)
8829 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8830 "too many arguments");
8831 return error_mark_node;
8832 }
8833
8834 int len = nparms + (nargs - arg_idx);
8835 tree args = make_tree_vec (len);
8836 int i = 0;
8837 for (; i < nparms; ++i)
8838 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8839 for (; i < len; ++i, ++arg_idx)
8840 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8841 arg_idx - pack_adjust);
8842 new_inner_args = args;
8843 }
8844
8845 if (lost)
8846 {
8847 gcc_assert (!(complain & tf_error) || seen_error ());
8848 return error_mark_node;
8849 }
8850
8851 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8852 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8853 TREE_VEC_LENGTH (new_inner_args));
8854
8855 return new_inner_args;
8856 }
8857
8858 /* Convert all template arguments to their appropriate types, and
8859 return a vector containing the innermost resulting template
8860 arguments. If any error occurs, return error_mark_node. Error and
8861 warning messages are not issued.
8862
8863 Note that no function argument deduction is performed, and default
8864 arguments are used to fill in unspecified arguments. */
8865 tree
8866 coerce_template_parms (tree parms, tree args, tree in_decl)
8867 {
8868 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8869 }
8870
8871 /* Convert all template arguments to their appropriate type, and
8872 instantiate default arguments as needed. This returns a vector
8873 containing the innermost resulting template arguments, or
8874 error_mark_node if unsuccessful. */
8875 tree
8876 coerce_template_parms (tree parms, tree args, tree in_decl,
8877 tsubst_flags_t complain)
8878 {
8879 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8880 }
8881
8882 /* Like coerce_template_parms. If PARMS represents all template
8883 parameters levels, this function returns a vector of vectors
8884 representing all the resulting argument levels. Note that in this
8885 case, only the innermost arguments are coerced because the
8886 outermost ones are supposed to have been coerced already.
8887
8888 Otherwise, if PARMS represents only (the innermost) vector of
8889 parameters, this function returns a vector containing just the
8890 innermost resulting arguments. */
8891
8892 static tree
8893 coerce_innermost_template_parms (tree parms,
8894 tree args,
8895 tree in_decl,
8896 tsubst_flags_t complain,
8897 bool require_all_args,
8898 bool use_default_args)
8899 {
8900 int parms_depth = TMPL_PARMS_DEPTH (parms);
8901 int args_depth = TMPL_ARGS_DEPTH (args);
8902 tree coerced_args;
8903
8904 if (parms_depth > 1)
8905 {
8906 coerced_args = make_tree_vec (parms_depth);
8907 tree level;
8908 int cur_depth;
8909
8910 for (level = parms, cur_depth = parms_depth;
8911 parms_depth > 0 && level != NULL_TREE;
8912 level = TREE_CHAIN (level), --cur_depth)
8913 {
8914 tree l;
8915 if (cur_depth == args_depth)
8916 l = coerce_template_parms (TREE_VALUE (level),
8917 args, in_decl, complain,
8918 require_all_args,
8919 use_default_args);
8920 else
8921 l = TMPL_ARGS_LEVEL (args, cur_depth);
8922
8923 if (l == error_mark_node)
8924 return error_mark_node;
8925
8926 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8927 }
8928 }
8929 else
8930 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8931 args, in_decl, complain,
8932 require_all_args,
8933 use_default_args);
8934 return coerced_args;
8935 }
8936
8937 /* Returns 1 if template args OT and NT are equivalent. */
8938
8939 int
8940 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8941 {
8942 if (nt == ot)
8943 return 1;
8944 if (nt == NULL_TREE || ot == NULL_TREE)
8945 return false;
8946 if (nt == any_targ_node || ot == any_targ_node)
8947 return true;
8948
8949 if (TREE_CODE (nt) == TREE_VEC)
8950 /* For member templates */
8951 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8952 else if (PACK_EXPANSION_P (ot))
8953 return (PACK_EXPANSION_P (nt)
8954 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8955 PACK_EXPANSION_PATTERN (nt))
8956 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8957 PACK_EXPANSION_EXTRA_ARGS (nt)));
8958 else if (ARGUMENT_PACK_P (ot))
8959 {
8960 int i, len;
8961 tree opack, npack;
8962
8963 if (!ARGUMENT_PACK_P (nt))
8964 return 0;
8965
8966 opack = ARGUMENT_PACK_ARGS (ot);
8967 npack = ARGUMENT_PACK_ARGS (nt);
8968 len = TREE_VEC_LENGTH (opack);
8969 if (TREE_VEC_LENGTH (npack) != len)
8970 return 0;
8971 for (i = 0; i < len; ++i)
8972 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8973 TREE_VEC_ELT (npack, i)))
8974 return 0;
8975 return 1;
8976 }
8977 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8978 gcc_unreachable ();
8979 else if (TYPE_P (nt))
8980 {
8981 if (!TYPE_P (ot))
8982 return false;
8983 /* Don't treat an alias template specialization with dependent
8984 arguments as equivalent to its underlying type when used as a
8985 template argument; we need them to be distinct so that we
8986 substitute into the specialization arguments at instantiation
8987 time. And aliases can't be equivalent without being ==, so
8988 we don't need to look any deeper.
8989
8990 During partial ordering, however, we need to treat them normally so
8991 that we can order uses of the same alias with different
8992 cv-qualification (79960). */
8993 if (!partial_order
8994 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8995 return false;
8996 else
8997 return same_type_p (ot, nt);
8998 }
8999 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
9000 return 0;
9001 else
9002 {
9003 /* Try to treat a template non-type argument that has been converted
9004 to the parameter type as equivalent to one that hasn't yet. */
9005 for (enum tree_code code1 = TREE_CODE (ot);
9006 CONVERT_EXPR_CODE_P (code1)
9007 || code1 == NON_LVALUE_EXPR;
9008 code1 = TREE_CODE (ot))
9009 ot = TREE_OPERAND (ot, 0);
9010 for (enum tree_code code2 = TREE_CODE (nt);
9011 CONVERT_EXPR_CODE_P (code2)
9012 || code2 == NON_LVALUE_EXPR;
9013 code2 = TREE_CODE (nt))
9014 nt = TREE_OPERAND (nt, 0);
9015
9016 return cp_tree_equal (ot, nt);
9017 }
9018 }
9019
9020 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9021 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9022 NEWARG_PTR with the offending arguments if they are non-NULL. */
9023
9024 int
9025 comp_template_args (tree oldargs, tree newargs,
9026 tree *oldarg_ptr, tree *newarg_ptr,
9027 bool partial_order)
9028 {
9029 int i;
9030
9031 if (oldargs == newargs)
9032 return 1;
9033
9034 if (!oldargs || !newargs)
9035 return 0;
9036
9037 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9038 return 0;
9039
9040 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9041 {
9042 tree nt = TREE_VEC_ELT (newargs, i);
9043 tree ot = TREE_VEC_ELT (oldargs, i);
9044
9045 if (! template_args_equal (ot, nt, partial_order))
9046 {
9047 if (oldarg_ptr != NULL)
9048 *oldarg_ptr = ot;
9049 if (newarg_ptr != NULL)
9050 *newarg_ptr = nt;
9051 return 0;
9052 }
9053 }
9054 return 1;
9055 }
9056
9057 inline bool
9058 comp_template_args_porder (tree oargs, tree nargs)
9059 {
9060 return comp_template_args (oargs, nargs, NULL, NULL, true);
9061 }
9062
9063 /* Implement a freelist interface for objects of type T.
9064
9065 Head is a separate object, rather than a regular member, so that we
9066 can define it as a GTY deletable pointer, which is highly
9067 desirable. A data member could be declared that way, but then the
9068 containing object would implicitly get GTY((user)), which would
9069 prevent us from instantiating freelists as global objects.
9070 Although this way we can create freelist global objects, they're
9071 such thin wrappers that instantiating temporaries at every use
9072 loses nothing and saves permanent storage for the freelist object.
9073
9074 Member functions next, anew, poison and reinit have default
9075 implementations that work for most of the types we're interested
9076 in, but if they don't work for some type, they should be explicitly
9077 specialized. See the comments before them for requirements, and
9078 the example specializations for the tree_list_freelist. */
9079 template <typename T>
9080 class freelist
9081 {
9082 /* Return the next object in a chain. We could just do type
9083 punning, but if we access the object with its underlying type, we
9084 avoid strict-aliasing trouble. This needs only work between
9085 poison and reinit. */
9086 static T *&next (T *obj) { return obj->next; }
9087
9088 /* Return a newly allocated, uninitialized or minimally-initialized
9089 object of type T. Any initialization performed by anew should
9090 either remain across the life of the object and the execution of
9091 poison, or be redone by reinit. */
9092 static T *anew () { return ggc_alloc<T> (); }
9093
9094 /* Optionally scribble all over the bits holding the object, so that
9095 they become (mostly?) uninitialized memory. This is called while
9096 preparing to make the object part of the free list. */
9097 static void poison (T *obj) {
9098 T *p ATTRIBUTE_UNUSED = obj;
9099 T **q ATTRIBUTE_UNUSED = &next (obj);
9100
9101 #ifdef ENABLE_GC_CHECKING
9102 /* Poison the data, to indicate the data is garbage. */
9103 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9104 memset (p, 0xa5, sizeof (*p));
9105 #endif
9106 /* Let valgrind know the object is free. */
9107 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9108
9109 /* Let valgrind know the next portion of the object is available,
9110 but uninitialized. */
9111 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9112 }
9113
9114 /* Bring an object that underwent at least one lifecycle after anew
9115 and before the most recent free and poison, back to a usable
9116 state, reinitializing whatever is needed for it to be
9117 functionally equivalent to an object just allocated and returned
9118 by anew. This may poison or clear the next field, used by
9119 freelist housekeeping after poison was called. */
9120 static void reinit (T *obj) {
9121 T **q ATTRIBUTE_UNUSED = &next (obj);
9122
9123 #ifdef ENABLE_GC_CHECKING
9124 memset (q, 0xa5, sizeof (*q));
9125 #endif
9126 /* Let valgrind know the entire object is available, but
9127 uninitialized. */
9128 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9129 }
9130
9131 /* Reference a GTY-deletable pointer that points to the first object
9132 in the free list proper. */
9133 T *&head;
9134 public:
9135 /* Construct a freelist object chaining objects off of HEAD. */
9136 freelist (T *&head) : head(head) {}
9137
9138 /* Add OBJ to the free object list. The former head becomes OBJ's
9139 successor. */
9140 void free (T *obj)
9141 {
9142 poison (obj);
9143 next (obj) = head;
9144 head = obj;
9145 }
9146
9147 /* Take an object from the free list, if one is available, or
9148 allocate a new one. Objects taken from the free list should be
9149 regarded as filled with garbage, except for bits that are
9150 configured to be preserved across free and alloc. */
9151 T *alloc ()
9152 {
9153 if (head)
9154 {
9155 T *obj = head;
9156 head = next (head);
9157 reinit (obj);
9158 return obj;
9159 }
9160 else
9161 return anew ();
9162 }
9163 };
9164
9165 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9166 want to allocate a TREE_LIST using the usual interface, and ensure
9167 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9168 build_tree_list logic in reinit, so this could go out of sync. */
9169 template <>
9170 inline tree &
9171 freelist<tree_node>::next (tree obj)
9172 {
9173 return TREE_CHAIN (obj);
9174 }
9175 template <>
9176 inline tree
9177 freelist<tree_node>::anew ()
9178 {
9179 return build_tree_list (NULL, NULL);
9180 }
9181 template <>
9182 inline void
9183 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9184 {
9185 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9186 tree p ATTRIBUTE_UNUSED = obj;
9187 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9188 tree *q ATTRIBUTE_UNUSED = &next (obj);
9189
9190 #ifdef ENABLE_GC_CHECKING
9191 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9192
9193 /* Poison the data, to indicate the data is garbage. */
9194 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9195 memset (p, 0xa5, size);
9196 #endif
9197 /* Let valgrind know the object is free. */
9198 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9199 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9200 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9201 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9202
9203 #ifdef ENABLE_GC_CHECKING
9204 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9205 /* Keep TREE_CHAIN functional. */
9206 TREE_SET_CODE (obj, TREE_LIST);
9207 #else
9208 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9209 #endif
9210 }
9211 template <>
9212 inline void
9213 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9214 {
9215 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9216
9217 #ifdef ENABLE_GC_CHECKING
9218 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9219 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9220 memset (obj, 0, sizeof (tree_list));
9221 #endif
9222
9223 /* Let valgrind know the entire object is available, but
9224 uninitialized. */
9225 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9226
9227 #ifdef ENABLE_GC_CHECKING
9228 TREE_SET_CODE (obj, TREE_LIST);
9229 #else
9230 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9231 #endif
9232 }
9233
9234 /* Point to the first object in the TREE_LIST freelist. */
9235 static GTY((deletable)) tree tree_list_freelist_head;
9236 /* Return the/an actual TREE_LIST freelist. */
9237 static inline freelist<tree_node>
9238 tree_list_freelist ()
9239 {
9240 return tree_list_freelist_head;
9241 }
9242
9243 /* Point to the first object in the tinst_level freelist. */
9244 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9245 /* Return the/an actual tinst_level freelist. */
9246 static inline freelist<tinst_level>
9247 tinst_level_freelist ()
9248 {
9249 return tinst_level_freelist_head;
9250 }
9251
9252 /* Point to the first object in the pending_template freelist. */
9253 static GTY((deletable)) pending_template *pending_template_freelist_head;
9254 /* Return the/an actual pending_template freelist. */
9255 static inline freelist<pending_template>
9256 pending_template_freelist ()
9257 {
9258 return pending_template_freelist_head;
9259 }
9260
9261 /* Build the TREE_LIST object out of a split list, store it
9262 permanently, and return it. */
9263 tree
9264 tinst_level::to_list ()
9265 {
9266 gcc_assert (split_list_p ());
9267 tree ret = tree_list_freelist ().alloc ();
9268 TREE_PURPOSE (ret) = tldcl;
9269 TREE_VALUE (ret) = targs;
9270 tldcl = ret;
9271 targs = NULL;
9272 gcc_assert (tree_list_p ());
9273 return ret;
9274 }
9275
9276 const unsigned short tinst_level::refcount_infinity;
9277
9278 /* Increment OBJ's refcount unless it is already infinite. */
9279 static tinst_level *
9280 inc_refcount_use (tinst_level *obj)
9281 {
9282 if (obj && obj->refcount != tinst_level::refcount_infinity)
9283 ++obj->refcount;
9284 return obj;
9285 }
9286
9287 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9288 void
9289 tinst_level::free (tinst_level *obj)
9290 {
9291 if (obj->tree_list_p ())
9292 tree_list_freelist ().free (obj->get_node ());
9293 tinst_level_freelist ().free (obj);
9294 }
9295
9296 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9297 OBJ's DECL and OBJ, and start over with the tinst_level object that
9298 used to be referenced by OBJ's NEXT. */
9299 static void
9300 dec_refcount_use (tinst_level *obj)
9301 {
9302 while (obj
9303 && obj->refcount != tinst_level::refcount_infinity
9304 && !--obj->refcount)
9305 {
9306 tinst_level *next = obj->next;
9307 tinst_level::free (obj);
9308 obj = next;
9309 }
9310 }
9311
9312 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9313 and of the former PTR. Omitting the second argument is equivalent
9314 to passing (T*)NULL; this is allowed because passing the
9315 zero-valued integral constant NULL confuses type deduction and/or
9316 overload resolution. */
9317 template <typename T>
9318 static void
9319 set_refcount_ptr (T *& ptr, T *obj = NULL)
9320 {
9321 T *save = ptr;
9322 ptr = inc_refcount_use (obj);
9323 dec_refcount_use (save);
9324 }
9325
9326 static void
9327 add_pending_template (tree d)
9328 {
9329 tree ti = (TYPE_P (d)
9330 ? CLASSTYPE_TEMPLATE_INFO (d)
9331 : DECL_TEMPLATE_INFO (d));
9332 struct pending_template *pt;
9333 int level;
9334
9335 if (TI_PENDING_TEMPLATE_FLAG (ti))
9336 return;
9337
9338 /* We are called both from instantiate_decl, where we've already had a
9339 tinst_level pushed, and instantiate_template, where we haven't.
9340 Compensate. */
9341 gcc_assert (TREE_CODE (d) != TREE_LIST);
9342 level = !current_tinst_level
9343 || current_tinst_level->maybe_get_node () != d;
9344
9345 if (level)
9346 push_tinst_level (d);
9347
9348 pt = pending_template_freelist ().alloc ();
9349 pt->next = NULL;
9350 pt->tinst = NULL;
9351 set_refcount_ptr (pt->tinst, current_tinst_level);
9352 if (last_pending_template)
9353 last_pending_template->next = pt;
9354 else
9355 pending_templates = pt;
9356
9357 last_pending_template = pt;
9358
9359 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9360
9361 if (level)
9362 pop_tinst_level ();
9363 }
9364
9365
9366 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9367 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9368 documentation for TEMPLATE_ID_EXPR. */
9369
9370 tree
9371 lookup_template_function (tree fns, tree arglist)
9372 {
9373 if (fns == error_mark_node || arglist == error_mark_node)
9374 return error_mark_node;
9375
9376 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9377
9378 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9379 {
9380 error ("%q#D is not a function template", fns);
9381 return error_mark_node;
9382 }
9383
9384 if (BASELINK_P (fns))
9385 {
9386 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9387 unknown_type_node,
9388 BASELINK_FUNCTIONS (fns),
9389 arglist);
9390 return fns;
9391 }
9392
9393 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9394 }
9395
9396 /* Within the scope of a template class S<T>, the name S gets bound
9397 (in build_self_reference) to a TYPE_DECL for the class, not a
9398 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9399 or one of its enclosing classes, and that type is a template,
9400 return the associated TEMPLATE_DECL. Otherwise, the original
9401 DECL is returned.
9402
9403 Also handle the case when DECL is a TREE_LIST of ambiguous
9404 injected-class-names from different bases. */
9405
9406 tree
9407 maybe_get_template_decl_from_type_decl (tree decl)
9408 {
9409 if (decl == NULL_TREE)
9410 return decl;
9411
9412 /* DR 176: A lookup that finds an injected-class-name (10.2
9413 [class.member.lookup]) can result in an ambiguity in certain cases
9414 (for example, if it is found in more than one base class). If all of
9415 the injected-class-names that are found refer to specializations of
9416 the same class template, and if the name is followed by a
9417 template-argument-list, the reference refers to the class template
9418 itself and not a specialization thereof, and is not ambiguous. */
9419 if (TREE_CODE (decl) == TREE_LIST)
9420 {
9421 tree t, tmpl = NULL_TREE;
9422 for (t = decl; t; t = TREE_CHAIN (t))
9423 {
9424 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9425 if (!tmpl)
9426 tmpl = elt;
9427 else if (tmpl != elt)
9428 break;
9429 }
9430 if (tmpl && t == NULL_TREE)
9431 return tmpl;
9432 else
9433 return decl;
9434 }
9435
9436 return (decl != NULL_TREE
9437 && DECL_SELF_REFERENCE_P (decl)
9438 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9439 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9440 }
9441
9442 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9443 parameters, find the desired type.
9444
9445 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9446
9447 IN_DECL, if non-NULL, is the template declaration we are trying to
9448 instantiate.
9449
9450 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9451 the class we are looking up.
9452
9453 Issue error and warning messages under control of COMPLAIN.
9454
9455 If the template class is really a local class in a template
9456 function, then the FUNCTION_CONTEXT is the function in which it is
9457 being instantiated.
9458
9459 ??? Note that this function is currently called *twice* for each
9460 template-id: the first time from the parser, while creating the
9461 incomplete type (finish_template_type), and the second type during the
9462 real instantiation (instantiate_template_class). This is surely something
9463 that we want to avoid. It also causes some problems with argument
9464 coercion (see convert_nontype_argument for more information on this). */
9465
9466 static tree
9467 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9468 int entering_scope, tsubst_flags_t complain)
9469 {
9470 tree templ = NULL_TREE, parmlist;
9471 tree t;
9472 spec_entry **slot;
9473 spec_entry *entry;
9474 spec_entry elt;
9475 hashval_t hash;
9476
9477 if (identifier_p (d1))
9478 {
9479 tree value = innermost_non_namespace_value (d1);
9480 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9481 templ = value;
9482 else
9483 {
9484 if (context)
9485 push_decl_namespace (context);
9486 templ = lookup_name (d1);
9487 templ = maybe_get_template_decl_from_type_decl (templ);
9488 if (context)
9489 pop_decl_namespace ();
9490 }
9491 if (templ)
9492 context = DECL_CONTEXT (templ);
9493 }
9494 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9495 {
9496 tree type = TREE_TYPE (d1);
9497
9498 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9499 an implicit typename for the second A. Deal with it. */
9500 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9501 type = TREE_TYPE (type);
9502
9503 if (CLASSTYPE_TEMPLATE_INFO (type))
9504 {
9505 templ = CLASSTYPE_TI_TEMPLATE (type);
9506 d1 = DECL_NAME (templ);
9507 }
9508 }
9509 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9510 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9511 {
9512 templ = TYPE_TI_TEMPLATE (d1);
9513 d1 = DECL_NAME (templ);
9514 }
9515 else if (DECL_TYPE_TEMPLATE_P (d1))
9516 {
9517 templ = d1;
9518 d1 = DECL_NAME (templ);
9519 context = DECL_CONTEXT (templ);
9520 }
9521 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9522 {
9523 templ = d1;
9524 d1 = DECL_NAME (templ);
9525 }
9526
9527 /* Issue an error message if we didn't find a template. */
9528 if (! templ)
9529 {
9530 if (complain & tf_error)
9531 error ("%qT is not a template", d1);
9532 return error_mark_node;
9533 }
9534
9535 if (TREE_CODE (templ) != TEMPLATE_DECL
9536 /* Make sure it's a user visible template, if it was named by
9537 the user. */
9538 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9539 && !PRIMARY_TEMPLATE_P (templ)))
9540 {
9541 if (complain & tf_error)
9542 {
9543 error ("non-template type %qT used as a template", d1);
9544 if (in_decl)
9545 error ("for template declaration %q+D", in_decl);
9546 }
9547 return error_mark_node;
9548 }
9549
9550 complain &= ~tf_user;
9551
9552 /* An alias that just changes the name of a template is equivalent to the
9553 other template, so if any of the arguments are pack expansions, strip
9554 the alias to avoid problems with a pack expansion passed to a non-pack
9555 alias template parameter (DR 1430). */
9556 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9557 templ = get_underlying_template (templ);
9558
9559 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9560 {
9561 tree parm;
9562 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9563 if (arglist2 == error_mark_node
9564 || (!uses_template_parms (arglist2)
9565 && check_instantiated_args (templ, arglist2, complain)))
9566 return error_mark_node;
9567
9568 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9569 return parm;
9570 }
9571 else
9572 {
9573 tree template_type = TREE_TYPE (templ);
9574 tree gen_tmpl;
9575 tree type_decl;
9576 tree found = NULL_TREE;
9577 int arg_depth;
9578 int parm_depth;
9579 int is_dependent_type;
9580 int use_partial_inst_tmpl = false;
9581
9582 if (template_type == error_mark_node)
9583 /* An error occurred while building the template TEMPL, and a
9584 diagnostic has most certainly been emitted for that
9585 already. Let's propagate that error. */
9586 return error_mark_node;
9587
9588 gen_tmpl = most_general_template (templ);
9589 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9590 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9591 arg_depth = TMPL_ARGS_DEPTH (arglist);
9592
9593 if (arg_depth == 1 && parm_depth > 1)
9594 {
9595 /* We've been given an incomplete set of template arguments.
9596 For example, given:
9597
9598 template <class T> struct S1 {
9599 template <class U> struct S2 {};
9600 template <class U> struct S2<U*> {};
9601 };
9602
9603 we will be called with an ARGLIST of `U*', but the
9604 TEMPLATE will be `template <class T> template
9605 <class U> struct S1<T>::S2'. We must fill in the missing
9606 arguments. */
9607 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9608 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9609 arg_depth = TMPL_ARGS_DEPTH (arglist);
9610 }
9611
9612 /* Now we should have enough arguments. */
9613 gcc_assert (parm_depth == arg_depth);
9614
9615 /* From here on, we're only interested in the most general
9616 template. */
9617
9618 /* Calculate the BOUND_ARGS. These will be the args that are
9619 actually tsubst'd into the definition to create the
9620 instantiation. */
9621 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9622 complain,
9623 /*require_all_args=*/true,
9624 /*use_default_args=*/true);
9625
9626 if (arglist == error_mark_node)
9627 /* We were unable to bind the arguments. */
9628 return error_mark_node;
9629
9630 /* In the scope of a template class, explicit references to the
9631 template class refer to the type of the template, not any
9632 instantiation of it. For example, in:
9633
9634 template <class T> class C { void f(C<T>); }
9635
9636 the `C<T>' is just the same as `C'. Outside of the
9637 class, however, such a reference is an instantiation. */
9638 if (entering_scope
9639 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9640 || currently_open_class (template_type))
9641 {
9642 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9643
9644 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9645 return template_type;
9646 }
9647
9648 /* If we already have this specialization, return it. */
9649 elt.tmpl = gen_tmpl;
9650 elt.args = arglist;
9651 elt.spec = NULL_TREE;
9652 hash = spec_hasher::hash (&elt);
9653 entry = type_specializations->find_with_hash (&elt, hash);
9654
9655 if (entry)
9656 return entry->spec;
9657
9658 /* If the the template's constraints are not satisfied,
9659 then we cannot form a valid type.
9660
9661 Note that the check is deferred until after the hash
9662 lookup. This prevents redundant checks on previously
9663 instantiated specializations. */
9664 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9665 {
9666 if (complain & tf_error)
9667 {
9668 auto_diagnostic_group d;
9669 error ("template constraint failure for %qD", gen_tmpl);
9670 diagnose_constraints (input_location, gen_tmpl, arglist);
9671 }
9672 return error_mark_node;
9673 }
9674
9675 is_dependent_type = uses_template_parms (arglist);
9676
9677 /* If the deduced arguments are invalid, then the binding
9678 failed. */
9679 if (!is_dependent_type
9680 && check_instantiated_args (gen_tmpl,
9681 INNERMOST_TEMPLATE_ARGS (arglist),
9682 complain))
9683 return error_mark_node;
9684
9685 if (!is_dependent_type
9686 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9687 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9688 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9689 {
9690 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9691 DECL_NAME (gen_tmpl),
9692 /*tag_scope=*/ts_global);
9693 return found;
9694 }
9695
9696 context = DECL_CONTEXT (gen_tmpl);
9697 if (context && TYPE_P (context))
9698 {
9699 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9700 context = complete_type (context);
9701 }
9702 else
9703 context = tsubst (context, arglist, complain, in_decl);
9704
9705 if (context == error_mark_node)
9706 return error_mark_node;
9707
9708 if (!context)
9709 context = global_namespace;
9710
9711 /* Create the type. */
9712 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9713 {
9714 /* The user referred to a specialization of an alias
9715 template represented by GEN_TMPL.
9716
9717 [temp.alias]/2 says:
9718
9719 When a template-id refers to the specialization of an
9720 alias template, it is equivalent to the associated
9721 type obtained by substitution of its
9722 template-arguments for the template-parameters in the
9723 type-id of the alias template. */
9724
9725 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9726 /* Note that the call above (by indirectly calling
9727 register_specialization in tsubst_decl) registers the
9728 TYPE_DECL representing the specialization of the alias
9729 template. So next time someone substitutes ARGLIST for
9730 the template parms into the alias template (GEN_TMPL),
9731 she'll get that TYPE_DECL back. */
9732
9733 if (t == error_mark_node)
9734 return t;
9735 }
9736 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9737 {
9738 if (!is_dependent_type)
9739 {
9740 set_current_access_from_decl (TYPE_NAME (template_type));
9741 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9742 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9743 arglist, complain, in_decl),
9744 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9745 arglist, complain, in_decl),
9746 SCOPED_ENUM_P (template_type), NULL);
9747
9748 if (t == error_mark_node)
9749 return t;
9750 }
9751 else
9752 {
9753 /* We don't want to call start_enum for this type, since
9754 the values for the enumeration constants may involve
9755 template parameters. And, no one should be interested
9756 in the enumeration constants for such a type. */
9757 t = cxx_make_type (ENUMERAL_TYPE);
9758 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9759 }
9760 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9761 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9762 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9763 }
9764 else if (CLASS_TYPE_P (template_type))
9765 {
9766 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9767 instantiated here. */
9768 gcc_assert (!LAMBDA_TYPE_P (template_type));
9769
9770 t = make_class_type (TREE_CODE (template_type));
9771 CLASSTYPE_DECLARED_CLASS (t)
9772 = CLASSTYPE_DECLARED_CLASS (template_type);
9773 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9774
9775 /* A local class. Make sure the decl gets registered properly. */
9776 if (context == current_function_decl)
9777 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9778 == error_mark_node)
9779 return error_mark_node;
9780
9781 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9782 /* This instantiation is another name for the primary
9783 template type. Set the TYPE_CANONICAL field
9784 appropriately. */
9785 TYPE_CANONICAL (t) = template_type;
9786 else if (any_template_arguments_need_structural_equality_p (arglist))
9787 /* Some of the template arguments require structural
9788 equality testing, so this template class requires
9789 structural equality testing. */
9790 SET_TYPE_STRUCTURAL_EQUALITY (t);
9791 }
9792 else
9793 gcc_unreachable ();
9794
9795 /* If we called start_enum or pushtag above, this information
9796 will already be set up. */
9797 if (!TYPE_NAME (t))
9798 {
9799 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9800
9801 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9802 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9803 DECL_SOURCE_LOCATION (type_decl)
9804 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9805 }
9806 else
9807 type_decl = TYPE_NAME (t);
9808
9809 if (CLASS_TYPE_P (template_type))
9810 {
9811 TREE_PRIVATE (type_decl)
9812 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9813 TREE_PROTECTED (type_decl)
9814 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9815 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9816 {
9817 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9818 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9819 }
9820 }
9821
9822 if (OVERLOAD_TYPE_P (t)
9823 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9824 {
9825 static const char *tags[] = {"abi_tag", "may_alias"};
9826
9827 for (unsigned ix = 0; ix != 2; ix++)
9828 {
9829 tree attributes
9830 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9831
9832 if (attributes)
9833 TYPE_ATTRIBUTES (t)
9834 = tree_cons (TREE_PURPOSE (attributes),
9835 TREE_VALUE (attributes),
9836 TYPE_ATTRIBUTES (t));
9837 }
9838 }
9839
9840 /* Let's consider the explicit specialization of a member
9841 of a class template specialization that is implicitly instantiated,
9842 e.g.:
9843 template<class T>
9844 struct S
9845 {
9846 template<class U> struct M {}; //#0
9847 };
9848
9849 template<>
9850 template<>
9851 struct S<int>::M<char> //#1
9852 {
9853 int i;
9854 };
9855 [temp.expl.spec]/4 says this is valid.
9856
9857 In this case, when we write:
9858 S<int>::M<char> m;
9859
9860 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9861 the one of #0.
9862
9863 When we encounter #1, we want to store the partial instantiation
9864 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9865
9866 For all cases other than this "explicit specialization of member of a
9867 class template", we just want to store the most general template into
9868 the CLASSTYPE_TI_TEMPLATE of M.
9869
9870 This case of "explicit specialization of member of a class template"
9871 only happens when:
9872 1/ the enclosing class is an instantiation of, and therefore not
9873 the same as, the context of the most general template, and
9874 2/ we aren't looking at the partial instantiation itself, i.e.
9875 the innermost arguments are not the same as the innermost parms of
9876 the most general template.
9877
9878 So it's only when 1/ and 2/ happens that we want to use the partial
9879 instantiation of the member template in lieu of its most general
9880 template. */
9881
9882 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9883 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9884 /* the enclosing class must be an instantiation... */
9885 && CLASS_TYPE_P (context)
9886 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9887 {
9888 TREE_VEC_LENGTH (arglist)--;
9889 ++processing_template_decl;
9890 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9891 tree partial_inst_args =
9892 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9893 arglist, complain, NULL_TREE);
9894 --processing_template_decl;
9895 TREE_VEC_LENGTH (arglist)++;
9896 if (partial_inst_args == error_mark_node)
9897 return error_mark_node;
9898 use_partial_inst_tmpl =
9899 /*...and we must not be looking at the partial instantiation
9900 itself. */
9901 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9902 partial_inst_args);
9903 }
9904
9905 if (!use_partial_inst_tmpl)
9906 /* This case is easy; there are no member templates involved. */
9907 found = gen_tmpl;
9908 else
9909 {
9910 /* This is a full instantiation of a member template. Find
9911 the partial instantiation of which this is an instance. */
9912
9913 /* Temporarily reduce by one the number of levels in the ARGLIST
9914 so as to avoid comparing the last set of arguments. */
9915 TREE_VEC_LENGTH (arglist)--;
9916 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9917 TREE_VEC_LENGTH (arglist)++;
9918 /* FOUND is either a proper class type, or an alias
9919 template specialization. In the later case, it's a
9920 TYPE_DECL, resulting from the substituting of arguments
9921 for parameters in the TYPE_DECL of the alias template
9922 done earlier. So be careful while getting the template
9923 of FOUND. */
9924 found = (TREE_CODE (found) == TEMPLATE_DECL
9925 ? found
9926 : (TREE_CODE (found) == TYPE_DECL
9927 ? DECL_TI_TEMPLATE (found)
9928 : CLASSTYPE_TI_TEMPLATE (found)));
9929
9930 if (DECL_CLASS_TEMPLATE_P (found)
9931 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9932 {
9933 /* If this partial instantiation is specialized, we want to
9934 use it for hash table lookup. */
9935 elt.tmpl = found;
9936 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9937 hash = spec_hasher::hash (&elt);
9938 }
9939 }
9940
9941 // Build template info for the new specialization.
9942 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9943
9944 elt.spec = t;
9945 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9946 gcc_checking_assert (*slot == NULL);
9947 entry = ggc_alloc<spec_entry> ();
9948 *entry = elt;
9949 *slot = entry;
9950
9951 /* Note this use of the partial instantiation so we can check it
9952 later in maybe_process_partial_specialization. */
9953 DECL_TEMPLATE_INSTANTIATIONS (found)
9954 = tree_cons (arglist, t,
9955 DECL_TEMPLATE_INSTANTIATIONS (found));
9956
9957 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9958 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9959 /* Now that the type has been registered on the instantiations
9960 list, we set up the enumerators. Because the enumeration
9961 constants may involve the enumeration type itself, we make
9962 sure to register the type first, and then create the
9963 constants. That way, doing tsubst_expr for the enumeration
9964 constants won't result in recursive calls here; we'll find
9965 the instantiation and exit above. */
9966 tsubst_enum (template_type, t, arglist);
9967
9968 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9969 /* If the type makes use of template parameters, the
9970 code that generates debugging information will crash. */
9971 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9972
9973 /* Possibly limit visibility based on template args. */
9974 TREE_PUBLIC (type_decl) = 1;
9975 determine_visibility (type_decl);
9976
9977 inherit_targ_abi_tags (t);
9978
9979 return t;
9980 }
9981 }
9982
9983 /* Wrapper for lookup_template_class_1. */
9984
9985 tree
9986 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9987 int entering_scope, tsubst_flags_t complain)
9988 {
9989 tree ret;
9990 timevar_push (TV_TEMPLATE_INST);
9991 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9992 entering_scope, complain);
9993 timevar_pop (TV_TEMPLATE_INST);
9994 return ret;
9995 }
9996
9997 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9998
9999 tree
10000 lookup_template_variable (tree templ, tree arglist)
10001 {
10002 if (flag_concepts && variable_concept_p (templ))
10003 return build_concept_check (templ, arglist, tf_none);
10004
10005 /* The type of the expression is NULL_TREE since the template-id could refer
10006 to an explicit or partial specialization. */
10007 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10008 }
10009
10010 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10011
10012 tree
10013 finish_template_variable (tree var, tsubst_flags_t complain)
10014 {
10015 tree templ = TREE_OPERAND (var, 0);
10016 tree arglist = TREE_OPERAND (var, 1);
10017
10018 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10019 arglist = add_outermost_template_args (tmpl_args, arglist);
10020
10021 templ = most_general_template (templ);
10022 tree parms = DECL_TEMPLATE_PARMS (templ);
10023 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10024 /*req_all*/true,
10025 /*use_default*/true);
10026
10027 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10028 {
10029 if (complain & tf_error)
10030 {
10031 auto_diagnostic_group d;
10032 error ("use of invalid variable template %qE", var);
10033 diagnose_constraints (location_of (var), templ, arglist);
10034 }
10035 return error_mark_node;
10036 }
10037
10038 return instantiate_template (templ, arglist, complain);
10039 }
10040
10041 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10042 TARGS template args, and instantiate it if it's not dependent. */
10043
10044 tree
10045 lookup_and_finish_template_variable (tree templ, tree targs,
10046 tsubst_flags_t complain)
10047 {
10048 templ = lookup_template_variable (templ, targs);
10049 if (!any_dependent_template_arguments_p (targs))
10050 {
10051 templ = finish_template_variable (templ, complain);
10052 mark_used (templ);
10053 }
10054
10055 return convert_from_reference (templ);
10056 }
10057
10058 \f
10059 struct pair_fn_data
10060 {
10061 tree_fn_t fn;
10062 tree_fn_t any_fn;
10063 void *data;
10064 /* True when we should also visit template parameters that occur in
10065 non-deduced contexts. */
10066 bool include_nondeduced_p;
10067 hash_set<tree> *visited;
10068 };
10069
10070 /* Called from for_each_template_parm via walk_tree. */
10071
10072 static tree
10073 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10074 {
10075 tree t = *tp;
10076 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10077 tree_fn_t fn = pfd->fn;
10078 void *data = pfd->data;
10079 tree result = NULL_TREE;
10080
10081 #define WALK_SUBTREE(NODE) \
10082 do \
10083 { \
10084 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10085 pfd->include_nondeduced_p, \
10086 pfd->any_fn); \
10087 if (result) goto out; \
10088 } \
10089 while (0)
10090
10091 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10092 return t;
10093
10094 if (TYPE_P (t)
10095 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10096 WALK_SUBTREE (TYPE_CONTEXT (t));
10097
10098 switch (TREE_CODE (t))
10099 {
10100 case RECORD_TYPE:
10101 if (TYPE_PTRMEMFUNC_P (t))
10102 break;
10103 /* Fall through. */
10104
10105 case UNION_TYPE:
10106 case ENUMERAL_TYPE:
10107 if (!TYPE_TEMPLATE_INFO (t))
10108 *walk_subtrees = 0;
10109 else
10110 WALK_SUBTREE (TYPE_TI_ARGS (t));
10111 break;
10112
10113 case INTEGER_TYPE:
10114 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10115 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10116 break;
10117
10118 case METHOD_TYPE:
10119 /* Since we're not going to walk subtrees, we have to do this
10120 explicitly here. */
10121 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10122 /* Fall through. */
10123
10124 case FUNCTION_TYPE:
10125 /* Check the return type. */
10126 WALK_SUBTREE (TREE_TYPE (t));
10127
10128 /* Check the parameter types. Since default arguments are not
10129 instantiated until they are needed, the TYPE_ARG_TYPES may
10130 contain expressions that involve template parameters. But,
10131 no-one should be looking at them yet. And, once they're
10132 instantiated, they don't contain template parameters, so
10133 there's no point in looking at them then, either. */
10134 {
10135 tree parm;
10136
10137 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10138 WALK_SUBTREE (TREE_VALUE (parm));
10139
10140 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10141 want walk_tree walking into them itself. */
10142 *walk_subtrees = 0;
10143 }
10144
10145 if (flag_noexcept_type)
10146 {
10147 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10148 if (spec)
10149 WALK_SUBTREE (TREE_PURPOSE (spec));
10150 }
10151 break;
10152
10153 case TYPEOF_TYPE:
10154 case DECLTYPE_TYPE:
10155 case UNDERLYING_TYPE:
10156 if (pfd->include_nondeduced_p
10157 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10158 pfd->visited,
10159 pfd->include_nondeduced_p,
10160 pfd->any_fn))
10161 return error_mark_node;
10162 *walk_subtrees = false;
10163 break;
10164
10165 case FUNCTION_DECL:
10166 case VAR_DECL:
10167 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10168 WALK_SUBTREE (DECL_TI_ARGS (t));
10169 /* Fall through. */
10170
10171 case PARM_DECL:
10172 case CONST_DECL:
10173 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10174 WALK_SUBTREE (DECL_INITIAL (t));
10175 if (DECL_CONTEXT (t)
10176 && pfd->include_nondeduced_p)
10177 WALK_SUBTREE (DECL_CONTEXT (t));
10178 break;
10179
10180 case BOUND_TEMPLATE_TEMPLATE_PARM:
10181 /* Record template parameters such as `T' inside `TT<T>'. */
10182 WALK_SUBTREE (TYPE_TI_ARGS (t));
10183 /* Fall through. */
10184
10185 case TEMPLATE_TEMPLATE_PARM:
10186 case TEMPLATE_TYPE_PARM:
10187 case TEMPLATE_PARM_INDEX:
10188 if (fn && (*fn)(t, data))
10189 return t;
10190 else if (!fn)
10191 return t;
10192 break;
10193
10194 case TEMPLATE_DECL:
10195 /* A template template parameter is encountered. */
10196 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10197 WALK_SUBTREE (TREE_TYPE (t));
10198
10199 /* Already substituted template template parameter */
10200 *walk_subtrees = 0;
10201 break;
10202
10203 case TYPENAME_TYPE:
10204 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10205 partial instantiation. */
10206 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10207 break;
10208
10209 case CONSTRUCTOR:
10210 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10211 && pfd->include_nondeduced_p)
10212 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10213 break;
10214
10215 case INDIRECT_REF:
10216 case COMPONENT_REF:
10217 /* If there's no type, then this thing must be some expression
10218 involving template parameters. */
10219 if (!fn && !TREE_TYPE (t))
10220 return error_mark_node;
10221 break;
10222
10223 case MODOP_EXPR:
10224 case CAST_EXPR:
10225 case IMPLICIT_CONV_EXPR:
10226 case REINTERPRET_CAST_EXPR:
10227 case CONST_CAST_EXPR:
10228 case STATIC_CAST_EXPR:
10229 case DYNAMIC_CAST_EXPR:
10230 case ARROW_EXPR:
10231 case DOTSTAR_EXPR:
10232 case TYPEID_EXPR:
10233 case PSEUDO_DTOR_EXPR:
10234 if (!fn)
10235 return error_mark_node;
10236 break;
10237
10238 case SCOPE_REF:
10239 if (pfd->include_nondeduced_p)
10240 WALK_SUBTREE (TREE_OPERAND (t, 0));
10241 break;
10242
10243 case REQUIRES_EXPR:
10244 {
10245 if (!fn)
10246 return error_mark_node;
10247
10248 /* Recursively walk the type of each constraint variable. */
10249 tree p = TREE_OPERAND (t, 0);
10250 while (p)
10251 {
10252 WALK_SUBTREE (TREE_TYPE (p));
10253 p = TREE_CHAIN (p);
10254 }
10255 }
10256 break;
10257
10258 default:
10259 break;
10260 }
10261
10262 #undef WALK_SUBTREE
10263
10264 /* We didn't find any template parameters we liked. */
10265 out:
10266 return result;
10267 }
10268
10269 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10270 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10271 call FN with the parameter and the DATA.
10272 If FN returns nonzero, the iteration is terminated, and
10273 for_each_template_parm returns 1. Otherwise, the iteration
10274 continues. If FN never returns a nonzero value, the value
10275 returned by for_each_template_parm is 0. If FN is NULL, it is
10276 considered to be the function which always returns 1.
10277
10278 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10279 parameters that occur in non-deduced contexts. When false, only
10280 visits those template parameters that can be deduced. */
10281
10282 static tree
10283 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10284 hash_set<tree> *visited,
10285 bool include_nondeduced_p,
10286 tree_fn_t any_fn)
10287 {
10288 struct pair_fn_data pfd;
10289 tree result;
10290
10291 /* Set up. */
10292 pfd.fn = fn;
10293 pfd.any_fn = any_fn;
10294 pfd.data = data;
10295 pfd.include_nondeduced_p = include_nondeduced_p;
10296
10297 /* Walk the tree. (Conceptually, we would like to walk without
10298 duplicates, but for_each_template_parm_r recursively calls
10299 for_each_template_parm, so we would need to reorganize a fair
10300 bit to use walk_tree_without_duplicates, so we keep our own
10301 visited list.) */
10302 if (visited)
10303 pfd.visited = visited;
10304 else
10305 pfd.visited = new hash_set<tree>;
10306 result = cp_walk_tree (&t,
10307 for_each_template_parm_r,
10308 &pfd,
10309 pfd.visited);
10310
10311 /* Clean up. */
10312 if (!visited)
10313 {
10314 delete pfd.visited;
10315 pfd.visited = 0;
10316 }
10317
10318 return result;
10319 }
10320
10321 struct find_template_parameter_info
10322 {
10323 explicit find_template_parameter_info (int d)
10324 : max_depth (d)
10325 {}
10326
10327 hash_set<tree> visited;
10328 hash_set<tree> parms;
10329 int max_depth;
10330 };
10331
10332 /* Appends the declaration of T to the list in DATA. */
10333
10334 static int
10335 keep_template_parm (tree t, void* data)
10336 {
10337 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10338
10339 /* Template parameters declared within the expression are not part of
10340 the parameter mapping. For example, in this concept:
10341
10342 template<typename T>
10343 concept C = requires { <expr> } -> same_as<int>;
10344
10345 the return specifier same_as<int> declares a new decltype parameter
10346 that must not be part of the parameter mapping. The same is true
10347 for generic lambda parameters, lambda template parameters, etc. */
10348 int level;
10349 int index;
10350 template_parm_level_and_index (t, &level, &index);
10351 if (level > ftpi->max_depth)
10352 return 0;
10353
10354 /* Arguments like const T yield parameters like const T. This means that
10355 a template-id like X<T, const T> would yield two distinct parameters:
10356 T and const T. Adjust types to their unqualified versions. */
10357 if (TYPE_P (t))
10358 t = TYPE_MAIN_VARIANT (t);
10359 ftpi->parms.add (t);
10360
10361 return 0;
10362 }
10363
10364 /* Ensure that we recursively examine certain terms that are not normally
10365 visited in for_each_template_parm_r. */
10366
10367 static int
10368 any_template_parm_r (tree t, void *data)
10369 {
10370 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10371
10372 #define WALK_SUBTREE(NODE) \
10373 do \
10374 { \
10375 for_each_template_parm (NODE, keep_template_parm, data, \
10376 &ftpi->visited, true, \
10377 any_template_parm_r); \
10378 } \
10379 while (0)
10380
10381 switch (TREE_CODE (t))
10382 {
10383 case RECORD_TYPE:
10384 case UNION_TYPE:
10385 case ENUMERAL_TYPE:
10386 /* Search for template parameters in type aliases. */
10387 if (alias_template_specialization_p (t))
10388 {
10389 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
10390 WALK_SUBTREE (TI_ARGS (tinfo));
10391 }
10392 break;
10393
10394 case TEMPLATE_TYPE_PARM:
10395 /* Type constraints of a placeholder type may contain parameters. */
10396 if (is_auto (t))
10397 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10398 WALK_SUBTREE (constr);
10399 break;
10400
10401 case TEMPLATE_ID_EXPR:
10402 /* Search through references to variable templates. */
10403 WALK_SUBTREE (TREE_OPERAND (t, 0));
10404 WALK_SUBTREE (TREE_OPERAND (t, 1));
10405 break;
10406
10407 case CONSTRUCTOR:
10408 if (TREE_TYPE (t))
10409 WALK_SUBTREE (TREE_TYPE (t));
10410 break;
10411
10412 case PARM_DECL:
10413 /* A parameter or constraint variable may also depend on a template
10414 parameter without explicitly naming it. */
10415 WALK_SUBTREE (TREE_TYPE (t));
10416 break;
10417
10418 default:
10419 break;
10420 }
10421
10422 /* Keep walking. */
10423 return 0;
10424 }
10425
10426 /* Returns a list of unique template parameters found within T. */
10427
10428 tree
10429 find_template_parameters (tree t, int depth)
10430 {
10431 find_template_parameter_info ftpi (depth);
10432 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10433 /*include_nondeduced*/true, any_template_parm_r);
10434 tree list = NULL_TREE;
10435 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10436 iter != ftpi.parms.end(); ++iter)
10437 list = tree_cons (NULL_TREE, *iter, list);
10438 return list;
10439 }
10440
10441 /* Returns true if T depends on any template parameter. */
10442
10443 int
10444 uses_template_parms (tree t)
10445 {
10446 if (t == NULL_TREE)
10447 return false;
10448
10449 bool dependent_p;
10450 int saved_processing_template_decl;
10451
10452 saved_processing_template_decl = processing_template_decl;
10453 if (!saved_processing_template_decl)
10454 processing_template_decl = 1;
10455 if (TYPE_P (t))
10456 dependent_p = dependent_type_p (t);
10457 else if (TREE_CODE (t) == TREE_VEC)
10458 dependent_p = any_dependent_template_arguments_p (t);
10459 else if (TREE_CODE (t) == TREE_LIST)
10460 dependent_p = (uses_template_parms (TREE_VALUE (t))
10461 || uses_template_parms (TREE_CHAIN (t)));
10462 else if (TREE_CODE (t) == TYPE_DECL)
10463 dependent_p = dependent_type_p (TREE_TYPE (t));
10464 else if (DECL_P (t)
10465 || EXPR_P (t)
10466 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10467 || TREE_CODE (t) == OVERLOAD
10468 || BASELINK_P (t)
10469 || identifier_p (t)
10470 || TREE_CODE (t) == TRAIT_EXPR
10471 || TREE_CODE (t) == CONSTRUCTOR
10472 || CONSTANT_CLASS_P (t))
10473 dependent_p = (type_dependent_expression_p (t)
10474 || value_dependent_expression_p (t));
10475 else
10476 {
10477 gcc_assert (t == error_mark_node);
10478 dependent_p = false;
10479 }
10480
10481 processing_template_decl = saved_processing_template_decl;
10482
10483 return dependent_p;
10484 }
10485
10486 /* Returns true iff current_function_decl is an incompletely instantiated
10487 template. Useful instead of processing_template_decl because the latter
10488 is set to 0 during instantiate_non_dependent_expr. */
10489
10490 bool
10491 in_template_function (void)
10492 {
10493 tree fn = current_function_decl;
10494 bool ret;
10495 ++processing_template_decl;
10496 ret = (fn && DECL_LANG_SPECIFIC (fn)
10497 && DECL_TEMPLATE_INFO (fn)
10498 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10499 --processing_template_decl;
10500 return ret;
10501 }
10502
10503 /* Returns true if T depends on any template parameter with level LEVEL. */
10504
10505 bool
10506 uses_template_parms_level (tree t, int level)
10507 {
10508 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10509 /*include_nondeduced_p=*/true);
10510 }
10511
10512 /* Returns true if the signature of DECL depends on any template parameter from
10513 its enclosing class. */
10514
10515 bool
10516 uses_outer_template_parms (tree decl)
10517 {
10518 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10519 if (depth == 0)
10520 return false;
10521 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10522 &depth, NULL, /*include_nondeduced_p=*/true))
10523 return true;
10524 if (PRIMARY_TEMPLATE_P (decl)
10525 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10526 (DECL_TEMPLATE_PARMS (decl)),
10527 template_parm_outer_level,
10528 &depth, NULL, /*include_nondeduced_p=*/true))
10529 return true;
10530 tree ci = get_constraints (decl);
10531 if (ci)
10532 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10533 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10534 &depth, NULL, /*nondeduced*/true))
10535 return true;
10536 return false;
10537 }
10538
10539 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10540 ill-formed translation unit, i.e. a variable or function that isn't
10541 usable in a constant expression. */
10542
10543 static inline bool
10544 neglectable_inst_p (tree d)
10545 {
10546 return (d && DECL_P (d)
10547 && !undeduced_auto_decl (d)
10548 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10549 : decl_maybe_constant_var_p (d)));
10550 }
10551
10552 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10553 neglectable and instantiated from within an erroneous instantiation. */
10554
10555 static bool
10556 limit_bad_template_recursion (tree decl)
10557 {
10558 struct tinst_level *lev = current_tinst_level;
10559 int errs = errorcount + sorrycount;
10560 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10561 return false;
10562
10563 for (; lev; lev = lev->next)
10564 if (neglectable_inst_p (lev->maybe_get_node ()))
10565 break;
10566
10567 return (lev && errs > lev->errors);
10568 }
10569
10570 static int tinst_depth;
10571 extern int max_tinst_depth;
10572 int depth_reached;
10573
10574 static GTY(()) struct tinst_level *last_error_tinst_level;
10575
10576 /* We're starting to instantiate D; record the template instantiation context
10577 at LOC for diagnostics and to restore it later. */
10578
10579 static bool
10580 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10581 {
10582 struct tinst_level *new_level;
10583
10584 if (tinst_depth >= max_tinst_depth)
10585 {
10586 /* Tell error.c not to try to instantiate any templates. */
10587 at_eof = 2;
10588 fatal_error (input_location,
10589 "template instantiation depth exceeds maximum of %d"
10590 " (use %<-ftemplate-depth=%> to increase the maximum)",
10591 max_tinst_depth);
10592 return false;
10593 }
10594
10595 /* If the current instantiation caused problems, don't let it instantiate
10596 anything else. Do allow deduction substitution and decls usable in
10597 constant expressions. */
10598 if (!targs && limit_bad_template_recursion (tldcl))
10599 return false;
10600
10601 /* When not -quiet, dump template instantiations other than functions, since
10602 announce_function will take care of those. */
10603 if (!quiet_flag && !targs
10604 && TREE_CODE (tldcl) != TREE_LIST
10605 && TREE_CODE (tldcl) != FUNCTION_DECL)
10606 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10607
10608 new_level = tinst_level_freelist ().alloc ();
10609 new_level->tldcl = tldcl;
10610 new_level->targs = targs;
10611 new_level->locus = loc;
10612 new_level->errors = errorcount + sorrycount;
10613 new_level->next = NULL;
10614 new_level->refcount = 0;
10615 set_refcount_ptr (new_level->next, current_tinst_level);
10616 set_refcount_ptr (current_tinst_level, new_level);
10617
10618 ++tinst_depth;
10619 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10620 depth_reached = tinst_depth;
10621
10622 return true;
10623 }
10624
10625 /* We're starting substitution of TMPL<ARGS>; record the template
10626 substitution context for diagnostics and to restore it later. */
10627
10628 static bool
10629 push_tinst_level (tree tmpl, tree args)
10630 {
10631 return push_tinst_level_loc (tmpl, args, input_location);
10632 }
10633
10634 /* We're starting to instantiate D; record INPUT_LOCATION and the
10635 template instantiation context for diagnostics and to restore it
10636 later. */
10637
10638 bool
10639 push_tinst_level (tree d)
10640 {
10641 return push_tinst_level_loc (d, input_location);
10642 }
10643
10644 /* Likewise, but record LOC as the program location. */
10645
10646 bool
10647 push_tinst_level_loc (tree d, location_t loc)
10648 {
10649 gcc_assert (TREE_CODE (d) != TREE_LIST);
10650 return push_tinst_level_loc (d, NULL, loc);
10651 }
10652
10653 /* We're done instantiating this template; return to the instantiation
10654 context. */
10655
10656 void
10657 pop_tinst_level (void)
10658 {
10659 /* Restore the filename and line number stashed away when we started
10660 this instantiation. */
10661 input_location = current_tinst_level->locus;
10662 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10663 --tinst_depth;
10664 }
10665
10666 /* We're instantiating a deferred template; restore the template
10667 instantiation context in which the instantiation was requested, which
10668 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10669
10670 static tree
10671 reopen_tinst_level (struct tinst_level *level)
10672 {
10673 struct tinst_level *t;
10674
10675 tinst_depth = 0;
10676 for (t = level; t; t = t->next)
10677 ++tinst_depth;
10678
10679 set_refcount_ptr (current_tinst_level, level);
10680 pop_tinst_level ();
10681 if (current_tinst_level)
10682 current_tinst_level->errors = errorcount+sorrycount;
10683 return level->maybe_get_node ();
10684 }
10685
10686 /* Returns the TINST_LEVEL which gives the original instantiation
10687 context. */
10688
10689 struct tinst_level *
10690 outermost_tinst_level (void)
10691 {
10692 struct tinst_level *level = current_tinst_level;
10693 if (level)
10694 while (level->next)
10695 level = level->next;
10696 return level;
10697 }
10698
10699 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10700 vector of template arguments, as for tsubst.
10701
10702 Returns an appropriate tsubst'd friend declaration. */
10703
10704 static tree
10705 tsubst_friend_function (tree decl, tree args)
10706 {
10707 tree new_friend;
10708
10709 if (TREE_CODE (decl) == FUNCTION_DECL
10710 && DECL_TEMPLATE_INSTANTIATION (decl)
10711 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10712 /* This was a friend declared with an explicit template
10713 argument list, e.g.:
10714
10715 friend void f<>(T);
10716
10717 to indicate that f was a template instantiation, not a new
10718 function declaration. Now, we have to figure out what
10719 instantiation of what template. */
10720 {
10721 tree template_id, arglist, fns;
10722 tree new_args;
10723 tree tmpl;
10724 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10725
10726 /* Friend functions are looked up in the containing namespace scope.
10727 We must enter that scope, to avoid finding member functions of the
10728 current class with same name. */
10729 push_nested_namespace (ns);
10730 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10731 tf_warning_or_error, NULL_TREE,
10732 /*integral_constant_expression_p=*/false);
10733 pop_nested_namespace (ns);
10734 arglist = tsubst (DECL_TI_ARGS (decl), args,
10735 tf_warning_or_error, NULL_TREE);
10736 template_id = lookup_template_function (fns, arglist);
10737
10738 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10739 tmpl = determine_specialization (template_id, new_friend,
10740 &new_args,
10741 /*need_member_template=*/0,
10742 TREE_VEC_LENGTH (args),
10743 tsk_none);
10744 return instantiate_template (tmpl, new_args, tf_error);
10745 }
10746
10747 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10748
10749 /* The NEW_FRIEND will look like an instantiation, to the
10750 compiler, but is not an instantiation from the point of view of
10751 the language. For example, we might have had:
10752
10753 template <class T> struct S {
10754 template <class U> friend void f(T, U);
10755 };
10756
10757 Then, in S<int>, template <class U> void f(int, U) is not an
10758 instantiation of anything. */
10759 if (new_friend == error_mark_node)
10760 return error_mark_node;
10761
10762 DECL_USE_TEMPLATE (new_friend) = 0;
10763 if (TREE_CODE (decl) == TEMPLATE_DECL)
10764 {
10765 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10766 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10767 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10768
10769 /* Attach the template requirements to the new declaration
10770 for declaration matching. We need to rebuild the requirements
10771 so that parameter levels match. */
10772 if (tree ci = get_constraints (decl))
10773 {
10774 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10775 tree args = generic_targs_for (new_friend);
10776 tree treqs = tsubst_constraint (CI_TEMPLATE_REQS (ci), args,
10777 tf_warning_or_error, NULL_TREE);
10778 tree freqs = tsubst_constraint (CI_DECLARATOR_REQS (ci), args,
10779 tf_warning_or_error, NULL_TREE);
10780
10781 /* Update the constraints -- these won't really be valid for
10782 checking, but that's not what we need them for. These ensure
10783 that the declared function can find the friend during
10784 declaration matching. */
10785 tree new_ci = get_constraints (new_friend);
10786 CI_TEMPLATE_REQS (new_ci) = treqs;
10787 CI_DECLARATOR_REQS (new_ci) = freqs;
10788
10789 /* Also update the template parameter list. */
10790 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10791 }
10792 }
10793
10794 /* The mangled name for the NEW_FRIEND is incorrect. The function
10795 is not a template instantiation and should not be mangled like
10796 one. Therefore, we forget the mangling here; we'll recompute it
10797 later if we need it. */
10798 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10799 {
10800 SET_DECL_RTL (new_friend, NULL);
10801 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10802 }
10803
10804 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10805 {
10806 tree old_decl;
10807 tree new_friend_template_info;
10808 tree new_friend_result_template_info;
10809 tree ns;
10810 int new_friend_is_defn;
10811
10812 /* We must save some information from NEW_FRIEND before calling
10813 duplicate decls since that function will free NEW_FRIEND if
10814 possible. */
10815 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10816 new_friend_is_defn =
10817 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10818 (template_for_substitution (new_friend)))
10819 != NULL_TREE);
10820 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10821 {
10822 /* This declaration is a `primary' template. */
10823 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10824
10825 new_friend_result_template_info
10826 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10827 }
10828 else
10829 new_friend_result_template_info = NULL_TREE;
10830
10831 /* Inside pushdecl_namespace_level, we will push into the
10832 current namespace. However, the friend function should go
10833 into the namespace of the template. */
10834 ns = decl_namespace_context (new_friend);
10835 push_nested_namespace (ns);
10836 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10837 pop_nested_namespace (ns);
10838
10839 if (old_decl == error_mark_node)
10840 return error_mark_node;
10841
10842 if (old_decl != new_friend)
10843 {
10844 /* This new friend declaration matched an existing
10845 declaration. For example, given:
10846
10847 template <class T> void f(T);
10848 template <class U> class C {
10849 template <class T> friend void f(T) {}
10850 };
10851
10852 the friend declaration actually provides the definition
10853 of `f', once C has been instantiated for some type. So,
10854 old_decl will be the out-of-class template declaration,
10855 while new_friend is the in-class definition.
10856
10857 But, if `f' was called before this point, the
10858 instantiation of `f' will have DECL_TI_ARGS corresponding
10859 to `T' but not to `U', references to which might appear
10860 in the definition of `f'. Previously, the most general
10861 template for an instantiation of `f' was the out-of-class
10862 version; now it is the in-class version. Therefore, we
10863 run through all specialization of `f', adding to their
10864 DECL_TI_ARGS appropriately. In particular, they need a
10865 new set of outer arguments, corresponding to the
10866 arguments for this class instantiation.
10867
10868 The same situation can arise with something like this:
10869
10870 friend void f(int);
10871 template <class T> class C {
10872 friend void f(T) {}
10873 };
10874
10875 when `C<int>' is instantiated. Now, `f(int)' is defined
10876 in the class. */
10877
10878 if (!new_friend_is_defn)
10879 /* On the other hand, if the in-class declaration does
10880 *not* provide a definition, then we don't want to alter
10881 existing definitions. We can just leave everything
10882 alone. */
10883 ;
10884 else
10885 {
10886 tree new_template = TI_TEMPLATE (new_friend_template_info);
10887 tree new_args = TI_ARGS (new_friend_template_info);
10888
10889 /* Overwrite whatever template info was there before, if
10890 any, with the new template information pertaining to
10891 the declaration. */
10892 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10893
10894 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10895 {
10896 /* We should have called reregister_specialization in
10897 duplicate_decls. */
10898 gcc_assert (retrieve_specialization (new_template,
10899 new_args, 0)
10900 == old_decl);
10901
10902 /* Instantiate it if the global has already been used. */
10903 if (DECL_ODR_USED (old_decl))
10904 instantiate_decl (old_decl, /*defer_ok=*/true,
10905 /*expl_inst_class_mem_p=*/false);
10906 }
10907 else
10908 {
10909 tree t;
10910
10911 /* Indicate that the old function template is a partial
10912 instantiation. */
10913 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10914 = new_friend_result_template_info;
10915
10916 gcc_assert (new_template
10917 == most_general_template (new_template));
10918 gcc_assert (new_template != old_decl);
10919
10920 /* Reassign any specializations already in the hash table
10921 to the new more general template, and add the
10922 additional template args. */
10923 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10924 t != NULL_TREE;
10925 t = TREE_CHAIN (t))
10926 {
10927 tree spec = TREE_VALUE (t);
10928 spec_entry elt;
10929
10930 elt.tmpl = old_decl;
10931 elt.args = DECL_TI_ARGS (spec);
10932 elt.spec = NULL_TREE;
10933
10934 decl_specializations->remove_elt (&elt);
10935
10936 DECL_TI_ARGS (spec)
10937 = add_outermost_template_args (new_args,
10938 DECL_TI_ARGS (spec));
10939
10940 register_specialization
10941 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10942
10943 }
10944 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10945 }
10946 }
10947
10948 /* The information from NEW_FRIEND has been merged into OLD_DECL
10949 by duplicate_decls. */
10950 new_friend = old_decl;
10951 }
10952 }
10953 else
10954 {
10955 tree context = DECL_CONTEXT (new_friend);
10956 bool dependent_p;
10957
10958 /* In the code
10959 template <class T> class C {
10960 template <class U> friend void C1<U>::f (); // case 1
10961 friend void C2<T>::f (); // case 2
10962 };
10963 we only need to make sure CONTEXT is a complete type for
10964 case 2. To distinguish between the two cases, we note that
10965 CONTEXT of case 1 remains dependent type after tsubst while
10966 this isn't true for case 2. */
10967 ++processing_template_decl;
10968 dependent_p = dependent_type_p (context);
10969 --processing_template_decl;
10970
10971 if (!dependent_p
10972 && !complete_type_or_else (context, NULL_TREE))
10973 return error_mark_node;
10974
10975 if (COMPLETE_TYPE_P (context))
10976 {
10977 tree fn = new_friend;
10978 /* do_friend adds the TEMPLATE_DECL for any member friend
10979 template even if it isn't a member template, i.e.
10980 template <class T> friend A<T>::f();
10981 Look through it in that case. */
10982 if (TREE_CODE (fn) == TEMPLATE_DECL
10983 && !PRIMARY_TEMPLATE_P (fn))
10984 fn = DECL_TEMPLATE_RESULT (fn);
10985 /* Check to see that the declaration is really present, and,
10986 possibly obtain an improved declaration. */
10987 fn = check_classfn (context, fn, NULL_TREE);
10988
10989 if (fn)
10990 new_friend = fn;
10991 }
10992 }
10993
10994 return new_friend;
10995 }
10996
10997 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10998 template arguments, as for tsubst.
10999
11000 Returns an appropriate tsubst'd friend type or error_mark_node on
11001 failure. */
11002
11003 static tree
11004 tsubst_friend_class (tree friend_tmpl, tree args)
11005 {
11006 tree tmpl;
11007
11008 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11009 {
11010 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11011 return TREE_TYPE (tmpl);
11012 }
11013
11014 tree context = CP_DECL_CONTEXT (friend_tmpl);
11015 if (TREE_CODE (context) == NAMESPACE_DECL)
11016 push_nested_namespace (context);
11017 else
11018 {
11019 context = tsubst (context, args, tf_error, NULL_TREE);
11020 push_nested_class (context);
11021 }
11022
11023 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11024 /*non_class=*/false, /*block_p=*/false,
11025 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11026
11027 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11028 {
11029 /* The friend template has already been declared. Just
11030 check to see that the declarations match, and install any new
11031 default parameters. We must tsubst the default parameters,
11032 of course. We only need the innermost template parameters
11033 because that is all that redeclare_class_template will look
11034 at. */
11035 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11036 > TMPL_ARGS_DEPTH (args))
11037 {
11038 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11039 args, tf_warning_or_error);
11040 location_t saved_input_location = input_location;
11041 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11042 tree cons = get_constraints (tmpl);
11043 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11044 input_location = saved_input_location;
11045 }
11046 }
11047 else
11048 {
11049 /* The friend template has not already been declared. In this
11050 case, the instantiation of the template class will cause the
11051 injection of this template into the namespace scope. */
11052 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11053
11054 if (tmpl != error_mark_node)
11055 {
11056 /* The new TMPL is not an instantiation of anything, so we
11057 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11058 for the new type because that is supposed to be the
11059 corresponding template decl, i.e., TMPL. */
11060 DECL_USE_TEMPLATE (tmpl) = 0;
11061 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11062 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11063 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11064 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11065
11066 /* It is hidden. */
11067 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11068 DECL_ANTICIPATED (tmpl)
11069 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11070
11071 /* Inject this template into the enclosing namspace scope. */
11072 tmpl = pushdecl_namespace_level (tmpl, true);
11073 }
11074 }
11075
11076 if (TREE_CODE (context) == NAMESPACE_DECL)
11077 pop_nested_namespace (context);
11078 else
11079 pop_nested_class ();
11080
11081 return TREE_TYPE (tmpl);
11082 }
11083
11084 /* Returns zero if TYPE cannot be completed later due to circularity.
11085 Otherwise returns one. */
11086
11087 static int
11088 can_complete_type_without_circularity (tree type)
11089 {
11090 if (type == NULL_TREE || type == error_mark_node)
11091 return 0;
11092 else if (COMPLETE_TYPE_P (type))
11093 return 1;
11094 else if (TREE_CODE (type) == ARRAY_TYPE)
11095 return can_complete_type_without_circularity (TREE_TYPE (type));
11096 else if (CLASS_TYPE_P (type)
11097 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11098 return 0;
11099 else
11100 return 1;
11101 }
11102
11103 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11104 tsubst_flags_t, tree);
11105
11106 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11107 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11108
11109 static tree
11110 tsubst_attribute (tree t, tree *decl_p, tree args,
11111 tsubst_flags_t complain, tree in_decl)
11112 {
11113 gcc_assert (ATTR_IS_DEPENDENT (t));
11114
11115 tree val = TREE_VALUE (t);
11116 if (val == NULL_TREE)
11117 /* Nothing to do. */;
11118 else if ((flag_openmp || flag_openmp_simd)
11119 && is_attribute_p ("omp declare simd",
11120 get_attribute_name (t)))
11121 {
11122 tree clauses = TREE_VALUE (val);
11123 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11124 complain, in_decl);
11125 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11126 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11127 tree parms = DECL_ARGUMENTS (*decl_p);
11128 clauses
11129 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11130 if (clauses)
11131 val = build_tree_list (NULL_TREE, clauses);
11132 else
11133 val = NULL_TREE;
11134 }
11135 else if (flag_openmp
11136 && is_attribute_p ("omp declare variant base",
11137 get_attribute_name (t)))
11138 {
11139 ++cp_unevaluated_operand;
11140 tree varid
11141 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11142 in_decl, /*integral_constant_expression_p=*/false);
11143 --cp_unevaluated_operand;
11144 tree chain = TREE_CHAIN (val);
11145 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11146 tree ctx = copy_list (TREE_VALUE (val));
11147 tree simd = get_identifier ("simd");
11148 tree score = get_identifier (" score");
11149 tree condition = get_identifier ("condition");
11150 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11151 {
11152 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11153 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11154 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11155 {
11156 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11157 {
11158 tree clauses = TREE_VALUE (t2);
11159 clauses = tsubst_omp_clauses (clauses,
11160 C_ORT_OMP_DECLARE_SIMD, args,
11161 complain, in_decl);
11162 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11163 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11164 TREE_VALUE (t2) = clauses;
11165 }
11166 else
11167 {
11168 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11169 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11170 if (TREE_VALUE (t3))
11171 {
11172 bool allow_string
11173 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11174 && TREE_PURPOSE (t3) != score);
11175 tree v = TREE_VALUE (t3);
11176 if (TREE_CODE (v) == STRING_CST && allow_string)
11177 continue;
11178 v = tsubst_expr (v, args, complain, in_decl, true);
11179 v = fold_non_dependent_expr (v);
11180 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11181 || (TREE_PURPOSE (t3) == score
11182 ? TREE_CODE (v) != INTEGER_CST
11183 : !tree_fits_shwi_p (v)))
11184 {
11185 location_t loc
11186 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11187 match_loc);
11188 if (TREE_PURPOSE (t3) == score)
11189 error_at (loc, "score argument must be "
11190 "constant integer expression");
11191 else if (allow_string)
11192 error_at (loc, "property must be constant "
11193 "integer expression or string "
11194 "literal");
11195 else
11196 error_at (loc, "property must be constant "
11197 "integer expression");
11198 return NULL_TREE;
11199 }
11200 else if (TREE_PURPOSE (t3) == score
11201 && tree_int_cst_sgn (v) < 0)
11202 {
11203 location_t loc
11204 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11205 match_loc);
11206 error_at (loc, "score argument must be "
11207 "non-negative");
11208 return NULL_TREE;
11209 }
11210 TREE_VALUE (t3) = v;
11211 }
11212 }
11213 }
11214 }
11215 val = tree_cons (varid, ctx, chain);
11216 }
11217 /* If the first attribute argument is an identifier, don't
11218 pass it through tsubst. Attributes like mode, format,
11219 cleanup and several target specific attributes expect it
11220 unmodified. */
11221 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11222 {
11223 tree chain
11224 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11225 /*integral_constant_expression_p=*/false);
11226 if (chain != TREE_CHAIN (val))
11227 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11228 }
11229 else if (PACK_EXPANSION_P (val))
11230 {
11231 /* An attribute pack expansion. */
11232 tree purp = TREE_PURPOSE (t);
11233 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11234 if (pack == error_mark_node)
11235 return error_mark_node;
11236 int len = TREE_VEC_LENGTH (pack);
11237 tree list = NULL_TREE;
11238 tree *q = &list;
11239 for (int i = 0; i < len; ++i)
11240 {
11241 tree elt = TREE_VEC_ELT (pack, i);
11242 *q = build_tree_list (purp, elt);
11243 q = &TREE_CHAIN (*q);
11244 }
11245 return list;
11246 }
11247 else
11248 val = tsubst_expr (val, args, complain, in_decl,
11249 /*integral_constant_expression_p=*/false);
11250
11251 if (val != TREE_VALUE (t))
11252 return build_tree_list (TREE_PURPOSE (t), val);
11253 return t;
11254 }
11255
11256 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11257 unchanged or a new TREE_LIST chain. */
11258
11259 static tree
11260 tsubst_attributes (tree attributes, tree args,
11261 tsubst_flags_t complain, tree in_decl)
11262 {
11263 tree last_dep = NULL_TREE;
11264
11265 for (tree t = attributes; t; t = TREE_CHAIN (t))
11266 if (ATTR_IS_DEPENDENT (t))
11267 {
11268 last_dep = t;
11269 attributes = copy_list (attributes);
11270 break;
11271 }
11272
11273 if (last_dep)
11274 for (tree *p = &attributes; *p; )
11275 {
11276 tree t = *p;
11277 if (ATTR_IS_DEPENDENT (t))
11278 {
11279 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11280 if (subst != t)
11281 {
11282 *p = subst;
11283 while (*p)
11284 p = &TREE_CHAIN (*p);
11285 *p = TREE_CHAIN (t);
11286 continue;
11287 }
11288 }
11289 p = &TREE_CHAIN (*p);
11290 }
11291
11292 return attributes;
11293 }
11294
11295 /* Apply any attributes which had to be deferred until instantiation
11296 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11297 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11298
11299 static void
11300 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11301 tree args, tsubst_flags_t complain, tree in_decl)
11302 {
11303 tree last_dep = NULL_TREE;
11304 tree t;
11305 tree *p;
11306
11307 if (attributes == NULL_TREE)
11308 return;
11309
11310 if (DECL_P (*decl_p))
11311 {
11312 if (TREE_TYPE (*decl_p) == error_mark_node)
11313 return;
11314 p = &DECL_ATTRIBUTES (*decl_p);
11315 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11316 to our attributes parameter. */
11317 gcc_assert (*p == attributes);
11318 }
11319 else
11320 {
11321 p = &TYPE_ATTRIBUTES (*decl_p);
11322 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11323 lookup_template_class_1, and should be preserved. */
11324 gcc_assert (*p != attributes);
11325 while (*p)
11326 p = &TREE_CHAIN (*p);
11327 }
11328
11329 for (t = attributes; t; t = TREE_CHAIN (t))
11330 if (ATTR_IS_DEPENDENT (t))
11331 {
11332 last_dep = t;
11333 attributes = copy_list (attributes);
11334 break;
11335 }
11336
11337 *p = attributes;
11338 if (last_dep)
11339 {
11340 tree late_attrs = NULL_TREE;
11341 tree *q = &late_attrs;
11342
11343 for (; *p; )
11344 {
11345 t = *p;
11346 if (ATTR_IS_DEPENDENT (t))
11347 {
11348 *p = TREE_CHAIN (t);
11349 TREE_CHAIN (t) = NULL_TREE;
11350 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11351 while (*q)
11352 q = &TREE_CHAIN (*q);
11353 }
11354 else
11355 p = &TREE_CHAIN (t);
11356 }
11357
11358 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11359 }
11360 }
11361
11362 /* Perform (or defer) access check for typedefs that were referenced
11363 from within the template TMPL code.
11364 This is a subroutine of instantiate_decl and instantiate_class_template.
11365 TMPL is the template to consider and TARGS is the list of arguments of
11366 that template. */
11367
11368 static void
11369 perform_typedefs_access_check (tree tmpl, tree targs)
11370 {
11371 unsigned i;
11372 qualified_typedef_usage_t *iter;
11373
11374 if (!tmpl
11375 || (!CLASS_TYPE_P (tmpl)
11376 && TREE_CODE (tmpl) != FUNCTION_DECL))
11377 return;
11378
11379 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11380 {
11381 tree type_decl = iter->typedef_decl;
11382 tree type_scope = iter->context;
11383
11384 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11385 continue;
11386
11387 if (uses_template_parms (type_decl))
11388 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11389 if (uses_template_parms (type_scope))
11390 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11391
11392 /* Make access check error messages point to the location
11393 of the use of the typedef. */
11394 iloc_sentinel ils (iter->locus);
11395 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11396 type_decl, type_decl,
11397 tf_warning_or_error);
11398 }
11399 }
11400
11401 static tree
11402 instantiate_class_template_1 (tree type)
11403 {
11404 tree templ, args, pattern, t, member;
11405 tree typedecl;
11406 tree pbinfo;
11407 tree base_list;
11408 unsigned int saved_maximum_field_alignment;
11409 tree fn_context;
11410
11411 if (type == error_mark_node)
11412 return error_mark_node;
11413
11414 if (COMPLETE_OR_OPEN_TYPE_P (type)
11415 || uses_template_parms (type))
11416 return type;
11417
11418 /* Figure out which template is being instantiated. */
11419 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11420 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11421
11422 /* Mark the type as in the process of being defined. */
11423 TYPE_BEING_DEFINED (type) = 1;
11424
11425 /* We may be in the middle of deferred access check. Disable
11426 it now. */
11427 deferring_access_check_sentinel acs (dk_no_deferred);
11428
11429 /* Determine what specialization of the original template to
11430 instantiate. */
11431 t = most_specialized_partial_spec (type, tf_warning_or_error);
11432 if (t == error_mark_node)
11433 return error_mark_node;
11434 else if (t)
11435 {
11436 /* This TYPE is actually an instantiation of a partial
11437 specialization. We replace the innermost set of ARGS with
11438 the arguments appropriate for substitution. For example,
11439 given:
11440
11441 template <class T> struct S {};
11442 template <class T> struct S<T*> {};
11443
11444 and supposing that we are instantiating S<int*>, ARGS will
11445 presently be {int*} -- but we need {int}. */
11446 pattern = TREE_TYPE (t);
11447 args = TREE_PURPOSE (t);
11448 }
11449 else
11450 {
11451 pattern = TREE_TYPE (templ);
11452 args = CLASSTYPE_TI_ARGS (type);
11453 }
11454
11455 /* If the template we're instantiating is incomplete, then clearly
11456 there's nothing we can do. */
11457 if (!COMPLETE_TYPE_P (pattern))
11458 {
11459 /* We can try again later. */
11460 TYPE_BEING_DEFINED (type) = 0;
11461 return type;
11462 }
11463
11464 /* If we've recursively instantiated too many templates, stop. */
11465 if (! push_tinst_level (type))
11466 return type;
11467
11468 int saved_unevaluated_operand = cp_unevaluated_operand;
11469 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11470
11471 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11472 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11473 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11474 fn_context = error_mark_node;
11475 if (!fn_context)
11476 push_to_top_level ();
11477 else
11478 {
11479 cp_unevaluated_operand = 0;
11480 c_inhibit_evaluation_warnings = 0;
11481 }
11482 /* Use #pragma pack from the template context. */
11483 saved_maximum_field_alignment = maximum_field_alignment;
11484 maximum_field_alignment = TYPE_PRECISION (pattern);
11485
11486 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11487
11488 /* Set the input location to the most specialized template definition.
11489 This is needed if tsubsting causes an error. */
11490 typedecl = TYPE_MAIN_DECL (pattern);
11491 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11492 DECL_SOURCE_LOCATION (typedecl);
11493
11494 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11495 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11496 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11497 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11498 if (ANON_AGGR_TYPE_P (pattern))
11499 SET_ANON_AGGR_TYPE_P (type);
11500 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11501 {
11502 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11503 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11504 /* Adjust visibility for template arguments. */
11505 determine_visibility (TYPE_MAIN_DECL (type));
11506 }
11507 if (CLASS_TYPE_P (type))
11508 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11509
11510 pbinfo = TYPE_BINFO (pattern);
11511
11512 /* We should never instantiate a nested class before its enclosing
11513 class; we need to look up the nested class by name before we can
11514 instantiate it, and that lookup should instantiate the enclosing
11515 class. */
11516 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11517 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11518
11519 base_list = NULL_TREE;
11520 if (BINFO_N_BASE_BINFOS (pbinfo))
11521 {
11522 tree pbase_binfo;
11523 tree pushed_scope;
11524 int i;
11525
11526 /* We must enter the scope containing the type, as that is where
11527 the accessibility of types named in dependent bases are
11528 looked up from. */
11529 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11530
11531 /* Substitute into each of the bases to determine the actual
11532 basetypes. */
11533 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11534 {
11535 tree base;
11536 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11537 tree expanded_bases = NULL_TREE;
11538 int idx, len = 1;
11539
11540 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11541 {
11542 expanded_bases =
11543 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11544 args, tf_error, NULL_TREE);
11545 if (expanded_bases == error_mark_node)
11546 continue;
11547
11548 len = TREE_VEC_LENGTH (expanded_bases);
11549 }
11550
11551 for (idx = 0; idx < len; idx++)
11552 {
11553 if (expanded_bases)
11554 /* Extract the already-expanded base class. */
11555 base = TREE_VEC_ELT (expanded_bases, idx);
11556 else
11557 /* Substitute to figure out the base class. */
11558 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11559 NULL_TREE);
11560
11561 if (base == error_mark_node)
11562 continue;
11563
11564 base_list = tree_cons (access, base, base_list);
11565 if (BINFO_VIRTUAL_P (pbase_binfo))
11566 TREE_TYPE (base_list) = integer_type_node;
11567 }
11568 }
11569
11570 /* The list is now in reverse order; correct that. */
11571 base_list = nreverse (base_list);
11572
11573 if (pushed_scope)
11574 pop_scope (pushed_scope);
11575 }
11576 /* Now call xref_basetypes to set up all the base-class
11577 information. */
11578 xref_basetypes (type, base_list);
11579
11580 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11581 (int) ATTR_FLAG_TYPE_IN_PLACE,
11582 args, tf_error, NULL_TREE);
11583 fixup_attribute_variants (type);
11584
11585 /* Now that our base classes are set up, enter the scope of the
11586 class, so that name lookups into base classes, etc. will work
11587 correctly. This is precisely analogous to what we do in
11588 begin_class_definition when defining an ordinary non-template
11589 class, except we also need to push the enclosing classes. */
11590 push_nested_class (type);
11591
11592 /* Now members are processed in the order of declaration. */
11593 for (member = CLASSTYPE_DECL_LIST (pattern);
11594 member; member = TREE_CHAIN (member))
11595 {
11596 tree t = TREE_VALUE (member);
11597
11598 if (TREE_PURPOSE (member))
11599 {
11600 if (TYPE_P (t))
11601 {
11602 if (LAMBDA_TYPE_P (t))
11603 /* A closure type for a lambda in an NSDMI or default argument.
11604 Ignore it; it will be regenerated when needed. */
11605 continue;
11606
11607 /* Build new CLASSTYPE_NESTED_UTDS. */
11608
11609 tree newtag;
11610 bool class_template_p;
11611
11612 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11613 && TYPE_LANG_SPECIFIC (t)
11614 && CLASSTYPE_IS_TEMPLATE (t));
11615 /* If the member is a class template, then -- even after
11616 substitution -- there may be dependent types in the
11617 template argument list for the class. We increment
11618 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11619 that function will assume that no types are dependent
11620 when outside of a template. */
11621 if (class_template_p)
11622 ++processing_template_decl;
11623 newtag = tsubst (t, args, tf_error, NULL_TREE);
11624 if (class_template_p)
11625 --processing_template_decl;
11626 if (newtag == error_mark_node)
11627 continue;
11628
11629 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11630 {
11631 tree name = TYPE_IDENTIFIER (t);
11632
11633 if (class_template_p)
11634 /* Unfortunately, lookup_template_class sets
11635 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11636 instantiation (i.e., for the type of a member
11637 template class nested within a template class.)
11638 This behavior is required for
11639 maybe_process_partial_specialization to work
11640 correctly, but is not accurate in this case;
11641 the TAG is not an instantiation of anything.
11642 (The corresponding TEMPLATE_DECL is an
11643 instantiation, but the TYPE is not.) */
11644 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11645
11646 /* Now, we call pushtag to put this NEWTAG into the scope of
11647 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11648 pushtag calling push_template_decl. We don't have to do
11649 this for enums because it will already have been done in
11650 tsubst_enum. */
11651 if (name)
11652 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11653 pushtag (name, newtag, /*tag_scope=*/ts_current);
11654 }
11655 }
11656 else if (DECL_DECLARES_FUNCTION_P (t))
11657 {
11658 tree r;
11659
11660 if (TREE_CODE (t) == TEMPLATE_DECL)
11661 ++processing_template_decl;
11662 r = tsubst (t, args, tf_error, NULL_TREE);
11663 if (TREE_CODE (t) == TEMPLATE_DECL)
11664 --processing_template_decl;
11665 set_current_access_from_decl (r);
11666 finish_member_declaration (r);
11667 /* Instantiate members marked with attribute used. */
11668 if (r != error_mark_node && DECL_PRESERVE_P (r))
11669 mark_used (r);
11670 if (TREE_CODE (r) == FUNCTION_DECL
11671 && DECL_OMP_DECLARE_REDUCTION_P (r))
11672 cp_check_omp_declare_reduction (r);
11673 }
11674 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11675 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11676 /* A closure type for a lambda in an NSDMI or default argument.
11677 Ignore it; it will be regenerated when needed. */;
11678 else
11679 {
11680 /* Build new TYPE_FIELDS. */
11681 if (TREE_CODE (t) == STATIC_ASSERT)
11682 {
11683 tree condition;
11684
11685 ++c_inhibit_evaluation_warnings;
11686 condition =
11687 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11688 tf_warning_or_error, NULL_TREE,
11689 /*integral_constant_expression_p=*/true);
11690 --c_inhibit_evaluation_warnings;
11691
11692 finish_static_assert (condition,
11693 STATIC_ASSERT_MESSAGE (t),
11694 STATIC_ASSERT_SOURCE_LOCATION (t),
11695 /*member_p=*/true);
11696 }
11697 else if (TREE_CODE (t) != CONST_DECL)
11698 {
11699 tree r;
11700 tree vec = NULL_TREE;
11701 int len = 1;
11702
11703 /* The file and line for this declaration, to
11704 assist in error message reporting. Since we
11705 called push_tinst_level above, we don't need to
11706 restore these. */
11707 input_location = DECL_SOURCE_LOCATION (t);
11708
11709 if (TREE_CODE (t) == TEMPLATE_DECL)
11710 ++processing_template_decl;
11711 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11712 if (TREE_CODE (t) == TEMPLATE_DECL)
11713 --processing_template_decl;
11714
11715 if (TREE_CODE (r) == TREE_VEC)
11716 {
11717 /* A capture pack became multiple fields. */
11718 vec = r;
11719 len = TREE_VEC_LENGTH (vec);
11720 }
11721
11722 for (int i = 0; i < len; ++i)
11723 {
11724 if (vec)
11725 r = TREE_VEC_ELT (vec, i);
11726 if (VAR_P (r))
11727 {
11728 /* In [temp.inst]:
11729
11730 [t]he initialization (and any associated
11731 side-effects) of a static data member does
11732 not occur unless the static data member is
11733 itself used in a way that requires the
11734 definition of the static data member to
11735 exist.
11736
11737 Therefore, we do not substitute into the
11738 initialized for the static data member here. */
11739 finish_static_data_member_decl
11740 (r,
11741 /*init=*/NULL_TREE,
11742 /*init_const_expr_p=*/false,
11743 /*asmspec_tree=*/NULL_TREE,
11744 /*flags=*/0);
11745 /* Instantiate members marked with attribute used. */
11746 if (r != error_mark_node && DECL_PRESERVE_P (r))
11747 mark_used (r);
11748 }
11749 else if (TREE_CODE (r) == FIELD_DECL)
11750 {
11751 /* Determine whether R has a valid type and can be
11752 completed later. If R is invalid, then its type
11753 is replaced by error_mark_node. */
11754 tree rtype = TREE_TYPE (r);
11755 if (can_complete_type_without_circularity (rtype))
11756 complete_type (rtype);
11757
11758 if (!complete_or_array_type_p (rtype))
11759 {
11760 /* If R's type couldn't be completed and
11761 it isn't a flexible array member (whose
11762 type is incomplete by definition) give
11763 an error. */
11764 cxx_incomplete_type_error (r, rtype);
11765 TREE_TYPE (r) = error_mark_node;
11766 }
11767 else if (TREE_CODE (rtype) == ARRAY_TYPE
11768 && TYPE_DOMAIN (rtype) == NULL_TREE
11769 && (TREE_CODE (type) == UNION_TYPE
11770 || TREE_CODE (type) == QUAL_UNION_TYPE))
11771 {
11772 error ("flexible array member %qD in union", r);
11773 TREE_TYPE (r) = error_mark_node;
11774 }
11775 }
11776
11777 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11778 such a thing will already have been added to the field
11779 list by tsubst_enum in finish_member_declaration in the
11780 CLASSTYPE_NESTED_UTDS case above. */
11781 if (!(TREE_CODE (r) == TYPE_DECL
11782 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11783 && DECL_ARTIFICIAL (r)))
11784 {
11785 set_current_access_from_decl (r);
11786 finish_member_declaration (r);
11787 }
11788 }
11789 }
11790 }
11791 }
11792 else
11793 {
11794 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11795 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11796 {
11797 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11798
11799 tree friend_type = t;
11800 bool adjust_processing_template_decl = false;
11801
11802 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11803 {
11804 /* template <class T> friend class C; */
11805 friend_type = tsubst_friend_class (friend_type, args);
11806 adjust_processing_template_decl = true;
11807 }
11808 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11809 {
11810 /* template <class T> friend class C::D; */
11811 friend_type = tsubst (friend_type, args,
11812 tf_warning_or_error, NULL_TREE);
11813 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11814 friend_type = TREE_TYPE (friend_type);
11815 adjust_processing_template_decl = true;
11816 }
11817 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11818 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11819 {
11820 /* This could be either
11821
11822 friend class T::C;
11823
11824 when dependent_type_p is false or
11825
11826 template <class U> friend class T::C;
11827
11828 otherwise. */
11829 /* Bump processing_template_decl in case this is something like
11830 template <class T> friend struct A<T>::B. */
11831 ++processing_template_decl;
11832 friend_type = tsubst (friend_type, args,
11833 tf_warning_or_error, NULL_TREE);
11834 if (dependent_type_p (friend_type))
11835 adjust_processing_template_decl = true;
11836 --processing_template_decl;
11837 }
11838 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11839 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11840 && TYPE_HIDDEN_P (friend_type))
11841 {
11842 /* friend class C;
11843
11844 where C hasn't been declared yet. Let's lookup name
11845 from namespace scope directly, bypassing any name that
11846 come from dependent base class. */
11847 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11848
11849 /* The call to xref_tag_from_type does injection for friend
11850 classes. */
11851 push_nested_namespace (ns);
11852 friend_type =
11853 xref_tag_from_type (friend_type, NULL_TREE,
11854 /*tag_scope=*/ts_current);
11855 pop_nested_namespace (ns);
11856 }
11857 else if (uses_template_parms (friend_type))
11858 /* friend class C<T>; */
11859 friend_type = tsubst (friend_type, args,
11860 tf_warning_or_error, NULL_TREE);
11861 /* Otherwise it's
11862
11863 friend class C;
11864
11865 where C is already declared or
11866
11867 friend class C<int>;
11868
11869 We don't have to do anything in these cases. */
11870
11871 if (adjust_processing_template_decl)
11872 /* Trick make_friend_class into realizing that the friend
11873 we're adding is a template, not an ordinary class. It's
11874 important that we use make_friend_class since it will
11875 perform some error-checking and output cross-reference
11876 information. */
11877 ++processing_template_decl;
11878
11879 if (friend_type != error_mark_node)
11880 make_friend_class (type, friend_type, /*complain=*/false);
11881
11882 if (adjust_processing_template_decl)
11883 --processing_template_decl;
11884 }
11885 else
11886 {
11887 /* Build new DECL_FRIENDLIST. */
11888 tree r;
11889
11890 /* The file and line for this declaration, to
11891 assist in error message reporting. Since we
11892 called push_tinst_level above, we don't need to
11893 restore these. */
11894 input_location = DECL_SOURCE_LOCATION (t);
11895
11896 if (TREE_CODE (t) == TEMPLATE_DECL)
11897 {
11898 ++processing_template_decl;
11899 push_deferring_access_checks (dk_no_check);
11900 }
11901
11902 r = tsubst_friend_function (t, args);
11903 add_friend (type, r, /*complain=*/false);
11904 if (TREE_CODE (t) == TEMPLATE_DECL)
11905 {
11906 pop_deferring_access_checks ();
11907 --processing_template_decl;
11908 }
11909 }
11910 }
11911 }
11912
11913 if (fn_context)
11914 {
11915 /* Restore these before substituting into the lambda capture
11916 initializers. */
11917 cp_unevaluated_operand = saved_unevaluated_operand;
11918 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11919 }
11920
11921 /* Set the file and line number information to whatever is given for
11922 the class itself. This puts error messages involving generated
11923 implicit functions at a predictable point, and the same point
11924 that would be used for non-template classes. */
11925 input_location = DECL_SOURCE_LOCATION (typedecl);
11926
11927 unreverse_member_declarations (type);
11928 finish_struct_1 (type);
11929 TYPE_BEING_DEFINED (type) = 0;
11930
11931 /* We don't instantiate default arguments for member functions. 14.7.1:
11932
11933 The implicit instantiation of a class template specialization causes
11934 the implicit instantiation of the declarations, but not of the
11935 definitions or default arguments, of the class member functions,
11936 member classes, static data members and member templates.... */
11937
11938 /* Some typedefs referenced from within the template code need to be access
11939 checked at template instantiation time, i.e now. These types were
11940 added to the template at parsing time. Let's get those and perform
11941 the access checks then. */
11942 perform_typedefs_access_check (pattern, args);
11943 perform_deferred_access_checks (tf_warning_or_error);
11944 pop_nested_class ();
11945 maximum_field_alignment = saved_maximum_field_alignment;
11946 if (!fn_context)
11947 pop_from_top_level ();
11948 pop_tinst_level ();
11949
11950 /* The vtable for a template class can be emitted in any translation
11951 unit in which the class is instantiated. When there is no key
11952 method, however, finish_struct_1 will already have added TYPE to
11953 the keyed_classes. */
11954 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11955 vec_safe_push (keyed_classes, type);
11956
11957 return type;
11958 }
11959
11960 /* Wrapper for instantiate_class_template_1. */
11961
11962 tree
11963 instantiate_class_template (tree type)
11964 {
11965 tree ret;
11966 timevar_push (TV_TEMPLATE_INST);
11967 ret = instantiate_class_template_1 (type);
11968 timevar_pop (TV_TEMPLATE_INST);
11969 return ret;
11970 }
11971
11972 tree
11973 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11974 {
11975 tree r;
11976
11977 if (!t)
11978 r = t;
11979 else if (TYPE_P (t))
11980 r = tsubst (t, args, complain, in_decl);
11981 else
11982 {
11983 if (!(complain & tf_warning))
11984 ++c_inhibit_evaluation_warnings;
11985 r = tsubst_expr (t, args, complain, in_decl,
11986 /*integral_constant_expression_p=*/true);
11987 if (!(complain & tf_warning))
11988 --c_inhibit_evaluation_warnings;
11989 }
11990
11991 return r;
11992 }
11993
11994 /* Given a function parameter pack TMPL_PARM and some function parameters
11995 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11996 and set *SPEC_P to point at the next point in the list. */
11997
11998 tree
11999 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12000 {
12001 /* Collect all of the extra "packed" parameters into an
12002 argument pack. */
12003 tree parmvec;
12004 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12005 tree spec_parm = *spec_p;
12006 int i, len;
12007
12008 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12009 if (tmpl_parm
12010 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12011 break;
12012
12013 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12014 parmvec = make_tree_vec (len);
12015 spec_parm = *spec_p;
12016 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12017 {
12018 tree elt = spec_parm;
12019 if (DECL_PACK_P (elt))
12020 elt = make_pack_expansion (elt);
12021 TREE_VEC_ELT (parmvec, i) = elt;
12022 }
12023
12024 /* Build the argument packs. */
12025 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12026 *spec_p = spec_parm;
12027
12028 return argpack;
12029 }
12030
12031 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12032 NONTYPE_ARGUMENT_PACK. */
12033
12034 static tree
12035 make_fnparm_pack (tree spec_parm)
12036 {
12037 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12038 }
12039
12040 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12041 pack expansion with no extra args, 2 if it has extra args, or 0
12042 if it is not a pack expansion. */
12043
12044 static int
12045 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12046 {
12047 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12048 /* We're being called before this happens in tsubst_pack_expansion. */
12049 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12050 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12051 if (i >= TREE_VEC_LENGTH (vec))
12052 return 0;
12053 tree elt = TREE_VEC_ELT (vec, i);
12054 if (DECL_P (elt))
12055 /* A decl pack is itself an expansion. */
12056 elt = TREE_TYPE (elt);
12057 if (!PACK_EXPANSION_P (elt))
12058 return 0;
12059 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12060 return 2;
12061 return 1;
12062 }
12063
12064
12065 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12066
12067 static tree
12068 make_argument_pack_select (tree arg_pack, unsigned index)
12069 {
12070 tree aps = make_node (ARGUMENT_PACK_SELECT);
12071
12072 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12073 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12074
12075 return aps;
12076 }
12077
12078 /* This is a subroutine of tsubst_pack_expansion.
12079
12080 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12081 mechanism to store the (non complete list of) arguments of the
12082 substitution and return a non substituted pack expansion, in order
12083 to wait for when we have enough arguments to really perform the
12084 substitution. */
12085
12086 static bool
12087 use_pack_expansion_extra_args_p (tree parm_packs,
12088 int arg_pack_len,
12089 bool has_empty_arg)
12090 {
12091 /* If one pack has an expansion and another pack has a normal
12092 argument or if one pack has an empty argument and an another
12093 one hasn't then tsubst_pack_expansion cannot perform the
12094 substitution and need to fall back on the
12095 PACK_EXPANSION_EXTRA mechanism. */
12096 if (parm_packs == NULL_TREE)
12097 return false;
12098 else if (has_empty_arg)
12099 {
12100 /* If all the actual packs are pack expansions, we can still
12101 subsitute directly. */
12102 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12103 {
12104 tree a = TREE_VALUE (p);
12105 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12106 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12107 a = ARGUMENT_PACK_ARGS (a);
12108 if (TREE_VEC_LENGTH (a) == 1)
12109 a = TREE_VEC_ELT (a, 0);
12110 if (PACK_EXPANSION_P (a))
12111 continue;
12112 return true;
12113 }
12114 return false;
12115 }
12116
12117 bool has_expansion_arg = false;
12118 for (int i = 0 ; i < arg_pack_len; ++i)
12119 {
12120 bool has_non_expansion_arg = false;
12121 for (tree parm_pack = parm_packs;
12122 parm_pack;
12123 parm_pack = TREE_CHAIN (parm_pack))
12124 {
12125 tree arg = TREE_VALUE (parm_pack);
12126
12127 int exp = argument_pack_element_is_expansion_p (arg, i);
12128 if (exp == 2)
12129 /* We can't substitute a pack expansion with extra args into
12130 our pattern. */
12131 return true;
12132 else if (exp)
12133 has_expansion_arg = true;
12134 else
12135 has_non_expansion_arg = true;
12136 }
12137
12138 if (has_expansion_arg && has_non_expansion_arg)
12139 return true;
12140 }
12141 return false;
12142 }
12143
12144 /* [temp.variadic]/6 says that:
12145
12146 The instantiation of a pack expansion [...]
12147 produces a list E1,E2, ..., En, where N is the number of elements
12148 in the pack expansion parameters.
12149
12150 This subroutine of tsubst_pack_expansion produces one of these Ei.
12151
12152 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12153 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12154 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12155 INDEX is the index 'i' of the element Ei to produce. ARGS,
12156 COMPLAIN, and IN_DECL are the same parameters as for the
12157 tsubst_pack_expansion function.
12158
12159 The function returns the resulting Ei upon successful completion,
12160 or error_mark_node.
12161
12162 Note that this function possibly modifies the ARGS parameter, so
12163 it's the responsibility of the caller to restore it. */
12164
12165 static tree
12166 gen_elem_of_pack_expansion_instantiation (tree pattern,
12167 tree parm_packs,
12168 unsigned index,
12169 tree args /* This parm gets
12170 modified. */,
12171 tsubst_flags_t complain,
12172 tree in_decl)
12173 {
12174 tree t;
12175 bool ith_elem_is_expansion = false;
12176
12177 /* For each parameter pack, change the substitution of the parameter
12178 pack to the ith argument in its argument pack, then expand the
12179 pattern. */
12180 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12181 {
12182 tree parm = TREE_PURPOSE (pack);
12183 tree arg_pack = TREE_VALUE (pack);
12184 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12185
12186 ith_elem_is_expansion |=
12187 argument_pack_element_is_expansion_p (arg_pack, index);
12188
12189 /* Select the Ith argument from the pack. */
12190 if (TREE_CODE (parm) == PARM_DECL
12191 || VAR_P (parm)
12192 || TREE_CODE (parm) == FIELD_DECL)
12193 {
12194 if (index == 0)
12195 {
12196 aps = make_argument_pack_select (arg_pack, index);
12197 if (!mark_used (parm, complain) && !(complain & tf_error))
12198 return error_mark_node;
12199 register_local_specialization (aps, parm);
12200 }
12201 else
12202 aps = retrieve_local_specialization (parm);
12203 }
12204 else
12205 {
12206 int idx, level;
12207 template_parm_level_and_index (parm, &level, &idx);
12208
12209 if (index == 0)
12210 {
12211 aps = make_argument_pack_select (arg_pack, index);
12212 /* Update the corresponding argument. */
12213 TMPL_ARG (args, level, idx) = aps;
12214 }
12215 else
12216 /* Re-use the ARGUMENT_PACK_SELECT. */
12217 aps = TMPL_ARG (args, level, idx);
12218 }
12219 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12220 }
12221
12222 /* Substitute into the PATTERN with the (possibly altered)
12223 arguments. */
12224 if (pattern == in_decl)
12225 /* Expanding a fixed parameter pack from
12226 coerce_template_parameter_pack. */
12227 t = tsubst_decl (pattern, args, complain);
12228 else if (pattern == error_mark_node)
12229 t = error_mark_node;
12230 else if (!TYPE_P (pattern))
12231 t = tsubst_expr (pattern, args, complain, in_decl,
12232 /*integral_constant_expression_p=*/false);
12233 else
12234 t = tsubst (pattern, args, complain, in_decl);
12235
12236 /* If the Ith argument pack element is a pack expansion, then
12237 the Ith element resulting from the substituting is going to
12238 be a pack expansion as well. */
12239 if (ith_elem_is_expansion)
12240 t = make_pack_expansion (t, complain);
12241
12242 return t;
12243 }
12244
12245 /* When the unexpanded parameter pack in a fold expression expands to an empty
12246 sequence, the value of the expression is as follows; the program is
12247 ill-formed if the operator is not listed in this table.
12248
12249 && true
12250 || false
12251 , void() */
12252
12253 tree
12254 expand_empty_fold (tree t, tsubst_flags_t complain)
12255 {
12256 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12257 if (!FOLD_EXPR_MODIFY_P (t))
12258 switch (code)
12259 {
12260 case TRUTH_ANDIF_EXPR:
12261 return boolean_true_node;
12262 case TRUTH_ORIF_EXPR:
12263 return boolean_false_node;
12264 case COMPOUND_EXPR:
12265 return void_node;
12266 default:
12267 break;
12268 }
12269
12270 if (complain & tf_error)
12271 error_at (location_of (t),
12272 "fold of empty expansion over %O", code);
12273 return error_mark_node;
12274 }
12275
12276 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12277 form an expression that combines the two terms using the
12278 operator of T. */
12279
12280 static tree
12281 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12282 {
12283 tree op = FOLD_EXPR_OP (t);
12284 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12285
12286 // Handle compound assignment operators.
12287 if (FOLD_EXPR_MODIFY_P (t))
12288 return build_x_modify_expr (input_location, left, code, right, complain);
12289
12290 switch (code)
12291 {
12292 case COMPOUND_EXPR:
12293 return build_x_compound_expr (input_location, left, right, complain);
12294 default:
12295 return build_x_binary_op (input_location, code,
12296 left, TREE_CODE (left),
12297 right, TREE_CODE (right),
12298 /*overload=*/NULL,
12299 complain);
12300 }
12301 }
12302
12303 /* Substitute ARGS into the pack of a fold expression T. */
12304
12305 static inline tree
12306 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12307 {
12308 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12309 }
12310
12311 /* Substitute ARGS into the pack of a fold expression T. */
12312
12313 static inline tree
12314 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12315 {
12316 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12317 }
12318
12319 /* Expand a PACK of arguments into a grouped as left fold.
12320 Given a pack containing elements A0, A1, ..., An and an
12321 operator @, this builds the expression:
12322
12323 ((A0 @ A1) @ A2) ... @ An
12324
12325 Note that PACK must not be empty.
12326
12327 The operator is defined by the original fold expression T. */
12328
12329 static tree
12330 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12331 {
12332 tree left = TREE_VEC_ELT (pack, 0);
12333 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12334 {
12335 tree right = TREE_VEC_ELT (pack, i);
12336 left = fold_expression (t, left, right, complain);
12337 }
12338 return left;
12339 }
12340
12341 /* Substitute into a unary left fold expression. */
12342
12343 static tree
12344 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12345 tree in_decl)
12346 {
12347 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12348 if (pack == error_mark_node)
12349 return error_mark_node;
12350 if (PACK_EXPANSION_P (pack))
12351 {
12352 tree r = copy_node (t);
12353 FOLD_EXPR_PACK (r) = pack;
12354 return r;
12355 }
12356 if (TREE_VEC_LENGTH (pack) == 0)
12357 return expand_empty_fold (t, complain);
12358 else
12359 return expand_left_fold (t, pack, complain);
12360 }
12361
12362 /* Substitute into a binary left fold expression.
12363
12364 Do ths by building a single (non-empty) vector of argumnts and
12365 building the expression from those elements. */
12366
12367 static tree
12368 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12369 tree in_decl)
12370 {
12371 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12372 if (pack == error_mark_node)
12373 return error_mark_node;
12374 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12375 if (init == error_mark_node)
12376 return error_mark_node;
12377
12378 if (PACK_EXPANSION_P (pack))
12379 {
12380 tree r = copy_node (t);
12381 FOLD_EXPR_PACK (r) = pack;
12382 FOLD_EXPR_INIT (r) = init;
12383 return r;
12384 }
12385
12386 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12387 TREE_VEC_ELT (vec, 0) = init;
12388 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12389 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12390
12391 return expand_left_fold (t, vec, complain);
12392 }
12393
12394 /* Expand a PACK of arguments into a grouped as right fold.
12395 Given a pack containing elementns A0, A1, ..., and an
12396 operator @, this builds the expression:
12397
12398 A0@ ... (An-2 @ (An-1 @ An))
12399
12400 Note that PACK must not be empty.
12401
12402 The operator is defined by the original fold expression T. */
12403
12404 tree
12405 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12406 {
12407 // Build the expression.
12408 int n = TREE_VEC_LENGTH (pack);
12409 tree right = TREE_VEC_ELT (pack, n - 1);
12410 for (--n; n != 0; --n)
12411 {
12412 tree left = TREE_VEC_ELT (pack, n - 1);
12413 right = fold_expression (t, left, right, complain);
12414 }
12415 return right;
12416 }
12417
12418 /* Substitute into a unary right fold expression. */
12419
12420 static tree
12421 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12422 tree in_decl)
12423 {
12424 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12425 if (pack == error_mark_node)
12426 return error_mark_node;
12427 if (PACK_EXPANSION_P (pack))
12428 {
12429 tree r = copy_node (t);
12430 FOLD_EXPR_PACK (r) = pack;
12431 return r;
12432 }
12433 if (TREE_VEC_LENGTH (pack) == 0)
12434 return expand_empty_fold (t, complain);
12435 else
12436 return expand_right_fold (t, pack, complain);
12437 }
12438
12439 /* Substitute into a binary right fold expression.
12440
12441 Do ths by building a single (non-empty) vector of arguments and
12442 building the expression from those elements. */
12443
12444 static tree
12445 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12446 tree in_decl)
12447 {
12448 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12449 if (pack == error_mark_node)
12450 return error_mark_node;
12451 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12452 if (init == error_mark_node)
12453 return error_mark_node;
12454
12455 if (PACK_EXPANSION_P (pack))
12456 {
12457 tree r = copy_node (t);
12458 FOLD_EXPR_PACK (r) = pack;
12459 FOLD_EXPR_INIT (r) = init;
12460 return r;
12461 }
12462
12463 int n = TREE_VEC_LENGTH (pack);
12464 tree vec = make_tree_vec (n + 1);
12465 for (int i = 0; i < n; ++i)
12466 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12467 TREE_VEC_ELT (vec, n) = init;
12468
12469 return expand_right_fold (t, vec, complain);
12470 }
12471
12472 /* Walk through the pattern of a pack expansion, adding everything in
12473 local_specializations to a list. */
12474
12475 class el_data
12476 {
12477 public:
12478 hash_set<tree> internal;
12479 tree extra;
12480 tsubst_flags_t complain;
12481
12482 el_data (tsubst_flags_t c)
12483 : extra (NULL_TREE), complain (c) {}
12484 };
12485 static tree
12486 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12487 {
12488 el_data &data = *reinterpret_cast<el_data*>(data_);
12489 tree *extra = &data.extra;
12490 tsubst_flags_t complain = data.complain;
12491
12492 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12493 /* Remember local typedefs (85214). */
12494 tp = &TYPE_NAME (*tp);
12495
12496 if (TREE_CODE (*tp) == DECL_EXPR)
12497 data.internal.add (DECL_EXPR_DECL (*tp));
12498 else if (tree spec = retrieve_local_specialization (*tp))
12499 {
12500 if (data.internal.contains (*tp))
12501 /* Don't mess with variables declared within the pattern. */
12502 return NULL_TREE;
12503 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12504 {
12505 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12506 tree args = ARGUMENT_PACK_ARGS (spec);
12507 if (TREE_VEC_LENGTH (args) == 1)
12508 {
12509 tree elt = TREE_VEC_ELT (args, 0);
12510 if (PACK_EXPANSION_P (elt))
12511 elt = PACK_EXPANSION_PATTERN (elt);
12512 if (DECL_PACK_P (elt))
12513 spec = elt;
12514 }
12515 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12516 {
12517 /* Handle lambda capture here, since we aren't doing any
12518 substitution now, and so tsubst_copy won't call
12519 process_outer_var_ref. */
12520 tree args = ARGUMENT_PACK_ARGS (spec);
12521 int len = TREE_VEC_LENGTH (args);
12522 for (int i = 0; i < len; ++i)
12523 {
12524 tree arg = TREE_VEC_ELT (args, i);
12525 tree carg = arg;
12526 if (outer_automatic_var_p (arg))
12527 carg = process_outer_var_ref (arg, complain);
12528 if (carg != arg)
12529 {
12530 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12531 proxies. */
12532 if (i == 0)
12533 {
12534 spec = copy_node (spec);
12535 args = copy_node (args);
12536 SET_ARGUMENT_PACK_ARGS (spec, args);
12537 register_local_specialization (spec, *tp);
12538 }
12539 TREE_VEC_ELT (args, i) = carg;
12540 }
12541 }
12542 }
12543 }
12544 if (outer_automatic_var_p (spec))
12545 spec = process_outer_var_ref (spec, complain);
12546 *extra = tree_cons (*tp, spec, *extra);
12547 }
12548 return NULL_TREE;
12549 }
12550 static tree
12551 extract_local_specs (tree pattern, tsubst_flags_t complain)
12552 {
12553 el_data data (complain);
12554 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12555 return data.extra;
12556 }
12557
12558 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12559 for use in PACK_EXPANSION_EXTRA_ARGS. */
12560
12561 tree
12562 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12563 {
12564 tree extra = args;
12565 if (local_specializations)
12566 if (tree locals = extract_local_specs (pattern, complain))
12567 extra = tree_cons (NULL_TREE, extra, locals);
12568 return extra;
12569 }
12570
12571 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12572 normal template args to ARGS. */
12573
12574 tree
12575 add_extra_args (tree extra, tree args)
12576 {
12577 if (extra && TREE_CODE (extra) == TREE_LIST)
12578 {
12579 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12580 {
12581 /* The partial instantiation involved local declarations collected in
12582 extract_local_specs; map from the general template to our local
12583 context. */
12584 tree gen = TREE_PURPOSE (elt);
12585 tree inst = TREE_VALUE (elt);
12586 if (DECL_P (inst))
12587 if (tree local = retrieve_local_specialization (inst))
12588 inst = local;
12589 /* else inst is already a full instantiation of the pack. */
12590 register_local_specialization (inst, gen);
12591 }
12592 gcc_assert (!TREE_PURPOSE (extra));
12593 extra = TREE_VALUE (extra);
12594 }
12595 #if 1
12596 /* I think we should always be able to substitute dependent args into the
12597 pattern. If that turns out to be incorrect in some cases, enable the
12598 alternate code (and add complain/in_decl parms to this function). */
12599 gcc_checking_assert (!uses_template_parms (extra));
12600 #else
12601 if (!uses_template_parms (extra))
12602 {
12603 gcc_unreachable ();
12604 extra = tsubst_template_args (extra, args, complain, in_decl);
12605 args = add_outermost_template_args (args, extra);
12606 }
12607 else
12608 #endif
12609 args = add_to_template_args (extra, args);
12610 return args;
12611 }
12612
12613 /* Substitute ARGS into T, which is an pack expansion
12614 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12615 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12616 (if only a partial substitution could be performed) or
12617 ERROR_MARK_NODE if there was an error. */
12618 tree
12619 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12620 tree in_decl)
12621 {
12622 tree pattern;
12623 tree pack, packs = NULL_TREE;
12624 bool unsubstituted_packs = false;
12625 bool unsubstituted_fn_pack = false;
12626 int i, len = -1;
12627 tree result;
12628 hash_map<tree, tree> *saved_local_specializations = NULL;
12629 bool need_local_specializations = false;
12630 int levels;
12631
12632 gcc_assert (PACK_EXPANSION_P (t));
12633 pattern = PACK_EXPANSION_PATTERN (t);
12634
12635 /* Add in any args remembered from an earlier partial instantiation. */
12636 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12637
12638 levels = TMPL_ARGS_DEPTH (args);
12639
12640 /* Determine the argument packs that will instantiate the parameter
12641 packs used in the expansion expression. While we're at it,
12642 compute the number of arguments to be expanded and make sure it
12643 is consistent. */
12644 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12645 pack = TREE_CHAIN (pack))
12646 {
12647 tree parm_pack = TREE_VALUE (pack);
12648 tree arg_pack = NULL_TREE;
12649 tree orig_arg = NULL_TREE;
12650 int level = 0;
12651
12652 if (TREE_CODE (parm_pack) == BASES)
12653 {
12654 gcc_assert (parm_pack == pattern);
12655 if (BASES_DIRECT (parm_pack))
12656 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12657 args, complain,
12658 in_decl, false),
12659 complain);
12660 else
12661 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12662 args, complain, in_decl,
12663 false), complain);
12664 }
12665 else if (builtin_pack_call_p (parm_pack))
12666 {
12667 if (parm_pack != pattern)
12668 {
12669 if (complain & tf_error)
12670 sorry ("%qE is not the entire pattern of the pack expansion",
12671 parm_pack);
12672 return error_mark_node;
12673 }
12674 return expand_builtin_pack_call (parm_pack, args,
12675 complain, in_decl);
12676 }
12677 else if (TREE_CODE (parm_pack) == PARM_DECL)
12678 {
12679 /* We know we have correct local_specializations if this
12680 expansion is at function scope, or if we're dealing with a
12681 local parameter in a requires expression; for the latter,
12682 tsubst_requires_expr set it up appropriately. */
12683 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12684 arg_pack = retrieve_local_specialization (parm_pack);
12685 else
12686 /* We can't rely on local_specializations for a parameter
12687 name used later in a function declaration (such as in a
12688 late-specified return type). Even if it exists, it might
12689 have the wrong value for a recursive call. */
12690 need_local_specializations = true;
12691
12692 if (!arg_pack)
12693 {
12694 /* This parameter pack was used in an unevaluated context. Just
12695 make a dummy decl, since it's only used for its type. */
12696 ++cp_unevaluated_operand;
12697 arg_pack = tsubst_decl (parm_pack, args, complain);
12698 --cp_unevaluated_operand;
12699 if (arg_pack && DECL_PACK_P (arg_pack))
12700 /* Partial instantiation of the parm_pack, we can't build
12701 up an argument pack yet. */
12702 arg_pack = NULL_TREE;
12703 else
12704 arg_pack = make_fnparm_pack (arg_pack);
12705 }
12706 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12707 /* This argument pack isn't fully instantiated yet. We set this
12708 flag rather than clear arg_pack because we do want to do the
12709 optimization below, and we don't want to substitute directly
12710 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12711 where it isn't expected). */
12712 unsubstituted_fn_pack = true;
12713 }
12714 else if (is_capture_proxy (parm_pack))
12715 {
12716 arg_pack = retrieve_local_specialization (parm_pack);
12717 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12718 unsubstituted_fn_pack = true;
12719 }
12720 else
12721 {
12722 int idx;
12723 template_parm_level_and_index (parm_pack, &level, &idx);
12724 if (level <= levels)
12725 arg_pack = TMPL_ARG (args, level, idx);
12726 }
12727
12728 orig_arg = arg_pack;
12729 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12730 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12731
12732 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12733 /* This can only happen if we forget to expand an argument
12734 pack somewhere else. Just return an error, silently. */
12735 {
12736 result = make_tree_vec (1);
12737 TREE_VEC_ELT (result, 0) = error_mark_node;
12738 return result;
12739 }
12740
12741 if (arg_pack)
12742 {
12743 int my_len =
12744 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12745
12746 /* Don't bother trying to do a partial substitution with
12747 incomplete packs; we'll try again after deduction. */
12748 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12749 return t;
12750
12751 if (len < 0)
12752 len = my_len;
12753 else if (len != my_len
12754 && !unsubstituted_fn_pack)
12755 {
12756 if (!(complain & tf_error))
12757 /* Fail quietly. */;
12758 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12759 error ("mismatched argument pack lengths while expanding %qT",
12760 pattern);
12761 else
12762 error ("mismatched argument pack lengths while expanding %qE",
12763 pattern);
12764 return error_mark_node;
12765 }
12766
12767 /* Keep track of the parameter packs and their corresponding
12768 argument packs. */
12769 packs = tree_cons (parm_pack, arg_pack, packs);
12770 TREE_TYPE (packs) = orig_arg;
12771 }
12772 else
12773 {
12774 /* We can't substitute for this parameter pack. We use a flag as
12775 well as the missing_level counter because function parameter
12776 packs don't have a level. */
12777 if (!(processing_template_decl || is_auto (parm_pack)))
12778 {
12779 gcc_unreachable ();
12780 }
12781 gcc_assert (processing_template_decl || is_auto (parm_pack));
12782 unsubstituted_packs = true;
12783 }
12784 }
12785
12786 /* If the expansion is just T..., return the matching argument pack, unless
12787 we need to call convert_from_reference on all the elements. This is an
12788 important optimization; see c++/68422. */
12789 if (!unsubstituted_packs
12790 && TREE_PURPOSE (packs) == pattern)
12791 {
12792 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12793
12794 /* If the argument pack is a single pack expansion, pull it out. */
12795 if (TREE_VEC_LENGTH (args) == 1
12796 && pack_expansion_args_count (args))
12797 return TREE_VEC_ELT (args, 0);
12798
12799 /* Types need no adjustment, nor does sizeof..., and if we still have
12800 some pack expansion args we won't do anything yet. */
12801 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12802 || PACK_EXPANSION_SIZEOF_P (t)
12803 || pack_expansion_args_count (args))
12804 return args;
12805 /* Also optimize expression pack expansions if we can tell that the
12806 elements won't have reference type. */
12807 tree type = TREE_TYPE (pattern);
12808 if (type && !TYPE_REF_P (type)
12809 && !PACK_EXPANSION_P (type)
12810 && !WILDCARD_TYPE_P (type))
12811 return args;
12812 /* Otherwise use the normal path so we get convert_from_reference. */
12813 }
12814
12815 /* We cannot expand this expansion expression, because we don't have
12816 all of the argument packs we need. */
12817 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12818 {
12819 /* We got some full packs, but we can't substitute them in until we
12820 have values for all the packs. So remember these until then. */
12821
12822 t = make_pack_expansion (pattern, complain);
12823 PACK_EXPANSION_EXTRA_ARGS (t)
12824 = build_extra_args (pattern, args, complain);
12825 return t;
12826 }
12827 else if (unsubstituted_packs)
12828 {
12829 /* There were no real arguments, we're just replacing a parameter
12830 pack with another version of itself. Substitute into the
12831 pattern and return a PACK_EXPANSION_*. The caller will need to
12832 deal with that. */
12833 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12834 t = tsubst_expr (pattern, args, complain, in_decl,
12835 /*integral_constant_expression_p=*/false);
12836 else
12837 t = tsubst (pattern, args, complain, in_decl);
12838 t = make_pack_expansion (t, complain);
12839 return t;
12840 }
12841
12842 gcc_assert (len >= 0);
12843
12844 if (need_local_specializations)
12845 {
12846 /* We're in a late-specified return type, so create our own local
12847 specializations map; the current map is either NULL or (in the
12848 case of recursive unification) might have bindings that we don't
12849 want to use or alter. */
12850 saved_local_specializations = local_specializations;
12851 local_specializations = new hash_map<tree, tree>;
12852 }
12853
12854 /* For each argument in each argument pack, substitute into the
12855 pattern. */
12856 result = make_tree_vec (len);
12857 tree elem_args = copy_template_args (args);
12858 for (i = 0; i < len; ++i)
12859 {
12860 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12861 i,
12862 elem_args, complain,
12863 in_decl);
12864 TREE_VEC_ELT (result, i) = t;
12865 if (t == error_mark_node)
12866 {
12867 result = error_mark_node;
12868 break;
12869 }
12870 }
12871
12872 /* Update ARGS to restore the substitution from parameter packs to
12873 their argument packs. */
12874 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12875 {
12876 tree parm = TREE_PURPOSE (pack);
12877
12878 if (TREE_CODE (parm) == PARM_DECL
12879 || VAR_P (parm)
12880 || TREE_CODE (parm) == FIELD_DECL)
12881 register_local_specialization (TREE_TYPE (pack), parm);
12882 else
12883 {
12884 int idx, level;
12885
12886 if (TREE_VALUE (pack) == NULL_TREE)
12887 continue;
12888
12889 template_parm_level_and_index (parm, &level, &idx);
12890
12891 /* Update the corresponding argument. */
12892 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12893 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12894 TREE_TYPE (pack);
12895 else
12896 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12897 }
12898 }
12899
12900 if (need_local_specializations)
12901 {
12902 delete local_specializations;
12903 local_specializations = saved_local_specializations;
12904 }
12905
12906 /* If the dependent pack arguments were such that we end up with only a
12907 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12908 if (len == 1 && TREE_CODE (result) == TREE_VEC
12909 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12910 return TREE_VEC_ELT (result, 0);
12911
12912 return result;
12913 }
12914
12915 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12916 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12917 parameter packs; all parms generated from a function parameter pack will
12918 have the same DECL_PARM_INDEX. */
12919
12920 tree
12921 get_pattern_parm (tree parm, tree tmpl)
12922 {
12923 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12924 tree patparm;
12925
12926 if (DECL_ARTIFICIAL (parm))
12927 {
12928 for (patparm = DECL_ARGUMENTS (pattern);
12929 patparm; patparm = DECL_CHAIN (patparm))
12930 if (DECL_ARTIFICIAL (patparm)
12931 && DECL_NAME (parm) == DECL_NAME (patparm))
12932 break;
12933 }
12934 else
12935 {
12936 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12937 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12938 gcc_assert (DECL_PARM_INDEX (patparm)
12939 == DECL_PARM_INDEX (parm));
12940 }
12941
12942 return patparm;
12943 }
12944
12945 /* Make an argument pack out of the TREE_VEC VEC. */
12946
12947 static tree
12948 make_argument_pack (tree vec)
12949 {
12950 tree pack;
12951 tree elt = TREE_VEC_ELT (vec, 0);
12952 if (TYPE_P (elt))
12953 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12954 else
12955 {
12956 pack = make_node (NONTYPE_ARGUMENT_PACK);
12957 TREE_CONSTANT (pack) = 1;
12958 }
12959 SET_ARGUMENT_PACK_ARGS (pack, vec);
12960 return pack;
12961 }
12962
12963 /* Return an exact copy of template args T that can be modified
12964 independently. */
12965
12966 static tree
12967 copy_template_args (tree t)
12968 {
12969 if (t == error_mark_node)
12970 return t;
12971
12972 int len = TREE_VEC_LENGTH (t);
12973 tree new_vec = make_tree_vec (len);
12974
12975 for (int i = 0; i < len; ++i)
12976 {
12977 tree elt = TREE_VEC_ELT (t, i);
12978 if (elt && TREE_CODE (elt) == TREE_VEC)
12979 elt = copy_template_args (elt);
12980 TREE_VEC_ELT (new_vec, i) = elt;
12981 }
12982
12983 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12984 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12985
12986 return new_vec;
12987 }
12988
12989 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
12990
12991 tree
12992 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
12993 tree in_decl)
12994 {
12995 /* Substitute into each of the arguments. */
12996 tree new_arg = TYPE_P (orig_arg)
12997 ? cxx_make_type (TREE_CODE (orig_arg))
12998 : make_node (TREE_CODE (orig_arg));
12999
13000 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13001 args, complain, in_decl);
13002 if (pack_args == error_mark_node)
13003 new_arg = error_mark_node;
13004 else
13005 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13006
13007 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13008 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13009
13010 return new_arg;
13011 }
13012
13013 /* Substitute ARGS into the vector or list of template arguments T. */
13014
13015 tree
13016 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13017 {
13018 tree orig_t = t;
13019 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13020 tree *elts;
13021
13022 if (t == error_mark_node)
13023 return error_mark_node;
13024
13025 len = TREE_VEC_LENGTH (t);
13026 elts = XALLOCAVEC (tree, len);
13027
13028 for (i = 0; i < len; i++)
13029 {
13030 tree orig_arg = TREE_VEC_ELT (t, i);
13031 tree new_arg;
13032
13033 if (TREE_CODE (orig_arg) == TREE_VEC)
13034 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13035 else if (PACK_EXPANSION_P (orig_arg))
13036 {
13037 /* Substitute into an expansion expression. */
13038 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13039
13040 if (TREE_CODE (new_arg) == TREE_VEC)
13041 /* Add to the expanded length adjustment the number of
13042 expanded arguments. We subtract one from this
13043 measurement, because the argument pack expression
13044 itself is already counted as 1 in
13045 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13046 the argument pack is empty. */
13047 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13048 }
13049 else if (ARGUMENT_PACK_P (orig_arg))
13050 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13051 else
13052 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13053
13054 if (new_arg == error_mark_node)
13055 return error_mark_node;
13056
13057 elts[i] = new_arg;
13058 if (new_arg != orig_arg)
13059 need_new = 1;
13060 }
13061
13062 if (!need_new)
13063 return t;
13064
13065 /* Make space for the expanded arguments coming from template
13066 argument packs. */
13067 t = make_tree_vec (len + expanded_len_adjust);
13068 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13069 arguments for a member template.
13070 In that case each TREE_VEC in ORIG_T represents a level of template
13071 arguments, and ORIG_T won't carry any non defaulted argument count.
13072 It will rather be the nested TREE_VECs that will carry one.
13073 In other words, ORIG_T carries a non defaulted argument count only
13074 if it doesn't contain any nested TREE_VEC. */
13075 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13076 {
13077 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13078 count += expanded_len_adjust;
13079 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13080 }
13081 for (i = 0, out = 0; i < len; i++)
13082 {
13083 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13084 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13085 && TREE_CODE (elts[i]) == TREE_VEC)
13086 {
13087 int idx;
13088
13089 /* Now expand the template argument pack "in place". */
13090 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13091 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13092 }
13093 else
13094 {
13095 TREE_VEC_ELT (t, out) = elts[i];
13096 out++;
13097 }
13098 }
13099
13100 return t;
13101 }
13102
13103 /* Substitute ARGS into one level PARMS of template parameters. */
13104
13105 static tree
13106 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13107 {
13108 if (parms == error_mark_node)
13109 return error_mark_node;
13110
13111 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13112
13113 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13114 {
13115 tree tuple = TREE_VEC_ELT (parms, i);
13116
13117 if (tuple == error_mark_node)
13118 continue;
13119
13120 TREE_VEC_ELT (new_vec, i) =
13121 tsubst_template_parm (tuple, args, complain);
13122 }
13123
13124 return new_vec;
13125 }
13126
13127 /* Return the result of substituting ARGS into the template parameters
13128 given by PARMS. If there are m levels of ARGS and m + n levels of
13129 PARMS, then the result will contain n levels of PARMS. For
13130 example, if PARMS is `template <class T> template <class U>
13131 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13132 result will be `template <int*, double, class V>'. */
13133
13134 static tree
13135 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13136 {
13137 tree r = NULL_TREE;
13138 tree* new_parms;
13139
13140 /* When substituting into a template, we must set
13141 PROCESSING_TEMPLATE_DECL as the template parameters may be
13142 dependent if they are based on one-another, and the dependency
13143 predicates are short-circuit outside of templates. */
13144 ++processing_template_decl;
13145
13146 for (new_parms = &r;
13147 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13148 new_parms = &(TREE_CHAIN (*new_parms)),
13149 parms = TREE_CHAIN (parms))
13150 {
13151 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13152 args, complain);
13153 *new_parms =
13154 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13155 - TMPL_ARGS_DEPTH (args)),
13156 new_vec, NULL_TREE);
13157 }
13158
13159 --processing_template_decl;
13160
13161 return r;
13162 }
13163
13164 /* Return the result of substituting ARGS into one template parameter
13165 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13166 parameter and which TREE_PURPOSE is the default argument of the
13167 template parameter. */
13168
13169 static tree
13170 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13171 {
13172 tree default_value, parm_decl;
13173
13174 if (args == NULL_TREE
13175 || t == NULL_TREE
13176 || t == error_mark_node)
13177 return t;
13178
13179 gcc_assert (TREE_CODE (t) == TREE_LIST);
13180
13181 default_value = TREE_PURPOSE (t);
13182 parm_decl = TREE_VALUE (t);
13183 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13184
13185 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13186 if (TREE_CODE (parm_decl) == PARM_DECL
13187 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13188 parm_decl = error_mark_node;
13189 default_value = tsubst_template_arg (default_value, args,
13190 complain, NULL_TREE);
13191 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13192
13193 tree r = build_tree_list (default_value, parm_decl);
13194 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13195 return r;
13196 }
13197
13198 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13199 type T. If T is not an aggregate or enumeration type, it is
13200 handled as if by tsubst. IN_DECL is as for tsubst. If
13201 ENTERING_SCOPE is nonzero, T is the context for a template which
13202 we are presently tsubst'ing. Return the substituted value. */
13203
13204 static tree
13205 tsubst_aggr_type (tree t,
13206 tree args,
13207 tsubst_flags_t complain,
13208 tree in_decl,
13209 int entering_scope)
13210 {
13211 if (t == NULL_TREE)
13212 return NULL_TREE;
13213
13214 switch (TREE_CODE (t))
13215 {
13216 case RECORD_TYPE:
13217 if (TYPE_PTRMEMFUNC_P (t))
13218 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13219
13220 /* Fall through. */
13221 case ENUMERAL_TYPE:
13222 case UNION_TYPE:
13223 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13224 {
13225 tree argvec;
13226 tree context;
13227 tree r;
13228
13229 /* In "sizeof(X<I>)" we need to evaluate "I". */
13230 cp_evaluated ev;
13231
13232 /* First, determine the context for the type we are looking
13233 up. */
13234 context = TYPE_CONTEXT (t);
13235 if (context && TYPE_P (context))
13236 {
13237 context = tsubst_aggr_type (context, args, complain,
13238 in_decl, /*entering_scope=*/1);
13239 /* If context is a nested class inside a class template,
13240 it may still need to be instantiated (c++/33959). */
13241 context = complete_type (context);
13242 }
13243
13244 /* Then, figure out what arguments are appropriate for the
13245 type we are trying to find. For example, given:
13246
13247 template <class T> struct S;
13248 template <class T, class U> void f(T, U) { S<U> su; }
13249
13250 and supposing that we are instantiating f<int, double>,
13251 then our ARGS will be {int, double}, but, when looking up
13252 S we only want {double}. */
13253 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13254 complain, in_decl);
13255 if (argvec == error_mark_node)
13256 r = error_mark_node;
13257 else
13258 {
13259 r = lookup_template_class (t, argvec, in_decl, context,
13260 entering_scope, complain);
13261 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13262 }
13263
13264 return r;
13265 }
13266 else
13267 /* This is not a template type, so there's nothing to do. */
13268 return t;
13269
13270 default:
13271 return tsubst (t, args, complain, in_decl);
13272 }
13273 }
13274
13275 static GTY((cache)) tree_cache_map *defarg_inst;
13276
13277 /* Substitute into the default argument ARG (a default argument for
13278 FN), which has the indicated TYPE. */
13279
13280 tree
13281 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13282 tsubst_flags_t complain)
13283 {
13284 int errs = errorcount + sorrycount;
13285
13286 /* This can happen in invalid code. */
13287 if (TREE_CODE (arg) == DEFERRED_PARSE)
13288 return arg;
13289
13290 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13291 parm = chain_index (parmnum, parm);
13292 tree parmtype = TREE_TYPE (parm);
13293 if (DECL_BY_REFERENCE (parm))
13294 parmtype = TREE_TYPE (parmtype);
13295 if (parmtype == error_mark_node)
13296 return error_mark_node;
13297
13298 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13299
13300 tree *slot;
13301 if (defarg_inst && (slot = defarg_inst->get (parm)))
13302 return *slot;
13303
13304 /* This default argument came from a template. Instantiate the
13305 default argument here, not in tsubst. In the case of
13306 something like:
13307
13308 template <class T>
13309 struct S {
13310 static T t();
13311 void f(T = t());
13312 };
13313
13314 we must be careful to do name lookup in the scope of S<T>,
13315 rather than in the current class. */
13316 push_to_top_level ();
13317 push_access_scope (fn);
13318 push_deferring_access_checks (dk_no_deferred);
13319 start_lambda_scope (parm);
13320
13321 /* The default argument expression may cause implicitly defined
13322 member functions to be synthesized, which will result in garbage
13323 collection. We must treat this situation as if we were within
13324 the body of function so as to avoid collecting live data on the
13325 stack. */
13326 ++function_depth;
13327 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13328 complain, NULL_TREE,
13329 /*integral_constant_expression_p=*/false);
13330 --function_depth;
13331
13332 finish_lambda_scope ();
13333
13334 /* Make sure the default argument is reasonable. */
13335 arg = check_default_argument (type, arg, complain);
13336
13337 if (errorcount+sorrycount > errs
13338 && (complain & tf_warning_or_error))
13339 inform (input_location,
13340 " when instantiating default argument for call to %qD", fn);
13341
13342 pop_deferring_access_checks ();
13343 pop_access_scope (fn);
13344 pop_from_top_level ();
13345
13346 if (arg != error_mark_node && !cp_unevaluated_operand)
13347 {
13348 if (!defarg_inst)
13349 defarg_inst = tree_cache_map::create_ggc (37);
13350 defarg_inst->put (parm, arg);
13351 }
13352
13353 return arg;
13354 }
13355
13356 /* Substitute into all the default arguments for FN. */
13357
13358 static void
13359 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13360 {
13361 tree arg;
13362 tree tmpl_args;
13363
13364 tmpl_args = DECL_TI_ARGS (fn);
13365
13366 /* If this function is not yet instantiated, we certainly don't need
13367 its default arguments. */
13368 if (uses_template_parms (tmpl_args))
13369 return;
13370 /* Don't do this again for clones. */
13371 if (DECL_CLONED_FUNCTION_P (fn))
13372 return;
13373
13374 int i = 0;
13375 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13376 arg;
13377 arg = TREE_CHAIN (arg), ++i)
13378 if (TREE_PURPOSE (arg))
13379 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13380 TREE_VALUE (arg),
13381 TREE_PURPOSE (arg),
13382 complain);
13383 }
13384
13385 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13386 static GTY((cache)) tree_cache_map *explicit_specifier_map;
13387
13388 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13389
13390 void
13391 store_explicit_specifier (tree v, tree t)
13392 {
13393 if (!explicit_specifier_map)
13394 explicit_specifier_map = tree_cache_map::create_ggc (37);
13395 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13396 explicit_specifier_map->put (v, t);
13397 }
13398
13399 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13400
13401 static tree
13402 lookup_explicit_specifier (tree v)
13403 {
13404 return *explicit_specifier_map->get (v);
13405 }
13406
13407 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13408
13409 static tree
13410 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13411 tree lambda_fntype)
13412 {
13413 tree gen_tmpl, argvec;
13414 hashval_t hash = 0;
13415 tree in_decl = t;
13416
13417 /* Nobody should be tsubst'ing into non-template functions. */
13418 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13419
13420 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13421 {
13422 /* If T is not dependent, just return it. */
13423 if (!uses_template_parms (DECL_TI_ARGS (t))
13424 && !LAMBDA_FUNCTION_P (t))
13425 return t;
13426
13427 /* Calculate the most general template of which R is a
13428 specialization. */
13429 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13430
13431 /* We're substituting a lambda function under tsubst_lambda_expr but not
13432 directly from it; find the matching function we're already inside.
13433 But don't do this if T is a generic lambda with a single level of
13434 template parms, as in that case we're doing a normal instantiation. */
13435 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13436 && (!generic_lambda_fn_p (t)
13437 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13438 return enclosing_instantiation_of (t);
13439
13440 /* Calculate the complete set of arguments used to
13441 specialize R. */
13442 argvec = tsubst_template_args (DECL_TI_ARGS
13443 (DECL_TEMPLATE_RESULT
13444 (DECL_TI_TEMPLATE (t))),
13445 args, complain, in_decl);
13446 if (argvec == error_mark_node)
13447 return error_mark_node;
13448
13449 /* Check to see if we already have this specialization. */
13450 if (!lambda_fntype)
13451 {
13452 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13453 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13454 return spec;
13455 }
13456
13457 /* We can see more levels of arguments than parameters if
13458 there was a specialization of a member template, like
13459 this:
13460
13461 template <class T> struct S { template <class U> void f(); }
13462 template <> template <class U> void S<int>::f(U);
13463
13464 Here, we'll be substituting into the specialization,
13465 because that's where we can find the code we actually
13466 want to generate, but we'll have enough arguments for
13467 the most general template.
13468
13469 We also deal with the peculiar case:
13470
13471 template <class T> struct S {
13472 template <class U> friend void f();
13473 };
13474 template <class U> void f() {}
13475 template S<int>;
13476 template void f<double>();
13477
13478 Here, the ARGS for the instantiation of will be {int,
13479 double}. But, we only need as many ARGS as there are
13480 levels of template parameters in CODE_PATTERN. We are
13481 careful not to get fooled into reducing the ARGS in
13482 situations like:
13483
13484 template <class T> struct S { template <class U> void f(U); }
13485 template <class T> template <> void S<T>::f(int) {}
13486
13487 which we can spot because the pattern will be a
13488 specialization in this case. */
13489 int args_depth = TMPL_ARGS_DEPTH (args);
13490 int parms_depth =
13491 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13492
13493 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13494 args = get_innermost_template_args (args, parms_depth);
13495 }
13496 else
13497 {
13498 /* This special case arises when we have something like this:
13499
13500 template <class T> struct S {
13501 friend void f<int>(int, double);
13502 };
13503
13504 Here, the DECL_TI_TEMPLATE for the friend declaration
13505 will be an IDENTIFIER_NODE. We are being called from
13506 tsubst_friend_function, and we want only to create a
13507 new decl (R) with appropriate types so that we can call
13508 determine_specialization. */
13509 gen_tmpl = NULL_TREE;
13510 argvec = NULL_TREE;
13511 }
13512
13513 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13514 : NULL_TREE);
13515 tree ctx = closure ? closure : DECL_CONTEXT (t);
13516 bool member = ctx && TYPE_P (ctx);
13517
13518 if (member && !closure)
13519 ctx = tsubst_aggr_type (ctx, args,
13520 complain, t, /*entering_scope=*/1);
13521
13522 tree type = (lambda_fntype ? lambda_fntype
13523 : tsubst (TREE_TYPE (t), args,
13524 complain | tf_fndecl_type, in_decl));
13525 if (type == error_mark_node)
13526 return error_mark_node;
13527
13528 /* If we hit excessive deduction depth, the type is bogus even if
13529 it isn't error_mark_node, so don't build a decl. */
13530 if (excessive_deduction_depth)
13531 return error_mark_node;
13532
13533 /* We do NOT check for matching decls pushed separately at this
13534 point, as they may not represent instantiations of this
13535 template, and in any case are considered separate under the
13536 discrete model. */
13537 tree r = copy_decl (t);
13538 DECL_USE_TEMPLATE (r) = 0;
13539 TREE_TYPE (r) = type;
13540 /* Clear out the mangled name and RTL for the instantiation. */
13541 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13542 SET_DECL_RTL (r, NULL);
13543 /* Leave DECL_INITIAL set on deleted instantiations. */
13544 if (!DECL_DELETED_FN (r))
13545 DECL_INITIAL (r) = NULL_TREE;
13546 DECL_CONTEXT (r) = ctx;
13547
13548 /* Handle explicit(dependent-expr). */
13549 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13550 {
13551 tree spec = lookup_explicit_specifier (t);
13552 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13553 /*function_p=*/false,
13554 /*i_c_e_p=*/true);
13555 spec = build_explicit_specifier (spec, complain);
13556 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13557 }
13558
13559 /* OpenMP UDRs have the only argument a reference to the declared
13560 type. We want to diagnose if the declared type is a reference,
13561 which is invalid, but as references to references are usually
13562 quietly merged, diagnose it here. */
13563 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13564 {
13565 tree argtype
13566 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13567 argtype = tsubst (argtype, args, complain, in_decl);
13568 if (TYPE_REF_P (argtype))
13569 error_at (DECL_SOURCE_LOCATION (t),
13570 "reference type %qT in "
13571 "%<#pragma omp declare reduction%>", argtype);
13572 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13573 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13574 argtype);
13575 }
13576
13577 if (member && DECL_CONV_FN_P (r))
13578 /* Type-conversion operator. Reconstruct the name, in
13579 case it's the name of one of the template's parameters. */
13580 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13581
13582 tree parms = DECL_ARGUMENTS (t);
13583 if (closure)
13584 parms = DECL_CHAIN (parms);
13585 parms = tsubst (parms, args, complain, t);
13586 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13587 DECL_CONTEXT (parm) = r;
13588 if (closure)
13589 {
13590 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13591 DECL_CHAIN (tparm) = parms;
13592 parms = tparm;
13593 }
13594 DECL_ARGUMENTS (r) = parms;
13595 DECL_RESULT (r) = NULL_TREE;
13596
13597 TREE_STATIC (r) = 0;
13598 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13599 DECL_EXTERNAL (r) = 1;
13600 /* If this is an instantiation of a function with internal
13601 linkage, we already know what object file linkage will be
13602 assigned to the instantiation. */
13603 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13604 DECL_DEFER_OUTPUT (r) = 0;
13605 DECL_CHAIN (r) = NULL_TREE;
13606 DECL_PENDING_INLINE_INFO (r) = 0;
13607 DECL_PENDING_INLINE_P (r) = 0;
13608 DECL_SAVED_TREE (r) = NULL_TREE;
13609 DECL_STRUCT_FUNCTION (r) = NULL;
13610 TREE_USED (r) = 0;
13611 /* We'll re-clone as appropriate in instantiate_template. */
13612 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13613
13614 /* If we aren't complaining now, return on error before we register
13615 the specialization so that we'll complain eventually. */
13616 if ((complain & tf_error) == 0
13617 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13618 && !grok_op_properties (r, /*complain=*/false))
13619 return error_mark_node;
13620
13621 /* Associate the constraints directly with the instantiation. We
13622 don't substitute through the constraints; that's only done when
13623 they are checked. */
13624 if (tree ci = get_constraints (t))
13625 set_constraints (r, ci);
13626
13627 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13628 SET_DECL_FRIEND_CONTEXT (r,
13629 tsubst (DECL_FRIEND_CONTEXT (t),
13630 args, complain, in_decl));
13631
13632 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13633 this in the special friend case mentioned above where
13634 GEN_TMPL is NULL. */
13635 if (gen_tmpl && !closure)
13636 {
13637 DECL_TEMPLATE_INFO (r)
13638 = build_template_info (gen_tmpl, argvec);
13639 SET_DECL_IMPLICIT_INSTANTIATION (r);
13640
13641 tree new_r
13642 = register_specialization (r, gen_tmpl, argvec, false, hash);
13643 if (new_r != r)
13644 /* We instantiated this while substituting into
13645 the type earlier (template/friend54.C). */
13646 return new_r;
13647
13648 /* We're not supposed to instantiate default arguments
13649 until they are called, for a template. But, for a
13650 declaration like:
13651
13652 template <class T> void f ()
13653 { extern void g(int i = T()); }
13654
13655 we should do the substitution when the template is
13656 instantiated. We handle the member function case in
13657 instantiate_class_template since the default arguments
13658 might refer to other members of the class. */
13659 if (!member
13660 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13661 && !uses_template_parms (argvec))
13662 tsubst_default_arguments (r, complain);
13663 }
13664 else
13665 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13666
13667 /* Copy the list of befriending classes. */
13668 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13669 *friends;
13670 friends = &TREE_CHAIN (*friends))
13671 {
13672 *friends = copy_node (*friends);
13673 TREE_VALUE (*friends)
13674 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13675 }
13676
13677 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13678 {
13679 maybe_retrofit_in_chrg (r);
13680 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13681 return error_mark_node;
13682 /* If this is an instantiation of a member template, clone it.
13683 If it isn't, that'll be handled by
13684 clone_constructors_and_destructors. */
13685 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13686 clone_function_decl (r, /*update_methods=*/false);
13687 }
13688 else if ((complain & tf_error) != 0
13689 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13690 && !grok_op_properties (r, /*complain=*/true))
13691 return error_mark_node;
13692
13693 /* Possibly limit visibility based on template args. */
13694 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13695 if (DECL_VISIBILITY_SPECIFIED (t))
13696 {
13697 DECL_VISIBILITY_SPECIFIED (r) = 0;
13698 DECL_ATTRIBUTES (r)
13699 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13700 }
13701 determine_visibility (r);
13702 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13703 && !processing_template_decl)
13704 defaulted_late_check (r);
13705
13706 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13707 args, complain, in_decl);
13708 if (flag_openmp)
13709 if (tree attr = lookup_attribute ("omp declare variant base",
13710 DECL_ATTRIBUTES (r)))
13711 omp_declare_variant_finalize (r, attr);
13712
13713 return r;
13714 }
13715
13716 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13717
13718 static tree
13719 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13720 tree lambda_fntype)
13721 {
13722 /* We can get here when processing a member function template,
13723 member class template, or template template parameter. */
13724 tree decl = DECL_TEMPLATE_RESULT (t);
13725 tree in_decl = t;
13726 tree spec;
13727 tree tmpl_args;
13728 tree full_args;
13729 tree r;
13730 hashval_t hash = 0;
13731
13732 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13733 {
13734 /* Template template parameter is treated here. */
13735 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13736 if (new_type == error_mark_node)
13737 r = error_mark_node;
13738 /* If we get a real template back, return it. This can happen in
13739 the context of most_specialized_partial_spec. */
13740 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13741 r = new_type;
13742 else
13743 /* The new TEMPLATE_DECL was built in
13744 reduce_template_parm_level. */
13745 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13746 return r;
13747 }
13748
13749 if (!lambda_fntype)
13750 {
13751 /* We might already have an instance of this template.
13752 The ARGS are for the surrounding class type, so the
13753 full args contain the tsubst'd args for the context,
13754 plus the innermost args from the template decl. */
13755 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13756 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13757 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13758 /* Because this is a template, the arguments will still be
13759 dependent, even after substitution. If
13760 PROCESSING_TEMPLATE_DECL is not set, the dependency
13761 predicates will short-circuit. */
13762 ++processing_template_decl;
13763 full_args = tsubst_template_args (tmpl_args, args,
13764 complain, in_decl);
13765 --processing_template_decl;
13766 if (full_args == error_mark_node)
13767 return error_mark_node;
13768
13769 /* If this is a default template template argument,
13770 tsubst might not have changed anything. */
13771 if (full_args == tmpl_args)
13772 return t;
13773
13774 hash = hash_tmpl_and_args (t, full_args);
13775 spec = retrieve_specialization (t, full_args, hash);
13776 if (spec != NULL_TREE)
13777 {
13778 if (TYPE_P (spec))
13779 /* Type partial instantiations are stored as the type by
13780 lookup_template_class_1, not here as the template. */
13781 spec = CLASSTYPE_TI_TEMPLATE (spec);
13782 return spec;
13783 }
13784 }
13785
13786 /* Make a new template decl. It will be similar to the
13787 original, but will record the current template arguments.
13788 We also create a new function declaration, which is just
13789 like the old one, but points to this new template, rather
13790 than the old one. */
13791 r = copy_decl (t);
13792 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13793 DECL_CHAIN (r) = NULL_TREE;
13794
13795 // Build new template info linking to the original template decl.
13796 if (!lambda_fntype)
13797 {
13798 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13799 SET_DECL_IMPLICIT_INSTANTIATION (r);
13800 }
13801 else
13802 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13803
13804 /* The template parameters for this new template are all the
13805 template parameters for the old template, except the
13806 outermost level of parameters. */
13807 DECL_TEMPLATE_PARMS (r)
13808 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13809 complain);
13810
13811 if (TREE_CODE (decl) == TYPE_DECL
13812 && !TYPE_DECL_ALIAS_P (decl))
13813 {
13814 tree new_type;
13815 ++processing_template_decl;
13816 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13817 --processing_template_decl;
13818 if (new_type == error_mark_node)
13819 return error_mark_node;
13820
13821 TREE_TYPE (r) = new_type;
13822 /* For a partial specialization, we need to keep pointing to
13823 the primary template. */
13824 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13825 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13826 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13827 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13828 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13829 }
13830 else
13831 {
13832 tree new_decl;
13833 ++processing_template_decl;
13834 if (TREE_CODE (decl) == FUNCTION_DECL)
13835 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13836 else
13837 new_decl = tsubst (decl, args, complain, in_decl);
13838 --processing_template_decl;
13839 if (new_decl == error_mark_node)
13840 return error_mark_node;
13841
13842 DECL_TEMPLATE_RESULT (r) = new_decl;
13843 TREE_TYPE (r) = TREE_TYPE (new_decl);
13844 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13845 if (lambda_fntype)
13846 {
13847 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13848 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13849 }
13850 else
13851 {
13852 DECL_TI_TEMPLATE (new_decl) = r;
13853 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13854 }
13855 }
13856
13857 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13858 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13859
13860 if (PRIMARY_TEMPLATE_P (t))
13861 DECL_PRIMARY_TEMPLATE (r) = r;
13862
13863 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13864 && !lambda_fntype)
13865 /* Record this non-type partial instantiation. */
13866 register_specialization (r, t,
13867 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13868 false, hash);
13869
13870 return r;
13871 }
13872
13873 /* True if FN is the op() for a lambda in an uninstantiated template. */
13874
13875 bool
13876 lambda_fn_in_template_p (tree fn)
13877 {
13878 if (!fn || !LAMBDA_FUNCTION_P (fn))
13879 return false;
13880 tree closure = DECL_CONTEXT (fn);
13881 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13882 }
13883
13884 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13885 which the above is true. */
13886
13887 bool
13888 instantiated_lambda_fn_p (tree fn)
13889 {
13890 if (!fn || !LAMBDA_FUNCTION_P (fn))
13891 return false;
13892 tree closure = DECL_CONTEXT (fn);
13893 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13894 return LAMBDA_EXPR_INSTANTIATED (lam);
13895 }
13896
13897 /* We're instantiating a variable from template function TCTX. Return the
13898 corresponding current enclosing scope. This gets complicated because lambda
13899 functions in templates are regenerated rather than instantiated, but generic
13900 lambda functions are subsequently instantiated. */
13901
13902 static tree
13903 enclosing_instantiation_of (tree otctx)
13904 {
13905 tree tctx = otctx;
13906 tree fn = current_function_decl;
13907 int lambda_count = 0;
13908
13909 for (; tctx && (lambda_fn_in_template_p (tctx)
13910 || instantiated_lambda_fn_p (tctx));
13911 tctx = decl_function_context (tctx))
13912 ++lambda_count;
13913 for (; fn; fn = decl_function_context (fn))
13914 {
13915 tree ofn = fn;
13916 int flambda_count = 0;
13917 for (; fn && instantiated_lambda_fn_p (fn);
13918 fn = decl_function_context (fn))
13919 ++flambda_count;
13920 if ((fn && DECL_TEMPLATE_INFO (fn))
13921 ? most_general_template (fn) != most_general_template (tctx)
13922 : fn != tctx)
13923 continue;
13924 if (flambda_count != lambda_count)
13925 {
13926 gcc_assert (flambda_count > lambda_count);
13927 for (; flambda_count > lambda_count; --flambda_count)
13928 ofn = decl_function_context (ofn);
13929 }
13930 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13931 || DECL_CONV_FN_P (ofn));
13932 return ofn;
13933 }
13934 gcc_unreachable ();
13935 }
13936
13937 /* Substitute the ARGS into the T, which is a _DECL. Return the
13938 result of the substitution. Issue error and warning messages under
13939 control of COMPLAIN. */
13940
13941 static tree
13942 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13943 {
13944 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13945 location_t saved_loc;
13946 tree r = NULL_TREE;
13947 tree in_decl = t;
13948 hashval_t hash = 0;
13949
13950 /* Set the filename and linenumber to improve error-reporting. */
13951 saved_loc = input_location;
13952 input_location = DECL_SOURCE_LOCATION (t);
13953
13954 switch (TREE_CODE (t))
13955 {
13956 case TEMPLATE_DECL:
13957 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13958 break;
13959
13960 case FUNCTION_DECL:
13961 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13962 break;
13963
13964 case PARM_DECL:
13965 {
13966 tree type = NULL_TREE;
13967 int i, len = 1;
13968 tree expanded_types = NULL_TREE;
13969 tree prev_r = NULL_TREE;
13970 tree first_r = NULL_TREE;
13971
13972 if (DECL_PACK_P (t))
13973 {
13974 /* If there is a local specialization that isn't a
13975 parameter pack, it means that we're doing a "simple"
13976 substitution from inside tsubst_pack_expansion. Just
13977 return the local specialization (which will be a single
13978 parm). */
13979 tree spec = retrieve_local_specialization (t);
13980 if (spec
13981 && TREE_CODE (spec) == PARM_DECL
13982 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13983 RETURN (spec);
13984
13985 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13986 the parameters in this function parameter pack. */
13987 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13988 complain, in_decl);
13989 if (TREE_CODE (expanded_types) == TREE_VEC)
13990 {
13991 len = TREE_VEC_LENGTH (expanded_types);
13992
13993 /* Zero-length parameter packs are boring. Just substitute
13994 into the chain. */
13995 if (len == 0)
13996 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13997 TREE_CHAIN (t)));
13998 }
13999 else
14000 {
14001 /* All we did was update the type. Make a note of that. */
14002 type = expanded_types;
14003 expanded_types = NULL_TREE;
14004 }
14005 }
14006
14007 /* Loop through all of the parameters we'll build. When T is
14008 a function parameter pack, LEN is the number of expanded
14009 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14010 r = NULL_TREE;
14011 for (i = 0; i < len; ++i)
14012 {
14013 prev_r = r;
14014 r = copy_node (t);
14015 if (DECL_TEMPLATE_PARM_P (t))
14016 SET_DECL_TEMPLATE_PARM_P (r);
14017
14018 if (expanded_types)
14019 /* We're on the Ith parameter of the function parameter
14020 pack. */
14021 {
14022 /* Get the Ith type. */
14023 type = TREE_VEC_ELT (expanded_types, i);
14024
14025 /* Rename the parameter to include the index. */
14026 DECL_NAME (r)
14027 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14028 }
14029 else if (!type)
14030 /* We're dealing with a normal parameter. */
14031 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14032
14033 type = type_decays_to (type);
14034 TREE_TYPE (r) = type;
14035 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14036
14037 if (DECL_INITIAL (r))
14038 {
14039 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14040 DECL_INITIAL (r) = TREE_TYPE (r);
14041 else
14042 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14043 complain, in_decl);
14044 }
14045
14046 DECL_CONTEXT (r) = NULL_TREE;
14047
14048 if (!DECL_TEMPLATE_PARM_P (r))
14049 DECL_ARG_TYPE (r) = type_passed_as (type);
14050
14051 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14052 args, complain, in_decl);
14053
14054 /* Keep track of the first new parameter we
14055 generate. That's what will be returned to the
14056 caller. */
14057 if (!first_r)
14058 first_r = r;
14059
14060 /* Build a proper chain of parameters when substituting
14061 into a function parameter pack. */
14062 if (prev_r)
14063 DECL_CHAIN (prev_r) = r;
14064 }
14065
14066 /* If cp_unevaluated_operand is set, we're just looking for a
14067 single dummy parameter, so don't keep going. */
14068 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14069 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14070 complain, DECL_CHAIN (t));
14071
14072 /* FIRST_R contains the start of the chain we've built. */
14073 r = first_r;
14074 }
14075 break;
14076
14077 case FIELD_DECL:
14078 {
14079 tree type = NULL_TREE;
14080 tree vec = NULL_TREE;
14081 tree expanded_types = NULL_TREE;
14082 int len = 1;
14083
14084 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14085 {
14086 /* This field is a lambda capture pack. Return a TREE_VEC of
14087 the expanded fields to instantiate_class_template_1. */
14088 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14089 complain, in_decl);
14090 if (TREE_CODE (expanded_types) == TREE_VEC)
14091 {
14092 len = TREE_VEC_LENGTH (expanded_types);
14093 vec = make_tree_vec (len);
14094 }
14095 else
14096 {
14097 /* All we did was update the type. Make a note of that. */
14098 type = expanded_types;
14099 expanded_types = NULL_TREE;
14100 }
14101 }
14102
14103 for (int i = 0; i < len; ++i)
14104 {
14105 r = copy_decl (t);
14106 if (expanded_types)
14107 {
14108 type = TREE_VEC_ELT (expanded_types, i);
14109 DECL_NAME (r)
14110 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14111 }
14112 else if (!type)
14113 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14114
14115 if (type == error_mark_node)
14116 RETURN (error_mark_node);
14117 TREE_TYPE (r) = type;
14118 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14119
14120 if (DECL_C_BIT_FIELD (r))
14121 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14122 number of bits. */
14123 DECL_BIT_FIELD_REPRESENTATIVE (r)
14124 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14125 complain, in_decl,
14126 /*integral_constant_expression_p=*/true);
14127 if (DECL_INITIAL (t))
14128 {
14129 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14130 NSDMI in perform_member_init. Still set DECL_INITIAL
14131 so that we know there is one. */
14132 DECL_INITIAL (r) = void_node;
14133 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14134 retrofit_lang_decl (r);
14135 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14136 }
14137 /* We don't have to set DECL_CONTEXT here; it is set by
14138 finish_member_declaration. */
14139 DECL_CHAIN (r) = NULL_TREE;
14140
14141 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14142 args, complain, in_decl);
14143
14144 if (vec)
14145 TREE_VEC_ELT (vec, i) = r;
14146 }
14147
14148 if (vec)
14149 r = vec;
14150 }
14151 break;
14152
14153 case USING_DECL:
14154 /* We reach here only for member using decls. We also need to check
14155 uses_template_parms because DECL_DEPENDENT_P is not set for a
14156 using-declaration that designates a member of the current
14157 instantiation (c++/53549). */
14158 if (DECL_DEPENDENT_P (t)
14159 || uses_template_parms (USING_DECL_SCOPE (t)))
14160 {
14161 tree scope = USING_DECL_SCOPE (t);
14162 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14163 if (PACK_EXPANSION_P (scope))
14164 {
14165 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14166 int len = TREE_VEC_LENGTH (vec);
14167 r = make_tree_vec (len);
14168 for (int i = 0; i < len; ++i)
14169 {
14170 tree escope = TREE_VEC_ELT (vec, i);
14171 tree elt = do_class_using_decl (escope, name);
14172 if (!elt)
14173 {
14174 r = error_mark_node;
14175 break;
14176 }
14177 else
14178 {
14179 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14180 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14181 }
14182 TREE_VEC_ELT (r, i) = elt;
14183 }
14184 }
14185 else
14186 {
14187 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14188 complain, in_decl);
14189 r = do_class_using_decl (inst_scope, name);
14190 if (!r)
14191 r = error_mark_node;
14192 else
14193 {
14194 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14195 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14196 }
14197 }
14198 }
14199 else
14200 {
14201 r = copy_node (t);
14202 DECL_CHAIN (r) = NULL_TREE;
14203 }
14204 break;
14205
14206 case TYPE_DECL:
14207 case VAR_DECL:
14208 {
14209 tree argvec = NULL_TREE;
14210 tree gen_tmpl = NULL_TREE;
14211 tree spec;
14212 tree tmpl = NULL_TREE;
14213 tree ctx;
14214 tree type = NULL_TREE;
14215 bool local_p;
14216
14217 if (TREE_TYPE (t) == error_mark_node)
14218 RETURN (error_mark_node);
14219
14220 if (TREE_CODE (t) == TYPE_DECL
14221 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14222 {
14223 /* If this is the canonical decl, we don't have to
14224 mess with instantiations, and often we can't (for
14225 typename, template type parms and such). Note that
14226 TYPE_NAME is not correct for the above test if
14227 we've copied the type for a typedef. */
14228 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14229 if (type == error_mark_node)
14230 RETURN (error_mark_node);
14231 r = TYPE_NAME (type);
14232 break;
14233 }
14234
14235 /* Check to see if we already have the specialization we
14236 need. */
14237 spec = NULL_TREE;
14238 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14239 {
14240 /* T is a static data member or namespace-scope entity.
14241 We have to substitute into namespace-scope variables
14242 (not just variable templates) because of cases like:
14243
14244 template <class T> void f() { extern T t; }
14245
14246 where the entity referenced is not known until
14247 instantiation time. */
14248 local_p = false;
14249 ctx = DECL_CONTEXT (t);
14250 if (DECL_CLASS_SCOPE_P (t))
14251 {
14252 ctx = tsubst_aggr_type (ctx, args,
14253 complain,
14254 in_decl, /*entering_scope=*/1);
14255 /* If CTX is unchanged, then T is in fact the
14256 specialization we want. That situation occurs when
14257 referencing a static data member within in its own
14258 class. We can use pointer equality, rather than
14259 same_type_p, because DECL_CONTEXT is always
14260 canonical... */
14261 if (ctx == DECL_CONTEXT (t)
14262 /* ... unless T is a member template; in which
14263 case our caller can be willing to create a
14264 specialization of that template represented
14265 by T. */
14266 && !(DECL_TI_TEMPLATE (t)
14267 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14268 spec = t;
14269 }
14270
14271 if (!spec)
14272 {
14273 tmpl = DECL_TI_TEMPLATE (t);
14274 gen_tmpl = most_general_template (tmpl);
14275 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14276 if (argvec != error_mark_node)
14277 argvec = (coerce_innermost_template_parms
14278 (DECL_TEMPLATE_PARMS (gen_tmpl),
14279 argvec, t, complain,
14280 /*all*/true, /*defarg*/true));
14281 if (argvec == error_mark_node)
14282 RETURN (error_mark_node);
14283 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14284 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14285 }
14286 }
14287 else
14288 {
14289 /* A local variable. */
14290 local_p = true;
14291 /* Subsequent calls to pushdecl will fill this in. */
14292 ctx = NULL_TREE;
14293 /* Unless this is a reference to a static variable from an
14294 enclosing function, in which case we need to fill it in now. */
14295 if (TREE_STATIC (t))
14296 {
14297 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14298 if (fn != current_function_decl)
14299 ctx = fn;
14300 }
14301 spec = retrieve_local_specialization (t);
14302 }
14303 /* If we already have the specialization we need, there is
14304 nothing more to do. */
14305 if (spec)
14306 {
14307 r = spec;
14308 break;
14309 }
14310
14311 /* Create a new node for the specialization we need. */
14312 if (type == NULL_TREE)
14313 {
14314 if (is_typedef_decl (t))
14315 type = DECL_ORIGINAL_TYPE (t);
14316 else
14317 type = TREE_TYPE (t);
14318 if (VAR_P (t)
14319 && VAR_HAD_UNKNOWN_BOUND (t)
14320 && type != error_mark_node)
14321 type = strip_array_domain (type);
14322 tree sub_args = args;
14323 if (tree auto_node = type_uses_auto (type))
14324 {
14325 /* Mask off any template args past the variable's context so we
14326 don't replace the auto with an unrelated argument. */
14327 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14328 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14329 if (extra > 0)
14330 /* This should never happen with the new lambda instantiation
14331 model, but keep the handling just in case. */
14332 gcc_assert (!CHECKING_P),
14333 sub_args = strip_innermost_template_args (args, extra);
14334 }
14335 type = tsubst (type, sub_args, complain, in_decl);
14336 /* Substituting the type might have recursively instantiated this
14337 same alias (c++/86171). */
14338 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14339 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14340 {
14341 r = spec;
14342 break;
14343 }
14344 }
14345 r = copy_decl (t);
14346 if (VAR_P (r))
14347 {
14348 DECL_INITIALIZED_P (r) = 0;
14349 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14350 if (type == error_mark_node)
14351 RETURN (error_mark_node);
14352 if (TREE_CODE (type) == FUNCTION_TYPE)
14353 {
14354 /* It may seem that this case cannot occur, since:
14355
14356 typedef void f();
14357 void g() { f x; }
14358
14359 declares a function, not a variable. However:
14360
14361 typedef void f();
14362 template <typename T> void g() { T t; }
14363 template void g<f>();
14364
14365 is an attempt to declare a variable with function
14366 type. */
14367 error ("variable %qD has function type",
14368 /* R is not yet sufficiently initialized, so we
14369 just use its name. */
14370 DECL_NAME (r));
14371 RETURN (error_mark_node);
14372 }
14373 type = complete_type (type);
14374 /* Wait until cp_finish_decl to set this again, to handle
14375 circular dependency (template/instantiate6.C). */
14376 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14377 type = check_var_type (DECL_NAME (r), type,
14378 DECL_SOURCE_LOCATION (r));
14379 if (DECL_HAS_VALUE_EXPR_P (t))
14380 {
14381 tree ve = DECL_VALUE_EXPR (t);
14382 ve = tsubst_expr (ve, args, complain, in_decl,
14383 /*constant_expression_p=*/false);
14384 if (REFERENCE_REF_P (ve))
14385 {
14386 gcc_assert (TYPE_REF_P (type));
14387 ve = TREE_OPERAND (ve, 0);
14388 }
14389 SET_DECL_VALUE_EXPR (r, ve);
14390 }
14391 if (CP_DECL_THREAD_LOCAL_P (r)
14392 && !processing_template_decl)
14393 set_decl_tls_model (r, decl_default_tls_model (r));
14394 }
14395 else if (DECL_SELF_REFERENCE_P (t))
14396 SET_DECL_SELF_REFERENCE_P (r);
14397 TREE_TYPE (r) = type;
14398 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14399 DECL_CONTEXT (r) = ctx;
14400 /* Clear out the mangled name and RTL for the instantiation. */
14401 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14402 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14403 SET_DECL_RTL (r, NULL);
14404 /* The initializer must not be expanded until it is required;
14405 see [temp.inst]. */
14406 DECL_INITIAL (r) = NULL_TREE;
14407 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14408 if (VAR_P (r))
14409 {
14410 if (DECL_LANG_SPECIFIC (r))
14411 SET_DECL_DEPENDENT_INIT_P (r, false);
14412
14413 SET_DECL_MODE (r, VOIDmode);
14414
14415 /* Possibly limit visibility based on template args. */
14416 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14417 if (DECL_VISIBILITY_SPECIFIED (t))
14418 {
14419 DECL_VISIBILITY_SPECIFIED (r) = 0;
14420 DECL_ATTRIBUTES (r)
14421 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14422 }
14423 determine_visibility (r);
14424 }
14425
14426 if (!local_p)
14427 {
14428 /* A static data member declaration is always marked
14429 external when it is declared in-class, even if an
14430 initializer is present. We mimic the non-template
14431 processing here. */
14432 DECL_EXTERNAL (r) = 1;
14433 if (DECL_NAMESPACE_SCOPE_P (t))
14434 DECL_NOT_REALLY_EXTERN (r) = 1;
14435
14436 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14437 SET_DECL_IMPLICIT_INSTANTIATION (r);
14438 /* Remember whether we require constant initialization of
14439 a non-constant template variable. */
14440 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14441 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14442 if (!error_operand_p (r) || (complain & tf_error))
14443 register_specialization (r, gen_tmpl, argvec, false, hash);
14444 }
14445 else
14446 {
14447 if (DECL_LANG_SPECIFIC (r))
14448 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14449 if (!cp_unevaluated_operand)
14450 register_local_specialization (r, t);
14451 }
14452
14453 DECL_CHAIN (r) = NULL_TREE;
14454
14455 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14456 /*flags=*/0,
14457 args, complain, in_decl);
14458
14459 /* Preserve a typedef that names a type. */
14460 if (is_typedef_decl (r) && type != error_mark_node)
14461 {
14462 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14463 set_underlying_type (r);
14464 if (TYPE_DECL_ALIAS_P (r))
14465 /* An alias template specialization can be dependent
14466 even if its underlying type is not. */
14467 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14468 }
14469
14470 layout_decl (r, 0);
14471 }
14472 break;
14473
14474 default:
14475 gcc_unreachable ();
14476 }
14477 #undef RETURN
14478
14479 out:
14480 /* Restore the file and line information. */
14481 input_location = saved_loc;
14482
14483 return r;
14484 }
14485
14486 /* Substitute into the complete parameter type list PARMS. */
14487
14488 tree
14489 tsubst_function_parms (tree parms,
14490 tree args,
14491 tsubst_flags_t complain,
14492 tree in_decl)
14493 {
14494 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14495 }
14496
14497 /* Substitute into the ARG_TYPES of a function type.
14498 If END is a TREE_CHAIN, leave it and any following types
14499 un-substituted. */
14500
14501 static tree
14502 tsubst_arg_types (tree arg_types,
14503 tree args,
14504 tree end,
14505 tsubst_flags_t complain,
14506 tree in_decl)
14507 {
14508 tree remaining_arg_types;
14509 tree type = NULL_TREE;
14510 int i = 1;
14511 tree expanded_args = NULL_TREE;
14512 tree default_arg;
14513
14514 if (!arg_types || arg_types == void_list_node || arg_types == end)
14515 return arg_types;
14516
14517 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14518 args, end, complain, in_decl);
14519 if (remaining_arg_types == error_mark_node)
14520 return error_mark_node;
14521
14522 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14523 {
14524 /* For a pack expansion, perform substitution on the
14525 entire expression. Later on, we'll handle the arguments
14526 one-by-one. */
14527 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14528 args, complain, in_decl);
14529
14530 if (TREE_CODE (expanded_args) == TREE_VEC)
14531 /* So that we'll spin through the parameters, one by one. */
14532 i = TREE_VEC_LENGTH (expanded_args);
14533 else
14534 {
14535 /* We only partially substituted into the parameter
14536 pack. Our type is TYPE_PACK_EXPANSION. */
14537 type = expanded_args;
14538 expanded_args = NULL_TREE;
14539 }
14540 }
14541
14542 while (i > 0) {
14543 --i;
14544
14545 if (expanded_args)
14546 type = TREE_VEC_ELT (expanded_args, i);
14547 else if (!type)
14548 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14549
14550 if (type == error_mark_node)
14551 return error_mark_node;
14552 if (VOID_TYPE_P (type))
14553 {
14554 if (complain & tf_error)
14555 {
14556 error ("invalid parameter type %qT", type);
14557 if (in_decl)
14558 error ("in declaration %q+D", in_decl);
14559 }
14560 return error_mark_node;
14561 }
14562 /* DR 657. */
14563 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14564 return error_mark_node;
14565
14566 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14567 top-level qualifiers as required. */
14568 type = cv_unqualified (type_decays_to (type));
14569
14570 /* We do not substitute into default arguments here. The standard
14571 mandates that they be instantiated only when needed, which is
14572 done in build_over_call. */
14573 default_arg = TREE_PURPOSE (arg_types);
14574
14575 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14576 since the new op() won't have any associated template arguments for us
14577 to refer to later. */
14578 if (lambda_fn_in_template_p (in_decl))
14579 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14580 false/*fn*/, false/*constexpr*/);
14581
14582 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14583 {
14584 /* We've instantiated a template before its default arguments
14585 have been parsed. This can happen for a nested template
14586 class, and is not an error unless we require the default
14587 argument in a call of this function. */
14588 remaining_arg_types =
14589 tree_cons (default_arg, type, remaining_arg_types);
14590 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14591 remaining_arg_types);
14592 }
14593 else
14594 remaining_arg_types =
14595 hash_tree_cons (default_arg, type, remaining_arg_types);
14596 }
14597
14598 return remaining_arg_types;
14599 }
14600
14601 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14602 *not* handle the exception-specification for FNTYPE, because the
14603 initial substitution of explicitly provided template parameters
14604 during argument deduction forbids substitution into the
14605 exception-specification:
14606
14607 [temp.deduct]
14608
14609 All references in the function type of the function template to the
14610 corresponding template parameters are replaced by the specified tem-
14611 plate argument values. If a substitution in a template parameter or
14612 in the function type of the function template results in an invalid
14613 type, type deduction fails. [Note: The equivalent substitution in
14614 exception specifications is done only when the function is instanti-
14615 ated, at which point a program is ill-formed if the substitution
14616 results in an invalid type.] */
14617
14618 static tree
14619 tsubst_function_type (tree t,
14620 tree args,
14621 tsubst_flags_t complain,
14622 tree in_decl)
14623 {
14624 tree return_type;
14625 tree arg_types = NULL_TREE;
14626 tree fntype;
14627
14628 /* The TYPE_CONTEXT is not used for function/method types. */
14629 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14630
14631 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14632 failure. */
14633 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14634
14635 if (late_return_type_p)
14636 {
14637 /* Substitute the argument types. */
14638 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14639 complain, in_decl);
14640 if (arg_types == error_mark_node)
14641 return error_mark_node;
14642
14643 tree save_ccp = current_class_ptr;
14644 tree save_ccr = current_class_ref;
14645 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14646 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14647 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14648 if (do_inject)
14649 {
14650 /* DR 1207: 'this' is in scope in the trailing return type. */
14651 inject_this_parameter (this_type, cp_type_quals (this_type));
14652 }
14653
14654 /* Substitute the return type. */
14655 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14656
14657 if (do_inject)
14658 {
14659 current_class_ptr = save_ccp;
14660 current_class_ref = save_ccr;
14661 }
14662 }
14663 else
14664 /* Substitute the return type. */
14665 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14666
14667 if (return_type == error_mark_node)
14668 return error_mark_node;
14669 /* DR 486 clarifies that creation of a function type with an
14670 invalid return type is a deduction failure. */
14671 if (TREE_CODE (return_type) == ARRAY_TYPE
14672 || TREE_CODE (return_type) == FUNCTION_TYPE)
14673 {
14674 if (complain & tf_error)
14675 {
14676 if (TREE_CODE (return_type) == ARRAY_TYPE)
14677 error ("function returning an array");
14678 else
14679 error ("function returning a function");
14680 }
14681 return error_mark_node;
14682 }
14683 /* And DR 657. */
14684 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14685 return error_mark_node;
14686
14687 if (!late_return_type_p)
14688 {
14689 /* Substitute the argument types. */
14690 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14691 complain, in_decl);
14692 if (arg_types == error_mark_node)
14693 return error_mark_node;
14694 }
14695
14696 /* Construct a new type node and return it. */
14697 if (TREE_CODE (t) == FUNCTION_TYPE)
14698 {
14699 fntype = build_function_type (return_type, arg_types);
14700 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14701 }
14702 else
14703 {
14704 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14705 /* Don't pick up extra function qualifiers from the basetype. */
14706 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14707 if (! MAYBE_CLASS_TYPE_P (r))
14708 {
14709 /* [temp.deduct]
14710
14711 Type deduction may fail for any of the following
14712 reasons:
14713
14714 -- Attempting to create "pointer to member of T" when T
14715 is not a class type. */
14716 if (complain & tf_error)
14717 error ("creating pointer to member function of non-class type %qT",
14718 r);
14719 return error_mark_node;
14720 }
14721
14722 fntype = build_method_type_directly (r, return_type,
14723 TREE_CHAIN (arg_types));
14724 }
14725 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14726
14727 /* See comment above. */
14728 tree raises = NULL_TREE;
14729 cp_ref_qualifier rqual = type_memfn_rqual (t);
14730 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14731
14732 return fntype;
14733 }
14734
14735 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14736 ARGS into that specification, and return the substituted
14737 specification. If there is no specification, return NULL_TREE. */
14738
14739 static tree
14740 tsubst_exception_specification (tree fntype,
14741 tree args,
14742 tsubst_flags_t complain,
14743 tree in_decl,
14744 bool defer_ok)
14745 {
14746 tree specs;
14747 tree new_specs;
14748
14749 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14750 new_specs = NULL_TREE;
14751 if (specs && TREE_PURPOSE (specs))
14752 {
14753 /* A noexcept-specifier. */
14754 tree expr = TREE_PURPOSE (specs);
14755 if (TREE_CODE (expr) == INTEGER_CST)
14756 new_specs = expr;
14757 else if (defer_ok)
14758 {
14759 /* Defer instantiation of noexcept-specifiers to avoid
14760 excessive instantiations (c++/49107). */
14761 new_specs = make_node (DEFERRED_NOEXCEPT);
14762 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14763 {
14764 /* We already partially instantiated this member template,
14765 so combine the new args with the old. */
14766 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14767 = DEFERRED_NOEXCEPT_PATTERN (expr);
14768 DEFERRED_NOEXCEPT_ARGS (new_specs)
14769 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14770 }
14771 else
14772 {
14773 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14774 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14775 }
14776 }
14777 else
14778 {
14779 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14780 {
14781 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14782 args);
14783 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14784 }
14785 new_specs = tsubst_copy_and_build
14786 (expr, args, complain, in_decl, /*function_p=*/false,
14787 /*integral_constant_expression_p=*/true);
14788 }
14789 new_specs = build_noexcept_spec (new_specs, complain);
14790 }
14791 else if (specs)
14792 {
14793 if (! TREE_VALUE (specs))
14794 new_specs = specs;
14795 else
14796 while (specs)
14797 {
14798 tree spec;
14799 int i, len = 1;
14800 tree expanded_specs = NULL_TREE;
14801
14802 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14803 {
14804 /* Expand the pack expansion type. */
14805 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14806 args, complain,
14807 in_decl);
14808
14809 if (expanded_specs == error_mark_node)
14810 return error_mark_node;
14811 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14812 len = TREE_VEC_LENGTH (expanded_specs);
14813 else
14814 {
14815 /* We're substituting into a member template, so
14816 we got a TYPE_PACK_EXPANSION back. Add that
14817 expansion and move on. */
14818 gcc_assert (TREE_CODE (expanded_specs)
14819 == TYPE_PACK_EXPANSION);
14820 new_specs = add_exception_specifier (new_specs,
14821 expanded_specs,
14822 complain);
14823 specs = TREE_CHAIN (specs);
14824 continue;
14825 }
14826 }
14827
14828 for (i = 0; i < len; ++i)
14829 {
14830 if (expanded_specs)
14831 spec = TREE_VEC_ELT (expanded_specs, i);
14832 else
14833 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14834 if (spec == error_mark_node)
14835 return spec;
14836 new_specs = add_exception_specifier (new_specs, spec,
14837 complain);
14838 }
14839
14840 specs = TREE_CHAIN (specs);
14841 }
14842 }
14843 return new_specs;
14844 }
14845
14846 /* Take the tree structure T and replace template parameters used
14847 therein with the argument vector ARGS. IN_DECL is an associated
14848 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14849 Issue error and warning messages under control of COMPLAIN. Note
14850 that we must be relatively non-tolerant of extensions here, in
14851 order to preserve conformance; if we allow substitutions that
14852 should not be allowed, we may allow argument deductions that should
14853 not succeed, and therefore report ambiguous overload situations
14854 where there are none. In theory, we could allow the substitution,
14855 but indicate that it should have failed, and allow our caller to
14856 make sure that the right thing happens, but we don't try to do this
14857 yet.
14858
14859 This function is used for dealing with types, decls and the like;
14860 for expressions, use tsubst_expr or tsubst_copy. */
14861
14862 tree
14863 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14864 {
14865 enum tree_code code;
14866 tree type, r = NULL_TREE;
14867
14868 if (t == NULL_TREE || t == error_mark_node
14869 || t == integer_type_node
14870 || t == void_type_node
14871 || t == char_type_node
14872 || t == unknown_type_node
14873 || TREE_CODE (t) == NAMESPACE_DECL
14874 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14875 return t;
14876
14877 if (DECL_P (t))
14878 return tsubst_decl (t, args, complain);
14879
14880 if (args == NULL_TREE)
14881 return t;
14882
14883 code = TREE_CODE (t);
14884
14885 if (code == IDENTIFIER_NODE)
14886 type = IDENTIFIER_TYPE_VALUE (t);
14887 else
14888 type = TREE_TYPE (t);
14889
14890 gcc_assert (type != unknown_type_node);
14891
14892 /* Reuse typedefs. We need to do this to handle dependent attributes,
14893 such as attribute aligned. */
14894 if (TYPE_P (t)
14895 && typedef_variant_p (t))
14896 {
14897 tree decl = TYPE_NAME (t);
14898
14899 if (alias_template_specialization_p (t))
14900 {
14901 /* DECL represents an alias template and we want to
14902 instantiate it. */
14903 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14904 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14905 r = instantiate_alias_template (tmpl, gen_args, complain);
14906 }
14907 else if (DECL_CLASS_SCOPE_P (decl)
14908 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14909 && uses_template_parms (DECL_CONTEXT (decl)))
14910 {
14911 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14912 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14913 r = retrieve_specialization (tmpl, gen_args, 0);
14914 }
14915 else if (DECL_FUNCTION_SCOPE_P (decl)
14916 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14917 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14918 r = retrieve_local_specialization (decl);
14919 else
14920 /* The typedef is from a non-template context. */
14921 return t;
14922
14923 if (r)
14924 {
14925 r = TREE_TYPE (r);
14926 r = cp_build_qualified_type_real
14927 (r, cp_type_quals (t) | cp_type_quals (r),
14928 complain | tf_ignore_bad_quals);
14929 return r;
14930 }
14931 else
14932 {
14933 /* We don't have an instantiation yet, so drop the typedef. */
14934 int quals = cp_type_quals (t);
14935 t = DECL_ORIGINAL_TYPE (decl);
14936 t = cp_build_qualified_type_real (t, quals,
14937 complain | tf_ignore_bad_quals);
14938 }
14939 }
14940
14941 bool fndecl_type = (complain & tf_fndecl_type);
14942 complain &= ~tf_fndecl_type;
14943
14944 if (type
14945 && code != TYPENAME_TYPE
14946 && code != TEMPLATE_TYPE_PARM
14947 && code != TEMPLATE_PARM_INDEX
14948 && code != IDENTIFIER_NODE
14949 && code != FUNCTION_TYPE
14950 && code != METHOD_TYPE)
14951 type = tsubst (type, args, complain, in_decl);
14952 if (type == error_mark_node)
14953 return error_mark_node;
14954
14955 switch (code)
14956 {
14957 case RECORD_TYPE:
14958 case UNION_TYPE:
14959 case ENUMERAL_TYPE:
14960 return tsubst_aggr_type (t, args, complain, in_decl,
14961 /*entering_scope=*/0);
14962
14963 case ERROR_MARK:
14964 case IDENTIFIER_NODE:
14965 case VOID_TYPE:
14966 case REAL_TYPE:
14967 case COMPLEX_TYPE:
14968 case VECTOR_TYPE:
14969 case BOOLEAN_TYPE:
14970 case NULLPTR_TYPE:
14971 case LANG_TYPE:
14972 return t;
14973
14974 case INTEGER_TYPE:
14975 if (t == integer_type_node)
14976 return t;
14977
14978 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14979 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14980 return t;
14981
14982 {
14983 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14984
14985 max = tsubst_expr (omax, args, complain, in_decl,
14986 /*integral_constant_expression_p=*/false);
14987
14988 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14989 needed. */
14990 if (TREE_CODE (max) == NOP_EXPR
14991 && TREE_SIDE_EFFECTS (omax)
14992 && !TREE_TYPE (max))
14993 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14994
14995 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14996 with TREE_SIDE_EFFECTS that indicates this is not an integral
14997 constant expression. */
14998 if (processing_template_decl
14999 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15000 {
15001 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15002 TREE_SIDE_EFFECTS (max) = 1;
15003 }
15004
15005 return compute_array_index_type (NULL_TREE, max, complain);
15006 }
15007
15008 case TEMPLATE_TYPE_PARM:
15009 case TEMPLATE_TEMPLATE_PARM:
15010 case BOUND_TEMPLATE_TEMPLATE_PARM:
15011 case TEMPLATE_PARM_INDEX:
15012 {
15013 int idx;
15014 int level;
15015 int levels;
15016 tree arg = NULL_TREE;
15017
15018 /* Early in template argument deduction substitution, we don't
15019 want to reduce the level of 'auto', or it will be confused
15020 with a normal template parm in subsequent deduction. */
15021 if (is_auto (t) && (complain & tf_partial))
15022 return t;
15023
15024 r = NULL_TREE;
15025
15026 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15027 template_parm_level_and_index (t, &level, &idx);
15028
15029 levels = TMPL_ARGS_DEPTH (args);
15030 if (level <= levels
15031 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15032 {
15033 arg = TMPL_ARG (args, level, idx);
15034
15035 /* See through ARGUMENT_PACK_SELECT arguments. */
15036 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15037 arg = argument_pack_select_arg (arg);
15038 }
15039
15040 if (arg == error_mark_node)
15041 return error_mark_node;
15042 else if (arg != NULL_TREE)
15043 {
15044 if (ARGUMENT_PACK_P (arg))
15045 /* If ARG is an argument pack, we don't actually want to
15046 perform a substitution here, because substitutions
15047 for argument packs are only done
15048 element-by-element. We can get to this point when
15049 substituting the type of a non-type template
15050 parameter pack, when that type actually contains
15051 template parameter packs from an outer template, e.g.,
15052
15053 template<typename... Types> struct A {
15054 template<Types... Values> struct B { };
15055 }; */
15056 return t;
15057
15058 if (code == TEMPLATE_TYPE_PARM)
15059 {
15060 int quals;
15061
15062 /* When building concept checks for the purpose of
15063 deducing placeholders, we can end up with wildcards
15064 where types are expected. Adjust this to the deduced
15065 value. */
15066 if (TREE_CODE (arg) == WILDCARD_DECL)
15067 arg = TREE_TYPE (TREE_TYPE (arg));
15068
15069 gcc_assert (TYPE_P (arg));
15070
15071 quals = cp_type_quals (arg) | cp_type_quals (t);
15072
15073 return cp_build_qualified_type_real
15074 (arg, quals, complain | tf_ignore_bad_quals);
15075 }
15076 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15077 {
15078 /* We are processing a type constructed from a
15079 template template parameter. */
15080 tree argvec = tsubst (TYPE_TI_ARGS (t),
15081 args, complain, in_decl);
15082 if (argvec == error_mark_node)
15083 return error_mark_node;
15084
15085 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15086 || TREE_CODE (arg) == TEMPLATE_DECL
15087 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15088
15089 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15090 /* Consider this code:
15091
15092 template <template <class> class Template>
15093 struct Internal {
15094 template <class Arg> using Bind = Template<Arg>;
15095 };
15096
15097 template <template <class> class Template, class Arg>
15098 using Instantiate = Template<Arg>; //#0
15099
15100 template <template <class> class Template,
15101 class Argument>
15102 using Bind =
15103 Instantiate<Internal<Template>::template Bind,
15104 Argument>; //#1
15105
15106 When #1 is parsed, the
15107 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15108 parameter `Template' in #0 matches the
15109 UNBOUND_CLASS_TEMPLATE representing the argument
15110 `Internal<Template>::template Bind'; We then want
15111 to assemble the type `Bind<Argument>' that can't
15112 be fully created right now, because
15113 `Internal<Template>' not being complete, the Bind
15114 template cannot be looked up in that context. So
15115 we need to "store" `Bind<Argument>' for later
15116 when the context of Bind becomes complete. Let's
15117 store that in a TYPENAME_TYPE. */
15118 return make_typename_type (TYPE_CONTEXT (arg),
15119 build_nt (TEMPLATE_ID_EXPR,
15120 TYPE_IDENTIFIER (arg),
15121 argvec),
15122 typename_type,
15123 complain);
15124
15125 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15126 are resolving nested-types in the signature of a
15127 member function templates. Otherwise ARG is a
15128 TEMPLATE_DECL and is the real template to be
15129 instantiated. */
15130 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15131 arg = TYPE_NAME (arg);
15132
15133 r = lookup_template_class (arg,
15134 argvec, in_decl,
15135 DECL_CONTEXT (arg),
15136 /*entering_scope=*/0,
15137 complain);
15138 return cp_build_qualified_type_real
15139 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15140 }
15141 else if (code == TEMPLATE_TEMPLATE_PARM)
15142 return arg;
15143 else
15144 /* TEMPLATE_PARM_INDEX. */
15145 return convert_from_reference (unshare_expr (arg));
15146 }
15147
15148 if (level == 1)
15149 /* This can happen during the attempted tsubst'ing in
15150 unify. This means that we don't yet have any information
15151 about the template parameter in question. */
15152 return t;
15153
15154 /* If we get here, we must have been looking at a parm for a
15155 more deeply nested template. Make a new version of this
15156 template parameter, but with a lower level. */
15157 switch (code)
15158 {
15159 case TEMPLATE_TYPE_PARM:
15160 case TEMPLATE_TEMPLATE_PARM:
15161 case BOUND_TEMPLATE_TEMPLATE_PARM:
15162 if (cp_type_quals (t))
15163 {
15164 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15165 r = cp_build_qualified_type_real
15166 (r, cp_type_quals (t),
15167 complain | (code == TEMPLATE_TYPE_PARM
15168 ? tf_ignore_bad_quals : 0));
15169 }
15170 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15171 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15172 && (r = (TEMPLATE_PARM_DESCENDANTS
15173 (TEMPLATE_TYPE_PARM_INDEX (t))))
15174 && (r = TREE_TYPE (r))
15175 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15176 /* Break infinite recursion when substituting the constraints
15177 of a constrained placeholder. */;
15178 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15179 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15180 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15181 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15182 r = TEMPLATE_PARM_DESCENDANTS (arg))
15183 && (TEMPLATE_PARM_LEVEL (r)
15184 == TEMPLATE_PARM_LEVEL (arg) - levels))
15185 /* Cache the simple case of lowering a type parameter. */
15186 r = TREE_TYPE (r);
15187 else
15188 {
15189 r = copy_type (t);
15190 TEMPLATE_TYPE_PARM_INDEX (r)
15191 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15192 r, levels, args, complain);
15193 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15194 TYPE_MAIN_VARIANT (r) = r;
15195 TYPE_POINTER_TO (r) = NULL_TREE;
15196 TYPE_REFERENCE_TO (r) = NULL_TREE;
15197
15198 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15199 {
15200 /* Propagate constraints on placeholders since they are
15201 only instantiated during satisfaction. */
15202 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15203 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15204 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15205 {
15206 pl = tsubst_copy (pl, args, complain, in_decl);
15207 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15208 }
15209 }
15210
15211 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15212 /* We have reduced the level of the template
15213 template parameter, but not the levels of its
15214 template parameters, so canonical_type_parameter
15215 will not be able to find the canonical template
15216 template parameter for this level. Thus, we
15217 require structural equality checking to compare
15218 TEMPLATE_TEMPLATE_PARMs. */
15219 SET_TYPE_STRUCTURAL_EQUALITY (r);
15220 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15221 SET_TYPE_STRUCTURAL_EQUALITY (r);
15222 else
15223 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15224
15225 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15226 {
15227 tree tinfo = TYPE_TEMPLATE_INFO (t);
15228 /* We might need to substitute into the types of non-type
15229 template parameters. */
15230 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15231 complain, in_decl);
15232 if (tmpl == error_mark_node)
15233 return error_mark_node;
15234 tree argvec = tsubst (TI_ARGS (tinfo), args,
15235 complain, in_decl);
15236 if (argvec == error_mark_node)
15237 return error_mark_node;
15238
15239 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15240 = build_template_info (tmpl, argvec);
15241 }
15242 }
15243 break;
15244
15245 case TEMPLATE_PARM_INDEX:
15246 /* OK, now substitute the type of the non-type parameter. We
15247 couldn't do it earlier because it might be an auto parameter,
15248 and we wouldn't need to if we had an argument. */
15249 type = tsubst (type, args, complain, in_decl);
15250 if (type == error_mark_node)
15251 return error_mark_node;
15252 r = reduce_template_parm_level (t, type, levels, args, complain);
15253 break;
15254
15255 default:
15256 gcc_unreachable ();
15257 }
15258
15259 return r;
15260 }
15261
15262 case TREE_LIST:
15263 {
15264 tree purpose, value, chain;
15265
15266 if (t == void_list_node)
15267 return t;
15268
15269 purpose = TREE_PURPOSE (t);
15270 if (purpose)
15271 {
15272 purpose = tsubst (purpose, args, complain, in_decl);
15273 if (purpose == error_mark_node)
15274 return error_mark_node;
15275 }
15276 value = TREE_VALUE (t);
15277 if (value)
15278 {
15279 value = tsubst (value, args, complain, in_decl);
15280 if (value == error_mark_node)
15281 return error_mark_node;
15282 }
15283 chain = TREE_CHAIN (t);
15284 if (chain && chain != void_type_node)
15285 {
15286 chain = tsubst (chain, args, complain, in_decl);
15287 if (chain == error_mark_node)
15288 return error_mark_node;
15289 }
15290 if (purpose == TREE_PURPOSE (t)
15291 && value == TREE_VALUE (t)
15292 && chain == TREE_CHAIN (t))
15293 return t;
15294 return hash_tree_cons (purpose, value, chain);
15295 }
15296
15297 case TREE_BINFO:
15298 /* We should never be tsubsting a binfo. */
15299 gcc_unreachable ();
15300
15301 case TREE_VEC:
15302 /* A vector of template arguments. */
15303 gcc_assert (!type);
15304 return tsubst_template_args (t, args, complain, in_decl);
15305
15306 case POINTER_TYPE:
15307 case REFERENCE_TYPE:
15308 {
15309 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15310 return t;
15311
15312 /* [temp.deduct]
15313
15314 Type deduction may fail for any of the following
15315 reasons:
15316
15317 -- Attempting to create a pointer to reference type.
15318 -- Attempting to create a reference to a reference type or
15319 a reference to void.
15320
15321 Core issue 106 says that creating a reference to a reference
15322 during instantiation is no longer a cause for failure. We
15323 only enforce this check in strict C++98 mode. */
15324 if ((TYPE_REF_P (type)
15325 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15326 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15327 {
15328 static location_t last_loc;
15329
15330 /* We keep track of the last time we issued this error
15331 message to avoid spewing a ton of messages during a
15332 single bad template instantiation. */
15333 if (complain & tf_error
15334 && last_loc != input_location)
15335 {
15336 if (VOID_TYPE_P (type))
15337 error ("forming reference to void");
15338 else if (code == POINTER_TYPE)
15339 error ("forming pointer to reference type %qT", type);
15340 else
15341 error ("forming reference to reference type %qT", type);
15342 last_loc = input_location;
15343 }
15344
15345 return error_mark_node;
15346 }
15347 else if (TREE_CODE (type) == FUNCTION_TYPE
15348 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15349 || type_memfn_rqual (type) != REF_QUAL_NONE))
15350 {
15351 if (complain & tf_error)
15352 {
15353 if (code == POINTER_TYPE)
15354 error ("forming pointer to qualified function type %qT",
15355 type);
15356 else
15357 error ("forming reference to qualified function type %qT",
15358 type);
15359 }
15360 return error_mark_node;
15361 }
15362 else if (code == POINTER_TYPE)
15363 {
15364 r = build_pointer_type (type);
15365 if (TREE_CODE (type) == METHOD_TYPE)
15366 r = build_ptrmemfunc_type (r);
15367 }
15368 else if (TYPE_REF_P (type))
15369 /* In C++0x, during template argument substitution, when there is an
15370 attempt to create a reference to a reference type, reference
15371 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15372
15373 "If a template-argument for a template-parameter T names a type
15374 that is a reference to a type A, an attempt to create the type
15375 'lvalue reference to cv T' creates the type 'lvalue reference to
15376 A,' while an attempt to create the type type rvalue reference to
15377 cv T' creates the type T"
15378 */
15379 r = cp_build_reference_type
15380 (TREE_TYPE (type),
15381 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15382 else
15383 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15384 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15385
15386 if (r != error_mark_node)
15387 /* Will this ever be needed for TYPE_..._TO values? */
15388 layout_type (r);
15389
15390 return r;
15391 }
15392 case OFFSET_TYPE:
15393 {
15394 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15395 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15396 {
15397 /* [temp.deduct]
15398
15399 Type deduction may fail for any of the following
15400 reasons:
15401
15402 -- Attempting to create "pointer to member of T" when T
15403 is not a class type. */
15404 if (complain & tf_error)
15405 error ("creating pointer to member of non-class type %qT", r);
15406 return error_mark_node;
15407 }
15408 if (TYPE_REF_P (type))
15409 {
15410 if (complain & tf_error)
15411 error ("creating pointer to member reference type %qT", type);
15412 return error_mark_node;
15413 }
15414 if (VOID_TYPE_P (type))
15415 {
15416 if (complain & tf_error)
15417 error ("creating pointer to member of type void");
15418 return error_mark_node;
15419 }
15420 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15421 if (TREE_CODE (type) == FUNCTION_TYPE)
15422 {
15423 /* The type of the implicit object parameter gets its
15424 cv-qualifiers from the FUNCTION_TYPE. */
15425 tree memptr;
15426 tree method_type
15427 = build_memfn_type (type, r, type_memfn_quals (type),
15428 type_memfn_rqual (type));
15429 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15430 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15431 complain);
15432 }
15433 else
15434 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15435 cp_type_quals (t),
15436 complain);
15437 }
15438 case FUNCTION_TYPE:
15439 case METHOD_TYPE:
15440 {
15441 tree fntype;
15442 tree specs;
15443 fntype = tsubst_function_type (t, args, complain, in_decl);
15444 if (fntype == error_mark_node)
15445 return error_mark_node;
15446
15447 /* Substitute the exception specification. */
15448 specs = tsubst_exception_specification (t, args, complain, in_decl,
15449 /*defer_ok*/fndecl_type);
15450 if (specs == error_mark_node)
15451 return error_mark_node;
15452 if (specs)
15453 fntype = build_exception_variant (fntype, specs);
15454 return fntype;
15455 }
15456 case ARRAY_TYPE:
15457 {
15458 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15459 if (domain == error_mark_node)
15460 return error_mark_node;
15461
15462 /* As an optimization, we avoid regenerating the array type if
15463 it will obviously be the same as T. */
15464 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15465 return t;
15466
15467 /* These checks should match the ones in create_array_type_for_decl.
15468
15469 [temp.deduct]
15470
15471 The deduction may fail for any of the following reasons:
15472
15473 -- Attempting to create an array with an element type that
15474 is void, a function type, or a reference type, or [DR337]
15475 an abstract class type. */
15476 if (VOID_TYPE_P (type)
15477 || TREE_CODE (type) == FUNCTION_TYPE
15478 || (TREE_CODE (type) == ARRAY_TYPE
15479 && TYPE_DOMAIN (type) == NULL_TREE)
15480 || TYPE_REF_P (type))
15481 {
15482 if (complain & tf_error)
15483 error ("creating array of %qT", type);
15484 return error_mark_node;
15485 }
15486
15487 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15488 return error_mark_node;
15489
15490 r = build_cplus_array_type (type, domain);
15491
15492 if (!valid_array_size_p (input_location, r, in_decl,
15493 (complain & tf_error)))
15494 return error_mark_node;
15495
15496 if (TYPE_USER_ALIGN (t))
15497 {
15498 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15499 TYPE_USER_ALIGN (r) = 1;
15500 }
15501
15502 return r;
15503 }
15504
15505 case TYPENAME_TYPE:
15506 {
15507 tree ctx = TYPE_CONTEXT (t);
15508 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15509 {
15510 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15511 if (ctx == error_mark_node
15512 || TREE_VEC_LENGTH (ctx) > 1)
15513 return error_mark_node;
15514 if (TREE_VEC_LENGTH (ctx) == 0)
15515 {
15516 if (complain & tf_error)
15517 error ("%qD is instantiated for an empty pack",
15518 TYPENAME_TYPE_FULLNAME (t));
15519 return error_mark_node;
15520 }
15521 ctx = TREE_VEC_ELT (ctx, 0);
15522 }
15523 else
15524 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15525 /*entering_scope=*/1);
15526 if (ctx == error_mark_node)
15527 return error_mark_node;
15528
15529 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15530 complain, in_decl);
15531 if (f == error_mark_node)
15532 return error_mark_node;
15533
15534 if (!MAYBE_CLASS_TYPE_P (ctx))
15535 {
15536 if (complain & tf_error)
15537 error ("%qT is not a class, struct, or union type", ctx);
15538 return error_mark_node;
15539 }
15540 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15541 {
15542 /* Normally, make_typename_type does not require that the CTX
15543 have complete type in order to allow things like:
15544
15545 template <class T> struct S { typename S<T>::X Y; };
15546
15547 But, such constructs have already been resolved by this
15548 point, so here CTX really should have complete type, unless
15549 it's a partial instantiation. */
15550 ctx = complete_type (ctx);
15551 if (!COMPLETE_TYPE_P (ctx))
15552 {
15553 if (complain & tf_error)
15554 cxx_incomplete_type_error (NULL_TREE, ctx);
15555 return error_mark_node;
15556 }
15557 }
15558
15559 f = make_typename_type (ctx, f, typename_type,
15560 complain | tf_keep_type_decl);
15561 if (f == error_mark_node)
15562 return f;
15563 if (TREE_CODE (f) == TYPE_DECL)
15564 {
15565 complain |= tf_ignore_bad_quals;
15566 f = TREE_TYPE (f);
15567 }
15568
15569 if (TREE_CODE (f) != TYPENAME_TYPE)
15570 {
15571 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15572 {
15573 if (complain & tf_error)
15574 error ("%qT resolves to %qT, which is not an enumeration type",
15575 t, f);
15576 else
15577 return error_mark_node;
15578 }
15579 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15580 {
15581 if (complain & tf_error)
15582 error ("%qT resolves to %qT, which is is not a class type",
15583 t, f);
15584 else
15585 return error_mark_node;
15586 }
15587 }
15588
15589 return cp_build_qualified_type_real
15590 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15591 }
15592
15593 case UNBOUND_CLASS_TEMPLATE:
15594 {
15595 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15596 in_decl, /*entering_scope=*/1);
15597 tree name = TYPE_IDENTIFIER (t);
15598 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15599
15600 if (ctx == error_mark_node || name == error_mark_node)
15601 return error_mark_node;
15602
15603 if (parm_list)
15604 parm_list = tsubst_template_parms (parm_list, args, complain);
15605 return make_unbound_class_template (ctx, name, parm_list, complain);
15606 }
15607
15608 case TYPEOF_TYPE:
15609 {
15610 tree type;
15611
15612 ++cp_unevaluated_operand;
15613 ++c_inhibit_evaluation_warnings;
15614
15615 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15616 complain, in_decl,
15617 /*integral_constant_expression_p=*/false);
15618
15619 --cp_unevaluated_operand;
15620 --c_inhibit_evaluation_warnings;
15621
15622 type = finish_typeof (type);
15623 return cp_build_qualified_type_real (type,
15624 cp_type_quals (t)
15625 | cp_type_quals (type),
15626 complain);
15627 }
15628
15629 case DECLTYPE_TYPE:
15630 {
15631 tree type;
15632
15633 ++cp_unevaluated_operand;
15634 ++c_inhibit_evaluation_warnings;
15635
15636 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15637 complain|tf_decltype, in_decl,
15638 /*function_p*/false,
15639 /*integral_constant_expression*/false);
15640
15641 --cp_unevaluated_operand;
15642 --c_inhibit_evaluation_warnings;
15643
15644 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15645 type = lambda_capture_field_type (type,
15646 false /*explicit_init*/,
15647 DECLTYPE_FOR_REF_CAPTURE (t));
15648 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15649 type = lambda_proxy_type (type);
15650 else
15651 {
15652 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15653 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15654 && EXPR_P (type))
15655 /* In a template ~id could be either a complement expression
15656 or an unqualified-id naming a destructor; if instantiating
15657 it produces an expression, it's not an id-expression or
15658 member access. */
15659 id = false;
15660 type = finish_decltype_type (type, id, complain);
15661 }
15662 return cp_build_qualified_type_real (type,
15663 cp_type_quals (t)
15664 | cp_type_quals (type),
15665 complain | tf_ignore_bad_quals);
15666 }
15667
15668 case UNDERLYING_TYPE:
15669 {
15670 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15671 complain, in_decl);
15672 return finish_underlying_type (type);
15673 }
15674
15675 case TYPE_ARGUMENT_PACK:
15676 case NONTYPE_ARGUMENT_PACK:
15677 {
15678 tree r;
15679
15680 if (code == NONTYPE_ARGUMENT_PACK)
15681 r = make_node (code);
15682 else
15683 r = cxx_make_type (code);
15684
15685 tree pack_args = ARGUMENT_PACK_ARGS (t);
15686 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15687 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15688
15689 return r;
15690 }
15691
15692 case VOID_CST:
15693 case INTEGER_CST:
15694 case REAL_CST:
15695 case STRING_CST:
15696 case PLUS_EXPR:
15697 case MINUS_EXPR:
15698 case NEGATE_EXPR:
15699 case NOP_EXPR:
15700 case INDIRECT_REF:
15701 case ADDR_EXPR:
15702 case CALL_EXPR:
15703 case ARRAY_REF:
15704 case SCOPE_REF:
15705 /* We should use one of the expression tsubsts for these codes. */
15706 gcc_unreachable ();
15707
15708 default:
15709 sorry ("use of %qs in template", get_tree_code_name (code));
15710 return error_mark_node;
15711 }
15712 }
15713
15714 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15715 expression on the left-hand side of the "." or "->" operator. We
15716 only do the lookup if we had a dependent BASELINK. Otherwise we
15717 adjust it onto the instantiated heirarchy. */
15718
15719 static tree
15720 tsubst_baselink (tree baselink, tree object_type,
15721 tree args, tsubst_flags_t complain, tree in_decl)
15722 {
15723 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15724 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15725 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15726
15727 tree optype = BASELINK_OPTYPE (baselink);
15728 optype = tsubst (optype, args, complain, in_decl);
15729
15730 tree template_args = NULL_TREE;
15731 bool template_id_p = false;
15732 tree fns = BASELINK_FUNCTIONS (baselink);
15733 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15734 {
15735 template_id_p = true;
15736 template_args = TREE_OPERAND (fns, 1);
15737 fns = TREE_OPERAND (fns, 0);
15738 if (template_args)
15739 template_args = tsubst_template_args (template_args, args,
15740 complain, in_decl);
15741 }
15742
15743 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15744 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15745 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15746
15747 if (dependent_p)
15748 {
15749 tree name = OVL_NAME (fns);
15750 if (IDENTIFIER_CONV_OP_P (name))
15751 name = make_conv_op_name (optype);
15752
15753 if (name == complete_dtor_identifier)
15754 /* Treat as-if non-dependent below. */
15755 dependent_p = false;
15756
15757 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15758 if (!baselink)
15759 {
15760 if ((complain & tf_error)
15761 && constructor_name_p (name, qualifying_scope))
15762 error ("cannot call constructor %<%T::%D%> directly",
15763 qualifying_scope, name);
15764 return error_mark_node;
15765 }
15766
15767 if (BASELINK_P (baselink))
15768 fns = BASELINK_FUNCTIONS (baselink);
15769 }
15770 else
15771 /* We're going to overwrite pieces below, make a duplicate. */
15772 baselink = copy_node (baselink);
15773
15774 /* If lookup found a single function, mark it as used at this point.
15775 (If lookup found multiple functions the one selected later by
15776 overload resolution will be marked as used at that point.) */
15777 if (!template_id_p && !really_overloaded_fn (fns))
15778 {
15779 tree fn = OVL_FIRST (fns);
15780 bool ok = mark_used (fn, complain);
15781 if (!ok && !(complain & tf_error))
15782 return error_mark_node;
15783 if (ok && BASELINK_P (baselink))
15784 /* We might have instantiated an auto function. */
15785 TREE_TYPE (baselink) = TREE_TYPE (fn);
15786 }
15787
15788 if (BASELINK_P (baselink))
15789 {
15790 /* Add back the template arguments, if present. */
15791 if (template_id_p)
15792 BASELINK_FUNCTIONS (baselink)
15793 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15794
15795 /* Update the conversion operator type. */
15796 BASELINK_OPTYPE (baselink) = optype;
15797 }
15798
15799 if (!object_type)
15800 object_type = current_class_type;
15801
15802 if (qualified_p || !dependent_p)
15803 {
15804 baselink = adjust_result_of_qualified_name_lookup (baselink,
15805 qualifying_scope,
15806 object_type);
15807 if (!qualified_p)
15808 /* We need to call adjust_result_of_qualified_name_lookup in case the
15809 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15810 so that we still get virtual function binding. */
15811 BASELINK_QUALIFIED_P (baselink) = false;
15812 }
15813
15814 return baselink;
15815 }
15816
15817 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15818 true if the qualified-id will be a postfix-expression in-and-of
15819 itself; false if more of the postfix-expression follows the
15820 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15821 of "&". */
15822
15823 static tree
15824 tsubst_qualified_id (tree qualified_id, tree args,
15825 tsubst_flags_t complain, tree in_decl,
15826 bool done, bool address_p)
15827 {
15828 tree expr;
15829 tree scope;
15830 tree name;
15831 bool is_template;
15832 tree template_args;
15833 location_t loc = UNKNOWN_LOCATION;
15834
15835 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15836
15837 /* Figure out what name to look up. */
15838 name = TREE_OPERAND (qualified_id, 1);
15839 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15840 {
15841 is_template = true;
15842 loc = EXPR_LOCATION (name);
15843 template_args = TREE_OPERAND (name, 1);
15844 if (template_args)
15845 template_args = tsubst_template_args (template_args, args,
15846 complain, in_decl);
15847 if (template_args == error_mark_node)
15848 return error_mark_node;
15849 name = TREE_OPERAND (name, 0);
15850 }
15851 else
15852 {
15853 is_template = false;
15854 template_args = NULL_TREE;
15855 }
15856
15857 /* Substitute into the qualifying scope. When there are no ARGS, we
15858 are just trying to simplify a non-dependent expression. In that
15859 case the qualifying scope may be dependent, and, in any case,
15860 substituting will not help. */
15861 scope = TREE_OPERAND (qualified_id, 0);
15862 if (args)
15863 {
15864 scope = tsubst (scope, args, complain, in_decl);
15865 expr = tsubst_copy (name, args, complain, in_decl);
15866 }
15867 else
15868 expr = name;
15869
15870 if (dependent_scope_p (scope))
15871 {
15872 if (is_template)
15873 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15874 tree r = build_qualified_name (NULL_TREE, scope, expr,
15875 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15876 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15877 return r;
15878 }
15879
15880 if (!BASELINK_P (name) && !DECL_P (expr))
15881 {
15882 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15883 {
15884 /* A BIT_NOT_EXPR is used to represent a destructor. */
15885 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15886 {
15887 error ("qualifying type %qT does not match destructor name ~%qT",
15888 scope, TREE_OPERAND (expr, 0));
15889 expr = error_mark_node;
15890 }
15891 else
15892 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15893 /*is_type_p=*/0, false);
15894 }
15895 else
15896 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15897 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15898 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15899 {
15900 if (complain & tf_error)
15901 {
15902 error ("dependent-name %qE is parsed as a non-type, but "
15903 "instantiation yields a type", qualified_id);
15904 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15905 }
15906 return error_mark_node;
15907 }
15908 }
15909
15910 if (DECL_P (expr))
15911 {
15912 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15913 scope);
15914 /* Remember that there was a reference to this entity. */
15915 if (!mark_used (expr, complain) && !(complain & tf_error))
15916 return error_mark_node;
15917 }
15918
15919 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15920 {
15921 if (complain & tf_error)
15922 qualified_name_lookup_error (scope,
15923 TREE_OPERAND (qualified_id, 1),
15924 expr, input_location);
15925 return error_mark_node;
15926 }
15927
15928 if (is_template)
15929 {
15930 /* We may be repeating a check already done during parsing, but
15931 if it was well-formed and passed then, it will pass again
15932 now, and if it didn't, we wouldn't have got here. The case
15933 we want to catch is when we couldn't tell then, and can now,
15934 namely when templ prior to substitution was an
15935 identifier. */
15936 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15937 return error_mark_node;
15938
15939 if (variable_template_p (expr))
15940 expr = lookup_and_finish_template_variable (expr, template_args,
15941 complain);
15942 else
15943 expr = lookup_template_function (expr, template_args);
15944 }
15945
15946 if (expr == error_mark_node && complain & tf_error)
15947 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15948 expr, input_location);
15949 else if (TYPE_P (scope))
15950 {
15951 expr = (adjust_result_of_qualified_name_lookup
15952 (expr, scope, current_nonlambda_class_type ()));
15953 expr = (finish_qualified_id_expr
15954 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15955 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15956 /*template_arg_p=*/false, complain));
15957 }
15958
15959 /* Expressions do not generally have reference type. */
15960 if (TREE_CODE (expr) != SCOPE_REF
15961 /* However, if we're about to form a pointer-to-member, we just
15962 want the referenced member referenced. */
15963 && TREE_CODE (expr) != OFFSET_REF)
15964 expr = convert_from_reference (expr);
15965
15966 if (REF_PARENTHESIZED_P (qualified_id))
15967 expr = force_paren_expr (expr);
15968
15969 return expr;
15970 }
15971
15972 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15973 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15974 for tsubst. */
15975
15976 static tree
15977 tsubst_init (tree init, tree decl, tree args,
15978 tsubst_flags_t complain, tree in_decl)
15979 {
15980 if (!init)
15981 return NULL_TREE;
15982
15983 init = tsubst_expr (init, args, complain, in_decl, false);
15984
15985 tree type = TREE_TYPE (decl);
15986
15987 if (!init && type != error_mark_node)
15988 {
15989 if (tree auto_node = type_uses_auto (type))
15990 {
15991 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15992 {
15993 if (complain & tf_error)
15994 error ("initializer for %q#D expands to an empty list "
15995 "of expressions", decl);
15996 return error_mark_node;
15997 }
15998 }
15999 else if (!dependent_type_p (type))
16000 {
16001 /* If we had an initializer but it
16002 instantiated to nothing,
16003 value-initialize the object. This will
16004 only occur when the initializer was a
16005 pack expansion where the parameter packs
16006 used in that expansion were of length
16007 zero. */
16008 init = build_value_init (type, complain);
16009 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16010 init = get_target_expr_sfinae (init, complain);
16011 if (TREE_CODE (init) == TARGET_EXPR)
16012 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16013 }
16014 }
16015
16016 return init;
16017 }
16018
16019 /* Like tsubst, but deals with expressions. This function just replaces
16020 template parms; to finish processing the resultant expression, use
16021 tsubst_copy_and_build or tsubst_expr. */
16022
16023 static tree
16024 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16025 {
16026 enum tree_code code;
16027 tree r;
16028
16029 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16030 return t;
16031
16032 code = TREE_CODE (t);
16033
16034 switch (code)
16035 {
16036 case PARM_DECL:
16037 r = retrieve_local_specialization (t);
16038
16039 if (r == NULL_TREE)
16040 {
16041 /* We get here for a use of 'this' in an NSDMI. */
16042 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16043 return current_class_ptr;
16044
16045 /* This can happen for a parameter name used later in a function
16046 declaration (such as in a late-specified return type). Just
16047 make a dummy decl, since it's only used for its type. */
16048 gcc_assert (cp_unevaluated_operand != 0);
16049 r = tsubst_decl (t, args, complain);
16050 /* Give it the template pattern as its context; its true context
16051 hasn't been instantiated yet and this is good enough for
16052 mangling. */
16053 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16054 }
16055
16056 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16057 r = argument_pack_select_arg (r);
16058 if (!mark_used (r, complain) && !(complain & tf_error))
16059 return error_mark_node;
16060 return r;
16061
16062 case CONST_DECL:
16063 {
16064 tree enum_type;
16065 tree v;
16066
16067 if (DECL_TEMPLATE_PARM_P (t))
16068 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16069 /* There is no need to substitute into namespace-scope
16070 enumerators. */
16071 if (DECL_NAMESPACE_SCOPE_P (t))
16072 return t;
16073 /* If ARGS is NULL, then T is known to be non-dependent. */
16074 if (args == NULL_TREE)
16075 return scalar_constant_value (t);
16076
16077 /* Unfortunately, we cannot just call lookup_name here.
16078 Consider:
16079
16080 template <int I> int f() {
16081 enum E { a = I };
16082 struct S { void g() { E e = a; } };
16083 };
16084
16085 When we instantiate f<7>::S::g(), say, lookup_name is not
16086 clever enough to find f<7>::a. */
16087 enum_type
16088 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16089 /*entering_scope=*/0);
16090
16091 for (v = TYPE_VALUES (enum_type);
16092 v != NULL_TREE;
16093 v = TREE_CHAIN (v))
16094 if (TREE_PURPOSE (v) == DECL_NAME (t))
16095 return TREE_VALUE (v);
16096
16097 /* We didn't find the name. That should never happen; if
16098 name-lookup found it during preliminary parsing, we
16099 should find it again here during instantiation. */
16100 gcc_unreachable ();
16101 }
16102 return t;
16103
16104 case FIELD_DECL:
16105 if (DECL_CONTEXT (t))
16106 {
16107 tree ctx;
16108
16109 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16110 /*entering_scope=*/1);
16111 if (ctx != DECL_CONTEXT (t))
16112 {
16113 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16114 if (!r)
16115 {
16116 if (complain & tf_error)
16117 error ("using invalid field %qD", t);
16118 return error_mark_node;
16119 }
16120 return r;
16121 }
16122 }
16123
16124 return t;
16125
16126 case VAR_DECL:
16127 case FUNCTION_DECL:
16128 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16129 r = tsubst (t, args, complain, in_decl);
16130 else if (local_variable_p (t)
16131 && uses_template_parms (DECL_CONTEXT (t)))
16132 {
16133 r = retrieve_local_specialization (t);
16134 if (r == NULL_TREE)
16135 {
16136 /* First try name lookup to find the instantiation. */
16137 r = lookup_name (DECL_NAME (t));
16138 if (r)
16139 {
16140 if (!VAR_P (r))
16141 {
16142 /* During error-recovery we may find a non-variable,
16143 even an OVERLOAD: just bail out and avoid ICEs and
16144 duplicate diagnostics (c++/62207). */
16145 gcc_assert (seen_error ());
16146 return error_mark_node;
16147 }
16148 if (!is_capture_proxy (r))
16149 {
16150 /* Make sure the one we found is the one we want. */
16151 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16152 if (ctx != DECL_CONTEXT (r))
16153 r = NULL_TREE;
16154 }
16155 }
16156
16157 if (r)
16158 /* OK */;
16159 else
16160 {
16161 /* This can happen for a variable used in a
16162 late-specified return type of a local lambda, or for a
16163 local static or constant. Building a new VAR_DECL
16164 should be OK in all those cases. */
16165 r = tsubst_decl (t, args, complain);
16166 if (local_specializations)
16167 /* Avoid infinite recursion (79640). */
16168 register_local_specialization (r, t);
16169 if (decl_maybe_constant_var_p (r))
16170 {
16171 /* We can't call cp_finish_decl, so handle the
16172 initializer by hand. */
16173 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16174 complain, in_decl);
16175 if (!processing_template_decl)
16176 init = maybe_constant_init (init);
16177 if (processing_template_decl
16178 ? potential_constant_expression (init)
16179 : reduced_constant_expression_p (init))
16180 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16181 = TREE_CONSTANT (r) = true;
16182 DECL_INITIAL (r) = init;
16183 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16184 TREE_TYPE (r)
16185 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16186 complain, adc_variable_type);
16187 }
16188 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16189 || decl_constant_var_p (r)
16190 || seen_error ());
16191 if (!processing_template_decl
16192 && !TREE_STATIC (r))
16193 r = process_outer_var_ref (r, complain);
16194 }
16195 /* Remember this for subsequent uses. */
16196 if (local_specializations)
16197 register_local_specialization (r, t);
16198 }
16199 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16200 r = argument_pack_select_arg (r);
16201 }
16202 else
16203 r = t;
16204 if (!mark_used (r, complain))
16205 return error_mark_node;
16206 return r;
16207
16208 case NAMESPACE_DECL:
16209 return t;
16210
16211 case OVERLOAD:
16212 return t;
16213
16214 case BASELINK:
16215 return tsubst_baselink (t, current_nonlambda_class_type (),
16216 args, complain, in_decl);
16217
16218 case TEMPLATE_DECL:
16219 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16220 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16221 args, complain, in_decl);
16222 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16223 return tsubst (t, args, complain, in_decl);
16224 else if (DECL_CLASS_SCOPE_P (t)
16225 && uses_template_parms (DECL_CONTEXT (t)))
16226 {
16227 /* Template template argument like the following example need
16228 special treatment:
16229
16230 template <template <class> class TT> struct C {};
16231 template <class T> struct D {
16232 template <class U> struct E {};
16233 C<E> c; // #1
16234 };
16235 D<int> d; // #2
16236
16237 We are processing the template argument `E' in #1 for
16238 the template instantiation #2. Originally, `E' is a
16239 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16240 have to substitute this with one having context `D<int>'. */
16241
16242 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16243 if (dependent_scope_p (context))
16244 {
16245 /* When rewriting a constructor into a deduction guide, a
16246 non-dependent name can become dependent, so memtmpl<args>
16247 becomes context::template memtmpl<args>. */
16248 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16249 return build_qualified_name (type, context, DECL_NAME (t),
16250 /*template*/true);
16251 }
16252 return lookup_field (context, DECL_NAME(t), 0, false);
16253 }
16254 else
16255 /* Ordinary template template argument. */
16256 return t;
16257
16258 case NON_LVALUE_EXPR:
16259 case VIEW_CONVERT_EXPR:
16260 {
16261 /* Handle location wrappers by substituting the wrapped node
16262 first, *then* reusing the resulting type. Doing the type
16263 first ensures that we handle template parameters and
16264 parameter pack expansions. */
16265 if (location_wrapper_p (t))
16266 {
16267 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16268 complain, in_decl);
16269 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16270 }
16271 tree op = TREE_OPERAND (t, 0);
16272 if (code == VIEW_CONVERT_EXPR
16273 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16274 {
16275 /* Wrapper to make a C++20 template parameter object const. */
16276 op = tsubst_copy (op, args, complain, in_decl);
16277 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16278 {
16279 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16280 return build1 (code, type, op);
16281 }
16282 else
16283 {
16284 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
16285 return op;
16286 }
16287 }
16288 /* We shouldn't see any other uses of these in templates. */
16289 gcc_unreachable ();
16290 }
16291
16292 case CAST_EXPR:
16293 case REINTERPRET_CAST_EXPR:
16294 case CONST_CAST_EXPR:
16295 case STATIC_CAST_EXPR:
16296 case DYNAMIC_CAST_EXPR:
16297 case IMPLICIT_CONV_EXPR:
16298 case CONVERT_EXPR:
16299 case NOP_EXPR:
16300 {
16301 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16302 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16303 return build1 (code, type, op0);
16304 }
16305
16306 case SIZEOF_EXPR:
16307 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16308 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16309 {
16310 tree expanded, op = TREE_OPERAND (t, 0);
16311 int len = 0;
16312
16313 if (SIZEOF_EXPR_TYPE_P (t))
16314 op = TREE_TYPE (op);
16315
16316 ++cp_unevaluated_operand;
16317 ++c_inhibit_evaluation_warnings;
16318 /* We only want to compute the number of arguments. */
16319 if (PACK_EXPANSION_P (op))
16320 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16321 else
16322 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16323 args, complain, in_decl);
16324 --cp_unevaluated_operand;
16325 --c_inhibit_evaluation_warnings;
16326
16327 if (TREE_CODE (expanded) == TREE_VEC)
16328 {
16329 len = TREE_VEC_LENGTH (expanded);
16330 /* Set TREE_USED for the benefit of -Wunused. */
16331 for (int i = 0; i < len; i++)
16332 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16333 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16334 }
16335
16336 if (expanded == error_mark_node)
16337 return error_mark_node;
16338 else if (PACK_EXPANSION_P (expanded)
16339 || (TREE_CODE (expanded) == TREE_VEC
16340 && pack_expansion_args_count (expanded)))
16341
16342 {
16343 if (PACK_EXPANSION_P (expanded))
16344 /* OK. */;
16345 else if (TREE_VEC_LENGTH (expanded) == 1)
16346 expanded = TREE_VEC_ELT (expanded, 0);
16347 else
16348 expanded = make_argument_pack (expanded);
16349
16350 if (TYPE_P (expanded))
16351 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
16352 false,
16353 complain & tf_error);
16354 else
16355 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
16356 complain & tf_error);
16357 }
16358 else
16359 return build_int_cst (size_type_node, len);
16360 }
16361 if (SIZEOF_EXPR_TYPE_P (t))
16362 {
16363 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16364 args, complain, in_decl);
16365 r = build1 (NOP_EXPR, r, error_mark_node);
16366 r = build1 (SIZEOF_EXPR,
16367 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16368 SIZEOF_EXPR_TYPE_P (r) = 1;
16369 return r;
16370 }
16371 /* Fall through */
16372
16373 case INDIRECT_REF:
16374 case NEGATE_EXPR:
16375 case TRUTH_NOT_EXPR:
16376 case BIT_NOT_EXPR:
16377 case ADDR_EXPR:
16378 case UNARY_PLUS_EXPR: /* Unary + */
16379 case ALIGNOF_EXPR:
16380 case AT_ENCODE_EXPR:
16381 case ARROW_EXPR:
16382 case THROW_EXPR:
16383 case TYPEID_EXPR:
16384 case REALPART_EXPR:
16385 case IMAGPART_EXPR:
16386 case PAREN_EXPR:
16387 {
16388 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16389 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16390 r = build1 (code, type, op0);
16391 if (code == ALIGNOF_EXPR)
16392 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16393 return r;
16394 }
16395
16396 case COMPONENT_REF:
16397 {
16398 tree object;
16399 tree name;
16400
16401 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16402 name = TREE_OPERAND (t, 1);
16403 if (TREE_CODE (name) == BIT_NOT_EXPR)
16404 {
16405 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16406 complain, in_decl);
16407 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16408 }
16409 else if (TREE_CODE (name) == SCOPE_REF
16410 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16411 {
16412 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16413 complain, in_decl);
16414 name = TREE_OPERAND (name, 1);
16415 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16416 complain, in_decl);
16417 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16418 name = build_qualified_name (/*type=*/NULL_TREE,
16419 base, name,
16420 /*template_p=*/false);
16421 }
16422 else if (BASELINK_P (name))
16423 name = tsubst_baselink (name,
16424 non_reference (TREE_TYPE (object)),
16425 args, complain,
16426 in_decl);
16427 else
16428 name = tsubst_copy (name, args, complain, in_decl);
16429 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16430 }
16431
16432 case PLUS_EXPR:
16433 case MINUS_EXPR:
16434 case MULT_EXPR:
16435 case TRUNC_DIV_EXPR:
16436 case CEIL_DIV_EXPR:
16437 case FLOOR_DIV_EXPR:
16438 case ROUND_DIV_EXPR:
16439 case EXACT_DIV_EXPR:
16440 case BIT_AND_EXPR:
16441 case BIT_IOR_EXPR:
16442 case BIT_XOR_EXPR:
16443 case TRUNC_MOD_EXPR:
16444 case FLOOR_MOD_EXPR:
16445 case TRUTH_ANDIF_EXPR:
16446 case TRUTH_ORIF_EXPR:
16447 case TRUTH_AND_EXPR:
16448 case TRUTH_OR_EXPR:
16449 case RSHIFT_EXPR:
16450 case LSHIFT_EXPR:
16451 case EQ_EXPR:
16452 case NE_EXPR:
16453 case MAX_EXPR:
16454 case MIN_EXPR:
16455 case LE_EXPR:
16456 case GE_EXPR:
16457 case LT_EXPR:
16458 case GT_EXPR:
16459 case COMPOUND_EXPR:
16460 case DOTSTAR_EXPR:
16461 case MEMBER_REF:
16462 case PREDECREMENT_EXPR:
16463 case PREINCREMENT_EXPR:
16464 case POSTDECREMENT_EXPR:
16465 case POSTINCREMENT_EXPR:
16466 {
16467 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16468 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16469 return build_nt (code, op0, op1);
16470 }
16471
16472 case SCOPE_REF:
16473 {
16474 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16475 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16476 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16477 QUALIFIED_NAME_IS_TEMPLATE (t));
16478 }
16479
16480 case ARRAY_REF:
16481 {
16482 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16483 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16484 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16485 }
16486
16487 case CALL_EXPR:
16488 {
16489 int n = VL_EXP_OPERAND_LENGTH (t);
16490 tree result = build_vl_exp (CALL_EXPR, n);
16491 int i;
16492 for (i = 0; i < n; i++)
16493 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16494 complain, in_decl);
16495 return result;
16496 }
16497
16498 case COND_EXPR:
16499 case MODOP_EXPR:
16500 case PSEUDO_DTOR_EXPR:
16501 case VEC_PERM_EXPR:
16502 {
16503 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16504 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16505 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16506 r = build_nt (code, op0, op1, op2);
16507 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16508 return r;
16509 }
16510
16511 case NEW_EXPR:
16512 {
16513 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16514 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16515 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16516 r = build_nt (code, op0, op1, op2);
16517 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16518 return r;
16519 }
16520
16521 case DELETE_EXPR:
16522 {
16523 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16524 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16525 r = build_nt (code, op0, op1);
16526 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16527 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16528 return r;
16529 }
16530
16531 case TEMPLATE_ID_EXPR:
16532 {
16533 /* Substituted template arguments */
16534 tree fn = TREE_OPERAND (t, 0);
16535 tree targs = TREE_OPERAND (t, 1);
16536
16537 fn = tsubst_copy (fn, args, complain, in_decl);
16538 if (targs)
16539 targs = tsubst_template_args (targs, args, complain, in_decl);
16540
16541 return lookup_template_function (fn, targs);
16542 }
16543
16544 case TREE_LIST:
16545 {
16546 tree purpose, value, chain;
16547
16548 if (t == void_list_node)
16549 return t;
16550
16551 purpose = TREE_PURPOSE (t);
16552 if (purpose)
16553 purpose = tsubst_copy (purpose, args, complain, in_decl);
16554 value = TREE_VALUE (t);
16555 if (value)
16556 value = tsubst_copy (value, args, complain, in_decl);
16557 chain = TREE_CHAIN (t);
16558 if (chain && chain != void_type_node)
16559 chain = tsubst_copy (chain, args, complain, in_decl);
16560 if (purpose == TREE_PURPOSE (t)
16561 && value == TREE_VALUE (t)
16562 && chain == TREE_CHAIN (t))
16563 return t;
16564 return tree_cons (purpose, value, chain);
16565 }
16566
16567 case RECORD_TYPE:
16568 case UNION_TYPE:
16569 case ENUMERAL_TYPE:
16570 case INTEGER_TYPE:
16571 case TEMPLATE_TYPE_PARM:
16572 case TEMPLATE_TEMPLATE_PARM:
16573 case BOUND_TEMPLATE_TEMPLATE_PARM:
16574 case TEMPLATE_PARM_INDEX:
16575 case POINTER_TYPE:
16576 case REFERENCE_TYPE:
16577 case OFFSET_TYPE:
16578 case FUNCTION_TYPE:
16579 case METHOD_TYPE:
16580 case ARRAY_TYPE:
16581 case TYPENAME_TYPE:
16582 case UNBOUND_CLASS_TEMPLATE:
16583 case TYPEOF_TYPE:
16584 case DECLTYPE_TYPE:
16585 case TYPE_DECL:
16586 return tsubst (t, args, complain, in_decl);
16587
16588 case USING_DECL:
16589 t = DECL_NAME (t);
16590 /* Fall through. */
16591 case IDENTIFIER_NODE:
16592 if (IDENTIFIER_CONV_OP_P (t))
16593 {
16594 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16595 return make_conv_op_name (new_type);
16596 }
16597 else
16598 return t;
16599
16600 case CONSTRUCTOR:
16601 /* This is handled by tsubst_copy_and_build. */
16602 gcc_unreachable ();
16603
16604 case VA_ARG_EXPR:
16605 {
16606 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16607 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16608 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16609 }
16610
16611 case CLEANUP_POINT_EXPR:
16612 /* We shouldn't have built any of these during initial template
16613 generation. Instead, they should be built during instantiation
16614 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16615 gcc_unreachable ();
16616
16617 case OFFSET_REF:
16618 {
16619 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16620 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16621 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16622 r = build2 (code, type, op0, op1);
16623 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16624 if (!mark_used (TREE_OPERAND (r, 1), complain)
16625 && !(complain & tf_error))
16626 return error_mark_node;
16627 return r;
16628 }
16629
16630 case EXPR_PACK_EXPANSION:
16631 error ("invalid use of pack expansion expression");
16632 return error_mark_node;
16633
16634 case NONTYPE_ARGUMENT_PACK:
16635 error ("use %<...%> to expand argument pack");
16636 return error_mark_node;
16637
16638 case VOID_CST:
16639 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16640 return t;
16641
16642 case INTEGER_CST:
16643 case REAL_CST:
16644 case STRING_CST:
16645 case COMPLEX_CST:
16646 {
16647 /* Instantiate any typedefs in the type. */
16648 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16649 r = fold_convert (type, t);
16650 gcc_assert (TREE_CODE (r) == code);
16651 return r;
16652 }
16653
16654 case PTRMEM_CST:
16655 /* These can sometimes show up in a partial instantiation, but never
16656 involve template parms. */
16657 gcc_assert (!uses_template_parms (t));
16658 return t;
16659
16660 case UNARY_LEFT_FOLD_EXPR:
16661 return tsubst_unary_left_fold (t, args, complain, in_decl);
16662 case UNARY_RIGHT_FOLD_EXPR:
16663 return tsubst_unary_right_fold (t, args, complain, in_decl);
16664 case BINARY_LEFT_FOLD_EXPR:
16665 return tsubst_binary_left_fold (t, args, complain, in_decl);
16666 case BINARY_RIGHT_FOLD_EXPR:
16667 return tsubst_binary_right_fold (t, args, complain, in_decl);
16668 case PREDICT_EXPR:
16669 return t;
16670
16671 case DEBUG_BEGIN_STMT:
16672 /* ??? There's no point in copying it for now, but maybe some
16673 day it will contain more information, such as a pointer back
16674 to the containing function, inlined copy or so. */
16675 return t;
16676
16677 default:
16678 /* We shouldn't get here, but keep going if !flag_checking. */
16679 if (flag_checking)
16680 gcc_unreachable ();
16681 return t;
16682 }
16683 }
16684
16685 /* Helper function for tsubst_omp_clauses, used for instantiation of
16686 OMP_CLAUSE_DECL of clauses. */
16687
16688 static tree
16689 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16690 tree in_decl, tree *iterator_cache)
16691 {
16692 if (decl == NULL_TREE)
16693 return NULL_TREE;
16694
16695 /* Handle OpenMP iterators. */
16696 if (TREE_CODE (decl) == TREE_LIST
16697 && TREE_PURPOSE (decl)
16698 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16699 {
16700 tree ret;
16701 if (iterator_cache[0] == TREE_PURPOSE (decl))
16702 ret = iterator_cache[1];
16703 else
16704 {
16705 tree *tp = &ret;
16706 begin_scope (sk_omp, NULL);
16707 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16708 {
16709 *tp = copy_node (it);
16710 TREE_VEC_ELT (*tp, 0)
16711 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16712 TREE_VEC_ELT (*tp, 1)
16713 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16714 /*integral_constant_expression_p=*/false);
16715 TREE_VEC_ELT (*tp, 2)
16716 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16717 /*integral_constant_expression_p=*/false);
16718 TREE_VEC_ELT (*tp, 3)
16719 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16720 /*integral_constant_expression_p=*/false);
16721 TREE_CHAIN (*tp) = NULL_TREE;
16722 tp = &TREE_CHAIN (*tp);
16723 }
16724 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16725 iterator_cache[0] = TREE_PURPOSE (decl);
16726 iterator_cache[1] = ret;
16727 }
16728 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16729 args, complain,
16730 in_decl, NULL));
16731 }
16732
16733 /* Handle an OpenMP array section represented as a TREE_LIST (or
16734 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16735 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16736 TREE_LIST. We can handle it exactly the same as an array section
16737 (purpose, value, and a chain), even though the nomenclature
16738 (low_bound, length, etc) is different. */
16739 if (TREE_CODE (decl) == TREE_LIST)
16740 {
16741 tree low_bound
16742 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16743 /*integral_constant_expression_p=*/false);
16744 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16745 /*integral_constant_expression_p=*/false);
16746 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16747 in_decl, NULL);
16748 if (TREE_PURPOSE (decl) == low_bound
16749 && TREE_VALUE (decl) == length
16750 && TREE_CHAIN (decl) == chain)
16751 return decl;
16752 tree ret = tree_cons (low_bound, length, chain);
16753 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16754 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16755 return ret;
16756 }
16757 tree ret = tsubst_expr (decl, args, complain, in_decl,
16758 /*integral_constant_expression_p=*/false);
16759 /* Undo convert_from_reference tsubst_expr could have called. */
16760 if (decl
16761 && REFERENCE_REF_P (ret)
16762 && !REFERENCE_REF_P (decl))
16763 ret = TREE_OPERAND (ret, 0);
16764 return ret;
16765 }
16766
16767 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16768
16769 static tree
16770 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16771 tree args, tsubst_flags_t complain, tree in_decl)
16772 {
16773 tree new_clauses = NULL_TREE, nc, oc;
16774 tree linear_no_step = NULL_TREE;
16775 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16776
16777 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16778 {
16779 nc = copy_node (oc);
16780 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16781 new_clauses = nc;
16782
16783 switch (OMP_CLAUSE_CODE (nc))
16784 {
16785 case OMP_CLAUSE_LASTPRIVATE:
16786 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16787 {
16788 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16789 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16790 in_decl, /*integral_constant_expression_p=*/false);
16791 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16792 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16793 }
16794 /* FALLTHRU */
16795 case OMP_CLAUSE_PRIVATE:
16796 case OMP_CLAUSE_SHARED:
16797 case OMP_CLAUSE_FIRSTPRIVATE:
16798 case OMP_CLAUSE_COPYIN:
16799 case OMP_CLAUSE_COPYPRIVATE:
16800 case OMP_CLAUSE_UNIFORM:
16801 case OMP_CLAUSE_DEPEND:
16802 case OMP_CLAUSE_FROM:
16803 case OMP_CLAUSE_TO:
16804 case OMP_CLAUSE_MAP:
16805 case OMP_CLAUSE_NONTEMPORAL:
16806 case OMP_CLAUSE_USE_DEVICE_PTR:
16807 case OMP_CLAUSE_USE_DEVICE_ADDR:
16808 case OMP_CLAUSE_IS_DEVICE_PTR:
16809 case OMP_CLAUSE_INCLUSIVE:
16810 case OMP_CLAUSE_EXCLUSIVE:
16811 OMP_CLAUSE_DECL (nc)
16812 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16813 in_decl, iterator_cache);
16814 break;
16815 case OMP_CLAUSE_TILE:
16816 case OMP_CLAUSE_IF:
16817 case OMP_CLAUSE_NUM_THREADS:
16818 case OMP_CLAUSE_SCHEDULE:
16819 case OMP_CLAUSE_COLLAPSE:
16820 case OMP_CLAUSE_FINAL:
16821 case OMP_CLAUSE_DEVICE:
16822 case OMP_CLAUSE_DIST_SCHEDULE:
16823 case OMP_CLAUSE_NUM_TEAMS:
16824 case OMP_CLAUSE_THREAD_LIMIT:
16825 case OMP_CLAUSE_SAFELEN:
16826 case OMP_CLAUSE_SIMDLEN:
16827 case OMP_CLAUSE_NUM_TASKS:
16828 case OMP_CLAUSE_GRAINSIZE:
16829 case OMP_CLAUSE_PRIORITY:
16830 case OMP_CLAUSE_ORDERED:
16831 case OMP_CLAUSE_HINT:
16832 case OMP_CLAUSE_NUM_GANGS:
16833 case OMP_CLAUSE_NUM_WORKERS:
16834 case OMP_CLAUSE_VECTOR_LENGTH:
16835 case OMP_CLAUSE_WORKER:
16836 case OMP_CLAUSE_VECTOR:
16837 case OMP_CLAUSE_ASYNC:
16838 case OMP_CLAUSE_WAIT:
16839 OMP_CLAUSE_OPERAND (nc, 0)
16840 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16841 in_decl, /*integral_constant_expression_p=*/false);
16842 break;
16843 case OMP_CLAUSE_REDUCTION:
16844 case OMP_CLAUSE_IN_REDUCTION:
16845 case OMP_CLAUSE_TASK_REDUCTION:
16846 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16847 {
16848 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16849 if (TREE_CODE (placeholder) == SCOPE_REF)
16850 {
16851 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16852 complain, in_decl);
16853 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16854 = build_qualified_name (NULL_TREE, scope,
16855 TREE_OPERAND (placeholder, 1),
16856 false);
16857 }
16858 else
16859 gcc_assert (identifier_p (placeholder));
16860 }
16861 OMP_CLAUSE_DECL (nc)
16862 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16863 in_decl, NULL);
16864 break;
16865 case OMP_CLAUSE_GANG:
16866 case OMP_CLAUSE_ALIGNED:
16867 OMP_CLAUSE_DECL (nc)
16868 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16869 in_decl, NULL);
16870 OMP_CLAUSE_OPERAND (nc, 1)
16871 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16872 in_decl, /*integral_constant_expression_p=*/false);
16873 break;
16874 case OMP_CLAUSE_LINEAR:
16875 OMP_CLAUSE_DECL (nc)
16876 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16877 in_decl, NULL);
16878 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16879 {
16880 gcc_assert (!linear_no_step);
16881 linear_no_step = nc;
16882 }
16883 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16884 OMP_CLAUSE_LINEAR_STEP (nc)
16885 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16886 complain, in_decl, NULL);
16887 else
16888 OMP_CLAUSE_LINEAR_STEP (nc)
16889 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16890 in_decl,
16891 /*integral_constant_expression_p=*/false);
16892 break;
16893 case OMP_CLAUSE_NOWAIT:
16894 case OMP_CLAUSE_DEFAULT:
16895 case OMP_CLAUSE_UNTIED:
16896 case OMP_CLAUSE_MERGEABLE:
16897 case OMP_CLAUSE_INBRANCH:
16898 case OMP_CLAUSE_NOTINBRANCH:
16899 case OMP_CLAUSE_PROC_BIND:
16900 case OMP_CLAUSE_FOR:
16901 case OMP_CLAUSE_PARALLEL:
16902 case OMP_CLAUSE_SECTIONS:
16903 case OMP_CLAUSE_TASKGROUP:
16904 case OMP_CLAUSE_NOGROUP:
16905 case OMP_CLAUSE_THREADS:
16906 case OMP_CLAUSE_SIMD:
16907 case OMP_CLAUSE_DEFAULTMAP:
16908 case OMP_CLAUSE_ORDER:
16909 case OMP_CLAUSE_BIND:
16910 case OMP_CLAUSE_INDEPENDENT:
16911 case OMP_CLAUSE_AUTO:
16912 case OMP_CLAUSE_SEQ:
16913 case OMP_CLAUSE_IF_PRESENT:
16914 case OMP_CLAUSE_FINALIZE:
16915 break;
16916 default:
16917 gcc_unreachable ();
16918 }
16919 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16920 switch (OMP_CLAUSE_CODE (nc))
16921 {
16922 case OMP_CLAUSE_SHARED:
16923 case OMP_CLAUSE_PRIVATE:
16924 case OMP_CLAUSE_FIRSTPRIVATE:
16925 case OMP_CLAUSE_LASTPRIVATE:
16926 case OMP_CLAUSE_COPYPRIVATE:
16927 case OMP_CLAUSE_LINEAR:
16928 case OMP_CLAUSE_REDUCTION:
16929 case OMP_CLAUSE_IN_REDUCTION:
16930 case OMP_CLAUSE_TASK_REDUCTION:
16931 case OMP_CLAUSE_USE_DEVICE_PTR:
16932 case OMP_CLAUSE_USE_DEVICE_ADDR:
16933 case OMP_CLAUSE_IS_DEVICE_PTR:
16934 case OMP_CLAUSE_INCLUSIVE:
16935 case OMP_CLAUSE_EXCLUSIVE:
16936 /* tsubst_expr on SCOPE_REF results in returning
16937 finish_non_static_data_member result. Undo that here. */
16938 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16939 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16940 == IDENTIFIER_NODE))
16941 {
16942 tree t = OMP_CLAUSE_DECL (nc);
16943 tree v = t;
16944 while (v)
16945 switch (TREE_CODE (v))
16946 {
16947 case COMPONENT_REF:
16948 case MEM_REF:
16949 case INDIRECT_REF:
16950 CASE_CONVERT:
16951 case POINTER_PLUS_EXPR:
16952 v = TREE_OPERAND (v, 0);
16953 continue;
16954 case PARM_DECL:
16955 if (DECL_CONTEXT (v) == current_function_decl
16956 && DECL_ARTIFICIAL (v)
16957 && DECL_NAME (v) == this_identifier)
16958 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16959 /* FALLTHRU */
16960 default:
16961 v = NULL_TREE;
16962 break;
16963 }
16964 }
16965 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16966 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16967 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16968 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16969 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16970 {
16971 tree decl = OMP_CLAUSE_DECL (nc);
16972 if (VAR_P (decl))
16973 {
16974 retrofit_lang_decl (decl);
16975 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16976 }
16977 }
16978 break;
16979 default:
16980 break;
16981 }
16982 }
16983
16984 new_clauses = nreverse (new_clauses);
16985 if (ort != C_ORT_OMP_DECLARE_SIMD)
16986 {
16987 new_clauses = finish_omp_clauses (new_clauses, ort);
16988 if (linear_no_step)
16989 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16990 if (nc == linear_no_step)
16991 {
16992 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16993 break;
16994 }
16995 }
16996 return new_clauses;
16997 }
16998
16999 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17000
17001 static tree
17002 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17003 tree in_decl)
17004 {
17005 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17006
17007 tree purpose, value, chain;
17008
17009 if (t == NULL)
17010 return t;
17011
17012 if (TREE_CODE (t) != TREE_LIST)
17013 return tsubst_copy_and_build (t, args, complain, in_decl,
17014 /*function_p=*/false,
17015 /*integral_constant_expression_p=*/false);
17016
17017 if (t == void_list_node)
17018 return t;
17019
17020 purpose = TREE_PURPOSE (t);
17021 if (purpose)
17022 purpose = RECUR (purpose);
17023 value = TREE_VALUE (t);
17024 if (value)
17025 {
17026 if (TREE_CODE (value) != LABEL_DECL)
17027 value = RECUR (value);
17028 else
17029 {
17030 value = lookup_label (DECL_NAME (value));
17031 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17032 TREE_USED (value) = 1;
17033 }
17034 }
17035 chain = TREE_CHAIN (t);
17036 if (chain && chain != void_type_node)
17037 chain = RECUR (chain);
17038 return tree_cons (purpose, value, chain);
17039 #undef RECUR
17040 }
17041
17042 /* Used to temporarily communicate the list of #pragma omp parallel
17043 clauses to #pragma omp for instantiation if they are combined
17044 together. */
17045
17046 static tree *omp_parallel_combined_clauses;
17047
17048 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17049 tree *, unsigned int *);
17050
17051 /* Substitute one OMP_FOR iterator. */
17052
17053 static bool
17054 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17055 tree initv, tree condv, tree incrv, tree *clauses,
17056 tree args, tsubst_flags_t complain, tree in_decl,
17057 bool integral_constant_expression_p)
17058 {
17059 #define RECUR(NODE) \
17060 tsubst_expr ((NODE), args, complain, in_decl, \
17061 integral_constant_expression_p)
17062 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17063 bool ret = false;
17064
17065 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17066 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17067
17068 decl = TREE_OPERAND (init, 0);
17069 init = TREE_OPERAND (init, 1);
17070 tree decl_expr = NULL_TREE;
17071 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17072 if (range_for)
17073 {
17074 bool decomp = false;
17075 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17076 {
17077 tree v = DECL_VALUE_EXPR (decl);
17078 if (TREE_CODE (v) == ARRAY_REF
17079 && VAR_P (TREE_OPERAND (v, 0))
17080 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17081 {
17082 tree decomp_first = NULL_TREE;
17083 unsigned decomp_cnt = 0;
17084 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17085 maybe_push_decl (d);
17086 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17087 in_decl, &decomp_first, &decomp_cnt);
17088 decomp = true;
17089 if (d == error_mark_node)
17090 decl = error_mark_node;
17091 else
17092 for (unsigned int i = 0; i < decomp_cnt; i++)
17093 {
17094 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17095 {
17096 tree v = build_nt (ARRAY_REF, d,
17097 size_int (decomp_cnt - i - 1),
17098 NULL_TREE, NULL_TREE);
17099 SET_DECL_VALUE_EXPR (decomp_first, v);
17100 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17101 }
17102 fit_decomposition_lang_decl (decomp_first, d);
17103 decomp_first = DECL_CHAIN (decomp_first);
17104 }
17105 }
17106 }
17107 decl = tsubst_decl (decl, args, complain);
17108 if (!decomp)
17109 maybe_push_decl (decl);
17110 }
17111 else if (init && TREE_CODE (init) == DECL_EXPR)
17112 {
17113 /* We need to jump through some hoops to handle declarations in the
17114 init-statement, since we might need to handle auto deduction,
17115 but we need to keep control of initialization. */
17116 decl_expr = init;
17117 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17118 decl = tsubst_decl (decl, args, complain);
17119 }
17120 else
17121 {
17122 if (TREE_CODE (decl) == SCOPE_REF)
17123 {
17124 decl = RECUR (decl);
17125 if (TREE_CODE (decl) == COMPONENT_REF)
17126 {
17127 tree v = decl;
17128 while (v)
17129 switch (TREE_CODE (v))
17130 {
17131 case COMPONENT_REF:
17132 case MEM_REF:
17133 case INDIRECT_REF:
17134 CASE_CONVERT:
17135 case POINTER_PLUS_EXPR:
17136 v = TREE_OPERAND (v, 0);
17137 continue;
17138 case PARM_DECL:
17139 if (DECL_CONTEXT (v) == current_function_decl
17140 && DECL_ARTIFICIAL (v)
17141 && DECL_NAME (v) == this_identifier)
17142 {
17143 decl = TREE_OPERAND (decl, 1);
17144 decl = omp_privatize_field (decl, false);
17145 }
17146 /* FALLTHRU */
17147 default:
17148 v = NULL_TREE;
17149 break;
17150 }
17151 }
17152 }
17153 else
17154 decl = RECUR (decl);
17155 }
17156 init = RECUR (init);
17157
17158 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17159 {
17160 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17161 if (TREE_CODE (o) == TREE_LIST)
17162 TREE_VEC_ELT (orig_declv, i)
17163 = tree_cons (RECUR (TREE_PURPOSE (o)),
17164 RECUR (TREE_VALUE (o)),
17165 NULL_TREE);
17166 else
17167 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17168 }
17169
17170 if (range_for)
17171 {
17172 tree this_pre_body = NULL_TREE;
17173 tree orig_init = NULL_TREE;
17174 tree orig_decl = NULL_TREE;
17175 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17176 orig_init, cond, incr);
17177 if (orig_decl)
17178 {
17179 if (orig_declv == NULL_TREE)
17180 orig_declv = copy_node (declv);
17181 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17182 ret = true;
17183 }
17184 else if (orig_declv)
17185 TREE_VEC_ELT (orig_declv, i) = decl;
17186 }
17187
17188 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17189 if (!range_for && auto_node && init)
17190 TREE_TYPE (decl)
17191 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17192
17193 gcc_assert (!type_dependent_expression_p (decl));
17194
17195 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17196 {
17197 if (decl_expr)
17198 {
17199 /* Declare the variable, but don't let that initialize it. */
17200 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17201 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17202 RECUR (decl_expr);
17203 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17204 }
17205
17206 if (!range_for)
17207 {
17208 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17209 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17210 if (TREE_CODE (incr) == MODIFY_EXPR)
17211 {
17212 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17213 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17214 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17215 NOP_EXPR, rhs, complain);
17216 }
17217 else
17218 incr = RECUR (incr);
17219 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17220 TREE_VEC_ELT (orig_declv, i) = decl;
17221 }
17222 TREE_VEC_ELT (declv, i) = decl;
17223 TREE_VEC_ELT (initv, i) = init;
17224 TREE_VEC_ELT (condv, i) = cond;
17225 TREE_VEC_ELT (incrv, i) = incr;
17226 return ret;
17227 }
17228
17229 if (decl_expr)
17230 {
17231 /* Declare and initialize the variable. */
17232 RECUR (decl_expr);
17233 init = NULL_TREE;
17234 }
17235 else if (init)
17236 {
17237 tree *pc;
17238 int j;
17239 for (j = ((omp_parallel_combined_clauses == NULL
17240 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17241 {
17242 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17243 {
17244 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17245 && OMP_CLAUSE_DECL (*pc) == decl)
17246 break;
17247 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17248 && OMP_CLAUSE_DECL (*pc) == decl)
17249 {
17250 if (j)
17251 break;
17252 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17253 tree c = *pc;
17254 *pc = OMP_CLAUSE_CHAIN (c);
17255 OMP_CLAUSE_CHAIN (c) = *clauses;
17256 *clauses = c;
17257 }
17258 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17259 && OMP_CLAUSE_DECL (*pc) == decl)
17260 {
17261 error ("iteration variable %qD should not be firstprivate",
17262 decl);
17263 *pc = OMP_CLAUSE_CHAIN (*pc);
17264 }
17265 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17266 && OMP_CLAUSE_DECL (*pc) == decl)
17267 {
17268 error ("iteration variable %qD should not be reduction",
17269 decl);
17270 *pc = OMP_CLAUSE_CHAIN (*pc);
17271 }
17272 else
17273 pc = &OMP_CLAUSE_CHAIN (*pc);
17274 }
17275 if (*pc)
17276 break;
17277 }
17278 if (*pc == NULL_TREE)
17279 {
17280 tree c = build_omp_clause (input_location,
17281 TREE_CODE (t) == OMP_LOOP
17282 ? OMP_CLAUSE_LASTPRIVATE
17283 : OMP_CLAUSE_PRIVATE);
17284 OMP_CLAUSE_DECL (c) = decl;
17285 c = finish_omp_clauses (c, C_ORT_OMP);
17286 if (c)
17287 {
17288 OMP_CLAUSE_CHAIN (c) = *clauses;
17289 *clauses = c;
17290 }
17291 }
17292 }
17293 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17294 if (COMPARISON_CLASS_P (cond))
17295 {
17296 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17297 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17298 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17299 }
17300 else
17301 cond = RECUR (cond);
17302 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17303 switch (TREE_CODE (incr))
17304 {
17305 case PREINCREMENT_EXPR:
17306 case PREDECREMENT_EXPR:
17307 case POSTINCREMENT_EXPR:
17308 case POSTDECREMENT_EXPR:
17309 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17310 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17311 break;
17312 case MODIFY_EXPR:
17313 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17314 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17315 {
17316 tree rhs = TREE_OPERAND (incr, 1);
17317 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17318 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17319 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17320 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17321 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17322 rhs0, rhs1));
17323 }
17324 else
17325 incr = RECUR (incr);
17326 break;
17327 case MODOP_EXPR:
17328 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17329 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17330 {
17331 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17332 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17333 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17334 TREE_TYPE (decl), lhs,
17335 RECUR (TREE_OPERAND (incr, 2))));
17336 }
17337 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17338 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17339 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17340 {
17341 tree rhs = TREE_OPERAND (incr, 2);
17342 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17343 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17344 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17345 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17346 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17347 rhs0, rhs1));
17348 }
17349 else
17350 incr = RECUR (incr);
17351 break;
17352 default:
17353 incr = RECUR (incr);
17354 break;
17355 }
17356
17357 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17358 TREE_VEC_ELT (orig_declv, i) = decl;
17359 TREE_VEC_ELT (declv, i) = decl;
17360 TREE_VEC_ELT (initv, i) = init;
17361 TREE_VEC_ELT (condv, i) = cond;
17362 TREE_VEC_ELT (incrv, i) = incr;
17363 return false;
17364 #undef RECUR
17365 }
17366
17367 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17368 of OMP_TARGET's body. */
17369
17370 static tree
17371 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17372 {
17373 *walk_subtrees = 0;
17374 switch (TREE_CODE (*tp))
17375 {
17376 case OMP_TEAMS:
17377 return *tp;
17378 case BIND_EXPR:
17379 case STATEMENT_LIST:
17380 *walk_subtrees = 1;
17381 break;
17382 default:
17383 break;
17384 }
17385 return NULL_TREE;
17386 }
17387
17388 /* Helper function for tsubst_expr. For decomposition declaration
17389 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17390 also the corresponding decls representing the identifiers
17391 of the decomposition declaration. Return DECL if successful
17392 or error_mark_node otherwise, set *FIRST to the first decl
17393 in the list chained through DECL_CHAIN and *CNT to the number
17394 of such decls. */
17395
17396 static tree
17397 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17398 tsubst_flags_t complain, tree in_decl, tree *first,
17399 unsigned int *cnt)
17400 {
17401 tree decl2, decl3, prev = decl;
17402 *cnt = 0;
17403 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17404 for (decl2 = DECL_CHAIN (pattern_decl);
17405 decl2
17406 && VAR_P (decl2)
17407 && DECL_DECOMPOSITION_P (decl2)
17408 && DECL_NAME (decl2);
17409 decl2 = DECL_CHAIN (decl2))
17410 {
17411 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17412 {
17413 gcc_assert (errorcount);
17414 return error_mark_node;
17415 }
17416 (*cnt)++;
17417 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17418 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17419 tree v = DECL_VALUE_EXPR (decl2);
17420 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17421 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17422 decl3 = tsubst (decl2, args, complain, in_decl);
17423 SET_DECL_VALUE_EXPR (decl2, v);
17424 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17425 if (VAR_P (decl3))
17426 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17427 else
17428 {
17429 gcc_assert (errorcount);
17430 decl = error_mark_node;
17431 continue;
17432 }
17433 maybe_push_decl (decl3);
17434 if (error_operand_p (decl3))
17435 decl = error_mark_node;
17436 else if (decl != error_mark_node
17437 && DECL_CHAIN (decl3) != prev
17438 && decl != prev)
17439 {
17440 gcc_assert (errorcount);
17441 decl = error_mark_node;
17442 }
17443 else
17444 prev = decl3;
17445 }
17446 *first = prev;
17447 return decl;
17448 }
17449
17450 /* Return the proper local_specialization for init-capture pack DECL. */
17451
17452 static tree
17453 lookup_init_capture_pack (tree decl)
17454 {
17455 /* We handle normal pack captures by forwarding to the specialization of the
17456 captured parameter. We can't do that for pack init-captures; we need them
17457 to have their own local_specialization. We created the individual
17458 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17459 when we process the DECL_EXPR for the pack init-capture in the template.
17460 So, how do we find them? We don't know the capture proxy pack when
17461 building the individual resulting proxies, and we don't know the
17462 individual proxies when instantiating the pack. What we have in common is
17463 the FIELD_DECL.
17464
17465 So...when we instantiate the FIELD_DECL, we stick the result in
17466 local_specializations. Then at the DECL_EXPR we look up that result, see
17467 how many elements it has, synthesize the names, and look them up. */
17468
17469 tree cname = DECL_NAME (decl);
17470 tree val = DECL_VALUE_EXPR (decl);
17471 tree field = TREE_OPERAND (val, 1);
17472 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17473 tree fpack = retrieve_local_specialization (field);
17474 if (fpack == error_mark_node)
17475 return error_mark_node;
17476
17477 int len = 1;
17478 tree vec = NULL_TREE;
17479 tree r = NULL_TREE;
17480 if (TREE_CODE (fpack) == TREE_VEC)
17481 {
17482 len = TREE_VEC_LENGTH (fpack);
17483 vec = make_tree_vec (len);
17484 r = make_node (NONTYPE_ARGUMENT_PACK);
17485 SET_ARGUMENT_PACK_ARGS (r, vec);
17486 }
17487 for (int i = 0; i < len; ++i)
17488 {
17489 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17490 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17491 if (vec)
17492 TREE_VEC_ELT (vec, i) = elt;
17493 else
17494 r = elt;
17495 }
17496 return r;
17497 }
17498
17499 /* Like tsubst_copy for expressions, etc. but also does semantic
17500 processing. */
17501
17502 tree
17503 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17504 bool integral_constant_expression_p)
17505 {
17506 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17507 #define RECUR(NODE) \
17508 tsubst_expr ((NODE), args, complain, in_decl, \
17509 integral_constant_expression_p)
17510
17511 tree stmt, tmp;
17512 tree r;
17513 location_t loc;
17514
17515 if (t == NULL_TREE || t == error_mark_node)
17516 return t;
17517
17518 loc = input_location;
17519 if (location_t eloc = cp_expr_location (t))
17520 input_location = eloc;
17521 if (STATEMENT_CODE_P (TREE_CODE (t)))
17522 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17523
17524 switch (TREE_CODE (t))
17525 {
17526 case STATEMENT_LIST:
17527 {
17528 tree_stmt_iterator i;
17529 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17530 RECUR (tsi_stmt (i));
17531 break;
17532 }
17533
17534 case CTOR_INITIALIZER:
17535 finish_mem_initializers (tsubst_initializer_list
17536 (TREE_OPERAND (t, 0), args));
17537 break;
17538
17539 case RETURN_EXPR:
17540 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17541 break;
17542
17543 case EXPR_STMT:
17544 tmp = RECUR (EXPR_STMT_EXPR (t));
17545 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17546 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17547 else
17548 finish_expr_stmt (tmp);
17549 break;
17550
17551 case USING_STMT:
17552 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17553 break;
17554
17555 case DECL_EXPR:
17556 {
17557 tree decl, pattern_decl;
17558 tree init;
17559
17560 pattern_decl = decl = DECL_EXPR_DECL (t);
17561 if (TREE_CODE (decl) == LABEL_DECL)
17562 finish_label_decl (DECL_NAME (decl));
17563 else if (TREE_CODE (decl) == USING_DECL)
17564 {
17565 tree scope = USING_DECL_SCOPE (decl);
17566 tree name = DECL_NAME (decl);
17567
17568 scope = tsubst (scope, args, complain, in_decl);
17569 finish_nonmember_using_decl (scope, name);
17570 }
17571 else if (is_capture_proxy (decl)
17572 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17573 {
17574 /* We're in tsubst_lambda_expr, we've already inserted a new
17575 capture proxy, so look it up and register it. */
17576 tree inst;
17577 if (!DECL_PACK_P (decl))
17578 {
17579 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17580 /*nonclass*/1, /*block_p=*/true,
17581 /*ns_only*/0, LOOKUP_HIDDEN);
17582 gcc_assert (inst != decl && is_capture_proxy (inst));
17583 }
17584 else if (is_normal_capture_proxy (decl))
17585 {
17586 inst = (retrieve_local_specialization
17587 (DECL_CAPTURED_VARIABLE (decl)));
17588 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17589 }
17590 else
17591 inst = lookup_init_capture_pack (decl);
17592
17593 register_local_specialization (inst, decl);
17594 break;
17595 }
17596 else if (DECL_PRETTY_FUNCTION_P (decl))
17597 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17598 DECL_NAME (decl),
17599 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17600 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17601 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17602 /* Don't copy the old closure; we'll create a new one in
17603 tsubst_lambda_expr. */
17604 break;
17605 else
17606 {
17607 init = DECL_INITIAL (decl);
17608 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17609 for local variables, so save if DECL was declared constinit. */
17610 const bool constinit_p
17611 = (VAR_P (decl)
17612 && DECL_LANG_SPECIFIC (decl)
17613 && DECL_TEMPLATE_INFO (decl)
17614 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17615 decl = tsubst (decl, args, complain, in_decl);
17616 if (decl != error_mark_node)
17617 {
17618 /* By marking the declaration as instantiated, we avoid
17619 trying to instantiate it. Since instantiate_decl can't
17620 handle local variables, and since we've already done
17621 all that needs to be done, that's the right thing to
17622 do. */
17623 if (VAR_P (decl))
17624 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17625 if (VAR_P (decl) && !DECL_NAME (decl)
17626 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17627 /* Anonymous aggregates are a special case. */
17628 finish_anon_union (decl);
17629 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17630 {
17631 DECL_CONTEXT (decl) = current_function_decl;
17632 if (DECL_NAME (decl) == this_identifier)
17633 {
17634 tree lam = DECL_CONTEXT (current_function_decl);
17635 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17636 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17637 }
17638 insert_capture_proxy (decl);
17639 }
17640 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17641 /* We already did a pushtag. */;
17642 else if (TREE_CODE (decl) == FUNCTION_DECL
17643 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17644 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17645 {
17646 DECL_CONTEXT (decl) = NULL_TREE;
17647 pushdecl (decl);
17648 DECL_CONTEXT (decl) = current_function_decl;
17649 cp_check_omp_declare_reduction (decl);
17650 }
17651 else
17652 {
17653 bool const_init = false;
17654 unsigned int cnt = 0;
17655 tree first = NULL_TREE, ndecl = error_mark_node;
17656 maybe_push_decl (decl);
17657
17658 if (VAR_P (decl)
17659 && DECL_DECOMPOSITION_P (decl)
17660 && TREE_TYPE (pattern_decl) != error_mark_node)
17661 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17662 complain, in_decl, &first,
17663 &cnt);
17664
17665 init = tsubst_init (init, decl, args, complain, in_decl);
17666
17667 if (VAR_P (decl))
17668 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17669 (pattern_decl));
17670
17671 if (ndecl != error_mark_node)
17672 cp_maybe_mangle_decomp (ndecl, first, cnt);
17673
17674 cp_finish_decl (decl, init, const_init, NULL_TREE,
17675 constinit_p ? LOOKUP_CONSTINIT : 0);
17676
17677 if (ndecl != error_mark_node)
17678 cp_finish_decomp (ndecl, first, cnt);
17679 }
17680 }
17681 }
17682
17683 break;
17684 }
17685
17686 case FOR_STMT:
17687 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17688 RECUR (FOR_INIT_STMT (t));
17689 finish_init_stmt (stmt);
17690 tmp = RECUR (FOR_COND (t));
17691 finish_for_cond (tmp, stmt, false, 0);
17692 tmp = RECUR (FOR_EXPR (t));
17693 finish_for_expr (tmp, stmt);
17694 {
17695 bool prev = note_iteration_stmt_body_start ();
17696 RECUR (FOR_BODY (t));
17697 note_iteration_stmt_body_end (prev);
17698 }
17699 finish_for_stmt (stmt);
17700 break;
17701
17702 case RANGE_FOR_STMT:
17703 {
17704 /* Construct another range_for, if this is not a final
17705 substitution (for inside inside a generic lambda of a
17706 template). Otherwise convert to a regular for. */
17707 tree decl, expr;
17708 stmt = (processing_template_decl
17709 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17710 : begin_for_stmt (NULL_TREE, NULL_TREE));
17711 RECUR (RANGE_FOR_INIT_STMT (t));
17712 decl = RANGE_FOR_DECL (t);
17713 decl = tsubst (decl, args, complain, in_decl);
17714 maybe_push_decl (decl);
17715 expr = RECUR (RANGE_FOR_EXPR (t));
17716
17717 tree decomp_first = NULL_TREE;
17718 unsigned decomp_cnt = 0;
17719 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17720 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17721 complain, in_decl,
17722 &decomp_first, &decomp_cnt);
17723
17724 if (processing_template_decl)
17725 {
17726 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17727 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17728 finish_range_for_decl (stmt, decl, expr);
17729 if (decomp_first && decl != error_mark_node)
17730 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17731 }
17732 else
17733 {
17734 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17735 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17736 stmt = cp_convert_range_for (stmt, decl, expr,
17737 decomp_first, decomp_cnt,
17738 RANGE_FOR_IVDEP (t), unroll);
17739 }
17740
17741 bool prev = note_iteration_stmt_body_start ();
17742 RECUR (RANGE_FOR_BODY (t));
17743 note_iteration_stmt_body_end (prev);
17744 finish_for_stmt (stmt);
17745 }
17746 break;
17747
17748 case WHILE_STMT:
17749 stmt = begin_while_stmt ();
17750 tmp = RECUR (WHILE_COND (t));
17751 finish_while_stmt_cond (tmp, stmt, false, 0);
17752 {
17753 bool prev = note_iteration_stmt_body_start ();
17754 RECUR (WHILE_BODY (t));
17755 note_iteration_stmt_body_end (prev);
17756 }
17757 finish_while_stmt (stmt);
17758 break;
17759
17760 case DO_STMT:
17761 stmt = begin_do_stmt ();
17762 {
17763 bool prev = note_iteration_stmt_body_start ();
17764 RECUR (DO_BODY (t));
17765 note_iteration_stmt_body_end (prev);
17766 }
17767 finish_do_body (stmt);
17768 tmp = RECUR (DO_COND (t));
17769 finish_do_stmt (tmp, stmt, false, 0);
17770 break;
17771
17772 case IF_STMT:
17773 stmt = begin_if_stmt ();
17774 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17775 if (IF_STMT_CONSTEXPR_P (t))
17776 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17777 tmp = RECUR (IF_COND (t));
17778 tmp = finish_if_stmt_cond (tmp, stmt);
17779 if (IF_STMT_CONSTEXPR_P (t)
17780 && instantiation_dependent_expression_p (tmp))
17781 {
17782 /* We're partially instantiating a generic lambda, but the condition
17783 of the constexpr if is still dependent. Don't substitute into the
17784 branches now, just remember the template arguments. */
17785 do_poplevel (IF_SCOPE (stmt));
17786 IF_COND (stmt) = IF_COND (t);
17787 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17788 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17789 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17790 add_stmt (stmt);
17791 break;
17792 }
17793 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17794 /* Don't instantiate the THEN_CLAUSE. */;
17795 else
17796 {
17797 tree folded = fold_non_dependent_expr (tmp, complain);
17798 bool inhibit = integer_zerop (folded);
17799 if (inhibit)
17800 ++c_inhibit_evaluation_warnings;
17801 RECUR (THEN_CLAUSE (t));
17802 if (inhibit)
17803 --c_inhibit_evaluation_warnings;
17804 }
17805 finish_then_clause (stmt);
17806
17807 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17808 /* Don't instantiate the ELSE_CLAUSE. */;
17809 else if (ELSE_CLAUSE (t))
17810 {
17811 tree folded = fold_non_dependent_expr (tmp, complain);
17812 bool inhibit = integer_nonzerop (folded);
17813 begin_else_clause (stmt);
17814 if (inhibit)
17815 ++c_inhibit_evaluation_warnings;
17816 RECUR (ELSE_CLAUSE (t));
17817 if (inhibit)
17818 --c_inhibit_evaluation_warnings;
17819 finish_else_clause (stmt);
17820 }
17821
17822 finish_if_stmt (stmt);
17823 break;
17824
17825 case BIND_EXPR:
17826 if (BIND_EXPR_BODY_BLOCK (t))
17827 stmt = begin_function_body ();
17828 else
17829 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17830 ? BCS_TRY_BLOCK : 0);
17831
17832 RECUR (BIND_EXPR_BODY (t));
17833
17834 if (BIND_EXPR_BODY_BLOCK (t))
17835 finish_function_body (stmt);
17836 else
17837 finish_compound_stmt (stmt);
17838 break;
17839
17840 case BREAK_STMT:
17841 finish_break_stmt ();
17842 break;
17843
17844 case CONTINUE_STMT:
17845 finish_continue_stmt ();
17846 break;
17847
17848 case SWITCH_STMT:
17849 stmt = begin_switch_stmt ();
17850 tmp = RECUR (SWITCH_STMT_COND (t));
17851 finish_switch_cond (tmp, stmt);
17852 RECUR (SWITCH_STMT_BODY (t));
17853 finish_switch_stmt (stmt);
17854 break;
17855
17856 case CASE_LABEL_EXPR:
17857 {
17858 tree decl = CASE_LABEL (t);
17859 tree low = RECUR (CASE_LOW (t));
17860 tree high = RECUR (CASE_HIGH (t));
17861 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17862 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17863 {
17864 tree label = CASE_LABEL (l);
17865 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17866 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17867 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17868 }
17869 }
17870 break;
17871
17872 case LABEL_EXPR:
17873 {
17874 tree decl = LABEL_EXPR_LABEL (t);
17875 tree label;
17876
17877 label = finish_label_stmt (DECL_NAME (decl));
17878 if (TREE_CODE (label) == LABEL_DECL)
17879 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17880 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17881 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17882 }
17883 break;
17884
17885 case GOTO_EXPR:
17886 tmp = GOTO_DESTINATION (t);
17887 if (TREE_CODE (tmp) != LABEL_DECL)
17888 /* Computed goto's must be tsubst'd into. On the other hand,
17889 non-computed gotos must not be; the identifier in question
17890 will have no binding. */
17891 tmp = RECUR (tmp);
17892 else
17893 tmp = DECL_NAME (tmp);
17894 finish_goto_stmt (tmp);
17895 break;
17896
17897 case ASM_EXPR:
17898 {
17899 tree string = RECUR (ASM_STRING (t));
17900 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17901 complain, in_decl);
17902 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17903 complain, in_decl);
17904 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17905 complain, in_decl);
17906 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17907 complain, in_decl);
17908 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17909 outputs, inputs, clobbers, labels,
17910 ASM_INLINE_P (t));
17911 tree asm_expr = tmp;
17912 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17913 asm_expr = TREE_OPERAND (asm_expr, 0);
17914 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17915 }
17916 break;
17917
17918 case TRY_BLOCK:
17919 if (CLEANUP_P (t))
17920 {
17921 stmt = begin_try_block ();
17922 RECUR (TRY_STMTS (t));
17923 finish_cleanup_try_block (stmt);
17924 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17925 }
17926 else
17927 {
17928 tree compound_stmt = NULL_TREE;
17929
17930 if (FN_TRY_BLOCK_P (t))
17931 stmt = begin_function_try_block (&compound_stmt);
17932 else
17933 stmt = begin_try_block ();
17934
17935 RECUR (TRY_STMTS (t));
17936
17937 if (FN_TRY_BLOCK_P (t))
17938 finish_function_try_block (stmt);
17939 else
17940 finish_try_block (stmt);
17941
17942 RECUR (TRY_HANDLERS (t));
17943 if (FN_TRY_BLOCK_P (t))
17944 finish_function_handler_sequence (stmt, compound_stmt);
17945 else
17946 finish_handler_sequence (stmt);
17947 }
17948 break;
17949
17950 case HANDLER:
17951 {
17952 tree decl = HANDLER_PARMS (t);
17953
17954 if (decl)
17955 {
17956 decl = tsubst (decl, args, complain, in_decl);
17957 /* Prevent instantiate_decl from trying to instantiate
17958 this variable. We've already done all that needs to be
17959 done. */
17960 if (decl != error_mark_node)
17961 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17962 }
17963 stmt = begin_handler ();
17964 finish_handler_parms (decl, stmt);
17965 RECUR (HANDLER_BODY (t));
17966 finish_handler (stmt);
17967 }
17968 break;
17969
17970 case TAG_DEFN:
17971 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17972 if (CLASS_TYPE_P (tmp))
17973 {
17974 /* Local classes are not independent templates; they are
17975 instantiated along with their containing function. And this
17976 way we don't have to deal with pushing out of one local class
17977 to instantiate a member of another local class. */
17978 /* Closures are handled by the LAMBDA_EXPR. */
17979 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17980 complete_type (tmp);
17981 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17982 if ((VAR_P (fld)
17983 || (TREE_CODE (fld) == FUNCTION_DECL
17984 && !DECL_ARTIFICIAL (fld)))
17985 && DECL_TEMPLATE_INSTANTIATION (fld))
17986 instantiate_decl (fld, /*defer_ok=*/false,
17987 /*expl_inst_class=*/false);
17988 }
17989 break;
17990
17991 case STATIC_ASSERT:
17992 {
17993 tree condition;
17994
17995 ++c_inhibit_evaluation_warnings;
17996 condition =
17997 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17998 args,
17999 complain, in_decl,
18000 /*integral_constant_expression_p=*/true);
18001 --c_inhibit_evaluation_warnings;
18002
18003 finish_static_assert (condition,
18004 STATIC_ASSERT_MESSAGE (t),
18005 STATIC_ASSERT_SOURCE_LOCATION (t),
18006 /*member_p=*/false);
18007 }
18008 break;
18009
18010 case OACC_KERNELS:
18011 case OACC_PARALLEL:
18012 case OACC_SERIAL:
18013 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18014 in_decl);
18015 stmt = begin_omp_parallel ();
18016 RECUR (OMP_BODY (t));
18017 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18018 break;
18019
18020 case OMP_PARALLEL:
18021 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18022 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18023 complain, in_decl);
18024 if (OMP_PARALLEL_COMBINED (t))
18025 omp_parallel_combined_clauses = &tmp;
18026 stmt = begin_omp_parallel ();
18027 RECUR (OMP_PARALLEL_BODY (t));
18028 gcc_assert (omp_parallel_combined_clauses == NULL);
18029 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18030 = OMP_PARALLEL_COMBINED (t);
18031 pop_omp_privatization_clauses (r);
18032 break;
18033
18034 case OMP_TASK:
18035 if (OMP_TASK_BODY (t) == NULL_TREE)
18036 {
18037 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18038 complain, in_decl);
18039 t = copy_node (t);
18040 OMP_TASK_CLAUSES (t) = tmp;
18041 add_stmt (t);
18042 break;
18043 }
18044 r = push_omp_privatization_clauses (false);
18045 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18046 complain, in_decl);
18047 stmt = begin_omp_task ();
18048 RECUR (OMP_TASK_BODY (t));
18049 finish_omp_task (tmp, stmt);
18050 pop_omp_privatization_clauses (r);
18051 break;
18052
18053 case OMP_FOR:
18054 case OMP_LOOP:
18055 case OMP_SIMD:
18056 case OMP_DISTRIBUTE:
18057 case OMP_TASKLOOP:
18058 case OACC_LOOP:
18059 {
18060 tree clauses, body, pre_body;
18061 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18062 tree orig_declv = NULL_TREE;
18063 tree incrv = NULL_TREE;
18064 enum c_omp_region_type ort = C_ORT_OMP;
18065 bool any_range_for = false;
18066 int i;
18067
18068 if (TREE_CODE (t) == OACC_LOOP)
18069 ort = C_ORT_ACC;
18070
18071 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18072 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18073 in_decl);
18074 if (OMP_FOR_INIT (t) != NULL_TREE)
18075 {
18076 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18077 if (OMP_FOR_ORIG_DECLS (t))
18078 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18079 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18080 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18081 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18082 }
18083
18084 keep_next_level (true);
18085 stmt = begin_omp_structured_block ();
18086
18087 pre_body = push_stmt_list ();
18088 RECUR (OMP_FOR_PRE_BODY (t));
18089 pre_body = pop_stmt_list (pre_body);
18090
18091 if (OMP_FOR_INIT (t) != NULL_TREE)
18092 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18093 any_range_for
18094 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18095 condv, incrv, &clauses, args,
18096 complain, in_decl,
18097 integral_constant_expression_p);
18098 omp_parallel_combined_clauses = NULL;
18099
18100 if (any_range_for)
18101 {
18102 gcc_assert (orig_declv);
18103 body = begin_omp_structured_block ();
18104 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18105 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18106 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18107 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18108 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18109 TREE_VEC_ELT (declv, i));
18110 }
18111 else
18112 body = push_stmt_list ();
18113 RECUR (OMP_FOR_BODY (t));
18114 if (any_range_for)
18115 body = finish_omp_structured_block (body);
18116 else
18117 body = pop_stmt_list (body);
18118
18119 if (OMP_FOR_INIT (t) != NULL_TREE)
18120 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18121 orig_declv, initv, condv, incrv, body, pre_body,
18122 NULL, clauses);
18123 else
18124 {
18125 t = make_node (TREE_CODE (t));
18126 TREE_TYPE (t) = void_type_node;
18127 OMP_FOR_BODY (t) = body;
18128 OMP_FOR_PRE_BODY (t) = pre_body;
18129 OMP_FOR_CLAUSES (t) = clauses;
18130 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18131 add_stmt (t);
18132 }
18133
18134 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18135 t));
18136 pop_omp_privatization_clauses (r);
18137 }
18138 break;
18139
18140 case OMP_SECTIONS:
18141 omp_parallel_combined_clauses = NULL;
18142 /* FALLTHRU */
18143 case OMP_SINGLE:
18144 case OMP_TEAMS:
18145 case OMP_CRITICAL:
18146 case OMP_TASKGROUP:
18147 case OMP_SCAN:
18148 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18149 && OMP_TEAMS_COMBINED (t));
18150 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18151 in_decl);
18152 if (TREE_CODE (t) == OMP_TEAMS)
18153 {
18154 keep_next_level (true);
18155 stmt = begin_omp_structured_block ();
18156 RECUR (OMP_BODY (t));
18157 stmt = finish_omp_structured_block (stmt);
18158 }
18159 else
18160 {
18161 stmt = push_stmt_list ();
18162 RECUR (OMP_BODY (t));
18163 stmt = pop_stmt_list (stmt);
18164 }
18165
18166 t = copy_node (t);
18167 OMP_BODY (t) = stmt;
18168 OMP_CLAUSES (t) = tmp;
18169 add_stmt (t);
18170 pop_omp_privatization_clauses (r);
18171 break;
18172
18173 case OMP_DEPOBJ:
18174 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18175 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18176 {
18177 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18178 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18179 {
18180 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18181 args, complain, in_decl);
18182 if (tmp == NULL_TREE)
18183 tmp = error_mark_node;
18184 }
18185 else
18186 {
18187 kind = (enum omp_clause_depend_kind)
18188 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18189 tmp = NULL_TREE;
18190 }
18191 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18192 }
18193 else
18194 finish_omp_depobj (EXPR_LOCATION (t), r,
18195 OMP_CLAUSE_DEPEND_SOURCE,
18196 OMP_DEPOBJ_CLAUSES (t));
18197 break;
18198
18199 case OACC_DATA:
18200 case OMP_TARGET_DATA:
18201 case OMP_TARGET:
18202 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18203 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18204 in_decl);
18205 keep_next_level (true);
18206 stmt = begin_omp_structured_block ();
18207
18208 RECUR (OMP_BODY (t));
18209 stmt = finish_omp_structured_block (stmt);
18210
18211 t = copy_node (t);
18212 OMP_BODY (t) = stmt;
18213 OMP_CLAUSES (t) = tmp;
18214 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18215 {
18216 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18217 if (teams)
18218 {
18219 /* For combined target teams, ensure the num_teams and
18220 thread_limit clause expressions are evaluated on the host,
18221 before entering the target construct. */
18222 tree c;
18223 for (c = OMP_TEAMS_CLAUSES (teams);
18224 c; c = OMP_CLAUSE_CHAIN (c))
18225 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18226 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18227 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18228 {
18229 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18230 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18231 if (expr == error_mark_node)
18232 continue;
18233 tmp = TARGET_EXPR_SLOT (expr);
18234 add_stmt (expr);
18235 OMP_CLAUSE_OPERAND (c, 0) = expr;
18236 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18237 OMP_CLAUSE_FIRSTPRIVATE);
18238 OMP_CLAUSE_DECL (tc) = tmp;
18239 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18240 OMP_TARGET_CLAUSES (t) = tc;
18241 }
18242 }
18243 }
18244 add_stmt (t);
18245 break;
18246
18247 case OACC_DECLARE:
18248 t = copy_node (t);
18249 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18250 complain, in_decl);
18251 OACC_DECLARE_CLAUSES (t) = tmp;
18252 add_stmt (t);
18253 break;
18254
18255 case OMP_TARGET_UPDATE:
18256 case OMP_TARGET_ENTER_DATA:
18257 case OMP_TARGET_EXIT_DATA:
18258 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18259 complain, in_decl);
18260 t = copy_node (t);
18261 OMP_STANDALONE_CLAUSES (t) = tmp;
18262 add_stmt (t);
18263 break;
18264
18265 case OACC_ENTER_DATA:
18266 case OACC_EXIT_DATA:
18267 case OACC_UPDATE:
18268 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18269 complain, in_decl);
18270 t = copy_node (t);
18271 OMP_STANDALONE_CLAUSES (t) = tmp;
18272 add_stmt (t);
18273 break;
18274
18275 case OMP_ORDERED:
18276 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18277 complain, in_decl);
18278 stmt = push_stmt_list ();
18279 RECUR (OMP_BODY (t));
18280 stmt = pop_stmt_list (stmt);
18281
18282 t = copy_node (t);
18283 OMP_BODY (t) = stmt;
18284 OMP_ORDERED_CLAUSES (t) = tmp;
18285 add_stmt (t);
18286 break;
18287
18288 case OMP_SECTION:
18289 case OMP_MASTER:
18290 stmt = push_stmt_list ();
18291 RECUR (OMP_BODY (t));
18292 stmt = pop_stmt_list (stmt);
18293
18294 t = copy_node (t);
18295 OMP_BODY (t) = stmt;
18296 add_stmt (t);
18297 break;
18298
18299 case OMP_ATOMIC:
18300 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18301 tmp = NULL_TREE;
18302 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18303 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18304 complain, in_decl);
18305 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18306 {
18307 tree op1 = TREE_OPERAND (t, 1);
18308 tree rhs1 = NULL_TREE;
18309 tree lhs, rhs;
18310 if (TREE_CODE (op1) == COMPOUND_EXPR)
18311 {
18312 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18313 op1 = TREE_OPERAND (op1, 1);
18314 }
18315 lhs = RECUR (TREE_OPERAND (op1, 0));
18316 rhs = RECUR (TREE_OPERAND (op1, 1));
18317 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18318 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18319 OMP_ATOMIC_MEMORY_ORDER (t));
18320 }
18321 else
18322 {
18323 tree op1 = TREE_OPERAND (t, 1);
18324 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18325 tree rhs1 = NULL_TREE;
18326 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18327 enum tree_code opcode = NOP_EXPR;
18328 if (code == OMP_ATOMIC_READ)
18329 {
18330 v = RECUR (TREE_OPERAND (op1, 0));
18331 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18332 }
18333 else if (code == OMP_ATOMIC_CAPTURE_OLD
18334 || code == OMP_ATOMIC_CAPTURE_NEW)
18335 {
18336 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18337 v = RECUR (TREE_OPERAND (op1, 0));
18338 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18339 if (TREE_CODE (op11) == COMPOUND_EXPR)
18340 {
18341 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18342 op11 = TREE_OPERAND (op11, 1);
18343 }
18344 lhs = RECUR (TREE_OPERAND (op11, 0));
18345 rhs = RECUR (TREE_OPERAND (op11, 1));
18346 opcode = TREE_CODE (op11);
18347 if (opcode == MODIFY_EXPR)
18348 opcode = NOP_EXPR;
18349 }
18350 else
18351 {
18352 code = OMP_ATOMIC;
18353 lhs = RECUR (TREE_OPERAND (op1, 0));
18354 rhs = RECUR (TREE_OPERAND (op1, 1));
18355 }
18356 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18357 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18358 }
18359 break;
18360
18361 case TRANSACTION_EXPR:
18362 {
18363 int flags = 0;
18364 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18365 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18366
18367 if (TRANSACTION_EXPR_IS_STMT (t))
18368 {
18369 tree body = TRANSACTION_EXPR_BODY (t);
18370 tree noex = NULL_TREE;
18371 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18372 {
18373 noex = MUST_NOT_THROW_COND (body);
18374 if (noex == NULL_TREE)
18375 noex = boolean_true_node;
18376 body = TREE_OPERAND (body, 0);
18377 }
18378 stmt = begin_transaction_stmt (input_location, NULL, flags);
18379 RECUR (body);
18380 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18381 }
18382 else
18383 {
18384 stmt = build_transaction_expr (EXPR_LOCATION (t),
18385 RECUR (TRANSACTION_EXPR_BODY (t)),
18386 flags, NULL_TREE);
18387 RETURN (stmt);
18388 }
18389 }
18390 break;
18391
18392 case MUST_NOT_THROW_EXPR:
18393 {
18394 tree op0 = RECUR (TREE_OPERAND (t, 0));
18395 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18396 RETURN (build_must_not_throw_expr (op0, cond));
18397 }
18398
18399 case EXPR_PACK_EXPANSION:
18400 error ("invalid use of pack expansion expression");
18401 RETURN (error_mark_node);
18402
18403 case NONTYPE_ARGUMENT_PACK:
18404 error ("use %<...%> to expand argument pack");
18405 RETURN (error_mark_node);
18406
18407 case COMPOUND_EXPR:
18408 tmp = RECUR (TREE_OPERAND (t, 0));
18409 if (tmp == NULL_TREE)
18410 /* If the first operand was a statement, we're done with it. */
18411 RETURN (RECUR (TREE_OPERAND (t, 1)));
18412 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18413 RECUR (TREE_OPERAND (t, 1)),
18414 complain));
18415
18416 case ANNOTATE_EXPR:
18417 tmp = RECUR (TREE_OPERAND (t, 0));
18418 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18419 TREE_TYPE (tmp), tmp,
18420 RECUR (TREE_OPERAND (t, 1)),
18421 RECUR (TREE_OPERAND (t, 2))));
18422
18423 case PREDICT_EXPR:
18424 RETURN (add_stmt (copy_node (t)));
18425
18426 default:
18427 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18428
18429 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18430 /*function_p=*/false,
18431 integral_constant_expression_p));
18432 }
18433
18434 RETURN (NULL_TREE);
18435 out:
18436 input_location = loc;
18437 return r;
18438 #undef RECUR
18439 #undef RETURN
18440 }
18441
18442 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18443 function. For description of the body see comment above
18444 cp_parser_omp_declare_reduction_exprs. */
18445
18446 static void
18447 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18448 {
18449 if (t == NULL_TREE || t == error_mark_node)
18450 return;
18451
18452 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18453
18454 tree_stmt_iterator tsi;
18455 int i;
18456 tree stmts[7];
18457 memset (stmts, 0, sizeof stmts);
18458 for (i = 0, tsi = tsi_start (t);
18459 i < 7 && !tsi_end_p (tsi);
18460 i++, tsi_next (&tsi))
18461 stmts[i] = tsi_stmt (tsi);
18462 gcc_assert (tsi_end_p (tsi));
18463
18464 if (i >= 3)
18465 {
18466 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18467 && TREE_CODE (stmts[1]) == DECL_EXPR);
18468 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18469 args, complain, in_decl);
18470 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18471 args, complain, in_decl);
18472 DECL_CONTEXT (omp_out) = current_function_decl;
18473 DECL_CONTEXT (omp_in) = current_function_decl;
18474 keep_next_level (true);
18475 tree block = begin_omp_structured_block ();
18476 tsubst_expr (stmts[2], args, complain, in_decl, false);
18477 block = finish_omp_structured_block (block);
18478 block = maybe_cleanup_point_expr_void (block);
18479 add_decl_expr (omp_out);
18480 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18481 TREE_NO_WARNING (omp_out) = 1;
18482 add_decl_expr (omp_in);
18483 finish_expr_stmt (block);
18484 }
18485 if (i >= 6)
18486 {
18487 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18488 && TREE_CODE (stmts[4]) == DECL_EXPR);
18489 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18490 args, complain, in_decl);
18491 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18492 args, complain, in_decl);
18493 DECL_CONTEXT (omp_priv) = current_function_decl;
18494 DECL_CONTEXT (omp_orig) = current_function_decl;
18495 keep_next_level (true);
18496 tree block = begin_omp_structured_block ();
18497 tsubst_expr (stmts[5], args, complain, in_decl, false);
18498 block = finish_omp_structured_block (block);
18499 block = maybe_cleanup_point_expr_void (block);
18500 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18501 add_decl_expr (omp_priv);
18502 add_decl_expr (omp_orig);
18503 finish_expr_stmt (block);
18504 if (i == 7)
18505 add_decl_expr (omp_orig);
18506 }
18507 }
18508
18509 /* T is a postfix-expression that is not being used in a function
18510 call. Return the substituted version of T. */
18511
18512 static tree
18513 tsubst_non_call_postfix_expression (tree t, tree args,
18514 tsubst_flags_t complain,
18515 tree in_decl)
18516 {
18517 if (TREE_CODE (t) == SCOPE_REF)
18518 t = tsubst_qualified_id (t, args, complain, in_decl,
18519 /*done=*/false, /*address_p=*/false);
18520 else
18521 t = tsubst_copy_and_build (t, args, complain, in_decl,
18522 /*function_p=*/false,
18523 /*integral_constant_expression_p=*/false);
18524
18525 return t;
18526 }
18527
18528 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18529 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18530 dependent init-capture. */
18531
18532 static void
18533 prepend_one_capture (tree field, tree init, tree &list,
18534 tsubst_flags_t complain)
18535 {
18536 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18537 {
18538 tree type = NULL_TREE;
18539 if (!init)
18540 {
18541 if (complain & tf_error)
18542 error ("empty initializer in lambda init-capture");
18543 init = error_mark_node;
18544 }
18545 else if (TREE_CODE (init) == TREE_LIST)
18546 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18547 if (!type)
18548 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18549 TREE_TYPE (field) = type;
18550 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18551 }
18552 list = tree_cons (field, init, list);
18553 }
18554
18555 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18556 instantiation context. Instantiating a pack expansion containing a lambda
18557 might result in multiple lambdas all based on the same lambda in the
18558 template. */
18559
18560 tree
18561 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18562 {
18563 tree oldfn = lambda_function (t);
18564 in_decl = oldfn;
18565
18566 tree r = build_lambda_expr ();
18567
18568 LAMBDA_EXPR_LOCATION (r)
18569 = LAMBDA_EXPR_LOCATION (t);
18570 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18571 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18572 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18573 LAMBDA_EXPR_INSTANTIATED (r) = true;
18574
18575 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18576 /* A lambda in a default argument outside a class gets no
18577 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18578 tsubst_default_argument calls start_lambda_scope, so we need to
18579 specifically ignore it here, and use the global scope. */
18580 record_null_lambda_scope (r);
18581 else
18582 record_lambda_scope (r);
18583
18584 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18585 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18586
18587 vec<tree,va_gc>* field_packs = NULL;
18588
18589 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18590 cap = TREE_CHAIN (cap))
18591 {
18592 tree ofield = TREE_PURPOSE (cap);
18593 if (PACK_EXPANSION_P (ofield))
18594 ofield = PACK_EXPANSION_PATTERN (ofield);
18595 tree field = tsubst_decl (ofield, args, complain);
18596
18597 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18598 {
18599 /* Remember these for when we've pushed local_specializations. */
18600 vec_safe_push (field_packs, ofield);
18601 vec_safe_push (field_packs, field);
18602 }
18603
18604 if (field == error_mark_node)
18605 return error_mark_node;
18606
18607 tree init = TREE_VALUE (cap);
18608 if (PACK_EXPANSION_P (init))
18609 init = tsubst_pack_expansion (init, args, complain, in_decl);
18610 else
18611 init = tsubst_copy_and_build (init, args, complain, in_decl,
18612 /*fn*/false, /*constexpr*/false);
18613
18614 if (TREE_CODE (field) == TREE_VEC)
18615 {
18616 int len = TREE_VEC_LENGTH (field);
18617 gcc_assert (TREE_CODE (init) == TREE_VEC
18618 && TREE_VEC_LENGTH (init) == len);
18619 for (int i = 0; i < len; ++i)
18620 prepend_one_capture (TREE_VEC_ELT (field, i),
18621 TREE_VEC_ELT (init, i),
18622 LAMBDA_EXPR_CAPTURE_LIST (r),
18623 complain);
18624 }
18625 else
18626 {
18627 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18628 complain);
18629
18630 if (id_equal (DECL_NAME (field), "__this"))
18631 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18632 }
18633 }
18634
18635 tree type = begin_lambda_type (r);
18636 if (type == error_mark_node)
18637 return error_mark_node;
18638
18639 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18640 determine_visibility (TYPE_NAME (type));
18641
18642 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18643
18644 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18645 ? DECL_TI_TEMPLATE (oldfn)
18646 : NULL_TREE);
18647
18648 tree fntype = static_fn_type (oldfn);
18649 if (oldtmpl)
18650 ++processing_template_decl;
18651 fntype = tsubst (fntype, args, complain, in_decl);
18652 if (oldtmpl)
18653 --processing_template_decl;
18654
18655 if (fntype == error_mark_node)
18656 r = error_mark_node;
18657 else
18658 {
18659 /* The body of a lambda-expression is not a subexpression of the
18660 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18661 which would be skipped if cp_unevaluated_operand. */
18662 cp_evaluated ev;
18663
18664 /* Fix the type of 'this'. */
18665 fntype = build_memfn_type (fntype, type,
18666 type_memfn_quals (fntype),
18667 type_memfn_rqual (fntype));
18668 tree fn, tmpl;
18669 if (oldtmpl)
18670 {
18671 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18672 fn = DECL_TEMPLATE_RESULT (tmpl);
18673 finish_member_declaration (tmpl);
18674 }
18675 else
18676 {
18677 tmpl = NULL_TREE;
18678 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18679 finish_member_declaration (fn);
18680 }
18681
18682 /* Let finish_function set this. */
18683 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18684
18685 bool nested = cfun;
18686 if (nested)
18687 push_function_context ();
18688 else
18689 /* Still increment function_depth so that we don't GC in the
18690 middle of an expression. */
18691 ++function_depth;
18692
18693 local_specialization_stack s (lss_copy);
18694
18695 tree body = start_lambda_function (fn, r);
18696
18697 /* Now record them for lookup_init_capture_pack. */
18698 int fplen = vec_safe_length (field_packs);
18699 for (int i = 0; i < fplen; )
18700 {
18701 tree pack = (*field_packs)[i++];
18702 tree inst = (*field_packs)[i++];
18703 register_local_specialization (inst, pack);
18704 }
18705 release_tree_vector (field_packs);
18706
18707 register_parameter_specializations (oldfn, fn);
18708
18709 if (oldtmpl)
18710 {
18711 /* We might not partially instantiate some parts of the function, so
18712 copy these flags from the original template. */
18713 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18714 current_function_returns_value = ol->returns_value;
18715 current_function_returns_null = ol->returns_null;
18716 current_function_returns_abnormally = ol->returns_abnormally;
18717 current_function_infinite_loop = ol->infinite_loop;
18718 }
18719
18720 /* [temp.deduct] A lambda-expression appearing in a function type or a
18721 template parameter is not considered part of the immediate context for
18722 the purposes of template argument deduction. */
18723 complain = tf_warning_or_error;
18724
18725 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18726 /*constexpr*/false);
18727
18728 finish_lambda_function (body);
18729
18730 if (nested)
18731 pop_function_context ();
18732 else
18733 --function_depth;
18734
18735 /* The capture list was built up in reverse order; fix that now. */
18736 LAMBDA_EXPR_CAPTURE_LIST (r)
18737 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18738
18739 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18740
18741 maybe_add_lambda_conv_op (type);
18742 }
18743
18744 finish_struct (type, /*attr*/NULL_TREE);
18745
18746 insert_pending_capture_proxies ();
18747
18748 return r;
18749 }
18750
18751 /* Like tsubst but deals with expressions and performs semantic
18752 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18753
18754 tree
18755 tsubst_copy_and_build (tree t,
18756 tree args,
18757 tsubst_flags_t complain,
18758 tree in_decl,
18759 bool function_p,
18760 bool integral_constant_expression_p)
18761 {
18762 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18763 #define RECUR(NODE) \
18764 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18765 /*function_p=*/false, \
18766 integral_constant_expression_p)
18767
18768 tree retval, op1;
18769 location_t loc;
18770
18771 if (t == NULL_TREE || t == error_mark_node)
18772 return t;
18773
18774 loc = input_location;
18775 if (location_t eloc = cp_expr_location (t))
18776 input_location = eloc;
18777
18778 /* N3276 decltype magic only applies to calls at the top level or on the
18779 right side of a comma. */
18780 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18781 complain &= ~tf_decltype;
18782
18783 switch (TREE_CODE (t))
18784 {
18785 case USING_DECL:
18786 t = DECL_NAME (t);
18787 /* Fall through. */
18788 case IDENTIFIER_NODE:
18789 {
18790 tree decl;
18791 cp_id_kind idk;
18792 bool non_integral_constant_expression_p;
18793 const char *error_msg;
18794
18795 if (IDENTIFIER_CONV_OP_P (t))
18796 {
18797 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18798 t = make_conv_op_name (new_type);
18799 }
18800
18801 /* Look up the name. */
18802 decl = lookup_name (t);
18803
18804 /* By convention, expressions use ERROR_MARK_NODE to indicate
18805 failure, not NULL_TREE. */
18806 if (decl == NULL_TREE)
18807 decl = error_mark_node;
18808
18809 decl = finish_id_expression (t, decl, NULL_TREE,
18810 &idk,
18811 integral_constant_expression_p,
18812 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18813 &non_integral_constant_expression_p,
18814 /*template_p=*/false,
18815 /*done=*/true,
18816 /*address_p=*/false,
18817 /*template_arg_p=*/false,
18818 &error_msg,
18819 input_location);
18820 if (error_msg)
18821 error (error_msg);
18822 if (!function_p && identifier_p (decl))
18823 {
18824 if (complain & tf_error)
18825 unqualified_name_lookup_error (decl);
18826 decl = error_mark_node;
18827 }
18828 RETURN (decl);
18829 }
18830
18831 case TEMPLATE_ID_EXPR:
18832 {
18833 tree object;
18834 tree templ = RECUR (TREE_OPERAND (t, 0));
18835 tree targs = TREE_OPERAND (t, 1);
18836
18837 if (targs)
18838 targs = tsubst_template_args (targs, args, complain, in_decl);
18839 if (targs == error_mark_node)
18840 RETURN (error_mark_node);
18841
18842 if (TREE_CODE (templ) == SCOPE_REF)
18843 {
18844 tree name = TREE_OPERAND (templ, 1);
18845 tree tid = lookup_template_function (name, targs);
18846 TREE_OPERAND (templ, 1) = tid;
18847 RETURN (templ);
18848 }
18849
18850 if (concept_definition_p (templ))
18851 {
18852 tree check = build_concept_check (templ, targs, complain);
18853 if (check == error_mark_node)
18854 RETURN (error_mark_node);
18855
18856 tree id = unpack_concept_check (check);
18857
18858 /* If we built a function concept check, return the underlying
18859 template-id. So we can evaluate it as a function call. */
18860 if (function_concept_p (TREE_OPERAND (id, 0)))
18861 RETURN (id);
18862
18863 /* Evaluate the concept, if needed. */
18864 tree args = TREE_OPERAND (id, 1);
18865 if (!uses_template_parms (args)
18866 && !processing_constraint_expression_p ())
18867 RETURN (evaluate_concept_check (check, complain));
18868
18869 RETURN (check);
18870 }
18871
18872 if (variable_template_p (templ))
18873 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18874
18875 if (TREE_CODE (templ) == COMPONENT_REF)
18876 {
18877 object = TREE_OPERAND (templ, 0);
18878 templ = TREE_OPERAND (templ, 1);
18879 }
18880 else
18881 object = NULL_TREE;
18882 templ = lookup_template_function (templ, targs);
18883
18884 if (object)
18885 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18886 object, templ, NULL_TREE));
18887 else
18888 RETURN (baselink_for_fns (templ));
18889 }
18890
18891 case INDIRECT_REF:
18892 {
18893 tree r = RECUR (TREE_OPERAND (t, 0));
18894
18895 if (REFERENCE_REF_P (t))
18896 {
18897 /* A type conversion to reference type will be enclosed in
18898 such an indirect ref, but the substitution of the cast
18899 will have also added such an indirect ref. */
18900 r = convert_from_reference (r);
18901 }
18902 else
18903 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18904 complain|decltype_flag);
18905
18906 if (REF_PARENTHESIZED_P (t))
18907 r = force_paren_expr (r);
18908
18909 RETURN (r);
18910 }
18911
18912 case NOP_EXPR:
18913 {
18914 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18915 tree op0 = RECUR (TREE_OPERAND (t, 0));
18916 RETURN (build_nop (type, op0));
18917 }
18918
18919 case IMPLICIT_CONV_EXPR:
18920 {
18921 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18922 tree expr = RECUR (TREE_OPERAND (t, 0));
18923 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18924 {
18925 retval = copy_node (t);
18926 TREE_TYPE (retval) = type;
18927 TREE_OPERAND (retval, 0) = expr;
18928 RETURN (retval);
18929 }
18930 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18931 /* We'll pass this to convert_nontype_argument again, we don't need
18932 to actually perform any conversion here. */
18933 RETURN (expr);
18934 int flags = LOOKUP_IMPLICIT;
18935 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18936 flags = LOOKUP_NORMAL;
18937 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18938 flags |= LOOKUP_NO_NARROWING;
18939 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18940 flags));
18941 }
18942
18943 case CONVERT_EXPR:
18944 {
18945 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18946 tree op0 = RECUR (TREE_OPERAND (t, 0));
18947 if (op0 == error_mark_node)
18948 RETURN (error_mark_node);
18949 RETURN (build1 (CONVERT_EXPR, type, op0));
18950 }
18951
18952 case CAST_EXPR:
18953 case REINTERPRET_CAST_EXPR:
18954 case CONST_CAST_EXPR:
18955 case DYNAMIC_CAST_EXPR:
18956 case STATIC_CAST_EXPR:
18957 {
18958 tree type;
18959 tree op, r = NULL_TREE;
18960
18961 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18962 if (integral_constant_expression_p
18963 && !cast_valid_in_integral_constant_expression_p (type))
18964 {
18965 if (complain & tf_error)
18966 error ("a cast to a type other than an integral or "
18967 "enumeration type cannot appear in a constant-expression");
18968 RETURN (error_mark_node);
18969 }
18970
18971 op = RECUR (TREE_OPERAND (t, 0));
18972
18973 warning_sentinel s(warn_useless_cast);
18974 warning_sentinel s2(warn_ignored_qualifiers);
18975 switch (TREE_CODE (t))
18976 {
18977 case CAST_EXPR:
18978 r = build_functional_cast (type, op, complain);
18979 break;
18980 case REINTERPRET_CAST_EXPR:
18981 r = build_reinterpret_cast (type, op, complain);
18982 break;
18983 case CONST_CAST_EXPR:
18984 r = build_const_cast (type, op, complain);
18985 break;
18986 case DYNAMIC_CAST_EXPR:
18987 r = build_dynamic_cast (type, op, complain);
18988 break;
18989 case STATIC_CAST_EXPR:
18990 r = build_static_cast (type, op, complain);
18991 break;
18992 default:
18993 gcc_unreachable ();
18994 }
18995
18996 RETURN (r);
18997 }
18998
18999 case POSTDECREMENT_EXPR:
19000 case POSTINCREMENT_EXPR:
19001 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19002 args, complain, in_decl);
19003 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19004 complain|decltype_flag));
19005
19006 case PREDECREMENT_EXPR:
19007 case PREINCREMENT_EXPR:
19008 case NEGATE_EXPR:
19009 case BIT_NOT_EXPR:
19010 case ABS_EXPR:
19011 case TRUTH_NOT_EXPR:
19012 case UNARY_PLUS_EXPR: /* Unary + */
19013 case REALPART_EXPR:
19014 case IMAGPART_EXPR:
19015 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19016 RECUR (TREE_OPERAND (t, 0)),
19017 complain|decltype_flag));
19018
19019 case FIX_TRUNC_EXPR:
19020 gcc_unreachable ();
19021
19022 case ADDR_EXPR:
19023 op1 = TREE_OPERAND (t, 0);
19024 if (TREE_CODE (op1) == LABEL_DECL)
19025 RETURN (finish_label_address_expr (DECL_NAME (op1),
19026 EXPR_LOCATION (op1)));
19027 if (TREE_CODE (op1) == SCOPE_REF)
19028 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19029 /*done=*/true, /*address_p=*/true);
19030 else
19031 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19032 in_decl);
19033 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19034 complain|decltype_flag));
19035
19036 case PLUS_EXPR:
19037 case MINUS_EXPR:
19038 case MULT_EXPR:
19039 case TRUNC_DIV_EXPR:
19040 case CEIL_DIV_EXPR:
19041 case FLOOR_DIV_EXPR:
19042 case ROUND_DIV_EXPR:
19043 case EXACT_DIV_EXPR:
19044 case BIT_AND_EXPR:
19045 case BIT_IOR_EXPR:
19046 case BIT_XOR_EXPR:
19047 case TRUNC_MOD_EXPR:
19048 case FLOOR_MOD_EXPR:
19049 case TRUTH_ANDIF_EXPR:
19050 case TRUTH_ORIF_EXPR:
19051 case TRUTH_AND_EXPR:
19052 case TRUTH_OR_EXPR:
19053 case RSHIFT_EXPR:
19054 case LSHIFT_EXPR:
19055 case EQ_EXPR:
19056 case NE_EXPR:
19057 case MAX_EXPR:
19058 case MIN_EXPR:
19059 case LE_EXPR:
19060 case GE_EXPR:
19061 case LT_EXPR:
19062 case GT_EXPR:
19063 case SPACESHIP_EXPR:
19064 case MEMBER_REF:
19065 case DOTSTAR_EXPR:
19066 {
19067 warning_sentinel s1(warn_type_limits);
19068 warning_sentinel s2(warn_div_by_zero);
19069 warning_sentinel s3(warn_logical_op);
19070 warning_sentinel s4(warn_tautological_compare);
19071 tree op0 = RECUR (TREE_OPERAND (t, 0));
19072 tree op1 = RECUR (TREE_OPERAND (t, 1));
19073 tree r = build_x_binary_op
19074 (input_location, TREE_CODE (t),
19075 op0,
19076 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19077 ? ERROR_MARK
19078 : TREE_CODE (TREE_OPERAND (t, 0))),
19079 op1,
19080 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19081 ? ERROR_MARK
19082 : TREE_CODE (TREE_OPERAND (t, 1))),
19083 /*overload=*/NULL,
19084 complain|decltype_flag);
19085 if (EXPR_P (r) && TREE_NO_WARNING (t))
19086 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19087
19088 RETURN (r);
19089 }
19090
19091 case POINTER_PLUS_EXPR:
19092 {
19093 tree op0 = RECUR (TREE_OPERAND (t, 0));
19094 tree op1 = RECUR (TREE_OPERAND (t, 1));
19095 RETURN (fold_build_pointer_plus (op0, op1));
19096 }
19097
19098 case SCOPE_REF:
19099 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19100 /*address_p=*/false));
19101 case ARRAY_REF:
19102 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19103 args, complain, in_decl);
19104 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19105 RECUR (TREE_OPERAND (t, 1)),
19106 complain|decltype_flag));
19107
19108 case SIZEOF_EXPR:
19109 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19110 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19111 RETURN (tsubst_copy (t, args, complain, in_decl));
19112 /* Fall through */
19113
19114 case ALIGNOF_EXPR:
19115 {
19116 tree r;
19117
19118 op1 = TREE_OPERAND (t, 0);
19119 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19120 op1 = TREE_TYPE (op1);
19121 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19122 && ALIGNOF_EXPR_STD_P (t));
19123 if (!args)
19124 {
19125 /* When there are no ARGS, we are trying to evaluate a
19126 non-dependent expression from the parser. Trying to do
19127 the substitutions may not work. */
19128 if (!TYPE_P (op1))
19129 op1 = TREE_TYPE (op1);
19130 }
19131 else
19132 {
19133 ++cp_unevaluated_operand;
19134 ++c_inhibit_evaluation_warnings;
19135 if (TYPE_P (op1))
19136 op1 = tsubst (op1, args, complain, in_decl);
19137 else
19138 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19139 /*function_p=*/false,
19140 /*integral_constant_expression_p=*/
19141 false);
19142 --cp_unevaluated_operand;
19143 --c_inhibit_evaluation_warnings;
19144 }
19145 if (TYPE_P (op1))
19146 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
19147 complain & tf_error);
19148 else
19149 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
19150 complain & tf_error);
19151 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19152 {
19153 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19154 {
19155 if (!processing_template_decl && TYPE_P (op1))
19156 {
19157 r = build_min (SIZEOF_EXPR, size_type_node,
19158 build1 (NOP_EXPR, op1, error_mark_node));
19159 SIZEOF_EXPR_TYPE_P (r) = 1;
19160 }
19161 else
19162 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19163 TREE_SIDE_EFFECTS (r) = 0;
19164 TREE_READONLY (r) = 1;
19165 }
19166 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19167 }
19168 RETURN (r);
19169 }
19170
19171 case AT_ENCODE_EXPR:
19172 {
19173 op1 = TREE_OPERAND (t, 0);
19174 ++cp_unevaluated_operand;
19175 ++c_inhibit_evaluation_warnings;
19176 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19177 /*function_p=*/false,
19178 /*integral_constant_expression_p=*/false);
19179 --cp_unevaluated_operand;
19180 --c_inhibit_evaluation_warnings;
19181 RETURN (objc_build_encode_expr (op1));
19182 }
19183
19184 case NOEXCEPT_EXPR:
19185 op1 = TREE_OPERAND (t, 0);
19186 ++cp_unevaluated_operand;
19187 ++c_inhibit_evaluation_warnings;
19188 ++cp_noexcept_operand;
19189 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19190 /*function_p=*/false,
19191 /*integral_constant_expression_p=*/false);
19192 --cp_unevaluated_operand;
19193 --c_inhibit_evaluation_warnings;
19194 --cp_noexcept_operand;
19195 RETURN (finish_noexcept_expr (op1, complain));
19196
19197 case MODOP_EXPR:
19198 {
19199 warning_sentinel s(warn_div_by_zero);
19200 tree lhs = RECUR (TREE_OPERAND (t, 0));
19201 tree rhs = RECUR (TREE_OPERAND (t, 2));
19202 tree r = build_x_modify_expr
19203 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19204 complain|decltype_flag);
19205 /* TREE_NO_WARNING must be set if either the expression was
19206 parenthesized or it uses an operator such as >>= rather
19207 than plain assignment. In the former case, it was already
19208 set and must be copied. In the latter case,
19209 build_x_modify_expr sets it and it must not be reset
19210 here. */
19211 if (TREE_NO_WARNING (t))
19212 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19213
19214 RETURN (r);
19215 }
19216
19217 case ARROW_EXPR:
19218 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19219 args, complain, in_decl);
19220 /* Remember that there was a reference to this entity. */
19221 if (DECL_P (op1)
19222 && !mark_used (op1, complain) && !(complain & tf_error))
19223 RETURN (error_mark_node);
19224 RETURN (build_x_arrow (input_location, op1, complain));
19225
19226 case NEW_EXPR:
19227 {
19228 tree placement = RECUR (TREE_OPERAND (t, 0));
19229 tree init = RECUR (TREE_OPERAND (t, 3));
19230 vec<tree, va_gc> *placement_vec;
19231 vec<tree, va_gc> *init_vec;
19232 tree ret;
19233
19234 if (placement == NULL_TREE)
19235 placement_vec = NULL;
19236 else
19237 {
19238 placement_vec = make_tree_vector ();
19239 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19240 vec_safe_push (placement_vec, TREE_VALUE (placement));
19241 }
19242
19243 /* If there was an initializer in the original tree, but it
19244 instantiated to an empty list, then we should pass a
19245 non-NULL empty vector to tell build_new that it was an
19246 empty initializer() rather than no initializer. This can
19247 only happen when the initializer is a pack expansion whose
19248 parameter packs are of length zero. */
19249 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19250 init_vec = NULL;
19251 else
19252 {
19253 init_vec = make_tree_vector ();
19254 if (init == void_node)
19255 gcc_assert (init_vec != NULL);
19256 else
19257 {
19258 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19259 vec_safe_push (init_vec, TREE_VALUE (init));
19260 }
19261 }
19262
19263 /* Avoid passing an enclosing decl to valid_array_size_p. */
19264 in_decl = NULL_TREE;
19265
19266 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19267 tree op2 = RECUR (TREE_OPERAND (t, 2));
19268 ret = build_new (&placement_vec, op1, op2, &init_vec,
19269 NEW_EXPR_USE_GLOBAL (t),
19270 complain);
19271
19272 if (placement_vec != NULL)
19273 release_tree_vector (placement_vec);
19274 if (init_vec != NULL)
19275 release_tree_vector (init_vec);
19276
19277 RETURN (ret);
19278 }
19279
19280 case DELETE_EXPR:
19281 {
19282 tree op0 = RECUR (TREE_OPERAND (t, 0));
19283 tree op1 = RECUR (TREE_OPERAND (t, 1));
19284 RETURN (delete_sanity (op0, op1,
19285 DELETE_EXPR_USE_VEC (t),
19286 DELETE_EXPR_USE_GLOBAL (t),
19287 complain));
19288 }
19289
19290 case COMPOUND_EXPR:
19291 {
19292 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19293 complain & ~tf_decltype, in_decl,
19294 /*function_p=*/false,
19295 integral_constant_expression_p);
19296 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19297 op0,
19298 RECUR (TREE_OPERAND (t, 1)),
19299 complain|decltype_flag));
19300 }
19301
19302 case CALL_EXPR:
19303 {
19304 tree function;
19305 unsigned int nargs, i;
19306 bool qualified_p;
19307 bool koenig_p;
19308 tree ret;
19309
19310 function = CALL_EXPR_FN (t);
19311 /* Internal function with no arguments. */
19312 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19313 RETURN (t);
19314
19315 /* When we parsed the expression, we determined whether or
19316 not Koenig lookup should be performed. */
19317 koenig_p = KOENIG_LOOKUP_P (t);
19318 if (function == NULL_TREE)
19319 {
19320 koenig_p = false;
19321 qualified_p = false;
19322 }
19323 else if (TREE_CODE (function) == SCOPE_REF)
19324 {
19325 qualified_p = true;
19326 function = tsubst_qualified_id (function, args, complain, in_decl,
19327 /*done=*/false,
19328 /*address_p=*/false);
19329 }
19330 else if (koenig_p && identifier_p (function))
19331 {
19332 /* Do nothing; calling tsubst_copy_and_build on an identifier
19333 would incorrectly perform unqualified lookup again.
19334
19335 Note that we can also have an IDENTIFIER_NODE if the earlier
19336 unqualified lookup found a member function; in that case
19337 koenig_p will be false and we do want to do the lookup
19338 again to find the instantiated member function.
19339
19340 FIXME but doing that causes c++/15272, so we need to stop
19341 using IDENTIFIER_NODE in that situation. */
19342 qualified_p = false;
19343 }
19344 else
19345 {
19346 if (TREE_CODE (function) == COMPONENT_REF)
19347 {
19348 tree op = TREE_OPERAND (function, 1);
19349
19350 qualified_p = (TREE_CODE (op) == SCOPE_REF
19351 || (BASELINK_P (op)
19352 && BASELINK_QUALIFIED_P (op)));
19353 }
19354 else
19355 qualified_p = false;
19356
19357 if (TREE_CODE (function) == ADDR_EXPR
19358 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19359 /* Avoid error about taking the address of a constructor. */
19360 function = TREE_OPERAND (function, 0);
19361
19362 function = tsubst_copy_and_build (function, args, complain,
19363 in_decl,
19364 !qualified_p,
19365 integral_constant_expression_p);
19366
19367 if (BASELINK_P (function))
19368 qualified_p = true;
19369 }
19370
19371 nargs = call_expr_nargs (t);
19372 releasing_vec call_args;
19373 for (i = 0; i < nargs; ++i)
19374 {
19375 tree arg = CALL_EXPR_ARG (t, i);
19376
19377 if (!PACK_EXPANSION_P (arg))
19378 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19379 else
19380 {
19381 /* Expand the pack expansion and push each entry onto
19382 CALL_ARGS. */
19383 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19384 if (TREE_CODE (arg) == TREE_VEC)
19385 {
19386 unsigned int len, j;
19387
19388 len = TREE_VEC_LENGTH (arg);
19389 for (j = 0; j < len; ++j)
19390 {
19391 tree value = TREE_VEC_ELT (arg, j);
19392 if (value != NULL_TREE)
19393 value = convert_from_reference (value);
19394 vec_safe_push (call_args, value);
19395 }
19396 }
19397 else
19398 {
19399 /* A partial substitution. Add one entry. */
19400 vec_safe_push (call_args, arg);
19401 }
19402 }
19403 }
19404
19405 /* Stripped-down processing for a call in a thunk. Specifically, in
19406 the thunk template for a generic lambda. */
19407 if (CALL_FROM_THUNK_P (t))
19408 {
19409 /* Now that we've expanded any packs, the number of call args
19410 might be different. */
19411 unsigned int cargs = call_args->length ();
19412 tree thisarg = NULL_TREE;
19413 if (TREE_CODE (function) == COMPONENT_REF)
19414 {
19415 thisarg = TREE_OPERAND (function, 0);
19416 if (TREE_CODE (thisarg) == INDIRECT_REF)
19417 thisarg = TREE_OPERAND (thisarg, 0);
19418 function = TREE_OPERAND (function, 1);
19419 if (TREE_CODE (function) == BASELINK)
19420 function = BASELINK_FUNCTIONS (function);
19421 }
19422 /* We aren't going to do normal overload resolution, so force the
19423 template-id to resolve. */
19424 function = resolve_nondeduced_context (function, complain);
19425 for (unsigned i = 0; i < cargs; ++i)
19426 {
19427 /* In a thunk, pass through args directly, without any
19428 conversions. */
19429 tree arg = (*call_args)[i];
19430 while (TREE_CODE (arg) != PARM_DECL)
19431 arg = TREE_OPERAND (arg, 0);
19432 (*call_args)[i] = arg;
19433 }
19434 if (thisarg)
19435 {
19436 /* If there are no other args, just push 'this'. */
19437 if (cargs == 0)
19438 vec_safe_push (call_args, thisarg);
19439 else
19440 {
19441 /* Otherwise, shift the other args over to make room. */
19442 tree last = (*call_args)[cargs - 1];
19443 vec_safe_push (call_args, last);
19444 for (int i = cargs - 1; i > 0; --i)
19445 (*call_args)[i] = (*call_args)[i - 1];
19446 (*call_args)[0] = thisarg;
19447 }
19448 }
19449 ret = build_call_a (function, call_args->length (),
19450 call_args->address ());
19451 /* The thunk location is not interesting. */
19452 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19453 CALL_FROM_THUNK_P (ret) = true;
19454 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19455 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19456
19457 RETURN (ret);
19458 }
19459
19460 /* We do not perform argument-dependent lookup if normal
19461 lookup finds a non-function, in accordance with the
19462 expected resolution of DR 218. */
19463 if (koenig_p
19464 && ((is_overloaded_fn (function)
19465 /* If lookup found a member function, the Koenig lookup is
19466 not appropriate, even if an unqualified-name was used
19467 to denote the function. */
19468 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19469 || identifier_p (function))
19470 /* Only do this when substitution turns a dependent call
19471 into a non-dependent call. */
19472 && type_dependent_expression_p_push (t)
19473 && !any_type_dependent_arguments_p (call_args))
19474 function = perform_koenig_lookup (function, call_args, tf_none);
19475
19476 if (function != NULL_TREE
19477 && identifier_p (function)
19478 && !any_type_dependent_arguments_p (call_args))
19479 {
19480 if (koenig_p && (complain & tf_warning_or_error))
19481 {
19482 /* For backwards compatibility and good diagnostics, try
19483 the unqualified lookup again if we aren't in SFINAE
19484 context. */
19485 tree unq = (tsubst_copy_and_build
19486 (function, args, complain, in_decl, true,
19487 integral_constant_expression_p));
19488 if (unq == error_mark_node)
19489 RETURN (error_mark_node);
19490
19491 if (unq != function)
19492 {
19493 /* In a lambda fn, we have to be careful to not
19494 introduce new this captures. Legacy code can't
19495 be using lambdas anyway, so it's ok to be
19496 stricter. */
19497 bool in_lambda = (current_class_type
19498 && LAMBDA_TYPE_P (current_class_type));
19499 char const *const msg
19500 = G_("%qD was not declared in this scope, "
19501 "and no declarations were found by "
19502 "argument-dependent lookup at the point "
19503 "of instantiation");
19504
19505 bool diag = true;
19506 if (in_lambda)
19507 error_at (cp_expr_loc_or_input_loc (t),
19508 msg, function);
19509 else
19510 diag = permerror (cp_expr_loc_or_input_loc (t),
19511 msg, function);
19512 if (diag)
19513 {
19514 tree fn = unq;
19515
19516 if (INDIRECT_REF_P (fn))
19517 fn = TREE_OPERAND (fn, 0);
19518 if (is_overloaded_fn (fn))
19519 fn = get_first_fn (fn);
19520
19521 if (!DECL_P (fn))
19522 /* Can't say anything more. */;
19523 else if (DECL_CLASS_SCOPE_P (fn))
19524 {
19525 location_t loc = cp_expr_loc_or_input_loc (t);
19526 inform (loc,
19527 "declarations in dependent base %qT are "
19528 "not found by unqualified lookup",
19529 DECL_CLASS_CONTEXT (fn));
19530 if (current_class_ptr)
19531 inform (loc,
19532 "use %<this->%D%> instead", function);
19533 else
19534 inform (loc,
19535 "use %<%T::%D%> instead",
19536 current_class_name, function);
19537 }
19538 else
19539 inform (DECL_SOURCE_LOCATION (fn),
19540 "%qD declared here, later in the "
19541 "translation unit", fn);
19542 if (in_lambda)
19543 RETURN (error_mark_node);
19544 }
19545
19546 function = unq;
19547 }
19548 }
19549 if (identifier_p (function))
19550 {
19551 if (complain & tf_error)
19552 unqualified_name_lookup_error (function);
19553 RETURN (error_mark_node);
19554 }
19555 }
19556
19557 /* Remember that there was a reference to this entity. */
19558 if (function != NULL_TREE
19559 && DECL_P (function)
19560 && !mark_used (function, complain) && !(complain & tf_error))
19561 RETURN (error_mark_node);
19562
19563 /* Put back tf_decltype for the actual call. */
19564 complain |= decltype_flag;
19565
19566 if (function == NULL_TREE)
19567 switch (CALL_EXPR_IFN (t))
19568 {
19569 case IFN_LAUNDER:
19570 gcc_assert (nargs == 1);
19571 if (vec_safe_length (call_args) != 1)
19572 {
19573 error_at (cp_expr_loc_or_input_loc (t),
19574 "wrong number of arguments to "
19575 "%<__builtin_launder%>");
19576 ret = error_mark_node;
19577 }
19578 else
19579 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19580 (*call_args)[0], complain);
19581 break;
19582
19583 case IFN_VEC_CONVERT:
19584 gcc_assert (nargs == 1);
19585 if (vec_safe_length (call_args) != 1)
19586 {
19587 error_at (cp_expr_loc_or_input_loc (t),
19588 "wrong number of arguments to "
19589 "%<__builtin_convertvector%>");
19590 ret = error_mark_node;
19591 break;
19592 }
19593 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19594 tsubst (TREE_TYPE (t), args,
19595 complain, in_decl),
19596 complain);
19597 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19598 RETURN (ret);
19599 break;
19600
19601 default:
19602 /* Unsupported internal function with arguments. */
19603 gcc_unreachable ();
19604 }
19605 else if (TREE_CODE (function) == OFFSET_REF
19606 || TREE_CODE (function) == DOTSTAR_EXPR
19607 || TREE_CODE (function) == MEMBER_REF)
19608 ret = build_offset_ref_call_from_tree (function, &call_args,
19609 complain);
19610 else if (TREE_CODE (function) == COMPONENT_REF)
19611 {
19612 tree instance = TREE_OPERAND (function, 0);
19613 tree fn = TREE_OPERAND (function, 1);
19614
19615 if (processing_template_decl
19616 && (type_dependent_expression_p (instance)
19617 || (!BASELINK_P (fn)
19618 && TREE_CODE (fn) != FIELD_DECL)
19619 || type_dependent_expression_p (fn)
19620 || any_type_dependent_arguments_p (call_args)))
19621 ret = build_min_nt_call_vec (function, call_args);
19622 else if (!BASELINK_P (fn))
19623 ret = finish_call_expr (function, &call_args,
19624 /*disallow_virtual=*/false,
19625 /*koenig_p=*/false,
19626 complain);
19627 else
19628 ret = (build_new_method_call
19629 (instance, fn,
19630 &call_args, NULL_TREE,
19631 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19632 /*fn_p=*/NULL,
19633 complain));
19634 }
19635 else if (concept_check_p (function))
19636 {
19637 /* FUNCTION is a template-id referring to a concept definition. */
19638 tree id = unpack_concept_check (function);
19639 tree tmpl = TREE_OPERAND (id, 0);
19640 tree args = TREE_OPERAND (id, 1);
19641
19642 /* Calls to standard and variable concepts should have been
19643 previously diagnosed. */
19644 gcc_assert (function_concept_p (tmpl));
19645
19646 /* Ensure the result is wrapped as a call expression. */
19647 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19648
19649 /* Possibly evaluate the check if it is non-dependent. */
19650 if (!uses_template_parms (args)
19651 && !processing_constraint_expression_p ())
19652 ret = evaluate_concept_check (ret, complain);
19653 }
19654 else
19655 ret = finish_call_expr (function, &call_args,
19656 /*disallow_virtual=*/qualified_p,
19657 koenig_p,
19658 complain);
19659
19660 if (ret != error_mark_node)
19661 {
19662 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19663 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19664 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19665 if (op || ord || rev)
19666 {
19667 function = extract_call_expr (ret);
19668 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19669 CALL_EXPR_ORDERED_ARGS (function) = ord;
19670 CALL_EXPR_REVERSE_ARGS (function) = rev;
19671 }
19672 }
19673
19674 RETURN (ret);
19675 }
19676
19677 case COND_EXPR:
19678 {
19679 tree cond = RECUR (TREE_OPERAND (t, 0));
19680 cond = mark_rvalue_use (cond);
19681 tree folded_cond = fold_non_dependent_expr (cond, complain);
19682 tree exp1, exp2;
19683
19684 if (TREE_CODE (folded_cond) == INTEGER_CST)
19685 {
19686 if (integer_zerop (folded_cond))
19687 {
19688 ++c_inhibit_evaluation_warnings;
19689 exp1 = RECUR (TREE_OPERAND (t, 1));
19690 --c_inhibit_evaluation_warnings;
19691 exp2 = RECUR (TREE_OPERAND (t, 2));
19692 }
19693 else
19694 {
19695 exp1 = RECUR (TREE_OPERAND (t, 1));
19696 ++c_inhibit_evaluation_warnings;
19697 exp2 = RECUR (TREE_OPERAND (t, 2));
19698 --c_inhibit_evaluation_warnings;
19699 }
19700 cond = folded_cond;
19701 }
19702 else
19703 {
19704 exp1 = RECUR (TREE_OPERAND (t, 1));
19705 exp2 = RECUR (TREE_OPERAND (t, 2));
19706 }
19707
19708 warning_sentinel s(warn_duplicated_branches);
19709 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19710 cond, exp1, exp2, complain));
19711 }
19712
19713 case PSEUDO_DTOR_EXPR:
19714 {
19715 tree op0 = RECUR (TREE_OPERAND (t, 0));
19716 tree op1 = RECUR (TREE_OPERAND (t, 1));
19717 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19718 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19719 input_location));
19720 }
19721
19722 case TREE_LIST:
19723 {
19724 tree purpose, value, chain;
19725
19726 if (t == void_list_node)
19727 RETURN (t);
19728
19729 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19730 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19731 {
19732 /* We have pack expansions, so expand those and
19733 create a new list out of it. */
19734 tree purposevec = NULL_TREE;
19735 tree valuevec = NULL_TREE;
19736 tree chain;
19737 int i, len = -1;
19738
19739 /* Expand the argument expressions. */
19740 if (TREE_PURPOSE (t))
19741 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19742 complain, in_decl);
19743 if (TREE_VALUE (t))
19744 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19745 complain, in_decl);
19746
19747 /* Build the rest of the list. */
19748 chain = TREE_CHAIN (t);
19749 if (chain && chain != void_type_node)
19750 chain = RECUR (chain);
19751
19752 /* Determine the number of arguments. */
19753 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19754 {
19755 len = TREE_VEC_LENGTH (purposevec);
19756 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19757 }
19758 else if (TREE_CODE (valuevec) == TREE_VEC)
19759 len = TREE_VEC_LENGTH (valuevec);
19760 else
19761 {
19762 /* Since we only performed a partial substitution into
19763 the argument pack, we only RETURN (a single list
19764 node. */
19765 if (purposevec == TREE_PURPOSE (t)
19766 && valuevec == TREE_VALUE (t)
19767 && chain == TREE_CHAIN (t))
19768 RETURN (t);
19769
19770 RETURN (tree_cons (purposevec, valuevec, chain));
19771 }
19772
19773 /* Convert the argument vectors into a TREE_LIST */
19774 i = len;
19775 while (i > 0)
19776 {
19777 /* Grab the Ith values. */
19778 i--;
19779 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19780 : NULL_TREE;
19781 value
19782 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19783 : NULL_TREE;
19784
19785 /* Build the list (backwards). */
19786 chain = tree_cons (purpose, value, chain);
19787 }
19788
19789 RETURN (chain);
19790 }
19791
19792 purpose = TREE_PURPOSE (t);
19793 if (purpose)
19794 purpose = RECUR (purpose);
19795 value = TREE_VALUE (t);
19796 if (value)
19797 value = RECUR (value);
19798 chain = TREE_CHAIN (t);
19799 if (chain && chain != void_type_node)
19800 chain = RECUR (chain);
19801 if (purpose == TREE_PURPOSE (t)
19802 && value == TREE_VALUE (t)
19803 && chain == TREE_CHAIN (t))
19804 RETURN (t);
19805 RETURN (tree_cons (purpose, value, chain));
19806 }
19807
19808 case COMPONENT_REF:
19809 {
19810 tree object;
19811 tree object_type;
19812 tree member;
19813 tree r;
19814
19815 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19816 args, complain, in_decl);
19817 /* Remember that there was a reference to this entity. */
19818 if (DECL_P (object)
19819 && !mark_used (object, complain) && !(complain & tf_error))
19820 RETURN (error_mark_node);
19821 object_type = TREE_TYPE (object);
19822
19823 member = TREE_OPERAND (t, 1);
19824 if (BASELINK_P (member))
19825 member = tsubst_baselink (member,
19826 non_reference (TREE_TYPE (object)),
19827 args, complain, in_decl);
19828 else
19829 member = tsubst_copy (member, args, complain, in_decl);
19830 if (member == error_mark_node)
19831 RETURN (error_mark_node);
19832
19833 if (TREE_CODE (member) == FIELD_DECL)
19834 {
19835 r = finish_non_static_data_member (member, object, NULL_TREE);
19836 if (TREE_CODE (r) == COMPONENT_REF)
19837 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19838 RETURN (r);
19839 }
19840 else if (type_dependent_expression_p (object))
19841 /* We can't do much here. */;
19842 else if (!CLASS_TYPE_P (object_type))
19843 {
19844 if (scalarish_type_p (object_type))
19845 {
19846 tree s = NULL_TREE;
19847 tree dtor = member;
19848
19849 if (TREE_CODE (dtor) == SCOPE_REF)
19850 {
19851 s = TREE_OPERAND (dtor, 0);
19852 dtor = TREE_OPERAND (dtor, 1);
19853 }
19854 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19855 {
19856 dtor = TREE_OPERAND (dtor, 0);
19857 if (TYPE_P (dtor))
19858 RETURN (finish_pseudo_destructor_expr
19859 (object, s, dtor, input_location));
19860 }
19861 }
19862 }
19863 else if (TREE_CODE (member) == SCOPE_REF
19864 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19865 {
19866 /* Lookup the template functions now that we know what the
19867 scope is. */
19868 tree scope = TREE_OPERAND (member, 0);
19869 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19870 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19871 member = lookup_qualified_name (scope, tmpl,
19872 /*is_type_p=*/false,
19873 /*complain=*/false);
19874 if (BASELINK_P (member))
19875 {
19876 BASELINK_FUNCTIONS (member)
19877 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19878 args);
19879 member = (adjust_result_of_qualified_name_lookup
19880 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19881 object_type));
19882 }
19883 else
19884 {
19885 qualified_name_lookup_error (scope, tmpl, member,
19886 input_location);
19887 RETURN (error_mark_node);
19888 }
19889 }
19890 else if (TREE_CODE (member) == SCOPE_REF
19891 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19892 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19893 {
19894 if (complain & tf_error)
19895 {
19896 if (TYPE_P (TREE_OPERAND (member, 0)))
19897 error ("%qT is not a class or namespace",
19898 TREE_OPERAND (member, 0));
19899 else
19900 error ("%qD is not a class or namespace",
19901 TREE_OPERAND (member, 0));
19902 }
19903 RETURN (error_mark_node);
19904 }
19905
19906 r = finish_class_member_access_expr (object, member,
19907 /*template_p=*/false,
19908 complain);
19909 if (TREE_CODE (r) == COMPONENT_REF)
19910 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19911 RETURN (r);
19912 }
19913
19914 case THROW_EXPR:
19915 RETURN (build_throw
19916 (RECUR (TREE_OPERAND (t, 0))));
19917
19918 case CONSTRUCTOR:
19919 {
19920 vec<constructor_elt, va_gc> *n;
19921 constructor_elt *ce;
19922 unsigned HOST_WIDE_INT idx;
19923 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19924 bool process_index_p;
19925 int newlen;
19926 bool need_copy_p = false;
19927 tree r;
19928
19929 if (type == error_mark_node)
19930 RETURN (error_mark_node);
19931
19932 /* We do not want to process the index of aggregate
19933 initializers as they are identifier nodes which will be
19934 looked up by digest_init. */
19935 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19936
19937 if (null_member_pointer_value_p (t))
19938 {
19939 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19940 RETURN (t);
19941 }
19942
19943 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19944 newlen = vec_safe_length (n);
19945 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19946 {
19947 if (ce->index && process_index_p
19948 /* An identifier index is looked up in the type
19949 being initialized, not the current scope. */
19950 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19951 ce->index = RECUR (ce->index);
19952
19953 if (PACK_EXPANSION_P (ce->value))
19954 {
19955 /* Substitute into the pack expansion. */
19956 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19957 in_decl);
19958
19959 if (ce->value == error_mark_node
19960 || PACK_EXPANSION_P (ce->value))
19961 ;
19962 else if (TREE_VEC_LENGTH (ce->value) == 1)
19963 /* Just move the argument into place. */
19964 ce->value = TREE_VEC_ELT (ce->value, 0);
19965 else
19966 {
19967 /* Update the length of the final CONSTRUCTOR
19968 arguments vector, and note that we will need to
19969 copy.*/
19970 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19971 need_copy_p = true;
19972 }
19973 }
19974 else
19975 ce->value = RECUR (ce->value);
19976 }
19977
19978 if (need_copy_p)
19979 {
19980 vec<constructor_elt, va_gc> *old_n = n;
19981
19982 vec_alloc (n, newlen);
19983 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19984 {
19985 if (TREE_CODE (ce->value) == TREE_VEC)
19986 {
19987 int i, len = TREE_VEC_LENGTH (ce->value);
19988 for (i = 0; i < len; ++i)
19989 CONSTRUCTOR_APPEND_ELT (n, 0,
19990 TREE_VEC_ELT (ce->value, i));
19991 }
19992 else
19993 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19994 }
19995 }
19996
19997 r = build_constructor (init_list_type_node, n);
19998 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19999 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20000 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20001
20002 if (TREE_HAS_CONSTRUCTOR (t))
20003 {
20004 fcl_t cl = fcl_functional;
20005 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20006 cl = fcl_c99;
20007 RETURN (finish_compound_literal (type, r, complain, cl));
20008 }
20009
20010 TREE_TYPE (r) = type;
20011 RETURN (r);
20012 }
20013
20014 case TYPEID_EXPR:
20015 {
20016 tree operand_0 = TREE_OPERAND (t, 0);
20017 if (TYPE_P (operand_0))
20018 {
20019 operand_0 = tsubst (operand_0, args, complain, in_decl);
20020 RETURN (get_typeid (operand_0, complain));
20021 }
20022 else
20023 {
20024 operand_0 = RECUR (operand_0);
20025 RETURN (build_typeid (operand_0, complain));
20026 }
20027 }
20028
20029 case VAR_DECL:
20030 if (!args)
20031 RETURN (t);
20032 /* Fall through */
20033
20034 case PARM_DECL:
20035 {
20036 tree r = tsubst_copy (t, args, complain, in_decl);
20037 /* ??? We're doing a subset of finish_id_expression here. */
20038 if (tree wrap = maybe_get_tls_wrapper_call (r))
20039 /* Replace an evaluated use of the thread_local variable with
20040 a call to its wrapper. */
20041 r = wrap;
20042 else if (outer_automatic_var_p (r))
20043 r = process_outer_var_ref (r, complain);
20044
20045 if (!TYPE_REF_P (TREE_TYPE (t)))
20046 /* If the original type was a reference, we'll be wrapped in
20047 the appropriate INDIRECT_REF. */
20048 r = convert_from_reference (r);
20049 RETURN (r);
20050 }
20051
20052 case VA_ARG_EXPR:
20053 {
20054 tree op0 = RECUR (TREE_OPERAND (t, 0));
20055 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20056 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20057 }
20058
20059 case OFFSETOF_EXPR:
20060 {
20061 tree object_ptr
20062 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20063 in_decl, /*function_p=*/false,
20064 /*integral_constant_expression_p=*/false);
20065 RETURN (finish_offsetof (object_ptr,
20066 RECUR (TREE_OPERAND (t, 0)),
20067 EXPR_LOCATION (t)));
20068 }
20069
20070 case ADDRESSOF_EXPR:
20071 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20072 RECUR (TREE_OPERAND (t, 0)), complain));
20073
20074 case TRAIT_EXPR:
20075 {
20076 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20077 complain, in_decl);
20078
20079 tree type2 = TRAIT_EXPR_TYPE2 (t);
20080 if (type2 && TREE_CODE (type2) == TREE_LIST)
20081 type2 = RECUR (type2);
20082 else if (type2)
20083 type2 = tsubst (type2, args, complain, in_decl);
20084
20085 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20086 TRAIT_EXPR_KIND (t), type1, type2));
20087 }
20088
20089 case STMT_EXPR:
20090 {
20091 tree old_stmt_expr = cur_stmt_expr;
20092 tree stmt_expr = begin_stmt_expr ();
20093
20094 cur_stmt_expr = stmt_expr;
20095 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20096 integral_constant_expression_p);
20097 stmt_expr = finish_stmt_expr (stmt_expr, false);
20098 cur_stmt_expr = old_stmt_expr;
20099
20100 /* If the resulting list of expression statement is empty,
20101 fold it further into void_node. */
20102 if (empty_expr_stmt_p (stmt_expr))
20103 stmt_expr = void_node;
20104
20105 RETURN (stmt_expr);
20106 }
20107
20108 case LAMBDA_EXPR:
20109 {
20110 if (complain & tf_partial)
20111 {
20112 /* We don't have a full set of template arguments yet; don't touch
20113 the lambda at all. */
20114 gcc_assert (processing_template_decl);
20115 return t;
20116 }
20117 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20118
20119 RETURN (build_lambda_object (r));
20120 }
20121
20122 case TARGET_EXPR:
20123 /* We can get here for a constant initializer of non-dependent type.
20124 FIXME stop folding in cp_parser_initializer_clause. */
20125 {
20126 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20127 complain);
20128 RETURN (r);
20129 }
20130
20131 case TRANSACTION_EXPR:
20132 RETURN (tsubst_expr(t, args, complain, in_decl,
20133 integral_constant_expression_p));
20134
20135 case PAREN_EXPR:
20136 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20137
20138 case VEC_PERM_EXPR:
20139 {
20140 tree op0 = RECUR (TREE_OPERAND (t, 0));
20141 tree op1 = RECUR (TREE_OPERAND (t, 1));
20142 tree op2 = RECUR (TREE_OPERAND (t, 2));
20143 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20144 complain));
20145 }
20146
20147 case REQUIRES_EXPR:
20148 {
20149 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20150 if (r == error_mark_node && (complain & tf_error))
20151 tsubst_requires_expr (t, args, complain, in_decl);
20152 RETURN (r);
20153 }
20154
20155 case RANGE_EXPR:
20156 /* No need to substitute further, a RANGE_EXPR will always be built
20157 with constant operands. */
20158 RETURN (t);
20159
20160 case NON_LVALUE_EXPR:
20161 case VIEW_CONVERT_EXPR:
20162 if (location_wrapper_p (t))
20163 /* We need to do this here as well as in tsubst_copy so we get the
20164 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20165 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20166 EXPR_LOCATION (t)));
20167 /* fallthrough. */
20168
20169 default:
20170 /* Handle Objective-C++ constructs, if appropriate. */
20171 {
20172 tree subst
20173 = objcp_tsubst_copy_and_build (t, args, complain,
20174 in_decl, /*function_p=*/false);
20175 if (subst)
20176 RETURN (subst);
20177 }
20178 RETURN (tsubst_copy (t, args, complain, in_decl));
20179 }
20180
20181 #undef RECUR
20182 #undef RETURN
20183 out:
20184 input_location = loc;
20185 return retval;
20186 }
20187
20188 /* Verify that the instantiated ARGS are valid. For type arguments,
20189 make sure that the type's linkage is ok. For non-type arguments,
20190 make sure they are constants if they are integral or enumerations.
20191 Emit an error under control of COMPLAIN, and return TRUE on error. */
20192
20193 static bool
20194 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20195 {
20196 if (dependent_template_arg_p (t))
20197 return false;
20198 if (ARGUMENT_PACK_P (t))
20199 {
20200 tree vec = ARGUMENT_PACK_ARGS (t);
20201 int len = TREE_VEC_LENGTH (vec);
20202 bool result = false;
20203 int i;
20204
20205 for (i = 0; i < len; ++i)
20206 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20207 result = true;
20208 return result;
20209 }
20210 else if (TYPE_P (t))
20211 {
20212 /* [basic.link]: A name with no linkage (notably, the name
20213 of a class or enumeration declared in a local scope)
20214 shall not be used to declare an entity with linkage.
20215 This implies that names with no linkage cannot be used as
20216 template arguments
20217
20218 DR 757 relaxes this restriction for C++0x. */
20219 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20220 : no_linkage_check (t, /*relaxed_p=*/false));
20221
20222 if (nt)
20223 {
20224 /* DR 488 makes use of a type with no linkage cause
20225 type deduction to fail. */
20226 if (complain & tf_error)
20227 {
20228 if (TYPE_UNNAMED_P (nt))
20229 error ("%qT is/uses unnamed type", t);
20230 else
20231 error ("template argument for %qD uses local type %qT",
20232 tmpl, t);
20233 }
20234 return true;
20235 }
20236 /* In order to avoid all sorts of complications, we do not
20237 allow variably-modified types as template arguments. */
20238 else if (variably_modified_type_p (t, NULL_TREE))
20239 {
20240 if (complain & tf_error)
20241 error ("%qT is a variably modified type", t);
20242 return true;
20243 }
20244 }
20245 /* Class template and alias template arguments should be OK. */
20246 else if (DECL_TYPE_TEMPLATE_P (t))
20247 ;
20248 /* A non-type argument of integral or enumerated type must be a
20249 constant. */
20250 else if (TREE_TYPE (t)
20251 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20252 && !REFERENCE_REF_P (t)
20253 && !TREE_CONSTANT (t))
20254 {
20255 if (complain & tf_error)
20256 error ("integral expression %qE is not constant", t);
20257 return true;
20258 }
20259 return false;
20260 }
20261
20262 static bool
20263 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20264 {
20265 int ix, len = DECL_NTPARMS (tmpl);
20266 bool result = false;
20267
20268 for (ix = 0; ix != len; ix++)
20269 {
20270 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20271 result = true;
20272 }
20273 if (result && (complain & tf_error))
20274 error (" trying to instantiate %qD", tmpl);
20275 return result;
20276 }
20277
20278 /* We're out of SFINAE context now, so generate diagnostics for the access
20279 errors we saw earlier when instantiating D from TMPL and ARGS. */
20280
20281 static void
20282 recheck_decl_substitution (tree d, tree tmpl, tree args)
20283 {
20284 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20285 tree type = TREE_TYPE (pattern);
20286 location_t loc = input_location;
20287
20288 push_access_scope (d);
20289 push_deferring_access_checks (dk_no_deferred);
20290 input_location = DECL_SOURCE_LOCATION (pattern);
20291 tsubst (type, args, tf_warning_or_error, d);
20292 input_location = loc;
20293 pop_deferring_access_checks ();
20294 pop_access_scope (d);
20295 }
20296
20297 /* Instantiate the indicated variable, function, or alias template TMPL with
20298 the template arguments in TARG_PTR. */
20299
20300 static tree
20301 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20302 {
20303 tree targ_ptr = orig_args;
20304 tree fndecl;
20305 tree gen_tmpl;
20306 tree spec;
20307 bool access_ok = true;
20308
20309 if (tmpl == error_mark_node)
20310 return error_mark_node;
20311
20312 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20313
20314 /* If this function is a clone, handle it specially. */
20315 if (DECL_CLONED_FUNCTION_P (tmpl))
20316 {
20317 tree spec;
20318 tree clone;
20319
20320 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20321 DECL_CLONED_FUNCTION. */
20322 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20323 targ_ptr, complain);
20324 if (spec == error_mark_node)
20325 return error_mark_node;
20326
20327 /* Look for the clone. */
20328 FOR_EACH_CLONE (clone, spec)
20329 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20330 return clone;
20331 /* We should always have found the clone by now. */
20332 gcc_unreachable ();
20333 return NULL_TREE;
20334 }
20335
20336 if (targ_ptr == error_mark_node)
20337 return error_mark_node;
20338
20339 /* Check to see if we already have this specialization. */
20340 gen_tmpl = most_general_template (tmpl);
20341 if (TMPL_ARGS_DEPTH (targ_ptr)
20342 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20343 /* targ_ptr only has the innermost template args, so add the outer ones
20344 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20345 the case of a non-dependent call within a template definition). */
20346 targ_ptr = (add_outermost_template_args
20347 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20348 targ_ptr));
20349
20350 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20351 but it doesn't seem to be on the hot path. */
20352 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20353
20354 gcc_assert (tmpl == gen_tmpl
20355 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20356 == spec)
20357 || fndecl == NULL_TREE);
20358
20359 if (spec != NULL_TREE)
20360 {
20361 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20362 {
20363 if (complain & tf_error)
20364 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20365 return error_mark_node;
20366 }
20367 return spec;
20368 }
20369
20370 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20371 complain))
20372 return error_mark_node;
20373
20374 /* We are building a FUNCTION_DECL, during which the access of its
20375 parameters and return types have to be checked. However this
20376 FUNCTION_DECL which is the desired context for access checking
20377 is not built yet. We solve this chicken-and-egg problem by
20378 deferring all checks until we have the FUNCTION_DECL. */
20379 push_deferring_access_checks (dk_deferred);
20380
20381 /* Instantiation of the function happens in the context of the function
20382 template, not the context of the overload resolution we're doing. */
20383 push_to_top_level ();
20384 /* If there are dependent arguments, e.g. because we're doing partial
20385 ordering, make sure processing_template_decl stays set. */
20386 if (uses_template_parms (targ_ptr))
20387 ++processing_template_decl;
20388 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20389 {
20390 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20391 complain, gen_tmpl, true);
20392 push_nested_class (ctx);
20393 }
20394
20395 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20396
20397 fndecl = NULL_TREE;
20398 if (VAR_P (pattern))
20399 {
20400 /* We need to determine if we're using a partial or explicit
20401 specialization now, because the type of the variable could be
20402 different. */
20403 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20404 tree elt = most_specialized_partial_spec (tid, complain);
20405 if (elt == error_mark_node)
20406 pattern = error_mark_node;
20407 else if (elt)
20408 {
20409 tree partial_tmpl = TREE_VALUE (elt);
20410 tree partial_args = TREE_PURPOSE (elt);
20411 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20412 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20413 }
20414 }
20415
20416 /* Substitute template parameters to obtain the specialization. */
20417 if (fndecl == NULL_TREE)
20418 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20419 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20420 pop_nested_class ();
20421 pop_from_top_level ();
20422
20423 if (fndecl == error_mark_node)
20424 {
20425 pop_deferring_access_checks ();
20426 return error_mark_node;
20427 }
20428
20429 /* The DECL_TI_TEMPLATE should always be the immediate parent
20430 template, not the most general template. */
20431 DECL_TI_TEMPLATE (fndecl) = tmpl;
20432 DECL_TI_ARGS (fndecl) = targ_ptr;
20433
20434 /* Now we know the specialization, compute access previously
20435 deferred. Do no access control for inheriting constructors,
20436 as we already checked access for the inherited constructor. */
20437 if (!(flag_new_inheriting_ctors
20438 && DECL_INHERITED_CTOR (fndecl)))
20439 {
20440 push_access_scope (fndecl);
20441 if (!perform_deferred_access_checks (complain))
20442 access_ok = false;
20443 pop_access_scope (fndecl);
20444 }
20445 pop_deferring_access_checks ();
20446
20447 /* If we've just instantiated the main entry point for a function,
20448 instantiate all the alternate entry points as well. We do this
20449 by cloning the instantiation of the main entry point, not by
20450 instantiating the template clones. */
20451 if (tree chain = DECL_CHAIN (gen_tmpl))
20452 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20453 clone_function_decl (fndecl, /*update_methods=*/false);
20454
20455 if (!access_ok)
20456 {
20457 if (!(complain & tf_error))
20458 {
20459 /* Remember to reinstantiate when we're out of SFINAE so the user
20460 can see the errors. */
20461 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20462 }
20463 return error_mark_node;
20464 }
20465 return fndecl;
20466 }
20467
20468 /* Wrapper for instantiate_template_1. */
20469
20470 tree
20471 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20472 {
20473 tree ret;
20474 timevar_push (TV_TEMPLATE_INST);
20475 ret = instantiate_template_1 (tmpl, orig_args, complain);
20476 timevar_pop (TV_TEMPLATE_INST);
20477 return ret;
20478 }
20479
20480 /* Instantiate the alias template TMPL with ARGS. Also push a template
20481 instantiation level, which instantiate_template doesn't do because
20482 functions and variables have sufficient context established by the
20483 callers. */
20484
20485 static tree
20486 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20487 {
20488 if (tmpl == error_mark_node || args == error_mark_node)
20489 return error_mark_node;
20490 if (!push_tinst_level (tmpl, args))
20491 return error_mark_node;
20492
20493 args =
20494 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20495 args, tmpl, complain,
20496 /*require_all_args=*/true,
20497 /*use_default_args=*/true);
20498
20499 tree r = instantiate_template (tmpl, args, complain);
20500 pop_tinst_level ();
20501
20502 return r;
20503 }
20504
20505 /* PARM is a template parameter pack for FN. Returns true iff
20506 PARM is used in a deducible way in the argument list of FN. */
20507
20508 static bool
20509 pack_deducible_p (tree parm, tree fn)
20510 {
20511 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20512 for (; t; t = TREE_CHAIN (t))
20513 {
20514 tree type = TREE_VALUE (t);
20515 tree packs;
20516 if (!PACK_EXPANSION_P (type))
20517 continue;
20518 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20519 packs; packs = TREE_CHAIN (packs))
20520 if (template_args_equal (TREE_VALUE (packs), parm))
20521 {
20522 /* The template parameter pack is used in a function parameter
20523 pack. If this is the end of the parameter list, the
20524 template parameter pack is deducible. */
20525 if (TREE_CHAIN (t) == void_list_node)
20526 return true;
20527 else
20528 /* Otherwise, not. Well, it could be deduced from
20529 a non-pack parameter, but doing so would end up with
20530 a deduction mismatch, so don't bother. */
20531 return false;
20532 }
20533 }
20534 /* The template parameter pack isn't used in any function parameter
20535 packs, but it might be used deeper, e.g. tuple<Args...>. */
20536 return true;
20537 }
20538
20539 /* Subroutine of fn_type_unification: check non-dependent parms for
20540 convertibility. */
20541
20542 static int
20543 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20544 tree fn, unification_kind_t strict, int flags,
20545 struct conversion **convs, bool explain_p)
20546 {
20547 /* Non-constructor methods need to leave a conversion for 'this', which
20548 isn't included in nargs here. */
20549 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20550 && !DECL_CONSTRUCTOR_P (fn));
20551
20552 for (unsigned ia = 0;
20553 parms && parms != void_list_node && ia < nargs; )
20554 {
20555 tree parm = TREE_VALUE (parms);
20556
20557 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20558 && (!TREE_CHAIN (parms)
20559 || TREE_CHAIN (parms) == void_list_node))
20560 /* For a function parameter pack that occurs at the end of the
20561 parameter-declaration-list, the type A of each remaining
20562 argument of the call is compared with the type P of the
20563 declarator-id of the function parameter pack. */
20564 break;
20565
20566 parms = TREE_CHAIN (parms);
20567
20568 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20569 /* For a function parameter pack that does not occur at the
20570 end of the parameter-declaration-list, the type of the
20571 parameter pack is a non-deduced context. */
20572 continue;
20573
20574 if (!uses_template_parms (parm))
20575 {
20576 tree arg = args[ia];
20577 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20578 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20579
20580 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20581 conv_p, explain_p))
20582 return 1;
20583 }
20584
20585 ++ia;
20586 }
20587
20588 return 0;
20589 }
20590
20591 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20592 NARGS elements of the arguments that are being used when calling
20593 it. TARGS is a vector into which the deduced template arguments
20594 are placed.
20595
20596 Returns either a FUNCTION_DECL for the matching specialization of FN or
20597 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20598 true, diagnostics will be printed to explain why it failed.
20599
20600 If FN is a conversion operator, or we are trying to produce a specific
20601 specialization, RETURN_TYPE is the return type desired.
20602
20603 The EXPLICIT_TARGS are explicit template arguments provided via a
20604 template-id.
20605
20606 The parameter STRICT is one of:
20607
20608 DEDUCE_CALL:
20609 We are deducing arguments for a function call, as in
20610 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20611 deducing arguments for a call to the result of a conversion
20612 function template, as in [over.call.object].
20613
20614 DEDUCE_CONV:
20615 We are deducing arguments for a conversion function, as in
20616 [temp.deduct.conv].
20617
20618 DEDUCE_EXACT:
20619 We are deducing arguments when doing an explicit instantiation
20620 as in [temp.explicit], when determining an explicit specialization
20621 as in [temp.expl.spec], or when taking the address of a function
20622 template, as in [temp.deduct.funcaddr]. */
20623
20624 tree
20625 fn_type_unification (tree fn,
20626 tree explicit_targs,
20627 tree targs,
20628 const tree *args,
20629 unsigned int nargs,
20630 tree return_type,
20631 unification_kind_t strict,
20632 int flags,
20633 struct conversion **convs,
20634 bool explain_p,
20635 bool decltype_p)
20636 {
20637 tree parms;
20638 tree fntype;
20639 tree decl = NULL_TREE;
20640 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20641 bool ok;
20642 static int deduction_depth;
20643 /* type_unification_real will pass back any access checks from default
20644 template argument substitution. */
20645 vec<deferred_access_check, va_gc> *checks = NULL;
20646 /* We don't have all the template args yet. */
20647 bool incomplete = true;
20648
20649 tree orig_fn = fn;
20650 if (flag_new_inheriting_ctors)
20651 fn = strip_inheriting_ctors (fn);
20652
20653 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20654 tree r = error_mark_node;
20655
20656 tree full_targs = targs;
20657 if (TMPL_ARGS_DEPTH (targs)
20658 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20659 full_targs = (add_outermost_template_args
20660 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20661 targs));
20662
20663 if (decltype_p)
20664 complain |= tf_decltype;
20665
20666 /* In C++0x, it's possible to have a function template whose type depends
20667 on itself recursively. This is most obvious with decltype, but can also
20668 occur with enumeration scope (c++/48969). So we need to catch infinite
20669 recursion and reject the substitution at deduction time; this function
20670 will return error_mark_node for any repeated substitution.
20671
20672 This also catches excessive recursion such as when f<N> depends on
20673 f<N-1> across all integers, and returns error_mark_node for all the
20674 substitutions back up to the initial one.
20675
20676 This is, of course, not reentrant. */
20677 if (excessive_deduction_depth)
20678 return error_mark_node;
20679 ++deduction_depth;
20680
20681 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20682
20683 fntype = TREE_TYPE (fn);
20684 if (explicit_targs)
20685 {
20686 /* [temp.deduct]
20687
20688 The specified template arguments must match the template
20689 parameters in kind (i.e., type, nontype, template), and there
20690 must not be more arguments than there are parameters;
20691 otherwise type deduction fails.
20692
20693 Nontype arguments must match the types of the corresponding
20694 nontype template parameters, or must be convertible to the
20695 types of the corresponding nontype parameters as specified in
20696 _temp.arg.nontype_, otherwise type deduction fails.
20697
20698 All references in the function type of the function template
20699 to the corresponding template parameters are replaced by the
20700 specified template argument values. If a substitution in a
20701 template parameter or in the function type of the function
20702 template results in an invalid type, type deduction fails. */
20703 int i, len = TREE_VEC_LENGTH (tparms);
20704 location_t loc = input_location;
20705 incomplete = false;
20706
20707 if (explicit_targs == error_mark_node)
20708 goto fail;
20709
20710 if (TMPL_ARGS_DEPTH (explicit_targs)
20711 < TMPL_ARGS_DEPTH (full_targs))
20712 explicit_targs = add_outermost_template_args (full_targs,
20713 explicit_targs);
20714
20715 /* Adjust any explicit template arguments before entering the
20716 substitution context. */
20717 explicit_targs
20718 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20719 complain|tf_partial,
20720 /*require_all_args=*/false,
20721 /*use_default_args=*/false));
20722 if (explicit_targs == error_mark_node)
20723 goto fail;
20724
20725 /* Substitute the explicit args into the function type. This is
20726 necessary so that, for instance, explicitly declared function
20727 arguments can match null pointed constants. If we were given
20728 an incomplete set of explicit args, we must not do semantic
20729 processing during substitution as we could create partial
20730 instantiations. */
20731 for (i = 0; i < len; i++)
20732 {
20733 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20734 bool parameter_pack = false;
20735 tree targ = TREE_VEC_ELT (explicit_targs, i);
20736
20737 /* Dig out the actual parm. */
20738 if (TREE_CODE (parm) == TYPE_DECL
20739 || TREE_CODE (parm) == TEMPLATE_DECL)
20740 {
20741 parm = TREE_TYPE (parm);
20742 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20743 }
20744 else if (TREE_CODE (parm) == PARM_DECL)
20745 {
20746 parm = DECL_INITIAL (parm);
20747 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20748 }
20749
20750 if (targ == NULL_TREE)
20751 /* No explicit argument for this template parameter. */
20752 incomplete = true;
20753 else if (parameter_pack && pack_deducible_p (parm, fn))
20754 {
20755 /* Mark the argument pack as "incomplete". We could
20756 still deduce more arguments during unification.
20757 We remove this mark in type_unification_real. */
20758 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20759 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20760 = ARGUMENT_PACK_ARGS (targ);
20761
20762 /* We have some incomplete argument packs. */
20763 incomplete = true;
20764 }
20765 }
20766
20767 if (incomplete)
20768 {
20769 if (!push_tinst_level (fn, explicit_targs))
20770 {
20771 excessive_deduction_depth = true;
20772 goto fail;
20773 }
20774 ++processing_template_decl;
20775 input_location = DECL_SOURCE_LOCATION (fn);
20776 /* Ignore any access checks; we'll see them again in
20777 instantiate_template and they might have the wrong
20778 access path at this point. */
20779 push_deferring_access_checks (dk_deferred);
20780 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20781 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20782 pop_deferring_access_checks ();
20783 input_location = loc;
20784 --processing_template_decl;
20785 pop_tinst_level ();
20786
20787 if (fntype == error_mark_node)
20788 goto fail;
20789 }
20790
20791 /* Place the explicitly specified arguments in TARGS. */
20792 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20793 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20794 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20795 if (!incomplete && CHECKING_P
20796 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20797 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20798 (targs, NUM_TMPL_ARGS (explicit_targs));
20799 }
20800
20801 if (return_type && strict != DEDUCE_CALL)
20802 {
20803 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20804 new_args[0] = return_type;
20805 memcpy (new_args + 1, args, nargs * sizeof (tree));
20806 args = new_args;
20807 ++nargs;
20808 }
20809
20810 if (!incomplete)
20811 goto deduced;
20812
20813 /* Never do unification on the 'this' parameter. */
20814 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20815
20816 if (return_type && strict == DEDUCE_CALL)
20817 {
20818 /* We're deducing for a call to the result of a template conversion
20819 function. The parms we really want are in return_type. */
20820 if (INDIRECT_TYPE_P (return_type))
20821 return_type = TREE_TYPE (return_type);
20822 parms = TYPE_ARG_TYPES (return_type);
20823 }
20824 else if (return_type)
20825 {
20826 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20827 }
20828
20829 /* We allow incomplete unification without an error message here
20830 because the standard doesn't seem to explicitly prohibit it. Our
20831 callers must be ready to deal with unification failures in any
20832 event. */
20833
20834 /* If we aren't explaining yet, push tinst context so we can see where
20835 any errors (e.g. from class instantiations triggered by instantiation
20836 of default template arguments) come from. If we are explaining, this
20837 context is redundant. */
20838 if (!explain_p && !push_tinst_level (fn, targs))
20839 {
20840 excessive_deduction_depth = true;
20841 goto fail;
20842 }
20843
20844 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20845 full_targs, parms, args, nargs, /*subr=*/0,
20846 strict, &checks, explain_p);
20847 if (!explain_p)
20848 pop_tinst_level ();
20849 if (!ok)
20850 goto fail;
20851
20852 /* Now that we have bindings for all of the template arguments,
20853 ensure that the arguments deduced for the template template
20854 parameters have compatible template parameter lists. We cannot
20855 check this property before we have deduced all template
20856 arguments, because the template parameter types of a template
20857 template parameter might depend on prior template parameters
20858 deduced after the template template parameter. The following
20859 ill-formed example illustrates this issue:
20860
20861 template<typename T, template<T> class C> void f(C<5>, T);
20862
20863 template<int N> struct X {};
20864
20865 void g() {
20866 f(X<5>(), 5l); // error: template argument deduction fails
20867 }
20868
20869 The template parameter list of 'C' depends on the template type
20870 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20871 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20872 time that we deduce 'C'. */
20873 if (!template_template_parm_bindings_ok_p
20874 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20875 {
20876 unify_inconsistent_template_template_parameters (explain_p);
20877 goto fail;
20878 }
20879
20880 /* DR 1391: All parameters have args, now check non-dependent parms for
20881 convertibility. */
20882 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20883 convs, explain_p))
20884 goto fail;
20885
20886 deduced:
20887 /* All is well so far. Now, check:
20888
20889 [temp.deduct]
20890
20891 When all template arguments have been deduced, all uses of
20892 template parameters in nondeduced contexts are replaced with
20893 the corresponding deduced argument values. If the
20894 substitution results in an invalid type, as described above,
20895 type deduction fails. */
20896 if (!push_tinst_level (fn, targs))
20897 {
20898 excessive_deduction_depth = true;
20899 goto fail;
20900 }
20901
20902 /* Also collect access checks from the instantiation. */
20903 reopen_deferring_access_checks (checks);
20904
20905 decl = instantiate_template (fn, targs, complain);
20906
20907 checks = get_deferred_access_checks ();
20908 pop_deferring_access_checks ();
20909
20910 pop_tinst_level ();
20911
20912 if (decl == error_mark_node)
20913 goto fail;
20914
20915 /* Now perform any access checks encountered during substitution. */
20916 push_access_scope (decl);
20917 ok = perform_access_checks (checks, complain);
20918 pop_access_scope (decl);
20919 if (!ok)
20920 goto fail;
20921
20922 /* If we're looking for an exact match, check that what we got
20923 is indeed an exact match. It might not be if some template
20924 parameters are used in non-deduced contexts. But don't check
20925 for an exact match if we have dependent template arguments;
20926 in that case we're doing partial ordering, and we already know
20927 that we have two candidates that will provide the actual type. */
20928 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20929 {
20930 tree substed = TREE_TYPE (decl);
20931 unsigned int i;
20932
20933 tree sarg
20934 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20935 if (return_type)
20936 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20937 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20938 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20939 {
20940 unify_type_mismatch (explain_p, args[i],
20941 TREE_VALUE (sarg));
20942 goto fail;
20943 }
20944 }
20945
20946 /* After doing deduction with the inherited constructor, actually return an
20947 instantiation of the inheriting constructor. */
20948 if (orig_fn != fn)
20949 decl = instantiate_template (orig_fn, targs, complain);
20950
20951 r = decl;
20952
20953 fail:
20954 --deduction_depth;
20955 if (excessive_deduction_depth)
20956 {
20957 if (deduction_depth == 0)
20958 /* Reset once we're all the way out. */
20959 excessive_deduction_depth = false;
20960 }
20961
20962 return r;
20963 }
20964
20965 /* Adjust types before performing type deduction, as described in
20966 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20967 sections are symmetric. PARM is the type of a function parameter
20968 or the return type of the conversion function. ARG is the type of
20969 the argument passed to the call, or the type of the value
20970 initialized with the result of the conversion function.
20971 ARG_EXPR is the original argument expression, which may be null. */
20972
20973 static int
20974 maybe_adjust_types_for_deduction (unification_kind_t strict,
20975 tree* parm,
20976 tree* arg,
20977 tree arg_expr)
20978 {
20979 int result = 0;
20980
20981 switch (strict)
20982 {
20983 case DEDUCE_CALL:
20984 break;
20985
20986 case DEDUCE_CONV:
20987 /* Swap PARM and ARG throughout the remainder of this
20988 function; the handling is precisely symmetric since PARM
20989 will initialize ARG rather than vice versa. */
20990 std::swap (parm, arg);
20991 break;
20992
20993 case DEDUCE_EXACT:
20994 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20995 too, but here handle it by stripping the reference from PARM
20996 rather than by adding it to ARG. */
20997 if (TYPE_REF_P (*parm)
20998 && TYPE_REF_IS_RVALUE (*parm)
20999 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21000 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21001 && TYPE_REF_P (*arg)
21002 && !TYPE_REF_IS_RVALUE (*arg))
21003 *parm = TREE_TYPE (*parm);
21004 /* Nothing else to do in this case. */
21005 return 0;
21006
21007 default:
21008 gcc_unreachable ();
21009 }
21010
21011 if (!TYPE_REF_P (*parm))
21012 {
21013 /* [temp.deduct.call]
21014
21015 If P is not a reference type:
21016
21017 --If A is an array type, the pointer type produced by the
21018 array-to-pointer standard conversion (_conv.array_) is
21019 used in place of A for type deduction; otherwise,
21020
21021 --If A is a function type, the pointer type produced by
21022 the function-to-pointer standard conversion
21023 (_conv.func_) is used in place of A for type deduction;
21024 otherwise,
21025
21026 --If A is a cv-qualified type, the top level
21027 cv-qualifiers of A's type are ignored for type
21028 deduction. */
21029 if (TREE_CODE (*arg) == ARRAY_TYPE)
21030 *arg = build_pointer_type (TREE_TYPE (*arg));
21031 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21032 *arg = build_pointer_type (*arg);
21033 else
21034 *arg = TYPE_MAIN_VARIANT (*arg);
21035 }
21036
21037 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21038 reference to a cv-unqualified template parameter that does not represent a
21039 template parameter of a class template (during class template argument
21040 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21041 an lvalue, the type "lvalue reference to A" is used in place of A for type
21042 deduction. */
21043 if (TYPE_REF_P (*parm)
21044 && TYPE_REF_IS_RVALUE (*parm)
21045 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21046 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21047 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21048 && (arg_expr ? lvalue_p (arg_expr)
21049 /* try_one_overload doesn't provide an arg_expr, but
21050 functions are always lvalues. */
21051 : TREE_CODE (*arg) == FUNCTION_TYPE))
21052 *arg = build_reference_type (*arg);
21053
21054 /* [temp.deduct.call]
21055
21056 If P is a cv-qualified type, the top level cv-qualifiers
21057 of P's type are ignored for type deduction. If P is a
21058 reference type, the type referred to by P is used for
21059 type deduction. */
21060 *parm = TYPE_MAIN_VARIANT (*parm);
21061 if (TYPE_REF_P (*parm))
21062 {
21063 *parm = TREE_TYPE (*parm);
21064 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21065 }
21066
21067 /* DR 322. For conversion deduction, remove a reference type on parm
21068 too (which has been swapped into ARG). */
21069 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21070 *arg = TREE_TYPE (*arg);
21071
21072 return result;
21073 }
21074
21075 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21076 template which doesn't contain any deducible template parameters; check if
21077 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21078 unify_one_argument. */
21079
21080 static int
21081 check_non_deducible_conversion (tree parm, tree arg, int strict,
21082 int flags, struct conversion **conv_p,
21083 bool explain_p)
21084 {
21085 tree type;
21086
21087 if (!TYPE_P (arg))
21088 type = TREE_TYPE (arg);
21089 else
21090 type = arg;
21091
21092 if (same_type_p (parm, type))
21093 return unify_success (explain_p);
21094
21095 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21096 if (strict == DEDUCE_CONV)
21097 {
21098 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21099 return unify_success (explain_p);
21100 }
21101 else if (strict != DEDUCE_EXACT)
21102 {
21103 bool ok = false;
21104 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21105 if (conv_p)
21106 /* Avoid recalculating this in add_function_candidate. */
21107 ok = (*conv_p
21108 = good_conversion (parm, type, conv_arg, flags, complain));
21109 else
21110 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21111 if (ok)
21112 return unify_success (explain_p);
21113 }
21114
21115 if (strict == DEDUCE_EXACT)
21116 return unify_type_mismatch (explain_p, parm, arg);
21117 else
21118 return unify_arg_conversion (explain_p, parm, type, arg);
21119 }
21120
21121 static bool uses_deducible_template_parms (tree type);
21122
21123 /* Returns true iff the expression EXPR is one from which a template
21124 argument can be deduced. In other words, if it's an undecorated
21125 use of a template non-type parameter. */
21126
21127 static bool
21128 deducible_expression (tree expr)
21129 {
21130 /* Strip implicit conversions. */
21131 while (CONVERT_EXPR_P (expr))
21132 expr = TREE_OPERAND (expr, 0);
21133 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21134 }
21135
21136 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21137 deducible way; that is, if it has a max value of <PARM> - 1. */
21138
21139 static bool
21140 deducible_array_bound (tree domain)
21141 {
21142 if (domain == NULL_TREE)
21143 return false;
21144
21145 tree max = TYPE_MAX_VALUE (domain);
21146 if (TREE_CODE (max) != MINUS_EXPR)
21147 return false;
21148
21149 return deducible_expression (TREE_OPERAND (max, 0));
21150 }
21151
21152 /* Returns true iff the template arguments ARGS use a template parameter
21153 in a deducible way. */
21154
21155 static bool
21156 deducible_template_args (tree args)
21157 {
21158 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21159 {
21160 bool deducible;
21161 tree elt = TREE_VEC_ELT (args, i);
21162 if (ARGUMENT_PACK_P (elt))
21163 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21164 else
21165 {
21166 if (PACK_EXPANSION_P (elt))
21167 elt = PACK_EXPANSION_PATTERN (elt);
21168 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21169 deducible = true;
21170 else if (TYPE_P (elt))
21171 deducible = uses_deducible_template_parms (elt);
21172 else
21173 deducible = deducible_expression (elt);
21174 }
21175 if (deducible)
21176 return true;
21177 }
21178 return false;
21179 }
21180
21181 /* Returns true iff TYPE contains any deducible references to template
21182 parameters, as per 14.8.2.5. */
21183
21184 static bool
21185 uses_deducible_template_parms (tree type)
21186 {
21187 if (PACK_EXPANSION_P (type))
21188 type = PACK_EXPANSION_PATTERN (type);
21189
21190 /* T
21191 cv-list T
21192 TT<T>
21193 TT<i>
21194 TT<> */
21195 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21196 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21197 return true;
21198
21199 /* T*
21200 T&
21201 T&& */
21202 if (INDIRECT_TYPE_P (type))
21203 return uses_deducible_template_parms (TREE_TYPE (type));
21204
21205 /* T[integer-constant ]
21206 type [i] */
21207 if (TREE_CODE (type) == ARRAY_TYPE)
21208 return (uses_deducible_template_parms (TREE_TYPE (type))
21209 || deducible_array_bound (TYPE_DOMAIN (type)));
21210
21211 /* T type ::*
21212 type T::*
21213 T T::*
21214 T (type ::*)()
21215 type (T::*)()
21216 type (type ::*)(T)
21217 type (T::*)(T)
21218 T (type ::*)(T)
21219 T (T::*)()
21220 T (T::*)(T) */
21221 if (TYPE_PTRMEM_P (type))
21222 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21223 || (uses_deducible_template_parms
21224 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21225
21226 /* template-name <T> (where template-name refers to a class template)
21227 template-name <i> (where template-name refers to a class template) */
21228 if (CLASS_TYPE_P (type)
21229 && CLASSTYPE_TEMPLATE_INFO (type)
21230 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21231 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21232 (CLASSTYPE_TI_ARGS (type)));
21233
21234 /* type (T)
21235 T()
21236 T(T) */
21237 if (FUNC_OR_METHOD_TYPE_P (type))
21238 {
21239 if (uses_deducible_template_parms (TREE_TYPE (type)))
21240 return true;
21241 tree parm = TYPE_ARG_TYPES (type);
21242 if (TREE_CODE (type) == METHOD_TYPE)
21243 parm = TREE_CHAIN (parm);
21244 for (; parm; parm = TREE_CHAIN (parm))
21245 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21246 return true;
21247 }
21248
21249 return false;
21250 }
21251
21252 /* Subroutine of type_unification_real and unify_pack_expansion to
21253 handle unification of a single P/A pair. Parameters are as
21254 for those functions. */
21255
21256 static int
21257 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21258 int subr, unification_kind_t strict,
21259 bool explain_p)
21260 {
21261 tree arg_expr = NULL_TREE;
21262 int arg_strict;
21263
21264 if (arg == error_mark_node || parm == error_mark_node)
21265 return unify_invalid (explain_p);
21266 if (arg == unknown_type_node)
21267 /* We can't deduce anything from this, but we might get all the
21268 template args from other function args. */
21269 return unify_success (explain_p);
21270
21271 /* Implicit conversions (Clause 4) will be performed on a function
21272 argument to convert it to the type of the corresponding function
21273 parameter if the parameter type contains no template-parameters that
21274 participate in template argument deduction. */
21275 if (strict != DEDUCE_EXACT
21276 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21277 /* For function parameters with no deducible template parameters,
21278 just return. We'll check non-dependent conversions later. */
21279 return unify_success (explain_p);
21280
21281 switch (strict)
21282 {
21283 case DEDUCE_CALL:
21284 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21285 | UNIFY_ALLOW_MORE_CV_QUAL
21286 | UNIFY_ALLOW_DERIVED);
21287 break;
21288
21289 case DEDUCE_CONV:
21290 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21291 break;
21292
21293 case DEDUCE_EXACT:
21294 arg_strict = UNIFY_ALLOW_NONE;
21295 break;
21296
21297 default:
21298 gcc_unreachable ();
21299 }
21300
21301 /* We only do these transformations if this is the top-level
21302 parameter_type_list in a call or declaration matching; in other
21303 situations (nested function declarators, template argument lists) we
21304 won't be comparing a type to an expression, and we don't do any type
21305 adjustments. */
21306 if (!subr)
21307 {
21308 if (!TYPE_P (arg))
21309 {
21310 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21311 if (type_unknown_p (arg))
21312 {
21313 /* [temp.deduct.type] A template-argument can be
21314 deduced from a pointer to function or pointer
21315 to member function argument if the set of
21316 overloaded functions does not contain function
21317 templates and at most one of a set of
21318 overloaded functions provides a unique
21319 match. */
21320 resolve_overloaded_unification (tparms, targs, parm,
21321 arg, strict,
21322 arg_strict, explain_p);
21323 /* If a unique match was not found, this is a
21324 non-deduced context, so we still succeed. */
21325 return unify_success (explain_p);
21326 }
21327
21328 arg_expr = arg;
21329 arg = unlowered_expr_type (arg);
21330 if (arg == error_mark_node)
21331 return unify_invalid (explain_p);
21332 }
21333
21334 arg_strict |=
21335 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21336 }
21337 else
21338 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21339 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21340 return unify_template_argument_mismatch (explain_p, parm, arg);
21341
21342 /* For deduction from an init-list we need the actual list. */
21343 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21344 arg = arg_expr;
21345 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21346 }
21347
21348 /* for_each_template_parm callback that always returns 0. */
21349
21350 static int
21351 zero_r (tree, void *)
21352 {
21353 return 0;
21354 }
21355
21356 /* for_each_template_parm any_fn callback to handle deduction of a template
21357 type argument from the type of an array bound. */
21358
21359 static int
21360 array_deduction_r (tree t, void *data)
21361 {
21362 tree_pair_p d = (tree_pair_p)data;
21363 tree &tparms = d->purpose;
21364 tree &targs = d->value;
21365
21366 if (TREE_CODE (t) == ARRAY_TYPE)
21367 if (tree dom = TYPE_DOMAIN (t))
21368 if (tree max = TYPE_MAX_VALUE (dom))
21369 {
21370 if (TREE_CODE (max) == MINUS_EXPR)
21371 max = TREE_OPERAND (max, 0);
21372 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21373 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21374 UNIFY_ALLOW_NONE, /*explain*/false);
21375 }
21376
21377 /* Keep walking. */
21378 return 0;
21379 }
21380
21381 /* Try to deduce any not-yet-deduced template type arguments from the type of
21382 an array bound. This is handled separately from unify because 14.8.2.5 says
21383 "The type of a type parameter is only deduced from an array bound if it is
21384 not otherwise deduced." */
21385
21386 static void
21387 try_array_deduction (tree tparms, tree targs, tree parm)
21388 {
21389 tree_pair_s data = { tparms, targs };
21390 hash_set<tree> visited;
21391 for_each_template_parm (parm, zero_r, &data, &visited,
21392 /*nondeduced*/false, array_deduction_r);
21393 }
21394
21395 /* Most parms like fn_type_unification.
21396
21397 If SUBR is 1, we're being called recursively (to unify the
21398 arguments of a function or method parameter of a function
21399 template).
21400
21401 CHECKS is a pointer to a vector of access checks encountered while
21402 substituting default template arguments. */
21403
21404 static int
21405 type_unification_real (tree tparms,
21406 tree full_targs,
21407 tree xparms,
21408 const tree *xargs,
21409 unsigned int xnargs,
21410 int subr,
21411 unification_kind_t strict,
21412 vec<deferred_access_check, va_gc> **checks,
21413 bool explain_p)
21414 {
21415 tree parm, arg;
21416 int i;
21417 int ntparms = TREE_VEC_LENGTH (tparms);
21418 int saw_undeduced = 0;
21419 tree parms;
21420 const tree *args;
21421 unsigned int nargs;
21422 unsigned int ia;
21423
21424 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21425 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21426 gcc_assert (ntparms > 0);
21427
21428 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21429
21430 /* Reset the number of non-defaulted template arguments contained
21431 in TARGS. */
21432 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21433
21434 again:
21435 parms = xparms;
21436 args = xargs;
21437 nargs = xnargs;
21438
21439 ia = 0;
21440 while (parms && parms != void_list_node
21441 && ia < nargs)
21442 {
21443 parm = TREE_VALUE (parms);
21444
21445 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21446 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21447 /* For a function parameter pack that occurs at the end of the
21448 parameter-declaration-list, the type A of each remaining
21449 argument of the call is compared with the type P of the
21450 declarator-id of the function parameter pack. */
21451 break;
21452
21453 parms = TREE_CHAIN (parms);
21454
21455 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21456 /* For a function parameter pack that does not occur at the
21457 end of the parameter-declaration-list, the type of the
21458 parameter pack is a non-deduced context. */
21459 continue;
21460
21461 arg = args[ia];
21462 ++ia;
21463
21464 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21465 explain_p))
21466 return 1;
21467 }
21468
21469 if (parms
21470 && parms != void_list_node
21471 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21472 {
21473 /* Unify the remaining arguments with the pack expansion type. */
21474 tree argvec;
21475 tree parmvec = make_tree_vec (1);
21476
21477 /* Allocate a TREE_VEC and copy in all of the arguments */
21478 argvec = make_tree_vec (nargs - ia);
21479 for (i = 0; ia < nargs; ++ia, ++i)
21480 TREE_VEC_ELT (argvec, i) = args[ia];
21481
21482 /* Copy the parameter into parmvec. */
21483 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21484 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21485 /*subr=*/subr, explain_p))
21486 return 1;
21487
21488 /* Advance to the end of the list of parameters. */
21489 parms = TREE_CHAIN (parms);
21490 }
21491
21492 /* Fail if we've reached the end of the parm list, and more args
21493 are present, and the parm list isn't variadic. */
21494 if (ia < nargs && parms == void_list_node)
21495 return unify_too_many_arguments (explain_p, nargs, ia);
21496 /* Fail if parms are left and they don't have default values and
21497 they aren't all deduced as empty packs (c++/57397). This is
21498 consistent with sufficient_parms_p. */
21499 if (parms && parms != void_list_node
21500 && TREE_PURPOSE (parms) == NULL_TREE)
21501 {
21502 unsigned int count = nargs;
21503 tree p = parms;
21504 bool type_pack_p;
21505 do
21506 {
21507 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21508 if (!type_pack_p)
21509 count++;
21510 p = TREE_CHAIN (p);
21511 }
21512 while (p && p != void_list_node);
21513 if (count != nargs)
21514 return unify_too_few_arguments (explain_p, ia, count,
21515 type_pack_p);
21516 }
21517
21518 if (!subr)
21519 {
21520 tsubst_flags_t complain = (explain_p
21521 ? tf_warning_or_error
21522 : tf_none);
21523 bool tried_array_deduction = (cxx_dialect < cxx17);
21524
21525 for (i = 0; i < ntparms; i++)
21526 {
21527 tree targ = TREE_VEC_ELT (targs, i);
21528 tree tparm = TREE_VEC_ELT (tparms, i);
21529
21530 /* Clear the "incomplete" flags on all argument packs now so that
21531 substituting them into later default arguments works. */
21532 if (targ && ARGUMENT_PACK_P (targ))
21533 {
21534 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21535 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21536 }
21537
21538 if (targ || tparm == error_mark_node)
21539 continue;
21540 tparm = TREE_VALUE (tparm);
21541
21542 if (TREE_CODE (tparm) == TYPE_DECL
21543 && !tried_array_deduction)
21544 {
21545 try_array_deduction (tparms, targs, xparms);
21546 tried_array_deduction = true;
21547 if (TREE_VEC_ELT (targs, i))
21548 continue;
21549 }
21550
21551 /* If this is an undeduced nontype parameter that depends on
21552 a type parameter, try another pass; its type may have been
21553 deduced from a later argument than the one from which
21554 this parameter can be deduced. */
21555 if (TREE_CODE (tparm) == PARM_DECL
21556 && uses_template_parms (TREE_TYPE (tparm))
21557 && saw_undeduced < 2)
21558 {
21559 saw_undeduced = 1;
21560 continue;
21561 }
21562
21563 /* Core issue #226 (C++0x) [temp.deduct]:
21564
21565 If a template argument has not been deduced, its
21566 default template argument, if any, is used.
21567
21568 When we are in C++98 mode, TREE_PURPOSE will either
21569 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21570 to explicitly check cxx_dialect here. */
21571 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21572 /* OK, there is a default argument. Wait until after the
21573 conversion check to do substitution. */
21574 continue;
21575
21576 /* If the type parameter is a parameter pack, then it will
21577 be deduced to an empty parameter pack. */
21578 if (template_parameter_pack_p (tparm))
21579 {
21580 tree arg;
21581
21582 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21583 {
21584 arg = make_node (NONTYPE_ARGUMENT_PACK);
21585 TREE_CONSTANT (arg) = 1;
21586 }
21587 else
21588 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21589
21590 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21591
21592 TREE_VEC_ELT (targs, i) = arg;
21593 continue;
21594 }
21595
21596 return unify_parameter_deduction_failure (explain_p, tparm);
21597 }
21598
21599 /* Now substitute into the default template arguments. */
21600 for (i = 0; i < ntparms; i++)
21601 {
21602 tree targ = TREE_VEC_ELT (targs, i);
21603 tree tparm = TREE_VEC_ELT (tparms, i);
21604
21605 if (targ || tparm == error_mark_node)
21606 continue;
21607 tree parm = TREE_VALUE (tparm);
21608 tree arg = TREE_PURPOSE (tparm);
21609 reopen_deferring_access_checks (*checks);
21610 location_t save_loc = input_location;
21611 if (DECL_P (parm))
21612 input_location = DECL_SOURCE_LOCATION (parm);
21613
21614 if (saw_undeduced == 1
21615 && TREE_CODE (parm) == PARM_DECL
21616 && uses_template_parms (TREE_TYPE (parm)))
21617 {
21618 /* The type of this non-type parameter depends on undeduced
21619 parameters. Don't try to use its default argument yet,
21620 since we might deduce an argument for it on the next pass,
21621 but do check whether the arguments we already have cause
21622 substitution failure, so that that happens before we try
21623 later default arguments (78489). */
21624 ++processing_template_decl;
21625 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21626 NULL_TREE);
21627 --processing_template_decl;
21628 if (type == error_mark_node)
21629 arg = error_mark_node;
21630 else
21631 arg = NULL_TREE;
21632 }
21633 else
21634 {
21635 /* Even if the call is happening in template context, getting
21636 here means it's non-dependent, and a default argument is
21637 considered a separate definition under [temp.decls], so we can
21638 do this substitution without processing_template_decl. This
21639 is important if the default argument contains something that
21640 might be instantiation-dependent like access (87480). */
21641 processing_template_decl_sentinel s;
21642 tree substed = NULL_TREE;
21643 if (saw_undeduced == 1)
21644 {
21645 /* First instatiate in template context, in case we still
21646 depend on undeduced template parameters. */
21647 ++processing_template_decl;
21648 substed = tsubst_template_arg (arg, full_targs, complain,
21649 NULL_TREE);
21650 --processing_template_decl;
21651 if (substed != error_mark_node
21652 && !uses_template_parms (substed))
21653 /* We replaced all the tparms, substitute again out of
21654 template context. */
21655 substed = NULL_TREE;
21656 }
21657 if (!substed)
21658 substed = tsubst_template_arg (arg, full_targs, complain,
21659 NULL_TREE);
21660
21661 if (!uses_template_parms (substed))
21662 arg = convert_template_argument (parm, substed, full_targs,
21663 complain, i, NULL_TREE);
21664 else if (saw_undeduced == 1)
21665 arg = NULL_TREE;
21666 else
21667 arg = error_mark_node;
21668 }
21669
21670 input_location = save_loc;
21671 *checks = get_deferred_access_checks ();
21672 pop_deferring_access_checks ();
21673
21674 if (arg == error_mark_node)
21675 return 1;
21676 else if (arg)
21677 {
21678 TREE_VEC_ELT (targs, i) = arg;
21679 /* The position of the first default template argument,
21680 is also the number of non-defaulted arguments in TARGS.
21681 Record that. */
21682 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21683 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21684 }
21685 }
21686
21687 if (saw_undeduced++ == 1)
21688 goto again;
21689 }
21690
21691 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21692 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21693
21694 return unify_success (explain_p);
21695 }
21696
21697 /* Subroutine of type_unification_real. Args are like the variables
21698 at the call site. ARG is an overloaded function (or template-id);
21699 we try deducing template args from each of the overloads, and if
21700 only one succeeds, we go with that. Modifies TARGS and returns
21701 true on success. */
21702
21703 static bool
21704 resolve_overloaded_unification (tree tparms,
21705 tree targs,
21706 tree parm,
21707 tree arg,
21708 unification_kind_t strict,
21709 int sub_strict,
21710 bool explain_p)
21711 {
21712 tree tempargs = copy_node (targs);
21713 int good = 0;
21714 tree goodfn = NULL_TREE;
21715 bool addr_p;
21716
21717 if (TREE_CODE (arg) == ADDR_EXPR)
21718 {
21719 arg = TREE_OPERAND (arg, 0);
21720 addr_p = true;
21721 }
21722 else
21723 addr_p = false;
21724
21725 if (TREE_CODE (arg) == COMPONENT_REF)
21726 /* Handle `&x' where `x' is some static or non-static member
21727 function name. */
21728 arg = TREE_OPERAND (arg, 1);
21729
21730 if (TREE_CODE (arg) == OFFSET_REF)
21731 arg = TREE_OPERAND (arg, 1);
21732
21733 /* Strip baselink information. */
21734 if (BASELINK_P (arg))
21735 arg = BASELINK_FUNCTIONS (arg);
21736
21737 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21738 {
21739 /* If we got some explicit template args, we need to plug them into
21740 the affected templates before we try to unify, in case the
21741 explicit args will completely resolve the templates in question. */
21742
21743 int ok = 0;
21744 tree expl_subargs = TREE_OPERAND (arg, 1);
21745 arg = TREE_OPERAND (arg, 0);
21746
21747 for (lkp_iterator iter (arg); iter; ++iter)
21748 {
21749 tree fn = *iter;
21750 tree subargs, elem;
21751
21752 if (TREE_CODE (fn) != TEMPLATE_DECL)
21753 continue;
21754
21755 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21756 expl_subargs, NULL_TREE, tf_none,
21757 /*require_all_args=*/true,
21758 /*use_default_args=*/true);
21759 if (subargs != error_mark_node
21760 && !any_dependent_template_arguments_p (subargs))
21761 {
21762 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21763 if (try_one_overload (tparms, targs, tempargs, parm,
21764 elem, strict, sub_strict, addr_p, explain_p)
21765 && (!goodfn || !same_type_p (goodfn, elem)))
21766 {
21767 goodfn = elem;
21768 ++good;
21769 }
21770 }
21771 else if (subargs)
21772 ++ok;
21773 }
21774 /* If no templates (or more than one) are fully resolved by the
21775 explicit arguments, this template-id is a non-deduced context; it
21776 could still be OK if we deduce all template arguments for the
21777 enclosing call through other arguments. */
21778 if (good != 1)
21779 good = ok;
21780 }
21781 else if (!OVL_P (arg))
21782 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21783 -- but the deduction does not succeed because the expression is
21784 not just the function on its own. */
21785 return false;
21786 else
21787 for (lkp_iterator iter (arg); iter; ++iter)
21788 {
21789 tree fn = *iter;
21790 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21791 strict, sub_strict, addr_p, explain_p)
21792 && (!goodfn || !decls_match (goodfn, fn)))
21793 {
21794 goodfn = fn;
21795 ++good;
21796 }
21797 }
21798
21799 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21800 to function or pointer to member function argument if the set of
21801 overloaded functions does not contain function templates and at most
21802 one of a set of overloaded functions provides a unique match.
21803
21804 So if we found multiple possibilities, we return success but don't
21805 deduce anything. */
21806
21807 if (good == 1)
21808 {
21809 int i = TREE_VEC_LENGTH (targs);
21810 for (; i--; )
21811 if (TREE_VEC_ELT (tempargs, i))
21812 {
21813 tree old = TREE_VEC_ELT (targs, i);
21814 tree new_ = TREE_VEC_ELT (tempargs, i);
21815 if (new_ && old && ARGUMENT_PACK_P (old)
21816 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21817 /* Don't forget explicit template arguments in a pack. */
21818 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21819 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21820 TREE_VEC_ELT (targs, i) = new_;
21821 }
21822 }
21823 if (good)
21824 return true;
21825
21826 return false;
21827 }
21828
21829 /* Core DR 115: In contexts where deduction is done and fails, or in
21830 contexts where deduction is not done, if a template argument list is
21831 specified and it, along with any default template arguments, identifies
21832 a single function template specialization, then the template-id is an
21833 lvalue for the function template specialization. */
21834
21835 tree
21836 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21837 {
21838 tree expr, offset, baselink;
21839 bool addr;
21840
21841 if (!type_unknown_p (orig_expr))
21842 return orig_expr;
21843
21844 expr = orig_expr;
21845 addr = false;
21846 offset = NULL_TREE;
21847 baselink = NULL_TREE;
21848
21849 if (TREE_CODE (expr) == ADDR_EXPR)
21850 {
21851 expr = TREE_OPERAND (expr, 0);
21852 addr = true;
21853 }
21854 if (TREE_CODE (expr) == OFFSET_REF)
21855 {
21856 offset = expr;
21857 expr = TREE_OPERAND (expr, 1);
21858 }
21859 if (BASELINK_P (expr))
21860 {
21861 baselink = expr;
21862 expr = BASELINK_FUNCTIONS (expr);
21863 }
21864
21865 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21866 {
21867 int good = 0;
21868 tree goodfn = NULL_TREE;
21869
21870 /* If we got some explicit template args, we need to plug them into
21871 the affected templates before we try to unify, in case the
21872 explicit args will completely resolve the templates in question. */
21873
21874 tree expl_subargs = TREE_OPERAND (expr, 1);
21875 tree arg = TREE_OPERAND (expr, 0);
21876 tree badfn = NULL_TREE;
21877 tree badargs = NULL_TREE;
21878
21879 for (lkp_iterator iter (arg); iter; ++iter)
21880 {
21881 tree fn = *iter;
21882 tree subargs, elem;
21883
21884 if (TREE_CODE (fn) != TEMPLATE_DECL)
21885 continue;
21886
21887 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21888 expl_subargs, NULL_TREE, tf_none,
21889 /*require_all_args=*/true,
21890 /*use_default_args=*/true);
21891 if (subargs != error_mark_node
21892 && !any_dependent_template_arguments_p (subargs))
21893 {
21894 elem = instantiate_template (fn, subargs, tf_none);
21895 if (elem == error_mark_node)
21896 {
21897 badfn = fn;
21898 badargs = subargs;
21899 }
21900 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21901 {
21902 goodfn = elem;
21903 ++good;
21904 }
21905 }
21906 }
21907 if (good == 1)
21908 {
21909 mark_used (goodfn);
21910 expr = goodfn;
21911 if (baselink)
21912 expr = build_baselink (BASELINK_BINFO (baselink),
21913 BASELINK_ACCESS_BINFO (baselink),
21914 expr, BASELINK_OPTYPE (baselink));
21915 if (offset)
21916 {
21917 tree base
21918 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21919 expr = build_offset_ref (base, expr, addr, complain);
21920 }
21921 if (addr)
21922 expr = cp_build_addr_expr (expr, complain);
21923 return expr;
21924 }
21925 else if (good == 0 && badargs && (complain & tf_error))
21926 /* There were no good options and at least one bad one, so let the
21927 user know what the problem is. */
21928 instantiate_template (badfn, badargs, complain);
21929 }
21930 return orig_expr;
21931 }
21932
21933 /* As above, but error out if the expression remains overloaded. */
21934
21935 tree
21936 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21937 {
21938 exp = resolve_nondeduced_context (exp, complain);
21939 if (type_unknown_p (exp))
21940 {
21941 if (complain & tf_error)
21942 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21943 return error_mark_node;
21944 }
21945 return exp;
21946 }
21947
21948 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21949 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21950 different overloads deduce different arguments for a given parm.
21951 ADDR_P is true if the expression for which deduction is being
21952 performed was of the form "& fn" rather than simply "fn".
21953
21954 Returns 1 on success. */
21955
21956 static int
21957 try_one_overload (tree tparms,
21958 tree orig_targs,
21959 tree targs,
21960 tree parm,
21961 tree arg,
21962 unification_kind_t strict,
21963 int sub_strict,
21964 bool addr_p,
21965 bool explain_p)
21966 {
21967 int nargs;
21968 tree tempargs;
21969 int i;
21970
21971 if (arg == error_mark_node)
21972 return 0;
21973
21974 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21975 to function or pointer to member function argument if the set of
21976 overloaded functions does not contain function templates and at most
21977 one of a set of overloaded functions provides a unique match.
21978
21979 So if this is a template, just return success. */
21980
21981 if (uses_template_parms (arg))
21982 return 1;
21983
21984 if (TREE_CODE (arg) == METHOD_TYPE)
21985 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21986 else if (addr_p)
21987 arg = build_pointer_type (arg);
21988
21989 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21990
21991 /* We don't copy orig_targs for this because if we have already deduced
21992 some template args from previous args, unify would complain when we
21993 try to deduce a template parameter for the same argument, even though
21994 there isn't really a conflict. */
21995 nargs = TREE_VEC_LENGTH (targs);
21996 tempargs = make_tree_vec (nargs);
21997
21998 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21999 return 0;
22000
22001 /* First make sure we didn't deduce anything that conflicts with
22002 explicitly specified args. */
22003 for (i = nargs; i--; )
22004 {
22005 tree elt = TREE_VEC_ELT (tempargs, i);
22006 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22007
22008 if (!elt)
22009 /*NOP*/;
22010 else if (uses_template_parms (elt))
22011 /* Since we're unifying against ourselves, we will fill in
22012 template args used in the function parm list with our own
22013 template parms. Discard them. */
22014 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22015 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22016 {
22017 /* Check that the argument at each index of the deduced argument pack
22018 is equivalent to the corresponding explicitly specified argument.
22019 We may have deduced more arguments than were explicitly specified,
22020 and that's OK. */
22021
22022 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22023 that's wrong if we deduce the same argument pack from multiple
22024 function arguments: it's only incomplete the first time. */
22025
22026 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22027 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22028
22029 if (TREE_VEC_LENGTH (deduced_pack)
22030 < TREE_VEC_LENGTH (explicit_pack))
22031 return 0;
22032
22033 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22034 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22035 TREE_VEC_ELT (deduced_pack, j)))
22036 return 0;
22037 }
22038 else if (oldelt && !template_args_equal (oldelt, elt))
22039 return 0;
22040 }
22041
22042 for (i = nargs; i--; )
22043 {
22044 tree elt = TREE_VEC_ELT (tempargs, i);
22045
22046 if (elt)
22047 TREE_VEC_ELT (targs, i) = elt;
22048 }
22049
22050 return 1;
22051 }
22052
22053 /* PARM is a template class (perhaps with unbound template
22054 parameters). ARG is a fully instantiated type. If ARG can be
22055 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22056 TARGS are as for unify. */
22057
22058 static tree
22059 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22060 bool explain_p)
22061 {
22062 tree copy_of_targs;
22063
22064 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22065 return NULL_TREE;
22066 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22067 /* Matches anything. */;
22068 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22069 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22070 return NULL_TREE;
22071
22072 /* We need to make a new template argument vector for the call to
22073 unify. If we used TARGS, we'd clutter it up with the result of
22074 the attempted unification, even if this class didn't work out.
22075 We also don't want to commit ourselves to all the unifications
22076 we've already done, since unification is supposed to be done on
22077 an argument-by-argument basis. In other words, consider the
22078 following pathological case:
22079
22080 template <int I, int J, int K>
22081 struct S {};
22082
22083 template <int I, int J>
22084 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22085
22086 template <int I, int J, int K>
22087 void f(S<I, J, K>, S<I, I, I>);
22088
22089 void g() {
22090 S<0, 0, 0> s0;
22091 S<0, 1, 2> s2;
22092
22093 f(s0, s2);
22094 }
22095
22096 Now, by the time we consider the unification involving `s2', we
22097 already know that we must have `f<0, 0, 0>'. But, even though
22098 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22099 because there are two ways to unify base classes of S<0, 1, 2>
22100 with S<I, I, I>. If we kept the already deduced knowledge, we
22101 would reject the possibility I=1. */
22102 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22103
22104 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22105 {
22106 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22107 return NULL_TREE;
22108 return arg;
22109 }
22110
22111 /* If unification failed, we're done. */
22112 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22113 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22114 return NULL_TREE;
22115
22116 return arg;
22117 }
22118
22119 /* Given a template type PARM and a class type ARG, find the unique
22120 base type in ARG that is an instance of PARM. We do not examine
22121 ARG itself; only its base-classes. If there is not exactly one
22122 appropriate base class, return NULL_TREE. PARM may be the type of
22123 a partial specialization, as well as a plain template type. Used
22124 by unify. */
22125
22126 static enum template_base_result
22127 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22128 bool explain_p, tree *result)
22129 {
22130 tree rval = NULL_TREE;
22131 tree binfo;
22132
22133 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22134
22135 binfo = TYPE_BINFO (complete_type (arg));
22136 if (!binfo)
22137 {
22138 /* The type could not be completed. */
22139 *result = NULL_TREE;
22140 return tbr_incomplete_type;
22141 }
22142
22143 /* Walk in inheritance graph order. The search order is not
22144 important, and this avoids multiple walks of virtual bases. */
22145 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22146 {
22147 tree r = try_class_unification (tparms, targs, parm,
22148 BINFO_TYPE (binfo), explain_p);
22149
22150 if (r)
22151 {
22152 /* If there is more than one satisfactory baseclass, then:
22153
22154 [temp.deduct.call]
22155
22156 If they yield more than one possible deduced A, the type
22157 deduction fails.
22158
22159 applies. */
22160 if (rval && !same_type_p (r, rval))
22161 {
22162 *result = NULL_TREE;
22163 return tbr_ambiguous_baseclass;
22164 }
22165
22166 rval = r;
22167 }
22168 }
22169
22170 *result = rval;
22171 return tbr_success;
22172 }
22173
22174 /* Returns the level of DECL, which declares a template parameter. */
22175
22176 static int
22177 template_decl_level (tree decl)
22178 {
22179 switch (TREE_CODE (decl))
22180 {
22181 case TYPE_DECL:
22182 case TEMPLATE_DECL:
22183 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22184
22185 case PARM_DECL:
22186 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22187
22188 default:
22189 gcc_unreachable ();
22190 }
22191 return 0;
22192 }
22193
22194 /* Decide whether ARG can be unified with PARM, considering only the
22195 cv-qualifiers of each type, given STRICT as documented for unify.
22196 Returns nonzero iff the unification is OK on that basis. */
22197
22198 static int
22199 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22200 {
22201 int arg_quals = cp_type_quals (arg);
22202 int parm_quals = cp_type_quals (parm);
22203
22204 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22205 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22206 {
22207 /* Although a CVR qualifier is ignored when being applied to a
22208 substituted template parameter ([8.3.2]/1 for example), that
22209 does not allow us to unify "const T" with "int&" because both
22210 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22211 It is ok when we're allowing additional CV qualifiers
22212 at the outer level [14.8.2.1]/3,1st bullet. */
22213 if ((TYPE_REF_P (arg)
22214 || FUNC_OR_METHOD_TYPE_P (arg))
22215 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22216 return 0;
22217
22218 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22219 && (parm_quals & TYPE_QUAL_RESTRICT))
22220 return 0;
22221 }
22222
22223 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22224 && (arg_quals & parm_quals) != parm_quals)
22225 return 0;
22226
22227 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22228 && (parm_quals & arg_quals) != arg_quals)
22229 return 0;
22230
22231 return 1;
22232 }
22233
22234 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22235 void
22236 template_parm_level_and_index (tree parm, int* level, int* index)
22237 {
22238 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22239 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22240 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22241 {
22242 *index = TEMPLATE_TYPE_IDX (parm);
22243 *level = TEMPLATE_TYPE_LEVEL (parm);
22244 }
22245 else
22246 {
22247 *index = TEMPLATE_PARM_IDX (parm);
22248 *level = TEMPLATE_PARM_LEVEL (parm);
22249 }
22250 }
22251
22252 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22253 do { \
22254 if (unify (TP, TA, P, A, S, EP)) \
22255 return 1; \
22256 } while (0)
22257
22258 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22259 expansion at the end of PACKED_PARMS. Returns 0 if the type
22260 deduction succeeds, 1 otherwise. STRICT is the same as in
22261 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22262 function call argument list. We'll need to adjust the arguments to make them
22263 types. SUBR tells us if this is from a recursive call to
22264 type_unification_real, or for comparing two template argument
22265 lists. */
22266
22267 static int
22268 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22269 tree packed_args, unification_kind_t strict,
22270 bool subr, bool explain_p)
22271 {
22272 tree parm
22273 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22274 tree pattern = PACK_EXPANSION_PATTERN (parm);
22275 tree pack, packs = NULL_TREE;
22276 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22277
22278 /* Add in any args remembered from an earlier partial instantiation. */
22279 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22280 int levels = TMPL_ARGS_DEPTH (targs);
22281
22282 packed_args = expand_template_argument_pack (packed_args);
22283
22284 int len = TREE_VEC_LENGTH (packed_args);
22285
22286 /* Determine the parameter packs we will be deducing from the
22287 pattern, and record their current deductions. */
22288 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22289 pack; pack = TREE_CHAIN (pack))
22290 {
22291 tree parm_pack = TREE_VALUE (pack);
22292 int idx, level;
22293
22294 /* Only template parameter packs can be deduced, not e.g. function
22295 parameter packs or __bases or __integer_pack. */
22296 if (!TEMPLATE_PARM_P (parm_pack))
22297 continue;
22298
22299 /* Determine the index and level of this parameter pack. */
22300 template_parm_level_and_index (parm_pack, &level, &idx);
22301 if (level < levels)
22302 continue;
22303
22304 /* Keep track of the parameter packs and their corresponding
22305 argument packs. */
22306 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22307 TREE_TYPE (packs) = make_tree_vec (len - start);
22308 }
22309
22310 /* Loop through all of the arguments that have not yet been
22311 unified and unify each with the pattern. */
22312 for (i = start; i < len; i++)
22313 {
22314 tree parm;
22315 bool any_explicit = false;
22316 tree arg = TREE_VEC_ELT (packed_args, i);
22317
22318 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22319 or the element of its argument pack at the current index if
22320 this argument was explicitly specified. */
22321 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22322 {
22323 int idx, level;
22324 tree arg, pargs;
22325 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22326
22327 arg = NULL_TREE;
22328 if (TREE_VALUE (pack)
22329 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22330 && (i - start < TREE_VEC_LENGTH (pargs)))
22331 {
22332 any_explicit = true;
22333 arg = TREE_VEC_ELT (pargs, i - start);
22334 }
22335 TMPL_ARG (targs, level, idx) = arg;
22336 }
22337
22338 /* If we had explicit template arguments, substitute them into the
22339 pattern before deduction. */
22340 if (any_explicit)
22341 {
22342 /* Some arguments might still be unspecified or dependent. */
22343 bool dependent;
22344 ++processing_template_decl;
22345 dependent = any_dependent_template_arguments_p (targs);
22346 if (!dependent)
22347 --processing_template_decl;
22348 parm = tsubst (pattern, targs,
22349 explain_p ? tf_warning_or_error : tf_none,
22350 NULL_TREE);
22351 if (dependent)
22352 --processing_template_decl;
22353 if (parm == error_mark_node)
22354 return 1;
22355 }
22356 else
22357 parm = pattern;
22358
22359 /* Unify the pattern with the current argument. */
22360 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22361 explain_p))
22362 return 1;
22363
22364 /* For each parameter pack, collect the deduced value. */
22365 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22366 {
22367 int idx, level;
22368 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22369
22370 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22371 TMPL_ARG (targs, level, idx);
22372 }
22373 }
22374
22375 /* Verify that the results of unification with the parameter packs
22376 produce results consistent with what we've seen before, and make
22377 the deduced argument packs available. */
22378 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22379 {
22380 tree old_pack = TREE_VALUE (pack);
22381 tree new_args = TREE_TYPE (pack);
22382 int i, len = TREE_VEC_LENGTH (new_args);
22383 int idx, level;
22384 bool nondeduced_p = false;
22385
22386 /* By default keep the original deduced argument pack.
22387 If necessary, more specific code is going to update the
22388 resulting deduced argument later down in this function. */
22389 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22390 TMPL_ARG (targs, level, idx) = old_pack;
22391
22392 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22393 actually deduce anything. */
22394 for (i = 0; i < len && !nondeduced_p; ++i)
22395 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22396 nondeduced_p = true;
22397 if (nondeduced_p)
22398 continue;
22399
22400 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22401 {
22402 /* If we had fewer function args than explicit template args,
22403 just use the explicits. */
22404 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22405 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22406 if (len < explicit_len)
22407 new_args = explicit_args;
22408 }
22409
22410 if (!old_pack)
22411 {
22412 tree result;
22413 /* Build the deduced *_ARGUMENT_PACK. */
22414 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22415 {
22416 result = make_node (NONTYPE_ARGUMENT_PACK);
22417 TREE_CONSTANT (result) = 1;
22418 }
22419 else
22420 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22421
22422 SET_ARGUMENT_PACK_ARGS (result, new_args);
22423
22424 /* Note the deduced argument packs for this parameter
22425 pack. */
22426 TMPL_ARG (targs, level, idx) = result;
22427 }
22428 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22429 && (ARGUMENT_PACK_ARGS (old_pack)
22430 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22431 {
22432 /* We only had the explicitly-provided arguments before, but
22433 now we have a complete set of arguments. */
22434 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22435
22436 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22437 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22438 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22439 }
22440 else
22441 {
22442 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22443 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22444
22445 if (!comp_template_args (old_args, new_args,
22446 &bad_old_arg, &bad_new_arg))
22447 /* Inconsistent unification of this parameter pack. */
22448 return unify_parameter_pack_inconsistent (explain_p,
22449 bad_old_arg,
22450 bad_new_arg);
22451 }
22452 }
22453
22454 return unify_success (explain_p);
22455 }
22456
22457 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22458 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22459 parameters and return value are as for unify. */
22460
22461 static int
22462 unify_array_domain (tree tparms, tree targs,
22463 tree parm_dom, tree arg_dom,
22464 bool explain_p)
22465 {
22466 tree parm_max;
22467 tree arg_max;
22468 bool parm_cst;
22469 bool arg_cst;
22470
22471 /* Our representation of array types uses "N - 1" as the
22472 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22473 not an integer constant. We cannot unify arbitrarily
22474 complex expressions, so we eliminate the MINUS_EXPRs
22475 here. */
22476 parm_max = TYPE_MAX_VALUE (parm_dom);
22477 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22478 if (!parm_cst)
22479 {
22480 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22481 parm_max = TREE_OPERAND (parm_max, 0);
22482 }
22483 arg_max = TYPE_MAX_VALUE (arg_dom);
22484 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22485 if (!arg_cst)
22486 {
22487 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22488 trying to unify the type of a variable with the type
22489 of a template parameter. For example:
22490
22491 template <unsigned int N>
22492 void f (char (&) [N]);
22493 int g();
22494 void h(int i) {
22495 char a[g(i)];
22496 f(a);
22497 }
22498
22499 Here, the type of the ARG will be "int [g(i)]", and
22500 may be a SAVE_EXPR, etc. */
22501 if (TREE_CODE (arg_max) != MINUS_EXPR)
22502 return unify_vla_arg (explain_p, arg_dom);
22503 arg_max = TREE_OPERAND (arg_max, 0);
22504 }
22505
22506 /* If only one of the bounds used a MINUS_EXPR, compensate
22507 by adding one to the other bound. */
22508 if (parm_cst && !arg_cst)
22509 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22510 integer_type_node,
22511 parm_max,
22512 integer_one_node);
22513 else if (arg_cst && !parm_cst)
22514 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22515 integer_type_node,
22516 arg_max,
22517 integer_one_node);
22518
22519 return unify (tparms, targs, parm_max, arg_max,
22520 UNIFY_ALLOW_INTEGER, explain_p);
22521 }
22522
22523 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22524
22525 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22526
22527 static pa_kind_t
22528 pa_kind (tree t)
22529 {
22530 if (PACK_EXPANSION_P (t))
22531 t = PACK_EXPANSION_PATTERN (t);
22532 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22533 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22534 || DECL_TYPE_TEMPLATE_P (t))
22535 return pa_tmpl;
22536 else if (TYPE_P (t))
22537 return pa_type;
22538 else
22539 return pa_expr;
22540 }
22541
22542 /* Deduce the value of template parameters. TPARMS is the (innermost)
22543 set of template parameters to a template. TARGS is the bindings
22544 for those template parameters, as determined thus far; TARGS may
22545 include template arguments for outer levels of template parameters
22546 as well. PARM is a parameter to a template function, or a
22547 subcomponent of that parameter; ARG is the corresponding argument.
22548 This function attempts to match PARM with ARG in a manner
22549 consistent with the existing assignments in TARGS. If more values
22550 are deduced, then TARGS is updated.
22551
22552 Returns 0 if the type deduction succeeds, 1 otherwise. The
22553 parameter STRICT is a bitwise or of the following flags:
22554
22555 UNIFY_ALLOW_NONE:
22556 Require an exact match between PARM and ARG.
22557 UNIFY_ALLOW_MORE_CV_QUAL:
22558 Allow the deduced ARG to be more cv-qualified (by qualification
22559 conversion) than ARG.
22560 UNIFY_ALLOW_LESS_CV_QUAL:
22561 Allow the deduced ARG to be less cv-qualified than ARG.
22562 UNIFY_ALLOW_DERIVED:
22563 Allow the deduced ARG to be a template base class of ARG,
22564 or a pointer to a template base class of the type pointed to by
22565 ARG.
22566 UNIFY_ALLOW_INTEGER:
22567 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22568 case for more information.
22569 UNIFY_ALLOW_OUTER_LEVEL:
22570 This is the outermost level of a deduction. Used to determine validity
22571 of qualification conversions. A valid qualification conversion must
22572 have const qualified pointers leading up to the inner type which
22573 requires additional CV quals, except at the outer level, where const
22574 is not required [conv.qual]. It would be normal to set this flag in
22575 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22576 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22577 This is the outermost level of a deduction, and PARM can be more CV
22578 qualified at this point.
22579 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22580 This is the outermost level of a deduction, and PARM can be less CV
22581 qualified at this point. */
22582
22583 static int
22584 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22585 bool explain_p)
22586 {
22587 int idx;
22588 tree targ;
22589 tree tparm;
22590 int strict_in = strict;
22591 tsubst_flags_t complain = (explain_p
22592 ? tf_warning_or_error
22593 : tf_none);
22594
22595 /* I don't think this will do the right thing with respect to types.
22596 But the only case I've seen it in so far has been array bounds, where
22597 signedness is the only information lost, and I think that will be
22598 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22599 finish_id_expression_1, and are also OK. */
22600 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22601 parm = TREE_OPERAND (parm, 0);
22602
22603 if (arg == error_mark_node)
22604 return unify_invalid (explain_p);
22605 if (arg == unknown_type_node
22606 || arg == init_list_type_node)
22607 /* We can't deduce anything from this, but we might get all the
22608 template args from other function args. */
22609 return unify_success (explain_p);
22610
22611 if (parm == any_targ_node || arg == any_targ_node)
22612 return unify_success (explain_p);
22613
22614 /* If PARM uses template parameters, then we can't bail out here,
22615 even if ARG == PARM, since we won't record unifications for the
22616 template parameters. We might need them if we're trying to
22617 figure out which of two things is more specialized. */
22618 if (arg == parm && !uses_template_parms (parm))
22619 return unify_success (explain_p);
22620
22621 /* Handle init lists early, so the rest of the function can assume
22622 we're dealing with a type. */
22623 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22624 {
22625 tree elt, elttype;
22626 unsigned i;
22627 tree orig_parm = parm;
22628
22629 if (!is_std_init_list (parm)
22630 && TREE_CODE (parm) != ARRAY_TYPE)
22631 /* We can only deduce from an initializer list argument if the
22632 parameter is std::initializer_list or an array; otherwise this
22633 is a non-deduced context. */
22634 return unify_success (explain_p);
22635
22636 if (TREE_CODE (parm) == ARRAY_TYPE)
22637 elttype = TREE_TYPE (parm);
22638 else
22639 {
22640 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22641 /* Deduction is defined in terms of a single type, so just punt
22642 on the (bizarre) std::initializer_list<T...>. */
22643 if (PACK_EXPANSION_P (elttype))
22644 return unify_success (explain_p);
22645 }
22646
22647 if (strict != DEDUCE_EXACT
22648 && TYPE_P (elttype)
22649 && !uses_deducible_template_parms (elttype))
22650 /* If ELTTYPE has no deducible template parms, skip deduction from
22651 the list elements. */;
22652 else
22653 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22654 {
22655 int elt_strict = strict;
22656
22657 if (elt == error_mark_node)
22658 return unify_invalid (explain_p);
22659
22660 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22661 {
22662 tree type = TREE_TYPE (elt);
22663 if (type == error_mark_node)
22664 return unify_invalid (explain_p);
22665 /* It should only be possible to get here for a call. */
22666 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22667 elt_strict |= maybe_adjust_types_for_deduction
22668 (DEDUCE_CALL, &elttype, &type, elt);
22669 elt = type;
22670 }
22671
22672 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22673 explain_p);
22674 }
22675
22676 if (TREE_CODE (parm) == ARRAY_TYPE
22677 && deducible_array_bound (TYPE_DOMAIN (parm)))
22678 {
22679 /* Also deduce from the length of the initializer list. */
22680 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22681 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22682 if (idx == error_mark_node)
22683 return unify_invalid (explain_p);
22684 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22685 idx, explain_p);
22686 }
22687
22688 /* If the std::initializer_list<T> deduction worked, replace the
22689 deduced A with std::initializer_list<A>. */
22690 if (orig_parm != parm)
22691 {
22692 idx = TEMPLATE_TYPE_IDX (orig_parm);
22693 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22694 targ = listify (targ);
22695 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22696 }
22697 return unify_success (explain_p);
22698 }
22699
22700 /* If parm and arg aren't the same kind of thing (template, type, or
22701 expression), fail early. */
22702 if (pa_kind (parm) != pa_kind (arg))
22703 return unify_invalid (explain_p);
22704
22705 /* Immediately reject some pairs that won't unify because of
22706 cv-qualification mismatches. */
22707 if (TREE_CODE (arg) == TREE_CODE (parm)
22708 && TYPE_P (arg)
22709 /* It is the elements of the array which hold the cv quals of an array
22710 type, and the elements might be template type parms. We'll check
22711 when we recurse. */
22712 && TREE_CODE (arg) != ARRAY_TYPE
22713 /* We check the cv-qualifiers when unifying with template type
22714 parameters below. We want to allow ARG `const T' to unify with
22715 PARM `T' for example, when computing which of two templates
22716 is more specialized, for example. */
22717 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22718 && !check_cv_quals_for_unify (strict_in, arg, parm))
22719 return unify_cv_qual_mismatch (explain_p, parm, arg);
22720
22721 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22722 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22723 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22724 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22725 strict &= ~UNIFY_ALLOW_DERIVED;
22726 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22727 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22728
22729 switch (TREE_CODE (parm))
22730 {
22731 case TYPENAME_TYPE:
22732 case SCOPE_REF:
22733 case UNBOUND_CLASS_TEMPLATE:
22734 /* In a type which contains a nested-name-specifier, template
22735 argument values cannot be deduced for template parameters used
22736 within the nested-name-specifier. */
22737 return unify_success (explain_p);
22738
22739 case TEMPLATE_TYPE_PARM:
22740 case TEMPLATE_TEMPLATE_PARM:
22741 case BOUND_TEMPLATE_TEMPLATE_PARM:
22742 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22743 if (error_operand_p (tparm))
22744 return unify_invalid (explain_p);
22745
22746 if (TEMPLATE_TYPE_LEVEL (parm)
22747 != template_decl_level (tparm))
22748 /* The PARM is not one we're trying to unify. Just check
22749 to see if it matches ARG. */
22750 {
22751 if (TREE_CODE (arg) == TREE_CODE (parm)
22752 && (is_auto (parm) ? is_auto (arg)
22753 : same_type_p (parm, arg)))
22754 return unify_success (explain_p);
22755 else
22756 return unify_type_mismatch (explain_p, parm, arg);
22757 }
22758 idx = TEMPLATE_TYPE_IDX (parm);
22759 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22760 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22761 if (error_operand_p (tparm))
22762 return unify_invalid (explain_p);
22763
22764 /* Check for mixed types and values. */
22765 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22766 && TREE_CODE (tparm) != TYPE_DECL)
22767 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22768 && TREE_CODE (tparm) != TEMPLATE_DECL))
22769 gcc_unreachable ();
22770
22771 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22772 {
22773 if ((strict_in & UNIFY_ALLOW_DERIVED)
22774 && CLASS_TYPE_P (arg))
22775 {
22776 /* First try to match ARG directly. */
22777 tree t = try_class_unification (tparms, targs, parm, arg,
22778 explain_p);
22779 if (!t)
22780 {
22781 /* Otherwise, look for a suitable base of ARG, as below. */
22782 enum template_base_result r;
22783 r = get_template_base (tparms, targs, parm, arg,
22784 explain_p, &t);
22785 if (!t)
22786 return unify_no_common_base (explain_p, r, parm, arg);
22787 arg = t;
22788 }
22789 }
22790 /* ARG must be constructed from a template class or a template
22791 template parameter. */
22792 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22793 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22794 return unify_template_deduction_failure (explain_p, parm, arg);
22795
22796 /* Deduce arguments T, i from TT<T> or TT<i>. */
22797 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22798 return 1;
22799
22800 arg = TYPE_TI_TEMPLATE (arg);
22801
22802 /* Fall through to deduce template name. */
22803 }
22804
22805 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22806 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22807 {
22808 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22809
22810 /* Simple cases: Value already set, does match or doesn't. */
22811 if (targ != NULL_TREE && template_args_equal (targ, arg))
22812 return unify_success (explain_p);
22813 else if (targ)
22814 return unify_inconsistency (explain_p, parm, targ, arg);
22815 }
22816 else
22817 {
22818 /* If PARM is `const T' and ARG is only `int', we don't have
22819 a match unless we are allowing additional qualification.
22820 If ARG is `const int' and PARM is just `T' that's OK;
22821 that binds `const int' to `T'. */
22822 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22823 arg, parm))
22824 return unify_cv_qual_mismatch (explain_p, parm, arg);
22825
22826 /* Consider the case where ARG is `const volatile int' and
22827 PARM is `const T'. Then, T should be `volatile int'. */
22828 arg = cp_build_qualified_type_real
22829 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22830 if (arg == error_mark_node)
22831 return unify_invalid (explain_p);
22832
22833 /* Simple cases: Value already set, does match or doesn't. */
22834 if (targ != NULL_TREE && same_type_p (targ, arg))
22835 return unify_success (explain_p);
22836 else if (targ)
22837 return unify_inconsistency (explain_p, parm, targ, arg);
22838
22839 /* Make sure that ARG is not a variable-sized array. (Note
22840 that were talking about variable-sized arrays (like
22841 `int[n]'), rather than arrays of unknown size (like
22842 `int[]').) We'll get very confused by such a type since
22843 the bound of the array is not constant, and therefore
22844 not mangleable. Besides, such types are not allowed in
22845 ISO C++, so we can do as we please here. We do allow
22846 them for 'auto' deduction, since that isn't ABI-exposed. */
22847 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22848 return unify_vla_arg (explain_p, arg);
22849
22850 /* Strip typedefs as in convert_template_argument. */
22851 arg = canonicalize_type_argument (arg, tf_none);
22852 }
22853
22854 /* If ARG is a parameter pack or an expansion, we cannot unify
22855 against it unless PARM is also a parameter pack. */
22856 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22857 && !template_parameter_pack_p (parm))
22858 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22859
22860 /* If the argument deduction results is a METHOD_TYPE,
22861 then there is a problem.
22862 METHOD_TYPE doesn't map to any real C++ type the result of
22863 the deduction cannot be of that type. */
22864 if (TREE_CODE (arg) == METHOD_TYPE)
22865 return unify_method_type_error (explain_p, arg);
22866
22867 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22868 return unify_success (explain_p);
22869
22870 case TEMPLATE_PARM_INDEX:
22871 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22872 if (error_operand_p (tparm))
22873 return unify_invalid (explain_p);
22874
22875 if (TEMPLATE_PARM_LEVEL (parm)
22876 != template_decl_level (tparm))
22877 {
22878 /* The PARM is not one we're trying to unify. Just check
22879 to see if it matches ARG. */
22880 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22881 && cp_tree_equal (parm, arg));
22882 if (result)
22883 unify_expression_unequal (explain_p, parm, arg);
22884 return result;
22885 }
22886
22887 idx = TEMPLATE_PARM_IDX (parm);
22888 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22889
22890 if (targ)
22891 {
22892 if ((strict & UNIFY_ALLOW_INTEGER)
22893 && TREE_TYPE (targ) && TREE_TYPE (arg)
22894 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22895 /* We're deducing from an array bound, the type doesn't matter. */
22896 arg = fold_convert (TREE_TYPE (targ), arg);
22897 int x = !cp_tree_equal (targ, arg);
22898 if (x)
22899 unify_inconsistency (explain_p, parm, targ, arg);
22900 return x;
22901 }
22902
22903 /* [temp.deduct.type] If, in the declaration of a function template
22904 with a non-type template-parameter, the non-type
22905 template-parameter is used in an expression in the function
22906 parameter-list and, if the corresponding template-argument is
22907 deduced, the template-argument type shall match the type of the
22908 template-parameter exactly, except that a template-argument
22909 deduced from an array bound may be of any integral type.
22910 The non-type parameter might use already deduced type parameters. */
22911 tparm = TREE_TYPE (parm);
22912 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22913 /* We don't have enough levels of args to do any substitution. This
22914 can happen in the context of -fnew-ttp-matching. */;
22915 else
22916 {
22917 ++processing_template_decl;
22918 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22919 --processing_template_decl;
22920
22921 if (tree a = type_uses_auto (tparm))
22922 {
22923 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22924 if (tparm == error_mark_node)
22925 return 1;
22926 }
22927 }
22928
22929 if (!TREE_TYPE (arg))
22930 /* Template-parameter dependent expression. Just accept it for now.
22931 It will later be processed in convert_template_argument. */
22932 ;
22933 else if (same_type_ignoring_top_level_qualifiers_p
22934 (non_reference (TREE_TYPE (arg)),
22935 non_reference (tparm)))
22936 /* OK. Ignore top-level quals here because a class-type template
22937 parameter object is const. */;
22938 else if ((strict & UNIFY_ALLOW_INTEGER)
22939 && CP_INTEGRAL_TYPE_P (tparm))
22940 /* Convert the ARG to the type of PARM; the deduced non-type
22941 template argument must exactly match the types of the
22942 corresponding parameter. */
22943 arg = fold (build_nop (tparm, arg));
22944 else if (uses_template_parms (tparm))
22945 {
22946 /* We haven't deduced the type of this parameter yet. */
22947 if (cxx_dialect >= cxx17
22948 /* We deduce from array bounds in try_array_deduction. */
22949 && !(strict & UNIFY_ALLOW_INTEGER))
22950 {
22951 /* Deduce it from the non-type argument. */
22952 tree atype = TREE_TYPE (arg);
22953 RECUR_AND_CHECK_FAILURE (tparms, targs,
22954 tparm, atype,
22955 UNIFY_ALLOW_NONE, explain_p);
22956 }
22957 else
22958 /* Try again later. */
22959 return unify_success (explain_p);
22960 }
22961 else
22962 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22963
22964 /* If ARG is a parameter pack or an expansion, we cannot unify
22965 against it unless PARM is also a parameter pack. */
22966 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22967 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22968 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22969
22970 {
22971 bool removed_attr = false;
22972 arg = strip_typedefs_expr (arg, &removed_attr);
22973 }
22974 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22975 return unify_success (explain_p);
22976
22977 case PTRMEM_CST:
22978 {
22979 /* A pointer-to-member constant can be unified only with
22980 another constant. */
22981 if (TREE_CODE (arg) != PTRMEM_CST)
22982 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22983
22984 /* Just unify the class member. It would be useless (and possibly
22985 wrong, depending on the strict flags) to unify also
22986 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22987 arg refer to the same variable, even if through different
22988 classes. For instance:
22989
22990 struct A { int x; };
22991 struct B : A { };
22992
22993 Unification of &A::x and &B::x must succeed. */
22994 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22995 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22996 }
22997
22998 case POINTER_TYPE:
22999 {
23000 if (!TYPE_PTR_P (arg))
23001 return unify_type_mismatch (explain_p, parm, arg);
23002
23003 /* [temp.deduct.call]
23004
23005 A can be another pointer or pointer to member type that can
23006 be converted to the deduced A via a qualification
23007 conversion (_conv.qual_).
23008
23009 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23010 This will allow for additional cv-qualification of the
23011 pointed-to types if appropriate. */
23012
23013 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23014 /* The derived-to-base conversion only persists through one
23015 level of pointers. */
23016 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23017
23018 return unify (tparms, targs, TREE_TYPE (parm),
23019 TREE_TYPE (arg), strict, explain_p);
23020 }
23021
23022 case REFERENCE_TYPE:
23023 if (!TYPE_REF_P (arg))
23024 return unify_type_mismatch (explain_p, parm, arg);
23025 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23026 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23027
23028 case ARRAY_TYPE:
23029 if (TREE_CODE (arg) != ARRAY_TYPE)
23030 return unify_type_mismatch (explain_p, parm, arg);
23031 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23032 != (TYPE_DOMAIN (arg) == NULL_TREE))
23033 return unify_type_mismatch (explain_p, parm, arg);
23034 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23035 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23036 if (TYPE_DOMAIN (parm) != NULL_TREE)
23037 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23038 TYPE_DOMAIN (arg), explain_p);
23039 return unify_success (explain_p);
23040
23041 case REAL_TYPE:
23042 case COMPLEX_TYPE:
23043 case VECTOR_TYPE:
23044 case INTEGER_TYPE:
23045 case BOOLEAN_TYPE:
23046 case ENUMERAL_TYPE:
23047 case VOID_TYPE:
23048 case NULLPTR_TYPE:
23049 if (TREE_CODE (arg) != TREE_CODE (parm))
23050 return unify_type_mismatch (explain_p, parm, arg);
23051
23052 /* We have already checked cv-qualification at the top of the
23053 function. */
23054 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23055 return unify_type_mismatch (explain_p, parm, arg);
23056
23057 /* As far as unification is concerned, this wins. Later checks
23058 will invalidate it if necessary. */
23059 return unify_success (explain_p);
23060
23061 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23062 /* Type INTEGER_CST can come from ordinary constant template args. */
23063 case INTEGER_CST:
23064 while (CONVERT_EXPR_P (arg))
23065 arg = TREE_OPERAND (arg, 0);
23066
23067 if (TREE_CODE (arg) != INTEGER_CST)
23068 return unify_template_argument_mismatch (explain_p, parm, arg);
23069 return (tree_int_cst_equal (parm, arg)
23070 ? unify_success (explain_p)
23071 : unify_template_argument_mismatch (explain_p, parm, arg));
23072
23073 case TREE_VEC:
23074 {
23075 int i, len, argslen;
23076 int parm_variadic_p = 0;
23077
23078 if (TREE_CODE (arg) != TREE_VEC)
23079 return unify_template_argument_mismatch (explain_p, parm, arg);
23080
23081 len = TREE_VEC_LENGTH (parm);
23082 argslen = TREE_VEC_LENGTH (arg);
23083
23084 /* Check for pack expansions in the parameters. */
23085 for (i = 0; i < len; ++i)
23086 {
23087 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23088 {
23089 if (i == len - 1)
23090 /* We can unify against something with a trailing
23091 parameter pack. */
23092 parm_variadic_p = 1;
23093 else
23094 /* [temp.deduct.type]/9: If the template argument list of
23095 P contains a pack expansion that is not the last
23096 template argument, the entire template argument list
23097 is a non-deduced context. */
23098 return unify_success (explain_p);
23099 }
23100 }
23101
23102 /* If we don't have enough arguments to satisfy the parameters
23103 (not counting the pack expression at the end), or we have
23104 too many arguments for a parameter list that doesn't end in
23105 a pack expression, we can't unify. */
23106 if (parm_variadic_p
23107 ? argslen < len - parm_variadic_p
23108 : argslen != len)
23109 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23110
23111 /* Unify all of the parameters that precede the (optional)
23112 pack expression. */
23113 for (i = 0; i < len - parm_variadic_p; ++i)
23114 {
23115 RECUR_AND_CHECK_FAILURE (tparms, targs,
23116 TREE_VEC_ELT (parm, i),
23117 TREE_VEC_ELT (arg, i),
23118 UNIFY_ALLOW_NONE, explain_p);
23119 }
23120 if (parm_variadic_p)
23121 return unify_pack_expansion (tparms, targs, parm, arg,
23122 DEDUCE_EXACT,
23123 /*subr=*/true, explain_p);
23124 return unify_success (explain_p);
23125 }
23126
23127 case RECORD_TYPE:
23128 case UNION_TYPE:
23129 if (TREE_CODE (arg) != TREE_CODE (parm))
23130 return unify_type_mismatch (explain_p, parm, arg);
23131
23132 if (TYPE_PTRMEMFUNC_P (parm))
23133 {
23134 if (!TYPE_PTRMEMFUNC_P (arg))
23135 return unify_type_mismatch (explain_p, parm, arg);
23136
23137 return unify (tparms, targs,
23138 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23139 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23140 strict, explain_p);
23141 }
23142 else if (TYPE_PTRMEMFUNC_P (arg))
23143 return unify_type_mismatch (explain_p, parm, arg);
23144
23145 if (CLASSTYPE_TEMPLATE_INFO (parm))
23146 {
23147 tree t = NULL_TREE;
23148
23149 if (strict_in & UNIFY_ALLOW_DERIVED)
23150 {
23151 /* First, we try to unify the PARM and ARG directly. */
23152 t = try_class_unification (tparms, targs,
23153 parm, arg, explain_p);
23154
23155 if (!t)
23156 {
23157 /* Fallback to the special case allowed in
23158 [temp.deduct.call]:
23159
23160 If P is a class, and P has the form
23161 template-id, then A can be a derived class of
23162 the deduced A. Likewise, if P is a pointer to
23163 a class of the form template-id, A can be a
23164 pointer to a derived class pointed to by the
23165 deduced A. */
23166 enum template_base_result r;
23167 r = get_template_base (tparms, targs, parm, arg,
23168 explain_p, &t);
23169
23170 if (!t)
23171 {
23172 /* Don't give the derived diagnostic if we're
23173 already dealing with the same template. */
23174 bool same_template
23175 = (CLASSTYPE_TEMPLATE_INFO (arg)
23176 && (CLASSTYPE_TI_TEMPLATE (parm)
23177 == CLASSTYPE_TI_TEMPLATE (arg)));
23178 return unify_no_common_base (explain_p && !same_template,
23179 r, parm, arg);
23180 }
23181 }
23182 }
23183 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23184 && (CLASSTYPE_TI_TEMPLATE (parm)
23185 == CLASSTYPE_TI_TEMPLATE (arg)))
23186 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23187 Then, we should unify `int' and `U'. */
23188 t = arg;
23189 else
23190 /* There's no chance of unification succeeding. */
23191 return unify_type_mismatch (explain_p, parm, arg);
23192
23193 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23194 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23195 }
23196 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23197 return unify_type_mismatch (explain_p, parm, arg);
23198 return unify_success (explain_p);
23199
23200 case METHOD_TYPE:
23201 case FUNCTION_TYPE:
23202 {
23203 unsigned int nargs;
23204 tree *args;
23205 tree a;
23206 unsigned int i;
23207
23208 if (TREE_CODE (arg) != TREE_CODE (parm))
23209 return unify_type_mismatch (explain_p, parm, arg);
23210
23211 /* CV qualifications for methods can never be deduced, they must
23212 match exactly. We need to check them explicitly here,
23213 because type_unification_real treats them as any other
23214 cv-qualified parameter. */
23215 if (TREE_CODE (parm) == METHOD_TYPE
23216 && (!check_cv_quals_for_unify
23217 (UNIFY_ALLOW_NONE,
23218 class_of_this_parm (arg),
23219 class_of_this_parm (parm))))
23220 return unify_cv_qual_mismatch (explain_p, parm, arg);
23221 if (TREE_CODE (arg) == FUNCTION_TYPE
23222 && type_memfn_quals (parm) != type_memfn_quals (arg))
23223 return unify_cv_qual_mismatch (explain_p, parm, arg);
23224 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23225 return unify_type_mismatch (explain_p, parm, arg);
23226
23227 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23228 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23229
23230 nargs = list_length (TYPE_ARG_TYPES (arg));
23231 args = XALLOCAVEC (tree, nargs);
23232 for (a = TYPE_ARG_TYPES (arg), i = 0;
23233 a != NULL_TREE && a != void_list_node;
23234 a = TREE_CHAIN (a), ++i)
23235 args[i] = TREE_VALUE (a);
23236 nargs = i;
23237
23238 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23239 args, nargs, 1, DEDUCE_EXACT,
23240 NULL, explain_p))
23241 return 1;
23242
23243 if (flag_noexcept_type)
23244 {
23245 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23246 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23247 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23248 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23249 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23250 && uses_template_parms (TREE_PURPOSE (pspec)))
23251 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23252 TREE_PURPOSE (aspec),
23253 UNIFY_ALLOW_NONE, explain_p);
23254 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23255 return unify_type_mismatch (explain_p, parm, arg);
23256 }
23257
23258 return 0;
23259 }
23260
23261 case OFFSET_TYPE:
23262 /* Unify a pointer to member with a pointer to member function, which
23263 deduces the type of the member as a function type. */
23264 if (TYPE_PTRMEMFUNC_P (arg))
23265 {
23266 /* Check top-level cv qualifiers */
23267 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23268 return unify_cv_qual_mismatch (explain_p, parm, arg);
23269
23270 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23271 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23272 UNIFY_ALLOW_NONE, explain_p);
23273
23274 /* Determine the type of the function we are unifying against. */
23275 tree fntype = static_fn_type (arg);
23276
23277 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23278 }
23279
23280 if (TREE_CODE (arg) != OFFSET_TYPE)
23281 return unify_type_mismatch (explain_p, parm, arg);
23282 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23283 TYPE_OFFSET_BASETYPE (arg),
23284 UNIFY_ALLOW_NONE, explain_p);
23285 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23286 strict, explain_p);
23287
23288 case CONST_DECL:
23289 if (DECL_TEMPLATE_PARM_P (parm))
23290 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23291 if (arg != scalar_constant_value (parm))
23292 return unify_template_argument_mismatch (explain_p, parm, arg);
23293 return unify_success (explain_p);
23294
23295 case FIELD_DECL:
23296 case TEMPLATE_DECL:
23297 /* Matched cases are handled by the ARG == PARM test above. */
23298 return unify_template_argument_mismatch (explain_p, parm, arg);
23299
23300 case VAR_DECL:
23301 /* We might get a variable as a non-type template argument in parm if the
23302 corresponding parameter is type-dependent. Make any necessary
23303 adjustments based on whether arg is a reference. */
23304 if (CONSTANT_CLASS_P (arg))
23305 parm = fold_non_dependent_expr (parm, complain);
23306 else if (REFERENCE_REF_P (arg))
23307 {
23308 tree sub = TREE_OPERAND (arg, 0);
23309 STRIP_NOPS (sub);
23310 if (TREE_CODE (sub) == ADDR_EXPR)
23311 arg = TREE_OPERAND (sub, 0);
23312 }
23313 /* Now use the normal expression code to check whether they match. */
23314 goto expr;
23315
23316 case TYPE_ARGUMENT_PACK:
23317 case NONTYPE_ARGUMENT_PACK:
23318 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23319 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23320
23321 case TYPEOF_TYPE:
23322 case DECLTYPE_TYPE:
23323 case UNDERLYING_TYPE:
23324 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23325 or UNDERLYING_TYPE nodes. */
23326 return unify_success (explain_p);
23327
23328 case ERROR_MARK:
23329 /* Unification fails if we hit an error node. */
23330 return unify_invalid (explain_p);
23331
23332 case INDIRECT_REF:
23333 if (REFERENCE_REF_P (parm))
23334 {
23335 bool pexp = PACK_EXPANSION_P (arg);
23336 if (pexp)
23337 arg = PACK_EXPANSION_PATTERN (arg);
23338 if (REFERENCE_REF_P (arg))
23339 arg = TREE_OPERAND (arg, 0);
23340 if (pexp)
23341 arg = make_pack_expansion (arg, complain);
23342 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23343 strict, explain_p);
23344 }
23345 /* FALLTHRU */
23346
23347 default:
23348 /* An unresolved overload is a nondeduced context. */
23349 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23350 return unify_success (explain_p);
23351 gcc_assert (EXPR_P (parm)
23352 || COMPOUND_LITERAL_P (parm)
23353 || TREE_CODE (parm) == TRAIT_EXPR);
23354 expr:
23355 /* We must be looking at an expression. This can happen with
23356 something like:
23357
23358 template <int I>
23359 void foo(S<I>, S<I + 2>);
23360
23361 or
23362
23363 template<typename T>
23364 void foo(A<T, T{}>);
23365
23366 This is a "non-deduced context":
23367
23368 [deduct.type]
23369
23370 The non-deduced contexts are:
23371
23372 --A non-type template argument or an array bound in which
23373 a subexpression references a template parameter.
23374
23375 In these cases, we assume deduction succeeded, but don't
23376 actually infer any unifications. */
23377
23378 if (!uses_template_parms (parm)
23379 && !template_args_equal (parm, arg))
23380 return unify_expression_unequal (explain_p, parm, arg);
23381 else
23382 return unify_success (explain_p);
23383 }
23384 }
23385 #undef RECUR_AND_CHECK_FAILURE
23386 \f
23387 /* Note that DECL can be defined in this translation unit, if
23388 required. */
23389
23390 static void
23391 mark_definable (tree decl)
23392 {
23393 tree clone;
23394 DECL_NOT_REALLY_EXTERN (decl) = 1;
23395 FOR_EACH_CLONE (clone, decl)
23396 DECL_NOT_REALLY_EXTERN (clone) = 1;
23397 }
23398
23399 /* Called if RESULT is explicitly instantiated, or is a member of an
23400 explicitly instantiated class. */
23401
23402 void
23403 mark_decl_instantiated (tree result, int extern_p)
23404 {
23405 SET_DECL_EXPLICIT_INSTANTIATION (result);
23406
23407 /* If this entity has already been written out, it's too late to
23408 make any modifications. */
23409 if (TREE_ASM_WRITTEN (result))
23410 return;
23411
23412 /* For anonymous namespace we don't need to do anything. */
23413 if (decl_anon_ns_mem_p (result))
23414 {
23415 gcc_assert (!TREE_PUBLIC (result));
23416 return;
23417 }
23418
23419 if (TREE_CODE (result) != FUNCTION_DECL)
23420 /* The TREE_PUBLIC flag for function declarations will have been
23421 set correctly by tsubst. */
23422 TREE_PUBLIC (result) = 1;
23423
23424 /* This might have been set by an earlier implicit instantiation. */
23425 DECL_COMDAT (result) = 0;
23426
23427 if (extern_p)
23428 DECL_NOT_REALLY_EXTERN (result) = 0;
23429 else
23430 {
23431 mark_definable (result);
23432 mark_needed (result);
23433 /* Always make artificials weak. */
23434 if (DECL_ARTIFICIAL (result) && flag_weak)
23435 comdat_linkage (result);
23436 /* For WIN32 we also want to put explicit instantiations in
23437 linkonce sections. */
23438 else if (TREE_PUBLIC (result))
23439 maybe_make_one_only (result);
23440 if (TREE_CODE (result) == FUNCTION_DECL
23441 && DECL_TEMPLATE_INSTANTIATED (result))
23442 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23443 since start_preparsed_function wouldn't have if we had an earlier
23444 extern explicit instantiation. */
23445 DECL_EXTERNAL (result) = 0;
23446 }
23447
23448 /* If EXTERN_P, then this function will not be emitted -- unless
23449 followed by an explicit instantiation, at which point its linkage
23450 will be adjusted. If !EXTERN_P, then this function will be
23451 emitted here. In neither circumstance do we want
23452 import_export_decl to adjust the linkage. */
23453 DECL_INTERFACE_KNOWN (result) = 1;
23454 }
23455
23456 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23457 important template arguments. If any are missing, we check whether
23458 they're important by using error_mark_node for substituting into any
23459 args that were used for partial ordering (the ones between ARGS and END)
23460 and seeing if it bubbles up. */
23461
23462 static bool
23463 check_undeduced_parms (tree targs, tree args, tree end)
23464 {
23465 bool found = false;
23466 int i;
23467 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23468 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23469 {
23470 found = true;
23471 TREE_VEC_ELT (targs, i) = error_mark_node;
23472 }
23473 if (found)
23474 {
23475 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23476 if (substed == error_mark_node)
23477 return true;
23478 }
23479 return false;
23480 }
23481
23482 /* Given two function templates PAT1 and PAT2, return:
23483
23484 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23485 -1 if PAT2 is more specialized than PAT1.
23486 0 if neither is more specialized.
23487
23488 LEN indicates the number of parameters we should consider
23489 (defaulted parameters should not be considered).
23490
23491 The 1998 std underspecified function template partial ordering, and
23492 DR214 addresses the issue. We take pairs of arguments, one from
23493 each of the templates, and deduce them against each other. One of
23494 the templates will be more specialized if all the *other*
23495 template's arguments deduce against its arguments and at least one
23496 of its arguments *does* *not* deduce against the other template's
23497 corresponding argument. Deduction is done as for class templates.
23498 The arguments used in deduction have reference and top level cv
23499 qualifiers removed. Iff both arguments were originally reference
23500 types *and* deduction succeeds in both directions, an lvalue reference
23501 wins against an rvalue reference and otherwise the template
23502 with the more cv-qualified argument wins for that pairing (if
23503 neither is more cv-qualified, they both are equal). Unlike regular
23504 deduction, after all the arguments have been deduced in this way,
23505 we do *not* verify the deduced template argument values can be
23506 substituted into non-deduced contexts.
23507
23508 The logic can be a bit confusing here, because we look at deduce1 and
23509 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23510 can find template arguments for pat1 to make arg1 look like arg2, that
23511 means that arg2 is at least as specialized as arg1. */
23512
23513 int
23514 more_specialized_fn (tree pat1, tree pat2, int len)
23515 {
23516 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23517 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23518 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23519 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23520 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23521 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23522 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23523 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23524 tree origs1, origs2;
23525 bool lose1 = false;
23526 bool lose2 = false;
23527
23528 /* Remove the this parameter from non-static member functions. If
23529 one is a non-static member function and the other is not a static
23530 member function, remove the first parameter from that function
23531 also. This situation occurs for operator functions where we
23532 locate both a member function (with this pointer) and non-member
23533 operator (with explicit first operand). */
23534 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23535 {
23536 len--; /* LEN is the number of significant arguments for DECL1 */
23537 args1 = TREE_CHAIN (args1);
23538 if (!DECL_STATIC_FUNCTION_P (decl2))
23539 args2 = TREE_CHAIN (args2);
23540 }
23541 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23542 {
23543 args2 = TREE_CHAIN (args2);
23544 if (!DECL_STATIC_FUNCTION_P (decl1))
23545 {
23546 len--;
23547 args1 = TREE_CHAIN (args1);
23548 }
23549 }
23550
23551 /* If only one is a conversion operator, they are unordered. */
23552 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23553 return 0;
23554
23555 /* Consider the return type for a conversion function */
23556 if (DECL_CONV_FN_P (decl1))
23557 {
23558 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23559 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23560 len++;
23561 }
23562
23563 processing_template_decl++;
23564
23565 origs1 = args1;
23566 origs2 = args2;
23567
23568 while (len--
23569 /* Stop when an ellipsis is seen. */
23570 && args1 != NULL_TREE && args2 != NULL_TREE)
23571 {
23572 tree arg1 = TREE_VALUE (args1);
23573 tree arg2 = TREE_VALUE (args2);
23574 int deduce1, deduce2;
23575 int quals1 = -1;
23576 int quals2 = -1;
23577 int ref1 = 0;
23578 int ref2 = 0;
23579
23580 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23581 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23582 {
23583 /* When both arguments are pack expansions, we need only
23584 unify the patterns themselves. */
23585 arg1 = PACK_EXPANSION_PATTERN (arg1);
23586 arg2 = PACK_EXPANSION_PATTERN (arg2);
23587
23588 /* This is the last comparison we need to do. */
23589 len = 0;
23590 }
23591
23592 /* DR 1847: If a particular P contains no template-parameters that
23593 participate in template argument deduction, that P is not used to
23594 determine the ordering. */
23595 if (!uses_deducible_template_parms (arg1)
23596 && !uses_deducible_template_parms (arg2))
23597 goto next;
23598
23599 if (TYPE_REF_P (arg1))
23600 {
23601 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23602 arg1 = TREE_TYPE (arg1);
23603 quals1 = cp_type_quals (arg1);
23604 }
23605
23606 if (TYPE_REF_P (arg2))
23607 {
23608 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23609 arg2 = TREE_TYPE (arg2);
23610 quals2 = cp_type_quals (arg2);
23611 }
23612
23613 arg1 = TYPE_MAIN_VARIANT (arg1);
23614 arg2 = TYPE_MAIN_VARIANT (arg2);
23615
23616 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23617 {
23618 int i, len2 = remaining_arguments (args2);
23619 tree parmvec = make_tree_vec (1);
23620 tree argvec = make_tree_vec (len2);
23621 tree ta = args2;
23622
23623 /* Setup the parameter vector, which contains only ARG1. */
23624 TREE_VEC_ELT (parmvec, 0) = arg1;
23625
23626 /* Setup the argument vector, which contains the remaining
23627 arguments. */
23628 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23629 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23630
23631 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23632 argvec, DEDUCE_EXACT,
23633 /*subr=*/true, /*explain_p=*/false)
23634 == 0);
23635
23636 /* We cannot deduce in the other direction, because ARG1 is
23637 a pack expansion but ARG2 is not. */
23638 deduce2 = 0;
23639 }
23640 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23641 {
23642 int i, len1 = remaining_arguments (args1);
23643 tree parmvec = make_tree_vec (1);
23644 tree argvec = make_tree_vec (len1);
23645 tree ta = args1;
23646
23647 /* Setup the parameter vector, which contains only ARG1. */
23648 TREE_VEC_ELT (parmvec, 0) = arg2;
23649
23650 /* Setup the argument vector, which contains the remaining
23651 arguments. */
23652 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23653 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23654
23655 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23656 argvec, DEDUCE_EXACT,
23657 /*subr=*/true, /*explain_p=*/false)
23658 == 0);
23659
23660 /* We cannot deduce in the other direction, because ARG2 is
23661 a pack expansion but ARG1 is not.*/
23662 deduce1 = 0;
23663 }
23664
23665 else
23666 {
23667 /* The normal case, where neither argument is a pack
23668 expansion. */
23669 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23670 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23671 == 0);
23672 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23673 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23674 == 0);
23675 }
23676
23677 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23678 arg2, then arg2 is not as specialized as arg1. */
23679 if (!deduce1)
23680 lose2 = true;
23681 if (!deduce2)
23682 lose1 = true;
23683
23684 /* "If, for a given type, deduction succeeds in both directions
23685 (i.e., the types are identical after the transformations above)
23686 and both P and A were reference types (before being replaced with
23687 the type referred to above):
23688 - if the type from the argument template was an lvalue reference and
23689 the type from the parameter template was not, the argument type is
23690 considered to be more specialized than the other; otherwise,
23691 - if the type from the argument template is more cv-qualified
23692 than the type from the parameter template (as described above),
23693 the argument type is considered to be more specialized than the other;
23694 otherwise,
23695 - neither type is more specialized than the other." */
23696
23697 if (deduce1 && deduce2)
23698 {
23699 if (ref1 && ref2 && ref1 != ref2)
23700 {
23701 if (ref1 > ref2)
23702 lose1 = true;
23703 else
23704 lose2 = true;
23705 }
23706 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23707 {
23708 if ((quals1 & quals2) == quals2)
23709 lose2 = true;
23710 if ((quals1 & quals2) == quals1)
23711 lose1 = true;
23712 }
23713 }
23714
23715 if (lose1 && lose2)
23716 /* We've failed to deduce something in either direction.
23717 These must be unordered. */
23718 break;
23719
23720 next:
23721
23722 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23723 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23724 /* We have already processed all of the arguments in our
23725 handing of the pack expansion type. */
23726 len = 0;
23727
23728 args1 = TREE_CHAIN (args1);
23729 args2 = TREE_CHAIN (args2);
23730 }
23731
23732 /* "In most cases, all template parameters must have values in order for
23733 deduction to succeed, but for partial ordering purposes a template
23734 parameter may remain without a value provided it is not used in the
23735 types being used for partial ordering."
23736
23737 Thus, if we are missing any of the targs1 we need to substitute into
23738 origs1, then pat2 is not as specialized as pat1. This can happen when
23739 there is a nondeduced context. */
23740 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23741 lose2 = true;
23742 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23743 lose1 = true;
23744
23745 processing_template_decl--;
23746
23747 /* If both deductions succeed, the partial ordering selects the more
23748 constrained template. */
23749 if (!lose1 && !lose2)
23750 {
23751 int winner = more_constrained (decl1, decl2);
23752 if (winner > 0)
23753 lose2 = true;
23754 else if (winner < 0)
23755 lose1 = true;
23756 }
23757
23758 /* All things being equal, if the next argument is a pack expansion
23759 for one function but not for the other, prefer the
23760 non-variadic function. FIXME this is bogus; see c++/41958. */
23761 if (lose1 == lose2
23762 && args1 && TREE_VALUE (args1)
23763 && args2 && TREE_VALUE (args2))
23764 {
23765 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23766 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23767 }
23768
23769 if (lose1 == lose2)
23770 return 0;
23771 else if (!lose1)
23772 return 1;
23773 else
23774 return -1;
23775 }
23776
23777 /* Determine which of two partial specializations of TMPL is more
23778 specialized.
23779
23780 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23781 to the first partial specialization. The TREE_PURPOSE is the
23782 innermost set of template parameters for the partial
23783 specialization. PAT2 is similar, but for the second template.
23784
23785 Return 1 if the first partial specialization is more specialized;
23786 -1 if the second is more specialized; 0 if neither is more
23787 specialized.
23788
23789 See [temp.class.order] for information about determining which of
23790 two templates is more specialized. */
23791
23792 static int
23793 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23794 {
23795 tree targs;
23796 int winner = 0;
23797 bool any_deductions = false;
23798
23799 tree tmpl1 = TREE_VALUE (pat1);
23800 tree tmpl2 = TREE_VALUE (pat2);
23801 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23802 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23803
23804 /* Just like what happens for functions, if we are ordering between
23805 different template specializations, we may encounter dependent
23806 types in the arguments, and we need our dependency check functions
23807 to behave correctly. */
23808 ++processing_template_decl;
23809 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23810 if (targs)
23811 {
23812 --winner;
23813 any_deductions = true;
23814 }
23815
23816 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23817 if (targs)
23818 {
23819 ++winner;
23820 any_deductions = true;
23821 }
23822 --processing_template_decl;
23823
23824 /* If both deductions succeed, the partial ordering selects the more
23825 constrained template. */
23826 if (!winner && any_deductions)
23827 winner = more_constrained (tmpl1, tmpl2);
23828
23829 /* In the case of a tie where at least one of the templates
23830 has a parameter pack at the end, the template with the most
23831 non-packed parameters wins. */
23832 if (winner == 0
23833 && any_deductions
23834 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23835 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23836 {
23837 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23838 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23839 int len1 = TREE_VEC_LENGTH (args1);
23840 int len2 = TREE_VEC_LENGTH (args2);
23841
23842 /* We don't count the pack expansion at the end. */
23843 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23844 --len1;
23845 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23846 --len2;
23847
23848 if (len1 > len2)
23849 return 1;
23850 else if (len1 < len2)
23851 return -1;
23852 }
23853
23854 return winner;
23855 }
23856
23857 /* Return the template arguments that will produce the function signature
23858 DECL from the function template FN, with the explicit template
23859 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23860 also match. Return NULL_TREE if no satisfactory arguments could be
23861 found. */
23862
23863 static tree
23864 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23865 {
23866 int ntparms = DECL_NTPARMS (fn);
23867 tree targs = make_tree_vec (ntparms);
23868 tree decl_type = TREE_TYPE (decl);
23869 tree decl_arg_types;
23870 tree *args;
23871 unsigned int nargs, ix;
23872 tree arg;
23873
23874 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23875
23876 /* Never do unification on the 'this' parameter. */
23877 decl_arg_types = skip_artificial_parms_for (decl,
23878 TYPE_ARG_TYPES (decl_type));
23879
23880 nargs = list_length (decl_arg_types);
23881 args = XALLOCAVEC (tree, nargs);
23882 for (arg = decl_arg_types, ix = 0;
23883 arg != NULL_TREE && arg != void_list_node;
23884 arg = TREE_CHAIN (arg), ++ix)
23885 args[ix] = TREE_VALUE (arg);
23886
23887 if (fn_type_unification (fn, explicit_args, targs,
23888 args, ix,
23889 (check_rettype || DECL_CONV_FN_P (fn)
23890 ? TREE_TYPE (decl_type) : NULL_TREE),
23891 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23892 /*explain_p=*/false,
23893 /*decltype*/false)
23894 == error_mark_node)
23895 return NULL_TREE;
23896
23897 return targs;
23898 }
23899
23900 /* Return the innermost template arguments that, when applied to a partial
23901 specialization SPEC_TMPL of TMPL, yield the ARGS.
23902
23903 For example, suppose we have:
23904
23905 template <class T, class U> struct S {};
23906 template <class T> struct S<T*, int> {};
23907
23908 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23909 partial specialization and the ARGS will be {double*, int}. The resulting
23910 vector will be {double}, indicating that `T' is bound to `double'. */
23911
23912 static tree
23913 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23914 {
23915 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23916 tree spec_args
23917 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23918 int i, ntparms = TREE_VEC_LENGTH (tparms);
23919 tree deduced_args;
23920 tree innermost_deduced_args;
23921
23922 innermost_deduced_args = make_tree_vec (ntparms);
23923 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23924 {
23925 deduced_args = copy_node (args);
23926 SET_TMPL_ARGS_LEVEL (deduced_args,
23927 TMPL_ARGS_DEPTH (deduced_args),
23928 innermost_deduced_args);
23929 }
23930 else
23931 deduced_args = innermost_deduced_args;
23932
23933 bool tried_array_deduction = (cxx_dialect < cxx17);
23934 again:
23935 if (unify (tparms, deduced_args,
23936 INNERMOST_TEMPLATE_ARGS (spec_args),
23937 INNERMOST_TEMPLATE_ARGS (args),
23938 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23939 return NULL_TREE;
23940
23941 for (i = 0; i < ntparms; ++i)
23942 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23943 {
23944 if (!tried_array_deduction)
23945 {
23946 try_array_deduction (tparms, innermost_deduced_args,
23947 INNERMOST_TEMPLATE_ARGS (spec_args));
23948 tried_array_deduction = true;
23949 if (TREE_VEC_ELT (innermost_deduced_args, i))
23950 goto again;
23951 }
23952 return NULL_TREE;
23953 }
23954
23955 if (!push_tinst_level (spec_tmpl, deduced_args))
23956 {
23957 excessive_deduction_depth = true;
23958 return NULL_TREE;
23959 }
23960
23961 /* Verify that nondeduced template arguments agree with the type
23962 obtained from argument deduction.
23963
23964 For example:
23965
23966 struct A { typedef int X; };
23967 template <class T, class U> struct C {};
23968 template <class T> struct C<T, typename T::X> {};
23969
23970 Then with the instantiation `C<A, int>', we can deduce that
23971 `T' is `A' but unify () does not check whether `typename T::X'
23972 is `int'. */
23973 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23974
23975 if (spec_args != error_mark_node)
23976 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23977 INNERMOST_TEMPLATE_ARGS (spec_args),
23978 tmpl, tf_none, false, false);
23979
23980 pop_tinst_level ();
23981
23982 if (spec_args == error_mark_node
23983 /* We only need to check the innermost arguments; the other
23984 arguments will always agree. */
23985 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23986 INNERMOST_TEMPLATE_ARGS (args)))
23987 return NULL_TREE;
23988
23989 /* Now that we have bindings for all of the template arguments,
23990 ensure that the arguments deduced for the template template
23991 parameters have compatible template parameter lists. See the use
23992 of template_template_parm_bindings_ok_p in fn_type_unification
23993 for more information. */
23994 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23995 return NULL_TREE;
23996
23997 return deduced_args;
23998 }
23999
24000 // Compare two function templates T1 and T2 by deducing bindings
24001 // from one against the other. If both deductions succeed, compare
24002 // constraints to see which is more constrained.
24003 static int
24004 more_specialized_inst (tree t1, tree t2)
24005 {
24006 int fate = 0;
24007 int count = 0;
24008
24009 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24010 {
24011 --fate;
24012 ++count;
24013 }
24014
24015 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24016 {
24017 ++fate;
24018 ++count;
24019 }
24020
24021 // If both deductions succeed, then one may be more constrained.
24022 if (count == 2 && fate == 0)
24023 fate = more_constrained (t1, t2);
24024
24025 return fate;
24026 }
24027
24028 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24029 Return the TREE_LIST node with the most specialized template, if
24030 any. If there is no most specialized template, the error_mark_node
24031 is returned.
24032
24033 Note that this function does not look at, or modify, the
24034 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24035 returned is one of the elements of INSTANTIATIONS, callers may
24036 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24037 and retrieve it from the value returned. */
24038
24039 tree
24040 most_specialized_instantiation (tree templates)
24041 {
24042 tree fn, champ;
24043
24044 ++processing_template_decl;
24045
24046 champ = templates;
24047 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24048 {
24049 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24050 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24051 if (fate == -1)
24052 champ = fn;
24053 else if (!fate)
24054 {
24055 /* Equally specialized, move to next function. If there
24056 is no next function, nothing's most specialized. */
24057 fn = TREE_CHAIN (fn);
24058 champ = fn;
24059 if (!fn)
24060 break;
24061 }
24062 }
24063
24064 if (champ)
24065 /* Now verify that champ is better than everything earlier in the
24066 instantiation list. */
24067 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24068 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24069 {
24070 champ = NULL_TREE;
24071 break;
24072 }
24073 }
24074
24075 processing_template_decl--;
24076
24077 if (!champ)
24078 return error_mark_node;
24079
24080 return champ;
24081 }
24082
24083 /* If DECL is a specialization of some template, return the most
24084 general such template. Otherwise, returns NULL_TREE.
24085
24086 For example, given:
24087
24088 template <class T> struct S { template <class U> void f(U); };
24089
24090 if TMPL is `template <class U> void S<int>::f(U)' this will return
24091 the full template. This function will not trace past partial
24092 specializations, however. For example, given in addition:
24093
24094 template <class T> struct S<T*> { template <class U> void f(U); };
24095
24096 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24097 `template <class T> template <class U> S<T*>::f(U)'. */
24098
24099 tree
24100 most_general_template (tree decl)
24101 {
24102 if (TREE_CODE (decl) != TEMPLATE_DECL)
24103 {
24104 if (tree tinfo = get_template_info (decl))
24105 decl = TI_TEMPLATE (tinfo);
24106 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24107 template friend, or a FIELD_DECL for a capture pack. */
24108 if (TREE_CODE (decl) != TEMPLATE_DECL)
24109 return NULL_TREE;
24110 }
24111
24112 /* Look for more and more general templates. */
24113 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24114 {
24115 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24116 (See cp-tree.h for details.) */
24117 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24118 break;
24119
24120 if (CLASS_TYPE_P (TREE_TYPE (decl))
24121 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24122 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24123 break;
24124
24125 /* Stop if we run into an explicitly specialized class template. */
24126 if (!DECL_NAMESPACE_SCOPE_P (decl)
24127 && DECL_CONTEXT (decl)
24128 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24129 break;
24130
24131 decl = DECL_TI_TEMPLATE (decl);
24132 }
24133
24134 return decl;
24135 }
24136
24137 /* Return the most specialized of the template partial specializations
24138 which can produce TARGET, a specialization of some class or variable
24139 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24140 a TEMPLATE_DECL node corresponding to the partial specialization, while
24141 the TREE_PURPOSE is the set of template arguments that must be
24142 substituted into the template pattern in order to generate TARGET.
24143
24144 If the choice of partial specialization is ambiguous, a diagnostic
24145 is issued, and the error_mark_node is returned. If there are no
24146 partial specializations matching TARGET, then NULL_TREE is
24147 returned, indicating that the primary template should be used. */
24148
24149 static tree
24150 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24151 {
24152 tree list = NULL_TREE;
24153 tree t;
24154 tree champ;
24155 int fate;
24156 bool ambiguous_p;
24157 tree outer_args = NULL_TREE;
24158 tree tmpl, args;
24159
24160 if (TYPE_P (target))
24161 {
24162 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24163 tmpl = TI_TEMPLATE (tinfo);
24164 args = TI_ARGS (tinfo);
24165 }
24166 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24167 {
24168 tmpl = TREE_OPERAND (target, 0);
24169 args = TREE_OPERAND (target, 1);
24170 }
24171 else if (VAR_P (target))
24172 {
24173 tree tinfo = DECL_TEMPLATE_INFO (target);
24174 tmpl = TI_TEMPLATE (tinfo);
24175 args = TI_ARGS (tinfo);
24176 }
24177 else
24178 gcc_unreachable ();
24179
24180 tree main_tmpl = most_general_template (tmpl);
24181
24182 /* For determining which partial specialization to use, only the
24183 innermost args are interesting. */
24184 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24185 {
24186 outer_args = strip_innermost_template_args (args, 1);
24187 args = INNERMOST_TEMPLATE_ARGS (args);
24188 }
24189
24190 /* The caller hasn't called push_to_top_level yet, but we need
24191 get_partial_spec_bindings to be done in non-template context so that we'll
24192 fully resolve everything. */
24193 processing_template_decl_sentinel ptds;
24194
24195 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24196 {
24197 tree spec_args;
24198 tree spec_tmpl = TREE_VALUE (t);
24199
24200 if (outer_args)
24201 {
24202 /* Substitute in the template args from the enclosing class. */
24203 ++processing_template_decl;
24204 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24205 --processing_template_decl;
24206 }
24207
24208 if (spec_tmpl == error_mark_node)
24209 return error_mark_node;
24210
24211 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24212 if (spec_args)
24213 {
24214 if (outer_args)
24215 spec_args = add_to_template_args (outer_args, spec_args);
24216
24217 /* Keep the candidate only if the constraints are satisfied,
24218 or if we're not compiling with concepts. */
24219 if (!flag_concepts
24220 || constraints_satisfied_p (spec_tmpl, spec_args))
24221 {
24222 list = tree_cons (spec_args, TREE_VALUE (t), list);
24223 TREE_TYPE (list) = TREE_TYPE (t);
24224 }
24225 }
24226 }
24227
24228 if (! list)
24229 return NULL_TREE;
24230
24231 ambiguous_p = false;
24232 t = list;
24233 champ = t;
24234 t = TREE_CHAIN (t);
24235 for (; t; t = TREE_CHAIN (t))
24236 {
24237 fate = more_specialized_partial_spec (tmpl, champ, t);
24238 if (fate == 1)
24239 ;
24240 else
24241 {
24242 if (fate == 0)
24243 {
24244 t = TREE_CHAIN (t);
24245 if (! t)
24246 {
24247 ambiguous_p = true;
24248 break;
24249 }
24250 }
24251 champ = t;
24252 }
24253 }
24254
24255 if (!ambiguous_p)
24256 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24257 {
24258 fate = more_specialized_partial_spec (tmpl, champ, t);
24259 if (fate != 1)
24260 {
24261 ambiguous_p = true;
24262 break;
24263 }
24264 }
24265
24266 if (ambiguous_p)
24267 {
24268 const char *str;
24269 char *spaces = NULL;
24270 if (!(complain & tf_error))
24271 return error_mark_node;
24272 if (TYPE_P (target))
24273 error ("ambiguous template instantiation for %q#T", target);
24274 else
24275 error ("ambiguous template instantiation for %q#D", target);
24276 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24277 for (t = list; t; t = TREE_CHAIN (t))
24278 {
24279 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24280 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24281 "%s %#qS", spaces ? spaces : str, subst);
24282 spaces = spaces ? spaces : get_spaces (str);
24283 }
24284 free (spaces);
24285 return error_mark_node;
24286 }
24287
24288 return champ;
24289 }
24290
24291 /* Explicitly instantiate DECL. */
24292
24293 void
24294 do_decl_instantiation (tree decl, tree storage)
24295 {
24296 tree result = NULL_TREE;
24297 int extern_p = 0;
24298
24299 if (!decl || decl == error_mark_node)
24300 /* An error occurred, for which grokdeclarator has already issued
24301 an appropriate message. */
24302 return;
24303 else if (! DECL_LANG_SPECIFIC (decl))
24304 {
24305 error ("explicit instantiation of non-template %q#D", decl);
24306 return;
24307 }
24308 else if (DECL_DECLARED_CONCEPT_P (decl))
24309 {
24310 if (VAR_P (decl))
24311 error ("explicit instantiation of variable concept %q#D", decl);
24312 else
24313 error ("explicit instantiation of function concept %q#D", decl);
24314 return;
24315 }
24316
24317 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24318 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24319
24320 if (VAR_P (decl) && !var_templ)
24321 {
24322 /* There is an asymmetry here in the way VAR_DECLs and
24323 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24324 the latter, the DECL we get back will be marked as a
24325 template instantiation, and the appropriate
24326 DECL_TEMPLATE_INFO will be set up. This does not happen for
24327 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24328 should handle VAR_DECLs as it currently handles
24329 FUNCTION_DECLs. */
24330 if (!DECL_CLASS_SCOPE_P (decl))
24331 {
24332 error ("%qD is not a static data member of a class template", decl);
24333 return;
24334 }
24335 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24336 if (!result || !VAR_P (result))
24337 {
24338 error ("no matching template for %qD found", decl);
24339 return;
24340 }
24341 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24342 {
24343 error ("type %qT for explicit instantiation %qD does not match "
24344 "declared type %qT", TREE_TYPE (result), decl,
24345 TREE_TYPE (decl));
24346 return;
24347 }
24348 }
24349 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24350 {
24351 error ("explicit instantiation of %q#D", decl);
24352 return;
24353 }
24354 else
24355 result = decl;
24356
24357 /* Check for various error cases. Note that if the explicit
24358 instantiation is valid the RESULT will currently be marked as an
24359 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24360 until we get here. */
24361
24362 if (DECL_TEMPLATE_SPECIALIZATION (result))
24363 {
24364 /* DR 259 [temp.spec].
24365
24366 Both an explicit instantiation and a declaration of an explicit
24367 specialization shall not appear in a program unless the explicit
24368 instantiation follows a declaration of the explicit specialization.
24369
24370 For a given set of template parameters, if an explicit
24371 instantiation of a template appears after a declaration of an
24372 explicit specialization for that template, the explicit
24373 instantiation has no effect. */
24374 return;
24375 }
24376 else if (DECL_EXPLICIT_INSTANTIATION (result))
24377 {
24378 /* [temp.spec]
24379
24380 No program shall explicitly instantiate any template more
24381 than once.
24382
24383 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24384 the first instantiation was `extern' and the second is not,
24385 and EXTERN_P for the opposite case. */
24386 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24387 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24388 /* If an "extern" explicit instantiation follows an ordinary
24389 explicit instantiation, the template is instantiated. */
24390 if (extern_p)
24391 return;
24392 }
24393 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24394 {
24395 error ("no matching template for %qD found", result);
24396 return;
24397 }
24398 else if (!DECL_TEMPLATE_INFO (result))
24399 {
24400 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24401 return;
24402 }
24403
24404 if (storage == NULL_TREE)
24405 ;
24406 else if (storage == ridpointers[(int) RID_EXTERN])
24407 {
24408 if (cxx_dialect == cxx98)
24409 pedwarn (input_location, OPT_Wpedantic,
24410 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24411 "instantiations");
24412 extern_p = 1;
24413 }
24414 else
24415 error ("storage class %qD applied to template instantiation", storage);
24416
24417 check_explicit_instantiation_namespace (result);
24418 mark_decl_instantiated (result, extern_p);
24419 if (! extern_p)
24420 instantiate_decl (result, /*defer_ok=*/true,
24421 /*expl_inst_class_mem_p=*/false);
24422 }
24423
24424 static void
24425 mark_class_instantiated (tree t, int extern_p)
24426 {
24427 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24428 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24429 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24430 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24431 if (! extern_p)
24432 {
24433 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24434 rest_of_type_compilation (t, 1);
24435 }
24436 }
24437
24438 /* Called from do_type_instantiation through binding_table_foreach to
24439 do recursive instantiation for the type bound in ENTRY. */
24440 static void
24441 bt_instantiate_type_proc (binding_entry entry, void *data)
24442 {
24443 tree storage = *(tree *) data;
24444
24445 if (MAYBE_CLASS_TYPE_P (entry->type)
24446 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24447 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24448 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24449 }
24450
24451 /* Perform an explicit instantiation of template class T. STORAGE, if
24452 non-null, is the RID for extern, inline or static. COMPLAIN is
24453 nonzero if this is called from the parser, zero if called recursively,
24454 since the standard is unclear (as detailed below). */
24455
24456 void
24457 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24458 {
24459 int extern_p = 0;
24460 int nomem_p = 0;
24461 int static_p = 0;
24462 int previous_instantiation_extern_p = 0;
24463
24464 if (TREE_CODE (t) == TYPE_DECL)
24465 t = TREE_TYPE (t);
24466
24467 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24468 {
24469 tree tmpl =
24470 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24471 if (tmpl)
24472 error ("explicit instantiation of non-class template %qD", tmpl);
24473 else
24474 error ("explicit instantiation of non-template type %qT", t);
24475 return;
24476 }
24477
24478 complete_type (t);
24479
24480 if (!COMPLETE_TYPE_P (t))
24481 {
24482 if (complain & tf_error)
24483 error ("explicit instantiation of %q#T before definition of template",
24484 t);
24485 return;
24486 }
24487
24488 if (storage != NULL_TREE)
24489 {
24490 if (storage == ridpointers[(int) RID_EXTERN])
24491 {
24492 if (cxx_dialect == cxx98)
24493 pedwarn (input_location, OPT_Wpedantic,
24494 "ISO C++ 1998 forbids the use of %<extern%> on "
24495 "explicit instantiations");
24496 }
24497 else
24498 pedwarn (input_location, OPT_Wpedantic,
24499 "ISO C++ forbids the use of %qE"
24500 " on explicit instantiations", storage);
24501
24502 if (storage == ridpointers[(int) RID_INLINE])
24503 nomem_p = 1;
24504 else if (storage == ridpointers[(int) RID_EXTERN])
24505 extern_p = 1;
24506 else if (storage == ridpointers[(int) RID_STATIC])
24507 static_p = 1;
24508 else
24509 {
24510 error ("storage class %qD applied to template instantiation",
24511 storage);
24512 extern_p = 0;
24513 }
24514 }
24515
24516 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24517 {
24518 /* DR 259 [temp.spec].
24519
24520 Both an explicit instantiation and a declaration of an explicit
24521 specialization shall not appear in a program unless the explicit
24522 instantiation follows a declaration of the explicit specialization.
24523
24524 For a given set of template parameters, if an explicit
24525 instantiation of a template appears after a declaration of an
24526 explicit specialization for that template, the explicit
24527 instantiation has no effect. */
24528 return;
24529 }
24530 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24531 {
24532 /* [temp.spec]
24533
24534 No program shall explicitly instantiate any template more
24535 than once.
24536
24537 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24538 instantiation was `extern'. If EXTERN_P then the second is.
24539 These cases are OK. */
24540 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24541
24542 if (!previous_instantiation_extern_p && !extern_p
24543 && (complain & tf_error))
24544 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24545
24546 /* If we've already instantiated the template, just return now. */
24547 if (!CLASSTYPE_INTERFACE_ONLY (t))
24548 return;
24549 }
24550
24551 check_explicit_instantiation_namespace (TYPE_NAME (t));
24552 mark_class_instantiated (t, extern_p);
24553
24554 if (nomem_p)
24555 return;
24556
24557 /* In contrast to implicit instantiation, where only the
24558 declarations, and not the definitions, of members are
24559 instantiated, we have here:
24560
24561 [temp.explicit]
24562
24563 The explicit instantiation of a class template specialization
24564 implies the instantiation of all of its members not
24565 previously explicitly specialized in the translation unit
24566 containing the explicit instantiation.
24567
24568 Of course, we can't instantiate member template classes, since we
24569 don't have any arguments for them. Note that the standard is
24570 unclear on whether the instantiation of the members are
24571 *explicit* instantiations or not. However, the most natural
24572 interpretation is that it should be an explicit
24573 instantiation. */
24574 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24575 if ((VAR_P (fld)
24576 || (TREE_CODE (fld) == FUNCTION_DECL
24577 && !static_p
24578 && user_provided_p (fld)))
24579 && DECL_TEMPLATE_INSTANTIATION (fld))
24580 {
24581 mark_decl_instantiated (fld, extern_p);
24582 if (! extern_p)
24583 instantiate_decl (fld, /*defer_ok=*/true,
24584 /*expl_inst_class_mem_p=*/true);
24585 }
24586
24587 if (CLASSTYPE_NESTED_UTDS (t))
24588 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24589 bt_instantiate_type_proc, &storage);
24590 }
24591
24592 /* Given a function DECL, which is a specialization of TMPL, modify
24593 DECL to be a re-instantiation of TMPL with the same template
24594 arguments. TMPL should be the template into which tsubst'ing
24595 should occur for DECL, not the most general template.
24596
24597 One reason for doing this is a scenario like this:
24598
24599 template <class T>
24600 void f(const T&, int i);
24601
24602 void g() { f(3, 7); }
24603
24604 template <class T>
24605 void f(const T& t, const int i) { }
24606
24607 Note that when the template is first instantiated, with
24608 instantiate_template, the resulting DECL will have no name for the
24609 first parameter, and the wrong type for the second. So, when we go
24610 to instantiate the DECL, we regenerate it. */
24611
24612 static void
24613 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24614 {
24615 /* The arguments used to instantiate DECL, from the most general
24616 template. */
24617 tree code_pattern;
24618
24619 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24620
24621 /* Make sure that we can see identifiers, and compute access
24622 correctly. */
24623 push_access_scope (decl);
24624
24625 if (TREE_CODE (decl) == FUNCTION_DECL)
24626 {
24627 tree decl_parm;
24628 tree pattern_parm;
24629 tree specs;
24630 int args_depth;
24631 int parms_depth;
24632
24633 args_depth = TMPL_ARGS_DEPTH (args);
24634 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24635 if (args_depth > parms_depth)
24636 args = get_innermost_template_args (args, parms_depth);
24637
24638 /* Instantiate a dynamic exception-specification. noexcept will be
24639 handled below. */
24640 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24641 if (TREE_VALUE (raises))
24642 {
24643 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24644 args, tf_error, NULL_TREE,
24645 /*defer_ok*/false);
24646 if (specs && specs != error_mark_node)
24647 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24648 specs);
24649 }
24650
24651 /* Merge parameter declarations. */
24652 decl_parm = skip_artificial_parms_for (decl,
24653 DECL_ARGUMENTS (decl));
24654 pattern_parm
24655 = skip_artificial_parms_for (code_pattern,
24656 DECL_ARGUMENTS (code_pattern));
24657 while (decl_parm && !DECL_PACK_P (pattern_parm))
24658 {
24659 tree parm_type;
24660 tree attributes;
24661
24662 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24663 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24664 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24665 NULL_TREE);
24666 parm_type = type_decays_to (parm_type);
24667 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24668 TREE_TYPE (decl_parm) = parm_type;
24669 attributes = DECL_ATTRIBUTES (pattern_parm);
24670 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24671 {
24672 DECL_ATTRIBUTES (decl_parm) = attributes;
24673 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24674 }
24675 decl_parm = DECL_CHAIN (decl_parm);
24676 pattern_parm = DECL_CHAIN (pattern_parm);
24677 }
24678 /* Merge any parameters that match with the function parameter
24679 pack. */
24680 if (pattern_parm && DECL_PACK_P (pattern_parm))
24681 {
24682 int i, len;
24683 tree expanded_types;
24684 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24685 the parameters in this function parameter pack. */
24686 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24687 args, tf_error, NULL_TREE);
24688 len = TREE_VEC_LENGTH (expanded_types);
24689 for (i = 0; i < len; i++)
24690 {
24691 tree parm_type;
24692 tree attributes;
24693
24694 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24695 /* Rename the parameter to include the index. */
24696 DECL_NAME (decl_parm) =
24697 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24698 parm_type = TREE_VEC_ELT (expanded_types, i);
24699 parm_type = type_decays_to (parm_type);
24700 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24701 TREE_TYPE (decl_parm) = parm_type;
24702 attributes = DECL_ATTRIBUTES (pattern_parm);
24703 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24704 {
24705 DECL_ATTRIBUTES (decl_parm) = attributes;
24706 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24707 }
24708 decl_parm = DECL_CHAIN (decl_parm);
24709 }
24710 }
24711 /* Merge additional specifiers from the CODE_PATTERN. */
24712 if (DECL_DECLARED_INLINE_P (code_pattern)
24713 && !DECL_DECLARED_INLINE_P (decl))
24714 DECL_DECLARED_INLINE_P (decl) = 1;
24715
24716 maybe_instantiate_noexcept (decl, tf_error);
24717 }
24718 else if (VAR_P (decl))
24719 {
24720 start_lambda_scope (decl);
24721 DECL_INITIAL (decl) =
24722 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24723 tf_error, DECL_TI_TEMPLATE (decl));
24724 finish_lambda_scope ();
24725 if (VAR_HAD_UNKNOWN_BOUND (decl))
24726 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24727 tf_error, DECL_TI_TEMPLATE (decl));
24728 }
24729 else
24730 gcc_unreachable ();
24731
24732 pop_access_scope (decl);
24733 }
24734
24735 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24736 substituted to get DECL. */
24737
24738 tree
24739 template_for_substitution (tree decl)
24740 {
24741 tree tmpl = DECL_TI_TEMPLATE (decl);
24742
24743 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24744 for the instantiation. This is not always the most general
24745 template. Consider, for example:
24746
24747 template <class T>
24748 struct S { template <class U> void f();
24749 template <> void f<int>(); };
24750
24751 and an instantiation of S<double>::f<int>. We want TD to be the
24752 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24753 while (/* An instantiation cannot have a definition, so we need a
24754 more general template. */
24755 DECL_TEMPLATE_INSTANTIATION (tmpl)
24756 /* We must also deal with friend templates. Given:
24757
24758 template <class T> struct S {
24759 template <class U> friend void f() {};
24760 };
24761
24762 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24763 so far as the language is concerned, but that's still
24764 where we get the pattern for the instantiation from. On
24765 other hand, if the definition comes outside the class, say:
24766
24767 template <class T> struct S {
24768 template <class U> friend void f();
24769 };
24770 template <class U> friend void f() {}
24771
24772 we don't need to look any further. That's what the check for
24773 DECL_INITIAL is for. */
24774 || (TREE_CODE (decl) == FUNCTION_DECL
24775 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24776 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24777 {
24778 /* The present template, TD, should not be a definition. If it
24779 were a definition, we should be using it! Note that we
24780 cannot restructure the loop to just keep going until we find
24781 a template with a definition, since that might go too far if
24782 a specialization was declared, but not defined. */
24783
24784 /* Fetch the more general template. */
24785 tmpl = DECL_TI_TEMPLATE (tmpl);
24786 }
24787
24788 return tmpl;
24789 }
24790
24791 /* Returns true if we need to instantiate this template instance even if we
24792 know we aren't going to emit it. */
24793
24794 bool
24795 always_instantiate_p (tree decl)
24796 {
24797 /* We always instantiate inline functions so that we can inline them. An
24798 explicit instantiation declaration prohibits implicit instantiation of
24799 non-inline functions. With high levels of optimization, we would
24800 normally inline non-inline functions -- but we're not allowed to do
24801 that for "extern template" functions. Therefore, we check
24802 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24803 return ((TREE_CODE (decl) == FUNCTION_DECL
24804 && (DECL_DECLARED_INLINE_P (decl)
24805 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24806 /* And we need to instantiate static data members so that
24807 their initializers are available in integral constant
24808 expressions. */
24809 || (VAR_P (decl)
24810 && decl_maybe_constant_var_p (decl)));
24811 }
24812
24813 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24814 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24815 error, true otherwise. */
24816
24817 bool
24818 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24819 {
24820 tree fntype, spec, noex, clone;
24821
24822 /* Don't instantiate a noexcept-specification from template context. */
24823 if (processing_template_decl
24824 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24825 return true;
24826
24827 if (DECL_CLONED_FUNCTION_P (fn))
24828 fn = DECL_CLONED_FUNCTION (fn);
24829
24830 tree orig_fn = NULL_TREE;
24831 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24832 its FUNCTION_DECL for the rest of this function -- push_access_scope
24833 doesn't accept TEMPLATE_DECLs. */
24834 if (DECL_FUNCTION_TEMPLATE_P (fn))
24835 {
24836 orig_fn = fn;
24837 fn = DECL_TEMPLATE_RESULT (fn);
24838 }
24839
24840 fntype = TREE_TYPE (fn);
24841 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24842
24843 if (!spec || !TREE_PURPOSE (spec))
24844 return true;
24845
24846 noex = TREE_PURPOSE (spec);
24847
24848 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24849 {
24850 static hash_set<tree>* fns = new hash_set<tree>;
24851 bool added = false;
24852 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24853 {
24854 spec = get_defaulted_eh_spec (fn, complain);
24855 if (spec == error_mark_node)
24856 /* This might have failed because of an unparsed DMI, so
24857 let's try again later. */
24858 return false;
24859 }
24860 else if (!(added = !fns->add (fn)))
24861 {
24862 /* If hash_set::add returns true, the element was already there. */
24863 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24864 DECL_SOURCE_LOCATION (fn));
24865 error_at (loc,
24866 "exception specification of %qD depends on itself",
24867 fn);
24868 spec = noexcept_false_spec;
24869 }
24870 else if (push_tinst_level (fn))
24871 {
24872 push_to_top_level ();
24873 push_access_scope (fn);
24874 push_deferring_access_checks (dk_no_deferred);
24875 input_location = DECL_SOURCE_LOCATION (fn);
24876
24877 /* If needed, set current_class_ptr for the benefit of
24878 tsubst_copy/PARM_DECL. */
24879 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24880 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24881 {
24882 tree this_parm = DECL_ARGUMENTS (tdecl);
24883 current_class_ptr = NULL_TREE;
24884 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24885 current_class_ptr = this_parm;
24886 }
24887
24888 /* If this function is represented by a TEMPLATE_DECL, then
24889 the deferred noexcept-specification might still contain
24890 dependent types, even after substitution. And we need the
24891 dependency check functions to work in build_noexcept_spec. */
24892 if (orig_fn)
24893 ++processing_template_decl;
24894
24895 /* Do deferred instantiation of the noexcept-specifier. */
24896 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24897 DEFERRED_NOEXCEPT_ARGS (noex),
24898 tf_warning_or_error, fn,
24899 /*function_p=*/false,
24900 /*i_c_e_p=*/true);
24901
24902 /* Build up the noexcept-specification. */
24903 spec = build_noexcept_spec (noex, tf_warning_or_error);
24904
24905 if (orig_fn)
24906 --processing_template_decl;
24907
24908 pop_deferring_access_checks ();
24909 pop_access_scope (fn);
24910 pop_tinst_level ();
24911 pop_from_top_level ();
24912 }
24913 else
24914 spec = noexcept_false_spec;
24915
24916 if (added)
24917 fns->remove (fn);
24918
24919 if (spec == error_mark_node)
24920 {
24921 /* This failed with a hard error, so let's go with false. */
24922 gcc_assert (seen_error ());
24923 spec = noexcept_false_spec;
24924 }
24925
24926 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24927 if (orig_fn)
24928 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24929
24930 FOR_EACH_CLONE (clone, fn)
24931 {
24932 if (TREE_TYPE (clone) == fntype)
24933 TREE_TYPE (clone) = TREE_TYPE (fn);
24934 else
24935 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24936 }
24937 }
24938
24939 return true;
24940 }
24941
24942 /* We're starting to process the function INST, an instantiation of PATTERN;
24943 add their parameters to local_specializations. */
24944
24945 static void
24946 register_parameter_specializations (tree pattern, tree inst)
24947 {
24948 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24949 tree spec_parm = DECL_ARGUMENTS (inst);
24950 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24951 {
24952 register_local_specialization (spec_parm, tmpl_parm);
24953 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24954 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24955 }
24956 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24957 {
24958 if (!DECL_PACK_P (tmpl_parm))
24959 {
24960 register_local_specialization (spec_parm, tmpl_parm);
24961 spec_parm = DECL_CHAIN (spec_parm);
24962 }
24963 else
24964 {
24965 /* Register the (value) argument pack as a specialization of
24966 TMPL_PARM, then move on. */
24967 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24968 register_local_specialization (argpack, tmpl_parm);
24969 }
24970 }
24971 gcc_assert (!spec_parm);
24972 }
24973
24974 /* Produce the definition of D, a _DECL generated from a template. If
24975 DEFER_OK is true, then we don't have to actually do the
24976 instantiation now; we just have to do it sometime. Normally it is
24977 an error if this is an explicit instantiation but D is undefined.
24978 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24979 instantiated class template. */
24980
24981 tree
24982 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24983 {
24984 tree tmpl = DECL_TI_TEMPLATE (d);
24985 tree gen_args;
24986 tree args;
24987 tree td;
24988 tree code_pattern;
24989 tree spec;
24990 tree gen_tmpl;
24991 bool pattern_defined;
24992 location_t saved_loc = input_location;
24993 int saved_unevaluated_operand = cp_unevaluated_operand;
24994 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24995 bool external_p;
24996 bool deleted_p;
24997
24998 /* This function should only be used to instantiate templates for
24999 functions and static member variables. */
25000 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25001
25002 /* A concept is never instantiated. */
25003 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25004
25005 /* Variables are never deferred; if instantiation is required, they
25006 are instantiated right away. That allows for better code in the
25007 case that an expression refers to the value of the variable --
25008 if the variable has a constant value the referring expression can
25009 take advantage of that fact. */
25010 if (VAR_P (d))
25011 defer_ok = false;
25012
25013 /* Don't instantiate cloned functions. Instead, instantiate the
25014 functions they cloned. */
25015 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25016 d = DECL_CLONED_FUNCTION (d);
25017
25018 if (DECL_TEMPLATE_INSTANTIATED (d)
25019 || (TREE_CODE (d) == FUNCTION_DECL
25020 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25021 || DECL_TEMPLATE_SPECIALIZATION (d))
25022 /* D has already been instantiated or explicitly specialized, so
25023 there's nothing for us to do here.
25024
25025 It might seem reasonable to check whether or not D is an explicit
25026 instantiation, and, if so, stop here. But when an explicit
25027 instantiation is deferred until the end of the compilation,
25028 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25029 the instantiation. */
25030 return d;
25031
25032 /* Check to see whether we know that this template will be
25033 instantiated in some other file, as with "extern template"
25034 extension. */
25035 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25036
25037 /* In general, we do not instantiate such templates. */
25038 if (external_p && !always_instantiate_p (d))
25039 return d;
25040
25041 gen_tmpl = most_general_template (tmpl);
25042 gen_args = DECL_TI_ARGS (d);
25043
25044 if (tmpl != gen_tmpl)
25045 /* We should already have the extra args. */
25046 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25047 == TMPL_ARGS_DEPTH (gen_args));
25048 /* And what's in the hash table should match D. */
25049 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25050 || spec == NULL_TREE);
25051
25052 /* This needs to happen before any tsubsting. */
25053 if (! push_tinst_level (d))
25054 return d;
25055
25056 timevar_push (TV_TEMPLATE_INST);
25057
25058 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25059 for the instantiation. */
25060 td = template_for_substitution (d);
25061 args = gen_args;
25062
25063 if (VAR_P (d))
25064 {
25065 /* Look up an explicit specialization, if any. */
25066 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25067 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25068 if (elt && elt != error_mark_node)
25069 {
25070 td = TREE_VALUE (elt);
25071 args = TREE_PURPOSE (elt);
25072 }
25073 }
25074
25075 code_pattern = DECL_TEMPLATE_RESULT (td);
25076
25077 /* We should never be trying to instantiate a member of a class
25078 template or partial specialization. */
25079 gcc_assert (d != code_pattern);
25080
25081 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25082 || DECL_TEMPLATE_SPECIALIZATION (td))
25083 /* In the case of a friend template whose definition is provided
25084 outside the class, we may have too many arguments. Drop the
25085 ones we don't need. The same is true for specializations. */
25086 args = get_innermost_template_args
25087 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25088
25089 if (TREE_CODE (d) == FUNCTION_DECL)
25090 {
25091 deleted_p = DECL_DELETED_FN (code_pattern);
25092 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25093 && DECL_INITIAL (code_pattern) != error_mark_node)
25094 || DECL_DEFAULTED_FN (code_pattern)
25095 || deleted_p);
25096 }
25097 else
25098 {
25099 deleted_p = false;
25100 if (DECL_CLASS_SCOPE_P (code_pattern))
25101 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25102 else
25103 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25104 }
25105
25106 /* We may be in the middle of deferred access check. Disable it now. */
25107 push_deferring_access_checks (dk_no_deferred);
25108
25109 /* Unless an explicit instantiation directive has already determined
25110 the linkage of D, remember that a definition is available for
25111 this entity. */
25112 if (pattern_defined
25113 && !DECL_INTERFACE_KNOWN (d)
25114 && !DECL_NOT_REALLY_EXTERN (d))
25115 mark_definable (d);
25116
25117 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25118 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25119 input_location = DECL_SOURCE_LOCATION (d);
25120
25121 /* If D is a member of an explicitly instantiated class template,
25122 and no definition is available, treat it like an implicit
25123 instantiation. */
25124 if (!pattern_defined && expl_inst_class_mem_p
25125 && DECL_EXPLICIT_INSTANTIATION (d))
25126 {
25127 /* Leave linkage flags alone on instantiations with anonymous
25128 visibility. */
25129 if (TREE_PUBLIC (d))
25130 {
25131 DECL_NOT_REALLY_EXTERN (d) = 0;
25132 DECL_INTERFACE_KNOWN (d) = 0;
25133 }
25134 SET_DECL_IMPLICIT_INSTANTIATION (d);
25135 }
25136
25137 /* Defer all other templates, unless we have been explicitly
25138 forbidden from doing so. */
25139 if (/* If there is no definition, we cannot instantiate the
25140 template. */
25141 ! pattern_defined
25142 /* If it's OK to postpone instantiation, do so. */
25143 || defer_ok
25144 /* If this is a static data member that will be defined
25145 elsewhere, we don't want to instantiate the entire data
25146 member, but we do want to instantiate the initializer so that
25147 we can substitute that elsewhere. */
25148 || (external_p && VAR_P (d))
25149 /* Handle here a deleted function too, avoid generating
25150 its body (c++/61080). */
25151 || deleted_p)
25152 {
25153 /* The definition of the static data member is now required so
25154 we must substitute the initializer. */
25155 if (VAR_P (d)
25156 && !DECL_INITIAL (d)
25157 && DECL_INITIAL (code_pattern))
25158 {
25159 tree ns;
25160 tree init;
25161 bool const_init = false;
25162 bool enter_context = DECL_CLASS_SCOPE_P (d);
25163
25164 ns = decl_namespace_context (d);
25165 push_nested_namespace (ns);
25166 if (enter_context)
25167 push_nested_class (DECL_CONTEXT (d));
25168 init = tsubst_expr (DECL_INITIAL (code_pattern),
25169 args,
25170 tf_warning_or_error, NULL_TREE,
25171 /*integral_constant_expression_p=*/false);
25172 /* If instantiating the initializer involved instantiating this
25173 again, don't call cp_finish_decl twice. */
25174 if (!DECL_INITIAL (d))
25175 {
25176 /* Make sure the initializer is still constant, in case of
25177 circular dependency (template/instantiate6.C). */
25178 const_init
25179 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25180 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25181 /*asmspec_tree=*/NULL_TREE,
25182 LOOKUP_ONLYCONVERTING);
25183 }
25184 if (enter_context)
25185 pop_nested_class ();
25186 pop_nested_namespace (ns);
25187 }
25188
25189 /* We restore the source position here because it's used by
25190 add_pending_template. */
25191 input_location = saved_loc;
25192
25193 if (at_eof && !pattern_defined
25194 && DECL_EXPLICIT_INSTANTIATION (d)
25195 && DECL_NOT_REALLY_EXTERN (d))
25196 /* [temp.explicit]
25197
25198 The definition of a non-exported function template, a
25199 non-exported member function template, or a non-exported
25200 member function or static data member of a class template
25201 shall be present in every translation unit in which it is
25202 explicitly instantiated. */
25203 permerror (input_location, "explicit instantiation of %qD "
25204 "but no definition available", d);
25205
25206 /* If we're in unevaluated context, we just wanted to get the
25207 constant value; this isn't an odr use, so don't queue
25208 a full instantiation. */
25209 if (cp_unevaluated_operand != 0)
25210 goto out;
25211 /* ??? Historically, we have instantiated inline functions, even
25212 when marked as "extern template". */
25213 if (!(external_p && VAR_P (d)))
25214 add_pending_template (d);
25215 goto out;
25216 }
25217
25218 bool push_to_top, nested;
25219 tree fn_context;
25220 fn_context = decl_function_context (d);
25221 if (LAMBDA_FUNCTION_P (d))
25222 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25223 fn_context = NULL_TREE;
25224 nested = current_function_decl != NULL_TREE;
25225 push_to_top = !(nested && fn_context == current_function_decl);
25226
25227 vec<tree> omp_privatization_save;
25228 if (nested)
25229 save_omp_privatization_clauses (omp_privatization_save);
25230
25231 if (push_to_top)
25232 push_to_top_level ();
25233 else
25234 {
25235 gcc_assert (!processing_template_decl);
25236 push_function_context ();
25237 cp_unevaluated_operand = 0;
25238 c_inhibit_evaluation_warnings = 0;
25239 }
25240
25241 /* Mark D as instantiated so that recursive calls to
25242 instantiate_decl do not try to instantiate it again. */
25243 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25244
25245 /* Regenerate the declaration in case the template has been modified
25246 by a subsequent redeclaration. */
25247 regenerate_decl_from_template (d, td, args);
25248
25249 /* We already set the file and line above. Reset them now in case
25250 they changed as a result of calling regenerate_decl_from_template. */
25251 input_location = DECL_SOURCE_LOCATION (d);
25252
25253 if (VAR_P (d))
25254 {
25255 tree init;
25256 bool const_init = false;
25257
25258 /* Clear out DECL_RTL; whatever was there before may not be right
25259 since we've reset the type of the declaration. */
25260 SET_DECL_RTL (d, NULL);
25261 DECL_IN_AGGR_P (d) = 0;
25262
25263 /* The initializer is placed in DECL_INITIAL by
25264 regenerate_decl_from_template so we don't need to
25265 push/pop_access_scope again here. Pull it out so that
25266 cp_finish_decl can process it. */
25267 init = DECL_INITIAL (d);
25268 DECL_INITIAL (d) = NULL_TREE;
25269 DECL_INITIALIZED_P (d) = 0;
25270
25271 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25272 initializer. That function will defer actual emission until
25273 we have a chance to determine linkage. */
25274 DECL_EXTERNAL (d) = 0;
25275
25276 /* Enter the scope of D so that access-checking works correctly. */
25277 bool enter_context = DECL_CLASS_SCOPE_P (d);
25278 if (enter_context)
25279 push_nested_class (DECL_CONTEXT (d));
25280
25281 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25282 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25283 ? LOOKUP_CONSTINIT : 0);
25284 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25285
25286 if (enter_context)
25287 pop_nested_class ();
25288
25289 if (variable_template_p (gen_tmpl))
25290 note_variable_template_instantiation (d);
25291 }
25292 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25293 synthesize_method (d);
25294 else if (TREE_CODE (d) == FUNCTION_DECL)
25295 {
25296 /* Set up the list of local specializations. */
25297 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25298 tree block = NULL_TREE;
25299
25300 /* Set up context. */
25301 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25302 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25303 block = push_stmt_list ();
25304 else
25305 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25306
25307 /* Some typedefs referenced from within the template code need to be
25308 access checked at template instantiation time, i.e now. These
25309 types were added to the template at parsing time. Let's get those
25310 and perform the access checks then. */
25311 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25312 args);
25313
25314 /* Create substitution entries for the parameters. */
25315 register_parameter_specializations (code_pattern, d);
25316
25317 /* Substitute into the body of the function. */
25318 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25319 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25320 tf_warning_or_error, tmpl);
25321 else
25322 {
25323 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25324 tf_warning_or_error, tmpl,
25325 /*integral_constant_expression_p=*/false);
25326
25327 /* Set the current input_location to the end of the function
25328 so that finish_function knows where we are. */
25329 input_location
25330 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25331
25332 /* Remember if we saw an infinite loop in the template. */
25333 current_function_infinite_loop
25334 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25335 }
25336
25337 /* Finish the function. */
25338 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25339 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25340 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25341 else
25342 {
25343 d = finish_function (/*inline_p=*/false);
25344 expand_or_defer_fn (d);
25345 }
25346
25347 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25348 cp_check_omp_declare_reduction (d);
25349 }
25350
25351 /* We're not deferring instantiation any more. */
25352 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25353
25354 if (push_to_top)
25355 pop_from_top_level ();
25356 else
25357 pop_function_context ();
25358
25359 if (nested)
25360 restore_omp_privatization_clauses (omp_privatization_save);
25361
25362 out:
25363 pop_deferring_access_checks ();
25364 timevar_pop (TV_TEMPLATE_INST);
25365 pop_tinst_level ();
25366 input_location = saved_loc;
25367 cp_unevaluated_operand = saved_unevaluated_operand;
25368 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25369
25370 return d;
25371 }
25372
25373 /* Run through the list of templates that we wish we could
25374 instantiate, and instantiate any we can. RETRIES is the
25375 number of times we retry pending template instantiation. */
25376
25377 void
25378 instantiate_pending_templates (int retries)
25379 {
25380 int reconsider;
25381 location_t saved_loc = input_location;
25382
25383 /* Instantiating templates may trigger vtable generation. This in turn
25384 may require further template instantiations. We place a limit here
25385 to avoid infinite loop. */
25386 if (pending_templates && retries >= max_tinst_depth)
25387 {
25388 tree decl = pending_templates->tinst->maybe_get_node ();
25389
25390 fatal_error (input_location,
25391 "template instantiation depth exceeds maximum of %d"
25392 " instantiating %q+D, possibly from virtual table generation"
25393 " (use %<-ftemplate-depth=%> to increase the maximum)",
25394 max_tinst_depth, decl);
25395 if (TREE_CODE (decl) == FUNCTION_DECL)
25396 /* Pretend that we defined it. */
25397 DECL_INITIAL (decl) = error_mark_node;
25398 return;
25399 }
25400
25401 do
25402 {
25403 struct pending_template **t = &pending_templates;
25404 struct pending_template *last = NULL;
25405 reconsider = 0;
25406 while (*t)
25407 {
25408 tree instantiation = reopen_tinst_level ((*t)->tinst);
25409 bool complete = false;
25410
25411 if (TYPE_P (instantiation))
25412 {
25413 if (!COMPLETE_TYPE_P (instantiation))
25414 {
25415 instantiate_class_template (instantiation);
25416 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25417 for (tree fld = TYPE_FIELDS (instantiation);
25418 fld; fld = TREE_CHAIN (fld))
25419 if ((VAR_P (fld)
25420 || (TREE_CODE (fld) == FUNCTION_DECL
25421 && !DECL_ARTIFICIAL (fld)))
25422 && DECL_TEMPLATE_INSTANTIATION (fld))
25423 instantiate_decl (fld,
25424 /*defer_ok=*/false,
25425 /*expl_inst_class_mem_p=*/false);
25426
25427 if (COMPLETE_TYPE_P (instantiation))
25428 reconsider = 1;
25429 }
25430
25431 complete = COMPLETE_TYPE_P (instantiation);
25432 }
25433 else
25434 {
25435 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25436 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25437 {
25438 instantiation
25439 = instantiate_decl (instantiation,
25440 /*defer_ok=*/false,
25441 /*expl_inst_class_mem_p=*/false);
25442 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25443 reconsider = 1;
25444 }
25445
25446 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25447 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25448 }
25449
25450 if (complete)
25451 {
25452 /* If INSTANTIATION has been instantiated, then we don't
25453 need to consider it again in the future. */
25454 struct pending_template *drop = *t;
25455 *t = (*t)->next;
25456 set_refcount_ptr (drop->tinst);
25457 pending_template_freelist ().free (drop);
25458 }
25459 else
25460 {
25461 last = *t;
25462 t = &(*t)->next;
25463 }
25464 tinst_depth = 0;
25465 set_refcount_ptr (current_tinst_level);
25466 }
25467 last_pending_template = last;
25468 }
25469 while (reconsider);
25470
25471 input_location = saved_loc;
25472 }
25473
25474 /* Substitute ARGVEC into T, which is a list of initializers for
25475 either base class or a non-static data member. The TREE_PURPOSEs
25476 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25477 instantiate_decl. */
25478
25479 static tree
25480 tsubst_initializer_list (tree t, tree argvec)
25481 {
25482 tree inits = NULL_TREE;
25483 tree target_ctor = error_mark_node;
25484
25485 for (; t; t = TREE_CHAIN (t))
25486 {
25487 tree decl;
25488 tree init;
25489 tree expanded_bases = NULL_TREE;
25490 tree expanded_arguments = NULL_TREE;
25491 int i, len = 1;
25492
25493 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25494 {
25495 tree expr;
25496 tree arg;
25497
25498 /* Expand the base class expansion type into separate base
25499 classes. */
25500 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25501 tf_warning_or_error,
25502 NULL_TREE);
25503 if (expanded_bases == error_mark_node)
25504 continue;
25505
25506 /* We'll be building separate TREE_LISTs of arguments for
25507 each base. */
25508 len = TREE_VEC_LENGTH (expanded_bases);
25509 expanded_arguments = make_tree_vec (len);
25510 for (i = 0; i < len; i++)
25511 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25512
25513 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25514 expand each argument in the TREE_VALUE of t. */
25515 expr = make_node (EXPR_PACK_EXPANSION);
25516 PACK_EXPANSION_LOCAL_P (expr) = true;
25517 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25518 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25519
25520 if (TREE_VALUE (t) == void_type_node)
25521 /* VOID_TYPE_NODE is used to indicate
25522 value-initialization. */
25523 {
25524 for (i = 0; i < len; i++)
25525 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25526 }
25527 else
25528 {
25529 /* Substitute parameter packs into each argument in the
25530 TREE_LIST. */
25531 in_base_initializer = 1;
25532 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25533 {
25534 tree expanded_exprs;
25535
25536 /* Expand the argument. */
25537 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25538 expanded_exprs
25539 = tsubst_pack_expansion (expr, argvec,
25540 tf_warning_or_error,
25541 NULL_TREE);
25542 if (expanded_exprs == error_mark_node)
25543 continue;
25544
25545 /* Prepend each of the expanded expressions to the
25546 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25547 for (i = 0; i < len; i++)
25548 {
25549 TREE_VEC_ELT (expanded_arguments, i) =
25550 tree_cons (NULL_TREE,
25551 TREE_VEC_ELT (expanded_exprs, i),
25552 TREE_VEC_ELT (expanded_arguments, i));
25553 }
25554 }
25555 in_base_initializer = 0;
25556
25557 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25558 since we built them backwards. */
25559 for (i = 0; i < len; i++)
25560 {
25561 TREE_VEC_ELT (expanded_arguments, i) =
25562 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25563 }
25564 }
25565 }
25566
25567 for (i = 0; i < len; ++i)
25568 {
25569 if (expanded_bases)
25570 {
25571 decl = TREE_VEC_ELT (expanded_bases, i);
25572 decl = expand_member_init (decl);
25573 init = TREE_VEC_ELT (expanded_arguments, i);
25574 }
25575 else
25576 {
25577 tree tmp;
25578 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25579 tf_warning_or_error, NULL_TREE);
25580
25581 decl = expand_member_init (decl);
25582 if (decl && !DECL_P (decl))
25583 in_base_initializer = 1;
25584
25585 init = TREE_VALUE (t);
25586 tmp = init;
25587 if (init != void_type_node)
25588 init = tsubst_expr (init, argvec,
25589 tf_warning_or_error, NULL_TREE,
25590 /*integral_constant_expression_p=*/false);
25591 if (init == NULL_TREE && tmp != NULL_TREE)
25592 /* If we had an initializer but it instantiated to nothing,
25593 value-initialize the object. This will only occur when
25594 the initializer was a pack expansion where the parameter
25595 packs used in that expansion were of length zero. */
25596 init = void_type_node;
25597 in_base_initializer = 0;
25598 }
25599
25600 if (target_ctor != error_mark_node
25601 && init != error_mark_node)
25602 {
25603 error ("mem-initializer for %qD follows constructor delegation",
25604 decl);
25605 return inits;
25606 }
25607 /* Look for a target constructor. */
25608 if (init != error_mark_node
25609 && decl && CLASS_TYPE_P (decl)
25610 && same_type_p (decl, current_class_type))
25611 {
25612 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25613 if (inits)
25614 {
25615 error ("constructor delegation follows mem-initializer for %qD",
25616 TREE_PURPOSE (inits));
25617 continue;
25618 }
25619 target_ctor = init;
25620 }
25621
25622 if (decl)
25623 {
25624 init = build_tree_list (decl, init);
25625 TREE_CHAIN (init) = inits;
25626 inits = init;
25627 }
25628 }
25629 }
25630 return inits;
25631 }
25632
25633 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25634
25635 static void
25636 set_current_access_from_decl (tree decl)
25637 {
25638 if (TREE_PRIVATE (decl))
25639 current_access_specifier = access_private_node;
25640 else if (TREE_PROTECTED (decl))
25641 current_access_specifier = access_protected_node;
25642 else
25643 current_access_specifier = access_public_node;
25644 }
25645
25646 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25647 is the instantiation (which should have been created with
25648 start_enum) and ARGS are the template arguments to use. */
25649
25650 static void
25651 tsubst_enum (tree tag, tree newtag, tree args)
25652 {
25653 tree e;
25654
25655 if (SCOPED_ENUM_P (newtag))
25656 begin_scope (sk_scoped_enum, newtag);
25657
25658 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25659 {
25660 tree value;
25661 tree decl;
25662
25663 decl = TREE_VALUE (e);
25664 /* Note that in a template enum, the TREE_VALUE is the
25665 CONST_DECL, not the corresponding INTEGER_CST. */
25666 value = tsubst_expr (DECL_INITIAL (decl),
25667 args, tf_warning_or_error, NULL_TREE,
25668 /*integral_constant_expression_p=*/true);
25669
25670 /* Give this enumeration constant the correct access. */
25671 set_current_access_from_decl (decl);
25672
25673 /* Actually build the enumerator itself. Here we're assuming that
25674 enumerators can't have dependent attributes. */
25675 build_enumerator (DECL_NAME (decl), value, newtag,
25676 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25677 }
25678
25679 if (SCOPED_ENUM_P (newtag))
25680 finish_scope ();
25681
25682 finish_enum_value_list (newtag);
25683 finish_enum (newtag);
25684
25685 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25686 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25687 }
25688
25689 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25690 its type -- but without substituting the innermost set of template
25691 arguments. So, innermost set of template parameters will appear in
25692 the type. */
25693
25694 tree
25695 get_mostly_instantiated_function_type (tree decl)
25696 {
25697 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25698 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25699 }
25700
25701 /* Return truthvalue if we're processing a template different from
25702 the last one involved in diagnostics. */
25703 bool
25704 problematic_instantiation_changed (void)
25705 {
25706 return current_tinst_level != last_error_tinst_level;
25707 }
25708
25709 /* Remember current template involved in diagnostics. */
25710 void
25711 record_last_problematic_instantiation (void)
25712 {
25713 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25714 }
25715
25716 struct tinst_level *
25717 current_instantiation (void)
25718 {
25719 return current_tinst_level;
25720 }
25721
25722 /* Return TRUE if current_function_decl is being instantiated, false
25723 otherwise. */
25724
25725 bool
25726 instantiating_current_function_p (void)
25727 {
25728 return (current_instantiation ()
25729 && (current_instantiation ()->maybe_get_node ()
25730 == current_function_decl));
25731 }
25732
25733 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25734 type. Return false for ok, true for disallowed. Issue error and
25735 inform messages under control of COMPLAIN. */
25736
25737 static bool
25738 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25739 {
25740 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25741 return false;
25742 else if (TYPE_PTR_P (type))
25743 return false;
25744 else if (TYPE_REF_P (type)
25745 && !TYPE_REF_IS_RVALUE (type))
25746 return false;
25747 else if (TYPE_PTRMEM_P (type))
25748 return false;
25749 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25750 return false;
25751 else if (TREE_CODE (type) == TYPENAME_TYPE)
25752 return false;
25753 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25754 return false;
25755 else if (TREE_CODE (type) == NULLPTR_TYPE)
25756 return false;
25757 /* A bound template template parm could later be instantiated to have a valid
25758 nontype parm type via an alias template. */
25759 else if (cxx_dialect >= cxx11
25760 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25761 return false;
25762 else if (CLASS_TYPE_P (type))
25763 {
25764 if (cxx_dialect < cxx2a)
25765 {
25766 if (complain & tf_error)
25767 error ("non-type template parameters of class type only available "
25768 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25769 return true;
25770 }
25771 if (dependent_type_p (type))
25772 return false;
25773 if (!complete_type_or_else (type, NULL_TREE))
25774 return true;
25775 if (!structural_type_p (type))
25776 {
25777 auto_diagnostic_group d;
25778 if (complain & tf_error)
25779 error ("%qT is not a valid type for a template non-type parameter "
25780 "because it is not structural", type);
25781 structural_type_p (type, true);
25782 return true;
25783 }
25784 return false;
25785 }
25786
25787 if (complain & tf_error)
25788 {
25789 if (type == error_mark_node)
25790 inform (input_location, "invalid template non-type parameter");
25791 else
25792 error ("%q#T is not a valid type for a template non-type parameter",
25793 type);
25794 }
25795 return true;
25796 }
25797
25798 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25799 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25800
25801 static bool
25802 dependent_type_p_r (tree type)
25803 {
25804 tree scope;
25805
25806 /* [temp.dep.type]
25807
25808 A type is dependent if it is:
25809
25810 -- a template parameter. Template template parameters are types
25811 for us (since TYPE_P holds true for them) so we handle
25812 them here. */
25813 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25814 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25815 return true;
25816 /* -- a qualified-id with a nested-name-specifier which contains a
25817 class-name that names a dependent type or whose unqualified-id
25818 names a dependent type. */
25819 if (TREE_CODE (type) == TYPENAME_TYPE)
25820 return true;
25821
25822 /* An alias template specialization can be dependent even if the
25823 resulting type is not. */
25824 if (dependent_alias_template_spec_p (type))
25825 return true;
25826
25827 /* -- a cv-qualified type where the cv-unqualified type is
25828 dependent.
25829 No code is necessary for this bullet; the code below handles
25830 cv-qualified types, and we don't want to strip aliases with
25831 TYPE_MAIN_VARIANT because of DR 1558. */
25832 /* -- a compound type constructed from any dependent type. */
25833 if (TYPE_PTRMEM_P (type))
25834 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25835 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25836 (type)));
25837 else if (INDIRECT_TYPE_P (type))
25838 return dependent_type_p (TREE_TYPE (type));
25839 else if (FUNC_OR_METHOD_TYPE_P (type))
25840 {
25841 tree arg_type;
25842
25843 if (dependent_type_p (TREE_TYPE (type)))
25844 return true;
25845 for (arg_type = TYPE_ARG_TYPES (type);
25846 arg_type;
25847 arg_type = TREE_CHAIN (arg_type))
25848 if (dependent_type_p (TREE_VALUE (arg_type)))
25849 return true;
25850 if (cxx_dialect >= cxx17)
25851 /* A value-dependent noexcept-specifier makes the type dependent. */
25852 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25853 if (tree noex = TREE_PURPOSE (spec))
25854 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25855 affect overload resolution and treating it as dependent breaks
25856 things. Same for an unparsed noexcept expression. */
25857 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25858 && TREE_CODE (noex) != DEFERRED_PARSE
25859 && value_dependent_expression_p (noex))
25860 return true;
25861 return false;
25862 }
25863 /* -- an array type constructed from any dependent type or whose
25864 size is specified by a constant expression that is
25865 value-dependent.
25866
25867 We checked for type- and value-dependence of the bounds in
25868 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25869 if (TREE_CODE (type) == ARRAY_TYPE)
25870 {
25871 if (TYPE_DOMAIN (type)
25872 && dependent_type_p (TYPE_DOMAIN (type)))
25873 return true;
25874 return dependent_type_p (TREE_TYPE (type));
25875 }
25876
25877 /* -- a template-id in which either the template name is a template
25878 parameter ... */
25879 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25880 return true;
25881 /* ... or any of the template arguments is a dependent type or
25882 an expression that is type-dependent or value-dependent. */
25883 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25884 && (any_dependent_template_arguments_p
25885 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25886 return true;
25887
25888 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25889 dependent; if the argument of the `typeof' expression is not
25890 type-dependent, then it should already been have resolved. */
25891 if (TREE_CODE (type) == TYPEOF_TYPE
25892 || TREE_CODE (type) == DECLTYPE_TYPE
25893 || TREE_CODE (type) == UNDERLYING_TYPE)
25894 return true;
25895
25896 /* A template argument pack is dependent if any of its packed
25897 arguments are. */
25898 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25899 {
25900 tree args = ARGUMENT_PACK_ARGS (type);
25901 int i, len = TREE_VEC_LENGTH (args);
25902 for (i = 0; i < len; ++i)
25903 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25904 return true;
25905 }
25906
25907 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25908 be template parameters. */
25909 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25910 return true;
25911
25912 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25913 return true;
25914
25915 /* The standard does not specifically mention types that are local
25916 to template functions or local classes, but they should be
25917 considered dependent too. For example:
25918
25919 template <int I> void f() {
25920 enum E { a = I };
25921 S<sizeof (E)> s;
25922 }
25923
25924 The size of `E' cannot be known until the value of `I' has been
25925 determined. Therefore, `E' must be considered dependent. */
25926 scope = TYPE_CONTEXT (type);
25927 if (scope && TYPE_P (scope))
25928 return dependent_type_p (scope);
25929 /* Don't use type_dependent_expression_p here, as it can lead
25930 to infinite recursion trying to determine whether a lambda
25931 nested in a lambda is dependent (c++/47687). */
25932 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25933 && DECL_LANG_SPECIFIC (scope)
25934 && DECL_TEMPLATE_INFO (scope)
25935 && (any_dependent_template_arguments_p
25936 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25937 return true;
25938
25939 /* Other types are non-dependent. */
25940 return false;
25941 }
25942
25943 /* Returns TRUE if TYPE is dependent, in the sense of
25944 [temp.dep.type]. Note that a NULL type is considered dependent. */
25945
25946 bool
25947 dependent_type_p (tree type)
25948 {
25949 /* If there are no template parameters in scope, then there can't be
25950 any dependent types. */
25951 if (!processing_template_decl)
25952 {
25953 /* If we are not processing a template, then nobody should be
25954 providing us with a dependent type. */
25955 gcc_assert (type);
25956 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25957 return false;
25958 }
25959
25960 /* If the type is NULL, we have not computed a type for the entity
25961 in question; in that case, the type is dependent. */
25962 if (!type)
25963 return true;
25964
25965 /* Erroneous types can be considered non-dependent. */
25966 if (type == error_mark_node)
25967 return false;
25968
25969 /* Getting here with global_type_node means we improperly called this
25970 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25971 gcc_checking_assert (type != global_type_node);
25972
25973 /* If we have not already computed the appropriate value for TYPE,
25974 do so now. */
25975 if (!TYPE_DEPENDENT_P_VALID (type))
25976 {
25977 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25978 TYPE_DEPENDENT_P_VALID (type) = 1;
25979 }
25980
25981 return TYPE_DEPENDENT_P (type);
25982 }
25983
25984 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25985 lookup. In other words, a dependent type that is not the current
25986 instantiation. */
25987
25988 bool
25989 dependent_scope_p (tree scope)
25990 {
25991 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25992 && !currently_open_class (scope));
25993 }
25994
25995 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25996 an unknown base of 'this' (and is therefore instantiation-dependent). */
25997
25998 static bool
25999 unknown_base_ref_p (tree t)
26000 {
26001 if (!current_class_ptr)
26002 return false;
26003
26004 tree mem = TREE_OPERAND (t, 1);
26005 if (shared_member_p (mem))
26006 return false;
26007
26008 tree cur = current_nonlambda_class_type ();
26009 if (!any_dependent_bases_p (cur))
26010 return false;
26011
26012 tree ctx = TREE_OPERAND (t, 0);
26013 if (DERIVED_FROM_P (ctx, cur))
26014 return false;
26015
26016 return true;
26017 }
26018
26019 /* T is a SCOPE_REF; return whether we need to consider it
26020 instantiation-dependent so that we can check access at instantiation
26021 time even though we know which member it resolves to. */
26022
26023 static bool
26024 instantiation_dependent_scope_ref_p (tree t)
26025 {
26026 if (DECL_P (TREE_OPERAND (t, 1))
26027 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26028 && !unknown_base_ref_p (t)
26029 && accessible_in_template_p (TREE_OPERAND (t, 0),
26030 TREE_OPERAND (t, 1)))
26031 return false;
26032 else
26033 return true;
26034 }
26035
26036 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26037 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26038 expression. */
26039
26040 /* Note that this predicate is not appropriate for general expressions;
26041 only constant expressions (that satisfy potential_constant_expression)
26042 can be tested for value dependence. */
26043
26044 bool
26045 value_dependent_expression_p (tree expression)
26046 {
26047 if (!processing_template_decl || expression == NULL_TREE)
26048 return false;
26049
26050 /* A type-dependent expression is also value-dependent. */
26051 if (type_dependent_expression_p (expression))
26052 return true;
26053
26054 switch (TREE_CODE (expression))
26055 {
26056 case BASELINK:
26057 /* A dependent member function of the current instantiation. */
26058 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26059
26060 case FUNCTION_DECL:
26061 /* A dependent member function of the current instantiation. */
26062 if (DECL_CLASS_SCOPE_P (expression)
26063 && dependent_type_p (DECL_CONTEXT (expression)))
26064 return true;
26065 break;
26066
26067 case IDENTIFIER_NODE:
26068 /* A name that has not been looked up -- must be dependent. */
26069 return true;
26070
26071 case TEMPLATE_PARM_INDEX:
26072 /* A non-type template parm. */
26073 return true;
26074
26075 case CONST_DECL:
26076 /* A non-type template parm. */
26077 if (DECL_TEMPLATE_PARM_P (expression))
26078 return true;
26079 return value_dependent_expression_p (DECL_INITIAL (expression));
26080
26081 case VAR_DECL:
26082 /* A constant with literal type and is initialized
26083 with an expression that is value-dependent. */
26084 if (DECL_DEPENDENT_INIT_P (expression)
26085 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26086 || TYPE_REF_P (TREE_TYPE (expression)))
26087 return true;
26088 if (DECL_HAS_VALUE_EXPR_P (expression))
26089 {
26090 tree value_expr = DECL_VALUE_EXPR (expression);
26091 if (value_dependent_expression_p (value_expr)
26092 /* __PRETTY_FUNCTION__ inside a template function is dependent
26093 on the name of the function. */
26094 || (DECL_PRETTY_FUNCTION_P (expression)
26095 /* It might be used in a template, but not a template
26096 function, in which case its DECL_VALUE_EXPR will be
26097 "top level". */
26098 && value_expr == error_mark_node))
26099 return true;
26100 }
26101 return false;
26102
26103 case DYNAMIC_CAST_EXPR:
26104 case STATIC_CAST_EXPR:
26105 case CONST_CAST_EXPR:
26106 case REINTERPRET_CAST_EXPR:
26107 case CAST_EXPR:
26108 case IMPLICIT_CONV_EXPR:
26109 /* These expressions are value-dependent if the type to which
26110 the cast occurs is dependent or the expression being casted
26111 is value-dependent. */
26112 {
26113 tree type = TREE_TYPE (expression);
26114
26115 if (dependent_type_p (type))
26116 return true;
26117
26118 /* A functional cast has a list of operands. */
26119 expression = TREE_OPERAND (expression, 0);
26120 if (!expression)
26121 {
26122 /* If there are no operands, it must be an expression such
26123 as "int()". This should not happen for aggregate types
26124 because it would form non-constant expressions. */
26125 gcc_assert (cxx_dialect >= cxx11
26126 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26127
26128 return false;
26129 }
26130
26131 if (TREE_CODE (expression) == TREE_LIST)
26132 return any_value_dependent_elements_p (expression);
26133
26134 return value_dependent_expression_p (expression);
26135 }
26136
26137 case SIZEOF_EXPR:
26138 if (SIZEOF_EXPR_TYPE_P (expression))
26139 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26140 /* FALLTHRU */
26141 case ALIGNOF_EXPR:
26142 case TYPEID_EXPR:
26143 /* A `sizeof' expression is value-dependent if the operand is
26144 type-dependent or is a pack expansion. */
26145 expression = TREE_OPERAND (expression, 0);
26146 if (PACK_EXPANSION_P (expression))
26147 return true;
26148 else if (TYPE_P (expression))
26149 return dependent_type_p (expression);
26150 return instantiation_dependent_uneval_expression_p (expression);
26151
26152 case AT_ENCODE_EXPR:
26153 /* An 'encode' expression is value-dependent if the operand is
26154 type-dependent. */
26155 expression = TREE_OPERAND (expression, 0);
26156 return dependent_type_p (expression);
26157
26158 case NOEXCEPT_EXPR:
26159 expression = TREE_OPERAND (expression, 0);
26160 return instantiation_dependent_uneval_expression_p (expression);
26161
26162 case SCOPE_REF:
26163 /* All instantiation-dependent expressions should also be considered
26164 value-dependent. */
26165 return instantiation_dependent_scope_ref_p (expression);
26166
26167 case COMPONENT_REF:
26168 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26169 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26170
26171 case NONTYPE_ARGUMENT_PACK:
26172 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26173 is value-dependent. */
26174 {
26175 tree values = ARGUMENT_PACK_ARGS (expression);
26176 int i, len = TREE_VEC_LENGTH (values);
26177
26178 for (i = 0; i < len; ++i)
26179 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26180 return true;
26181
26182 return false;
26183 }
26184
26185 case TRAIT_EXPR:
26186 {
26187 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26188
26189 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26190 return true;
26191
26192 if (!type2)
26193 return false;
26194
26195 if (TREE_CODE (type2) != TREE_LIST)
26196 return dependent_type_p (type2);
26197
26198 for (; type2; type2 = TREE_CHAIN (type2))
26199 if (dependent_type_p (TREE_VALUE (type2)))
26200 return true;
26201
26202 return false;
26203 }
26204
26205 case MODOP_EXPR:
26206 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26207 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26208
26209 case ARRAY_REF:
26210 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26211 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26212
26213 case ADDR_EXPR:
26214 {
26215 tree op = TREE_OPERAND (expression, 0);
26216 return (value_dependent_expression_p (op)
26217 || has_value_dependent_address (op));
26218 }
26219
26220 case REQUIRES_EXPR:
26221 /* Treat all requires-expressions as value-dependent so
26222 we don't try to fold them. */
26223 return true;
26224
26225 case TYPE_REQ:
26226 return dependent_type_p (TREE_OPERAND (expression, 0));
26227
26228 case CALL_EXPR:
26229 {
26230 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26231 return true;
26232 tree fn = get_callee_fndecl (expression);
26233 int i, nargs;
26234 nargs = call_expr_nargs (expression);
26235 for (i = 0; i < nargs; ++i)
26236 {
26237 tree op = CALL_EXPR_ARG (expression, i);
26238 /* In a call to a constexpr member function, look through the
26239 implicit ADDR_EXPR on the object argument so that it doesn't
26240 cause the call to be considered value-dependent. We also
26241 look through it in potential_constant_expression. */
26242 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26243 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26244 && TREE_CODE (op) == ADDR_EXPR)
26245 op = TREE_OPERAND (op, 0);
26246 if (value_dependent_expression_p (op))
26247 return true;
26248 }
26249 return false;
26250 }
26251
26252 case TEMPLATE_ID_EXPR:
26253 return concept_definition_p (TREE_OPERAND (expression, 0));
26254
26255 case CONSTRUCTOR:
26256 {
26257 unsigned ix;
26258 tree val;
26259 if (dependent_type_p (TREE_TYPE (expression)))
26260 return true;
26261 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26262 if (value_dependent_expression_p (val))
26263 return true;
26264 return false;
26265 }
26266
26267 case STMT_EXPR:
26268 /* Treat a GNU statement expression as dependent to avoid crashing
26269 under instantiate_non_dependent_expr; it can't be constant. */
26270 return true;
26271
26272 default:
26273 /* A constant expression is value-dependent if any subexpression is
26274 value-dependent. */
26275 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26276 {
26277 case tcc_reference:
26278 case tcc_unary:
26279 case tcc_comparison:
26280 case tcc_binary:
26281 case tcc_expression:
26282 case tcc_vl_exp:
26283 {
26284 int i, len = cp_tree_operand_length (expression);
26285
26286 for (i = 0; i < len; i++)
26287 {
26288 tree t = TREE_OPERAND (expression, i);
26289
26290 /* In some cases, some of the operands may be missing.
26291 (For example, in the case of PREDECREMENT_EXPR, the
26292 amount to increment by may be missing.) That doesn't
26293 make the expression dependent. */
26294 if (t && value_dependent_expression_p (t))
26295 return true;
26296 }
26297 }
26298 break;
26299 default:
26300 break;
26301 }
26302 break;
26303 }
26304
26305 /* The expression is not value-dependent. */
26306 return false;
26307 }
26308
26309 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26310 [temp.dep.expr]. Note that an expression with no type is
26311 considered dependent. Other parts of the compiler arrange for an
26312 expression with type-dependent subexpressions to have no type, so
26313 this function doesn't have to be fully recursive. */
26314
26315 bool
26316 type_dependent_expression_p (tree expression)
26317 {
26318 if (!processing_template_decl)
26319 return false;
26320
26321 if (expression == NULL_TREE || expression == error_mark_node)
26322 return false;
26323
26324 STRIP_ANY_LOCATION_WRAPPER (expression);
26325
26326 /* An unresolved name is always dependent. */
26327 if (identifier_p (expression)
26328 || TREE_CODE (expression) == USING_DECL
26329 || TREE_CODE (expression) == WILDCARD_DECL)
26330 return true;
26331
26332 /* A lambda-expression in template context is dependent. dependent_type_p is
26333 true for a lambda in the scope of a class or function template, but that
26334 doesn't cover all template contexts, like a default template argument. */
26335 if (TREE_CODE (expression) == LAMBDA_EXPR)
26336 return true;
26337
26338 /* A fold expression is type-dependent. */
26339 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26340 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26341 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26342 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26343 return true;
26344
26345 /* Some expression forms are never type-dependent. */
26346 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26347 || TREE_CODE (expression) == SIZEOF_EXPR
26348 || TREE_CODE (expression) == ALIGNOF_EXPR
26349 || TREE_CODE (expression) == AT_ENCODE_EXPR
26350 || TREE_CODE (expression) == NOEXCEPT_EXPR
26351 || TREE_CODE (expression) == TRAIT_EXPR
26352 || TREE_CODE (expression) == TYPEID_EXPR
26353 || TREE_CODE (expression) == DELETE_EXPR
26354 || TREE_CODE (expression) == VEC_DELETE_EXPR
26355 || TREE_CODE (expression) == THROW_EXPR
26356 || TREE_CODE (expression) == REQUIRES_EXPR)
26357 return false;
26358
26359 /* The types of these expressions depends only on the type to which
26360 the cast occurs. */
26361 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26362 || TREE_CODE (expression) == STATIC_CAST_EXPR
26363 || TREE_CODE (expression) == CONST_CAST_EXPR
26364 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26365 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26366 || TREE_CODE (expression) == CAST_EXPR)
26367 return dependent_type_p (TREE_TYPE (expression));
26368
26369 /* The types of these expressions depends only on the type created
26370 by the expression. */
26371 if (TREE_CODE (expression) == NEW_EXPR
26372 || TREE_CODE (expression) == VEC_NEW_EXPR)
26373 {
26374 /* For NEW_EXPR tree nodes created inside a template, either
26375 the object type itself or a TREE_LIST may appear as the
26376 operand 1. */
26377 tree type = TREE_OPERAND (expression, 1);
26378 if (TREE_CODE (type) == TREE_LIST)
26379 /* This is an array type. We need to check array dimensions
26380 as well. */
26381 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26382 || value_dependent_expression_p
26383 (TREE_OPERAND (TREE_VALUE (type), 1));
26384 else
26385 return dependent_type_p (type);
26386 }
26387
26388 if (TREE_CODE (expression) == SCOPE_REF)
26389 {
26390 tree scope = TREE_OPERAND (expression, 0);
26391 tree name = TREE_OPERAND (expression, 1);
26392
26393 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26394 contains an identifier associated by name lookup with one or more
26395 declarations declared with a dependent type, or...a
26396 nested-name-specifier or qualified-id that names a member of an
26397 unknown specialization. */
26398 return (type_dependent_expression_p (name)
26399 || dependent_scope_p (scope));
26400 }
26401
26402 if (TREE_CODE (expression) == TEMPLATE_DECL
26403 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26404 return uses_outer_template_parms (expression);
26405
26406 if (TREE_CODE (expression) == STMT_EXPR)
26407 expression = stmt_expr_value_expr (expression);
26408
26409 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26410 {
26411 tree elt;
26412 unsigned i;
26413
26414 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26415 {
26416 if (type_dependent_expression_p (elt))
26417 return true;
26418 }
26419 return false;
26420 }
26421
26422 /* A static data member of the current instantiation with incomplete
26423 array type is type-dependent, as the definition and specializations
26424 can have different bounds. */
26425 if (VAR_P (expression)
26426 && DECL_CLASS_SCOPE_P (expression)
26427 && dependent_type_p (DECL_CONTEXT (expression))
26428 && VAR_HAD_UNKNOWN_BOUND (expression))
26429 return true;
26430
26431 /* An array of unknown bound depending on a variadic parameter, eg:
26432
26433 template<typename... Args>
26434 void foo (Args... args)
26435 {
26436 int arr[] = { args... };
26437 }
26438
26439 template<int... vals>
26440 void bar ()
26441 {
26442 int arr[] = { vals... };
26443 }
26444
26445 If the array has no length and has an initializer, it must be that
26446 we couldn't determine its length in cp_complete_array_type because
26447 it is dependent. */
26448 if (VAR_P (expression)
26449 && TREE_TYPE (expression) != NULL_TREE
26450 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26451 && !TYPE_DOMAIN (TREE_TYPE (expression))
26452 && DECL_INITIAL (expression))
26453 return true;
26454
26455 /* A function or variable template-id is type-dependent if it has any
26456 dependent template arguments. */
26457 if (VAR_OR_FUNCTION_DECL_P (expression)
26458 && DECL_LANG_SPECIFIC (expression)
26459 && DECL_TEMPLATE_INFO (expression))
26460 {
26461 /* Consider the innermost template arguments, since those are the ones
26462 that come from the template-id; the template arguments for the
26463 enclosing class do not make it type-dependent unless they are used in
26464 the type of the decl. */
26465 if (instantiates_primary_template_p (expression)
26466 && (any_dependent_template_arguments_p
26467 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26468 return true;
26469 }
26470
26471 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26472 type-dependent. Checking this is important for functions with auto return
26473 type, which looks like a dependent type. */
26474 if (TREE_CODE (expression) == FUNCTION_DECL
26475 && !(DECL_CLASS_SCOPE_P (expression)
26476 && dependent_type_p (DECL_CONTEXT (expression)))
26477 && !(DECL_LANG_SPECIFIC (expression)
26478 && DECL_FRIEND_P (expression)
26479 && (!DECL_FRIEND_CONTEXT (expression)
26480 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26481 && !DECL_LOCAL_FUNCTION_P (expression))
26482 {
26483 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26484 || undeduced_auto_decl (expression));
26485 return false;
26486 }
26487
26488 /* Always dependent, on the number of arguments if nothing else. */
26489 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26490 return true;
26491
26492 if (TREE_TYPE (expression) == unknown_type_node)
26493 {
26494 if (TREE_CODE (expression) == ADDR_EXPR)
26495 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26496 if (TREE_CODE (expression) == COMPONENT_REF
26497 || TREE_CODE (expression) == OFFSET_REF)
26498 {
26499 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26500 return true;
26501 expression = TREE_OPERAND (expression, 1);
26502 if (identifier_p (expression))
26503 return false;
26504 }
26505 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26506 if (TREE_CODE (expression) == SCOPE_REF)
26507 return false;
26508
26509 if (BASELINK_P (expression))
26510 {
26511 if (BASELINK_OPTYPE (expression)
26512 && dependent_type_p (BASELINK_OPTYPE (expression)))
26513 return true;
26514 expression = BASELINK_FUNCTIONS (expression);
26515 }
26516
26517 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26518 {
26519 if (any_dependent_template_arguments_p
26520 (TREE_OPERAND (expression, 1)))
26521 return true;
26522 expression = TREE_OPERAND (expression, 0);
26523 if (identifier_p (expression))
26524 return true;
26525 }
26526
26527 gcc_assert (OVL_P (expression));
26528
26529 for (lkp_iterator iter (expression); iter; ++iter)
26530 if (type_dependent_expression_p (*iter))
26531 return true;
26532
26533 return false;
26534 }
26535
26536 /* The type of a non-type template parm declared with a placeholder type
26537 depends on the corresponding template argument, even though
26538 placeholders are not normally considered dependent. */
26539 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26540 && is_auto (TREE_TYPE (expression)))
26541 return true;
26542
26543 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26544
26545 /* Dependent type attributes might not have made it from the decl to
26546 the type yet. */
26547 if (DECL_P (expression)
26548 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26549 return true;
26550
26551 return (dependent_type_p (TREE_TYPE (expression)));
26552 }
26553
26554 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26555 type-dependent if the expression refers to a member of the current
26556 instantiation and the type of the referenced member is dependent, or the
26557 class member access expression refers to a member of an unknown
26558 specialization.
26559
26560 This function returns true if the OBJECT in such a class member access
26561 expression is of an unknown specialization. */
26562
26563 bool
26564 type_dependent_object_expression_p (tree object)
26565 {
26566 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26567 dependent. */
26568 if (TREE_CODE (object) == IDENTIFIER_NODE)
26569 return true;
26570 tree scope = TREE_TYPE (object);
26571 return (!scope || dependent_scope_p (scope));
26572 }
26573
26574 /* walk_tree callback function for instantiation_dependent_expression_p,
26575 below. Returns non-zero if a dependent subexpression is found. */
26576
26577 static tree
26578 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26579 void * /*data*/)
26580 {
26581 if (TYPE_P (*tp))
26582 {
26583 /* We don't have to worry about decltype currently because decltype
26584 of an instantiation-dependent expr is a dependent type. This
26585 might change depending on the resolution of DR 1172. */
26586 *walk_subtrees = false;
26587 return NULL_TREE;
26588 }
26589 enum tree_code code = TREE_CODE (*tp);
26590 switch (code)
26591 {
26592 /* Don't treat an argument list as dependent just because it has no
26593 TREE_TYPE. */
26594 case TREE_LIST:
26595 case TREE_VEC:
26596 case NONTYPE_ARGUMENT_PACK:
26597 return NULL_TREE;
26598
26599 case TEMPLATE_PARM_INDEX:
26600 if (dependent_type_p (TREE_TYPE (*tp)))
26601 return *tp;
26602 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26603 return *tp;
26604 /* We'll check value-dependence separately. */
26605 return NULL_TREE;
26606
26607 /* Handle expressions with type operands. */
26608 case SIZEOF_EXPR:
26609 case ALIGNOF_EXPR:
26610 case TYPEID_EXPR:
26611 case AT_ENCODE_EXPR:
26612 {
26613 tree op = TREE_OPERAND (*tp, 0);
26614 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26615 op = TREE_TYPE (op);
26616 if (TYPE_P (op))
26617 {
26618 if (dependent_type_p (op))
26619 return *tp;
26620 else
26621 {
26622 *walk_subtrees = false;
26623 return NULL_TREE;
26624 }
26625 }
26626 break;
26627 }
26628
26629 case COMPONENT_REF:
26630 if (identifier_p (TREE_OPERAND (*tp, 1)))
26631 /* In a template, finish_class_member_access_expr creates a
26632 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26633 type-dependent, so that we can check access control at
26634 instantiation time (PR 42277). See also Core issue 1273. */
26635 return *tp;
26636 break;
26637
26638 case SCOPE_REF:
26639 if (instantiation_dependent_scope_ref_p (*tp))
26640 return *tp;
26641 else
26642 break;
26643
26644 /* Treat statement-expressions as dependent. */
26645 case BIND_EXPR:
26646 return *tp;
26647
26648 /* Treat requires-expressions as dependent. */
26649 case REQUIRES_EXPR:
26650 return *tp;
26651
26652 case CALL_EXPR:
26653 /* Treat concept checks as dependent. */
26654 if (concept_check_p (*tp))
26655 return *tp;
26656 break;
26657
26658 case TEMPLATE_ID_EXPR:
26659 /* Treat concept checks as dependent. */
26660 if (concept_check_p (*tp))
26661 return *tp;
26662 break;
26663
26664 case CONSTRUCTOR:
26665 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26666 return *tp;
26667 break;
26668
26669 default:
26670 break;
26671 }
26672
26673 if (type_dependent_expression_p (*tp))
26674 return *tp;
26675 else
26676 return NULL_TREE;
26677 }
26678
26679 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26680 sense defined by the ABI:
26681
26682 "An expression is instantiation-dependent if it is type-dependent
26683 or value-dependent, or it has a subexpression that is type-dependent
26684 or value-dependent."
26685
26686 Except don't actually check value-dependence for unevaluated expressions,
26687 because in sizeof(i) we don't care about the value of i. Checking
26688 type-dependence will in turn check value-dependence of array bounds/template
26689 arguments as needed. */
26690
26691 bool
26692 instantiation_dependent_uneval_expression_p (tree expression)
26693 {
26694 tree result;
26695
26696 if (!processing_template_decl)
26697 return false;
26698
26699 if (expression == error_mark_node)
26700 return false;
26701
26702 result = cp_walk_tree_without_duplicates (&expression,
26703 instantiation_dependent_r, NULL);
26704 return result != NULL_TREE;
26705 }
26706
26707 /* As above, but also check value-dependence of the expression as a whole. */
26708
26709 bool
26710 instantiation_dependent_expression_p (tree expression)
26711 {
26712 return (instantiation_dependent_uneval_expression_p (expression)
26713 || value_dependent_expression_p (expression));
26714 }
26715
26716 /* Like type_dependent_expression_p, but it also works while not processing
26717 a template definition, i.e. during substitution or mangling. */
26718
26719 bool
26720 type_dependent_expression_p_push (tree expr)
26721 {
26722 bool b;
26723 ++processing_template_decl;
26724 b = type_dependent_expression_p (expr);
26725 --processing_template_decl;
26726 return b;
26727 }
26728
26729 /* Returns TRUE if ARGS contains a type-dependent expression. */
26730
26731 bool
26732 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26733 {
26734 unsigned int i;
26735 tree arg;
26736
26737 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26738 {
26739 if (type_dependent_expression_p (arg))
26740 return true;
26741 }
26742 return false;
26743 }
26744
26745 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26746 expressions) contains any type-dependent expressions. */
26747
26748 bool
26749 any_type_dependent_elements_p (const_tree list)
26750 {
26751 for (; list; list = TREE_CHAIN (list))
26752 if (type_dependent_expression_p (TREE_VALUE (list)))
26753 return true;
26754
26755 return false;
26756 }
26757
26758 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26759 expressions) contains any value-dependent expressions. */
26760
26761 bool
26762 any_value_dependent_elements_p (const_tree list)
26763 {
26764 for (; list; list = TREE_CHAIN (list))
26765 if (value_dependent_expression_p (TREE_VALUE (list)))
26766 return true;
26767
26768 return false;
26769 }
26770
26771 /* Returns TRUE if the ARG (a template argument) is dependent. */
26772
26773 bool
26774 dependent_template_arg_p (tree arg)
26775 {
26776 if (!processing_template_decl)
26777 return false;
26778
26779 /* Assume a template argument that was wrongly written by the user
26780 is dependent. This is consistent with what
26781 any_dependent_template_arguments_p [that calls this function]
26782 does. */
26783 if (!arg || arg == error_mark_node)
26784 return true;
26785
26786 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26787 arg = argument_pack_select_arg (arg);
26788
26789 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26790 return true;
26791 if (TREE_CODE (arg) == TEMPLATE_DECL)
26792 {
26793 if (DECL_TEMPLATE_PARM_P (arg))
26794 return true;
26795 /* A member template of a dependent class is not necessarily
26796 type-dependent, but it is a dependent template argument because it
26797 will be a member of an unknown specialization to that template. */
26798 tree scope = CP_DECL_CONTEXT (arg);
26799 return TYPE_P (scope) && dependent_type_p (scope);
26800 }
26801 else if (ARGUMENT_PACK_P (arg))
26802 {
26803 tree args = ARGUMENT_PACK_ARGS (arg);
26804 int i, len = TREE_VEC_LENGTH (args);
26805 for (i = 0; i < len; ++i)
26806 {
26807 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26808 return true;
26809 }
26810
26811 return false;
26812 }
26813 else if (TYPE_P (arg))
26814 return dependent_type_p (arg);
26815 else
26816 return (type_dependent_expression_p (arg)
26817 || value_dependent_expression_p (arg));
26818 }
26819
26820 /* Returns true if ARGS (a collection of template arguments) contains
26821 any types that require structural equality testing. */
26822
26823 bool
26824 any_template_arguments_need_structural_equality_p (tree args)
26825 {
26826 int i;
26827 int j;
26828
26829 if (!args)
26830 return false;
26831 if (args == error_mark_node)
26832 return true;
26833
26834 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26835 {
26836 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26837 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26838 {
26839 tree arg = TREE_VEC_ELT (level, j);
26840 tree packed_args = NULL_TREE;
26841 int k, len = 1;
26842
26843 if (ARGUMENT_PACK_P (arg))
26844 {
26845 /* Look inside the argument pack. */
26846 packed_args = ARGUMENT_PACK_ARGS (arg);
26847 len = TREE_VEC_LENGTH (packed_args);
26848 }
26849
26850 for (k = 0; k < len; ++k)
26851 {
26852 if (packed_args)
26853 arg = TREE_VEC_ELT (packed_args, k);
26854
26855 if (error_operand_p (arg))
26856 return true;
26857 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26858 continue;
26859 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26860 return true;
26861 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26862 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26863 return true;
26864 }
26865 }
26866 }
26867
26868 return false;
26869 }
26870
26871 /* Returns true if ARGS (a collection of template arguments) contains
26872 any dependent arguments. */
26873
26874 bool
26875 any_dependent_template_arguments_p (const_tree args)
26876 {
26877 int i;
26878 int j;
26879
26880 if (!args)
26881 return false;
26882 if (args == error_mark_node)
26883 return true;
26884
26885 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26886 {
26887 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26888 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26889 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26890 return true;
26891 }
26892
26893 return false;
26894 }
26895
26896 /* Returns true if ARGS contains any errors. */
26897
26898 bool
26899 any_erroneous_template_args_p (const_tree args)
26900 {
26901 int i;
26902 int j;
26903
26904 if (args == error_mark_node)
26905 return true;
26906
26907 if (args && TREE_CODE (args) != TREE_VEC)
26908 {
26909 if (tree ti = get_template_info (args))
26910 args = TI_ARGS (ti);
26911 else
26912 args = NULL_TREE;
26913 }
26914
26915 if (!args)
26916 return false;
26917
26918 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26919 {
26920 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26921 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26922 if (error_operand_p (TREE_VEC_ELT (level, j)))
26923 return true;
26924 }
26925
26926 return false;
26927 }
26928
26929 /* Returns TRUE if the template TMPL is type-dependent. */
26930
26931 bool
26932 dependent_template_p (tree tmpl)
26933 {
26934 if (TREE_CODE (tmpl) == OVERLOAD)
26935 {
26936 for (lkp_iterator iter (tmpl); iter; ++iter)
26937 if (dependent_template_p (*iter))
26938 return true;
26939 return false;
26940 }
26941
26942 /* Template template parameters are dependent. */
26943 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26944 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26945 return true;
26946 /* So are names that have not been looked up. */
26947 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26948 return true;
26949 return false;
26950 }
26951
26952 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26953
26954 bool
26955 dependent_template_id_p (tree tmpl, tree args)
26956 {
26957 return (dependent_template_p (tmpl)
26958 || any_dependent_template_arguments_p (args));
26959 }
26960
26961 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26962 are dependent. */
26963
26964 bool
26965 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26966 {
26967 int i;
26968
26969 if (!processing_template_decl)
26970 return false;
26971
26972 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26973 {
26974 tree decl = TREE_VEC_ELT (declv, i);
26975 tree init = TREE_VEC_ELT (initv, i);
26976 tree cond = TREE_VEC_ELT (condv, i);
26977 tree incr = TREE_VEC_ELT (incrv, i);
26978
26979 if (type_dependent_expression_p (decl)
26980 || TREE_CODE (decl) == SCOPE_REF)
26981 return true;
26982
26983 if (init && type_dependent_expression_p (init))
26984 return true;
26985
26986 if (cond == global_namespace)
26987 return true;
26988
26989 if (type_dependent_expression_p (cond))
26990 return true;
26991
26992 if (COMPARISON_CLASS_P (cond)
26993 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26994 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26995 return true;
26996
26997 if (TREE_CODE (incr) == MODOP_EXPR)
26998 {
26999 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27000 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27001 return true;
27002 }
27003 else if (type_dependent_expression_p (incr))
27004 return true;
27005 else if (TREE_CODE (incr) == MODIFY_EXPR)
27006 {
27007 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27008 return true;
27009 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27010 {
27011 tree t = TREE_OPERAND (incr, 1);
27012 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27013 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27014 return true;
27015
27016 /* If this loop has a class iterator with != comparison
27017 with increment other than i++/++i/i--/--i, make sure the
27018 increment is constant. */
27019 if (CLASS_TYPE_P (TREE_TYPE (decl))
27020 && TREE_CODE (cond) == NE_EXPR)
27021 {
27022 if (TREE_OPERAND (t, 0) == decl)
27023 t = TREE_OPERAND (t, 1);
27024 else
27025 t = TREE_OPERAND (t, 0);
27026 if (TREE_CODE (t) != INTEGER_CST)
27027 return true;
27028 }
27029 }
27030 }
27031 }
27032
27033 return false;
27034 }
27035
27036 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27037 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27038 no such TYPE can be found. Note that this function peers inside
27039 uninstantiated templates and therefore should be used only in
27040 extremely limited situations. ONLY_CURRENT_P restricts this
27041 peering to the currently open classes hierarchy (which is required
27042 when comparing types). */
27043
27044 tree
27045 resolve_typename_type (tree type, bool only_current_p)
27046 {
27047 tree scope;
27048 tree name;
27049 tree decl;
27050 int quals;
27051 tree pushed_scope;
27052 tree result;
27053
27054 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27055
27056 scope = TYPE_CONTEXT (type);
27057 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27058 gcc_checking_assert (uses_template_parms (scope));
27059
27060 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27061 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27062 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27063 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27064 identifier of the TYPENAME_TYPE anymore.
27065 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27066 TYPENAME_TYPE instead, we avoid messing up with a possible
27067 typedef variant case. */
27068 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27069
27070 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27071 it first before we can figure out what NAME refers to. */
27072 if (TREE_CODE (scope) == TYPENAME_TYPE)
27073 {
27074 if (TYPENAME_IS_RESOLVING_P (scope))
27075 /* Given a class template A with a dependent base with nested type C,
27076 typedef typename A::C::C C will land us here, as trying to resolve
27077 the initial A::C leads to the local C typedef, which leads back to
27078 A::C::C. So we break the recursion now. */
27079 return type;
27080 else
27081 scope = resolve_typename_type (scope, only_current_p);
27082 }
27083 /* If we don't know what SCOPE refers to, then we cannot resolve the
27084 TYPENAME_TYPE. */
27085 if (!CLASS_TYPE_P (scope))
27086 return type;
27087 /* If this is a typedef, we don't want to look inside (c++/11987). */
27088 if (typedef_variant_p (type))
27089 return type;
27090 /* If SCOPE isn't the template itself, it will not have a valid
27091 TYPE_FIELDS list. */
27092 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27093 /* scope is either the template itself or a compatible instantiation
27094 like X<T>, so look up the name in the original template. */
27095 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27096 /* If scope has no fields, it can't be a current instantiation. Check this
27097 before currently_open_class to avoid infinite recursion (71515). */
27098 if (!TYPE_FIELDS (scope))
27099 return type;
27100 /* If the SCOPE is not the current instantiation, there's no reason
27101 to look inside it. */
27102 if (only_current_p && !currently_open_class (scope))
27103 return type;
27104 /* Enter the SCOPE so that name lookup will be resolved as if we
27105 were in the class definition. In particular, SCOPE will no
27106 longer be considered a dependent type. */
27107 pushed_scope = push_scope (scope);
27108 /* Look up the declaration. */
27109 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27110 tf_warning_or_error);
27111
27112 result = NULL_TREE;
27113
27114 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27115 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27116 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27117 if (!decl)
27118 /*nop*/;
27119 else if (identifier_p (fullname)
27120 && TREE_CODE (decl) == TYPE_DECL)
27121 {
27122 result = TREE_TYPE (decl);
27123 if (result == error_mark_node)
27124 result = NULL_TREE;
27125 }
27126 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27127 && DECL_CLASS_TEMPLATE_P (decl))
27128 {
27129 /* Obtain the template and the arguments. */
27130 tree tmpl = TREE_OPERAND (fullname, 0);
27131 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27132 {
27133 /* We get here with a plain identifier because a previous tentative
27134 parse of the nested-name-specifier as part of a ptr-operator saw
27135 ::template X<A>. The use of ::template is necessary in a
27136 ptr-operator, but wrong in a declarator-id.
27137
27138 [temp.names]: In a qualified-id of a declarator-id, the keyword
27139 template shall not appear at the top level. */
27140 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27141 "keyword %<template%> not allowed in declarator-id");
27142 tmpl = decl;
27143 }
27144 tree args = TREE_OPERAND (fullname, 1);
27145 /* Instantiate the template. */
27146 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27147 /*entering_scope=*/true,
27148 tf_error | tf_user);
27149 if (result == error_mark_node)
27150 result = NULL_TREE;
27151 }
27152
27153 /* Leave the SCOPE. */
27154 if (pushed_scope)
27155 pop_scope (pushed_scope);
27156
27157 /* If we failed to resolve it, return the original typename. */
27158 if (!result)
27159 return type;
27160
27161 /* If lookup found a typename type, resolve that too. */
27162 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27163 {
27164 /* Ill-formed programs can cause infinite recursion here, so we
27165 must catch that. */
27166 TYPENAME_IS_RESOLVING_P (result) = 1;
27167 result = resolve_typename_type (result, only_current_p);
27168 TYPENAME_IS_RESOLVING_P (result) = 0;
27169 }
27170
27171 /* Qualify the resulting type. */
27172 quals = cp_type_quals (type);
27173 if (quals)
27174 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27175
27176 return result;
27177 }
27178
27179 /* EXPR is an expression which is not type-dependent. Return a proxy
27180 for EXPR that can be used to compute the types of larger
27181 expressions containing EXPR. */
27182
27183 tree
27184 build_non_dependent_expr (tree expr)
27185 {
27186 tree orig_expr = expr;
27187 tree inner_expr;
27188
27189 /* When checking, try to get a constant value for all non-dependent
27190 expressions in order to expose bugs in *_dependent_expression_p
27191 and constexpr. This can affect code generation, see PR70704, so
27192 only do this for -fchecking=2. */
27193 if (flag_checking > 1
27194 && cxx_dialect >= cxx11
27195 /* Don't do this during nsdmi parsing as it can lead to
27196 unexpected recursive instantiations. */
27197 && !parsing_nsdmi ()
27198 /* Don't do this during concept processing either and for
27199 the same reason. */
27200 && !processing_constraint_expression_p ())
27201 fold_non_dependent_expr (expr, tf_none);
27202
27203 STRIP_ANY_LOCATION_WRAPPER (expr);
27204
27205 /* Preserve OVERLOADs; the functions must be available to resolve
27206 types. */
27207 inner_expr = expr;
27208 if (TREE_CODE (inner_expr) == STMT_EXPR)
27209 inner_expr = stmt_expr_value_expr (inner_expr);
27210 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27211 inner_expr = TREE_OPERAND (inner_expr, 0);
27212 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27213 inner_expr = TREE_OPERAND (inner_expr, 1);
27214 if (is_overloaded_fn (inner_expr)
27215 || TREE_CODE (inner_expr) == OFFSET_REF)
27216 return orig_expr;
27217 /* There is no need to return a proxy for a variable or enumerator. */
27218 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27219 return orig_expr;
27220 /* Preserve string constants; conversions from string constants to
27221 "char *" are allowed, even though normally a "const char *"
27222 cannot be used to initialize a "char *". */
27223 if (TREE_CODE (expr) == STRING_CST)
27224 return orig_expr;
27225 /* Preserve void and arithmetic constants, as an optimization -- there is no
27226 reason to create a new node. */
27227 if (TREE_CODE (expr) == VOID_CST
27228 || TREE_CODE (expr) == INTEGER_CST
27229 || TREE_CODE (expr) == REAL_CST)
27230 return orig_expr;
27231 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27232 There is at least one place where we want to know that a
27233 particular expression is a throw-expression: when checking a ?:
27234 expression, there are special rules if the second or third
27235 argument is a throw-expression. */
27236 if (TREE_CODE (expr) == THROW_EXPR)
27237 return orig_expr;
27238
27239 /* Don't wrap an initializer list, we need to be able to look inside. */
27240 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27241 return orig_expr;
27242
27243 /* Don't wrap a dummy object, we need to be able to test for it. */
27244 if (is_dummy_object (expr))
27245 return orig_expr;
27246
27247 if (TREE_CODE (expr) == COND_EXPR)
27248 return build3 (COND_EXPR,
27249 TREE_TYPE (expr),
27250 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27251 (TREE_OPERAND (expr, 1)
27252 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27253 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27254 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27255 if (TREE_CODE (expr) == COMPOUND_EXPR
27256 && !COMPOUND_EXPR_OVERLOADED (expr))
27257 return build2 (COMPOUND_EXPR,
27258 TREE_TYPE (expr),
27259 TREE_OPERAND (expr, 0),
27260 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27261
27262 /* If the type is unknown, it can't really be non-dependent */
27263 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27264
27265 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27266 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27267 TREE_TYPE (expr), expr);
27268 }
27269
27270 /* ARGS is a vector of expressions as arguments to a function call.
27271 Replace the arguments with equivalent non-dependent expressions.
27272 This modifies ARGS in place. */
27273
27274 void
27275 make_args_non_dependent (vec<tree, va_gc> *args)
27276 {
27277 unsigned int ix;
27278 tree arg;
27279
27280 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27281 {
27282 tree newarg = build_non_dependent_expr (arg);
27283 if (newarg != arg)
27284 (*args)[ix] = newarg;
27285 }
27286 }
27287
27288 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27289 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27290 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27291
27292 static tree
27293 make_auto_1 (tree name, bool set_canonical)
27294 {
27295 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27296 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27297 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27298 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27299 (0, processing_template_decl + 1, processing_template_decl + 1,
27300 TYPE_NAME (au), NULL_TREE);
27301 if (set_canonical)
27302 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27303 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27304 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27305
27306 return au;
27307 }
27308
27309 tree
27310 make_decltype_auto (void)
27311 {
27312 return make_auto_1 (decltype_auto_identifier, true);
27313 }
27314
27315 tree
27316 make_auto (void)
27317 {
27318 return make_auto_1 (auto_identifier, true);
27319 }
27320
27321 /* Return a C++17 deduction placeholder for class template TMPL. */
27322
27323 tree
27324 make_template_placeholder (tree tmpl)
27325 {
27326 tree t = make_auto_1 (auto_identifier, false);
27327 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27328 /* Our canonical type depends on the placeholder. */
27329 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27330 return t;
27331 }
27332
27333 /* True iff T is a C++17 class template deduction placeholder. */
27334
27335 bool
27336 template_placeholder_p (tree t)
27337 {
27338 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27339 }
27340
27341 /* Make a "constrained auto" type-specifier. This is an auto or
27342 decltype(auto) type with constraints that must be associated after
27343 deduction. The constraint is formed from the given concept CON
27344 and its optional sequence of template arguments ARGS.
27345
27346 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27347
27348 static tree
27349 make_constrained_placeholder_type (tree type, tree con, tree args)
27350 {
27351 /* Build the constraint. */
27352 tree tmpl = DECL_TI_TEMPLATE (con);
27353 tree expr = tmpl;
27354 if (TREE_CODE (con) == FUNCTION_DECL)
27355 expr = ovl_make (tmpl);
27356 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27357
27358 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27359
27360 /* Our canonical type depends on the constraint. */
27361 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27362
27363 /* Attach the constraint to the type declaration. */
27364 return TYPE_NAME (type);
27365 }
27366
27367 /* Make a "constrained auto" type-specifier. */
27368
27369 tree
27370 make_constrained_auto (tree con, tree args)
27371 {
27372 tree type = make_auto_1 (auto_identifier, false);
27373 return make_constrained_placeholder_type (type, con, args);
27374 }
27375
27376 /* Make a "constrained decltype(auto)" type-specifier. */
27377
27378 tree
27379 make_constrained_decltype_auto (tree con, tree args)
27380 {
27381 tree type = make_auto_1 (decltype_auto_identifier, false);
27382 /* FIXME: I don't know why this isn't done in make_auto_1. */
27383 AUTO_IS_DECLTYPE (type) = true;
27384 return make_constrained_placeholder_type (type, con, args);
27385 }
27386
27387 /* Build and return a concept definition. Like other templates, the
27388 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27389 the TEMPLATE_DECL. */
27390
27391 tree
27392 finish_concept_definition (cp_expr id, tree init)
27393 {
27394 gcc_assert (identifier_p (id));
27395 gcc_assert (processing_template_decl);
27396
27397 location_t loc = id.get_location();
27398
27399 /* A concept-definition shall not have associated constraints. */
27400 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27401 {
27402 error_at (loc, "a concept cannot be constrained");
27403 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27404 }
27405
27406 /* A concept-definition shall appear in namespace scope. Templates
27407 aren't allowed in block scope, so we only need to check for class
27408 scope. */
27409 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27410 {
27411 error_at (loc, "concept %qE not in namespace scope", *id);
27412 return error_mark_node;
27413 }
27414
27415 /* Initially build the concept declaration; it's type is bool. */
27416 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27417 DECL_CONTEXT (decl) = current_scope ();
27418 DECL_INITIAL (decl) = init;
27419
27420 /* Push the enclosing template. */
27421 return push_template_decl (decl);
27422 }
27423
27424 /* Given type ARG, return std::initializer_list<ARG>. */
27425
27426 static tree
27427 listify (tree arg)
27428 {
27429 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27430
27431 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27432 {
27433 gcc_rich_location richloc (input_location);
27434 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27435 error_at (&richloc,
27436 "deducing from brace-enclosed initializer list"
27437 " requires %<#include <initializer_list>%>");
27438
27439 return error_mark_node;
27440 }
27441 tree argvec = make_tree_vec (1);
27442 TREE_VEC_ELT (argvec, 0) = arg;
27443
27444 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27445 NULL_TREE, 0, tf_warning_or_error);
27446 }
27447
27448 /* Replace auto in TYPE with std::initializer_list<auto>. */
27449
27450 static tree
27451 listify_autos (tree type, tree auto_node)
27452 {
27453 tree init_auto = listify (strip_top_quals (auto_node));
27454 tree argvec = make_tree_vec (1);
27455 TREE_VEC_ELT (argvec, 0) = init_auto;
27456 if (processing_template_decl)
27457 argvec = add_to_template_args (current_template_args (), argvec);
27458 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27459 }
27460
27461 /* Hash traits for hashing possibly constrained 'auto'
27462 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27463
27464 struct auto_hash : default_hash_traits<tree>
27465 {
27466 static inline hashval_t hash (tree);
27467 static inline bool equal (tree, tree);
27468 };
27469
27470 /* Hash the 'auto' T. */
27471
27472 inline hashval_t
27473 auto_hash::hash (tree t)
27474 {
27475 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27476 /* Matching constrained-type-specifiers denote the same template
27477 parameter, so hash the constraint. */
27478 return hash_placeholder_constraint (c);
27479 else
27480 /* But unconstrained autos are all separate, so just hash the pointer. */
27481 return iterative_hash_object (t, 0);
27482 }
27483
27484 /* Compare two 'auto's. */
27485
27486 inline bool
27487 auto_hash::equal (tree t1, tree t2)
27488 {
27489 if (t1 == t2)
27490 return true;
27491
27492 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27493 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27494
27495 /* Two unconstrained autos are distinct. */
27496 if (!c1 || !c2)
27497 return false;
27498
27499 return equivalent_placeholder_constraints (c1, c2);
27500 }
27501
27502 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27503 constrained) auto, add it to the vector. */
27504
27505 static int
27506 extract_autos_r (tree t, void *data)
27507 {
27508 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27509 if (is_auto (t))
27510 {
27511 /* All the autos were built with index 0; fix that up now. */
27512 tree *p = hash.find_slot (t, INSERT);
27513 unsigned idx;
27514 if (*p)
27515 /* If this is a repeated constrained-type-specifier, use the index we
27516 chose before. */
27517 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27518 else
27519 {
27520 /* Otherwise this is new, so use the current count. */
27521 *p = t;
27522 idx = hash.elements () - 1;
27523 }
27524 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27525 }
27526
27527 /* Always keep walking. */
27528 return 0;
27529 }
27530
27531 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27532 says they can appear anywhere in the type. */
27533
27534 static tree
27535 extract_autos (tree type)
27536 {
27537 hash_set<tree> visited;
27538 hash_table<auto_hash> hash (2);
27539
27540 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27541
27542 tree tree_vec = make_tree_vec (hash.elements());
27543 for (hash_table<auto_hash>::iterator iter = hash.begin();
27544 iter != hash.end(); ++iter)
27545 {
27546 tree elt = *iter;
27547 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27548 TREE_VEC_ELT (tree_vec, i)
27549 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27550 }
27551
27552 return tree_vec;
27553 }
27554
27555 /* The stem for deduction guide names. */
27556 const char *const dguide_base = "__dguide_";
27557
27558 /* Return the name for a deduction guide for class template TMPL. */
27559
27560 tree
27561 dguide_name (tree tmpl)
27562 {
27563 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27564 tree tname = TYPE_IDENTIFIER (type);
27565 char *buf = (char *) alloca (1 + strlen (dguide_base)
27566 + IDENTIFIER_LENGTH (tname));
27567 memcpy (buf, dguide_base, strlen (dguide_base));
27568 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27569 IDENTIFIER_LENGTH (tname) + 1);
27570 tree dname = get_identifier (buf);
27571 TREE_TYPE (dname) = type;
27572 return dname;
27573 }
27574
27575 /* True if NAME is the name of a deduction guide. */
27576
27577 bool
27578 dguide_name_p (tree name)
27579 {
27580 return (TREE_CODE (name) == IDENTIFIER_NODE
27581 && TREE_TYPE (name)
27582 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27583 strlen (dguide_base)));
27584 }
27585
27586 /* True if FN is a deduction guide. */
27587
27588 bool
27589 deduction_guide_p (const_tree fn)
27590 {
27591 if (DECL_P (fn))
27592 if (tree name = DECL_NAME (fn))
27593 return dguide_name_p (name);
27594 return false;
27595 }
27596
27597 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27598
27599 bool
27600 copy_guide_p (const_tree fn)
27601 {
27602 gcc_assert (deduction_guide_p (fn));
27603 if (!DECL_ARTIFICIAL (fn))
27604 return false;
27605 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27606 return (TREE_CHAIN (parms) == void_list_node
27607 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27608 }
27609
27610 /* True if FN is a guide generated from a constructor template. */
27611
27612 bool
27613 template_guide_p (const_tree fn)
27614 {
27615 gcc_assert (deduction_guide_p (fn));
27616 if (!DECL_ARTIFICIAL (fn))
27617 return false;
27618 tree tmpl = DECL_TI_TEMPLATE (fn);
27619 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27620 return PRIMARY_TEMPLATE_P (org);
27621 return false;
27622 }
27623
27624 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27625 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27626 template parameter types. Note that the handling of template template
27627 parameters relies on current_template_parms being set appropriately for the
27628 new template. */
27629
27630 static tree
27631 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27632 tree tsubst_args, tsubst_flags_t complain)
27633 {
27634 if (olddecl == error_mark_node)
27635 return error_mark_node;
27636
27637 tree oldidx = get_template_parm_index (olddecl);
27638
27639 tree newtype;
27640 if (TREE_CODE (olddecl) == TYPE_DECL
27641 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27642 {
27643 tree oldtype = TREE_TYPE (olddecl);
27644 newtype = cxx_make_type (TREE_CODE (oldtype));
27645 TYPE_MAIN_VARIANT (newtype) = newtype;
27646 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27647 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27648 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27649 }
27650 else
27651 {
27652 newtype = TREE_TYPE (olddecl);
27653 if (type_uses_auto (newtype))
27654 {
27655 // Substitute once to fix references to other template parameters.
27656 newtype = tsubst (newtype, tsubst_args,
27657 complain|tf_partial, NULL_TREE);
27658 // Now substitute again to reduce the level of the auto.
27659 newtype = tsubst (newtype, current_template_args (),
27660 complain, NULL_TREE);
27661 }
27662 else
27663 newtype = tsubst (newtype, tsubst_args,
27664 complain, NULL_TREE);
27665 }
27666
27667 tree newdecl
27668 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27669 DECL_NAME (olddecl), newtype);
27670 SET_DECL_TEMPLATE_PARM_P (newdecl);
27671
27672 tree newidx;
27673 if (TREE_CODE (olddecl) == TYPE_DECL
27674 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27675 {
27676 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27677 = build_template_parm_index (index, level, level,
27678 newdecl, newtype);
27679 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27680 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27681 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27682 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27683
27684 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27685 {
27686 DECL_TEMPLATE_RESULT (newdecl)
27687 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27688 DECL_NAME (olddecl), newtype);
27689 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27690 // First create a copy (ttargs) of tsubst_args with an
27691 // additional level for the template template parameter's own
27692 // template parameters (ttparms).
27693 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27694 (DECL_TEMPLATE_PARMS (olddecl)));
27695 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27696 tree ttargs = make_tree_vec (depth + 1);
27697 for (int i = 0; i < depth; ++i)
27698 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27699 TREE_VEC_ELT (ttargs, depth)
27700 = template_parms_level_to_args (ttparms);
27701 // Substitute ttargs into ttparms to fix references to
27702 // other template parameters.
27703 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27704 complain|tf_partial);
27705 // Now substitute again with args based on tparms, to reduce
27706 // the level of the ttparms.
27707 ttargs = current_template_args ();
27708 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27709 complain);
27710 // Finally, tack the adjusted parms onto tparms.
27711 ttparms = tree_cons (size_int (depth), ttparms,
27712 current_template_parms);
27713 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27714 }
27715 }
27716 else
27717 {
27718 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27719 tree newconst
27720 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27721 TREE_CODE (oldconst),
27722 DECL_NAME (oldconst), newtype);
27723 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27724 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27725 SET_DECL_TEMPLATE_PARM_P (newconst);
27726 newidx = build_template_parm_index (index, level, level,
27727 newconst, newtype);
27728 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27729 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27730 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27731 }
27732
27733 return newdecl;
27734 }
27735
27736 /* Returns a C++17 class deduction guide template based on the constructor
27737 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27738 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27739 aggregate initialization guide. */
27740
27741 static tree
27742 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27743 {
27744 tree tparms, targs, fparms, fargs, ci;
27745 bool memtmpl = false;
27746 bool explicit_p;
27747 location_t loc;
27748 tree fn_tmpl = NULL_TREE;
27749
27750 if (outer_args)
27751 {
27752 ++processing_template_decl;
27753 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
27754 --processing_template_decl;
27755 }
27756
27757 if (!DECL_DECLARES_FUNCTION_P (ctor))
27758 {
27759 if (TYPE_P (ctor))
27760 {
27761 bool copy_p = TYPE_REF_P (ctor);
27762 if (copy_p)
27763 fparms = tree_cons (NULL_TREE, type, void_list_node);
27764 else
27765 fparms = void_list_node;
27766 }
27767 else if (TREE_CODE (ctor) == TREE_LIST)
27768 fparms = ctor;
27769 else
27770 gcc_unreachable ();
27771
27772 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27773 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27774 targs = CLASSTYPE_TI_ARGS (type);
27775 ci = NULL_TREE;
27776 fargs = NULL_TREE;
27777 loc = DECL_SOURCE_LOCATION (ctmpl);
27778 explicit_p = false;
27779 }
27780 else
27781 {
27782 ++processing_template_decl;
27783 bool ok = true;
27784
27785 fn_tmpl
27786 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27787 : DECL_TI_TEMPLATE (ctor));
27788 if (outer_args)
27789 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27790 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27791
27792 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27793 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27794 fully specialized args for the enclosing class. Strip those off, as
27795 the deduction guide won't have those template parameters. */
27796 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27797 TMPL_PARMS_DEPTH (tparms));
27798 /* Discard the 'this' parameter. */
27799 fparms = FUNCTION_ARG_CHAIN (ctor);
27800 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27801 ci = get_constraints (ctor);
27802 loc = DECL_SOURCE_LOCATION (ctor);
27803 explicit_p = DECL_NONCONVERTING_P (ctor);
27804
27805 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27806 {
27807 memtmpl = true;
27808
27809 /* For a member template constructor, we need to flatten the two
27810 template parameter lists into one, and then adjust the function
27811 signature accordingly. This gets...complicated. */
27812 tree save_parms = current_template_parms;
27813
27814 /* For a member template we should have two levels of parms/args, one
27815 for the class and one for the constructor. We stripped
27816 specialized args for further enclosing classes above. */
27817 const int depth = 2;
27818 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27819
27820 /* Template args for translating references to the two-level template
27821 parameters into references to the one-level template parameters we
27822 are creating. */
27823 tree tsubst_args = copy_node (targs);
27824 TMPL_ARGS_LEVEL (tsubst_args, depth)
27825 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27826
27827 /* Template parms for the constructor template. */
27828 tree ftparms = TREE_VALUE (tparms);
27829 unsigned flen = TREE_VEC_LENGTH (ftparms);
27830 /* Template parms for the class template. */
27831 tparms = TREE_CHAIN (tparms);
27832 tree ctparms = TREE_VALUE (tparms);
27833 unsigned clen = TREE_VEC_LENGTH (ctparms);
27834 /* Template parms for the deduction guide start as a copy of the
27835 template parms for the class. We set current_template_parms for
27836 lookup_template_class_1. */
27837 current_template_parms = tparms = copy_node (tparms);
27838 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27839 for (unsigned i = 0; i < clen; ++i)
27840 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27841
27842 /* Now we need to rewrite the constructor parms to append them to the
27843 class parms. */
27844 for (unsigned i = 0; i < flen; ++i)
27845 {
27846 unsigned index = i + clen;
27847 unsigned level = 1;
27848 tree oldelt = TREE_VEC_ELT (ftparms, i);
27849 tree olddecl = TREE_VALUE (oldelt);
27850 tree newdecl = rewrite_template_parm (olddecl, index, level,
27851 tsubst_args, complain);
27852 if (newdecl == error_mark_node)
27853 ok = false;
27854 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27855 tsubst_args, complain, ctor);
27856 tree list = build_tree_list (newdef, newdecl);
27857 TEMPLATE_PARM_CONSTRAINTS (list)
27858 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27859 tsubst_args, complain, ctor);
27860 TREE_VEC_ELT (new_vec, index) = list;
27861 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27862 }
27863
27864 /* Now we have a final set of template parms to substitute into the
27865 function signature. */
27866 targs = template_parms_to_args (tparms);
27867 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27868 complain, ctor);
27869 if (fparms == error_mark_node)
27870 ok = false;
27871 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27872 if (ci)
27873 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27874
27875 current_template_parms = save_parms;
27876 }
27877
27878 --processing_template_decl;
27879 if (!ok)
27880 return error_mark_node;
27881 }
27882
27883 if (!memtmpl)
27884 {
27885 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27886 tparms = copy_node (tparms);
27887 INNERMOST_TEMPLATE_PARMS (tparms)
27888 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27889 }
27890
27891 tree fntype = build_function_type (type, fparms);
27892 tree ded_fn = build_lang_decl_loc (loc,
27893 FUNCTION_DECL,
27894 dguide_name (type), fntype);
27895 DECL_ARGUMENTS (ded_fn) = fargs;
27896 DECL_ARTIFICIAL (ded_fn) = true;
27897 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27898 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27899 DECL_ARTIFICIAL (ded_tmpl) = true;
27900 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27901 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27902 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27903 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27904 if (DECL_P (ctor))
27905 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27906 if (ci)
27907 set_constraints (ded_tmpl, ci);
27908
27909 return ded_tmpl;
27910 }
27911
27912 /* Add to LIST the member types for the reshaped initializer CTOR. */
27913
27914 static tree
27915 collect_ctor_idx_types (tree ctor, tree list)
27916 {
27917 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
27918 tree idx, val; unsigned i;
27919 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
27920 {
27921 if (BRACE_ENCLOSED_INITIALIZER_P (val)
27922 && CONSTRUCTOR_NELTS (val))
27923 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
27924 if (TREE_CODE (subidx) == FIELD_DECL)
27925 {
27926 list = collect_ctor_idx_types (val, list);
27927 continue;
27928 }
27929 tree ftype = finish_decltype_type (idx, true, tf_none);
27930 list = tree_cons (NULL_TREE, ftype, list);
27931 }
27932
27933 return list;
27934 }
27935
27936 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
27937 INIT. */
27938
27939 static tree
27940 maybe_aggr_guide (tree type, tree init)
27941 {
27942 if (cxx_dialect < cxx2a)
27943 return NULL_TREE;
27944
27945 if (init == NULL_TREE)
27946 return NULL_TREE;
27947 if (!CP_AGGREGATE_TYPE_P (type))
27948 return NULL_TREE;
27949
27950 /* If we encounter a problem, we just won't add the candidate. */
27951 tsubst_flags_t complain = tf_none;
27952
27953 tree parms = NULL_TREE;
27954 if (TREE_CODE (init) == CONSTRUCTOR)
27955 {
27956 init = reshape_init (type, init, complain);
27957 if (init == error_mark_node)
27958 return NULL_TREE;
27959 parms = collect_ctor_idx_types (init, parms);
27960 }
27961 else if (TREE_CODE (init) == TREE_LIST)
27962 {
27963 int len = list_length (init);
27964 for (tree field = TYPE_FIELDS (type);
27965 len;
27966 --len, field = DECL_CHAIN (field))
27967 {
27968 field = next_initializable_field (field);
27969 if (!field)
27970 return NULL_TREE;
27971 tree ftype = finish_decltype_type (field, true, complain);
27972 parms = tree_cons (NULL_TREE, ftype, parms);
27973 }
27974 }
27975 else
27976 /* Aggregate initialization doesn't apply to an initializer expression. */
27977 return NULL_TREE;
27978
27979 if (parms)
27980 {
27981 tree last = parms;
27982 parms = nreverse (parms);
27983 TREE_CHAIN (last) = void_list_node;
27984 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
27985 return guide;
27986 }
27987
27988 return NULL_TREE;
27989 }
27990
27991 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
27992
27993 static bool
27994 is_spec_or_derived (tree etype, tree tmpl)
27995 {
27996 if (!etype || !CLASS_TYPE_P (etype))
27997 return false;
27998
27999 tree type = TREE_TYPE (tmpl);
28000 tree tparms = (INNERMOST_TEMPLATE_PARMS
28001 (DECL_TEMPLATE_PARMS (tmpl)));
28002 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28003 int err = unify (tparms, targs, type, etype,
28004 UNIFY_ALLOW_DERIVED, /*explain*/false);
28005 ggc_free (targs);
28006 return !err;
28007 }
28008
28009 /* Deduce template arguments for the class template placeholder PTYPE for
28010 template TMPL based on the initializer INIT, and return the resulting
28011 type. */
28012
28013 static tree
28014 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
28015 tsubst_flags_t complain)
28016 {
28017 if (!DECL_CLASS_TEMPLATE_P (tmpl))
28018 {
28019 /* We should have handled this in the caller. */
28020 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28021 return ptype;
28022 if (complain & tf_error)
28023 error ("non-class template %qT used without template arguments", tmpl);
28024 return error_mark_node;
28025 }
28026 if (init && TREE_TYPE (init) == ptype)
28027 /* Using the template parm as its own argument. */
28028 return ptype;
28029
28030 tree type = TREE_TYPE (tmpl);
28031
28032 bool try_list_ctor = false;
28033 bool copy_init = false;
28034
28035 releasing_vec rv_args = NULL;
28036 vec<tree,va_gc> *&args = *&rv_args;
28037 if (init == NULL_TREE)
28038 args = make_tree_vector ();
28039 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28040 {
28041 if (CONSTRUCTOR_NELTS (init) == 1)
28042 {
28043 /* As an exception, the first phase in 16.3.1.7 (considering the
28044 initializer list as a single argument) is omitted if the
28045 initializer list consists of a single expression of type cv U,
28046 where U is a specialization of C or a class derived from a
28047 specialization of C. */
28048 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28049 copy_init = is_spec_or_derived (TREE_TYPE (elt), tmpl);
28050 }
28051 try_list_ctor = !copy_init && TYPE_HAS_LIST_CTOR (type);
28052 if (try_list_ctor || is_std_init_list (type))
28053 args = make_tree_vector_single (init);
28054 else
28055 args = make_tree_vector_from_ctor (init);
28056 }
28057 else
28058 {
28059 if (TREE_CODE (init) == TREE_LIST)
28060 args = make_tree_vector_from_list (init);
28061 else
28062 args = make_tree_vector_single (init);
28063
28064 if (args->length() == 1)
28065 copy_init = is_spec_or_derived (TREE_TYPE ((*args)[0]), tmpl);
28066 }
28067
28068 tree dname = dguide_name (tmpl);
28069 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
28070 /*type*/false, /*complain*/false,
28071 /*hidden*/false);
28072 bool elided = false;
28073 if (cands == error_mark_node)
28074 cands = NULL_TREE;
28075
28076 /* Prune explicit deduction guides in copy-initialization context. */
28077 if (flags & LOOKUP_ONLYCONVERTING)
28078 {
28079 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28080 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28081 elided = true;
28082
28083 if (elided)
28084 {
28085 /* Found a nonconverting guide, prune the candidates. */
28086 tree pruned = NULL_TREE;
28087 for (lkp_iterator iter (cands); iter; ++iter)
28088 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28089 pruned = lookup_add (*iter, pruned);
28090
28091 cands = pruned;
28092 }
28093 }
28094
28095 tree outer_args = NULL_TREE;
28096 if (DECL_CLASS_SCOPE_P (tmpl)
28097 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28098 {
28099 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28100 type = TREE_TYPE (most_general_template (tmpl));
28101 }
28102
28103 bool saw_ctor = false;
28104 // FIXME cache artificial deduction guides
28105 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28106 {
28107 /* Skip inherited constructors. */
28108 if (iter.using_p ())
28109 continue;
28110
28111 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28112 if (guide == error_mark_node)
28113 return error_mark_node;
28114 if ((flags & LOOKUP_ONLYCONVERTING)
28115 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
28116 elided = true;
28117 else
28118 cands = lookup_add (guide, cands);
28119
28120 saw_ctor = true;
28121 }
28122
28123 if (!copy_init)
28124 if (tree guide = maybe_aggr_guide (type, init))
28125 cands = lookup_add (guide, cands);
28126
28127 tree call = error_mark_node;
28128
28129 /* If this is list-initialization and the class has a list constructor, first
28130 try deducing from the list as a single argument, as [over.match.list]. */
28131 tree list_cands = NULL_TREE;
28132 if (try_list_ctor && cands)
28133 for (lkp_iterator iter (cands); iter; ++iter)
28134 {
28135 tree dg = *iter;
28136 if (is_list_ctor (dg))
28137 list_cands = lookup_add (dg, list_cands);
28138 }
28139 if (list_cands)
28140 {
28141 ++cp_unevaluated_operand;
28142 call = build_new_function_call (list_cands, &args, tf_decltype);
28143 --cp_unevaluated_operand;
28144
28145 if (call == error_mark_node)
28146 {
28147 /* That didn't work, now try treating the list as a sequence of
28148 arguments. */
28149 release_tree_vector (args);
28150 args = make_tree_vector_from_ctor (init);
28151 }
28152 }
28153
28154 /* Maybe generate an implicit deduction guide. */
28155 if (call == error_mark_node && args->length () < 2)
28156 {
28157 tree gtype = NULL_TREE;
28158
28159 if (args->length () == 1)
28160 /* Generate a copy guide. */
28161 gtype = build_reference_type (type);
28162 else if (!saw_ctor)
28163 /* Generate a default guide. */
28164 gtype = type;
28165
28166 if (gtype)
28167 {
28168 tree guide = build_deduction_guide (type, gtype, outer_args,
28169 complain);
28170 if (guide == error_mark_node)
28171 return error_mark_node;
28172 cands = lookup_add (guide, cands);
28173 }
28174 }
28175
28176 if (elided && !cands)
28177 {
28178 error ("cannot deduce template arguments for copy-initialization"
28179 " of %qT, as it has no non-explicit deduction guides or "
28180 "user-declared constructors", type);
28181 return error_mark_node;
28182 }
28183 else if (!cands && call == error_mark_node)
28184 {
28185 error ("cannot deduce template arguments of %qT, as it has no viable "
28186 "deduction guides", type);
28187 return error_mark_node;
28188 }
28189
28190 if (call == error_mark_node)
28191 {
28192 ++cp_unevaluated_operand;
28193 call = build_new_function_call (cands, &args, tf_decltype);
28194 --cp_unevaluated_operand;
28195 }
28196
28197 if (call == error_mark_node && (complain & tf_warning_or_error))
28198 {
28199 error ("class template argument deduction failed:");
28200
28201 ++cp_unevaluated_operand;
28202 call = build_new_function_call (cands, &args, complain | tf_decltype);
28203 --cp_unevaluated_operand;
28204
28205 if (elided)
28206 inform (input_location, "explicit deduction guides not considered "
28207 "for copy-initialization");
28208 }
28209
28210 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28211 }
28212
28213 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28214 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28215 The CONTEXT determines the context in which auto deduction is performed
28216 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28217 OUTER_TARGS are used during template argument deduction
28218 (context == adc_unify) to properly substitute the result, and is ignored
28219 in other contexts.
28220
28221 For partial-concept-ids, extra args may be appended to the list of deduced
28222 template arguments prior to determining constraint satisfaction. */
28223
28224 tree
28225 do_auto_deduction (tree type, tree init, tree auto_node,
28226 tsubst_flags_t complain, auto_deduction_context context,
28227 tree outer_targs, int flags)
28228 {
28229 tree targs;
28230
28231 if (init == error_mark_node)
28232 return error_mark_node;
28233
28234 if (init && type_dependent_expression_p (init)
28235 && context != adc_unify)
28236 /* Defining a subset of type-dependent expressions that we can deduce
28237 from ahead of time isn't worth the trouble. */
28238 return type;
28239
28240 /* Similarly, we can't deduce from another undeduced decl. */
28241 if (init && undeduced_auto_decl (init))
28242 return type;
28243
28244 /* We may be doing a partial substitution, but we still want to replace
28245 auto_node. */
28246 complain &= ~tf_partial;
28247
28248 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28249 /* C++17 class template argument deduction. */
28250 return do_class_deduction (type, tmpl, init, flags, complain);
28251
28252 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28253 /* Nothing we can do with this, even in deduction context. */
28254 return type;
28255
28256 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28257 with either a new invented type template parameter U or, if the
28258 initializer is a braced-init-list (8.5.4), with
28259 std::initializer_list<U>. */
28260 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28261 {
28262 if (!DIRECT_LIST_INIT_P (init))
28263 type = listify_autos (type, auto_node);
28264 else if (CONSTRUCTOR_NELTS (init) == 1)
28265 init = CONSTRUCTOR_ELT (init, 0)->value;
28266 else
28267 {
28268 if (complain & tf_warning_or_error)
28269 {
28270 if (permerror (input_location, "direct-list-initialization of "
28271 "%<auto%> requires exactly one element"))
28272 inform (input_location,
28273 "for deduction to %<std::initializer_list%>, use copy-"
28274 "list-initialization (i.e. add %<=%> before the %<{%>)");
28275 }
28276 type = listify_autos (type, auto_node);
28277 }
28278 }
28279
28280 if (type == error_mark_node)
28281 return error_mark_node;
28282
28283 init = resolve_nondeduced_context (init, complain);
28284
28285 if (context == adc_decomp_type
28286 && auto_node == type
28287 && init != error_mark_node
28288 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28289 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28290 and initializer has array type, deduce cv-qualified array type. */
28291 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28292 complain);
28293 else if (AUTO_IS_DECLTYPE (auto_node))
28294 {
28295 tree stripped_init = tree_strip_any_location_wrapper (init);
28296 bool id = (DECL_P (stripped_init)
28297 || ((TREE_CODE (init) == COMPONENT_REF
28298 || TREE_CODE (init) == SCOPE_REF)
28299 && !REF_PARENTHESIZED_P (init)));
28300 targs = make_tree_vec (1);
28301 TREE_VEC_ELT (targs, 0)
28302 = finish_decltype_type (init, id, tf_warning_or_error);
28303 if (type != auto_node)
28304 {
28305 if (complain & tf_error)
28306 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28307 return error_mark_node;
28308 }
28309 }
28310 else
28311 {
28312 if (error_operand_p (init))
28313 return error_mark_node;
28314
28315 tree parms = build_tree_list (NULL_TREE, type);
28316 tree tparms;
28317
28318 if (flag_concepts)
28319 tparms = extract_autos (type);
28320 else
28321 {
28322 tparms = make_tree_vec (1);
28323 TREE_VEC_ELT (tparms, 0)
28324 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28325 }
28326
28327 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28328 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28329 DEDUCE_CALL,
28330 NULL, /*explain_p=*/false);
28331 if (val > 0)
28332 {
28333 if (processing_template_decl)
28334 /* Try again at instantiation time. */
28335 return type;
28336 if (type && type != error_mark_node
28337 && (complain & tf_error))
28338 /* If type is error_mark_node a diagnostic must have been
28339 emitted by now. Also, having a mention to '<type error>'
28340 in the diagnostic is not really useful to the user. */
28341 {
28342 if (cfun
28343 && FNDECL_USED_AUTO (current_function_decl)
28344 && (auto_node
28345 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28346 && LAMBDA_FUNCTION_P (current_function_decl))
28347 error ("unable to deduce lambda return type from %qE", init);
28348 else
28349 error ("unable to deduce %qT from %qE", type, init);
28350 type_unification_real (tparms, targs, parms, &init, 1, 0,
28351 DEDUCE_CALL,
28352 NULL, /*explain_p=*/true);
28353 }
28354 return error_mark_node;
28355 }
28356 }
28357
28358 /* Check any placeholder constraints against the deduced type. */
28359 if (flag_concepts && !processing_template_decl)
28360 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28361 {
28362 /* Use the deduced type to check the associated constraints. If we
28363 have a partial-concept-id, rebuild the argument list so that
28364 we check using the extra arguments. */
28365 check = unpack_concept_check (check);
28366 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28367 tree cdecl = TREE_OPERAND (check, 0);
28368 if (OVL_P (cdecl))
28369 cdecl = OVL_FIRST (cdecl);
28370 tree cargs = TREE_OPERAND (check, 1);
28371 if (TREE_VEC_LENGTH (cargs) > 1)
28372 {
28373 cargs = copy_node (cargs);
28374 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28375 }
28376 else
28377 cargs = targs;
28378
28379 /* Rebuild the check using the deduced arguments. */
28380 check = build_concept_check (cdecl, cargs, tf_none);
28381
28382 if (!constraints_satisfied_p (check))
28383 {
28384 if (complain & tf_warning_or_error)
28385 {
28386 auto_diagnostic_group d;
28387 switch (context)
28388 {
28389 case adc_unspecified:
28390 case adc_unify:
28391 error("placeholder constraints not satisfied");
28392 break;
28393 case adc_variable_type:
28394 case adc_decomp_type:
28395 error ("deduced initializer does not satisfy "
28396 "placeholder constraints");
28397 break;
28398 case adc_return_type:
28399 error ("deduced return type does not satisfy "
28400 "placeholder constraints");
28401 break;
28402 case adc_requirement:
28403 error ("deduced expression type does not satisfy "
28404 "placeholder constraints");
28405 break;
28406 }
28407 diagnose_constraints (input_location, check, targs);
28408 }
28409 return error_mark_node;
28410 }
28411 }
28412
28413 if (processing_template_decl && context != adc_unify)
28414 outer_targs = current_template_args ();
28415 targs = add_to_template_args (outer_targs, targs);
28416 return tsubst (type, targs, complain, NULL_TREE);
28417 }
28418
28419 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28420 result. */
28421
28422 tree
28423 splice_late_return_type (tree type, tree late_return_type)
28424 {
28425 if (is_auto (type))
28426 {
28427 if (late_return_type)
28428 return late_return_type;
28429
28430 tree idx = get_template_parm_index (type);
28431 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28432 /* In an abbreviated function template we didn't know we were dealing
28433 with a function template when we saw the auto return type, so update
28434 it to have the correct level. */
28435 return make_auto_1 (TYPE_IDENTIFIER (type), true);
28436 }
28437 return type;
28438 }
28439
28440 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28441 'decltype(auto)' or a deduced class template. */
28442
28443 bool
28444 is_auto (const_tree type)
28445 {
28446 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28447 && (TYPE_IDENTIFIER (type) == auto_identifier
28448 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28449 return true;
28450 else
28451 return false;
28452 }
28453
28454 /* for_each_template_parm callback for type_uses_auto. */
28455
28456 int
28457 is_auto_r (tree tp, void */*data*/)
28458 {
28459 return is_auto (tp);
28460 }
28461
28462 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28463 a use of `auto'. Returns NULL_TREE otherwise. */
28464
28465 tree
28466 type_uses_auto (tree type)
28467 {
28468 if (type == NULL_TREE)
28469 return NULL_TREE;
28470 else if (flag_concepts)
28471 {
28472 /* The Concepts TS allows multiple autos in one type-specifier; just
28473 return the first one we find, do_auto_deduction will collect all of
28474 them. */
28475 if (uses_template_parms (type))
28476 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28477 /*visited*/NULL, /*nondeduced*/false);
28478 else
28479 return NULL_TREE;
28480 }
28481 else
28482 return find_type_usage (type, is_auto);
28483 }
28484
28485 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28486 concepts are enabled, auto is acceptable in template arguments, but
28487 only when TEMPL identifies a template class. Return TRUE if any
28488 such errors were reported. */
28489
28490 bool
28491 check_auto_in_tmpl_args (tree tmpl, tree args)
28492 {
28493 /* If there were previous errors, nevermind. */
28494 if (!args || TREE_CODE (args) != TREE_VEC)
28495 return false;
28496
28497 /* If TMPL is an identifier, we're parsing and we can't tell yet
28498 whether TMPL is supposed to be a type, a function or a variable.
28499 We'll only be able to tell during template substitution, so we
28500 expect to be called again then. If concepts are enabled and we
28501 know we have a type, we're ok. */
28502 if (flag_concepts
28503 && (identifier_p (tmpl)
28504 || (DECL_P (tmpl)
28505 && (DECL_TYPE_TEMPLATE_P (tmpl)
28506 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
28507 return false;
28508
28509 /* Quickly search for any occurrences of auto; usually there won't
28510 be any, and then we'll avoid allocating the vector. */
28511 if (!type_uses_auto (args))
28512 return false;
28513
28514 bool errors = false;
28515
28516 tree vec = extract_autos (args);
28517 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
28518 {
28519 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
28520 error_at (DECL_SOURCE_LOCATION (xauto),
28521 "invalid use of %qT in template argument", xauto);
28522 errors = true;
28523 }
28524
28525 return errors;
28526 }
28527
28528 /* For a given template T, return the vector of typedefs referenced
28529 in T for which access check is needed at T instantiation time.
28530 T is either a FUNCTION_DECL or a RECORD_TYPE.
28531 Those typedefs were added to T by the function
28532 append_type_to_template_for_access_check. */
28533
28534 vec<qualified_typedef_usage_t, va_gc> *
28535 get_types_needing_access_check (tree t)
28536 {
28537 tree ti;
28538 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
28539
28540 if (!t || t == error_mark_node)
28541 return NULL;
28542
28543 if (!(ti = get_template_info (t)))
28544 return NULL;
28545
28546 if (CLASS_TYPE_P (t)
28547 || TREE_CODE (t) == FUNCTION_DECL)
28548 {
28549 if (!TI_TEMPLATE (ti))
28550 return NULL;
28551
28552 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
28553 }
28554
28555 return result;
28556 }
28557
28558 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28559 tied to T. That list of typedefs will be access checked at
28560 T instantiation time.
28561 T is either a FUNCTION_DECL or a RECORD_TYPE.
28562 TYPE_DECL is a TYPE_DECL node representing a typedef.
28563 SCOPE is the scope through which TYPE_DECL is accessed.
28564 LOCATION is the location of the usage point of TYPE_DECL.
28565
28566 This function is a subroutine of
28567 append_type_to_template_for_access_check. */
28568
28569 static void
28570 append_type_to_template_for_access_check_1 (tree t,
28571 tree type_decl,
28572 tree scope,
28573 location_t location)
28574 {
28575 qualified_typedef_usage_t typedef_usage;
28576 tree ti;
28577
28578 if (!t || t == error_mark_node)
28579 return;
28580
28581 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
28582 || CLASS_TYPE_P (t))
28583 && type_decl
28584 && TREE_CODE (type_decl) == TYPE_DECL
28585 && scope);
28586
28587 if (!(ti = get_template_info (t)))
28588 return;
28589
28590 gcc_assert (TI_TEMPLATE (ti));
28591
28592 typedef_usage.typedef_decl = type_decl;
28593 typedef_usage.context = scope;
28594 typedef_usage.locus = location;
28595
28596 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
28597 }
28598
28599 /* Append TYPE_DECL to the template TEMPL.
28600 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28601 At TEMPL instanciation time, TYPE_DECL will be checked to see
28602 if it can be accessed through SCOPE.
28603 LOCATION is the location of the usage point of TYPE_DECL.
28604
28605 e.g. consider the following code snippet:
28606
28607 class C
28608 {
28609 typedef int myint;
28610 };
28611
28612 template<class U> struct S
28613 {
28614 C::myint mi; // <-- usage point of the typedef C::myint
28615 };
28616
28617 S<char> s;
28618
28619 At S<char> instantiation time, we need to check the access of C::myint
28620 In other words, we need to check the access of the myint typedef through
28621 the C scope. For that purpose, this function will add the myint typedef
28622 and the scope C through which its being accessed to a list of typedefs
28623 tied to the template S. That list will be walked at template instantiation
28624 time and access check performed on each typedefs it contains.
28625 Note that this particular code snippet should yield an error because
28626 myint is private to C. */
28627
28628 void
28629 append_type_to_template_for_access_check (tree templ,
28630 tree type_decl,
28631 tree scope,
28632 location_t location)
28633 {
28634 qualified_typedef_usage_t *iter;
28635 unsigned i;
28636
28637 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
28638
28639 /* Make sure we don't append the type to the template twice. */
28640 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
28641 if (iter->typedef_decl == type_decl && scope == iter->context)
28642 return;
28643
28644 append_type_to_template_for_access_check_1 (templ, type_decl,
28645 scope, location);
28646 }
28647
28648 /* Recursively walk over && expressions searching for EXPR. Return a reference
28649 to that expression. */
28650
28651 static tree *find_template_requirement (tree *t, tree key)
28652 {
28653 if (*t == key)
28654 return t;
28655 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
28656 {
28657 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
28658 return p;
28659 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
28660 return p;
28661 }
28662 return 0;
28663 }
28664
28665 /* Convert the generic type parameters in PARM that match the types given in the
28666 range [START_IDX, END_IDX) from the current_template_parms into generic type
28667 packs. */
28668
28669 tree
28670 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
28671 {
28672 tree current = current_template_parms;
28673 int depth = TMPL_PARMS_DEPTH (current);
28674 current = INNERMOST_TEMPLATE_PARMS (current);
28675 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
28676
28677 for (int i = 0; i < start_idx; ++i)
28678 TREE_VEC_ELT (replacement, i)
28679 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28680
28681 for (int i = start_idx; i < end_idx; ++i)
28682 {
28683 /* Create a distinct parameter pack type from the current parm and add it
28684 to the replacement args to tsubst below into the generic function
28685 parameter. */
28686 tree node = TREE_VEC_ELT (current, i);
28687 tree o = TREE_TYPE (TREE_VALUE (node));
28688 tree t = copy_type (o);
28689 TEMPLATE_TYPE_PARM_INDEX (t)
28690 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
28691 t, 0, 0, tf_none);
28692 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
28693 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
28694 TYPE_MAIN_VARIANT (t) = t;
28695 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
28696 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28697 TREE_VEC_ELT (replacement, i) = t;
28698
28699 /* Replace the current template parameter with new pack. */
28700 TREE_VALUE (node) = TREE_CHAIN (t);
28701
28702 /* Surgically adjust the associated constraint of adjusted parameter
28703 and it's corresponding contribution to the current template
28704 requirements. */
28705 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
28706 {
28707 tree id = unpack_concept_check (constr);
28708 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
28709 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
28710 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
28711
28712 /* If there was a constraint, we also need to replace that in
28713 the template requirements, which we've already built. */
28714 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
28715 reqs = find_template_requirement (reqs, constr);
28716 *reqs = fold;
28717 }
28718 }
28719
28720 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
28721 TREE_VEC_ELT (replacement, i)
28722 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28723
28724 /* If there are more levels then build up the replacement with the outer
28725 template parms. */
28726 if (depth > 1)
28727 replacement = add_to_template_args (template_parms_to_args
28728 (TREE_CHAIN (current_template_parms)),
28729 replacement);
28730
28731 return tsubst (parm, replacement, tf_none, NULL_TREE);
28732 }
28733
28734 GTY(()) tree current_failed_constraint;
28735
28736 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28737 0..N-1. */
28738
28739 void
28740 declare_integer_pack (void)
28741 {
28742 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28743 build_function_type_list (integer_type_node,
28744 integer_type_node,
28745 NULL_TREE),
28746 NULL_TREE, ECF_CONST);
28747 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28748 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
28749 CP_BUILT_IN_INTEGER_PACK);
28750 }
28751
28752 /* Set up the hash tables for template instantiations. */
28753
28754 void
28755 init_template_processing (void)
28756 {
28757 /* FIXME: enable sanitization (PR87847) */
28758 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28759 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28760
28761 if (cxx_dialect >= cxx11)
28762 declare_integer_pack ();
28763 }
28764
28765 /* Print stats about the template hash tables for -fstats. */
28766
28767 void
28768 print_template_statistics (void)
28769 {
28770 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28771 "%f collisions\n", (long) decl_specializations->size (),
28772 (long) decl_specializations->elements (),
28773 decl_specializations->collisions ());
28774 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28775 "%f collisions\n", (long) type_specializations->size (),
28776 (long) type_specializations->elements (),
28777 type_specializations->collisions ());
28778 }
28779
28780 #if CHECKING_P
28781
28782 namespace selftest {
28783
28784 /* Verify that build_non_dependent_expr () works, for various expressions,
28785 and that location wrappers don't affect the results. */
28786
28787 static void
28788 test_build_non_dependent_expr ()
28789 {
28790 location_t loc = BUILTINS_LOCATION;
28791
28792 /* Verify constants, without and with location wrappers. */
28793 tree int_cst = build_int_cst (integer_type_node, 42);
28794 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28795
28796 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28797 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28798 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28799
28800 tree string_lit = build_string (4, "foo");
28801 TREE_TYPE (string_lit) = char_array_type_node;
28802 string_lit = fix_string_type (string_lit);
28803 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28804
28805 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28806 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28807 ASSERT_EQ (wrapped_string_lit,
28808 build_non_dependent_expr (wrapped_string_lit));
28809 }
28810
28811 /* Verify that type_dependent_expression_p () works correctly, even
28812 in the presence of location wrapper nodes. */
28813
28814 static void
28815 test_type_dependent_expression_p ()
28816 {
28817 location_t loc = BUILTINS_LOCATION;
28818
28819 tree name = get_identifier ("foo");
28820
28821 /* If no templates are involved, nothing is type-dependent. */
28822 gcc_assert (!processing_template_decl);
28823 ASSERT_FALSE (type_dependent_expression_p (name));
28824
28825 ++processing_template_decl;
28826
28827 /* Within a template, an unresolved name is always type-dependent. */
28828 ASSERT_TRUE (type_dependent_expression_p (name));
28829
28830 /* Ensure it copes with NULL_TREE and errors. */
28831 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28832 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28833
28834 /* A USING_DECL in a template should be type-dependent, even if wrapped
28835 with a location wrapper (PR c++/83799). */
28836 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28837 TREE_TYPE (using_decl) = integer_type_node;
28838 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28839 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28840 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28841 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28842
28843 --processing_template_decl;
28844 }
28845
28846 /* Run all of the selftests within this file. */
28847
28848 void
28849 cp_pt_c_tests ()
28850 {
28851 test_build_non_dependent_expr ();
28852 test_type_dependent_expression_p ();
28853 }
28854
28855 } // namespace selftest
28856
28857 #endif /* #if CHECKING_P */
28858
28859 #include "gt-cp-pt.h"