]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
re PR c++/92078 (error: 'struct std::ptr<Iter>' redeclared with different access)
[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 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6399 if (!tinfo)
6400 break;
6401
6402 tree underlying = TI_TEMPLATE (tinfo);
6403 if (!PRIMARY_TEMPLATE_P (underlying)
6404 || (num_innermost_template_parms (tmpl)
6405 != num_innermost_template_parms (underlying)))
6406 break;
6407
6408 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6409 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6410 break;
6411
6412 /* Alias is equivalent. Strip it and repeat. */
6413 tmpl = underlying;
6414 }
6415
6416 return tmpl;
6417 }
6418
6419 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6420 must be a reference-to-function or a pointer-to-function type, as specified
6421 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6422 and check that the resulting function has external linkage. */
6423
6424 static tree
6425 convert_nontype_argument_function (tree type, tree expr,
6426 tsubst_flags_t complain)
6427 {
6428 tree fns = expr;
6429 tree fn, fn_no_ptr;
6430 linkage_kind linkage;
6431
6432 fn = instantiate_type (type, fns, tf_none);
6433 if (fn == error_mark_node)
6434 return error_mark_node;
6435
6436 if (value_dependent_expression_p (fn))
6437 goto accept;
6438
6439 fn_no_ptr = strip_fnptr_conv (fn);
6440 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6441 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6442 if (BASELINK_P (fn_no_ptr))
6443 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6444
6445 /* [temp.arg.nontype]/1
6446
6447 A template-argument for a non-type, non-template template-parameter
6448 shall be one of:
6449 [...]
6450 -- the address of an object or function with external [C++11: or
6451 internal] linkage. */
6452
6453 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6454 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6455 {
6456 if (complain & tf_error)
6457 {
6458 location_t loc = cp_expr_loc_or_input_loc (expr);
6459 error_at (loc, "%qE is not a valid template argument for type %qT",
6460 expr, type);
6461 if (TYPE_PTR_P (type))
6462 inform (loc, "it must be the address of a function "
6463 "with external linkage");
6464 else
6465 inform (loc, "it must be the name of a function with "
6466 "external linkage");
6467 }
6468 return NULL_TREE;
6469 }
6470
6471 linkage = decl_linkage (fn_no_ptr);
6472 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6473 {
6474 if (complain & tf_error)
6475 {
6476 location_t loc = cp_expr_loc_or_input_loc (expr);
6477 if (cxx_dialect >= cxx11)
6478 error_at (loc, "%qE is not a valid template argument for type "
6479 "%qT because %qD has no linkage",
6480 expr, type, fn_no_ptr);
6481 else
6482 error_at (loc, "%qE is not a valid template argument for type "
6483 "%qT because %qD does not have external linkage",
6484 expr, type, fn_no_ptr);
6485 }
6486 return NULL_TREE;
6487 }
6488
6489 accept:
6490 if (TYPE_REF_P (type))
6491 {
6492 if (REFERENCE_REF_P (fn))
6493 fn = TREE_OPERAND (fn, 0);
6494 else
6495 fn = build_address (fn);
6496 }
6497 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6498 fn = build_nop (type, fn);
6499
6500 return fn;
6501 }
6502
6503 /* Subroutine of convert_nontype_argument.
6504 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6505 Emit an error otherwise. */
6506
6507 static bool
6508 check_valid_ptrmem_cst_expr (tree type, tree expr,
6509 tsubst_flags_t complain)
6510 {
6511 tree orig_expr = expr;
6512 STRIP_NOPS (expr);
6513 if (null_ptr_cst_p (expr))
6514 return true;
6515 if (TREE_CODE (expr) == PTRMEM_CST
6516 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6517 PTRMEM_CST_CLASS (expr)))
6518 return true;
6519 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6520 return true;
6521 if (processing_template_decl
6522 && TREE_CODE (expr) == ADDR_EXPR
6523 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6524 return true;
6525 if (complain & tf_error)
6526 {
6527 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6528 error_at (loc, "%qE is not a valid template argument for type %qT",
6529 orig_expr, type);
6530 if (TREE_CODE (expr) != PTRMEM_CST)
6531 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6532 else
6533 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6534 }
6535 return false;
6536 }
6537
6538 /* Returns TRUE iff the address of OP is value-dependent.
6539
6540 14.6.2.4 [temp.dep.temp]:
6541 A non-integral non-type template-argument is dependent if its type is
6542 dependent or it has either of the following forms
6543 qualified-id
6544 & qualified-id
6545 and contains a nested-name-specifier which specifies a class-name that
6546 names a dependent type.
6547
6548 We generalize this to just say that the address of a member of a
6549 dependent class is value-dependent; the above doesn't cover the
6550 address of a static data member named with an unqualified-id. */
6551
6552 static bool
6553 has_value_dependent_address (tree op)
6554 {
6555 STRIP_ANY_LOCATION_WRAPPER (op);
6556
6557 /* We could use get_inner_reference here, but there's no need;
6558 this is only relevant for template non-type arguments, which
6559 can only be expressed as &id-expression. */
6560 if (DECL_P (op))
6561 {
6562 tree ctx = CP_DECL_CONTEXT (op);
6563 if (TYPE_P (ctx) && dependent_type_p (ctx))
6564 return true;
6565 }
6566
6567 return false;
6568 }
6569
6570 /* The next set of functions are used for providing helpful explanatory
6571 diagnostics for failed overload resolution. Their messages should be
6572 indented by two spaces for consistency with the messages in
6573 call.c */
6574
6575 static int
6576 unify_success (bool /*explain_p*/)
6577 {
6578 return 0;
6579 }
6580
6581 /* Other failure functions should call this one, to provide a single function
6582 for setting a breakpoint on. */
6583
6584 static int
6585 unify_invalid (bool /*explain_p*/)
6586 {
6587 return 1;
6588 }
6589
6590 static int
6591 unify_parameter_deduction_failure (bool explain_p, tree parm)
6592 {
6593 if (explain_p)
6594 inform (input_location,
6595 " couldn%'t deduce template parameter %qD", parm);
6596 return unify_invalid (explain_p);
6597 }
6598
6599 static int
6600 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6601 {
6602 if (explain_p)
6603 inform (input_location,
6604 " types %qT and %qT have incompatible cv-qualifiers",
6605 parm, arg);
6606 return unify_invalid (explain_p);
6607 }
6608
6609 static int
6610 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6611 {
6612 if (explain_p)
6613 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6614 return unify_invalid (explain_p);
6615 }
6616
6617 static int
6618 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6619 {
6620 if (explain_p)
6621 inform (input_location,
6622 " template parameter %qD is not a parameter pack, but "
6623 "argument %qD is",
6624 parm, arg);
6625 return unify_invalid (explain_p);
6626 }
6627
6628 static int
6629 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6630 {
6631 if (explain_p)
6632 inform (input_location,
6633 " template argument %qE does not match "
6634 "pointer-to-member constant %qE",
6635 arg, parm);
6636 return unify_invalid (explain_p);
6637 }
6638
6639 static int
6640 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6641 {
6642 if (explain_p)
6643 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6644 return unify_invalid (explain_p);
6645 }
6646
6647 static int
6648 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6649 {
6650 if (explain_p)
6651 inform (input_location,
6652 " inconsistent parameter pack deduction with %qT and %qT",
6653 old_arg, new_arg);
6654 return unify_invalid (explain_p);
6655 }
6656
6657 static int
6658 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6659 {
6660 if (explain_p)
6661 {
6662 if (TYPE_P (parm))
6663 inform (input_location,
6664 " deduced conflicting types for parameter %qT (%qT and %qT)",
6665 parm, first, second);
6666 else
6667 inform (input_location,
6668 " deduced conflicting values for non-type parameter "
6669 "%qE (%qE and %qE)", parm, first, second);
6670 }
6671 return unify_invalid (explain_p);
6672 }
6673
6674 static int
6675 unify_vla_arg (bool explain_p, tree arg)
6676 {
6677 if (explain_p)
6678 inform (input_location,
6679 " variable-sized array type %qT is not "
6680 "a valid template argument",
6681 arg);
6682 return unify_invalid (explain_p);
6683 }
6684
6685 static int
6686 unify_method_type_error (bool explain_p, tree arg)
6687 {
6688 if (explain_p)
6689 inform (input_location,
6690 " member function type %qT is not a valid template argument",
6691 arg);
6692 return unify_invalid (explain_p);
6693 }
6694
6695 static int
6696 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6697 {
6698 if (explain_p)
6699 {
6700 if (least_p)
6701 inform_n (input_location, wanted,
6702 " candidate expects at least %d argument, %d provided",
6703 " candidate expects at least %d arguments, %d provided",
6704 wanted, have);
6705 else
6706 inform_n (input_location, wanted,
6707 " candidate expects %d argument, %d provided",
6708 " candidate expects %d arguments, %d provided",
6709 wanted, have);
6710 }
6711 return unify_invalid (explain_p);
6712 }
6713
6714 static int
6715 unify_too_many_arguments (bool explain_p, int have, int wanted)
6716 {
6717 return unify_arity (explain_p, have, wanted);
6718 }
6719
6720 static int
6721 unify_too_few_arguments (bool explain_p, int have, int wanted,
6722 bool least_p = false)
6723 {
6724 return unify_arity (explain_p, have, wanted, least_p);
6725 }
6726
6727 static int
6728 unify_arg_conversion (bool explain_p, tree to_type,
6729 tree from_type, tree arg)
6730 {
6731 if (explain_p)
6732 inform (cp_expr_loc_or_input_loc (arg),
6733 " cannot convert %qE (type %qT) to type %qT",
6734 arg, from_type, to_type);
6735 return unify_invalid (explain_p);
6736 }
6737
6738 static int
6739 unify_no_common_base (bool explain_p, enum template_base_result r,
6740 tree parm, tree arg)
6741 {
6742 if (explain_p)
6743 switch (r)
6744 {
6745 case tbr_ambiguous_baseclass:
6746 inform (input_location, " %qT is an ambiguous base class of %qT",
6747 parm, arg);
6748 break;
6749 default:
6750 inform (input_location, " %qT is not derived from %qT", arg, parm);
6751 break;
6752 }
6753 return unify_invalid (explain_p);
6754 }
6755
6756 static int
6757 unify_inconsistent_template_template_parameters (bool explain_p)
6758 {
6759 if (explain_p)
6760 inform (input_location,
6761 " template parameters of a template template argument are "
6762 "inconsistent with other deduced template arguments");
6763 return unify_invalid (explain_p);
6764 }
6765
6766 static int
6767 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6768 {
6769 if (explain_p)
6770 inform (input_location,
6771 " cannot deduce a template for %qT from non-template type %qT",
6772 parm, arg);
6773 return unify_invalid (explain_p);
6774 }
6775
6776 static int
6777 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6778 {
6779 if (explain_p)
6780 inform (input_location,
6781 " template argument %qE does not match %qE", arg, parm);
6782 return unify_invalid (explain_p);
6783 }
6784
6785 /* True if T is a C++20 template parameter object to store the argument for a
6786 template parameter of class type. */
6787
6788 bool
6789 template_parm_object_p (const_tree t)
6790 {
6791 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6792 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6793 }
6794
6795 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6796 argument for TYPE, points to an unsuitable object. */
6797
6798 static bool
6799 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6800 {
6801 switch (TREE_CODE (expr))
6802 {
6803 CASE_CONVERT:
6804 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6805 complain);
6806
6807 case TARGET_EXPR:
6808 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6809 complain);
6810
6811 case CONSTRUCTOR:
6812 {
6813 unsigned i; tree elt;
6814 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6815 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6816 return true;
6817 }
6818 break;
6819
6820 case ADDR_EXPR:
6821 {
6822 tree decl = TREE_OPERAND (expr, 0);
6823
6824 if (!VAR_P (decl))
6825 {
6826 if (complain & tf_error)
6827 error_at (cp_expr_loc_or_input_loc (expr),
6828 "%qE is not a valid template argument of type %qT "
6829 "because %qE is not a variable", expr, type, decl);
6830 return true;
6831 }
6832 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6833 {
6834 if (complain & tf_error)
6835 error_at (cp_expr_loc_or_input_loc (expr),
6836 "%qE is not a valid template argument of type %qT "
6837 "in C++98 because %qD does not have external linkage",
6838 expr, type, decl);
6839 return true;
6840 }
6841 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6842 && decl_linkage (decl) == lk_none)
6843 {
6844 if (complain & tf_error)
6845 error_at (cp_expr_loc_or_input_loc (expr),
6846 "%qE is not a valid template argument of type %qT "
6847 "because %qD has no linkage", expr, type, decl);
6848 return true;
6849 }
6850 /* C++17: For a non-type template-parameter of reference or pointer
6851 type, the value of the constant expression shall not refer to (or
6852 for a pointer type, shall not be the address of):
6853 * a subobject (4.5),
6854 * a temporary object (15.2),
6855 * a string literal (5.13.5),
6856 * the result of a typeid expression (8.2.8), or
6857 * a predefined __func__ variable (11.4.1). */
6858 else if (DECL_ARTIFICIAL (decl))
6859 {
6860 if (complain & tf_error)
6861 error ("the address of %qD is not a valid template argument",
6862 decl);
6863 return true;
6864 }
6865 else if (!same_type_ignoring_top_level_qualifiers_p
6866 (strip_array_types (TREE_TYPE (type)),
6867 strip_array_types (TREE_TYPE (decl))))
6868 {
6869 if (complain & tf_error)
6870 error ("the address of the %qT subobject of %qD is not a "
6871 "valid template argument", TREE_TYPE (type), decl);
6872 return true;
6873 }
6874 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6875 {
6876 if (complain & tf_error)
6877 error ("the address of %qD is not a valid template argument "
6878 "because it does not have static storage duration",
6879 decl);
6880 return true;
6881 }
6882 }
6883 break;
6884
6885 default:
6886 if (!INDIRECT_TYPE_P (type))
6887 /* We're only concerned about pointers and references here. */;
6888 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6889 /* Null pointer values are OK in C++11. */;
6890 else
6891 {
6892 if (VAR_P (expr))
6893 {
6894 if (complain & tf_error)
6895 error ("%qD is not a valid template argument "
6896 "because %qD is a variable, not the address of "
6897 "a variable", expr, expr);
6898 return true;
6899 }
6900 else
6901 {
6902 if (complain & tf_error)
6903 error ("%qE is not a valid template argument for %qT "
6904 "because it is not the address of a variable",
6905 expr, type);
6906 return true;
6907 }
6908 }
6909 }
6910 return false;
6911
6912 }
6913
6914 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6915 template argument EXPR. */
6916
6917 static tree
6918 get_template_parm_object (tree expr, tsubst_flags_t complain)
6919 {
6920 if (TREE_CODE (expr) == TARGET_EXPR)
6921 expr = TARGET_EXPR_INITIAL (expr);
6922
6923 if (!TREE_CONSTANT (expr))
6924 {
6925 if ((complain & tf_error)
6926 && require_rvalue_constant_expression (expr))
6927 cxx_constant_value (expr);
6928 return error_mark_node;
6929 }
6930 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6931 return error_mark_node;
6932
6933 tree name = mangle_template_parm_object (expr);
6934 tree decl = get_global_binding (name);
6935 if (decl)
6936 return decl;
6937
6938 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6939 decl = create_temporary_var (type);
6940 TREE_STATIC (decl) = true;
6941 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6942 TREE_READONLY (decl) = true;
6943 DECL_NAME (decl) = name;
6944 SET_DECL_ASSEMBLER_NAME (decl, name);
6945 DECL_CONTEXT (decl) = global_namespace;
6946 comdat_linkage (decl);
6947 pushdecl_top_level_and_finish (decl, expr);
6948 return decl;
6949 }
6950
6951 /* Attempt to convert the non-type template parameter EXPR to the
6952 indicated TYPE. If the conversion is successful, return the
6953 converted value. If the conversion is unsuccessful, return
6954 NULL_TREE if we issued an error message, or error_mark_node if we
6955 did not. We issue error messages for out-and-out bad template
6956 parameters, but not simply because the conversion failed, since we
6957 might be just trying to do argument deduction. Both TYPE and EXPR
6958 must be non-dependent.
6959
6960 The conversion follows the special rules described in
6961 [temp.arg.nontype], and it is much more strict than an implicit
6962 conversion.
6963
6964 This function is called twice for each template argument (see
6965 lookup_template_class for a more accurate description of this
6966 problem). This means that we need to handle expressions which
6967 are not valid in a C++ source, but can be created from the
6968 first call (for instance, casts to perform conversions). These
6969 hacks can go away after we fix the double coercion problem. */
6970
6971 static tree
6972 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6973 {
6974 tree expr_type;
6975 location_t loc = cp_expr_loc_or_input_loc (expr);
6976
6977 /* Detect immediately string literals as invalid non-type argument.
6978 This special-case is not needed for correctness (we would easily
6979 catch this later), but only to provide better diagnostic for this
6980 common user mistake. As suggested by DR 100, we do not mention
6981 linkage issues in the diagnostic as this is not the point. */
6982 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6983 {
6984 if (complain & tf_error)
6985 error ("%qE is not a valid template argument for type %qT "
6986 "because string literals can never be used in this context",
6987 expr, type);
6988 return NULL_TREE;
6989 }
6990
6991 /* Add the ADDR_EXPR now for the benefit of
6992 value_dependent_expression_p. */
6993 if (TYPE_PTROBV_P (type)
6994 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6995 {
6996 expr = decay_conversion (expr, complain);
6997 if (expr == error_mark_node)
6998 return error_mark_node;
6999 }
7000
7001 /* If we are in a template, EXPR may be non-dependent, but still
7002 have a syntactic, rather than semantic, form. For example, EXPR
7003 might be a SCOPE_REF, rather than the VAR_DECL to which the
7004 SCOPE_REF refers. Preserving the qualifying scope is necessary
7005 so that access checking can be performed when the template is
7006 instantiated -- but here we need the resolved form so that we can
7007 convert the argument. */
7008 bool non_dep = false;
7009 if (TYPE_REF_OBJ_P (type)
7010 && has_value_dependent_address (expr))
7011 /* If we want the address and it's value-dependent, don't fold. */;
7012 else if (processing_template_decl
7013 && is_nondependent_constant_expression (expr))
7014 non_dep = true;
7015 if (error_operand_p (expr))
7016 return error_mark_node;
7017 expr_type = TREE_TYPE (expr);
7018
7019 /* If the argument is non-dependent, perform any conversions in
7020 non-dependent context as well. */
7021 processing_template_decl_sentinel s (non_dep);
7022 if (non_dep)
7023 expr = instantiate_non_dependent_expr_internal (expr, complain);
7024
7025 if (value_dependent_expression_p (expr))
7026 expr = canonicalize_expr_argument (expr, complain);
7027
7028 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7029 to a non-type argument of "nullptr". */
7030 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7031 expr = fold_simple (convert (type, expr));
7032
7033 /* In C++11, integral or enumeration non-type template arguments can be
7034 arbitrary constant expressions. Pointer and pointer to
7035 member arguments can be general constant expressions that evaluate
7036 to a null value, but otherwise still need to be of a specific form. */
7037 if (cxx_dialect >= cxx11)
7038 {
7039 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7040 /* A PTRMEM_CST is already constant, and a valid template
7041 argument for a parameter of pointer to member type, we just want
7042 to leave it in that form rather than lower it to a
7043 CONSTRUCTOR. */;
7044 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7045 || cxx_dialect >= cxx17)
7046 {
7047 /* Calling build_converted_constant_expr might create a call to
7048 a conversion function with a value-dependent argument, which
7049 could invoke taking the address of a temporary representing
7050 the result of the conversion. */
7051 if (COMPOUND_LITERAL_P (expr)
7052 && CONSTRUCTOR_IS_DEPENDENT (expr)
7053 && MAYBE_CLASS_TYPE_P (expr_type)
7054 && TYPE_HAS_CONVERSION (expr_type))
7055 {
7056 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
7057 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7058 return expr;
7059 }
7060 /* C++17: A template-argument for a non-type template-parameter shall
7061 be a converted constant expression (8.20) of the type of the
7062 template-parameter. */
7063 expr = build_converted_constant_expr (type, expr, complain);
7064 if (expr == error_mark_node)
7065 /* Make sure we return NULL_TREE only if we have really issued
7066 an error, as described above. */
7067 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7068 expr = maybe_constant_value (expr, NULL_TREE,
7069 /*manifestly_const_eval=*/true);
7070 expr = convert_from_reference (expr);
7071 }
7072 else if (TYPE_PTR_OR_PTRMEM_P (type))
7073 {
7074 tree folded = maybe_constant_value (expr, NULL_TREE,
7075 /*manifestly_const_eval=*/true);
7076 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7077 : null_member_pointer_value_p (folded))
7078 expr = folded;
7079 }
7080 }
7081
7082 if (TYPE_REF_P (type))
7083 expr = mark_lvalue_use (expr);
7084 else
7085 expr = mark_rvalue_use (expr);
7086
7087 /* HACK: Due to double coercion, we can get a
7088 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7089 which is the tree that we built on the first call (see
7090 below when coercing to reference to object or to reference to
7091 function). We just strip everything and get to the arg.
7092 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7093 for examples. */
7094 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7095 {
7096 tree probe_type, probe = expr;
7097 if (REFERENCE_REF_P (probe))
7098 probe = TREE_OPERAND (probe, 0);
7099 probe_type = TREE_TYPE (probe);
7100 if (TREE_CODE (probe) == NOP_EXPR)
7101 {
7102 /* ??? Maybe we could use convert_from_reference here, but we
7103 would need to relax its constraints because the NOP_EXPR
7104 could actually change the type to something more cv-qualified,
7105 and this is not folded by convert_from_reference. */
7106 tree addr = TREE_OPERAND (probe, 0);
7107 if (TYPE_REF_P (probe_type)
7108 && TREE_CODE (addr) == ADDR_EXPR
7109 && TYPE_PTR_P (TREE_TYPE (addr))
7110 && (same_type_ignoring_top_level_qualifiers_p
7111 (TREE_TYPE (probe_type),
7112 TREE_TYPE (TREE_TYPE (addr)))))
7113 {
7114 expr = TREE_OPERAND (addr, 0);
7115 expr_type = TREE_TYPE (probe_type);
7116 }
7117 }
7118 }
7119
7120 /* [temp.arg.nontype]/5, bullet 1
7121
7122 For a non-type template-parameter of integral or enumeration type,
7123 integral promotions (_conv.prom_) and integral conversions
7124 (_conv.integral_) are applied. */
7125 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7126 {
7127 if (cxx_dialect < cxx11)
7128 {
7129 tree t = build_converted_constant_expr (type, expr, complain);
7130 t = maybe_constant_value (t);
7131 if (t != error_mark_node)
7132 expr = t;
7133 }
7134
7135 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7136 return error_mark_node;
7137
7138 /* Notice that there are constant expressions like '4 % 0' which
7139 do not fold into integer constants. */
7140 if (TREE_CODE (expr) != INTEGER_CST
7141 && !value_dependent_expression_p (expr))
7142 {
7143 if (complain & tf_error)
7144 {
7145 int errs = errorcount, warns = warningcount + werrorcount;
7146 if (!require_potential_constant_expression (expr))
7147 expr = error_mark_node;
7148 else
7149 expr = cxx_constant_value (expr);
7150 if (errorcount > errs || warningcount + werrorcount > warns)
7151 inform (loc, "in template argument for type %qT", type);
7152 if (expr == error_mark_node)
7153 return NULL_TREE;
7154 /* else cxx_constant_value complained but gave us
7155 a real constant, so go ahead. */
7156 if (TREE_CODE (expr) != INTEGER_CST)
7157 {
7158 /* Some assemble time constant expressions like
7159 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7160 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7161 as we can emit them into .rodata initializers of
7162 variables, yet they can't fold into an INTEGER_CST at
7163 compile time. Refuse them here. */
7164 gcc_checking_assert (reduced_constant_expression_p (expr));
7165 error_at (loc, "template argument %qE for type %qT not "
7166 "a constant integer", expr, type);
7167 return NULL_TREE;
7168 }
7169 }
7170 else
7171 return NULL_TREE;
7172 }
7173
7174 /* Avoid typedef problems. */
7175 if (TREE_TYPE (expr) != type)
7176 expr = fold_convert (type, expr);
7177 }
7178 /* [temp.arg.nontype]/5, bullet 2
7179
7180 For a non-type template-parameter of type pointer to object,
7181 qualification conversions (_conv.qual_) and the array-to-pointer
7182 conversion (_conv.array_) are applied. */
7183 else if (TYPE_PTROBV_P (type))
7184 {
7185 tree decayed = expr;
7186
7187 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7188 decay_conversion or an explicit cast. If it's a problematic cast,
7189 we'll complain about it below. */
7190 if (TREE_CODE (expr) == NOP_EXPR)
7191 {
7192 tree probe = expr;
7193 STRIP_NOPS (probe);
7194 if (TREE_CODE (probe) == ADDR_EXPR
7195 && TYPE_PTR_P (TREE_TYPE (probe)))
7196 {
7197 expr = probe;
7198 expr_type = TREE_TYPE (expr);
7199 }
7200 }
7201
7202 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7203
7204 A template-argument for a non-type, non-template template-parameter
7205 shall be one of: [...]
7206
7207 -- the name of a non-type template-parameter;
7208 -- the address of an object or function with external linkage, [...]
7209 expressed as "& id-expression" where the & is optional if the name
7210 refers to a function or array, or if the corresponding
7211 template-parameter is a reference.
7212
7213 Here, we do not care about functions, as they are invalid anyway
7214 for a parameter of type pointer-to-object. */
7215
7216 if (value_dependent_expression_p (expr))
7217 /* Non-type template parameters are OK. */
7218 ;
7219 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7220 /* Null pointer values are OK in C++11. */;
7221 else if (TREE_CODE (expr) != ADDR_EXPR
7222 && !INDIRECT_TYPE_P (expr_type))
7223 /* Other values, like integer constants, might be valid
7224 non-type arguments of some other type. */
7225 return error_mark_node;
7226 else if (invalid_tparm_referent_p (type, expr, complain))
7227 return NULL_TREE;
7228
7229 expr = decayed;
7230
7231 expr = perform_qualification_conversions (type, expr);
7232 if (expr == error_mark_node)
7233 return error_mark_node;
7234 }
7235 /* [temp.arg.nontype]/5, bullet 3
7236
7237 For a non-type template-parameter of type reference to object, no
7238 conversions apply. The type referred to by the reference may be more
7239 cv-qualified than the (otherwise identical) type of the
7240 template-argument. The template-parameter is bound directly to the
7241 template-argument, which must be an lvalue. */
7242 else if (TYPE_REF_OBJ_P (type))
7243 {
7244 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7245 expr_type))
7246 return error_mark_node;
7247
7248 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7249 {
7250 if (complain & tf_error)
7251 error ("%qE is not a valid template argument for type %qT "
7252 "because of conflicts in cv-qualification", expr, type);
7253 return NULL_TREE;
7254 }
7255
7256 if (!lvalue_p (expr))
7257 {
7258 if (complain & tf_error)
7259 error ("%qE is not a valid template argument for type %qT "
7260 "because it is not an lvalue", expr, type);
7261 return NULL_TREE;
7262 }
7263
7264 /* [temp.arg.nontype]/1
7265
7266 A template-argument for a non-type, non-template template-parameter
7267 shall be one of: [...]
7268
7269 -- the address of an object or function with external linkage. */
7270 if (INDIRECT_REF_P (expr)
7271 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7272 {
7273 expr = TREE_OPERAND (expr, 0);
7274 if (DECL_P (expr))
7275 {
7276 if (complain & tf_error)
7277 error ("%q#D is not a valid template argument for type %qT "
7278 "because a reference variable does not have a constant "
7279 "address", expr, type);
7280 return NULL_TREE;
7281 }
7282 }
7283
7284 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7285 && value_dependent_expression_p (expr))
7286 /* OK, dependent reference. We don't want to ask whether a DECL is
7287 itself value-dependent, since what we want here is its address. */;
7288 else
7289 {
7290 expr = build_address (expr);
7291
7292 if (invalid_tparm_referent_p (type, expr, complain))
7293 return NULL_TREE;
7294 }
7295
7296 if (!same_type_p (type, TREE_TYPE (expr)))
7297 expr = build_nop (type, expr);
7298 }
7299 /* [temp.arg.nontype]/5, bullet 4
7300
7301 For a non-type template-parameter of type pointer to function, only
7302 the function-to-pointer conversion (_conv.func_) is applied. If the
7303 template-argument represents a set of overloaded functions (or a
7304 pointer to such), the matching function is selected from the set
7305 (_over.over_). */
7306 else if (TYPE_PTRFN_P (type))
7307 {
7308 /* If the argument is a template-id, we might not have enough
7309 context information to decay the pointer. */
7310 if (!type_unknown_p (expr_type))
7311 {
7312 expr = decay_conversion (expr, complain);
7313 if (expr == error_mark_node)
7314 return error_mark_node;
7315 }
7316
7317 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7318 /* Null pointer values are OK in C++11. */
7319 return perform_qualification_conversions (type, expr);
7320
7321 expr = convert_nontype_argument_function (type, expr, complain);
7322 if (!expr || expr == error_mark_node)
7323 return expr;
7324 }
7325 /* [temp.arg.nontype]/5, bullet 5
7326
7327 For a non-type template-parameter of type reference to function, no
7328 conversions apply. If the template-argument represents a set of
7329 overloaded functions, the matching function is selected from the set
7330 (_over.over_). */
7331 else if (TYPE_REFFN_P (type))
7332 {
7333 if (TREE_CODE (expr) == ADDR_EXPR)
7334 {
7335 if (complain & tf_error)
7336 {
7337 error ("%qE is not a valid template argument for type %qT "
7338 "because it is a pointer", expr, type);
7339 inform (input_location, "try using %qE instead",
7340 TREE_OPERAND (expr, 0));
7341 }
7342 return NULL_TREE;
7343 }
7344
7345 expr = convert_nontype_argument_function (type, expr, complain);
7346 if (!expr || expr == error_mark_node)
7347 return expr;
7348 }
7349 /* [temp.arg.nontype]/5, bullet 6
7350
7351 For a non-type template-parameter of type pointer to member function,
7352 no conversions apply. If the template-argument represents a set of
7353 overloaded member functions, the matching member function is selected
7354 from the set (_over.over_). */
7355 else if (TYPE_PTRMEMFUNC_P (type))
7356 {
7357 expr = instantiate_type (type, expr, tf_none);
7358 if (expr == error_mark_node)
7359 return error_mark_node;
7360
7361 /* [temp.arg.nontype] bullet 1 says the pointer to member
7362 expression must be a pointer-to-member constant. */
7363 if (!value_dependent_expression_p (expr)
7364 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7365 return NULL_TREE;
7366
7367 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7368 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7369 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7370 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7371 }
7372 /* [temp.arg.nontype]/5, bullet 7
7373
7374 For a non-type template-parameter of type pointer to data member,
7375 qualification conversions (_conv.qual_) are applied. */
7376 else if (TYPE_PTRDATAMEM_P (type))
7377 {
7378 /* [temp.arg.nontype] bullet 1 says the pointer to member
7379 expression must be a pointer-to-member constant. */
7380 if (!value_dependent_expression_p (expr)
7381 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7382 return NULL_TREE;
7383
7384 expr = perform_qualification_conversions (type, expr);
7385 if (expr == error_mark_node)
7386 return expr;
7387 }
7388 else if (NULLPTR_TYPE_P (type))
7389 {
7390 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7391 {
7392 if (complain & tf_error)
7393 error ("%qE is not a valid template argument for type %qT "
7394 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7395 return NULL_TREE;
7396 }
7397 return expr;
7398 }
7399 else if (CLASS_TYPE_P (type))
7400 {
7401 /* Replace the argument with a reference to the corresponding template
7402 parameter object. */
7403 if (!value_dependent_expression_p (expr))
7404 expr = get_template_parm_object (expr, complain);
7405 if (expr == error_mark_node)
7406 return NULL_TREE;
7407 }
7408 /* A template non-type parameter must be one of the above. */
7409 else
7410 gcc_unreachable ();
7411
7412 /* Sanity check: did we actually convert the argument to the
7413 right type? */
7414 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7415 (type, TREE_TYPE (expr)));
7416 return convert_from_reference (expr);
7417 }
7418
7419 /* Subroutine of coerce_template_template_parms, which returns 1 if
7420 PARM_PARM and ARG_PARM match using the rule for the template
7421 parameters of template template parameters. Both PARM and ARG are
7422 template parameters; the rest of the arguments are the same as for
7423 coerce_template_template_parms.
7424 */
7425 static int
7426 coerce_template_template_parm (tree parm,
7427 tree arg,
7428 tsubst_flags_t complain,
7429 tree in_decl,
7430 tree outer_args)
7431 {
7432 if (arg == NULL_TREE || error_operand_p (arg)
7433 || parm == NULL_TREE || error_operand_p (parm))
7434 return 0;
7435
7436 if (TREE_CODE (arg) != TREE_CODE (parm))
7437 return 0;
7438
7439 switch (TREE_CODE (parm))
7440 {
7441 case TEMPLATE_DECL:
7442 /* We encounter instantiations of templates like
7443 template <template <template <class> class> class TT>
7444 class C; */
7445 {
7446 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7447 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7448
7449 if (!coerce_template_template_parms
7450 (parmparm, argparm, complain, in_decl, outer_args))
7451 return 0;
7452 }
7453 /* Fall through. */
7454
7455 case TYPE_DECL:
7456 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7457 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7458 /* Argument is a parameter pack but parameter is not. */
7459 return 0;
7460 break;
7461
7462 case PARM_DECL:
7463 /* The tsubst call is used to handle cases such as
7464
7465 template <int> class C {};
7466 template <class T, template <T> class TT> class D {};
7467 D<int, C> d;
7468
7469 i.e. the parameter list of TT depends on earlier parameters. */
7470 if (!uses_template_parms (TREE_TYPE (arg)))
7471 {
7472 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7473 if (!uses_template_parms (t)
7474 && !same_type_p (t, TREE_TYPE (arg)))
7475 return 0;
7476 }
7477
7478 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7479 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7480 /* Argument is a parameter pack but parameter is not. */
7481 return 0;
7482
7483 break;
7484
7485 default:
7486 gcc_unreachable ();
7487 }
7488
7489 return 1;
7490 }
7491
7492 /* Coerce template argument list ARGLIST for use with template
7493 template-parameter TEMPL. */
7494
7495 static tree
7496 coerce_template_args_for_ttp (tree templ, tree arglist,
7497 tsubst_flags_t complain)
7498 {
7499 /* Consider an example where a template template parameter declared as
7500
7501 template <class T, class U = std::allocator<T> > class TT
7502
7503 The template parameter level of T and U are one level larger than
7504 of TT. To proper process the default argument of U, say when an
7505 instantiation `TT<int>' is seen, we need to build the full
7506 arguments containing {int} as the innermost level. Outer levels,
7507 available when not appearing as default template argument, can be
7508 obtained from the arguments of the enclosing template.
7509
7510 Suppose that TT is later substituted with std::vector. The above
7511 instantiation is `TT<int, std::allocator<T> >' with TT at
7512 level 1, and T at level 2, while the template arguments at level 1
7513 becomes {std::vector} and the inner level 2 is {int}. */
7514
7515 tree outer = DECL_CONTEXT (templ);
7516 if (outer)
7517 outer = generic_targs_for (outer);
7518 else if (current_template_parms)
7519 {
7520 /* This is an argument of the current template, so we haven't set
7521 DECL_CONTEXT yet. */
7522 tree relevant_template_parms;
7523
7524 /* Parameter levels that are greater than the level of the given
7525 template template parm are irrelevant. */
7526 relevant_template_parms = current_template_parms;
7527 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7528 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7529 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7530
7531 outer = template_parms_to_args (relevant_template_parms);
7532 }
7533
7534 if (outer)
7535 arglist = add_to_template_args (outer, arglist);
7536
7537 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7538 return coerce_template_parms (parmlist, arglist, templ,
7539 complain,
7540 /*require_all_args=*/true,
7541 /*use_default_args=*/true);
7542 }
7543
7544 /* A cache of template template parameters with match-all default
7545 arguments. */
7546 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7547
7548 /* T is a bound template template-parameter. Copy its arguments into default
7549 arguments of the template template-parameter's template parameters. */
7550
7551 static tree
7552 add_defaults_to_ttp (tree otmpl)
7553 {
7554 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7555 return *c;
7556
7557 tree ntmpl = copy_node (otmpl);
7558
7559 tree ntype = copy_node (TREE_TYPE (otmpl));
7560 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7561 TYPE_MAIN_VARIANT (ntype) = ntype;
7562 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7563 TYPE_NAME (ntype) = ntmpl;
7564 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7565
7566 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7567 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7568 TEMPLATE_PARM_DECL (idx) = ntmpl;
7569 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7570
7571 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7572 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7573 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7574 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7575 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7576 {
7577 tree o = TREE_VEC_ELT (vec, i);
7578 if (!template_parameter_pack_p (TREE_VALUE (o)))
7579 {
7580 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7581 TREE_PURPOSE (n) = any_targ_node;
7582 }
7583 }
7584
7585 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7586 return ntmpl;
7587 }
7588
7589 /* ARG is a bound potential template template-argument, and PARGS is a list
7590 of arguments for the corresponding template template-parameter. Adjust
7591 PARGS as appropriate for application to ARG's template, and if ARG is a
7592 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7593 arguments to the template template parameter. */
7594
7595 static tree
7596 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7597 {
7598 ++processing_template_decl;
7599 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7600 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7601 {
7602 /* When comparing two template template-parameters in partial ordering,
7603 rewrite the one currently being used as an argument to have default
7604 arguments for all parameters. */
7605 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7606 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7607 if (pargs != error_mark_node)
7608 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7609 TYPE_TI_ARGS (arg));
7610 }
7611 else
7612 {
7613 tree aparms
7614 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7615 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7616 /*require_all*/true,
7617 /*use_default*/true);
7618 }
7619 --processing_template_decl;
7620 return pargs;
7621 }
7622
7623 /* Subroutine of unify for the case when PARM is a
7624 BOUND_TEMPLATE_TEMPLATE_PARM. */
7625
7626 static int
7627 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7628 bool explain_p)
7629 {
7630 tree parmvec = TYPE_TI_ARGS (parm);
7631 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7632
7633 /* The template template parm might be variadic and the argument
7634 not, so flatten both argument lists. */
7635 parmvec = expand_template_argument_pack (parmvec);
7636 argvec = expand_template_argument_pack (argvec);
7637
7638 if (flag_new_ttp)
7639 {
7640 /* In keeping with P0522R0, adjust P's template arguments
7641 to apply to A's template; then flatten it again. */
7642 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7643 nparmvec = expand_template_argument_pack (nparmvec);
7644
7645 if (unify (tparms, targs, nparmvec, argvec,
7646 UNIFY_ALLOW_NONE, explain_p))
7647 return 1;
7648
7649 /* If the P0522 adjustment eliminated a pack expansion, deduce
7650 empty packs. */
7651 if (flag_new_ttp
7652 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7653 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7654 DEDUCE_EXACT, /*sub*/true, explain_p))
7655 return 1;
7656 }
7657 else
7658 {
7659 /* Deduce arguments T, i from TT<T> or TT<i>.
7660 We check each element of PARMVEC and ARGVEC individually
7661 rather than the whole TREE_VEC since they can have
7662 different number of elements, which is allowed under N2555. */
7663
7664 int len = TREE_VEC_LENGTH (parmvec);
7665
7666 /* Check if the parameters end in a pack, making them
7667 variadic. */
7668 int parm_variadic_p = 0;
7669 if (len > 0
7670 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7671 parm_variadic_p = 1;
7672
7673 for (int i = 0; i < len - parm_variadic_p; ++i)
7674 /* If the template argument list of P contains a pack
7675 expansion that is not the last template argument, the
7676 entire template argument list is a non-deduced
7677 context. */
7678 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7679 return unify_success (explain_p);
7680
7681 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7682 return unify_too_few_arguments (explain_p,
7683 TREE_VEC_LENGTH (argvec), len);
7684
7685 for (int i = 0; i < len - parm_variadic_p; ++i)
7686 if (unify (tparms, targs,
7687 TREE_VEC_ELT (parmvec, i),
7688 TREE_VEC_ELT (argvec, i),
7689 UNIFY_ALLOW_NONE, explain_p))
7690 return 1;
7691
7692 if (parm_variadic_p
7693 && unify_pack_expansion (tparms, targs,
7694 parmvec, argvec,
7695 DEDUCE_EXACT,
7696 /*subr=*/true, explain_p))
7697 return 1;
7698 }
7699
7700 return 0;
7701 }
7702
7703 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7704 template template parameters. Both PARM_PARMS and ARG_PARMS are
7705 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7706 or PARM_DECL.
7707
7708 Consider the example:
7709 template <class T> class A;
7710 template<template <class U> class TT> class B;
7711
7712 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7713 the parameters to A, and OUTER_ARGS contains A. */
7714
7715 static int
7716 coerce_template_template_parms (tree parm_parms,
7717 tree arg_parms,
7718 tsubst_flags_t complain,
7719 tree in_decl,
7720 tree outer_args)
7721 {
7722 int nparms, nargs, i;
7723 tree parm, arg;
7724 int variadic_p = 0;
7725
7726 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7727 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7728
7729 nparms = TREE_VEC_LENGTH (parm_parms);
7730 nargs = TREE_VEC_LENGTH (arg_parms);
7731
7732 if (flag_new_ttp)
7733 {
7734 /* P0522R0: A template template-parameter P is at least as specialized as
7735 a template template-argument A if, given the following rewrite to two
7736 function templates, the function template corresponding to P is at
7737 least as specialized as the function template corresponding to A
7738 according to the partial ordering rules for function templates
7739 ([temp.func.order]). Given an invented class template X with the
7740 template parameter list of A (including default arguments):
7741
7742 * Each of the two function templates has the same template parameters,
7743 respectively, as P or A.
7744
7745 * Each function template has a single function parameter whose type is
7746 a specialization of X with template arguments corresponding to the
7747 template parameters from the respective function template where, for
7748 each template parameter PP in the template parameter list of the
7749 function template, a corresponding template argument AA is formed. If
7750 PP declares a parameter pack, then AA is the pack expansion
7751 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7752
7753 If the rewrite produces an invalid type, then P is not at least as
7754 specialized as A. */
7755
7756 /* So coerce P's args to apply to A's parms, and then deduce between A's
7757 args and the converted args. If that succeeds, A is at least as
7758 specialized as P, so they match.*/
7759 tree pargs = template_parms_level_to_args (parm_parms);
7760 pargs = add_outermost_template_args (outer_args, pargs);
7761 ++processing_template_decl;
7762 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7763 /*require_all*/true, /*use_default*/true);
7764 --processing_template_decl;
7765 if (pargs != error_mark_node)
7766 {
7767 tree targs = make_tree_vec (nargs);
7768 tree aargs = template_parms_level_to_args (arg_parms);
7769 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7770 /*explain*/false))
7771 return 1;
7772 }
7773 }
7774
7775 /* Determine whether we have a parameter pack at the end of the
7776 template template parameter's template parameter list. */
7777 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7778 {
7779 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7780
7781 if (error_operand_p (parm))
7782 return 0;
7783
7784 switch (TREE_CODE (parm))
7785 {
7786 case TEMPLATE_DECL:
7787 case TYPE_DECL:
7788 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7789 variadic_p = 1;
7790 break;
7791
7792 case PARM_DECL:
7793 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7794 variadic_p = 1;
7795 break;
7796
7797 default:
7798 gcc_unreachable ();
7799 }
7800 }
7801
7802 if (nargs != nparms
7803 && !(variadic_p && nargs >= nparms - 1))
7804 return 0;
7805
7806 /* Check all of the template parameters except the parameter pack at
7807 the end (if any). */
7808 for (i = 0; i < nparms - variadic_p; ++i)
7809 {
7810 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7811 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7812 continue;
7813
7814 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7815 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7816
7817 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7818 outer_args))
7819 return 0;
7820
7821 }
7822
7823 if (variadic_p)
7824 {
7825 /* Check each of the template parameters in the template
7826 argument against the template parameter pack at the end of
7827 the template template parameter. */
7828 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7829 return 0;
7830
7831 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7832
7833 for (; i < nargs; ++i)
7834 {
7835 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7836 continue;
7837
7838 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7839
7840 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7841 outer_args))
7842 return 0;
7843 }
7844 }
7845
7846 return 1;
7847 }
7848
7849 /* Verifies that the deduced template arguments (in TARGS) for the
7850 template template parameters (in TPARMS) represent valid bindings,
7851 by comparing the template parameter list of each template argument
7852 to the template parameter list of its corresponding template
7853 template parameter, in accordance with DR150. This
7854 routine can only be called after all template arguments have been
7855 deduced. It will return TRUE if all of the template template
7856 parameter bindings are okay, FALSE otherwise. */
7857 bool
7858 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7859 {
7860 int i, ntparms = TREE_VEC_LENGTH (tparms);
7861 bool ret = true;
7862
7863 /* We're dealing with template parms in this process. */
7864 ++processing_template_decl;
7865
7866 targs = INNERMOST_TEMPLATE_ARGS (targs);
7867
7868 for (i = 0; i < ntparms; ++i)
7869 {
7870 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7871 tree targ = TREE_VEC_ELT (targs, i);
7872
7873 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7874 {
7875 tree packed_args = NULL_TREE;
7876 int idx, len = 1;
7877
7878 if (ARGUMENT_PACK_P (targ))
7879 {
7880 /* Look inside the argument pack. */
7881 packed_args = ARGUMENT_PACK_ARGS (targ);
7882 len = TREE_VEC_LENGTH (packed_args);
7883 }
7884
7885 for (idx = 0; idx < len; ++idx)
7886 {
7887 tree targ_parms = NULL_TREE;
7888
7889 if (packed_args)
7890 /* Extract the next argument from the argument
7891 pack. */
7892 targ = TREE_VEC_ELT (packed_args, idx);
7893
7894 if (PACK_EXPANSION_P (targ))
7895 /* Look at the pattern of the pack expansion. */
7896 targ = PACK_EXPANSION_PATTERN (targ);
7897
7898 /* Extract the template parameters from the template
7899 argument. */
7900 if (TREE_CODE (targ) == TEMPLATE_DECL)
7901 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7902 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7903 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7904
7905 /* Verify that we can coerce the template template
7906 parameters from the template argument to the template
7907 parameter. This requires an exact match. */
7908 if (targ_parms
7909 && !coerce_template_template_parms
7910 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7911 targ_parms,
7912 tf_none,
7913 tparm,
7914 targs))
7915 {
7916 ret = false;
7917 goto out;
7918 }
7919 }
7920 }
7921 }
7922
7923 out:
7924
7925 --processing_template_decl;
7926 return ret;
7927 }
7928
7929 /* Since type attributes aren't mangled, we need to strip them from
7930 template type arguments. */
7931
7932 static tree
7933 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7934 {
7935 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7936 return arg;
7937 bool removed_attributes = false;
7938 tree canon = strip_typedefs (arg, &removed_attributes);
7939 if (removed_attributes
7940 && (complain & tf_warning))
7941 warning (OPT_Wignored_attributes,
7942 "ignoring attributes on template argument %qT", arg);
7943 return canon;
7944 }
7945
7946 /* And from inside dependent non-type arguments like sizeof(Type). */
7947
7948 static tree
7949 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7950 {
7951 if (!arg || arg == error_mark_node)
7952 return arg;
7953 bool removed_attributes = false;
7954 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7955 if (removed_attributes
7956 && (complain & tf_warning))
7957 warning (OPT_Wignored_attributes,
7958 "ignoring attributes in template argument %qE", arg);
7959 return canon;
7960 }
7961
7962 // A template declaration can be substituted for a constrained
7963 // template template parameter only when the argument is more
7964 // constrained than the parameter.
7965 static bool
7966 is_compatible_template_arg (tree parm, tree arg)
7967 {
7968 tree parm_cons = get_constraints (parm);
7969
7970 /* For now, allow constrained template template arguments
7971 and unconstrained template template parameters. */
7972 if (parm_cons == NULL_TREE)
7973 return true;
7974
7975 /* If the template parameter is constrained, we need to rewrite its
7976 constraints in terms of the ARG's template parameters. This ensures
7977 that all of the template parameter types will have the same depth.
7978
7979 Note that this is only valid when coerce_template_template_parm is
7980 true for the innermost template parameters of PARM and ARG. In other
7981 words, because coercion is successful, this conversion will be valid. */
7982 tree new_args = NULL_TREE;
7983 if (parm_cons)
7984 {
7985 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7986 new_args = template_parms_level_to_args (aparms);
7987 parm_cons = tsubst_constraint_info (parm_cons, new_args,
7988 tf_none, NULL_TREE);
7989 if (parm_cons == error_mark_node)
7990 return false;
7991 }
7992
7993 return weakly_subsumes (parm_cons, new_args, arg);
7994 }
7995
7996 // Convert a placeholder argument into a binding to the original
7997 // parameter. The original parameter is saved as the TREE_TYPE of
7998 // ARG.
7999 static inline tree
8000 convert_wildcard_argument (tree parm, tree arg)
8001 {
8002 TREE_TYPE (arg) = parm;
8003 return arg;
8004 }
8005
8006 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8007 because one of them is dependent. But we need to represent the
8008 conversion for the benefit of cp_tree_equal. */
8009
8010 static tree
8011 maybe_convert_nontype_argument (tree type, tree arg)
8012 {
8013 /* Auto parms get no conversion. */
8014 if (type_uses_auto (type))
8015 return arg;
8016 /* We don't need or want to add this conversion now if we're going to use the
8017 argument for deduction. */
8018 if (value_dependent_expression_p (arg))
8019 return arg;
8020
8021 type = cv_unqualified (type);
8022 tree argtype = TREE_TYPE (arg);
8023 if (same_type_p (type, argtype))
8024 return arg;
8025
8026 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8027 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8028 return arg;
8029 }
8030
8031 /* Convert the indicated template ARG as necessary to match the
8032 indicated template PARM. Returns the converted ARG, or
8033 error_mark_node if the conversion was unsuccessful. Error and
8034 warning messages are issued under control of COMPLAIN. This
8035 conversion is for the Ith parameter in the parameter list. ARGS is
8036 the full set of template arguments deduced so far. */
8037
8038 static tree
8039 convert_template_argument (tree parm,
8040 tree arg,
8041 tree args,
8042 tsubst_flags_t complain,
8043 int i,
8044 tree in_decl)
8045 {
8046 tree orig_arg;
8047 tree val;
8048 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8049
8050 if (parm == error_mark_node || error_operand_p (arg))
8051 return error_mark_node;
8052
8053 /* Trivially convert placeholders. */
8054 if (TREE_CODE (arg) == WILDCARD_DECL)
8055 return convert_wildcard_argument (parm, arg);
8056
8057 if (arg == any_targ_node)
8058 return arg;
8059
8060 if (TREE_CODE (arg) == TREE_LIST
8061 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8062 {
8063 /* The template argument was the name of some
8064 member function. That's usually
8065 invalid, but static members are OK. In any
8066 case, grab the underlying fields/functions
8067 and issue an error later if required. */
8068 TREE_TYPE (arg) = unknown_type_node;
8069 }
8070
8071 orig_arg = arg;
8072
8073 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8074 requires_type = (TREE_CODE (parm) == TYPE_DECL
8075 || requires_tmpl_type);
8076
8077 /* When determining whether an argument pack expansion is a template,
8078 look at the pattern. */
8079 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
8080 arg = PACK_EXPANSION_PATTERN (arg);
8081
8082 /* Deal with an injected-class-name used as a template template arg. */
8083 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8084 {
8085 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8086 if (TREE_CODE (t) == TEMPLATE_DECL)
8087 {
8088 if (cxx_dialect >= cxx11)
8089 /* OK under DR 1004. */;
8090 else if (complain & tf_warning_or_error)
8091 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8092 " used as template template argument", TYPE_NAME (arg));
8093 else if (flag_pedantic_errors)
8094 t = arg;
8095
8096 arg = t;
8097 }
8098 }
8099
8100 is_tmpl_type =
8101 ((TREE_CODE (arg) == TEMPLATE_DECL
8102 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8103 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8104 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8105 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8106
8107 if (is_tmpl_type
8108 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8109 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8110 arg = TYPE_STUB_DECL (arg);
8111
8112 is_type = TYPE_P (arg) || is_tmpl_type;
8113
8114 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8115 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8116 {
8117 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8118 {
8119 if (complain & tf_error)
8120 error ("invalid use of destructor %qE as a type", orig_arg);
8121 return error_mark_node;
8122 }
8123
8124 permerror (input_location,
8125 "to refer to a type member of a template parameter, "
8126 "use %<typename %E%>", orig_arg);
8127
8128 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8129 TREE_OPERAND (arg, 1),
8130 typename_type,
8131 complain);
8132 arg = orig_arg;
8133 is_type = 1;
8134 }
8135 if (is_type != requires_type)
8136 {
8137 if (in_decl)
8138 {
8139 if (complain & tf_error)
8140 {
8141 error ("type/value mismatch at argument %d in template "
8142 "parameter list for %qD",
8143 i + 1, in_decl);
8144 if (is_type)
8145 {
8146 /* The template argument is a type, but we're expecting
8147 an expression. */
8148 inform (input_location,
8149 " expected a constant of type %qT, got %qT",
8150 TREE_TYPE (parm),
8151 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8152 /* [temp.arg]/2: "In a template-argument, an ambiguity
8153 between a type-id and an expression is resolved to a
8154 type-id, regardless of the form of the corresponding
8155 template-parameter." So give the user a clue. */
8156 if (TREE_CODE (arg) == FUNCTION_TYPE)
8157 inform (input_location, " ambiguous template argument "
8158 "for non-type template parameter is treated as "
8159 "function type");
8160 }
8161 else if (requires_tmpl_type)
8162 inform (input_location,
8163 " expected a class template, got %qE", orig_arg);
8164 else
8165 inform (input_location,
8166 " expected a type, got %qE", orig_arg);
8167 }
8168 }
8169 return error_mark_node;
8170 }
8171 if (is_tmpl_type ^ requires_tmpl_type)
8172 {
8173 if (in_decl && (complain & tf_error))
8174 {
8175 error ("type/value mismatch at argument %d in template "
8176 "parameter list for %qD",
8177 i + 1, in_decl);
8178 if (is_tmpl_type)
8179 inform (input_location,
8180 " expected a type, got %qT", DECL_NAME (arg));
8181 else
8182 inform (input_location,
8183 " expected a class template, got %qT", orig_arg);
8184 }
8185 return error_mark_node;
8186 }
8187
8188 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8189 /* We already did the appropriate conversion when packing args. */
8190 val = orig_arg;
8191 else if (is_type)
8192 {
8193 if (requires_tmpl_type)
8194 {
8195 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8196 /* The number of argument required is not known yet.
8197 Just accept it for now. */
8198 val = orig_arg;
8199 else
8200 {
8201 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8202 tree argparm;
8203
8204 /* Strip alias templates that are equivalent to another
8205 template. */
8206 arg = get_underlying_template (arg);
8207 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8208
8209 if (coerce_template_template_parms (parmparm, argparm,
8210 complain, in_decl,
8211 args))
8212 {
8213 val = arg;
8214
8215 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8216 TEMPLATE_DECL. */
8217 if (val != error_mark_node)
8218 {
8219 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8220 val = TREE_TYPE (val);
8221 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8222 val = make_pack_expansion (val, complain);
8223 }
8224 }
8225 else
8226 {
8227 if (in_decl && (complain & tf_error))
8228 {
8229 error ("type/value mismatch at argument %d in "
8230 "template parameter list for %qD",
8231 i + 1, in_decl);
8232 inform (input_location,
8233 " expected a template of type %qD, got %qT",
8234 parm, orig_arg);
8235 }
8236
8237 val = error_mark_node;
8238 }
8239
8240 // Check that the constraints are compatible before allowing the
8241 // substitution.
8242 if (val != error_mark_node)
8243 if (!is_compatible_template_arg (parm, arg))
8244 {
8245 if (in_decl && (complain & tf_error))
8246 {
8247 error ("constraint mismatch at argument %d in "
8248 "template parameter list for %qD",
8249 i + 1, in_decl);
8250 inform (input_location, " expected %qD but got %qD",
8251 parm, arg);
8252 }
8253 val = error_mark_node;
8254 }
8255 }
8256 }
8257 else
8258 val = orig_arg;
8259 /* We only form one instance of each template specialization.
8260 Therefore, if we use a non-canonical variant (i.e., a
8261 typedef), any future messages referring to the type will use
8262 the typedef, which is confusing if those future uses do not
8263 themselves also use the typedef. */
8264 if (TYPE_P (val))
8265 val = canonicalize_type_argument (val, complain);
8266 }
8267 else
8268 {
8269 tree t = TREE_TYPE (parm);
8270
8271 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8272 > TMPL_ARGS_DEPTH (args))
8273 /* We don't have enough levels of args to do any substitution. This
8274 can happen in the context of -fnew-ttp-matching. */;
8275 else if (tree a = type_uses_auto (t))
8276 {
8277 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8278 if (t == error_mark_node)
8279 return error_mark_node;
8280 }
8281 else
8282 t = tsubst (t, args, complain, in_decl);
8283
8284 if (invalid_nontype_parm_type_p (t, complain))
8285 return error_mark_node;
8286
8287 if (t != TREE_TYPE (parm))
8288 t = canonicalize_type_argument (t, complain);
8289
8290 if (!type_dependent_expression_p (orig_arg)
8291 && !uses_template_parms (t))
8292 /* We used to call digest_init here. However, digest_init
8293 will report errors, which we don't want when complain
8294 is zero. More importantly, digest_init will try too
8295 hard to convert things: for example, `0' should not be
8296 converted to pointer type at this point according to
8297 the standard. Accepting this is not merely an
8298 extension, since deciding whether or not these
8299 conversions can occur is part of determining which
8300 function template to call, or whether a given explicit
8301 argument specification is valid. */
8302 val = convert_nontype_argument (t, orig_arg, complain);
8303 else
8304 {
8305 val = canonicalize_expr_argument (orig_arg, complain);
8306 val = maybe_convert_nontype_argument (t, val);
8307 }
8308
8309
8310 if (val == NULL_TREE)
8311 val = error_mark_node;
8312 else if (val == error_mark_node && (complain & tf_error))
8313 error_at (cp_expr_loc_or_input_loc (orig_arg),
8314 "could not convert template argument %qE from %qT to %qT",
8315 orig_arg, TREE_TYPE (orig_arg), t);
8316
8317 if (INDIRECT_REF_P (val))
8318 {
8319 /* Reject template arguments that are references to built-in
8320 functions with no library fallbacks. */
8321 const_tree inner = TREE_OPERAND (val, 0);
8322 const_tree innertype = TREE_TYPE (inner);
8323 if (innertype
8324 && TYPE_REF_P (innertype)
8325 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8326 && TREE_OPERAND_LENGTH (inner) > 0
8327 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8328 return error_mark_node;
8329 }
8330
8331 if (TREE_CODE (val) == SCOPE_REF)
8332 {
8333 /* Strip typedefs from the SCOPE_REF. */
8334 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8335 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8336 complain);
8337 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8338 QUALIFIED_NAME_IS_TEMPLATE (val));
8339 }
8340 }
8341
8342 return val;
8343 }
8344
8345 /* Coerces the remaining template arguments in INNER_ARGS (from
8346 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8347 Returns the coerced argument pack. PARM_IDX is the position of this
8348 parameter in the template parameter list. ARGS is the original
8349 template argument list. */
8350 static tree
8351 coerce_template_parameter_pack (tree parms,
8352 int parm_idx,
8353 tree args,
8354 tree inner_args,
8355 int arg_idx,
8356 tree new_args,
8357 int* lost,
8358 tree in_decl,
8359 tsubst_flags_t complain)
8360 {
8361 tree parm = TREE_VEC_ELT (parms, parm_idx);
8362 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8363 tree packed_args;
8364 tree argument_pack;
8365 tree packed_parms = NULL_TREE;
8366
8367 if (arg_idx > nargs)
8368 arg_idx = nargs;
8369
8370 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8371 {
8372 /* When the template parameter is a non-type template parameter pack
8373 or template template parameter pack whose type or template
8374 parameters use parameter packs, we know exactly how many arguments
8375 we are looking for. Build a vector of the instantiated decls for
8376 these template parameters in PACKED_PARMS. */
8377 /* We can't use make_pack_expansion here because it would interpret a
8378 _DECL as a use rather than a declaration. */
8379 tree decl = TREE_VALUE (parm);
8380 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8381 SET_PACK_EXPANSION_PATTERN (exp, decl);
8382 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8383 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8384
8385 TREE_VEC_LENGTH (args)--;
8386 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8387 TREE_VEC_LENGTH (args)++;
8388
8389 if (packed_parms == error_mark_node)
8390 return error_mark_node;
8391
8392 /* If we're doing a partial instantiation of a member template,
8393 verify that all of the types used for the non-type
8394 template parameter pack are, in fact, valid for non-type
8395 template parameters. */
8396 if (arg_idx < nargs
8397 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8398 {
8399 int j, len = TREE_VEC_LENGTH (packed_parms);
8400 for (j = 0; j < len; ++j)
8401 {
8402 tree t = TREE_VEC_ELT (packed_parms, j);
8403 if (TREE_CODE (t) == PARM_DECL
8404 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8405 return error_mark_node;
8406 }
8407 /* We don't know how many args we have yet, just
8408 use the unconverted ones for now. */
8409 return NULL_TREE;
8410 }
8411
8412 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8413 }
8414 /* Check if we have a placeholder pack, which indicates we're
8415 in the context of a introduction list. In that case we want
8416 to match this pack to the single placeholder. */
8417 else if (arg_idx < nargs
8418 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8419 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8420 {
8421 nargs = arg_idx + 1;
8422 packed_args = make_tree_vec (1);
8423 }
8424 else
8425 packed_args = make_tree_vec (nargs - arg_idx);
8426
8427 /* Convert the remaining arguments, which will be a part of the
8428 parameter pack "parm". */
8429 int first_pack_arg = arg_idx;
8430 for (; arg_idx < nargs; ++arg_idx)
8431 {
8432 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8433 tree actual_parm = TREE_VALUE (parm);
8434 int pack_idx = arg_idx - first_pack_arg;
8435
8436 if (packed_parms)
8437 {
8438 /* Once we've packed as many args as we have types, stop. */
8439 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8440 break;
8441 else if (PACK_EXPANSION_P (arg))
8442 /* We don't know how many args we have yet, just
8443 use the unconverted ones for now. */
8444 return NULL_TREE;
8445 else
8446 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8447 }
8448
8449 if (arg == error_mark_node)
8450 {
8451 if (complain & tf_error)
8452 error ("template argument %d is invalid", arg_idx + 1);
8453 }
8454 else
8455 arg = convert_template_argument (actual_parm,
8456 arg, new_args, complain, parm_idx,
8457 in_decl);
8458 if (arg == error_mark_node)
8459 (*lost)++;
8460 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8461 }
8462
8463 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8464 && TREE_VEC_LENGTH (packed_args) > 0)
8465 {
8466 if (complain & tf_error)
8467 error ("wrong number of template arguments (%d, should be %d)",
8468 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8469 return error_mark_node;
8470 }
8471
8472 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8473 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8474 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8475 else
8476 {
8477 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8478 TREE_CONSTANT (argument_pack) = 1;
8479 }
8480
8481 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8482 if (CHECKING_P)
8483 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8484 TREE_VEC_LENGTH (packed_args));
8485 return argument_pack;
8486 }
8487
8488 /* Returns the number of pack expansions in the template argument vector
8489 ARGS. */
8490
8491 static int
8492 pack_expansion_args_count (tree args)
8493 {
8494 int i;
8495 int count = 0;
8496 if (args)
8497 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8498 {
8499 tree elt = TREE_VEC_ELT (args, i);
8500 if (elt && PACK_EXPANSION_P (elt))
8501 ++count;
8502 }
8503 return count;
8504 }
8505
8506 /* Convert all template arguments to their appropriate types, and
8507 return a vector containing the innermost resulting template
8508 arguments. If any error occurs, return error_mark_node. Error and
8509 warning messages are issued under control of COMPLAIN.
8510
8511 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8512 for arguments not specified in ARGS. Otherwise, if
8513 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8514 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8515 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8516 ARGS. */
8517
8518 static tree
8519 coerce_template_parms (tree parms,
8520 tree args,
8521 tree in_decl,
8522 tsubst_flags_t complain,
8523 bool require_all_args,
8524 bool use_default_args)
8525 {
8526 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8527 tree orig_inner_args;
8528 tree inner_args;
8529 tree new_args;
8530 tree new_inner_args;
8531
8532 /* When used as a boolean value, indicates whether this is a
8533 variadic template parameter list. Since it's an int, we can also
8534 subtract it from nparms to get the number of non-variadic
8535 parameters. */
8536 int variadic_p = 0;
8537 int variadic_args_p = 0;
8538 int post_variadic_parms = 0;
8539
8540 /* Adjustment to nparms for fixed parameter packs. */
8541 int fixed_pack_adjust = 0;
8542 int fixed_packs = 0;
8543 int missing = 0;
8544
8545 /* Likewise for parameters with default arguments. */
8546 int default_p = 0;
8547
8548 if (args == error_mark_node)
8549 return error_mark_node;
8550
8551 nparms = TREE_VEC_LENGTH (parms);
8552
8553 /* Determine if there are any parameter packs or default arguments. */
8554 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8555 {
8556 tree parm = TREE_VEC_ELT (parms, parm_idx);
8557 if (variadic_p)
8558 ++post_variadic_parms;
8559 if (template_parameter_pack_p (TREE_VALUE (parm)))
8560 ++variadic_p;
8561 if (TREE_PURPOSE (parm))
8562 ++default_p;
8563 }
8564
8565 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8566 /* If there are no parameters that follow a parameter pack, we need to
8567 expand any argument packs so that we can deduce a parameter pack from
8568 some non-packed args followed by an argument pack, as in variadic85.C.
8569 If there are such parameters, we need to leave argument packs intact
8570 so the arguments are assigned properly. This can happen when dealing
8571 with a nested class inside a partial specialization of a class
8572 template, as in variadic92.C, or when deducing a template parameter pack
8573 from a sub-declarator, as in variadic114.C. */
8574 if (!post_variadic_parms)
8575 inner_args = expand_template_argument_pack (inner_args);
8576
8577 /* Count any pack expansion args. */
8578 variadic_args_p = pack_expansion_args_count (inner_args);
8579
8580 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8581 if ((nargs - variadic_args_p > nparms && !variadic_p)
8582 || (nargs < nparms - variadic_p
8583 && require_all_args
8584 && !variadic_args_p
8585 && (!use_default_args
8586 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8587 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8588 {
8589 bad_nargs:
8590 if (complain & tf_error)
8591 {
8592 if (variadic_p || default_p)
8593 {
8594 nparms -= variadic_p + default_p;
8595 error ("wrong number of template arguments "
8596 "(%d, should be at least %d)", nargs, nparms);
8597 }
8598 else
8599 error ("wrong number of template arguments "
8600 "(%d, should be %d)", nargs, nparms);
8601
8602 if (in_decl)
8603 inform (DECL_SOURCE_LOCATION (in_decl),
8604 "provided for %qD", in_decl);
8605 }
8606
8607 return error_mark_node;
8608 }
8609 /* We can't pass a pack expansion to a non-pack parameter of an alias
8610 template (DR 1430). */
8611 else if (in_decl
8612 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8613 || concept_definition_p (in_decl))
8614 && variadic_args_p
8615 && nargs - variadic_args_p < nparms - variadic_p)
8616 {
8617 if (complain & tf_error)
8618 {
8619 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8620 {
8621 tree arg = TREE_VEC_ELT (inner_args, i);
8622 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8623
8624 if (PACK_EXPANSION_P (arg)
8625 && !template_parameter_pack_p (parm))
8626 {
8627 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8628 error_at (location_of (arg),
8629 "pack expansion argument for non-pack parameter "
8630 "%qD of alias template %qD", parm, in_decl);
8631 else
8632 error_at (location_of (arg),
8633 "pack expansion argument for non-pack parameter "
8634 "%qD of concept %qD", parm, in_decl);
8635 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8636 goto found;
8637 }
8638 }
8639 gcc_unreachable ();
8640 found:;
8641 }
8642 return error_mark_node;
8643 }
8644
8645 /* We need to evaluate the template arguments, even though this
8646 template-id may be nested within a "sizeof". */
8647 cp_evaluated ev;
8648
8649 new_inner_args = make_tree_vec (nparms);
8650 new_args = add_outermost_template_args (args, new_inner_args);
8651 int pack_adjust = 0;
8652 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8653 {
8654 tree arg;
8655 tree parm;
8656
8657 /* Get the Ith template parameter. */
8658 parm = TREE_VEC_ELT (parms, parm_idx);
8659
8660 if (parm == error_mark_node)
8661 {
8662 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8663 continue;
8664 }
8665
8666 /* Calculate the next argument. */
8667 if (arg_idx < nargs)
8668 arg = TREE_VEC_ELT (inner_args, arg_idx);
8669 else
8670 arg = NULL_TREE;
8671
8672 if (template_parameter_pack_p (TREE_VALUE (parm))
8673 && (arg || require_all_args || !(complain & tf_partial))
8674 && !(arg && ARGUMENT_PACK_P (arg)))
8675 {
8676 /* Some arguments will be placed in the
8677 template parameter pack PARM. */
8678 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8679 inner_args, arg_idx,
8680 new_args, &lost,
8681 in_decl, complain);
8682
8683 if (arg == NULL_TREE)
8684 {
8685 /* We don't know how many args we have yet, just use the
8686 unconverted (and still packed) ones for now. */
8687 new_inner_args = orig_inner_args;
8688 arg_idx = nargs;
8689 break;
8690 }
8691
8692 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8693
8694 /* Store this argument. */
8695 if (arg == error_mark_node)
8696 {
8697 lost++;
8698 /* We are done with all of the arguments. */
8699 arg_idx = nargs;
8700 break;
8701 }
8702 else
8703 {
8704 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8705 arg_idx += pack_adjust;
8706 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8707 {
8708 ++fixed_packs;
8709 fixed_pack_adjust += pack_adjust;
8710 }
8711 }
8712
8713 continue;
8714 }
8715 else if (arg)
8716 {
8717 if (PACK_EXPANSION_P (arg))
8718 {
8719 /* "If every valid specialization of a variadic template
8720 requires an empty template parameter pack, the template is
8721 ill-formed, no diagnostic required." So check that the
8722 pattern works with this parameter. */
8723 tree pattern = PACK_EXPANSION_PATTERN (arg);
8724 tree conv = convert_template_argument (TREE_VALUE (parm),
8725 pattern, new_args,
8726 complain, parm_idx,
8727 in_decl);
8728 if (conv == error_mark_node)
8729 {
8730 if (complain & tf_error)
8731 inform (input_location, "so any instantiation with a "
8732 "non-empty parameter pack would be ill-formed");
8733 ++lost;
8734 }
8735 else if (TYPE_P (conv) && !TYPE_P (pattern))
8736 /* Recover from missing typename. */
8737 TREE_VEC_ELT (inner_args, arg_idx)
8738 = make_pack_expansion (conv, complain);
8739
8740 /* We don't know how many args we have yet, just
8741 use the unconverted ones for now. */
8742 new_inner_args = inner_args;
8743 arg_idx = nargs;
8744 break;
8745 }
8746 }
8747 else if (require_all_args)
8748 {
8749 /* There must be a default arg in this case. */
8750 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8751 complain, in_decl);
8752 /* The position of the first default template argument,
8753 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8754 Record that. */
8755 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8756 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8757 arg_idx - pack_adjust);
8758 }
8759 else
8760 break;
8761
8762 if (arg == error_mark_node)
8763 {
8764 if (complain & tf_error)
8765 error ("template argument %d is invalid", arg_idx + 1);
8766 }
8767 else if (!arg)
8768 {
8769 /* This can occur if there was an error in the template
8770 parameter list itself (which we would already have
8771 reported) that we are trying to recover from, e.g., a class
8772 template with a parameter list such as
8773 template<typename..., typename> (cpp0x/variadic150.C). */
8774 ++lost;
8775
8776 /* This can also happen with a fixed parameter pack (71834). */
8777 if (arg_idx >= nargs)
8778 ++missing;
8779 }
8780 else
8781 arg = convert_template_argument (TREE_VALUE (parm),
8782 arg, new_args, complain,
8783 parm_idx, in_decl);
8784
8785 if (arg == error_mark_node)
8786 lost++;
8787
8788 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8789 }
8790
8791 if (missing || arg_idx < nargs - variadic_args_p)
8792 {
8793 /* If we had fixed parameter packs, we didn't know how many arguments we
8794 actually needed earlier; now we do. */
8795 nparms += fixed_pack_adjust;
8796 variadic_p -= fixed_packs;
8797 goto bad_nargs;
8798 }
8799
8800 if (arg_idx < nargs)
8801 {
8802 /* We had some pack expansion arguments that will only work if the packs
8803 are empty, but wait until instantiation time to complain.
8804 See variadic-ttp3.C. */
8805
8806 /* Except that we can't provide empty packs to alias templates or
8807 concepts when there are no corresponding parameters. Basically,
8808 we can get here with this:
8809
8810 template<typename T> concept C = true;
8811
8812 template<typename... Args>
8813 requires C<Args...>
8814 void f();
8815
8816 When parsing C<Args...>, we try to form a concept check of
8817 C<?, Args...>. Without the extra check for substituting an empty
8818 pack past the last parameter, we can accept the check as valid.
8819
8820 FIXME: This may be valid for alias templates (but I doubt it).
8821
8822 FIXME: The error could be better also. */
8823 if (in_decl && concept_definition_p (in_decl))
8824 {
8825 if (complain & tf_error)
8826 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
8827 "too many arguments");
8828 return error_mark_node;
8829 }
8830
8831 int len = nparms + (nargs - arg_idx);
8832 tree args = make_tree_vec (len);
8833 int i = 0;
8834 for (; i < nparms; ++i)
8835 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8836 for (; i < len; ++i, ++arg_idx)
8837 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8838 arg_idx - pack_adjust);
8839 new_inner_args = args;
8840 }
8841
8842 if (lost)
8843 {
8844 gcc_assert (!(complain & tf_error) || seen_error ());
8845 return error_mark_node;
8846 }
8847
8848 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8849 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8850 TREE_VEC_LENGTH (new_inner_args));
8851
8852 return new_inner_args;
8853 }
8854
8855 /* Convert all template arguments to their appropriate types, and
8856 return a vector containing the innermost resulting template
8857 arguments. If any error occurs, return error_mark_node. Error and
8858 warning messages are not issued.
8859
8860 Note that no function argument deduction is performed, and default
8861 arguments are used to fill in unspecified arguments. */
8862 tree
8863 coerce_template_parms (tree parms, tree args, tree in_decl)
8864 {
8865 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8866 }
8867
8868 /* Convert all template arguments to their appropriate type, and
8869 instantiate default arguments as needed. This returns a vector
8870 containing the innermost resulting template arguments, or
8871 error_mark_node if unsuccessful. */
8872 tree
8873 coerce_template_parms (tree parms, tree args, tree in_decl,
8874 tsubst_flags_t complain)
8875 {
8876 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8877 }
8878
8879 /* Like coerce_template_parms. If PARMS represents all template
8880 parameters levels, this function returns a vector of vectors
8881 representing all the resulting argument levels. Note that in this
8882 case, only the innermost arguments are coerced because the
8883 outermost ones are supposed to have been coerced already.
8884
8885 Otherwise, if PARMS represents only (the innermost) vector of
8886 parameters, this function returns a vector containing just the
8887 innermost resulting arguments. */
8888
8889 static tree
8890 coerce_innermost_template_parms (tree parms,
8891 tree args,
8892 tree in_decl,
8893 tsubst_flags_t complain,
8894 bool require_all_args,
8895 bool use_default_args)
8896 {
8897 int parms_depth = TMPL_PARMS_DEPTH (parms);
8898 int args_depth = TMPL_ARGS_DEPTH (args);
8899 tree coerced_args;
8900
8901 if (parms_depth > 1)
8902 {
8903 coerced_args = make_tree_vec (parms_depth);
8904 tree level;
8905 int cur_depth;
8906
8907 for (level = parms, cur_depth = parms_depth;
8908 parms_depth > 0 && level != NULL_TREE;
8909 level = TREE_CHAIN (level), --cur_depth)
8910 {
8911 tree l;
8912 if (cur_depth == args_depth)
8913 l = coerce_template_parms (TREE_VALUE (level),
8914 args, in_decl, complain,
8915 require_all_args,
8916 use_default_args);
8917 else
8918 l = TMPL_ARGS_LEVEL (args, cur_depth);
8919
8920 if (l == error_mark_node)
8921 return error_mark_node;
8922
8923 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8924 }
8925 }
8926 else
8927 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8928 args, in_decl, complain,
8929 require_all_args,
8930 use_default_args);
8931 return coerced_args;
8932 }
8933
8934 /* Returns 1 if template args OT and NT are equivalent. */
8935
8936 int
8937 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8938 {
8939 if (nt == ot)
8940 return 1;
8941 if (nt == NULL_TREE || ot == NULL_TREE)
8942 return false;
8943 if (nt == any_targ_node || ot == any_targ_node)
8944 return true;
8945
8946 if (TREE_CODE (nt) == TREE_VEC)
8947 /* For member templates */
8948 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8949 else if (PACK_EXPANSION_P (ot))
8950 return (PACK_EXPANSION_P (nt)
8951 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8952 PACK_EXPANSION_PATTERN (nt))
8953 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8954 PACK_EXPANSION_EXTRA_ARGS (nt)));
8955 else if (ARGUMENT_PACK_P (ot))
8956 {
8957 int i, len;
8958 tree opack, npack;
8959
8960 if (!ARGUMENT_PACK_P (nt))
8961 return 0;
8962
8963 opack = ARGUMENT_PACK_ARGS (ot);
8964 npack = ARGUMENT_PACK_ARGS (nt);
8965 len = TREE_VEC_LENGTH (opack);
8966 if (TREE_VEC_LENGTH (npack) != len)
8967 return 0;
8968 for (i = 0; i < len; ++i)
8969 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8970 TREE_VEC_ELT (npack, i)))
8971 return 0;
8972 return 1;
8973 }
8974 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8975 gcc_unreachable ();
8976 else if (TYPE_P (nt))
8977 {
8978 if (!TYPE_P (ot))
8979 return false;
8980 /* Don't treat an alias template specialization with dependent
8981 arguments as equivalent to its underlying type when used as a
8982 template argument; we need them to be distinct so that we
8983 substitute into the specialization arguments at instantiation
8984 time. And aliases can't be equivalent without being ==, so
8985 we don't need to look any deeper.
8986
8987 During partial ordering, however, we need to treat them normally so
8988 that we can order uses of the same alias with different
8989 cv-qualification (79960). */
8990 if (!partial_order
8991 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8992 return false;
8993 else
8994 return same_type_p (ot, nt);
8995 }
8996 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8997 return 0;
8998 else
8999 {
9000 /* Try to treat a template non-type argument that has been converted
9001 to the parameter type as equivalent to one that hasn't yet. */
9002 for (enum tree_code code1 = TREE_CODE (ot);
9003 CONVERT_EXPR_CODE_P (code1)
9004 || code1 == NON_LVALUE_EXPR;
9005 code1 = TREE_CODE (ot))
9006 ot = TREE_OPERAND (ot, 0);
9007 for (enum tree_code code2 = TREE_CODE (nt);
9008 CONVERT_EXPR_CODE_P (code2)
9009 || code2 == NON_LVALUE_EXPR;
9010 code2 = TREE_CODE (nt))
9011 nt = TREE_OPERAND (nt, 0);
9012
9013 return cp_tree_equal (ot, nt);
9014 }
9015 }
9016
9017 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9018 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9019 NEWARG_PTR with the offending arguments if they are non-NULL. */
9020
9021 int
9022 comp_template_args (tree oldargs, tree newargs,
9023 tree *oldarg_ptr, tree *newarg_ptr,
9024 bool partial_order)
9025 {
9026 int i;
9027
9028 if (oldargs == newargs)
9029 return 1;
9030
9031 if (!oldargs || !newargs)
9032 return 0;
9033
9034 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9035 return 0;
9036
9037 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9038 {
9039 tree nt = TREE_VEC_ELT (newargs, i);
9040 tree ot = TREE_VEC_ELT (oldargs, i);
9041
9042 if (! template_args_equal (ot, nt, partial_order))
9043 {
9044 if (oldarg_ptr != NULL)
9045 *oldarg_ptr = ot;
9046 if (newarg_ptr != NULL)
9047 *newarg_ptr = nt;
9048 return 0;
9049 }
9050 }
9051 return 1;
9052 }
9053
9054 inline bool
9055 comp_template_args_porder (tree oargs, tree nargs)
9056 {
9057 return comp_template_args (oargs, nargs, NULL, NULL, true);
9058 }
9059
9060 /* Implement a freelist interface for objects of type T.
9061
9062 Head is a separate object, rather than a regular member, so that we
9063 can define it as a GTY deletable pointer, which is highly
9064 desirable. A data member could be declared that way, but then the
9065 containing object would implicitly get GTY((user)), which would
9066 prevent us from instantiating freelists as global objects.
9067 Although this way we can create freelist global objects, they're
9068 such thin wrappers that instantiating temporaries at every use
9069 loses nothing and saves permanent storage for the freelist object.
9070
9071 Member functions next, anew, poison and reinit have default
9072 implementations that work for most of the types we're interested
9073 in, but if they don't work for some type, they should be explicitly
9074 specialized. See the comments before them for requirements, and
9075 the example specializations for the tree_list_freelist. */
9076 template <typename T>
9077 class freelist
9078 {
9079 /* Return the next object in a chain. We could just do type
9080 punning, but if we access the object with its underlying type, we
9081 avoid strict-aliasing trouble. This needs only work between
9082 poison and reinit. */
9083 static T *&next (T *obj) { return obj->next; }
9084
9085 /* Return a newly allocated, uninitialized or minimally-initialized
9086 object of type T. Any initialization performed by anew should
9087 either remain across the life of the object and the execution of
9088 poison, or be redone by reinit. */
9089 static T *anew () { return ggc_alloc<T> (); }
9090
9091 /* Optionally scribble all over the bits holding the object, so that
9092 they become (mostly?) uninitialized memory. This is called while
9093 preparing to make the object part of the free list. */
9094 static void poison (T *obj) {
9095 T *p ATTRIBUTE_UNUSED = obj;
9096 T **q ATTRIBUTE_UNUSED = &next (obj);
9097
9098 #ifdef ENABLE_GC_CHECKING
9099 /* Poison the data, to indicate the data is garbage. */
9100 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9101 memset (p, 0xa5, sizeof (*p));
9102 #endif
9103 /* Let valgrind know the object is free. */
9104 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9105
9106 /* Let valgrind know the next portion of the object is available,
9107 but uninitialized. */
9108 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9109 }
9110
9111 /* Bring an object that underwent at least one lifecycle after anew
9112 and before the most recent free and poison, back to a usable
9113 state, reinitializing whatever is needed for it to be
9114 functionally equivalent to an object just allocated and returned
9115 by anew. This may poison or clear the next field, used by
9116 freelist housekeeping after poison was called. */
9117 static void reinit (T *obj) {
9118 T **q ATTRIBUTE_UNUSED = &next (obj);
9119
9120 #ifdef ENABLE_GC_CHECKING
9121 memset (q, 0xa5, sizeof (*q));
9122 #endif
9123 /* Let valgrind know the entire object is available, but
9124 uninitialized. */
9125 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9126 }
9127
9128 /* Reference a GTY-deletable pointer that points to the first object
9129 in the free list proper. */
9130 T *&head;
9131 public:
9132 /* Construct a freelist object chaining objects off of HEAD. */
9133 freelist (T *&head) : head(head) {}
9134
9135 /* Add OBJ to the free object list. The former head becomes OBJ's
9136 successor. */
9137 void free (T *obj)
9138 {
9139 poison (obj);
9140 next (obj) = head;
9141 head = obj;
9142 }
9143
9144 /* Take an object from the free list, if one is available, or
9145 allocate a new one. Objects taken from the free list should be
9146 regarded as filled with garbage, except for bits that are
9147 configured to be preserved across free and alloc. */
9148 T *alloc ()
9149 {
9150 if (head)
9151 {
9152 T *obj = head;
9153 head = next (head);
9154 reinit (obj);
9155 return obj;
9156 }
9157 else
9158 return anew ();
9159 }
9160 };
9161
9162 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9163 want to allocate a TREE_LIST using the usual interface, and ensure
9164 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9165 build_tree_list logic in reinit, so this could go out of sync. */
9166 template <>
9167 inline tree &
9168 freelist<tree_node>::next (tree obj)
9169 {
9170 return TREE_CHAIN (obj);
9171 }
9172 template <>
9173 inline tree
9174 freelist<tree_node>::anew ()
9175 {
9176 return build_tree_list (NULL, NULL);
9177 }
9178 template <>
9179 inline void
9180 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9181 {
9182 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9183 tree p ATTRIBUTE_UNUSED = obj;
9184 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9185 tree *q ATTRIBUTE_UNUSED = &next (obj);
9186
9187 #ifdef ENABLE_GC_CHECKING
9188 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9189
9190 /* Poison the data, to indicate the data is garbage. */
9191 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9192 memset (p, 0xa5, size);
9193 #endif
9194 /* Let valgrind know the object is free. */
9195 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9196 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9197 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9198 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9199
9200 #ifdef ENABLE_GC_CHECKING
9201 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9202 /* Keep TREE_CHAIN functional. */
9203 TREE_SET_CODE (obj, TREE_LIST);
9204 #else
9205 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9206 #endif
9207 }
9208 template <>
9209 inline void
9210 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9211 {
9212 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9213
9214 #ifdef ENABLE_GC_CHECKING
9215 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9216 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9217 memset (obj, 0, sizeof (tree_list));
9218 #endif
9219
9220 /* Let valgrind know the entire object is available, but
9221 uninitialized. */
9222 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9223
9224 #ifdef ENABLE_GC_CHECKING
9225 TREE_SET_CODE (obj, TREE_LIST);
9226 #else
9227 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9228 #endif
9229 }
9230
9231 /* Point to the first object in the TREE_LIST freelist. */
9232 static GTY((deletable)) tree tree_list_freelist_head;
9233 /* Return the/an actual TREE_LIST freelist. */
9234 static inline freelist<tree_node>
9235 tree_list_freelist ()
9236 {
9237 return tree_list_freelist_head;
9238 }
9239
9240 /* Point to the first object in the tinst_level freelist. */
9241 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9242 /* Return the/an actual tinst_level freelist. */
9243 static inline freelist<tinst_level>
9244 tinst_level_freelist ()
9245 {
9246 return tinst_level_freelist_head;
9247 }
9248
9249 /* Point to the first object in the pending_template freelist. */
9250 static GTY((deletable)) pending_template *pending_template_freelist_head;
9251 /* Return the/an actual pending_template freelist. */
9252 static inline freelist<pending_template>
9253 pending_template_freelist ()
9254 {
9255 return pending_template_freelist_head;
9256 }
9257
9258 /* Build the TREE_LIST object out of a split list, store it
9259 permanently, and return it. */
9260 tree
9261 tinst_level::to_list ()
9262 {
9263 gcc_assert (split_list_p ());
9264 tree ret = tree_list_freelist ().alloc ();
9265 TREE_PURPOSE (ret) = tldcl;
9266 TREE_VALUE (ret) = targs;
9267 tldcl = ret;
9268 targs = NULL;
9269 gcc_assert (tree_list_p ());
9270 return ret;
9271 }
9272
9273 const unsigned short tinst_level::refcount_infinity;
9274
9275 /* Increment OBJ's refcount unless it is already infinite. */
9276 static tinst_level *
9277 inc_refcount_use (tinst_level *obj)
9278 {
9279 if (obj && obj->refcount != tinst_level::refcount_infinity)
9280 ++obj->refcount;
9281 return obj;
9282 }
9283
9284 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9285 void
9286 tinst_level::free (tinst_level *obj)
9287 {
9288 if (obj->tree_list_p ())
9289 tree_list_freelist ().free (obj->get_node ());
9290 tinst_level_freelist ().free (obj);
9291 }
9292
9293 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9294 OBJ's DECL and OBJ, and start over with the tinst_level object that
9295 used to be referenced by OBJ's NEXT. */
9296 static void
9297 dec_refcount_use (tinst_level *obj)
9298 {
9299 while (obj
9300 && obj->refcount != tinst_level::refcount_infinity
9301 && !--obj->refcount)
9302 {
9303 tinst_level *next = obj->next;
9304 tinst_level::free (obj);
9305 obj = next;
9306 }
9307 }
9308
9309 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9310 and of the former PTR. Omitting the second argument is equivalent
9311 to passing (T*)NULL; this is allowed because passing the
9312 zero-valued integral constant NULL confuses type deduction and/or
9313 overload resolution. */
9314 template <typename T>
9315 static void
9316 set_refcount_ptr (T *& ptr, T *obj = NULL)
9317 {
9318 T *save = ptr;
9319 ptr = inc_refcount_use (obj);
9320 dec_refcount_use (save);
9321 }
9322
9323 static void
9324 add_pending_template (tree d)
9325 {
9326 tree ti = (TYPE_P (d)
9327 ? CLASSTYPE_TEMPLATE_INFO (d)
9328 : DECL_TEMPLATE_INFO (d));
9329 struct pending_template *pt;
9330 int level;
9331
9332 if (TI_PENDING_TEMPLATE_FLAG (ti))
9333 return;
9334
9335 /* We are called both from instantiate_decl, where we've already had a
9336 tinst_level pushed, and instantiate_template, where we haven't.
9337 Compensate. */
9338 gcc_assert (TREE_CODE (d) != TREE_LIST);
9339 level = !current_tinst_level
9340 || current_tinst_level->maybe_get_node () != d;
9341
9342 if (level)
9343 push_tinst_level (d);
9344
9345 pt = pending_template_freelist ().alloc ();
9346 pt->next = NULL;
9347 pt->tinst = NULL;
9348 set_refcount_ptr (pt->tinst, current_tinst_level);
9349 if (last_pending_template)
9350 last_pending_template->next = pt;
9351 else
9352 pending_templates = pt;
9353
9354 last_pending_template = pt;
9355
9356 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9357
9358 if (level)
9359 pop_tinst_level ();
9360 }
9361
9362
9363 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9364 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9365 documentation for TEMPLATE_ID_EXPR. */
9366
9367 tree
9368 lookup_template_function (tree fns, tree arglist)
9369 {
9370 if (fns == error_mark_node || arglist == error_mark_node)
9371 return error_mark_node;
9372
9373 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9374
9375 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9376 {
9377 error ("%q#D is not a function template", fns);
9378 return error_mark_node;
9379 }
9380
9381 if (BASELINK_P (fns))
9382 {
9383 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9384 unknown_type_node,
9385 BASELINK_FUNCTIONS (fns),
9386 arglist);
9387 return fns;
9388 }
9389
9390 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9391 }
9392
9393 /* Within the scope of a template class S<T>, the name S gets bound
9394 (in build_self_reference) to a TYPE_DECL for the class, not a
9395 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9396 or one of its enclosing classes, and that type is a template,
9397 return the associated TEMPLATE_DECL. Otherwise, the original
9398 DECL is returned.
9399
9400 Also handle the case when DECL is a TREE_LIST of ambiguous
9401 injected-class-names from different bases. */
9402
9403 tree
9404 maybe_get_template_decl_from_type_decl (tree decl)
9405 {
9406 if (decl == NULL_TREE)
9407 return decl;
9408
9409 /* DR 176: A lookup that finds an injected-class-name (10.2
9410 [class.member.lookup]) can result in an ambiguity in certain cases
9411 (for example, if it is found in more than one base class). If all of
9412 the injected-class-names that are found refer to specializations of
9413 the same class template, and if the name is followed by a
9414 template-argument-list, the reference refers to the class template
9415 itself and not a specialization thereof, and is not ambiguous. */
9416 if (TREE_CODE (decl) == TREE_LIST)
9417 {
9418 tree t, tmpl = NULL_TREE;
9419 for (t = decl; t; t = TREE_CHAIN (t))
9420 {
9421 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9422 if (!tmpl)
9423 tmpl = elt;
9424 else if (tmpl != elt)
9425 break;
9426 }
9427 if (tmpl && t == NULL_TREE)
9428 return tmpl;
9429 else
9430 return decl;
9431 }
9432
9433 return (decl != NULL_TREE
9434 && DECL_SELF_REFERENCE_P (decl)
9435 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9436 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9437 }
9438
9439 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9440 parameters, find the desired type.
9441
9442 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9443
9444 IN_DECL, if non-NULL, is the template declaration we are trying to
9445 instantiate.
9446
9447 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9448 the class we are looking up.
9449
9450 Issue error and warning messages under control of COMPLAIN.
9451
9452 If the template class is really a local class in a template
9453 function, then the FUNCTION_CONTEXT is the function in which it is
9454 being instantiated.
9455
9456 ??? Note that this function is currently called *twice* for each
9457 template-id: the first time from the parser, while creating the
9458 incomplete type (finish_template_type), and the second type during the
9459 real instantiation (instantiate_template_class). This is surely something
9460 that we want to avoid. It also causes some problems with argument
9461 coercion (see convert_nontype_argument for more information on this). */
9462
9463 static tree
9464 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9465 int entering_scope, tsubst_flags_t complain)
9466 {
9467 tree templ = NULL_TREE, parmlist;
9468 tree t;
9469 spec_entry **slot;
9470 spec_entry *entry;
9471 spec_entry elt;
9472 hashval_t hash;
9473
9474 if (identifier_p (d1))
9475 {
9476 tree value = innermost_non_namespace_value (d1);
9477 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9478 templ = value;
9479 else
9480 {
9481 if (context)
9482 push_decl_namespace (context);
9483 templ = lookup_name (d1);
9484 templ = maybe_get_template_decl_from_type_decl (templ);
9485 if (context)
9486 pop_decl_namespace ();
9487 }
9488 if (templ)
9489 context = DECL_CONTEXT (templ);
9490 }
9491 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9492 {
9493 tree type = TREE_TYPE (d1);
9494
9495 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9496 an implicit typename for the second A. Deal with it. */
9497 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9498 type = TREE_TYPE (type);
9499
9500 if (CLASSTYPE_TEMPLATE_INFO (type))
9501 {
9502 templ = CLASSTYPE_TI_TEMPLATE (type);
9503 d1 = DECL_NAME (templ);
9504 }
9505 }
9506 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9507 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9508 {
9509 templ = TYPE_TI_TEMPLATE (d1);
9510 d1 = DECL_NAME (templ);
9511 }
9512 else if (DECL_TYPE_TEMPLATE_P (d1))
9513 {
9514 templ = d1;
9515 d1 = DECL_NAME (templ);
9516 context = DECL_CONTEXT (templ);
9517 }
9518 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9519 {
9520 templ = d1;
9521 d1 = DECL_NAME (templ);
9522 }
9523
9524 /* Issue an error message if we didn't find a template. */
9525 if (! templ)
9526 {
9527 if (complain & tf_error)
9528 error ("%qT is not a template", d1);
9529 return error_mark_node;
9530 }
9531
9532 if (TREE_CODE (templ) != TEMPLATE_DECL
9533 /* Make sure it's a user visible template, if it was named by
9534 the user. */
9535 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9536 && !PRIMARY_TEMPLATE_P (templ)))
9537 {
9538 if (complain & tf_error)
9539 {
9540 error ("non-template type %qT used as a template", d1);
9541 if (in_decl)
9542 error ("for template declaration %q+D", in_decl);
9543 }
9544 return error_mark_node;
9545 }
9546
9547 complain &= ~tf_user;
9548
9549 /* An alias that just changes the name of a template is equivalent to the
9550 other template, so if any of the arguments are pack expansions, strip
9551 the alias to avoid problems with a pack expansion passed to a non-pack
9552 alias template parameter (DR 1430). */
9553 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9554 templ = get_underlying_template (templ);
9555
9556 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9557 {
9558 tree parm;
9559 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9560 if (arglist2 == error_mark_node
9561 || (!uses_template_parms (arglist2)
9562 && check_instantiated_args (templ, arglist2, complain)))
9563 return error_mark_node;
9564
9565 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9566 return parm;
9567 }
9568 else
9569 {
9570 tree template_type = TREE_TYPE (templ);
9571 tree gen_tmpl;
9572 tree type_decl;
9573 tree found = NULL_TREE;
9574 int arg_depth;
9575 int parm_depth;
9576 int is_dependent_type;
9577 int use_partial_inst_tmpl = false;
9578
9579 if (template_type == error_mark_node)
9580 /* An error occurred while building the template TEMPL, and a
9581 diagnostic has most certainly been emitted for that
9582 already. Let's propagate that error. */
9583 return error_mark_node;
9584
9585 gen_tmpl = most_general_template (templ);
9586 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9587 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9588 arg_depth = TMPL_ARGS_DEPTH (arglist);
9589
9590 if (arg_depth == 1 && parm_depth > 1)
9591 {
9592 /* We've been given an incomplete set of template arguments.
9593 For example, given:
9594
9595 template <class T> struct S1 {
9596 template <class U> struct S2 {};
9597 template <class U> struct S2<U*> {};
9598 };
9599
9600 we will be called with an ARGLIST of `U*', but the
9601 TEMPLATE will be `template <class T> template
9602 <class U> struct S1<T>::S2'. We must fill in the missing
9603 arguments. */
9604 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9605 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9606 arg_depth = TMPL_ARGS_DEPTH (arglist);
9607 }
9608
9609 /* Now we should have enough arguments. */
9610 gcc_assert (parm_depth == arg_depth);
9611
9612 /* From here on, we're only interested in the most general
9613 template. */
9614
9615 /* Calculate the BOUND_ARGS. These will be the args that are
9616 actually tsubst'd into the definition to create the
9617 instantiation. */
9618 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9619 complain,
9620 /*require_all_args=*/true,
9621 /*use_default_args=*/true);
9622
9623 if (arglist == error_mark_node)
9624 /* We were unable to bind the arguments. */
9625 return error_mark_node;
9626
9627 /* In the scope of a template class, explicit references to the
9628 template class refer to the type of the template, not any
9629 instantiation of it. For example, in:
9630
9631 template <class T> class C { void f(C<T>); }
9632
9633 the `C<T>' is just the same as `C'. Outside of the
9634 class, however, such a reference is an instantiation. */
9635 if (entering_scope
9636 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9637 || currently_open_class (template_type))
9638 {
9639 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9640
9641 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9642 return template_type;
9643 }
9644
9645 /* If we already have this specialization, return it. */
9646 elt.tmpl = gen_tmpl;
9647 elt.args = arglist;
9648 elt.spec = NULL_TREE;
9649 hash = spec_hasher::hash (&elt);
9650 entry = type_specializations->find_with_hash (&elt, hash);
9651
9652 if (entry)
9653 return entry->spec;
9654
9655 /* If the the template's constraints are not satisfied,
9656 then we cannot form a valid type.
9657
9658 Note that the check is deferred until after the hash
9659 lookup. This prevents redundant checks on previously
9660 instantiated specializations. */
9661 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9662 {
9663 if (complain & tf_error)
9664 {
9665 auto_diagnostic_group d;
9666 error ("template constraint failure for %qD", gen_tmpl);
9667 diagnose_constraints (input_location, gen_tmpl, arglist);
9668 }
9669 return error_mark_node;
9670 }
9671
9672 is_dependent_type = uses_template_parms (arglist);
9673
9674 /* If the deduced arguments are invalid, then the binding
9675 failed. */
9676 if (!is_dependent_type
9677 && check_instantiated_args (gen_tmpl,
9678 INNERMOST_TEMPLATE_ARGS (arglist),
9679 complain))
9680 return error_mark_node;
9681
9682 if (!is_dependent_type
9683 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9684 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9685 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9686 {
9687 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9688 DECL_NAME (gen_tmpl),
9689 /*tag_scope=*/ts_global);
9690 return found;
9691 }
9692
9693 context = DECL_CONTEXT (gen_tmpl);
9694 if (context && TYPE_P (context))
9695 {
9696 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9697 context = complete_type (context);
9698 }
9699 else
9700 context = tsubst (context, arglist, complain, in_decl);
9701
9702 if (context == error_mark_node)
9703 return error_mark_node;
9704
9705 if (!context)
9706 context = global_namespace;
9707
9708 /* Create the type. */
9709 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9710 {
9711 /* The user referred to a specialization of an alias
9712 template represented by GEN_TMPL.
9713
9714 [temp.alias]/2 says:
9715
9716 When a template-id refers to the specialization of an
9717 alias template, it is equivalent to the associated
9718 type obtained by substitution of its
9719 template-arguments for the template-parameters in the
9720 type-id of the alias template. */
9721
9722 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9723 /* Note that the call above (by indirectly calling
9724 register_specialization in tsubst_decl) registers the
9725 TYPE_DECL representing the specialization of the alias
9726 template. So next time someone substitutes ARGLIST for
9727 the template parms into the alias template (GEN_TMPL),
9728 she'll get that TYPE_DECL back. */
9729
9730 if (t == error_mark_node)
9731 return t;
9732 }
9733 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9734 {
9735 if (!is_dependent_type)
9736 {
9737 set_current_access_from_decl (TYPE_NAME (template_type));
9738 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9739 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9740 arglist, complain, in_decl),
9741 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9742 arglist, complain, in_decl),
9743 SCOPED_ENUM_P (template_type), NULL);
9744
9745 if (t == error_mark_node)
9746 return t;
9747 }
9748 else
9749 {
9750 /* We don't want to call start_enum for this type, since
9751 the values for the enumeration constants may involve
9752 template parameters. And, no one should be interested
9753 in the enumeration constants for such a type. */
9754 t = cxx_make_type (ENUMERAL_TYPE);
9755 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9756 }
9757 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9758 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9759 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9760 }
9761 else if (CLASS_TYPE_P (template_type))
9762 {
9763 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9764 instantiated here. */
9765 gcc_assert (!LAMBDA_TYPE_P (template_type));
9766
9767 t = make_class_type (TREE_CODE (template_type));
9768 CLASSTYPE_DECLARED_CLASS (t)
9769 = CLASSTYPE_DECLARED_CLASS (template_type);
9770 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9771
9772 /* A local class. Make sure the decl gets registered properly. */
9773 if (context == current_function_decl)
9774 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9775 == error_mark_node)
9776 return error_mark_node;
9777
9778 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9779 /* This instantiation is another name for the primary
9780 template type. Set the TYPE_CANONICAL field
9781 appropriately. */
9782 TYPE_CANONICAL (t) = template_type;
9783 else if (any_template_arguments_need_structural_equality_p (arglist))
9784 /* Some of the template arguments require structural
9785 equality testing, so this template class requires
9786 structural equality testing. */
9787 SET_TYPE_STRUCTURAL_EQUALITY (t);
9788 }
9789 else
9790 gcc_unreachable ();
9791
9792 /* If we called start_enum or pushtag above, this information
9793 will already be set up. */
9794 if (!TYPE_NAME (t))
9795 {
9796 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9797
9798 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9799 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9800 DECL_SOURCE_LOCATION (type_decl)
9801 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9802 }
9803 else
9804 type_decl = TYPE_NAME (t);
9805
9806 if (CLASS_TYPE_P (template_type))
9807 {
9808 TREE_PRIVATE (type_decl)
9809 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9810 TREE_PROTECTED (type_decl)
9811 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9812 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9813 {
9814 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9815 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9816 }
9817 }
9818
9819 if (OVERLOAD_TYPE_P (t)
9820 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9821 {
9822 static const char *tags[] = {"abi_tag", "may_alias"};
9823
9824 for (unsigned ix = 0; ix != 2; ix++)
9825 {
9826 tree attributes
9827 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9828
9829 if (attributes)
9830 TYPE_ATTRIBUTES (t)
9831 = tree_cons (TREE_PURPOSE (attributes),
9832 TREE_VALUE (attributes),
9833 TYPE_ATTRIBUTES (t));
9834 }
9835 }
9836
9837 /* Let's consider the explicit specialization of a member
9838 of a class template specialization that is implicitly instantiated,
9839 e.g.:
9840 template<class T>
9841 struct S
9842 {
9843 template<class U> struct M {}; //#0
9844 };
9845
9846 template<>
9847 template<>
9848 struct S<int>::M<char> //#1
9849 {
9850 int i;
9851 };
9852 [temp.expl.spec]/4 says this is valid.
9853
9854 In this case, when we write:
9855 S<int>::M<char> m;
9856
9857 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9858 the one of #0.
9859
9860 When we encounter #1, we want to store the partial instantiation
9861 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9862
9863 For all cases other than this "explicit specialization of member of a
9864 class template", we just want to store the most general template into
9865 the CLASSTYPE_TI_TEMPLATE of M.
9866
9867 This case of "explicit specialization of member of a class template"
9868 only happens when:
9869 1/ the enclosing class is an instantiation of, and therefore not
9870 the same as, the context of the most general template, and
9871 2/ we aren't looking at the partial instantiation itself, i.e.
9872 the innermost arguments are not the same as the innermost parms of
9873 the most general template.
9874
9875 So it's only when 1/ and 2/ happens that we want to use the partial
9876 instantiation of the member template in lieu of its most general
9877 template. */
9878
9879 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9880 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9881 /* the enclosing class must be an instantiation... */
9882 && CLASS_TYPE_P (context)
9883 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9884 {
9885 TREE_VEC_LENGTH (arglist)--;
9886 ++processing_template_decl;
9887 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9888 tree partial_inst_args =
9889 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9890 arglist, complain, NULL_TREE);
9891 --processing_template_decl;
9892 TREE_VEC_LENGTH (arglist)++;
9893 if (partial_inst_args == error_mark_node)
9894 return error_mark_node;
9895 use_partial_inst_tmpl =
9896 /*...and we must not be looking at the partial instantiation
9897 itself. */
9898 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9899 partial_inst_args);
9900 }
9901
9902 if (!use_partial_inst_tmpl)
9903 /* This case is easy; there are no member templates involved. */
9904 found = gen_tmpl;
9905 else
9906 {
9907 /* This is a full instantiation of a member template. Find
9908 the partial instantiation of which this is an instance. */
9909
9910 /* Temporarily reduce by one the number of levels in the ARGLIST
9911 so as to avoid comparing the last set of arguments. */
9912 TREE_VEC_LENGTH (arglist)--;
9913 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9914 TREE_VEC_LENGTH (arglist)++;
9915 /* FOUND is either a proper class type, or an alias
9916 template specialization. In the later case, it's a
9917 TYPE_DECL, resulting from the substituting of arguments
9918 for parameters in the TYPE_DECL of the alias template
9919 done earlier. So be careful while getting the template
9920 of FOUND. */
9921 found = (TREE_CODE (found) == TEMPLATE_DECL
9922 ? found
9923 : (TREE_CODE (found) == TYPE_DECL
9924 ? DECL_TI_TEMPLATE (found)
9925 : CLASSTYPE_TI_TEMPLATE (found)));
9926
9927 if (DECL_CLASS_TEMPLATE_P (found)
9928 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9929 {
9930 /* If this partial instantiation is specialized, we want to
9931 use it for hash table lookup. */
9932 elt.tmpl = found;
9933 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9934 hash = spec_hasher::hash (&elt);
9935 }
9936 }
9937
9938 // Build template info for the new specialization.
9939 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9940
9941 elt.spec = t;
9942 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9943 gcc_checking_assert (*slot == NULL);
9944 entry = ggc_alloc<spec_entry> ();
9945 *entry = elt;
9946 *slot = entry;
9947
9948 /* Note this use of the partial instantiation so we can check it
9949 later in maybe_process_partial_specialization. */
9950 DECL_TEMPLATE_INSTANTIATIONS (found)
9951 = tree_cons (arglist, t,
9952 DECL_TEMPLATE_INSTANTIATIONS (found));
9953
9954 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9955 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9956 /* Now that the type has been registered on the instantiations
9957 list, we set up the enumerators. Because the enumeration
9958 constants may involve the enumeration type itself, we make
9959 sure to register the type first, and then create the
9960 constants. That way, doing tsubst_expr for the enumeration
9961 constants won't result in recursive calls here; we'll find
9962 the instantiation and exit above. */
9963 tsubst_enum (template_type, t, arglist);
9964
9965 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9966 /* If the type makes use of template parameters, the
9967 code that generates debugging information will crash. */
9968 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9969
9970 /* Possibly limit visibility based on template args. */
9971 TREE_PUBLIC (type_decl) = 1;
9972 determine_visibility (type_decl);
9973
9974 inherit_targ_abi_tags (t);
9975
9976 return t;
9977 }
9978 }
9979
9980 /* Wrapper for lookup_template_class_1. */
9981
9982 tree
9983 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9984 int entering_scope, tsubst_flags_t complain)
9985 {
9986 tree ret;
9987 timevar_push (TV_TEMPLATE_INST);
9988 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9989 entering_scope, complain);
9990 timevar_pop (TV_TEMPLATE_INST);
9991 return ret;
9992 }
9993
9994 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9995
9996 tree
9997 lookup_template_variable (tree templ, tree arglist)
9998 {
9999 if (flag_concepts && variable_concept_p (templ))
10000 return build_concept_check (templ, arglist, tf_none);
10001
10002 /* The type of the expression is NULL_TREE since the template-id could refer
10003 to an explicit or partial specialization. */
10004 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10005 }
10006
10007 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10008
10009 tree
10010 finish_template_variable (tree var, tsubst_flags_t complain)
10011 {
10012 tree templ = TREE_OPERAND (var, 0);
10013 tree arglist = TREE_OPERAND (var, 1);
10014
10015 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10016 arglist = add_outermost_template_args (tmpl_args, arglist);
10017
10018 templ = most_general_template (templ);
10019 tree parms = DECL_TEMPLATE_PARMS (templ);
10020 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10021 /*req_all*/true,
10022 /*use_default*/true);
10023
10024 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10025 {
10026 if (complain & tf_error)
10027 {
10028 auto_diagnostic_group d;
10029 error ("use of invalid variable template %qE", var);
10030 diagnose_constraints (location_of (var), templ, arglist);
10031 }
10032 return error_mark_node;
10033 }
10034
10035 return instantiate_template (templ, arglist, complain);
10036 }
10037
10038 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10039 TARGS template args, and instantiate it if it's not dependent. */
10040
10041 tree
10042 lookup_and_finish_template_variable (tree templ, tree targs,
10043 tsubst_flags_t complain)
10044 {
10045 templ = lookup_template_variable (templ, targs);
10046 if (!any_dependent_template_arguments_p (targs))
10047 {
10048 templ = finish_template_variable (templ, complain);
10049 mark_used (templ);
10050 }
10051
10052 return convert_from_reference (templ);
10053 }
10054
10055 \f
10056 struct pair_fn_data
10057 {
10058 tree_fn_t fn;
10059 tree_fn_t any_fn;
10060 void *data;
10061 /* True when we should also visit template parameters that occur in
10062 non-deduced contexts. */
10063 bool include_nondeduced_p;
10064 hash_set<tree> *visited;
10065 };
10066
10067 /* Called from for_each_template_parm via walk_tree. */
10068
10069 static tree
10070 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10071 {
10072 tree t = *tp;
10073 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10074 tree_fn_t fn = pfd->fn;
10075 void *data = pfd->data;
10076 tree result = NULL_TREE;
10077
10078 #define WALK_SUBTREE(NODE) \
10079 do \
10080 { \
10081 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10082 pfd->include_nondeduced_p, \
10083 pfd->any_fn); \
10084 if (result) goto out; \
10085 } \
10086 while (0)
10087
10088 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10089 return t;
10090
10091 if (TYPE_P (t)
10092 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10093 WALK_SUBTREE (TYPE_CONTEXT (t));
10094
10095 switch (TREE_CODE (t))
10096 {
10097 case RECORD_TYPE:
10098 if (TYPE_PTRMEMFUNC_P (t))
10099 break;
10100 /* Fall through. */
10101
10102 case UNION_TYPE:
10103 case ENUMERAL_TYPE:
10104 if (!TYPE_TEMPLATE_INFO (t))
10105 *walk_subtrees = 0;
10106 else
10107 WALK_SUBTREE (TYPE_TI_ARGS (t));
10108 break;
10109
10110 case INTEGER_TYPE:
10111 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10112 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10113 break;
10114
10115 case METHOD_TYPE:
10116 /* Since we're not going to walk subtrees, we have to do this
10117 explicitly here. */
10118 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10119 /* Fall through. */
10120
10121 case FUNCTION_TYPE:
10122 /* Check the return type. */
10123 WALK_SUBTREE (TREE_TYPE (t));
10124
10125 /* Check the parameter types. Since default arguments are not
10126 instantiated until they are needed, the TYPE_ARG_TYPES may
10127 contain expressions that involve template parameters. But,
10128 no-one should be looking at them yet. And, once they're
10129 instantiated, they don't contain template parameters, so
10130 there's no point in looking at them then, either. */
10131 {
10132 tree parm;
10133
10134 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10135 WALK_SUBTREE (TREE_VALUE (parm));
10136
10137 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10138 want walk_tree walking into them itself. */
10139 *walk_subtrees = 0;
10140 }
10141
10142 if (flag_noexcept_type)
10143 {
10144 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10145 if (spec)
10146 WALK_SUBTREE (TREE_PURPOSE (spec));
10147 }
10148 break;
10149
10150 case TYPEOF_TYPE:
10151 case DECLTYPE_TYPE:
10152 case UNDERLYING_TYPE:
10153 if (pfd->include_nondeduced_p
10154 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10155 pfd->visited,
10156 pfd->include_nondeduced_p,
10157 pfd->any_fn))
10158 return error_mark_node;
10159 *walk_subtrees = false;
10160 break;
10161
10162 case FUNCTION_DECL:
10163 case VAR_DECL:
10164 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10165 WALK_SUBTREE (DECL_TI_ARGS (t));
10166 /* Fall through. */
10167
10168 case PARM_DECL:
10169 case CONST_DECL:
10170 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10171 WALK_SUBTREE (DECL_INITIAL (t));
10172 if (DECL_CONTEXT (t)
10173 && pfd->include_nondeduced_p)
10174 WALK_SUBTREE (DECL_CONTEXT (t));
10175 break;
10176
10177 case BOUND_TEMPLATE_TEMPLATE_PARM:
10178 /* Record template parameters such as `T' inside `TT<T>'. */
10179 WALK_SUBTREE (TYPE_TI_ARGS (t));
10180 /* Fall through. */
10181
10182 case TEMPLATE_TEMPLATE_PARM:
10183 case TEMPLATE_TYPE_PARM:
10184 case TEMPLATE_PARM_INDEX:
10185 if (fn && (*fn)(t, data))
10186 return t;
10187 else if (!fn)
10188 return t;
10189 break;
10190
10191 case TEMPLATE_DECL:
10192 /* A template template parameter is encountered. */
10193 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10194 WALK_SUBTREE (TREE_TYPE (t));
10195
10196 /* Already substituted template template parameter */
10197 *walk_subtrees = 0;
10198 break;
10199
10200 case TYPENAME_TYPE:
10201 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10202 partial instantiation. */
10203 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10204 break;
10205
10206 case CONSTRUCTOR:
10207 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10208 && pfd->include_nondeduced_p)
10209 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10210 break;
10211
10212 case INDIRECT_REF:
10213 case COMPONENT_REF:
10214 /* If there's no type, then this thing must be some expression
10215 involving template parameters. */
10216 if (!fn && !TREE_TYPE (t))
10217 return error_mark_node;
10218 break;
10219
10220 case MODOP_EXPR:
10221 case CAST_EXPR:
10222 case IMPLICIT_CONV_EXPR:
10223 case REINTERPRET_CAST_EXPR:
10224 case CONST_CAST_EXPR:
10225 case STATIC_CAST_EXPR:
10226 case DYNAMIC_CAST_EXPR:
10227 case ARROW_EXPR:
10228 case DOTSTAR_EXPR:
10229 case TYPEID_EXPR:
10230 case PSEUDO_DTOR_EXPR:
10231 if (!fn)
10232 return error_mark_node;
10233 break;
10234
10235 case SCOPE_REF:
10236 if (pfd->include_nondeduced_p)
10237 WALK_SUBTREE (TREE_OPERAND (t, 0));
10238 break;
10239
10240 case REQUIRES_EXPR:
10241 {
10242 if (!fn)
10243 return error_mark_node;
10244
10245 /* Recursively walk the type of each constraint variable. */
10246 tree p = TREE_OPERAND (t, 0);
10247 while (p)
10248 {
10249 WALK_SUBTREE (TREE_TYPE (p));
10250 p = TREE_CHAIN (p);
10251 }
10252 }
10253 break;
10254
10255 default:
10256 break;
10257 }
10258
10259 #undef WALK_SUBTREE
10260
10261 /* We didn't find any template parameters we liked. */
10262 out:
10263 return result;
10264 }
10265
10266 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10267 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10268 call FN with the parameter and the DATA.
10269 If FN returns nonzero, the iteration is terminated, and
10270 for_each_template_parm returns 1. Otherwise, the iteration
10271 continues. If FN never returns a nonzero value, the value
10272 returned by for_each_template_parm is 0. If FN is NULL, it is
10273 considered to be the function which always returns 1.
10274
10275 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10276 parameters that occur in non-deduced contexts. When false, only
10277 visits those template parameters that can be deduced. */
10278
10279 static tree
10280 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10281 hash_set<tree> *visited,
10282 bool include_nondeduced_p,
10283 tree_fn_t any_fn)
10284 {
10285 struct pair_fn_data pfd;
10286 tree result;
10287
10288 /* Set up. */
10289 pfd.fn = fn;
10290 pfd.any_fn = any_fn;
10291 pfd.data = data;
10292 pfd.include_nondeduced_p = include_nondeduced_p;
10293
10294 /* Walk the tree. (Conceptually, we would like to walk without
10295 duplicates, but for_each_template_parm_r recursively calls
10296 for_each_template_parm, so we would need to reorganize a fair
10297 bit to use walk_tree_without_duplicates, so we keep our own
10298 visited list.) */
10299 if (visited)
10300 pfd.visited = visited;
10301 else
10302 pfd.visited = new hash_set<tree>;
10303 result = cp_walk_tree (&t,
10304 for_each_template_parm_r,
10305 &pfd,
10306 pfd.visited);
10307
10308 /* Clean up. */
10309 if (!visited)
10310 {
10311 delete pfd.visited;
10312 pfd.visited = 0;
10313 }
10314
10315 return result;
10316 }
10317
10318 struct find_template_parameter_info
10319 {
10320 explicit find_template_parameter_info (int d)
10321 : max_depth (d)
10322 {}
10323
10324 hash_set<tree> visited;
10325 hash_set<tree> parms;
10326 int max_depth;
10327 };
10328
10329 /* Appends the declaration of T to the list in DATA. */
10330
10331 static int
10332 keep_template_parm (tree t, void* data)
10333 {
10334 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10335
10336 /* Template parameters declared within the expression are not part of
10337 the parameter mapping. For example, in this concept:
10338
10339 template<typename T>
10340 concept C = requires { <expr> } -> same_as<int>;
10341
10342 the return specifier same_as<int> declares a new decltype parameter
10343 that must not be part of the parameter mapping. The same is true
10344 for generic lambda parameters, lambda template parameters, etc. */
10345 int level;
10346 int index;
10347 template_parm_level_and_index (t, &level, &index);
10348 if (level > ftpi->max_depth)
10349 return 0;
10350
10351 /* Arguments like const T yield parameters like const T. This means that
10352 a template-id like X<T, const T> would yield two distinct parameters:
10353 T and const T. Adjust types to their unqualified versions. */
10354 if (TYPE_P (t))
10355 t = TYPE_MAIN_VARIANT (t);
10356 ftpi->parms.add (t);
10357
10358 return 0;
10359 }
10360
10361 /* Ensure that we recursively examine certain terms that are not normally
10362 visited in for_each_template_parm_r. */
10363
10364 static int
10365 any_template_parm_r (tree t, void *data)
10366 {
10367 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10368
10369 #define WALK_SUBTREE(NODE) \
10370 do \
10371 { \
10372 for_each_template_parm (NODE, keep_template_parm, data, \
10373 &ftpi->visited, true, \
10374 any_template_parm_r); \
10375 } \
10376 while (0)
10377
10378 switch (TREE_CODE (t))
10379 {
10380 case RECORD_TYPE:
10381 case UNION_TYPE:
10382 case ENUMERAL_TYPE:
10383 /* Search for template parameters in type aliases. */
10384 if (alias_template_specialization_p (t))
10385 {
10386 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
10387 WALK_SUBTREE (TI_ARGS (tinfo));
10388 }
10389 break;
10390
10391 case TEMPLATE_TYPE_PARM:
10392 /* Type constraints of a placeholder type may contain parameters. */
10393 if (is_auto (t))
10394 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10395 WALK_SUBTREE (constr);
10396 break;
10397
10398 case TEMPLATE_ID_EXPR:
10399 /* Search through references to variable templates. */
10400 WALK_SUBTREE (TREE_OPERAND (t, 0));
10401 WALK_SUBTREE (TREE_OPERAND (t, 1));
10402 break;
10403
10404 case CONSTRUCTOR:
10405 if (TREE_TYPE (t))
10406 WALK_SUBTREE (TREE_TYPE (t));
10407 break;
10408
10409 case PARM_DECL:
10410 /* A parameter or constraint variable may also depend on a template
10411 parameter without explicitly naming it. */
10412 WALK_SUBTREE (TREE_TYPE (t));
10413 break;
10414
10415 default:
10416 break;
10417 }
10418
10419 /* Keep walking. */
10420 return 0;
10421 }
10422
10423 /* Returns a list of unique template parameters found within T. */
10424
10425 tree
10426 find_template_parameters (tree t, int depth)
10427 {
10428 find_template_parameter_info ftpi (depth);
10429 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10430 /*include_nondeduced*/true, any_template_parm_r);
10431 tree list = NULL_TREE;
10432 for (hash_set<tree>::iterator iter = ftpi.parms.begin();
10433 iter != ftpi.parms.end(); ++iter)
10434 list = tree_cons (NULL_TREE, *iter, list);
10435 return list;
10436 }
10437
10438 /* Returns true if T depends on any template parameter. */
10439
10440 int
10441 uses_template_parms (tree t)
10442 {
10443 if (t == NULL_TREE)
10444 return false;
10445
10446 bool dependent_p;
10447 int saved_processing_template_decl;
10448
10449 saved_processing_template_decl = processing_template_decl;
10450 if (!saved_processing_template_decl)
10451 processing_template_decl = 1;
10452 if (TYPE_P (t))
10453 dependent_p = dependent_type_p (t);
10454 else if (TREE_CODE (t) == TREE_VEC)
10455 dependent_p = any_dependent_template_arguments_p (t);
10456 else if (TREE_CODE (t) == TREE_LIST)
10457 dependent_p = (uses_template_parms (TREE_VALUE (t))
10458 || uses_template_parms (TREE_CHAIN (t)));
10459 else if (TREE_CODE (t) == TYPE_DECL)
10460 dependent_p = dependent_type_p (TREE_TYPE (t));
10461 else if (DECL_P (t)
10462 || EXPR_P (t)
10463 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10464 || TREE_CODE (t) == OVERLOAD
10465 || BASELINK_P (t)
10466 || identifier_p (t)
10467 || TREE_CODE (t) == TRAIT_EXPR
10468 || TREE_CODE (t) == CONSTRUCTOR
10469 || CONSTANT_CLASS_P (t))
10470 dependent_p = (type_dependent_expression_p (t)
10471 || value_dependent_expression_p (t));
10472 else
10473 {
10474 gcc_assert (t == error_mark_node);
10475 dependent_p = false;
10476 }
10477
10478 processing_template_decl = saved_processing_template_decl;
10479
10480 return dependent_p;
10481 }
10482
10483 /* Returns true iff current_function_decl is an incompletely instantiated
10484 template. Useful instead of processing_template_decl because the latter
10485 is set to 0 during instantiate_non_dependent_expr. */
10486
10487 bool
10488 in_template_function (void)
10489 {
10490 tree fn = current_function_decl;
10491 bool ret;
10492 ++processing_template_decl;
10493 ret = (fn && DECL_LANG_SPECIFIC (fn)
10494 && DECL_TEMPLATE_INFO (fn)
10495 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10496 --processing_template_decl;
10497 return ret;
10498 }
10499
10500 /* Returns true if T depends on any template parameter with level LEVEL. */
10501
10502 bool
10503 uses_template_parms_level (tree t, int level)
10504 {
10505 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10506 /*include_nondeduced_p=*/true);
10507 }
10508
10509 /* Returns true if the signature of DECL depends on any template parameter from
10510 its enclosing class. */
10511
10512 bool
10513 uses_outer_template_parms (tree decl)
10514 {
10515 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10516 if (depth == 0)
10517 return false;
10518 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10519 &depth, NULL, /*include_nondeduced_p=*/true))
10520 return true;
10521 if (PRIMARY_TEMPLATE_P (decl)
10522 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10523 (DECL_TEMPLATE_PARMS (decl)),
10524 template_parm_outer_level,
10525 &depth, NULL, /*include_nondeduced_p=*/true))
10526 return true;
10527 tree ci = get_constraints (decl);
10528 if (ci)
10529 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10530 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10531 &depth, NULL, /*nondeduced*/true))
10532 return true;
10533 return false;
10534 }
10535
10536 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10537 ill-formed translation unit, i.e. a variable or function that isn't
10538 usable in a constant expression. */
10539
10540 static inline bool
10541 neglectable_inst_p (tree d)
10542 {
10543 return (d && DECL_P (d)
10544 && !undeduced_auto_decl (d)
10545 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10546 : decl_maybe_constant_var_p (d)));
10547 }
10548
10549 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10550 neglectable and instantiated from within an erroneous instantiation. */
10551
10552 static bool
10553 limit_bad_template_recursion (tree decl)
10554 {
10555 struct tinst_level *lev = current_tinst_level;
10556 int errs = errorcount + sorrycount;
10557 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10558 return false;
10559
10560 for (; lev; lev = lev->next)
10561 if (neglectable_inst_p (lev->maybe_get_node ()))
10562 break;
10563
10564 return (lev && errs > lev->errors);
10565 }
10566
10567 static int tinst_depth;
10568 extern int max_tinst_depth;
10569 int depth_reached;
10570
10571 static GTY(()) struct tinst_level *last_error_tinst_level;
10572
10573 /* We're starting to instantiate D; record the template instantiation context
10574 at LOC for diagnostics and to restore it later. */
10575
10576 static bool
10577 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10578 {
10579 struct tinst_level *new_level;
10580
10581 if (tinst_depth >= max_tinst_depth)
10582 {
10583 /* Tell error.c not to try to instantiate any templates. */
10584 at_eof = 2;
10585 fatal_error (input_location,
10586 "template instantiation depth exceeds maximum of %d"
10587 " (use %<-ftemplate-depth=%> to increase the maximum)",
10588 max_tinst_depth);
10589 return false;
10590 }
10591
10592 /* If the current instantiation caused problems, don't let it instantiate
10593 anything else. Do allow deduction substitution and decls usable in
10594 constant expressions. */
10595 if (!targs && limit_bad_template_recursion (tldcl))
10596 return false;
10597
10598 /* When not -quiet, dump template instantiations other than functions, since
10599 announce_function will take care of those. */
10600 if (!quiet_flag && !targs
10601 && TREE_CODE (tldcl) != TREE_LIST
10602 && TREE_CODE (tldcl) != FUNCTION_DECL)
10603 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10604
10605 new_level = tinst_level_freelist ().alloc ();
10606 new_level->tldcl = tldcl;
10607 new_level->targs = targs;
10608 new_level->locus = loc;
10609 new_level->errors = errorcount + sorrycount;
10610 new_level->next = NULL;
10611 new_level->refcount = 0;
10612 set_refcount_ptr (new_level->next, current_tinst_level);
10613 set_refcount_ptr (current_tinst_level, new_level);
10614
10615 ++tinst_depth;
10616 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10617 depth_reached = tinst_depth;
10618
10619 return true;
10620 }
10621
10622 /* We're starting substitution of TMPL<ARGS>; record the template
10623 substitution context for diagnostics and to restore it later. */
10624
10625 static bool
10626 push_tinst_level (tree tmpl, tree args)
10627 {
10628 return push_tinst_level_loc (tmpl, args, input_location);
10629 }
10630
10631 /* We're starting to instantiate D; record INPUT_LOCATION and the
10632 template instantiation context for diagnostics and to restore it
10633 later. */
10634
10635 bool
10636 push_tinst_level (tree d)
10637 {
10638 return push_tinst_level_loc (d, input_location);
10639 }
10640
10641 /* Likewise, but record LOC as the program location. */
10642
10643 bool
10644 push_tinst_level_loc (tree d, location_t loc)
10645 {
10646 gcc_assert (TREE_CODE (d) != TREE_LIST);
10647 return push_tinst_level_loc (d, NULL, loc);
10648 }
10649
10650 /* We're done instantiating this template; return to the instantiation
10651 context. */
10652
10653 void
10654 pop_tinst_level (void)
10655 {
10656 /* Restore the filename and line number stashed away when we started
10657 this instantiation. */
10658 input_location = current_tinst_level->locus;
10659 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10660 --tinst_depth;
10661 }
10662
10663 /* We're instantiating a deferred template; restore the template
10664 instantiation context in which the instantiation was requested, which
10665 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10666
10667 static tree
10668 reopen_tinst_level (struct tinst_level *level)
10669 {
10670 struct tinst_level *t;
10671
10672 tinst_depth = 0;
10673 for (t = level; t; t = t->next)
10674 ++tinst_depth;
10675
10676 set_refcount_ptr (current_tinst_level, level);
10677 pop_tinst_level ();
10678 if (current_tinst_level)
10679 current_tinst_level->errors = errorcount+sorrycount;
10680 return level->maybe_get_node ();
10681 }
10682
10683 /* Returns the TINST_LEVEL which gives the original instantiation
10684 context. */
10685
10686 struct tinst_level *
10687 outermost_tinst_level (void)
10688 {
10689 struct tinst_level *level = current_tinst_level;
10690 if (level)
10691 while (level->next)
10692 level = level->next;
10693 return level;
10694 }
10695
10696 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10697 vector of template arguments, as for tsubst.
10698
10699 Returns an appropriate tsubst'd friend declaration. */
10700
10701 static tree
10702 tsubst_friend_function (tree decl, tree args)
10703 {
10704 tree new_friend;
10705
10706 if (TREE_CODE (decl) == FUNCTION_DECL
10707 && DECL_TEMPLATE_INSTANTIATION (decl)
10708 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10709 /* This was a friend declared with an explicit template
10710 argument list, e.g.:
10711
10712 friend void f<>(T);
10713
10714 to indicate that f was a template instantiation, not a new
10715 function declaration. Now, we have to figure out what
10716 instantiation of what template. */
10717 {
10718 tree template_id, arglist, fns;
10719 tree new_args;
10720 tree tmpl;
10721 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10722
10723 /* Friend functions are looked up in the containing namespace scope.
10724 We must enter that scope, to avoid finding member functions of the
10725 current class with same name. */
10726 push_nested_namespace (ns);
10727 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10728 tf_warning_or_error, NULL_TREE,
10729 /*integral_constant_expression_p=*/false);
10730 pop_nested_namespace (ns);
10731 arglist = tsubst (DECL_TI_ARGS (decl), args,
10732 tf_warning_or_error, NULL_TREE);
10733 template_id = lookup_template_function (fns, arglist);
10734
10735 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10736 tmpl = determine_specialization (template_id, new_friend,
10737 &new_args,
10738 /*need_member_template=*/0,
10739 TREE_VEC_LENGTH (args),
10740 tsk_none);
10741 return instantiate_template (tmpl, new_args, tf_error);
10742 }
10743
10744 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10745
10746 /* The NEW_FRIEND will look like an instantiation, to the
10747 compiler, but is not an instantiation from the point of view of
10748 the language. For example, we might have had:
10749
10750 template <class T> struct S {
10751 template <class U> friend void f(T, U);
10752 };
10753
10754 Then, in S<int>, template <class U> void f(int, U) is not an
10755 instantiation of anything. */
10756 if (new_friend == error_mark_node)
10757 return error_mark_node;
10758
10759 DECL_USE_TEMPLATE (new_friend) = 0;
10760 if (TREE_CODE (decl) == TEMPLATE_DECL)
10761 {
10762 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10763 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10764 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10765
10766 /* Attach the template requirements to the new declaration
10767 for declaration matching. We need to rebuild the requirements
10768 so that parameter levels match. */
10769 if (tree ci = get_constraints (decl))
10770 {
10771 tree parms = DECL_TEMPLATE_PARMS (new_friend);
10772 tree args = generic_targs_for (new_friend);
10773 tree treqs = tsubst_constraint (CI_TEMPLATE_REQS (ci), args,
10774 tf_warning_or_error, NULL_TREE);
10775 tree freqs = tsubst_constraint (CI_DECLARATOR_REQS (ci), args,
10776 tf_warning_or_error, NULL_TREE);
10777
10778 /* Update the constraints -- these won't really be valid for
10779 checking, but that's not what we need them for. These ensure
10780 that the declared function can find the friend during
10781 declaration matching. */
10782 tree new_ci = get_constraints (new_friend);
10783 CI_TEMPLATE_REQS (new_ci) = treqs;
10784 CI_DECLARATOR_REQS (new_ci) = freqs;
10785
10786 /* Also update the template parameter list. */
10787 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
10788 }
10789 }
10790
10791 /* The mangled name for the NEW_FRIEND is incorrect. The function
10792 is not a template instantiation and should not be mangled like
10793 one. Therefore, we forget the mangling here; we'll recompute it
10794 later if we need it. */
10795 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10796 {
10797 SET_DECL_RTL (new_friend, NULL);
10798 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10799 }
10800
10801 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10802 {
10803 tree old_decl;
10804 tree new_friend_template_info;
10805 tree new_friend_result_template_info;
10806 tree ns;
10807 int new_friend_is_defn;
10808
10809 /* We must save some information from NEW_FRIEND before calling
10810 duplicate decls since that function will free NEW_FRIEND if
10811 possible. */
10812 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10813 new_friend_is_defn =
10814 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10815 (template_for_substitution (new_friend)))
10816 != NULL_TREE);
10817 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10818 {
10819 /* This declaration is a `primary' template. */
10820 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10821
10822 new_friend_result_template_info
10823 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10824 }
10825 else
10826 new_friend_result_template_info = NULL_TREE;
10827
10828 /* Inside pushdecl_namespace_level, we will push into the
10829 current namespace. However, the friend function should go
10830 into the namespace of the template. */
10831 ns = decl_namespace_context (new_friend);
10832 push_nested_namespace (ns);
10833 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10834 pop_nested_namespace (ns);
10835
10836 if (old_decl == error_mark_node)
10837 return error_mark_node;
10838
10839 if (old_decl != new_friend)
10840 {
10841 /* This new friend declaration matched an existing
10842 declaration. For example, given:
10843
10844 template <class T> void f(T);
10845 template <class U> class C {
10846 template <class T> friend void f(T) {}
10847 };
10848
10849 the friend declaration actually provides the definition
10850 of `f', once C has been instantiated for some type. So,
10851 old_decl will be the out-of-class template declaration,
10852 while new_friend is the in-class definition.
10853
10854 But, if `f' was called before this point, the
10855 instantiation of `f' will have DECL_TI_ARGS corresponding
10856 to `T' but not to `U', references to which might appear
10857 in the definition of `f'. Previously, the most general
10858 template for an instantiation of `f' was the out-of-class
10859 version; now it is the in-class version. Therefore, we
10860 run through all specialization of `f', adding to their
10861 DECL_TI_ARGS appropriately. In particular, they need a
10862 new set of outer arguments, corresponding to the
10863 arguments for this class instantiation.
10864
10865 The same situation can arise with something like this:
10866
10867 friend void f(int);
10868 template <class T> class C {
10869 friend void f(T) {}
10870 };
10871
10872 when `C<int>' is instantiated. Now, `f(int)' is defined
10873 in the class. */
10874
10875 if (!new_friend_is_defn)
10876 /* On the other hand, if the in-class declaration does
10877 *not* provide a definition, then we don't want to alter
10878 existing definitions. We can just leave everything
10879 alone. */
10880 ;
10881 else
10882 {
10883 tree new_template = TI_TEMPLATE (new_friend_template_info);
10884 tree new_args = TI_ARGS (new_friend_template_info);
10885
10886 /* Overwrite whatever template info was there before, if
10887 any, with the new template information pertaining to
10888 the declaration. */
10889 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10890
10891 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10892 {
10893 /* We should have called reregister_specialization in
10894 duplicate_decls. */
10895 gcc_assert (retrieve_specialization (new_template,
10896 new_args, 0)
10897 == old_decl);
10898
10899 /* Instantiate it if the global has already been used. */
10900 if (DECL_ODR_USED (old_decl))
10901 instantiate_decl (old_decl, /*defer_ok=*/true,
10902 /*expl_inst_class_mem_p=*/false);
10903 }
10904 else
10905 {
10906 tree t;
10907
10908 /* Indicate that the old function template is a partial
10909 instantiation. */
10910 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10911 = new_friend_result_template_info;
10912
10913 gcc_assert (new_template
10914 == most_general_template (new_template));
10915 gcc_assert (new_template != old_decl);
10916
10917 /* Reassign any specializations already in the hash table
10918 to the new more general template, and add the
10919 additional template args. */
10920 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10921 t != NULL_TREE;
10922 t = TREE_CHAIN (t))
10923 {
10924 tree spec = TREE_VALUE (t);
10925 spec_entry elt;
10926
10927 elt.tmpl = old_decl;
10928 elt.args = DECL_TI_ARGS (spec);
10929 elt.spec = NULL_TREE;
10930
10931 decl_specializations->remove_elt (&elt);
10932
10933 DECL_TI_ARGS (spec)
10934 = add_outermost_template_args (new_args,
10935 DECL_TI_ARGS (spec));
10936
10937 register_specialization
10938 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10939
10940 }
10941 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10942 }
10943 }
10944
10945 /* The information from NEW_FRIEND has been merged into OLD_DECL
10946 by duplicate_decls. */
10947 new_friend = old_decl;
10948 }
10949 }
10950 else
10951 {
10952 tree context = DECL_CONTEXT (new_friend);
10953 bool dependent_p;
10954
10955 /* In the code
10956 template <class T> class C {
10957 template <class U> friend void C1<U>::f (); // case 1
10958 friend void C2<T>::f (); // case 2
10959 };
10960 we only need to make sure CONTEXT is a complete type for
10961 case 2. To distinguish between the two cases, we note that
10962 CONTEXT of case 1 remains dependent type after tsubst while
10963 this isn't true for case 2. */
10964 ++processing_template_decl;
10965 dependent_p = dependent_type_p (context);
10966 --processing_template_decl;
10967
10968 if (!dependent_p
10969 && !complete_type_or_else (context, NULL_TREE))
10970 return error_mark_node;
10971
10972 if (COMPLETE_TYPE_P (context))
10973 {
10974 tree fn = new_friend;
10975 /* do_friend adds the TEMPLATE_DECL for any member friend
10976 template even if it isn't a member template, i.e.
10977 template <class T> friend A<T>::f();
10978 Look through it in that case. */
10979 if (TREE_CODE (fn) == TEMPLATE_DECL
10980 && !PRIMARY_TEMPLATE_P (fn))
10981 fn = DECL_TEMPLATE_RESULT (fn);
10982 /* Check to see that the declaration is really present, and,
10983 possibly obtain an improved declaration. */
10984 fn = check_classfn (context, fn, NULL_TREE);
10985
10986 if (fn)
10987 new_friend = fn;
10988 }
10989 }
10990
10991 return new_friend;
10992 }
10993
10994 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10995 template arguments, as for tsubst.
10996
10997 Returns an appropriate tsubst'd friend type or error_mark_node on
10998 failure. */
10999
11000 static tree
11001 tsubst_friend_class (tree friend_tmpl, tree args)
11002 {
11003 tree tmpl;
11004
11005 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11006 {
11007 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11008 return TREE_TYPE (tmpl);
11009 }
11010
11011 tree context = CP_DECL_CONTEXT (friend_tmpl);
11012 if (TREE_CODE (context) == NAMESPACE_DECL)
11013 push_nested_namespace (context);
11014 else
11015 {
11016 context = tsubst (context, args, tf_error, NULL_TREE);
11017 push_nested_class (context);
11018 }
11019
11020 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
11021 /*non_class=*/false, /*block_p=*/false,
11022 /*namespaces_only=*/false, LOOKUP_HIDDEN);
11023
11024 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11025 {
11026 /* The friend template has already been declared. Just
11027 check to see that the declarations match, and install any new
11028 default parameters. We must tsubst the default parameters,
11029 of course. We only need the innermost template parameters
11030 because that is all that redeclare_class_template will look
11031 at. */
11032 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11033 > TMPL_ARGS_DEPTH (args))
11034 {
11035 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11036 args, tf_warning_or_error);
11037 location_t saved_input_location = input_location;
11038 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11039 tree cons = get_constraints (tmpl);
11040 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11041 input_location = saved_input_location;
11042 }
11043 }
11044 else
11045 {
11046 /* The friend template has not already been declared. In this
11047 case, the instantiation of the template class will cause the
11048 injection of this template into the namespace scope. */
11049 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11050
11051 if (tmpl != error_mark_node)
11052 {
11053 /* The new TMPL is not an instantiation of anything, so we
11054 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11055 for the new type because that is supposed to be the
11056 corresponding template decl, i.e., TMPL. */
11057 DECL_USE_TEMPLATE (tmpl) = 0;
11058 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11059 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11060 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11061 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11062
11063 /* It is hidden. */
11064 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
11065 DECL_ANTICIPATED (tmpl)
11066 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
11067
11068 /* Inject this template into the enclosing namspace scope. */
11069 tmpl = pushdecl_namespace_level (tmpl, true);
11070 }
11071 }
11072
11073 if (TREE_CODE (context) == NAMESPACE_DECL)
11074 pop_nested_namespace (context);
11075 else
11076 pop_nested_class ();
11077
11078 return TREE_TYPE (tmpl);
11079 }
11080
11081 /* Returns zero if TYPE cannot be completed later due to circularity.
11082 Otherwise returns one. */
11083
11084 static int
11085 can_complete_type_without_circularity (tree type)
11086 {
11087 if (type == NULL_TREE || type == error_mark_node)
11088 return 0;
11089 else if (COMPLETE_TYPE_P (type))
11090 return 1;
11091 else if (TREE_CODE (type) == ARRAY_TYPE)
11092 return can_complete_type_without_circularity (TREE_TYPE (type));
11093 else if (CLASS_TYPE_P (type)
11094 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11095 return 0;
11096 else
11097 return 1;
11098 }
11099
11100 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11101 tsubst_flags_t, tree);
11102
11103 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11104 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11105
11106 static tree
11107 tsubst_attribute (tree t, tree *decl_p, tree args,
11108 tsubst_flags_t complain, tree in_decl)
11109 {
11110 gcc_assert (ATTR_IS_DEPENDENT (t));
11111
11112 tree val = TREE_VALUE (t);
11113 if (val == NULL_TREE)
11114 /* Nothing to do. */;
11115 else if ((flag_openmp || flag_openmp_simd)
11116 && is_attribute_p ("omp declare simd",
11117 get_attribute_name (t)))
11118 {
11119 tree clauses = TREE_VALUE (val);
11120 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11121 complain, in_decl);
11122 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11123 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11124 tree parms = DECL_ARGUMENTS (*decl_p);
11125 clauses
11126 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11127 if (clauses)
11128 val = build_tree_list (NULL_TREE, clauses);
11129 else
11130 val = NULL_TREE;
11131 }
11132 else if (flag_openmp
11133 && is_attribute_p ("omp declare variant base",
11134 get_attribute_name (t)))
11135 {
11136 ++cp_unevaluated_operand;
11137 tree varid
11138 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11139 in_decl, /*integral_constant_expression_p=*/false);
11140 --cp_unevaluated_operand;
11141 tree chain = TREE_CHAIN (val);
11142 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11143 tree ctx = copy_list (TREE_VALUE (val));
11144 tree simd = get_identifier ("simd");
11145 tree score = get_identifier (" score");
11146 tree condition = get_identifier ("condition");
11147 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11148 {
11149 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11150 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11151 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11152 {
11153 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11154 {
11155 tree clauses = TREE_VALUE (t2);
11156 clauses = tsubst_omp_clauses (clauses,
11157 C_ORT_OMP_DECLARE_SIMD, args,
11158 complain, in_decl);
11159 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11160 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11161 TREE_VALUE (t2) = clauses;
11162 }
11163 else
11164 {
11165 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11166 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11167 if (TREE_VALUE (t3))
11168 {
11169 bool allow_string
11170 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11171 && TREE_PURPOSE (t3) != score);
11172 tree v = TREE_VALUE (t3);
11173 if (TREE_CODE (v) == STRING_CST && allow_string)
11174 continue;
11175 v = tsubst_expr (v, args, complain, in_decl, true);
11176 v = fold_non_dependent_expr (v);
11177 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11178 || (TREE_PURPOSE (t3) == score
11179 ? TREE_CODE (v) != INTEGER_CST
11180 : !tree_fits_shwi_p (v)))
11181 {
11182 location_t loc
11183 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11184 match_loc);
11185 if (TREE_PURPOSE (t3) == score)
11186 error_at (loc, "score argument must be "
11187 "constant integer expression");
11188 else if (allow_string)
11189 error_at (loc, "property must be constant "
11190 "integer expression or string "
11191 "literal");
11192 else
11193 error_at (loc, "property must be constant "
11194 "integer expression");
11195 return NULL_TREE;
11196 }
11197 else if (TREE_PURPOSE (t3) == score
11198 && tree_int_cst_sgn (v) < 0)
11199 {
11200 location_t loc
11201 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11202 match_loc);
11203 error_at (loc, "score argument must be "
11204 "non-negative");
11205 return NULL_TREE;
11206 }
11207 TREE_VALUE (t3) = v;
11208 }
11209 }
11210 }
11211 }
11212 val = tree_cons (varid, ctx, chain);
11213 }
11214 /* If the first attribute argument is an identifier, don't
11215 pass it through tsubst. Attributes like mode, format,
11216 cleanup and several target specific attributes expect it
11217 unmodified. */
11218 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11219 {
11220 tree chain
11221 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11222 /*integral_constant_expression_p=*/false);
11223 if (chain != TREE_CHAIN (val))
11224 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11225 }
11226 else if (PACK_EXPANSION_P (val))
11227 {
11228 /* An attribute pack expansion. */
11229 tree purp = TREE_PURPOSE (t);
11230 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11231 if (pack == error_mark_node)
11232 return error_mark_node;
11233 int len = TREE_VEC_LENGTH (pack);
11234 tree list = NULL_TREE;
11235 tree *q = &list;
11236 for (int i = 0; i < len; ++i)
11237 {
11238 tree elt = TREE_VEC_ELT (pack, i);
11239 *q = build_tree_list (purp, elt);
11240 q = &TREE_CHAIN (*q);
11241 }
11242 return list;
11243 }
11244 else
11245 val = tsubst_expr (val, args, complain, in_decl,
11246 /*integral_constant_expression_p=*/false);
11247
11248 if (val != TREE_VALUE (t))
11249 return build_tree_list (TREE_PURPOSE (t), val);
11250 return t;
11251 }
11252
11253 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11254 unchanged or a new TREE_LIST chain. */
11255
11256 static tree
11257 tsubst_attributes (tree attributes, tree args,
11258 tsubst_flags_t complain, tree in_decl)
11259 {
11260 tree last_dep = NULL_TREE;
11261
11262 for (tree t = attributes; t; t = TREE_CHAIN (t))
11263 if (ATTR_IS_DEPENDENT (t))
11264 {
11265 last_dep = t;
11266 attributes = copy_list (attributes);
11267 break;
11268 }
11269
11270 if (last_dep)
11271 for (tree *p = &attributes; *p; )
11272 {
11273 tree t = *p;
11274 if (ATTR_IS_DEPENDENT (t))
11275 {
11276 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11277 if (subst != t)
11278 {
11279 *p = subst;
11280 while (*p)
11281 p = &TREE_CHAIN (*p);
11282 *p = TREE_CHAIN (t);
11283 continue;
11284 }
11285 }
11286 p = &TREE_CHAIN (*p);
11287 }
11288
11289 return attributes;
11290 }
11291
11292 /* Apply any attributes which had to be deferred until instantiation
11293 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11294 ARGS, COMPLAIN, IN_DECL are as tsubst. */
11295
11296 static void
11297 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11298 tree args, tsubst_flags_t complain, tree in_decl)
11299 {
11300 tree last_dep = NULL_TREE;
11301 tree t;
11302 tree *p;
11303
11304 if (attributes == NULL_TREE)
11305 return;
11306
11307 if (DECL_P (*decl_p))
11308 {
11309 if (TREE_TYPE (*decl_p) == error_mark_node)
11310 return;
11311 p = &DECL_ATTRIBUTES (*decl_p);
11312 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11313 to our attributes parameter. */
11314 gcc_assert (*p == attributes);
11315 }
11316 else
11317 {
11318 p = &TYPE_ATTRIBUTES (*decl_p);
11319 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11320 lookup_template_class_1, and should be preserved. */
11321 gcc_assert (*p != attributes);
11322 while (*p)
11323 p = &TREE_CHAIN (*p);
11324 }
11325
11326 for (t = attributes; t; t = TREE_CHAIN (t))
11327 if (ATTR_IS_DEPENDENT (t))
11328 {
11329 last_dep = t;
11330 attributes = copy_list (attributes);
11331 break;
11332 }
11333
11334 *p = attributes;
11335 if (last_dep)
11336 {
11337 tree late_attrs = NULL_TREE;
11338 tree *q = &late_attrs;
11339
11340 for (; *p; )
11341 {
11342 t = *p;
11343 if (ATTR_IS_DEPENDENT (t))
11344 {
11345 *p = TREE_CHAIN (t);
11346 TREE_CHAIN (t) = NULL_TREE;
11347 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11348 while (*q)
11349 q = &TREE_CHAIN (*q);
11350 }
11351 else
11352 p = &TREE_CHAIN (t);
11353 }
11354
11355 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11356 }
11357 }
11358
11359 /* Perform (or defer) access check for typedefs that were referenced
11360 from within the template TMPL code.
11361 This is a subroutine of instantiate_decl and instantiate_class_template.
11362 TMPL is the template to consider and TARGS is the list of arguments of
11363 that template. */
11364
11365 static void
11366 perform_typedefs_access_check (tree tmpl, tree targs)
11367 {
11368 unsigned i;
11369 qualified_typedef_usage_t *iter;
11370
11371 if (!tmpl
11372 || (!CLASS_TYPE_P (tmpl)
11373 && TREE_CODE (tmpl) != FUNCTION_DECL))
11374 return;
11375
11376 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
11377 {
11378 tree type_decl = iter->typedef_decl;
11379 tree type_scope = iter->context;
11380
11381 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
11382 continue;
11383
11384 if (uses_template_parms (type_decl))
11385 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
11386 if (uses_template_parms (type_scope))
11387 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11388
11389 /* Make access check error messages point to the location
11390 of the use of the typedef. */
11391 iloc_sentinel ils (iter->locus);
11392 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11393 type_decl, type_decl,
11394 tf_warning_or_error);
11395 }
11396 }
11397
11398 static tree
11399 instantiate_class_template_1 (tree type)
11400 {
11401 tree templ, args, pattern, t, member;
11402 tree typedecl;
11403 tree pbinfo;
11404 tree base_list;
11405 unsigned int saved_maximum_field_alignment;
11406 tree fn_context;
11407
11408 if (type == error_mark_node)
11409 return error_mark_node;
11410
11411 if (COMPLETE_OR_OPEN_TYPE_P (type)
11412 || uses_template_parms (type))
11413 return type;
11414
11415 /* Figure out which template is being instantiated. */
11416 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11417 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11418
11419 /* Mark the type as in the process of being defined. */
11420 TYPE_BEING_DEFINED (type) = 1;
11421
11422 /* We may be in the middle of deferred access check. Disable
11423 it now. */
11424 deferring_access_check_sentinel acs (dk_no_deferred);
11425
11426 /* Determine what specialization of the original template to
11427 instantiate. */
11428 t = most_specialized_partial_spec (type, tf_warning_or_error);
11429 if (t == error_mark_node)
11430 return error_mark_node;
11431 else if (t)
11432 {
11433 /* This TYPE is actually an instantiation of a partial
11434 specialization. We replace the innermost set of ARGS with
11435 the arguments appropriate for substitution. For example,
11436 given:
11437
11438 template <class T> struct S {};
11439 template <class T> struct S<T*> {};
11440
11441 and supposing that we are instantiating S<int*>, ARGS will
11442 presently be {int*} -- but we need {int}. */
11443 pattern = TREE_TYPE (t);
11444 args = TREE_PURPOSE (t);
11445 }
11446 else
11447 {
11448 pattern = TREE_TYPE (templ);
11449 args = CLASSTYPE_TI_ARGS (type);
11450 }
11451
11452 /* If the template we're instantiating is incomplete, then clearly
11453 there's nothing we can do. */
11454 if (!COMPLETE_TYPE_P (pattern))
11455 {
11456 /* We can try again later. */
11457 TYPE_BEING_DEFINED (type) = 0;
11458 return type;
11459 }
11460
11461 /* If we've recursively instantiated too many templates, stop. */
11462 if (! push_tinst_level (type))
11463 return type;
11464
11465 int saved_unevaluated_operand = cp_unevaluated_operand;
11466 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11467
11468 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11469 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11470 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11471 fn_context = error_mark_node;
11472 if (!fn_context)
11473 push_to_top_level ();
11474 else
11475 {
11476 cp_unevaluated_operand = 0;
11477 c_inhibit_evaluation_warnings = 0;
11478 }
11479 /* Use #pragma pack from the template context. */
11480 saved_maximum_field_alignment = maximum_field_alignment;
11481 maximum_field_alignment = TYPE_PRECISION (pattern);
11482
11483 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11484
11485 /* Set the input location to the most specialized template definition.
11486 This is needed if tsubsting causes an error. */
11487 typedecl = TYPE_MAIN_DECL (pattern);
11488 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11489 DECL_SOURCE_LOCATION (typedecl);
11490
11491 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11492 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11493 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11494 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11495 if (ANON_AGGR_TYPE_P (pattern))
11496 SET_ANON_AGGR_TYPE_P (type);
11497 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11498 {
11499 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11500 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11501 /* Adjust visibility for template arguments. */
11502 determine_visibility (TYPE_MAIN_DECL (type));
11503 }
11504 if (CLASS_TYPE_P (type))
11505 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11506
11507 pbinfo = TYPE_BINFO (pattern);
11508
11509 /* We should never instantiate a nested class before its enclosing
11510 class; we need to look up the nested class by name before we can
11511 instantiate it, and that lookup should instantiate the enclosing
11512 class. */
11513 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11514 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11515
11516 base_list = NULL_TREE;
11517 if (BINFO_N_BASE_BINFOS (pbinfo))
11518 {
11519 tree pbase_binfo;
11520 tree pushed_scope;
11521 int i;
11522
11523 /* We must enter the scope containing the type, as that is where
11524 the accessibility of types named in dependent bases are
11525 looked up from. */
11526 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11527
11528 /* Substitute into each of the bases to determine the actual
11529 basetypes. */
11530 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11531 {
11532 tree base;
11533 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11534 tree expanded_bases = NULL_TREE;
11535 int idx, len = 1;
11536
11537 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11538 {
11539 expanded_bases =
11540 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11541 args, tf_error, NULL_TREE);
11542 if (expanded_bases == error_mark_node)
11543 continue;
11544
11545 len = TREE_VEC_LENGTH (expanded_bases);
11546 }
11547
11548 for (idx = 0; idx < len; idx++)
11549 {
11550 if (expanded_bases)
11551 /* Extract the already-expanded base class. */
11552 base = TREE_VEC_ELT (expanded_bases, idx);
11553 else
11554 /* Substitute to figure out the base class. */
11555 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11556 NULL_TREE);
11557
11558 if (base == error_mark_node)
11559 continue;
11560
11561 base_list = tree_cons (access, base, base_list);
11562 if (BINFO_VIRTUAL_P (pbase_binfo))
11563 TREE_TYPE (base_list) = integer_type_node;
11564 }
11565 }
11566
11567 /* The list is now in reverse order; correct that. */
11568 base_list = nreverse (base_list);
11569
11570 if (pushed_scope)
11571 pop_scope (pushed_scope);
11572 }
11573 /* Now call xref_basetypes to set up all the base-class
11574 information. */
11575 xref_basetypes (type, base_list);
11576
11577 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11578 (int) ATTR_FLAG_TYPE_IN_PLACE,
11579 args, tf_error, NULL_TREE);
11580 fixup_attribute_variants (type);
11581
11582 /* Now that our base classes are set up, enter the scope of the
11583 class, so that name lookups into base classes, etc. will work
11584 correctly. This is precisely analogous to what we do in
11585 begin_class_definition when defining an ordinary non-template
11586 class, except we also need to push the enclosing classes. */
11587 push_nested_class (type);
11588
11589 /* Now members are processed in the order of declaration. */
11590 for (member = CLASSTYPE_DECL_LIST (pattern);
11591 member; member = TREE_CHAIN (member))
11592 {
11593 tree t = TREE_VALUE (member);
11594
11595 if (TREE_PURPOSE (member))
11596 {
11597 if (TYPE_P (t))
11598 {
11599 if (LAMBDA_TYPE_P (t))
11600 /* A closure type for a lambda in an NSDMI or default argument.
11601 Ignore it; it will be regenerated when needed. */
11602 continue;
11603
11604 /* Build new CLASSTYPE_NESTED_UTDS. */
11605
11606 tree newtag;
11607 bool class_template_p;
11608
11609 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11610 && TYPE_LANG_SPECIFIC (t)
11611 && CLASSTYPE_IS_TEMPLATE (t));
11612 /* If the member is a class template, then -- even after
11613 substitution -- there may be dependent types in the
11614 template argument list for the class. We increment
11615 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11616 that function will assume that no types are dependent
11617 when outside of a template. */
11618 if (class_template_p)
11619 ++processing_template_decl;
11620 newtag = tsubst (t, args, tf_error, NULL_TREE);
11621 if (class_template_p)
11622 --processing_template_decl;
11623 if (newtag == error_mark_node)
11624 continue;
11625
11626 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11627 {
11628 tree name = TYPE_IDENTIFIER (t);
11629
11630 if (class_template_p)
11631 /* Unfortunately, lookup_template_class sets
11632 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11633 instantiation (i.e., for the type of a member
11634 template class nested within a template class.)
11635 This behavior is required for
11636 maybe_process_partial_specialization to work
11637 correctly, but is not accurate in this case;
11638 the TAG is not an instantiation of anything.
11639 (The corresponding TEMPLATE_DECL is an
11640 instantiation, but the TYPE is not.) */
11641 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11642
11643 /* Now, we call pushtag to put this NEWTAG into the scope of
11644 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11645 pushtag calling push_template_decl. We don't have to do
11646 this for enums because it will already have been done in
11647 tsubst_enum. */
11648 if (name)
11649 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11650 pushtag (name, newtag, /*tag_scope=*/ts_current);
11651 }
11652 }
11653 else if (DECL_DECLARES_FUNCTION_P (t))
11654 {
11655 tree r;
11656
11657 if (TREE_CODE (t) == TEMPLATE_DECL)
11658 ++processing_template_decl;
11659 r = tsubst (t, args, tf_error, NULL_TREE);
11660 if (TREE_CODE (t) == TEMPLATE_DECL)
11661 --processing_template_decl;
11662 set_current_access_from_decl (r);
11663 finish_member_declaration (r);
11664 /* Instantiate members marked with attribute used. */
11665 if (r != error_mark_node && DECL_PRESERVE_P (r))
11666 mark_used (r);
11667 if (TREE_CODE (r) == FUNCTION_DECL
11668 && DECL_OMP_DECLARE_REDUCTION_P (r))
11669 cp_check_omp_declare_reduction (r);
11670 }
11671 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11672 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11673 /* A closure type for a lambda in an NSDMI or default argument.
11674 Ignore it; it will be regenerated when needed. */;
11675 else
11676 {
11677 /* Build new TYPE_FIELDS. */
11678 if (TREE_CODE (t) == STATIC_ASSERT)
11679 {
11680 tree condition;
11681
11682 ++c_inhibit_evaluation_warnings;
11683 condition =
11684 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11685 tf_warning_or_error, NULL_TREE,
11686 /*integral_constant_expression_p=*/true);
11687 --c_inhibit_evaluation_warnings;
11688
11689 finish_static_assert (condition,
11690 STATIC_ASSERT_MESSAGE (t),
11691 STATIC_ASSERT_SOURCE_LOCATION (t),
11692 /*member_p=*/true);
11693 }
11694 else if (TREE_CODE (t) != CONST_DECL)
11695 {
11696 tree r;
11697 tree vec = NULL_TREE;
11698 int len = 1;
11699
11700 /* The file and line for this declaration, to
11701 assist in error message reporting. Since we
11702 called push_tinst_level above, we don't need to
11703 restore these. */
11704 input_location = DECL_SOURCE_LOCATION (t);
11705
11706 if (TREE_CODE (t) == TEMPLATE_DECL)
11707 ++processing_template_decl;
11708 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11709 if (TREE_CODE (t) == TEMPLATE_DECL)
11710 --processing_template_decl;
11711
11712 if (TREE_CODE (r) == TREE_VEC)
11713 {
11714 /* A capture pack became multiple fields. */
11715 vec = r;
11716 len = TREE_VEC_LENGTH (vec);
11717 }
11718
11719 for (int i = 0; i < len; ++i)
11720 {
11721 if (vec)
11722 r = TREE_VEC_ELT (vec, i);
11723 if (VAR_P (r))
11724 {
11725 /* In [temp.inst]:
11726
11727 [t]he initialization (and any associated
11728 side-effects) of a static data member does
11729 not occur unless the static data member is
11730 itself used in a way that requires the
11731 definition of the static data member to
11732 exist.
11733
11734 Therefore, we do not substitute into the
11735 initialized for the static data member here. */
11736 finish_static_data_member_decl
11737 (r,
11738 /*init=*/NULL_TREE,
11739 /*init_const_expr_p=*/false,
11740 /*asmspec_tree=*/NULL_TREE,
11741 /*flags=*/0);
11742 /* Instantiate members marked with attribute used. */
11743 if (r != error_mark_node && DECL_PRESERVE_P (r))
11744 mark_used (r);
11745 }
11746 else if (TREE_CODE (r) == FIELD_DECL)
11747 {
11748 /* Determine whether R has a valid type and can be
11749 completed later. If R is invalid, then its type
11750 is replaced by error_mark_node. */
11751 tree rtype = TREE_TYPE (r);
11752 if (can_complete_type_without_circularity (rtype))
11753 complete_type (rtype);
11754
11755 if (!complete_or_array_type_p (rtype))
11756 {
11757 /* If R's type couldn't be completed and
11758 it isn't a flexible array member (whose
11759 type is incomplete by definition) give
11760 an error. */
11761 cxx_incomplete_type_error (r, rtype);
11762 TREE_TYPE (r) = error_mark_node;
11763 }
11764 else if (TREE_CODE (rtype) == ARRAY_TYPE
11765 && TYPE_DOMAIN (rtype) == NULL_TREE
11766 && (TREE_CODE (type) == UNION_TYPE
11767 || TREE_CODE (type) == QUAL_UNION_TYPE))
11768 {
11769 error ("flexible array member %qD in union", r);
11770 TREE_TYPE (r) = error_mark_node;
11771 }
11772 }
11773
11774 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11775 such a thing will already have been added to the field
11776 list by tsubst_enum in finish_member_declaration in the
11777 CLASSTYPE_NESTED_UTDS case above. */
11778 if (!(TREE_CODE (r) == TYPE_DECL
11779 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11780 && DECL_ARTIFICIAL (r)))
11781 {
11782 set_current_access_from_decl (r);
11783 finish_member_declaration (r);
11784 }
11785 }
11786 }
11787 }
11788 }
11789 else
11790 {
11791 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11792 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11793 {
11794 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11795
11796 tree friend_type = t;
11797 bool adjust_processing_template_decl = false;
11798
11799 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11800 {
11801 /* template <class T> friend class C; */
11802 friend_type = tsubst_friend_class (friend_type, args);
11803 adjust_processing_template_decl = true;
11804 }
11805 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11806 {
11807 /* template <class T> friend class C::D; */
11808 friend_type = tsubst (friend_type, args,
11809 tf_warning_or_error, NULL_TREE);
11810 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11811 friend_type = TREE_TYPE (friend_type);
11812 adjust_processing_template_decl = true;
11813 }
11814 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11815 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11816 {
11817 /* This could be either
11818
11819 friend class T::C;
11820
11821 when dependent_type_p is false or
11822
11823 template <class U> friend class T::C;
11824
11825 otherwise. */
11826 /* Bump processing_template_decl in case this is something like
11827 template <class T> friend struct A<T>::B. */
11828 ++processing_template_decl;
11829 friend_type = tsubst (friend_type, args,
11830 tf_warning_or_error, NULL_TREE);
11831 if (dependent_type_p (friend_type))
11832 adjust_processing_template_decl = true;
11833 --processing_template_decl;
11834 }
11835 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11836 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11837 && TYPE_HIDDEN_P (friend_type))
11838 {
11839 /* friend class C;
11840
11841 where C hasn't been declared yet. Let's lookup name
11842 from namespace scope directly, bypassing any name that
11843 come from dependent base class. */
11844 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11845
11846 /* The call to xref_tag_from_type does injection for friend
11847 classes. */
11848 push_nested_namespace (ns);
11849 friend_type =
11850 xref_tag_from_type (friend_type, NULL_TREE,
11851 /*tag_scope=*/ts_current);
11852 pop_nested_namespace (ns);
11853 }
11854 else if (uses_template_parms (friend_type))
11855 /* friend class C<T>; */
11856 friend_type = tsubst (friend_type, args,
11857 tf_warning_or_error, NULL_TREE);
11858 /* Otherwise it's
11859
11860 friend class C;
11861
11862 where C is already declared or
11863
11864 friend class C<int>;
11865
11866 We don't have to do anything in these cases. */
11867
11868 if (adjust_processing_template_decl)
11869 /* Trick make_friend_class into realizing that the friend
11870 we're adding is a template, not an ordinary class. It's
11871 important that we use make_friend_class since it will
11872 perform some error-checking and output cross-reference
11873 information. */
11874 ++processing_template_decl;
11875
11876 if (friend_type != error_mark_node)
11877 make_friend_class (type, friend_type, /*complain=*/false);
11878
11879 if (adjust_processing_template_decl)
11880 --processing_template_decl;
11881 }
11882 else
11883 {
11884 /* Build new DECL_FRIENDLIST. */
11885 tree r;
11886
11887 /* The file and line for this declaration, to
11888 assist in error message reporting. Since we
11889 called push_tinst_level above, we don't need to
11890 restore these. */
11891 input_location = DECL_SOURCE_LOCATION (t);
11892
11893 if (TREE_CODE (t) == TEMPLATE_DECL)
11894 {
11895 ++processing_template_decl;
11896 push_deferring_access_checks (dk_no_check);
11897 }
11898
11899 r = tsubst_friend_function (t, args);
11900 add_friend (type, r, /*complain=*/false);
11901 if (TREE_CODE (t) == TEMPLATE_DECL)
11902 {
11903 pop_deferring_access_checks ();
11904 --processing_template_decl;
11905 }
11906 }
11907 }
11908 }
11909
11910 if (fn_context)
11911 {
11912 /* Restore these before substituting into the lambda capture
11913 initializers. */
11914 cp_unevaluated_operand = saved_unevaluated_operand;
11915 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11916 }
11917
11918 /* Set the file and line number information to whatever is given for
11919 the class itself. This puts error messages involving generated
11920 implicit functions at a predictable point, and the same point
11921 that would be used for non-template classes. */
11922 input_location = DECL_SOURCE_LOCATION (typedecl);
11923
11924 unreverse_member_declarations (type);
11925 finish_struct_1 (type);
11926 TYPE_BEING_DEFINED (type) = 0;
11927
11928 /* We don't instantiate default arguments for member functions. 14.7.1:
11929
11930 The implicit instantiation of a class template specialization causes
11931 the implicit instantiation of the declarations, but not of the
11932 definitions or default arguments, of the class member functions,
11933 member classes, static data members and member templates.... */
11934
11935 /* Some typedefs referenced from within the template code need to be access
11936 checked at template instantiation time, i.e now. These types were
11937 added to the template at parsing time. Let's get those and perform
11938 the access checks then. */
11939 perform_typedefs_access_check (pattern, args);
11940 perform_deferred_access_checks (tf_warning_or_error);
11941 pop_nested_class ();
11942 maximum_field_alignment = saved_maximum_field_alignment;
11943 if (!fn_context)
11944 pop_from_top_level ();
11945 pop_tinst_level ();
11946
11947 /* The vtable for a template class can be emitted in any translation
11948 unit in which the class is instantiated. When there is no key
11949 method, however, finish_struct_1 will already have added TYPE to
11950 the keyed_classes. */
11951 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11952 vec_safe_push (keyed_classes, type);
11953
11954 return type;
11955 }
11956
11957 /* Wrapper for instantiate_class_template_1. */
11958
11959 tree
11960 instantiate_class_template (tree type)
11961 {
11962 tree ret;
11963 timevar_push (TV_TEMPLATE_INST);
11964 ret = instantiate_class_template_1 (type);
11965 timevar_pop (TV_TEMPLATE_INST);
11966 return ret;
11967 }
11968
11969 tree
11970 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11971 {
11972 tree r;
11973
11974 if (!t)
11975 r = t;
11976 else if (TYPE_P (t))
11977 r = tsubst (t, args, complain, in_decl);
11978 else
11979 {
11980 if (!(complain & tf_warning))
11981 ++c_inhibit_evaluation_warnings;
11982 r = tsubst_expr (t, args, complain, in_decl,
11983 /*integral_constant_expression_p=*/true);
11984 if (!(complain & tf_warning))
11985 --c_inhibit_evaluation_warnings;
11986 }
11987
11988 return r;
11989 }
11990
11991 /* Given a function parameter pack TMPL_PARM and some function parameters
11992 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11993 and set *SPEC_P to point at the next point in the list. */
11994
11995 tree
11996 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11997 {
11998 /* Collect all of the extra "packed" parameters into an
11999 argument pack. */
12000 tree parmvec;
12001 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
12002 tree spec_parm = *spec_p;
12003 int i, len;
12004
12005 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12006 if (tmpl_parm
12007 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12008 break;
12009
12010 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
12011 parmvec = make_tree_vec (len);
12012 spec_parm = *spec_p;
12013 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
12014 {
12015 tree elt = spec_parm;
12016 if (DECL_PACK_P (elt))
12017 elt = make_pack_expansion (elt);
12018 TREE_VEC_ELT (parmvec, i) = elt;
12019 }
12020
12021 /* Build the argument packs. */
12022 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12023 *spec_p = spec_parm;
12024
12025 return argpack;
12026 }
12027
12028 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12029 NONTYPE_ARGUMENT_PACK. */
12030
12031 static tree
12032 make_fnparm_pack (tree spec_parm)
12033 {
12034 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12035 }
12036
12037 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12038 pack expansion with no extra args, 2 if it has extra args, or 0
12039 if it is not a pack expansion. */
12040
12041 static int
12042 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12043 {
12044 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12045 /* We're being called before this happens in tsubst_pack_expansion. */
12046 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12047 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12048 if (i >= TREE_VEC_LENGTH (vec))
12049 return 0;
12050 tree elt = TREE_VEC_ELT (vec, i);
12051 if (DECL_P (elt))
12052 /* A decl pack is itself an expansion. */
12053 elt = TREE_TYPE (elt);
12054 if (!PACK_EXPANSION_P (elt))
12055 return 0;
12056 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12057 return 2;
12058 return 1;
12059 }
12060
12061
12062 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12063
12064 static tree
12065 make_argument_pack_select (tree arg_pack, unsigned index)
12066 {
12067 tree aps = make_node (ARGUMENT_PACK_SELECT);
12068
12069 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12070 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12071
12072 return aps;
12073 }
12074
12075 /* This is a subroutine of tsubst_pack_expansion.
12076
12077 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12078 mechanism to store the (non complete list of) arguments of the
12079 substitution and return a non substituted pack expansion, in order
12080 to wait for when we have enough arguments to really perform the
12081 substitution. */
12082
12083 static bool
12084 use_pack_expansion_extra_args_p (tree parm_packs,
12085 int arg_pack_len,
12086 bool has_empty_arg)
12087 {
12088 /* If one pack has an expansion and another pack has a normal
12089 argument or if one pack has an empty argument and an another
12090 one hasn't then tsubst_pack_expansion cannot perform the
12091 substitution and need to fall back on the
12092 PACK_EXPANSION_EXTRA mechanism. */
12093 if (parm_packs == NULL_TREE)
12094 return false;
12095 else if (has_empty_arg)
12096 {
12097 /* If all the actual packs are pack expansions, we can still
12098 subsitute directly. */
12099 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12100 {
12101 tree a = TREE_VALUE (p);
12102 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12103 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12104 a = ARGUMENT_PACK_ARGS (a);
12105 if (TREE_VEC_LENGTH (a) == 1)
12106 a = TREE_VEC_ELT (a, 0);
12107 if (PACK_EXPANSION_P (a))
12108 continue;
12109 return true;
12110 }
12111 return false;
12112 }
12113
12114 bool has_expansion_arg = false;
12115 for (int i = 0 ; i < arg_pack_len; ++i)
12116 {
12117 bool has_non_expansion_arg = false;
12118 for (tree parm_pack = parm_packs;
12119 parm_pack;
12120 parm_pack = TREE_CHAIN (parm_pack))
12121 {
12122 tree arg = TREE_VALUE (parm_pack);
12123
12124 int exp = argument_pack_element_is_expansion_p (arg, i);
12125 if (exp == 2)
12126 /* We can't substitute a pack expansion with extra args into
12127 our pattern. */
12128 return true;
12129 else if (exp)
12130 has_expansion_arg = true;
12131 else
12132 has_non_expansion_arg = true;
12133 }
12134
12135 if (has_expansion_arg && has_non_expansion_arg)
12136 return true;
12137 }
12138 return false;
12139 }
12140
12141 /* [temp.variadic]/6 says that:
12142
12143 The instantiation of a pack expansion [...]
12144 produces a list E1,E2, ..., En, where N is the number of elements
12145 in the pack expansion parameters.
12146
12147 This subroutine of tsubst_pack_expansion produces one of these Ei.
12148
12149 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12150 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12151 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12152 INDEX is the index 'i' of the element Ei to produce. ARGS,
12153 COMPLAIN, and IN_DECL are the same parameters as for the
12154 tsubst_pack_expansion function.
12155
12156 The function returns the resulting Ei upon successful completion,
12157 or error_mark_node.
12158
12159 Note that this function possibly modifies the ARGS parameter, so
12160 it's the responsibility of the caller to restore it. */
12161
12162 static tree
12163 gen_elem_of_pack_expansion_instantiation (tree pattern,
12164 tree parm_packs,
12165 unsigned index,
12166 tree args /* This parm gets
12167 modified. */,
12168 tsubst_flags_t complain,
12169 tree in_decl)
12170 {
12171 tree t;
12172 bool ith_elem_is_expansion = false;
12173
12174 /* For each parameter pack, change the substitution of the parameter
12175 pack to the ith argument in its argument pack, then expand the
12176 pattern. */
12177 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12178 {
12179 tree parm = TREE_PURPOSE (pack);
12180 tree arg_pack = TREE_VALUE (pack);
12181 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12182
12183 ith_elem_is_expansion |=
12184 argument_pack_element_is_expansion_p (arg_pack, index);
12185
12186 /* Select the Ith argument from the pack. */
12187 if (TREE_CODE (parm) == PARM_DECL
12188 || VAR_P (parm)
12189 || TREE_CODE (parm) == FIELD_DECL)
12190 {
12191 if (index == 0)
12192 {
12193 aps = make_argument_pack_select (arg_pack, index);
12194 if (!mark_used (parm, complain) && !(complain & tf_error))
12195 return error_mark_node;
12196 register_local_specialization (aps, parm);
12197 }
12198 else
12199 aps = retrieve_local_specialization (parm);
12200 }
12201 else
12202 {
12203 int idx, level;
12204 template_parm_level_and_index (parm, &level, &idx);
12205
12206 if (index == 0)
12207 {
12208 aps = make_argument_pack_select (arg_pack, index);
12209 /* Update the corresponding argument. */
12210 TMPL_ARG (args, level, idx) = aps;
12211 }
12212 else
12213 /* Re-use the ARGUMENT_PACK_SELECT. */
12214 aps = TMPL_ARG (args, level, idx);
12215 }
12216 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12217 }
12218
12219 /* Substitute into the PATTERN with the (possibly altered)
12220 arguments. */
12221 if (pattern == in_decl)
12222 /* Expanding a fixed parameter pack from
12223 coerce_template_parameter_pack. */
12224 t = tsubst_decl (pattern, args, complain);
12225 else if (pattern == error_mark_node)
12226 t = error_mark_node;
12227 else if (!TYPE_P (pattern))
12228 t = tsubst_expr (pattern, args, complain, in_decl,
12229 /*integral_constant_expression_p=*/false);
12230 else
12231 t = tsubst (pattern, args, complain, in_decl);
12232
12233 /* If the Ith argument pack element is a pack expansion, then
12234 the Ith element resulting from the substituting is going to
12235 be a pack expansion as well. */
12236 if (ith_elem_is_expansion)
12237 t = make_pack_expansion (t, complain);
12238
12239 return t;
12240 }
12241
12242 /* When the unexpanded parameter pack in a fold expression expands to an empty
12243 sequence, the value of the expression is as follows; the program is
12244 ill-formed if the operator is not listed in this table.
12245
12246 && true
12247 || false
12248 , void() */
12249
12250 tree
12251 expand_empty_fold (tree t, tsubst_flags_t complain)
12252 {
12253 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12254 if (!FOLD_EXPR_MODIFY_P (t))
12255 switch (code)
12256 {
12257 case TRUTH_ANDIF_EXPR:
12258 return boolean_true_node;
12259 case TRUTH_ORIF_EXPR:
12260 return boolean_false_node;
12261 case COMPOUND_EXPR:
12262 return void_node;
12263 default:
12264 break;
12265 }
12266
12267 if (complain & tf_error)
12268 error_at (location_of (t),
12269 "fold of empty expansion over %O", code);
12270 return error_mark_node;
12271 }
12272
12273 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12274 form an expression that combines the two terms using the
12275 operator of T. */
12276
12277 static tree
12278 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12279 {
12280 tree op = FOLD_EXPR_OP (t);
12281 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12282
12283 // Handle compound assignment operators.
12284 if (FOLD_EXPR_MODIFY_P (t))
12285 return build_x_modify_expr (input_location, left, code, right, complain);
12286
12287 switch (code)
12288 {
12289 case COMPOUND_EXPR:
12290 return build_x_compound_expr (input_location, left, right, complain);
12291 default:
12292 return build_x_binary_op (input_location, code,
12293 left, TREE_CODE (left),
12294 right, TREE_CODE (right),
12295 /*overload=*/NULL,
12296 complain);
12297 }
12298 }
12299
12300 /* Substitute ARGS into the pack of a fold expression T. */
12301
12302 static inline tree
12303 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12304 {
12305 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12306 }
12307
12308 /* Substitute ARGS into the pack of a fold expression T. */
12309
12310 static inline tree
12311 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12312 {
12313 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12314 }
12315
12316 /* Expand a PACK of arguments into a grouped as left fold.
12317 Given a pack containing elements A0, A1, ..., An and an
12318 operator @, this builds the expression:
12319
12320 ((A0 @ A1) @ A2) ... @ An
12321
12322 Note that PACK must not be empty.
12323
12324 The operator is defined by the original fold expression T. */
12325
12326 static tree
12327 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12328 {
12329 tree left = TREE_VEC_ELT (pack, 0);
12330 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12331 {
12332 tree right = TREE_VEC_ELT (pack, i);
12333 left = fold_expression (t, left, right, complain);
12334 }
12335 return left;
12336 }
12337
12338 /* Substitute into a unary left fold expression. */
12339
12340 static tree
12341 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12342 tree in_decl)
12343 {
12344 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12345 if (pack == error_mark_node)
12346 return error_mark_node;
12347 if (PACK_EXPANSION_P (pack))
12348 {
12349 tree r = copy_node (t);
12350 FOLD_EXPR_PACK (r) = pack;
12351 return r;
12352 }
12353 if (TREE_VEC_LENGTH (pack) == 0)
12354 return expand_empty_fold (t, complain);
12355 else
12356 return expand_left_fold (t, pack, complain);
12357 }
12358
12359 /* Substitute into a binary left fold expression.
12360
12361 Do ths by building a single (non-empty) vector of argumnts and
12362 building the expression from those elements. */
12363
12364 static tree
12365 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12366 tree in_decl)
12367 {
12368 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12369 if (pack == error_mark_node)
12370 return error_mark_node;
12371 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12372 if (init == error_mark_node)
12373 return error_mark_node;
12374
12375 if (PACK_EXPANSION_P (pack))
12376 {
12377 tree r = copy_node (t);
12378 FOLD_EXPR_PACK (r) = pack;
12379 FOLD_EXPR_INIT (r) = init;
12380 return r;
12381 }
12382
12383 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12384 TREE_VEC_ELT (vec, 0) = init;
12385 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12386 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12387
12388 return expand_left_fold (t, vec, complain);
12389 }
12390
12391 /* Expand a PACK of arguments into a grouped as right fold.
12392 Given a pack containing elementns A0, A1, ..., and an
12393 operator @, this builds the expression:
12394
12395 A0@ ... (An-2 @ (An-1 @ An))
12396
12397 Note that PACK must not be empty.
12398
12399 The operator is defined by the original fold expression T. */
12400
12401 tree
12402 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12403 {
12404 // Build the expression.
12405 int n = TREE_VEC_LENGTH (pack);
12406 tree right = TREE_VEC_ELT (pack, n - 1);
12407 for (--n; n != 0; --n)
12408 {
12409 tree left = TREE_VEC_ELT (pack, n - 1);
12410 right = fold_expression (t, left, right, complain);
12411 }
12412 return right;
12413 }
12414
12415 /* Substitute into a unary right fold expression. */
12416
12417 static tree
12418 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12419 tree in_decl)
12420 {
12421 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12422 if (pack == error_mark_node)
12423 return error_mark_node;
12424 if (PACK_EXPANSION_P (pack))
12425 {
12426 tree r = copy_node (t);
12427 FOLD_EXPR_PACK (r) = pack;
12428 return r;
12429 }
12430 if (TREE_VEC_LENGTH (pack) == 0)
12431 return expand_empty_fold (t, complain);
12432 else
12433 return expand_right_fold (t, pack, complain);
12434 }
12435
12436 /* Substitute into a binary right fold expression.
12437
12438 Do ths by building a single (non-empty) vector of arguments and
12439 building the expression from those elements. */
12440
12441 static tree
12442 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12443 tree in_decl)
12444 {
12445 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12446 if (pack == error_mark_node)
12447 return error_mark_node;
12448 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12449 if (init == error_mark_node)
12450 return error_mark_node;
12451
12452 if (PACK_EXPANSION_P (pack))
12453 {
12454 tree r = copy_node (t);
12455 FOLD_EXPR_PACK (r) = pack;
12456 FOLD_EXPR_INIT (r) = init;
12457 return r;
12458 }
12459
12460 int n = TREE_VEC_LENGTH (pack);
12461 tree vec = make_tree_vec (n + 1);
12462 for (int i = 0; i < n; ++i)
12463 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12464 TREE_VEC_ELT (vec, n) = init;
12465
12466 return expand_right_fold (t, vec, complain);
12467 }
12468
12469 /* Walk through the pattern of a pack expansion, adding everything in
12470 local_specializations to a list. */
12471
12472 class el_data
12473 {
12474 public:
12475 hash_set<tree> internal;
12476 tree extra;
12477 tsubst_flags_t complain;
12478
12479 el_data (tsubst_flags_t c)
12480 : extra (NULL_TREE), complain (c) {}
12481 };
12482 static tree
12483 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12484 {
12485 el_data &data = *reinterpret_cast<el_data*>(data_);
12486 tree *extra = &data.extra;
12487 tsubst_flags_t complain = data.complain;
12488
12489 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12490 /* Remember local typedefs (85214). */
12491 tp = &TYPE_NAME (*tp);
12492
12493 if (TREE_CODE (*tp) == DECL_EXPR)
12494 data.internal.add (DECL_EXPR_DECL (*tp));
12495 else if (tree spec = retrieve_local_specialization (*tp))
12496 {
12497 if (data.internal.contains (*tp))
12498 /* Don't mess with variables declared within the pattern. */
12499 return NULL_TREE;
12500 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12501 {
12502 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12503 tree args = ARGUMENT_PACK_ARGS (spec);
12504 if (TREE_VEC_LENGTH (args) == 1)
12505 {
12506 tree elt = TREE_VEC_ELT (args, 0);
12507 if (PACK_EXPANSION_P (elt))
12508 elt = PACK_EXPANSION_PATTERN (elt);
12509 if (DECL_PACK_P (elt))
12510 spec = elt;
12511 }
12512 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12513 {
12514 /* Handle lambda capture here, since we aren't doing any
12515 substitution now, and so tsubst_copy won't call
12516 process_outer_var_ref. */
12517 tree args = ARGUMENT_PACK_ARGS (spec);
12518 int len = TREE_VEC_LENGTH (args);
12519 for (int i = 0; i < len; ++i)
12520 {
12521 tree arg = TREE_VEC_ELT (args, i);
12522 tree carg = arg;
12523 if (outer_automatic_var_p (arg))
12524 carg = process_outer_var_ref (arg, complain);
12525 if (carg != arg)
12526 {
12527 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12528 proxies. */
12529 if (i == 0)
12530 {
12531 spec = copy_node (spec);
12532 args = copy_node (args);
12533 SET_ARGUMENT_PACK_ARGS (spec, args);
12534 register_local_specialization (spec, *tp);
12535 }
12536 TREE_VEC_ELT (args, i) = carg;
12537 }
12538 }
12539 }
12540 }
12541 if (outer_automatic_var_p (spec))
12542 spec = process_outer_var_ref (spec, complain);
12543 *extra = tree_cons (*tp, spec, *extra);
12544 }
12545 return NULL_TREE;
12546 }
12547 static tree
12548 extract_local_specs (tree pattern, tsubst_flags_t complain)
12549 {
12550 el_data data (complain);
12551 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12552 return data.extra;
12553 }
12554
12555 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12556 for use in PACK_EXPANSION_EXTRA_ARGS. */
12557
12558 tree
12559 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12560 {
12561 tree extra = args;
12562 if (local_specializations)
12563 if (tree locals = extract_local_specs (pattern, complain))
12564 extra = tree_cons (NULL_TREE, extra, locals);
12565 return extra;
12566 }
12567
12568 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12569 normal template args to ARGS. */
12570
12571 tree
12572 add_extra_args (tree extra, tree args)
12573 {
12574 if (extra && TREE_CODE (extra) == TREE_LIST)
12575 {
12576 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12577 {
12578 /* The partial instantiation involved local declarations collected in
12579 extract_local_specs; map from the general template to our local
12580 context. */
12581 tree gen = TREE_PURPOSE (elt);
12582 tree inst = TREE_VALUE (elt);
12583 if (DECL_P (inst))
12584 if (tree local = retrieve_local_specialization (inst))
12585 inst = local;
12586 /* else inst is already a full instantiation of the pack. */
12587 register_local_specialization (inst, gen);
12588 }
12589 gcc_assert (!TREE_PURPOSE (extra));
12590 extra = TREE_VALUE (extra);
12591 }
12592 #if 1
12593 /* I think we should always be able to substitute dependent args into the
12594 pattern. If that turns out to be incorrect in some cases, enable the
12595 alternate code (and add complain/in_decl parms to this function). */
12596 gcc_checking_assert (!uses_template_parms (extra));
12597 #else
12598 if (!uses_template_parms (extra))
12599 {
12600 gcc_unreachable ();
12601 extra = tsubst_template_args (extra, args, complain, in_decl);
12602 args = add_outermost_template_args (args, extra);
12603 }
12604 else
12605 #endif
12606 args = add_to_template_args (extra, args);
12607 return args;
12608 }
12609
12610 /* Substitute ARGS into T, which is an pack expansion
12611 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12612 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12613 (if only a partial substitution could be performed) or
12614 ERROR_MARK_NODE if there was an error. */
12615 tree
12616 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12617 tree in_decl)
12618 {
12619 tree pattern;
12620 tree pack, packs = NULL_TREE;
12621 bool unsubstituted_packs = false;
12622 bool unsubstituted_fn_pack = false;
12623 int i, len = -1;
12624 tree result;
12625 hash_map<tree, tree> *saved_local_specializations = NULL;
12626 bool need_local_specializations = false;
12627 int levels;
12628
12629 gcc_assert (PACK_EXPANSION_P (t));
12630 pattern = PACK_EXPANSION_PATTERN (t);
12631
12632 /* Add in any args remembered from an earlier partial instantiation. */
12633 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12634
12635 levels = TMPL_ARGS_DEPTH (args);
12636
12637 /* Determine the argument packs that will instantiate the parameter
12638 packs used in the expansion expression. While we're at it,
12639 compute the number of arguments to be expanded and make sure it
12640 is consistent. */
12641 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12642 pack = TREE_CHAIN (pack))
12643 {
12644 tree parm_pack = TREE_VALUE (pack);
12645 tree arg_pack = NULL_TREE;
12646 tree orig_arg = NULL_TREE;
12647 int level = 0;
12648
12649 if (TREE_CODE (parm_pack) == BASES)
12650 {
12651 gcc_assert (parm_pack == pattern);
12652 if (BASES_DIRECT (parm_pack))
12653 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12654 args, complain,
12655 in_decl, false),
12656 complain);
12657 else
12658 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12659 args, complain, in_decl,
12660 false), complain);
12661 }
12662 else if (builtin_pack_call_p (parm_pack))
12663 {
12664 if (parm_pack != pattern)
12665 {
12666 if (complain & tf_error)
12667 sorry ("%qE is not the entire pattern of the pack expansion",
12668 parm_pack);
12669 return error_mark_node;
12670 }
12671 return expand_builtin_pack_call (parm_pack, args,
12672 complain, in_decl);
12673 }
12674 else if (TREE_CODE (parm_pack) == PARM_DECL)
12675 {
12676 /* We know we have correct local_specializations if this
12677 expansion is at function scope, or if we're dealing with a
12678 local parameter in a requires expression; for the latter,
12679 tsubst_requires_expr set it up appropriately. */
12680 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12681 arg_pack = retrieve_local_specialization (parm_pack);
12682 else
12683 /* We can't rely on local_specializations for a parameter
12684 name used later in a function declaration (such as in a
12685 late-specified return type). Even if it exists, it might
12686 have the wrong value for a recursive call. */
12687 need_local_specializations = true;
12688
12689 if (!arg_pack)
12690 {
12691 /* This parameter pack was used in an unevaluated context. Just
12692 make a dummy decl, since it's only used for its type. */
12693 ++cp_unevaluated_operand;
12694 arg_pack = tsubst_decl (parm_pack, args, complain);
12695 --cp_unevaluated_operand;
12696 if (arg_pack && DECL_PACK_P (arg_pack))
12697 /* Partial instantiation of the parm_pack, we can't build
12698 up an argument pack yet. */
12699 arg_pack = NULL_TREE;
12700 else
12701 arg_pack = make_fnparm_pack (arg_pack);
12702 }
12703 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12704 /* This argument pack isn't fully instantiated yet. We set this
12705 flag rather than clear arg_pack because we do want to do the
12706 optimization below, and we don't want to substitute directly
12707 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12708 where it isn't expected). */
12709 unsubstituted_fn_pack = true;
12710 }
12711 else if (is_capture_proxy (parm_pack))
12712 {
12713 arg_pack = retrieve_local_specialization (parm_pack);
12714 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12715 unsubstituted_fn_pack = true;
12716 }
12717 else
12718 {
12719 int idx;
12720 template_parm_level_and_index (parm_pack, &level, &idx);
12721 if (level <= levels)
12722 arg_pack = TMPL_ARG (args, level, idx);
12723 }
12724
12725 orig_arg = arg_pack;
12726 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12727 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12728
12729 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12730 /* This can only happen if we forget to expand an argument
12731 pack somewhere else. Just return an error, silently. */
12732 {
12733 result = make_tree_vec (1);
12734 TREE_VEC_ELT (result, 0) = error_mark_node;
12735 return result;
12736 }
12737
12738 if (arg_pack)
12739 {
12740 int my_len =
12741 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12742
12743 /* Don't bother trying to do a partial substitution with
12744 incomplete packs; we'll try again after deduction. */
12745 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12746 return t;
12747
12748 if (len < 0)
12749 len = my_len;
12750 else if (len != my_len
12751 && !unsubstituted_fn_pack)
12752 {
12753 if (!(complain & tf_error))
12754 /* Fail quietly. */;
12755 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12756 error ("mismatched argument pack lengths while expanding %qT",
12757 pattern);
12758 else
12759 error ("mismatched argument pack lengths while expanding %qE",
12760 pattern);
12761 return error_mark_node;
12762 }
12763
12764 /* Keep track of the parameter packs and their corresponding
12765 argument packs. */
12766 packs = tree_cons (parm_pack, arg_pack, packs);
12767 TREE_TYPE (packs) = orig_arg;
12768 }
12769 else
12770 {
12771 /* We can't substitute for this parameter pack. We use a flag as
12772 well as the missing_level counter because function parameter
12773 packs don't have a level. */
12774 if (!(processing_template_decl || is_auto (parm_pack)))
12775 {
12776 gcc_unreachable ();
12777 }
12778 gcc_assert (processing_template_decl || is_auto (parm_pack));
12779 unsubstituted_packs = true;
12780 }
12781 }
12782
12783 /* If the expansion is just T..., return the matching argument pack, unless
12784 we need to call convert_from_reference on all the elements. This is an
12785 important optimization; see c++/68422. */
12786 if (!unsubstituted_packs
12787 && TREE_PURPOSE (packs) == pattern)
12788 {
12789 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12790
12791 /* If the argument pack is a single pack expansion, pull it out. */
12792 if (TREE_VEC_LENGTH (args) == 1
12793 && pack_expansion_args_count (args))
12794 return TREE_VEC_ELT (args, 0);
12795
12796 /* Types need no adjustment, nor does sizeof..., and if we still have
12797 some pack expansion args we won't do anything yet. */
12798 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12799 || PACK_EXPANSION_SIZEOF_P (t)
12800 || pack_expansion_args_count (args))
12801 return args;
12802 /* Also optimize expression pack expansions if we can tell that the
12803 elements won't have reference type. */
12804 tree type = TREE_TYPE (pattern);
12805 if (type && !TYPE_REF_P (type)
12806 && !PACK_EXPANSION_P (type)
12807 && !WILDCARD_TYPE_P (type))
12808 return args;
12809 /* Otherwise use the normal path so we get convert_from_reference. */
12810 }
12811
12812 /* We cannot expand this expansion expression, because we don't have
12813 all of the argument packs we need. */
12814 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12815 {
12816 /* We got some full packs, but we can't substitute them in until we
12817 have values for all the packs. So remember these until then. */
12818
12819 t = make_pack_expansion (pattern, complain);
12820 PACK_EXPANSION_EXTRA_ARGS (t)
12821 = build_extra_args (pattern, args, complain);
12822 return t;
12823 }
12824 else if (unsubstituted_packs)
12825 {
12826 /* There were no real arguments, we're just replacing a parameter
12827 pack with another version of itself. Substitute into the
12828 pattern and return a PACK_EXPANSION_*. The caller will need to
12829 deal with that. */
12830 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12831 t = tsubst_expr (pattern, args, complain, in_decl,
12832 /*integral_constant_expression_p=*/false);
12833 else
12834 t = tsubst (pattern, args, complain, in_decl);
12835 t = make_pack_expansion (t, complain);
12836 return t;
12837 }
12838
12839 gcc_assert (len >= 0);
12840
12841 if (need_local_specializations)
12842 {
12843 /* We're in a late-specified return type, so create our own local
12844 specializations map; the current map is either NULL or (in the
12845 case of recursive unification) might have bindings that we don't
12846 want to use or alter. */
12847 saved_local_specializations = local_specializations;
12848 local_specializations = new hash_map<tree, tree>;
12849 }
12850
12851 /* For each argument in each argument pack, substitute into the
12852 pattern. */
12853 result = make_tree_vec (len);
12854 tree elem_args = copy_template_args (args);
12855 for (i = 0; i < len; ++i)
12856 {
12857 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12858 i,
12859 elem_args, complain,
12860 in_decl);
12861 TREE_VEC_ELT (result, i) = t;
12862 if (t == error_mark_node)
12863 {
12864 result = error_mark_node;
12865 break;
12866 }
12867 }
12868
12869 /* Update ARGS to restore the substitution from parameter packs to
12870 their argument packs. */
12871 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12872 {
12873 tree parm = TREE_PURPOSE (pack);
12874
12875 if (TREE_CODE (parm) == PARM_DECL
12876 || VAR_P (parm)
12877 || TREE_CODE (parm) == FIELD_DECL)
12878 register_local_specialization (TREE_TYPE (pack), parm);
12879 else
12880 {
12881 int idx, level;
12882
12883 if (TREE_VALUE (pack) == NULL_TREE)
12884 continue;
12885
12886 template_parm_level_and_index (parm, &level, &idx);
12887
12888 /* Update the corresponding argument. */
12889 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12890 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12891 TREE_TYPE (pack);
12892 else
12893 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12894 }
12895 }
12896
12897 if (need_local_specializations)
12898 {
12899 delete local_specializations;
12900 local_specializations = saved_local_specializations;
12901 }
12902
12903 /* If the dependent pack arguments were such that we end up with only a
12904 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12905 if (len == 1 && TREE_CODE (result) == TREE_VEC
12906 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12907 return TREE_VEC_ELT (result, 0);
12908
12909 return result;
12910 }
12911
12912 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12913 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12914 parameter packs; all parms generated from a function parameter pack will
12915 have the same DECL_PARM_INDEX. */
12916
12917 tree
12918 get_pattern_parm (tree parm, tree tmpl)
12919 {
12920 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12921 tree patparm;
12922
12923 if (DECL_ARTIFICIAL (parm))
12924 {
12925 for (patparm = DECL_ARGUMENTS (pattern);
12926 patparm; patparm = DECL_CHAIN (patparm))
12927 if (DECL_ARTIFICIAL (patparm)
12928 && DECL_NAME (parm) == DECL_NAME (patparm))
12929 break;
12930 }
12931 else
12932 {
12933 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12934 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12935 gcc_assert (DECL_PARM_INDEX (patparm)
12936 == DECL_PARM_INDEX (parm));
12937 }
12938
12939 return patparm;
12940 }
12941
12942 /* Make an argument pack out of the TREE_VEC VEC. */
12943
12944 static tree
12945 make_argument_pack (tree vec)
12946 {
12947 tree pack;
12948 tree elt = TREE_VEC_ELT (vec, 0);
12949 if (TYPE_P (elt))
12950 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12951 else
12952 {
12953 pack = make_node (NONTYPE_ARGUMENT_PACK);
12954 TREE_CONSTANT (pack) = 1;
12955 }
12956 SET_ARGUMENT_PACK_ARGS (pack, vec);
12957 return pack;
12958 }
12959
12960 /* Return an exact copy of template args T that can be modified
12961 independently. */
12962
12963 static tree
12964 copy_template_args (tree t)
12965 {
12966 if (t == error_mark_node)
12967 return t;
12968
12969 int len = TREE_VEC_LENGTH (t);
12970 tree new_vec = make_tree_vec (len);
12971
12972 for (int i = 0; i < len; ++i)
12973 {
12974 tree elt = TREE_VEC_ELT (t, i);
12975 if (elt && TREE_CODE (elt) == TREE_VEC)
12976 elt = copy_template_args (elt);
12977 TREE_VEC_ELT (new_vec, i) = elt;
12978 }
12979
12980 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12981 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12982
12983 return new_vec;
12984 }
12985
12986 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
12987
12988 tree
12989 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
12990 tree in_decl)
12991 {
12992 /* Substitute into each of the arguments. */
12993 tree new_arg = TYPE_P (orig_arg)
12994 ? cxx_make_type (TREE_CODE (orig_arg))
12995 : make_node (TREE_CODE (orig_arg));
12996
12997 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12998 args, complain, in_decl);
12999 if (pack_args == error_mark_node)
13000 new_arg = error_mark_node;
13001 else
13002 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13003
13004 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
13005 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13006
13007 return new_arg;
13008 }
13009
13010 /* Substitute ARGS into the vector or list of template arguments T. */
13011
13012 tree
13013 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13014 {
13015 tree orig_t = t;
13016 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13017 tree *elts;
13018
13019 if (t == error_mark_node)
13020 return error_mark_node;
13021
13022 len = TREE_VEC_LENGTH (t);
13023 elts = XALLOCAVEC (tree, len);
13024
13025 for (i = 0; i < len; i++)
13026 {
13027 tree orig_arg = TREE_VEC_ELT (t, i);
13028 tree new_arg;
13029
13030 if (TREE_CODE (orig_arg) == TREE_VEC)
13031 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13032 else if (PACK_EXPANSION_P (orig_arg))
13033 {
13034 /* Substitute into an expansion expression. */
13035 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13036
13037 if (TREE_CODE (new_arg) == TREE_VEC)
13038 /* Add to the expanded length adjustment the number of
13039 expanded arguments. We subtract one from this
13040 measurement, because the argument pack expression
13041 itself is already counted as 1 in
13042 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13043 the argument pack is empty. */
13044 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13045 }
13046 else if (ARGUMENT_PACK_P (orig_arg))
13047 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13048 else
13049 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13050
13051 if (new_arg == error_mark_node)
13052 return error_mark_node;
13053
13054 elts[i] = new_arg;
13055 if (new_arg != orig_arg)
13056 need_new = 1;
13057 }
13058
13059 if (!need_new)
13060 return t;
13061
13062 /* Make space for the expanded arguments coming from template
13063 argument packs. */
13064 t = make_tree_vec (len + expanded_len_adjust);
13065 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13066 arguments for a member template.
13067 In that case each TREE_VEC in ORIG_T represents a level of template
13068 arguments, and ORIG_T won't carry any non defaulted argument count.
13069 It will rather be the nested TREE_VECs that will carry one.
13070 In other words, ORIG_T carries a non defaulted argument count only
13071 if it doesn't contain any nested TREE_VEC. */
13072 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13073 {
13074 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13075 count += expanded_len_adjust;
13076 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13077 }
13078 for (i = 0, out = 0; i < len; i++)
13079 {
13080 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13081 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13082 && TREE_CODE (elts[i]) == TREE_VEC)
13083 {
13084 int idx;
13085
13086 /* Now expand the template argument pack "in place". */
13087 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13088 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13089 }
13090 else
13091 {
13092 TREE_VEC_ELT (t, out) = elts[i];
13093 out++;
13094 }
13095 }
13096
13097 return t;
13098 }
13099
13100 /* Substitute ARGS into one level PARMS of template parameters. */
13101
13102 static tree
13103 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13104 {
13105 if (parms == error_mark_node)
13106 return error_mark_node;
13107
13108 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13109
13110 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13111 {
13112 tree tuple = TREE_VEC_ELT (parms, i);
13113
13114 if (tuple == error_mark_node)
13115 continue;
13116
13117 TREE_VEC_ELT (new_vec, i) =
13118 tsubst_template_parm (tuple, args, complain);
13119 }
13120
13121 return new_vec;
13122 }
13123
13124 /* Return the result of substituting ARGS into the template parameters
13125 given by PARMS. If there are m levels of ARGS and m + n levels of
13126 PARMS, then the result will contain n levels of PARMS. For
13127 example, if PARMS is `template <class T> template <class U>
13128 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13129 result will be `template <int*, double, class V>'. */
13130
13131 static tree
13132 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13133 {
13134 tree r = NULL_TREE;
13135 tree* new_parms;
13136
13137 /* When substituting into a template, we must set
13138 PROCESSING_TEMPLATE_DECL as the template parameters may be
13139 dependent if they are based on one-another, and the dependency
13140 predicates are short-circuit outside of templates. */
13141 ++processing_template_decl;
13142
13143 for (new_parms = &r;
13144 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13145 new_parms = &(TREE_CHAIN (*new_parms)),
13146 parms = TREE_CHAIN (parms))
13147 {
13148 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13149 args, complain);
13150 *new_parms =
13151 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13152 - TMPL_ARGS_DEPTH (args)),
13153 new_vec, NULL_TREE);
13154 }
13155
13156 --processing_template_decl;
13157
13158 return r;
13159 }
13160
13161 /* Return the result of substituting ARGS into one template parameter
13162 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13163 parameter and which TREE_PURPOSE is the default argument of the
13164 template parameter. */
13165
13166 static tree
13167 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13168 {
13169 tree default_value, parm_decl;
13170
13171 if (args == NULL_TREE
13172 || t == NULL_TREE
13173 || t == error_mark_node)
13174 return t;
13175
13176 gcc_assert (TREE_CODE (t) == TREE_LIST);
13177
13178 default_value = TREE_PURPOSE (t);
13179 parm_decl = TREE_VALUE (t);
13180 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13181
13182 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13183 if (TREE_CODE (parm_decl) == PARM_DECL
13184 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13185 parm_decl = error_mark_node;
13186 default_value = tsubst_template_arg (default_value, args,
13187 complain, NULL_TREE);
13188 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13189
13190 tree r = build_tree_list (default_value, parm_decl);
13191 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13192 return r;
13193 }
13194
13195 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13196 type T. If T is not an aggregate or enumeration type, it is
13197 handled as if by tsubst. IN_DECL is as for tsubst. If
13198 ENTERING_SCOPE is nonzero, T is the context for a template which
13199 we are presently tsubst'ing. Return the substituted value. */
13200
13201 static tree
13202 tsubst_aggr_type (tree t,
13203 tree args,
13204 tsubst_flags_t complain,
13205 tree in_decl,
13206 int entering_scope)
13207 {
13208 if (t == NULL_TREE)
13209 return NULL_TREE;
13210
13211 switch (TREE_CODE (t))
13212 {
13213 case RECORD_TYPE:
13214 if (TYPE_PTRMEMFUNC_P (t))
13215 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13216
13217 /* Fall through. */
13218 case ENUMERAL_TYPE:
13219 case UNION_TYPE:
13220 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13221 {
13222 tree argvec;
13223 tree context;
13224 tree r;
13225
13226 /* In "sizeof(X<I>)" we need to evaluate "I". */
13227 cp_evaluated ev;
13228
13229 /* First, determine the context for the type we are looking
13230 up. */
13231 context = TYPE_CONTEXT (t);
13232 if (context && TYPE_P (context))
13233 {
13234 context = tsubst_aggr_type (context, args, complain,
13235 in_decl, /*entering_scope=*/1);
13236 /* If context is a nested class inside a class template,
13237 it may still need to be instantiated (c++/33959). */
13238 context = complete_type (context);
13239 }
13240
13241 /* Then, figure out what arguments are appropriate for the
13242 type we are trying to find. For example, given:
13243
13244 template <class T> struct S;
13245 template <class T, class U> void f(T, U) { S<U> su; }
13246
13247 and supposing that we are instantiating f<int, double>,
13248 then our ARGS will be {int, double}, but, when looking up
13249 S we only want {double}. */
13250 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13251 complain, in_decl);
13252 if (argvec == error_mark_node)
13253 r = error_mark_node;
13254 else
13255 {
13256 r = lookup_template_class (t, argvec, in_decl, context,
13257 entering_scope, complain);
13258 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13259 }
13260
13261 return r;
13262 }
13263 else
13264 /* This is not a template type, so there's nothing to do. */
13265 return t;
13266
13267 default:
13268 return tsubst (t, args, complain, in_decl);
13269 }
13270 }
13271
13272 static GTY((cache)) tree_cache_map *defarg_inst;
13273
13274 /* Substitute into the default argument ARG (a default argument for
13275 FN), which has the indicated TYPE. */
13276
13277 tree
13278 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13279 tsubst_flags_t complain)
13280 {
13281 int errs = errorcount + sorrycount;
13282
13283 /* This can happen in invalid code. */
13284 if (TREE_CODE (arg) == DEFERRED_PARSE)
13285 return arg;
13286
13287 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13288 parm = chain_index (parmnum, parm);
13289 tree parmtype = TREE_TYPE (parm);
13290 if (DECL_BY_REFERENCE (parm))
13291 parmtype = TREE_TYPE (parmtype);
13292 if (parmtype == error_mark_node)
13293 return error_mark_node;
13294
13295 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13296
13297 tree *slot;
13298 if (defarg_inst && (slot = defarg_inst->get (parm)))
13299 return *slot;
13300
13301 /* This default argument came from a template. Instantiate the
13302 default argument here, not in tsubst. In the case of
13303 something like:
13304
13305 template <class T>
13306 struct S {
13307 static T t();
13308 void f(T = t());
13309 };
13310
13311 we must be careful to do name lookup in the scope of S<T>,
13312 rather than in the current class. */
13313 push_to_top_level ();
13314 push_access_scope (fn);
13315 push_deferring_access_checks (dk_no_deferred);
13316 start_lambda_scope (parm);
13317
13318 /* The default argument expression may cause implicitly defined
13319 member functions to be synthesized, which will result in garbage
13320 collection. We must treat this situation as if we were within
13321 the body of function so as to avoid collecting live data on the
13322 stack. */
13323 ++function_depth;
13324 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13325 complain, NULL_TREE,
13326 /*integral_constant_expression_p=*/false);
13327 --function_depth;
13328
13329 finish_lambda_scope ();
13330
13331 /* Make sure the default argument is reasonable. */
13332 arg = check_default_argument (type, arg, complain);
13333
13334 if (errorcount+sorrycount > errs
13335 && (complain & tf_warning_or_error))
13336 inform (input_location,
13337 " when instantiating default argument for call to %qD", fn);
13338
13339 pop_deferring_access_checks ();
13340 pop_access_scope (fn);
13341 pop_from_top_level ();
13342
13343 if (arg != error_mark_node && !cp_unevaluated_operand)
13344 {
13345 if (!defarg_inst)
13346 defarg_inst = tree_cache_map::create_ggc (37);
13347 defarg_inst->put (parm, arg);
13348 }
13349
13350 return arg;
13351 }
13352
13353 /* Substitute into all the default arguments for FN. */
13354
13355 static void
13356 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13357 {
13358 tree arg;
13359 tree tmpl_args;
13360
13361 tmpl_args = DECL_TI_ARGS (fn);
13362
13363 /* If this function is not yet instantiated, we certainly don't need
13364 its default arguments. */
13365 if (uses_template_parms (tmpl_args))
13366 return;
13367 /* Don't do this again for clones. */
13368 if (DECL_CLONED_FUNCTION_P (fn))
13369 return;
13370
13371 int i = 0;
13372 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13373 arg;
13374 arg = TREE_CHAIN (arg), ++i)
13375 if (TREE_PURPOSE (arg))
13376 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13377 TREE_VALUE (arg),
13378 TREE_PURPOSE (arg),
13379 complain);
13380 }
13381
13382 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13383 static GTY((cache)) tree_cache_map *explicit_specifier_map;
13384
13385 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13386
13387 void
13388 store_explicit_specifier (tree v, tree t)
13389 {
13390 if (!explicit_specifier_map)
13391 explicit_specifier_map = tree_cache_map::create_ggc (37);
13392 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13393 explicit_specifier_map->put (v, t);
13394 }
13395
13396 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13397
13398 static tree
13399 lookup_explicit_specifier (tree v)
13400 {
13401 return *explicit_specifier_map->get (v);
13402 }
13403
13404 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13405
13406 static tree
13407 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13408 tree lambda_fntype)
13409 {
13410 tree gen_tmpl, argvec;
13411 hashval_t hash = 0;
13412 tree in_decl = t;
13413
13414 /* Nobody should be tsubst'ing into non-template functions. */
13415 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
13416
13417 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13418 {
13419 /* If T is not dependent, just return it. */
13420 if (!uses_template_parms (DECL_TI_ARGS (t))
13421 && !LAMBDA_FUNCTION_P (t))
13422 return t;
13423
13424 /* Calculate the most general template of which R is a
13425 specialization. */
13426 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13427
13428 /* We're substituting a lambda function under tsubst_lambda_expr but not
13429 directly from it; find the matching function we're already inside.
13430 But don't do this if T is a generic lambda with a single level of
13431 template parms, as in that case we're doing a normal instantiation. */
13432 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13433 && (!generic_lambda_fn_p (t)
13434 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13435 return enclosing_instantiation_of (t);
13436
13437 /* Calculate the complete set of arguments used to
13438 specialize R. */
13439 argvec = tsubst_template_args (DECL_TI_ARGS
13440 (DECL_TEMPLATE_RESULT
13441 (DECL_TI_TEMPLATE (t))),
13442 args, complain, in_decl);
13443 if (argvec == error_mark_node)
13444 return error_mark_node;
13445
13446 /* Check to see if we already have this specialization. */
13447 if (!lambda_fntype)
13448 {
13449 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13450 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13451 return spec;
13452 }
13453
13454 /* We can see more levels of arguments than parameters if
13455 there was a specialization of a member template, like
13456 this:
13457
13458 template <class T> struct S { template <class U> void f(); }
13459 template <> template <class U> void S<int>::f(U);
13460
13461 Here, we'll be substituting into the specialization,
13462 because that's where we can find the code we actually
13463 want to generate, but we'll have enough arguments for
13464 the most general template.
13465
13466 We also deal with the peculiar case:
13467
13468 template <class T> struct S {
13469 template <class U> friend void f();
13470 };
13471 template <class U> void f() {}
13472 template S<int>;
13473 template void f<double>();
13474
13475 Here, the ARGS for the instantiation of will be {int,
13476 double}. But, we only need as many ARGS as there are
13477 levels of template parameters in CODE_PATTERN. We are
13478 careful not to get fooled into reducing the ARGS in
13479 situations like:
13480
13481 template <class T> struct S { template <class U> void f(U); }
13482 template <class T> template <> void S<T>::f(int) {}
13483
13484 which we can spot because the pattern will be a
13485 specialization in this case. */
13486 int args_depth = TMPL_ARGS_DEPTH (args);
13487 int parms_depth =
13488 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13489
13490 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13491 args = get_innermost_template_args (args, parms_depth);
13492 }
13493 else
13494 {
13495 /* This special case arises when we have something like this:
13496
13497 template <class T> struct S {
13498 friend void f<int>(int, double);
13499 };
13500
13501 Here, the DECL_TI_TEMPLATE for the friend declaration
13502 will be an IDENTIFIER_NODE. We are being called from
13503 tsubst_friend_function, and we want only to create a
13504 new decl (R) with appropriate types so that we can call
13505 determine_specialization. */
13506 gen_tmpl = NULL_TREE;
13507 argvec = NULL_TREE;
13508 }
13509
13510 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13511 : NULL_TREE);
13512 tree ctx = closure ? closure : DECL_CONTEXT (t);
13513 bool member = ctx && TYPE_P (ctx);
13514
13515 if (member && !closure)
13516 ctx = tsubst_aggr_type (ctx, args,
13517 complain, t, /*entering_scope=*/1);
13518
13519 tree type = (lambda_fntype ? lambda_fntype
13520 : tsubst (TREE_TYPE (t), args,
13521 complain | tf_fndecl_type, in_decl));
13522 if (type == error_mark_node)
13523 return error_mark_node;
13524
13525 /* If we hit excessive deduction depth, the type is bogus even if
13526 it isn't error_mark_node, so don't build a decl. */
13527 if (excessive_deduction_depth)
13528 return error_mark_node;
13529
13530 /* We do NOT check for matching decls pushed separately at this
13531 point, as they may not represent instantiations of this
13532 template, and in any case are considered separate under the
13533 discrete model. */
13534 tree r = copy_decl (t);
13535 DECL_USE_TEMPLATE (r) = 0;
13536 TREE_TYPE (r) = type;
13537 /* Clear out the mangled name and RTL for the instantiation. */
13538 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13539 SET_DECL_RTL (r, NULL);
13540 /* Leave DECL_INITIAL set on deleted instantiations. */
13541 if (!DECL_DELETED_FN (r))
13542 DECL_INITIAL (r) = NULL_TREE;
13543 DECL_CONTEXT (r) = ctx;
13544
13545 /* Handle explicit(dependent-expr). */
13546 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13547 {
13548 tree spec = lookup_explicit_specifier (t);
13549 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13550 /*function_p=*/false,
13551 /*i_c_e_p=*/true);
13552 spec = build_explicit_specifier (spec, complain);
13553 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13554 }
13555
13556 /* OpenMP UDRs have the only argument a reference to the declared
13557 type. We want to diagnose if the declared type is a reference,
13558 which is invalid, but as references to references are usually
13559 quietly merged, diagnose it here. */
13560 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13561 {
13562 tree argtype
13563 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13564 argtype = tsubst (argtype, args, complain, in_decl);
13565 if (TYPE_REF_P (argtype))
13566 error_at (DECL_SOURCE_LOCATION (t),
13567 "reference type %qT in "
13568 "%<#pragma omp declare reduction%>", argtype);
13569 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13570 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13571 argtype);
13572 }
13573
13574 if (member && DECL_CONV_FN_P (r))
13575 /* Type-conversion operator. Reconstruct the name, in
13576 case it's the name of one of the template's parameters. */
13577 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13578
13579 tree parms = DECL_ARGUMENTS (t);
13580 if (closure)
13581 parms = DECL_CHAIN (parms);
13582 parms = tsubst (parms, args, complain, t);
13583 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13584 DECL_CONTEXT (parm) = r;
13585 if (closure)
13586 {
13587 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13588 DECL_CHAIN (tparm) = parms;
13589 parms = tparm;
13590 }
13591 DECL_ARGUMENTS (r) = parms;
13592 DECL_RESULT (r) = NULL_TREE;
13593
13594 TREE_STATIC (r) = 0;
13595 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13596 DECL_EXTERNAL (r) = 1;
13597 /* If this is an instantiation of a function with internal
13598 linkage, we already know what object file linkage will be
13599 assigned to the instantiation. */
13600 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13601 DECL_DEFER_OUTPUT (r) = 0;
13602 DECL_CHAIN (r) = NULL_TREE;
13603 DECL_PENDING_INLINE_INFO (r) = 0;
13604 DECL_PENDING_INLINE_P (r) = 0;
13605 DECL_SAVED_TREE (r) = NULL_TREE;
13606 DECL_STRUCT_FUNCTION (r) = NULL;
13607 TREE_USED (r) = 0;
13608 /* We'll re-clone as appropriate in instantiate_template. */
13609 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13610
13611 /* If we aren't complaining now, return on error before we register
13612 the specialization so that we'll complain eventually. */
13613 if ((complain & tf_error) == 0
13614 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13615 && !grok_op_properties (r, /*complain=*/false))
13616 return error_mark_node;
13617
13618 /* Associate the constraints directly with the instantiation. We
13619 don't substitute through the constraints; that's only done when
13620 they are checked. */
13621 if (tree ci = get_constraints (t))
13622 set_constraints (r, ci);
13623
13624 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13625 SET_DECL_FRIEND_CONTEXT (r,
13626 tsubst (DECL_FRIEND_CONTEXT (t),
13627 args, complain, in_decl));
13628
13629 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13630 this in the special friend case mentioned above where
13631 GEN_TMPL is NULL. */
13632 if (gen_tmpl && !closure)
13633 {
13634 DECL_TEMPLATE_INFO (r)
13635 = build_template_info (gen_tmpl, argvec);
13636 SET_DECL_IMPLICIT_INSTANTIATION (r);
13637
13638 tree new_r
13639 = register_specialization (r, gen_tmpl, argvec, false, hash);
13640 if (new_r != r)
13641 /* We instantiated this while substituting into
13642 the type earlier (template/friend54.C). */
13643 return new_r;
13644
13645 /* We're not supposed to instantiate default arguments
13646 until they are called, for a template. But, for a
13647 declaration like:
13648
13649 template <class T> void f ()
13650 { extern void g(int i = T()); }
13651
13652 we should do the substitution when the template is
13653 instantiated. We handle the member function case in
13654 instantiate_class_template since the default arguments
13655 might refer to other members of the class. */
13656 if (!member
13657 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13658 && !uses_template_parms (argvec))
13659 tsubst_default_arguments (r, complain);
13660 }
13661 else
13662 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13663
13664 /* Copy the list of befriending classes. */
13665 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13666 *friends;
13667 friends = &TREE_CHAIN (*friends))
13668 {
13669 *friends = copy_node (*friends);
13670 TREE_VALUE (*friends)
13671 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13672 }
13673
13674 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13675 {
13676 maybe_retrofit_in_chrg (r);
13677 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13678 return error_mark_node;
13679 /* If this is an instantiation of a member template, clone it.
13680 If it isn't, that'll be handled by
13681 clone_constructors_and_destructors. */
13682 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13683 clone_function_decl (r, /*update_methods=*/false);
13684 }
13685 else if ((complain & tf_error) != 0
13686 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13687 && !grok_op_properties (r, /*complain=*/true))
13688 return error_mark_node;
13689
13690 /* Possibly limit visibility based on template args. */
13691 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13692 if (DECL_VISIBILITY_SPECIFIED (t))
13693 {
13694 DECL_VISIBILITY_SPECIFIED (r) = 0;
13695 DECL_ATTRIBUTES (r)
13696 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13697 }
13698 determine_visibility (r);
13699 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13700 && !processing_template_decl)
13701 defaulted_late_check (r);
13702
13703 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13704 args, complain, in_decl);
13705 if (flag_openmp)
13706 if (tree attr = lookup_attribute ("omp declare variant base",
13707 DECL_ATTRIBUTES (r)))
13708 omp_declare_variant_finalize (r, attr);
13709
13710 return r;
13711 }
13712
13713 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13714
13715 static tree
13716 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13717 tree lambda_fntype)
13718 {
13719 /* We can get here when processing a member function template,
13720 member class template, or template template parameter. */
13721 tree decl = DECL_TEMPLATE_RESULT (t);
13722 tree in_decl = t;
13723 tree spec;
13724 tree tmpl_args;
13725 tree full_args;
13726 tree r;
13727 hashval_t hash = 0;
13728
13729 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13730 {
13731 /* Template template parameter is treated here. */
13732 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13733 if (new_type == error_mark_node)
13734 r = error_mark_node;
13735 /* If we get a real template back, return it. This can happen in
13736 the context of most_specialized_partial_spec. */
13737 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13738 r = new_type;
13739 else
13740 /* The new TEMPLATE_DECL was built in
13741 reduce_template_parm_level. */
13742 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13743 return r;
13744 }
13745
13746 if (!lambda_fntype)
13747 {
13748 /* We might already have an instance of this template.
13749 The ARGS are for the surrounding class type, so the
13750 full args contain the tsubst'd args for the context,
13751 plus the innermost args from the template decl. */
13752 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13753 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13754 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13755 /* Because this is a template, the arguments will still be
13756 dependent, even after substitution. If
13757 PROCESSING_TEMPLATE_DECL is not set, the dependency
13758 predicates will short-circuit. */
13759 ++processing_template_decl;
13760 full_args = tsubst_template_args (tmpl_args, args,
13761 complain, in_decl);
13762 --processing_template_decl;
13763 if (full_args == error_mark_node)
13764 return error_mark_node;
13765
13766 /* If this is a default template template argument,
13767 tsubst might not have changed anything. */
13768 if (full_args == tmpl_args)
13769 return t;
13770
13771 hash = hash_tmpl_and_args (t, full_args);
13772 spec = retrieve_specialization (t, full_args, hash);
13773 if (spec != NULL_TREE)
13774 {
13775 if (TYPE_P (spec))
13776 /* Type partial instantiations are stored as the type by
13777 lookup_template_class_1, not here as the template. */
13778 spec = CLASSTYPE_TI_TEMPLATE (spec);
13779 return spec;
13780 }
13781 }
13782
13783 /* Make a new template decl. It will be similar to the
13784 original, but will record the current template arguments.
13785 We also create a new function declaration, which is just
13786 like the old one, but points to this new template, rather
13787 than the old one. */
13788 r = copy_decl (t);
13789 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13790 DECL_CHAIN (r) = NULL_TREE;
13791
13792 // Build new template info linking to the original template decl.
13793 if (!lambda_fntype)
13794 {
13795 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13796 SET_DECL_IMPLICIT_INSTANTIATION (r);
13797 }
13798 else
13799 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13800
13801 /* The template parameters for this new template are all the
13802 template parameters for the old template, except the
13803 outermost level of parameters. */
13804 DECL_TEMPLATE_PARMS (r)
13805 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13806 complain);
13807
13808 if (TREE_CODE (decl) == TYPE_DECL
13809 && !TYPE_DECL_ALIAS_P (decl))
13810 {
13811 tree new_type;
13812 ++processing_template_decl;
13813 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13814 --processing_template_decl;
13815 if (new_type == error_mark_node)
13816 return error_mark_node;
13817
13818 TREE_TYPE (r) = new_type;
13819 /* For a partial specialization, we need to keep pointing to
13820 the primary template. */
13821 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13822 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13823 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13824 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13825 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13826 }
13827 else
13828 {
13829 tree new_decl;
13830 ++processing_template_decl;
13831 if (TREE_CODE (decl) == FUNCTION_DECL)
13832 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13833 else
13834 new_decl = tsubst (decl, args, complain, in_decl);
13835 --processing_template_decl;
13836 if (new_decl == error_mark_node)
13837 return error_mark_node;
13838
13839 DECL_TEMPLATE_RESULT (r) = new_decl;
13840 TREE_TYPE (r) = TREE_TYPE (new_decl);
13841 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13842 if (lambda_fntype)
13843 {
13844 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13845 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13846 }
13847 else
13848 {
13849 DECL_TI_TEMPLATE (new_decl) = r;
13850 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13851 }
13852 }
13853
13854 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13855 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13856
13857 if (PRIMARY_TEMPLATE_P (t))
13858 DECL_PRIMARY_TEMPLATE (r) = r;
13859
13860 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13861 && !lambda_fntype)
13862 /* Record this non-type partial instantiation. */
13863 register_specialization (r, t,
13864 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13865 false, hash);
13866
13867 return r;
13868 }
13869
13870 /* True if FN is the op() for a lambda in an uninstantiated template. */
13871
13872 bool
13873 lambda_fn_in_template_p (tree fn)
13874 {
13875 if (!fn || !LAMBDA_FUNCTION_P (fn))
13876 return false;
13877 tree closure = DECL_CONTEXT (fn);
13878 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13879 }
13880
13881 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13882 which the above is true. */
13883
13884 bool
13885 instantiated_lambda_fn_p (tree fn)
13886 {
13887 if (!fn || !LAMBDA_FUNCTION_P (fn))
13888 return false;
13889 tree closure = DECL_CONTEXT (fn);
13890 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13891 return LAMBDA_EXPR_INSTANTIATED (lam);
13892 }
13893
13894 /* We're instantiating a variable from template function TCTX. Return the
13895 corresponding current enclosing scope. This gets complicated because lambda
13896 functions in templates are regenerated rather than instantiated, but generic
13897 lambda functions are subsequently instantiated. */
13898
13899 static tree
13900 enclosing_instantiation_of (tree otctx)
13901 {
13902 tree tctx = otctx;
13903 tree fn = current_function_decl;
13904 int lambda_count = 0;
13905
13906 for (; tctx && (lambda_fn_in_template_p (tctx)
13907 || instantiated_lambda_fn_p (tctx));
13908 tctx = decl_function_context (tctx))
13909 ++lambda_count;
13910 for (; fn; fn = decl_function_context (fn))
13911 {
13912 tree ofn = fn;
13913 int flambda_count = 0;
13914 for (; fn && instantiated_lambda_fn_p (fn);
13915 fn = decl_function_context (fn))
13916 ++flambda_count;
13917 if ((fn && DECL_TEMPLATE_INFO (fn))
13918 ? most_general_template (fn) != most_general_template (tctx)
13919 : fn != tctx)
13920 continue;
13921 if (flambda_count != lambda_count)
13922 {
13923 gcc_assert (flambda_count > lambda_count);
13924 for (; flambda_count > lambda_count; --flambda_count)
13925 ofn = decl_function_context (ofn);
13926 }
13927 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13928 || DECL_CONV_FN_P (ofn));
13929 return ofn;
13930 }
13931 gcc_unreachable ();
13932 }
13933
13934 /* Substitute the ARGS into the T, which is a _DECL. Return the
13935 result of the substitution. Issue error and warning messages under
13936 control of COMPLAIN. */
13937
13938 static tree
13939 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13940 {
13941 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13942 location_t saved_loc;
13943 tree r = NULL_TREE;
13944 tree in_decl = t;
13945 hashval_t hash = 0;
13946
13947 /* Set the filename and linenumber to improve error-reporting. */
13948 saved_loc = input_location;
13949 input_location = DECL_SOURCE_LOCATION (t);
13950
13951 switch (TREE_CODE (t))
13952 {
13953 case TEMPLATE_DECL:
13954 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13955 break;
13956
13957 case FUNCTION_DECL:
13958 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13959 break;
13960
13961 case PARM_DECL:
13962 {
13963 tree type = NULL_TREE;
13964 int i, len = 1;
13965 tree expanded_types = NULL_TREE;
13966 tree prev_r = NULL_TREE;
13967 tree first_r = NULL_TREE;
13968
13969 if (DECL_PACK_P (t))
13970 {
13971 /* If there is a local specialization that isn't a
13972 parameter pack, it means that we're doing a "simple"
13973 substitution from inside tsubst_pack_expansion. Just
13974 return the local specialization (which will be a single
13975 parm). */
13976 tree spec = retrieve_local_specialization (t);
13977 if (spec
13978 && TREE_CODE (spec) == PARM_DECL
13979 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13980 RETURN (spec);
13981
13982 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13983 the parameters in this function parameter pack. */
13984 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13985 complain, in_decl);
13986 if (TREE_CODE (expanded_types) == TREE_VEC)
13987 {
13988 len = TREE_VEC_LENGTH (expanded_types);
13989
13990 /* Zero-length parameter packs are boring. Just substitute
13991 into the chain. */
13992 if (len == 0)
13993 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13994 TREE_CHAIN (t)));
13995 }
13996 else
13997 {
13998 /* All we did was update the type. Make a note of that. */
13999 type = expanded_types;
14000 expanded_types = NULL_TREE;
14001 }
14002 }
14003
14004 /* Loop through all of the parameters we'll build. When T is
14005 a function parameter pack, LEN is the number of expanded
14006 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14007 r = NULL_TREE;
14008 for (i = 0; i < len; ++i)
14009 {
14010 prev_r = r;
14011 r = copy_node (t);
14012 if (DECL_TEMPLATE_PARM_P (t))
14013 SET_DECL_TEMPLATE_PARM_P (r);
14014
14015 if (expanded_types)
14016 /* We're on the Ith parameter of the function parameter
14017 pack. */
14018 {
14019 /* Get the Ith type. */
14020 type = TREE_VEC_ELT (expanded_types, i);
14021
14022 /* Rename the parameter to include the index. */
14023 DECL_NAME (r)
14024 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14025 }
14026 else if (!type)
14027 /* We're dealing with a normal parameter. */
14028 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14029
14030 type = type_decays_to (type);
14031 TREE_TYPE (r) = type;
14032 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14033
14034 if (DECL_INITIAL (r))
14035 {
14036 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14037 DECL_INITIAL (r) = TREE_TYPE (r);
14038 else
14039 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14040 complain, in_decl);
14041 }
14042
14043 DECL_CONTEXT (r) = NULL_TREE;
14044
14045 if (!DECL_TEMPLATE_PARM_P (r))
14046 DECL_ARG_TYPE (r) = type_passed_as (type);
14047
14048 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14049 args, complain, in_decl);
14050
14051 /* Keep track of the first new parameter we
14052 generate. That's what will be returned to the
14053 caller. */
14054 if (!first_r)
14055 first_r = r;
14056
14057 /* Build a proper chain of parameters when substituting
14058 into a function parameter pack. */
14059 if (prev_r)
14060 DECL_CHAIN (prev_r) = r;
14061 }
14062
14063 /* If cp_unevaluated_operand is set, we're just looking for a
14064 single dummy parameter, so don't keep going. */
14065 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14066 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14067 complain, DECL_CHAIN (t));
14068
14069 /* FIRST_R contains the start of the chain we've built. */
14070 r = first_r;
14071 }
14072 break;
14073
14074 case FIELD_DECL:
14075 {
14076 tree type = NULL_TREE;
14077 tree vec = NULL_TREE;
14078 tree expanded_types = NULL_TREE;
14079 int len = 1;
14080
14081 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14082 {
14083 /* This field is a lambda capture pack. Return a TREE_VEC of
14084 the expanded fields to instantiate_class_template_1. */
14085 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14086 complain, in_decl);
14087 if (TREE_CODE (expanded_types) == TREE_VEC)
14088 {
14089 len = TREE_VEC_LENGTH (expanded_types);
14090 vec = make_tree_vec (len);
14091 }
14092 else
14093 {
14094 /* All we did was update the type. Make a note of that. */
14095 type = expanded_types;
14096 expanded_types = NULL_TREE;
14097 }
14098 }
14099
14100 for (int i = 0; i < len; ++i)
14101 {
14102 r = copy_decl (t);
14103 if (expanded_types)
14104 {
14105 type = TREE_VEC_ELT (expanded_types, i);
14106 DECL_NAME (r)
14107 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14108 }
14109 else if (!type)
14110 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14111
14112 if (type == error_mark_node)
14113 RETURN (error_mark_node);
14114 TREE_TYPE (r) = type;
14115 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14116
14117 if (DECL_C_BIT_FIELD (r))
14118 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14119 number of bits. */
14120 DECL_BIT_FIELD_REPRESENTATIVE (r)
14121 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14122 complain, in_decl,
14123 /*integral_constant_expression_p=*/true);
14124 if (DECL_INITIAL (t))
14125 {
14126 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14127 NSDMI in perform_member_init. Still set DECL_INITIAL
14128 so that we know there is one. */
14129 DECL_INITIAL (r) = void_node;
14130 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14131 retrofit_lang_decl (r);
14132 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14133 }
14134 /* We don't have to set DECL_CONTEXT here; it is set by
14135 finish_member_declaration. */
14136 DECL_CHAIN (r) = NULL_TREE;
14137
14138 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14139 args, complain, in_decl);
14140
14141 if (vec)
14142 TREE_VEC_ELT (vec, i) = r;
14143 }
14144
14145 if (vec)
14146 r = vec;
14147 }
14148 break;
14149
14150 case USING_DECL:
14151 /* We reach here only for member using decls. We also need to check
14152 uses_template_parms because DECL_DEPENDENT_P is not set for a
14153 using-declaration that designates a member of the current
14154 instantiation (c++/53549). */
14155 if (DECL_DEPENDENT_P (t)
14156 || uses_template_parms (USING_DECL_SCOPE (t)))
14157 {
14158 tree scope = USING_DECL_SCOPE (t);
14159 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14160 if (PACK_EXPANSION_P (scope))
14161 {
14162 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14163 int len = TREE_VEC_LENGTH (vec);
14164 r = make_tree_vec (len);
14165 for (int i = 0; i < len; ++i)
14166 {
14167 tree escope = TREE_VEC_ELT (vec, i);
14168 tree elt = do_class_using_decl (escope, name);
14169 if (!elt)
14170 {
14171 r = error_mark_node;
14172 break;
14173 }
14174 else
14175 {
14176 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14177 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14178 }
14179 TREE_VEC_ELT (r, i) = elt;
14180 }
14181 }
14182 else
14183 {
14184 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14185 complain, in_decl);
14186 r = do_class_using_decl (inst_scope, name);
14187 if (!r)
14188 r = error_mark_node;
14189 else
14190 {
14191 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14192 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14193 }
14194 }
14195 }
14196 else
14197 {
14198 r = copy_node (t);
14199 DECL_CHAIN (r) = NULL_TREE;
14200 }
14201 break;
14202
14203 case TYPE_DECL:
14204 case VAR_DECL:
14205 {
14206 tree argvec = NULL_TREE;
14207 tree gen_tmpl = NULL_TREE;
14208 tree spec;
14209 tree tmpl = NULL_TREE;
14210 tree ctx;
14211 tree type = NULL_TREE;
14212 bool local_p;
14213
14214 if (TREE_TYPE (t) == error_mark_node)
14215 RETURN (error_mark_node);
14216
14217 if (TREE_CODE (t) == TYPE_DECL
14218 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14219 {
14220 /* If this is the canonical decl, we don't have to
14221 mess with instantiations, and often we can't (for
14222 typename, template type parms and such). Note that
14223 TYPE_NAME is not correct for the above test if
14224 we've copied the type for a typedef. */
14225 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14226 if (type == error_mark_node)
14227 RETURN (error_mark_node);
14228 r = TYPE_NAME (type);
14229 break;
14230 }
14231
14232 /* Check to see if we already have the specialization we
14233 need. */
14234 spec = NULL_TREE;
14235 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
14236 {
14237 /* T is a static data member or namespace-scope entity.
14238 We have to substitute into namespace-scope variables
14239 (not just variable templates) because of cases like:
14240
14241 template <class T> void f() { extern T t; }
14242
14243 where the entity referenced is not known until
14244 instantiation time. */
14245 local_p = false;
14246 ctx = DECL_CONTEXT (t);
14247 if (DECL_CLASS_SCOPE_P (t))
14248 {
14249 ctx = tsubst_aggr_type (ctx, args,
14250 complain,
14251 in_decl, /*entering_scope=*/1);
14252 /* If CTX is unchanged, then T is in fact the
14253 specialization we want. That situation occurs when
14254 referencing a static data member within in its own
14255 class. We can use pointer equality, rather than
14256 same_type_p, because DECL_CONTEXT is always
14257 canonical... */
14258 if (ctx == DECL_CONTEXT (t)
14259 /* ... unless T is a member template; in which
14260 case our caller can be willing to create a
14261 specialization of that template represented
14262 by T. */
14263 && !(DECL_TI_TEMPLATE (t)
14264 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14265 spec = t;
14266 }
14267
14268 if (!spec)
14269 {
14270 tmpl = DECL_TI_TEMPLATE (t);
14271 gen_tmpl = most_general_template (tmpl);
14272 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14273 if (argvec != error_mark_node)
14274 argvec = (coerce_innermost_template_parms
14275 (DECL_TEMPLATE_PARMS (gen_tmpl),
14276 argvec, t, complain,
14277 /*all*/true, /*defarg*/true));
14278 if (argvec == error_mark_node)
14279 RETURN (error_mark_node);
14280 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14281 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14282 }
14283 }
14284 else
14285 {
14286 /* A local variable. */
14287 local_p = true;
14288 /* Subsequent calls to pushdecl will fill this in. */
14289 ctx = NULL_TREE;
14290 /* Unless this is a reference to a static variable from an
14291 enclosing function, in which case we need to fill it in now. */
14292 if (TREE_STATIC (t))
14293 {
14294 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14295 if (fn != current_function_decl)
14296 ctx = fn;
14297 }
14298 spec = retrieve_local_specialization (t);
14299 }
14300 /* If we already have the specialization we need, there is
14301 nothing more to do. */
14302 if (spec)
14303 {
14304 r = spec;
14305 break;
14306 }
14307
14308 /* Create a new node for the specialization we need. */
14309 if (type == NULL_TREE)
14310 {
14311 if (is_typedef_decl (t))
14312 type = DECL_ORIGINAL_TYPE (t);
14313 else
14314 type = TREE_TYPE (t);
14315 if (VAR_P (t)
14316 && VAR_HAD_UNKNOWN_BOUND (t)
14317 && type != error_mark_node)
14318 type = strip_array_domain (type);
14319 tree sub_args = args;
14320 if (tree auto_node = type_uses_auto (type))
14321 {
14322 /* Mask off any template args past the variable's context so we
14323 don't replace the auto with an unrelated argument. */
14324 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14325 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14326 if (extra > 0)
14327 /* This should never happen with the new lambda instantiation
14328 model, but keep the handling just in case. */
14329 gcc_assert (!CHECKING_P),
14330 sub_args = strip_innermost_template_args (args, extra);
14331 }
14332 type = tsubst (type, sub_args, complain, in_decl);
14333 /* Substituting the type might have recursively instantiated this
14334 same alias (c++/86171). */
14335 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14336 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14337 {
14338 r = spec;
14339 break;
14340 }
14341 }
14342 r = copy_decl (t);
14343 if (VAR_P (r))
14344 {
14345 DECL_INITIALIZED_P (r) = 0;
14346 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14347 if (type == error_mark_node)
14348 RETURN (error_mark_node);
14349 if (TREE_CODE (type) == FUNCTION_TYPE)
14350 {
14351 /* It may seem that this case cannot occur, since:
14352
14353 typedef void f();
14354 void g() { f x; }
14355
14356 declares a function, not a variable. However:
14357
14358 typedef void f();
14359 template <typename T> void g() { T t; }
14360 template void g<f>();
14361
14362 is an attempt to declare a variable with function
14363 type. */
14364 error ("variable %qD has function type",
14365 /* R is not yet sufficiently initialized, so we
14366 just use its name. */
14367 DECL_NAME (r));
14368 RETURN (error_mark_node);
14369 }
14370 type = complete_type (type);
14371 /* Wait until cp_finish_decl to set this again, to handle
14372 circular dependency (template/instantiate6.C). */
14373 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14374 type = check_var_type (DECL_NAME (r), type,
14375 DECL_SOURCE_LOCATION (r));
14376 if (DECL_HAS_VALUE_EXPR_P (t))
14377 {
14378 tree ve = DECL_VALUE_EXPR (t);
14379 ve = tsubst_expr (ve, args, complain, in_decl,
14380 /*constant_expression_p=*/false);
14381 if (REFERENCE_REF_P (ve))
14382 {
14383 gcc_assert (TYPE_REF_P (type));
14384 ve = TREE_OPERAND (ve, 0);
14385 }
14386 SET_DECL_VALUE_EXPR (r, ve);
14387 }
14388 if (CP_DECL_THREAD_LOCAL_P (r)
14389 && !processing_template_decl)
14390 set_decl_tls_model (r, decl_default_tls_model (r));
14391 }
14392 else if (DECL_SELF_REFERENCE_P (t))
14393 SET_DECL_SELF_REFERENCE_P (r);
14394 TREE_TYPE (r) = type;
14395 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14396 DECL_CONTEXT (r) = ctx;
14397 /* Clear out the mangled name and RTL for the instantiation. */
14398 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14399 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14400 SET_DECL_RTL (r, NULL);
14401 /* The initializer must not be expanded until it is required;
14402 see [temp.inst]. */
14403 DECL_INITIAL (r) = NULL_TREE;
14404 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14405 if (VAR_P (r))
14406 {
14407 if (DECL_LANG_SPECIFIC (r))
14408 SET_DECL_DEPENDENT_INIT_P (r, false);
14409
14410 SET_DECL_MODE (r, VOIDmode);
14411
14412 /* Possibly limit visibility based on template args. */
14413 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14414 if (DECL_VISIBILITY_SPECIFIED (t))
14415 {
14416 DECL_VISIBILITY_SPECIFIED (r) = 0;
14417 DECL_ATTRIBUTES (r)
14418 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14419 }
14420 determine_visibility (r);
14421 }
14422
14423 if (!local_p)
14424 {
14425 /* A static data member declaration is always marked
14426 external when it is declared in-class, even if an
14427 initializer is present. We mimic the non-template
14428 processing here. */
14429 DECL_EXTERNAL (r) = 1;
14430 if (DECL_NAMESPACE_SCOPE_P (t))
14431 DECL_NOT_REALLY_EXTERN (r) = 1;
14432
14433 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14434 SET_DECL_IMPLICIT_INSTANTIATION (r);
14435 /* Remember whether we require constant initialization of
14436 a non-constant template variable. */
14437 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
14438 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
14439 if (!error_operand_p (r) || (complain & tf_error))
14440 register_specialization (r, gen_tmpl, argvec, false, hash);
14441 }
14442 else
14443 {
14444 if (DECL_LANG_SPECIFIC (r))
14445 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14446 if (!cp_unevaluated_operand)
14447 register_local_specialization (r, t);
14448 }
14449
14450 DECL_CHAIN (r) = NULL_TREE;
14451
14452 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14453 /*flags=*/0,
14454 args, complain, in_decl);
14455
14456 /* Preserve a typedef that names a type. */
14457 if (is_typedef_decl (r) && type != error_mark_node)
14458 {
14459 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14460 set_underlying_type (r);
14461 if (TYPE_DECL_ALIAS_P (r))
14462 /* An alias template specialization can be dependent
14463 even if its underlying type is not. */
14464 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
14465 }
14466
14467 layout_decl (r, 0);
14468 }
14469 break;
14470
14471 default:
14472 gcc_unreachable ();
14473 }
14474 #undef RETURN
14475
14476 out:
14477 /* Restore the file and line information. */
14478 input_location = saved_loc;
14479
14480 return r;
14481 }
14482
14483 /* Substitute into the complete parameter type list PARMS. */
14484
14485 tree
14486 tsubst_function_parms (tree parms,
14487 tree args,
14488 tsubst_flags_t complain,
14489 tree in_decl)
14490 {
14491 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14492 }
14493
14494 /* Substitute into the ARG_TYPES of a function type.
14495 If END is a TREE_CHAIN, leave it and any following types
14496 un-substituted. */
14497
14498 static tree
14499 tsubst_arg_types (tree arg_types,
14500 tree args,
14501 tree end,
14502 tsubst_flags_t complain,
14503 tree in_decl)
14504 {
14505 tree remaining_arg_types;
14506 tree type = NULL_TREE;
14507 int i = 1;
14508 tree expanded_args = NULL_TREE;
14509 tree default_arg;
14510
14511 if (!arg_types || arg_types == void_list_node || arg_types == end)
14512 return arg_types;
14513
14514 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14515 args, end, complain, in_decl);
14516 if (remaining_arg_types == error_mark_node)
14517 return error_mark_node;
14518
14519 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14520 {
14521 /* For a pack expansion, perform substitution on the
14522 entire expression. Later on, we'll handle the arguments
14523 one-by-one. */
14524 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14525 args, complain, in_decl);
14526
14527 if (TREE_CODE (expanded_args) == TREE_VEC)
14528 /* So that we'll spin through the parameters, one by one. */
14529 i = TREE_VEC_LENGTH (expanded_args);
14530 else
14531 {
14532 /* We only partially substituted into the parameter
14533 pack. Our type is TYPE_PACK_EXPANSION. */
14534 type = expanded_args;
14535 expanded_args = NULL_TREE;
14536 }
14537 }
14538
14539 while (i > 0) {
14540 --i;
14541
14542 if (expanded_args)
14543 type = TREE_VEC_ELT (expanded_args, i);
14544 else if (!type)
14545 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14546
14547 if (type == error_mark_node)
14548 return error_mark_node;
14549 if (VOID_TYPE_P (type))
14550 {
14551 if (complain & tf_error)
14552 {
14553 error ("invalid parameter type %qT", type);
14554 if (in_decl)
14555 error ("in declaration %q+D", in_decl);
14556 }
14557 return error_mark_node;
14558 }
14559 /* DR 657. */
14560 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14561 return error_mark_node;
14562
14563 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14564 top-level qualifiers as required. */
14565 type = cv_unqualified (type_decays_to (type));
14566
14567 /* We do not substitute into default arguments here. The standard
14568 mandates that they be instantiated only when needed, which is
14569 done in build_over_call. */
14570 default_arg = TREE_PURPOSE (arg_types);
14571
14572 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14573 since the new op() won't have any associated template arguments for us
14574 to refer to later. */
14575 if (lambda_fn_in_template_p (in_decl))
14576 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14577 false/*fn*/, false/*constexpr*/);
14578
14579 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14580 {
14581 /* We've instantiated a template before its default arguments
14582 have been parsed. This can happen for a nested template
14583 class, and is not an error unless we require the default
14584 argument in a call of this function. */
14585 remaining_arg_types =
14586 tree_cons (default_arg, type, remaining_arg_types);
14587 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14588 remaining_arg_types);
14589 }
14590 else
14591 remaining_arg_types =
14592 hash_tree_cons (default_arg, type, remaining_arg_types);
14593 }
14594
14595 return remaining_arg_types;
14596 }
14597
14598 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14599 *not* handle the exception-specification for FNTYPE, because the
14600 initial substitution of explicitly provided template parameters
14601 during argument deduction forbids substitution into the
14602 exception-specification:
14603
14604 [temp.deduct]
14605
14606 All references in the function type of the function template to the
14607 corresponding template parameters are replaced by the specified tem-
14608 plate argument values. If a substitution in a template parameter or
14609 in the function type of the function template results in an invalid
14610 type, type deduction fails. [Note: The equivalent substitution in
14611 exception specifications is done only when the function is instanti-
14612 ated, at which point a program is ill-formed if the substitution
14613 results in an invalid type.] */
14614
14615 static tree
14616 tsubst_function_type (tree t,
14617 tree args,
14618 tsubst_flags_t complain,
14619 tree in_decl)
14620 {
14621 tree return_type;
14622 tree arg_types = NULL_TREE;
14623 tree fntype;
14624
14625 /* The TYPE_CONTEXT is not used for function/method types. */
14626 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14627
14628 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14629 failure. */
14630 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14631
14632 if (late_return_type_p)
14633 {
14634 /* Substitute the argument types. */
14635 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14636 complain, in_decl);
14637 if (arg_types == error_mark_node)
14638 return error_mark_node;
14639
14640 tree save_ccp = current_class_ptr;
14641 tree save_ccr = current_class_ref;
14642 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14643 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14644 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14645 if (do_inject)
14646 {
14647 /* DR 1207: 'this' is in scope in the trailing return type. */
14648 inject_this_parameter (this_type, cp_type_quals (this_type));
14649 }
14650
14651 /* Substitute the return type. */
14652 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14653
14654 if (do_inject)
14655 {
14656 current_class_ptr = save_ccp;
14657 current_class_ref = save_ccr;
14658 }
14659 }
14660 else
14661 /* Substitute the return type. */
14662 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14663
14664 if (return_type == error_mark_node)
14665 return error_mark_node;
14666 /* DR 486 clarifies that creation of a function type with an
14667 invalid return type is a deduction failure. */
14668 if (TREE_CODE (return_type) == ARRAY_TYPE
14669 || TREE_CODE (return_type) == FUNCTION_TYPE)
14670 {
14671 if (complain & tf_error)
14672 {
14673 if (TREE_CODE (return_type) == ARRAY_TYPE)
14674 error ("function returning an array");
14675 else
14676 error ("function returning a function");
14677 }
14678 return error_mark_node;
14679 }
14680 /* And DR 657. */
14681 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14682 return error_mark_node;
14683
14684 if (!late_return_type_p)
14685 {
14686 /* Substitute the argument types. */
14687 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14688 complain, in_decl);
14689 if (arg_types == error_mark_node)
14690 return error_mark_node;
14691 }
14692
14693 /* Construct a new type node and return it. */
14694 if (TREE_CODE (t) == FUNCTION_TYPE)
14695 {
14696 fntype = build_function_type (return_type, arg_types);
14697 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14698 }
14699 else
14700 {
14701 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14702 /* Don't pick up extra function qualifiers from the basetype. */
14703 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14704 if (! MAYBE_CLASS_TYPE_P (r))
14705 {
14706 /* [temp.deduct]
14707
14708 Type deduction may fail for any of the following
14709 reasons:
14710
14711 -- Attempting to create "pointer to member of T" when T
14712 is not a class type. */
14713 if (complain & tf_error)
14714 error ("creating pointer to member function of non-class type %qT",
14715 r);
14716 return error_mark_node;
14717 }
14718
14719 fntype = build_method_type_directly (r, return_type,
14720 TREE_CHAIN (arg_types));
14721 }
14722 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14723
14724 /* See comment above. */
14725 tree raises = NULL_TREE;
14726 cp_ref_qualifier rqual = type_memfn_rqual (t);
14727 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14728
14729 return fntype;
14730 }
14731
14732 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14733 ARGS into that specification, and return the substituted
14734 specification. If there is no specification, return NULL_TREE. */
14735
14736 static tree
14737 tsubst_exception_specification (tree fntype,
14738 tree args,
14739 tsubst_flags_t complain,
14740 tree in_decl,
14741 bool defer_ok)
14742 {
14743 tree specs;
14744 tree new_specs;
14745
14746 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14747 new_specs = NULL_TREE;
14748 if (specs && TREE_PURPOSE (specs))
14749 {
14750 /* A noexcept-specifier. */
14751 tree expr = TREE_PURPOSE (specs);
14752 if (TREE_CODE (expr) == INTEGER_CST)
14753 new_specs = expr;
14754 else if (defer_ok)
14755 {
14756 /* Defer instantiation of noexcept-specifiers to avoid
14757 excessive instantiations (c++/49107). */
14758 new_specs = make_node (DEFERRED_NOEXCEPT);
14759 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14760 {
14761 /* We already partially instantiated this member template,
14762 so combine the new args with the old. */
14763 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14764 = DEFERRED_NOEXCEPT_PATTERN (expr);
14765 DEFERRED_NOEXCEPT_ARGS (new_specs)
14766 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14767 }
14768 else
14769 {
14770 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14771 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14772 }
14773 }
14774 else
14775 {
14776 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14777 {
14778 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14779 args);
14780 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14781 }
14782 new_specs = tsubst_copy_and_build
14783 (expr, args, complain, in_decl, /*function_p=*/false,
14784 /*integral_constant_expression_p=*/true);
14785 }
14786 new_specs = build_noexcept_spec (new_specs, complain);
14787 }
14788 else if (specs)
14789 {
14790 if (! TREE_VALUE (specs))
14791 new_specs = specs;
14792 else
14793 while (specs)
14794 {
14795 tree spec;
14796 int i, len = 1;
14797 tree expanded_specs = NULL_TREE;
14798
14799 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14800 {
14801 /* Expand the pack expansion type. */
14802 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14803 args, complain,
14804 in_decl);
14805
14806 if (expanded_specs == error_mark_node)
14807 return error_mark_node;
14808 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14809 len = TREE_VEC_LENGTH (expanded_specs);
14810 else
14811 {
14812 /* We're substituting into a member template, so
14813 we got a TYPE_PACK_EXPANSION back. Add that
14814 expansion and move on. */
14815 gcc_assert (TREE_CODE (expanded_specs)
14816 == TYPE_PACK_EXPANSION);
14817 new_specs = add_exception_specifier (new_specs,
14818 expanded_specs,
14819 complain);
14820 specs = TREE_CHAIN (specs);
14821 continue;
14822 }
14823 }
14824
14825 for (i = 0; i < len; ++i)
14826 {
14827 if (expanded_specs)
14828 spec = TREE_VEC_ELT (expanded_specs, i);
14829 else
14830 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14831 if (spec == error_mark_node)
14832 return spec;
14833 new_specs = add_exception_specifier (new_specs, spec,
14834 complain);
14835 }
14836
14837 specs = TREE_CHAIN (specs);
14838 }
14839 }
14840 return new_specs;
14841 }
14842
14843 /* Take the tree structure T and replace template parameters used
14844 therein with the argument vector ARGS. IN_DECL is an associated
14845 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14846 Issue error and warning messages under control of COMPLAIN. Note
14847 that we must be relatively non-tolerant of extensions here, in
14848 order to preserve conformance; if we allow substitutions that
14849 should not be allowed, we may allow argument deductions that should
14850 not succeed, and therefore report ambiguous overload situations
14851 where there are none. In theory, we could allow the substitution,
14852 but indicate that it should have failed, and allow our caller to
14853 make sure that the right thing happens, but we don't try to do this
14854 yet.
14855
14856 This function is used for dealing with types, decls and the like;
14857 for expressions, use tsubst_expr or tsubst_copy. */
14858
14859 tree
14860 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14861 {
14862 enum tree_code code;
14863 tree type, r = NULL_TREE;
14864
14865 if (t == NULL_TREE || t == error_mark_node
14866 || t == integer_type_node
14867 || t == void_type_node
14868 || t == char_type_node
14869 || t == unknown_type_node
14870 || TREE_CODE (t) == NAMESPACE_DECL
14871 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14872 return t;
14873
14874 if (DECL_P (t))
14875 return tsubst_decl (t, args, complain);
14876
14877 if (args == NULL_TREE)
14878 return t;
14879
14880 code = TREE_CODE (t);
14881
14882 if (code == IDENTIFIER_NODE)
14883 type = IDENTIFIER_TYPE_VALUE (t);
14884 else
14885 type = TREE_TYPE (t);
14886
14887 gcc_assert (type != unknown_type_node);
14888
14889 /* Reuse typedefs. We need to do this to handle dependent attributes,
14890 such as attribute aligned. */
14891 if (TYPE_P (t)
14892 && typedef_variant_p (t))
14893 {
14894 tree decl = TYPE_NAME (t);
14895
14896 if (alias_template_specialization_p (t))
14897 {
14898 /* DECL represents an alias template and we want to
14899 instantiate it. */
14900 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14901 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14902 r = instantiate_alias_template (tmpl, gen_args, complain);
14903 }
14904 else if (DECL_CLASS_SCOPE_P (decl)
14905 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14906 && uses_template_parms (DECL_CONTEXT (decl)))
14907 {
14908 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14909 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14910 r = retrieve_specialization (tmpl, gen_args, 0);
14911 }
14912 else if (DECL_FUNCTION_SCOPE_P (decl)
14913 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14914 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14915 r = retrieve_local_specialization (decl);
14916 else
14917 /* The typedef is from a non-template context. */
14918 return t;
14919
14920 if (r)
14921 {
14922 r = TREE_TYPE (r);
14923 r = cp_build_qualified_type_real
14924 (r, cp_type_quals (t) | cp_type_quals (r),
14925 complain | tf_ignore_bad_quals);
14926 return r;
14927 }
14928 else
14929 {
14930 /* We don't have an instantiation yet, so drop the typedef. */
14931 int quals = cp_type_quals (t);
14932 t = DECL_ORIGINAL_TYPE (decl);
14933 t = cp_build_qualified_type_real (t, quals,
14934 complain | tf_ignore_bad_quals);
14935 }
14936 }
14937
14938 bool fndecl_type = (complain & tf_fndecl_type);
14939 complain &= ~tf_fndecl_type;
14940
14941 if (type
14942 && code != TYPENAME_TYPE
14943 && code != TEMPLATE_TYPE_PARM
14944 && code != TEMPLATE_PARM_INDEX
14945 && code != IDENTIFIER_NODE
14946 && code != FUNCTION_TYPE
14947 && code != METHOD_TYPE)
14948 type = tsubst (type, args, complain, in_decl);
14949 if (type == error_mark_node)
14950 return error_mark_node;
14951
14952 switch (code)
14953 {
14954 case RECORD_TYPE:
14955 case UNION_TYPE:
14956 case ENUMERAL_TYPE:
14957 return tsubst_aggr_type (t, args, complain, in_decl,
14958 /*entering_scope=*/0);
14959
14960 case ERROR_MARK:
14961 case IDENTIFIER_NODE:
14962 case VOID_TYPE:
14963 case REAL_TYPE:
14964 case COMPLEX_TYPE:
14965 case VECTOR_TYPE:
14966 case BOOLEAN_TYPE:
14967 case NULLPTR_TYPE:
14968 case LANG_TYPE:
14969 return t;
14970
14971 case INTEGER_TYPE:
14972 if (t == integer_type_node)
14973 return t;
14974
14975 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14976 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14977 return t;
14978
14979 {
14980 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14981
14982 max = tsubst_expr (omax, args, complain, in_decl,
14983 /*integral_constant_expression_p=*/false);
14984
14985 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14986 needed. */
14987 if (TREE_CODE (max) == NOP_EXPR
14988 && TREE_SIDE_EFFECTS (omax)
14989 && !TREE_TYPE (max))
14990 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14991
14992 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14993 with TREE_SIDE_EFFECTS that indicates this is not an integral
14994 constant expression. */
14995 if (processing_template_decl
14996 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14997 {
14998 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14999 TREE_SIDE_EFFECTS (max) = 1;
15000 }
15001
15002 return compute_array_index_type (NULL_TREE, max, complain);
15003 }
15004
15005 case TEMPLATE_TYPE_PARM:
15006 case TEMPLATE_TEMPLATE_PARM:
15007 case BOUND_TEMPLATE_TEMPLATE_PARM:
15008 case TEMPLATE_PARM_INDEX:
15009 {
15010 int idx;
15011 int level;
15012 int levels;
15013 tree arg = NULL_TREE;
15014
15015 /* Early in template argument deduction substitution, we don't
15016 want to reduce the level of 'auto', or it will be confused
15017 with a normal template parm in subsequent deduction. */
15018 if (is_auto (t) && (complain & tf_partial))
15019 return t;
15020
15021 r = NULL_TREE;
15022
15023 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15024 template_parm_level_and_index (t, &level, &idx);
15025
15026 levels = TMPL_ARGS_DEPTH (args);
15027 if (level <= levels
15028 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15029 {
15030 arg = TMPL_ARG (args, level, idx);
15031
15032 /* See through ARGUMENT_PACK_SELECT arguments. */
15033 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15034 arg = argument_pack_select_arg (arg);
15035 }
15036
15037 if (arg == error_mark_node)
15038 return error_mark_node;
15039 else if (arg != NULL_TREE)
15040 {
15041 if (ARGUMENT_PACK_P (arg))
15042 /* If ARG is an argument pack, we don't actually want to
15043 perform a substitution here, because substitutions
15044 for argument packs are only done
15045 element-by-element. We can get to this point when
15046 substituting the type of a non-type template
15047 parameter pack, when that type actually contains
15048 template parameter packs from an outer template, e.g.,
15049
15050 template<typename... Types> struct A {
15051 template<Types... Values> struct B { };
15052 }; */
15053 return t;
15054
15055 if (code == TEMPLATE_TYPE_PARM)
15056 {
15057 int quals;
15058
15059 /* When building concept checks for the purpose of
15060 deducing placeholders, we can end up with wildcards
15061 where types are expected. Adjust this to the deduced
15062 value. */
15063 if (TREE_CODE (arg) == WILDCARD_DECL)
15064 arg = TREE_TYPE (TREE_TYPE (arg));
15065
15066 gcc_assert (TYPE_P (arg));
15067
15068 quals = cp_type_quals (arg) | cp_type_quals (t);
15069
15070 return cp_build_qualified_type_real
15071 (arg, quals, complain | tf_ignore_bad_quals);
15072 }
15073 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15074 {
15075 /* We are processing a type constructed from a
15076 template template parameter. */
15077 tree argvec = tsubst (TYPE_TI_ARGS (t),
15078 args, complain, in_decl);
15079 if (argvec == error_mark_node)
15080 return error_mark_node;
15081
15082 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15083 || TREE_CODE (arg) == TEMPLATE_DECL
15084 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15085
15086 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15087 /* Consider this code:
15088
15089 template <template <class> class Template>
15090 struct Internal {
15091 template <class Arg> using Bind = Template<Arg>;
15092 };
15093
15094 template <template <class> class Template, class Arg>
15095 using Instantiate = Template<Arg>; //#0
15096
15097 template <template <class> class Template,
15098 class Argument>
15099 using Bind =
15100 Instantiate<Internal<Template>::template Bind,
15101 Argument>; //#1
15102
15103 When #1 is parsed, the
15104 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15105 parameter `Template' in #0 matches the
15106 UNBOUND_CLASS_TEMPLATE representing the argument
15107 `Internal<Template>::template Bind'; We then want
15108 to assemble the type `Bind<Argument>' that can't
15109 be fully created right now, because
15110 `Internal<Template>' not being complete, the Bind
15111 template cannot be looked up in that context. So
15112 we need to "store" `Bind<Argument>' for later
15113 when the context of Bind becomes complete. Let's
15114 store that in a TYPENAME_TYPE. */
15115 return make_typename_type (TYPE_CONTEXT (arg),
15116 build_nt (TEMPLATE_ID_EXPR,
15117 TYPE_IDENTIFIER (arg),
15118 argvec),
15119 typename_type,
15120 complain);
15121
15122 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15123 are resolving nested-types in the signature of a
15124 member function templates. Otherwise ARG is a
15125 TEMPLATE_DECL and is the real template to be
15126 instantiated. */
15127 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15128 arg = TYPE_NAME (arg);
15129
15130 r = lookup_template_class (arg,
15131 argvec, in_decl,
15132 DECL_CONTEXT (arg),
15133 /*entering_scope=*/0,
15134 complain);
15135 return cp_build_qualified_type_real
15136 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15137 }
15138 else if (code == TEMPLATE_TEMPLATE_PARM)
15139 return arg;
15140 else
15141 /* TEMPLATE_PARM_INDEX. */
15142 return convert_from_reference (unshare_expr (arg));
15143 }
15144
15145 if (level == 1)
15146 /* This can happen during the attempted tsubst'ing in
15147 unify. This means that we don't yet have any information
15148 about the template parameter in question. */
15149 return t;
15150
15151 /* If we get here, we must have been looking at a parm for a
15152 more deeply nested template. Make a new version of this
15153 template parameter, but with a lower level. */
15154 switch (code)
15155 {
15156 case TEMPLATE_TYPE_PARM:
15157 case TEMPLATE_TEMPLATE_PARM:
15158 case BOUND_TEMPLATE_TEMPLATE_PARM:
15159 if (cp_type_quals (t))
15160 {
15161 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15162 r = cp_build_qualified_type_real
15163 (r, cp_type_quals (t),
15164 complain | (code == TEMPLATE_TYPE_PARM
15165 ? tf_ignore_bad_quals : 0));
15166 }
15167 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15168 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
15169 && (r = (TEMPLATE_PARM_DESCENDANTS
15170 (TEMPLATE_TYPE_PARM_INDEX (t))))
15171 && (r = TREE_TYPE (r))
15172 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
15173 /* Break infinite recursion when substituting the constraints
15174 of a constrained placeholder. */;
15175 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15176 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
15177 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15178 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15179 r = TEMPLATE_PARM_DESCENDANTS (arg))
15180 && (TEMPLATE_PARM_LEVEL (r)
15181 == TEMPLATE_PARM_LEVEL (arg) - levels))
15182 /* Cache the simple case of lowering a type parameter. */
15183 r = TREE_TYPE (r);
15184 else
15185 {
15186 r = copy_type (t);
15187 TEMPLATE_TYPE_PARM_INDEX (r)
15188 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15189 r, levels, args, complain);
15190 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15191 TYPE_MAIN_VARIANT (r) = r;
15192 TYPE_POINTER_TO (r) = NULL_TREE;
15193 TYPE_REFERENCE_TO (r) = NULL_TREE;
15194
15195 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15196 {
15197 /* Propagate constraints on placeholders since they are
15198 only instantiated during satisfaction. */
15199 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
15200 PLACEHOLDER_TYPE_CONSTRAINTS (r) = constr;
15201 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15202 {
15203 pl = tsubst_copy (pl, args, complain, in_decl);
15204 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15205 }
15206 }
15207
15208 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15209 /* We have reduced the level of the template
15210 template parameter, but not the levels of its
15211 template parameters, so canonical_type_parameter
15212 will not be able to find the canonical template
15213 template parameter for this level. Thus, we
15214 require structural equality checking to compare
15215 TEMPLATE_TEMPLATE_PARMs. */
15216 SET_TYPE_STRUCTURAL_EQUALITY (r);
15217 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15218 SET_TYPE_STRUCTURAL_EQUALITY (r);
15219 else
15220 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15221
15222 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15223 {
15224 tree tinfo = TYPE_TEMPLATE_INFO (t);
15225 /* We might need to substitute into the types of non-type
15226 template parameters. */
15227 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15228 complain, in_decl);
15229 if (tmpl == error_mark_node)
15230 return error_mark_node;
15231 tree argvec = tsubst (TI_ARGS (tinfo), args,
15232 complain, in_decl);
15233 if (argvec == error_mark_node)
15234 return error_mark_node;
15235
15236 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15237 = build_template_info (tmpl, argvec);
15238 }
15239 }
15240 break;
15241
15242 case TEMPLATE_PARM_INDEX:
15243 /* OK, now substitute the type of the non-type parameter. We
15244 couldn't do it earlier because it might be an auto parameter,
15245 and we wouldn't need to if we had an argument. */
15246 type = tsubst (type, args, complain, in_decl);
15247 if (type == error_mark_node)
15248 return error_mark_node;
15249 r = reduce_template_parm_level (t, type, levels, args, complain);
15250 break;
15251
15252 default:
15253 gcc_unreachable ();
15254 }
15255
15256 return r;
15257 }
15258
15259 case TREE_LIST:
15260 {
15261 tree purpose, value, chain;
15262
15263 if (t == void_list_node)
15264 return t;
15265
15266 purpose = TREE_PURPOSE (t);
15267 if (purpose)
15268 {
15269 purpose = tsubst (purpose, args, complain, in_decl);
15270 if (purpose == error_mark_node)
15271 return error_mark_node;
15272 }
15273 value = TREE_VALUE (t);
15274 if (value)
15275 {
15276 value = tsubst (value, args, complain, in_decl);
15277 if (value == error_mark_node)
15278 return error_mark_node;
15279 }
15280 chain = TREE_CHAIN (t);
15281 if (chain && chain != void_type_node)
15282 {
15283 chain = tsubst (chain, args, complain, in_decl);
15284 if (chain == error_mark_node)
15285 return error_mark_node;
15286 }
15287 if (purpose == TREE_PURPOSE (t)
15288 && value == TREE_VALUE (t)
15289 && chain == TREE_CHAIN (t))
15290 return t;
15291 return hash_tree_cons (purpose, value, chain);
15292 }
15293
15294 case TREE_BINFO:
15295 /* We should never be tsubsting a binfo. */
15296 gcc_unreachable ();
15297
15298 case TREE_VEC:
15299 /* A vector of template arguments. */
15300 gcc_assert (!type);
15301 return tsubst_template_args (t, args, complain, in_decl);
15302
15303 case POINTER_TYPE:
15304 case REFERENCE_TYPE:
15305 {
15306 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15307 return t;
15308
15309 /* [temp.deduct]
15310
15311 Type deduction may fail for any of the following
15312 reasons:
15313
15314 -- Attempting to create a pointer to reference type.
15315 -- Attempting to create a reference to a reference type or
15316 a reference to void.
15317
15318 Core issue 106 says that creating a reference to a reference
15319 during instantiation is no longer a cause for failure. We
15320 only enforce this check in strict C++98 mode. */
15321 if ((TYPE_REF_P (type)
15322 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15323 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15324 {
15325 static location_t last_loc;
15326
15327 /* We keep track of the last time we issued this error
15328 message to avoid spewing a ton of messages during a
15329 single bad template instantiation. */
15330 if (complain & tf_error
15331 && last_loc != input_location)
15332 {
15333 if (VOID_TYPE_P (type))
15334 error ("forming reference to void");
15335 else if (code == POINTER_TYPE)
15336 error ("forming pointer to reference type %qT", type);
15337 else
15338 error ("forming reference to reference type %qT", type);
15339 last_loc = input_location;
15340 }
15341
15342 return error_mark_node;
15343 }
15344 else if (TREE_CODE (type) == FUNCTION_TYPE
15345 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15346 || type_memfn_rqual (type) != REF_QUAL_NONE))
15347 {
15348 if (complain & tf_error)
15349 {
15350 if (code == POINTER_TYPE)
15351 error ("forming pointer to qualified function type %qT",
15352 type);
15353 else
15354 error ("forming reference to qualified function type %qT",
15355 type);
15356 }
15357 return error_mark_node;
15358 }
15359 else if (code == POINTER_TYPE)
15360 {
15361 r = build_pointer_type (type);
15362 if (TREE_CODE (type) == METHOD_TYPE)
15363 r = build_ptrmemfunc_type (r);
15364 }
15365 else if (TYPE_REF_P (type))
15366 /* In C++0x, during template argument substitution, when there is an
15367 attempt to create a reference to a reference type, reference
15368 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15369
15370 "If a template-argument for a template-parameter T names a type
15371 that is a reference to a type A, an attempt to create the type
15372 'lvalue reference to cv T' creates the type 'lvalue reference to
15373 A,' while an attempt to create the type type rvalue reference to
15374 cv T' creates the type T"
15375 */
15376 r = cp_build_reference_type
15377 (TREE_TYPE (type),
15378 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15379 else
15380 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15381 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15382
15383 if (r != error_mark_node)
15384 /* Will this ever be needed for TYPE_..._TO values? */
15385 layout_type (r);
15386
15387 return r;
15388 }
15389 case OFFSET_TYPE:
15390 {
15391 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15392 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15393 {
15394 /* [temp.deduct]
15395
15396 Type deduction may fail for any of the following
15397 reasons:
15398
15399 -- Attempting to create "pointer to member of T" when T
15400 is not a class type. */
15401 if (complain & tf_error)
15402 error ("creating pointer to member of non-class type %qT", r);
15403 return error_mark_node;
15404 }
15405 if (TYPE_REF_P (type))
15406 {
15407 if (complain & tf_error)
15408 error ("creating pointer to member reference type %qT", type);
15409 return error_mark_node;
15410 }
15411 if (VOID_TYPE_P (type))
15412 {
15413 if (complain & tf_error)
15414 error ("creating pointer to member of type void");
15415 return error_mark_node;
15416 }
15417 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15418 if (TREE_CODE (type) == FUNCTION_TYPE)
15419 {
15420 /* The type of the implicit object parameter gets its
15421 cv-qualifiers from the FUNCTION_TYPE. */
15422 tree memptr;
15423 tree method_type
15424 = build_memfn_type (type, r, type_memfn_quals (type),
15425 type_memfn_rqual (type));
15426 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15427 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15428 complain);
15429 }
15430 else
15431 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15432 cp_type_quals (t),
15433 complain);
15434 }
15435 case FUNCTION_TYPE:
15436 case METHOD_TYPE:
15437 {
15438 tree fntype;
15439 tree specs;
15440 fntype = tsubst_function_type (t, args, complain, in_decl);
15441 if (fntype == error_mark_node)
15442 return error_mark_node;
15443
15444 /* Substitute the exception specification. */
15445 specs = tsubst_exception_specification (t, args, complain, in_decl,
15446 /*defer_ok*/fndecl_type);
15447 if (specs == error_mark_node)
15448 return error_mark_node;
15449 if (specs)
15450 fntype = build_exception_variant (fntype, specs);
15451 return fntype;
15452 }
15453 case ARRAY_TYPE:
15454 {
15455 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15456 if (domain == error_mark_node)
15457 return error_mark_node;
15458
15459 /* As an optimization, we avoid regenerating the array type if
15460 it will obviously be the same as T. */
15461 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15462 return t;
15463
15464 /* These checks should match the ones in create_array_type_for_decl.
15465
15466 [temp.deduct]
15467
15468 The deduction may fail for any of the following reasons:
15469
15470 -- Attempting to create an array with an element type that
15471 is void, a function type, or a reference type, or [DR337]
15472 an abstract class type. */
15473 if (VOID_TYPE_P (type)
15474 || TREE_CODE (type) == FUNCTION_TYPE
15475 || (TREE_CODE (type) == ARRAY_TYPE
15476 && TYPE_DOMAIN (type) == NULL_TREE)
15477 || TYPE_REF_P (type))
15478 {
15479 if (complain & tf_error)
15480 error ("creating array of %qT", type);
15481 return error_mark_node;
15482 }
15483
15484 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15485 return error_mark_node;
15486
15487 r = build_cplus_array_type (type, domain);
15488
15489 if (!valid_array_size_p (input_location, r, in_decl,
15490 (complain & tf_error)))
15491 return error_mark_node;
15492
15493 if (TYPE_USER_ALIGN (t))
15494 {
15495 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15496 TYPE_USER_ALIGN (r) = 1;
15497 }
15498
15499 return r;
15500 }
15501
15502 case TYPENAME_TYPE:
15503 {
15504 tree ctx = TYPE_CONTEXT (t);
15505 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15506 {
15507 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15508 if (ctx == error_mark_node
15509 || TREE_VEC_LENGTH (ctx) > 1)
15510 return error_mark_node;
15511 if (TREE_VEC_LENGTH (ctx) == 0)
15512 {
15513 if (complain & tf_error)
15514 error ("%qD is instantiated for an empty pack",
15515 TYPENAME_TYPE_FULLNAME (t));
15516 return error_mark_node;
15517 }
15518 ctx = TREE_VEC_ELT (ctx, 0);
15519 }
15520 else
15521 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15522 /*entering_scope=*/1);
15523 if (ctx == error_mark_node)
15524 return error_mark_node;
15525
15526 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15527 complain, in_decl);
15528 if (f == error_mark_node)
15529 return error_mark_node;
15530
15531 if (!MAYBE_CLASS_TYPE_P (ctx))
15532 {
15533 if (complain & tf_error)
15534 error ("%qT is not a class, struct, or union type", ctx);
15535 return error_mark_node;
15536 }
15537 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15538 {
15539 /* Normally, make_typename_type does not require that the CTX
15540 have complete type in order to allow things like:
15541
15542 template <class T> struct S { typename S<T>::X Y; };
15543
15544 But, such constructs have already been resolved by this
15545 point, so here CTX really should have complete type, unless
15546 it's a partial instantiation. */
15547 ctx = complete_type (ctx);
15548 if (!COMPLETE_TYPE_P (ctx))
15549 {
15550 if (complain & tf_error)
15551 cxx_incomplete_type_error (NULL_TREE, ctx);
15552 return error_mark_node;
15553 }
15554 }
15555
15556 f = make_typename_type (ctx, f, typename_type,
15557 complain | tf_keep_type_decl);
15558 if (f == error_mark_node)
15559 return f;
15560 if (TREE_CODE (f) == TYPE_DECL)
15561 {
15562 complain |= tf_ignore_bad_quals;
15563 f = TREE_TYPE (f);
15564 }
15565
15566 if (TREE_CODE (f) != TYPENAME_TYPE)
15567 {
15568 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15569 {
15570 if (complain & tf_error)
15571 error ("%qT resolves to %qT, which is not an enumeration type",
15572 t, f);
15573 else
15574 return error_mark_node;
15575 }
15576 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15577 {
15578 if (complain & tf_error)
15579 error ("%qT resolves to %qT, which is is not a class type",
15580 t, f);
15581 else
15582 return error_mark_node;
15583 }
15584 }
15585
15586 return cp_build_qualified_type_real
15587 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15588 }
15589
15590 case UNBOUND_CLASS_TEMPLATE:
15591 {
15592 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15593 in_decl, /*entering_scope=*/1);
15594 tree name = TYPE_IDENTIFIER (t);
15595 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15596
15597 if (ctx == error_mark_node || name == error_mark_node)
15598 return error_mark_node;
15599
15600 if (parm_list)
15601 parm_list = tsubst_template_parms (parm_list, args, complain);
15602 return make_unbound_class_template (ctx, name, parm_list, complain);
15603 }
15604
15605 case TYPEOF_TYPE:
15606 {
15607 tree type;
15608
15609 ++cp_unevaluated_operand;
15610 ++c_inhibit_evaluation_warnings;
15611
15612 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15613 complain, in_decl,
15614 /*integral_constant_expression_p=*/false);
15615
15616 --cp_unevaluated_operand;
15617 --c_inhibit_evaluation_warnings;
15618
15619 type = finish_typeof (type);
15620 return cp_build_qualified_type_real (type,
15621 cp_type_quals (t)
15622 | cp_type_quals (type),
15623 complain);
15624 }
15625
15626 case DECLTYPE_TYPE:
15627 {
15628 tree type;
15629
15630 ++cp_unevaluated_operand;
15631 ++c_inhibit_evaluation_warnings;
15632
15633 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15634 complain|tf_decltype, in_decl,
15635 /*function_p*/false,
15636 /*integral_constant_expression*/false);
15637
15638 --cp_unevaluated_operand;
15639 --c_inhibit_evaluation_warnings;
15640
15641 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15642 type = lambda_capture_field_type (type,
15643 false /*explicit_init*/,
15644 DECLTYPE_FOR_REF_CAPTURE (t));
15645 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15646 type = lambda_proxy_type (type);
15647 else
15648 {
15649 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15650 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15651 && EXPR_P (type))
15652 /* In a template ~id could be either a complement expression
15653 or an unqualified-id naming a destructor; if instantiating
15654 it produces an expression, it's not an id-expression or
15655 member access. */
15656 id = false;
15657 type = finish_decltype_type (type, id, complain);
15658 }
15659 return cp_build_qualified_type_real (type,
15660 cp_type_quals (t)
15661 | cp_type_quals (type),
15662 complain | tf_ignore_bad_quals);
15663 }
15664
15665 case UNDERLYING_TYPE:
15666 {
15667 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15668 complain, in_decl);
15669 return finish_underlying_type (type);
15670 }
15671
15672 case TYPE_ARGUMENT_PACK:
15673 case NONTYPE_ARGUMENT_PACK:
15674 {
15675 tree r;
15676
15677 if (code == NONTYPE_ARGUMENT_PACK)
15678 r = make_node (code);
15679 else
15680 r = cxx_make_type (code);
15681
15682 tree pack_args = ARGUMENT_PACK_ARGS (t);
15683 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15684 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15685
15686 return r;
15687 }
15688
15689 case VOID_CST:
15690 case INTEGER_CST:
15691 case REAL_CST:
15692 case STRING_CST:
15693 case PLUS_EXPR:
15694 case MINUS_EXPR:
15695 case NEGATE_EXPR:
15696 case NOP_EXPR:
15697 case INDIRECT_REF:
15698 case ADDR_EXPR:
15699 case CALL_EXPR:
15700 case ARRAY_REF:
15701 case SCOPE_REF:
15702 /* We should use one of the expression tsubsts for these codes. */
15703 gcc_unreachable ();
15704
15705 default:
15706 sorry ("use of %qs in template", get_tree_code_name (code));
15707 return error_mark_node;
15708 }
15709 }
15710
15711 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15712 expression on the left-hand side of the "." or "->" operator. We
15713 only do the lookup if we had a dependent BASELINK. Otherwise we
15714 adjust it onto the instantiated heirarchy. */
15715
15716 static tree
15717 tsubst_baselink (tree baselink, tree object_type,
15718 tree args, tsubst_flags_t complain, tree in_decl)
15719 {
15720 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15721 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15722 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15723
15724 tree optype = BASELINK_OPTYPE (baselink);
15725 optype = tsubst (optype, args, complain, in_decl);
15726
15727 tree template_args = NULL_TREE;
15728 bool template_id_p = false;
15729 tree fns = BASELINK_FUNCTIONS (baselink);
15730 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15731 {
15732 template_id_p = true;
15733 template_args = TREE_OPERAND (fns, 1);
15734 fns = TREE_OPERAND (fns, 0);
15735 if (template_args)
15736 template_args = tsubst_template_args (template_args, args,
15737 complain, in_decl);
15738 }
15739
15740 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15741 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15742 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15743
15744 if (dependent_p)
15745 {
15746 tree name = OVL_NAME (fns);
15747 if (IDENTIFIER_CONV_OP_P (name))
15748 name = make_conv_op_name (optype);
15749
15750 if (name == complete_dtor_identifier)
15751 /* Treat as-if non-dependent below. */
15752 dependent_p = false;
15753
15754 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15755 if (!baselink)
15756 {
15757 if ((complain & tf_error)
15758 && constructor_name_p (name, qualifying_scope))
15759 error ("cannot call constructor %<%T::%D%> directly",
15760 qualifying_scope, name);
15761 return error_mark_node;
15762 }
15763
15764 if (BASELINK_P (baselink))
15765 fns = BASELINK_FUNCTIONS (baselink);
15766 }
15767 else
15768 /* We're going to overwrite pieces below, make a duplicate. */
15769 baselink = copy_node (baselink);
15770
15771 /* If lookup found a single function, mark it as used at this point.
15772 (If lookup found multiple functions the one selected later by
15773 overload resolution will be marked as used at that point.) */
15774 if (!template_id_p && !really_overloaded_fn (fns))
15775 {
15776 tree fn = OVL_FIRST (fns);
15777 bool ok = mark_used (fn, complain);
15778 if (!ok && !(complain & tf_error))
15779 return error_mark_node;
15780 if (ok && BASELINK_P (baselink))
15781 /* We might have instantiated an auto function. */
15782 TREE_TYPE (baselink) = TREE_TYPE (fn);
15783 }
15784
15785 if (BASELINK_P (baselink))
15786 {
15787 /* Add back the template arguments, if present. */
15788 if (template_id_p)
15789 BASELINK_FUNCTIONS (baselink)
15790 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15791
15792 /* Update the conversion operator type. */
15793 BASELINK_OPTYPE (baselink) = optype;
15794 }
15795
15796 if (!object_type)
15797 object_type = current_class_type;
15798
15799 if (qualified_p || !dependent_p)
15800 {
15801 baselink = adjust_result_of_qualified_name_lookup (baselink,
15802 qualifying_scope,
15803 object_type);
15804 if (!qualified_p)
15805 /* We need to call adjust_result_of_qualified_name_lookup in case the
15806 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15807 so that we still get virtual function binding. */
15808 BASELINK_QUALIFIED_P (baselink) = false;
15809 }
15810
15811 return baselink;
15812 }
15813
15814 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15815 true if the qualified-id will be a postfix-expression in-and-of
15816 itself; false if more of the postfix-expression follows the
15817 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15818 of "&". */
15819
15820 static tree
15821 tsubst_qualified_id (tree qualified_id, tree args,
15822 tsubst_flags_t complain, tree in_decl,
15823 bool done, bool address_p)
15824 {
15825 tree expr;
15826 tree scope;
15827 tree name;
15828 bool is_template;
15829 tree template_args;
15830 location_t loc = UNKNOWN_LOCATION;
15831
15832 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15833
15834 /* Figure out what name to look up. */
15835 name = TREE_OPERAND (qualified_id, 1);
15836 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15837 {
15838 is_template = true;
15839 loc = EXPR_LOCATION (name);
15840 template_args = TREE_OPERAND (name, 1);
15841 if (template_args)
15842 template_args = tsubst_template_args (template_args, args,
15843 complain, in_decl);
15844 if (template_args == error_mark_node)
15845 return error_mark_node;
15846 name = TREE_OPERAND (name, 0);
15847 }
15848 else
15849 {
15850 is_template = false;
15851 template_args = NULL_TREE;
15852 }
15853
15854 /* Substitute into the qualifying scope. When there are no ARGS, we
15855 are just trying to simplify a non-dependent expression. In that
15856 case the qualifying scope may be dependent, and, in any case,
15857 substituting will not help. */
15858 scope = TREE_OPERAND (qualified_id, 0);
15859 if (args)
15860 {
15861 scope = tsubst (scope, args, complain, in_decl);
15862 expr = tsubst_copy (name, args, complain, in_decl);
15863 }
15864 else
15865 expr = name;
15866
15867 if (dependent_scope_p (scope))
15868 {
15869 if (is_template)
15870 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15871 tree r = build_qualified_name (NULL_TREE, scope, expr,
15872 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15873 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15874 return r;
15875 }
15876
15877 if (!BASELINK_P (name) && !DECL_P (expr))
15878 {
15879 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15880 {
15881 /* A BIT_NOT_EXPR is used to represent a destructor. */
15882 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15883 {
15884 error ("qualifying type %qT does not match destructor name ~%qT",
15885 scope, TREE_OPERAND (expr, 0));
15886 expr = error_mark_node;
15887 }
15888 else
15889 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15890 /*is_type_p=*/0, false);
15891 }
15892 else
15893 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15894 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15895 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15896 {
15897 if (complain & tf_error)
15898 {
15899 error ("dependent-name %qE is parsed as a non-type, but "
15900 "instantiation yields a type", qualified_id);
15901 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15902 }
15903 return error_mark_node;
15904 }
15905 }
15906
15907 if (DECL_P (expr))
15908 {
15909 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15910 scope);
15911 /* Remember that there was a reference to this entity. */
15912 if (!mark_used (expr, complain) && !(complain & tf_error))
15913 return error_mark_node;
15914 }
15915
15916 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15917 {
15918 if (complain & tf_error)
15919 qualified_name_lookup_error (scope,
15920 TREE_OPERAND (qualified_id, 1),
15921 expr, input_location);
15922 return error_mark_node;
15923 }
15924
15925 if (is_template)
15926 {
15927 /* We may be repeating a check already done during parsing, but
15928 if it was well-formed and passed then, it will pass again
15929 now, and if it didn't, we wouldn't have got here. The case
15930 we want to catch is when we couldn't tell then, and can now,
15931 namely when templ prior to substitution was an
15932 identifier. */
15933 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15934 return error_mark_node;
15935
15936 if (variable_template_p (expr))
15937 expr = lookup_and_finish_template_variable (expr, template_args,
15938 complain);
15939 else
15940 expr = lookup_template_function (expr, template_args);
15941 }
15942
15943 if (expr == error_mark_node && complain & tf_error)
15944 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15945 expr, input_location);
15946 else if (TYPE_P (scope))
15947 {
15948 expr = (adjust_result_of_qualified_name_lookup
15949 (expr, scope, current_nonlambda_class_type ()));
15950 expr = (finish_qualified_id_expr
15951 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15952 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15953 /*template_arg_p=*/false, complain));
15954 }
15955
15956 /* Expressions do not generally have reference type. */
15957 if (TREE_CODE (expr) != SCOPE_REF
15958 /* However, if we're about to form a pointer-to-member, we just
15959 want the referenced member referenced. */
15960 && TREE_CODE (expr) != OFFSET_REF)
15961 expr = convert_from_reference (expr);
15962
15963 if (REF_PARENTHESIZED_P (qualified_id))
15964 expr = force_paren_expr (expr);
15965
15966 return expr;
15967 }
15968
15969 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15970 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15971 for tsubst. */
15972
15973 static tree
15974 tsubst_init (tree init, tree decl, tree args,
15975 tsubst_flags_t complain, tree in_decl)
15976 {
15977 if (!init)
15978 return NULL_TREE;
15979
15980 init = tsubst_expr (init, args, complain, in_decl, false);
15981
15982 tree type = TREE_TYPE (decl);
15983
15984 if (!init && type != error_mark_node)
15985 {
15986 if (tree auto_node = type_uses_auto (type))
15987 {
15988 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15989 {
15990 if (complain & tf_error)
15991 error ("initializer for %q#D expands to an empty list "
15992 "of expressions", decl);
15993 return error_mark_node;
15994 }
15995 }
15996 else if (!dependent_type_p (type))
15997 {
15998 /* If we had an initializer but it
15999 instantiated to nothing,
16000 value-initialize the object. This will
16001 only occur when the initializer was a
16002 pack expansion where the parameter packs
16003 used in that expansion were of length
16004 zero. */
16005 init = build_value_init (type, complain);
16006 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16007 init = get_target_expr_sfinae (init, complain);
16008 if (TREE_CODE (init) == TARGET_EXPR)
16009 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16010 }
16011 }
16012
16013 return init;
16014 }
16015
16016 /* Like tsubst, but deals with expressions. This function just replaces
16017 template parms; to finish processing the resultant expression, use
16018 tsubst_copy_and_build or tsubst_expr. */
16019
16020 static tree
16021 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16022 {
16023 enum tree_code code;
16024 tree r;
16025
16026 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16027 return t;
16028
16029 code = TREE_CODE (t);
16030
16031 switch (code)
16032 {
16033 case PARM_DECL:
16034 r = retrieve_local_specialization (t);
16035
16036 if (r == NULL_TREE)
16037 {
16038 /* We get here for a use of 'this' in an NSDMI. */
16039 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16040 return current_class_ptr;
16041
16042 /* This can happen for a parameter name used later in a function
16043 declaration (such as in a late-specified return type). Just
16044 make a dummy decl, since it's only used for its type. */
16045 gcc_assert (cp_unevaluated_operand != 0);
16046 r = tsubst_decl (t, args, complain);
16047 /* Give it the template pattern as its context; its true context
16048 hasn't been instantiated yet and this is good enough for
16049 mangling. */
16050 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16051 }
16052
16053 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16054 r = argument_pack_select_arg (r);
16055 if (!mark_used (r, complain) && !(complain & tf_error))
16056 return error_mark_node;
16057 return r;
16058
16059 case CONST_DECL:
16060 {
16061 tree enum_type;
16062 tree v;
16063
16064 if (DECL_TEMPLATE_PARM_P (t))
16065 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16066 /* There is no need to substitute into namespace-scope
16067 enumerators. */
16068 if (DECL_NAMESPACE_SCOPE_P (t))
16069 return t;
16070 /* If ARGS is NULL, then T is known to be non-dependent. */
16071 if (args == NULL_TREE)
16072 return scalar_constant_value (t);
16073
16074 /* Unfortunately, we cannot just call lookup_name here.
16075 Consider:
16076
16077 template <int I> int f() {
16078 enum E { a = I };
16079 struct S { void g() { E e = a; } };
16080 };
16081
16082 When we instantiate f<7>::S::g(), say, lookup_name is not
16083 clever enough to find f<7>::a. */
16084 enum_type
16085 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16086 /*entering_scope=*/0);
16087
16088 for (v = TYPE_VALUES (enum_type);
16089 v != NULL_TREE;
16090 v = TREE_CHAIN (v))
16091 if (TREE_PURPOSE (v) == DECL_NAME (t))
16092 return TREE_VALUE (v);
16093
16094 /* We didn't find the name. That should never happen; if
16095 name-lookup found it during preliminary parsing, we
16096 should find it again here during instantiation. */
16097 gcc_unreachable ();
16098 }
16099 return t;
16100
16101 case FIELD_DECL:
16102 if (DECL_CONTEXT (t))
16103 {
16104 tree ctx;
16105
16106 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16107 /*entering_scope=*/1);
16108 if (ctx != DECL_CONTEXT (t))
16109 {
16110 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16111 if (!r)
16112 {
16113 if (complain & tf_error)
16114 error ("using invalid field %qD", t);
16115 return error_mark_node;
16116 }
16117 return r;
16118 }
16119 }
16120
16121 return t;
16122
16123 case VAR_DECL:
16124 case FUNCTION_DECL:
16125 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16126 r = tsubst (t, args, complain, in_decl);
16127 else if (local_variable_p (t)
16128 && uses_template_parms (DECL_CONTEXT (t)))
16129 {
16130 r = retrieve_local_specialization (t);
16131 if (r == NULL_TREE)
16132 {
16133 /* First try name lookup to find the instantiation. */
16134 r = lookup_name (DECL_NAME (t));
16135 if (r)
16136 {
16137 if (!VAR_P (r))
16138 {
16139 /* During error-recovery we may find a non-variable,
16140 even an OVERLOAD: just bail out and avoid ICEs and
16141 duplicate diagnostics (c++/62207). */
16142 gcc_assert (seen_error ());
16143 return error_mark_node;
16144 }
16145 if (!is_capture_proxy (r))
16146 {
16147 /* Make sure the one we found is the one we want. */
16148 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16149 if (ctx != DECL_CONTEXT (r))
16150 r = NULL_TREE;
16151 }
16152 }
16153
16154 if (r)
16155 /* OK */;
16156 else
16157 {
16158 /* This can happen for a variable used in a
16159 late-specified return type of a local lambda, or for a
16160 local static or constant. Building a new VAR_DECL
16161 should be OK in all those cases. */
16162 r = tsubst_decl (t, args, complain);
16163 if (local_specializations)
16164 /* Avoid infinite recursion (79640). */
16165 register_local_specialization (r, t);
16166 if (decl_maybe_constant_var_p (r))
16167 {
16168 /* We can't call cp_finish_decl, so handle the
16169 initializer by hand. */
16170 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16171 complain, in_decl);
16172 if (!processing_template_decl)
16173 init = maybe_constant_init (init);
16174 if (processing_template_decl
16175 ? potential_constant_expression (init)
16176 : reduced_constant_expression_p (init))
16177 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16178 = TREE_CONSTANT (r) = true;
16179 DECL_INITIAL (r) = init;
16180 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16181 TREE_TYPE (r)
16182 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16183 complain, adc_variable_type);
16184 }
16185 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16186 || decl_constant_var_p (r)
16187 || seen_error ());
16188 if (!processing_template_decl
16189 && !TREE_STATIC (r))
16190 r = process_outer_var_ref (r, complain);
16191 }
16192 /* Remember this for subsequent uses. */
16193 if (local_specializations)
16194 register_local_specialization (r, t);
16195 }
16196 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16197 r = argument_pack_select_arg (r);
16198 }
16199 else
16200 r = t;
16201 if (!mark_used (r, complain))
16202 return error_mark_node;
16203 return r;
16204
16205 case NAMESPACE_DECL:
16206 return t;
16207
16208 case OVERLOAD:
16209 return t;
16210
16211 case BASELINK:
16212 return tsubst_baselink (t, current_nonlambda_class_type (),
16213 args, complain, in_decl);
16214
16215 case TEMPLATE_DECL:
16216 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16217 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16218 args, complain, in_decl);
16219 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16220 return tsubst (t, args, complain, in_decl);
16221 else if (DECL_CLASS_SCOPE_P (t)
16222 && uses_template_parms (DECL_CONTEXT (t)))
16223 {
16224 /* Template template argument like the following example need
16225 special treatment:
16226
16227 template <template <class> class TT> struct C {};
16228 template <class T> struct D {
16229 template <class U> struct E {};
16230 C<E> c; // #1
16231 };
16232 D<int> d; // #2
16233
16234 We are processing the template argument `E' in #1 for
16235 the template instantiation #2. Originally, `E' is a
16236 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16237 have to substitute this with one having context `D<int>'. */
16238
16239 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16240 if (dependent_scope_p (context))
16241 {
16242 /* When rewriting a constructor into a deduction guide, a
16243 non-dependent name can become dependent, so memtmpl<args>
16244 becomes context::template memtmpl<args>. */
16245 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16246 return build_qualified_name (type, context, DECL_NAME (t),
16247 /*template*/true);
16248 }
16249 return lookup_field (context, DECL_NAME(t), 0, false);
16250 }
16251 else
16252 /* Ordinary template template argument. */
16253 return t;
16254
16255 case NON_LVALUE_EXPR:
16256 case VIEW_CONVERT_EXPR:
16257 {
16258 /* Handle location wrappers by substituting the wrapped node
16259 first, *then* reusing the resulting type. Doing the type
16260 first ensures that we handle template parameters and
16261 parameter pack expansions. */
16262 if (location_wrapper_p (t))
16263 {
16264 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16265 complain, in_decl);
16266 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16267 }
16268 tree op = TREE_OPERAND (t, 0);
16269 if (code == VIEW_CONVERT_EXPR
16270 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16271 {
16272 /* Wrapper to make a C++20 template parameter object const. */
16273 op = tsubst_copy (op, args, complain, in_decl);
16274 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16275 {
16276 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16277 return build1 (code, type, op);
16278 }
16279 else
16280 {
16281 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
16282 return op;
16283 }
16284 }
16285 /* We shouldn't see any other uses of these in templates. */
16286 gcc_unreachable ();
16287 }
16288
16289 case CAST_EXPR:
16290 case REINTERPRET_CAST_EXPR:
16291 case CONST_CAST_EXPR:
16292 case STATIC_CAST_EXPR:
16293 case DYNAMIC_CAST_EXPR:
16294 case IMPLICIT_CONV_EXPR:
16295 case CONVERT_EXPR:
16296 case NOP_EXPR:
16297 {
16298 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16299 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16300 return build1 (code, type, op0);
16301 }
16302
16303 case SIZEOF_EXPR:
16304 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16305 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16306 {
16307 tree expanded, op = TREE_OPERAND (t, 0);
16308 int len = 0;
16309
16310 if (SIZEOF_EXPR_TYPE_P (t))
16311 op = TREE_TYPE (op);
16312
16313 ++cp_unevaluated_operand;
16314 ++c_inhibit_evaluation_warnings;
16315 /* We only want to compute the number of arguments. */
16316 if (PACK_EXPANSION_P (op))
16317 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16318 else
16319 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16320 args, complain, in_decl);
16321 --cp_unevaluated_operand;
16322 --c_inhibit_evaluation_warnings;
16323
16324 if (TREE_CODE (expanded) == TREE_VEC)
16325 {
16326 len = TREE_VEC_LENGTH (expanded);
16327 /* Set TREE_USED for the benefit of -Wunused. */
16328 for (int i = 0; i < len; i++)
16329 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16330 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16331 }
16332
16333 if (expanded == error_mark_node)
16334 return error_mark_node;
16335 else if (PACK_EXPANSION_P (expanded)
16336 || (TREE_CODE (expanded) == TREE_VEC
16337 && pack_expansion_args_count (expanded)))
16338
16339 {
16340 if (PACK_EXPANSION_P (expanded))
16341 /* OK. */;
16342 else if (TREE_VEC_LENGTH (expanded) == 1)
16343 expanded = TREE_VEC_ELT (expanded, 0);
16344 else
16345 expanded = make_argument_pack (expanded);
16346
16347 if (TYPE_P (expanded))
16348 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
16349 false,
16350 complain & tf_error);
16351 else
16352 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
16353 complain & tf_error);
16354 }
16355 else
16356 return build_int_cst (size_type_node, len);
16357 }
16358 if (SIZEOF_EXPR_TYPE_P (t))
16359 {
16360 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16361 args, complain, in_decl);
16362 r = build1 (NOP_EXPR, r, error_mark_node);
16363 r = build1 (SIZEOF_EXPR,
16364 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16365 SIZEOF_EXPR_TYPE_P (r) = 1;
16366 return r;
16367 }
16368 /* Fall through */
16369
16370 case INDIRECT_REF:
16371 case NEGATE_EXPR:
16372 case TRUTH_NOT_EXPR:
16373 case BIT_NOT_EXPR:
16374 case ADDR_EXPR:
16375 case UNARY_PLUS_EXPR: /* Unary + */
16376 case ALIGNOF_EXPR:
16377 case AT_ENCODE_EXPR:
16378 case ARROW_EXPR:
16379 case THROW_EXPR:
16380 case TYPEID_EXPR:
16381 case REALPART_EXPR:
16382 case IMAGPART_EXPR:
16383 case PAREN_EXPR:
16384 {
16385 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16386 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16387 r = build1 (code, type, op0);
16388 if (code == ALIGNOF_EXPR)
16389 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16390 return r;
16391 }
16392
16393 case COMPONENT_REF:
16394 {
16395 tree object;
16396 tree name;
16397
16398 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16399 name = TREE_OPERAND (t, 1);
16400 if (TREE_CODE (name) == BIT_NOT_EXPR)
16401 {
16402 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16403 complain, in_decl);
16404 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16405 }
16406 else if (TREE_CODE (name) == SCOPE_REF
16407 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16408 {
16409 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16410 complain, in_decl);
16411 name = TREE_OPERAND (name, 1);
16412 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16413 complain, in_decl);
16414 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16415 name = build_qualified_name (/*type=*/NULL_TREE,
16416 base, name,
16417 /*template_p=*/false);
16418 }
16419 else if (BASELINK_P (name))
16420 name = tsubst_baselink (name,
16421 non_reference (TREE_TYPE (object)),
16422 args, complain,
16423 in_decl);
16424 else
16425 name = tsubst_copy (name, args, complain, in_decl);
16426 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
16427 }
16428
16429 case PLUS_EXPR:
16430 case MINUS_EXPR:
16431 case MULT_EXPR:
16432 case TRUNC_DIV_EXPR:
16433 case CEIL_DIV_EXPR:
16434 case FLOOR_DIV_EXPR:
16435 case ROUND_DIV_EXPR:
16436 case EXACT_DIV_EXPR:
16437 case BIT_AND_EXPR:
16438 case BIT_IOR_EXPR:
16439 case BIT_XOR_EXPR:
16440 case TRUNC_MOD_EXPR:
16441 case FLOOR_MOD_EXPR:
16442 case TRUTH_ANDIF_EXPR:
16443 case TRUTH_ORIF_EXPR:
16444 case TRUTH_AND_EXPR:
16445 case TRUTH_OR_EXPR:
16446 case RSHIFT_EXPR:
16447 case LSHIFT_EXPR:
16448 case EQ_EXPR:
16449 case NE_EXPR:
16450 case MAX_EXPR:
16451 case MIN_EXPR:
16452 case LE_EXPR:
16453 case GE_EXPR:
16454 case LT_EXPR:
16455 case GT_EXPR:
16456 case COMPOUND_EXPR:
16457 case DOTSTAR_EXPR:
16458 case MEMBER_REF:
16459 case PREDECREMENT_EXPR:
16460 case PREINCREMENT_EXPR:
16461 case POSTDECREMENT_EXPR:
16462 case POSTINCREMENT_EXPR:
16463 {
16464 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16465 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16466 return build_nt (code, op0, op1);
16467 }
16468
16469 case SCOPE_REF:
16470 {
16471 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16472 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16473 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
16474 QUALIFIED_NAME_IS_TEMPLATE (t));
16475 }
16476
16477 case ARRAY_REF:
16478 {
16479 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16480 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16481 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16482 }
16483
16484 case CALL_EXPR:
16485 {
16486 int n = VL_EXP_OPERAND_LENGTH (t);
16487 tree result = build_vl_exp (CALL_EXPR, n);
16488 int i;
16489 for (i = 0; i < n; i++)
16490 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16491 complain, in_decl);
16492 return result;
16493 }
16494
16495 case COND_EXPR:
16496 case MODOP_EXPR:
16497 case PSEUDO_DTOR_EXPR:
16498 case VEC_PERM_EXPR:
16499 {
16500 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16501 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16502 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16503 r = build_nt (code, op0, op1, op2);
16504 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16505 return r;
16506 }
16507
16508 case NEW_EXPR:
16509 {
16510 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16511 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16512 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16513 r = build_nt (code, op0, op1, op2);
16514 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16515 return r;
16516 }
16517
16518 case DELETE_EXPR:
16519 {
16520 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16521 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16522 r = build_nt (code, op0, op1);
16523 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16524 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16525 return r;
16526 }
16527
16528 case TEMPLATE_ID_EXPR:
16529 {
16530 /* Substituted template arguments */
16531 tree fn = TREE_OPERAND (t, 0);
16532 tree targs = TREE_OPERAND (t, 1);
16533
16534 fn = tsubst_copy (fn, args, complain, in_decl);
16535 if (targs)
16536 targs = tsubst_template_args (targs, args, complain, in_decl);
16537
16538 return lookup_template_function (fn, targs);
16539 }
16540
16541 case TREE_LIST:
16542 {
16543 tree purpose, value, chain;
16544
16545 if (t == void_list_node)
16546 return t;
16547
16548 purpose = TREE_PURPOSE (t);
16549 if (purpose)
16550 purpose = tsubst_copy (purpose, args, complain, in_decl);
16551 value = TREE_VALUE (t);
16552 if (value)
16553 value = tsubst_copy (value, args, complain, in_decl);
16554 chain = TREE_CHAIN (t);
16555 if (chain && chain != void_type_node)
16556 chain = tsubst_copy (chain, args, complain, in_decl);
16557 if (purpose == TREE_PURPOSE (t)
16558 && value == TREE_VALUE (t)
16559 && chain == TREE_CHAIN (t))
16560 return t;
16561 return tree_cons (purpose, value, chain);
16562 }
16563
16564 case RECORD_TYPE:
16565 case UNION_TYPE:
16566 case ENUMERAL_TYPE:
16567 case INTEGER_TYPE:
16568 case TEMPLATE_TYPE_PARM:
16569 case TEMPLATE_TEMPLATE_PARM:
16570 case BOUND_TEMPLATE_TEMPLATE_PARM:
16571 case TEMPLATE_PARM_INDEX:
16572 case POINTER_TYPE:
16573 case REFERENCE_TYPE:
16574 case OFFSET_TYPE:
16575 case FUNCTION_TYPE:
16576 case METHOD_TYPE:
16577 case ARRAY_TYPE:
16578 case TYPENAME_TYPE:
16579 case UNBOUND_CLASS_TEMPLATE:
16580 case TYPEOF_TYPE:
16581 case DECLTYPE_TYPE:
16582 case TYPE_DECL:
16583 return tsubst (t, args, complain, in_decl);
16584
16585 case USING_DECL:
16586 t = DECL_NAME (t);
16587 /* Fall through. */
16588 case IDENTIFIER_NODE:
16589 if (IDENTIFIER_CONV_OP_P (t))
16590 {
16591 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16592 return make_conv_op_name (new_type);
16593 }
16594 else
16595 return t;
16596
16597 case CONSTRUCTOR:
16598 /* This is handled by tsubst_copy_and_build. */
16599 gcc_unreachable ();
16600
16601 case VA_ARG_EXPR:
16602 {
16603 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16604 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16605 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16606 }
16607
16608 case CLEANUP_POINT_EXPR:
16609 /* We shouldn't have built any of these during initial template
16610 generation. Instead, they should be built during instantiation
16611 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16612 gcc_unreachable ();
16613
16614 case OFFSET_REF:
16615 {
16616 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16617 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16618 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16619 r = build2 (code, type, op0, op1);
16620 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16621 if (!mark_used (TREE_OPERAND (r, 1), complain)
16622 && !(complain & tf_error))
16623 return error_mark_node;
16624 return r;
16625 }
16626
16627 case EXPR_PACK_EXPANSION:
16628 error ("invalid use of pack expansion expression");
16629 return error_mark_node;
16630
16631 case NONTYPE_ARGUMENT_PACK:
16632 error ("use %<...%> to expand argument pack");
16633 return error_mark_node;
16634
16635 case VOID_CST:
16636 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16637 return t;
16638
16639 case INTEGER_CST:
16640 case REAL_CST:
16641 case STRING_CST:
16642 case COMPLEX_CST:
16643 {
16644 /* Instantiate any typedefs in the type. */
16645 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16646 r = fold_convert (type, t);
16647 gcc_assert (TREE_CODE (r) == code);
16648 return r;
16649 }
16650
16651 case PTRMEM_CST:
16652 /* These can sometimes show up in a partial instantiation, but never
16653 involve template parms. */
16654 gcc_assert (!uses_template_parms (t));
16655 return t;
16656
16657 case UNARY_LEFT_FOLD_EXPR:
16658 return tsubst_unary_left_fold (t, args, complain, in_decl);
16659 case UNARY_RIGHT_FOLD_EXPR:
16660 return tsubst_unary_right_fold (t, args, complain, in_decl);
16661 case BINARY_LEFT_FOLD_EXPR:
16662 return tsubst_binary_left_fold (t, args, complain, in_decl);
16663 case BINARY_RIGHT_FOLD_EXPR:
16664 return tsubst_binary_right_fold (t, args, complain, in_decl);
16665 case PREDICT_EXPR:
16666 return t;
16667
16668 case DEBUG_BEGIN_STMT:
16669 /* ??? There's no point in copying it for now, but maybe some
16670 day it will contain more information, such as a pointer back
16671 to the containing function, inlined copy or so. */
16672 return t;
16673
16674 default:
16675 /* We shouldn't get here, but keep going if !flag_checking. */
16676 if (flag_checking)
16677 gcc_unreachable ();
16678 return t;
16679 }
16680 }
16681
16682 /* Helper function for tsubst_omp_clauses, used for instantiation of
16683 OMP_CLAUSE_DECL of clauses. */
16684
16685 static tree
16686 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16687 tree in_decl, tree *iterator_cache)
16688 {
16689 if (decl == NULL_TREE)
16690 return NULL_TREE;
16691
16692 /* Handle OpenMP iterators. */
16693 if (TREE_CODE (decl) == TREE_LIST
16694 && TREE_PURPOSE (decl)
16695 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16696 {
16697 tree ret;
16698 if (iterator_cache[0] == TREE_PURPOSE (decl))
16699 ret = iterator_cache[1];
16700 else
16701 {
16702 tree *tp = &ret;
16703 begin_scope (sk_omp, NULL);
16704 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16705 {
16706 *tp = copy_node (it);
16707 TREE_VEC_ELT (*tp, 0)
16708 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16709 TREE_VEC_ELT (*tp, 1)
16710 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16711 /*integral_constant_expression_p=*/false);
16712 TREE_VEC_ELT (*tp, 2)
16713 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16714 /*integral_constant_expression_p=*/false);
16715 TREE_VEC_ELT (*tp, 3)
16716 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16717 /*integral_constant_expression_p=*/false);
16718 TREE_CHAIN (*tp) = NULL_TREE;
16719 tp = &TREE_CHAIN (*tp);
16720 }
16721 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16722 iterator_cache[0] = TREE_PURPOSE (decl);
16723 iterator_cache[1] = ret;
16724 }
16725 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16726 args, complain,
16727 in_decl, NULL));
16728 }
16729
16730 /* Handle an OpenMP array section represented as a TREE_LIST (or
16731 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16732 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16733 TREE_LIST. We can handle it exactly the same as an array section
16734 (purpose, value, and a chain), even though the nomenclature
16735 (low_bound, length, etc) is different. */
16736 if (TREE_CODE (decl) == TREE_LIST)
16737 {
16738 tree low_bound
16739 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16740 /*integral_constant_expression_p=*/false);
16741 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16742 /*integral_constant_expression_p=*/false);
16743 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16744 in_decl, NULL);
16745 if (TREE_PURPOSE (decl) == low_bound
16746 && TREE_VALUE (decl) == length
16747 && TREE_CHAIN (decl) == chain)
16748 return decl;
16749 tree ret = tree_cons (low_bound, length, chain);
16750 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16751 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16752 return ret;
16753 }
16754 tree ret = tsubst_expr (decl, args, complain, in_decl,
16755 /*integral_constant_expression_p=*/false);
16756 /* Undo convert_from_reference tsubst_expr could have called. */
16757 if (decl
16758 && REFERENCE_REF_P (ret)
16759 && !REFERENCE_REF_P (decl))
16760 ret = TREE_OPERAND (ret, 0);
16761 return ret;
16762 }
16763
16764 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16765
16766 static tree
16767 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16768 tree args, tsubst_flags_t complain, tree in_decl)
16769 {
16770 tree new_clauses = NULL_TREE, nc, oc;
16771 tree linear_no_step = NULL_TREE;
16772 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16773
16774 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16775 {
16776 nc = copy_node (oc);
16777 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16778 new_clauses = nc;
16779
16780 switch (OMP_CLAUSE_CODE (nc))
16781 {
16782 case OMP_CLAUSE_LASTPRIVATE:
16783 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16784 {
16785 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16786 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16787 in_decl, /*integral_constant_expression_p=*/false);
16788 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16789 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16790 }
16791 /* FALLTHRU */
16792 case OMP_CLAUSE_PRIVATE:
16793 case OMP_CLAUSE_SHARED:
16794 case OMP_CLAUSE_FIRSTPRIVATE:
16795 case OMP_CLAUSE_COPYIN:
16796 case OMP_CLAUSE_COPYPRIVATE:
16797 case OMP_CLAUSE_UNIFORM:
16798 case OMP_CLAUSE_DEPEND:
16799 case OMP_CLAUSE_FROM:
16800 case OMP_CLAUSE_TO:
16801 case OMP_CLAUSE_MAP:
16802 case OMP_CLAUSE_NONTEMPORAL:
16803 case OMP_CLAUSE_USE_DEVICE_PTR:
16804 case OMP_CLAUSE_USE_DEVICE_ADDR:
16805 case OMP_CLAUSE_IS_DEVICE_PTR:
16806 case OMP_CLAUSE_INCLUSIVE:
16807 case OMP_CLAUSE_EXCLUSIVE:
16808 OMP_CLAUSE_DECL (nc)
16809 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16810 in_decl, iterator_cache);
16811 break;
16812 case OMP_CLAUSE_TILE:
16813 case OMP_CLAUSE_IF:
16814 case OMP_CLAUSE_NUM_THREADS:
16815 case OMP_CLAUSE_SCHEDULE:
16816 case OMP_CLAUSE_COLLAPSE:
16817 case OMP_CLAUSE_FINAL:
16818 case OMP_CLAUSE_DEVICE:
16819 case OMP_CLAUSE_DIST_SCHEDULE:
16820 case OMP_CLAUSE_NUM_TEAMS:
16821 case OMP_CLAUSE_THREAD_LIMIT:
16822 case OMP_CLAUSE_SAFELEN:
16823 case OMP_CLAUSE_SIMDLEN:
16824 case OMP_CLAUSE_NUM_TASKS:
16825 case OMP_CLAUSE_GRAINSIZE:
16826 case OMP_CLAUSE_PRIORITY:
16827 case OMP_CLAUSE_ORDERED:
16828 case OMP_CLAUSE_HINT:
16829 case OMP_CLAUSE_NUM_GANGS:
16830 case OMP_CLAUSE_NUM_WORKERS:
16831 case OMP_CLAUSE_VECTOR_LENGTH:
16832 case OMP_CLAUSE_WORKER:
16833 case OMP_CLAUSE_VECTOR:
16834 case OMP_CLAUSE_ASYNC:
16835 case OMP_CLAUSE_WAIT:
16836 OMP_CLAUSE_OPERAND (nc, 0)
16837 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16838 in_decl, /*integral_constant_expression_p=*/false);
16839 break;
16840 case OMP_CLAUSE_REDUCTION:
16841 case OMP_CLAUSE_IN_REDUCTION:
16842 case OMP_CLAUSE_TASK_REDUCTION:
16843 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16844 {
16845 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16846 if (TREE_CODE (placeholder) == SCOPE_REF)
16847 {
16848 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16849 complain, in_decl);
16850 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16851 = build_qualified_name (NULL_TREE, scope,
16852 TREE_OPERAND (placeholder, 1),
16853 false);
16854 }
16855 else
16856 gcc_assert (identifier_p (placeholder));
16857 }
16858 OMP_CLAUSE_DECL (nc)
16859 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16860 in_decl, NULL);
16861 break;
16862 case OMP_CLAUSE_GANG:
16863 case OMP_CLAUSE_ALIGNED:
16864 OMP_CLAUSE_DECL (nc)
16865 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16866 in_decl, NULL);
16867 OMP_CLAUSE_OPERAND (nc, 1)
16868 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16869 in_decl, /*integral_constant_expression_p=*/false);
16870 break;
16871 case OMP_CLAUSE_LINEAR:
16872 OMP_CLAUSE_DECL (nc)
16873 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16874 in_decl, NULL);
16875 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16876 {
16877 gcc_assert (!linear_no_step);
16878 linear_no_step = nc;
16879 }
16880 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16881 OMP_CLAUSE_LINEAR_STEP (nc)
16882 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16883 complain, in_decl, NULL);
16884 else
16885 OMP_CLAUSE_LINEAR_STEP (nc)
16886 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16887 in_decl,
16888 /*integral_constant_expression_p=*/false);
16889 break;
16890 case OMP_CLAUSE_NOWAIT:
16891 case OMP_CLAUSE_DEFAULT:
16892 case OMP_CLAUSE_UNTIED:
16893 case OMP_CLAUSE_MERGEABLE:
16894 case OMP_CLAUSE_INBRANCH:
16895 case OMP_CLAUSE_NOTINBRANCH:
16896 case OMP_CLAUSE_PROC_BIND:
16897 case OMP_CLAUSE_FOR:
16898 case OMP_CLAUSE_PARALLEL:
16899 case OMP_CLAUSE_SECTIONS:
16900 case OMP_CLAUSE_TASKGROUP:
16901 case OMP_CLAUSE_NOGROUP:
16902 case OMP_CLAUSE_THREADS:
16903 case OMP_CLAUSE_SIMD:
16904 case OMP_CLAUSE_DEFAULTMAP:
16905 case OMP_CLAUSE_ORDER:
16906 case OMP_CLAUSE_BIND:
16907 case OMP_CLAUSE_INDEPENDENT:
16908 case OMP_CLAUSE_AUTO:
16909 case OMP_CLAUSE_SEQ:
16910 case OMP_CLAUSE_IF_PRESENT:
16911 case OMP_CLAUSE_FINALIZE:
16912 break;
16913 default:
16914 gcc_unreachable ();
16915 }
16916 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16917 switch (OMP_CLAUSE_CODE (nc))
16918 {
16919 case OMP_CLAUSE_SHARED:
16920 case OMP_CLAUSE_PRIVATE:
16921 case OMP_CLAUSE_FIRSTPRIVATE:
16922 case OMP_CLAUSE_LASTPRIVATE:
16923 case OMP_CLAUSE_COPYPRIVATE:
16924 case OMP_CLAUSE_LINEAR:
16925 case OMP_CLAUSE_REDUCTION:
16926 case OMP_CLAUSE_IN_REDUCTION:
16927 case OMP_CLAUSE_TASK_REDUCTION:
16928 case OMP_CLAUSE_USE_DEVICE_PTR:
16929 case OMP_CLAUSE_USE_DEVICE_ADDR:
16930 case OMP_CLAUSE_IS_DEVICE_PTR:
16931 case OMP_CLAUSE_INCLUSIVE:
16932 case OMP_CLAUSE_EXCLUSIVE:
16933 /* tsubst_expr on SCOPE_REF results in returning
16934 finish_non_static_data_member result. Undo that here. */
16935 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16936 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16937 == IDENTIFIER_NODE))
16938 {
16939 tree t = OMP_CLAUSE_DECL (nc);
16940 tree v = t;
16941 while (v)
16942 switch (TREE_CODE (v))
16943 {
16944 case COMPONENT_REF:
16945 case MEM_REF:
16946 case INDIRECT_REF:
16947 CASE_CONVERT:
16948 case POINTER_PLUS_EXPR:
16949 v = TREE_OPERAND (v, 0);
16950 continue;
16951 case PARM_DECL:
16952 if (DECL_CONTEXT (v) == current_function_decl
16953 && DECL_ARTIFICIAL (v)
16954 && DECL_NAME (v) == this_identifier)
16955 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16956 /* FALLTHRU */
16957 default:
16958 v = NULL_TREE;
16959 break;
16960 }
16961 }
16962 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16963 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16964 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16965 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16966 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16967 {
16968 tree decl = OMP_CLAUSE_DECL (nc);
16969 if (VAR_P (decl))
16970 {
16971 retrofit_lang_decl (decl);
16972 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16973 }
16974 }
16975 break;
16976 default:
16977 break;
16978 }
16979 }
16980
16981 new_clauses = nreverse (new_clauses);
16982 if (ort != C_ORT_OMP_DECLARE_SIMD)
16983 {
16984 new_clauses = finish_omp_clauses (new_clauses, ort);
16985 if (linear_no_step)
16986 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16987 if (nc == linear_no_step)
16988 {
16989 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16990 break;
16991 }
16992 }
16993 return new_clauses;
16994 }
16995
16996 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16997
16998 static tree
16999 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17000 tree in_decl)
17001 {
17002 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17003
17004 tree purpose, value, chain;
17005
17006 if (t == NULL)
17007 return t;
17008
17009 if (TREE_CODE (t) != TREE_LIST)
17010 return tsubst_copy_and_build (t, args, complain, in_decl,
17011 /*function_p=*/false,
17012 /*integral_constant_expression_p=*/false);
17013
17014 if (t == void_list_node)
17015 return t;
17016
17017 purpose = TREE_PURPOSE (t);
17018 if (purpose)
17019 purpose = RECUR (purpose);
17020 value = TREE_VALUE (t);
17021 if (value)
17022 {
17023 if (TREE_CODE (value) != LABEL_DECL)
17024 value = RECUR (value);
17025 else
17026 {
17027 value = lookup_label (DECL_NAME (value));
17028 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17029 TREE_USED (value) = 1;
17030 }
17031 }
17032 chain = TREE_CHAIN (t);
17033 if (chain && chain != void_type_node)
17034 chain = RECUR (chain);
17035 return tree_cons (purpose, value, chain);
17036 #undef RECUR
17037 }
17038
17039 /* Used to temporarily communicate the list of #pragma omp parallel
17040 clauses to #pragma omp for instantiation if they are combined
17041 together. */
17042
17043 static tree *omp_parallel_combined_clauses;
17044
17045 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17046 tree *, unsigned int *);
17047
17048 /* Substitute one OMP_FOR iterator. */
17049
17050 static bool
17051 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17052 tree initv, tree condv, tree incrv, tree *clauses,
17053 tree args, tsubst_flags_t complain, tree in_decl,
17054 bool integral_constant_expression_p)
17055 {
17056 #define RECUR(NODE) \
17057 tsubst_expr ((NODE), args, complain, in_decl, \
17058 integral_constant_expression_p)
17059 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17060 bool ret = false;
17061
17062 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17063 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17064
17065 decl = TREE_OPERAND (init, 0);
17066 init = TREE_OPERAND (init, 1);
17067 tree decl_expr = NULL_TREE;
17068 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17069 if (range_for)
17070 {
17071 bool decomp = false;
17072 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17073 {
17074 tree v = DECL_VALUE_EXPR (decl);
17075 if (TREE_CODE (v) == ARRAY_REF
17076 && VAR_P (TREE_OPERAND (v, 0))
17077 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17078 {
17079 tree decomp_first = NULL_TREE;
17080 unsigned decomp_cnt = 0;
17081 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17082 maybe_push_decl (d);
17083 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17084 in_decl, &decomp_first, &decomp_cnt);
17085 decomp = true;
17086 if (d == error_mark_node)
17087 decl = error_mark_node;
17088 else
17089 for (unsigned int i = 0; i < decomp_cnt; i++)
17090 {
17091 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17092 {
17093 tree v = build_nt (ARRAY_REF, d,
17094 size_int (decomp_cnt - i - 1),
17095 NULL_TREE, NULL_TREE);
17096 SET_DECL_VALUE_EXPR (decomp_first, v);
17097 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17098 }
17099 fit_decomposition_lang_decl (decomp_first, d);
17100 decomp_first = DECL_CHAIN (decomp_first);
17101 }
17102 }
17103 }
17104 decl = tsubst_decl (decl, args, complain);
17105 if (!decomp)
17106 maybe_push_decl (decl);
17107 }
17108 else if (init && TREE_CODE (init) == DECL_EXPR)
17109 {
17110 /* We need to jump through some hoops to handle declarations in the
17111 init-statement, since we might need to handle auto deduction,
17112 but we need to keep control of initialization. */
17113 decl_expr = init;
17114 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17115 decl = tsubst_decl (decl, args, complain);
17116 }
17117 else
17118 {
17119 if (TREE_CODE (decl) == SCOPE_REF)
17120 {
17121 decl = RECUR (decl);
17122 if (TREE_CODE (decl) == COMPONENT_REF)
17123 {
17124 tree v = decl;
17125 while (v)
17126 switch (TREE_CODE (v))
17127 {
17128 case COMPONENT_REF:
17129 case MEM_REF:
17130 case INDIRECT_REF:
17131 CASE_CONVERT:
17132 case POINTER_PLUS_EXPR:
17133 v = TREE_OPERAND (v, 0);
17134 continue;
17135 case PARM_DECL:
17136 if (DECL_CONTEXT (v) == current_function_decl
17137 && DECL_ARTIFICIAL (v)
17138 && DECL_NAME (v) == this_identifier)
17139 {
17140 decl = TREE_OPERAND (decl, 1);
17141 decl = omp_privatize_field (decl, false);
17142 }
17143 /* FALLTHRU */
17144 default:
17145 v = NULL_TREE;
17146 break;
17147 }
17148 }
17149 }
17150 else
17151 decl = RECUR (decl);
17152 }
17153 init = RECUR (init);
17154
17155 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17156 {
17157 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17158 if (TREE_CODE (o) == TREE_LIST)
17159 TREE_VEC_ELT (orig_declv, i)
17160 = tree_cons (RECUR (TREE_PURPOSE (o)),
17161 RECUR (TREE_VALUE (o)),
17162 NULL_TREE);
17163 else
17164 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17165 }
17166
17167 if (range_for)
17168 {
17169 tree this_pre_body = NULL_TREE;
17170 tree orig_init = NULL_TREE;
17171 tree orig_decl = NULL_TREE;
17172 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17173 orig_init, cond, incr);
17174 if (orig_decl)
17175 {
17176 if (orig_declv == NULL_TREE)
17177 orig_declv = copy_node (declv);
17178 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17179 ret = true;
17180 }
17181 else if (orig_declv)
17182 TREE_VEC_ELT (orig_declv, i) = decl;
17183 }
17184
17185 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17186 if (!range_for && auto_node && init)
17187 TREE_TYPE (decl)
17188 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17189
17190 gcc_assert (!type_dependent_expression_p (decl));
17191
17192 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17193 {
17194 if (decl_expr)
17195 {
17196 /* Declare the variable, but don't let that initialize it. */
17197 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17198 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17199 RECUR (decl_expr);
17200 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17201 }
17202
17203 if (!range_for)
17204 {
17205 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
17206 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17207 if (TREE_CODE (incr) == MODIFY_EXPR)
17208 {
17209 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17210 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17211 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17212 NOP_EXPR, rhs, complain);
17213 }
17214 else
17215 incr = RECUR (incr);
17216 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17217 TREE_VEC_ELT (orig_declv, i) = decl;
17218 }
17219 TREE_VEC_ELT (declv, i) = decl;
17220 TREE_VEC_ELT (initv, i) = init;
17221 TREE_VEC_ELT (condv, i) = cond;
17222 TREE_VEC_ELT (incrv, i) = incr;
17223 return ret;
17224 }
17225
17226 if (decl_expr)
17227 {
17228 /* Declare and initialize the variable. */
17229 RECUR (decl_expr);
17230 init = NULL_TREE;
17231 }
17232 else if (init)
17233 {
17234 tree *pc;
17235 int j;
17236 for (j = ((omp_parallel_combined_clauses == NULL
17237 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17238 {
17239 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17240 {
17241 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17242 && OMP_CLAUSE_DECL (*pc) == decl)
17243 break;
17244 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17245 && OMP_CLAUSE_DECL (*pc) == decl)
17246 {
17247 if (j)
17248 break;
17249 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17250 tree c = *pc;
17251 *pc = OMP_CLAUSE_CHAIN (c);
17252 OMP_CLAUSE_CHAIN (c) = *clauses;
17253 *clauses = c;
17254 }
17255 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17256 && OMP_CLAUSE_DECL (*pc) == decl)
17257 {
17258 error ("iteration variable %qD should not be firstprivate",
17259 decl);
17260 *pc = OMP_CLAUSE_CHAIN (*pc);
17261 }
17262 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17263 && OMP_CLAUSE_DECL (*pc) == decl)
17264 {
17265 error ("iteration variable %qD should not be reduction",
17266 decl);
17267 *pc = OMP_CLAUSE_CHAIN (*pc);
17268 }
17269 else
17270 pc = &OMP_CLAUSE_CHAIN (*pc);
17271 }
17272 if (*pc)
17273 break;
17274 }
17275 if (*pc == NULL_TREE)
17276 {
17277 tree c = build_omp_clause (input_location,
17278 TREE_CODE (t) == OMP_LOOP
17279 ? OMP_CLAUSE_LASTPRIVATE
17280 : OMP_CLAUSE_PRIVATE);
17281 OMP_CLAUSE_DECL (c) = decl;
17282 c = finish_omp_clauses (c, C_ORT_OMP);
17283 if (c)
17284 {
17285 OMP_CLAUSE_CHAIN (c) = *clauses;
17286 *clauses = c;
17287 }
17288 }
17289 }
17290 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17291 if (COMPARISON_CLASS_P (cond))
17292 {
17293 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17294 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17295 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17296 }
17297 else
17298 cond = RECUR (cond);
17299 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17300 switch (TREE_CODE (incr))
17301 {
17302 case PREINCREMENT_EXPR:
17303 case PREDECREMENT_EXPR:
17304 case POSTINCREMENT_EXPR:
17305 case POSTDECREMENT_EXPR:
17306 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17307 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17308 break;
17309 case MODIFY_EXPR:
17310 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17311 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17312 {
17313 tree rhs = TREE_OPERAND (incr, 1);
17314 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17315 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17316 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17317 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17318 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17319 rhs0, rhs1));
17320 }
17321 else
17322 incr = RECUR (incr);
17323 break;
17324 case MODOP_EXPR:
17325 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17326 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17327 {
17328 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17329 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17330 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17331 TREE_TYPE (decl), lhs,
17332 RECUR (TREE_OPERAND (incr, 2))));
17333 }
17334 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17335 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17336 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17337 {
17338 tree rhs = TREE_OPERAND (incr, 2);
17339 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17340 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17341 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17342 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17343 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17344 rhs0, rhs1));
17345 }
17346 else
17347 incr = RECUR (incr);
17348 break;
17349 default:
17350 incr = RECUR (incr);
17351 break;
17352 }
17353
17354 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17355 TREE_VEC_ELT (orig_declv, i) = decl;
17356 TREE_VEC_ELT (declv, i) = decl;
17357 TREE_VEC_ELT (initv, i) = init;
17358 TREE_VEC_ELT (condv, i) = cond;
17359 TREE_VEC_ELT (incrv, i) = incr;
17360 return false;
17361 #undef RECUR
17362 }
17363
17364 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17365 of OMP_TARGET's body. */
17366
17367 static tree
17368 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17369 {
17370 *walk_subtrees = 0;
17371 switch (TREE_CODE (*tp))
17372 {
17373 case OMP_TEAMS:
17374 return *tp;
17375 case BIND_EXPR:
17376 case STATEMENT_LIST:
17377 *walk_subtrees = 1;
17378 break;
17379 default:
17380 break;
17381 }
17382 return NULL_TREE;
17383 }
17384
17385 /* Helper function for tsubst_expr. For decomposition declaration
17386 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
17387 also the corresponding decls representing the identifiers
17388 of the decomposition declaration. Return DECL if successful
17389 or error_mark_node otherwise, set *FIRST to the first decl
17390 in the list chained through DECL_CHAIN and *CNT to the number
17391 of such decls. */
17392
17393 static tree
17394 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
17395 tsubst_flags_t complain, tree in_decl, tree *first,
17396 unsigned int *cnt)
17397 {
17398 tree decl2, decl3, prev = decl;
17399 *cnt = 0;
17400 gcc_assert (DECL_NAME (decl) == NULL_TREE);
17401 for (decl2 = DECL_CHAIN (pattern_decl);
17402 decl2
17403 && VAR_P (decl2)
17404 && DECL_DECOMPOSITION_P (decl2)
17405 && DECL_NAME (decl2);
17406 decl2 = DECL_CHAIN (decl2))
17407 {
17408 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
17409 {
17410 gcc_assert (errorcount);
17411 return error_mark_node;
17412 }
17413 (*cnt)++;
17414 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
17415 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
17416 tree v = DECL_VALUE_EXPR (decl2);
17417 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
17418 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
17419 decl3 = tsubst (decl2, args, complain, in_decl);
17420 SET_DECL_VALUE_EXPR (decl2, v);
17421 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
17422 if (VAR_P (decl3))
17423 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
17424 else
17425 {
17426 gcc_assert (errorcount);
17427 decl = error_mark_node;
17428 continue;
17429 }
17430 maybe_push_decl (decl3);
17431 if (error_operand_p (decl3))
17432 decl = error_mark_node;
17433 else if (decl != error_mark_node
17434 && DECL_CHAIN (decl3) != prev
17435 && decl != prev)
17436 {
17437 gcc_assert (errorcount);
17438 decl = error_mark_node;
17439 }
17440 else
17441 prev = decl3;
17442 }
17443 *first = prev;
17444 return decl;
17445 }
17446
17447 /* Return the proper local_specialization for init-capture pack DECL. */
17448
17449 static tree
17450 lookup_init_capture_pack (tree decl)
17451 {
17452 /* We handle normal pack captures by forwarding to the specialization of the
17453 captured parameter. We can't do that for pack init-captures; we need them
17454 to have their own local_specialization. We created the individual
17455 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
17456 when we process the DECL_EXPR for the pack init-capture in the template.
17457 So, how do we find them? We don't know the capture proxy pack when
17458 building the individual resulting proxies, and we don't know the
17459 individual proxies when instantiating the pack. What we have in common is
17460 the FIELD_DECL.
17461
17462 So...when we instantiate the FIELD_DECL, we stick the result in
17463 local_specializations. Then at the DECL_EXPR we look up that result, see
17464 how many elements it has, synthesize the names, and look them up. */
17465
17466 tree cname = DECL_NAME (decl);
17467 tree val = DECL_VALUE_EXPR (decl);
17468 tree field = TREE_OPERAND (val, 1);
17469 gcc_assert (TREE_CODE (field) == FIELD_DECL);
17470 tree fpack = retrieve_local_specialization (field);
17471 if (fpack == error_mark_node)
17472 return error_mark_node;
17473
17474 int len = 1;
17475 tree vec = NULL_TREE;
17476 tree r = NULL_TREE;
17477 if (TREE_CODE (fpack) == TREE_VEC)
17478 {
17479 len = TREE_VEC_LENGTH (fpack);
17480 vec = make_tree_vec (len);
17481 r = make_node (NONTYPE_ARGUMENT_PACK);
17482 SET_ARGUMENT_PACK_ARGS (r, vec);
17483 }
17484 for (int i = 0; i < len; ++i)
17485 {
17486 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17487 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17488 if (vec)
17489 TREE_VEC_ELT (vec, i) = elt;
17490 else
17491 r = elt;
17492 }
17493 return r;
17494 }
17495
17496 /* Like tsubst_copy for expressions, etc. but also does semantic
17497 processing. */
17498
17499 tree
17500 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17501 bool integral_constant_expression_p)
17502 {
17503 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17504 #define RECUR(NODE) \
17505 tsubst_expr ((NODE), args, complain, in_decl, \
17506 integral_constant_expression_p)
17507
17508 tree stmt, tmp;
17509 tree r;
17510 location_t loc;
17511
17512 if (t == NULL_TREE || t == error_mark_node)
17513 return t;
17514
17515 loc = input_location;
17516 if (location_t eloc = cp_expr_location (t))
17517 input_location = eloc;
17518 if (STATEMENT_CODE_P (TREE_CODE (t)))
17519 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17520
17521 switch (TREE_CODE (t))
17522 {
17523 case STATEMENT_LIST:
17524 {
17525 tree_stmt_iterator i;
17526 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17527 RECUR (tsi_stmt (i));
17528 break;
17529 }
17530
17531 case CTOR_INITIALIZER:
17532 finish_mem_initializers (tsubst_initializer_list
17533 (TREE_OPERAND (t, 0), args));
17534 break;
17535
17536 case RETURN_EXPR:
17537 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17538 break;
17539
17540 case EXPR_STMT:
17541 tmp = RECUR (EXPR_STMT_EXPR (t));
17542 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17543 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17544 else
17545 finish_expr_stmt (tmp);
17546 break;
17547
17548 case USING_STMT:
17549 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17550 break;
17551
17552 case DECL_EXPR:
17553 {
17554 tree decl, pattern_decl;
17555 tree init;
17556
17557 pattern_decl = decl = DECL_EXPR_DECL (t);
17558 if (TREE_CODE (decl) == LABEL_DECL)
17559 finish_label_decl (DECL_NAME (decl));
17560 else if (TREE_CODE (decl) == USING_DECL)
17561 {
17562 tree scope = USING_DECL_SCOPE (decl);
17563 tree name = DECL_NAME (decl);
17564
17565 scope = tsubst (scope, args, complain, in_decl);
17566 finish_nonmember_using_decl (scope, name);
17567 }
17568 else if (is_capture_proxy (decl)
17569 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17570 {
17571 /* We're in tsubst_lambda_expr, we've already inserted a new
17572 capture proxy, so look it up and register it. */
17573 tree inst;
17574 if (!DECL_PACK_P (decl))
17575 {
17576 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17577 /*nonclass*/1, /*block_p=*/true,
17578 /*ns_only*/0, LOOKUP_HIDDEN);
17579 gcc_assert (inst != decl && is_capture_proxy (inst));
17580 }
17581 else if (is_normal_capture_proxy (decl))
17582 {
17583 inst = (retrieve_local_specialization
17584 (DECL_CAPTURED_VARIABLE (decl)));
17585 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17586 }
17587 else
17588 inst = lookup_init_capture_pack (decl);
17589
17590 register_local_specialization (inst, decl);
17591 break;
17592 }
17593 else if (DECL_PRETTY_FUNCTION_P (decl))
17594 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17595 DECL_NAME (decl),
17596 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17597 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17598 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17599 /* Don't copy the old closure; we'll create a new one in
17600 tsubst_lambda_expr. */
17601 break;
17602 else
17603 {
17604 init = DECL_INITIAL (decl);
17605 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17606 for local variables, so save if DECL was declared constinit. */
17607 const bool constinit_p
17608 = (VAR_P (decl)
17609 && DECL_LANG_SPECIFIC (decl)
17610 && DECL_TEMPLATE_INFO (decl)
17611 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17612 decl = tsubst (decl, args, complain, in_decl);
17613 if (decl != error_mark_node)
17614 {
17615 /* By marking the declaration as instantiated, we avoid
17616 trying to instantiate it. Since instantiate_decl can't
17617 handle local variables, and since we've already done
17618 all that needs to be done, that's the right thing to
17619 do. */
17620 if (VAR_P (decl))
17621 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17622 if (VAR_P (decl) && !DECL_NAME (decl)
17623 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17624 /* Anonymous aggregates are a special case. */
17625 finish_anon_union (decl);
17626 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17627 {
17628 DECL_CONTEXT (decl) = current_function_decl;
17629 if (DECL_NAME (decl) == this_identifier)
17630 {
17631 tree lam = DECL_CONTEXT (current_function_decl);
17632 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17633 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17634 }
17635 insert_capture_proxy (decl);
17636 }
17637 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17638 /* We already did a pushtag. */;
17639 else if (TREE_CODE (decl) == FUNCTION_DECL
17640 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17641 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17642 {
17643 DECL_CONTEXT (decl) = NULL_TREE;
17644 pushdecl (decl);
17645 DECL_CONTEXT (decl) = current_function_decl;
17646 cp_check_omp_declare_reduction (decl);
17647 }
17648 else
17649 {
17650 bool const_init = false;
17651 unsigned int cnt = 0;
17652 tree first = NULL_TREE, ndecl = error_mark_node;
17653 maybe_push_decl (decl);
17654
17655 if (VAR_P (decl)
17656 && DECL_DECOMPOSITION_P (decl)
17657 && TREE_TYPE (pattern_decl) != error_mark_node)
17658 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17659 complain, in_decl, &first,
17660 &cnt);
17661
17662 init = tsubst_init (init, decl, args, complain, in_decl);
17663
17664 if (VAR_P (decl))
17665 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17666 (pattern_decl));
17667
17668 if (ndecl != error_mark_node)
17669 cp_maybe_mangle_decomp (ndecl, first, cnt);
17670
17671 cp_finish_decl (decl, init, const_init, NULL_TREE,
17672 constinit_p ? LOOKUP_CONSTINIT : 0);
17673
17674 if (ndecl != error_mark_node)
17675 cp_finish_decomp (ndecl, first, cnt);
17676 }
17677 }
17678 }
17679
17680 break;
17681 }
17682
17683 case FOR_STMT:
17684 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17685 RECUR (FOR_INIT_STMT (t));
17686 finish_init_stmt (stmt);
17687 tmp = RECUR (FOR_COND (t));
17688 finish_for_cond (tmp, stmt, false, 0);
17689 tmp = RECUR (FOR_EXPR (t));
17690 finish_for_expr (tmp, stmt);
17691 {
17692 bool prev = note_iteration_stmt_body_start ();
17693 RECUR (FOR_BODY (t));
17694 note_iteration_stmt_body_end (prev);
17695 }
17696 finish_for_stmt (stmt);
17697 break;
17698
17699 case RANGE_FOR_STMT:
17700 {
17701 /* Construct another range_for, if this is not a final
17702 substitution (for inside inside a generic lambda of a
17703 template). Otherwise convert to a regular for. */
17704 tree decl, expr;
17705 stmt = (processing_template_decl
17706 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17707 : begin_for_stmt (NULL_TREE, NULL_TREE));
17708 RECUR (RANGE_FOR_INIT_STMT (t));
17709 decl = RANGE_FOR_DECL (t);
17710 decl = tsubst (decl, args, complain, in_decl);
17711 maybe_push_decl (decl);
17712 expr = RECUR (RANGE_FOR_EXPR (t));
17713
17714 tree decomp_first = NULL_TREE;
17715 unsigned decomp_cnt = 0;
17716 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17717 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17718 complain, in_decl,
17719 &decomp_first, &decomp_cnt);
17720
17721 if (processing_template_decl)
17722 {
17723 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17724 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17725 finish_range_for_decl (stmt, decl, expr);
17726 if (decomp_first && decl != error_mark_node)
17727 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17728 }
17729 else
17730 {
17731 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17732 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17733 stmt = cp_convert_range_for (stmt, decl, expr,
17734 decomp_first, decomp_cnt,
17735 RANGE_FOR_IVDEP (t), unroll);
17736 }
17737
17738 bool prev = note_iteration_stmt_body_start ();
17739 RECUR (RANGE_FOR_BODY (t));
17740 note_iteration_stmt_body_end (prev);
17741 finish_for_stmt (stmt);
17742 }
17743 break;
17744
17745 case WHILE_STMT:
17746 stmt = begin_while_stmt ();
17747 tmp = RECUR (WHILE_COND (t));
17748 finish_while_stmt_cond (tmp, stmt, false, 0);
17749 {
17750 bool prev = note_iteration_stmt_body_start ();
17751 RECUR (WHILE_BODY (t));
17752 note_iteration_stmt_body_end (prev);
17753 }
17754 finish_while_stmt (stmt);
17755 break;
17756
17757 case DO_STMT:
17758 stmt = begin_do_stmt ();
17759 {
17760 bool prev = note_iteration_stmt_body_start ();
17761 RECUR (DO_BODY (t));
17762 note_iteration_stmt_body_end (prev);
17763 }
17764 finish_do_body (stmt);
17765 tmp = RECUR (DO_COND (t));
17766 finish_do_stmt (tmp, stmt, false, 0);
17767 break;
17768
17769 case IF_STMT:
17770 stmt = begin_if_stmt ();
17771 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17772 if (IF_STMT_CONSTEXPR_P (t))
17773 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17774 tmp = RECUR (IF_COND (t));
17775 tmp = finish_if_stmt_cond (tmp, stmt);
17776 if (IF_STMT_CONSTEXPR_P (t)
17777 && instantiation_dependent_expression_p (tmp))
17778 {
17779 /* We're partially instantiating a generic lambda, but the condition
17780 of the constexpr if is still dependent. Don't substitute into the
17781 branches now, just remember the template arguments. */
17782 do_poplevel (IF_SCOPE (stmt));
17783 IF_COND (stmt) = IF_COND (t);
17784 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17785 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17786 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17787 add_stmt (stmt);
17788 break;
17789 }
17790 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17791 /* Don't instantiate the THEN_CLAUSE. */;
17792 else
17793 {
17794 tree folded = fold_non_dependent_expr (tmp, complain);
17795 bool inhibit = integer_zerop (folded);
17796 if (inhibit)
17797 ++c_inhibit_evaluation_warnings;
17798 RECUR (THEN_CLAUSE (t));
17799 if (inhibit)
17800 --c_inhibit_evaluation_warnings;
17801 }
17802 finish_then_clause (stmt);
17803
17804 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17805 /* Don't instantiate the ELSE_CLAUSE. */;
17806 else if (ELSE_CLAUSE (t))
17807 {
17808 tree folded = fold_non_dependent_expr (tmp, complain);
17809 bool inhibit = integer_nonzerop (folded);
17810 begin_else_clause (stmt);
17811 if (inhibit)
17812 ++c_inhibit_evaluation_warnings;
17813 RECUR (ELSE_CLAUSE (t));
17814 if (inhibit)
17815 --c_inhibit_evaluation_warnings;
17816 finish_else_clause (stmt);
17817 }
17818
17819 finish_if_stmt (stmt);
17820 break;
17821
17822 case BIND_EXPR:
17823 if (BIND_EXPR_BODY_BLOCK (t))
17824 stmt = begin_function_body ();
17825 else
17826 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17827 ? BCS_TRY_BLOCK : 0);
17828
17829 RECUR (BIND_EXPR_BODY (t));
17830
17831 if (BIND_EXPR_BODY_BLOCK (t))
17832 finish_function_body (stmt);
17833 else
17834 finish_compound_stmt (stmt);
17835 break;
17836
17837 case BREAK_STMT:
17838 finish_break_stmt ();
17839 break;
17840
17841 case CONTINUE_STMT:
17842 finish_continue_stmt ();
17843 break;
17844
17845 case SWITCH_STMT:
17846 stmt = begin_switch_stmt ();
17847 tmp = RECUR (SWITCH_STMT_COND (t));
17848 finish_switch_cond (tmp, stmt);
17849 RECUR (SWITCH_STMT_BODY (t));
17850 finish_switch_stmt (stmt);
17851 break;
17852
17853 case CASE_LABEL_EXPR:
17854 {
17855 tree decl = CASE_LABEL (t);
17856 tree low = RECUR (CASE_LOW (t));
17857 tree high = RECUR (CASE_HIGH (t));
17858 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17859 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17860 {
17861 tree label = CASE_LABEL (l);
17862 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17863 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17864 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17865 }
17866 }
17867 break;
17868
17869 case LABEL_EXPR:
17870 {
17871 tree decl = LABEL_EXPR_LABEL (t);
17872 tree label;
17873
17874 label = finish_label_stmt (DECL_NAME (decl));
17875 if (TREE_CODE (label) == LABEL_DECL)
17876 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17877 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17878 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17879 }
17880 break;
17881
17882 case GOTO_EXPR:
17883 tmp = GOTO_DESTINATION (t);
17884 if (TREE_CODE (tmp) != LABEL_DECL)
17885 /* Computed goto's must be tsubst'd into. On the other hand,
17886 non-computed gotos must not be; the identifier in question
17887 will have no binding. */
17888 tmp = RECUR (tmp);
17889 else
17890 tmp = DECL_NAME (tmp);
17891 finish_goto_stmt (tmp);
17892 break;
17893
17894 case ASM_EXPR:
17895 {
17896 tree string = RECUR (ASM_STRING (t));
17897 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17898 complain, in_decl);
17899 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17900 complain, in_decl);
17901 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17902 complain, in_decl);
17903 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17904 complain, in_decl);
17905 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17906 outputs, inputs, clobbers, labels,
17907 ASM_INLINE_P (t));
17908 tree asm_expr = tmp;
17909 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17910 asm_expr = TREE_OPERAND (asm_expr, 0);
17911 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17912 }
17913 break;
17914
17915 case TRY_BLOCK:
17916 if (CLEANUP_P (t))
17917 {
17918 stmt = begin_try_block ();
17919 RECUR (TRY_STMTS (t));
17920 finish_cleanup_try_block (stmt);
17921 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17922 }
17923 else
17924 {
17925 tree compound_stmt = NULL_TREE;
17926
17927 if (FN_TRY_BLOCK_P (t))
17928 stmt = begin_function_try_block (&compound_stmt);
17929 else
17930 stmt = begin_try_block ();
17931
17932 RECUR (TRY_STMTS (t));
17933
17934 if (FN_TRY_BLOCK_P (t))
17935 finish_function_try_block (stmt);
17936 else
17937 finish_try_block (stmt);
17938
17939 RECUR (TRY_HANDLERS (t));
17940 if (FN_TRY_BLOCK_P (t))
17941 finish_function_handler_sequence (stmt, compound_stmt);
17942 else
17943 finish_handler_sequence (stmt);
17944 }
17945 break;
17946
17947 case HANDLER:
17948 {
17949 tree decl = HANDLER_PARMS (t);
17950
17951 if (decl)
17952 {
17953 decl = tsubst (decl, args, complain, in_decl);
17954 /* Prevent instantiate_decl from trying to instantiate
17955 this variable. We've already done all that needs to be
17956 done. */
17957 if (decl != error_mark_node)
17958 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17959 }
17960 stmt = begin_handler ();
17961 finish_handler_parms (decl, stmt);
17962 RECUR (HANDLER_BODY (t));
17963 finish_handler (stmt);
17964 }
17965 break;
17966
17967 case TAG_DEFN:
17968 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17969 if (CLASS_TYPE_P (tmp))
17970 {
17971 /* Local classes are not independent templates; they are
17972 instantiated along with their containing function. And this
17973 way we don't have to deal with pushing out of one local class
17974 to instantiate a member of another local class. */
17975 /* Closures are handled by the LAMBDA_EXPR. */
17976 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17977 complete_type (tmp);
17978 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17979 if ((VAR_P (fld)
17980 || (TREE_CODE (fld) == FUNCTION_DECL
17981 && !DECL_ARTIFICIAL (fld)))
17982 && DECL_TEMPLATE_INSTANTIATION (fld))
17983 instantiate_decl (fld, /*defer_ok=*/false,
17984 /*expl_inst_class=*/false);
17985 }
17986 break;
17987
17988 case STATIC_ASSERT:
17989 {
17990 tree condition;
17991
17992 ++c_inhibit_evaluation_warnings;
17993 condition =
17994 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17995 args,
17996 complain, in_decl,
17997 /*integral_constant_expression_p=*/true);
17998 --c_inhibit_evaluation_warnings;
17999
18000 finish_static_assert (condition,
18001 STATIC_ASSERT_MESSAGE (t),
18002 STATIC_ASSERT_SOURCE_LOCATION (t),
18003 /*member_p=*/false);
18004 }
18005 break;
18006
18007 case OACC_KERNELS:
18008 case OACC_PARALLEL:
18009 case OACC_SERIAL:
18010 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18011 in_decl);
18012 stmt = begin_omp_parallel ();
18013 RECUR (OMP_BODY (t));
18014 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18015 break;
18016
18017 case OMP_PARALLEL:
18018 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18019 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18020 complain, in_decl);
18021 if (OMP_PARALLEL_COMBINED (t))
18022 omp_parallel_combined_clauses = &tmp;
18023 stmt = begin_omp_parallel ();
18024 RECUR (OMP_PARALLEL_BODY (t));
18025 gcc_assert (omp_parallel_combined_clauses == NULL);
18026 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18027 = OMP_PARALLEL_COMBINED (t);
18028 pop_omp_privatization_clauses (r);
18029 break;
18030
18031 case OMP_TASK:
18032 if (OMP_TASK_BODY (t) == NULL_TREE)
18033 {
18034 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18035 complain, in_decl);
18036 t = copy_node (t);
18037 OMP_TASK_CLAUSES (t) = tmp;
18038 add_stmt (t);
18039 break;
18040 }
18041 r = push_omp_privatization_clauses (false);
18042 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18043 complain, in_decl);
18044 stmt = begin_omp_task ();
18045 RECUR (OMP_TASK_BODY (t));
18046 finish_omp_task (tmp, stmt);
18047 pop_omp_privatization_clauses (r);
18048 break;
18049
18050 case OMP_FOR:
18051 case OMP_LOOP:
18052 case OMP_SIMD:
18053 case OMP_DISTRIBUTE:
18054 case OMP_TASKLOOP:
18055 case OACC_LOOP:
18056 {
18057 tree clauses, body, pre_body;
18058 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18059 tree orig_declv = NULL_TREE;
18060 tree incrv = NULL_TREE;
18061 enum c_omp_region_type ort = C_ORT_OMP;
18062 bool any_range_for = false;
18063 int i;
18064
18065 if (TREE_CODE (t) == OACC_LOOP)
18066 ort = C_ORT_ACC;
18067
18068 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18069 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18070 in_decl);
18071 if (OMP_FOR_INIT (t) != NULL_TREE)
18072 {
18073 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18074 if (OMP_FOR_ORIG_DECLS (t))
18075 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18076 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18077 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18078 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18079 }
18080
18081 keep_next_level (true);
18082 stmt = begin_omp_structured_block ();
18083
18084 pre_body = push_stmt_list ();
18085 RECUR (OMP_FOR_PRE_BODY (t));
18086 pre_body = pop_stmt_list (pre_body);
18087
18088 if (OMP_FOR_INIT (t) != NULL_TREE)
18089 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18090 any_range_for
18091 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18092 condv, incrv, &clauses, args,
18093 complain, in_decl,
18094 integral_constant_expression_p);
18095 omp_parallel_combined_clauses = NULL;
18096
18097 if (any_range_for)
18098 {
18099 gcc_assert (orig_declv);
18100 body = begin_omp_structured_block ();
18101 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18102 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18103 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18104 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18105 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18106 TREE_VEC_ELT (declv, i));
18107 }
18108 else
18109 body = push_stmt_list ();
18110 RECUR (OMP_FOR_BODY (t));
18111 if (any_range_for)
18112 body = finish_omp_structured_block (body);
18113 else
18114 body = pop_stmt_list (body);
18115
18116 if (OMP_FOR_INIT (t) != NULL_TREE)
18117 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18118 orig_declv, initv, condv, incrv, body, pre_body,
18119 NULL, clauses);
18120 else
18121 {
18122 t = make_node (TREE_CODE (t));
18123 TREE_TYPE (t) = void_type_node;
18124 OMP_FOR_BODY (t) = body;
18125 OMP_FOR_PRE_BODY (t) = pre_body;
18126 OMP_FOR_CLAUSES (t) = clauses;
18127 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18128 add_stmt (t);
18129 }
18130
18131 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18132 t));
18133 pop_omp_privatization_clauses (r);
18134 }
18135 break;
18136
18137 case OMP_SECTIONS:
18138 omp_parallel_combined_clauses = NULL;
18139 /* FALLTHRU */
18140 case OMP_SINGLE:
18141 case OMP_TEAMS:
18142 case OMP_CRITICAL:
18143 case OMP_TASKGROUP:
18144 case OMP_SCAN:
18145 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18146 && OMP_TEAMS_COMBINED (t));
18147 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18148 in_decl);
18149 if (TREE_CODE (t) == OMP_TEAMS)
18150 {
18151 keep_next_level (true);
18152 stmt = begin_omp_structured_block ();
18153 RECUR (OMP_BODY (t));
18154 stmt = finish_omp_structured_block (stmt);
18155 }
18156 else
18157 {
18158 stmt = push_stmt_list ();
18159 RECUR (OMP_BODY (t));
18160 stmt = pop_stmt_list (stmt);
18161 }
18162
18163 t = copy_node (t);
18164 OMP_BODY (t) = stmt;
18165 OMP_CLAUSES (t) = tmp;
18166 add_stmt (t);
18167 pop_omp_privatization_clauses (r);
18168 break;
18169
18170 case OMP_DEPOBJ:
18171 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18172 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18173 {
18174 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18175 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18176 {
18177 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18178 args, complain, in_decl);
18179 if (tmp == NULL_TREE)
18180 tmp = error_mark_node;
18181 }
18182 else
18183 {
18184 kind = (enum omp_clause_depend_kind)
18185 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18186 tmp = NULL_TREE;
18187 }
18188 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18189 }
18190 else
18191 finish_omp_depobj (EXPR_LOCATION (t), r,
18192 OMP_CLAUSE_DEPEND_SOURCE,
18193 OMP_DEPOBJ_CLAUSES (t));
18194 break;
18195
18196 case OACC_DATA:
18197 case OMP_TARGET_DATA:
18198 case OMP_TARGET:
18199 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18200 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18201 in_decl);
18202 keep_next_level (true);
18203 stmt = begin_omp_structured_block ();
18204
18205 RECUR (OMP_BODY (t));
18206 stmt = finish_omp_structured_block (stmt);
18207
18208 t = copy_node (t);
18209 OMP_BODY (t) = stmt;
18210 OMP_CLAUSES (t) = tmp;
18211 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18212 {
18213 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18214 if (teams)
18215 {
18216 /* For combined target teams, ensure the num_teams and
18217 thread_limit clause expressions are evaluated on the host,
18218 before entering the target construct. */
18219 tree c;
18220 for (c = OMP_TEAMS_CLAUSES (teams);
18221 c; c = OMP_CLAUSE_CHAIN (c))
18222 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18223 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18224 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18225 {
18226 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18227 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18228 if (expr == error_mark_node)
18229 continue;
18230 tmp = TARGET_EXPR_SLOT (expr);
18231 add_stmt (expr);
18232 OMP_CLAUSE_OPERAND (c, 0) = expr;
18233 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18234 OMP_CLAUSE_FIRSTPRIVATE);
18235 OMP_CLAUSE_DECL (tc) = tmp;
18236 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18237 OMP_TARGET_CLAUSES (t) = tc;
18238 }
18239 }
18240 }
18241 add_stmt (t);
18242 break;
18243
18244 case OACC_DECLARE:
18245 t = copy_node (t);
18246 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18247 complain, in_decl);
18248 OACC_DECLARE_CLAUSES (t) = tmp;
18249 add_stmt (t);
18250 break;
18251
18252 case OMP_TARGET_UPDATE:
18253 case OMP_TARGET_ENTER_DATA:
18254 case OMP_TARGET_EXIT_DATA:
18255 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18256 complain, in_decl);
18257 t = copy_node (t);
18258 OMP_STANDALONE_CLAUSES (t) = tmp;
18259 add_stmt (t);
18260 break;
18261
18262 case OACC_ENTER_DATA:
18263 case OACC_EXIT_DATA:
18264 case OACC_UPDATE:
18265 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18266 complain, in_decl);
18267 t = copy_node (t);
18268 OMP_STANDALONE_CLAUSES (t) = tmp;
18269 add_stmt (t);
18270 break;
18271
18272 case OMP_ORDERED:
18273 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18274 complain, in_decl);
18275 stmt = push_stmt_list ();
18276 RECUR (OMP_BODY (t));
18277 stmt = pop_stmt_list (stmt);
18278
18279 t = copy_node (t);
18280 OMP_BODY (t) = stmt;
18281 OMP_ORDERED_CLAUSES (t) = tmp;
18282 add_stmt (t);
18283 break;
18284
18285 case OMP_SECTION:
18286 case OMP_MASTER:
18287 stmt = push_stmt_list ();
18288 RECUR (OMP_BODY (t));
18289 stmt = pop_stmt_list (stmt);
18290
18291 t = copy_node (t);
18292 OMP_BODY (t) = stmt;
18293 add_stmt (t);
18294 break;
18295
18296 case OMP_ATOMIC:
18297 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18298 tmp = NULL_TREE;
18299 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18300 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18301 complain, in_decl);
18302 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18303 {
18304 tree op1 = TREE_OPERAND (t, 1);
18305 tree rhs1 = NULL_TREE;
18306 tree lhs, rhs;
18307 if (TREE_CODE (op1) == COMPOUND_EXPR)
18308 {
18309 rhs1 = RECUR (TREE_OPERAND (op1, 0));
18310 op1 = TREE_OPERAND (op1, 1);
18311 }
18312 lhs = RECUR (TREE_OPERAND (op1, 0));
18313 rhs = RECUR (TREE_OPERAND (op1, 1));
18314 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
18315 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
18316 OMP_ATOMIC_MEMORY_ORDER (t));
18317 }
18318 else
18319 {
18320 tree op1 = TREE_OPERAND (t, 1);
18321 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
18322 tree rhs1 = NULL_TREE;
18323 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
18324 enum tree_code opcode = NOP_EXPR;
18325 if (code == OMP_ATOMIC_READ)
18326 {
18327 v = RECUR (TREE_OPERAND (op1, 0));
18328 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18329 }
18330 else if (code == OMP_ATOMIC_CAPTURE_OLD
18331 || code == OMP_ATOMIC_CAPTURE_NEW)
18332 {
18333 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
18334 v = RECUR (TREE_OPERAND (op1, 0));
18335 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
18336 if (TREE_CODE (op11) == COMPOUND_EXPR)
18337 {
18338 rhs1 = RECUR (TREE_OPERAND (op11, 0));
18339 op11 = TREE_OPERAND (op11, 1);
18340 }
18341 lhs = RECUR (TREE_OPERAND (op11, 0));
18342 rhs = RECUR (TREE_OPERAND (op11, 1));
18343 opcode = TREE_CODE (op11);
18344 if (opcode == MODIFY_EXPR)
18345 opcode = NOP_EXPR;
18346 }
18347 else
18348 {
18349 code = OMP_ATOMIC;
18350 lhs = RECUR (TREE_OPERAND (op1, 0));
18351 rhs = RECUR (TREE_OPERAND (op1, 1));
18352 }
18353 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
18354 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
18355 }
18356 break;
18357
18358 case TRANSACTION_EXPR:
18359 {
18360 int flags = 0;
18361 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
18362 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
18363
18364 if (TRANSACTION_EXPR_IS_STMT (t))
18365 {
18366 tree body = TRANSACTION_EXPR_BODY (t);
18367 tree noex = NULL_TREE;
18368 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
18369 {
18370 noex = MUST_NOT_THROW_COND (body);
18371 if (noex == NULL_TREE)
18372 noex = boolean_true_node;
18373 body = TREE_OPERAND (body, 0);
18374 }
18375 stmt = begin_transaction_stmt (input_location, NULL, flags);
18376 RECUR (body);
18377 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
18378 }
18379 else
18380 {
18381 stmt = build_transaction_expr (EXPR_LOCATION (t),
18382 RECUR (TRANSACTION_EXPR_BODY (t)),
18383 flags, NULL_TREE);
18384 RETURN (stmt);
18385 }
18386 }
18387 break;
18388
18389 case MUST_NOT_THROW_EXPR:
18390 {
18391 tree op0 = RECUR (TREE_OPERAND (t, 0));
18392 tree cond = RECUR (MUST_NOT_THROW_COND (t));
18393 RETURN (build_must_not_throw_expr (op0, cond));
18394 }
18395
18396 case EXPR_PACK_EXPANSION:
18397 error ("invalid use of pack expansion expression");
18398 RETURN (error_mark_node);
18399
18400 case NONTYPE_ARGUMENT_PACK:
18401 error ("use %<...%> to expand argument pack");
18402 RETURN (error_mark_node);
18403
18404 case COMPOUND_EXPR:
18405 tmp = RECUR (TREE_OPERAND (t, 0));
18406 if (tmp == NULL_TREE)
18407 /* If the first operand was a statement, we're done with it. */
18408 RETURN (RECUR (TREE_OPERAND (t, 1)));
18409 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
18410 RECUR (TREE_OPERAND (t, 1)),
18411 complain));
18412
18413 case ANNOTATE_EXPR:
18414 tmp = RECUR (TREE_OPERAND (t, 0));
18415 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
18416 TREE_TYPE (tmp), tmp,
18417 RECUR (TREE_OPERAND (t, 1)),
18418 RECUR (TREE_OPERAND (t, 2))));
18419
18420 case PREDICT_EXPR:
18421 RETURN (add_stmt (copy_node (t)));
18422
18423 default:
18424 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
18425
18426 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
18427 /*function_p=*/false,
18428 integral_constant_expression_p));
18429 }
18430
18431 RETURN (NULL_TREE);
18432 out:
18433 input_location = loc;
18434 return r;
18435 #undef RECUR
18436 #undef RETURN
18437 }
18438
18439 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
18440 function. For description of the body see comment above
18441 cp_parser_omp_declare_reduction_exprs. */
18442
18443 static void
18444 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18445 {
18446 if (t == NULL_TREE || t == error_mark_node)
18447 return;
18448
18449 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
18450
18451 tree_stmt_iterator tsi;
18452 int i;
18453 tree stmts[7];
18454 memset (stmts, 0, sizeof stmts);
18455 for (i = 0, tsi = tsi_start (t);
18456 i < 7 && !tsi_end_p (tsi);
18457 i++, tsi_next (&tsi))
18458 stmts[i] = tsi_stmt (tsi);
18459 gcc_assert (tsi_end_p (tsi));
18460
18461 if (i >= 3)
18462 {
18463 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
18464 && TREE_CODE (stmts[1]) == DECL_EXPR);
18465 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
18466 args, complain, in_decl);
18467 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
18468 args, complain, in_decl);
18469 DECL_CONTEXT (omp_out) = current_function_decl;
18470 DECL_CONTEXT (omp_in) = current_function_decl;
18471 keep_next_level (true);
18472 tree block = begin_omp_structured_block ();
18473 tsubst_expr (stmts[2], args, complain, in_decl, false);
18474 block = finish_omp_structured_block (block);
18475 block = maybe_cleanup_point_expr_void (block);
18476 add_decl_expr (omp_out);
18477 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
18478 TREE_NO_WARNING (omp_out) = 1;
18479 add_decl_expr (omp_in);
18480 finish_expr_stmt (block);
18481 }
18482 if (i >= 6)
18483 {
18484 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18485 && TREE_CODE (stmts[4]) == DECL_EXPR);
18486 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18487 args, complain, in_decl);
18488 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18489 args, complain, in_decl);
18490 DECL_CONTEXT (omp_priv) = current_function_decl;
18491 DECL_CONTEXT (omp_orig) = current_function_decl;
18492 keep_next_level (true);
18493 tree block = begin_omp_structured_block ();
18494 tsubst_expr (stmts[5], args, complain, in_decl, false);
18495 block = finish_omp_structured_block (block);
18496 block = maybe_cleanup_point_expr_void (block);
18497 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18498 add_decl_expr (omp_priv);
18499 add_decl_expr (omp_orig);
18500 finish_expr_stmt (block);
18501 if (i == 7)
18502 add_decl_expr (omp_orig);
18503 }
18504 }
18505
18506 /* T is a postfix-expression that is not being used in a function
18507 call. Return the substituted version of T. */
18508
18509 static tree
18510 tsubst_non_call_postfix_expression (tree t, tree args,
18511 tsubst_flags_t complain,
18512 tree in_decl)
18513 {
18514 if (TREE_CODE (t) == SCOPE_REF)
18515 t = tsubst_qualified_id (t, args, complain, in_decl,
18516 /*done=*/false, /*address_p=*/false);
18517 else
18518 t = tsubst_copy_and_build (t, args, complain, in_decl,
18519 /*function_p=*/false,
18520 /*integral_constant_expression_p=*/false);
18521
18522 return t;
18523 }
18524
18525 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18526 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18527 dependent init-capture. */
18528
18529 static void
18530 prepend_one_capture (tree field, tree init, tree &list,
18531 tsubst_flags_t complain)
18532 {
18533 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18534 {
18535 tree type = NULL_TREE;
18536 if (!init)
18537 {
18538 if (complain & tf_error)
18539 error ("empty initializer in lambda init-capture");
18540 init = error_mark_node;
18541 }
18542 else if (TREE_CODE (init) == TREE_LIST)
18543 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18544 if (!type)
18545 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18546 TREE_TYPE (field) = type;
18547 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18548 }
18549 list = tree_cons (field, init, list);
18550 }
18551
18552 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18553 instantiation context. Instantiating a pack expansion containing a lambda
18554 might result in multiple lambdas all based on the same lambda in the
18555 template. */
18556
18557 tree
18558 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18559 {
18560 tree oldfn = lambda_function (t);
18561 in_decl = oldfn;
18562
18563 tree r = build_lambda_expr ();
18564
18565 LAMBDA_EXPR_LOCATION (r)
18566 = LAMBDA_EXPR_LOCATION (t);
18567 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18568 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18569 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18570 LAMBDA_EXPR_INSTANTIATED (r) = true;
18571
18572 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18573 /* A lambda in a default argument outside a class gets no
18574 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18575 tsubst_default_argument calls start_lambda_scope, so we need to
18576 specifically ignore it here, and use the global scope. */
18577 record_null_lambda_scope (r);
18578 else
18579 record_lambda_scope (r);
18580
18581 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18582 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18583
18584 vec<tree,va_gc>* field_packs = NULL;
18585
18586 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18587 cap = TREE_CHAIN (cap))
18588 {
18589 tree ofield = TREE_PURPOSE (cap);
18590 if (PACK_EXPANSION_P (ofield))
18591 ofield = PACK_EXPANSION_PATTERN (ofield);
18592 tree field = tsubst_decl (ofield, args, complain);
18593
18594 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18595 {
18596 /* Remember these for when we've pushed local_specializations. */
18597 vec_safe_push (field_packs, ofield);
18598 vec_safe_push (field_packs, field);
18599 }
18600
18601 if (field == error_mark_node)
18602 return error_mark_node;
18603
18604 tree init = TREE_VALUE (cap);
18605 if (PACK_EXPANSION_P (init))
18606 init = tsubst_pack_expansion (init, args, complain, in_decl);
18607 else
18608 init = tsubst_copy_and_build (init, args, complain, in_decl,
18609 /*fn*/false, /*constexpr*/false);
18610
18611 if (TREE_CODE (field) == TREE_VEC)
18612 {
18613 int len = TREE_VEC_LENGTH (field);
18614 gcc_assert (TREE_CODE (init) == TREE_VEC
18615 && TREE_VEC_LENGTH (init) == len);
18616 for (int i = 0; i < len; ++i)
18617 prepend_one_capture (TREE_VEC_ELT (field, i),
18618 TREE_VEC_ELT (init, i),
18619 LAMBDA_EXPR_CAPTURE_LIST (r),
18620 complain);
18621 }
18622 else
18623 {
18624 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18625 complain);
18626
18627 if (id_equal (DECL_NAME (field), "__this"))
18628 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18629 }
18630 }
18631
18632 tree type = begin_lambda_type (r);
18633 if (type == error_mark_node)
18634 return error_mark_node;
18635
18636 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18637 determine_visibility (TYPE_NAME (type));
18638
18639 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18640
18641 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18642 ? DECL_TI_TEMPLATE (oldfn)
18643 : NULL_TREE);
18644
18645 tree fntype = static_fn_type (oldfn);
18646 if (oldtmpl)
18647 ++processing_template_decl;
18648 fntype = tsubst (fntype, args, complain, in_decl);
18649 if (oldtmpl)
18650 --processing_template_decl;
18651
18652 if (fntype == error_mark_node)
18653 r = error_mark_node;
18654 else
18655 {
18656 /* The body of a lambda-expression is not a subexpression of the
18657 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18658 which would be skipped if cp_unevaluated_operand. */
18659 cp_evaluated ev;
18660
18661 /* Fix the type of 'this'. */
18662 fntype = build_memfn_type (fntype, type,
18663 type_memfn_quals (fntype),
18664 type_memfn_rqual (fntype));
18665 tree fn, tmpl;
18666 if (oldtmpl)
18667 {
18668 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18669 fn = DECL_TEMPLATE_RESULT (tmpl);
18670 finish_member_declaration (tmpl);
18671 }
18672 else
18673 {
18674 tmpl = NULL_TREE;
18675 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18676 finish_member_declaration (fn);
18677 }
18678
18679 /* Let finish_function set this. */
18680 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18681
18682 bool nested = cfun;
18683 if (nested)
18684 push_function_context ();
18685 else
18686 /* Still increment function_depth so that we don't GC in the
18687 middle of an expression. */
18688 ++function_depth;
18689
18690 local_specialization_stack s (lss_copy);
18691
18692 tree body = start_lambda_function (fn, r);
18693
18694 /* Now record them for lookup_init_capture_pack. */
18695 int fplen = vec_safe_length (field_packs);
18696 for (int i = 0; i < fplen; )
18697 {
18698 tree pack = (*field_packs)[i++];
18699 tree inst = (*field_packs)[i++];
18700 register_local_specialization (inst, pack);
18701 }
18702 release_tree_vector (field_packs);
18703
18704 register_parameter_specializations (oldfn, fn);
18705
18706 if (oldtmpl)
18707 {
18708 /* We might not partially instantiate some parts of the function, so
18709 copy these flags from the original template. */
18710 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18711 current_function_returns_value = ol->returns_value;
18712 current_function_returns_null = ol->returns_null;
18713 current_function_returns_abnormally = ol->returns_abnormally;
18714 current_function_infinite_loop = ol->infinite_loop;
18715 }
18716
18717 /* [temp.deduct] A lambda-expression appearing in a function type or a
18718 template parameter is not considered part of the immediate context for
18719 the purposes of template argument deduction. */
18720 complain = tf_warning_or_error;
18721
18722 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18723 /*constexpr*/false);
18724
18725 finish_lambda_function (body);
18726
18727 if (nested)
18728 pop_function_context ();
18729 else
18730 --function_depth;
18731
18732 /* The capture list was built up in reverse order; fix that now. */
18733 LAMBDA_EXPR_CAPTURE_LIST (r)
18734 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18735
18736 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18737
18738 maybe_add_lambda_conv_op (type);
18739 }
18740
18741 finish_struct (type, /*attr*/NULL_TREE);
18742
18743 insert_pending_capture_proxies ();
18744
18745 return r;
18746 }
18747
18748 /* Like tsubst but deals with expressions and performs semantic
18749 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18750
18751 tree
18752 tsubst_copy_and_build (tree t,
18753 tree args,
18754 tsubst_flags_t complain,
18755 tree in_decl,
18756 bool function_p,
18757 bool integral_constant_expression_p)
18758 {
18759 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18760 #define RECUR(NODE) \
18761 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18762 /*function_p=*/false, \
18763 integral_constant_expression_p)
18764
18765 tree retval, op1;
18766 location_t loc;
18767
18768 if (t == NULL_TREE || t == error_mark_node)
18769 return t;
18770
18771 loc = input_location;
18772 if (location_t eloc = cp_expr_location (t))
18773 input_location = eloc;
18774
18775 /* N3276 decltype magic only applies to calls at the top level or on the
18776 right side of a comma. */
18777 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18778 complain &= ~tf_decltype;
18779
18780 switch (TREE_CODE (t))
18781 {
18782 case USING_DECL:
18783 t = DECL_NAME (t);
18784 /* Fall through. */
18785 case IDENTIFIER_NODE:
18786 {
18787 tree decl;
18788 cp_id_kind idk;
18789 bool non_integral_constant_expression_p;
18790 const char *error_msg;
18791
18792 if (IDENTIFIER_CONV_OP_P (t))
18793 {
18794 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18795 t = make_conv_op_name (new_type);
18796 }
18797
18798 /* Look up the name. */
18799 decl = lookup_name (t);
18800
18801 /* By convention, expressions use ERROR_MARK_NODE to indicate
18802 failure, not NULL_TREE. */
18803 if (decl == NULL_TREE)
18804 decl = error_mark_node;
18805
18806 decl = finish_id_expression (t, decl, NULL_TREE,
18807 &idk,
18808 integral_constant_expression_p,
18809 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18810 &non_integral_constant_expression_p,
18811 /*template_p=*/false,
18812 /*done=*/true,
18813 /*address_p=*/false,
18814 /*template_arg_p=*/false,
18815 &error_msg,
18816 input_location);
18817 if (error_msg)
18818 error (error_msg);
18819 if (!function_p && identifier_p (decl))
18820 {
18821 if (complain & tf_error)
18822 unqualified_name_lookup_error (decl);
18823 decl = error_mark_node;
18824 }
18825 RETURN (decl);
18826 }
18827
18828 case TEMPLATE_ID_EXPR:
18829 {
18830 tree object;
18831 tree templ = RECUR (TREE_OPERAND (t, 0));
18832 tree targs = TREE_OPERAND (t, 1);
18833
18834 if (targs)
18835 targs = tsubst_template_args (targs, args, complain, in_decl);
18836 if (targs == error_mark_node)
18837 RETURN (error_mark_node);
18838
18839 if (TREE_CODE (templ) == SCOPE_REF)
18840 {
18841 tree name = TREE_OPERAND (templ, 1);
18842 tree tid = lookup_template_function (name, targs);
18843 TREE_OPERAND (templ, 1) = tid;
18844 RETURN (templ);
18845 }
18846
18847 if (concept_definition_p (templ))
18848 {
18849 tree check = build_concept_check (templ, targs, complain);
18850 if (check == error_mark_node)
18851 RETURN (error_mark_node);
18852
18853 tree id = unpack_concept_check (check);
18854
18855 /* If we built a function concept check, return the underlying
18856 template-id. So we can evaluate it as a function call. */
18857 if (function_concept_p (TREE_OPERAND (id, 0)))
18858 RETURN (id);
18859
18860 /* Evaluate the concept, if needed. */
18861 tree args = TREE_OPERAND (id, 1);
18862 if (!uses_template_parms (args)
18863 && !processing_constraint_expression_p ())
18864 RETURN (evaluate_concept_check (check, complain));
18865
18866 RETURN (check);
18867 }
18868
18869 if (variable_template_p (templ))
18870 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18871
18872 if (TREE_CODE (templ) == COMPONENT_REF)
18873 {
18874 object = TREE_OPERAND (templ, 0);
18875 templ = TREE_OPERAND (templ, 1);
18876 }
18877 else
18878 object = NULL_TREE;
18879 templ = lookup_template_function (templ, targs);
18880
18881 if (object)
18882 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18883 object, templ, NULL_TREE));
18884 else
18885 RETURN (baselink_for_fns (templ));
18886 }
18887
18888 case INDIRECT_REF:
18889 {
18890 tree r = RECUR (TREE_OPERAND (t, 0));
18891
18892 if (REFERENCE_REF_P (t))
18893 {
18894 /* A type conversion to reference type will be enclosed in
18895 such an indirect ref, but the substitution of the cast
18896 will have also added such an indirect ref. */
18897 r = convert_from_reference (r);
18898 }
18899 else
18900 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18901 complain|decltype_flag);
18902
18903 if (REF_PARENTHESIZED_P (t))
18904 r = force_paren_expr (r);
18905
18906 RETURN (r);
18907 }
18908
18909 case NOP_EXPR:
18910 {
18911 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18912 tree op0 = RECUR (TREE_OPERAND (t, 0));
18913 RETURN (build_nop (type, op0));
18914 }
18915
18916 case IMPLICIT_CONV_EXPR:
18917 {
18918 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18919 tree expr = RECUR (TREE_OPERAND (t, 0));
18920 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18921 {
18922 retval = copy_node (t);
18923 TREE_TYPE (retval) = type;
18924 TREE_OPERAND (retval, 0) = expr;
18925 RETURN (retval);
18926 }
18927 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18928 /* We'll pass this to convert_nontype_argument again, we don't need
18929 to actually perform any conversion here. */
18930 RETURN (expr);
18931 int flags = LOOKUP_IMPLICIT;
18932 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18933 flags = LOOKUP_NORMAL;
18934 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18935 flags |= LOOKUP_NO_NARROWING;
18936 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18937 flags));
18938 }
18939
18940 case CONVERT_EXPR:
18941 {
18942 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18943 tree op0 = RECUR (TREE_OPERAND (t, 0));
18944 if (op0 == error_mark_node)
18945 RETURN (error_mark_node);
18946 RETURN (build1 (CONVERT_EXPR, type, op0));
18947 }
18948
18949 case CAST_EXPR:
18950 case REINTERPRET_CAST_EXPR:
18951 case CONST_CAST_EXPR:
18952 case DYNAMIC_CAST_EXPR:
18953 case STATIC_CAST_EXPR:
18954 {
18955 tree type;
18956 tree op, r = NULL_TREE;
18957
18958 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18959 if (integral_constant_expression_p
18960 && !cast_valid_in_integral_constant_expression_p (type))
18961 {
18962 if (complain & tf_error)
18963 error ("a cast to a type other than an integral or "
18964 "enumeration type cannot appear in a constant-expression");
18965 RETURN (error_mark_node);
18966 }
18967
18968 op = RECUR (TREE_OPERAND (t, 0));
18969
18970 warning_sentinel s(warn_useless_cast);
18971 warning_sentinel s2(warn_ignored_qualifiers);
18972 switch (TREE_CODE (t))
18973 {
18974 case CAST_EXPR:
18975 r = build_functional_cast (type, op, complain);
18976 break;
18977 case REINTERPRET_CAST_EXPR:
18978 r = build_reinterpret_cast (type, op, complain);
18979 break;
18980 case CONST_CAST_EXPR:
18981 r = build_const_cast (type, op, complain);
18982 break;
18983 case DYNAMIC_CAST_EXPR:
18984 r = build_dynamic_cast (type, op, complain);
18985 break;
18986 case STATIC_CAST_EXPR:
18987 r = build_static_cast (type, op, complain);
18988 break;
18989 default:
18990 gcc_unreachable ();
18991 }
18992
18993 RETURN (r);
18994 }
18995
18996 case POSTDECREMENT_EXPR:
18997 case POSTINCREMENT_EXPR:
18998 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18999 args, complain, in_decl);
19000 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19001 complain|decltype_flag));
19002
19003 case PREDECREMENT_EXPR:
19004 case PREINCREMENT_EXPR:
19005 case NEGATE_EXPR:
19006 case BIT_NOT_EXPR:
19007 case ABS_EXPR:
19008 case TRUTH_NOT_EXPR:
19009 case UNARY_PLUS_EXPR: /* Unary + */
19010 case REALPART_EXPR:
19011 case IMAGPART_EXPR:
19012 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19013 RECUR (TREE_OPERAND (t, 0)),
19014 complain|decltype_flag));
19015
19016 case FIX_TRUNC_EXPR:
19017 gcc_unreachable ();
19018
19019 case ADDR_EXPR:
19020 op1 = TREE_OPERAND (t, 0);
19021 if (TREE_CODE (op1) == LABEL_DECL)
19022 RETURN (finish_label_address_expr (DECL_NAME (op1),
19023 EXPR_LOCATION (op1)));
19024 if (TREE_CODE (op1) == SCOPE_REF)
19025 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19026 /*done=*/true, /*address_p=*/true);
19027 else
19028 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19029 in_decl);
19030 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19031 complain|decltype_flag));
19032
19033 case PLUS_EXPR:
19034 case MINUS_EXPR:
19035 case MULT_EXPR:
19036 case TRUNC_DIV_EXPR:
19037 case CEIL_DIV_EXPR:
19038 case FLOOR_DIV_EXPR:
19039 case ROUND_DIV_EXPR:
19040 case EXACT_DIV_EXPR:
19041 case BIT_AND_EXPR:
19042 case BIT_IOR_EXPR:
19043 case BIT_XOR_EXPR:
19044 case TRUNC_MOD_EXPR:
19045 case FLOOR_MOD_EXPR:
19046 case TRUTH_ANDIF_EXPR:
19047 case TRUTH_ORIF_EXPR:
19048 case TRUTH_AND_EXPR:
19049 case TRUTH_OR_EXPR:
19050 case RSHIFT_EXPR:
19051 case LSHIFT_EXPR:
19052 case EQ_EXPR:
19053 case NE_EXPR:
19054 case MAX_EXPR:
19055 case MIN_EXPR:
19056 case LE_EXPR:
19057 case GE_EXPR:
19058 case LT_EXPR:
19059 case GT_EXPR:
19060 case SPACESHIP_EXPR:
19061 case MEMBER_REF:
19062 case DOTSTAR_EXPR:
19063 {
19064 warning_sentinel s1(warn_type_limits);
19065 warning_sentinel s2(warn_div_by_zero);
19066 warning_sentinel s3(warn_logical_op);
19067 warning_sentinel s4(warn_tautological_compare);
19068 tree op0 = RECUR (TREE_OPERAND (t, 0));
19069 tree op1 = RECUR (TREE_OPERAND (t, 1));
19070 tree r = build_x_binary_op
19071 (input_location, TREE_CODE (t),
19072 op0,
19073 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19074 ? ERROR_MARK
19075 : TREE_CODE (TREE_OPERAND (t, 0))),
19076 op1,
19077 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19078 ? ERROR_MARK
19079 : TREE_CODE (TREE_OPERAND (t, 1))),
19080 /*overload=*/NULL,
19081 complain|decltype_flag);
19082 if (EXPR_P (r) && TREE_NO_WARNING (t))
19083 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19084
19085 RETURN (r);
19086 }
19087
19088 case POINTER_PLUS_EXPR:
19089 {
19090 tree op0 = RECUR (TREE_OPERAND (t, 0));
19091 tree op1 = RECUR (TREE_OPERAND (t, 1));
19092 RETURN (fold_build_pointer_plus (op0, op1));
19093 }
19094
19095 case SCOPE_REF:
19096 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19097 /*address_p=*/false));
19098 case ARRAY_REF:
19099 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19100 args, complain, in_decl);
19101 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19102 RECUR (TREE_OPERAND (t, 1)),
19103 complain|decltype_flag));
19104
19105 case SIZEOF_EXPR:
19106 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19107 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19108 RETURN (tsubst_copy (t, args, complain, in_decl));
19109 /* Fall through */
19110
19111 case ALIGNOF_EXPR:
19112 {
19113 tree r;
19114
19115 op1 = TREE_OPERAND (t, 0);
19116 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19117 op1 = TREE_TYPE (op1);
19118 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19119 && ALIGNOF_EXPR_STD_P (t));
19120 if (!args)
19121 {
19122 /* When there are no ARGS, we are trying to evaluate a
19123 non-dependent expression from the parser. Trying to do
19124 the substitutions may not work. */
19125 if (!TYPE_P (op1))
19126 op1 = TREE_TYPE (op1);
19127 }
19128 else
19129 {
19130 ++cp_unevaluated_operand;
19131 ++c_inhibit_evaluation_warnings;
19132 if (TYPE_P (op1))
19133 op1 = tsubst (op1, args, complain, in_decl);
19134 else
19135 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19136 /*function_p=*/false,
19137 /*integral_constant_expression_p=*/
19138 false);
19139 --cp_unevaluated_operand;
19140 --c_inhibit_evaluation_warnings;
19141 }
19142 if (TYPE_P (op1))
19143 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
19144 complain & tf_error);
19145 else
19146 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
19147 complain & tf_error);
19148 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19149 {
19150 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19151 {
19152 if (!processing_template_decl && TYPE_P (op1))
19153 {
19154 r = build_min (SIZEOF_EXPR, size_type_node,
19155 build1 (NOP_EXPR, op1, error_mark_node));
19156 SIZEOF_EXPR_TYPE_P (r) = 1;
19157 }
19158 else
19159 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19160 TREE_SIDE_EFFECTS (r) = 0;
19161 TREE_READONLY (r) = 1;
19162 }
19163 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19164 }
19165 RETURN (r);
19166 }
19167
19168 case AT_ENCODE_EXPR:
19169 {
19170 op1 = TREE_OPERAND (t, 0);
19171 ++cp_unevaluated_operand;
19172 ++c_inhibit_evaluation_warnings;
19173 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19174 /*function_p=*/false,
19175 /*integral_constant_expression_p=*/false);
19176 --cp_unevaluated_operand;
19177 --c_inhibit_evaluation_warnings;
19178 RETURN (objc_build_encode_expr (op1));
19179 }
19180
19181 case NOEXCEPT_EXPR:
19182 op1 = TREE_OPERAND (t, 0);
19183 ++cp_unevaluated_operand;
19184 ++c_inhibit_evaluation_warnings;
19185 ++cp_noexcept_operand;
19186 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19187 /*function_p=*/false,
19188 /*integral_constant_expression_p=*/false);
19189 --cp_unevaluated_operand;
19190 --c_inhibit_evaluation_warnings;
19191 --cp_noexcept_operand;
19192 RETURN (finish_noexcept_expr (op1, complain));
19193
19194 case MODOP_EXPR:
19195 {
19196 warning_sentinel s(warn_div_by_zero);
19197 tree lhs = RECUR (TREE_OPERAND (t, 0));
19198 tree rhs = RECUR (TREE_OPERAND (t, 2));
19199 tree r = build_x_modify_expr
19200 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19201 complain|decltype_flag);
19202 /* TREE_NO_WARNING must be set if either the expression was
19203 parenthesized or it uses an operator such as >>= rather
19204 than plain assignment. In the former case, it was already
19205 set and must be copied. In the latter case,
19206 build_x_modify_expr sets it and it must not be reset
19207 here. */
19208 if (TREE_NO_WARNING (t))
19209 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19210
19211 RETURN (r);
19212 }
19213
19214 case ARROW_EXPR:
19215 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19216 args, complain, in_decl);
19217 /* Remember that there was a reference to this entity. */
19218 if (DECL_P (op1)
19219 && !mark_used (op1, complain) && !(complain & tf_error))
19220 RETURN (error_mark_node);
19221 RETURN (build_x_arrow (input_location, op1, complain));
19222
19223 case NEW_EXPR:
19224 {
19225 tree placement = RECUR (TREE_OPERAND (t, 0));
19226 tree init = RECUR (TREE_OPERAND (t, 3));
19227 vec<tree, va_gc> *placement_vec;
19228 vec<tree, va_gc> *init_vec;
19229 tree ret;
19230
19231 if (placement == NULL_TREE)
19232 placement_vec = NULL;
19233 else
19234 {
19235 placement_vec = make_tree_vector ();
19236 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
19237 vec_safe_push (placement_vec, TREE_VALUE (placement));
19238 }
19239
19240 /* If there was an initializer in the original tree, but it
19241 instantiated to an empty list, then we should pass a
19242 non-NULL empty vector to tell build_new that it was an
19243 empty initializer() rather than no initializer. This can
19244 only happen when the initializer is a pack expansion whose
19245 parameter packs are of length zero. */
19246 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
19247 init_vec = NULL;
19248 else
19249 {
19250 init_vec = make_tree_vector ();
19251 if (init == void_node)
19252 gcc_assert (init_vec != NULL);
19253 else
19254 {
19255 for (; init != NULL_TREE; init = TREE_CHAIN (init))
19256 vec_safe_push (init_vec, TREE_VALUE (init));
19257 }
19258 }
19259
19260 /* Avoid passing an enclosing decl to valid_array_size_p. */
19261 in_decl = NULL_TREE;
19262
19263 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
19264 tree op2 = RECUR (TREE_OPERAND (t, 2));
19265 ret = build_new (&placement_vec, op1, op2, &init_vec,
19266 NEW_EXPR_USE_GLOBAL (t),
19267 complain);
19268
19269 if (placement_vec != NULL)
19270 release_tree_vector (placement_vec);
19271 if (init_vec != NULL)
19272 release_tree_vector (init_vec);
19273
19274 RETURN (ret);
19275 }
19276
19277 case DELETE_EXPR:
19278 {
19279 tree op0 = RECUR (TREE_OPERAND (t, 0));
19280 tree op1 = RECUR (TREE_OPERAND (t, 1));
19281 RETURN (delete_sanity (op0, op1,
19282 DELETE_EXPR_USE_VEC (t),
19283 DELETE_EXPR_USE_GLOBAL (t),
19284 complain));
19285 }
19286
19287 case COMPOUND_EXPR:
19288 {
19289 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19290 complain & ~tf_decltype, in_decl,
19291 /*function_p=*/false,
19292 integral_constant_expression_p);
19293 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
19294 op0,
19295 RECUR (TREE_OPERAND (t, 1)),
19296 complain|decltype_flag));
19297 }
19298
19299 case CALL_EXPR:
19300 {
19301 tree function;
19302 unsigned int nargs, i;
19303 bool qualified_p;
19304 bool koenig_p;
19305 tree ret;
19306
19307 function = CALL_EXPR_FN (t);
19308 /* Internal function with no arguments. */
19309 if (function == NULL_TREE && call_expr_nargs (t) == 0)
19310 RETURN (t);
19311
19312 /* When we parsed the expression, we determined whether or
19313 not Koenig lookup should be performed. */
19314 koenig_p = KOENIG_LOOKUP_P (t);
19315 if (function == NULL_TREE)
19316 {
19317 koenig_p = false;
19318 qualified_p = false;
19319 }
19320 else if (TREE_CODE (function) == SCOPE_REF)
19321 {
19322 qualified_p = true;
19323 function = tsubst_qualified_id (function, args, complain, in_decl,
19324 /*done=*/false,
19325 /*address_p=*/false);
19326 }
19327 else if (koenig_p && identifier_p (function))
19328 {
19329 /* Do nothing; calling tsubst_copy_and_build on an identifier
19330 would incorrectly perform unqualified lookup again.
19331
19332 Note that we can also have an IDENTIFIER_NODE if the earlier
19333 unqualified lookup found a member function; in that case
19334 koenig_p will be false and we do want to do the lookup
19335 again to find the instantiated member function.
19336
19337 FIXME but doing that causes c++/15272, so we need to stop
19338 using IDENTIFIER_NODE in that situation. */
19339 qualified_p = false;
19340 }
19341 else
19342 {
19343 if (TREE_CODE (function) == COMPONENT_REF)
19344 {
19345 tree op = TREE_OPERAND (function, 1);
19346
19347 qualified_p = (TREE_CODE (op) == SCOPE_REF
19348 || (BASELINK_P (op)
19349 && BASELINK_QUALIFIED_P (op)));
19350 }
19351 else
19352 qualified_p = false;
19353
19354 if (TREE_CODE (function) == ADDR_EXPR
19355 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
19356 /* Avoid error about taking the address of a constructor. */
19357 function = TREE_OPERAND (function, 0);
19358
19359 function = tsubst_copy_and_build (function, args, complain,
19360 in_decl,
19361 !qualified_p,
19362 integral_constant_expression_p);
19363
19364 if (BASELINK_P (function))
19365 qualified_p = true;
19366 }
19367
19368 nargs = call_expr_nargs (t);
19369 releasing_vec call_args;
19370 for (i = 0; i < nargs; ++i)
19371 {
19372 tree arg = CALL_EXPR_ARG (t, i);
19373
19374 if (!PACK_EXPANSION_P (arg))
19375 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
19376 else
19377 {
19378 /* Expand the pack expansion and push each entry onto
19379 CALL_ARGS. */
19380 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
19381 if (TREE_CODE (arg) == TREE_VEC)
19382 {
19383 unsigned int len, j;
19384
19385 len = TREE_VEC_LENGTH (arg);
19386 for (j = 0; j < len; ++j)
19387 {
19388 tree value = TREE_VEC_ELT (arg, j);
19389 if (value != NULL_TREE)
19390 value = convert_from_reference (value);
19391 vec_safe_push (call_args, value);
19392 }
19393 }
19394 else
19395 {
19396 /* A partial substitution. Add one entry. */
19397 vec_safe_push (call_args, arg);
19398 }
19399 }
19400 }
19401
19402 /* Stripped-down processing for a call in a thunk. Specifically, in
19403 the thunk template for a generic lambda. */
19404 if (CALL_FROM_THUNK_P (t))
19405 {
19406 /* Now that we've expanded any packs, the number of call args
19407 might be different. */
19408 unsigned int cargs = call_args->length ();
19409 tree thisarg = NULL_TREE;
19410 if (TREE_CODE (function) == COMPONENT_REF)
19411 {
19412 thisarg = TREE_OPERAND (function, 0);
19413 if (TREE_CODE (thisarg) == INDIRECT_REF)
19414 thisarg = TREE_OPERAND (thisarg, 0);
19415 function = TREE_OPERAND (function, 1);
19416 if (TREE_CODE (function) == BASELINK)
19417 function = BASELINK_FUNCTIONS (function);
19418 }
19419 /* We aren't going to do normal overload resolution, so force the
19420 template-id to resolve. */
19421 function = resolve_nondeduced_context (function, complain);
19422 for (unsigned i = 0; i < cargs; ++i)
19423 {
19424 /* In a thunk, pass through args directly, without any
19425 conversions. */
19426 tree arg = (*call_args)[i];
19427 while (TREE_CODE (arg) != PARM_DECL)
19428 arg = TREE_OPERAND (arg, 0);
19429 (*call_args)[i] = arg;
19430 }
19431 if (thisarg)
19432 {
19433 /* If there are no other args, just push 'this'. */
19434 if (cargs == 0)
19435 vec_safe_push (call_args, thisarg);
19436 else
19437 {
19438 /* Otherwise, shift the other args over to make room. */
19439 tree last = (*call_args)[cargs - 1];
19440 vec_safe_push (call_args, last);
19441 for (int i = cargs - 1; i > 0; --i)
19442 (*call_args)[i] = (*call_args)[i - 1];
19443 (*call_args)[0] = thisarg;
19444 }
19445 }
19446 ret = build_call_a (function, call_args->length (),
19447 call_args->address ());
19448 /* The thunk location is not interesting. */
19449 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
19450 CALL_FROM_THUNK_P (ret) = true;
19451 if (CLASS_TYPE_P (TREE_TYPE (ret)))
19452 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
19453
19454 RETURN (ret);
19455 }
19456
19457 /* We do not perform argument-dependent lookup if normal
19458 lookup finds a non-function, in accordance with the
19459 expected resolution of DR 218. */
19460 if (koenig_p
19461 && ((is_overloaded_fn (function)
19462 /* If lookup found a member function, the Koenig lookup is
19463 not appropriate, even if an unqualified-name was used
19464 to denote the function. */
19465 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
19466 || identifier_p (function))
19467 /* Only do this when substitution turns a dependent call
19468 into a non-dependent call. */
19469 && type_dependent_expression_p_push (t)
19470 && !any_type_dependent_arguments_p (call_args))
19471 function = perform_koenig_lookup (function, call_args, tf_none);
19472
19473 if (function != NULL_TREE
19474 && identifier_p (function)
19475 && !any_type_dependent_arguments_p (call_args))
19476 {
19477 if (koenig_p && (complain & tf_warning_or_error))
19478 {
19479 /* For backwards compatibility and good diagnostics, try
19480 the unqualified lookup again if we aren't in SFINAE
19481 context. */
19482 tree unq = (tsubst_copy_and_build
19483 (function, args, complain, in_decl, true,
19484 integral_constant_expression_p));
19485 if (unq == error_mark_node)
19486 RETURN (error_mark_node);
19487
19488 if (unq != function)
19489 {
19490 /* In a lambda fn, we have to be careful to not
19491 introduce new this captures. Legacy code can't
19492 be using lambdas anyway, so it's ok to be
19493 stricter. */
19494 bool in_lambda = (current_class_type
19495 && LAMBDA_TYPE_P (current_class_type));
19496 char const *const msg
19497 = G_("%qD was not declared in this scope, "
19498 "and no declarations were found by "
19499 "argument-dependent lookup at the point "
19500 "of instantiation");
19501
19502 bool diag = true;
19503 if (in_lambda)
19504 error_at (cp_expr_loc_or_input_loc (t),
19505 msg, function);
19506 else
19507 diag = permerror (cp_expr_loc_or_input_loc (t),
19508 msg, function);
19509 if (diag)
19510 {
19511 tree fn = unq;
19512
19513 if (INDIRECT_REF_P (fn))
19514 fn = TREE_OPERAND (fn, 0);
19515 if (is_overloaded_fn (fn))
19516 fn = get_first_fn (fn);
19517
19518 if (!DECL_P (fn))
19519 /* Can't say anything more. */;
19520 else if (DECL_CLASS_SCOPE_P (fn))
19521 {
19522 location_t loc = cp_expr_loc_or_input_loc (t);
19523 inform (loc,
19524 "declarations in dependent base %qT are "
19525 "not found by unqualified lookup",
19526 DECL_CLASS_CONTEXT (fn));
19527 if (current_class_ptr)
19528 inform (loc,
19529 "use %<this->%D%> instead", function);
19530 else
19531 inform (loc,
19532 "use %<%T::%D%> instead",
19533 current_class_name, function);
19534 }
19535 else
19536 inform (DECL_SOURCE_LOCATION (fn),
19537 "%qD declared here, later in the "
19538 "translation unit", fn);
19539 if (in_lambda)
19540 RETURN (error_mark_node);
19541 }
19542
19543 function = unq;
19544 }
19545 }
19546 if (identifier_p (function))
19547 {
19548 if (complain & tf_error)
19549 unqualified_name_lookup_error (function);
19550 RETURN (error_mark_node);
19551 }
19552 }
19553
19554 /* Remember that there was a reference to this entity. */
19555 if (function != NULL_TREE
19556 && DECL_P (function)
19557 && !mark_used (function, complain) && !(complain & tf_error))
19558 RETURN (error_mark_node);
19559
19560 /* Put back tf_decltype for the actual call. */
19561 complain |= decltype_flag;
19562
19563 if (function == NULL_TREE)
19564 switch (CALL_EXPR_IFN (t))
19565 {
19566 case IFN_LAUNDER:
19567 gcc_assert (nargs == 1);
19568 if (vec_safe_length (call_args) != 1)
19569 {
19570 error_at (cp_expr_loc_or_input_loc (t),
19571 "wrong number of arguments to "
19572 "%<__builtin_launder%>");
19573 ret = error_mark_node;
19574 }
19575 else
19576 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19577 (*call_args)[0], complain);
19578 break;
19579
19580 case IFN_VEC_CONVERT:
19581 gcc_assert (nargs == 1);
19582 if (vec_safe_length (call_args) != 1)
19583 {
19584 error_at (cp_expr_loc_or_input_loc (t),
19585 "wrong number of arguments to "
19586 "%<__builtin_convertvector%>");
19587 ret = error_mark_node;
19588 break;
19589 }
19590 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19591 tsubst (TREE_TYPE (t), args,
19592 complain, in_decl),
19593 complain);
19594 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19595 RETURN (ret);
19596 break;
19597
19598 default:
19599 /* Unsupported internal function with arguments. */
19600 gcc_unreachable ();
19601 }
19602 else if (TREE_CODE (function) == OFFSET_REF
19603 || TREE_CODE (function) == DOTSTAR_EXPR
19604 || TREE_CODE (function) == MEMBER_REF)
19605 ret = build_offset_ref_call_from_tree (function, &call_args,
19606 complain);
19607 else if (TREE_CODE (function) == COMPONENT_REF)
19608 {
19609 tree instance = TREE_OPERAND (function, 0);
19610 tree fn = TREE_OPERAND (function, 1);
19611
19612 if (processing_template_decl
19613 && (type_dependent_expression_p (instance)
19614 || (!BASELINK_P (fn)
19615 && TREE_CODE (fn) != FIELD_DECL)
19616 || type_dependent_expression_p (fn)
19617 || any_type_dependent_arguments_p (call_args)))
19618 ret = build_min_nt_call_vec (function, call_args);
19619 else if (!BASELINK_P (fn))
19620 ret = finish_call_expr (function, &call_args,
19621 /*disallow_virtual=*/false,
19622 /*koenig_p=*/false,
19623 complain);
19624 else
19625 ret = (build_new_method_call
19626 (instance, fn,
19627 &call_args, NULL_TREE,
19628 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19629 /*fn_p=*/NULL,
19630 complain));
19631 }
19632 else if (concept_check_p (function))
19633 {
19634 /* FUNCTION is a template-id referring to a concept definition. */
19635 tree id = unpack_concept_check (function);
19636 tree tmpl = TREE_OPERAND (id, 0);
19637 tree args = TREE_OPERAND (id, 1);
19638
19639 /* Calls to standard and variable concepts should have been
19640 previously diagnosed. */
19641 gcc_assert (function_concept_p (tmpl));
19642
19643 /* Ensure the result is wrapped as a call expression. */
19644 ret = build_concept_check (tmpl, args, tf_warning_or_error);
19645
19646 /* Possibly evaluate the check if it is non-dependent. */
19647 if (!uses_template_parms (args)
19648 && !processing_constraint_expression_p ())
19649 ret = evaluate_concept_check (ret, complain);
19650 }
19651 else
19652 ret = finish_call_expr (function, &call_args,
19653 /*disallow_virtual=*/qualified_p,
19654 koenig_p,
19655 complain);
19656
19657 if (ret != error_mark_node)
19658 {
19659 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19660 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19661 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19662 if (op || ord || rev)
19663 {
19664 function = extract_call_expr (ret);
19665 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19666 CALL_EXPR_ORDERED_ARGS (function) = ord;
19667 CALL_EXPR_REVERSE_ARGS (function) = rev;
19668 }
19669 }
19670
19671 RETURN (ret);
19672 }
19673
19674 case COND_EXPR:
19675 {
19676 tree cond = RECUR (TREE_OPERAND (t, 0));
19677 cond = mark_rvalue_use (cond);
19678 tree folded_cond = fold_non_dependent_expr (cond, complain);
19679 tree exp1, exp2;
19680
19681 if (TREE_CODE (folded_cond) == INTEGER_CST)
19682 {
19683 if (integer_zerop (folded_cond))
19684 {
19685 ++c_inhibit_evaluation_warnings;
19686 exp1 = RECUR (TREE_OPERAND (t, 1));
19687 --c_inhibit_evaluation_warnings;
19688 exp2 = RECUR (TREE_OPERAND (t, 2));
19689 }
19690 else
19691 {
19692 exp1 = RECUR (TREE_OPERAND (t, 1));
19693 ++c_inhibit_evaluation_warnings;
19694 exp2 = RECUR (TREE_OPERAND (t, 2));
19695 --c_inhibit_evaluation_warnings;
19696 }
19697 cond = folded_cond;
19698 }
19699 else
19700 {
19701 exp1 = RECUR (TREE_OPERAND (t, 1));
19702 exp2 = RECUR (TREE_OPERAND (t, 2));
19703 }
19704
19705 warning_sentinel s(warn_duplicated_branches);
19706 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19707 cond, exp1, exp2, complain));
19708 }
19709
19710 case PSEUDO_DTOR_EXPR:
19711 {
19712 tree op0 = RECUR (TREE_OPERAND (t, 0));
19713 tree op1 = RECUR (TREE_OPERAND (t, 1));
19714 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19715 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19716 input_location));
19717 }
19718
19719 case TREE_LIST:
19720 {
19721 tree purpose, value, chain;
19722
19723 if (t == void_list_node)
19724 RETURN (t);
19725
19726 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19727 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19728 {
19729 /* We have pack expansions, so expand those and
19730 create a new list out of it. */
19731 tree purposevec = NULL_TREE;
19732 tree valuevec = NULL_TREE;
19733 tree chain;
19734 int i, len = -1;
19735
19736 /* Expand the argument expressions. */
19737 if (TREE_PURPOSE (t))
19738 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19739 complain, in_decl);
19740 if (TREE_VALUE (t))
19741 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19742 complain, in_decl);
19743
19744 /* Build the rest of the list. */
19745 chain = TREE_CHAIN (t);
19746 if (chain && chain != void_type_node)
19747 chain = RECUR (chain);
19748
19749 /* Determine the number of arguments. */
19750 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19751 {
19752 len = TREE_VEC_LENGTH (purposevec);
19753 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19754 }
19755 else if (TREE_CODE (valuevec) == TREE_VEC)
19756 len = TREE_VEC_LENGTH (valuevec);
19757 else
19758 {
19759 /* Since we only performed a partial substitution into
19760 the argument pack, we only RETURN (a single list
19761 node. */
19762 if (purposevec == TREE_PURPOSE (t)
19763 && valuevec == TREE_VALUE (t)
19764 && chain == TREE_CHAIN (t))
19765 RETURN (t);
19766
19767 RETURN (tree_cons (purposevec, valuevec, chain));
19768 }
19769
19770 /* Convert the argument vectors into a TREE_LIST */
19771 i = len;
19772 while (i > 0)
19773 {
19774 /* Grab the Ith values. */
19775 i--;
19776 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19777 : NULL_TREE;
19778 value
19779 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19780 : NULL_TREE;
19781
19782 /* Build the list (backwards). */
19783 chain = tree_cons (purpose, value, chain);
19784 }
19785
19786 RETURN (chain);
19787 }
19788
19789 purpose = TREE_PURPOSE (t);
19790 if (purpose)
19791 purpose = RECUR (purpose);
19792 value = TREE_VALUE (t);
19793 if (value)
19794 value = RECUR (value);
19795 chain = TREE_CHAIN (t);
19796 if (chain && chain != void_type_node)
19797 chain = RECUR (chain);
19798 if (purpose == TREE_PURPOSE (t)
19799 && value == TREE_VALUE (t)
19800 && chain == TREE_CHAIN (t))
19801 RETURN (t);
19802 RETURN (tree_cons (purpose, value, chain));
19803 }
19804
19805 case COMPONENT_REF:
19806 {
19807 tree object;
19808 tree object_type;
19809 tree member;
19810 tree r;
19811
19812 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19813 args, complain, in_decl);
19814 /* Remember that there was a reference to this entity. */
19815 if (DECL_P (object)
19816 && !mark_used (object, complain) && !(complain & tf_error))
19817 RETURN (error_mark_node);
19818 object_type = TREE_TYPE (object);
19819
19820 member = TREE_OPERAND (t, 1);
19821 if (BASELINK_P (member))
19822 member = tsubst_baselink (member,
19823 non_reference (TREE_TYPE (object)),
19824 args, complain, in_decl);
19825 else
19826 member = tsubst_copy (member, args, complain, in_decl);
19827 if (member == error_mark_node)
19828 RETURN (error_mark_node);
19829
19830 if (TREE_CODE (member) == FIELD_DECL)
19831 {
19832 r = finish_non_static_data_member (member, object, NULL_TREE);
19833 if (TREE_CODE (r) == COMPONENT_REF)
19834 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19835 RETURN (r);
19836 }
19837 else if (type_dependent_expression_p (object))
19838 /* We can't do much here. */;
19839 else if (!CLASS_TYPE_P (object_type))
19840 {
19841 if (scalarish_type_p (object_type))
19842 {
19843 tree s = NULL_TREE;
19844 tree dtor = member;
19845
19846 if (TREE_CODE (dtor) == SCOPE_REF)
19847 {
19848 s = TREE_OPERAND (dtor, 0);
19849 dtor = TREE_OPERAND (dtor, 1);
19850 }
19851 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19852 {
19853 dtor = TREE_OPERAND (dtor, 0);
19854 if (TYPE_P (dtor))
19855 RETURN (finish_pseudo_destructor_expr
19856 (object, s, dtor, input_location));
19857 }
19858 }
19859 }
19860 else if (TREE_CODE (member) == SCOPE_REF
19861 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19862 {
19863 /* Lookup the template functions now that we know what the
19864 scope is. */
19865 tree scope = TREE_OPERAND (member, 0);
19866 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19867 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19868 member = lookup_qualified_name (scope, tmpl,
19869 /*is_type_p=*/false,
19870 /*complain=*/false);
19871 if (BASELINK_P (member))
19872 {
19873 BASELINK_FUNCTIONS (member)
19874 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19875 args);
19876 member = (adjust_result_of_qualified_name_lookup
19877 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19878 object_type));
19879 }
19880 else
19881 {
19882 qualified_name_lookup_error (scope, tmpl, member,
19883 input_location);
19884 RETURN (error_mark_node);
19885 }
19886 }
19887 else if (TREE_CODE (member) == SCOPE_REF
19888 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19889 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19890 {
19891 if (complain & tf_error)
19892 {
19893 if (TYPE_P (TREE_OPERAND (member, 0)))
19894 error ("%qT is not a class or namespace",
19895 TREE_OPERAND (member, 0));
19896 else
19897 error ("%qD is not a class or namespace",
19898 TREE_OPERAND (member, 0));
19899 }
19900 RETURN (error_mark_node);
19901 }
19902
19903 r = finish_class_member_access_expr (object, member,
19904 /*template_p=*/false,
19905 complain);
19906 if (TREE_CODE (r) == COMPONENT_REF)
19907 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19908 RETURN (r);
19909 }
19910
19911 case THROW_EXPR:
19912 RETURN (build_throw
19913 (RECUR (TREE_OPERAND (t, 0))));
19914
19915 case CONSTRUCTOR:
19916 {
19917 vec<constructor_elt, va_gc> *n;
19918 constructor_elt *ce;
19919 unsigned HOST_WIDE_INT idx;
19920 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19921 bool process_index_p;
19922 int newlen;
19923 bool need_copy_p = false;
19924 tree r;
19925
19926 if (type == error_mark_node)
19927 RETURN (error_mark_node);
19928
19929 /* We do not want to process the index of aggregate
19930 initializers as they are identifier nodes which will be
19931 looked up by digest_init. */
19932 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19933
19934 if (null_member_pointer_value_p (t))
19935 {
19936 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19937 RETURN (t);
19938 }
19939
19940 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19941 newlen = vec_safe_length (n);
19942 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19943 {
19944 if (ce->index && process_index_p
19945 /* An identifier index is looked up in the type
19946 being initialized, not the current scope. */
19947 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19948 ce->index = RECUR (ce->index);
19949
19950 if (PACK_EXPANSION_P (ce->value))
19951 {
19952 /* Substitute into the pack expansion. */
19953 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19954 in_decl);
19955
19956 if (ce->value == error_mark_node
19957 || PACK_EXPANSION_P (ce->value))
19958 ;
19959 else if (TREE_VEC_LENGTH (ce->value) == 1)
19960 /* Just move the argument into place. */
19961 ce->value = TREE_VEC_ELT (ce->value, 0);
19962 else
19963 {
19964 /* Update the length of the final CONSTRUCTOR
19965 arguments vector, and note that we will need to
19966 copy.*/
19967 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19968 need_copy_p = true;
19969 }
19970 }
19971 else
19972 ce->value = RECUR (ce->value);
19973 }
19974
19975 if (need_copy_p)
19976 {
19977 vec<constructor_elt, va_gc> *old_n = n;
19978
19979 vec_alloc (n, newlen);
19980 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19981 {
19982 if (TREE_CODE (ce->value) == TREE_VEC)
19983 {
19984 int i, len = TREE_VEC_LENGTH (ce->value);
19985 for (i = 0; i < len; ++i)
19986 CONSTRUCTOR_APPEND_ELT (n, 0,
19987 TREE_VEC_ELT (ce->value, i));
19988 }
19989 else
19990 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19991 }
19992 }
19993
19994 r = build_constructor (init_list_type_node, n);
19995 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19996 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19997 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19998
19999 if (TREE_HAS_CONSTRUCTOR (t))
20000 {
20001 fcl_t cl = fcl_functional;
20002 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20003 cl = fcl_c99;
20004 RETURN (finish_compound_literal (type, r, complain, cl));
20005 }
20006
20007 TREE_TYPE (r) = type;
20008 RETURN (r);
20009 }
20010
20011 case TYPEID_EXPR:
20012 {
20013 tree operand_0 = TREE_OPERAND (t, 0);
20014 if (TYPE_P (operand_0))
20015 {
20016 operand_0 = tsubst (operand_0, args, complain, in_decl);
20017 RETURN (get_typeid (operand_0, complain));
20018 }
20019 else
20020 {
20021 operand_0 = RECUR (operand_0);
20022 RETURN (build_typeid (operand_0, complain));
20023 }
20024 }
20025
20026 case VAR_DECL:
20027 if (!args)
20028 RETURN (t);
20029 /* Fall through */
20030
20031 case PARM_DECL:
20032 {
20033 tree r = tsubst_copy (t, args, complain, in_decl);
20034 /* ??? We're doing a subset of finish_id_expression here. */
20035 if (tree wrap = maybe_get_tls_wrapper_call (r))
20036 /* Replace an evaluated use of the thread_local variable with
20037 a call to its wrapper. */
20038 r = wrap;
20039 else if (outer_automatic_var_p (r))
20040 r = process_outer_var_ref (r, complain);
20041
20042 if (!TYPE_REF_P (TREE_TYPE (t)))
20043 /* If the original type was a reference, we'll be wrapped in
20044 the appropriate INDIRECT_REF. */
20045 r = convert_from_reference (r);
20046 RETURN (r);
20047 }
20048
20049 case VA_ARG_EXPR:
20050 {
20051 tree op0 = RECUR (TREE_OPERAND (t, 0));
20052 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20053 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20054 }
20055
20056 case OFFSETOF_EXPR:
20057 {
20058 tree object_ptr
20059 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20060 in_decl, /*function_p=*/false,
20061 /*integral_constant_expression_p=*/false);
20062 RETURN (finish_offsetof (object_ptr,
20063 RECUR (TREE_OPERAND (t, 0)),
20064 EXPR_LOCATION (t)));
20065 }
20066
20067 case ADDRESSOF_EXPR:
20068 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20069 RECUR (TREE_OPERAND (t, 0)), complain));
20070
20071 case TRAIT_EXPR:
20072 {
20073 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20074 complain, in_decl);
20075
20076 tree type2 = TRAIT_EXPR_TYPE2 (t);
20077 if (type2 && TREE_CODE (type2) == TREE_LIST)
20078 type2 = RECUR (type2);
20079 else if (type2)
20080 type2 = tsubst (type2, args, complain, in_decl);
20081
20082 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20083 TRAIT_EXPR_KIND (t), type1, type2));
20084 }
20085
20086 case STMT_EXPR:
20087 {
20088 tree old_stmt_expr = cur_stmt_expr;
20089 tree stmt_expr = begin_stmt_expr ();
20090
20091 cur_stmt_expr = stmt_expr;
20092 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20093 integral_constant_expression_p);
20094 stmt_expr = finish_stmt_expr (stmt_expr, false);
20095 cur_stmt_expr = old_stmt_expr;
20096
20097 /* If the resulting list of expression statement is empty,
20098 fold it further into void_node. */
20099 if (empty_expr_stmt_p (stmt_expr))
20100 stmt_expr = void_node;
20101
20102 RETURN (stmt_expr);
20103 }
20104
20105 case LAMBDA_EXPR:
20106 {
20107 if (complain & tf_partial)
20108 {
20109 /* We don't have a full set of template arguments yet; don't touch
20110 the lambda at all. */
20111 gcc_assert (processing_template_decl);
20112 return t;
20113 }
20114 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20115
20116 RETURN (build_lambda_object (r));
20117 }
20118
20119 case TARGET_EXPR:
20120 /* We can get here for a constant initializer of non-dependent type.
20121 FIXME stop folding in cp_parser_initializer_clause. */
20122 {
20123 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20124 complain);
20125 RETURN (r);
20126 }
20127
20128 case TRANSACTION_EXPR:
20129 RETURN (tsubst_expr(t, args, complain, in_decl,
20130 integral_constant_expression_p));
20131
20132 case PAREN_EXPR:
20133 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20134
20135 case VEC_PERM_EXPR:
20136 {
20137 tree op0 = RECUR (TREE_OPERAND (t, 0));
20138 tree op1 = RECUR (TREE_OPERAND (t, 1));
20139 tree op2 = RECUR (TREE_OPERAND (t, 2));
20140 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20141 complain));
20142 }
20143
20144 case REQUIRES_EXPR:
20145 {
20146 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20147 if (r == error_mark_node && (complain & tf_error))
20148 tsubst_requires_expr (t, args, complain, in_decl);
20149 RETURN (r);
20150 }
20151
20152 case RANGE_EXPR:
20153 /* No need to substitute further, a RANGE_EXPR will always be built
20154 with constant operands. */
20155 RETURN (t);
20156
20157 case NON_LVALUE_EXPR:
20158 case VIEW_CONVERT_EXPR:
20159 if (location_wrapper_p (t))
20160 /* We need to do this here as well as in tsubst_copy so we get the
20161 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20162 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20163 EXPR_LOCATION (t)));
20164 /* fallthrough. */
20165
20166 default:
20167 /* Handle Objective-C++ constructs, if appropriate. */
20168 {
20169 tree subst
20170 = objcp_tsubst_copy_and_build (t, args, complain,
20171 in_decl, /*function_p=*/false);
20172 if (subst)
20173 RETURN (subst);
20174 }
20175 RETURN (tsubst_copy (t, args, complain, in_decl));
20176 }
20177
20178 #undef RECUR
20179 #undef RETURN
20180 out:
20181 input_location = loc;
20182 return retval;
20183 }
20184
20185 /* Verify that the instantiated ARGS are valid. For type arguments,
20186 make sure that the type's linkage is ok. For non-type arguments,
20187 make sure they are constants if they are integral or enumerations.
20188 Emit an error under control of COMPLAIN, and return TRUE on error. */
20189
20190 static bool
20191 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20192 {
20193 if (dependent_template_arg_p (t))
20194 return false;
20195 if (ARGUMENT_PACK_P (t))
20196 {
20197 tree vec = ARGUMENT_PACK_ARGS (t);
20198 int len = TREE_VEC_LENGTH (vec);
20199 bool result = false;
20200 int i;
20201
20202 for (i = 0; i < len; ++i)
20203 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20204 result = true;
20205 return result;
20206 }
20207 else if (TYPE_P (t))
20208 {
20209 /* [basic.link]: A name with no linkage (notably, the name
20210 of a class or enumeration declared in a local scope)
20211 shall not be used to declare an entity with linkage.
20212 This implies that names with no linkage cannot be used as
20213 template arguments
20214
20215 DR 757 relaxes this restriction for C++0x. */
20216 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20217 : no_linkage_check (t, /*relaxed_p=*/false));
20218
20219 if (nt)
20220 {
20221 /* DR 488 makes use of a type with no linkage cause
20222 type deduction to fail. */
20223 if (complain & tf_error)
20224 {
20225 if (TYPE_UNNAMED_P (nt))
20226 error ("%qT is/uses unnamed type", t);
20227 else
20228 error ("template argument for %qD uses local type %qT",
20229 tmpl, t);
20230 }
20231 return true;
20232 }
20233 /* In order to avoid all sorts of complications, we do not
20234 allow variably-modified types as template arguments. */
20235 else if (variably_modified_type_p (t, NULL_TREE))
20236 {
20237 if (complain & tf_error)
20238 error ("%qT is a variably modified type", t);
20239 return true;
20240 }
20241 }
20242 /* Class template and alias template arguments should be OK. */
20243 else if (DECL_TYPE_TEMPLATE_P (t))
20244 ;
20245 /* A non-type argument of integral or enumerated type must be a
20246 constant. */
20247 else if (TREE_TYPE (t)
20248 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20249 && !REFERENCE_REF_P (t)
20250 && !TREE_CONSTANT (t))
20251 {
20252 if (complain & tf_error)
20253 error ("integral expression %qE is not constant", t);
20254 return true;
20255 }
20256 return false;
20257 }
20258
20259 static bool
20260 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20261 {
20262 int ix, len = DECL_NTPARMS (tmpl);
20263 bool result = false;
20264
20265 for (ix = 0; ix != len; ix++)
20266 {
20267 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20268 result = true;
20269 }
20270 if (result && (complain & tf_error))
20271 error (" trying to instantiate %qD", tmpl);
20272 return result;
20273 }
20274
20275 /* We're out of SFINAE context now, so generate diagnostics for the access
20276 errors we saw earlier when instantiating D from TMPL and ARGS. */
20277
20278 static void
20279 recheck_decl_substitution (tree d, tree tmpl, tree args)
20280 {
20281 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
20282 tree type = TREE_TYPE (pattern);
20283 location_t loc = input_location;
20284
20285 push_access_scope (d);
20286 push_deferring_access_checks (dk_no_deferred);
20287 input_location = DECL_SOURCE_LOCATION (pattern);
20288 tsubst (type, args, tf_warning_or_error, d);
20289 input_location = loc;
20290 pop_deferring_access_checks ();
20291 pop_access_scope (d);
20292 }
20293
20294 /* Instantiate the indicated variable, function, or alias template TMPL with
20295 the template arguments in TARG_PTR. */
20296
20297 static tree
20298 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
20299 {
20300 tree targ_ptr = orig_args;
20301 tree fndecl;
20302 tree gen_tmpl;
20303 tree spec;
20304 bool access_ok = true;
20305
20306 if (tmpl == error_mark_node)
20307 return error_mark_node;
20308
20309 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
20310
20311 /* If this function is a clone, handle it specially. */
20312 if (DECL_CLONED_FUNCTION_P (tmpl))
20313 {
20314 tree spec;
20315 tree clone;
20316
20317 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
20318 DECL_CLONED_FUNCTION. */
20319 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
20320 targ_ptr, complain);
20321 if (spec == error_mark_node)
20322 return error_mark_node;
20323
20324 /* Look for the clone. */
20325 FOR_EACH_CLONE (clone, spec)
20326 if (DECL_NAME (clone) == DECL_NAME (tmpl))
20327 return clone;
20328 /* We should always have found the clone by now. */
20329 gcc_unreachable ();
20330 return NULL_TREE;
20331 }
20332
20333 if (targ_ptr == error_mark_node)
20334 return error_mark_node;
20335
20336 /* Check to see if we already have this specialization. */
20337 gen_tmpl = most_general_template (tmpl);
20338 if (TMPL_ARGS_DEPTH (targ_ptr)
20339 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
20340 /* targ_ptr only has the innermost template args, so add the outer ones
20341 from tmpl, which could be either a partial instantiation or gen_tmpl (in
20342 the case of a non-dependent call within a template definition). */
20343 targ_ptr = (add_outermost_template_args
20344 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
20345 targ_ptr));
20346
20347 /* It would be nice to avoid hashing here and then again in tsubst_decl,
20348 but it doesn't seem to be on the hot path. */
20349 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
20350
20351 gcc_assert (tmpl == gen_tmpl
20352 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
20353 == spec)
20354 || fndecl == NULL_TREE);
20355
20356 if (spec != NULL_TREE)
20357 {
20358 if (FNDECL_HAS_ACCESS_ERRORS (spec))
20359 {
20360 if (complain & tf_error)
20361 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
20362 return error_mark_node;
20363 }
20364 return spec;
20365 }
20366
20367 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
20368 complain))
20369 return error_mark_node;
20370
20371 /* We are building a FUNCTION_DECL, during which the access of its
20372 parameters and return types have to be checked. However this
20373 FUNCTION_DECL which is the desired context for access checking
20374 is not built yet. We solve this chicken-and-egg problem by
20375 deferring all checks until we have the FUNCTION_DECL. */
20376 push_deferring_access_checks (dk_deferred);
20377
20378 /* Instantiation of the function happens in the context of the function
20379 template, not the context of the overload resolution we're doing. */
20380 push_to_top_level ();
20381 /* If there are dependent arguments, e.g. because we're doing partial
20382 ordering, make sure processing_template_decl stays set. */
20383 if (uses_template_parms (targ_ptr))
20384 ++processing_template_decl;
20385 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20386 {
20387 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
20388 complain, gen_tmpl, true);
20389 push_nested_class (ctx);
20390 }
20391
20392 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
20393
20394 fndecl = NULL_TREE;
20395 if (VAR_P (pattern))
20396 {
20397 /* We need to determine if we're using a partial or explicit
20398 specialization now, because the type of the variable could be
20399 different. */
20400 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
20401 tree elt = most_specialized_partial_spec (tid, complain);
20402 if (elt == error_mark_node)
20403 pattern = error_mark_node;
20404 else if (elt)
20405 {
20406 tree partial_tmpl = TREE_VALUE (elt);
20407 tree partial_args = TREE_PURPOSE (elt);
20408 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
20409 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
20410 }
20411 }
20412
20413 /* Substitute template parameters to obtain the specialization. */
20414 if (fndecl == NULL_TREE)
20415 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
20416 if (DECL_CLASS_SCOPE_P (gen_tmpl))
20417 pop_nested_class ();
20418 pop_from_top_level ();
20419
20420 if (fndecl == error_mark_node)
20421 {
20422 pop_deferring_access_checks ();
20423 return error_mark_node;
20424 }
20425
20426 /* The DECL_TI_TEMPLATE should always be the immediate parent
20427 template, not the most general template. */
20428 DECL_TI_TEMPLATE (fndecl) = tmpl;
20429 DECL_TI_ARGS (fndecl) = targ_ptr;
20430
20431 /* Now we know the specialization, compute access previously
20432 deferred. Do no access control for inheriting constructors,
20433 as we already checked access for the inherited constructor. */
20434 if (!(flag_new_inheriting_ctors
20435 && DECL_INHERITED_CTOR (fndecl)))
20436 {
20437 push_access_scope (fndecl);
20438 if (!perform_deferred_access_checks (complain))
20439 access_ok = false;
20440 pop_access_scope (fndecl);
20441 }
20442 pop_deferring_access_checks ();
20443
20444 /* If we've just instantiated the main entry point for a function,
20445 instantiate all the alternate entry points as well. We do this
20446 by cloning the instantiation of the main entry point, not by
20447 instantiating the template clones. */
20448 if (tree chain = DECL_CHAIN (gen_tmpl))
20449 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
20450 clone_function_decl (fndecl, /*update_methods=*/false);
20451
20452 if (!access_ok)
20453 {
20454 if (!(complain & tf_error))
20455 {
20456 /* Remember to reinstantiate when we're out of SFINAE so the user
20457 can see the errors. */
20458 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
20459 }
20460 return error_mark_node;
20461 }
20462 return fndecl;
20463 }
20464
20465 /* Wrapper for instantiate_template_1. */
20466
20467 tree
20468 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
20469 {
20470 tree ret;
20471 timevar_push (TV_TEMPLATE_INST);
20472 ret = instantiate_template_1 (tmpl, orig_args, complain);
20473 timevar_pop (TV_TEMPLATE_INST);
20474 return ret;
20475 }
20476
20477 /* Instantiate the alias template TMPL with ARGS. Also push a template
20478 instantiation level, which instantiate_template doesn't do because
20479 functions and variables have sufficient context established by the
20480 callers. */
20481
20482 static tree
20483 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
20484 {
20485 if (tmpl == error_mark_node || args == error_mark_node)
20486 return error_mark_node;
20487 if (!push_tinst_level (tmpl, args))
20488 return error_mark_node;
20489
20490 args =
20491 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
20492 args, tmpl, complain,
20493 /*require_all_args=*/true,
20494 /*use_default_args=*/true);
20495
20496 tree r = instantiate_template (tmpl, args, complain);
20497 pop_tinst_level ();
20498
20499 return r;
20500 }
20501
20502 /* PARM is a template parameter pack for FN. Returns true iff
20503 PARM is used in a deducible way in the argument list of FN. */
20504
20505 static bool
20506 pack_deducible_p (tree parm, tree fn)
20507 {
20508 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
20509 for (; t; t = TREE_CHAIN (t))
20510 {
20511 tree type = TREE_VALUE (t);
20512 tree packs;
20513 if (!PACK_EXPANSION_P (type))
20514 continue;
20515 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
20516 packs; packs = TREE_CHAIN (packs))
20517 if (template_args_equal (TREE_VALUE (packs), parm))
20518 {
20519 /* The template parameter pack is used in a function parameter
20520 pack. If this is the end of the parameter list, the
20521 template parameter pack is deducible. */
20522 if (TREE_CHAIN (t) == void_list_node)
20523 return true;
20524 else
20525 /* Otherwise, not. Well, it could be deduced from
20526 a non-pack parameter, but doing so would end up with
20527 a deduction mismatch, so don't bother. */
20528 return false;
20529 }
20530 }
20531 /* The template parameter pack isn't used in any function parameter
20532 packs, but it might be used deeper, e.g. tuple<Args...>. */
20533 return true;
20534 }
20535
20536 /* Subroutine of fn_type_unification: check non-dependent parms for
20537 convertibility. */
20538
20539 static int
20540 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20541 tree fn, unification_kind_t strict, int flags,
20542 struct conversion **convs, bool explain_p)
20543 {
20544 /* Non-constructor methods need to leave a conversion for 'this', which
20545 isn't included in nargs here. */
20546 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20547 && !DECL_CONSTRUCTOR_P (fn));
20548
20549 for (unsigned ia = 0;
20550 parms && parms != void_list_node && ia < nargs; )
20551 {
20552 tree parm = TREE_VALUE (parms);
20553
20554 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20555 && (!TREE_CHAIN (parms)
20556 || TREE_CHAIN (parms) == void_list_node))
20557 /* For a function parameter pack that occurs at the end of the
20558 parameter-declaration-list, the type A of each remaining
20559 argument of the call is compared with the type P of the
20560 declarator-id of the function parameter pack. */
20561 break;
20562
20563 parms = TREE_CHAIN (parms);
20564
20565 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20566 /* For a function parameter pack that does not occur at the
20567 end of the parameter-declaration-list, the type of the
20568 parameter pack is a non-deduced context. */
20569 continue;
20570
20571 if (!uses_template_parms (parm))
20572 {
20573 tree arg = args[ia];
20574 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20575 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20576
20577 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20578 conv_p, explain_p))
20579 return 1;
20580 }
20581
20582 ++ia;
20583 }
20584
20585 return 0;
20586 }
20587
20588 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20589 NARGS elements of the arguments that are being used when calling
20590 it. TARGS is a vector into which the deduced template arguments
20591 are placed.
20592
20593 Returns either a FUNCTION_DECL for the matching specialization of FN or
20594 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20595 true, diagnostics will be printed to explain why it failed.
20596
20597 If FN is a conversion operator, or we are trying to produce a specific
20598 specialization, RETURN_TYPE is the return type desired.
20599
20600 The EXPLICIT_TARGS are explicit template arguments provided via a
20601 template-id.
20602
20603 The parameter STRICT is one of:
20604
20605 DEDUCE_CALL:
20606 We are deducing arguments for a function call, as in
20607 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20608 deducing arguments for a call to the result of a conversion
20609 function template, as in [over.call.object].
20610
20611 DEDUCE_CONV:
20612 We are deducing arguments for a conversion function, as in
20613 [temp.deduct.conv].
20614
20615 DEDUCE_EXACT:
20616 We are deducing arguments when doing an explicit instantiation
20617 as in [temp.explicit], when determining an explicit specialization
20618 as in [temp.expl.spec], or when taking the address of a function
20619 template, as in [temp.deduct.funcaddr]. */
20620
20621 tree
20622 fn_type_unification (tree fn,
20623 tree explicit_targs,
20624 tree targs,
20625 const tree *args,
20626 unsigned int nargs,
20627 tree return_type,
20628 unification_kind_t strict,
20629 int flags,
20630 struct conversion **convs,
20631 bool explain_p,
20632 bool decltype_p)
20633 {
20634 tree parms;
20635 tree fntype;
20636 tree decl = NULL_TREE;
20637 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20638 bool ok;
20639 static int deduction_depth;
20640 /* type_unification_real will pass back any access checks from default
20641 template argument substitution. */
20642 vec<deferred_access_check, va_gc> *checks = NULL;
20643 /* We don't have all the template args yet. */
20644 bool incomplete = true;
20645
20646 tree orig_fn = fn;
20647 if (flag_new_inheriting_ctors)
20648 fn = strip_inheriting_ctors (fn);
20649
20650 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20651 tree r = error_mark_node;
20652
20653 tree full_targs = targs;
20654 if (TMPL_ARGS_DEPTH (targs)
20655 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20656 full_targs = (add_outermost_template_args
20657 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20658 targs));
20659
20660 if (decltype_p)
20661 complain |= tf_decltype;
20662
20663 /* In C++0x, it's possible to have a function template whose type depends
20664 on itself recursively. This is most obvious with decltype, but can also
20665 occur with enumeration scope (c++/48969). So we need to catch infinite
20666 recursion and reject the substitution at deduction time; this function
20667 will return error_mark_node for any repeated substitution.
20668
20669 This also catches excessive recursion such as when f<N> depends on
20670 f<N-1> across all integers, and returns error_mark_node for all the
20671 substitutions back up to the initial one.
20672
20673 This is, of course, not reentrant. */
20674 if (excessive_deduction_depth)
20675 return error_mark_node;
20676 ++deduction_depth;
20677
20678 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20679
20680 fntype = TREE_TYPE (fn);
20681 if (explicit_targs)
20682 {
20683 /* [temp.deduct]
20684
20685 The specified template arguments must match the template
20686 parameters in kind (i.e., type, nontype, template), and there
20687 must not be more arguments than there are parameters;
20688 otherwise type deduction fails.
20689
20690 Nontype arguments must match the types of the corresponding
20691 nontype template parameters, or must be convertible to the
20692 types of the corresponding nontype parameters as specified in
20693 _temp.arg.nontype_, otherwise type deduction fails.
20694
20695 All references in the function type of the function template
20696 to the corresponding template parameters are replaced by the
20697 specified template argument values. If a substitution in a
20698 template parameter or in the function type of the function
20699 template results in an invalid type, type deduction fails. */
20700 int i, len = TREE_VEC_LENGTH (tparms);
20701 location_t loc = input_location;
20702 incomplete = false;
20703
20704 if (explicit_targs == error_mark_node)
20705 goto fail;
20706
20707 if (TMPL_ARGS_DEPTH (explicit_targs)
20708 < TMPL_ARGS_DEPTH (full_targs))
20709 explicit_targs = add_outermost_template_args (full_targs,
20710 explicit_targs);
20711
20712 /* Adjust any explicit template arguments before entering the
20713 substitution context. */
20714 explicit_targs
20715 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20716 complain|tf_partial,
20717 /*require_all_args=*/false,
20718 /*use_default_args=*/false));
20719 if (explicit_targs == error_mark_node)
20720 goto fail;
20721
20722 /* Substitute the explicit args into the function type. This is
20723 necessary so that, for instance, explicitly declared function
20724 arguments can match null pointed constants. If we were given
20725 an incomplete set of explicit args, we must not do semantic
20726 processing during substitution as we could create partial
20727 instantiations. */
20728 for (i = 0; i < len; i++)
20729 {
20730 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20731 bool parameter_pack = false;
20732 tree targ = TREE_VEC_ELT (explicit_targs, i);
20733
20734 /* Dig out the actual parm. */
20735 if (TREE_CODE (parm) == TYPE_DECL
20736 || TREE_CODE (parm) == TEMPLATE_DECL)
20737 {
20738 parm = TREE_TYPE (parm);
20739 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20740 }
20741 else if (TREE_CODE (parm) == PARM_DECL)
20742 {
20743 parm = DECL_INITIAL (parm);
20744 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20745 }
20746
20747 if (targ == NULL_TREE)
20748 /* No explicit argument for this template parameter. */
20749 incomplete = true;
20750 else if (parameter_pack && pack_deducible_p (parm, fn))
20751 {
20752 /* Mark the argument pack as "incomplete". We could
20753 still deduce more arguments during unification.
20754 We remove this mark in type_unification_real. */
20755 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20756 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20757 = ARGUMENT_PACK_ARGS (targ);
20758
20759 /* We have some incomplete argument packs. */
20760 incomplete = true;
20761 }
20762 }
20763
20764 if (incomplete)
20765 {
20766 if (!push_tinst_level (fn, explicit_targs))
20767 {
20768 excessive_deduction_depth = true;
20769 goto fail;
20770 }
20771 ++processing_template_decl;
20772 input_location = DECL_SOURCE_LOCATION (fn);
20773 /* Ignore any access checks; we'll see them again in
20774 instantiate_template and they might have the wrong
20775 access path at this point. */
20776 push_deferring_access_checks (dk_deferred);
20777 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20778 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20779 pop_deferring_access_checks ();
20780 input_location = loc;
20781 --processing_template_decl;
20782 pop_tinst_level ();
20783
20784 if (fntype == error_mark_node)
20785 goto fail;
20786 }
20787
20788 /* Place the explicitly specified arguments in TARGS. */
20789 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20790 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20791 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20792 if (!incomplete && CHECKING_P
20793 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20794 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20795 (targs, NUM_TMPL_ARGS (explicit_targs));
20796 }
20797
20798 if (return_type && strict != DEDUCE_CALL)
20799 {
20800 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20801 new_args[0] = return_type;
20802 memcpy (new_args + 1, args, nargs * sizeof (tree));
20803 args = new_args;
20804 ++nargs;
20805 }
20806
20807 if (!incomplete)
20808 goto deduced;
20809
20810 /* Never do unification on the 'this' parameter. */
20811 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20812
20813 if (return_type && strict == DEDUCE_CALL)
20814 {
20815 /* We're deducing for a call to the result of a template conversion
20816 function. The parms we really want are in return_type. */
20817 if (INDIRECT_TYPE_P (return_type))
20818 return_type = TREE_TYPE (return_type);
20819 parms = TYPE_ARG_TYPES (return_type);
20820 }
20821 else if (return_type)
20822 {
20823 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20824 }
20825
20826 /* We allow incomplete unification without an error message here
20827 because the standard doesn't seem to explicitly prohibit it. Our
20828 callers must be ready to deal with unification failures in any
20829 event. */
20830
20831 /* If we aren't explaining yet, push tinst context so we can see where
20832 any errors (e.g. from class instantiations triggered by instantiation
20833 of default template arguments) come from. If we are explaining, this
20834 context is redundant. */
20835 if (!explain_p && !push_tinst_level (fn, targs))
20836 {
20837 excessive_deduction_depth = true;
20838 goto fail;
20839 }
20840
20841 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20842 full_targs, parms, args, nargs, /*subr=*/0,
20843 strict, &checks, explain_p);
20844 if (!explain_p)
20845 pop_tinst_level ();
20846 if (!ok)
20847 goto fail;
20848
20849 /* Now that we have bindings for all of the template arguments,
20850 ensure that the arguments deduced for the template template
20851 parameters have compatible template parameter lists. We cannot
20852 check this property before we have deduced all template
20853 arguments, because the template parameter types of a template
20854 template parameter might depend on prior template parameters
20855 deduced after the template template parameter. The following
20856 ill-formed example illustrates this issue:
20857
20858 template<typename T, template<T> class C> void f(C<5>, T);
20859
20860 template<int N> struct X {};
20861
20862 void g() {
20863 f(X<5>(), 5l); // error: template argument deduction fails
20864 }
20865
20866 The template parameter list of 'C' depends on the template type
20867 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20868 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20869 time that we deduce 'C'. */
20870 if (!template_template_parm_bindings_ok_p
20871 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20872 {
20873 unify_inconsistent_template_template_parameters (explain_p);
20874 goto fail;
20875 }
20876
20877 /* DR 1391: All parameters have args, now check non-dependent parms for
20878 convertibility. */
20879 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20880 convs, explain_p))
20881 goto fail;
20882
20883 deduced:
20884 /* All is well so far. Now, check:
20885
20886 [temp.deduct]
20887
20888 When all template arguments have been deduced, all uses of
20889 template parameters in nondeduced contexts are replaced with
20890 the corresponding deduced argument values. If the
20891 substitution results in an invalid type, as described above,
20892 type deduction fails. */
20893 if (!push_tinst_level (fn, targs))
20894 {
20895 excessive_deduction_depth = true;
20896 goto fail;
20897 }
20898
20899 /* Also collect access checks from the instantiation. */
20900 reopen_deferring_access_checks (checks);
20901
20902 decl = instantiate_template (fn, targs, complain);
20903
20904 checks = get_deferred_access_checks ();
20905 pop_deferring_access_checks ();
20906
20907 pop_tinst_level ();
20908
20909 if (decl == error_mark_node)
20910 goto fail;
20911
20912 /* Now perform any access checks encountered during substitution. */
20913 push_access_scope (decl);
20914 ok = perform_access_checks (checks, complain);
20915 pop_access_scope (decl);
20916 if (!ok)
20917 goto fail;
20918
20919 /* If we're looking for an exact match, check that what we got
20920 is indeed an exact match. It might not be if some template
20921 parameters are used in non-deduced contexts. But don't check
20922 for an exact match if we have dependent template arguments;
20923 in that case we're doing partial ordering, and we already know
20924 that we have two candidates that will provide the actual type. */
20925 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20926 {
20927 tree substed = TREE_TYPE (decl);
20928 unsigned int i;
20929
20930 tree sarg
20931 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20932 if (return_type)
20933 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20934 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20935 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20936 {
20937 unify_type_mismatch (explain_p, args[i],
20938 TREE_VALUE (sarg));
20939 goto fail;
20940 }
20941 }
20942
20943 /* After doing deduction with the inherited constructor, actually return an
20944 instantiation of the inheriting constructor. */
20945 if (orig_fn != fn)
20946 decl = instantiate_template (orig_fn, targs, complain);
20947
20948 r = decl;
20949
20950 fail:
20951 --deduction_depth;
20952 if (excessive_deduction_depth)
20953 {
20954 if (deduction_depth == 0)
20955 /* Reset once we're all the way out. */
20956 excessive_deduction_depth = false;
20957 }
20958
20959 return r;
20960 }
20961
20962 /* Adjust types before performing type deduction, as described in
20963 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20964 sections are symmetric. PARM is the type of a function parameter
20965 or the return type of the conversion function. ARG is the type of
20966 the argument passed to the call, or the type of the value
20967 initialized with the result of the conversion function.
20968 ARG_EXPR is the original argument expression, which may be null. */
20969
20970 static int
20971 maybe_adjust_types_for_deduction (unification_kind_t strict,
20972 tree* parm,
20973 tree* arg,
20974 tree arg_expr)
20975 {
20976 int result = 0;
20977
20978 switch (strict)
20979 {
20980 case DEDUCE_CALL:
20981 break;
20982
20983 case DEDUCE_CONV:
20984 /* Swap PARM and ARG throughout the remainder of this
20985 function; the handling is precisely symmetric since PARM
20986 will initialize ARG rather than vice versa. */
20987 std::swap (parm, arg);
20988 break;
20989
20990 case DEDUCE_EXACT:
20991 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20992 too, but here handle it by stripping the reference from PARM
20993 rather than by adding it to ARG. */
20994 if (TYPE_REF_P (*parm)
20995 && TYPE_REF_IS_RVALUE (*parm)
20996 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20997 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20998 && TYPE_REF_P (*arg)
20999 && !TYPE_REF_IS_RVALUE (*arg))
21000 *parm = TREE_TYPE (*parm);
21001 /* Nothing else to do in this case. */
21002 return 0;
21003
21004 default:
21005 gcc_unreachable ();
21006 }
21007
21008 if (!TYPE_REF_P (*parm))
21009 {
21010 /* [temp.deduct.call]
21011
21012 If P is not a reference type:
21013
21014 --If A is an array type, the pointer type produced by the
21015 array-to-pointer standard conversion (_conv.array_) is
21016 used in place of A for type deduction; otherwise,
21017
21018 --If A is a function type, the pointer type produced by
21019 the function-to-pointer standard conversion
21020 (_conv.func_) is used in place of A for type deduction;
21021 otherwise,
21022
21023 --If A is a cv-qualified type, the top level
21024 cv-qualifiers of A's type are ignored for type
21025 deduction. */
21026 if (TREE_CODE (*arg) == ARRAY_TYPE)
21027 *arg = build_pointer_type (TREE_TYPE (*arg));
21028 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21029 *arg = build_pointer_type (*arg);
21030 else
21031 *arg = TYPE_MAIN_VARIANT (*arg);
21032 }
21033
21034 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21035 reference to a cv-unqualified template parameter that does not represent a
21036 template parameter of a class template (during class template argument
21037 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21038 an lvalue, the type "lvalue reference to A" is used in place of A for type
21039 deduction. */
21040 if (TYPE_REF_P (*parm)
21041 && TYPE_REF_IS_RVALUE (*parm)
21042 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21043 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21044 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21045 && (arg_expr ? lvalue_p (arg_expr)
21046 /* try_one_overload doesn't provide an arg_expr, but
21047 functions are always lvalues. */
21048 : TREE_CODE (*arg) == FUNCTION_TYPE))
21049 *arg = build_reference_type (*arg);
21050
21051 /* [temp.deduct.call]
21052
21053 If P is a cv-qualified type, the top level cv-qualifiers
21054 of P's type are ignored for type deduction. If P is a
21055 reference type, the type referred to by P is used for
21056 type deduction. */
21057 *parm = TYPE_MAIN_VARIANT (*parm);
21058 if (TYPE_REF_P (*parm))
21059 {
21060 *parm = TREE_TYPE (*parm);
21061 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21062 }
21063
21064 /* DR 322. For conversion deduction, remove a reference type on parm
21065 too (which has been swapped into ARG). */
21066 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21067 *arg = TREE_TYPE (*arg);
21068
21069 return result;
21070 }
21071
21072 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21073 template which doesn't contain any deducible template parameters; check if
21074 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21075 unify_one_argument. */
21076
21077 static int
21078 check_non_deducible_conversion (tree parm, tree arg, int strict,
21079 int flags, struct conversion **conv_p,
21080 bool explain_p)
21081 {
21082 tree type;
21083
21084 if (!TYPE_P (arg))
21085 type = TREE_TYPE (arg);
21086 else
21087 type = arg;
21088
21089 if (same_type_p (parm, type))
21090 return unify_success (explain_p);
21091
21092 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21093 if (strict == DEDUCE_CONV)
21094 {
21095 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21096 return unify_success (explain_p);
21097 }
21098 else if (strict != DEDUCE_EXACT)
21099 {
21100 bool ok = false;
21101 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21102 if (conv_p)
21103 /* Avoid recalculating this in add_function_candidate. */
21104 ok = (*conv_p
21105 = good_conversion (parm, type, conv_arg, flags, complain));
21106 else
21107 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21108 if (ok)
21109 return unify_success (explain_p);
21110 }
21111
21112 if (strict == DEDUCE_EXACT)
21113 return unify_type_mismatch (explain_p, parm, arg);
21114 else
21115 return unify_arg_conversion (explain_p, parm, type, arg);
21116 }
21117
21118 static bool uses_deducible_template_parms (tree type);
21119
21120 /* Returns true iff the expression EXPR is one from which a template
21121 argument can be deduced. In other words, if it's an undecorated
21122 use of a template non-type parameter. */
21123
21124 static bool
21125 deducible_expression (tree expr)
21126 {
21127 /* Strip implicit conversions. */
21128 while (CONVERT_EXPR_P (expr))
21129 expr = TREE_OPERAND (expr, 0);
21130 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21131 }
21132
21133 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21134 deducible way; that is, if it has a max value of <PARM> - 1. */
21135
21136 static bool
21137 deducible_array_bound (tree domain)
21138 {
21139 if (domain == NULL_TREE)
21140 return false;
21141
21142 tree max = TYPE_MAX_VALUE (domain);
21143 if (TREE_CODE (max) != MINUS_EXPR)
21144 return false;
21145
21146 return deducible_expression (TREE_OPERAND (max, 0));
21147 }
21148
21149 /* Returns true iff the template arguments ARGS use a template parameter
21150 in a deducible way. */
21151
21152 static bool
21153 deducible_template_args (tree args)
21154 {
21155 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21156 {
21157 bool deducible;
21158 tree elt = TREE_VEC_ELT (args, i);
21159 if (ARGUMENT_PACK_P (elt))
21160 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21161 else
21162 {
21163 if (PACK_EXPANSION_P (elt))
21164 elt = PACK_EXPANSION_PATTERN (elt);
21165 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21166 deducible = true;
21167 else if (TYPE_P (elt))
21168 deducible = uses_deducible_template_parms (elt);
21169 else
21170 deducible = deducible_expression (elt);
21171 }
21172 if (deducible)
21173 return true;
21174 }
21175 return false;
21176 }
21177
21178 /* Returns true iff TYPE contains any deducible references to template
21179 parameters, as per 14.8.2.5. */
21180
21181 static bool
21182 uses_deducible_template_parms (tree type)
21183 {
21184 if (PACK_EXPANSION_P (type))
21185 type = PACK_EXPANSION_PATTERN (type);
21186
21187 /* T
21188 cv-list T
21189 TT<T>
21190 TT<i>
21191 TT<> */
21192 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21193 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21194 return true;
21195
21196 /* T*
21197 T&
21198 T&& */
21199 if (INDIRECT_TYPE_P (type))
21200 return uses_deducible_template_parms (TREE_TYPE (type));
21201
21202 /* T[integer-constant ]
21203 type [i] */
21204 if (TREE_CODE (type) == ARRAY_TYPE)
21205 return (uses_deducible_template_parms (TREE_TYPE (type))
21206 || deducible_array_bound (TYPE_DOMAIN (type)));
21207
21208 /* T type ::*
21209 type T::*
21210 T T::*
21211 T (type ::*)()
21212 type (T::*)()
21213 type (type ::*)(T)
21214 type (T::*)(T)
21215 T (type ::*)(T)
21216 T (T::*)()
21217 T (T::*)(T) */
21218 if (TYPE_PTRMEM_P (type))
21219 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21220 || (uses_deducible_template_parms
21221 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21222
21223 /* template-name <T> (where template-name refers to a class template)
21224 template-name <i> (where template-name refers to a class template) */
21225 if (CLASS_TYPE_P (type)
21226 && CLASSTYPE_TEMPLATE_INFO (type)
21227 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21228 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21229 (CLASSTYPE_TI_ARGS (type)));
21230
21231 /* type (T)
21232 T()
21233 T(T) */
21234 if (FUNC_OR_METHOD_TYPE_P (type))
21235 {
21236 if (uses_deducible_template_parms (TREE_TYPE (type)))
21237 return true;
21238 tree parm = TYPE_ARG_TYPES (type);
21239 if (TREE_CODE (type) == METHOD_TYPE)
21240 parm = TREE_CHAIN (parm);
21241 for (; parm; parm = TREE_CHAIN (parm))
21242 if (uses_deducible_template_parms (TREE_VALUE (parm)))
21243 return true;
21244 }
21245
21246 return false;
21247 }
21248
21249 /* Subroutine of type_unification_real and unify_pack_expansion to
21250 handle unification of a single P/A pair. Parameters are as
21251 for those functions. */
21252
21253 static int
21254 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
21255 int subr, unification_kind_t strict,
21256 bool explain_p)
21257 {
21258 tree arg_expr = NULL_TREE;
21259 int arg_strict;
21260
21261 if (arg == error_mark_node || parm == error_mark_node)
21262 return unify_invalid (explain_p);
21263 if (arg == unknown_type_node)
21264 /* We can't deduce anything from this, but we might get all the
21265 template args from other function args. */
21266 return unify_success (explain_p);
21267
21268 /* Implicit conversions (Clause 4) will be performed on a function
21269 argument to convert it to the type of the corresponding function
21270 parameter if the parameter type contains no template-parameters that
21271 participate in template argument deduction. */
21272 if (strict != DEDUCE_EXACT
21273 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
21274 /* For function parameters with no deducible template parameters,
21275 just return. We'll check non-dependent conversions later. */
21276 return unify_success (explain_p);
21277
21278 switch (strict)
21279 {
21280 case DEDUCE_CALL:
21281 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
21282 | UNIFY_ALLOW_MORE_CV_QUAL
21283 | UNIFY_ALLOW_DERIVED);
21284 break;
21285
21286 case DEDUCE_CONV:
21287 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
21288 break;
21289
21290 case DEDUCE_EXACT:
21291 arg_strict = UNIFY_ALLOW_NONE;
21292 break;
21293
21294 default:
21295 gcc_unreachable ();
21296 }
21297
21298 /* We only do these transformations if this is the top-level
21299 parameter_type_list in a call or declaration matching; in other
21300 situations (nested function declarators, template argument lists) we
21301 won't be comparing a type to an expression, and we don't do any type
21302 adjustments. */
21303 if (!subr)
21304 {
21305 if (!TYPE_P (arg))
21306 {
21307 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
21308 if (type_unknown_p (arg))
21309 {
21310 /* [temp.deduct.type] A template-argument can be
21311 deduced from a pointer to function or pointer
21312 to member function argument if the set of
21313 overloaded functions does not contain function
21314 templates and at most one of a set of
21315 overloaded functions provides a unique
21316 match. */
21317 resolve_overloaded_unification (tparms, targs, parm,
21318 arg, strict,
21319 arg_strict, explain_p);
21320 /* If a unique match was not found, this is a
21321 non-deduced context, so we still succeed. */
21322 return unify_success (explain_p);
21323 }
21324
21325 arg_expr = arg;
21326 arg = unlowered_expr_type (arg);
21327 if (arg == error_mark_node)
21328 return unify_invalid (explain_p);
21329 }
21330
21331 arg_strict |=
21332 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
21333 }
21334 else
21335 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
21336 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
21337 return unify_template_argument_mismatch (explain_p, parm, arg);
21338
21339 /* For deduction from an init-list we need the actual list. */
21340 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
21341 arg = arg_expr;
21342 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
21343 }
21344
21345 /* for_each_template_parm callback that always returns 0. */
21346
21347 static int
21348 zero_r (tree, void *)
21349 {
21350 return 0;
21351 }
21352
21353 /* for_each_template_parm any_fn callback to handle deduction of a template
21354 type argument from the type of an array bound. */
21355
21356 static int
21357 array_deduction_r (tree t, void *data)
21358 {
21359 tree_pair_p d = (tree_pair_p)data;
21360 tree &tparms = d->purpose;
21361 tree &targs = d->value;
21362
21363 if (TREE_CODE (t) == ARRAY_TYPE)
21364 if (tree dom = TYPE_DOMAIN (t))
21365 if (tree max = TYPE_MAX_VALUE (dom))
21366 {
21367 if (TREE_CODE (max) == MINUS_EXPR)
21368 max = TREE_OPERAND (max, 0);
21369 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
21370 unify (tparms, targs, TREE_TYPE (max), size_type_node,
21371 UNIFY_ALLOW_NONE, /*explain*/false);
21372 }
21373
21374 /* Keep walking. */
21375 return 0;
21376 }
21377
21378 /* Try to deduce any not-yet-deduced template type arguments from the type of
21379 an array bound. This is handled separately from unify because 14.8.2.5 says
21380 "The type of a type parameter is only deduced from an array bound if it is
21381 not otherwise deduced." */
21382
21383 static void
21384 try_array_deduction (tree tparms, tree targs, tree parm)
21385 {
21386 tree_pair_s data = { tparms, targs };
21387 hash_set<tree> visited;
21388 for_each_template_parm (parm, zero_r, &data, &visited,
21389 /*nondeduced*/false, array_deduction_r);
21390 }
21391
21392 /* Most parms like fn_type_unification.
21393
21394 If SUBR is 1, we're being called recursively (to unify the
21395 arguments of a function or method parameter of a function
21396 template).
21397
21398 CHECKS is a pointer to a vector of access checks encountered while
21399 substituting default template arguments. */
21400
21401 static int
21402 type_unification_real (tree tparms,
21403 tree full_targs,
21404 tree xparms,
21405 const tree *xargs,
21406 unsigned int xnargs,
21407 int subr,
21408 unification_kind_t strict,
21409 vec<deferred_access_check, va_gc> **checks,
21410 bool explain_p)
21411 {
21412 tree parm, arg;
21413 int i;
21414 int ntparms = TREE_VEC_LENGTH (tparms);
21415 int saw_undeduced = 0;
21416 tree parms;
21417 const tree *args;
21418 unsigned int nargs;
21419 unsigned int ia;
21420
21421 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
21422 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
21423 gcc_assert (ntparms > 0);
21424
21425 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
21426
21427 /* Reset the number of non-defaulted template arguments contained
21428 in TARGS. */
21429 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
21430
21431 again:
21432 parms = xparms;
21433 args = xargs;
21434 nargs = xnargs;
21435
21436 ia = 0;
21437 while (parms && parms != void_list_node
21438 && ia < nargs)
21439 {
21440 parm = TREE_VALUE (parms);
21441
21442 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21443 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
21444 /* For a function parameter pack that occurs at the end of the
21445 parameter-declaration-list, the type A of each remaining
21446 argument of the call is compared with the type P of the
21447 declarator-id of the function parameter pack. */
21448 break;
21449
21450 parms = TREE_CHAIN (parms);
21451
21452 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21453 /* For a function parameter pack that does not occur at the
21454 end of the parameter-declaration-list, the type of the
21455 parameter pack is a non-deduced context. */
21456 continue;
21457
21458 arg = args[ia];
21459 ++ia;
21460
21461 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
21462 explain_p))
21463 return 1;
21464 }
21465
21466 if (parms
21467 && parms != void_list_node
21468 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
21469 {
21470 /* Unify the remaining arguments with the pack expansion type. */
21471 tree argvec;
21472 tree parmvec = make_tree_vec (1);
21473
21474 /* Allocate a TREE_VEC and copy in all of the arguments */
21475 argvec = make_tree_vec (nargs - ia);
21476 for (i = 0; ia < nargs; ++ia, ++i)
21477 TREE_VEC_ELT (argvec, i) = args[ia];
21478
21479 /* Copy the parameter into parmvec. */
21480 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
21481 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
21482 /*subr=*/subr, explain_p))
21483 return 1;
21484
21485 /* Advance to the end of the list of parameters. */
21486 parms = TREE_CHAIN (parms);
21487 }
21488
21489 /* Fail if we've reached the end of the parm list, and more args
21490 are present, and the parm list isn't variadic. */
21491 if (ia < nargs && parms == void_list_node)
21492 return unify_too_many_arguments (explain_p, nargs, ia);
21493 /* Fail if parms are left and they don't have default values and
21494 they aren't all deduced as empty packs (c++/57397). This is
21495 consistent with sufficient_parms_p. */
21496 if (parms && parms != void_list_node
21497 && TREE_PURPOSE (parms) == NULL_TREE)
21498 {
21499 unsigned int count = nargs;
21500 tree p = parms;
21501 bool type_pack_p;
21502 do
21503 {
21504 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
21505 if (!type_pack_p)
21506 count++;
21507 p = TREE_CHAIN (p);
21508 }
21509 while (p && p != void_list_node);
21510 if (count != nargs)
21511 return unify_too_few_arguments (explain_p, ia, count,
21512 type_pack_p);
21513 }
21514
21515 if (!subr)
21516 {
21517 tsubst_flags_t complain = (explain_p
21518 ? tf_warning_or_error
21519 : tf_none);
21520 bool tried_array_deduction = (cxx_dialect < cxx17);
21521
21522 for (i = 0; i < ntparms; i++)
21523 {
21524 tree targ = TREE_VEC_ELT (targs, i);
21525 tree tparm = TREE_VEC_ELT (tparms, i);
21526
21527 /* Clear the "incomplete" flags on all argument packs now so that
21528 substituting them into later default arguments works. */
21529 if (targ && ARGUMENT_PACK_P (targ))
21530 {
21531 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21532 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21533 }
21534
21535 if (targ || tparm == error_mark_node)
21536 continue;
21537 tparm = TREE_VALUE (tparm);
21538
21539 if (TREE_CODE (tparm) == TYPE_DECL
21540 && !tried_array_deduction)
21541 {
21542 try_array_deduction (tparms, targs, xparms);
21543 tried_array_deduction = true;
21544 if (TREE_VEC_ELT (targs, i))
21545 continue;
21546 }
21547
21548 /* If this is an undeduced nontype parameter that depends on
21549 a type parameter, try another pass; its type may have been
21550 deduced from a later argument than the one from which
21551 this parameter can be deduced. */
21552 if (TREE_CODE (tparm) == PARM_DECL
21553 && uses_template_parms (TREE_TYPE (tparm))
21554 && saw_undeduced < 2)
21555 {
21556 saw_undeduced = 1;
21557 continue;
21558 }
21559
21560 /* Core issue #226 (C++0x) [temp.deduct]:
21561
21562 If a template argument has not been deduced, its
21563 default template argument, if any, is used.
21564
21565 When we are in C++98 mode, TREE_PURPOSE will either
21566 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21567 to explicitly check cxx_dialect here. */
21568 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21569 /* OK, there is a default argument. Wait until after the
21570 conversion check to do substitution. */
21571 continue;
21572
21573 /* If the type parameter is a parameter pack, then it will
21574 be deduced to an empty parameter pack. */
21575 if (template_parameter_pack_p (tparm))
21576 {
21577 tree arg;
21578
21579 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21580 {
21581 arg = make_node (NONTYPE_ARGUMENT_PACK);
21582 TREE_CONSTANT (arg) = 1;
21583 }
21584 else
21585 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21586
21587 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21588
21589 TREE_VEC_ELT (targs, i) = arg;
21590 continue;
21591 }
21592
21593 return unify_parameter_deduction_failure (explain_p, tparm);
21594 }
21595
21596 /* Now substitute into the default template arguments. */
21597 for (i = 0; i < ntparms; i++)
21598 {
21599 tree targ = TREE_VEC_ELT (targs, i);
21600 tree tparm = TREE_VEC_ELT (tparms, i);
21601
21602 if (targ || tparm == error_mark_node)
21603 continue;
21604 tree parm = TREE_VALUE (tparm);
21605 tree arg = TREE_PURPOSE (tparm);
21606 reopen_deferring_access_checks (*checks);
21607 location_t save_loc = input_location;
21608 if (DECL_P (parm))
21609 input_location = DECL_SOURCE_LOCATION (parm);
21610
21611 if (saw_undeduced == 1
21612 && TREE_CODE (parm) == PARM_DECL
21613 && uses_template_parms (TREE_TYPE (parm)))
21614 {
21615 /* The type of this non-type parameter depends on undeduced
21616 parameters. Don't try to use its default argument yet,
21617 since we might deduce an argument for it on the next pass,
21618 but do check whether the arguments we already have cause
21619 substitution failure, so that that happens before we try
21620 later default arguments (78489). */
21621 ++processing_template_decl;
21622 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21623 NULL_TREE);
21624 --processing_template_decl;
21625 if (type == error_mark_node)
21626 arg = error_mark_node;
21627 else
21628 arg = NULL_TREE;
21629 }
21630 else
21631 {
21632 /* Even if the call is happening in template context, getting
21633 here means it's non-dependent, and a default argument is
21634 considered a separate definition under [temp.decls], so we can
21635 do this substitution without processing_template_decl. This
21636 is important if the default argument contains something that
21637 might be instantiation-dependent like access (87480). */
21638 processing_template_decl_sentinel s;
21639 tree substed = NULL_TREE;
21640 if (saw_undeduced == 1)
21641 {
21642 /* First instatiate in template context, in case we still
21643 depend on undeduced template parameters. */
21644 ++processing_template_decl;
21645 substed = tsubst_template_arg (arg, full_targs, complain,
21646 NULL_TREE);
21647 --processing_template_decl;
21648 if (substed != error_mark_node
21649 && !uses_template_parms (substed))
21650 /* We replaced all the tparms, substitute again out of
21651 template context. */
21652 substed = NULL_TREE;
21653 }
21654 if (!substed)
21655 substed = tsubst_template_arg (arg, full_targs, complain,
21656 NULL_TREE);
21657
21658 if (!uses_template_parms (substed))
21659 arg = convert_template_argument (parm, substed, full_targs,
21660 complain, i, NULL_TREE);
21661 else if (saw_undeduced == 1)
21662 arg = NULL_TREE;
21663 else
21664 arg = error_mark_node;
21665 }
21666
21667 input_location = save_loc;
21668 *checks = get_deferred_access_checks ();
21669 pop_deferring_access_checks ();
21670
21671 if (arg == error_mark_node)
21672 return 1;
21673 else if (arg)
21674 {
21675 TREE_VEC_ELT (targs, i) = arg;
21676 /* The position of the first default template argument,
21677 is also the number of non-defaulted arguments in TARGS.
21678 Record that. */
21679 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21680 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21681 }
21682 }
21683
21684 if (saw_undeduced++ == 1)
21685 goto again;
21686 }
21687
21688 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21689 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21690
21691 return unify_success (explain_p);
21692 }
21693
21694 /* Subroutine of type_unification_real. Args are like the variables
21695 at the call site. ARG is an overloaded function (or template-id);
21696 we try deducing template args from each of the overloads, and if
21697 only one succeeds, we go with that. Modifies TARGS and returns
21698 true on success. */
21699
21700 static bool
21701 resolve_overloaded_unification (tree tparms,
21702 tree targs,
21703 tree parm,
21704 tree arg,
21705 unification_kind_t strict,
21706 int sub_strict,
21707 bool explain_p)
21708 {
21709 tree tempargs = copy_node (targs);
21710 int good = 0;
21711 tree goodfn = NULL_TREE;
21712 bool addr_p;
21713
21714 if (TREE_CODE (arg) == ADDR_EXPR)
21715 {
21716 arg = TREE_OPERAND (arg, 0);
21717 addr_p = true;
21718 }
21719 else
21720 addr_p = false;
21721
21722 if (TREE_CODE (arg) == COMPONENT_REF)
21723 /* Handle `&x' where `x' is some static or non-static member
21724 function name. */
21725 arg = TREE_OPERAND (arg, 1);
21726
21727 if (TREE_CODE (arg) == OFFSET_REF)
21728 arg = TREE_OPERAND (arg, 1);
21729
21730 /* Strip baselink information. */
21731 if (BASELINK_P (arg))
21732 arg = BASELINK_FUNCTIONS (arg);
21733
21734 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21735 {
21736 /* If we got some explicit template args, we need to plug them into
21737 the affected templates before we try to unify, in case the
21738 explicit args will completely resolve the templates in question. */
21739
21740 int ok = 0;
21741 tree expl_subargs = TREE_OPERAND (arg, 1);
21742 arg = TREE_OPERAND (arg, 0);
21743
21744 for (lkp_iterator iter (arg); iter; ++iter)
21745 {
21746 tree fn = *iter;
21747 tree subargs, elem;
21748
21749 if (TREE_CODE (fn) != TEMPLATE_DECL)
21750 continue;
21751
21752 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21753 expl_subargs, NULL_TREE, tf_none,
21754 /*require_all_args=*/true,
21755 /*use_default_args=*/true);
21756 if (subargs != error_mark_node
21757 && !any_dependent_template_arguments_p (subargs))
21758 {
21759 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21760 if (try_one_overload (tparms, targs, tempargs, parm,
21761 elem, strict, sub_strict, addr_p, explain_p)
21762 && (!goodfn || !same_type_p (goodfn, elem)))
21763 {
21764 goodfn = elem;
21765 ++good;
21766 }
21767 }
21768 else if (subargs)
21769 ++ok;
21770 }
21771 /* If no templates (or more than one) are fully resolved by the
21772 explicit arguments, this template-id is a non-deduced context; it
21773 could still be OK if we deduce all template arguments for the
21774 enclosing call through other arguments. */
21775 if (good != 1)
21776 good = ok;
21777 }
21778 else if (!OVL_P (arg))
21779 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21780 -- but the deduction does not succeed because the expression is
21781 not just the function on its own. */
21782 return false;
21783 else
21784 for (lkp_iterator iter (arg); iter; ++iter)
21785 {
21786 tree fn = *iter;
21787 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21788 strict, sub_strict, addr_p, explain_p)
21789 && (!goodfn || !decls_match (goodfn, fn)))
21790 {
21791 goodfn = fn;
21792 ++good;
21793 }
21794 }
21795
21796 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21797 to function or pointer to member function argument if the set of
21798 overloaded functions does not contain function templates and at most
21799 one of a set of overloaded functions provides a unique match.
21800
21801 So if we found multiple possibilities, we return success but don't
21802 deduce anything. */
21803
21804 if (good == 1)
21805 {
21806 int i = TREE_VEC_LENGTH (targs);
21807 for (; i--; )
21808 if (TREE_VEC_ELT (tempargs, i))
21809 {
21810 tree old = TREE_VEC_ELT (targs, i);
21811 tree new_ = TREE_VEC_ELT (tempargs, i);
21812 if (new_ && old && ARGUMENT_PACK_P (old)
21813 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21814 /* Don't forget explicit template arguments in a pack. */
21815 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21816 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21817 TREE_VEC_ELT (targs, i) = new_;
21818 }
21819 }
21820 if (good)
21821 return true;
21822
21823 return false;
21824 }
21825
21826 /* Core DR 115: In contexts where deduction is done and fails, or in
21827 contexts where deduction is not done, if a template argument list is
21828 specified and it, along with any default template arguments, identifies
21829 a single function template specialization, then the template-id is an
21830 lvalue for the function template specialization. */
21831
21832 tree
21833 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21834 {
21835 tree expr, offset, baselink;
21836 bool addr;
21837
21838 if (!type_unknown_p (orig_expr))
21839 return orig_expr;
21840
21841 expr = orig_expr;
21842 addr = false;
21843 offset = NULL_TREE;
21844 baselink = NULL_TREE;
21845
21846 if (TREE_CODE (expr) == ADDR_EXPR)
21847 {
21848 expr = TREE_OPERAND (expr, 0);
21849 addr = true;
21850 }
21851 if (TREE_CODE (expr) == OFFSET_REF)
21852 {
21853 offset = expr;
21854 expr = TREE_OPERAND (expr, 1);
21855 }
21856 if (BASELINK_P (expr))
21857 {
21858 baselink = expr;
21859 expr = BASELINK_FUNCTIONS (expr);
21860 }
21861
21862 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21863 {
21864 int good = 0;
21865 tree goodfn = NULL_TREE;
21866
21867 /* If we got some explicit template args, we need to plug them into
21868 the affected templates before we try to unify, in case the
21869 explicit args will completely resolve the templates in question. */
21870
21871 tree expl_subargs = TREE_OPERAND (expr, 1);
21872 tree arg = TREE_OPERAND (expr, 0);
21873 tree badfn = NULL_TREE;
21874 tree badargs = NULL_TREE;
21875
21876 for (lkp_iterator iter (arg); iter; ++iter)
21877 {
21878 tree fn = *iter;
21879 tree subargs, elem;
21880
21881 if (TREE_CODE (fn) != TEMPLATE_DECL)
21882 continue;
21883
21884 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21885 expl_subargs, NULL_TREE, tf_none,
21886 /*require_all_args=*/true,
21887 /*use_default_args=*/true);
21888 if (subargs != error_mark_node
21889 && !any_dependent_template_arguments_p (subargs))
21890 {
21891 elem = instantiate_template (fn, subargs, tf_none);
21892 if (elem == error_mark_node)
21893 {
21894 badfn = fn;
21895 badargs = subargs;
21896 }
21897 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21898 {
21899 goodfn = elem;
21900 ++good;
21901 }
21902 }
21903 }
21904 if (good == 1)
21905 {
21906 mark_used (goodfn);
21907 expr = goodfn;
21908 if (baselink)
21909 expr = build_baselink (BASELINK_BINFO (baselink),
21910 BASELINK_ACCESS_BINFO (baselink),
21911 expr, BASELINK_OPTYPE (baselink));
21912 if (offset)
21913 {
21914 tree base
21915 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21916 expr = build_offset_ref (base, expr, addr, complain);
21917 }
21918 if (addr)
21919 expr = cp_build_addr_expr (expr, complain);
21920 return expr;
21921 }
21922 else if (good == 0 && badargs && (complain & tf_error))
21923 /* There were no good options and at least one bad one, so let the
21924 user know what the problem is. */
21925 instantiate_template (badfn, badargs, complain);
21926 }
21927 return orig_expr;
21928 }
21929
21930 /* As above, but error out if the expression remains overloaded. */
21931
21932 tree
21933 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21934 {
21935 exp = resolve_nondeduced_context (exp, complain);
21936 if (type_unknown_p (exp))
21937 {
21938 if (complain & tf_error)
21939 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21940 return error_mark_node;
21941 }
21942 return exp;
21943 }
21944
21945 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21946 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21947 different overloads deduce different arguments for a given parm.
21948 ADDR_P is true if the expression for which deduction is being
21949 performed was of the form "& fn" rather than simply "fn".
21950
21951 Returns 1 on success. */
21952
21953 static int
21954 try_one_overload (tree tparms,
21955 tree orig_targs,
21956 tree targs,
21957 tree parm,
21958 tree arg,
21959 unification_kind_t strict,
21960 int sub_strict,
21961 bool addr_p,
21962 bool explain_p)
21963 {
21964 int nargs;
21965 tree tempargs;
21966 int i;
21967
21968 if (arg == error_mark_node)
21969 return 0;
21970
21971 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21972 to function or pointer to member function argument if the set of
21973 overloaded functions does not contain function templates and at most
21974 one of a set of overloaded functions provides a unique match.
21975
21976 So if this is a template, just return success. */
21977
21978 if (uses_template_parms (arg))
21979 return 1;
21980
21981 if (TREE_CODE (arg) == METHOD_TYPE)
21982 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21983 else if (addr_p)
21984 arg = build_pointer_type (arg);
21985
21986 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21987
21988 /* We don't copy orig_targs for this because if we have already deduced
21989 some template args from previous args, unify would complain when we
21990 try to deduce a template parameter for the same argument, even though
21991 there isn't really a conflict. */
21992 nargs = TREE_VEC_LENGTH (targs);
21993 tempargs = make_tree_vec (nargs);
21994
21995 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21996 return 0;
21997
21998 /* First make sure we didn't deduce anything that conflicts with
21999 explicitly specified args. */
22000 for (i = nargs; i--; )
22001 {
22002 tree elt = TREE_VEC_ELT (tempargs, i);
22003 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22004
22005 if (!elt)
22006 /*NOP*/;
22007 else if (uses_template_parms (elt))
22008 /* Since we're unifying against ourselves, we will fill in
22009 template args used in the function parm list with our own
22010 template parms. Discard them. */
22011 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22012 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22013 {
22014 /* Check that the argument at each index of the deduced argument pack
22015 is equivalent to the corresponding explicitly specified argument.
22016 We may have deduced more arguments than were explicitly specified,
22017 and that's OK. */
22018
22019 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22020 that's wrong if we deduce the same argument pack from multiple
22021 function arguments: it's only incomplete the first time. */
22022
22023 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22024 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22025
22026 if (TREE_VEC_LENGTH (deduced_pack)
22027 < TREE_VEC_LENGTH (explicit_pack))
22028 return 0;
22029
22030 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22031 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22032 TREE_VEC_ELT (deduced_pack, j)))
22033 return 0;
22034 }
22035 else if (oldelt && !template_args_equal (oldelt, elt))
22036 return 0;
22037 }
22038
22039 for (i = nargs; i--; )
22040 {
22041 tree elt = TREE_VEC_ELT (tempargs, i);
22042
22043 if (elt)
22044 TREE_VEC_ELT (targs, i) = elt;
22045 }
22046
22047 return 1;
22048 }
22049
22050 /* PARM is a template class (perhaps with unbound template
22051 parameters). ARG is a fully instantiated type. If ARG can be
22052 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22053 TARGS are as for unify. */
22054
22055 static tree
22056 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22057 bool explain_p)
22058 {
22059 tree copy_of_targs;
22060
22061 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22062 return NULL_TREE;
22063 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22064 /* Matches anything. */;
22065 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22066 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22067 return NULL_TREE;
22068
22069 /* We need to make a new template argument vector for the call to
22070 unify. If we used TARGS, we'd clutter it up with the result of
22071 the attempted unification, even if this class didn't work out.
22072 We also don't want to commit ourselves to all the unifications
22073 we've already done, since unification is supposed to be done on
22074 an argument-by-argument basis. In other words, consider the
22075 following pathological case:
22076
22077 template <int I, int J, int K>
22078 struct S {};
22079
22080 template <int I, int J>
22081 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22082
22083 template <int I, int J, int K>
22084 void f(S<I, J, K>, S<I, I, I>);
22085
22086 void g() {
22087 S<0, 0, 0> s0;
22088 S<0, 1, 2> s2;
22089
22090 f(s0, s2);
22091 }
22092
22093 Now, by the time we consider the unification involving `s2', we
22094 already know that we must have `f<0, 0, 0>'. But, even though
22095 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22096 because there are two ways to unify base classes of S<0, 1, 2>
22097 with S<I, I, I>. If we kept the already deduced knowledge, we
22098 would reject the possibility I=1. */
22099 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22100
22101 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22102 {
22103 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22104 return NULL_TREE;
22105 return arg;
22106 }
22107
22108 /* If unification failed, we're done. */
22109 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22110 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22111 return NULL_TREE;
22112
22113 return arg;
22114 }
22115
22116 /* Given a template type PARM and a class type ARG, find the unique
22117 base type in ARG that is an instance of PARM. We do not examine
22118 ARG itself; only its base-classes. If there is not exactly one
22119 appropriate base class, return NULL_TREE. PARM may be the type of
22120 a partial specialization, as well as a plain template type. Used
22121 by unify. */
22122
22123 static enum template_base_result
22124 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22125 bool explain_p, tree *result)
22126 {
22127 tree rval = NULL_TREE;
22128 tree binfo;
22129
22130 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22131
22132 binfo = TYPE_BINFO (complete_type (arg));
22133 if (!binfo)
22134 {
22135 /* The type could not be completed. */
22136 *result = NULL_TREE;
22137 return tbr_incomplete_type;
22138 }
22139
22140 /* Walk in inheritance graph order. The search order is not
22141 important, and this avoids multiple walks of virtual bases. */
22142 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22143 {
22144 tree r = try_class_unification (tparms, targs, parm,
22145 BINFO_TYPE (binfo), explain_p);
22146
22147 if (r)
22148 {
22149 /* If there is more than one satisfactory baseclass, then:
22150
22151 [temp.deduct.call]
22152
22153 If they yield more than one possible deduced A, the type
22154 deduction fails.
22155
22156 applies. */
22157 if (rval && !same_type_p (r, rval))
22158 {
22159 *result = NULL_TREE;
22160 return tbr_ambiguous_baseclass;
22161 }
22162
22163 rval = r;
22164 }
22165 }
22166
22167 *result = rval;
22168 return tbr_success;
22169 }
22170
22171 /* Returns the level of DECL, which declares a template parameter. */
22172
22173 static int
22174 template_decl_level (tree decl)
22175 {
22176 switch (TREE_CODE (decl))
22177 {
22178 case TYPE_DECL:
22179 case TEMPLATE_DECL:
22180 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22181
22182 case PARM_DECL:
22183 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22184
22185 default:
22186 gcc_unreachable ();
22187 }
22188 return 0;
22189 }
22190
22191 /* Decide whether ARG can be unified with PARM, considering only the
22192 cv-qualifiers of each type, given STRICT as documented for unify.
22193 Returns nonzero iff the unification is OK on that basis. */
22194
22195 static int
22196 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22197 {
22198 int arg_quals = cp_type_quals (arg);
22199 int parm_quals = cp_type_quals (parm);
22200
22201 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22202 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22203 {
22204 /* Although a CVR qualifier is ignored when being applied to a
22205 substituted template parameter ([8.3.2]/1 for example), that
22206 does not allow us to unify "const T" with "int&" because both
22207 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
22208 It is ok when we're allowing additional CV qualifiers
22209 at the outer level [14.8.2.1]/3,1st bullet. */
22210 if ((TYPE_REF_P (arg)
22211 || FUNC_OR_METHOD_TYPE_P (arg))
22212 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
22213 return 0;
22214
22215 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
22216 && (parm_quals & TYPE_QUAL_RESTRICT))
22217 return 0;
22218 }
22219
22220 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22221 && (arg_quals & parm_quals) != parm_quals)
22222 return 0;
22223
22224 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
22225 && (parm_quals & arg_quals) != arg_quals)
22226 return 0;
22227
22228 return 1;
22229 }
22230
22231 /* Determines the LEVEL and INDEX for the template parameter PARM. */
22232 void
22233 template_parm_level_and_index (tree parm, int* level, int* index)
22234 {
22235 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22236 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22237 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22238 {
22239 *index = TEMPLATE_TYPE_IDX (parm);
22240 *level = TEMPLATE_TYPE_LEVEL (parm);
22241 }
22242 else
22243 {
22244 *index = TEMPLATE_PARM_IDX (parm);
22245 *level = TEMPLATE_PARM_LEVEL (parm);
22246 }
22247 }
22248
22249 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
22250 do { \
22251 if (unify (TP, TA, P, A, S, EP)) \
22252 return 1; \
22253 } while (0)
22254
22255 /* Unifies the remaining arguments in PACKED_ARGS with the pack
22256 expansion at the end of PACKED_PARMS. Returns 0 if the type
22257 deduction succeeds, 1 otherwise. STRICT is the same as in
22258 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
22259 function call argument list. We'll need to adjust the arguments to make them
22260 types. SUBR tells us if this is from a recursive call to
22261 type_unification_real, or for comparing two template argument
22262 lists. */
22263
22264 static int
22265 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
22266 tree packed_args, unification_kind_t strict,
22267 bool subr, bool explain_p)
22268 {
22269 tree parm
22270 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
22271 tree pattern = PACK_EXPANSION_PATTERN (parm);
22272 tree pack, packs = NULL_TREE;
22273 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
22274
22275 /* Add in any args remembered from an earlier partial instantiation. */
22276 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
22277 int levels = TMPL_ARGS_DEPTH (targs);
22278
22279 packed_args = expand_template_argument_pack (packed_args);
22280
22281 int len = TREE_VEC_LENGTH (packed_args);
22282
22283 /* Determine the parameter packs we will be deducing from the
22284 pattern, and record their current deductions. */
22285 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
22286 pack; pack = TREE_CHAIN (pack))
22287 {
22288 tree parm_pack = TREE_VALUE (pack);
22289 int idx, level;
22290
22291 /* Only template parameter packs can be deduced, not e.g. function
22292 parameter packs or __bases or __integer_pack. */
22293 if (!TEMPLATE_PARM_P (parm_pack))
22294 continue;
22295
22296 /* Determine the index and level of this parameter pack. */
22297 template_parm_level_and_index (parm_pack, &level, &idx);
22298 if (level < levels)
22299 continue;
22300
22301 /* Keep track of the parameter packs and their corresponding
22302 argument packs. */
22303 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
22304 TREE_TYPE (packs) = make_tree_vec (len - start);
22305 }
22306
22307 /* Loop through all of the arguments that have not yet been
22308 unified and unify each with the pattern. */
22309 for (i = start; i < len; i++)
22310 {
22311 tree parm;
22312 bool any_explicit = false;
22313 tree arg = TREE_VEC_ELT (packed_args, i);
22314
22315 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
22316 or the element of its argument pack at the current index if
22317 this argument was explicitly specified. */
22318 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22319 {
22320 int idx, level;
22321 tree arg, pargs;
22322 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22323
22324 arg = NULL_TREE;
22325 if (TREE_VALUE (pack)
22326 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
22327 && (i - start < TREE_VEC_LENGTH (pargs)))
22328 {
22329 any_explicit = true;
22330 arg = TREE_VEC_ELT (pargs, i - start);
22331 }
22332 TMPL_ARG (targs, level, idx) = arg;
22333 }
22334
22335 /* If we had explicit template arguments, substitute them into the
22336 pattern before deduction. */
22337 if (any_explicit)
22338 {
22339 /* Some arguments might still be unspecified or dependent. */
22340 bool dependent;
22341 ++processing_template_decl;
22342 dependent = any_dependent_template_arguments_p (targs);
22343 if (!dependent)
22344 --processing_template_decl;
22345 parm = tsubst (pattern, targs,
22346 explain_p ? tf_warning_or_error : tf_none,
22347 NULL_TREE);
22348 if (dependent)
22349 --processing_template_decl;
22350 if (parm == error_mark_node)
22351 return 1;
22352 }
22353 else
22354 parm = pattern;
22355
22356 /* Unify the pattern with the current argument. */
22357 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
22358 explain_p))
22359 return 1;
22360
22361 /* For each parameter pack, collect the deduced value. */
22362 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22363 {
22364 int idx, level;
22365 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22366
22367 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
22368 TMPL_ARG (targs, level, idx);
22369 }
22370 }
22371
22372 /* Verify that the results of unification with the parameter packs
22373 produce results consistent with what we've seen before, and make
22374 the deduced argument packs available. */
22375 for (pack = packs; pack; pack = TREE_CHAIN (pack))
22376 {
22377 tree old_pack = TREE_VALUE (pack);
22378 tree new_args = TREE_TYPE (pack);
22379 int i, len = TREE_VEC_LENGTH (new_args);
22380 int idx, level;
22381 bool nondeduced_p = false;
22382
22383 /* By default keep the original deduced argument pack.
22384 If necessary, more specific code is going to update the
22385 resulting deduced argument later down in this function. */
22386 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
22387 TMPL_ARG (targs, level, idx) = old_pack;
22388
22389 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
22390 actually deduce anything. */
22391 for (i = 0; i < len && !nondeduced_p; ++i)
22392 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
22393 nondeduced_p = true;
22394 if (nondeduced_p)
22395 continue;
22396
22397 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
22398 {
22399 /* If we had fewer function args than explicit template args,
22400 just use the explicits. */
22401 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22402 int explicit_len = TREE_VEC_LENGTH (explicit_args);
22403 if (len < explicit_len)
22404 new_args = explicit_args;
22405 }
22406
22407 if (!old_pack)
22408 {
22409 tree result;
22410 /* Build the deduced *_ARGUMENT_PACK. */
22411 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
22412 {
22413 result = make_node (NONTYPE_ARGUMENT_PACK);
22414 TREE_CONSTANT (result) = 1;
22415 }
22416 else
22417 result = cxx_make_type (TYPE_ARGUMENT_PACK);
22418
22419 SET_ARGUMENT_PACK_ARGS (result, new_args);
22420
22421 /* Note the deduced argument packs for this parameter
22422 pack. */
22423 TMPL_ARG (targs, level, idx) = result;
22424 }
22425 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
22426 && (ARGUMENT_PACK_ARGS (old_pack)
22427 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
22428 {
22429 /* We only had the explicitly-provided arguments before, but
22430 now we have a complete set of arguments. */
22431 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
22432
22433 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
22434 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
22435 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
22436 }
22437 else
22438 {
22439 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
22440 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
22441
22442 if (!comp_template_args (old_args, new_args,
22443 &bad_old_arg, &bad_new_arg))
22444 /* Inconsistent unification of this parameter pack. */
22445 return unify_parameter_pack_inconsistent (explain_p,
22446 bad_old_arg,
22447 bad_new_arg);
22448 }
22449 }
22450
22451 return unify_success (explain_p);
22452 }
22453
22454 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
22455 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
22456 parameters and return value are as for unify. */
22457
22458 static int
22459 unify_array_domain (tree tparms, tree targs,
22460 tree parm_dom, tree arg_dom,
22461 bool explain_p)
22462 {
22463 tree parm_max;
22464 tree arg_max;
22465 bool parm_cst;
22466 bool arg_cst;
22467
22468 /* Our representation of array types uses "N - 1" as the
22469 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
22470 not an integer constant. We cannot unify arbitrarily
22471 complex expressions, so we eliminate the MINUS_EXPRs
22472 here. */
22473 parm_max = TYPE_MAX_VALUE (parm_dom);
22474 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
22475 if (!parm_cst)
22476 {
22477 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
22478 parm_max = TREE_OPERAND (parm_max, 0);
22479 }
22480 arg_max = TYPE_MAX_VALUE (arg_dom);
22481 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
22482 if (!arg_cst)
22483 {
22484 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
22485 trying to unify the type of a variable with the type
22486 of a template parameter. For example:
22487
22488 template <unsigned int N>
22489 void f (char (&) [N]);
22490 int g();
22491 void h(int i) {
22492 char a[g(i)];
22493 f(a);
22494 }
22495
22496 Here, the type of the ARG will be "int [g(i)]", and
22497 may be a SAVE_EXPR, etc. */
22498 if (TREE_CODE (arg_max) != MINUS_EXPR)
22499 return unify_vla_arg (explain_p, arg_dom);
22500 arg_max = TREE_OPERAND (arg_max, 0);
22501 }
22502
22503 /* If only one of the bounds used a MINUS_EXPR, compensate
22504 by adding one to the other bound. */
22505 if (parm_cst && !arg_cst)
22506 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
22507 integer_type_node,
22508 parm_max,
22509 integer_one_node);
22510 else if (arg_cst && !parm_cst)
22511 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
22512 integer_type_node,
22513 arg_max,
22514 integer_one_node);
22515
22516 return unify (tparms, targs, parm_max, arg_max,
22517 UNIFY_ALLOW_INTEGER, explain_p);
22518 }
22519
22520 /* Returns whether T, a P or A in unify, is a type, template or expression. */
22521
22522 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
22523
22524 static pa_kind_t
22525 pa_kind (tree t)
22526 {
22527 if (PACK_EXPANSION_P (t))
22528 t = PACK_EXPANSION_PATTERN (t);
22529 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22530 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22531 || DECL_TYPE_TEMPLATE_P (t))
22532 return pa_tmpl;
22533 else if (TYPE_P (t))
22534 return pa_type;
22535 else
22536 return pa_expr;
22537 }
22538
22539 /* Deduce the value of template parameters. TPARMS is the (innermost)
22540 set of template parameters to a template. TARGS is the bindings
22541 for those template parameters, as determined thus far; TARGS may
22542 include template arguments for outer levels of template parameters
22543 as well. PARM is a parameter to a template function, or a
22544 subcomponent of that parameter; ARG is the corresponding argument.
22545 This function attempts to match PARM with ARG in a manner
22546 consistent with the existing assignments in TARGS. If more values
22547 are deduced, then TARGS is updated.
22548
22549 Returns 0 if the type deduction succeeds, 1 otherwise. The
22550 parameter STRICT is a bitwise or of the following flags:
22551
22552 UNIFY_ALLOW_NONE:
22553 Require an exact match between PARM and ARG.
22554 UNIFY_ALLOW_MORE_CV_QUAL:
22555 Allow the deduced ARG to be more cv-qualified (by qualification
22556 conversion) than ARG.
22557 UNIFY_ALLOW_LESS_CV_QUAL:
22558 Allow the deduced ARG to be less cv-qualified than ARG.
22559 UNIFY_ALLOW_DERIVED:
22560 Allow the deduced ARG to be a template base class of ARG,
22561 or a pointer to a template base class of the type pointed to by
22562 ARG.
22563 UNIFY_ALLOW_INTEGER:
22564 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22565 case for more information.
22566 UNIFY_ALLOW_OUTER_LEVEL:
22567 This is the outermost level of a deduction. Used to determine validity
22568 of qualification conversions. A valid qualification conversion must
22569 have const qualified pointers leading up to the inner type which
22570 requires additional CV quals, except at the outer level, where const
22571 is not required [conv.qual]. It would be normal to set this flag in
22572 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22573 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22574 This is the outermost level of a deduction, and PARM can be more CV
22575 qualified at this point.
22576 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22577 This is the outermost level of a deduction, and PARM can be less CV
22578 qualified at this point. */
22579
22580 static int
22581 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22582 bool explain_p)
22583 {
22584 int idx;
22585 tree targ;
22586 tree tparm;
22587 int strict_in = strict;
22588 tsubst_flags_t complain = (explain_p
22589 ? tf_warning_or_error
22590 : tf_none);
22591
22592 /* I don't think this will do the right thing with respect to types.
22593 But the only case I've seen it in so far has been array bounds, where
22594 signedness is the only information lost, and I think that will be
22595 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
22596 finish_id_expression_1, and are also OK. */
22597 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
22598 parm = TREE_OPERAND (parm, 0);
22599
22600 if (arg == error_mark_node)
22601 return unify_invalid (explain_p);
22602 if (arg == unknown_type_node
22603 || arg == init_list_type_node)
22604 /* We can't deduce anything from this, but we might get all the
22605 template args from other function args. */
22606 return unify_success (explain_p);
22607
22608 if (parm == any_targ_node || arg == any_targ_node)
22609 return unify_success (explain_p);
22610
22611 /* If PARM uses template parameters, then we can't bail out here,
22612 even if ARG == PARM, since we won't record unifications for the
22613 template parameters. We might need them if we're trying to
22614 figure out which of two things is more specialized. */
22615 if (arg == parm && !uses_template_parms (parm))
22616 return unify_success (explain_p);
22617
22618 /* Handle init lists early, so the rest of the function can assume
22619 we're dealing with a type. */
22620 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22621 {
22622 tree elt, elttype;
22623 unsigned i;
22624 tree orig_parm = parm;
22625
22626 if (!is_std_init_list (parm)
22627 && TREE_CODE (parm) != ARRAY_TYPE)
22628 /* We can only deduce from an initializer list argument if the
22629 parameter is std::initializer_list or an array; otherwise this
22630 is a non-deduced context. */
22631 return unify_success (explain_p);
22632
22633 if (TREE_CODE (parm) == ARRAY_TYPE)
22634 elttype = TREE_TYPE (parm);
22635 else
22636 {
22637 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22638 /* Deduction is defined in terms of a single type, so just punt
22639 on the (bizarre) std::initializer_list<T...>. */
22640 if (PACK_EXPANSION_P (elttype))
22641 return unify_success (explain_p);
22642 }
22643
22644 if (strict != DEDUCE_EXACT
22645 && TYPE_P (elttype)
22646 && !uses_deducible_template_parms (elttype))
22647 /* If ELTTYPE has no deducible template parms, skip deduction from
22648 the list elements. */;
22649 else
22650 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22651 {
22652 int elt_strict = strict;
22653
22654 if (elt == error_mark_node)
22655 return unify_invalid (explain_p);
22656
22657 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22658 {
22659 tree type = TREE_TYPE (elt);
22660 if (type == error_mark_node)
22661 return unify_invalid (explain_p);
22662 /* It should only be possible to get here for a call. */
22663 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22664 elt_strict |= maybe_adjust_types_for_deduction
22665 (DEDUCE_CALL, &elttype, &type, elt);
22666 elt = type;
22667 }
22668
22669 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22670 explain_p);
22671 }
22672
22673 if (TREE_CODE (parm) == ARRAY_TYPE
22674 && deducible_array_bound (TYPE_DOMAIN (parm)))
22675 {
22676 /* Also deduce from the length of the initializer list. */
22677 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22678 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22679 if (idx == error_mark_node)
22680 return unify_invalid (explain_p);
22681 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22682 idx, explain_p);
22683 }
22684
22685 /* If the std::initializer_list<T> deduction worked, replace the
22686 deduced A with std::initializer_list<A>. */
22687 if (orig_parm != parm)
22688 {
22689 idx = TEMPLATE_TYPE_IDX (orig_parm);
22690 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22691 targ = listify (targ);
22692 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22693 }
22694 return unify_success (explain_p);
22695 }
22696
22697 /* If parm and arg aren't the same kind of thing (template, type, or
22698 expression), fail early. */
22699 if (pa_kind (parm) != pa_kind (arg))
22700 return unify_invalid (explain_p);
22701
22702 /* Immediately reject some pairs that won't unify because of
22703 cv-qualification mismatches. */
22704 if (TREE_CODE (arg) == TREE_CODE (parm)
22705 && TYPE_P (arg)
22706 /* It is the elements of the array which hold the cv quals of an array
22707 type, and the elements might be template type parms. We'll check
22708 when we recurse. */
22709 && TREE_CODE (arg) != ARRAY_TYPE
22710 /* We check the cv-qualifiers when unifying with template type
22711 parameters below. We want to allow ARG `const T' to unify with
22712 PARM `T' for example, when computing which of two templates
22713 is more specialized, for example. */
22714 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22715 && !check_cv_quals_for_unify (strict_in, arg, parm))
22716 return unify_cv_qual_mismatch (explain_p, parm, arg);
22717
22718 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22719 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22720 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22721 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22722 strict &= ~UNIFY_ALLOW_DERIVED;
22723 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22724 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22725
22726 switch (TREE_CODE (parm))
22727 {
22728 case TYPENAME_TYPE:
22729 case SCOPE_REF:
22730 case UNBOUND_CLASS_TEMPLATE:
22731 /* In a type which contains a nested-name-specifier, template
22732 argument values cannot be deduced for template parameters used
22733 within the nested-name-specifier. */
22734 return unify_success (explain_p);
22735
22736 case TEMPLATE_TYPE_PARM:
22737 case TEMPLATE_TEMPLATE_PARM:
22738 case BOUND_TEMPLATE_TEMPLATE_PARM:
22739 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22740 if (error_operand_p (tparm))
22741 return unify_invalid (explain_p);
22742
22743 if (TEMPLATE_TYPE_LEVEL (parm)
22744 != template_decl_level (tparm))
22745 /* The PARM is not one we're trying to unify. Just check
22746 to see if it matches ARG. */
22747 {
22748 if (TREE_CODE (arg) == TREE_CODE (parm)
22749 && (is_auto (parm) ? is_auto (arg)
22750 : same_type_p (parm, arg)))
22751 return unify_success (explain_p);
22752 else
22753 return unify_type_mismatch (explain_p, parm, arg);
22754 }
22755 idx = TEMPLATE_TYPE_IDX (parm);
22756 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22757 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22758 if (error_operand_p (tparm))
22759 return unify_invalid (explain_p);
22760
22761 /* Check for mixed types and values. */
22762 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22763 && TREE_CODE (tparm) != TYPE_DECL)
22764 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22765 && TREE_CODE (tparm) != TEMPLATE_DECL))
22766 gcc_unreachable ();
22767
22768 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22769 {
22770 if ((strict_in & UNIFY_ALLOW_DERIVED)
22771 && CLASS_TYPE_P (arg))
22772 {
22773 /* First try to match ARG directly. */
22774 tree t = try_class_unification (tparms, targs, parm, arg,
22775 explain_p);
22776 if (!t)
22777 {
22778 /* Otherwise, look for a suitable base of ARG, as below. */
22779 enum template_base_result r;
22780 r = get_template_base (tparms, targs, parm, arg,
22781 explain_p, &t);
22782 if (!t)
22783 return unify_no_common_base (explain_p, r, parm, arg);
22784 arg = t;
22785 }
22786 }
22787 /* ARG must be constructed from a template class or a template
22788 template parameter. */
22789 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22790 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22791 return unify_template_deduction_failure (explain_p, parm, arg);
22792
22793 /* Deduce arguments T, i from TT<T> or TT<i>. */
22794 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22795 return 1;
22796
22797 arg = TYPE_TI_TEMPLATE (arg);
22798
22799 /* Fall through to deduce template name. */
22800 }
22801
22802 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22803 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22804 {
22805 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22806
22807 /* Simple cases: Value already set, does match or doesn't. */
22808 if (targ != NULL_TREE && template_args_equal (targ, arg))
22809 return unify_success (explain_p);
22810 else if (targ)
22811 return unify_inconsistency (explain_p, parm, targ, arg);
22812 }
22813 else
22814 {
22815 /* If PARM is `const T' and ARG is only `int', we don't have
22816 a match unless we are allowing additional qualification.
22817 If ARG is `const int' and PARM is just `T' that's OK;
22818 that binds `const int' to `T'. */
22819 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22820 arg, parm))
22821 return unify_cv_qual_mismatch (explain_p, parm, arg);
22822
22823 /* Consider the case where ARG is `const volatile int' and
22824 PARM is `const T'. Then, T should be `volatile int'. */
22825 arg = cp_build_qualified_type_real
22826 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22827 if (arg == error_mark_node)
22828 return unify_invalid (explain_p);
22829
22830 /* Simple cases: Value already set, does match or doesn't. */
22831 if (targ != NULL_TREE && same_type_p (targ, arg))
22832 return unify_success (explain_p);
22833 else if (targ)
22834 return unify_inconsistency (explain_p, parm, targ, arg);
22835
22836 /* Make sure that ARG is not a variable-sized array. (Note
22837 that were talking about variable-sized arrays (like
22838 `int[n]'), rather than arrays of unknown size (like
22839 `int[]').) We'll get very confused by such a type since
22840 the bound of the array is not constant, and therefore
22841 not mangleable. Besides, such types are not allowed in
22842 ISO C++, so we can do as we please here. We do allow
22843 them for 'auto' deduction, since that isn't ABI-exposed. */
22844 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22845 return unify_vla_arg (explain_p, arg);
22846
22847 /* Strip typedefs as in convert_template_argument. */
22848 arg = canonicalize_type_argument (arg, tf_none);
22849 }
22850
22851 /* If ARG is a parameter pack or an expansion, we cannot unify
22852 against it unless PARM is also a parameter pack. */
22853 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22854 && !template_parameter_pack_p (parm))
22855 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22856
22857 /* If the argument deduction results is a METHOD_TYPE,
22858 then there is a problem.
22859 METHOD_TYPE doesn't map to any real C++ type the result of
22860 the deduction cannot be of that type. */
22861 if (TREE_CODE (arg) == METHOD_TYPE)
22862 return unify_method_type_error (explain_p, arg);
22863
22864 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22865 return unify_success (explain_p);
22866
22867 case TEMPLATE_PARM_INDEX:
22868 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22869 if (error_operand_p (tparm))
22870 return unify_invalid (explain_p);
22871
22872 if (TEMPLATE_PARM_LEVEL (parm)
22873 != template_decl_level (tparm))
22874 {
22875 /* The PARM is not one we're trying to unify. Just check
22876 to see if it matches ARG. */
22877 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22878 && cp_tree_equal (parm, arg));
22879 if (result)
22880 unify_expression_unequal (explain_p, parm, arg);
22881 return result;
22882 }
22883
22884 idx = TEMPLATE_PARM_IDX (parm);
22885 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22886
22887 if (targ)
22888 {
22889 if ((strict & UNIFY_ALLOW_INTEGER)
22890 && TREE_TYPE (targ) && TREE_TYPE (arg)
22891 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22892 /* We're deducing from an array bound, the type doesn't matter. */
22893 arg = fold_convert (TREE_TYPE (targ), arg);
22894 int x = !cp_tree_equal (targ, arg);
22895 if (x)
22896 unify_inconsistency (explain_p, parm, targ, arg);
22897 return x;
22898 }
22899
22900 /* [temp.deduct.type] If, in the declaration of a function template
22901 with a non-type template-parameter, the non-type
22902 template-parameter is used in an expression in the function
22903 parameter-list and, if the corresponding template-argument is
22904 deduced, the template-argument type shall match the type of the
22905 template-parameter exactly, except that a template-argument
22906 deduced from an array bound may be of any integral type.
22907 The non-type parameter might use already deduced type parameters. */
22908 tparm = TREE_TYPE (parm);
22909 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22910 /* We don't have enough levels of args to do any substitution. This
22911 can happen in the context of -fnew-ttp-matching. */;
22912 else
22913 {
22914 ++processing_template_decl;
22915 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22916 --processing_template_decl;
22917
22918 if (tree a = type_uses_auto (tparm))
22919 {
22920 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22921 if (tparm == error_mark_node)
22922 return 1;
22923 }
22924 }
22925
22926 if (!TREE_TYPE (arg))
22927 /* Template-parameter dependent expression. Just accept it for now.
22928 It will later be processed in convert_template_argument. */
22929 ;
22930 else if (same_type_ignoring_top_level_qualifiers_p
22931 (non_reference (TREE_TYPE (arg)),
22932 non_reference (tparm)))
22933 /* OK. Ignore top-level quals here because a class-type template
22934 parameter object is const. */;
22935 else if ((strict & UNIFY_ALLOW_INTEGER)
22936 && CP_INTEGRAL_TYPE_P (tparm))
22937 /* Convert the ARG to the type of PARM; the deduced non-type
22938 template argument must exactly match the types of the
22939 corresponding parameter. */
22940 arg = fold (build_nop (tparm, arg));
22941 else if (uses_template_parms (tparm))
22942 {
22943 /* We haven't deduced the type of this parameter yet. */
22944 if (cxx_dialect >= cxx17
22945 /* We deduce from array bounds in try_array_deduction. */
22946 && !(strict & UNIFY_ALLOW_INTEGER))
22947 {
22948 /* Deduce it from the non-type argument. */
22949 tree atype = TREE_TYPE (arg);
22950 RECUR_AND_CHECK_FAILURE (tparms, targs,
22951 tparm, atype,
22952 UNIFY_ALLOW_NONE, explain_p);
22953 }
22954 else
22955 /* Try again later. */
22956 return unify_success (explain_p);
22957 }
22958 else
22959 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22960
22961 /* If ARG is a parameter pack or an expansion, we cannot unify
22962 against it unless PARM is also a parameter pack. */
22963 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22964 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22965 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22966
22967 {
22968 bool removed_attr = false;
22969 arg = strip_typedefs_expr (arg, &removed_attr);
22970 }
22971 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22972 return unify_success (explain_p);
22973
22974 case PTRMEM_CST:
22975 {
22976 /* A pointer-to-member constant can be unified only with
22977 another constant. */
22978 if (TREE_CODE (arg) != PTRMEM_CST)
22979 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22980
22981 /* Just unify the class member. It would be useless (and possibly
22982 wrong, depending on the strict flags) to unify also
22983 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22984 arg refer to the same variable, even if through different
22985 classes. For instance:
22986
22987 struct A { int x; };
22988 struct B : A { };
22989
22990 Unification of &A::x and &B::x must succeed. */
22991 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22992 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22993 }
22994
22995 case POINTER_TYPE:
22996 {
22997 if (!TYPE_PTR_P (arg))
22998 return unify_type_mismatch (explain_p, parm, arg);
22999
23000 /* [temp.deduct.call]
23001
23002 A can be another pointer or pointer to member type that can
23003 be converted to the deduced A via a qualification
23004 conversion (_conv.qual_).
23005
23006 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23007 This will allow for additional cv-qualification of the
23008 pointed-to types if appropriate. */
23009
23010 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23011 /* The derived-to-base conversion only persists through one
23012 level of pointers. */
23013 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23014
23015 return unify (tparms, targs, TREE_TYPE (parm),
23016 TREE_TYPE (arg), strict, explain_p);
23017 }
23018
23019 case REFERENCE_TYPE:
23020 if (!TYPE_REF_P (arg))
23021 return unify_type_mismatch (explain_p, parm, arg);
23022 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23023 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23024
23025 case ARRAY_TYPE:
23026 if (TREE_CODE (arg) != ARRAY_TYPE)
23027 return unify_type_mismatch (explain_p, parm, arg);
23028 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23029 != (TYPE_DOMAIN (arg) == NULL_TREE))
23030 return unify_type_mismatch (explain_p, parm, arg);
23031 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23032 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23033 if (TYPE_DOMAIN (parm) != NULL_TREE)
23034 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23035 TYPE_DOMAIN (arg), explain_p);
23036 return unify_success (explain_p);
23037
23038 case REAL_TYPE:
23039 case COMPLEX_TYPE:
23040 case VECTOR_TYPE:
23041 case INTEGER_TYPE:
23042 case BOOLEAN_TYPE:
23043 case ENUMERAL_TYPE:
23044 case VOID_TYPE:
23045 case NULLPTR_TYPE:
23046 if (TREE_CODE (arg) != TREE_CODE (parm))
23047 return unify_type_mismatch (explain_p, parm, arg);
23048
23049 /* We have already checked cv-qualification at the top of the
23050 function. */
23051 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23052 return unify_type_mismatch (explain_p, parm, arg);
23053
23054 /* As far as unification is concerned, this wins. Later checks
23055 will invalidate it if necessary. */
23056 return unify_success (explain_p);
23057
23058 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23059 /* Type INTEGER_CST can come from ordinary constant template args. */
23060 case INTEGER_CST:
23061 while (CONVERT_EXPR_P (arg))
23062 arg = TREE_OPERAND (arg, 0);
23063
23064 if (TREE_CODE (arg) != INTEGER_CST)
23065 return unify_template_argument_mismatch (explain_p, parm, arg);
23066 return (tree_int_cst_equal (parm, arg)
23067 ? unify_success (explain_p)
23068 : unify_template_argument_mismatch (explain_p, parm, arg));
23069
23070 case TREE_VEC:
23071 {
23072 int i, len, argslen;
23073 int parm_variadic_p = 0;
23074
23075 if (TREE_CODE (arg) != TREE_VEC)
23076 return unify_template_argument_mismatch (explain_p, parm, arg);
23077
23078 len = TREE_VEC_LENGTH (parm);
23079 argslen = TREE_VEC_LENGTH (arg);
23080
23081 /* Check for pack expansions in the parameters. */
23082 for (i = 0; i < len; ++i)
23083 {
23084 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23085 {
23086 if (i == len - 1)
23087 /* We can unify against something with a trailing
23088 parameter pack. */
23089 parm_variadic_p = 1;
23090 else
23091 /* [temp.deduct.type]/9: If the template argument list of
23092 P contains a pack expansion that is not the last
23093 template argument, the entire template argument list
23094 is a non-deduced context. */
23095 return unify_success (explain_p);
23096 }
23097 }
23098
23099 /* If we don't have enough arguments to satisfy the parameters
23100 (not counting the pack expression at the end), or we have
23101 too many arguments for a parameter list that doesn't end in
23102 a pack expression, we can't unify. */
23103 if (parm_variadic_p
23104 ? argslen < len - parm_variadic_p
23105 : argslen != len)
23106 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23107
23108 /* Unify all of the parameters that precede the (optional)
23109 pack expression. */
23110 for (i = 0; i < len - parm_variadic_p; ++i)
23111 {
23112 RECUR_AND_CHECK_FAILURE (tparms, targs,
23113 TREE_VEC_ELT (parm, i),
23114 TREE_VEC_ELT (arg, i),
23115 UNIFY_ALLOW_NONE, explain_p);
23116 }
23117 if (parm_variadic_p)
23118 return unify_pack_expansion (tparms, targs, parm, arg,
23119 DEDUCE_EXACT,
23120 /*subr=*/true, explain_p);
23121 return unify_success (explain_p);
23122 }
23123
23124 case RECORD_TYPE:
23125 case UNION_TYPE:
23126 if (TREE_CODE (arg) != TREE_CODE (parm))
23127 return unify_type_mismatch (explain_p, parm, arg);
23128
23129 if (TYPE_PTRMEMFUNC_P (parm))
23130 {
23131 if (!TYPE_PTRMEMFUNC_P (arg))
23132 return unify_type_mismatch (explain_p, parm, arg);
23133
23134 return unify (tparms, targs,
23135 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23136 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23137 strict, explain_p);
23138 }
23139 else if (TYPE_PTRMEMFUNC_P (arg))
23140 return unify_type_mismatch (explain_p, parm, arg);
23141
23142 if (CLASSTYPE_TEMPLATE_INFO (parm))
23143 {
23144 tree t = NULL_TREE;
23145
23146 if (strict_in & UNIFY_ALLOW_DERIVED)
23147 {
23148 /* First, we try to unify the PARM and ARG directly. */
23149 t = try_class_unification (tparms, targs,
23150 parm, arg, explain_p);
23151
23152 if (!t)
23153 {
23154 /* Fallback to the special case allowed in
23155 [temp.deduct.call]:
23156
23157 If P is a class, and P has the form
23158 template-id, then A can be a derived class of
23159 the deduced A. Likewise, if P is a pointer to
23160 a class of the form template-id, A can be a
23161 pointer to a derived class pointed to by the
23162 deduced A. */
23163 enum template_base_result r;
23164 r = get_template_base (tparms, targs, parm, arg,
23165 explain_p, &t);
23166
23167 if (!t)
23168 {
23169 /* Don't give the derived diagnostic if we're
23170 already dealing with the same template. */
23171 bool same_template
23172 = (CLASSTYPE_TEMPLATE_INFO (arg)
23173 && (CLASSTYPE_TI_TEMPLATE (parm)
23174 == CLASSTYPE_TI_TEMPLATE (arg)));
23175 return unify_no_common_base (explain_p && !same_template,
23176 r, parm, arg);
23177 }
23178 }
23179 }
23180 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23181 && (CLASSTYPE_TI_TEMPLATE (parm)
23182 == CLASSTYPE_TI_TEMPLATE (arg)))
23183 /* Perhaps PARM is something like S<U> and ARG is S<int>.
23184 Then, we should unify `int' and `U'. */
23185 t = arg;
23186 else
23187 /* There's no chance of unification succeeding. */
23188 return unify_type_mismatch (explain_p, parm, arg);
23189
23190 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
23191 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
23192 }
23193 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
23194 return unify_type_mismatch (explain_p, parm, arg);
23195 return unify_success (explain_p);
23196
23197 case METHOD_TYPE:
23198 case FUNCTION_TYPE:
23199 {
23200 unsigned int nargs;
23201 tree *args;
23202 tree a;
23203 unsigned int i;
23204
23205 if (TREE_CODE (arg) != TREE_CODE (parm))
23206 return unify_type_mismatch (explain_p, parm, arg);
23207
23208 /* CV qualifications for methods can never be deduced, they must
23209 match exactly. We need to check them explicitly here,
23210 because type_unification_real treats them as any other
23211 cv-qualified parameter. */
23212 if (TREE_CODE (parm) == METHOD_TYPE
23213 && (!check_cv_quals_for_unify
23214 (UNIFY_ALLOW_NONE,
23215 class_of_this_parm (arg),
23216 class_of_this_parm (parm))))
23217 return unify_cv_qual_mismatch (explain_p, parm, arg);
23218 if (TREE_CODE (arg) == FUNCTION_TYPE
23219 && type_memfn_quals (parm) != type_memfn_quals (arg))
23220 return unify_cv_qual_mismatch (explain_p, parm, arg);
23221 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
23222 return unify_type_mismatch (explain_p, parm, arg);
23223
23224 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
23225 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
23226
23227 nargs = list_length (TYPE_ARG_TYPES (arg));
23228 args = XALLOCAVEC (tree, nargs);
23229 for (a = TYPE_ARG_TYPES (arg), i = 0;
23230 a != NULL_TREE && a != void_list_node;
23231 a = TREE_CHAIN (a), ++i)
23232 args[i] = TREE_VALUE (a);
23233 nargs = i;
23234
23235 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
23236 args, nargs, 1, DEDUCE_EXACT,
23237 NULL, explain_p))
23238 return 1;
23239
23240 if (flag_noexcept_type)
23241 {
23242 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
23243 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
23244 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
23245 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
23246 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
23247 && uses_template_parms (TREE_PURPOSE (pspec)))
23248 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
23249 TREE_PURPOSE (aspec),
23250 UNIFY_ALLOW_NONE, explain_p);
23251 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
23252 return unify_type_mismatch (explain_p, parm, arg);
23253 }
23254
23255 return 0;
23256 }
23257
23258 case OFFSET_TYPE:
23259 /* Unify a pointer to member with a pointer to member function, which
23260 deduces the type of the member as a function type. */
23261 if (TYPE_PTRMEMFUNC_P (arg))
23262 {
23263 /* Check top-level cv qualifiers */
23264 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
23265 return unify_cv_qual_mismatch (explain_p, parm, arg);
23266
23267 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23268 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
23269 UNIFY_ALLOW_NONE, explain_p);
23270
23271 /* Determine the type of the function we are unifying against. */
23272 tree fntype = static_fn_type (arg);
23273
23274 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
23275 }
23276
23277 if (TREE_CODE (arg) != OFFSET_TYPE)
23278 return unify_type_mismatch (explain_p, parm, arg);
23279 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
23280 TYPE_OFFSET_BASETYPE (arg),
23281 UNIFY_ALLOW_NONE, explain_p);
23282 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23283 strict, explain_p);
23284
23285 case CONST_DECL:
23286 if (DECL_TEMPLATE_PARM_P (parm))
23287 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
23288 if (arg != scalar_constant_value (parm))
23289 return unify_template_argument_mismatch (explain_p, parm, arg);
23290 return unify_success (explain_p);
23291
23292 case FIELD_DECL:
23293 case TEMPLATE_DECL:
23294 /* Matched cases are handled by the ARG == PARM test above. */
23295 return unify_template_argument_mismatch (explain_p, parm, arg);
23296
23297 case VAR_DECL:
23298 /* We might get a variable as a non-type template argument in parm if the
23299 corresponding parameter is type-dependent. Make any necessary
23300 adjustments based on whether arg is a reference. */
23301 if (CONSTANT_CLASS_P (arg))
23302 parm = fold_non_dependent_expr (parm, complain);
23303 else if (REFERENCE_REF_P (arg))
23304 {
23305 tree sub = TREE_OPERAND (arg, 0);
23306 STRIP_NOPS (sub);
23307 if (TREE_CODE (sub) == ADDR_EXPR)
23308 arg = TREE_OPERAND (sub, 0);
23309 }
23310 /* Now use the normal expression code to check whether they match. */
23311 goto expr;
23312
23313 case TYPE_ARGUMENT_PACK:
23314 case NONTYPE_ARGUMENT_PACK:
23315 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
23316 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
23317
23318 case TYPEOF_TYPE:
23319 case DECLTYPE_TYPE:
23320 case UNDERLYING_TYPE:
23321 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
23322 or UNDERLYING_TYPE nodes. */
23323 return unify_success (explain_p);
23324
23325 case ERROR_MARK:
23326 /* Unification fails if we hit an error node. */
23327 return unify_invalid (explain_p);
23328
23329 case INDIRECT_REF:
23330 if (REFERENCE_REF_P (parm))
23331 {
23332 bool pexp = PACK_EXPANSION_P (arg);
23333 if (pexp)
23334 arg = PACK_EXPANSION_PATTERN (arg);
23335 if (REFERENCE_REF_P (arg))
23336 arg = TREE_OPERAND (arg, 0);
23337 if (pexp)
23338 arg = make_pack_expansion (arg, complain);
23339 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
23340 strict, explain_p);
23341 }
23342 /* FALLTHRU */
23343
23344 default:
23345 /* An unresolved overload is a nondeduced context. */
23346 if (is_overloaded_fn (parm) || type_unknown_p (parm))
23347 return unify_success (explain_p);
23348 gcc_assert (EXPR_P (parm)
23349 || COMPOUND_LITERAL_P (parm)
23350 || TREE_CODE (parm) == TRAIT_EXPR);
23351 expr:
23352 /* We must be looking at an expression. This can happen with
23353 something like:
23354
23355 template <int I>
23356 void foo(S<I>, S<I + 2>);
23357
23358 or
23359
23360 template<typename T>
23361 void foo(A<T, T{}>);
23362
23363 This is a "non-deduced context":
23364
23365 [deduct.type]
23366
23367 The non-deduced contexts are:
23368
23369 --A non-type template argument or an array bound in which
23370 a subexpression references a template parameter.
23371
23372 In these cases, we assume deduction succeeded, but don't
23373 actually infer any unifications. */
23374
23375 if (!uses_template_parms (parm)
23376 && !template_args_equal (parm, arg))
23377 return unify_expression_unequal (explain_p, parm, arg);
23378 else
23379 return unify_success (explain_p);
23380 }
23381 }
23382 #undef RECUR_AND_CHECK_FAILURE
23383 \f
23384 /* Note that DECL can be defined in this translation unit, if
23385 required. */
23386
23387 static void
23388 mark_definable (tree decl)
23389 {
23390 tree clone;
23391 DECL_NOT_REALLY_EXTERN (decl) = 1;
23392 FOR_EACH_CLONE (clone, decl)
23393 DECL_NOT_REALLY_EXTERN (clone) = 1;
23394 }
23395
23396 /* Called if RESULT is explicitly instantiated, or is a member of an
23397 explicitly instantiated class. */
23398
23399 void
23400 mark_decl_instantiated (tree result, int extern_p)
23401 {
23402 SET_DECL_EXPLICIT_INSTANTIATION (result);
23403
23404 /* If this entity has already been written out, it's too late to
23405 make any modifications. */
23406 if (TREE_ASM_WRITTEN (result))
23407 return;
23408
23409 /* For anonymous namespace we don't need to do anything. */
23410 if (decl_anon_ns_mem_p (result))
23411 {
23412 gcc_assert (!TREE_PUBLIC (result));
23413 return;
23414 }
23415
23416 if (TREE_CODE (result) != FUNCTION_DECL)
23417 /* The TREE_PUBLIC flag for function declarations will have been
23418 set correctly by tsubst. */
23419 TREE_PUBLIC (result) = 1;
23420
23421 /* This might have been set by an earlier implicit instantiation. */
23422 DECL_COMDAT (result) = 0;
23423
23424 if (extern_p)
23425 DECL_NOT_REALLY_EXTERN (result) = 0;
23426 else
23427 {
23428 mark_definable (result);
23429 mark_needed (result);
23430 /* Always make artificials weak. */
23431 if (DECL_ARTIFICIAL (result) && flag_weak)
23432 comdat_linkage (result);
23433 /* For WIN32 we also want to put explicit instantiations in
23434 linkonce sections. */
23435 else if (TREE_PUBLIC (result))
23436 maybe_make_one_only (result);
23437 if (TREE_CODE (result) == FUNCTION_DECL
23438 && DECL_TEMPLATE_INSTANTIATED (result))
23439 /* If the function has already been instantiated, clear DECL_EXTERNAL,
23440 since start_preparsed_function wouldn't have if we had an earlier
23441 extern explicit instantiation. */
23442 DECL_EXTERNAL (result) = 0;
23443 }
23444
23445 /* If EXTERN_P, then this function will not be emitted -- unless
23446 followed by an explicit instantiation, at which point its linkage
23447 will be adjusted. If !EXTERN_P, then this function will be
23448 emitted here. In neither circumstance do we want
23449 import_export_decl to adjust the linkage. */
23450 DECL_INTERFACE_KNOWN (result) = 1;
23451 }
23452
23453 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
23454 important template arguments. If any are missing, we check whether
23455 they're important by using error_mark_node for substituting into any
23456 args that were used for partial ordering (the ones between ARGS and END)
23457 and seeing if it bubbles up. */
23458
23459 static bool
23460 check_undeduced_parms (tree targs, tree args, tree end)
23461 {
23462 bool found = false;
23463 int i;
23464 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
23465 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
23466 {
23467 found = true;
23468 TREE_VEC_ELT (targs, i) = error_mark_node;
23469 }
23470 if (found)
23471 {
23472 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
23473 if (substed == error_mark_node)
23474 return true;
23475 }
23476 return false;
23477 }
23478
23479 /* Given two function templates PAT1 and PAT2, return:
23480
23481 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
23482 -1 if PAT2 is more specialized than PAT1.
23483 0 if neither is more specialized.
23484
23485 LEN indicates the number of parameters we should consider
23486 (defaulted parameters should not be considered).
23487
23488 The 1998 std underspecified function template partial ordering, and
23489 DR214 addresses the issue. We take pairs of arguments, one from
23490 each of the templates, and deduce them against each other. One of
23491 the templates will be more specialized if all the *other*
23492 template's arguments deduce against its arguments and at least one
23493 of its arguments *does* *not* deduce against the other template's
23494 corresponding argument. Deduction is done as for class templates.
23495 The arguments used in deduction have reference and top level cv
23496 qualifiers removed. Iff both arguments were originally reference
23497 types *and* deduction succeeds in both directions, an lvalue reference
23498 wins against an rvalue reference and otherwise the template
23499 with the more cv-qualified argument wins for that pairing (if
23500 neither is more cv-qualified, they both are equal). Unlike regular
23501 deduction, after all the arguments have been deduced in this way,
23502 we do *not* verify the deduced template argument values can be
23503 substituted into non-deduced contexts.
23504
23505 The logic can be a bit confusing here, because we look at deduce1 and
23506 targs1 to see if pat2 is at least as specialized, and vice versa; if we
23507 can find template arguments for pat1 to make arg1 look like arg2, that
23508 means that arg2 is at least as specialized as arg1. */
23509
23510 int
23511 more_specialized_fn (tree pat1, tree pat2, int len)
23512 {
23513 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
23514 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
23515 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
23516 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
23517 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
23518 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
23519 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
23520 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
23521 tree origs1, origs2;
23522 bool lose1 = false;
23523 bool lose2 = false;
23524
23525 /* Remove the this parameter from non-static member functions. If
23526 one is a non-static member function and the other is not a static
23527 member function, remove the first parameter from that function
23528 also. This situation occurs for operator functions where we
23529 locate both a member function (with this pointer) and non-member
23530 operator (with explicit first operand). */
23531 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23532 {
23533 len--; /* LEN is the number of significant arguments for DECL1 */
23534 args1 = TREE_CHAIN (args1);
23535 if (!DECL_STATIC_FUNCTION_P (decl2))
23536 args2 = TREE_CHAIN (args2);
23537 }
23538 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23539 {
23540 args2 = TREE_CHAIN (args2);
23541 if (!DECL_STATIC_FUNCTION_P (decl1))
23542 {
23543 len--;
23544 args1 = TREE_CHAIN (args1);
23545 }
23546 }
23547
23548 /* If only one is a conversion operator, they are unordered. */
23549 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23550 return 0;
23551
23552 /* Consider the return type for a conversion function */
23553 if (DECL_CONV_FN_P (decl1))
23554 {
23555 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23556 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23557 len++;
23558 }
23559
23560 processing_template_decl++;
23561
23562 origs1 = args1;
23563 origs2 = args2;
23564
23565 while (len--
23566 /* Stop when an ellipsis is seen. */
23567 && args1 != NULL_TREE && args2 != NULL_TREE)
23568 {
23569 tree arg1 = TREE_VALUE (args1);
23570 tree arg2 = TREE_VALUE (args2);
23571 int deduce1, deduce2;
23572 int quals1 = -1;
23573 int quals2 = -1;
23574 int ref1 = 0;
23575 int ref2 = 0;
23576
23577 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23578 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23579 {
23580 /* When both arguments are pack expansions, we need only
23581 unify the patterns themselves. */
23582 arg1 = PACK_EXPANSION_PATTERN (arg1);
23583 arg2 = PACK_EXPANSION_PATTERN (arg2);
23584
23585 /* This is the last comparison we need to do. */
23586 len = 0;
23587 }
23588
23589 /* DR 1847: If a particular P contains no template-parameters that
23590 participate in template argument deduction, that P is not used to
23591 determine the ordering. */
23592 if (!uses_deducible_template_parms (arg1)
23593 && !uses_deducible_template_parms (arg2))
23594 goto next;
23595
23596 if (TYPE_REF_P (arg1))
23597 {
23598 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23599 arg1 = TREE_TYPE (arg1);
23600 quals1 = cp_type_quals (arg1);
23601 }
23602
23603 if (TYPE_REF_P (arg2))
23604 {
23605 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23606 arg2 = TREE_TYPE (arg2);
23607 quals2 = cp_type_quals (arg2);
23608 }
23609
23610 arg1 = TYPE_MAIN_VARIANT (arg1);
23611 arg2 = TYPE_MAIN_VARIANT (arg2);
23612
23613 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23614 {
23615 int i, len2 = remaining_arguments (args2);
23616 tree parmvec = make_tree_vec (1);
23617 tree argvec = make_tree_vec (len2);
23618 tree ta = args2;
23619
23620 /* Setup the parameter vector, which contains only ARG1. */
23621 TREE_VEC_ELT (parmvec, 0) = arg1;
23622
23623 /* Setup the argument vector, which contains the remaining
23624 arguments. */
23625 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23626 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23627
23628 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23629 argvec, DEDUCE_EXACT,
23630 /*subr=*/true, /*explain_p=*/false)
23631 == 0);
23632
23633 /* We cannot deduce in the other direction, because ARG1 is
23634 a pack expansion but ARG2 is not. */
23635 deduce2 = 0;
23636 }
23637 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23638 {
23639 int i, len1 = remaining_arguments (args1);
23640 tree parmvec = make_tree_vec (1);
23641 tree argvec = make_tree_vec (len1);
23642 tree ta = args1;
23643
23644 /* Setup the parameter vector, which contains only ARG1. */
23645 TREE_VEC_ELT (parmvec, 0) = arg2;
23646
23647 /* Setup the argument vector, which contains the remaining
23648 arguments. */
23649 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23650 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23651
23652 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23653 argvec, DEDUCE_EXACT,
23654 /*subr=*/true, /*explain_p=*/false)
23655 == 0);
23656
23657 /* We cannot deduce in the other direction, because ARG2 is
23658 a pack expansion but ARG1 is not.*/
23659 deduce1 = 0;
23660 }
23661
23662 else
23663 {
23664 /* The normal case, where neither argument is a pack
23665 expansion. */
23666 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23667 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23668 == 0);
23669 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23670 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23671 == 0);
23672 }
23673
23674 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23675 arg2, then arg2 is not as specialized as arg1. */
23676 if (!deduce1)
23677 lose2 = true;
23678 if (!deduce2)
23679 lose1 = true;
23680
23681 /* "If, for a given type, deduction succeeds in both directions
23682 (i.e., the types are identical after the transformations above)
23683 and both P and A were reference types (before being replaced with
23684 the type referred to above):
23685 - if the type from the argument template was an lvalue reference and
23686 the type from the parameter template was not, the argument type is
23687 considered to be more specialized than the other; otherwise,
23688 - if the type from the argument template is more cv-qualified
23689 than the type from the parameter template (as described above),
23690 the argument type is considered to be more specialized than the other;
23691 otherwise,
23692 - neither type is more specialized than the other." */
23693
23694 if (deduce1 && deduce2)
23695 {
23696 if (ref1 && ref2 && ref1 != ref2)
23697 {
23698 if (ref1 > ref2)
23699 lose1 = true;
23700 else
23701 lose2 = true;
23702 }
23703 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23704 {
23705 if ((quals1 & quals2) == quals2)
23706 lose2 = true;
23707 if ((quals1 & quals2) == quals1)
23708 lose1 = true;
23709 }
23710 }
23711
23712 if (lose1 && lose2)
23713 /* We've failed to deduce something in either direction.
23714 These must be unordered. */
23715 break;
23716
23717 next:
23718
23719 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23720 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23721 /* We have already processed all of the arguments in our
23722 handing of the pack expansion type. */
23723 len = 0;
23724
23725 args1 = TREE_CHAIN (args1);
23726 args2 = TREE_CHAIN (args2);
23727 }
23728
23729 /* "In most cases, all template parameters must have values in order for
23730 deduction to succeed, but for partial ordering purposes a template
23731 parameter may remain without a value provided it is not used in the
23732 types being used for partial ordering."
23733
23734 Thus, if we are missing any of the targs1 we need to substitute into
23735 origs1, then pat2 is not as specialized as pat1. This can happen when
23736 there is a nondeduced context. */
23737 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23738 lose2 = true;
23739 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23740 lose1 = true;
23741
23742 processing_template_decl--;
23743
23744 /* If both deductions succeed, the partial ordering selects the more
23745 constrained template. */
23746 if (!lose1 && !lose2)
23747 {
23748 int winner = more_constrained (decl1, decl2);
23749 if (winner > 0)
23750 lose2 = true;
23751 else if (winner < 0)
23752 lose1 = true;
23753 }
23754
23755 /* All things being equal, if the next argument is a pack expansion
23756 for one function but not for the other, prefer the
23757 non-variadic function. FIXME this is bogus; see c++/41958. */
23758 if (lose1 == lose2
23759 && args1 && TREE_VALUE (args1)
23760 && args2 && TREE_VALUE (args2))
23761 {
23762 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23763 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23764 }
23765
23766 if (lose1 == lose2)
23767 return 0;
23768 else if (!lose1)
23769 return 1;
23770 else
23771 return -1;
23772 }
23773
23774 /* Determine which of two partial specializations of TMPL is more
23775 specialized.
23776
23777 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23778 to the first partial specialization. The TREE_PURPOSE is the
23779 innermost set of template parameters for the partial
23780 specialization. PAT2 is similar, but for the second template.
23781
23782 Return 1 if the first partial specialization is more specialized;
23783 -1 if the second is more specialized; 0 if neither is more
23784 specialized.
23785
23786 See [temp.class.order] for information about determining which of
23787 two templates is more specialized. */
23788
23789 static int
23790 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23791 {
23792 tree targs;
23793 int winner = 0;
23794 bool any_deductions = false;
23795
23796 tree tmpl1 = TREE_VALUE (pat1);
23797 tree tmpl2 = TREE_VALUE (pat2);
23798 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23799 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23800
23801 /* Just like what happens for functions, if we are ordering between
23802 different template specializations, we may encounter dependent
23803 types in the arguments, and we need our dependency check functions
23804 to behave correctly. */
23805 ++processing_template_decl;
23806 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23807 if (targs)
23808 {
23809 --winner;
23810 any_deductions = true;
23811 }
23812
23813 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23814 if (targs)
23815 {
23816 ++winner;
23817 any_deductions = true;
23818 }
23819 --processing_template_decl;
23820
23821 /* If both deductions succeed, the partial ordering selects the more
23822 constrained template. */
23823 if (!winner && any_deductions)
23824 winner = more_constrained (tmpl1, tmpl2);
23825
23826 /* In the case of a tie where at least one of the templates
23827 has a parameter pack at the end, the template with the most
23828 non-packed parameters wins. */
23829 if (winner == 0
23830 && any_deductions
23831 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23832 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23833 {
23834 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23835 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23836 int len1 = TREE_VEC_LENGTH (args1);
23837 int len2 = TREE_VEC_LENGTH (args2);
23838
23839 /* We don't count the pack expansion at the end. */
23840 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23841 --len1;
23842 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23843 --len2;
23844
23845 if (len1 > len2)
23846 return 1;
23847 else if (len1 < len2)
23848 return -1;
23849 }
23850
23851 return winner;
23852 }
23853
23854 /* Return the template arguments that will produce the function signature
23855 DECL from the function template FN, with the explicit template
23856 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23857 also match. Return NULL_TREE if no satisfactory arguments could be
23858 found. */
23859
23860 static tree
23861 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23862 {
23863 int ntparms = DECL_NTPARMS (fn);
23864 tree targs = make_tree_vec (ntparms);
23865 tree decl_type = TREE_TYPE (decl);
23866 tree decl_arg_types;
23867 tree *args;
23868 unsigned int nargs, ix;
23869 tree arg;
23870
23871 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23872
23873 /* Never do unification on the 'this' parameter. */
23874 decl_arg_types = skip_artificial_parms_for (decl,
23875 TYPE_ARG_TYPES (decl_type));
23876
23877 nargs = list_length (decl_arg_types);
23878 args = XALLOCAVEC (tree, nargs);
23879 for (arg = decl_arg_types, ix = 0;
23880 arg != NULL_TREE && arg != void_list_node;
23881 arg = TREE_CHAIN (arg), ++ix)
23882 args[ix] = TREE_VALUE (arg);
23883
23884 if (fn_type_unification (fn, explicit_args, targs,
23885 args, ix,
23886 (check_rettype || DECL_CONV_FN_P (fn)
23887 ? TREE_TYPE (decl_type) : NULL_TREE),
23888 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23889 /*explain_p=*/false,
23890 /*decltype*/false)
23891 == error_mark_node)
23892 return NULL_TREE;
23893
23894 return targs;
23895 }
23896
23897 /* Return the innermost template arguments that, when applied to a partial
23898 specialization SPEC_TMPL of TMPL, yield the ARGS.
23899
23900 For example, suppose we have:
23901
23902 template <class T, class U> struct S {};
23903 template <class T> struct S<T*, int> {};
23904
23905 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23906 partial specialization and the ARGS will be {double*, int}. The resulting
23907 vector will be {double}, indicating that `T' is bound to `double'. */
23908
23909 static tree
23910 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23911 {
23912 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23913 tree spec_args
23914 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23915 int i, ntparms = TREE_VEC_LENGTH (tparms);
23916 tree deduced_args;
23917 tree innermost_deduced_args;
23918
23919 innermost_deduced_args = make_tree_vec (ntparms);
23920 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23921 {
23922 deduced_args = copy_node (args);
23923 SET_TMPL_ARGS_LEVEL (deduced_args,
23924 TMPL_ARGS_DEPTH (deduced_args),
23925 innermost_deduced_args);
23926 }
23927 else
23928 deduced_args = innermost_deduced_args;
23929
23930 bool tried_array_deduction = (cxx_dialect < cxx17);
23931 again:
23932 if (unify (tparms, deduced_args,
23933 INNERMOST_TEMPLATE_ARGS (spec_args),
23934 INNERMOST_TEMPLATE_ARGS (args),
23935 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23936 return NULL_TREE;
23937
23938 for (i = 0; i < ntparms; ++i)
23939 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23940 {
23941 if (!tried_array_deduction)
23942 {
23943 try_array_deduction (tparms, innermost_deduced_args,
23944 INNERMOST_TEMPLATE_ARGS (spec_args));
23945 tried_array_deduction = true;
23946 if (TREE_VEC_ELT (innermost_deduced_args, i))
23947 goto again;
23948 }
23949 return NULL_TREE;
23950 }
23951
23952 if (!push_tinst_level (spec_tmpl, deduced_args))
23953 {
23954 excessive_deduction_depth = true;
23955 return NULL_TREE;
23956 }
23957
23958 /* Verify that nondeduced template arguments agree with the type
23959 obtained from argument deduction.
23960
23961 For example:
23962
23963 struct A { typedef int X; };
23964 template <class T, class U> struct C {};
23965 template <class T> struct C<T, typename T::X> {};
23966
23967 Then with the instantiation `C<A, int>', we can deduce that
23968 `T' is `A' but unify () does not check whether `typename T::X'
23969 is `int'. */
23970 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23971
23972 if (spec_args != error_mark_node)
23973 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23974 INNERMOST_TEMPLATE_ARGS (spec_args),
23975 tmpl, tf_none, false, false);
23976
23977 pop_tinst_level ();
23978
23979 if (spec_args == error_mark_node
23980 /* We only need to check the innermost arguments; the other
23981 arguments will always agree. */
23982 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23983 INNERMOST_TEMPLATE_ARGS (args)))
23984 return NULL_TREE;
23985
23986 /* Now that we have bindings for all of the template arguments,
23987 ensure that the arguments deduced for the template template
23988 parameters have compatible template parameter lists. See the use
23989 of template_template_parm_bindings_ok_p in fn_type_unification
23990 for more information. */
23991 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23992 return NULL_TREE;
23993
23994 return deduced_args;
23995 }
23996
23997 // Compare two function templates T1 and T2 by deducing bindings
23998 // from one against the other. If both deductions succeed, compare
23999 // constraints to see which is more constrained.
24000 static int
24001 more_specialized_inst (tree t1, tree t2)
24002 {
24003 int fate = 0;
24004 int count = 0;
24005
24006 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24007 {
24008 --fate;
24009 ++count;
24010 }
24011
24012 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24013 {
24014 ++fate;
24015 ++count;
24016 }
24017
24018 // If both deductions succeed, then one may be more constrained.
24019 if (count == 2 && fate == 0)
24020 fate = more_constrained (t1, t2);
24021
24022 return fate;
24023 }
24024
24025 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24026 Return the TREE_LIST node with the most specialized template, if
24027 any. If there is no most specialized template, the error_mark_node
24028 is returned.
24029
24030 Note that this function does not look at, or modify, the
24031 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24032 returned is one of the elements of INSTANTIATIONS, callers may
24033 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24034 and retrieve it from the value returned. */
24035
24036 tree
24037 most_specialized_instantiation (tree templates)
24038 {
24039 tree fn, champ;
24040
24041 ++processing_template_decl;
24042
24043 champ = templates;
24044 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24045 {
24046 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24047 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24048 if (fate == -1)
24049 champ = fn;
24050 else if (!fate)
24051 {
24052 /* Equally specialized, move to next function. If there
24053 is no next function, nothing's most specialized. */
24054 fn = TREE_CHAIN (fn);
24055 champ = fn;
24056 if (!fn)
24057 break;
24058 }
24059 }
24060
24061 if (champ)
24062 /* Now verify that champ is better than everything earlier in the
24063 instantiation list. */
24064 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24065 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24066 {
24067 champ = NULL_TREE;
24068 break;
24069 }
24070 }
24071
24072 processing_template_decl--;
24073
24074 if (!champ)
24075 return error_mark_node;
24076
24077 return champ;
24078 }
24079
24080 /* If DECL is a specialization of some template, return the most
24081 general such template. Otherwise, returns NULL_TREE.
24082
24083 For example, given:
24084
24085 template <class T> struct S { template <class U> void f(U); };
24086
24087 if TMPL is `template <class U> void S<int>::f(U)' this will return
24088 the full template. This function will not trace past partial
24089 specializations, however. For example, given in addition:
24090
24091 template <class T> struct S<T*> { template <class U> void f(U); };
24092
24093 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24094 `template <class T> template <class U> S<T*>::f(U)'. */
24095
24096 tree
24097 most_general_template (tree decl)
24098 {
24099 if (TREE_CODE (decl) != TEMPLATE_DECL)
24100 {
24101 if (tree tinfo = get_template_info (decl))
24102 decl = TI_TEMPLATE (tinfo);
24103 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24104 template friend, or a FIELD_DECL for a capture pack. */
24105 if (TREE_CODE (decl) != TEMPLATE_DECL)
24106 return NULL_TREE;
24107 }
24108
24109 /* Look for more and more general templates. */
24110 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24111 {
24112 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24113 (See cp-tree.h for details.) */
24114 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24115 break;
24116
24117 if (CLASS_TYPE_P (TREE_TYPE (decl))
24118 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24119 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24120 break;
24121
24122 /* Stop if we run into an explicitly specialized class template. */
24123 if (!DECL_NAMESPACE_SCOPE_P (decl)
24124 && DECL_CONTEXT (decl)
24125 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24126 break;
24127
24128 decl = DECL_TI_TEMPLATE (decl);
24129 }
24130
24131 return decl;
24132 }
24133
24134 /* Return the most specialized of the template partial specializations
24135 which can produce TARGET, a specialization of some class or variable
24136 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24137 a TEMPLATE_DECL node corresponding to the partial specialization, while
24138 the TREE_PURPOSE is the set of template arguments that must be
24139 substituted into the template pattern in order to generate TARGET.
24140
24141 If the choice of partial specialization is ambiguous, a diagnostic
24142 is issued, and the error_mark_node is returned. If there are no
24143 partial specializations matching TARGET, then NULL_TREE is
24144 returned, indicating that the primary template should be used. */
24145
24146 static tree
24147 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24148 {
24149 tree list = NULL_TREE;
24150 tree t;
24151 tree champ;
24152 int fate;
24153 bool ambiguous_p;
24154 tree outer_args = NULL_TREE;
24155 tree tmpl, args;
24156
24157 if (TYPE_P (target))
24158 {
24159 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24160 tmpl = TI_TEMPLATE (tinfo);
24161 args = TI_ARGS (tinfo);
24162 }
24163 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24164 {
24165 tmpl = TREE_OPERAND (target, 0);
24166 args = TREE_OPERAND (target, 1);
24167 }
24168 else if (VAR_P (target))
24169 {
24170 tree tinfo = DECL_TEMPLATE_INFO (target);
24171 tmpl = TI_TEMPLATE (tinfo);
24172 args = TI_ARGS (tinfo);
24173 }
24174 else
24175 gcc_unreachable ();
24176
24177 tree main_tmpl = most_general_template (tmpl);
24178
24179 /* For determining which partial specialization to use, only the
24180 innermost args are interesting. */
24181 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24182 {
24183 outer_args = strip_innermost_template_args (args, 1);
24184 args = INNERMOST_TEMPLATE_ARGS (args);
24185 }
24186
24187 /* The caller hasn't called push_to_top_level yet, but we need
24188 get_partial_spec_bindings to be done in non-template context so that we'll
24189 fully resolve everything. */
24190 processing_template_decl_sentinel ptds;
24191
24192 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
24193 {
24194 tree spec_args;
24195 tree spec_tmpl = TREE_VALUE (t);
24196
24197 if (outer_args)
24198 {
24199 /* Substitute in the template args from the enclosing class. */
24200 ++processing_template_decl;
24201 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
24202 --processing_template_decl;
24203 }
24204
24205 if (spec_tmpl == error_mark_node)
24206 return error_mark_node;
24207
24208 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
24209 if (spec_args)
24210 {
24211 if (outer_args)
24212 spec_args = add_to_template_args (outer_args, spec_args);
24213
24214 /* Keep the candidate only if the constraints are satisfied,
24215 or if we're not compiling with concepts. */
24216 if (!flag_concepts
24217 || constraints_satisfied_p (spec_tmpl, spec_args))
24218 {
24219 list = tree_cons (spec_args, TREE_VALUE (t), list);
24220 TREE_TYPE (list) = TREE_TYPE (t);
24221 }
24222 }
24223 }
24224
24225 if (! list)
24226 return NULL_TREE;
24227
24228 ambiguous_p = false;
24229 t = list;
24230 champ = t;
24231 t = TREE_CHAIN (t);
24232 for (; t; t = TREE_CHAIN (t))
24233 {
24234 fate = more_specialized_partial_spec (tmpl, champ, t);
24235 if (fate == 1)
24236 ;
24237 else
24238 {
24239 if (fate == 0)
24240 {
24241 t = TREE_CHAIN (t);
24242 if (! t)
24243 {
24244 ambiguous_p = true;
24245 break;
24246 }
24247 }
24248 champ = t;
24249 }
24250 }
24251
24252 if (!ambiguous_p)
24253 for (t = list; t && t != champ; t = TREE_CHAIN (t))
24254 {
24255 fate = more_specialized_partial_spec (tmpl, champ, t);
24256 if (fate != 1)
24257 {
24258 ambiguous_p = true;
24259 break;
24260 }
24261 }
24262
24263 if (ambiguous_p)
24264 {
24265 const char *str;
24266 char *spaces = NULL;
24267 if (!(complain & tf_error))
24268 return error_mark_node;
24269 if (TYPE_P (target))
24270 error ("ambiguous template instantiation for %q#T", target);
24271 else
24272 error ("ambiguous template instantiation for %q#D", target);
24273 str = ngettext ("candidate is:", "candidates are:", list_length (list));
24274 for (t = list; t; t = TREE_CHAIN (t))
24275 {
24276 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
24277 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
24278 "%s %#qS", spaces ? spaces : str, subst);
24279 spaces = spaces ? spaces : get_spaces (str);
24280 }
24281 free (spaces);
24282 return error_mark_node;
24283 }
24284
24285 return champ;
24286 }
24287
24288 /* Explicitly instantiate DECL. */
24289
24290 void
24291 do_decl_instantiation (tree decl, tree storage)
24292 {
24293 tree result = NULL_TREE;
24294 int extern_p = 0;
24295
24296 if (!decl || decl == error_mark_node)
24297 /* An error occurred, for which grokdeclarator has already issued
24298 an appropriate message. */
24299 return;
24300 else if (! DECL_LANG_SPECIFIC (decl))
24301 {
24302 error ("explicit instantiation of non-template %q#D", decl);
24303 return;
24304 }
24305 else if (DECL_DECLARED_CONCEPT_P (decl))
24306 {
24307 if (VAR_P (decl))
24308 error ("explicit instantiation of variable concept %q#D", decl);
24309 else
24310 error ("explicit instantiation of function concept %q#D", decl);
24311 return;
24312 }
24313
24314 bool var_templ = (DECL_TEMPLATE_INFO (decl)
24315 && variable_template_p (DECL_TI_TEMPLATE (decl)));
24316
24317 if (VAR_P (decl) && !var_templ)
24318 {
24319 /* There is an asymmetry here in the way VAR_DECLs and
24320 FUNCTION_DECLs are handled by grokdeclarator. In the case of
24321 the latter, the DECL we get back will be marked as a
24322 template instantiation, and the appropriate
24323 DECL_TEMPLATE_INFO will be set up. This does not happen for
24324 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
24325 should handle VAR_DECLs as it currently handles
24326 FUNCTION_DECLs. */
24327 if (!DECL_CLASS_SCOPE_P (decl))
24328 {
24329 error ("%qD is not a static data member of a class template", decl);
24330 return;
24331 }
24332 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
24333 if (!result || !VAR_P (result))
24334 {
24335 error ("no matching template for %qD found", decl);
24336 return;
24337 }
24338 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
24339 {
24340 error ("type %qT for explicit instantiation %qD does not match "
24341 "declared type %qT", TREE_TYPE (result), decl,
24342 TREE_TYPE (decl));
24343 return;
24344 }
24345 }
24346 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
24347 {
24348 error ("explicit instantiation of %q#D", decl);
24349 return;
24350 }
24351 else
24352 result = decl;
24353
24354 /* Check for various error cases. Note that if the explicit
24355 instantiation is valid the RESULT will currently be marked as an
24356 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
24357 until we get here. */
24358
24359 if (DECL_TEMPLATE_SPECIALIZATION (result))
24360 {
24361 /* DR 259 [temp.spec].
24362
24363 Both an explicit instantiation and a declaration of an explicit
24364 specialization shall not appear in a program unless the explicit
24365 instantiation follows a declaration of the explicit specialization.
24366
24367 For a given set of template parameters, if an explicit
24368 instantiation of a template appears after a declaration of an
24369 explicit specialization for that template, the explicit
24370 instantiation has no effect. */
24371 return;
24372 }
24373 else if (DECL_EXPLICIT_INSTANTIATION (result))
24374 {
24375 /* [temp.spec]
24376
24377 No program shall explicitly instantiate any template more
24378 than once.
24379
24380 We check DECL_NOT_REALLY_EXTERN so as not to complain when
24381 the first instantiation was `extern' and the second is not,
24382 and EXTERN_P for the opposite case. */
24383 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
24384 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
24385 /* If an "extern" explicit instantiation follows an ordinary
24386 explicit instantiation, the template is instantiated. */
24387 if (extern_p)
24388 return;
24389 }
24390 else if (!DECL_IMPLICIT_INSTANTIATION (result))
24391 {
24392 error ("no matching template for %qD found", result);
24393 return;
24394 }
24395 else if (!DECL_TEMPLATE_INFO (result))
24396 {
24397 permerror (input_location, "explicit instantiation of non-template %q#D", result);
24398 return;
24399 }
24400
24401 if (storage == NULL_TREE)
24402 ;
24403 else if (storage == ridpointers[(int) RID_EXTERN])
24404 {
24405 if (cxx_dialect == cxx98)
24406 pedwarn (input_location, OPT_Wpedantic,
24407 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
24408 "instantiations");
24409 extern_p = 1;
24410 }
24411 else
24412 error ("storage class %qD applied to template instantiation", storage);
24413
24414 check_explicit_instantiation_namespace (result);
24415 mark_decl_instantiated (result, extern_p);
24416 if (! extern_p)
24417 instantiate_decl (result, /*defer_ok=*/true,
24418 /*expl_inst_class_mem_p=*/false);
24419 }
24420
24421 static void
24422 mark_class_instantiated (tree t, int extern_p)
24423 {
24424 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
24425 SET_CLASSTYPE_INTERFACE_KNOWN (t);
24426 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
24427 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
24428 if (! extern_p)
24429 {
24430 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
24431 rest_of_type_compilation (t, 1);
24432 }
24433 }
24434
24435 /* Called from do_type_instantiation through binding_table_foreach to
24436 do recursive instantiation for the type bound in ENTRY. */
24437 static void
24438 bt_instantiate_type_proc (binding_entry entry, void *data)
24439 {
24440 tree storage = *(tree *) data;
24441
24442 if (MAYBE_CLASS_TYPE_P (entry->type)
24443 && CLASSTYPE_TEMPLATE_INFO (entry->type)
24444 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
24445 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
24446 }
24447
24448 /* Perform an explicit instantiation of template class T. STORAGE, if
24449 non-null, is the RID for extern, inline or static. COMPLAIN is
24450 nonzero if this is called from the parser, zero if called recursively,
24451 since the standard is unclear (as detailed below). */
24452
24453 void
24454 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
24455 {
24456 int extern_p = 0;
24457 int nomem_p = 0;
24458 int static_p = 0;
24459 int previous_instantiation_extern_p = 0;
24460
24461 if (TREE_CODE (t) == TYPE_DECL)
24462 t = TREE_TYPE (t);
24463
24464 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
24465 {
24466 tree tmpl =
24467 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
24468 if (tmpl)
24469 error ("explicit instantiation of non-class template %qD", tmpl);
24470 else
24471 error ("explicit instantiation of non-template type %qT", t);
24472 return;
24473 }
24474
24475 complete_type (t);
24476
24477 if (!COMPLETE_TYPE_P (t))
24478 {
24479 if (complain & tf_error)
24480 error ("explicit instantiation of %q#T before definition of template",
24481 t);
24482 return;
24483 }
24484
24485 if (storage != NULL_TREE)
24486 {
24487 if (storage == ridpointers[(int) RID_EXTERN])
24488 {
24489 if (cxx_dialect == cxx98)
24490 pedwarn (input_location, OPT_Wpedantic,
24491 "ISO C++ 1998 forbids the use of %<extern%> on "
24492 "explicit instantiations");
24493 }
24494 else
24495 pedwarn (input_location, OPT_Wpedantic,
24496 "ISO C++ forbids the use of %qE"
24497 " on explicit instantiations", storage);
24498
24499 if (storage == ridpointers[(int) RID_INLINE])
24500 nomem_p = 1;
24501 else if (storage == ridpointers[(int) RID_EXTERN])
24502 extern_p = 1;
24503 else if (storage == ridpointers[(int) RID_STATIC])
24504 static_p = 1;
24505 else
24506 {
24507 error ("storage class %qD applied to template instantiation",
24508 storage);
24509 extern_p = 0;
24510 }
24511 }
24512
24513 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
24514 {
24515 /* DR 259 [temp.spec].
24516
24517 Both an explicit instantiation and a declaration of an explicit
24518 specialization shall not appear in a program unless the explicit
24519 instantiation follows a declaration of the explicit specialization.
24520
24521 For a given set of template parameters, if an explicit
24522 instantiation of a template appears after a declaration of an
24523 explicit specialization for that template, the explicit
24524 instantiation has no effect. */
24525 return;
24526 }
24527 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24528 {
24529 /* [temp.spec]
24530
24531 No program shall explicitly instantiate any template more
24532 than once.
24533
24534 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24535 instantiation was `extern'. If EXTERN_P then the second is.
24536 These cases are OK. */
24537 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24538
24539 if (!previous_instantiation_extern_p && !extern_p
24540 && (complain & tf_error))
24541 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24542
24543 /* If we've already instantiated the template, just return now. */
24544 if (!CLASSTYPE_INTERFACE_ONLY (t))
24545 return;
24546 }
24547
24548 check_explicit_instantiation_namespace (TYPE_NAME (t));
24549 mark_class_instantiated (t, extern_p);
24550
24551 if (nomem_p)
24552 return;
24553
24554 /* In contrast to implicit instantiation, where only the
24555 declarations, and not the definitions, of members are
24556 instantiated, we have here:
24557
24558 [temp.explicit]
24559
24560 The explicit instantiation of a class template specialization
24561 implies the instantiation of all of its members not
24562 previously explicitly specialized in the translation unit
24563 containing the explicit instantiation.
24564
24565 Of course, we can't instantiate member template classes, since we
24566 don't have any arguments for them. Note that the standard is
24567 unclear on whether the instantiation of the members are
24568 *explicit* instantiations or not. However, the most natural
24569 interpretation is that it should be an explicit
24570 instantiation. */
24571 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24572 if ((VAR_P (fld)
24573 || (TREE_CODE (fld) == FUNCTION_DECL
24574 && !static_p
24575 && user_provided_p (fld)))
24576 && DECL_TEMPLATE_INSTANTIATION (fld))
24577 {
24578 mark_decl_instantiated (fld, extern_p);
24579 if (! extern_p)
24580 instantiate_decl (fld, /*defer_ok=*/true,
24581 /*expl_inst_class_mem_p=*/true);
24582 }
24583
24584 if (CLASSTYPE_NESTED_UTDS (t))
24585 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24586 bt_instantiate_type_proc, &storage);
24587 }
24588
24589 /* Given a function DECL, which is a specialization of TMPL, modify
24590 DECL to be a re-instantiation of TMPL with the same template
24591 arguments. TMPL should be the template into which tsubst'ing
24592 should occur for DECL, not the most general template.
24593
24594 One reason for doing this is a scenario like this:
24595
24596 template <class T>
24597 void f(const T&, int i);
24598
24599 void g() { f(3, 7); }
24600
24601 template <class T>
24602 void f(const T& t, const int i) { }
24603
24604 Note that when the template is first instantiated, with
24605 instantiate_template, the resulting DECL will have no name for the
24606 first parameter, and the wrong type for the second. So, when we go
24607 to instantiate the DECL, we regenerate it. */
24608
24609 static void
24610 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24611 {
24612 /* The arguments used to instantiate DECL, from the most general
24613 template. */
24614 tree code_pattern;
24615
24616 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24617
24618 /* Make sure that we can see identifiers, and compute access
24619 correctly. */
24620 push_access_scope (decl);
24621
24622 if (TREE_CODE (decl) == FUNCTION_DECL)
24623 {
24624 tree decl_parm;
24625 tree pattern_parm;
24626 tree specs;
24627 int args_depth;
24628 int parms_depth;
24629
24630 args_depth = TMPL_ARGS_DEPTH (args);
24631 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24632 if (args_depth > parms_depth)
24633 args = get_innermost_template_args (args, parms_depth);
24634
24635 /* Instantiate a dynamic exception-specification. noexcept will be
24636 handled below. */
24637 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24638 if (TREE_VALUE (raises))
24639 {
24640 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24641 args, tf_error, NULL_TREE,
24642 /*defer_ok*/false);
24643 if (specs && specs != error_mark_node)
24644 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24645 specs);
24646 }
24647
24648 /* Merge parameter declarations. */
24649 decl_parm = skip_artificial_parms_for (decl,
24650 DECL_ARGUMENTS (decl));
24651 pattern_parm
24652 = skip_artificial_parms_for (code_pattern,
24653 DECL_ARGUMENTS (code_pattern));
24654 while (decl_parm && !DECL_PACK_P (pattern_parm))
24655 {
24656 tree parm_type;
24657 tree attributes;
24658
24659 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24660 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24661 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24662 NULL_TREE);
24663 parm_type = type_decays_to (parm_type);
24664 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24665 TREE_TYPE (decl_parm) = parm_type;
24666 attributes = DECL_ATTRIBUTES (pattern_parm);
24667 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24668 {
24669 DECL_ATTRIBUTES (decl_parm) = attributes;
24670 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24671 }
24672 decl_parm = DECL_CHAIN (decl_parm);
24673 pattern_parm = DECL_CHAIN (pattern_parm);
24674 }
24675 /* Merge any parameters that match with the function parameter
24676 pack. */
24677 if (pattern_parm && DECL_PACK_P (pattern_parm))
24678 {
24679 int i, len;
24680 tree expanded_types;
24681 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24682 the parameters in this function parameter pack. */
24683 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24684 args, tf_error, NULL_TREE);
24685 len = TREE_VEC_LENGTH (expanded_types);
24686 for (i = 0; i < len; i++)
24687 {
24688 tree parm_type;
24689 tree attributes;
24690
24691 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24692 /* Rename the parameter to include the index. */
24693 DECL_NAME (decl_parm) =
24694 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24695 parm_type = TREE_VEC_ELT (expanded_types, i);
24696 parm_type = type_decays_to (parm_type);
24697 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24698 TREE_TYPE (decl_parm) = parm_type;
24699 attributes = DECL_ATTRIBUTES (pattern_parm);
24700 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24701 {
24702 DECL_ATTRIBUTES (decl_parm) = attributes;
24703 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24704 }
24705 decl_parm = DECL_CHAIN (decl_parm);
24706 }
24707 }
24708 /* Merge additional specifiers from the CODE_PATTERN. */
24709 if (DECL_DECLARED_INLINE_P (code_pattern)
24710 && !DECL_DECLARED_INLINE_P (decl))
24711 DECL_DECLARED_INLINE_P (decl) = 1;
24712
24713 maybe_instantiate_noexcept (decl, tf_error);
24714 }
24715 else if (VAR_P (decl))
24716 {
24717 start_lambda_scope (decl);
24718 DECL_INITIAL (decl) =
24719 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24720 tf_error, DECL_TI_TEMPLATE (decl));
24721 finish_lambda_scope ();
24722 if (VAR_HAD_UNKNOWN_BOUND (decl))
24723 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24724 tf_error, DECL_TI_TEMPLATE (decl));
24725 }
24726 else
24727 gcc_unreachable ();
24728
24729 pop_access_scope (decl);
24730 }
24731
24732 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24733 substituted to get DECL. */
24734
24735 tree
24736 template_for_substitution (tree decl)
24737 {
24738 tree tmpl = DECL_TI_TEMPLATE (decl);
24739
24740 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24741 for the instantiation. This is not always the most general
24742 template. Consider, for example:
24743
24744 template <class T>
24745 struct S { template <class U> void f();
24746 template <> void f<int>(); };
24747
24748 and an instantiation of S<double>::f<int>. We want TD to be the
24749 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24750 while (/* An instantiation cannot have a definition, so we need a
24751 more general template. */
24752 DECL_TEMPLATE_INSTANTIATION (tmpl)
24753 /* We must also deal with friend templates. Given:
24754
24755 template <class T> struct S {
24756 template <class U> friend void f() {};
24757 };
24758
24759 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24760 so far as the language is concerned, but that's still
24761 where we get the pattern for the instantiation from. On
24762 other hand, if the definition comes outside the class, say:
24763
24764 template <class T> struct S {
24765 template <class U> friend void f();
24766 };
24767 template <class U> friend void f() {}
24768
24769 we don't need to look any further. That's what the check for
24770 DECL_INITIAL is for. */
24771 || (TREE_CODE (decl) == FUNCTION_DECL
24772 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24773 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24774 {
24775 /* The present template, TD, should not be a definition. If it
24776 were a definition, we should be using it! Note that we
24777 cannot restructure the loop to just keep going until we find
24778 a template with a definition, since that might go too far if
24779 a specialization was declared, but not defined. */
24780
24781 /* Fetch the more general template. */
24782 tmpl = DECL_TI_TEMPLATE (tmpl);
24783 }
24784
24785 return tmpl;
24786 }
24787
24788 /* Returns true if we need to instantiate this template instance even if we
24789 know we aren't going to emit it. */
24790
24791 bool
24792 always_instantiate_p (tree decl)
24793 {
24794 /* We always instantiate inline functions so that we can inline them. An
24795 explicit instantiation declaration prohibits implicit instantiation of
24796 non-inline functions. With high levels of optimization, we would
24797 normally inline non-inline functions -- but we're not allowed to do
24798 that for "extern template" functions. Therefore, we check
24799 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24800 return ((TREE_CODE (decl) == FUNCTION_DECL
24801 && (DECL_DECLARED_INLINE_P (decl)
24802 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24803 /* And we need to instantiate static data members so that
24804 their initializers are available in integral constant
24805 expressions. */
24806 || (VAR_P (decl)
24807 && decl_maybe_constant_var_p (decl)));
24808 }
24809
24810 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24811 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24812 error, true otherwise. */
24813
24814 bool
24815 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24816 {
24817 tree fntype, spec, noex, clone;
24818
24819 /* Don't instantiate a noexcept-specification from template context. */
24820 if (processing_template_decl
24821 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24822 return true;
24823
24824 if (DECL_CLONED_FUNCTION_P (fn))
24825 fn = DECL_CLONED_FUNCTION (fn);
24826
24827 tree orig_fn = NULL_TREE;
24828 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24829 its FUNCTION_DECL for the rest of this function -- push_access_scope
24830 doesn't accept TEMPLATE_DECLs. */
24831 if (DECL_FUNCTION_TEMPLATE_P (fn))
24832 {
24833 orig_fn = fn;
24834 fn = DECL_TEMPLATE_RESULT (fn);
24835 }
24836
24837 fntype = TREE_TYPE (fn);
24838 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24839
24840 if (!spec || !TREE_PURPOSE (spec))
24841 return true;
24842
24843 noex = TREE_PURPOSE (spec);
24844
24845 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24846 {
24847 static hash_set<tree>* fns = new hash_set<tree>;
24848 bool added = false;
24849 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24850 {
24851 spec = get_defaulted_eh_spec (fn, complain);
24852 if (spec == error_mark_node)
24853 /* This might have failed because of an unparsed DMI, so
24854 let's try again later. */
24855 return false;
24856 }
24857 else if (!(added = !fns->add (fn)))
24858 {
24859 /* If hash_set::add returns true, the element was already there. */
24860 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24861 DECL_SOURCE_LOCATION (fn));
24862 error_at (loc,
24863 "exception specification of %qD depends on itself",
24864 fn);
24865 spec = noexcept_false_spec;
24866 }
24867 else if (push_tinst_level (fn))
24868 {
24869 push_to_top_level ();
24870 push_access_scope (fn);
24871 push_deferring_access_checks (dk_no_deferred);
24872 input_location = DECL_SOURCE_LOCATION (fn);
24873
24874 /* If needed, set current_class_ptr for the benefit of
24875 tsubst_copy/PARM_DECL. */
24876 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24877 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24878 {
24879 tree this_parm = DECL_ARGUMENTS (tdecl);
24880 current_class_ptr = NULL_TREE;
24881 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24882 current_class_ptr = this_parm;
24883 }
24884
24885 /* If this function is represented by a TEMPLATE_DECL, then
24886 the deferred noexcept-specification might still contain
24887 dependent types, even after substitution. And we need the
24888 dependency check functions to work in build_noexcept_spec. */
24889 if (orig_fn)
24890 ++processing_template_decl;
24891
24892 /* Do deferred instantiation of the noexcept-specifier. */
24893 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24894 DEFERRED_NOEXCEPT_ARGS (noex),
24895 tf_warning_or_error, fn,
24896 /*function_p=*/false,
24897 /*i_c_e_p=*/true);
24898
24899 /* Build up the noexcept-specification. */
24900 spec = build_noexcept_spec (noex, tf_warning_or_error);
24901
24902 if (orig_fn)
24903 --processing_template_decl;
24904
24905 pop_deferring_access_checks ();
24906 pop_access_scope (fn);
24907 pop_tinst_level ();
24908 pop_from_top_level ();
24909 }
24910 else
24911 spec = noexcept_false_spec;
24912
24913 if (added)
24914 fns->remove (fn);
24915
24916 if (spec == error_mark_node)
24917 {
24918 /* This failed with a hard error, so let's go with false. */
24919 gcc_assert (seen_error ());
24920 spec = noexcept_false_spec;
24921 }
24922
24923 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24924 if (orig_fn)
24925 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24926
24927 FOR_EACH_CLONE (clone, fn)
24928 {
24929 if (TREE_TYPE (clone) == fntype)
24930 TREE_TYPE (clone) = TREE_TYPE (fn);
24931 else
24932 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24933 }
24934 }
24935
24936 return true;
24937 }
24938
24939 /* We're starting to process the function INST, an instantiation of PATTERN;
24940 add their parameters to local_specializations. */
24941
24942 static void
24943 register_parameter_specializations (tree pattern, tree inst)
24944 {
24945 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24946 tree spec_parm = DECL_ARGUMENTS (inst);
24947 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24948 {
24949 register_local_specialization (spec_parm, tmpl_parm);
24950 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24951 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24952 }
24953 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24954 {
24955 if (!DECL_PACK_P (tmpl_parm))
24956 {
24957 register_local_specialization (spec_parm, tmpl_parm);
24958 spec_parm = DECL_CHAIN (spec_parm);
24959 }
24960 else
24961 {
24962 /* Register the (value) argument pack as a specialization of
24963 TMPL_PARM, then move on. */
24964 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24965 register_local_specialization (argpack, tmpl_parm);
24966 }
24967 }
24968 gcc_assert (!spec_parm);
24969 }
24970
24971 /* Produce the definition of D, a _DECL generated from a template. If
24972 DEFER_OK is true, then we don't have to actually do the
24973 instantiation now; we just have to do it sometime. Normally it is
24974 an error if this is an explicit instantiation but D is undefined.
24975 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24976 instantiated class template. */
24977
24978 tree
24979 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24980 {
24981 tree tmpl = DECL_TI_TEMPLATE (d);
24982 tree gen_args;
24983 tree args;
24984 tree td;
24985 tree code_pattern;
24986 tree spec;
24987 tree gen_tmpl;
24988 bool pattern_defined;
24989 location_t saved_loc = input_location;
24990 int saved_unevaluated_operand = cp_unevaluated_operand;
24991 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24992 bool external_p;
24993 bool deleted_p;
24994
24995 /* This function should only be used to instantiate templates for
24996 functions and static member variables. */
24997 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24998
24999 /* A concept is never instantiated. */
25000 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25001
25002 /* Variables are never deferred; if instantiation is required, they
25003 are instantiated right away. That allows for better code in the
25004 case that an expression refers to the value of the variable --
25005 if the variable has a constant value the referring expression can
25006 take advantage of that fact. */
25007 if (VAR_P (d))
25008 defer_ok = false;
25009
25010 /* Don't instantiate cloned functions. Instead, instantiate the
25011 functions they cloned. */
25012 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25013 d = DECL_CLONED_FUNCTION (d);
25014
25015 if (DECL_TEMPLATE_INSTANTIATED (d)
25016 || (TREE_CODE (d) == FUNCTION_DECL
25017 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25018 || DECL_TEMPLATE_SPECIALIZATION (d))
25019 /* D has already been instantiated or explicitly specialized, so
25020 there's nothing for us to do here.
25021
25022 It might seem reasonable to check whether or not D is an explicit
25023 instantiation, and, if so, stop here. But when an explicit
25024 instantiation is deferred until the end of the compilation,
25025 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25026 the instantiation. */
25027 return d;
25028
25029 /* Check to see whether we know that this template will be
25030 instantiated in some other file, as with "extern template"
25031 extension. */
25032 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25033
25034 /* In general, we do not instantiate such templates. */
25035 if (external_p && !always_instantiate_p (d))
25036 return d;
25037
25038 gen_tmpl = most_general_template (tmpl);
25039 gen_args = DECL_TI_ARGS (d);
25040
25041 if (tmpl != gen_tmpl)
25042 /* We should already have the extra args. */
25043 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25044 == TMPL_ARGS_DEPTH (gen_args));
25045 /* And what's in the hash table should match D. */
25046 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
25047 || spec == NULL_TREE);
25048
25049 /* This needs to happen before any tsubsting. */
25050 if (! push_tinst_level (d))
25051 return d;
25052
25053 timevar_push (TV_TEMPLATE_INST);
25054
25055 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25056 for the instantiation. */
25057 td = template_for_substitution (d);
25058 args = gen_args;
25059
25060 if (VAR_P (d))
25061 {
25062 /* Look up an explicit specialization, if any. */
25063 tree tid = lookup_template_variable (gen_tmpl, gen_args);
25064 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
25065 if (elt && elt != error_mark_node)
25066 {
25067 td = TREE_VALUE (elt);
25068 args = TREE_PURPOSE (elt);
25069 }
25070 }
25071
25072 code_pattern = DECL_TEMPLATE_RESULT (td);
25073
25074 /* We should never be trying to instantiate a member of a class
25075 template or partial specialization. */
25076 gcc_assert (d != code_pattern);
25077
25078 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
25079 || DECL_TEMPLATE_SPECIALIZATION (td))
25080 /* In the case of a friend template whose definition is provided
25081 outside the class, we may have too many arguments. Drop the
25082 ones we don't need. The same is true for specializations. */
25083 args = get_innermost_template_args
25084 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
25085
25086 if (TREE_CODE (d) == FUNCTION_DECL)
25087 {
25088 deleted_p = DECL_DELETED_FN (code_pattern);
25089 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
25090 && DECL_INITIAL (code_pattern) != error_mark_node)
25091 || DECL_DEFAULTED_FN (code_pattern)
25092 || deleted_p);
25093 }
25094 else
25095 {
25096 deleted_p = false;
25097 if (DECL_CLASS_SCOPE_P (code_pattern))
25098 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
25099 else
25100 pattern_defined = ! DECL_EXTERNAL (code_pattern);
25101 }
25102
25103 /* We may be in the middle of deferred access check. Disable it now. */
25104 push_deferring_access_checks (dk_no_deferred);
25105
25106 /* Unless an explicit instantiation directive has already determined
25107 the linkage of D, remember that a definition is available for
25108 this entity. */
25109 if (pattern_defined
25110 && !DECL_INTERFACE_KNOWN (d)
25111 && !DECL_NOT_REALLY_EXTERN (d))
25112 mark_definable (d);
25113
25114 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
25115 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
25116 input_location = DECL_SOURCE_LOCATION (d);
25117
25118 /* If D is a member of an explicitly instantiated class template,
25119 and no definition is available, treat it like an implicit
25120 instantiation. */
25121 if (!pattern_defined && expl_inst_class_mem_p
25122 && DECL_EXPLICIT_INSTANTIATION (d))
25123 {
25124 /* Leave linkage flags alone on instantiations with anonymous
25125 visibility. */
25126 if (TREE_PUBLIC (d))
25127 {
25128 DECL_NOT_REALLY_EXTERN (d) = 0;
25129 DECL_INTERFACE_KNOWN (d) = 0;
25130 }
25131 SET_DECL_IMPLICIT_INSTANTIATION (d);
25132 }
25133
25134 /* Defer all other templates, unless we have been explicitly
25135 forbidden from doing so. */
25136 if (/* If there is no definition, we cannot instantiate the
25137 template. */
25138 ! pattern_defined
25139 /* If it's OK to postpone instantiation, do so. */
25140 || defer_ok
25141 /* If this is a static data member that will be defined
25142 elsewhere, we don't want to instantiate the entire data
25143 member, but we do want to instantiate the initializer so that
25144 we can substitute that elsewhere. */
25145 || (external_p && VAR_P (d))
25146 /* Handle here a deleted function too, avoid generating
25147 its body (c++/61080). */
25148 || deleted_p)
25149 {
25150 /* The definition of the static data member is now required so
25151 we must substitute the initializer. */
25152 if (VAR_P (d)
25153 && !DECL_INITIAL (d)
25154 && DECL_INITIAL (code_pattern))
25155 {
25156 tree ns;
25157 tree init;
25158 bool const_init = false;
25159 bool enter_context = DECL_CLASS_SCOPE_P (d);
25160
25161 ns = decl_namespace_context (d);
25162 push_nested_namespace (ns);
25163 if (enter_context)
25164 push_nested_class (DECL_CONTEXT (d));
25165 init = tsubst_expr (DECL_INITIAL (code_pattern),
25166 args,
25167 tf_warning_or_error, NULL_TREE,
25168 /*integral_constant_expression_p=*/false);
25169 /* If instantiating the initializer involved instantiating this
25170 again, don't call cp_finish_decl twice. */
25171 if (!DECL_INITIAL (d))
25172 {
25173 /* Make sure the initializer is still constant, in case of
25174 circular dependency (template/instantiate6.C). */
25175 const_init
25176 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25177 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
25178 /*asmspec_tree=*/NULL_TREE,
25179 LOOKUP_ONLYCONVERTING);
25180 }
25181 if (enter_context)
25182 pop_nested_class ();
25183 pop_nested_namespace (ns);
25184 }
25185
25186 /* We restore the source position here because it's used by
25187 add_pending_template. */
25188 input_location = saved_loc;
25189
25190 if (at_eof && !pattern_defined
25191 && DECL_EXPLICIT_INSTANTIATION (d)
25192 && DECL_NOT_REALLY_EXTERN (d))
25193 /* [temp.explicit]
25194
25195 The definition of a non-exported function template, a
25196 non-exported member function template, or a non-exported
25197 member function or static data member of a class template
25198 shall be present in every translation unit in which it is
25199 explicitly instantiated. */
25200 permerror (input_location, "explicit instantiation of %qD "
25201 "but no definition available", d);
25202
25203 /* If we're in unevaluated context, we just wanted to get the
25204 constant value; this isn't an odr use, so don't queue
25205 a full instantiation. */
25206 if (cp_unevaluated_operand != 0)
25207 goto out;
25208 /* ??? Historically, we have instantiated inline functions, even
25209 when marked as "extern template". */
25210 if (!(external_p && VAR_P (d)))
25211 add_pending_template (d);
25212 goto out;
25213 }
25214
25215 bool push_to_top, nested;
25216 tree fn_context;
25217 fn_context = decl_function_context (d);
25218 if (LAMBDA_FUNCTION_P (d))
25219 /* tsubst_lambda_expr resolved any references to enclosing functions. */
25220 fn_context = NULL_TREE;
25221 nested = current_function_decl != NULL_TREE;
25222 push_to_top = !(nested && fn_context == current_function_decl);
25223
25224 vec<tree> omp_privatization_save;
25225 if (nested)
25226 save_omp_privatization_clauses (omp_privatization_save);
25227
25228 if (push_to_top)
25229 push_to_top_level ();
25230 else
25231 {
25232 gcc_assert (!processing_template_decl);
25233 push_function_context ();
25234 cp_unevaluated_operand = 0;
25235 c_inhibit_evaluation_warnings = 0;
25236 }
25237
25238 /* Mark D as instantiated so that recursive calls to
25239 instantiate_decl do not try to instantiate it again. */
25240 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25241
25242 /* Regenerate the declaration in case the template has been modified
25243 by a subsequent redeclaration. */
25244 regenerate_decl_from_template (d, td, args);
25245
25246 /* We already set the file and line above. Reset them now in case
25247 they changed as a result of calling regenerate_decl_from_template. */
25248 input_location = DECL_SOURCE_LOCATION (d);
25249
25250 if (VAR_P (d))
25251 {
25252 tree init;
25253 bool const_init = false;
25254
25255 /* Clear out DECL_RTL; whatever was there before may not be right
25256 since we've reset the type of the declaration. */
25257 SET_DECL_RTL (d, NULL);
25258 DECL_IN_AGGR_P (d) = 0;
25259
25260 /* The initializer is placed in DECL_INITIAL by
25261 regenerate_decl_from_template so we don't need to
25262 push/pop_access_scope again here. Pull it out so that
25263 cp_finish_decl can process it. */
25264 init = DECL_INITIAL (d);
25265 DECL_INITIAL (d) = NULL_TREE;
25266 DECL_INITIALIZED_P (d) = 0;
25267
25268 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25269 initializer. That function will defer actual emission until
25270 we have a chance to determine linkage. */
25271 DECL_EXTERNAL (d) = 0;
25272
25273 /* Enter the scope of D so that access-checking works correctly. */
25274 bool enter_context = DECL_CLASS_SCOPE_P (d);
25275 if (enter_context)
25276 push_nested_class (DECL_CONTEXT (d));
25277
25278 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25279 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
25280 ? LOOKUP_CONSTINIT : 0);
25281 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
25282
25283 if (enter_context)
25284 pop_nested_class ();
25285
25286 if (variable_template_p (gen_tmpl))
25287 note_variable_template_instantiation (d);
25288 }
25289 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25290 synthesize_method (d);
25291 else if (TREE_CODE (d) == FUNCTION_DECL)
25292 {
25293 /* Set up the list of local specializations. */
25294 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25295 tree block = NULL_TREE;
25296
25297 /* Set up context. */
25298 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25299 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25300 block = push_stmt_list ();
25301 else
25302 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25303
25304 /* Some typedefs referenced from within the template code need to be
25305 access checked at template instantiation time, i.e now. These
25306 types were added to the template at parsing time. Let's get those
25307 and perform the access checks then. */
25308 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
25309 args);
25310
25311 /* Create substitution entries for the parameters. */
25312 register_parameter_specializations (code_pattern, d);
25313
25314 /* Substitute into the body of the function. */
25315 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25316 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25317 tf_warning_or_error, tmpl);
25318 else
25319 {
25320 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25321 tf_warning_or_error, tmpl,
25322 /*integral_constant_expression_p=*/false);
25323
25324 /* Set the current input_location to the end of the function
25325 so that finish_function knows where we are. */
25326 input_location
25327 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25328
25329 /* Remember if we saw an infinite loop in the template. */
25330 current_function_infinite_loop
25331 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25332 }
25333
25334 /* Finish the function. */
25335 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
25336 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
25337 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25338 else
25339 {
25340 d = finish_function (/*inline_p=*/false);
25341 expand_or_defer_fn (d);
25342 }
25343
25344 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25345 cp_check_omp_declare_reduction (d);
25346 }
25347
25348 /* We're not deferring instantiation any more. */
25349 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25350
25351 if (push_to_top)
25352 pop_from_top_level ();
25353 else
25354 pop_function_context ();
25355
25356 if (nested)
25357 restore_omp_privatization_clauses (omp_privatization_save);
25358
25359 out:
25360 pop_deferring_access_checks ();
25361 timevar_pop (TV_TEMPLATE_INST);
25362 pop_tinst_level ();
25363 input_location = saved_loc;
25364 cp_unevaluated_operand = saved_unevaluated_operand;
25365 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
25366
25367 return d;
25368 }
25369
25370 /* Run through the list of templates that we wish we could
25371 instantiate, and instantiate any we can. RETRIES is the
25372 number of times we retry pending template instantiation. */
25373
25374 void
25375 instantiate_pending_templates (int retries)
25376 {
25377 int reconsider;
25378 location_t saved_loc = input_location;
25379
25380 /* Instantiating templates may trigger vtable generation. This in turn
25381 may require further template instantiations. We place a limit here
25382 to avoid infinite loop. */
25383 if (pending_templates && retries >= max_tinst_depth)
25384 {
25385 tree decl = pending_templates->tinst->maybe_get_node ();
25386
25387 fatal_error (input_location,
25388 "template instantiation depth exceeds maximum of %d"
25389 " instantiating %q+D, possibly from virtual table generation"
25390 " (use %<-ftemplate-depth=%> to increase the maximum)",
25391 max_tinst_depth, decl);
25392 if (TREE_CODE (decl) == FUNCTION_DECL)
25393 /* Pretend that we defined it. */
25394 DECL_INITIAL (decl) = error_mark_node;
25395 return;
25396 }
25397
25398 do
25399 {
25400 struct pending_template **t = &pending_templates;
25401 struct pending_template *last = NULL;
25402 reconsider = 0;
25403 while (*t)
25404 {
25405 tree instantiation = reopen_tinst_level ((*t)->tinst);
25406 bool complete = false;
25407
25408 if (TYPE_P (instantiation))
25409 {
25410 if (!COMPLETE_TYPE_P (instantiation))
25411 {
25412 instantiate_class_template (instantiation);
25413 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
25414 for (tree fld = TYPE_FIELDS (instantiation);
25415 fld; fld = TREE_CHAIN (fld))
25416 if ((VAR_P (fld)
25417 || (TREE_CODE (fld) == FUNCTION_DECL
25418 && !DECL_ARTIFICIAL (fld)))
25419 && DECL_TEMPLATE_INSTANTIATION (fld))
25420 instantiate_decl (fld,
25421 /*defer_ok=*/false,
25422 /*expl_inst_class_mem_p=*/false);
25423
25424 if (COMPLETE_TYPE_P (instantiation))
25425 reconsider = 1;
25426 }
25427
25428 complete = COMPLETE_TYPE_P (instantiation);
25429 }
25430 else
25431 {
25432 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
25433 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
25434 {
25435 instantiation
25436 = instantiate_decl (instantiation,
25437 /*defer_ok=*/false,
25438 /*expl_inst_class_mem_p=*/false);
25439 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
25440 reconsider = 1;
25441 }
25442
25443 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
25444 || DECL_TEMPLATE_INSTANTIATED (instantiation));
25445 }
25446
25447 if (complete)
25448 {
25449 /* If INSTANTIATION has been instantiated, then we don't
25450 need to consider it again in the future. */
25451 struct pending_template *drop = *t;
25452 *t = (*t)->next;
25453 set_refcount_ptr (drop->tinst);
25454 pending_template_freelist ().free (drop);
25455 }
25456 else
25457 {
25458 last = *t;
25459 t = &(*t)->next;
25460 }
25461 tinst_depth = 0;
25462 set_refcount_ptr (current_tinst_level);
25463 }
25464 last_pending_template = last;
25465 }
25466 while (reconsider);
25467
25468 input_location = saved_loc;
25469 }
25470
25471 /* Substitute ARGVEC into T, which is a list of initializers for
25472 either base class or a non-static data member. The TREE_PURPOSEs
25473 are DECLs, and the TREE_VALUEs are the initializer values. Used by
25474 instantiate_decl. */
25475
25476 static tree
25477 tsubst_initializer_list (tree t, tree argvec)
25478 {
25479 tree inits = NULL_TREE;
25480 tree target_ctor = error_mark_node;
25481
25482 for (; t; t = TREE_CHAIN (t))
25483 {
25484 tree decl;
25485 tree init;
25486 tree expanded_bases = NULL_TREE;
25487 tree expanded_arguments = NULL_TREE;
25488 int i, len = 1;
25489
25490 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
25491 {
25492 tree expr;
25493 tree arg;
25494
25495 /* Expand the base class expansion type into separate base
25496 classes. */
25497 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
25498 tf_warning_or_error,
25499 NULL_TREE);
25500 if (expanded_bases == error_mark_node)
25501 continue;
25502
25503 /* We'll be building separate TREE_LISTs of arguments for
25504 each base. */
25505 len = TREE_VEC_LENGTH (expanded_bases);
25506 expanded_arguments = make_tree_vec (len);
25507 for (i = 0; i < len; i++)
25508 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
25509
25510 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
25511 expand each argument in the TREE_VALUE of t. */
25512 expr = make_node (EXPR_PACK_EXPANSION);
25513 PACK_EXPANSION_LOCAL_P (expr) = true;
25514 PACK_EXPANSION_PARAMETER_PACKS (expr) =
25515 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
25516
25517 if (TREE_VALUE (t) == void_type_node)
25518 /* VOID_TYPE_NODE is used to indicate
25519 value-initialization. */
25520 {
25521 for (i = 0; i < len; i++)
25522 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
25523 }
25524 else
25525 {
25526 /* Substitute parameter packs into each argument in the
25527 TREE_LIST. */
25528 in_base_initializer = 1;
25529 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25530 {
25531 tree expanded_exprs;
25532
25533 /* Expand the argument. */
25534 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25535 expanded_exprs
25536 = tsubst_pack_expansion (expr, argvec,
25537 tf_warning_or_error,
25538 NULL_TREE);
25539 if (expanded_exprs == error_mark_node)
25540 continue;
25541
25542 /* Prepend each of the expanded expressions to the
25543 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25544 for (i = 0; i < len; i++)
25545 {
25546 TREE_VEC_ELT (expanded_arguments, i) =
25547 tree_cons (NULL_TREE,
25548 TREE_VEC_ELT (expanded_exprs, i),
25549 TREE_VEC_ELT (expanded_arguments, i));
25550 }
25551 }
25552 in_base_initializer = 0;
25553
25554 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25555 since we built them backwards. */
25556 for (i = 0; i < len; i++)
25557 {
25558 TREE_VEC_ELT (expanded_arguments, i) =
25559 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25560 }
25561 }
25562 }
25563
25564 for (i = 0; i < len; ++i)
25565 {
25566 if (expanded_bases)
25567 {
25568 decl = TREE_VEC_ELT (expanded_bases, i);
25569 decl = expand_member_init (decl);
25570 init = TREE_VEC_ELT (expanded_arguments, i);
25571 }
25572 else
25573 {
25574 tree tmp;
25575 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25576 tf_warning_or_error, NULL_TREE);
25577
25578 decl = expand_member_init (decl);
25579 if (decl && !DECL_P (decl))
25580 in_base_initializer = 1;
25581
25582 init = TREE_VALUE (t);
25583 tmp = init;
25584 if (init != void_type_node)
25585 init = tsubst_expr (init, argvec,
25586 tf_warning_or_error, NULL_TREE,
25587 /*integral_constant_expression_p=*/false);
25588 if (init == NULL_TREE && tmp != NULL_TREE)
25589 /* If we had an initializer but it instantiated to nothing,
25590 value-initialize the object. This will only occur when
25591 the initializer was a pack expansion where the parameter
25592 packs used in that expansion were of length zero. */
25593 init = void_type_node;
25594 in_base_initializer = 0;
25595 }
25596
25597 if (target_ctor != error_mark_node
25598 && init != error_mark_node)
25599 {
25600 error ("mem-initializer for %qD follows constructor delegation",
25601 decl);
25602 return inits;
25603 }
25604 /* Look for a target constructor. */
25605 if (init != error_mark_node
25606 && decl && CLASS_TYPE_P (decl)
25607 && same_type_p (decl, current_class_type))
25608 {
25609 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25610 if (inits)
25611 {
25612 error ("constructor delegation follows mem-initializer for %qD",
25613 TREE_PURPOSE (inits));
25614 continue;
25615 }
25616 target_ctor = init;
25617 }
25618
25619 if (decl)
25620 {
25621 init = build_tree_list (decl, init);
25622 TREE_CHAIN (init) = inits;
25623 inits = init;
25624 }
25625 }
25626 }
25627 return inits;
25628 }
25629
25630 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25631
25632 static void
25633 set_current_access_from_decl (tree decl)
25634 {
25635 if (TREE_PRIVATE (decl))
25636 current_access_specifier = access_private_node;
25637 else if (TREE_PROTECTED (decl))
25638 current_access_specifier = access_protected_node;
25639 else
25640 current_access_specifier = access_public_node;
25641 }
25642
25643 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25644 is the instantiation (which should have been created with
25645 start_enum) and ARGS are the template arguments to use. */
25646
25647 static void
25648 tsubst_enum (tree tag, tree newtag, tree args)
25649 {
25650 tree e;
25651
25652 if (SCOPED_ENUM_P (newtag))
25653 begin_scope (sk_scoped_enum, newtag);
25654
25655 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25656 {
25657 tree value;
25658 tree decl;
25659
25660 decl = TREE_VALUE (e);
25661 /* Note that in a template enum, the TREE_VALUE is the
25662 CONST_DECL, not the corresponding INTEGER_CST. */
25663 value = tsubst_expr (DECL_INITIAL (decl),
25664 args, tf_warning_or_error, NULL_TREE,
25665 /*integral_constant_expression_p=*/true);
25666
25667 /* Give this enumeration constant the correct access. */
25668 set_current_access_from_decl (decl);
25669
25670 /* Actually build the enumerator itself. Here we're assuming that
25671 enumerators can't have dependent attributes. */
25672 build_enumerator (DECL_NAME (decl), value, newtag,
25673 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25674 }
25675
25676 if (SCOPED_ENUM_P (newtag))
25677 finish_scope ();
25678
25679 finish_enum_value_list (newtag);
25680 finish_enum (newtag);
25681
25682 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25683 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25684 }
25685
25686 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25687 its type -- but without substituting the innermost set of template
25688 arguments. So, innermost set of template parameters will appear in
25689 the type. */
25690
25691 tree
25692 get_mostly_instantiated_function_type (tree decl)
25693 {
25694 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25695 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25696 }
25697
25698 /* Return truthvalue if we're processing a template different from
25699 the last one involved in diagnostics. */
25700 bool
25701 problematic_instantiation_changed (void)
25702 {
25703 return current_tinst_level != last_error_tinst_level;
25704 }
25705
25706 /* Remember current template involved in diagnostics. */
25707 void
25708 record_last_problematic_instantiation (void)
25709 {
25710 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25711 }
25712
25713 struct tinst_level *
25714 current_instantiation (void)
25715 {
25716 return current_tinst_level;
25717 }
25718
25719 /* Return TRUE if current_function_decl is being instantiated, false
25720 otherwise. */
25721
25722 bool
25723 instantiating_current_function_p (void)
25724 {
25725 return (current_instantiation ()
25726 && (current_instantiation ()->maybe_get_node ()
25727 == current_function_decl));
25728 }
25729
25730 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25731 type. Return false for ok, true for disallowed. Issue error and
25732 inform messages under control of COMPLAIN. */
25733
25734 static bool
25735 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25736 {
25737 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25738 return false;
25739 else if (TYPE_PTR_P (type))
25740 return false;
25741 else if (TYPE_REF_P (type)
25742 && !TYPE_REF_IS_RVALUE (type))
25743 return false;
25744 else if (TYPE_PTRMEM_P (type))
25745 return false;
25746 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25747 return false;
25748 else if (TREE_CODE (type) == TYPENAME_TYPE)
25749 return false;
25750 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25751 return false;
25752 else if (TREE_CODE (type) == NULLPTR_TYPE)
25753 return false;
25754 /* A bound template template parm could later be instantiated to have a valid
25755 nontype parm type via an alias template. */
25756 else if (cxx_dialect >= cxx11
25757 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25758 return false;
25759 else if (CLASS_TYPE_P (type))
25760 {
25761 if (cxx_dialect < cxx2a)
25762 {
25763 if (complain & tf_error)
25764 error ("non-type template parameters of class type only available "
25765 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25766 return true;
25767 }
25768 if (dependent_type_p (type))
25769 return false;
25770 if (!complete_type_or_else (type, NULL_TREE))
25771 return true;
25772 if (!structural_type_p (type))
25773 {
25774 auto_diagnostic_group d;
25775 if (complain & tf_error)
25776 error ("%qT is not a valid type for a template non-type parameter "
25777 "because it is not structural", type);
25778 structural_type_p (type, true);
25779 return true;
25780 }
25781 return false;
25782 }
25783
25784 if (complain & tf_error)
25785 {
25786 if (type == error_mark_node)
25787 inform (input_location, "invalid template non-type parameter");
25788 else
25789 error ("%q#T is not a valid type for a template non-type parameter",
25790 type);
25791 }
25792 return true;
25793 }
25794
25795 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25796 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25797
25798 static bool
25799 dependent_type_p_r (tree type)
25800 {
25801 tree scope;
25802
25803 /* [temp.dep.type]
25804
25805 A type is dependent if it is:
25806
25807 -- a template parameter. Template template parameters are types
25808 for us (since TYPE_P holds true for them) so we handle
25809 them here. */
25810 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25811 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25812 return true;
25813 /* -- a qualified-id with a nested-name-specifier which contains a
25814 class-name that names a dependent type or whose unqualified-id
25815 names a dependent type. */
25816 if (TREE_CODE (type) == TYPENAME_TYPE)
25817 return true;
25818
25819 /* An alias template specialization can be dependent even if the
25820 resulting type is not. */
25821 if (dependent_alias_template_spec_p (type))
25822 return true;
25823
25824 /* -- a cv-qualified type where the cv-unqualified type is
25825 dependent.
25826 No code is necessary for this bullet; the code below handles
25827 cv-qualified types, and we don't want to strip aliases with
25828 TYPE_MAIN_VARIANT because of DR 1558. */
25829 /* -- a compound type constructed from any dependent type. */
25830 if (TYPE_PTRMEM_P (type))
25831 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25832 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25833 (type)));
25834 else if (INDIRECT_TYPE_P (type))
25835 return dependent_type_p (TREE_TYPE (type));
25836 else if (FUNC_OR_METHOD_TYPE_P (type))
25837 {
25838 tree arg_type;
25839
25840 if (dependent_type_p (TREE_TYPE (type)))
25841 return true;
25842 for (arg_type = TYPE_ARG_TYPES (type);
25843 arg_type;
25844 arg_type = TREE_CHAIN (arg_type))
25845 if (dependent_type_p (TREE_VALUE (arg_type)))
25846 return true;
25847 if (cxx_dialect >= cxx17)
25848 /* A value-dependent noexcept-specifier makes the type dependent. */
25849 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25850 if (tree noex = TREE_PURPOSE (spec))
25851 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25852 affect overload resolution and treating it as dependent breaks
25853 things. Same for an unparsed noexcept expression. */
25854 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25855 && TREE_CODE (noex) != DEFERRED_PARSE
25856 && value_dependent_expression_p (noex))
25857 return true;
25858 return false;
25859 }
25860 /* -- an array type constructed from any dependent type or whose
25861 size is specified by a constant expression that is
25862 value-dependent.
25863
25864 We checked for type- and value-dependence of the bounds in
25865 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25866 if (TREE_CODE (type) == ARRAY_TYPE)
25867 {
25868 if (TYPE_DOMAIN (type)
25869 && dependent_type_p (TYPE_DOMAIN (type)))
25870 return true;
25871 return dependent_type_p (TREE_TYPE (type));
25872 }
25873
25874 /* -- a template-id in which either the template name is a template
25875 parameter ... */
25876 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25877 return true;
25878 /* ... or any of the template arguments is a dependent type or
25879 an expression that is type-dependent or value-dependent. */
25880 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25881 && (any_dependent_template_arguments_p
25882 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25883 return true;
25884
25885 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25886 dependent; if the argument of the `typeof' expression is not
25887 type-dependent, then it should already been have resolved. */
25888 if (TREE_CODE (type) == TYPEOF_TYPE
25889 || TREE_CODE (type) == DECLTYPE_TYPE
25890 || TREE_CODE (type) == UNDERLYING_TYPE)
25891 return true;
25892
25893 /* A template argument pack is dependent if any of its packed
25894 arguments are. */
25895 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25896 {
25897 tree args = ARGUMENT_PACK_ARGS (type);
25898 int i, len = TREE_VEC_LENGTH (args);
25899 for (i = 0; i < len; ++i)
25900 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25901 return true;
25902 }
25903
25904 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25905 be template parameters. */
25906 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25907 return true;
25908
25909 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25910 return true;
25911
25912 /* The standard does not specifically mention types that are local
25913 to template functions or local classes, but they should be
25914 considered dependent too. For example:
25915
25916 template <int I> void f() {
25917 enum E { a = I };
25918 S<sizeof (E)> s;
25919 }
25920
25921 The size of `E' cannot be known until the value of `I' has been
25922 determined. Therefore, `E' must be considered dependent. */
25923 scope = TYPE_CONTEXT (type);
25924 if (scope && TYPE_P (scope))
25925 return dependent_type_p (scope);
25926 /* Don't use type_dependent_expression_p here, as it can lead
25927 to infinite recursion trying to determine whether a lambda
25928 nested in a lambda is dependent (c++/47687). */
25929 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25930 && DECL_LANG_SPECIFIC (scope)
25931 && DECL_TEMPLATE_INFO (scope)
25932 && (any_dependent_template_arguments_p
25933 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25934 return true;
25935
25936 /* Other types are non-dependent. */
25937 return false;
25938 }
25939
25940 /* Returns TRUE if TYPE is dependent, in the sense of
25941 [temp.dep.type]. Note that a NULL type is considered dependent. */
25942
25943 bool
25944 dependent_type_p (tree type)
25945 {
25946 /* If there are no template parameters in scope, then there can't be
25947 any dependent types. */
25948 if (!processing_template_decl)
25949 {
25950 /* If we are not processing a template, then nobody should be
25951 providing us with a dependent type. */
25952 gcc_assert (type);
25953 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25954 return false;
25955 }
25956
25957 /* If the type is NULL, we have not computed a type for the entity
25958 in question; in that case, the type is dependent. */
25959 if (!type)
25960 return true;
25961
25962 /* Erroneous types can be considered non-dependent. */
25963 if (type == error_mark_node)
25964 return false;
25965
25966 /* Getting here with global_type_node means we improperly called this
25967 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25968 gcc_checking_assert (type != global_type_node);
25969
25970 /* If we have not already computed the appropriate value for TYPE,
25971 do so now. */
25972 if (!TYPE_DEPENDENT_P_VALID (type))
25973 {
25974 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25975 TYPE_DEPENDENT_P_VALID (type) = 1;
25976 }
25977
25978 return TYPE_DEPENDENT_P (type);
25979 }
25980
25981 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25982 lookup. In other words, a dependent type that is not the current
25983 instantiation. */
25984
25985 bool
25986 dependent_scope_p (tree scope)
25987 {
25988 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25989 && !currently_open_class (scope));
25990 }
25991
25992 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25993 an unknown base of 'this' (and is therefore instantiation-dependent). */
25994
25995 static bool
25996 unknown_base_ref_p (tree t)
25997 {
25998 if (!current_class_ptr)
25999 return false;
26000
26001 tree mem = TREE_OPERAND (t, 1);
26002 if (shared_member_p (mem))
26003 return false;
26004
26005 tree cur = current_nonlambda_class_type ();
26006 if (!any_dependent_bases_p (cur))
26007 return false;
26008
26009 tree ctx = TREE_OPERAND (t, 0);
26010 if (DERIVED_FROM_P (ctx, cur))
26011 return false;
26012
26013 return true;
26014 }
26015
26016 /* T is a SCOPE_REF; return whether we need to consider it
26017 instantiation-dependent so that we can check access at instantiation
26018 time even though we know which member it resolves to. */
26019
26020 static bool
26021 instantiation_dependent_scope_ref_p (tree t)
26022 {
26023 if (DECL_P (TREE_OPERAND (t, 1))
26024 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26025 && !unknown_base_ref_p (t)
26026 && accessible_in_template_p (TREE_OPERAND (t, 0),
26027 TREE_OPERAND (t, 1)))
26028 return false;
26029 else
26030 return true;
26031 }
26032
26033 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26034 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26035 expression. */
26036
26037 /* Note that this predicate is not appropriate for general expressions;
26038 only constant expressions (that satisfy potential_constant_expression)
26039 can be tested for value dependence. */
26040
26041 bool
26042 value_dependent_expression_p (tree expression)
26043 {
26044 if (!processing_template_decl || expression == NULL_TREE)
26045 return false;
26046
26047 /* A type-dependent expression is also value-dependent. */
26048 if (type_dependent_expression_p (expression))
26049 return true;
26050
26051 switch (TREE_CODE (expression))
26052 {
26053 case BASELINK:
26054 /* A dependent member function of the current instantiation. */
26055 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26056
26057 case FUNCTION_DECL:
26058 /* A dependent member function of the current instantiation. */
26059 if (DECL_CLASS_SCOPE_P (expression)
26060 && dependent_type_p (DECL_CONTEXT (expression)))
26061 return true;
26062 break;
26063
26064 case IDENTIFIER_NODE:
26065 /* A name that has not been looked up -- must be dependent. */
26066 return true;
26067
26068 case TEMPLATE_PARM_INDEX:
26069 /* A non-type template parm. */
26070 return true;
26071
26072 case CONST_DECL:
26073 /* A non-type template parm. */
26074 if (DECL_TEMPLATE_PARM_P (expression))
26075 return true;
26076 return value_dependent_expression_p (DECL_INITIAL (expression));
26077
26078 case VAR_DECL:
26079 /* A constant with literal type and is initialized
26080 with an expression that is value-dependent. */
26081 if (DECL_DEPENDENT_INIT_P (expression)
26082 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26083 || TYPE_REF_P (TREE_TYPE (expression)))
26084 return true;
26085 if (DECL_HAS_VALUE_EXPR_P (expression))
26086 {
26087 tree value_expr = DECL_VALUE_EXPR (expression);
26088 if (value_dependent_expression_p (value_expr)
26089 /* __PRETTY_FUNCTION__ inside a template function is dependent
26090 on the name of the function. */
26091 || (DECL_PRETTY_FUNCTION_P (expression)
26092 /* It might be used in a template, but not a template
26093 function, in which case its DECL_VALUE_EXPR will be
26094 "top level". */
26095 && value_expr == error_mark_node))
26096 return true;
26097 }
26098 return false;
26099
26100 case DYNAMIC_CAST_EXPR:
26101 case STATIC_CAST_EXPR:
26102 case CONST_CAST_EXPR:
26103 case REINTERPRET_CAST_EXPR:
26104 case CAST_EXPR:
26105 case IMPLICIT_CONV_EXPR:
26106 /* These expressions are value-dependent if the type to which
26107 the cast occurs is dependent or the expression being casted
26108 is value-dependent. */
26109 {
26110 tree type = TREE_TYPE (expression);
26111
26112 if (dependent_type_p (type))
26113 return true;
26114
26115 /* A functional cast has a list of operands. */
26116 expression = TREE_OPERAND (expression, 0);
26117 if (!expression)
26118 {
26119 /* If there are no operands, it must be an expression such
26120 as "int()". This should not happen for aggregate types
26121 because it would form non-constant expressions. */
26122 gcc_assert (cxx_dialect >= cxx11
26123 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26124
26125 return false;
26126 }
26127
26128 if (TREE_CODE (expression) == TREE_LIST)
26129 return any_value_dependent_elements_p (expression);
26130
26131 return value_dependent_expression_p (expression);
26132 }
26133
26134 case SIZEOF_EXPR:
26135 if (SIZEOF_EXPR_TYPE_P (expression))
26136 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26137 /* FALLTHRU */
26138 case ALIGNOF_EXPR:
26139 case TYPEID_EXPR:
26140 /* A `sizeof' expression is value-dependent if the operand is
26141 type-dependent or is a pack expansion. */
26142 expression = TREE_OPERAND (expression, 0);
26143 if (PACK_EXPANSION_P (expression))
26144 return true;
26145 else if (TYPE_P (expression))
26146 return dependent_type_p (expression);
26147 return instantiation_dependent_uneval_expression_p (expression);
26148
26149 case AT_ENCODE_EXPR:
26150 /* An 'encode' expression is value-dependent if the operand is
26151 type-dependent. */
26152 expression = TREE_OPERAND (expression, 0);
26153 return dependent_type_p (expression);
26154
26155 case NOEXCEPT_EXPR:
26156 expression = TREE_OPERAND (expression, 0);
26157 return instantiation_dependent_uneval_expression_p (expression);
26158
26159 case SCOPE_REF:
26160 /* All instantiation-dependent expressions should also be considered
26161 value-dependent. */
26162 return instantiation_dependent_scope_ref_p (expression);
26163
26164 case COMPONENT_REF:
26165 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26166 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26167
26168 case NONTYPE_ARGUMENT_PACK:
26169 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26170 is value-dependent. */
26171 {
26172 tree values = ARGUMENT_PACK_ARGS (expression);
26173 int i, len = TREE_VEC_LENGTH (values);
26174
26175 for (i = 0; i < len; ++i)
26176 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26177 return true;
26178
26179 return false;
26180 }
26181
26182 case TRAIT_EXPR:
26183 {
26184 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26185
26186 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26187 return true;
26188
26189 if (!type2)
26190 return false;
26191
26192 if (TREE_CODE (type2) != TREE_LIST)
26193 return dependent_type_p (type2);
26194
26195 for (; type2; type2 = TREE_CHAIN (type2))
26196 if (dependent_type_p (TREE_VALUE (type2)))
26197 return true;
26198
26199 return false;
26200 }
26201
26202 case MODOP_EXPR:
26203 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26204 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
26205
26206 case ARRAY_REF:
26207 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
26208 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
26209
26210 case ADDR_EXPR:
26211 {
26212 tree op = TREE_OPERAND (expression, 0);
26213 return (value_dependent_expression_p (op)
26214 || has_value_dependent_address (op));
26215 }
26216
26217 case REQUIRES_EXPR:
26218 /* Treat all requires-expressions as value-dependent so
26219 we don't try to fold them. */
26220 return true;
26221
26222 case TYPE_REQ:
26223 return dependent_type_p (TREE_OPERAND (expression, 0));
26224
26225 case CALL_EXPR:
26226 {
26227 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
26228 return true;
26229 tree fn = get_callee_fndecl (expression);
26230 int i, nargs;
26231 nargs = call_expr_nargs (expression);
26232 for (i = 0; i < nargs; ++i)
26233 {
26234 tree op = CALL_EXPR_ARG (expression, i);
26235 /* In a call to a constexpr member function, look through the
26236 implicit ADDR_EXPR on the object argument so that it doesn't
26237 cause the call to be considered value-dependent. We also
26238 look through it in potential_constant_expression. */
26239 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
26240 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
26241 && TREE_CODE (op) == ADDR_EXPR)
26242 op = TREE_OPERAND (op, 0);
26243 if (value_dependent_expression_p (op))
26244 return true;
26245 }
26246 return false;
26247 }
26248
26249 case TEMPLATE_ID_EXPR:
26250 return concept_definition_p (TREE_OPERAND (expression, 0));
26251
26252 case CONSTRUCTOR:
26253 {
26254 unsigned ix;
26255 tree val;
26256 if (dependent_type_p (TREE_TYPE (expression)))
26257 return true;
26258 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
26259 if (value_dependent_expression_p (val))
26260 return true;
26261 return false;
26262 }
26263
26264 case STMT_EXPR:
26265 /* Treat a GNU statement expression as dependent to avoid crashing
26266 under instantiate_non_dependent_expr; it can't be constant. */
26267 return true;
26268
26269 default:
26270 /* A constant expression is value-dependent if any subexpression is
26271 value-dependent. */
26272 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
26273 {
26274 case tcc_reference:
26275 case tcc_unary:
26276 case tcc_comparison:
26277 case tcc_binary:
26278 case tcc_expression:
26279 case tcc_vl_exp:
26280 {
26281 int i, len = cp_tree_operand_length (expression);
26282
26283 for (i = 0; i < len; i++)
26284 {
26285 tree t = TREE_OPERAND (expression, i);
26286
26287 /* In some cases, some of the operands may be missing.
26288 (For example, in the case of PREDECREMENT_EXPR, the
26289 amount to increment by may be missing.) That doesn't
26290 make the expression dependent. */
26291 if (t && value_dependent_expression_p (t))
26292 return true;
26293 }
26294 }
26295 break;
26296 default:
26297 break;
26298 }
26299 break;
26300 }
26301
26302 /* The expression is not value-dependent. */
26303 return false;
26304 }
26305
26306 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
26307 [temp.dep.expr]. Note that an expression with no type is
26308 considered dependent. Other parts of the compiler arrange for an
26309 expression with type-dependent subexpressions to have no type, so
26310 this function doesn't have to be fully recursive. */
26311
26312 bool
26313 type_dependent_expression_p (tree expression)
26314 {
26315 if (!processing_template_decl)
26316 return false;
26317
26318 if (expression == NULL_TREE || expression == error_mark_node)
26319 return false;
26320
26321 STRIP_ANY_LOCATION_WRAPPER (expression);
26322
26323 /* An unresolved name is always dependent. */
26324 if (identifier_p (expression)
26325 || TREE_CODE (expression) == USING_DECL
26326 || TREE_CODE (expression) == WILDCARD_DECL)
26327 return true;
26328
26329 /* A lambda-expression in template context is dependent. dependent_type_p is
26330 true for a lambda in the scope of a class or function template, but that
26331 doesn't cover all template contexts, like a default template argument. */
26332 if (TREE_CODE (expression) == LAMBDA_EXPR)
26333 return true;
26334
26335 /* A fold expression is type-dependent. */
26336 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
26337 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
26338 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
26339 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
26340 return true;
26341
26342 /* Some expression forms are never type-dependent. */
26343 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
26344 || TREE_CODE (expression) == SIZEOF_EXPR
26345 || TREE_CODE (expression) == ALIGNOF_EXPR
26346 || TREE_CODE (expression) == AT_ENCODE_EXPR
26347 || TREE_CODE (expression) == NOEXCEPT_EXPR
26348 || TREE_CODE (expression) == TRAIT_EXPR
26349 || TREE_CODE (expression) == TYPEID_EXPR
26350 || TREE_CODE (expression) == DELETE_EXPR
26351 || TREE_CODE (expression) == VEC_DELETE_EXPR
26352 || TREE_CODE (expression) == THROW_EXPR
26353 || TREE_CODE (expression) == REQUIRES_EXPR)
26354 return false;
26355
26356 /* The types of these expressions depends only on the type to which
26357 the cast occurs. */
26358 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
26359 || TREE_CODE (expression) == STATIC_CAST_EXPR
26360 || TREE_CODE (expression) == CONST_CAST_EXPR
26361 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
26362 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
26363 || TREE_CODE (expression) == CAST_EXPR)
26364 return dependent_type_p (TREE_TYPE (expression));
26365
26366 /* The types of these expressions depends only on the type created
26367 by the expression. */
26368 if (TREE_CODE (expression) == NEW_EXPR
26369 || TREE_CODE (expression) == VEC_NEW_EXPR)
26370 {
26371 /* For NEW_EXPR tree nodes created inside a template, either
26372 the object type itself or a TREE_LIST may appear as the
26373 operand 1. */
26374 tree type = TREE_OPERAND (expression, 1);
26375 if (TREE_CODE (type) == TREE_LIST)
26376 /* This is an array type. We need to check array dimensions
26377 as well. */
26378 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
26379 || value_dependent_expression_p
26380 (TREE_OPERAND (TREE_VALUE (type), 1));
26381 else
26382 return dependent_type_p (type);
26383 }
26384
26385 if (TREE_CODE (expression) == SCOPE_REF)
26386 {
26387 tree scope = TREE_OPERAND (expression, 0);
26388 tree name = TREE_OPERAND (expression, 1);
26389
26390 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
26391 contains an identifier associated by name lookup with one or more
26392 declarations declared with a dependent type, or...a
26393 nested-name-specifier or qualified-id that names a member of an
26394 unknown specialization. */
26395 return (type_dependent_expression_p (name)
26396 || dependent_scope_p (scope));
26397 }
26398
26399 if (TREE_CODE (expression) == TEMPLATE_DECL
26400 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
26401 return uses_outer_template_parms (expression);
26402
26403 if (TREE_CODE (expression) == STMT_EXPR)
26404 expression = stmt_expr_value_expr (expression);
26405
26406 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
26407 {
26408 tree elt;
26409 unsigned i;
26410
26411 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
26412 {
26413 if (type_dependent_expression_p (elt))
26414 return true;
26415 }
26416 return false;
26417 }
26418
26419 /* A static data member of the current instantiation with incomplete
26420 array type is type-dependent, as the definition and specializations
26421 can have different bounds. */
26422 if (VAR_P (expression)
26423 && DECL_CLASS_SCOPE_P (expression)
26424 && dependent_type_p (DECL_CONTEXT (expression))
26425 && VAR_HAD_UNKNOWN_BOUND (expression))
26426 return true;
26427
26428 /* An array of unknown bound depending on a variadic parameter, eg:
26429
26430 template<typename... Args>
26431 void foo (Args... args)
26432 {
26433 int arr[] = { args... };
26434 }
26435
26436 template<int... vals>
26437 void bar ()
26438 {
26439 int arr[] = { vals... };
26440 }
26441
26442 If the array has no length and has an initializer, it must be that
26443 we couldn't determine its length in cp_complete_array_type because
26444 it is dependent. */
26445 if (VAR_P (expression)
26446 && TREE_TYPE (expression) != NULL_TREE
26447 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
26448 && !TYPE_DOMAIN (TREE_TYPE (expression))
26449 && DECL_INITIAL (expression))
26450 return true;
26451
26452 /* A function or variable template-id is type-dependent if it has any
26453 dependent template arguments. */
26454 if (VAR_OR_FUNCTION_DECL_P (expression)
26455 && DECL_LANG_SPECIFIC (expression)
26456 && DECL_TEMPLATE_INFO (expression))
26457 {
26458 /* Consider the innermost template arguments, since those are the ones
26459 that come from the template-id; the template arguments for the
26460 enclosing class do not make it type-dependent unless they are used in
26461 the type of the decl. */
26462 if (instantiates_primary_template_p (expression)
26463 && (any_dependent_template_arguments_p
26464 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
26465 return true;
26466 }
26467
26468 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
26469 type-dependent. Checking this is important for functions with auto return
26470 type, which looks like a dependent type. */
26471 if (TREE_CODE (expression) == FUNCTION_DECL
26472 && !(DECL_CLASS_SCOPE_P (expression)
26473 && dependent_type_p (DECL_CONTEXT (expression)))
26474 && !(DECL_LANG_SPECIFIC (expression)
26475 && DECL_FRIEND_P (expression)
26476 && (!DECL_FRIEND_CONTEXT (expression)
26477 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
26478 && !DECL_LOCAL_FUNCTION_P (expression))
26479 {
26480 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
26481 || undeduced_auto_decl (expression));
26482 return false;
26483 }
26484
26485 /* Always dependent, on the number of arguments if nothing else. */
26486 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
26487 return true;
26488
26489 if (TREE_TYPE (expression) == unknown_type_node)
26490 {
26491 if (TREE_CODE (expression) == ADDR_EXPR)
26492 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
26493 if (TREE_CODE (expression) == COMPONENT_REF
26494 || TREE_CODE (expression) == OFFSET_REF)
26495 {
26496 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
26497 return true;
26498 expression = TREE_OPERAND (expression, 1);
26499 if (identifier_p (expression))
26500 return false;
26501 }
26502 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
26503 if (TREE_CODE (expression) == SCOPE_REF)
26504 return false;
26505
26506 if (BASELINK_P (expression))
26507 {
26508 if (BASELINK_OPTYPE (expression)
26509 && dependent_type_p (BASELINK_OPTYPE (expression)))
26510 return true;
26511 expression = BASELINK_FUNCTIONS (expression);
26512 }
26513
26514 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
26515 {
26516 if (any_dependent_template_arguments_p
26517 (TREE_OPERAND (expression, 1)))
26518 return true;
26519 expression = TREE_OPERAND (expression, 0);
26520 if (identifier_p (expression))
26521 return true;
26522 }
26523
26524 gcc_assert (OVL_P (expression));
26525
26526 for (lkp_iterator iter (expression); iter; ++iter)
26527 if (type_dependent_expression_p (*iter))
26528 return true;
26529
26530 return false;
26531 }
26532
26533 /* The type of a non-type template parm declared with a placeholder type
26534 depends on the corresponding template argument, even though
26535 placeholders are not normally considered dependent. */
26536 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26537 && is_auto (TREE_TYPE (expression)))
26538 return true;
26539
26540 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26541
26542 /* Dependent type attributes might not have made it from the decl to
26543 the type yet. */
26544 if (DECL_P (expression)
26545 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26546 return true;
26547
26548 return (dependent_type_p (TREE_TYPE (expression)));
26549 }
26550
26551 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26552 type-dependent if the expression refers to a member of the current
26553 instantiation and the type of the referenced member is dependent, or the
26554 class member access expression refers to a member of an unknown
26555 specialization.
26556
26557 This function returns true if the OBJECT in such a class member access
26558 expression is of an unknown specialization. */
26559
26560 bool
26561 type_dependent_object_expression_p (tree object)
26562 {
26563 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26564 dependent. */
26565 if (TREE_CODE (object) == IDENTIFIER_NODE)
26566 return true;
26567 tree scope = TREE_TYPE (object);
26568 return (!scope || dependent_scope_p (scope));
26569 }
26570
26571 /* walk_tree callback function for instantiation_dependent_expression_p,
26572 below. Returns non-zero if a dependent subexpression is found. */
26573
26574 static tree
26575 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26576 void * /*data*/)
26577 {
26578 if (TYPE_P (*tp))
26579 {
26580 /* We don't have to worry about decltype currently because decltype
26581 of an instantiation-dependent expr is a dependent type. This
26582 might change depending on the resolution of DR 1172. */
26583 *walk_subtrees = false;
26584 return NULL_TREE;
26585 }
26586 enum tree_code code = TREE_CODE (*tp);
26587 switch (code)
26588 {
26589 /* Don't treat an argument list as dependent just because it has no
26590 TREE_TYPE. */
26591 case TREE_LIST:
26592 case TREE_VEC:
26593 case NONTYPE_ARGUMENT_PACK:
26594 return NULL_TREE;
26595
26596 case TEMPLATE_PARM_INDEX:
26597 if (dependent_type_p (TREE_TYPE (*tp)))
26598 return *tp;
26599 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26600 return *tp;
26601 /* We'll check value-dependence separately. */
26602 return NULL_TREE;
26603
26604 /* Handle expressions with type operands. */
26605 case SIZEOF_EXPR:
26606 case ALIGNOF_EXPR:
26607 case TYPEID_EXPR:
26608 case AT_ENCODE_EXPR:
26609 {
26610 tree op = TREE_OPERAND (*tp, 0);
26611 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26612 op = TREE_TYPE (op);
26613 if (TYPE_P (op))
26614 {
26615 if (dependent_type_p (op))
26616 return *tp;
26617 else
26618 {
26619 *walk_subtrees = false;
26620 return NULL_TREE;
26621 }
26622 }
26623 break;
26624 }
26625
26626 case COMPONENT_REF:
26627 if (identifier_p (TREE_OPERAND (*tp, 1)))
26628 /* In a template, finish_class_member_access_expr creates a
26629 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26630 type-dependent, so that we can check access control at
26631 instantiation time (PR 42277). See also Core issue 1273. */
26632 return *tp;
26633 break;
26634
26635 case SCOPE_REF:
26636 if (instantiation_dependent_scope_ref_p (*tp))
26637 return *tp;
26638 else
26639 break;
26640
26641 /* Treat statement-expressions as dependent. */
26642 case BIND_EXPR:
26643 return *tp;
26644
26645 /* Treat requires-expressions as dependent. */
26646 case REQUIRES_EXPR:
26647 return *tp;
26648
26649 case CALL_EXPR:
26650 /* Treat concept checks as dependent. */
26651 if (concept_check_p (*tp))
26652 return *tp;
26653 break;
26654
26655 case TEMPLATE_ID_EXPR:
26656 /* Treat concept checks as dependent. */
26657 if (concept_check_p (*tp))
26658 return *tp;
26659 break;
26660
26661 case CONSTRUCTOR:
26662 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26663 return *tp;
26664 break;
26665
26666 default:
26667 break;
26668 }
26669
26670 if (type_dependent_expression_p (*tp))
26671 return *tp;
26672 else
26673 return NULL_TREE;
26674 }
26675
26676 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26677 sense defined by the ABI:
26678
26679 "An expression is instantiation-dependent if it is type-dependent
26680 or value-dependent, or it has a subexpression that is type-dependent
26681 or value-dependent."
26682
26683 Except don't actually check value-dependence for unevaluated expressions,
26684 because in sizeof(i) we don't care about the value of i. Checking
26685 type-dependence will in turn check value-dependence of array bounds/template
26686 arguments as needed. */
26687
26688 bool
26689 instantiation_dependent_uneval_expression_p (tree expression)
26690 {
26691 tree result;
26692
26693 if (!processing_template_decl)
26694 return false;
26695
26696 if (expression == error_mark_node)
26697 return false;
26698
26699 result = cp_walk_tree_without_duplicates (&expression,
26700 instantiation_dependent_r, NULL);
26701 return result != NULL_TREE;
26702 }
26703
26704 /* As above, but also check value-dependence of the expression as a whole. */
26705
26706 bool
26707 instantiation_dependent_expression_p (tree expression)
26708 {
26709 return (instantiation_dependent_uneval_expression_p (expression)
26710 || value_dependent_expression_p (expression));
26711 }
26712
26713 /* Like type_dependent_expression_p, but it also works while not processing
26714 a template definition, i.e. during substitution or mangling. */
26715
26716 bool
26717 type_dependent_expression_p_push (tree expr)
26718 {
26719 bool b;
26720 ++processing_template_decl;
26721 b = type_dependent_expression_p (expr);
26722 --processing_template_decl;
26723 return b;
26724 }
26725
26726 /* Returns TRUE if ARGS contains a type-dependent expression. */
26727
26728 bool
26729 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26730 {
26731 unsigned int i;
26732 tree arg;
26733
26734 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26735 {
26736 if (type_dependent_expression_p (arg))
26737 return true;
26738 }
26739 return false;
26740 }
26741
26742 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26743 expressions) contains any type-dependent expressions. */
26744
26745 bool
26746 any_type_dependent_elements_p (const_tree list)
26747 {
26748 for (; list; list = TREE_CHAIN (list))
26749 if (type_dependent_expression_p (TREE_VALUE (list)))
26750 return true;
26751
26752 return false;
26753 }
26754
26755 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26756 expressions) contains any value-dependent expressions. */
26757
26758 bool
26759 any_value_dependent_elements_p (const_tree list)
26760 {
26761 for (; list; list = TREE_CHAIN (list))
26762 if (value_dependent_expression_p (TREE_VALUE (list)))
26763 return true;
26764
26765 return false;
26766 }
26767
26768 /* Returns TRUE if the ARG (a template argument) is dependent. */
26769
26770 bool
26771 dependent_template_arg_p (tree arg)
26772 {
26773 if (!processing_template_decl)
26774 return false;
26775
26776 /* Assume a template argument that was wrongly written by the user
26777 is dependent. This is consistent with what
26778 any_dependent_template_arguments_p [that calls this function]
26779 does. */
26780 if (!arg || arg == error_mark_node)
26781 return true;
26782
26783 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26784 arg = argument_pack_select_arg (arg);
26785
26786 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26787 return true;
26788 if (TREE_CODE (arg) == TEMPLATE_DECL)
26789 {
26790 if (DECL_TEMPLATE_PARM_P (arg))
26791 return true;
26792 /* A member template of a dependent class is not necessarily
26793 type-dependent, but it is a dependent template argument because it
26794 will be a member of an unknown specialization to that template. */
26795 tree scope = CP_DECL_CONTEXT (arg);
26796 return TYPE_P (scope) && dependent_type_p (scope);
26797 }
26798 else if (ARGUMENT_PACK_P (arg))
26799 {
26800 tree args = ARGUMENT_PACK_ARGS (arg);
26801 int i, len = TREE_VEC_LENGTH (args);
26802 for (i = 0; i < len; ++i)
26803 {
26804 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26805 return true;
26806 }
26807
26808 return false;
26809 }
26810 else if (TYPE_P (arg))
26811 return dependent_type_p (arg);
26812 else
26813 return (type_dependent_expression_p (arg)
26814 || value_dependent_expression_p (arg));
26815 }
26816
26817 /* Returns true if ARGS (a collection of template arguments) contains
26818 any types that require structural equality testing. */
26819
26820 bool
26821 any_template_arguments_need_structural_equality_p (tree args)
26822 {
26823 int i;
26824 int j;
26825
26826 if (!args)
26827 return false;
26828 if (args == error_mark_node)
26829 return true;
26830
26831 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26832 {
26833 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26834 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26835 {
26836 tree arg = TREE_VEC_ELT (level, j);
26837 tree packed_args = NULL_TREE;
26838 int k, len = 1;
26839
26840 if (ARGUMENT_PACK_P (arg))
26841 {
26842 /* Look inside the argument pack. */
26843 packed_args = ARGUMENT_PACK_ARGS (arg);
26844 len = TREE_VEC_LENGTH (packed_args);
26845 }
26846
26847 for (k = 0; k < len; ++k)
26848 {
26849 if (packed_args)
26850 arg = TREE_VEC_ELT (packed_args, k);
26851
26852 if (error_operand_p (arg))
26853 return true;
26854 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26855 continue;
26856 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26857 return true;
26858 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26859 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26860 return true;
26861 }
26862 }
26863 }
26864
26865 return false;
26866 }
26867
26868 /* Returns true if ARGS (a collection of template arguments) contains
26869 any dependent arguments. */
26870
26871 bool
26872 any_dependent_template_arguments_p (const_tree args)
26873 {
26874 int i;
26875 int j;
26876
26877 if (!args)
26878 return false;
26879 if (args == error_mark_node)
26880 return true;
26881
26882 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26883 {
26884 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26885 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26886 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26887 return true;
26888 }
26889
26890 return false;
26891 }
26892
26893 /* Returns true if ARGS contains any errors. */
26894
26895 bool
26896 any_erroneous_template_args_p (const_tree args)
26897 {
26898 int i;
26899 int j;
26900
26901 if (args == error_mark_node)
26902 return true;
26903
26904 if (args && TREE_CODE (args) != TREE_VEC)
26905 {
26906 if (tree ti = get_template_info (args))
26907 args = TI_ARGS (ti);
26908 else
26909 args = NULL_TREE;
26910 }
26911
26912 if (!args)
26913 return false;
26914
26915 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26916 {
26917 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26918 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26919 if (error_operand_p (TREE_VEC_ELT (level, j)))
26920 return true;
26921 }
26922
26923 return false;
26924 }
26925
26926 /* Returns TRUE if the template TMPL is type-dependent. */
26927
26928 bool
26929 dependent_template_p (tree tmpl)
26930 {
26931 if (TREE_CODE (tmpl) == OVERLOAD)
26932 {
26933 for (lkp_iterator iter (tmpl); iter; ++iter)
26934 if (dependent_template_p (*iter))
26935 return true;
26936 return false;
26937 }
26938
26939 /* Template template parameters are dependent. */
26940 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26941 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26942 return true;
26943 /* So are names that have not been looked up. */
26944 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26945 return true;
26946 return false;
26947 }
26948
26949 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26950
26951 bool
26952 dependent_template_id_p (tree tmpl, tree args)
26953 {
26954 return (dependent_template_p (tmpl)
26955 || any_dependent_template_arguments_p (args));
26956 }
26957
26958 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26959 are dependent. */
26960
26961 bool
26962 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26963 {
26964 int i;
26965
26966 if (!processing_template_decl)
26967 return false;
26968
26969 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26970 {
26971 tree decl = TREE_VEC_ELT (declv, i);
26972 tree init = TREE_VEC_ELT (initv, i);
26973 tree cond = TREE_VEC_ELT (condv, i);
26974 tree incr = TREE_VEC_ELT (incrv, i);
26975
26976 if (type_dependent_expression_p (decl)
26977 || TREE_CODE (decl) == SCOPE_REF)
26978 return true;
26979
26980 if (init && type_dependent_expression_p (init))
26981 return true;
26982
26983 if (cond == global_namespace)
26984 return true;
26985
26986 if (type_dependent_expression_p (cond))
26987 return true;
26988
26989 if (COMPARISON_CLASS_P (cond)
26990 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26991 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26992 return true;
26993
26994 if (TREE_CODE (incr) == MODOP_EXPR)
26995 {
26996 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26997 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26998 return true;
26999 }
27000 else if (type_dependent_expression_p (incr))
27001 return true;
27002 else if (TREE_CODE (incr) == MODIFY_EXPR)
27003 {
27004 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27005 return true;
27006 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27007 {
27008 tree t = TREE_OPERAND (incr, 1);
27009 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27010 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27011 return true;
27012
27013 /* If this loop has a class iterator with != comparison
27014 with increment other than i++/++i/i--/--i, make sure the
27015 increment is constant. */
27016 if (CLASS_TYPE_P (TREE_TYPE (decl))
27017 && TREE_CODE (cond) == NE_EXPR)
27018 {
27019 if (TREE_OPERAND (t, 0) == decl)
27020 t = TREE_OPERAND (t, 1);
27021 else
27022 t = TREE_OPERAND (t, 0);
27023 if (TREE_CODE (t) != INTEGER_CST)
27024 return true;
27025 }
27026 }
27027 }
27028 }
27029
27030 return false;
27031 }
27032
27033 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27034 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27035 no such TYPE can be found. Note that this function peers inside
27036 uninstantiated templates and therefore should be used only in
27037 extremely limited situations. ONLY_CURRENT_P restricts this
27038 peering to the currently open classes hierarchy (which is required
27039 when comparing types). */
27040
27041 tree
27042 resolve_typename_type (tree type, bool only_current_p)
27043 {
27044 tree scope;
27045 tree name;
27046 tree decl;
27047 int quals;
27048 tree pushed_scope;
27049 tree result;
27050
27051 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27052
27053 scope = TYPE_CONTEXT (type);
27054 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27055 gcc_checking_assert (uses_template_parms (scope));
27056
27057 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27058 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
27059 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
27060 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
27061 identifier of the TYPENAME_TYPE anymore.
27062 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
27063 TYPENAME_TYPE instead, we avoid messing up with a possible
27064 typedef variant case. */
27065 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27066
27067 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27068 it first before we can figure out what NAME refers to. */
27069 if (TREE_CODE (scope) == TYPENAME_TYPE)
27070 {
27071 if (TYPENAME_IS_RESOLVING_P (scope))
27072 /* Given a class template A with a dependent base with nested type C,
27073 typedef typename A::C::C C will land us here, as trying to resolve
27074 the initial A::C leads to the local C typedef, which leads back to
27075 A::C::C. So we break the recursion now. */
27076 return type;
27077 else
27078 scope = resolve_typename_type (scope, only_current_p);
27079 }
27080 /* If we don't know what SCOPE refers to, then we cannot resolve the
27081 TYPENAME_TYPE. */
27082 if (!CLASS_TYPE_P (scope))
27083 return type;
27084 /* If this is a typedef, we don't want to look inside (c++/11987). */
27085 if (typedef_variant_p (type))
27086 return type;
27087 /* If SCOPE isn't the template itself, it will not have a valid
27088 TYPE_FIELDS list. */
27089 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27090 /* scope is either the template itself or a compatible instantiation
27091 like X<T>, so look up the name in the original template. */
27092 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27093 /* If scope has no fields, it can't be a current instantiation. Check this
27094 before currently_open_class to avoid infinite recursion (71515). */
27095 if (!TYPE_FIELDS (scope))
27096 return type;
27097 /* If the SCOPE is not the current instantiation, there's no reason
27098 to look inside it. */
27099 if (only_current_p && !currently_open_class (scope))
27100 return type;
27101 /* Enter the SCOPE so that name lookup will be resolved as if we
27102 were in the class definition. In particular, SCOPE will no
27103 longer be considered a dependent type. */
27104 pushed_scope = push_scope (scope);
27105 /* Look up the declaration. */
27106 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27107 tf_warning_or_error);
27108
27109 result = NULL_TREE;
27110
27111 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27112 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27113 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27114 if (!decl)
27115 /*nop*/;
27116 else if (identifier_p (fullname)
27117 && TREE_CODE (decl) == TYPE_DECL)
27118 {
27119 result = TREE_TYPE (decl);
27120 if (result == error_mark_node)
27121 result = NULL_TREE;
27122 }
27123 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27124 && DECL_CLASS_TEMPLATE_P (decl))
27125 {
27126 /* Obtain the template and the arguments. */
27127 tree tmpl = TREE_OPERAND (fullname, 0);
27128 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27129 {
27130 /* We get here with a plain identifier because a previous tentative
27131 parse of the nested-name-specifier as part of a ptr-operator saw
27132 ::template X<A>. The use of ::template is necessary in a
27133 ptr-operator, but wrong in a declarator-id.
27134
27135 [temp.names]: In a qualified-id of a declarator-id, the keyword
27136 template shall not appear at the top level. */
27137 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27138 "keyword %<template%> not allowed in declarator-id");
27139 tmpl = decl;
27140 }
27141 tree args = TREE_OPERAND (fullname, 1);
27142 /* Instantiate the template. */
27143 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27144 /*entering_scope=*/true,
27145 tf_error | tf_user);
27146 if (result == error_mark_node)
27147 result = NULL_TREE;
27148 }
27149
27150 /* Leave the SCOPE. */
27151 if (pushed_scope)
27152 pop_scope (pushed_scope);
27153
27154 /* If we failed to resolve it, return the original typename. */
27155 if (!result)
27156 return type;
27157
27158 /* If lookup found a typename type, resolve that too. */
27159 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27160 {
27161 /* Ill-formed programs can cause infinite recursion here, so we
27162 must catch that. */
27163 TYPENAME_IS_RESOLVING_P (result) = 1;
27164 result = resolve_typename_type (result, only_current_p);
27165 TYPENAME_IS_RESOLVING_P (result) = 0;
27166 }
27167
27168 /* Qualify the resulting type. */
27169 quals = cp_type_quals (type);
27170 if (quals)
27171 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27172
27173 return result;
27174 }
27175
27176 /* EXPR is an expression which is not type-dependent. Return a proxy
27177 for EXPR that can be used to compute the types of larger
27178 expressions containing EXPR. */
27179
27180 tree
27181 build_non_dependent_expr (tree expr)
27182 {
27183 tree orig_expr = expr;
27184 tree inner_expr;
27185
27186 /* When checking, try to get a constant value for all non-dependent
27187 expressions in order to expose bugs in *_dependent_expression_p
27188 and constexpr. This can affect code generation, see PR70704, so
27189 only do this for -fchecking=2. */
27190 if (flag_checking > 1
27191 && cxx_dialect >= cxx11
27192 /* Don't do this during nsdmi parsing as it can lead to
27193 unexpected recursive instantiations. */
27194 && !parsing_nsdmi ()
27195 /* Don't do this during concept processing either and for
27196 the same reason. */
27197 && !processing_constraint_expression_p ())
27198 fold_non_dependent_expr (expr, tf_none);
27199
27200 STRIP_ANY_LOCATION_WRAPPER (expr);
27201
27202 /* Preserve OVERLOADs; the functions must be available to resolve
27203 types. */
27204 inner_expr = expr;
27205 if (TREE_CODE (inner_expr) == STMT_EXPR)
27206 inner_expr = stmt_expr_value_expr (inner_expr);
27207 if (TREE_CODE (inner_expr) == ADDR_EXPR)
27208 inner_expr = TREE_OPERAND (inner_expr, 0);
27209 if (TREE_CODE (inner_expr) == COMPONENT_REF)
27210 inner_expr = TREE_OPERAND (inner_expr, 1);
27211 if (is_overloaded_fn (inner_expr)
27212 || TREE_CODE (inner_expr) == OFFSET_REF)
27213 return orig_expr;
27214 /* There is no need to return a proxy for a variable or enumerator. */
27215 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
27216 return orig_expr;
27217 /* Preserve string constants; conversions from string constants to
27218 "char *" are allowed, even though normally a "const char *"
27219 cannot be used to initialize a "char *". */
27220 if (TREE_CODE (expr) == STRING_CST)
27221 return orig_expr;
27222 /* Preserve void and arithmetic constants, as an optimization -- there is no
27223 reason to create a new node. */
27224 if (TREE_CODE (expr) == VOID_CST
27225 || TREE_CODE (expr) == INTEGER_CST
27226 || TREE_CODE (expr) == REAL_CST)
27227 return orig_expr;
27228 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
27229 There is at least one place where we want to know that a
27230 particular expression is a throw-expression: when checking a ?:
27231 expression, there are special rules if the second or third
27232 argument is a throw-expression. */
27233 if (TREE_CODE (expr) == THROW_EXPR)
27234 return orig_expr;
27235
27236 /* Don't wrap an initializer list, we need to be able to look inside. */
27237 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
27238 return orig_expr;
27239
27240 /* Don't wrap a dummy object, we need to be able to test for it. */
27241 if (is_dummy_object (expr))
27242 return orig_expr;
27243
27244 if (TREE_CODE (expr) == COND_EXPR)
27245 return build3 (COND_EXPR,
27246 TREE_TYPE (expr),
27247 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
27248 (TREE_OPERAND (expr, 1)
27249 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
27250 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
27251 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
27252 if (TREE_CODE (expr) == COMPOUND_EXPR
27253 && !COMPOUND_EXPR_OVERLOADED (expr))
27254 return build2 (COMPOUND_EXPR,
27255 TREE_TYPE (expr),
27256 TREE_OPERAND (expr, 0),
27257 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
27258
27259 /* If the type is unknown, it can't really be non-dependent */
27260 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
27261
27262 /* Otherwise, build a NON_DEPENDENT_EXPR. */
27263 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
27264 TREE_TYPE (expr), expr);
27265 }
27266
27267 /* ARGS is a vector of expressions as arguments to a function call.
27268 Replace the arguments with equivalent non-dependent expressions.
27269 This modifies ARGS in place. */
27270
27271 void
27272 make_args_non_dependent (vec<tree, va_gc> *args)
27273 {
27274 unsigned int ix;
27275 tree arg;
27276
27277 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
27278 {
27279 tree newarg = build_non_dependent_expr (arg);
27280 if (newarg != arg)
27281 (*args)[ix] = newarg;
27282 }
27283 }
27284
27285 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
27286 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
27287 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
27288
27289 static tree
27290 make_auto_1 (tree name, bool set_canonical)
27291 {
27292 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
27293 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
27294 TYPE_STUB_DECL (au) = TYPE_NAME (au);
27295 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
27296 (0, processing_template_decl + 1, processing_template_decl + 1,
27297 TYPE_NAME (au), NULL_TREE);
27298 if (set_canonical)
27299 TYPE_CANONICAL (au) = canonical_type_parameter (au);
27300 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
27301 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
27302
27303 return au;
27304 }
27305
27306 tree
27307 make_decltype_auto (void)
27308 {
27309 return make_auto_1 (decltype_auto_identifier, true);
27310 }
27311
27312 tree
27313 make_auto (void)
27314 {
27315 return make_auto_1 (auto_identifier, true);
27316 }
27317
27318 /* Return a C++17 deduction placeholder for class template TMPL. */
27319
27320 tree
27321 make_template_placeholder (tree tmpl)
27322 {
27323 tree t = make_auto_1 (auto_identifier, false);
27324 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
27325 /* Our canonical type depends on the placeholder. */
27326 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27327 return t;
27328 }
27329
27330 /* True iff T is a C++17 class template deduction placeholder. */
27331
27332 bool
27333 template_placeholder_p (tree t)
27334 {
27335 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
27336 }
27337
27338 /* Make a "constrained auto" type-specifier. This is an auto or
27339 decltype(auto) type with constraints that must be associated after
27340 deduction. The constraint is formed from the given concept CON
27341 and its optional sequence of template arguments ARGS.
27342
27343 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
27344
27345 static tree
27346 make_constrained_placeholder_type (tree type, tree con, tree args)
27347 {
27348 /* Build the constraint. */
27349 tree tmpl = DECL_TI_TEMPLATE (con);
27350 tree expr = tmpl;
27351 if (TREE_CODE (con) == FUNCTION_DECL)
27352 expr = ovl_make (tmpl);
27353 expr = build_concept_check (expr, type, args, tf_warning_or_error);
27354
27355 PLACEHOLDER_TYPE_CONSTRAINTS (type) = expr;
27356
27357 /* Our canonical type depends on the constraint. */
27358 TYPE_CANONICAL (type) = canonical_type_parameter (type);
27359
27360 /* Attach the constraint to the type declaration. */
27361 return TYPE_NAME (type);
27362 }
27363
27364 /* Make a "constrained auto" type-specifier. */
27365
27366 tree
27367 make_constrained_auto (tree con, tree args)
27368 {
27369 tree type = make_auto_1 (auto_identifier, false);
27370 return make_constrained_placeholder_type (type, con, args);
27371 }
27372
27373 /* Make a "constrained decltype(auto)" type-specifier. */
27374
27375 tree
27376 make_constrained_decltype_auto (tree con, tree args)
27377 {
27378 tree type = make_auto_1 (decltype_auto_identifier, false);
27379 /* FIXME: I don't know why this isn't done in make_auto_1. */
27380 AUTO_IS_DECLTYPE (type) = true;
27381 return make_constrained_placeholder_type (type, con, args);
27382 }
27383
27384 /* Build and return a concept definition. Like other templates, the
27385 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
27386 the TEMPLATE_DECL. */
27387
27388 tree
27389 finish_concept_definition (cp_expr id, tree init)
27390 {
27391 gcc_assert (identifier_p (id));
27392 gcc_assert (processing_template_decl);
27393
27394 location_t loc = id.get_location();
27395
27396 /* A concept-definition shall not have associated constraints. */
27397 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
27398 {
27399 error_at (loc, "a concept cannot be constrained");
27400 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
27401 }
27402
27403 /* A concept-definition shall appear in namespace scope. Templates
27404 aren't allowed in block scope, so we only need to check for class
27405 scope. */
27406 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
27407 {
27408 error_at (loc, "concept %qE not in namespace scope", *id);
27409 return error_mark_node;
27410 }
27411
27412 /* Initially build the concept declaration; it's type is bool. */
27413 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
27414 DECL_CONTEXT (decl) = current_scope ();
27415 DECL_INITIAL (decl) = init;
27416
27417 /* Push the enclosing template. */
27418 return push_template_decl (decl);
27419 }
27420
27421 /* Given type ARG, return std::initializer_list<ARG>. */
27422
27423 static tree
27424 listify (tree arg)
27425 {
27426 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
27427
27428 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
27429 {
27430 gcc_rich_location richloc (input_location);
27431 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
27432 error_at (&richloc,
27433 "deducing from brace-enclosed initializer list"
27434 " requires %<#include <initializer_list>%>");
27435
27436 return error_mark_node;
27437 }
27438 tree argvec = make_tree_vec (1);
27439 TREE_VEC_ELT (argvec, 0) = arg;
27440
27441 return lookup_template_class (std_init_list, argvec, NULL_TREE,
27442 NULL_TREE, 0, tf_warning_or_error);
27443 }
27444
27445 /* Replace auto in TYPE with std::initializer_list<auto>. */
27446
27447 static tree
27448 listify_autos (tree type, tree auto_node)
27449 {
27450 tree init_auto = listify (strip_top_quals (auto_node));
27451 tree argvec = make_tree_vec (1);
27452 TREE_VEC_ELT (argvec, 0) = init_auto;
27453 if (processing_template_decl)
27454 argvec = add_to_template_args (current_template_args (), argvec);
27455 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
27456 }
27457
27458 /* Hash traits for hashing possibly constrained 'auto'
27459 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
27460
27461 struct auto_hash : default_hash_traits<tree>
27462 {
27463 static inline hashval_t hash (tree);
27464 static inline bool equal (tree, tree);
27465 };
27466
27467 /* Hash the 'auto' T. */
27468
27469 inline hashval_t
27470 auto_hash::hash (tree t)
27471 {
27472 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
27473 /* Matching constrained-type-specifiers denote the same template
27474 parameter, so hash the constraint. */
27475 return hash_placeholder_constraint (c);
27476 else
27477 /* But unconstrained autos are all separate, so just hash the pointer. */
27478 return iterative_hash_object (t, 0);
27479 }
27480
27481 /* Compare two 'auto's. */
27482
27483 inline bool
27484 auto_hash::equal (tree t1, tree t2)
27485 {
27486 if (t1 == t2)
27487 return true;
27488
27489 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
27490 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
27491
27492 /* Two unconstrained autos are distinct. */
27493 if (!c1 || !c2)
27494 return false;
27495
27496 return equivalent_placeholder_constraints (c1, c2);
27497 }
27498
27499 /* for_each_template_parm callback for extract_autos: if t is a (possibly
27500 constrained) auto, add it to the vector. */
27501
27502 static int
27503 extract_autos_r (tree t, void *data)
27504 {
27505 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
27506 if (is_auto (t))
27507 {
27508 /* All the autos were built with index 0; fix that up now. */
27509 tree *p = hash.find_slot (t, INSERT);
27510 unsigned idx;
27511 if (*p)
27512 /* If this is a repeated constrained-type-specifier, use the index we
27513 chose before. */
27514 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
27515 else
27516 {
27517 /* Otherwise this is new, so use the current count. */
27518 *p = t;
27519 idx = hash.elements () - 1;
27520 }
27521 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
27522 }
27523
27524 /* Always keep walking. */
27525 return 0;
27526 }
27527
27528 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
27529 says they can appear anywhere in the type. */
27530
27531 static tree
27532 extract_autos (tree type)
27533 {
27534 hash_set<tree> visited;
27535 hash_table<auto_hash> hash (2);
27536
27537 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
27538
27539 tree tree_vec = make_tree_vec (hash.elements());
27540 for (hash_table<auto_hash>::iterator iter = hash.begin();
27541 iter != hash.end(); ++iter)
27542 {
27543 tree elt = *iter;
27544 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
27545 TREE_VEC_ELT (tree_vec, i)
27546 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
27547 }
27548
27549 return tree_vec;
27550 }
27551
27552 /* The stem for deduction guide names. */
27553 const char *const dguide_base = "__dguide_";
27554
27555 /* Return the name for a deduction guide for class template TMPL. */
27556
27557 tree
27558 dguide_name (tree tmpl)
27559 {
27560 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
27561 tree tname = TYPE_IDENTIFIER (type);
27562 char *buf = (char *) alloca (1 + strlen (dguide_base)
27563 + IDENTIFIER_LENGTH (tname));
27564 memcpy (buf, dguide_base, strlen (dguide_base));
27565 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
27566 IDENTIFIER_LENGTH (tname) + 1);
27567 tree dname = get_identifier (buf);
27568 TREE_TYPE (dname) = type;
27569 return dname;
27570 }
27571
27572 /* True if NAME is the name of a deduction guide. */
27573
27574 bool
27575 dguide_name_p (tree name)
27576 {
27577 return (TREE_CODE (name) == IDENTIFIER_NODE
27578 && TREE_TYPE (name)
27579 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27580 strlen (dguide_base)));
27581 }
27582
27583 /* True if FN is a deduction guide. */
27584
27585 bool
27586 deduction_guide_p (const_tree fn)
27587 {
27588 if (DECL_P (fn))
27589 if (tree name = DECL_NAME (fn))
27590 return dguide_name_p (name);
27591 return false;
27592 }
27593
27594 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27595
27596 bool
27597 copy_guide_p (const_tree fn)
27598 {
27599 gcc_assert (deduction_guide_p (fn));
27600 if (!DECL_ARTIFICIAL (fn))
27601 return false;
27602 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27603 return (TREE_CHAIN (parms) == void_list_node
27604 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27605 }
27606
27607 /* True if FN is a guide generated from a constructor template. */
27608
27609 bool
27610 template_guide_p (const_tree fn)
27611 {
27612 gcc_assert (deduction_guide_p (fn));
27613 if (!DECL_ARTIFICIAL (fn))
27614 return false;
27615 tree tmpl = DECL_TI_TEMPLATE (fn);
27616 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27617 return PRIMARY_TEMPLATE_P (org);
27618 return false;
27619 }
27620
27621 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27622 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27623 template parameter types. Note that the handling of template template
27624 parameters relies on current_template_parms being set appropriately for the
27625 new template. */
27626
27627 static tree
27628 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27629 tree tsubst_args, tsubst_flags_t complain)
27630 {
27631 if (olddecl == error_mark_node)
27632 return error_mark_node;
27633
27634 tree oldidx = get_template_parm_index (olddecl);
27635
27636 tree newtype;
27637 if (TREE_CODE (olddecl) == TYPE_DECL
27638 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27639 {
27640 tree oldtype = TREE_TYPE (olddecl);
27641 newtype = cxx_make_type (TREE_CODE (oldtype));
27642 TYPE_MAIN_VARIANT (newtype) = newtype;
27643 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27644 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27645 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27646 }
27647 else
27648 {
27649 newtype = TREE_TYPE (olddecl);
27650 if (type_uses_auto (newtype))
27651 {
27652 // Substitute once to fix references to other template parameters.
27653 newtype = tsubst (newtype, tsubst_args,
27654 complain|tf_partial, NULL_TREE);
27655 // Now substitute again to reduce the level of the auto.
27656 newtype = tsubst (newtype, current_template_args (),
27657 complain, NULL_TREE);
27658 }
27659 else
27660 newtype = tsubst (newtype, tsubst_args,
27661 complain, NULL_TREE);
27662 }
27663
27664 tree newdecl
27665 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27666 DECL_NAME (olddecl), newtype);
27667 SET_DECL_TEMPLATE_PARM_P (newdecl);
27668
27669 tree newidx;
27670 if (TREE_CODE (olddecl) == TYPE_DECL
27671 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27672 {
27673 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27674 = build_template_parm_index (index, level, level,
27675 newdecl, newtype);
27676 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27677 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27678 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27679 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27680
27681 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27682 {
27683 DECL_TEMPLATE_RESULT (newdecl)
27684 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27685 DECL_NAME (olddecl), newtype);
27686 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27687 // First create a copy (ttargs) of tsubst_args with an
27688 // additional level for the template template parameter's own
27689 // template parameters (ttparms).
27690 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27691 (DECL_TEMPLATE_PARMS (olddecl)));
27692 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27693 tree ttargs = make_tree_vec (depth + 1);
27694 for (int i = 0; i < depth; ++i)
27695 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27696 TREE_VEC_ELT (ttargs, depth)
27697 = template_parms_level_to_args (ttparms);
27698 // Substitute ttargs into ttparms to fix references to
27699 // other template parameters.
27700 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27701 complain|tf_partial);
27702 // Now substitute again with args based on tparms, to reduce
27703 // the level of the ttparms.
27704 ttargs = current_template_args ();
27705 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27706 complain);
27707 // Finally, tack the adjusted parms onto tparms.
27708 ttparms = tree_cons (size_int (depth), ttparms,
27709 current_template_parms);
27710 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27711 }
27712 }
27713 else
27714 {
27715 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27716 tree newconst
27717 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27718 TREE_CODE (oldconst),
27719 DECL_NAME (oldconst), newtype);
27720 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27721 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27722 SET_DECL_TEMPLATE_PARM_P (newconst);
27723 newidx = build_template_parm_index (index, level, level,
27724 newconst, newtype);
27725 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27726 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27727 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27728 }
27729
27730 return newdecl;
27731 }
27732
27733 /* Returns a C++17 class deduction guide template based on the constructor
27734 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27735 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
27736 aggregate initialization guide. */
27737
27738 static tree
27739 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
27740 {
27741 tree tparms, targs, fparms, fargs, ci;
27742 bool memtmpl = false;
27743 bool explicit_p;
27744 location_t loc;
27745 tree fn_tmpl = NULL_TREE;
27746
27747 if (outer_args)
27748 {
27749 ++processing_template_decl;
27750 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
27751 --processing_template_decl;
27752 }
27753
27754 if (!DECL_DECLARES_FUNCTION_P (ctor))
27755 {
27756 if (TYPE_P (ctor))
27757 {
27758 bool copy_p = TYPE_REF_P (ctor);
27759 if (copy_p)
27760 fparms = tree_cons (NULL_TREE, type, void_list_node);
27761 else
27762 fparms = void_list_node;
27763 }
27764 else if (TREE_CODE (ctor) == TREE_LIST)
27765 fparms = ctor;
27766 else
27767 gcc_unreachable ();
27768
27769 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27770 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27771 targs = CLASSTYPE_TI_ARGS (type);
27772 ci = NULL_TREE;
27773 fargs = NULL_TREE;
27774 loc = DECL_SOURCE_LOCATION (ctmpl);
27775 explicit_p = false;
27776 }
27777 else
27778 {
27779 ++processing_template_decl;
27780 bool ok = true;
27781
27782 fn_tmpl
27783 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27784 : DECL_TI_TEMPLATE (ctor));
27785 if (outer_args)
27786 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27787 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27788
27789 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27790 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27791 fully specialized args for the enclosing class. Strip those off, as
27792 the deduction guide won't have those template parameters. */
27793 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27794 TMPL_PARMS_DEPTH (tparms));
27795 /* Discard the 'this' parameter. */
27796 fparms = FUNCTION_ARG_CHAIN (ctor);
27797 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27798 ci = get_constraints (ctor);
27799 loc = DECL_SOURCE_LOCATION (ctor);
27800 explicit_p = DECL_NONCONVERTING_P (ctor);
27801
27802 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27803 {
27804 memtmpl = true;
27805
27806 /* For a member template constructor, we need to flatten the two
27807 template parameter lists into one, and then adjust the function
27808 signature accordingly. This gets...complicated. */
27809 tree save_parms = current_template_parms;
27810
27811 /* For a member template we should have two levels of parms/args, one
27812 for the class and one for the constructor. We stripped
27813 specialized args for further enclosing classes above. */
27814 const int depth = 2;
27815 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27816
27817 /* Template args for translating references to the two-level template
27818 parameters into references to the one-level template parameters we
27819 are creating. */
27820 tree tsubst_args = copy_node (targs);
27821 TMPL_ARGS_LEVEL (tsubst_args, depth)
27822 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27823
27824 /* Template parms for the constructor template. */
27825 tree ftparms = TREE_VALUE (tparms);
27826 unsigned flen = TREE_VEC_LENGTH (ftparms);
27827 /* Template parms for the class template. */
27828 tparms = TREE_CHAIN (tparms);
27829 tree ctparms = TREE_VALUE (tparms);
27830 unsigned clen = TREE_VEC_LENGTH (ctparms);
27831 /* Template parms for the deduction guide start as a copy of the
27832 template parms for the class. We set current_template_parms for
27833 lookup_template_class_1. */
27834 current_template_parms = tparms = copy_node (tparms);
27835 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27836 for (unsigned i = 0; i < clen; ++i)
27837 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27838
27839 /* Now we need to rewrite the constructor parms to append them to the
27840 class parms. */
27841 for (unsigned i = 0; i < flen; ++i)
27842 {
27843 unsigned index = i + clen;
27844 unsigned level = 1;
27845 tree oldelt = TREE_VEC_ELT (ftparms, i);
27846 tree olddecl = TREE_VALUE (oldelt);
27847 tree newdecl = rewrite_template_parm (olddecl, index, level,
27848 tsubst_args, complain);
27849 if (newdecl == error_mark_node)
27850 ok = false;
27851 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27852 tsubst_args, complain, ctor);
27853 tree list = build_tree_list (newdef, newdecl);
27854 TEMPLATE_PARM_CONSTRAINTS (list)
27855 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27856 tsubst_args, complain, ctor);
27857 TREE_VEC_ELT (new_vec, index) = list;
27858 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27859 }
27860
27861 /* Now we have a final set of template parms to substitute into the
27862 function signature. */
27863 targs = template_parms_to_args (tparms);
27864 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27865 complain, ctor);
27866 if (fparms == error_mark_node)
27867 ok = false;
27868 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27869 if (ci)
27870 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27871
27872 current_template_parms = save_parms;
27873 }
27874
27875 --processing_template_decl;
27876 if (!ok)
27877 return error_mark_node;
27878 }
27879
27880 if (!memtmpl)
27881 {
27882 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27883 tparms = copy_node (tparms);
27884 INNERMOST_TEMPLATE_PARMS (tparms)
27885 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27886 }
27887
27888 tree fntype = build_function_type (type, fparms);
27889 tree ded_fn = build_lang_decl_loc (loc,
27890 FUNCTION_DECL,
27891 dguide_name (type), fntype);
27892 DECL_ARGUMENTS (ded_fn) = fargs;
27893 DECL_ARTIFICIAL (ded_fn) = true;
27894 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27895 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27896 DECL_ARTIFICIAL (ded_tmpl) = true;
27897 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27898 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27899 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27900 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27901 if (DECL_P (ctor))
27902 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27903 if (ci)
27904 set_constraints (ded_tmpl, ci);
27905
27906 return ded_tmpl;
27907 }
27908
27909 /* Add to LIST the member types for the reshaped initializer CTOR. */
27910
27911 static tree
27912 collect_ctor_idx_types (tree ctor, tree list)
27913 {
27914 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
27915 tree idx, val; unsigned i;
27916 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
27917 {
27918 if (BRACE_ENCLOSED_INITIALIZER_P (val)
27919 && CONSTRUCTOR_NELTS (val))
27920 if (tree subidx = CONSTRUCTOR_ELT (val, 0)->index)
27921 if (TREE_CODE (subidx) == FIELD_DECL)
27922 {
27923 list = collect_ctor_idx_types (val, list);
27924 continue;
27925 }
27926 tree ftype = finish_decltype_type (idx, true, tf_none);
27927 list = tree_cons (NULL_TREE, ftype, list);
27928 }
27929
27930 return list;
27931 }
27932
27933 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
27934 INIT. */
27935
27936 static tree
27937 maybe_aggr_guide (tree type, tree init)
27938 {
27939 if (cxx_dialect < cxx2a)
27940 return NULL_TREE;
27941
27942 if (init == NULL_TREE)
27943 return NULL_TREE;
27944 if (!CP_AGGREGATE_TYPE_P (type))
27945 return NULL_TREE;
27946
27947 /* If we encounter a problem, we just won't add the candidate. */
27948 tsubst_flags_t complain = tf_none;
27949
27950 tree parms = NULL_TREE;
27951 if (TREE_CODE (init) == CONSTRUCTOR)
27952 {
27953 init = reshape_init (type, init, complain);
27954 if (init == error_mark_node)
27955 return NULL_TREE;
27956 parms = collect_ctor_idx_types (init, parms);
27957 }
27958 else if (TREE_CODE (init) == TREE_LIST)
27959 {
27960 int len = list_length (init);
27961 for (tree field = TYPE_FIELDS (type);
27962 len;
27963 --len, field = DECL_CHAIN (field))
27964 {
27965 field = next_initializable_field (field);
27966 if (!field)
27967 return NULL_TREE;
27968 tree ftype = finish_decltype_type (field, true, complain);
27969 parms = tree_cons (NULL_TREE, ftype, parms);
27970 }
27971 }
27972 else
27973 /* Aggregate initialization doesn't apply to an initializer expression. */
27974 return NULL_TREE;
27975
27976 if (parms)
27977 {
27978 tree last = parms;
27979 parms = nreverse (parms);
27980 TREE_CHAIN (last) = void_list_node;
27981 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
27982 return guide;
27983 }
27984
27985 return NULL_TREE;
27986 }
27987
27988 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
27989
27990 static bool
27991 is_spec_or_derived (tree etype, tree tmpl)
27992 {
27993 if (!etype || !CLASS_TYPE_P (etype))
27994 return false;
27995
27996 tree type = TREE_TYPE (tmpl);
27997 tree tparms = (INNERMOST_TEMPLATE_PARMS
27998 (DECL_TEMPLATE_PARMS (tmpl)));
27999 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28000 int err = unify (tparms, targs, type, etype,
28001 UNIFY_ALLOW_DERIVED, /*explain*/false);
28002 ggc_free (targs);
28003 return !err;
28004 }
28005
28006 /* Deduce template arguments for the class template placeholder PTYPE for
28007 template TMPL based on the initializer INIT, and return the resulting
28008 type. */
28009
28010 static tree
28011 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
28012 tsubst_flags_t complain)
28013 {
28014 if (!DECL_CLASS_TEMPLATE_P (tmpl))
28015 {
28016 /* We should have handled this in the caller. */
28017 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
28018 return ptype;
28019 if (complain & tf_error)
28020 error ("non-class template %qT used without template arguments", tmpl);
28021 return error_mark_node;
28022 }
28023 if (init && TREE_TYPE (init) == ptype)
28024 /* Using the template parm as its own argument. */
28025 return ptype;
28026
28027 tree type = TREE_TYPE (tmpl);
28028
28029 bool try_list_ctor = false;
28030 bool copy_init = false;
28031
28032 releasing_vec rv_args = NULL;
28033 vec<tree,va_gc> *&args = *&rv_args;
28034 if (init == NULL_TREE)
28035 args = make_tree_vector ();
28036 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
28037 {
28038 if (CONSTRUCTOR_NELTS (init) == 1)
28039 {
28040 /* As an exception, the first phase in 16.3.1.7 (considering the
28041 initializer list as a single argument) is omitted if the
28042 initializer list consists of a single expression of type cv U,
28043 where U is a specialization of C or a class derived from a
28044 specialization of C. */
28045 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
28046 copy_init = is_spec_or_derived (TREE_TYPE (elt), tmpl);
28047 }
28048 try_list_ctor = !copy_init && TYPE_HAS_LIST_CTOR (type);
28049 if (try_list_ctor || is_std_init_list (type))
28050 args = make_tree_vector_single (init);
28051 else
28052 args = make_tree_vector_from_ctor (init);
28053 }
28054 else
28055 {
28056 if (TREE_CODE (init) == TREE_LIST)
28057 args = make_tree_vector_from_list (init);
28058 else
28059 args = make_tree_vector_single (init);
28060
28061 if (args->length() == 1)
28062 copy_init = is_spec_or_derived (TREE_TYPE ((*args)[0]), tmpl);
28063 }
28064
28065 tree dname = dguide_name (tmpl);
28066 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
28067 /*type*/false, /*complain*/false,
28068 /*hidden*/false);
28069 bool elided = false;
28070 if (cands == error_mark_node)
28071 cands = NULL_TREE;
28072
28073 /* Prune explicit deduction guides in copy-initialization context. */
28074 if (flags & LOOKUP_ONLYCONVERTING)
28075 {
28076 for (lkp_iterator iter (cands); !elided && iter; ++iter)
28077 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28078 elided = true;
28079
28080 if (elided)
28081 {
28082 /* Found a nonconverting guide, prune the candidates. */
28083 tree pruned = NULL_TREE;
28084 for (lkp_iterator iter (cands); iter; ++iter)
28085 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
28086 pruned = lookup_add (*iter, pruned);
28087
28088 cands = pruned;
28089 }
28090 }
28091
28092 tree outer_args = NULL_TREE;
28093 if (DECL_CLASS_SCOPE_P (tmpl)
28094 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
28095 {
28096 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
28097 type = TREE_TYPE (most_general_template (tmpl));
28098 }
28099
28100 bool saw_ctor = false;
28101 // FIXME cache artificial deduction guides
28102 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
28103 {
28104 /* Skip inherited constructors. */
28105 if (iter.using_p ())
28106 continue;
28107
28108 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
28109 if (guide == error_mark_node)
28110 return error_mark_node;
28111 if ((flags & LOOKUP_ONLYCONVERTING)
28112 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
28113 elided = true;
28114 else
28115 cands = lookup_add (guide, cands);
28116
28117 saw_ctor = true;
28118 }
28119
28120 if (!copy_init)
28121 if (tree guide = maybe_aggr_guide (type, init))
28122 cands = lookup_add (guide, cands);
28123
28124 tree call = error_mark_node;
28125
28126 /* If this is list-initialization and the class has a list constructor, first
28127 try deducing from the list as a single argument, as [over.match.list]. */
28128 tree list_cands = NULL_TREE;
28129 if (try_list_ctor && cands)
28130 for (lkp_iterator iter (cands); iter; ++iter)
28131 {
28132 tree dg = *iter;
28133 if (is_list_ctor (dg))
28134 list_cands = lookup_add (dg, list_cands);
28135 }
28136 if (list_cands)
28137 {
28138 ++cp_unevaluated_operand;
28139 call = build_new_function_call (list_cands, &args, tf_decltype);
28140 --cp_unevaluated_operand;
28141
28142 if (call == error_mark_node)
28143 {
28144 /* That didn't work, now try treating the list as a sequence of
28145 arguments. */
28146 release_tree_vector (args);
28147 args = make_tree_vector_from_ctor (init);
28148 }
28149 }
28150
28151 /* Maybe generate an implicit deduction guide. */
28152 if (call == error_mark_node && args->length () < 2)
28153 {
28154 tree gtype = NULL_TREE;
28155
28156 if (args->length () == 1)
28157 /* Generate a copy guide. */
28158 gtype = build_reference_type (type);
28159 else if (!saw_ctor)
28160 /* Generate a default guide. */
28161 gtype = type;
28162
28163 if (gtype)
28164 {
28165 tree guide = build_deduction_guide (type, gtype, outer_args,
28166 complain);
28167 if (guide == error_mark_node)
28168 return error_mark_node;
28169 cands = lookup_add (guide, cands);
28170 }
28171 }
28172
28173 if (elided && !cands)
28174 {
28175 error ("cannot deduce template arguments for copy-initialization"
28176 " of %qT, as it has no non-explicit deduction guides or "
28177 "user-declared constructors", type);
28178 return error_mark_node;
28179 }
28180 else if (!cands && call == error_mark_node)
28181 {
28182 error ("cannot deduce template arguments of %qT, as it has no viable "
28183 "deduction guides", type);
28184 return error_mark_node;
28185 }
28186
28187 if (call == error_mark_node)
28188 {
28189 ++cp_unevaluated_operand;
28190 call = build_new_function_call (cands, &args, tf_decltype);
28191 --cp_unevaluated_operand;
28192 }
28193
28194 if (call == error_mark_node && (complain & tf_warning_or_error))
28195 {
28196 error ("class template argument deduction failed:");
28197
28198 ++cp_unevaluated_operand;
28199 call = build_new_function_call (cands, &args, complain | tf_decltype);
28200 --cp_unevaluated_operand;
28201
28202 if (elided)
28203 inform (input_location, "explicit deduction guides not considered "
28204 "for copy-initialization");
28205 }
28206
28207 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
28208 }
28209
28210 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
28211 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
28212 The CONTEXT determines the context in which auto deduction is performed
28213 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
28214 OUTER_TARGS are used during template argument deduction
28215 (context == adc_unify) to properly substitute the result, and is ignored
28216 in other contexts.
28217
28218 For partial-concept-ids, extra args may be appended to the list of deduced
28219 template arguments prior to determining constraint satisfaction. */
28220
28221 tree
28222 do_auto_deduction (tree type, tree init, tree auto_node,
28223 tsubst_flags_t complain, auto_deduction_context context,
28224 tree outer_targs, int flags)
28225 {
28226 tree targs;
28227
28228 if (init == error_mark_node)
28229 return error_mark_node;
28230
28231 if (init && type_dependent_expression_p (init)
28232 && context != adc_unify)
28233 /* Defining a subset of type-dependent expressions that we can deduce
28234 from ahead of time isn't worth the trouble. */
28235 return type;
28236
28237 /* Similarly, we can't deduce from another undeduced decl. */
28238 if (init && undeduced_auto_decl (init))
28239 return type;
28240
28241 /* We may be doing a partial substitution, but we still want to replace
28242 auto_node. */
28243 complain &= ~tf_partial;
28244
28245 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
28246 /* C++17 class template argument deduction. */
28247 return do_class_deduction (type, tmpl, init, flags, complain);
28248
28249 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
28250 /* Nothing we can do with this, even in deduction context. */
28251 return type;
28252
28253 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
28254 with either a new invented type template parameter U or, if the
28255 initializer is a braced-init-list (8.5.4), with
28256 std::initializer_list<U>. */
28257 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28258 {
28259 if (!DIRECT_LIST_INIT_P (init))
28260 type = listify_autos (type, auto_node);
28261 else if (CONSTRUCTOR_NELTS (init) == 1)
28262 init = CONSTRUCTOR_ELT (init, 0)->value;
28263 else
28264 {
28265 if (complain & tf_warning_or_error)
28266 {
28267 if (permerror (input_location, "direct-list-initialization of "
28268 "%<auto%> requires exactly one element"))
28269 inform (input_location,
28270 "for deduction to %<std::initializer_list%>, use copy-"
28271 "list-initialization (i.e. add %<=%> before the %<{%>)");
28272 }
28273 type = listify_autos (type, auto_node);
28274 }
28275 }
28276
28277 if (type == error_mark_node)
28278 return error_mark_node;
28279
28280 init = resolve_nondeduced_context (init, complain);
28281
28282 if (context == adc_decomp_type
28283 && auto_node == type
28284 && init != error_mark_node
28285 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
28286 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
28287 and initializer has array type, deduce cv-qualified array type. */
28288 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
28289 complain);
28290 else if (AUTO_IS_DECLTYPE (auto_node))
28291 {
28292 tree stripped_init = tree_strip_any_location_wrapper (init);
28293 bool id = (DECL_P (stripped_init)
28294 || ((TREE_CODE (init) == COMPONENT_REF
28295 || TREE_CODE (init) == SCOPE_REF)
28296 && !REF_PARENTHESIZED_P (init)));
28297 targs = make_tree_vec (1);
28298 TREE_VEC_ELT (targs, 0)
28299 = finish_decltype_type (init, id, tf_warning_or_error);
28300 if (type != auto_node)
28301 {
28302 if (complain & tf_error)
28303 error ("%qT as type rather than plain %<decltype(auto)%>", type);
28304 return error_mark_node;
28305 }
28306 }
28307 else
28308 {
28309 if (error_operand_p (init))
28310 return error_mark_node;
28311
28312 tree parms = build_tree_list (NULL_TREE, type);
28313 tree tparms;
28314
28315 if (flag_concepts)
28316 tparms = extract_autos (type);
28317 else
28318 {
28319 tparms = make_tree_vec (1);
28320 TREE_VEC_ELT (tparms, 0)
28321 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
28322 }
28323
28324 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28325 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
28326 DEDUCE_CALL,
28327 NULL, /*explain_p=*/false);
28328 if (val > 0)
28329 {
28330 if (processing_template_decl)
28331 /* Try again at instantiation time. */
28332 return type;
28333 if (type && type != error_mark_node
28334 && (complain & tf_error))
28335 /* If type is error_mark_node a diagnostic must have been
28336 emitted by now. Also, having a mention to '<type error>'
28337 in the diagnostic is not really useful to the user. */
28338 {
28339 if (cfun
28340 && FNDECL_USED_AUTO (current_function_decl)
28341 && (auto_node
28342 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
28343 && LAMBDA_FUNCTION_P (current_function_decl))
28344 error ("unable to deduce lambda return type from %qE", init);
28345 else
28346 error ("unable to deduce %qT from %qE", type, init);
28347 type_unification_real (tparms, targs, parms, &init, 1, 0,
28348 DEDUCE_CALL,
28349 NULL, /*explain_p=*/true);
28350 }
28351 return error_mark_node;
28352 }
28353 }
28354
28355 /* Check any placeholder constraints against the deduced type. */
28356 if (flag_concepts && !processing_template_decl)
28357 if (tree check = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
28358 {
28359 /* Use the deduced type to check the associated constraints. If we
28360 have a partial-concept-id, rebuild the argument list so that
28361 we check using the extra arguments. */
28362 check = unpack_concept_check (check);
28363 gcc_assert (TREE_CODE (check) == TEMPLATE_ID_EXPR);
28364 tree cdecl = TREE_OPERAND (check, 0);
28365 if (OVL_P (cdecl))
28366 cdecl = OVL_FIRST (cdecl);
28367 tree cargs = TREE_OPERAND (check, 1);
28368 if (TREE_VEC_LENGTH (cargs) > 1)
28369 {
28370 cargs = copy_node (cargs);
28371 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
28372 }
28373 else
28374 cargs = targs;
28375
28376 /* Rebuild the check using the deduced arguments. */
28377 check = build_concept_check (cdecl, cargs, tf_none);
28378
28379 if (!constraints_satisfied_p (check))
28380 {
28381 if (complain & tf_warning_or_error)
28382 {
28383 auto_diagnostic_group d;
28384 switch (context)
28385 {
28386 case adc_unspecified:
28387 case adc_unify:
28388 error("placeholder constraints not satisfied");
28389 break;
28390 case adc_variable_type:
28391 case adc_decomp_type:
28392 error ("deduced initializer does not satisfy "
28393 "placeholder constraints");
28394 break;
28395 case adc_return_type:
28396 error ("deduced return type does not satisfy "
28397 "placeholder constraints");
28398 break;
28399 case adc_requirement:
28400 error ("deduced expression type does not satisfy "
28401 "placeholder constraints");
28402 break;
28403 }
28404 diagnose_constraints (input_location, check, targs);
28405 }
28406 return error_mark_node;
28407 }
28408 }
28409
28410 if (processing_template_decl && context != adc_unify)
28411 outer_targs = current_template_args ();
28412 targs = add_to_template_args (outer_targs, targs);
28413 return tsubst (type, targs, complain, NULL_TREE);
28414 }
28415
28416 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
28417 result. */
28418
28419 tree
28420 splice_late_return_type (tree type, tree late_return_type)
28421 {
28422 if (is_auto (type))
28423 {
28424 if (late_return_type)
28425 return late_return_type;
28426
28427 tree idx = get_template_parm_index (type);
28428 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
28429 /* In an abbreviated function template we didn't know we were dealing
28430 with a function template when we saw the auto return type, so update
28431 it to have the correct level. */
28432 return make_auto_1 (TYPE_IDENTIFIER (type), true);
28433 }
28434 return type;
28435 }
28436
28437 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
28438 'decltype(auto)' or a deduced class template. */
28439
28440 bool
28441 is_auto (const_tree type)
28442 {
28443 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
28444 && (TYPE_IDENTIFIER (type) == auto_identifier
28445 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
28446 return true;
28447 else
28448 return false;
28449 }
28450
28451 /* for_each_template_parm callback for type_uses_auto. */
28452
28453 int
28454 is_auto_r (tree tp, void */*data*/)
28455 {
28456 return is_auto (tp);
28457 }
28458
28459 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
28460 a use of `auto'. Returns NULL_TREE otherwise. */
28461
28462 tree
28463 type_uses_auto (tree type)
28464 {
28465 if (type == NULL_TREE)
28466 return NULL_TREE;
28467 else if (flag_concepts)
28468 {
28469 /* The Concepts TS allows multiple autos in one type-specifier; just
28470 return the first one we find, do_auto_deduction will collect all of
28471 them. */
28472 if (uses_template_parms (type))
28473 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
28474 /*visited*/NULL, /*nondeduced*/false);
28475 else
28476 return NULL_TREE;
28477 }
28478 else
28479 return find_type_usage (type, is_auto);
28480 }
28481
28482 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
28483 concepts are enabled, auto is acceptable in template arguments, but
28484 only when TEMPL identifies a template class. Return TRUE if any
28485 such errors were reported. */
28486
28487 bool
28488 check_auto_in_tmpl_args (tree tmpl, tree args)
28489 {
28490 /* If there were previous errors, nevermind. */
28491 if (!args || TREE_CODE (args) != TREE_VEC)
28492 return false;
28493
28494 /* If TMPL is an identifier, we're parsing and we can't tell yet
28495 whether TMPL is supposed to be a type, a function or a variable.
28496 We'll only be able to tell during template substitution, so we
28497 expect to be called again then. If concepts are enabled and we
28498 know we have a type, we're ok. */
28499 if (flag_concepts
28500 && (identifier_p (tmpl)
28501 || (DECL_P (tmpl)
28502 && (DECL_TYPE_TEMPLATE_P (tmpl)
28503 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
28504 return false;
28505
28506 /* Quickly search for any occurrences of auto; usually there won't
28507 be any, and then we'll avoid allocating the vector. */
28508 if (!type_uses_auto (args))
28509 return false;
28510
28511 bool errors = false;
28512
28513 tree vec = extract_autos (args);
28514 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
28515 {
28516 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
28517 error_at (DECL_SOURCE_LOCATION (xauto),
28518 "invalid use of %qT in template argument", xauto);
28519 errors = true;
28520 }
28521
28522 return errors;
28523 }
28524
28525 /* For a given template T, return the vector of typedefs referenced
28526 in T for which access check is needed at T instantiation time.
28527 T is either a FUNCTION_DECL or a RECORD_TYPE.
28528 Those typedefs were added to T by the function
28529 append_type_to_template_for_access_check. */
28530
28531 vec<qualified_typedef_usage_t, va_gc> *
28532 get_types_needing_access_check (tree t)
28533 {
28534 tree ti;
28535 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
28536
28537 if (!t || t == error_mark_node)
28538 return NULL;
28539
28540 if (!(ti = get_template_info (t)))
28541 return NULL;
28542
28543 if (CLASS_TYPE_P (t)
28544 || TREE_CODE (t) == FUNCTION_DECL)
28545 {
28546 if (!TI_TEMPLATE (ti))
28547 return NULL;
28548
28549 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
28550 }
28551
28552 return result;
28553 }
28554
28555 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
28556 tied to T. That list of typedefs will be access checked at
28557 T instantiation time.
28558 T is either a FUNCTION_DECL or a RECORD_TYPE.
28559 TYPE_DECL is a TYPE_DECL node representing a typedef.
28560 SCOPE is the scope through which TYPE_DECL is accessed.
28561 LOCATION is the location of the usage point of TYPE_DECL.
28562
28563 This function is a subroutine of
28564 append_type_to_template_for_access_check. */
28565
28566 static void
28567 append_type_to_template_for_access_check_1 (tree t,
28568 tree type_decl,
28569 tree scope,
28570 location_t location)
28571 {
28572 qualified_typedef_usage_t typedef_usage;
28573 tree ti;
28574
28575 if (!t || t == error_mark_node)
28576 return;
28577
28578 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
28579 || CLASS_TYPE_P (t))
28580 && type_decl
28581 && TREE_CODE (type_decl) == TYPE_DECL
28582 && scope);
28583
28584 if (!(ti = get_template_info (t)))
28585 return;
28586
28587 gcc_assert (TI_TEMPLATE (ti));
28588
28589 typedef_usage.typedef_decl = type_decl;
28590 typedef_usage.context = scope;
28591 typedef_usage.locus = location;
28592
28593 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
28594 }
28595
28596 /* Append TYPE_DECL to the template TEMPL.
28597 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
28598 At TEMPL instanciation time, TYPE_DECL will be checked to see
28599 if it can be accessed through SCOPE.
28600 LOCATION is the location of the usage point of TYPE_DECL.
28601
28602 e.g. consider the following code snippet:
28603
28604 class C
28605 {
28606 typedef int myint;
28607 };
28608
28609 template<class U> struct S
28610 {
28611 C::myint mi; // <-- usage point of the typedef C::myint
28612 };
28613
28614 S<char> s;
28615
28616 At S<char> instantiation time, we need to check the access of C::myint
28617 In other words, we need to check the access of the myint typedef through
28618 the C scope. For that purpose, this function will add the myint typedef
28619 and the scope C through which its being accessed to a list of typedefs
28620 tied to the template S. That list will be walked at template instantiation
28621 time and access check performed on each typedefs it contains.
28622 Note that this particular code snippet should yield an error because
28623 myint is private to C. */
28624
28625 void
28626 append_type_to_template_for_access_check (tree templ,
28627 tree type_decl,
28628 tree scope,
28629 location_t location)
28630 {
28631 qualified_typedef_usage_t *iter;
28632 unsigned i;
28633
28634 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
28635
28636 /* Make sure we don't append the type to the template twice. */
28637 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
28638 if (iter->typedef_decl == type_decl && scope == iter->context)
28639 return;
28640
28641 append_type_to_template_for_access_check_1 (templ, type_decl,
28642 scope, location);
28643 }
28644
28645 /* Recursively walk over && expressions searching for EXPR. Return a reference
28646 to that expression. */
28647
28648 static tree *find_template_requirement (tree *t, tree key)
28649 {
28650 if (*t == key)
28651 return t;
28652 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
28653 {
28654 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
28655 return p;
28656 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
28657 return p;
28658 }
28659 return 0;
28660 }
28661
28662 /* Convert the generic type parameters in PARM that match the types given in the
28663 range [START_IDX, END_IDX) from the current_template_parms into generic type
28664 packs. */
28665
28666 tree
28667 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
28668 {
28669 tree current = current_template_parms;
28670 int depth = TMPL_PARMS_DEPTH (current);
28671 current = INNERMOST_TEMPLATE_PARMS (current);
28672 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
28673
28674 for (int i = 0; i < start_idx; ++i)
28675 TREE_VEC_ELT (replacement, i)
28676 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28677
28678 for (int i = start_idx; i < end_idx; ++i)
28679 {
28680 /* Create a distinct parameter pack type from the current parm and add it
28681 to the replacement args to tsubst below into the generic function
28682 parameter. */
28683 tree node = TREE_VEC_ELT (current, i);
28684 tree o = TREE_TYPE (TREE_VALUE (node));
28685 tree t = copy_type (o);
28686 TEMPLATE_TYPE_PARM_INDEX (t)
28687 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
28688 t, 0, 0, tf_none);
28689 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
28690 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
28691 TYPE_MAIN_VARIANT (t) = t;
28692 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
28693 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28694 TREE_VEC_ELT (replacement, i) = t;
28695
28696 /* Replace the current template parameter with new pack. */
28697 TREE_VALUE (node) = TREE_CHAIN (t);
28698
28699 /* Surgically adjust the associated constraint of adjusted parameter
28700 and it's corresponding contribution to the current template
28701 requirements. */
28702 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
28703 {
28704 tree id = unpack_concept_check (constr);
28705 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = template_parm_to_arg (t);
28706 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
28707 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
28708
28709 /* If there was a constraint, we also need to replace that in
28710 the template requirements, which we've already built. */
28711 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
28712 reqs = find_template_requirement (reqs, constr);
28713 *reqs = fold;
28714 }
28715 }
28716
28717 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
28718 TREE_VEC_ELT (replacement, i)
28719 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
28720
28721 /* If there are more levels then build up the replacement with the outer
28722 template parms. */
28723 if (depth > 1)
28724 replacement = add_to_template_args (template_parms_to_args
28725 (TREE_CHAIN (current_template_parms)),
28726 replacement);
28727
28728 return tsubst (parm, replacement, tf_none, NULL_TREE);
28729 }
28730
28731 GTY(()) tree current_failed_constraint;
28732
28733 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28734 0..N-1. */
28735
28736 void
28737 declare_integer_pack (void)
28738 {
28739 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28740 build_function_type_list (integer_type_node,
28741 integer_type_node,
28742 NULL_TREE),
28743 NULL_TREE, ECF_CONST);
28744 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28745 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
28746 CP_BUILT_IN_INTEGER_PACK);
28747 }
28748
28749 /* Set up the hash tables for template instantiations. */
28750
28751 void
28752 init_template_processing (void)
28753 {
28754 /* FIXME: enable sanitization (PR87847) */
28755 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28756 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28757
28758 if (cxx_dialect >= cxx11)
28759 declare_integer_pack ();
28760 }
28761
28762 /* Print stats about the template hash tables for -fstats. */
28763
28764 void
28765 print_template_statistics (void)
28766 {
28767 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28768 "%f collisions\n", (long) decl_specializations->size (),
28769 (long) decl_specializations->elements (),
28770 decl_specializations->collisions ());
28771 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28772 "%f collisions\n", (long) type_specializations->size (),
28773 (long) type_specializations->elements (),
28774 type_specializations->collisions ());
28775 }
28776
28777 #if CHECKING_P
28778
28779 namespace selftest {
28780
28781 /* Verify that build_non_dependent_expr () works, for various expressions,
28782 and that location wrappers don't affect the results. */
28783
28784 static void
28785 test_build_non_dependent_expr ()
28786 {
28787 location_t loc = BUILTINS_LOCATION;
28788
28789 /* Verify constants, without and with location wrappers. */
28790 tree int_cst = build_int_cst (integer_type_node, 42);
28791 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28792
28793 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28794 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28795 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28796
28797 tree string_lit = build_string (4, "foo");
28798 TREE_TYPE (string_lit) = char_array_type_node;
28799 string_lit = fix_string_type (string_lit);
28800 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28801
28802 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28803 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28804 ASSERT_EQ (wrapped_string_lit,
28805 build_non_dependent_expr (wrapped_string_lit));
28806 }
28807
28808 /* Verify that type_dependent_expression_p () works correctly, even
28809 in the presence of location wrapper nodes. */
28810
28811 static void
28812 test_type_dependent_expression_p ()
28813 {
28814 location_t loc = BUILTINS_LOCATION;
28815
28816 tree name = get_identifier ("foo");
28817
28818 /* If no templates are involved, nothing is type-dependent. */
28819 gcc_assert (!processing_template_decl);
28820 ASSERT_FALSE (type_dependent_expression_p (name));
28821
28822 ++processing_template_decl;
28823
28824 /* Within a template, an unresolved name is always type-dependent. */
28825 ASSERT_TRUE (type_dependent_expression_p (name));
28826
28827 /* Ensure it copes with NULL_TREE and errors. */
28828 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28829 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28830
28831 /* A USING_DECL in a template should be type-dependent, even if wrapped
28832 with a location wrapper (PR c++/83799). */
28833 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28834 TREE_TYPE (using_decl) = integer_type_node;
28835 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28836 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28837 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28838 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28839
28840 --processing_template_decl;
28841 }
28842
28843 /* Run all of the selftests within this file. */
28844
28845 void
28846 cp_pt_c_tests ()
28847 {
28848 test_build_non_dependent_expr ();
28849 test_type_dependent_expression_p ();
28850 }
28851
28852 } // namespace selftest
28853
28854 #endif /* #if CHECKING_P */
28855
28856 #include "gt-cp-pt.h"