]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
tree.def (OMP_LOOP): New tree code.
[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(()) tree 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 void push_access_scope (tree);
139 static void pop_access_scope (tree);
140 static bool resolve_overloaded_unification (tree, tree, tree, tree,
141 unification_kind_t, int,
142 bool);
143 static int try_one_overload (tree, tree, tree, tree, tree,
144 unification_kind_t, int, bool, bool);
145 static int unify (tree, tree, tree, tree, int, bool);
146 static void add_pending_template (tree);
147 static tree reopen_tinst_level (struct tinst_level *);
148 static tree tsubst_initializer_list (tree, tree);
149 static tree get_partial_spec_bindings (tree, tree, tree);
150 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
153 bool, bool);
154 static void tsubst_enum (tree, tree, tree);
155 static tree add_to_template_args (tree, tree);
156 static tree add_outermost_template_args (tree, tree);
157 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
158 static int check_non_deducible_conversion (tree, tree, int, int,
159 struct conversion **, bool);
160 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
161 tree);
162 static int type_unification_real (tree, tree, tree, const tree *,
163 unsigned int, int, unification_kind_t,
164 vec<deferred_access_check, va_gc> **,
165 bool);
166 static void note_template_header (int);
167 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
168 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
169 static tree convert_template_argument (tree, tree, tree,
170 tsubst_flags_t, int, tree);
171 static tree for_each_template_parm (tree, tree_fn_t, void*,
172 hash_set<tree> *, bool, tree_fn_t = NULL);
173 static tree expand_template_argument_pack (tree);
174 static tree build_template_parm_index (int, int, int, tree, tree);
175 static bool inline_needs_template_parms (tree, bool);
176 static void push_inline_template_parms_recursive (tree, int);
177 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
178 static int mark_template_parm (tree, void *);
179 static int template_parm_this_level_p (tree, void *);
180 static tree tsubst_friend_function (tree, tree);
181 static tree tsubst_friend_class (tree, tree);
182 static int can_complete_type_without_circularity (tree);
183 static tree get_bindings (tree, tree, tree, bool);
184 static int template_decl_level (tree);
185 static int check_cv_quals_for_unify (int, tree, tree);
186 static void template_parm_level_and_index (tree, int*, int*);
187 static int unify_pack_expansion (tree, tree, tree,
188 tree, unification_kind_t, bool, bool);
189 static tree copy_template_args (tree);
190 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
191 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
193 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
194 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
195 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
196 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
197 static bool check_specialization_scope (void);
198 static tree process_partial_specialization (tree);
199 static void set_current_access_from_decl (tree);
200 static enum template_base_result get_template_base (tree, tree, tree, tree,
201 bool , tree *);
202 static tree try_class_unification (tree, tree, tree, tree, bool);
203 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
204 tree, tree);
205 static bool template_template_parm_bindings_ok_p (tree, tree);
206 static void tsubst_default_arguments (tree, tsubst_flags_t);
207 static tree for_each_template_parm_r (tree *, int *, void *);
208 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
209 static void copy_default_args_to_explicit_spec (tree);
210 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
211 static bool dependent_template_arg_p (tree);
212 static bool any_template_arguments_need_structural_equality_p (tree);
213 static bool dependent_type_p_r (tree);
214 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
215 static tree tsubst_decl (tree, tree, tsubst_flags_t);
216 static void perform_typedefs_access_check (tree tmpl, tree targs);
217 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
218 location_t);
219 static tree listify (tree);
220 static tree listify_autos (tree, tree);
221 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
222 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
223 static bool complex_alias_template_p (const_tree tmpl);
224 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
225 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
226 static tree make_argument_pack (tree);
227 static void register_parameter_specializations (tree, tree);
228 static tree enclosing_instantiation_of (tree tctx);
229
230 /* Make the current scope suitable for access checking when we are
231 processing T. T can be FUNCTION_DECL for instantiated function
232 template, VAR_DECL for static member variable, or TYPE_DECL for
233 alias template (needed by instantiate_decl). */
234
235 static void
236 push_access_scope (tree t)
237 {
238 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
239 || TREE_CODE (t) == TYPE_DECL);
240
241 if (DECL_FRIEND_CONTEXT (t))
242 push_nested_class (DECL_FRIEND_CONTEXT (t));
243 else if (DECL_CLASS_SCOPE_P (t))
244 push_nested_class (DECL_CONTEXT (t));
245 else
246 push_to_top_level ();
247
248 if (TREE_CODE (t) == FUNCTION_DECL)
249 {
250 saved_access_scope = tree_cons
251 (NULL_TREE, current_function_decl, saved_access_scope);
252 current_function_decl = t;
253 }
254 }
255
256 /* Restore the scope set up by push_access_scope. T is the node we
257 are processing. */
258
259 static void
260 pop_access_scope (tree t)
261 {
262 if (TREE_CODE (t) == FUNCTION_DECL)
263 {
264 current_function_decl = TREE_VALUE (saved_access_scope);
265 saved_access_scope = TREE_CHAIN (saved_access_scope);
266 }
267
268 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
269 pop_nested_class ();
270 else
271 pop_from_top_level ();
272 }
273
274 /* Do any processing required when DECL (a member template
275 declaration) is finished. Returns the TEMPLATE_DECL corresponding
276 to DECL, unless it is a specialization, in which case the DECL
277 itself is returned. */
278
279 tree
280 finish_member_template_decl (tree decl)
281 {
282 if (decl == error_mark_node)
283 return error_mark_node;
284
285 gcc_assert (DECL_P (decl));
286
287 if (TREE_CODE (decl) == TYPE_DECL)
288 {
289 tree type;
290
291 type = TREE_TYPE (decl);
292 if (type == error_mark_node)
293 return error_mark_node;
294 if (MAYBE_CLASS_TYPE_P (type)
295 && CLASSTYPE_TEMPLATE_INFO (type)
296 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
297 {
298 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
299 check_member_template (tmpl);
300 return tmpl;
301 }
302 return NULL_TREE;
303 }
304 else if (TREE_CODE (decl) == FIELD_DECL)
305 error ("data member %qD cannot be a member template", decl);
306 else if (DECL_TEMPLATE_INFO (decl))
307 {
308 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
309 {
310 check_member_template (DECL_TI_TEMPLATE (decl));
311 return DECL_TI_TEMPLATE (decl);
312 }
313 else
314 return decl;
315 }
316 else
317 error ("invalid member template declaration %qD", decl);
318
319 return error_mark_node;
320 }
321
322 /* Create a template info node. */
323
324 tree
325 build_template_info (tree template_decl, tree template_args)
326 {
327 tree result = make_node (TEMPLATE_INFO);
328 TI_TEMPLATE (result) = template_decl;
329 TI_ARGS (result) = template_args;
330 return result;
331 }
332
333 /* Return the template info node corresponding to T, whatever T is. */
334
335 tree
336 get_template_info (const_tree t)
337 {
338 tree tinfo = NULL_TREE;
339
340 if (!t || t == error_mark_node)
341 return NULL;
342
343 if (TREE_CODE (t) == NAMESPACE_DECL
344 || TREE_CODE (t) == PARM_DECL)
345 return NULL;
346
347 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
348 tinfo = DECL_TEMPLATE_INFO (t);
349
350 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
351 t = TREE_TYPE (t);
352
353 if (OVERLOAD_TYPE_P (t))
354 tinfo = TYPE_TEMPLATE_INFO (t);
355 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
356 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
357
358 return tinfo;
359 }
360
361 /* Returns the template nesting level of the indicated class TYPE.
362
363 For example, in:
364 template <class T>
365 struct A
366 {
367 template <class U>
368 struct B {};
369 };
370
371 A<T>::B<U> has depth two, while A<T> has depth one.
372 Both A<T>::B<int> and A<int>::B<U> have depth one, if
373 they are instantiations, not specializations.
374
375 This function is guaranteed to return 0 if passed NULL_TREE so
376 that, for example, `template_class_depth (current_class_type)' is
377 always safe. */
378
379 int
380 template_class_depth (tree type)
381 {
382 int depth;
383
384 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
385 {
386 tree tinfo = get_template_info (type);
387
388 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
389 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
390 ++depth;
391
392 if (DECL_P (type))
393 type = CP_DECL_CONTEXT (type);
394 else if (LAMBDA_TYPE_P (type))
395 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
396 else
397 type = CP_TYPE_CONTEXT (type);
398 }
399
400 return depth;
401 }
402
403 /* Return TRUE if NODE instantiates a template that has arguments of
404 its own, be it directly a primary template or indirectly through a
405 partial specializations. */
406 static bool
407 instantiates_primary_template_p (tree node)
408 {
409 tree tinfo = get_template_info (node);
410 if (!tinfo)
411 return false;
412
413 tree tmpl = TI_TEMPLATE (tinfo);
414 if (PRIMARY_TEMPLATE_P (tmpl))
415 return true;
416
417 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
418 return false;
419
420 /* So now we know we have a specialization, but it could be a full
421 or a partial specialization. To tell which, compare the depth of
422 its template arguments with those of its context. */
423
424 tree ctxt = DECL_CONTEXT (tmpl);
425 tree ctinfo = get_template_info (ctxt);
426 if (!ctinfo)
427 return true;
428
429 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
430 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
431 }
432
433 /* Subroutine of maybe_begin_member_template_processing.
434 Returns true if processing DECL needs us to push template parms. */
435
436 static bool
437 inline_needs_template_parms (tree decl, bool nsdmi)
438 {
439 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
440 return false;
441
442 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
443 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
444 }
445
446 /* Subroutine of maybe_begin_member_template_processing.
447 Push the template parms in PARMS, starting from LEVELS steps into the
448 chain, and ending at the beginning, since template parms are listed
449 innermost first. */
450
451 static void
452 push_inline_template_parms_recursive (tree parmlist, int levels)
453 {
454 tree parms = TREE_VALUE (parmlist);
455 int i;
456
457 if (levels > 1)
458 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
459
460 ++processing_template_decl;
461 current_template_parms
462 = tree_cons (size_int (processing_template_decl),
463 parms, current_template_parms);
464 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
465
466 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
467 NULL);
468 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
469 {
470 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
471
472 if (error_operand_p (parm))
473 continue;
474
475 gcc_assert (DECL_P (parm));
476
477 switch (TREE_CODE (parm))
478 {
479 case TYPE_DECL:
480 case TEMPLATE_DECL:
481 pushdecl (parm);
482 break;
483
484 case PARM_DECL:
485 /* Push the CONST_DECL. */
486 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
487 break;
488
489 default:
490 gcc_unreachable ();
491 }
492 }
493 }
494
495 /* Restore the template parameter context for a member template, a
496 friend template defined in a class definition, or a non-template
497 member of template class. */
498
499 void
500 maybe_begin_member_template_processing (tree decl)
501 {
502 tree parms;
503 int levels = 0;
504 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
505
506 if (nsdmi)
507 {
508 tree ctx = DECL_CONTEXT (decl);
509 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
510 /* Disregard full specializations (c++/60999). */
511 && uses_template_parms (ctx)
512 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
513 }
514
515 if (inline_needs_template_parms (decl, nsdmi))
516 {
517 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
518 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
519
520 if (DECL_TEMPLATE_SPECIALIZATION (decl))
521 {
522 --levels;
523 parms = TREE_CHAIN (parms);
524 }
525
526 push_inline_template_parms_recursive (parms, levels);
527 }
528
529 /* Remember how many levels of template parameters we pushed so that
530 we can pop them later. */
531 inline_parm_levels.safe_push (levels);
532 }
533
534 /* Undo the effects of maybe_begin_member_template_processing. */
535
536 void
537 maybe_end_member_template_processing (void)
538 {
539 int i;
540 int last;
541
542 if (inline_parm_levels.length () == 0)
543 return;
544
545 last = inline_parm_levels.pop ();
546 for (i = 0; i < last; ++i)
547 {
548 --processing_template_decl;
549 current_template_parms = TREE_CHAIN (current_template_parms);
550 poplevel (0, 0, 0);
551 }
552 }
553
554 /* Return a new template argument vector which contains all of ARGS,
555 but has as its innermost set of arguments the EXTRA_ARGS. */
556
557 static tree
558 add_to_template_args (tree args, tree extra_args)
559 {
560 tree new_args;
561 int extra_depth;
562 int i;
563 int j;
564
565 if (args == NULL_TREE || extra_args == error_mark_node)
566 return extra_args;
567
568 extra_depth = TMPL_ARGS_DEPTH (extra_args);
569 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
570
571 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
573
574 for (j = 1; j <= extra_depth; ++j, ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
576
577 return new_args;
578 }
579
580 /* Like add_to_template_args, but only the outermost ARGS are added to
581 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
582 (EXTRA_ARGS) levels are added. This function is used to combine
583 the template arguments from a partial instantiation with the
584 template arguments used to attain the full instantiation from the
585 partial instantiation. */
586
587 static tree
588 add_outermost_template_args (tree args, tree extra_args)
589 {
590 tree new_args;
591
592 /* If there are more levels of EXTRA_ARGS than there are ARGS,
593 something very fishy is going on. */
594 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
595
596 /* If *all* the new arguments will be the EXTRA_ARGS, just return
597 them. */
598 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
599 return extra_args;
600
601 /* For the moment, we make ARGS look like it contains fewer levels. */
602 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
603
604 new_args = add_to_template_args (args, extra_args);
605
606 /* Now, we restore ARGS to its full dimensions. */
607 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
608
609 return new_args;
610 }
611
612 /* Return the N levels of innermost template arguments from the ARGS. */
613
614 tree
615 get_innermost_template_args (tree args, int n)
616 {
617 tree new_args;
618 int extra_levels;
619 int i;
620
621 gcc_assert (n >= 0);
622
623 /* If N is 1, just return the innermost set of template arguments. */
624 if (n == 1)
625 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
626
627 /* If we're not removing anything, just return the arguments we were
628 given. */
629 extra_levels = TMPL_ARGS_DEPTH (args) - n;
630 gcc_assert (extra_levels >= 0);
631 if (extra_levels == 0)
632 return args;
633
634 /* Make a new set of arguments, not containing the outer arguments. */
635 new_args = make_tree_vec (n);
636 for (i = 1; i <= n; ++i)
637 SET_TMPL_ARGS_LEVEL (new_args, i,
638 TMPL_ARGS_LEVEL (args, i + extra_levels));
639
640 return new_args;
641 }
642
643 /* The inverse of get_innermost_template_args: Return all but the innermost
644 EXTRA_LEVELS levels of template arguments from the ARGS. */
645
646 static tree
647 strip_innermost_template_args (tree args, int extra_levels)
648 {
649 tree new_args;
650 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
651 int i;
652
653 gcc_assert (n >= 0);
654
655 /* If N is 1, just return the outermost set of template arguments. */
656 if (n == 1)
657 return TMPL_ARGS_LEVEL (args, 1);
658
659 /* If we're not removing anything, just return the arguments we were
660 given. */
661 gcc_assert (extra_levels >= 0);
662 if (extra_levels == 0)
663 return args;
664
665 /* Make a new set of arguments, not containing the inner arguments. */
666 new_args = make_tree_vec (n);
667 for (i = 1; i <= n; ++i)
668 SET_TMPL_ARGS_LEVEL (new_args, i,
669 TMPL_ARGS_LEVEL (args, i));
670
671 return new_args;
672 }
673
674 /* We've got a template header coming up; push to a new level for storing
675 the parms. */
676
677 void
678 begin_template_parm_list (void)
679 {
680 /* We use a non-tag-transparent scope here, which causes pushtag to
681 put tags in this scope, rather than in the enclosing class or
682 namespace scope. This is the right thing, since we want
683 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
684 global template class, push_template_decl handles putting the
685 TEMPLATE_DECL into top-level scope. For a nested template class,
686 e.g.:
687
688 template <class T> struct S1 {
689 template <class T> struct S2 {};
690 };
691
692 pushtag contains special code to insert the TEMPLATE_DECL for S2
693 at the right scope. */
694 begin_scope (sk_template_parms, NULL);
695 ++processing_template_decl;
696 ++processing_template_parmlist;
697 note_template_header (0);
698
699 /* Add a dummy parameter level while we process the parameter list. */
700 current_template_parms
701 = tree_cons (size_int (processing_template_decl),
702 make_tree_vec (0),
703 current_template_parms);
704 }
705
706 /* This routine is called when a specialization is declared. If it is
707 invalid to declare a specialization here, an error is reported and
708 false is returned, otherwise this routine will return true. */
709
710 static bool
711 check_specialization_scope (void)
712 {
713 tree scope = current_scope ();
714
715 /* [temp.expl.spec]
716
717 An explicit specialization shall be declared in the namespace of
718 which the template is a member, or, for member templates, in the
719 namespace of which the enclosing class or enclosing class
720 template is a member. An explicit specialization of a member
721 function, member class or static data member of a class template
722 shall be declared in the namespace of which the class template
723 is a member. */
724 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
725 {
726 error ("explicit specialization in non-namespace scope %qD", scope);
727 return false;
728 }
729
730 /* [temp.expl.spec]
731
732 In an explicit specialization declaration for a member of a class
733 template or a member template that appears in namespace scope,
734 the member template and some of its enclosing class templates may
735 remain unspecialized, except that the declaration shall not
736 explicitly specialize a class member template if its enclosing
737 class templates are not explicitly specialized as well. */
738 if (current_template_parms)
739 {
740 error ("enclosing class templates are not explicitly specialized");
741 return false;
742 }
743
744 return true;
745 }
746
747 /* We've just seen template <>. */
748
749 bool
750 begin_specialization (void)
751 {
752 begin_scope (sk_template_spec, NULL);
753 note_template_header (1);
754 return check_specialization_scope ();
755 }
756
757 /* Called at then end of processing a declaration preceded by
758 template<>. */
759
760 void
761 end_specialization (void)
762 {
763 finish_scope ();
764 reset_specialization ();
765 }
766
767 /* Any template <>'s that we have seen thus far are not referring to a
768 function specialization. */
769
770 void
771 reset_specialization (void)
772 {
773 processing_specialization = 0;
774 template_header_count = 0;
775 }
776
777 /* We've just seen a template header. If SPECIALIZATION is nonzero,
778 it was of the form template <>. */
779
780 static void
781 note_template_header (int specialization)
782 {
783 processing_specialization = specialization;
784 template_header_count++;
785 }
786
787 /* We're beginning an explicit instantiation. */
788
789 void
790 begin_explicit_instantiation (void)
791 {
792 gcc_assert (!processing_explicit_instantiation);
793 processing_explicit_instantiation = true;
794 }
795
796
797 void
798 end_explicit_instantiation (void)
799 {
800 gcc_assert (processing_explicit_instantiation);
801 processing_explicit_instantiation = false;
802 }
803
804 /* An explicit specialization or partial specialization of TMPL is being
805 declared. Check that the namespace in which the specialization is
806 occurring is permissible. Returns false iff it is invalid to
807 specialize TMPL in the current namespace. */
808
809 static bool
810 check_specialization_namespace (tree tmpl)
811 {
812 tree tpl_ns = decl_namespace_context (tmpl);
813
814 /* [tmpl.expl.spec]
815
816 An explicit specialization shall be declared in a namespace enclosing the
817 specialized template. An explicit specialization whose declarator-id is
818 not qualified shall be declared in the nearest enclosing namespace of the
819 template, or, if the namespace is inline (7.3.1), any namespace from its
820 enclosing namespace set. */
821 if (current_scope() != DECL_CONTEXT (tmpl)
822 && !at_namespace_scope_p ())
823 {
824 error ("specialization of %qD must appear at namespace scope", tmpl);
825 return false;
826 }
827
828 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
829 /* Same or enclosing namespace. */
830 return true;
831 else
832 {
833 auto_diagnostic_group d;
834 if (permerror (input_location,
835 "specialization of %qD in different namespace", tmpl))
836 inform (DECL_SOURCE_LOCATION (tmpl),
837 " from definition of %q#D", tmpl);
838 return false;
839 }
840 }
841
842 /* SPEC is an explicit instantiation. Check that it is valid to
843 perform this explicit instantiation in the current namespace. */
844
845 static void
846 check_explicit_instantiation_namespace (tree spec)
847 {
848 tree ns;
849
850 /* DR 275: An explicit instantiation shall appear in an enclosing
851 namespace of its template. */
852 ns = decl_namespace_context (spec);
853 if (!is_nested_namespace (current_namespace, ns))
854 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
855 "(which does not enclose namespace %qD)",
856 spec, current_namespace, ns);
857 }
858
859 // Returns the type of a template specialization only if that
860 // specialization needs to be defined. Otherwise (e.g., if the type has
861 // already been defined), the function returns NULL_TREE.
862 static tree
863 maybe_new_partial_specialization (tree type)
864 {
865 // An implicit instantiation of an incomplete type implies
866 // the definition of a new class template.
867 //
868 // template<typename T>
869 // struct S;
870 //
871 // template<typename T>
872 // struct S<T*>;
873 //
874 // Here, S<T*> is an implicit instantiation of S whose type
875 // is incomplete.
876 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
877 return type;
878
879 // It can also be the case that TYPE is a completed specialization.
880 // Continuing the previous example, suppose we also declare:
881 //
882 // template<typename T>
883 // requires Integral<T>
884 // struct S<T*>;
885 //
886 // Here, S<T*> refers to the specialization S<T*> defined
887 // above. However, we need to differentiate definitions because
888 // we intend to define a new partial specialization. In this case,
889 // we rely on the fact that the constraints are different for
890 // this declaration than that above.
891 //
892 // Note that we also get here for injected class names and
893 // late-parsed template definitions. We must ensure that we
894 // do not create new type declarations for those cases.
895 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
896 {
897 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
898 tree args = CLASSTYPE_TI_ARGS (type);
899
900 // If there are no template parameters, this cannot be a new
901 // partial template specializtion?
902 if (!current_template_parms)
903 return NULL_TREE;
904
905 // The injected-class-name is not a new partial specialization.
906 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
907 return NULL_TREE;
908
909 // If the constraints are not the same as those of the primary
910 // then, we can probably create a new specialization.
911 tree type_constr = current_template_constraints ();
912
913 if (type == TREE_TYPE (tmpl))
914 {
915 tree main_constr = get_constraints (tmpl);
916 if (equivalent_constraints (type_constr, main_constr))
917 return NULL_TREE;
918 }
919
920 // Also, if there's a pre-existing specialization with matching
921 // constraints, then this also isn't new.
922 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
923 while (specs)
924 {
925 tree spec_tmpl = TREE_VALUE (specs);
926 tree spec_args = TREE_PURPOSE (specs);
927 tree spec_constr = get_constraints (spec_tmpl);
928 if (comp_template_args (args, spec_args)
929 && equivalent_constraints (type_constr, spec_constr))
930 return NULL_TREE;
931 specs = TREE_CHAIN (specs);
932 }
933
934 // Create a new type node (and corresponding type decl)
935 // for the newly declared specialization.
936 tree t = make_class_type (TREE_CODE (type));
937 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
938 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
939
940 /* We only need a separate type node for storing the definition of this
941 partial specialization; uses of S<T*> are unconstrained, so all are
942 equivalent. So keep TYPE_CANONICAL the same. */
943 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
944
945 // Build the corresponding type decl.
946 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
947 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
948 DECL_SOURCE_LOCATION (d) = input_location;
949
950 return t;
951 }
952
953 return NULL_TREE;
954 }
955
956 /* The TYPE is being declared. If it is a template type, that means it
957 is a partial specialization. Do appropriate error-checking. */
958
959 tree
960 maybe_process_partial_specialization (tree type)
961 {
962 tree context;
963
964 if (type == error_mark_node)
965 return error_mark_node;
966
967 /* A lambda that appears in specialization context is not itself a
968 specialization. */
969 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
970 return type;
971
972 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
973 {
974 error ("name of class shadows template template parameter %qD",
975 TYPE_NAME (type));
976 return error_mark_node;
977 }
978
979 context = TYPE_CONTEXT (type);
980
981 if (TYPE_ALIAS_P (type))
982 {
983 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
984
985 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
986 error ("specialization of alias template %qD",
987 TI_TEMPLATE (tinfo));
988 else
989 error ("explicit specialization of non-template %qT", type);
990 return error_mark_node;
991 }
992 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
993 {
994 /* This is for ordinary explicit specialization and partial
995 specialization of a template class such as:
996
997 template <> class C<int>;
998
999 or:
1000
1001 template <class T> class C<T*>;
1002
1003 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1004
1005 if (tree t = maybe_new_partial_specialization (type))
1006 {
1007 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1008 && !at_namespace_scope_p ())
1009 return error_mark_node;
1010 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1011 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1012 if (processing_template_decl)
1013 {
1014 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1015 if (decl == error_mark_node)
1016 return error_mark_node;
1017 return TREE_TYPE (decl);
1018 }
1019 }
1020 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1021 error ("specialization of %qT after instantiation", type);
1022 else if (errorcount && !processing_specialization
1023 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1024 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1025 /* Trying to define a specialization either without a template<> header
1026 or in an inappropriate place. We've already given an error, so just
1027 bail now so we don't actually define the specialization. */
1028 return error_mark_node;
1029 }
1030 else if (CLASS_TYPE_P (type)
1031 && !CLASSTYPE_USE_TEMPLATE (type)
1032 && CLASSTYPE_TEMPLATE_INFO (type)
1033 && context && CLASS_TYPE_P (context)
1034 && CLASSTYPE_TEMPLATE_INFO (context))
1035 {
1036 /* This is for an explicit specialization of member class
1037 template according to [temp.expl.spec/18]:
1038
1039 template <> template <class U> class C<int>::D;
1040
1041 The context `C<int>' must be an implicit instantiation.
1042 Otherwise this is just a member class template declared
1043 earlier like:
1044
1045 template <> class C<int> { template <class U> class D; };
1046 template <> template <class U> class C<int>::D;
1047
1048 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1049 while in the second case, `C<int>::D' is a primary template
1050 and `C<T>::D' may not exist. */
1051
1052 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1053 && !COMPLETE_TYPE_P (type))
1054 {
1055 tree t;
1056 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1057
1058 if (current_namespace
1059 != decl_namespace_context (tmpl))
1060 {
1061 if (permerror (input_location,
1062 "specialization of %qD in different namespace",
1063 type))
1064 inform (DECL_SOURCE_LOCATION (tmpl),
1065 "from definition of %q#D", tmpl);
1066 }
1067
1068 /* Check for invalid specialization after instantiation:
1069
1070 template <> template <> class C<int>::D<int>;
1071 template <> template <class U> class C<int>::D; */
1072
1073 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1074 t; t = TREE_CHAIN (t))
1075 {
1076 tree inst = TREE_VALUE (t);
1077 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1078 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1079 {
1080 /* We already have a full specialization of this partial
1081 instantiation, or a full specialization has been
1082 looked up but not instantiated. Reassign it to the
1083 new member specialization template. */
1084 spec_entry elt;
1085 spec_entry *entry;
1086
1087 elt.tmpl = most_general_template (tmpl);
1088 elt.args = CLASSTYPE_TI_ARGS (inst);
1089 elt.spec = inst;
1090
1091 type_specializations->remove_elt (&elt);
1092
1093 elt.tmpl = tmpl;
1094 CLASSTYPE_TI_ARGS (inst)
1095 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1096
1097 spec_entry **slot
1098 = type_specializations->find_slot (&elt, INSERT);
1099 entry = ggc_alloc<spec_entry> ();
1100 *entry = elt;
1101 *slot = entry;
1102 }
1103 else
1104 /* But if we've had an implicit instantiation, that's a
1105 problem ([temp.expl.spec]/6). */
1106 error ("specialization %qT after instantiation %qT",
1107 type, inst);
1108 }
1109
1110 /* Mark TYPE as a specialization. And as a result, we only
1111 have one level of template argument for the innermost
1112 class template. */
1113 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1114 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1115 CLASSTYPE_TI_ARGS (type)
1116 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1117 }
1118 }
1119 else if (processing_specialization)
1120 {
1121 /* Someday C++0x may allow for enum template specialization. */
1122 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1123 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1124 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1125 "of %qD not allowed by ISO C++", type);
1126 else
1127 {
1128 error ("explicit specialization of non-template %qT", type);
1129 return error_mark_node;
1130 }
1131 }
1132
1133 return type;
1134 }
1135
1136 /* Returns nonzero if we can optimize the retrieval of specializations
1137 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1138 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1139
1140 static inline bool
1141 optimize_specialization_lookup_p (tree tmpl)
1142 {
1143 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1144 && DECL_CLASS_SCOPE_P (tmpl)
1145 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1146 parameter. */
1147 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1148 /* The optimized lookup depends on the fact that the
1149 template arguments for the member function template apply
1150 purely to the containing class, which is not true if the
1151 containing class is an explicit or partial
1152 specialization. */
1153 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1154 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1155 && !DECL_CONV_FN_P (tmpl)
1156 /* It is possible to have a template that is not a member
1157 template and is not a member of a template class:
1158
1159 template <typename T>
1160 struct S { friend A::f(); };
1161
1162 Here, the friend function is a template, but the context does
1163 not have template information. The optimized lookup relies
1164 on having ARGS be the template arguments for both the class
1165 and the function template. */
1166 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1167 }
1168
1169 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1170 gone through coerce_template_parms by now. */
1171
1172 static void
1173 verify_unstripped_args_1 (tree inner)
1174 {
1175 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1176 {
1177 tree arg = TREE_VEC_ELT (inner, i);
1178 if (TREE_CODE (arg) == TEMPLATE_DECL)
1179 /* OK */;
1180 else if (TYPE_P (arg))
1181 gcc_assert (strip_typedefs (arg, NULL) == arg);
1182 else if (ARGUMENT_PACK_P (arg))
1183 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1184 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1185 /* Allow typedefs on the type of a non-type argument, since a
1186 parameter can have them. */;
1187 else
1188 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1189 }
1190 }
1191
1192 static void
1193 verify_unstripped_args (tree args)
1194 {
1195 ++processing_template_decl;
1196 if (!any_dependent_template_arguments_p (args))
1197 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1198 --processing_template_decl;
1199 }
1200
1201 /* Retrieve the specialization (in the sense of [temp.spec] - a
1202 specialization is either an instantiation or an explicit
1203 specialization) of TMPL for the given template ARGS. If there is
1204 no such specialization, return NULL_TREE. The ARGS are a vector of
1205 arguments, or a vector of vectors of arguments, in the case of
1206 templates with more than one level of parameters.
1207
1208 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1209 then we search for a partial specialization matching ARGS. This
1210 parameter is ignored if TMPL is not a class template.
1211
1212 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1213 result is a NONTYPE_ARGUMENT_PACK. */
1214
1215 static tree
1216 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1217 {
1218 if (tmpl == NULL_TREE)
1219 return NULL_TREE;
1220
1221 if (args == error_mark_node)
1222 return NULL_TREE;
1223
1224 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1225 || TREE_CODE (tmpl) == FIELD_DECL);
1226
1227 /* There should be as many levels of arguments as there are
1228 levels of parameters. */
1229 gcc_assert (TMPL_ARGS_DEPTH (args)
1230 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1231 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1232 : template_class_depth (DECL_CONTEXT (tmpl))));
1233
1234 if (flag_checking)
1235 verify_unstripped_args (args);
1236
1237 /* Lambda functions in templates aren't instantiated normally, but through
1238 tsubst_lambda_expr. */
1239 if (lambda_fn_in_template_p (tmpl))
1240 return NULL_TREE;
1241
1242 if (optimize_specialization_lookup_p (tmpl))
1243 {
1244 /* The template arguments actually apply to the containing
1245 class. Find the class specialization with those
1246 arguments. */
1247 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1248 tree class_specialization
1249 = retrieve_specialization (class_template, args, 0);
1250 if (!class_specialization)
1251 return NULL_TREE;
1252
1253 /* Find the instance of TMPL. */
1254 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1255 for (ovl_iterator iter (fns); iter; ++iter)
1256 {
1257 tree fn = *iter;
1258 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1259 /* using-declarations can add base methods to the method vec,
1260 and we don't want those here. */
1261 && DECL_CONTEXT (fn) == class_specialization)
1262 return fn;
1263 }
1264 return NULL_TREE;
1265 }
1266 else
1267 {
1268 spec_entry *found;
1269 spec_entry elt;
1270 hash_table<spec_hasher> *specializations;
1271
1272 elt.tmpl = tmpl;
1273 elt.args = args;
1274 elt.spec = NULL_TREE;
1275
1276 if (DECL_CLASS_TEMPLATE_P (tmpl))
1277 specializations = type_specializations;
1278 else
1279 specializations = decl_specializations;
1280
1281 if (hash == 0)
1282 hash = spec_hasher::hash (&elt);
1283 found = specializations->find_with_hash (&elt, hash);
1284 if (found)
1285 return found->spec;
1286 }
1287
1288 return NULL_TREE;
1289 }
1290
1291 /* Like retrieve_specialization, but for local declarations. */
1292
1293 tree
1294 retrieve_local_specialization (tree tmpl)
1295 {
1296 if (local_specializations == NULL)
1297 return NULL_TREE;
1298
1299 tree *slot = local_specializations->get (tmpl);
1300 return slot ? *slot : NULL_TREE;
1301 }
1302
1303 /* Returns nonzero iff DECL is a specialization of TMPL. */
1304
1305 int
1306 is_specialization_of (tree decl, tree tmpl)
1307 {
1308 tree t;
1309
1310 if (TREE_CODE (decl) == FUNCTION_DECL)
1311 {
1312 for (t = decl;
1313 t != NULL_TREE;
1314 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1315 if (t == tmpl)
1316 return 1;
1317 }
1318 else
1319 {
1320 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1321
1322 for (t = TREE_TYPE (decl);
1323 t != NULL_TREE;
1324 t = CLASSTYPE_USE_TEMPLATE (t)
1325 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1326 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1327 return 1;
1328 }
1329
1330 return 0;
1331 }
1332
1333 /* Returns nonzero iff DECL is a specialization of friend declaration
1334 FRIEND_DECL according to [temp.friend]. */
1335
1336 bool
1337 is_specialization_of_friend (tree decl, tree friend_decl)
1338 {
1339 bool need_template = true;
1340 int template_depth;
1341
1342 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1343 || TREE_CODE (decl) == TYPE_DECL);
1344
1345 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1346 of a template class, we want to check if DECL is a specialization
1347 if this. */
1348 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1349 && DECL_TEMPLATE_INFO (friend_decl)
1350 && !DECL_USE_TEMPLATE (friend_decl))
1351 {
1352 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1353 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1354 need_template = false;
1355 }
1356 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1357 && !PRIMARY_TEMPLATE_P (friend_decl))
1358 need_template = false;
1359
1360 /* There is nothing to do if this is not a template friend. */
1361 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1362 return false;
1363
1364 if (is_specialization_of (decl, friend_decl))
1365 return true;
1366
1367 /* [temp.friend/6]
1368 A member of a class template may be declared to be a friend of a
1369 non-template class. In this case, the corresponding member of
1370 every specialization of the class template is a friend of the
1371 class granting friendship.
1372
1373 For example, given a template friend declaration
1374
1375 template <class T> friend void A<T>::f();
1376
1377 the member function below is considered a friend
1378
1379 template <> struct A<int> {
1380 void f();
1381 };
1382
1383 For this type of template friend, TEMPLATE_DEPTH below will be
1384 nonzero. To determine if DECL is a friend of FRIEND, we first
1385 check if the enclosing class is a specialization of another. */
1386
1387 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1388 if (template_depth
1389 && DECL_CLASS_SCOPE_P (decl)
1390 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1391 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1392 {
1393 /* Next, we check the members themselves. In order to handle
1394 a few tricky cases, such as when FRIEND_DECL's are
1395
1396 template <class T> friend void A<T>::g(T t);
1397 template <class T> template <T t> friend void A<T>::h();
1398
1399 and DECL's are
1400
1401 void A<int>::g(int);
1402 template <int> void A<int>::h();
1403
1404 we need to figure out ARGS, the template arguments from
1405 the context of DECL. This is required for template substitution
1406 of `T' in the function parameter of `g' and template parameter
1407 of `h' in the above examples. Here ARGS corresponds to `int'. */
1408
1409 tree context = DECL_CONTEXT (decl);
1410 tree args = NULL_TREE;
1411 int current_depth = 0;
1412
1413 while (current_depth < template_depth)
1414 {
1415 if (CLASSTYPE_TEMPLATE_INFO (context))
1416 {
1417 if (current_depth == 0)
1418 args = TYPE_TI_ARGS (context);
1419 else
1420 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1421 current_depth++;
1422 }
1423 context = TYPE_CONTEXT (context);
1424 }
1425
1426 if (TREE_CODE (decl) == FUNCTION_DECL)
1427 {
1428 bool is_template;
1429 tree friend_type;
1430 tree decl_type;
1431 tree friend_args_type;
1432 tree decl_args_type;
1433
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 non-templates. */
1436 is_template = DECL_TEMPLATE_INFO (decl)
1437 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1438 if (need_template ^ is_template)
1439 return false;
1440 else if (is_template)
1441 {
1442 /* If both are templates, check template parameter list. */
1443 tree friend_parms
1444 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1445 args, tf_none);
1446 if (!comp_template_parms
1447 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1448 friend_parms))
1449 return false;
1450
1451 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1452 }
1453 else
1454 decl_type = TREE_TYPE (decl);
1455
1456 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1457 tf_none, NULL_TREE);
1458 if (friend_type == error_mark_node)
1459 return false;
1460
1461 /* Check if return types match. */
1462 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1463 return false;
1464
1465 /* Check if function parameter types match, ignoring the
1466 `this' parameter. */
1467 friend_args_type = TYPE_ARG_TYPES (friend_type);
1468 decl_args_type = TYPE_ARG_TYPES (decl_type);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1470 friend_args_type = TREE_CHAIN (friend_args_type);
1471 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1472 decl_args_type = TREE_CHAIN (decl_args_type);
1473
1474 return compparms (decl_args_type, friend_args_type);
1475 }
1476 else
1477 {
1478 /* DECL is a TYPE_DECL */
1479 bool is_template;
1480 tree decl_type = TREE_TYPE (decl);
1481
1482 /* Make sure that both DECL and FRIEND_DECL are templates or
1483 non-templates. */
1484 is_template
1485 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1486 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1487
1488 if (need_template ^ is_template)
1489 return false;
1490 else if (is_template)
1491 {
1492 tree friend_parms;
1493 /* If both are templates, check the name of the two
1494 TEMPLATE_DECL's first because is_friend didn't. */
1495 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1496 != DECL_NAME (friend_decl))
1497 return false;
1498
1499 /* Now check template parameter list. */
1500 friend_parms
1501 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1502 args, tf_none);
1503 return comp_template_parms
1504 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1505 friend_parms);
1506 }
1507 else
1508 return (DECL_NAME (decl)
1509 == DECL_NAME (friend_decl));
1510 }
1511 }
1512 return false;
1513 }
1514
1515 /* Register the specialization SPEC as a specialization of TMPL with
1516 the indicated ARGS. IS_FRIEND indicates whether the specialization
1517 is actually just a friend declaration. ATTRLIST is the list of
1518 attributes that the specialization is declared with or NULL when
1519 it isn't. Returns SPEC, or an equivalent prior declaration, if
1520 available.
1521
1522 We also store instantiations of field packs in the hash table, even
1523 though they are not themselves templates, to make lookup easier. */
1524
1525 static tree
1526 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1527 hashval_t hash)
1528 {
1529 tree fn;
1530 spec_entry **slot = NULL;
1531 spec_entry elt;
1532
1533 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1534 || (TREE_CODE (tmpl) == FIELD_DECL
1535 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1536
1537 if (TREE_CODE (spec) == FUNCTION_DECL
1538 && uses_template_parms (DECL_TI_ARGS (spec)))
1539 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1540 register it; we want the corresponding TEMPLATE_DECL instead.
1541 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1542 the more obvious `uses_template_parms (spec)' to avoid problems
1543 with default function arguments. In particular, given
1544 something like this:
1545
1546 template <class T> void f(T t1, T t = T())
1547
1548 the default argument expression is not substituted for in an
1549 instantiation unless and until it is actually needed. */
1550 return spec;
1551
1552 if (optimize_specialization_lookup_p (tmpl))
1553 /* We don't put these specializations in the hash table, but we might
1554 want to give an error about a mismatch. */
1555 fn = retrieve_specialization (tmpl, args, 0);
1556 else
1557 {
1558 elt.tmpl = tmpl;
1559 elt.args = args;
1560 elt.spec = spec;
1561
1562 if (hash == 0)
1563 hash = spec_hasher::hash (&elt);
1564
1565 slot =
1566 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1567 if (*slot)
1568 fn = ((spec_entry *) *slot)->spec;
1569 else
1570 fn = NULL_TREE;
1571 }
1572
1573 /* We can sometimes try to re-register a specialization that we've
1574 already got. In particular, regenerate_decl_from_template calls
1575 duplicate_decls which will update the specialization list. But,
1576 we'll still get called again here anyhow. It's more convenient
1577 to simply allow this than to try to prevent it. */
1578 if (fn == spec)
1579 return spec;
1580 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1581 {
1582 if (DECL_TEMPLATE_INSTANTIATION (fn))
1583 {
1584 if (DECL_ODR_USED (fn)
1585 || DECL_EXPLICIT_INSTANTIATION (fn))
1586 {
1587 error ("specialization of %qD after instantiation",
1588 fn);
1589 return error_mark_node;
1590 }
1591 else
1592 {
1593 tree clone;
1594 /* This situation should occur only if the first
1595 specialization is an implicit instantiation, the
1596 second is an explicit specialization, and the
1597 implicit instantiation has not yet been used. That
1598 situation can occur if we have implicitly
1599 instantiated a member function and then specialized
1600 it later.
1601
1602 We can also wind up here if a friend declaration that
1603 looked like an instantiation turns out to be a
1604 specialization:
1605
1606 template <class T> void foo(T);
1607 class S { friend void foo<>(int) };
1608 template <> void foo(int);
1609
1610 We transform the existing DECL in place so that any
1611 pointers to it become pointers to the updated
1612 declaration.
1613
1614 If there was a definition for the template, but not
1615 for the specialization, we want this to look as if
1616 there were no definition, and vice versa. */
1617 DECL_INITIAL (fn) = NULL_TREE;
1618 duplicate_decls (spec, fn, is_friend);
1619 /* The call to duplicate_decls will have applied
1620 [temp.expl.spec]:
1621
1622 An explicit specialization of a function template
1623 is inline only if it is explicitly declared to be,
1624 and independently of whether its function template
1625 is.
1626
1627 to the primary function; now copy the inline bits to
1628 the various clones. */
1629 FOR_EACH_CLONE (clone, fn)
1630 {
1631 DECL_DECLARED_INLINE_P (clone)
1632 = DECL_DECLARED_INLINE_P (fn);
1633 DECL_SOURCE_LOCATION (clone)
1634 = DECL_SOURCE_LOCATION (fn);
1635 DECL_DELETED_FN (clone)
1636 = DECL_DELETED_FN (fn);
1637 }
1638 check_specialization_namespace (tmpl);
1639
1640 return fn;
1641 }
1642 }
1643 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1644 {
1645 tree dd = duplicate_decls (spec, fn, is_friend);
1646 if (dd == error_mark_node)
1647 /* We've already complained in duplicate_decls. */
1648 return error_mark_node;
1649
1650 if (dd == NULL_TREE && DECL_INITIAL (spec))
1651 /* Dup decl failed, but this is a new definition. Set the
1652 line number so any errors match this new
1653 definition. */
1654 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1655
1656 return fn;
1657 }
1658 }
1659 else if (fn)
1660 return duplicate_decls (spec, fn, is_friend);
1661
1662 /* A specialization must be declared in the same namespace as the
1663 template it is specializing. */
1664 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1665 && !check_specialization_namespace (tmpl))
1666 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1667
1668 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1669 {
1670 spec_entry *entry = ggc_alloc<spec_entry> ();
1671 gcc_assert (tmpl && args && spec);
1672 *entry = elt;
1673 *slot = entry;
1674 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1675 && PRIMARY_TEMPLATE_P (tmpl)
1676 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1677 || variable_template_p (tmpl))
1678 /* If TMPL is a forward declaration of a template function, keep a list
1679 of all specializations in case we need to reassign them to a friend
1680 template later in tsubst_friend_function.
1681
1682 Also keep a list of all variable template instantiations so that
1683 process_partial_specialization can check whether a later partial
1684 specialization would have used it. */
1685 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1686 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1687 }
1688
1689 return spec;
1690 }
1691
1692 /* Returns true iff two spec_entry nodes are equivalent. */
1693
1694 int comparing_specializations;
1695
1696 bool
1697 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1698 {
1699 int equal;
1700
1701 ++comparing_specializations;
1702 equal = (e1->tmpl == e2->tmpl
1703 && comp_template_args (e1->args, e2->args));
1704 if (equal && flag_concepts
1705 /* tmpl could be a FIELD_DECL for a capture pack. */
1706 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1707 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1708 && uses_template_parms (e1->args))
1709 {
1710 /* Partial specializations of a variable template can be distinguished by
1711 constraints. */
1712 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1713 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1714 equal = equivalent_constraints (c1, c2);
1715 }
1716 --comparing_specializations;
1717
1718 return equal;
1719 }
1720
1721 /* Returns a hash for a template TMPL and template arguments ARGS. */
1722
1723 static hashval_t
1724 hash_tmpl_and_args (tree tmpl, tree args)
1725 {
1726 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1727 return iterative_hash_template_arg (args, val);
1728 }
1729
1730 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1731 ignoring SPEC. */
1732
1733 hashval_t
1734 spec_hasher::hash (spec_entry *e)
1735 {
1736 return hash_tmpl_and_args (e->tmpl, e->args);
1737 }
1738
1739 /* Recursively calculate a hash value for a template argument ARG, for use
1740 in the hash tables of template specializations. */
1741
1742 hashval_t
1743 iterative_hash_template_arg (tree arg, hashval_t val)
1744 {
1745 unsigned HOST_WIDE_INT i;
1746 enum tree_code code;
1747 char tclass;
1748
1749 if (arg == NULL_TREE)
1750 return iterative_hash_object (arg, val);
1751
1752 if (!TYPE_P (arg))
1753 STRIP_NOPS (arg);
1754
1755 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1756 gcc_unreachable ();
1757
1758 code = TREE_CODE (arg);
1759 tclass = TREE_CODE_CLASS (code);
1760
1761 val = iterative_hash_object (code, val);
1762
1763 switch (code)
1764 {
1765 case ERROR_MARK:
1766 return val;
1767
1768 case IDENTIFIER_NODE:
1769 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1770
1771 case TREE_VEC:
1772 {
1773 int i, len = TREE_VEC_LENGTH (arg);
1774 for (i = 0; i < len; ++i)
1775 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1776 return val;
1777 }
1778
1779 case TYPE_PACK_EXPANSION:
1780 case EXPR_PACK_EXPANSION:
1781 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1782 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1783
1784 case TYPE_ARGUMENT_PACK:
1785 case NONTYPE_ARGUMENT_PACK:
1786 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1787
1788 case TREE_LIST:
1789 for (; arg; arg = TREE_CHAIN (arg))
1790 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1791 return val;
1792
1793 case OVERLOAD:
1794 for (lkp_iterator iter (arg); iter; ++iter)
1795 val = iterative_hash_template_arg (*iter, val);
1796 return val;
1797
1798 case CONSTRUCTOR:
1799 {
1800 tree field, value;
1801 iterative_hash_template_arg (TREE_TYPE (arg), val);
1802 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1803 {
1804 val = iterative_hash_template_arg (field, val);
1805 val = iterative_hash_template_arg (value, val);
1806 }
1807 return val;
1808 }
1809
1810 case PARM_DECL:
1811 if (!DECL_ARTIFICIAL (arg))
1812 {
1813 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1814 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1815 }
1816 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1817
1818 case TARGET_EXPR:
1819 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1820
1821 case PTRMEM_CST:
1822 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1823 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1824
1825 case TEMPLATE_PARM_INDEX:
1826 val = iterative_hash_template_arg
1827 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1828 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1829 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1830
1831 case TRAIT_EXPR:
1832 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1833 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1834 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1835
1836 case BASELINK:
1837 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1838 val);
1839 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1840 val);
1841
1842 case MODOP_EXPR:
1843 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1844 code = TREE_CODE (TREE_OPERAND (arg, 1));
1845 val = iterative_hash_object (code, val);
1846 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1847
1848 case LAMBDA_EXPR:
1849 /* [temp.over.link] Two lambda-expressions are never considered
1850 equivalent.
1851
1852 So just hash the closure type. */
1853 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1854
1855 case CAST_EXPR:
1856 case IMPLICIT_CONV_EXPR:
1857 case STATIC_CAST_EXPR:
1858 case REINTERPRET_CAST_EXPR:
1859 case CONST_CAST_EXPR:
1860 case DYNAMIC_CAST_EXPR:
1861 case NEW_EXPR:
1862 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1863 /* Now hash operands as usual. */
1864 break;
1865
1866 case CALL_EXPR:
1867 {
1868 tree fn = CALL_EXPR_FN (arg);
1869 if (tree name = dependent_name (fn))
1870 {
1871 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1872 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1873 fn = name;
1874 }
1875 val = iterative_hash_template_arg (fn, val);
1876 call_expr_arg_iterator ai;
1877 for (tree x = first_call_expr_arg (arg, &ai); x;
1878 x = next_call_expr_arg (&ai))
1879 val = iterative_hash_template_arg (x, val);
1880 return val;
1881 }
1882
1883 default:
1884 break;
1885 }
1886
1887 switch (tclass)
1888 {
1889 case tcc_type:
1890 if (alias_template_specialization_p (arg))
1891 {
1892 // We want an alias specialization that survived strip_typedefs
1893 // to hash differently from its TYPE_CANONICAL, to avoid hash
1894 // collisions that compare as different in template_args_equal.
1895 // These could be dependent specializations that strip_typedefs
1896 // left alone, or untouched specializations because
1897 // coerce_template_parms returns the unconverted template
1898 // arguments if it sees incomplete argument packs.
1899 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1900 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1901 }
1902 if (TYPE_CANONICAL (arg))
1903 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1904 val);
1905 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1906 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1907 /* Otherwise just compare the types during lookup. */
1908 return val;
1909
1910 case tcc_declaration:
1911 case tcc_constant:
1912 return iterative_hash_expr (arg, val);
1913
1914 default:
1915 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1916 {
1917 unsigned n = cp_tree_operand_length (arg);
1918 for (i = 0; i < n; ++i)
1919 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1920 return val;
1921 }
1922 }
1923 gcc_unreachable ();
1924 return 0;
1925 }
1926
1927 /* Unregister the specialization SPEC as a specialization of TMPL.
1928 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1929 if the SPEC was listed as a specialization of TMPL.
1930
1931 Note that SPEC has been ggc_freed, so we can't look inside it. */
1932
1933 bool
1934 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1935 {
1936 spec_entry *entry;
1937 spec_entry elt;
1938
1939 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1940 elt.args = TI_ARGS (tinfo);
1941 elt.spec = NULL_TREE;
1942
1943 entry = decl_specializations->find (&elt);
1944 if (entry != NULL)
1945 {
1946 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1947 gcc_assert (new_spec != NULL_TREE);
1948 entry->spec = new_spec;
1949 return 1;
1950 }
1951
1952 return 0;
1953 }
1954
1955 /* Like register_specialization, but for local declarations. We are
1956 registering SPEC, an instantiation of TMPL. */
1957
1958 void
1959 register_local_specialization (tree spec, tree tmpl)
1960 {
1961 gcc_assert (tmpl != spec);
1962 local_specializations->put (tmpl, spec);
1963 }
1964
1965 /* TYPE is a class type. Returns true if TYPE is an explicitly
1966 specialized class. */
1967
1968 bool
1969 explicit_class_specialization_p (tree type)
1970 {
1971 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1972 return false;
1973 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1974 }
1975
1976 /* Print the list of functions at FNS, going through all the overloads
1977 for each element of the list. Alternatively, FNS cannot be a
1978 TREE_LIST, in which case it will be printed together with all the
1979 overloads.
1980
1981 MORE and *STR should respectively be FALSE and NULL when the function
1982 is called from the outside. They are used internally on recursive
1983 calls. print_candidates manages the two parameters and leaves NULL
1984 in *STR when it ends. */
1985
1986 static void
1987 print_candidates_1 (tree fns, char **str, bool more = false)
1988 {
1989 if (TREE_CODE (fns) == TREE_LIST)
1990 for (; fns; fns = TREE_CHAIN (fns))
1991 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1992 else
1993 for (lkp_iterator iter (fns); iter;)
1994 {
1995 tree cand = *iter;
1996 ++iter;
1997
1998 const char *pfx = *str;
1999 if (!pfx)
2000 {
2001 if (more || iter)
2002 pfx = _("candidates are:");
2003 else
2004 pfx = _("candidate is:");
2005 *str = get_spaces (pfx);
2006 }
2007 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2008 }
2009 }
2010
2011 /* Print the list of candidate FNS in an error message. FNS can also
2012 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2013
2014 void
2015 print_candidates (tree fns)
2016 {
2017 char *str = NULL;
2018 print_candidates_1 (fns, &str);
2019 free (str);
2020 }
2021
2022 /* Get a (possibly) constrained template declaration for the
2023 purpose of ordering candidates. */
2024 static tree
2025 get_template_for_ordering (tree list)
2026 {
2027 gcc_assert (TREE_CODE (list) == TREE_LIST);
2028 tree f = TREE_VALUE (list);
2029 if (tree ti = DECL_TEMPLATE_INFO (f))
2030 return TI_TEMPLATE (ti);
2031 return f;
2032 }
2033
2034 /* Among candidates having the same signature, return the
2035 most constrained or NULL_TREE if there is no best candidate.
2036 If the signatures of candidates vary (e.g., template
2037 specialization vs. member function), then there can be no
2038 most constrained.
2039
2040 Note that we don't compare constraints on the functions
2041 themselves, but rather those of their templates. */
2042 static tree
2043 most_constrained_function (tree candidates)
2044 {
2045 // Try to find the best candidate in a first pass.
2046 tree champ = candidates;
2047 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2048 {
2049 int winner = more_constrained (get_template_for_ordering (champ),
2050 get_template_for_ordering (c));
2051 if (winner == -1)
2052 champ = c; // The candidate is more constrained
2053 else if (winner == 0)
2054 return NULL_TREE; // Neither is more constrained
2055 }
2056
2057 // Verify that the champ is better than previous candidates.
2058 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2059 if (!more_constrained (get_template_for_ordering (champ),
2060 get_template_for_ordering (c)))
2061 return NULL_TREE;
2062 }
2063
2064 return champ;
2065 }
2066
2067
2068 /* Returns the template (one of the functions given by TEMPLATE_ID)
2069 which can be specialized to match the indicated DECL with the
2070 explicit template args given in TEMPLATE_ID. The DECL may be
2071 NULL_TREE if none is available. In that case, the functions in
2072 TEMPLATE_ID are non-members.
2073
2074 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2075 specialization of a member template.
2076
2077 The TEMPLATE_COUNT is the number of references to qualifying
2078 template classes that appeared in the name of the function. See
2079 check_explicit_specialization for a more accurate description.
2080
2081 TSK indicates what kind of template declaration (if any) is being
2082 declared. TSK_TEMPLATE indicates that the declaration given by
2083 DECL, though a FUNCTION_DECL, has template parameters, and is
2084 therefore a template function.
2085
2086 The template args (those explicitly specified and those deduced)
2087 are output in a newly created vector *TARGS_OUT.
2088
2089 If it is impossible to determine the result, an error message is
2090 issued. The error_mark_node is returned to indicate failure. */
2091
2092 static tree
2093 determine_specialization (tree template_id,
2094 tree decl,
2095 tree* targs_out,
2096 int need_member_template,
2097 int template_count,
2098 tmpl_spec_kind tsk)
2099 {
2100 tree fns;
2101 tree targs;
2102 tree explicit_targs;
2103 tree candidates = NULL_TREE;
2104
2105 /* A TREE_LIST of templates of which DECL may be a specialization.
2106 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2107 corresponding TREE_PURPOSE is the set of template arguments that,
2108 when used to instantiate the template, would produce a function
2109 with the signature of DECL. */
2110 tree templates = NULL_TREE;
2111 int header_count;
2112 cp_binding_level *b;
2113
2114 *targs_out = NULL_TREE;
2115
2116 if (template_id == error_mark_node || decl == error_mark_node)
2117 return error_mark_node;
2118
2119 /* We shouldn't be specializing a member template of an
2120 unspecialized class template; we already gave an error in
2121 check_specialization_scope, now avoid crashing. */
2122 if (!VAR_P (decl)
2123 && template_count && DECL_CLASS_SCOPE_P (decl)
2124 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2125 {
2126 gcc_assert (errorcount);
2127 return error_mark_node;
2128 }
2129
2130 fns = TREE_OPERAND (template_id, 0);
2131 explicit_targs = TREE_OPERAND (template_id, 1);
2132
2133 if (fns == error_mark_node)
2134 return error_mark_node;
2135
2136 /* Check for baselinks. */
2137 if (BASELINK_P (fns))
2138 fns = BASELINK_FUNCTIONS (fns);
2139
2140 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2141 {
2142 error ("%qD is not a function template", fns);
2143 return error_mark_node;
2144 }
2145 else if (VAR_P (decl) && !variable_template_p (fns))
2146 {
2147 error ("%qD is not a variable template", fns);
2148 return error_mark_node;
2149 }
2150
2151 /* Count the number of template headers specified for this
2152 specialization. */
2153 header_count = 0;
2154 for (b = current_binding_level;
2155 b->kind == sk_template_parms;
2156 b = b->level_chain)
2157 ++header_count;
2158
2159 tree orig_fns = fns;
2160
2161 if (variable_template_p (fns))
2162 {
2163 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2164 targs = coerce_template_parms (parms, explicit_targs, fns,
2165 tf_warning_or_error,
2166 /*req_all*/true, /*use_defarg*/true);
2167 if (targs != error_mark_node)
2168 templates = tree_cons (targs, fns, templates);
2169 }
2170 else for (lkp_iterator iter (fns); iter; ++iter)
2171 {
2172 tree fn = *iter;
2173
2174 if (TREE_CODE (fn) == TEMPLATE_DECL)
2175 {
2176 tree decl_arg_types;
2177 tree fn_arg_types;
2178 tree insttype;
2179
2180 /* In case of explicit specialization, we need to check if
2181 the number of template headers appearing in the specialization
2182 is correct. This is usually done in check_explicit_specialization,
2183 but the check done there cannot be exhaustive when specializing
2184 member functions. Consider the following code:
2185
2186 template <> void A<int>::f(int);
2187 template <> template <> void A<int>::f(int);
2188
2189 Assuming that A<int> is not itself an explicit specialization
2190 already, the first line specializes "f" which is a non-template
2191 member function, whilst the second line specializes "f" which
2192 is a template member function. So both lines are syntactically
2193 correct, and check_explicit_specialization does not reject
2194 them.
2195
2196 Here, we can do better, as we are matching the specialization
2197 against the declarations. We count the number of template
2198 headers, and we check if they match TEMPLATE_COUNT + 1
2199 (TEMPLATE_COUNT is the number of qualifying template classes,
2200 plus there must be another header for the member template
2201 itself).
2202
2203 Notice that if header_count is zero, this is not a
2204 specialization but rather a template instantiation, so there
2205 is no check we can perform here. */
2206 if (header_count && header_count != template_count + 1)
2207 continue;
2208
2209 /* Check that the number of template arguments at the
2210 innermost level for DECL is the same as for FN. */
2211 if (current_binding_level->kind == sk_template_parms
2212 && !current_binding_level->explicit_spec_p
2213 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2214 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2215 (current_template_parms))))
2216 continue;
2217
2218 /* DECL might be a specialization of FN. */
2219 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2220 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2221
2222 /* For a non-static member function, we need to make sure
2223 that the const qualification is the same. Since
2224 get_bindings does not try to merge the "this" parameter,
2225 we must do the comparison explicitly. */
2226 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2227 {
2228 if (!same_type_p (TREE_VALUE (fn_arg_types),
2229 TREE_VALUE (decl_arg_types)))
2230 continue;
2231
2232 /* And the ref-qualification. */
2233 if (type_memfn_rqual (TREE_TYPE (decl))
2234 != type_memfn_rqual (TREE_TYPE (fn)))
2235 continue;
2236 }
2237
2238 /* Skip the "this" parameter and, for constructors of
2239 classes with virtual bases, the VTT parameter. A
2240 full specialization of a constructor will have a VTT
2241 parameter, but a template never will. */
2242 decl_arg_types
2243 = skip_artificial_parms_for (decl, decl_arg_types);
2244 fn_arg_types
2245 = skip_artificial_parms_for (fn, fn_arg_types);
2246
2247 /* Function templates cannot be specializations; there are
2248 no partial specializations of functions. Therefore, if
2249 the type of DECL does not match FN, there is no
2250 match.
2251
2252 Note that it should never be the case that we have both
2253 candidates added here, and for regular member functions
2254 below. */
2255 if (tsk == tsk_template)
2256 {
2257 if (compparms (fn_arg_types, decl_arg_types))
2258 candidates = tree_cons (NULL_TREE, fn, candidates);
2259 continue;
2260 }
2261
2262 /* See whether this function might be a specialization of this
2263 template. Suppress access control because we might be trying
2264 to make this specialization a friend, and we have already done
2265 access control for the declaration of the specialization. */
2266 push_deferring_access_checks (dk_no_check);
2267 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2268 pop_deferring_access_checks ();
2269
2270 if (!targs)
2271 /* We cannot deduce template arguments that when used to
2272 specialize TMPL will produce DECL. */
2273 continue;
2274
2275 if (uses_template_parms (targs))
2276 /* We deduced something involving 'auto', which isn't a valid
2277 template argument. */
2278 continue;
2279
2280 /* Remove, from the set of candidates, all those functions
2281 whose constraints are not satisfied. */
2282 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2283 continue;
2284
2285 // Then, try to form the new function type.
2286 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2287 if (insttype == error_mark_node)
2288 continue;
2289 fn_arg_types
2290 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2291 if (!compparms (fn_arg_types, decl_arg_types))
2292 continue;
2293
2294 /* Save this template, and the arguments deduced. */
2295 templates = tree_cons (targs, fn, templates);
2296 }
2297 else if (need_member_template)
2298 /* FN is an ordinary member function, and we need a
2299 specialization of a member template. */
2300 ;
2301 else if (TREE_CODE (fn) != FUNCTION_DECL)
2302 /* We can get IDENTIFIER_NODEs here in certain erroneous
2303 cases. */
2304 ;
2305 else if (!DECL_FUNCTION_MEMBER_P (fn))
2306 /* This is just an ordinary non-member function. Nothing can
2307 be a specialization of that. */
2308 ;
2309 else if (DECL_ARTIFICIAL (fn))
2310 /* Cannot specialize functions that are created implicitly. */
2311 ;
2312 else
2313 {
2314 tree decl_arg_types;
2315
2316 /* This is an ordinary member function. However, since
2317 we're here, we can assume its enclosing class is a
2318 template class. For example,
2319
2320 template <typename T> struct S { void f(); };
2321 template <> void S<int>::f() {}
2322
2323 Here, S<int>::f is a non-template, but S<int> is a
2324 template class. If FN has the same type as DECL, we
2325 might be in business. */
2326
2327 if (!DECL_TEMPLATE_INFO (fn))
2328 /* Its enclosing class is an explicit specialization
2329 of a template class. This is not a candidate. */
2330 continue;
2331
2332 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2333 TREE_TYPE (TREE_TYPE (fn))))
2334 /* The return types differ. */
2335 continue;
2336
2337 /* Adjust the type of DECL in case FN is a static member. */
2338 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2339 if (DECL_STATIC_FUNCTION_P (fn)
2340 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2341 decl_arg_types = TREE_CHAIN (decl_arg_types);
2342
2343 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2344 decl_arg_types))
2345 continue;
2346
2347 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2348 && (type_memfn_rqual (TREE_TYPE (decl))
2349 != type_memfn_rqual (TREE_TYPE (fn))))
2350 continue;
2351
2352 // If the deduced arguments do not satisfy the constraints,
2353 // this is not a candidate.
2354 if (flag_concepts && !constraints_satisfied_p (fn))
2355 continue;
2356
2357 // Add the candidate.
2358 candidates = tree_cons (NULL_TREE, fn, candidates);
2359 }
2360 }
2361
2362 if (templates && TREE_CHAIN (templates))
2363 {
2364 /* We have:
2365
2366 [temp.expl.spec]
2367
2368 It is possible for a specialization with a given function
2369 signature to be instantiated from more than one function
2370 template. In such cases, explicit specification of the
2371 template arguments must be used to uniquely identify the
2372 function template specialization being specialized.
2373
2374 Note that here, there's no suggestion that we're supposed to
2375 determine which of the candidate templates is most
2376 specialized. However, we, also have:
2377
2378 [temp.func.order]
2379
2380 Partial ordering of overloaded function template
2381 declarations is used in the following contexts to select
2382 the function template to which a function template
2383 specialization refers:
2384
2385 -- when an explicit specialization refers to a function
2386 template.
2387
2388 So, we do use the partial ordering rules, at least for now.
2389 This extension can only serve to make invalid programs valid,
2390 so it's safe. And, there is strong anecdotal evidence that
2391 the committee intended the partial ordering rules to apply;
2392 the EDG front end has that behavior, and John Spicer claims
2393 that the committee simply forgot to delete the wording in
2394 [temp.expl.spec]. */
2395 tree tmpl = most_specialized_instantiation (templates);
2396 if (tmpl != error_mark_node)
2397 {
2398 templates = tmpl;
2399 TREE_CHAIN (templates) = NULL_TREE;
2400 }
2401 }
2402
2403 // Concepts allows multiple declarations of member functions
2404 // with the same signature. Like above, we need to rely on
2405 // on the partial ordering of those candidates to determine which
2406 // is the best.
2407 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2408 {
2409 if (tree cand = most_constrained_function (candidates))
2410 {
2411 candidates = cand;
2412 TREE_CHAIN (cand) = NULL_TREE;
2413 }
2414 }
2415
2416 if (templates == NULL_TREE && candidates == NULL_TREE)
2417 {
2418 error ("template-id %qD for %q+D does not match any template "
2419 "declaration", template_id, decl);
2420 if (header_count && header_count != template_count + 1)
2421 inform (input_location, "saw %d %<template<>%>, need %d for "
2422 "specializing a member function template",
2423 header_count, template_count + 1);
2424 else
2425 print_candidates (orig_fns);
2426 return error_mark_node;
2427 }
2428 else if ((templates && TREE_CHAIN (templates))
2429 || (candidates && TREE_CHAIN (candidates))
2430 || (templates && candidates))
2431 {
2432 error ("ambiguous template specialization %qD for %q+D",
2433 template_id, decl);
2434 candidates = chainon (candidates, templates);
2435 print_candidates (candidates);
2436 return error_mark_node;
2437 }
2438
2439 /* We have one, and exactly one, match. */
2440 if (candidates)
2441 {
2442 tree fn = TREE_VALUE (candidates);
2443 *targs_out = copy_node (DECL_TI_ARGS (fn));
2444
2445 // Propagate the candidate's constraints to the declaration.
2446 set_constraints (decl, get_constraints (fn));
2447
2448 /* DECL is a re-declaration or partial instantiation of a template
2449 function. */
2450 if (TREE_CODE (fn) == TEMPLATE_DECL)
2451 return fn;
2452 /* It was a specialization of an ordinary member function in a
2453 template class. */
2454 return DECL_TI_TEMPLATE (fn);
2455 }
2456
2457 /* It was a specialization of a template. */
2458 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2459 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2460 {
2461 *targs_out = copy_node (targs);
2462 SET_TMPL_ARGS_LEVEL (*targs_out,
2463 TMPL_ARGS_DEPTH (*targs_out),
2464 TREE_PURPOSE (templates));
2465 }
2466 else
2467 *targs_out = TREE_PURPOSE (templates);
2468 return TREE_VALUE (templates);
2469 }
2470
2471 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2472 but with the default argument values filled in from those in the
2473 TMPL_TYPES. */
2474
2475 static tree
2476 copy_default_args_to_explicit_spec_1 (tree spec_types,
2477 tree tmpl_types)
2478 {
2479 tree new_spec_types;
2480
2481 if (!spec_types)
2482 return NULL_TREE;
2483
2484 if (spec_types == void_list_node)
2485 return void_list_node;
2486
2487 /* Substitute into the rest of the list. */
2488 new_spec_types =
2489 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2490 TREE_CHAIN (tmpl_types));
2491
2492 /* Add the default argument for this parameter. */
2493 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2494 TREE_VALUE (spec_types),
2495 new_spec_types);
2496 }
2497
2498 /* DECL is an explicit specialization. Replicate default arguments
2499 from the template it specializes. (That way, code like:
2500
2501 template <class T> void f(T = 3);
2502 template <> void f(double);
2503 void g () { f (); }
2504
2505 works, as required.) An alternative approach would be to look up
2506 the correct default arguments at the call-site, but this approach
2507 is consistent with how implicit instantiations are handled. */
2508
2509 static void
2510 copy_default_args_to_explicit_spec (tree decl)
2511 {
2512 tree tmpl;
2513 tree spec_types;
2514 tree tmpl_types;
2515 tree new_spec_types;
2516 tree old_type;
2517 tree new_type;
2518 tree t;
2519 tree object_type = NULL_TREE;
2520 tree in_charge = NULL_TREE;
2521 tree vtt = NULL_TREE;
2522
2523 /* See if there's anything we need to do. */
2524 tmpl = DECL_TI_TEMPLATE (decl);
2525 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2526 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2527 if (TREE_PURPOSE (t))
2528 break;
2529 if (!t)
2530 return;
2531
2532 old_type = TREE_TYPE (decl);
2533 spec_types = TYPE_ARG_TYPES (old_type);
2534
2535 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2536 {
2537 /* Remove the this pointer, but remember the object's type for
2538 CV quals. */
2539 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2540 spec_types = TREE_CHAIN (spec_types);
2541 tmpl_types = TREE_CHAIN (tmpl_types);
2542
2543 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2544 {
2545 /* DECL may contain more parameters than TMPL due to the extra
2546 in-charge parameter in constructors and destructors. */
2547 in_charge = spec_types;
2548 spec_types = TREE_CHAIN (spec_types);
2549 }
2550 if (DECL_HAS_VTT_PARM_P (decl))
2551 {
2552 vtt = spec_types;
2553 spec_types = TREE_CHAIN (spec_types);
2554 }
2555 }
2556
2557 /* Compute the merged default arguments. */
2558 new_spec_types =
2559 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2560
2561 /* Compute the new FUNCTION_TYPE. */
2562 if (object_type)
2563 {
2564 if (vtt)
2565 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2566 TREE_VALUE (vtt),
2567 new_spec_types);
2568
2569 if (in_charge)
2570 /* Put the in-charge parameter back. */
2571 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2572 TREE_VALUE (in_charge),
2573 new_spec_types);
2574
2575 new_type = build_method_type_directly (object_type,
2576 TREE_TYPE (old_type),
2577 new_spec_types);
2578 }
2579 else
2580 new_type = build_function_type (TREE_TYPE (old_type),
2581 new_spec_types);
2582 new_type = cp_build_type_attribute_variant (new_type,
2583 TYPE_ATTRIBUTES (old_type));
2584 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2585
2586 TREE_TYPE (decl) = new_type;
2587 }
2588
2589 /* Return the number of template headers we expect to see for a definition
2590 or specialization of CTYPE or one of its non-template members. */
2591
2592 int
2593 num_template_headers_for_class (tree ctype)
2594 {
2595 int num_templates = 0;
2596
2597 while (ctype && CLASS_TYPE_P (ctype))
2598 {
2599 /* You're supposed to have one `template <...>' for every
2600 template class, but you don't need one for a full
2601 specialization. For example:
2602
2603 template <class T> struct S{};
2604 template <> struct S<int> { void f(); };
2605 void S<int>::f () {}
2606
2607 is correct; there shouldn't be a `template <>' for the
2608 definition of `S<int>::f'. */
2609 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2610 /* If CTYPE does not have template information of any
2611 kind, then it is not a template, nor is it nested
2612 within a template. */
2613 break;
2614 if (explicit_class_specialization_p (ctype))
2615 break;
2616 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2617 ++num_templates;
2618
2619 ctype = TYPE_CONTEXT (ctype);
2620 }
2621
2622 return num_templates;
2623 }
2624
2625 /* Do a simple sanity check on the template headers that precede the
2626 variable declaration DECL. */
2627
2628 void
2629 check_template_variable (tree decl)
2630 {
2631 tree ctx = CP_DECL_CONTEXT (decl);
2632 int wanted = num_template_headers_for_class (ctx);
2633 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2634 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2635 {
2636 if (cxx_dialect < cxx14)
2637 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2638 "variable templates only available with "
2639 "%<-std=c++14%> or %<-std=gnu++14%>");
2640
2641 // Namespace-scope variable templates should have a template header.
2642 ++wanted;
2643 }
2644 if (template_header_count > wanted)
2645 {
2646 auto_diagnostic_group d;
2647 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2648 "too many template headers for %qD "
2649 "(should be %d)",
2650 decl, wanted);
2651 if (warned && CLASS_TYPE_P (ctx)
2652 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2653 inform (DECL_SOURCE_LOCATION (decl),
2654 "members of an explicitly specialized class are defined "
2655 "without a template header");
2656 }
2657 }
2658
2659 /* An explicit specialization whose declarator-id or class-head-name is not
2660 qualified shall be declared in the nearest enclosing namespace of the
2661 template, or, if the namespace is inline (7.3.1), any namespace from its
2662 enclosing namespace set.
2663
2664 If the name declared in the explicit instantiation is an unqualified name,
2665 the explicit instantiation shall appear in the namespace where its template
2666 is declared or, if that namespace is inline (7.3.1), any namespace from its
2667 enclosing namespace set. */
2668
2669 void
2670 check_unqualified_spec_or_inst (tree t, location_t loc)
2671 {
2672 tree tmpl = most_general_template (t);
2673 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2674 && !is_nested_namespace (current_namespace,
2675 CP_DECL_CONTEXT (tmpl), true))
2676 {
2677 if (processing_specialization)
2678 permerror (loc, "explicit specialization of %qD outside its "
2679 "namespace must use a nested-name-specifier", tmpl);
2680 else if (processing_explicit_instantiation
2681 && cxx_dialect >= cxx11)
2682 /* This was allowed in C++98, so only pedwarn. */
2683 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2684 "outside its namespace must use a nested-name-"
2685 "specifier", tmpl);
2686 }
2687 }
2688
2689 /* Warn for a template specialization SPEC that is missing some of a set
2690 of function or type attributes that the template TEMPL is declared with.
2691 ATTRLIST is a list of additional attributes that SPEC should be taken
2692 to ultimately be declared with. */
2693
2694 static void
2695 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2696 {
2697 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2698 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2699
2700 /* Avoid warning if the difference between the primary and
2701 the specialization is not in one of the attributes below. */
2702 const char* const blacklist[] = {
2703 "alloc_align", "alloc_size", "assume_aligned", "format",
2704 "format_arg", "malloc", "nonnull", NULL
2705 };
2706
2707 /* Put together a list of the black listed attributes that the primary
2708 template is declared with that the specialization is not, in case
2709 it's not apparent from the most recent declaration of the primary. */
2710 pretty_printer str;
2711 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2712 blacklist, &str);
2713
2714 if (!nattrs)
2715 return;
2716
2717 auto_diagnostic_group d;
2718 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2719 "explicit specialization %q#D may be missing attributes",
2720 spec))
2721 inform (DECL_SOURCE_LOCATION (tmpl),
2722 nattrs > 1
2723 ? G_("missing primary template attributes %s")
2724 : G_("missing primary template attribute %s"),
2725 pp_formatted_text (&str));
2726 }
2727
2728 /* Check to see if the function just declared, as indicated in
2729 DECLARATOR, and in DECL, is a specialization of a function
2730 template. We may also discover that the declaration is an explicit
2731 instantiation at this point.
2732
2733 Returns DECL, or an equivalent declaration that should be used
2734 instead if all goes well. Issues an error message if something is
2735 amiss. Returns error_mark_node if the error is not easily
2736 recoverable.
2737
2738 FLAGS is a bitmask consisting of the following flags:
2739
2740 2: The function has a definition.
2741 4: The function is a friend.
2742
2743 The TEMPLATE_COUNT is the number of references to qualifying
2744 template classes that appeared in the name of the function. For
2745 example, in
2746
2747 template <class T> struct S { void f(); };
2748 void S<int>::f();
2749
2750 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2751 classes are not counted in the TEMPLATE_COUNT, so that in
2752
2753 template <class T> struct S {};
2754 template <> struct S<int> { void f(); }
2755 template <> void S<int>::f();
2756
2757 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2758 invalid; there should be no template <>.)
2759
2760 If the function is a specialization, it is marked as such via
2761 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2762 is set up correctly, and it is added to the list of specializations
2763 for that template. */
2764
2765 tree
2766 check_explicit_specialization (tree declarator,
2767 tree decl,
2768 int template_count,
2769 int flags,
2770 tree attrlist)
2771 {
2772 int have_def = flags & 2;
2773 int is_friend = flags & 4;
2774 bool is_concept = flags & 8;
2775 int specialization = 0;
2776 int explicit_instantiation = 0;
2777 int member_specialization = 0;
2778 tree ctype = DECL_CLASS_CONTEXT (decl);
2779 tree dname = DECL_NAME (decl);
2780 tmpl_spec_kind tsk;
2781
2782 if (is_friend)
2783 {
2784 if (!processing_specialization)
2785 tsk = tsk_none;
2786 else
2787 tsk = tsk_excessive_parms;
2788 }
2789 else
2790 tsk = current_tmpl_spec_kind (template_count);
2791
2792 switch (tsk)
2793 {
2794 case tsk_none:
2795 if (processing_specialization && !VAR_P (decl))
2796 {
2797 specialization = 1;
2798 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2799 }
2800 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2801 {
2802 if (is_friend)
2803 /* This could be something like:
2804
2805 template <class T> void f(T);
2806 class S { friend void f<>(int); } */
2807 specialization = 1;
2808 else
2809 {
2810 /* This case handles bogus declarations like template <>
2811 template <class T> void f<int>(); */
2812
2813 error ("template-id %qD in declaration of primary template",
2814 declarator);
2815 return decl;
2816 }
2817 }
2818 break;
2819
2820 case tsk_invalid_member_spec:
2821 /* The error has already been reported in
2822 check_specialization_scope. */
2823 return error_mark_node;
2824
2825 case tsk_invalid_expl_inst:
2826 error ("template parameter list used in explicit instantiation");
2827
2828 /* Fall through. */
2829
2830 case tsk_expl_inst:
2831 if (have_def)
2832 error ("definition provided for explicit instantiation");
2833
2834 explicit_instantiation = 1;
2835 break;
2836
2837 case tsk_excessive_parms:
2838 case tsk_insufficient_parms:
2839 if (tsk == tsk_excessive_parms)
2840 error ("too many template parameter lists in declaration of %qD",
2841 decl);
2842 else if (template_header_count)
2843 error("too few template parameter lists in declaration of %qD", decl);
2844 else
2845 error("explicit specialization of %qD must be introduced by "
2846 "%<template <>%>", decl);
2847
2848 /* Fall through. */
2849 case tsk_expl_spec:
2850 if (is_concept)
2851 error ("explicit specialization declared %<concept%>");
2852
2853 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2854 /* In cases like template<> constexpr bool v = true;
2855 We'll give an error in check_template_variable. */
2856 break;
2857
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2859 if (ctype)
2860 member_specialization = 1;
2861 else
2862 specialization = 1;
2863 break;
2864
2865 case tsk_template:
2866 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2867 {
2868 /* This case handles bogus declarations like template <>
2869 template <class T> void f<int>(); */
2870
2871 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2872 error ("template-id %qD in declaration of primary template",
2873 declarator);
2874 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2875 {
2876 /* Partial specialization of variable template. */
2877 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2878 specialization = 1;
2879 goto ok;
2880 }
2881 else if (cxx_dialect < cxx14)
2882 error ("non-type partial specialization %qD "
2883 "is not allowed", declarator);
2884 else
2885 error ("non-class, non-variable partial specialization %qD "
2886 "is not allowed", declarator);
2887 return decl;
2888 ok:;
2889 }
2890
2891 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2892 /* This is a specialization of a member template, without
2893 specialization the containing class. Something like:
2894
2895 template <class T> struct S {
2896 template <class U> void f (U);
2897 };
2898 template <> template <class U> void S<int>::f(U) {}
2899
2900 That's a specialization -- but of the entire template. */
2901 specialization = 1;
2902 break;
2903
2904 default:
2905 gcc_unreachable ();
2906 }
2907
2908 if ((specialization || member_specialization)
2909 /* This doesn't apply to variable templates. */
2910 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2911 {
2912 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2913 for (; t; t = TREE_CHAIN (t))
2914 if (TREE_PURPOSE (t))
2915 {
2916 permerror (input_location,
2917 "default argument specified in explicit specialization");
2918 break;
2919 }
2920 }
2921
2922 if (specialization || member_specialization || explicit_instantiation)
2923 {
2924 tree tmpl = NULL_TREE;
2925 tree targs = NULL_TREE;
2926 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2927
2928 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2929 if (!was_template_id)
2930 {
2931 tree fns;
2932
2933 gcc_assert (identifier_p (declarator));
2934 if (ctype)
2935 fns = dname;
2936 else
2937 {
2938 /* If there is no class context, the explicit instantiation
2939 must be at namespace scope. */
2940 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2941
2942 /* Find the namespace binding, using the declaration
2943 context. */
2944 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2945 false, true);
2946 if (fns == error_mark_node)
2947 /* If lookup fails, look for a friend declaration so we can
2948 give a better diagnostic. */
2949 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2950 /*type*/false, /*complain*/true,
2951 /*hidden*/true);
2952
2953 if (fns == error_mark_node || !is_overloaded_fn (fns))
2954 {
2955 error ("%qD is not a template function", dname);
2956 fns = error_mark_node;
2957 }
2958 }
2959
2960 declarator = lookup_template_function (fns, NULL_TREE);
2961 }
2962
2963 if (declarator == error_mark_node)
2964 return error_mark_node;
2965
2966 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2967 {
2968 if (!explicit_instantiation)
2969 /* A specialization in class scope. This is invalid,
2970 but the error will already have been flagged by
2971 check_specialization_scope. */
2972 return error_mark_node;
2973 else
2974 {
2975 /* It's not valid to write an explicit instantiation in
2976 class scope, e.g.:
2977
2978 class C { template void f(); }
2979
2980 This case is caught by the parser. However, on
2981 something like:
2982
2983 template class C { void f(); };
2984
2985 (which is invalid) we can get here. The error will be
2986 issued later. */
2987 ;
2988 }
2989
2990 return decl;
2991 }
2992 else if (ctype != NULL_TREE
2993 && (identifier_p (TREE_OPERAND (declarator, 0))))
2994 {
2995 // We'll match variable templates in start_decl.
2996 if (VAR_P (decl))
2997 return decl;
2998
2999 /* Find the list of functions in ctype that have the same
3000 name as the declared function. */
3001 tree name = TREE_OPERAND (declarator, 0);
3002
3003 if (constructor_name_p (name, ctype))
3004 {
3005 if (DECL_CONSTRUCTOR_P (decl)
3006 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3007 : !CLASSTYPE_DESTRUCTOR (ctype))
3008 {
3009 /* From [temp.expl.spec]:
3010
3011 If such an explicit specialization for the member
3012 of a class template names an implicitly-declared
3013 special member function (clause _special_), the
3014 program is ill-formed.
3015
3016 Similar language is found in [temp.explicit]. */
3017 error ("specialization of implicitly-declared special member function");
3018 return error_mark_node;
3019 }
3020
3021 name = DECL_NAME (decl);
3022 }
3023
3024 /* For a type-conversion operator, We might be looking for
3025 `operator int' which will be a specialization of
3026 `operator T'. Grab all the conversion operators, and
3027 then select from them. */
3028 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3029 ? conv_op_identifier : name);
3030
3031 if (fns == NULL_TREE)
3032 {
3033 error ("no member function %qD declared in %qT", name, ctype);
3034 return error_mark_node;
3035 }
3036 else
3037 TREE_OPERAND (declarator, 0) = fns;
3038 }
3039
3040 /* Figure out what exactly is being specialized at this point.
3041 Note that for an explicit instantiation, even one for a
3042 member function, we cannot tell a priori whether the
3043 instantiation is for a member template, or just a member
3044 function of a template class. Even if a member template is
3045 being instantiated, the member template arguments may be
3046 elided if they can be deduced from the rest of the
3047 declaration. */
3048 tmpl = determine_specialization (declarator, decl,
3049 &targs,
3050 member_specialization,
3051 template_count,
3052 tsk);
3053
3054 if (!tmpl || tmpl == error_mark_node)
3055 /* We couldn't figure out what this declaration was
3056 specializing. */
3057 return error_mark_node;
3058 else
3059 {
3060 if (TREE_CODE (decl) == FUNCTION_DECL
3061 && DECL_HIDDEN_FRIEND_P (tmpl))
3062 {
3063 auto_diagnostic_group d;
3064 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3065 "friend declaration %qD is not visible to "
3066 "explicit specialization", tmpl))
3067 inform (DECL_SOURCE_LOCATION (tmpl),
3068 "friend declaration here");
3069 }
3070 else if (!ctype && !is_friend
3071 && CP_DECL_CONTEXT (decl) == current_namespace)
3072 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3073
3074 tree gen_tmpl = most_general_template (tmpl);
3075
3076 if (explicit_instantiation)
3077 {
3078 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3079 is done by do_decl_instantiation later. */
3080
3081 int arg_depth = TMPL_ARGS_DEPTH (targs);
3082 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3083
3084 if (arg_depth > parm_depth)
3085 {
3086 /* If TMPL is not the most general template (for
3087 example, if TMPL is a friend template that is
3088 injected into namespace scope), then there will
3089 be too many levels of TARGS. Remove some of them
3090 here. */
3091 int i;
3092 tree new_targs;
3093
3094 new_targs = make_tree_vec (parm_depth);
3095 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3096 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3097 = TREE_VEC_ELT (targs, i);
3098 targs = new_targs;
3099 }
3100
3101 return instantiate_template (tmpl, targs, tf_error);
3102 }
3103
3104 /* If we thought that the DECL was a member function, but it
3105 turns out to be specializing a static member function,
3106 make DECL a static member function as well. */
3107 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3108 && DECL_STATIC_FUNCTION_P (tmpl)
3109 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3110 revert_static_member_fn (decl);
3111
3112 /* If this is a specialization of a member template of a
3113 template class, we want to return the TEMPLATE_DECL, not
3114 the specialization of it. */
3115 if (tsk == tsk_template && !was_template_id)
3116 {
3117 tree result = DECL_TEMPLATE_RESULT (tmpl);
3118 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3119 DECL_INITIAL (result) = NULL_TREE;
3120 if (have_def)
3121 {
3122 tree parm;
3123 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3124 DECL_SOURCE_LOCATION (result)
3125 = DECL_SOURCE_LOCATION (decl);
3126 /* We want to use the argument list specified in the
3127 definition, not in the original declaration. */
3128 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3129 for (parm = DECL_ARGUMENTS (result); parm;
3130 parm = DECL_CHAIN (parm))
3131 DECL_CONTEXT (parm) = result;
3132 }
3133 return register_specialization (tmpl, gen_tmpl, targs,
3134 is_friend, 0);
3135 }
3136
3137 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3138 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3139
3140 if (was_template_id)
3141 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3142
3143 /* Inherit default function arguments from the template
3144 DECL is specializing. */
3145 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3146 copy_default_args_to_explicit_spec (decl);
3147
3148 /* This specialization has the same protection as the
3149 template it specializes. */
3150 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3151 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3152
3153 /* 7.1.1-1 [dcl.stc]
3154
3155 A storage-class-specifier shall not be specified in an
3156 explicit specialization...
3157
3158 The parser rejects these, so unless action is taken here,
3159 explicit function specializations will always appear with
3160 global linkage.
3161
3162 The action recommended by the C++ CWG in response to C++
3163 defect report 605 is to make the storage class and linkage
3164 of the explicit specialization match the templated function:
3165
3166 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3167 */
3168 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3169 {
3170 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3171 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3172
3173 /* A concept cannot be specialized. */
3174 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3175 {
3176 error ("explicit specialization of function concept %qD",
3177 gen_tmpl);
3178 return error_mark_node;
3179 }
3180
3181 /* This specialization has the same linkage and visibility as
3182 the function template it specializes. */
3183 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3184 if (! TREE_PUBLIC (decl))
3185 {
3186 DECL_INTERFACE_KNOWN (decl) = 1;
3187 DECL_NOT_REALLY_EXTERN (decl) = 1;
3188 }
3189 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3190 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3191 {
3192 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3193 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3194 }
3195 }
3196
3197 /* If DECL is a friend declaration, declared using an
3198 unqualified name, the namespace associated with DECL may
3199 have been set incorrectly. For example, in:
3200
3201 template <typename T> void f(T);
3202 namespace N {
3203 struct S { friend void f<int>(int); }
3204 }
3205
3206 we will have set the DECL_CONTEXT for the friend
3207 declaration to N, rather than to the global namespace. */
3208 if (DECL_NAMESPACE_SCOPE_P (decl))
3209 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3210
3211 if (is_friend && !have_def)
3212 /* This is not really a declaration of a specialization.
3213 It's just the name of an instantiation. But, it's not
3214 a request for an instantiation, either. */
3215 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3216 else if (TREE_CODE (decl) == FUNCTION_DECL)
3217 /* A specialization is not necessarily COMDAT. */
3218 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3219 && DECL_DECLARED_INLINE_P (decl));
3220 else if (VAR_P (decl))
3221 DECL_COMDAT (decl) = false;
3222
3223 /* If this is a full specialization, register it so that we can find
3224 it again. Partial specializations will be registered in
3225 process_partial_specialization. */
3226 if (!processing_template_decl)
3227 {
3228 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3229
3230 decl = register_specialization (decl, gen_tmpl, targs,
3231 is_friend, 0);
3232 }
3233
3234
3235 /* A 'structor should already have clones. */
3236 gcc_assert (decl == error_mark_node
3237 || variable_template_p (tmpl)
3238 || !(DECL_CONSTRUCTOR_P (decl)
3239 || DECL_DESTRUCTOR_P (decl))
3240 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3241 }
3242 }
3243
3244 return decl;
3245 }
3246
3247 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3248 parameters. These are represented in the same format used for
3249 DECL_TEMPLATE_PARMS. */
3250
3251 int
3252 comp_template_parms (const_tree parms1, const_tree parms2)
3253 {
3254 const_tree p1;
3255 const_tree p2;
3256
3257 if (parms1 == parms2)
3258 return 1;
3259
3260 for (p1 = parms1, p2 = parms2;
3261 p1 != NULL_TREE && p2 != NULL_TREE;
3262 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3263 {
3264 tree t1 = TREE_VALUE (p1);
3265 tree t2 = TREE_VALUE (p2);
3266 int i;
3267
3268 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3269 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3270
3271 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3272 return 0;
3273
3274 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3275 {
3276 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3277 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3278
3279 /* If either of the template parameters are invalid, assume
3280 they match for the sake of error recovery. */
3281 if (error_operand_p (parm1) || error_operand_p (parm2))
3282 return 1;
3283
3284 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3285 return 0;
3286
3287 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3288 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3289 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3290 continue;
3291 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3292 return 0;
3293 }
3294 }
3295
3296 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3297 /* One set of parameters has more parameters lists than the
3298 other. */
3299 return 0;
3300
3301 return 1;
3302 }
3303
3304 /* Determine whether PARM is a parameter pack. */
3305
3306 bool
3307 template_parameter_pack_p (const_tree parm)
3308 {
3309 /* Determine if we have a non-type template parameter pack. */
3310 if (TREE_CODE (parm) == PARM_DECL)
3311 return (DECL_TEMPLATE_PARM_P (parm)
3312 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3313 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3314 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3315
3316 /* If this is a list of template parameters, we could get a
3317 TYPE_DECL or a TEMPLATE_DECL. */
3318 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3319 parm = TREE_TYPE (parm);
3320
3321 /* Otherwise it must be a type template parameter. */
3322 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3323 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3324 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3325 }
3326
3327 /* Determine if T is a function parameter pack. */
3328
3329 bool
3330 function_parameter_pack_p (const_tree t)
3331 {
3332 if (t && TREE_CODE (t) == PARM_DECL)
3333 return DECL_PACK_P (t);
3334 return false;
3335 }
3336
3337 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3338 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3339
3340 tree
3341 get_function_template_decl (const_tree primary_func_tmpl_inst)
3342 {
3343 if (! primary_func_tmpl_inst
3344 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3345 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3346 return NULL;
3347
3348 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3349 }
3350
3351 /* Return true iff the function parameter PARAM_DECL was expanded
3352 from the function parameter pack PACK. */
3353
3354 bool
3355 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3356 {
3357 if (DECL_ARTIFICIAL (param_decl)
3358 || !function_parameter_pack_p (pack))
3359 return false;
3360
3361 /* The parameter pack and its pack arguments have the same
3362 DECL_PARM_INDEX. */
3363 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3364 }
3365
3366 /* Determine whether ARGS describes a variadic template args list,
3367 i.e., one that is terminated by a template argument pack. */
3368
3369 static bool
3370 template_args_variadic_p (tree args)
3371 {
3372 int nargs;
3373 tree last_parm;
3374
3375 if (args == NULL_TREE)
3376 return false;
3377
3378 args = INNERMOST_TEMPLATE_ARGS (args);
3379 nargs = TREE_VEC_LENGTH (args);
3380
3381 if (nargs == 0)
3382 return false;
3383
3384 last_parm = TREE_VEC_ELT (args, nargs - 1);
3385
3386 return ARGUMENT_PACK_P (last_parm);
3387 }
3388
3389 /* Generate a new name for the parameter pack name NAME (an
3390 IDENTIFIER_NODE) that incorporates its */
3391
3392 static tree
3393 make_ith_pack_parameter_name (tree name, int i)
3394 {
3395 /* Munge the name to include the parameter index. */
3396 #define NUMBUF_LEN 128
3397 char numbuf[NUMBUF_LEN];
3398 char* newname;
3399 int newname_len;
3400
3401 if (name == NULL_TREE)
3402 return name;
3403 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3404 newname_len = IDENTIFIER_LENGTH (name)
3405 + strlen (numbuf) + 2;
3406 newname = (char*)alloca (newname_len);
3407 snprintf (newname, newname_len,
3408 "%s#%i", IDENTIFIER_POINTER (name), i);
3409 return get_identifier (newname);
3410 }
3411
3412 /* Return true if T is a primary function, class or alias template
3413 specialization, not including the template pattern. */
3414
3415 bool
3416 primary_template_specialization_p (const_tree t)
3417 {
3418 if (!t)
3419 return false;
3420
3421 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3422 return (DECL_LANG_SPECIFIC (t)
3423 && DECL_USE_TEMPLATE (t)
3424 && DECL_TEMPLATE_INFO (t)
3425 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3426 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3427 return (CLASSTYPE_TEMPLATE_INFO (t)
3428 && CLASSTYPE_USE_TEMPLATE (t)
3429 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3430 else if (alias_template_specialization_p (t))
3431 return true;
3432 return false;
3433 }
3434
3435 /* Return true if PARM is a template template parameter. */
3436
3437 bool
3438 template_template_parameter_p (const_tree parm)
3439 {
3440 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3441 }
3442
3443 /* Return true iff PARM is a DECL representing a type template
3444 parameter. */
3445
3446 bool
3447 template_type_parameter_p (const_tree parm)
3448 {
3449 return (parm
3450 && (TREE_CODE (parm) == TYPE_DECL
3451 || TREE_CODE (parm) == TEMPLATE_DECL)
3452 && DECL_TEMPLATE_PARM_P (parm));
3453 }
3454
3455 /* Return the template parameters of T if T is a
3456 primary template instantiation, NULL otherwise. */
3457
3458 tree
3459 get_primary_template_innermost_parameters (const_tree t)
3460 {
3461 tree parms = NULL, template_info = NULL;
3462
3463 if ((template_info = get_template_info (t))
3464 && primary_template_specialization_p (t))
3465 parms = INNERMOST_TEMPLATE_PARMS
3466 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3467
3468 return parms;
3469 }
3470
3471 /* Return the template parameters of the LEVELth level from the full list
3472 of template parameters PARMS. */
3473
3474 tree
3475 get_template_parms_at_level (tree parms, int level)
3476 {
3477 tree p;
3478 if (!parms
3479 || TREE_CODE (parms) != TREE_LIST
3480 || level > TMPL_PARMS_DEPTH (parms))
3481 return NULL_TREE;
3482
3483 for (p = parms; p; p = TREE_CHAIN (p))
3484 if (TMPL_PARMS_DEPTH (p) == level)
3485 return p;
3486
3487 return NULL_TREE;
3488 }
3489
3490 /* Returns the template arguments of T if T is a template instantiation,
3491 NULL otherwise. */
3492
3493 tree
3494 get_template_innermost_arguments (const_tree t)
3495 {
3496 tree args = NULL, template_info = NULL;
3497
3498 if ((template_info = get_template_info (t))
3499 && TI_ARGS (template_info))
3500 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3501
3502 return args;
3503 }
3504
3505 /* Return the argument pack elements of T if T is a template argument pack,
3506 NULL otherwise. */
3507
3508 tree
3509 get_template_argument_pack_elems (const_tree t)
3510 {
3511 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3512 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3513 return NULL;
3514
3515 return ARGUMENT_PACK_ARGS (t);
3516 }
3517
3518 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3519 ARGUMENT_PACK_SELECT represents. */
3520
3521 static tree
3522 argument_pack_select_arg (tree t)
3523 {
3524 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3525 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3526
3527 /* If the selected argument is an expansion E, that most likely means we were
3528 called from gen_elem_of_pack_expansion_instantiation during the
3529 substituting of an argument pack (of which the Ith element is a pack
3530 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3531 In this case, the Ith element resulting from this substituting is going to
3532 be a pack expansion, which pattern is the pattern of E. Let's return the
3533 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3534 resulting pack expansion from it. */
3535 if (PACK_EXPANSION_P (arg))
3536 {
3537 /* Make sure we aren't throwing away arg info. */
3538 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3539 arg = PACK_EXPANSION_PATTERN (arg);
3540 }
3541
3542 return arg;
3543 }
3544
3545
3546 /* True iff FN is a function representing a built-in variadic parameter
3547 pack. */
3548
3549 bool
3550 builtin_pack_fn_p (tree fn)
3551 {
3552 if (!fn
3553 || TREE_CODE (fn) != FUNCTION_DECL
3554 || !DECL_IS_BUILTIN (fn))
3555 return false;
3556
3557 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3558 return true;
3559
3560 return false;
3561 }
3562
3563 /* True iff CALL is a call to a function representing a built-in variadic
3564 parameter pack. */
3565
3566 static bool
3567 builtin_pack_call_p (tree call)
3568 {
3569 if (TREE_CODE (call) != CALL_EXPR)
3570 return false;
3571 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3572 }
3573
3574 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3575
3576 static tree
3577 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3578 tree in_decl)
3579 {
3580 tree ohi = CALL_EXPR_ARG (call, 0);
3581 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3582 false/*fn*/, true/*int_cst*/);
3583
3584 if (value_dependent_expression_p (hi))
3585 {
3586 if (hi != ohi)
3587 {
3588 call = copy_node (call);
3589 CALL_EXPR_ARG (call, 0) = hi;
3590 }
3591 tree ex = make_pack_expansion (call, complain);
3592 tree vec = make_tree_vec (1);
3593 TREE_VEC_ELT (vec, 0) = ex;
3594 return vec;
3595 }
3596 else
3597 {
3598 hi = cxx_constant_value (hi);
3599 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3600
3601 /* Calculate the largest value of len that won't make the size of the vec
3602 overflow an int. The compiler will exceed resource limits long before
3603 this, but it seems a decent place to diagnose. */
3604 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3605
3606 if (len < 0 || len > max)
3607 {
3608 if ((complain & tf_error)
3609 && hi != error_mark_node)
3610 error ("argument to %<__integer_pack%> must be between 0 and %d",
3611 max);
3612 return error_mark_node;
3613 }
3614
3615 tree vec = make_tree_vec (len);
3616
3617 for (int i = 0; i < len; ++i)
3618 TREE_VEC_ELT (vec, i) = size_int (i);
3619
3620 return vec;
3621 }
3622 }
3623
3624 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3625 CALL. */
3626
3627 static tree
3628 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3629 tree in_decl)
3630 {
3631 if (!builtin_pack_call_p (call))
3632 return NULL_TREE;
3633
3634 tree fn = CALL_EXPR_FN (call);
3635
3636 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3637 return expand_integer_pack (call, args, complain, in_decl);
3638
3639 return NULL_TREE;
3640 }
3641
3642 /* Structure used to track the progress of find_parameter_packs_r. */
3643 struct find_parameter_pack_data
3644 {
3645 /* TREE_LIST that will contain all of the parameter packs found by
3646 the traversal. */
3647 tree* parameter_packs;
3648
3649 /* Set of AST nodes that have been visited by the traversal. */
3650 hash_set<tree> *visited;
3651
3652 /* True iff we're making a type pack expansion. */
3653 bool type_pack_expansion_p;
3654 };
3655
3656 /* Identifies all of the argument packs that occur in a template
3657 argument and appends them to the TREE_LIST inside DATA, which is a
3658 find_parameter_pack_data structure. This is a subroutine of
3659 make_pack_expansion and uses_parameter_packs. */
3660 static tree
3661 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3662 {
3663 tree t = *tp;
3664 struct find_parameter_pack_data* ppd =
3665 (struct find_parameter_pack_data*)data;
3666 bool parameter_pack_p = false;
3667
3668 /* Handle type aliases/typedefs. */
3669 if (TYPE_ALIAS_P (t))
3670 {
3671 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3672 cp_walk_tree (&TI_ARGS (tinfo),
3673 &find_parameter_packs_r,
3674 ppd, ppd->visited);
3675 *walk_subtrees = 0;
3676 return NULL_TREE;
3677 }
3678
3679 /* Identify whether this is a parameter pack or not. */
3680 switch (TREE_CODE (t))
3681 {
3682 case TEMPLATE_PARM_INDEX:
3683 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3684 parameter_pack_p = true;
3685 break;
3686
3687 case TEMPLATE_TYPE_PARM:
3688 t = TYPE_MAIN_VARIANT (t);
3689 /* FALLTHRU */
3690 case TEMPLATE_TEMPLATE_PARM:
3691 /* If the placeholder appears in the decl-specifier-seq of a function
3692 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3693 is a pack expansion, the invented template parameter is a template
3694 parameter pack. */
3695 if (ppd->type_pack_expansion_p && is_auto (t))
3696 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3697 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3698 parameter_pack_p = true;
3699 break;
3700
3701 case FIELD_DECL:
3702 case PARM_DECL:
3703 if (DECL_PACK_P (t))
3704 {
3705 /* We don't want to walk into the type of a PARM_DECL,
3706 because we don't want to see the type parameter pack. */
3707 *walk_subtrees = 0;
3708 parameter_pack_p = true;
3709 }
3710 break;
3711
3712 case VAR_DECL:
3713 if (DECL_PACK_P (t))
3714 {
3715 /* We don't want to walk into the type of a variadic capture proxy,
3716 because we don't want to see the type parameter pack. */
3717 *walk_subtrees = 0;
3718 parameter_pack_p = true;
3719 }
3720 else if (variable_template_specialization_p (t))
3721 {
3722 cp_walk_tree (&DECL_TI_ARGS (t),
3723 find_parameter_packs_r,
3724 ppd, ppd->visited);
3725 *walk_subtrees = 0;
3726 }
3727 break;
3728
3729 case CALL_EXPR:
3730 if (builtin_pack_call_p (t))
3731 parameter_pack_p = true;
3732 break;
3733
3734 case BASES:
3735 parameter_pack_p = true;
3736 break;
3737 default:
3738 /* Not a parameter pack. */
3739 break;
3740 }
3741
3742 if (parameter_pack_p)
3743 {
3744 /* Add this parameter pack to the list. */
3745 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3746 }
3747
3748 if (TYPE_P (t))
3749 cp_walk_tree (&TYPE_CONTEXT (t),
3750 &find_parameter_packs_r, ppd, ppd->visited);
3751
3752 /* This switch statement will return immediately if we don't find a
3753 parameter pack. */
3754 switch (TREE_CODE (t))
3755 {
3756 case TEMPLATE_PARM_INDEX:
3757 return NULL_TREE;
3758
3759 case BOUND_TEMPLATE_TEMPLATE_PARM:
3760 /* Check the template itself. */
3761 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3762 &find_parameter_packs_r, ppd, ppd->visited);
3763 /* Check the template arguments. */
3764 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3765 ppd->visited);
3766 *walk_subtrees = 0;
3767 return NULL_TREE;
3768
3769 case TEMPLATE_TYPE_PARM:
3770 case TEMPLATE_TEMPLATE_PARM:
3771 return NULL_TREE;
3772
3773 case PARM_DECL:
3774 return NULL_TREE;
3775
3776 case DECL_EXPR:
3777 /* Ignore the declaration of a capture proxy for a parameter pack. */
3778 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3779 *walk_subtrees = 0;
3780 return NULL_TREE;
3781
3782 case RECORD_TYPE:
3783 if (TYPE_PTRMEMFUNC_P (t))
3784 return NULL_TREE;
3785 /* Fall through. */
3786
3787 case UNION_TYPE:
3788 case ENUMERAL_TYPE:
3789 if (TYPE_TEMPLATE_INFO (t))
3790 cp_walk_tree (&TYPE_TI_ARGS (t),
3791 &find_parameter_packs_r, ppd, ppd->visited);
3792
3793 *walk_subtrees = 0;
3794 return NULL_TREE;
3795
3796 case TEMPLATE_DECL:
3797 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3798 return NULL_TREE;
3799 gcc_fallthrough();
3800
3801 case CONSTRUCTOR:
3802 cp_walk_tree (&TREE_TYPE (t),
3803 &find_parameter_packs_r, ppd, ppd->visited);
3804 return NULL_TREE;
3805
3806 case TYPENAME_TYPE:
3807 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3808 ppd, ppd->visited);
3809 *walk_subtrees = 0;
3810 return NULL_TREE;
3811
3812 case TYPE_PACK_EXPANSION:
3813 case EXPR_PACK_EXPANSION:
3814 *walk_subtrees = 0;
3815 return NULL_TREE;
3816
3817 case INTEGER_TYPE:
3818 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3819 ppd, ppd->visited);
3820 *walk_subtrees = 0;
3821 return NULL_TREE;
3822
3823 case IDENTIFIER_NODE:
3824 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3825 ppd->visited);
3826 *walk_subtrees = 0;
3827 return NULL_TREE;
3828
3829 case LAMBDA_EXPR:
3830 {
3831 /* Look at explicit captures. */
3832 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3833 cap; cap = TREE_CHAIN (cap))
3834 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3835 ppd->visited);
3836 /* Since we defer implicit capture, look in the parms and body. */
3837 tree fn = lambda_function (t);
3838 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3839 ppd->visited);
3840 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3841 ppd->visited);
3842 *walk_subtrees = 0;
3843 return NULL_TREE;
3844 }
3845
3846 case DECLTYPE_TYPE:
3847 {
3848 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3849 type_pack_expansion_p to false so that any placeholders
3850 within the expression don't get marked as parameter packs. */
3851 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3852 ppd->type_pack_expansion_p = false;
3853 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3854 ppd, ppd->visited);
3855 ppd->type_pack_expansion_p = type_pack_expansion_p;
3856 *walk_subtrees = 0;
3857 return NULL_TREE;
3858 }
3859
3860 case IF_STMT:
3861 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3862 ppd, ppd->visited);
3863 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3864 ppd, ppd->visited);
3865 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3866 ppd, ppd->visited);
3867 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3868 *walk_subtrees = 0;
3869 return NULL_TREE;
3870
3871 default:
3872 return NULL_TREE;
3873 }
3874
3875 return NULL_TREE;
3876 }
3877
3878 /* Determines if the expression or type T uses any parameter packs. */
3879 bool
3880 uses_parameter_packs (tree t)
3881 {
3882 tree parameter_packs = NULL_TREE;
3883 struct find_parameter_pack_data ppd;
3884 ppd.parameter_packs = &parameter_packs;
3885 ppd.visited = new hash_set<tree>;
3886 ppd.type_pack_expansion_p = false;
3887 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3888 delete ppd.visited;
3889 return parameter_packs != NULL_TREE;
3890 }
3891
3892 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3893 representation a base-class initializer into a parameter pack
3894 expansion. If all goes well, the resulting node will be an
3895 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3896 respectively. */
3897 tree
3898 make_pack_expansion (tree arg, tsubst_flags_t complain)
3899 {
3900 tree result;
3901 tree parameter_packs = NULL_TREE;
3902 bool for_types = false;
3903 struct find_parameter_pack_data ppd;
3904
3905 if (!arg || arg == error_mark_node)
3906 return arg;
3907
3908 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3909 {
3910 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3911 class initializer. In this case, the TREE_PURPOSE will be a
3912 _TYPE node (representing the base class expansion we're
3913 initializing) and the TREE_VALUE will be a TREE_LIST
3914 containing the initialization arguments.
3915
3916 The resulting expansion looks somewhat different from most
3917 expansions. Rather than returning just one _EXPANSION, we
3918 return a TREE_LIST whose TREE_PURPOSE is a
3919 TYPE_PACK_EXPANSION containing the bases that will be
3920 initialized. The TREE_VALUE will be identical to the
3921 original TREE_VALUE, which is a list of arguments that will
3922 be passed to each base. We do not introduce any new pack
3923 expansion nodes into the TREE_VALUE (although it is possible
3924 that some already exist), because the TREE_PURPOSE and
3925 TREE_VALUE all need to be expanded together with the same
3926 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3927 resulting TREE_PURPOSE will mention the parameter packs in
3928 both the bases and the arguments to the bases. */
3929 tree purpose;
3930 tree value;
3931 tree parameter_packs = NULL_TREE;
3932
3933 /* Determine which parameter packs will be used by the base
3934 class expansion. */
3935 ppd.visited = new hash_set<tree>;
3936 ppd.parameter_packs = &parameter_packs;
3937 ppd.type_pack_expansion_p = false;
3938 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3939 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3940 &ppd, ppd.visited);
3941
3942 if (parameter_packs == NULL_TREE)
3943 {
3944 if (complain & tf_error)
3945 error ("base initializer expansion %qT contains no parameter packs",
3946 arg);
3947 delete ppd.visited;
3948 return error_mark_node;
3949 }
3950
3951 if (TREE_VALUE (arg) != void_type_node)
3952 {
3953 /* Collect the sets of parameter packs used in each of the
3954 initialization arguments. */
3955 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3956 {
3957 /* Determine which parameter packs will be expanded in this
3958 argument. */
3959 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3960 &ppd, ppd.visited);
3961 }
3962 }
3963
3964 delete ppd.visited;
3965
3966 /* Create the pack expansion type for the base type. */
3967 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3968 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3969 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3970 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3971
3972 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3973 they will rarely be compared to anything. */
3974 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3975
3976 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3977 }
3978
3979 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3980 for_types = true;
3981
3982 /* Build the PACK_EXPANSION_* node. */
3983 result = for_types
3984 ? cxx_make_type (TYPE_PACK_EXPANSION)
3985 : make_node (EXPR_PACK_EXPANSION);
3986 SET_PACK_EXPANSION_PATTERN (result, arg);
3987 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3988 {
3989 /* Propagate type and const-expression information. */
3990 TREE_TYPE (result) = TREE_TYPE (arg);
3991 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3992 /* Mark this read now, since the expansion might be length 0. */
3993 mark_exp_read (arg);
3994 }
3995 else
3996 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3997 they will rarely be compared to anything. */
3998 SET_TYPE_STRUCTURAL_EQUALITY (result);
3999
4000 /* Determine which parameter packs will be expanded. */
4001 ppd.parameter_packs = &parameter_packs;
4002 ppd.visited = new hash_set<tree>;
4003 ppd.type_pack_expansion_p = TYPE_P (arg);
4004 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4005 delete ppd.visited;
4006
4007 /* Make sure we found some parameter packs. */
4008 if (parameter_packs == NULL_TREE)
4009 {
4010 if (complain & tf_error)
4011 {
4012 if (TYPE_P (arg))
4013 error ("expansion pattern %qT contains no parameter packs", arg);
4014 else
4015 error ("expansion pattern %qE contains no parameter packs", arg);
4016 }
4017 return error_mark_node;
4018 }
4019 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4020
4021 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4022
4023 return result;
4024 }
4025
4026 /* Checks T for any "bare" parameter packs, which have not yet been
4027 expanded, and issues an error if any are found. This operation can
4028 only be done on full expressions or types (e.g., an expression
4029 statement, "if" condition, etc.), because we could have expressions like:
4030
4031 foo(f(g(h(args)))...)
4032
4033 where "args" is a parameter pack. check_for_bare_parameter_packs
4034 should not be called for the subexpressions args, h(args),
4035 g(h(args)), or f(g(h(args))), because we would produce erroneous
4036 error messages.
4037
4038 Returns TRUE and emits an error if there were bare parameter packs,
4039 returns FALSE otherwise. */
4040 bool
4041 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4042 {
4043 tree parameter_packs = NULL_TREE;
4044 struct find_parameter_pack_data ppd;
4045
4046 if (!processing_template_decl || !t || t == error_mark_node)
4047 return false;
4048
4049 /* A lambda might use a parameter pack from the containing context. */
4050 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4051 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4052 return false;
4053
4054 if (TREE_CODE (t) == TYPE_DECL)
4055 t = TREE_TYPE (t);
4056
4057 ppd.parameter_packs = &parameter_packs;
4058 ppd.visited = new hash_set<tree>;
4059 ppd.type_pack_expansion_p = false;
4060 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4061 delete ppd.visited;
4062
4063 if (parameter_packs)
4064 {
4065 if (loc == UNKNOWN_LOCATION)
4066 loc = cp_expr_loc_or_loc (t, input_location);
4067 error_at (loc, "parameter packs not expanded with %<...%>:");
4068 while (parameter_packs)
4069 {
4070 tree pack = TREE_VALUE (parameter_packs);
4071 tree name = NULL_TREE;
4072
4073 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4074 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4075 name = TYPE_NAME (pack);
4076 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4077 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4078 else if (TREE_CODE (pack) == CALL_EXPR)
4079 name = DECL_NAME (CALL_EXPR_FN (pack));
4080 else
4081 name = DECL_NAME (pack);
4082
4083 if (name)
4084 inform (loc, " %qD", name);
4085 else
4086 inform (loc, " %s", "<anonymous>");
4087
4088 parameter_packs = TREE_CHAIN (parameter_packs);
4089 }
4090
4091 return true;
4092 }
4093
4094 return false;
4095 }
4096
4097 /* Expand any parameter packs that occur in the template arguments in
4098 ARGS. */
4099 tree
4100 expand_template_argument_pack (tree args)
4101 {
4102 if (args == error_mark_node)
4103 return error_mark_node;
4104
4105 tree result_args = NULL_TREE;
4106 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4107 int num_result_args = -1;
4108 int non_default_args_count = -1;
4109
4110 /* First, determine if we need to expand anything, and the number of
4111 slots we'll need. */
4112 for (in_arg = 0; in_arg < nargs; ++in_arg)
4113 {
4114 tree arg = TREE_VEC_ELT (args, in_arg);
4115 if (arg == NULL_TREE)
4116 return args;
4117 if (ARGUMENT_PACK_P (arg))
4118 {
4119 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4120 if (num_result_args < 0)
4121 num_result_args = in_arg + num_packed;
4122 else
4123 num_result_args += num_packed;
4124 }
4125 else
4126 {
4127 if (num_result_args >= 0)
4128 num_result_args++;
4129 }
4130 }
4131
4132 /* If no expansion is necessary, we're done. */
4133 if (num_result_args < 0)
4134 return args;
4135
4136 /* Expand arguments. */
4137 result_args = make_tree_vec (num_result_args);
4138 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4139 non_default_args_count =
4140 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4141 for (in_arg = 0; in_arg < nargs; ++in_arg)
4142 {
4143 tree arg = TREE_VEC_ELT (args, in_arg);
4144 if (ARGUMENT_PACK_P (arg))
4145 {
4146 tree packed = ARGUMENT_PACK_ARGS (arg);
4147 int i, num_packed = TREE_VEC_LENGTH (packed);
4148 for (i = 0; i < num_packed; ++i, ++out_arg)
4149 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4150 if (non_default_args_count > 0)
4151 non_default_args_count += num_packed - 1;
4152 }
4153 else
4154 {
4155 TREE_VEC_ELT (result_args, out_arg) = arg;
4156 ++out_arg;
4157 }
4158 }
4159 if (non_default_args_count >= 0)
4160 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4161 return result_args;
4162 }
4163
4164 /* Checks if DECL shadows a template parameter.
4165
4166 [temp.local]: A template-parameter shall not be redeclared within its
4167 scope (including nested scopes).
4168
4169 Emits an error and returns TRUE if the DECL shadows a parameter,
4170 returns FALSE otherwise. */
4171
4172 bool
4173 check_template_shadow (tree decl)
4174 {
4175 tree olddecl;
4176
4177 /* If we're not in a template, we can't possibly shadow a template
4178 parameter. */
4179 if (!current_template_parms)
4180 return true;
4181
4182 /* Figure out what we're shadowing. */
4183 decl = OVL_FIRST (decl);
4184 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4185
4186 /* If there's no previous binding for this name, we're not shadowing
4187 anything, let alone a template parameter. */
4188 if (!olddecl)
4189 return true;
4190
4191 /* If we're not shadowing a template parameter, we're done. Note
4192 that OLDDECL might be an OVERLOAD (or perhaps even an
4193 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4194 node. */
4195 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4196 return true;
4197
4198 /* We check for decl != olddecl to avoid bogus errors for using a
4199 name inside a class. We check TPFI to avoid duplicate errors for
4200 inline member templates. */
4201 if (decl == olddecl
4202 || (DECL_TEMPLATE_PARM_P (decl)
4203 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4204 return true;
4205
4206 /* Don't complain about the injected class name, as we've already
4207 complained about the class itself. */
4208 if (DECL_SELF_REFERENCE_P (decl))
4209 return false;
4210
4211 if (DECL_TEMPLATE_PARM_P (decl))
4212 error ("declaration of template parameter %q+D shadows "
4213 "template parameter", decl);
4214 else
4215 error ("declaration of %q+#D shadows template parameter", decl);
4216 inform (DECL_SOURCE_LOCATION (olddecl),
4217 "template parameter %qD declared here", olddecl);
4218 return false;
4219 }
4220
4221 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4222 ORIG_LEVEL, DECL, and TYPE. */
4223
4224 static tree
4225 build_template_parm_index (int index,
4226 int level,
4227 int orig_level,
4228 tree decl,
4229 tree type)
4230 {
4231 tree t = make_node (TEMPLATE_PARM_INDEX);
4232 TEMPLATE_PARM_IDX (t) = index;
4233 TEMPLATE_PARM_LEVEL (t) = level;
4234 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4235 TEMPLATE_PARM_DECL (t) = decl;
4236 TREE_TYPE (t) = type;
4237 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4238 TREE_READONLY (t) = TREE_READONLY (decl);
4239
4240 return t;
4241 }
4242
4243 /* Find the canonical type parameter for the given template type
4244 parameter. Returns the canonical type parameter, which may be TYPE
4245 if no such parameter existed. */
4246
4247 static tree
4248 canonical_type_parameter (tree type)
4249 {
4250 tree list;
4251 int idx = TEMPLATE_TYPE_IDX (type);
4252 if (!canonical_template_parms)
4253 vec_alloc (canonical_template_parms, idx + 1);
4254
4255 if (canonical_template_parms->length () <= (unsigned) idx)
4256 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4257
4258 list = (*canonical_template_parms)[idx];
4259 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4260 list = TREE_CHAIN (list);
4261
4262 if (list)
4263 return TREE_VALUE (list);
4264 else
4265 {
4266 (*canonical_template_parms)[idx]
4267 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4268 return type;
4269 }
4270 }
4271
4272 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4273 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4274 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4275 new one is created. */
4276
4277 static tree
4278 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4279 tsubst_flags_t complain)
4280 {
4281 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4282 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4283 != TEMPLATE_PARM_LEVEL (index) - levels)
4284 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4285 {
4286 tree orig_decl = TEMPLATE_PARM_DECL (index);
4287 tree decl, t;
4288
4289 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4290 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4291 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4292 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4293 DECL_ARTIFICIAL (decl) = 1;
4294 SET_DECL_TEMPLATE_PARM_P (decl);
4295
4296 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4297 TEMPLATE_PARM_LEVEL (index) - levels,
4298 TEMPLATE_PARM_ORIG_LEVEL (index),
4299 decl, type);
4300 TEMPLATE_PARM_DESCENDANTS (index) = t;
4301 TEMPLATE_PARM_PARAMETER_PACK (t)
4302 = TEMPLATE_PARM_PARAMETER_PACK (index);
4303
4304 /* Template template parameters need this. */
4305 if (TREE_CODE (decl) == TEMPLATE_DECL)
4306 {
4307 DECL_TEMPLATE_RESULT (decl)
4308 = build_decl (DECL_SOURCE_LOCATION (decl),
4309 TYPE_DECL, DECL_NAME (decl), type);
4310 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4311 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4312 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4313 }
4314 }
4315
4316 return TEMPLATE_PARM_DESCENDANTS (index);
4317 }
4318
4319 /* Process information from new template parameter PARM and append it
4320 to the LIST being built. This new parameter is a non-type
4321 parameter iff IS_NON_TYPE is true. This new parameter is a
4322 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4323 is in PARM_LOC. */
4324
4325 tree
4326 process_template_parm (tree list, location_t parm_loc, tree parm,
4327 bool is_non_type, bool is_parameter_pack)
4328 {
4329 tree decl = 0;
4330 int idx = 0;
4331
4332 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4333 tree defval = TREE_PURPOSE (parm);
4334 tree constr = TREE_TYPE (parm);
4335
4336 if (list)
4337 {
4338 tree p = tree_last (list);
4339
4340 if (p && TREE_VALUE (p) != error_mark_node)
4341 {
4342 p = TREE_VALUE (p);
4343 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4344 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4345 else
4346 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4347 }
4348
4349 ++idx;
4350 }
4351
4352 if (is_non_type)
4353 {
4354 parm = TREE_VALUE (parm);
4355
4356 SET_DECL_TEMPLATE_PARM_P (parm);
4357
4358 if (TREE_TYPE (parm) != error_mark_node)
4359 {
4360 /* [temp.param]
4361
4362 The top-level cv-qualifiers on the template-parameter are
4363 ignored when determining its type. */
4364 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4365 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4366 TREE_TYPE (parm) = error_mark_node;
4367 else if (uses_parameter_packs (TREE_TYPE (parm))
4368 && !is_parameter_pack
4369 /* If we're in a nested template parameter list, the template
4370 template parameter could be a parameter pack. */
4371 && processing_template_parmlist == 1)
4372 {
4373 /* This template parameter is not a parameter pack, but it
4374 should be. Complain about "bare" parameter packs. */
4375 check_for_bare_parameter_packs (TREE_TYPE (parm));
4376
4377 /* Recover by calling this a parameter pack. */
4378 is_parameter_pack = true;
4379 }
4380 }
4381
4382 /* A template parameter is not modifiable. */
4383 TREE_CONSTANT (parm) = 1;
4384 TREE_READONLY (parm) = 1;
4385 decl = build_decl (parm_loc,
4386 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4387 TREE_CONSTANT (decl) = 1;
4388 TREE_READONLY (decl) = 1;
4389 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4390 = build_template_parm_index (idx, processing_template_decl,
4391 processing_template_decl,
4392 decl, TREE_TYPE (parm));
4393
4394 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4395 = is_parameter_pack;
4396 }
4397 else
4398 {
4399 tree t;
4400 parm = TREE_VALUE (TREE_VALUE (parm));
4401
4402 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4403 {
4404 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4405 /* This is for distinguishing between real templates and template
4406 template parameters */
4407 TREE_TYPE (parm) = t;
4408 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4409 decl = parm;
4410 }
4411 else
4412 {
4413 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4414 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4415 decl = build_decl (parm_loc,
4416 TYPE_DECL, parm, t);
4417 }
4418
4419 TYPE_NAME (t) = decl;
4420 TYPE_STUB_DECL (t) = decl;
4421 parm = decl;
4422 TEMPLATE_TYPE_PARM_INDEX (t)
4423 = build_template_parm_index (idx, processing_template_decl,
4424 processing_template_decl,
4425 decl, TREE_TYPE (parm));
4426 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4427 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4428 }
4429 DECL_ARTIFICIAL (decl) = 1;
4430 SET_DECL_TEMPLATE_PARM_P (decl);
4431
4432 /* Build requirements for the type/template parameter.
4433 This must be done after SET_DECL_TEMPLATE_PARM_P or
4434 process_template_parm could fail. */
4435 tree reqs = finish_shorthand_constraint (parm, constr);
4436
4437 decl = pushdecl (decl);
4438 if (!is_non_type)
4439 parm = decl;
4440
4441 /* Build the parameter node linking the parameter declaration,
4442 its default argument (if any), and its constraints (if any). */
4443 parm = build_tree_list (defval, parm);
4444 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4445
4446 return chainon (list, parm);
4447 }
4448
4449 /* The end of a template parameter list has been reached. Process the
4450 tree list into a parameter vector, converting each parameter into a more
4451 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4452 as PARM_DECLs. */
4453
4454 tree
4455 end_template_parm_list (tree parms)
4456 {
4457 int nparms;
4458 tree parm, next;
4459 tree saved_parmlist = make_tree_vec (list_length (parms));
4460
4461 /* Pop the dummy parameter level and add the real one. */
4462 current_template_parms = TREE_CHAIN (current_template_parms);
4463
4464 current_template_parms
4465 = tree_cons (size_int (processing_template_decl),
4466 saved_parmlist, current_template_parms);
4467
4468 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4469 {
4470 next = TREE_CHAIN (parm);
4471 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4472 TREE_CHAIN (parm) = NULL_TREE;
4473 }
4474
4475 --processing_template_parmlist;
4476
4477 return saved_parmlist;
4478 }
4479
4480 // Explicitly indicate the end of the template parameter list. We assume
4481 // that the current template parameters have been constructed and/or
4482 // managed explicitly, as when creating new template template parameters
4483 // from a shorthand constraint.
4484 void
4485 end_template_parm_list ()
4486 {
4487 --processing_template_parmlist;
4488 }
4489
4490 /* end_template_decl is called after a template declaration is seen. */
4491
4492 void
4493 end_template_decl (void)
4494 {
4495 reset_specialization ();
4496
4497 if (! processing_template_decl)
4498 return;
4499
4500 /* This matches the pushlevel in begin_template_parm_list. */
4501 finish_scope ();
4502
4503 --processing_template_decl;
4504 current_template_parms = TREE_CHAIN (current_template_parms);
4505 }
4506
4507 /* Takes a TREE_LIST representing a template parameter and convert it
4508 into an argument suitable to be passed to the type substitution
4509 functions. Note that If the TREE_LIST contains an error_mark
4510 node, the returned argument is error_mark_node. */
4511
4512 tree
4513 template_parm_to_arg (tree t)
4514 {
4515
4516 if (t == NULL_TREE
4517 || TREE_CODE (t) != TREE_LIST)
4518 return t;
4519
4520 if (error_operand_p (TREE_VALUE (t)))
4521 return error_mark_node;
4522
4523 t = TREE_VALUE (t);
4524
4525 if (TREE_CODE (t) == TYPE_DECL
4526 || TREE_CODE (t) == TEMPLATE_DECL)
4527 {
4528 t = TREE_TYPE (t);
4529
4530 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4531 {
4532 /* Turn this argument into a TYPE_ARGUMENT_PACK
4533 with a single element, which expands T. */
4534 tree vec = make_tree_vec (1);
4535 if (CHECKING_P)
4536 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4537
4538 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4539
4540 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4541 SET_ARGUMENT_PACK_ARGS (t, vec);
4542 }
4543 }
4544 else
4545 {
4546 t = DECL_INITIAL (t);
4547
4548 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4549 {
4550 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4551 with a single element, which expands T. */
4552 tree vec = make_tree_vec (1);
4553 if (CHECKING_P)
4554 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4555
4556 t = convert_from_reference (t);
4557 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4558
4559 t = make_node (NONTYPE_ARGUMENT_PACK);
4560 SET_ARGUMENT_PACK_ARGS (t, vec);
4561 }
4562 else
4563 t = convert_from_reference (t);
4564 }
4565 return t;
4566 }
4567
4568 /* Given a single level of template parameters (a TREE_VEC), return it
4569 as a set of template arguments. */
4570
4571 static tree
4572 template_parms_level_to_args (tree parms)
4573 {
4574 tree a = copy_node (parms);
4575 TREE_TYPE (a) = NULL_TREE;
4576 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4577 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4578
4579 if (CHECKING_P)
4580 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4581
4582 return a;
4583 }
4584
4585 /* Given a set of template parameters, return them as a set of template
4586 arguments. The template parameters are represented as a TREE_VEC, in
4587 the form documented in cp-tree.h for template arguments. */
4588
4589 static tree
4590 template_parms_to_args (tree parms)
4591 {
4592 tree header;
4593 tree args = NULL_TREE;
4594 int length = TMPL_PARMS_DEPTH (parms);
4595 int l = length;
4596
4597 /* If there is only one level of template parameters, we do not
4598 create a TREE_VEC of TREE_VECs. Instead, we return a single
4599 TREE_VEC containing the arguments. */
4600 if (length > 1)
4601 args = make_tree_vec (length);
4602
4603 for (header = parms; header; header = TREE_CHAIN (header))
4604 {
4605 tree a = template_parms_level_to_args (TREE_VALUE (header));
4606
4607 if (length > 1)
4608 TREE_VEC_ELT (args, --l) = a;
4609 else
4610 args = a;
4611 }
4612
4613 return args;
4614 }
4615
4616 /* Within the declaration of a template, return the currently active
4617 template parameters as an argument TREE_VEC. */
4618
4619 static tree
4620 current_template_args (void)
4621 {
4622 return template_parms_to_args (current_template_parms);
4623 }
4624
4625 /* Update the declared TYPE by doing any lookups which were thought to be
4626 dependent, but are not now that we know the SCOPE of the declarator. */
4627
4628 tree
4629 maybe_update_decl_type (tree orig_type, tree scope)
4630 {
4631 tree type = orig_type;
4632
4633 if (type == NULL_TREE)
4634 return type;
4635
4636 if (TREE_CODE (orig_type) == TYPE_DECL)
4637 type = TREE_TYPE (type);
4638
4639 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4640 && dependent_type_p (type)
4641 /* Don't bother building up the args in this case. */
4642 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4643 {
4644 /* tsubst in the args corresponding to the template parameters,
4645 including auto if present. Most things will be unchanged, but
4646 make_typename_type and tsubst_qualified_id will resolve
4647 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4648 tree args = current_template_args ();
4649 tree auto_node = type_uses_auto (type);
4650 tree pushed;
4651 if (auto_node)
4652 {
4653 tree auto_vec = make_tree_vec (1);
4654 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4655 args = add_to_template_args (args, auto_vec);
4656 }
4657 pushed = push_scope (scope);
4658 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4659 if (pushed)
4660 pop_scope (scope);
4661 }
4662
4663 if (type == error_mark_node)
4664 return orig_type;
4665
4666 if (TREE_CODE (orig_type) == TYPE_DECL)
4667 {
4668 if (same_type_p (type, TREE_TYPE (orig_type)))
4669 type = orig_type;
4670 else
4671 type = TYPE_NAME (type);
4672 }
4673 return type;
4674 }
4675
4676 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4677 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4678 the new template is a member template. */
4679
4680 static tree
4681 build_template_decl (tree decl, tree parms, bool member_template_p)
4682 {
4683 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4684 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4685 DECL_TEMPLATE_PARMS (tmpl) = parms;
4686 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4687 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4688 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4689
4690 return tmpl;
4691 }
4692
4693 struct template_parm_data
4694 {
4695 /* The level of the template parameters we are currently
4696 processing. */
4697 int level;
4698
4699 /* The index of the specialization argument we are currently
4700 processing. */
4701 int current_arg;
4702
4703 /* An array whose size is the number of template parameters. The
4704 elements are nonzero if the parameter has been used in any one
4705 of the arguments processed so far. */
4706 int* parms;
4707
4708 /* An array whose size is the number of template arguments. The
4709 elements are nonzero if the argument makes use of template
4710 parameters of this level. */
4711 int* arg_uses_template_parms;
4712 };
4713
4714 /* Subroutine of push_template_decl used to see if each template
4715 parameter in a partial specialization is used in the explicit
4716 argument list. If T is of the LEVEL given in DATA (which is
4717 treated as a template_parm_data*), then DATA->PARMS is marked
4718 appropriately. */
4719
4720 static int
4721 mark_template_parm (tree t, void* data)
4722 {
4723 int level;
4724 int idx;
4725 struct template_parm_data* tpd = (struct template_parm_data*) data;
4726
4727 template_parm_level_and_index (t, &level, &idx);
4728
4729 if (level == tpd->level)
4730 {
4731 tpd->parms[idx] = 1;
4732 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4733 }
4734
4735 /* In C++17 the type of a non-type argument is a deduced context. */
4736 if (cxx_dialect >= cxx17
4737 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4738 for_each_template_parm (TREE_TYPE (t),
4739 &mark_template_parm,
4740 data,
4741 NULL,
4742 /*include_nondeduced_p=*/false);
4743
4744 /* Return zero so that for_each_template_parm will continue the
4745 traversal of the tree; we want to mark *every* template parm. */
4746 return 0;
4747 }
4748
4749 /* Process the partial specialization DECL. */
4750
4751 static tree
4752 process_partial_specialization (tree decl)
4753 {
4754 tree type = TREE_TYPE (decl);
4755 tree tinfo = get_template_info (decl);
4756 tree maintmpl = TI_TEMPLATE (tinfo);
4757 tree specargs = TI_ARGS (tinfo);
4758 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4759 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4760 tree inner_parms;
4761 tree inst;
4762 int nargs = TREE_VEC_LENGTH (inner_args);
4763 int ntparms;
4764 int i;
4765 bool did_error_intro = false;
4766 struct template_parm_data tpd;
4767 struct template_parm_data tpd2;
4768
4769 gcc_assert (current_template_parms);
4770
4771 /* A concept cannot be specialized. */
4772 if (flag_concepts && variable_concept_p (maintmpl))
4773 {
4774 error ("specialization of variable concept %q#D", maintmpl);
4775 return error_mark_node;
4776 }
4777
4778 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4779 ntparms = TREE_VEC_LENGTH (inner_parms);
4780
4781 /* We check that each of the template parameters given in the
4782 partial specialization is used in the argument list to the
4783 specialization. For example:
4784
4785 template <class T> struct S;
4786 template <class T> struct S<T*>;
4787
4788 The second declaration is OK because `T*' uses the template
4789 parameter T, whereas
4790
4791 template <class T> struct S<int>;
4792
4793 is no good. Even trickier is:
4794
4795 template <class T>
4796 struct S1
4797 {
4798 template <class U>
4799 struct S2;
4800 template <class U>
4801 struct S2<T>;
4802 };
4803
4804 The S2<T> declaration is actually invalid; it is a
4805 full-specialization. Of course,
4806
4807 template <class U>
4808 struct S2<T (*)(U)>;
4809
4810 or some such would have been OK. */
4811 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4812 tpd.parms = XALLOCAVEC (int, ntparms);
4813 memset (tpd.parms, 0, sizeof (int) * ntparms);
4814
4815 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4816 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4817 for (i = 0; i < nargs; ++i)
4818 {
4819 tpd.current_arg = i;
4820 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4821 &mark_template_parm,
4822 &tpd,
4823 NULL,
4824 /*include_nondeduced_p=*/false);
4825 }
4826 for (i = 0; i < ntparms; ++i)
4827 if (tpd.parms[i] == 0)
4828 {
4829 /* One of the template parms was not used in a deduced context in the
4830 specialization. */
4831 if (!did_error_intro)
4832 {
4833 error ("template parameters not deducible in "
4834 "partial specialization:");
4835 did_error_intro = true;
4836 }
4837
4838 inform (input_location, " %qD",
4839 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4840 }
4841
4842 if (did_error_intro)
4843 return error_mark_node;
4844
4845 /* [temp.class.spec]
4846
4847 The argument list of the specialization shall not be identical to
4848 the implicit argument list of the primary template. */
4849 tree main_args
4850 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4851 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4852 && (!flag_concepts
4853 || !strictly_subsumes (current_template_constraints (),
4854 get_constraints (maintmpl))))
4855 {
4856 if (!flag_concepts)
4857 error ("partial specialization %q+D does not specialize "
4858 "any template arguments; to define the primary template, "
4859 "remove the template argument list", decl);
4860 else
4861 error ("partial specialization %q+D does not specialize any "
4862 "template arguments and is not more constrained than "
4863 "the primary template; to define the primary template, "
4864 "remove the template argument list", decl);
4865 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4866 }
4867
4868 /* A partial specialization that replaces multiple parameters of the
4869 primary template with a pack expansion is less specialized for those
4870 parameters. */
4871 if (nargs < DECL_NTPARMS (maintmpl))
4872 {
4873 error ("partial specialization is not more specialized than the "
4874 "primary template because it replaces multiple parameters "
4875 "with a pack expansion");
4876 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4877 /* Avoid crash in process_partial_specialization. */
4878 return decl;
4879 }
4880
4881 /* If we aren't in a dependent class, we can actually try deduction. */
4882 else if (tpd.level == 1
4883 /* FIXME we should be able to handle a partial specialization of a
4884 partial instantiation, but currently we can't (c++/41727). */
4885 && TMPL_ARGS_DEPTH (specargs) == 1
4886 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4887 {
4888 auto_diagnostic_group d;
4889 if (permerror (input_location, "partial specialization %qD is not "
4890 "more specialized than", decl))
4891 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4892 maintmpl);
4893 }
4894
4895 /* [temp.class.spec]
4896
4897 A partially specialized non-type argument expression shall not
4898 involve template parameters of the partial specialization except
4899 when the argument expression is a simple identifier.
4900
4901 The type of a template parameter corresponding to a specialized
4902 non-type argument shall not be dependent on a parameter of the
4903 specialization.
4904
4905 Also, we verify that pack expansions only occur at the
4906 end of the argument list. */
4907 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4908 tpd2.parms = 0;
4909 for (i = 0; i < nargs; ++i)
4910 {
4911 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4912 tree arg = TREE_VEC_ELT (inner_args, i);
4913 tree packed_args = NULL_TREE;
4914 int j, len = 1;
4915
4916 if (ARGUMENT_PACK_P (arg))
4917 {
4918 /* Extract the arguments from the argument pack. We'll be
4919 iterating over these in the following loop. */
4920 packed_args = ARGUMENT_PACK_ARGS (arg);
4921 len = TREE_VEC_LENGTH (packed_args);
4922 }
4923
4924 for (j = 0; j < len; j++)
4925 {
4926 if (packed_args)
4927 /* Get the Jth argument in the parameter pack. */
4928 arg = TREE_VEC_ELT (packed_args, j);
4929
4930 if (PACK_EXPANSION_P (arg))
4931 {
4932 /* Pack expansions must come at the end of the
4933 argument list. */
4934 if ((packed_args && j < len - 1)
4935 || (!packed_args && i < nargs - 1))
4936 {
4937 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4938 error ("parameter pack argument %qE must be at the "
4939 "end of the template argument list", arg);
4940 else
4941 error ("parameter pack argument %qT must be at the "
4942 "end of the template argument list", arg);
4943 }
4944 }
4945
4946 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4947 /* We only care about the pattern. */
4948 arg = PACK_EXPANSION_PATTERN (arg);
4949
4950 if (/* These first two lines are the `non-type' bit. */
4951 !TYPE_P (arg)
4952 && TREE_CODE (arg) != TEMPLATE_DECL
4953 /* This next two lines are the `argument expression is not just a
4954 simple identifier' condition and also the `specialized
4955 non-type argument' bit. */
4956 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4957 && !((REFERENCE_REF_P (arg)
4958 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
4959 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4960 {
4961 if ((!packed_args && tpd.arg_uses_template_parms[i])
4962 || (packed_args && uses_template_parms (arg)))
4963 error ("template argument %qE involves template parameter(s)",
4964 arg);
4965 else
4966 {
4967 /* Look at the corresponding template parameter,
4968 marking which template parameters its type depends
4969 upon. */
4970 tree type = TREE_TYPE (parm);
4971
4972 if (!tpd2.parms)
4973 {
4974 /* We haven't yet initialized TPD2. Do so now. */
4975 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4976 /* The number of parameters here is the number in the
4977 main template, which, as checked in the assertion
4978 above, is NARGS. */
4979 tpd2.parms = XALLOCAVEC (int, nargs);
4980 tpd2.level =
4981 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4982 }
4983
4984 /* Mark the template parameters. But this time, we're
4985 looking for the template parameters of the main
4986 template, not in the specialization. */
4987 tpd2.current_arg = i;
4988 tpd2.arg_uses_template_parms[i] = 0;
4989 memset (tpd2.parms, 0, sizeof (int) * nargs);
4990 for_each_template_parm (type,
4991 &mark_template_parm,
4992 &tpd2,
4993 NULL,
4994 /*include_nondeduced_p=*/false);
4995
4996 if (tpd2.arg_uses_template_parms [i])
4997 {
4998 /* The type depended on some template parameters.
4999 If they are fully specialized in the
5000 specialization, that's OK. */
5001 int j;
5002 int count = 0;
5003 for (j = 0; j < nargs; ++j)
5004 if (tpd2.parms[j] != 0
5005 && tpd.arg_uses_template_parms [j])
5006 ++count;
5007 if (count != 0)
5008 error_n (input_location, count,
5009 "type %qT of template argument %qE depends "
5010 "on a template parameter",
5011 "type %qT of template argument %qE depends "
5012 "on template parameters",
5013 type,
5014 arg);
5015 }
5016 }
5017 }
5018 }
5019 }
5020
5021 /* We should only get here once. */
5022 if (TREE_CODE (decl) == TYPE_DECL)
5023 gcc_assert (!COMPLETE_TYPE_P (type));
5024
5025 // Build the template decl.
5026 tree tmpl = build_template_decl (decl, current_template_parms,
5027 DECL_MEMBER_TEMPLATE_P (maintmpl));
5028 TREE_TYPE (tmpl) = type;
5029 DECL_TEMPLATE_RESULT (tmpl) = decl;
5030 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5031 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5032 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5033
5034 /* Give template template parms a DECL_CONTEXT of the template
5035 for which they are a parameter. */
5036 for (i = 0; i < ntparms; ++i)
5037 {
5038 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5039 if (TREE_CODE (parm) == TEMPLATE_DECL)
5040 DECL_CONTEXT (parm) = tmpl;
5041 }
5042
5043 if (VAR_P (decl))
5044 /* We didn't register this in check_explicit_specialization so we could
5045 wait until the constraints were set. */
5046 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5047 else
5048 associate_classtype_constraints (type);
5049
5050 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5051 = tree_cons (specargs, tmpl,
5052 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5053 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5054
5055 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5056 inst = TREE_CHAIN (inst))
5057 {
5058 tree instance = TREE_VALUE (inst);
5059 if (TYPE_P (instance)
5060 ? (COMPLETE_TYPE_P (instance)
5061 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5062 : DECL_TEMPLATE_INSTANTIATION (instance))
5063 {
5064 tree spec = most_specialized_partial_spec (instance, tf_none);
5065 tree inst_decl = (DECL_P (instance)
5066 ? instance : TYPE_NAME (instance));
5067 if (!spec)
5068 /* OK */;
5069 else if (spec == error_mark_node)
5070 permerror (input_location,
5071 "declaration of %qD ambiguates earlier template "
5072 "instantiation for %qD", decl, inst_decl);
5073 else if (TREE_VALUE (spec) == tmpl)
5074 permerror (input_location,
5075 "partial specialization of %qD after instantiation "
5076 "of %qD", decl, inst_decl);
5077 }
5078 }
5079
5080 return decl;
5081 }
5082
5083 /* PARM is a template parameter of some form; return the corresponding
5084 TEMPLATE_PARM_INDEX. */
5085
5086 static tree
5087 get_template_parm_index (tree parm)
5088 {
5089 if (TREE_CODE (parm) == PARM_DECL
5090 || TREE_CODE (parm) == CONST_DECL)
5091 parm = DECL_INITIAL (parm);
5092 else if (TREE_CODE (parm) == TYPE_DECL
5093 || TREE_CODE (parm) == TEMPLATE_DECL)
5094 parm = TREE_TYPE (parm);
5095 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5096 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5097 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5098 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5099 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5100 return parm;
5101 }
5102
5103 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5104 parameter packs used by the template parameter PARM. */
5105
5106 static void
5107 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5108 {
5109 /* A type parm can't refer to another parm. */
5110 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5111 return;
5112 else if (TREE_CODE (parm) == PARM_DECL)
5113 {
5114 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5115 ppd, ppd->visited);
5116 return;
5117 }
5118
5119 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5120
5121 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5122 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5123 {
5124 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5125 if (template_parameter_pack_p (p))
5126 /* Any packs in the type are expanded by this parameter. */;
5127 else
5128 fixed_parameter_pack_p_1 (p, ppd);
5129 }
5130 }
5131
5132 /* PARM is a template parameter pack. Return any parameter packs used in
5133 its type or the type of any of its template parameters. If there are
5134 any such packs, it will be instantiated into a fixed template parameter
5135 list by partial instantiation rather than be fully deduced. */
5136
5137 tree
5138 fixed_parameter_pack_p (tree parm)
5139 {
5140 /* This can only be true in a member template. */
5141 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5142 return NULL_TREE;
5143 /* This can only be true for a parameter pack. */
5144 if (!template_parameter_pack_p (parm))
5145 return NULL_TREE;
5146 /* A type parm can't refer to another parm. */
5147 if (TREE_CODE (parm) == TYPE_DECL)
5148 return NULL_TREE;
5149
5150 tree parameter_packs = NULL_TREE;
5151 struct find_parameter_pack_data ppd;
5152 ppd.parameter_packs = &parameter_packs;
5153 ppd.visited = new hash_set<tree>;
5154 ppd.type_pack_expansion_p = false;
5155
5156 fixed_parameter_pack_p_1 (parm, &ppd);
5157
5158 delete ppd.visited;
5159 return parameter_packs;
5160 }
5161
5162 /* Check that a template declaration's use of default arguments and
5163 parameter packs is not invalid. Here, PARMS are the template
5164 parameters. IS_PRIMARY is true if DECL is the thing declared by
5165 a primary template. IS_PARTIAL is true if DECL is a partial
5166 specialization.
5167
5168 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5169 function template declaration or a friend class template
5170 declaration. In the function case, 1 indicates a declaration, 2
5171 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5172 emitted for extraneous default arguments.
5173
5174 Returns TRUE if there were no errors found, FALSE otherwise. */
5175
5176 bool
5177 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5178 bool is_partial, int is_friend_decl)
5179 {
5180 const char *msg;
5181 int last_level_to_check;
5182 tree parm_level;
5183 bool no_errors = true;
5184
5185 /* [temp.param]
5186
5187 A default template-argument shall not be specified in a
5188 function template declaration or a function template definition, nor
5189 in the template-parameter-list of the definition of a member of a
5190 class template. */
5191
5192 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5193 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5194 /* You can't have a function template declaration in a local
5195 scope, nor you can you define a member of a class template in a
5196 local scope. */
5197 return true;
5198
5199 if ((TREE_CODE (decl) == TYPE_DECL
5200 && TREE_TYPE (decl)
5201 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5202 || (TREE_CODE (decl) == FUNCTION_DECL
5203 && LAMBDA_FUNCTION_P (decl)))
5204 /* A lambda doesn't have an explicit declaration; don't complain
5205 about the parms of the enclosing class. */
5206 return true;
5207
5208 if (current_class_type
5209 && !TYPE_BEING_DEFINED (current_class_type)
5210 && DECL_LANG_SPECIFIC (decl)
5211 && DECL_DECLARES_FUNCTION_P (decl)
5212 /* If this is either a friend defined in the scope of the class
5213 or a member function. */
5214 && (DECL_FUNCTION_MEMBER_P (decl)
5215 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5216 : DECL_FRIEND_CONTEXT (decl)
5217 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5218 : false)
5219 /* And, if it was a member function, it really was defined in
5220 the scope of the class. */
5221 && (!DECL_FUNCTION_MEMBER_P (decl)
5222 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5223 /* We already checked these parameters when the template was
5224 declared, so there's no need to do it again now. This function
5225 was defined in class scope, but we're processing its body now
5226 that the class is complete. */
5227 return true;
5228
5229 /* Core issue 226 (C++0x only): the following only applies to class
5230 templates. */
5231 if (is_primary
5232 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5233 {
5234 /* [temp.param]
5235
5236 If a template-parameter has a default template-argument, all
5237 subsequent template-parameters shall have a default
5238 template-argument supplied. */
5239 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5240 {
5241 tree inner_parms = TREE_VALUE (parm_level);
5242 int ntparms = TREE_VEC_LENGTH (inner_parms);
5243 int seen_def_arg_p = 0;
5244 int i;
5245
5246 for (i = 0; i < ntparms; ++i)
5247 {
5248 tree parm = TREE_VEC_ELT (inner_parms, i);
5249
5250 if (parm == error_mark_node)
5251 continue;
5252
5253 if (TREE_PURPOSE (parm))
5254 seen_def_arg_p = 1;
5255 else if (seen_def_arg_p
5256 && !template_parameter_pack_p (TREE_VALUE (parm)))
5257 {
5258 error ("no default argument for %qD", TREE_VALUE (parm));
5259 /* For better subsequent error-recovery, we indicate that
5260 there should have been a default argument. */
5261 TREE_PURPOSE (parm) = error_mark_node;
5262 no_errors = false;
5263 }
5264 else if (!is_partial
5265 && !is_friend_decl
5266 /* Don't complain about an enclosing partial
5267 specialization. */
5268 && parm_level == parms
5269 && TREE_CODE (decl) == TYPE_DECL
5270 && i < ntparms - 1
5271 && template_parameter_pack_p (TREE_VALUE (parm))
5272 /* A fixed parameter pack will be partially
5273 instantiated into a fixed length list. */
5274 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5275 {
5276 /* A primary class template can only have one
5277 parameter pack, at the end of the template
5278 parameter list. */
5279
5280 error ("parameter pack %q+D must be at the end of the"
5281 " template parameter list", TREE_VALUE (parm));
5282
5283 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5284 = error_mark_node;
5285 no_errors = false;
5286 }
5287 }
5288 }
5289 }
5290
5291 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5292 || is_partial
5293 || !is_primary
5294 || is_friend_decl)
5295 /* For an ordinary class template, default template arguments are
5296 allowed at the innermost level, e.g.:
5297 template <class T = int>
5298 struct S {};
5299 but, in a partial specialization, they're not allowed even
5300 there, as we have in [temp.class.spec]:
5301
5302 The template parameter list of a specialization shall not
5303 contain default template argument values.
5304
5305 So, for a partial specialization, or for a function template
5306 (in C++98/C++03), we look at all of them. */
5307 ;
5308 else
5309 /* But, for a primary class template that is not a partial
5310 specialization we look at all template parameters except the
5311 innermost ones. */
5312 parms = TREE_CHAIN (parms);
5313
5314 /* Figure out what error message to issue. */
5315 if (is_friend_decl == 2)
5316 msg = G_("default template arguments may not be used in function template "
5317 "friend re-declaration");
5318 else if (is_friend_decl)
5319 msg = G_("default template arguments may not be used in template "
5320 "friend declarations");
5321 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5322 msg = G_("default template arguments may not be used in function templates "
5323 "without %<-std=c++11%> or %<-std=gnu++11%>");
5324 else if (is_partial)
5325 msg = G_("default template arguments may not be used in "
5326 "partial specializations");
5327 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5328 msg = G_("default argument for template parameter for class enclosing %qD");
5329 else
5330 /* Per [temp.param]/9, "A default template-argument shall not be
5331 specified in the template-parameter-lists of the definition of
5332 a member of a class template that appears outside of the member's
5333 class.", thus if we aren't handling a member of a class template
5334 there is no need to examine the parameters. */
5335 return true;
5336
5337 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5338 /* If we're inside a class definition, there's no need to
5339 examine the parameters to the class itself. On the one
5340 hand, they will be checked when the class is defined, and,
5341 on the other, default arguments are valid in things like:
5342 template <class T = double>
5343 struct S { template <class U> void f(U); };
5344 Here the default argument for `S' has no bearing on the
5345 declaration of `f'. */
5346 last_level_to_check = template_class_depth (current_class_type) + 1;
5347 else
5348 /* Check everything. */
5349 last_level_to_check = 0;
5350
5351 for (parm_level = parms;
5352 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5353 parm_level = TREE_CHAIN (parm_level))
5354 {
5355 tree inner_parms = TREE_VALUE (parm_level);
5356 int i;
5357 int ntparms;
5358
5359 ntparms = TREE_VEC_LENGTH (inner_parms);
5360 for (i = 0; i < ntparms; ++i)
5361 {
5362 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5363 continue;
5364
5365 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5366 {
5367 if (msg)
5368 {
5369 no_errors = false;
5370 if (is_friend_decl == 2)
5371 return no_errors;
5372
5373 error (msg, decl);
5374 msg = 0;
5375 }
5376
5377 /* Clear out the default argument so that we are not
5378 confused later. */
5379 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5380 }
5381 }
5382
5383 /* At this point, if we're still interested in issuing messages,
5384 they must apply to classes surrounding the object declared. */
5385 if (msg)
5386 msg = G_("default argument for template parameter for class "
5387 "enclosing %qD");
5388 }
5389
5390 return no_errors;
5391 }
5392
5393 /* Worker for push_template_decl_real, called via
5394 for_each_template_parm. DATA is really an int, indicating the
5395 level of the parameters we are interested in. If T is a template
5396 parameter of that level, return nonzero. */
5397
5398 static int
5399 template_parm_this_level_p (tree t, void* data)
5400 {
5401 int this_level = *(int *)data;
5402 int level;
5403
5404 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5405 level = TEMPLATE_PARM_LEVEL (t);
5406 else
5407 level = TEMPLATE_TYPE_LEVEL (t);
5408 return level == this_level;
5409 }
5410
5411 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5412 DATA is really an int, indicating the innermost outer level of parameters.
5413 If T is a template parameter of that level or further out, return
5414 nonzero. */
5415
5416 static int
5417 template_parm_outer_level (tree t, void *data)
5418 {
5419 int this_level = *(int *)data;
5420 int level;
5421
5422 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5423 level = TEMPLATE_PARM_LEVEL (t);
5424 else
5425 level = TEMPLATE_TYPE_LEVEL (t);
5426 return level <= this_level;
5427 }
5428
5429 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5430 parameters given by current_template_args, or reuses a
5431 previously existing one, if appropriate. Returns the DECL, or an
5432 equivalent one, if it is replaced via a call to duplicate_decls.
5433
5434 If IS_FRIEND is true, DECL is a friend declaration. */
5435
5436 tree
5437 push_template_decl_real (tree decl, bool is_friend)
5438 {
5439 tree tmpl;
5440 tree args;
5441 tree info;
5442 tree ctx;
5443 bool is_primary;
5444 bool is_partial;
5445 int new_template_p = 0;
5446 /* True if the template is a member template, in the sense of
5447 [temp.mem]. */
5448 bool member_template_p = false;
5449
5450 if (decl == error_mark_node || !current_template_parms)
5451 return error_mark_node;
5452
5453 /* See if this is a partial specialization. */
5454 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5455 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5456 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5457 || (VAR_P (decl)
5458 && DECL_LANG_SPECIFIC (decl)
5459 && DECL_TEMPLATE_SPECIALIZATION (decl)
5460 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5461
5462 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5463 is_friend = true;
5464
5465 if (is_friend)
5466 /* For a friend, we want the context of the friend, not
5467 the type of which it is a friend. */
5468 ctx = CP_DECL_CONTEXT (decl);
5469 else if (CP_DECL_CONTEXT (decl)
5470 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5471 /* In the case of a virtual function, we want the class in which
5472 it is defined. */
5473 ctx = CP_DECL_CONTEXT (decl);
5474 else
5475 /* Otherwise, if we're currently defining some class, the DECL
5476 is assumed to be a member of the class. */
5477 ctx = current_scope ();
5478
5479 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5480 ctx = NULL_TREE;
5481
5482 if (!DECL_CONTEXT (decl))
5483 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5484
5485 /* See if this is a primary template. */
5486 if (is_friend && ctx
5487 && uses_template_parms_level (ctx, processing_template_decl))
5488 /* A friend template that specifies a class context, i.e.
5489 template <typename T> friend void A<T>::f();
5490 is not primary. */
5491 is_primary = false;
5492 else if (TREE_CODE (decl) == TYPE_DECL
5493 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5494 is_primary = false;
5495 else
5496 is_primary = template_parm_scope_p ();
5497
5498 if (is_primary)
5499 {
5500 warning (OPT_Wtemplates, "template %qD declared", decl);
5501
5502 if (DECL_CLASS_SCOPE_P (decl))
5503 member_template_p = true;
5504 if (TREE_CODE (decl) == TYPE_DECL
5505 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5506 {
5507 error ("template class without a name");
5508 return error_mark_node;
5509 }
5510 else if (TREE_CODE (decl) == FUNCTION_DECL)
5511 {
5512 if (member_template_p)
5513 {
5514 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5515 error ("member template %qD may not have virt-specifiers", decl);
5516 }
5517 if (DECL_DESTRUCTOR_P (decl))
5518 {
5519 /* [temp.mem]
5520
5521 A destructor shall not be a member template. */
5522 error ("destructor %qD declared as member template", decl);
5523 return error_mark_node;
5524 }
5525 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5526 && (!prototype_p (TREE_TYPE (decl))
5527 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5528 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5529 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5530 == void_list_node)))
5531 {
5532 /* [basic.stc.dynamic.allocation]
5533
5534 An allocation function can be a function
5535 template. ... Template allocation functions shall
5536 have two or more parameters. */
5537 error ("invalid template declaration of %qD", decl);
5538 return error_mark_node;
5539 }
5540 }
5541 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5542 && CLASS_TYPE_P (TREE_TYPE (decl)))
5543 {
5544 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5545 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5546 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5547 {
5548 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5549 if (TREE_CODE (t) == TYPE_DECL)
5550 t = TREE_TYPE (t);
5551 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5552 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5553 }
5554 }
5555 else if (TREE_CODE (decl) == TYPE_DECL
5556 && TYPE_DECL_ALIAS_P (decl))
5557 /* alias-declaration */
5558 gcc_assert (!DECL_ARTIFICIAL (decl));
5559 else if (VAR_P (decl))
5560 /* C++14 variable template. */;
5561 else
5562 {
5563 error ("template declaration of %q#D", decl);
5564 return error_mark_node;
5565 }
5566 }
5567
5568 /* Check to see that the rules regarding the use of default
5569 arguments are not being violated. We check args for a friend
5570 functions when we know whether it's a definition, introducing
5571 declaration or re-declaration. */
5572 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5573 check_default_tmpl_args (decl, current_template_parms,
5574 is_primary, is_partial, is_friend);
5575
5576 /* Ensure that there are no parameter packs in the type of this
5577 declaration that have not been expanded. */
5578 if (TREE_CODE (decl) == FUNCTION_DECL)
5579 {
5580 /* Check each of the arguments individually to see if there are
5581 any bare parameter packs. */
5582 tree type = TREE_TYPE (decl);
5583 tree arg = DECL_ARGUMENTS (decl);
5584 tree argtype = TYPE_ARG_TYPES (type);
5585
5586 while (arg && argtype)
5587 {
5588 if (!DECL_PACK_P (arg)
5589 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5590 {
5591 /* This is a PARM_DECL that contains unexpanded parameter
5592 packs. We have already complained about this in the
5593 check_for_bare_parameter_packs call, so just replace
5594 these types with ERROR_MARK_NODE. */
5595 TREE_TYPE (arg) = error_mark_node;
5596 TREE_VALUE (argtype) = error_mark_node;
5597 }
5598
5599 arg = DECL_CHAIN (arg);
5600 argtype = TREE_CHAIN (argtype);
5601 }
5602
5603 /* Check for bare parameter packs in the return type and the
5604 exception specifiers. */
5605 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5606 /* Errors were already issued, set return type to int
5607 as the frontend doesn't expect error_mark_node as
5608 the return type. */
5609 TREE_TYPE (type) = integer_type_node;
5610 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5611 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5612 }
5613 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5614 && TYPE_DECL_ALIAS_P (decl))
5615 ? DECL_ORIGINAL_TYPE (decl)
5616 : TREE_TYPE (decl)))
5617 {
5618 TREE_TYPE (decl) = error_mark_node;
5619 return error_mark_node;
5620 }
5621
5622 if (is_partial)
5623 return process_partial_specialization (decl);
5624
5625 args = current_template_args ();
5626
5627 if (!ctx
5628 || TREE_CODE (ctx) == FUNCTION_DECL
5629 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5630 || (TREE_CODE (decl) == TYPE_DECL
5631 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5632 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5633 {
5634 if (DECL_LANG_SPECIFIC (decl)
5635 && DECL_TEMPLATE_INFO (decl)
5636 && DECL_TI_TEMPLATE (decl))
5637 tmpl = DECL_TI_TEMPLATE (decl);
5638 /* If DECL is a TYPE_DECL for a class-template, then there won't
5639 be DECL_LANG_SPECIFIC. The information equivalent to
5640 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5641 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5642 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5643 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5644 {
5645 /* Since a template declaration already existed for this
5646 class-type, we must be redeclaring it here. Make sure
5647 that the redeclaration is valid. */
5648 redeclare_class_template (TREE_TYPE (decl),
5649 current_template_parms,
5650 current_template_constraints ());
5651 /* We don't need to create a new TEMPLATE_DECL; just use the
5652 one we already had. */
5653 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5654 }
5655 else
5656 {
5657 tmpl = build_template_decl (decl, current_template_parms,
5658 member_template_p);
5659 new_template_p = 1;
5660
5661 if (DECL_LANG_SPECIFIC (decl)
5662 && DECL_TEMPLATE_SPECIALIZATION (decl))
5663 {
5664 /* A specialization of a member template of a template
5665 class. */
5666 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5667 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5668 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5669 }
5670 }
5671 }
5672 else
5673 {
5674 tree a, t, current, parms;
5675 int i;
5676 tree tinfo = get_template_info (decl);
5677
5678 if (!tinfo)
5679 {
5680 error ("template definition of non-template %q#D", decl);
5681 return error_mark_node;
5682 }
5683
5684 tmpl = TI_TEMPLATE (tinfo);
5685
5686 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5687 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5688 && DECL_TEMPLATE_SPECIALIZATION (decl)
5689 && DECL_MEMBER_TEMPLATE_P (tmpl))
5690 {
5691 tree new_tmpl;
5692
5693 /* The declaration is a specialization of a member
5694 template, declared outside the class. Therefore, the
5695 innermost template arguments will be NULL, so we
5696 replace them with the arguments determined by the
5697 earlier call to check_explicit_specialization. */
5698 args = DECL_TI_ARGS (decl);
5699
5700 new_tmpl
5701 = build_template_decl (decl, current_template_parms,
5702 member_template_p);
5703 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5704 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5705 DECL_TI_TEMPLATE (decl) = new_tmpl;
5706 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5707 DECL_TEMPLATE_INFO (new_tmpl)
5708 = build_template_info (tmpl, args);
5709
5710 register_specialization (new_tmpl,
5711 most_general_template (tmpl),
5712 args,
5713 is_friend, 0);
5714 return decl;
5715 }
5716
5717 /* Make sure the template headers we got make sense. */
5718
5719 parms = DECL_TEMPLATE_PARMS (tmpl);
5720 i = TMPL_PARMS_DEPTH (parms);
5721 if (TMPL_ARGS_DEPTH (args) != i)
5722 {
5723 error ("expected %d levels of template parms for %q#D, got %d",
5724 i, decl, TMPL_ARGS_DEPTH (args));
5725 DECL_INTERFACE_KNOWN (decl) = 1;
5726 return error_mark_node;
5727 }
5728 else
5729 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5730 {
5731 a = TMPL_ARGS_LEVEL (args, i);
5732 t = INNERMOST_TEMPLATE_PARMS (parms);
5733
5734 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5735 {
5736 if (current == decl)
5737 error ("got %d template parameters for %q#D",
5738 TREE_VEC_LENGTH (a), decl);
5739 else
5740 error ("got %d template parameters for %q#T",
5741 TREE_VEC_LENGTH (a), current);
5742 error (" but %d required", TREE_VEC_LENGTH (t));
5743 /* Avoid crash in import_export_decl. */
5744 DECL_INTERFACE_KNOWN (decl) = 1;
5745 return error_mark_node;
5746 }
5747
5748 if (current == decl)
5749 current = ctx;
5750 else if (current == NULL_TREE)
5751 /* Can happen in erroneous input. */
5752 break;
5753 else
5754 current = get_containing_scope (current);
5755 }
5756
5757 /* Check that the parms are used in the appropriate qualifying scopes
5758 in the declarator. */
5759 if (!comp_template_args
5760 (TI_ARGS (tinfo),
5761 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5762 {
5763 error ("template arguments to %qD do not match original "
5764 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5765 if (!uses_template_parms (TI_ARGS (tinfo)))
5766 inform (input_location, "use %<template<>%> for"
5767 " an explicit specialization");
5768 /* Avoid crash in import_export_decl. */
5769 DECL_INTERFACE_KNOWN (decl) = 1;
5770 return error_mark_node;
5771 }
5772 }
5773
5774 DECL_TEMPLATE_RESULT (tmpl) = decl;
5775 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5776
5777 /* Push template declarations for global functions and types. Note
5778 that we do not try to push a global template friend declared in a
5779 template class; such a thing may well depend on the template
5780 parameters of the class. */
5781 if (new_template_p && !ctx
5782 && !(is_friend && template_class_depth (current_class_type) > 0))
5783 {
5784 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5785 if (tmpl == error_mark_node)
5786 return error_mark_node;
5787
5788 /* Hide template friend classes that haven't been declared yet. */
5789 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5790 {
5791 DECL_ANTICIPATED (tmpl) = 1;
5792 DECL_FRIEND_P (tmpl) = 1;
5793 }
5794 }
5795
5796 if (is_primary)
5797 {
5798 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5799
5800 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5801
5802 /* Give template template parms a DECL_CONTEXT of the template
5803 for which they are a parameter. */
5804 parms = INNERMOST_TEMPLATE_PARMS (parms);
5805 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5806 {
5807 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5808 if (TREE_CODE (parm) == TEMPLATE_DECL)
5809 DECL_CONTEXT (parm) = tmpl;
5810 }
5811
5812 if (TREE_CODE (decl) == TYPE_DECL
5813 && TYPE_DECL_ALIAS_P (decl)
5814 && complex_alias_template_p (tmpl))
5815 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5816 }
5817
5818 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5819 back to its most general template. If TMPL is a specialization,
5820 ARGS may only have the innermost set of arguments. Add the missing
5821 argument levels if necessary. */
5822 if (DECL_TEMPLATE_INFO (tmpl))
5823 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5824
5825 info = build_template_info (tmpl, args);
5826
5827 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5828 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5829 else
5830 {
5831 if (is_primary)
5832 retrofit_lang_decl (decl);
5833 if (DECL_LANG_SPECIFIC (decl))
5834 DECL_TEMPLATE_INFO (decl) = info;
5835 }
5836
5837 if (flag_implicit_templates
5838 && !is_friend
5839 && TREE_PUBLIC (decl)
5840 && VAR_OR_FUNCTION_DECL_P (decl))
5841 /* Set DECL_COMDAT on template instantiations; if we force
5842 them to be emitted by explicit instantiation or -frepo,
5843 mark_needed will tell cgraph to do the right thing. */
5844 DECL_COMDAT (decl) = true;
5845
5846 return DECL_TEMPLATE_RESULT (tmpl);
5847 }
5848
5849 tree
5850 push_template_decl (tree decl)
5851 {
5852 return push_template_decl_real (decl, false);
5853 }
5854
5855 /* FN is an inheriting constructor that inherits from the constructor
5856 template INHERITED; turn FN into a constructor template with a matching
5857 template header. */
5858
5859 tree
5860 add_inherited_template_parms (tree fn, tree inherited)
5861 {
5862 tree inner_parms
5863 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5864 inner_parms = copy_node (inner_parms);
5865 tree parms
5866 = tree_cons (size_int (processing_template_decl + 1),
5867 inner_parms, current_template_parms);
5868 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5869 tree args = template_parms_to_args (parms);
5870 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5871 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5872 DECL_TEMPLATE_RESULT (tmpl) = fn;
5873 DECL_ARTIFICIAL (tmpl) = true;
5874 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5875 return tmpl;
5876 }
5877
5878 /* Called when a class template TYPE is redeclared with the indicated
5879 template PARMS, e.g.:
5880
5881 template <class T> struct S;
5882 template <class T> struct S {}; */
5883
5884 bool
5885 redeclare_class_template (tree type, tree parms, tree cons)
5886 {
5887 tree tmpl;
5888 tree tmpl_parms;
5889 int i;
5890
5891 if (!TYPE_TEMPLATE_INFO (type))
5892 {
5893 error ("%qT is not a template type", type);
5894 return false;
5895 }
5896
5897 tmpl = TYPE_TI_TEMPLATE (type);
5898 if (!PRIMARY_TEMPLATE_P (tmpl))
5899 /* The type is nested in some template class. Nothing to worry
5900 about here; there are no new template parameters for the nested
5901 type. */
5902 return true;
5903
5904 if (!parms)
5905 {
5906 error ("template specifiers not specified in declaration of %qD",
5907 tmpl);
5908 return false;
5909 }
5910
5911 parms = INNERMOST_TEMPLATE_PARMS (parms);
5912 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5913
5914 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5915 {
5916 error_n (input_location, TREE_VEC_LENGTH (parms),
5917 "redeclared with %d template parameter",
5918 "redeclared with %d template parameters",
5919 TREE_VEC_LENGTH (parms));
5920 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5921 "previous declaration %qD used %d template parameter",
5922 "previous declaration %qD used %d template parameters",
5923 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5924 return false;
5925 }
5926
5927 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5928 {
5929 tree tmpl_parm;
5930 tree parm;
5931 tree tmpl_default;
5932 tree parm_default;
5933
5934 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5935 || TREE_VEC_ELT (parms, i) == error_mark_node)
5936 continue;
5937
5938 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5939 if (error_operand_p (tmpl_parm))
5940 return false;
5941
5942 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5943 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5944 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5945
5946 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5947 TEMPLATE_DECL. */
5948 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5949 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5950 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5951 || (TREE_CODE (tmpl_parm) != PARM_DECL
5952 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5953 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5954 || (TREE_CODE (tmpl_parm) == PARM_DECL
5955 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5956 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5957 {
5958 error ("template parameter %q+#D", tmpl_parm);
5959 error ("redeclared here as %q#D", parm);
5960 return false;
5961 }
5962
5963 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5964 {
5965 /* We have in [temp.param]:
5966
5967 A template-parameter may not be given default arguments
5968 by two different declarations in the same scope. */
5969 error_at (input_location, "redefinition of default argument for %q#D", parm);
5970 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5971 "original definition appeared here");
5972 return false;
5973 }
5974
5975 if (parm_default != NULL_TREE)
5976 /* Update the previous template parameters (which are the ones
5977 that will really count) with the new default value. */
5978 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5979 else if (tmpl_default != NULL_TREE)
5980 /* Update the new parameters, too; they'll be used as the
5981 parameters for any members. */
5982 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5983
5984 /* Give each template template parm in this redeclaration a
5985 DECL_CONTEXT of the template for which they are a parameter. */
5986 if (TREE_CODE (parm) == TEMPLATE_DECL)
5987 {
5988 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5989 DECL_CONTEXT (parm) = tmpl;
5990 }
5991
5992 if (TREE_CODE (parm) == TYPE_DECL)
5993 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5994 }
5995
5996 // Cannot redeclare a class template with a different set of constraints.
5997 if (!equivalent_constraints (get_constraints (tmpl), cons))
5998 {
5999 error_at (input_location, "redeclaration %q#D with different "
6000 "constraints", tmpl);
6001 inform (DECL_SOURCE_LOCATION (tmpl),
6002 "original declaration appeared here");
6003 }
6004
6005 return true;
6006 }
6007
6008 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6009 to be used when the caller has already checked
6010 (processing_template_decl
6011 && !instantiation_dependent_expression_p (expr)
6012 && potential_constant_expression (expr))
6013 and cleared processing_template_decl. */
6014
6015 tree
6016 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6017 {
6018 return tsubst_copy_and_build (expr,
6019 /*args=*/NULL_TREE,
6020 complain,
6021 /*in_decl=*/NULL_TREE,
6022 /*function_p=*/false,
6023 /*integral_constant_expression_p=*/true);
6024 }
6025
6026 /* Simplify EXPR if it is a non-dependent expression. Returns the
6027 (possibly simplified) expression. */
6028
6029 tree
6030 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6031 {
6032 if (expr == NULL_TREE)
6033 return NULL_TREE;
6034
6035 /* If we're in a template, but EXPR isn't value dependent, simplify
6036 it. We're supposed to treat:
6037
6038 template <typename T> void f(T[1 + 1]);
6039 template <typename T> void f(T[2]);
6040
6041 as two declarations of the same function, for example. */
6042 if (processing_template_decl
6043 && is_nondependent_constant_expression (expr))
6044 {
6045 processing_template_decl_sentinel s;
6046 expr = instantiate_non_dependent_expr_internal (expr, complain);
6047 }
6048 return expr;
6049 }
6050
6051 tree
6052 instantiate_non_dependent_expr (tree expr)
6053 {
6054 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6055 }
6056
6057 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6058 an uninstantiated expression. */
6059
6060 tree
6061 instantiate_non_dependent_or_null (tree expr)
6062 {
6063 if (expr == NULL_TREE)
6064 return NULL_TREE;
6065 if (processing_template_decl)
6066 {
6067 if (!is_nondependent_constant_expression (expr))
6068 expr = NULL_TREE;
6069 else
6070 {
6071 processing_template_decl_sentinel s;
6072 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6073 }
6074 }
6075 return expr;
6076 }
6077
6078 /* True iff T is a specialization of a variable template. */
6079
6080 bool
6081 variable_template_specialization_p (tree t)
6082 {
6083 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6084 return false;
6085 tree tmpl = DECL_TI_TEMPLATE (t);
6086 return variable_template_p (tmpl);
6087 }
6088
6089 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6090 template declaration, or a TYPE_DECL for an alias declaration. */
6091
6092 bool
6093 alias_type_or_template_p (tree t)
6094 {
6095 if (t == NULL_TREE)
6096 return false;
6097 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6098 || (TYPE_P (t)
6099 && TYPE_NAME (t)
6100 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6101 || DECL_ALIAS_TEMPLATE_P (t));
6102 }
6103
6104 /* Return TRUE iff T is a specialization of an alias template. */
6105
6106 bool
6107 alias_template_specialization_p (const_tree t)
6108 {
6109 /* It's an alias template specialization if it's an alias and its
6110 TYPE_NAME is a specialization of a primary template. */
6111 if (TYPE_ALIAS_P (t))
6112 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6113 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6114
6115 return false;
6116 }
6117
6118 /* An alias template is complex from a SFINAE perspective if a template-id
6119 using that alias can be ill-formed when the expansion is not, as with
6120 the void_t template. We determine this by checking whether the
6121 expansion for the alias template uses all its template parameters. */
6122
6123 struct uses_all_template_parms_data
6124 {
6125 int level;
6126 bool *seen;
6127 };
6128
6129 static int
6130 uses_all_template_parms_r (tree t, void *data_)
6131 {
6132 struct uses_all_template_parms_data &data
6133 = *(struct uses_all_template_parms_data*)data_;
6134 tree idx = get_template_parm_index (t);
6135
6136 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6137 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6138 return 0;
6139 }
6140
6141 static bool
6142 complex_alias_template_p (const_tree tmpl)
6143 {
6144 struct uses_all_template_parms_data data;
6145 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6146 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6147 data.level = TMPL_PARMS_DEPTH (parms);
6148 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6149 data.seen = XALLOCAVEC (bool, len);
6150 for (int i = 0; i < len; ++i)
6151 data.seen[i] = false;
6152
6153 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6154 for (int i = 0; i < len; ++i)
6155 if (!data.seen[i])
6156 return true;
6157 return false;
6158 }
6159
6160 /* Return TRUE iff T is a specialization of a complex alias template with
6161 dependent template-arguments. */
6162
6163 bool
6164 dependent_alias_template_spec_p (const_tree t)
6165 {
6166 if (!alias_template_specialization_p (t))
6167 return false;
6168
6169 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6170 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6171 return false;
6172
6173 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6174 if (!any_dependent_template_arguments_p (args))
6175 return false;
6176
6177 return true;
6178 }
6179
6180 /* Return the number of innermost template parameters in TMPL. */
6181
6182 static int
6183 num_innermost_template_parms (tree tmpl)
6184 {
6185 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6186 return TREE_VEC_LENGTH (parms);
6187 }
6188
6189 /* Return either TMPL or another template that it is equivalent to under DR
6190 1286: An alias that just changes the name of a template is equivalent to
6191 the other template. */
6192
6193 static tree
6194 get_underlying_template (tree tmpl)
6195 {
6196 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6197 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6198 {
6199 /* Determine if the alias is equivalent to an underlying template. */
6200 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6201 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6202 if (!tinfo)
6203 break;
6204
6205 tree underlying = TI_TEMPLATE (tinfo);
6206 if (!PRIMARY_TEMPLATE_P (underlying)
6207 || (num_innermost_template_parms (tmpl)
6208 != num_innermost_template_parms (underlying)))
6209 break;
6210
6211 tree alias_args = INNERMOST_TEMPLATE_ARGS
6212 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6213 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6214 break;
6215
6216 /* Alias is equivalent. Strip it and repeat. */
6217 tmpl = underlying;
6218 }
6219
6220 return tmpl;
6221 }
6222
6223 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6224 must be a reference-to-function or a pointer-to-function type, as specified
6225 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6226 and check that the resulting function has external linkage. */
6227
6228 static tree
6229 convert_nontype_argument_function (tree type, tree expr,
6230 tsubst_flags_t complain)
6231 {
6232 tree fns = expr;
6233 tree fn, fn_no_ptr;
6234 linkage_kind linkage;
6235
6236 fn = instantiate_type (type, fns, tf_none);
6237 if (fn == error_mark_node)
6238 return error_mark_node;
6239
6240 if (value_dependent_expression_p (fn))
6241 goto accept;
6242
6243 fn_no_ptr = strip_fnptr_conv (fn);
6244 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6245 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6246 if (BASELINK_P (fn_no_ptr))
6247 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6248
6249 /* [temp.arg.nontype]/1
6250
6251 A template-argument for a non-type, non-template template-parameter
6252 shall be one of:
6253 [...]
6254 -- the address of an object or function with external [C++11: or
6255 internal] linkage. */
6256
6257 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6258 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6259 {
6260 if (complain & tf_error)
6261 {
6262 error ("%qE is not a valid template argument for type %qT",
6263 expr, type);
6264 if (TYPE_PTR_P (type))
6265 inform (input_location, "it must be the address of a function "
6266 "with external linkage");
6267 else
6268 inform (input_location, "it must be the name of a function with "
6269 "external linkage");
6270 }
6271 return NULL_TREE;
6272 }
6273
6274 linkage = decl_linkage (fn_no_ptr);
6275 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6276 {
6277 if (complain & tf_error)
6278 {
6279 if (cxx_dialect >= cxx11)
6280 error ("%qE is not a valid template argument for type %qT "
6281 "because %qD has no linkage",
6282 expr, type, fn_no_ptr);
6283 else
6284 error ("%qE is not a valid template argument for type %qT "
6285 "because %qD does not have external linkage",
6286 expr, type, fn_no_ptr);
6287 }
6288 return NULL_TREE;
6289 }
6290
6291 accept:
6292 if (TYPE_REF_P (type))
6293 {
6294 if (REFERENCE_REF_P (fn))
6295 fn = TREE_OPERAND (fn, 0);
6296 else
6297 fn = build_address (fn);
6298 }
6299 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6300 fn = build_nop (type, fn);
6301
6302 return fn;
6303 }
6304
6305 /* Subroutine of convert_nontype_argument.
6306 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6307 Emit an error otherwise. */
6308
6309 static bool
6310 check_valid_ptrmem_cst_expr (tree type, tree expr,
6311 tsubst_flags_t complain)
6312 {
6313 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6314 tree orig_expr = expr;
6315 STRIP_NOPS (expr);
6316 if (null_ptr_cst_p (expr))
6317 return true;
6318 if (TREE_CODE (expr) == PTRMEM_CST
6319 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6320 PTRMEM_CST_CLASS (expr)))
6321 return true;
6322 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6323 return true;
6324 if (processing_template_decl
6325 && TREE_CODE (expr) == ADDR_EXPR
6326 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6327 return true;
6328 if (complain & tf_error)
6329 {
6330 error_at (loc, "%qE is not a valid template argument for type %qT",
6331 orig_expr, type);
6332 if (TREE_CODE (expr) != PTRMEM_CST)
6333 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6334 else
6335 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6336 }
6337 return false;
6338 }
6339
6340 /* Returns TRUE iff the address of OP is value-dependent.
6341
6342 14.6.2.4 [temp.dep.temp]:
6343 A non-integral non-type template-argument is dependent if its type is
6344 dependent or it has either of the following forms
6345 qualified-id
6346 & qualified-id
6347 and contains a nested-name-specifier which specifies a class-name that
6348 names a dependent type.
6349
6350 We generalize this to just say that the address of a member of a
6351 dependent class is value-dependent; the above doesn't cover the
6352 address of a static data member named with an unqualified-id. */
6353
6354 static bool
6355 has_value_dependent_address (tree op)
6356 {
6357 /* We could use get_inner_reference here, but there's no need;
6358 this is only relevant for template non-type arguments, which
6359 can only be expressed as &id-expression. */
6360 if (DECL_P (op))
6361 {
6362 tree ctx = CP_DECL_CONTEXT (op);
6363 if (TYPE_P (ctx) && dependent_type_p (ctx))
6364 return true;
6365 }
6366
6367 return false;
6368 }
6369
6370 /* The next set of functions are used for providing helpful explanatory
6371 diagnostics for failed overload resolution. Their messages should be
6372 indented by two spaces for consistency with the messages in
6373 call.c */
6374
6375 static int
6376 unify_success (bool /*explain_p*/)
6377 {
6378 return 0;
6379 }
6380
6381 /* Other failure functions should call this one, to provide a single function
6382 for setting a breakpoint on. */
6383
6384 static int
6385 unify_invalid (bool /*explain_p*/)
6386 {
6387 return 1;
6388 }
6389
6390 static int
6391 unify_parameter_deduction_failure (bool explain_p, tree parm)
6392 {
6393 if (explain_p)
6394 inform (input_location,
6395 " couldn%'t deduce template parameter %qD", parm);
6396 return unify_invalid (explain_p);
6397 }
6398
6399 static int
6400 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6401 {
6402 if (explain_p)
6403 inform (input_location,
6404 " types %qT and %qT have incompatible cv-qualifiers",
6405 parm, arg);
6406 return unify_invalid (explain_p);
6407 }
6408
6409 static int
6410 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6411 {
6412 if (explain_p)
6413 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6414 return unify_invalid (explain_p);
6415 }
6416
6417 static int
6418 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6419 {
6420 if (explain_p)
6421 inform (input_location,
6422 " template parameter %qD is not a parameter pack, but "
6423 "argument %qD is",
6424 parm, arg);
6425 return unify_invalid (explain_p);
6426 }
6427
6428 static int
6429 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6430 {
6431 if (explain_p)
6432 inform (input_location,
6433 " template argument %qE does not match "
6434 "pointer-to-member constant %qE",
6435 arg, parm);
6436 return unify_invalid (explain_p);
6437 }
6438
6439 static int
6440 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6441 {
6442 if (explain_p)
6443 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6444 return unify_invalid (explain_p);
6445 }
6446
6447 static int
6448 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6449 {
6450 if (explain_p)
6451 inform (input_location,
6452 " inconsistent parameter pack deduction with %qT and %qT",
6453 old_arg, new_arg);
6454 return unify_invalid (explain_p);
6455 }
6456
6457 static int
6458 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6459 {
6460 if (explain_p)
6461 {
6462 if (TYPE_P (parm))
6463 inform (input_location,
6464 " deduced conflicting types for parameter %qT (%qT and %qT)",
6465 parm, first, second);
6466 else
6467 inform (input_location,
6468 " deduced conflicting values for non-type parameter "
6469 "%qE (%qE and %qE)", parm, first, second);
6470 }
6471 return unify_invalid (explain_p);
6472 }
6473
6474 static int
6475 unify_vla_arg (bool explain_p, tree arg)
6476 {
6477 if (explain_p)
6478 inform (input_location,
6479 " variable-sized array type %qT is not "
6480 "a valid template argument",
6481 arg);
6482 return unify_invalid (explain_p);
6483 }
6484
6485 static int
6486 unify_method_type_error (bool explain_p, tree arg)
6487 {
6488 if (explain_p)
6489 inform (input_location,
6490 " member function type %qT is not a valid template argument",
6491 arg);
6492 return unify_invalid (explain_p);
6493 }
6494
6495 static int
6496 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6497 {
6498 if (explain_p)
6499 {
6500 if (least_p)
6501 inform_n (input_location, wanted,
6502 " candidate expects at least %d argument, %d provided",
6503 " candidate expects at least %d arguments, %d provided",
6504 wanted, have);
6505 else
6506 inform_n (input_location, wanted,
6507 " candidate expects %d argument, %d provided",
6508 " candidate expects %d arguments, %d provided",
6509 wanted, have);
6510 }
6511 return unify_invalid (explain_p);
6512 }
6513
6514 static int
6515 unify_too_many_arguments (bool explain_p, int have, int wanted)
6516 {
6517 return unify_arity (explain_p, have, wanted);
6518 }
6519
6520 static int
6521 unify_too_few_arguments (bool explain_p, int have, int wanted,
6522 bool least_p = false)
6523 {
6524 return unify_arity (explain_p, have, wanted, least_p);
6525 }
6526
6527 static int
6528 unify_arg_conversion (bool explain_p, tree to_type,
6529 tree from_type, tree arg)
6530 {
6531 if (explain_p)
6532 inform (cp_expr_loc_or_loc (arg, input_location),
6533 " cannot convert %qE (type %qT) to type %qT",
6534 arg, from_type, to_type);
6535 return unify_invalid (explain_p);
6536 }
6537
6538 static int
6539 unify_no_common_base (bool explain_p, enum template_base_result r,
6540 tree parm, tree arg)
6541 {
6542 if (explain_p)
6543 switch (r)
6544 {
6545 case tbr_ambiguous_baseclass:
6546 inform (input_location, " %qT is an ambiguous base class of %qT",
6547 parm, arg);
6548 break;
6549 default:
6550 inform (input_location, " %qT is not derived from %qT", arg, parm);
6551 break;
6552 }
6553 return unify_invalid (explain_p);
6554 }
6555
6556 static int
6557 unify_inconsistent_template_template_parameters (bool explain_p)
6558 {
6559 if (explain_p)
6560 inform (input_location,
6561 " template parameters of a template template argument are "
6562 "inconsistent with other deduced template arguments");
6563 return unify_invalid (explain_p);
6564 }
6565
6566 static int
6567 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6568 {
6569 if (explain_p)
6570 inform (input_location,
6571 " cannot deduce a template for %qT from non-template type %qT",
6572 parm, arg);
6573 return unify_invalid (explain_p);
6574 }
6575
6576 static int
6577 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6578 {
6579 if (explain_p)
6580 inform (input_location,
6581 " template argument %qE does not match %qE", arg, parm);
6582 return unify_invalid (explain_p);
6583 }
6584
6585 /* True if T is a C++20 template parameter object to store the argument for a
6586 template parameter of class type. */
6587
6588 bool
6589 template_parm_object_p (const_tree t)
6590 {
6591 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6592 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6593 }
6594
6595 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6596 argument for TYPE, points to an unsuitable object. */
6597
6598 static bool
6599 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6600 {
6601 switch (TREE_CODE (expr))
6602 {
6603 CASE_CONVERT:
6604 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6605 complain);
6606
6607 case TARGET_EXPR:
6608 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6609 complain);
6610
6611 case CONSTRUCTOR:
6612 {
6613 unsigned i; tree elt;
6614 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6615 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6616 return true;
6617 }
6618 break;
6619
6620 case ADDR_EXPR:
6621 {
6622 tree decl = TREE_OPERAND (expr, 0);
6623
6624 if (!VAR_P (decl))
6625 {
6626 if (complain & tf_error)
6627 error ("%qE is not a valid template argument of type %qT "
6628 "because %qE is not a variable", expr, type, decl);
6629 return true;
6630 }
6631 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6632 {
6633 if (complain & tf_error)
6634 error ("%qE is not a valid template argument of type %qT "
6635 "in C++98 because %qD does not have external linkage",
6636 expr, type, decl);
6637 return true;
6638 }
6639 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6640 && decl_linkage (decl) == lk_none)
6641 {
6642 if (complain & tf_error)
6643 error ("%qE is not a valid template argument of type %qT "
6644 "because %qD has no linkage", expr, type, decl);
6645 return true;
6646 }
6647 /* C++17: For a non-type template-parameter of reference or pointer
6648 type, the value of the constant expression shall not refer to (or
6649 for a pointer type, shall not be the address of):
6650 * a subobject (4.5),
6651 * a temporary object (15.2),
6652 * a string literal (5.13.5),
6653 * the result of a typeid expression (8.2.8), or
6654 * a predefined __func__ variable (11.4.1). */
6655 else if (DECL_ARTIFICIAL (decl))
6656 {
6657 if (complain & tf_error)
6658 error ("the address of %qD is not a valid template argument",
6659 decl);
6660 return true;
6661 }
6662 else if (!same_type_ignoring_top_level_qualifiers_p
6663 (strip_array_types (TREE_TYPE (type)),
6664 strip_array_types (TREE_TYPE (decl))))
6665 {
6666 if (complain & tf_error)
6667 error ("the address of the %qT subobject of %qD is not a "
6668 "valid template argument", TREE_TYPE (type), decl);
6669 return true;
6670 }
6671 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6672 {
6673 if (complain & tf_error)
6674 error ("the address of %qD is not a valid template argument "
6675 "because it does not have static storage duration",
6676 decl);
6677 return true;
6678 }
6679 }
6680 break;
6681
6682 default:
6683 if (!INDIRECT_TYPE_P (type))
6684 /* We're only concerned about pointers and references here. */;
6685 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6686 /* Null pointer values are OK in C++11. */;
6687 else
6688 {
6689 if (VAR_P (expr))
6690 {
6691 if (complain & tf_error)
6692 error ("%qD is not a valid template argument "
6693 "because %qD is a variable, not the address of "
6694 "a variable", expr, expr);
6695 return true;
6696 }
6697 else
6698 {
6699 if (complain & tf_error)
6700 error ("%qE is not a valid template argument for %qT "
6701 "because it is not the address of a variable",
6702 expr, type);
6703 return true;
6704 }
6705 }
6706 }
6707 return false;
6708
6709 }
6710
6711 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6712 template argument EXPR. */
6713
6714 static tree
6715 get_template_parm_object (tree expr, tsubst_flags_t complain)
6716 {
6717 if (TREE_CODE (expr) == TARGET_EXPR)
6718 expr = TARGET_EXPR_INITIAL (expr);
6719
6720 if (!TREE_CONSTANT (expr))
6721 {
6722 if ((complain & tf_error)
6723 && require_rvalue_constant_expression (expr))
6724 cxx_constant_value (expr);
6725 return error_mark_node;
6726 }
6727 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6728 return error_mark_node;
6729
6730 tree name = mangle_template_parm_object (expr);
6731 tree decl = get_global_binding (name);
6732 if (decl)
6733 return decl;
6734
6735 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6736 decl = create_temporary_var (type);
6737 TREE_STATIC (decl) = true;
6738 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6739 TREE_READONLY (decl) = true;
6740 DECL_NAME (decl) = name;
6741 SET_DECL_ASSEMBLER_NAME (decl, name);
6742 DECL_CONTEXT (decl) = global_namespace;
6743 comdat_linkage (decl);
6744 pushdecl_top_level_and_finish (decl, expr);
6745 return decl;
6746 }
6747
6748 /* Attempt to convert the non-type template parameter EXPR to the
6749 indicated TYPE. If the conversion is successful, return the
6750 converted value. If the conversion is unsuccessful, return
6751 NULL_TREE if we issued an error message, or error_mark_node if we
6752 did not. We issue error messages for out-and-out bad template
6753 parameters, but not simply because the conversion failed, since we
6754 might be just trying to do argument deduction. Both TYPE and EXPR
6755 must be non-dependent.
6756
6757 The conversion follows the special rules described in
6758 [temp.arg.nontype], and it is much more strict than an implicit
6759 conversion.
6760
6761 This function is called twice for each template argument (see
6762 lookup_template_class for a more accurate description of this
6763 problem). This means that we need to handle expressions which
6764 are not valid in a C++ source, but can be created from the
6765 first call (for instance, casts to perform conversions). These
6766 hacks can go away after we fix the double coercion problem. */
6767
6768 static tree
6769 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6770 {
6771 tree expr_type;
6772 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6773
6774 /* Detect immediately string literals as invalid non-type argument.
6775 This special-case is not needed for correctness (we would easily
6776 catch this later), but only to provide better diagnostic for this
6777 common user mistake. As suggested by DR 100, we do not mention
6778 linkage issues in the diagnostic as this is not the point. */
6779 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6780 {
6781 if (complain & tf_error)
6782 error ("%qE is not a valid template argument for type %qT "
6783 "because string literals can never be used in this context",
6784 expr, type);
6785 return NULL_TREE;
6786 }
6787
6788 /* Add the ADDR_EXPR now for the benefit of
6789 value_dependent_expression_p. */
6790 if (TYPE_PTROBV_P (type)
6791 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6792 {
6793 expr = decay_conversion (expr, complain);
6794 if (expr == error_mark_node)
6795 return error_mark_node;
6796 }
6797
6798 /* If we are in a template, EXPR may be non-dependent, but still
6799 have a syntactic, rather than semantic, form. For example, EXPR
6800 might be a SCOPE_REF, rather than the VAR_DECL to which the
6801 SCOPE_REF refers. Preserving the qualifying scope is necessary
6802 so that access checking can be performed when the template is
6803 instantiated -- but here we need the resolved form so that we can
6804 convert the argument. */
6805 bool non_dep = false;
6806 if (TYPE_REF_OBJ_P (type)
6807 && has_value_dependent_address (expr))
6808 /* If we want the address and it's value-dependent, don't fold. */;
6809 else if (processing_template_decl
6810 && is_nondependent_constant_expression (expr))
6811 non_dep = true;
6812 if (error_operand_p (expr))
6813 return error_mark_node;
6814 expr_type = TREE_TYPE (expr);
6815
6816 /* If the argument is non-dependent, perform any conversions in
6817 non-dependent context as well. */
6818 processing_template_decl_sentinel s (non_dep);
6819 if (non_dep)
6820 expr = instantiate_non_dependent_expr_internal (expr, complain);
6821
6822 if (value_dependent_expression_p (expr))
6823 expr = canonicalize_expr_argument (expr, complain);
6824
6825 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6826 to a non-type argument of "nullptr". */
6827 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6828 expr = fold_simple (convert (type, expr));
6829
6830 /* In C++11, integral or enumeration non-type template arguments can be
6831 arbitrary constant expressions. Pointer and pointer to
6832 member arguments can be general constant expressions that evaluate
6833 to a null value, but otherwise still need to be of a specific form. */
6834 if (cxx_dialect >= cxx11)
6835 {
6836 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6837 /* A PTRMEM_CST is already constant, and a valid template
6838 argument for a parameter of pointer to member type, we just want
6839 to leave it in that form rather than lower it to a
6840 CONSTRUCTOR. */;
6841 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6842 || cxx_dialect >= cxx17)
6843 {
6844 /* Calling build_converted_constant_expr might create a call to
6845 a conversion function with a value-dependent argument, which
6846 could invoke taking the address of a temporary representing
6847 the result of the conversion. */
6848 if (COMPOUND_LITERAL_P (expr)
6849 && CONSTRUCTOR_IS_DEPENDENT (expr)
6850 && MAYBE_CLASS_TYPE_P (expr_type)
6851 && TYPE_HAS_CONVERSION (expr_type))
6852 {
6853 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6854 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6855 return expr;
6856 }
6857 /* C++17: A template-argument for a non-type template-parameter shall
6858 be a converted constant expression (8.20) of the type of the
6859 template-parameter. */
6860 expr = build_converted_constant_expr (type, expr, complain);
6861 if (expr == error_mark_node)
6862 /* Make sure we return NULL_TREE only if we have really issued
6863 an error, as described above. */
6864 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6865 expr = maybe_constant_value (expr, NULL_TREE,
6866 /*manifestly_const_eval=*/true);
6867 expr = convert_from_reference (expr);
6868 }
6869 else if (TYPE_PTR_OR_PTRMEM_P (type))
6870 {
6871 tree folded = maybe_constant_value (expr, NULL_TREE,
6872 /*manifestly_const_eval=*/true);
6873 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6874 : null_member_pointer_value_p (folded))
6875 expr = folded;
6876 }
6877 }
6878
6879 if (TYPE_REF_P (type))
6880 expr = mark_lvalue_use (expr);
6881 else
6882 expr = mark_rvalue_use (expr);
6883
6884 /* HACK: Due to double coercion, we can get a
6885 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6886 which is the tree that we built on the first call (see
6887 below when coercing to reference to object or to reference to
6888 function). We just strip everything and get to the arg.
6889 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6890 for examples. */
6891 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6892 {
6893 tree probe_type, probe = expr;
6894 if (REFERENCE_REF_P (probe))
6895 probe = TREE_OPERAND (probe, 0);
6896 probe_type = TREE_TYPE (probe);
6897 if (TREE_CODE (probe) == NOP_EXPR)
6898 {
6899 /* ??? Maybe we could use convert_from_reference here, but we
6900 would need to relax its constraints because the NOP_EXPR
6901 could actually change the type to something more cv-qualified,
6902 and this is not folded by convert_from_reference. */
6903 tree addr = TREE_OPERAND (probe, 0);
6904 if (TYPE_REF_P (probe_type)
6905 && TREE_CODE (addr) == ADDR_EXPR
6906 && TYPE_PTR_P (TREE_TYPE (addr))
6907 && (same_type_ignoring_top_level_qualifiers_p
6908 (TREE_TYPE (probe_type),
6909 TREE_TYPE (TREE_TYPE (addr)))))
6910 {
6911 expr = TREE_OPERAND (addr, 0);
6912 expr_type = TREE_TYPE (probe_type);
6913 }
6914 }
6915 }
6916
6917 /* [temp.arg.nontype]/5, bullet 1
6918
6919 For a non-type template-parameter of integral or enumeration type,
6920 integral promotions (_conv.prom_) and integral conversions
6921 (_conv.integral_) are applied. */
6922 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6923 {
6924 if (cxx_dialect < cxx11)
6925 {
6926 tree t = build_converted_constant_expr (type, expr, complain);
6927 t = maybe_constant_value (t);
6928 if (t != error_mark_node)
6929 expr = t;
6930 }
6931
6932 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6933 return error_mark_node;
6934
6935 /* Notice that there are constant expressions like '4 % 0' which
6936 do not fold into integer constants. */
6937 if (TREE_CODE (expr) != INTEGER_CST
6938 && !value_dependent_expression_p (expr))
6939 {
6940 if (complain & tf_error)
6941 {
6942 int errs = errorcount, warns = warningcount + werrorcount;
6943 if (!require_potential_constant_expression (expr))
6944 expr = error_mark_node;
6945 else
6946 expr = cxx_constant_value (expr);
6947 if (errorcount > errs || warningcount + werrorcount > warns)
6948 inform (loc, "in template argument for type %qT", type);
6949 if (expr == error_mark_node)
6950 return NULL_TREE;
6951 /* else cxx_constant_value complained but gave us
6952 a real constant, so go ahead. */
6953 if (TREE_CODE (expr) != INTEGER_CST)
6954 {
6955 /* Some assemble time constant expressions like
6956 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6957 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6958 as we can emit them into .rodata initializers of
6959 variables, yet they can't fold into an INTEGER_CST at
6960 compile time. Refuse them here. */
6961 gcc_checking_assert (reduced_constant_expression_p (expr));
6962 error_at (loc, "template argument %qE for type %qT not "
6963 "a constant integer", expr, type);
6964 return NULL_TREE;
6965 }
6966 }
6967 else
6968 return NULL_TREE;
6969 }
6970
6971 /* Avoid typedef problems. */
6972 if (TREE_TYPE (expr) != type)
6973 expr = fold_convert (type, expr);
6974 }
6975 /* [temp.arg.nontype]/5, bullet 2
6976
6977 For a non-type template-parameter of type pointer to object,
6978 qualification conversions (_conv.qual_) and the array-to-pointer
6979 conversion (_conv.array_) are applied. */
6980 else if (TYPE_PTROBV_P (type))
6981 {
6982 tree decayed = expr;
6983
6984 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6985 decay_conversion or an explicit cast. If it's a problematic cast,
6986 we'll complain about it below. */
6987 if (TREE_CODE (expr) == NOP_EXPR)
6988 {
6989 tree probe = expr;
6990 STRIP_NOPS (probe);
6991 if (TREE_CODE (probe) == ADDR_EXPR
6992 && TYPE_PTR_P (TREE_TYPE (probe)))
6993 {
6994 expr = probe;
6995 expr_type = TREE_TYPE (expr);
6996 }
6997 }
6998
6999 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7000
7001 A template-argument for a non-type, non-template template-parameter
7002 shall be one of: [...]
7003
7004 -- the name of a non-type template-parameter;
7005 -- the address of an object or function with external linkage, [...]
7006 expressed as "& id-expression" where the & is optional if the name
7007 refers to a function or array, or if the corresponding
7008 template-parameter is a reference.
7009
7010 Here, we do not care about functions, as they are invalid anyway
7011 for a parameter of type pointer-to-object. */
7012
7013 if (value_dependent_expression_p (expr))
7014 /* Non-type template parameters are OK. */
7015 ;
7016 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7017 /* Null pointer values are OK in C++11. */;
7018 else if (TREE_CODE (expr) != ADDR_EXPR
7019 && !INDIRECT_TYPE_P (expr_type))
7020 /* Other values, like integer constants, might be valid
7021 non-type arguments of some other type. */
7022 return error_mark_node;
7023 else if (invalid_tparm_referent_p (type, expr, complain))
7024 return NULL_TREE;
7025
7026 expr = decayed;
7027
7028 expr = perform_qualification_conversions (type, expr);
7029 if (expr == error_mark_node)
7030 return error_mark_node;
7031 }
7032 /* [temp.arg.nontype]/5, bullet 3
7033
7034 For a non-type template-parameter of type reference to object, no
7035 conversions apply. The type referred to by the reference may be more
7036 cv-qualified than the (otherwise identical) type of the
7037 template-argument. The template-parameter is bound directly to the
7038 template-argument, which must be an lvalue. */
7039 else if (TYPE_REF_OBJ_P (type))
7040 {
7041 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7042 expr_type))
7043 return error_mark_node;
7044
7045 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7046 {
7047 if (complain & tf_error)
7048 error ("%qE is not a valid template argument for type %qT "
7049 "because of conflicts in cv-qualification", expr, type);
7050 return NULL_TREE;
7051 }
7052
7053 if (!lvalue_p (expr))
7054 {
7055 if (complain & tf_error)
7056 error ("%qE is not a valid template argument for type %qT "
7057 "because it is not an lvalue", expr, type);
7058 return NULL_TREE;
7059 }
7060
7061 /* [temp.arg.nontype]/1
7062
7063 A template-argument for a non-type, non-template template-parameter
7064 shall be one of: [...]
7065
7066 -- the address of an object or function with external linkage. */
7067 if (INDIRECT_REF_P (expr)
7068 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7069 {
7070 expr = TREE_OPERAND (expr, 0);
7071 if (DECL_P (expr))
7072 {
7073 if (complain & tf_error)
7074 error ("%q#D is not a valid template argument for type %qT "
7075 "because a reference variable does not have a constant "
7076 "address", expr, type);
7077 return NULL_TREE;
7078 }
7079 }
7080
7081 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7082 && value_dependent_expression_p (expr))
7083 /* OK, dependent reference. We don't want to ask whether a DECL is
7084 itself value-dependent, since what we want here is its address. */;
7085 else
7086 {
7087 expr = build_address (expr);
7088
7089 if (invalid_tparm_referent_p (type, expr, complain))
7090 return NULL_TREE;
7091 }
7092
7093 if (!same_type_p (type, TREE_TYPE (expr)))
7094 expr = build_nop (type, expr);
7095 }
7096 /* [temp.arg.nontype]/5, bullet 4
7097
7098 For a non-type template-parameter of type pointer to function, only
7099 the function-to-pointer conversion (_conv.func_) is applied. If the
7100 template-argument represents a set of overloaded functions (or a
7101 pointer to such), the matching function is selected from the set
7102 (_over.over_). */
7103 else if (TYPE_PTRFN_P (type))
7104 {
7105 /* If the argument is a template-id, we might not have enough
7106 context information to decay the pointer. */
7107 if (!type_unknown_p (expr_type))
7108 {
7109 expr = decay_conversion (expr, complain);
7110 if (expr == error_mark_node)
7111 return error_mark_node;
7112 }
7113
7114 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7115 /* Null pointer values are OK in C++11. */
7116 return perform_qualification_conversions (type, expr);
7117
7118 expr = convert_nontype_argument_function (type, expr, complain);
7119 if (!expr || expr == error_mark_node)
7120 return expr;
7121 }
7122 /* [temp.arg.nontype]/5, bullet 5
7123
7124 For a non-type template-parameter of type reference to function, no
7125 conversions apply. If the template-argument represents a set of
7126 overloaded functions, the matching function is selected from the set
7127 (_over.over_). */
7128 else if (TYPE_REFFN_P (type))
7129 {
7130 if (TREE_CODE (expr) == ADDR_EXPR)
7131 {
7132 if (complain & tf_error)
7133 {
7134 error ("%qE is not a valid template argument for type %qT "
7135 "because it is a pointer", expr, type);
7136 inform (input_location, "try using %qE instead",
7137 TREE_OPERAND (expr, 0));
7138 }
7139 return NULL_TREE;
7140 }
7141
7142 expr = convert_nontype_argument_function (type, expr, complain);
7143 if (!expr || expr == error_mark_node)
7144 return expr;
7145 }
7146 /* [temp.arg.nontype]/5, bullet 6
7147
7148 For a non-type template-parameter of type pointer to member function,
7149 no conversions apply. If the template-argument represents a set of
7150 overloaded member functions, the matching member function is selected
7151 from the set (_over.over_). */
7152 else if (TYPE_PTRMEMFUNC_P (type))
7153 {
7154 expr = instantiate_type (type, expr, tf_none);
7155 if (expr == error_mark_node)
7156 return error_mark_node;
7157
7158 /* [temp.arg.nontype] bullet 1 says the pointer to member
7159 expression must be a pointer-to-member constant. */
7160 if (!value_dependent_expression_p (expr)
7161 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7162 return NULL_TREE;
7163
7164 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7165 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7166 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7167 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7168 }
7169 /* [temp.arg.nontype]/5, bullet 7
7170
7171 For a non-type template-parameter of type pointer to data member,
7172 qualification conversions (_conv.qual_) are applied. */
7173 else if (TYPE_PTRDATAMEM_P (type))
7174 {
7175 /* [temp.arg.nontype] bullet 1 says the pointer to member
7176 expression must be a pointer-to-member constant. */
7177 if (!value_dependent_expression_p (expr)
7178 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7179 return NULL_TREE;
7180
7181 expr = perform_qualification_conversions (type, expr);
7182 if (expr == error_mark_node)
7183 return expr;
7184 }
7185 else if (NULLPTR_TYPE_P (type))
7186 {
7187 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7188 {
7189 if (complain & tf_error)
7190 error ("%qE is not a valid template argument for type %qT "
7191 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7192 return NULL_TREE;
7193 }
7194 return expr;
7195 }
7196 else if (CLASS_TYPE_P (type))
7197 {
7198 /* Replace the argument with a reference to the corresponding template
7199 parameter object. */
7200 if (!value_dependent_expression_p (expr))
7201 expr = get_template_parm_object (expr, complain);
7202 if (expr == error_mark_node)
7203 return NULL_TREE;
7204 }
7205 /* A template non-type parameter must be one of the above. */
7206 else
7207 gcc_unreachable ();
7208
7209 /* Sanity check: did we actually convert the argument to the
7210 right type? */
7211 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7212 (type, TREE_TYPE (expr)));
7213 return convert_from_reference (expr);
7214 }
7215
7216 /* Subroutine of coerce_template_template_parms, which returns 1 if
7217 PARM_PARM and ARG_PARM match using the rule for the template
7218 parameters of template template parameters. Both PARM and ARG are
7219 template parameters; the rest of the arguments are the same as for
7220 coerce_template_template_parms.
7221 */
7222 static int
7223 coerce_template_template_parm (tree parm,
7224 tree arg,
7225 tsubst_flags_t complain,
7226 tree in_decl,
7227 tree outer_args)
7228 {
7229 if (arg == NULL_TREE || error_operand_p (arg)
7230 || parm == NULL_TREE || error_operand_p (parm))
7231 return 0;
7232
7233 if (TREE_CODE (arg) != TREE_CODE (parm))
7234 return 0;
7235
7236 switch (TREE_CODE (parm))
7237 {
7238 case TEMPLATE_DECL:
7239 /* We encounter instantiations of templates like
7240 template <template <template <class> class> class TT>
7241 class C; */
7242 {
7243 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7244 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7245
7246 if (!coerce_template_template_parms
7247 (parmparm, argparm, complain, in_decl, outer_args))
7248 return 0;
7249 }
7250 /* Fall through. */
7251
7252 case TYPE_DECL:
7253 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7254 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7255 /* Argument is a parameter pack but parameter is not. */
7256 return 0;
7257 break;
7258
7259 case PARM_DECL:
7260 /* The tsubst call is used to handle cases such as
7261
7262 template <int> class C {};
7263 template <class T, template <T> class TT> class D {};
7264 D<int, C> d;
7265
7266 i.e. the parameter list of TT depends on earlier parameters. */
7267 if (!uses_template_parms (TREE_TYPE (arg)))
7268 {
7269 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7270 if (!uses_template_parms (t)
7271 && !same_type_p (t, TREE_TYPE (arg)))
7272 return 0;
7273 }
7274
7275 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7276 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7277 /* Argument is a parameter pack but parameter is not. */
7278 return 0;
7279
7280 break;
7281
7282 default:
7283 gcc_unreachable ();
7284 }
7285
7286 return 1;
7287 }
7288
7289 /* Coerce template argument list ARGLIST for use with template
7290 template-parameter TEMPL. */
7291
7292 static tree
7293 coerce_template_args_for_ttp (tree templ, tree arglist,
7294 tsubst_flags_t complain)
7295 {
7296 /* Consider an example where a template template parameter declared as
7297
7298 template <class T, class U = std::allocator<T> > class TT
7299
7300 The template parameter level of T and U are one level larger than
7301 of TT. To proper process the default argument of U, say when an
7302 instantiation `TT<int>' is seen, we need to build the full
7303 arguments containing {int} as the innermost level. Outer levels,
7304 available when not appearing as default template argument, can be
7305 obtained from the arguments of the enclosing template.
7306
7307 Suppose that TT is later substituted with std::vector. The above
7308 instantiation is `TT<int, std::allocator<T> >' with TT at
7309 level 1, and T at level 2, while the template arguments at level 1
7310 becomes {std::vector} and the inner level 2 is {int}. */
7311
7312 tree outer = DECL_CONTEXT (templ);
7313 if (outer)
7314 {
7315 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7316 /* We want arguments for the partial specialization, not arguments for
7317 the primary template. */
7318 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7319 else
7320 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7321 }
7322 else if (current_template_parms)
7323 {
7324 /* This is an argument of the current template, so we haven't set
7325 DECL_CONTEXT yet. */
7326 tree relevant_template_parms;
7327
7328 /* Parameter levels that are greater than the level of the given
7329 template template parm are irrelevant. */
7330 relevant_template_parms = current_template_parms;
7331 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7332 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7333 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7334
7335 outer = template_parms_to_args (relevant_template_parms);
7336 }
7337
7338 if (outer)
7339 arglist = add_to_template_args (outer, arglist);
7340
7341 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7342 return coerce_template_parms (parmlist, arglist, templ,
7343 complain,
7344 /*require_all_args=*/true,
7345 /*use_default_args=*/true);
7346 }
7347
7348 /* A cache of template template parameters with match-all default
7349 arguments. */
7350 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7351 static void
7352 store_defaulted_ttp (tree v, tree t)
7353 {
7354 if (!defaulted_ttp_cache)
7355 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7356 defaulted_ttp_cache->put (v, t);
7357 }
7358 static tree
7359 lookup_defaulted_ttp (tree v)
7360 {
7361 if (defaulted_ttp_cache)
7362 if (tree *p = defaulted_ttp_cache->get (v))
7363 return *p;
7364 return NULL_TREE;
7365 }
7366
7367 /* T is a bound template template-parameter. Copy its arguments into default
7368 arguments of the template template-parameter's template parameters. */
7369
7370 static tree
7371 add_defaults_to_ttp (tree otmpl)
7372 {
7373 if (tree c = lookup_defaulted_ttp (otmpl))
7374 return c;
7375
7376 tree ntmpl = copy_node (otmpl);
7377
7378 tree ntype = copy_node (TREE_TYPE (otmpl));
7379 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7380 TYPE_MAIN_VARIANT (ntype) = ntype;
7381 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7382 TYPE_NAME (ntype) = ntmpl;
7383 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7384
7385 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7386 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7387 TEMPLATE_PARM_DECL (idx) = ntmpl;
7388 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7389
7390 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7391 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7392 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7393 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7394 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7395 {
7396 tree o = TREE_VEC_ELT (vec, i);
7397 if (!template_parameter_pack_p (TREE_VALUE (o)))
7398 {
7399 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7400 TREE_PURPOSE (n) = any_targ_node;
7401 }
7402 }
7403
7404 store_defaulted_ttp (otmpl, ntmpl);
7405 return ntmpl;
7406 }
7407
7408 /* ARG is a bound potential template template-argument, and PARGS is a list
7409 of arguments for the corresponding template template-parameter. Adjust
7410 PARGS as appropriate for application to ARG's template, and if ARG is a
7411 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7412 arguments to the template template parameter. */
7413
7414 static tree
7415 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7416 {
7417 ++processing_template_decl;
7418 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7419 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7420 {
7421 /* When comparing two template template-parameters in partial ordering,
7422 rewrite the one currently being used as an argument to have default
7423 arguments for all parameters. */
7424 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7425 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7426 if (pargs != error_mark_node)
7427 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7428 TYPE_TI_ARGS (arg));
7429 }
7430 else
7431 {
7432 tree aparms
7433 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7434 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7435 /*require_all*/true,
7436 /*use_default*/true);
7437 }
7438 --processing_template_decl;
7439 return pargs;
7440 }
7441
7442 /* Subroutine of unify for the case when PARM is a
7443 BOUND_TEMPLATE_TEMPLATE_PARM. */
7444
7445 static int
7446 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7447 bool explain_p)
7448 {
7449 tree parmvec = TYPE_TI_ARGS (parm);
7450 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7451
7452 /* The template template parm might be variadic and the argument
7453 not, so flatten both argument lists. */
7454 parmvec = expand_template_argument_pack (parmvec);
7455 argvec = expand_template_argument_pack (argvec);
7456
7457 if (flag_new_ttp)
7458 {
7459 /* In keeping with P0522R0, adjust P's template arguments
7460 to apply to A's template; then flatten it again. */
7461 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7462 nparmvec = expand_template_argument_pack (nparmvec);
7463
7464 if (unify (tparms, targs, nparmvec, argvec,
7465 UNIFY_ALLOW_NONE, explain_p))
7466 return 1;
7467
7468 /* If the P0522 adjustment eliminated a pack expansion, deduce
7469 empty packs. */
7470 if (flag_new_ttp
7471 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7472 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7473 DEDUCE_EXACT, /*sub*/true, explain_p))
7474 return 1;
7475 }
7476 else
7477 {
7478 /* Deduce arguments T, i from TT<T> or TT<i>.
7479 We check each element of PARMVEC and ARGVEC individually
7480 rather than the whole TREE_VEC since they can have
7481 different number of elements, which is allowed under N2555. */
7482
7483 int len = TREE_VEC_LENGTH (parmvec);
7484
7485 /* Check if the parameters end in a pack, making them
7486 variadic. */
7487 int parm_variadic_p = 0;
7488 if (len > 0
7489 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7490 parm_variadic_p = 1;
7491
7492 for (int i = 0; i < len - parm_variadic_p; ++i)
7493 /* If the template argument list of P contains a pack
7494 expansion that is not the last template argument, the
7495 entire template argument list is a non-deduced
7496 context. */
7497 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7498 return unify_success (explain_p);
7499
7500 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7501 return unify_too_few_arguments (explain_p,
7502 TREE_VEC_LENGTH (argvec), len);
7503
7504 for (int i = 0; i < len - parm_variadic_p; ++i)
7505 if (unify (tparms, targs,
7506 TREE_VEC_ELT (parmvec, i),
7507 TREE_VEC_ELT (argvec, i),
7508 UNIFY_ALLOW_NONE, explain_p))
7509 return 1;
7510
7511 if (parm_variadic_p
7512 && unify_pack_expansion (tparms, targs,
7513 parmvec, argvec,
7514 DEDUCE_EXACT,
7515 /*subr=*/true, explain_p))
7516 return 1;
7517 }
7518
7519 return 0;
7520 }
7521
7522 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7523 template template parameters. Both PARM_PARMS and ARG_PARMS are
7524 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7525 or PARM_DECL.
7526
7527 Consider the example:
7528 template <class T> class A;
7529 template<template <class U> class TT> class B;
7530
7531 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7532 the parameters to A, and OUTER_ARGS contains A. */
7533
7534 static int
7535 coerce_template_template_parms (tree parm_parms,
7536 tree arg_parms,
7537 tsubst_flags_t complain,
7538 tree in_decl,
7539 tree outer_args)
7540 {
7541 int nparms, nargs, i;
7542 tree parm, arg;
7543 int variadic_p = 0;
7544
7545 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7546 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7547
7548 nparms = TREE_VEC_LENGTH (parm_parms);
7549 nargs = TREE_VEC_LENGTH (arg_parms);
7550
7551 if (flag_new_ttp)
7552 {
7553 /* P0522R0: A template template-parameter P is at least as specialized as
7554 a template template-argument A if, given the following rewrite to two
7555 function templates, the function template corresponding to P is at
7556 least as specialized as the function template corresponding to A
7557 according to the partial ordering rules for function templates
7558 ([temp.func.order]). Given an invented class template X with the
7559 template parameter list of A (including default arguments):
7560
7561 * Each of the two function templates has the same template parameters,
7562 respectively, as P or A.
7563
7564 * Each function template has a single function parameter whose type is
7565 a specialization of X with template arguments corresponding to the
7566 template parameters from the respective function template where, for
7567 each template parameter PP in the template parameter list of the
7568 function template, a corresponding template argument AA is formed. If
7569 PP declares a parameter pack, then AA is the pack expansion
7570 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7571
7572 If the rewrite produces an invalid type, then P is not at least as
7573 specialized as A. */
7574
7575 /* So coerce P's args to apply to A's parms, and then deduce between A's
7576 args and the converted args. If that succeeds, A is at least as
7577 specialized as P, so they match.*/
7578 tree pargs = template_parms_level_to_args (parm_parms);
7579 pargs = add_outermost_template_args (outer_args, pargs);
7580 ++processing_template_decl;
7581 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7582 /*require_all*/true, /*use_default*/true);
7583 --processing_template_decl;
7584 if (pargs != error_mark_node)
7585 {
7586 tree targs = make_tree_vec (nargs);
7587 tree aargs = template_parms_level_to_args (arg_parms);
7588 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7589 /*explain*/false))
7590 return 1;
7591 }
7592 }
7593
7594 /* Determine whether we have a parameter pack at the end of the
7595 template template parameter's template parameter list. */
7596 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7597 {
7598 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7599
7600 if (error_operand_p (parm))
7601 return 0;
7602
7603 switch (TREE_CODE (parm))
7604 {
7605 case TEMPLATE_DECL:
7606 case TYPE_DECL:
7607 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7608 variadic_p = 1;
7609 break;
7610
7611 case PARM_DECL:
7612 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7613 variadic_p = 1;
7614 break;
7615
7616 default:
7617 gcc_unreachable ();
7618 }
7619 }
7620
7621 if (nargs != nparms
7622 && !(variadic_p && nargs >= nparms - 1))
7623 return 0;
7624
7625 /* Check all of the template parameters except the parameter pack at
7626 the end (if any). */
7627 for (i = 0; i < nparms - variadic_p; ++i)
7628 {
7629 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7630 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7631 continue;
7632
7633 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7634 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7635
7636 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7637 outer_args))
7638 return 0;
7639
7640 }
7641
7642 if (variadic_p)
7643 {
7644 /* Check each of the template parameters in the template
7645 argument against the template parameter pack at the end of
7646 the template template parameter. */
7647 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7648 return 0;
7649
7650 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7651
7652 for (; i < nargs; ++i)
7653 {
7654 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7655 continue;
7656
7657 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7658
7659 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7660 outer_args))
7661 return 0;
7662 }
7663 }
7664
7665 return 1;
7666 }
7667
7668 /* Verifies that the deduced template arguments (in TARGS) for the
7669 template template parameters (in TPARMS) represent valid bindings,
7670 by comparing the template parameter list of each template argument
7671 to the template parameter list of its corresponding template
7672 template parameter, in accordance with DR150. This
7673 routine can only be called after all template arguments have been
7674 deduced. It will return TRUE if all of the template template
7675 parameter bindings are okay, FALSE otherwise. */
7676 bool
7677 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7678 {
7679 int i, ntparms = TREE_VEC_LENGTH (tparms);
7680 bool ret = true;
7681
7682 /* We're dealing with template parms in this process. */
7683 ++processing_template_decl;
7684
7685 targs = INNERMOST_TEMPLATE_ARGS (targs);
7686
7687 for (i = 0; i < ntparms; ++i)
7688 {
7689 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7690 tree targ = TREE_VEC_ELT (targs, i);
7691
7692 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7693 {
7694 tree packed_args = NULL_TREE;
7695 int idx, len = 1;
7696
7697 if (ARGUMENT_PACK_P (targ))
7698 {
7699 /* Look inside the argument pack. */
7700 packed_args = ARGUMENT_PACK_ARGS (targ);
7701 len = TREE_VEC_LENGTH (packed_args);
7702 }
7703
7704 for (idx = 0; idx < len; ++idx)
7705 {
7706 tree targ_parms = NULL_TREE;
7707
7708 if (packed_args)
7709 /* Extract the next argument from the argument
7710 pack. */
7711 targ = TREE_VEC_ELT (packed_args, idx);
7712
7713 if (PACK_EXPANSION_P (targ))
7714 /* Look at the pattern of the pack expansion. */
7715 targ = PACK_EXPANSION_PATTERN (targ);
7716
7717 /* Extract the template parameters from the template
7718 argument. */
7719 if (TREE_CODE (targ) == TEMPLATE_DECL)
7720 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7721 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7722 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7723
7724 /* Verify that we can coerce the template template
7725 parameters from the template argument to the template
7726 parameter. This requires an exact match. */
7727 if (targ_parms
7728 && !coerce_template_template_parms
7729 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7730 targ_parms,
7731 tf_none,
7732 tparm,
7733 targs))
7734 {
7735 ret = false;
7736 goto out;
7737 }
7738 }
7739 }
7740 }
7741
7742 out:
7743
7744 --processing_template_decl;
7745 return ret;
7746 }
7747
7748 /* Since type attributes aren't mangled, we need to strip them from
7749 template type arguments. */
7750
7751 static tree
7752 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7753 {
7754 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7755 return arg;
7756 bool removed_attributes = false;
7757 tree canon = strip_typedefs (arg, &removed_attributes);
7758 if (removed_attributes
7759 && (complain & tf_warning))
7760 warning (OPT_Wignored_attributes,
7761 "ignoring attributes on template argument %qT", arg);
7762 return canon;
7763 }
7764
7765 /* And from inside dependent non-type arguments like sizeof(Type). */
7766
7767 static tree
7768 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7769 {
7770 if (!arg || arg == error_mark_node)
7771 return arg;
7772 bool removed_attributes = false;
7773 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7774 if (removed_attributes
7775 && (complain & tf_warning))
7776 warning (OPT_Wignored_attributes,
7777 "ignoring attributes in template argument %qE", arg);
7778 return canon;
7779 }
7780
7781 // A template declaration can be substituted for a constrained
7782 // template template parameter only when the argument is more
7783 // constrained than the parameter.
7784 static bool
7785 is_compatible_template_arg (tree parm, tree arg)
7786 {
7787 tree parm_cons = get_constraints (parm);
7788
7789 /* For now, allow constrained template template arguments
7790 and unconstrained template template parameters. */
7791 if (parm_cons == NULL_TREE)
7792 return true;
7793
7794 tree arg_cons = get_constraints (arg);
7795
7796 // If the template parameter is constrained, we need to rewrite its
7797 // constraints in terms of the ARG's template parameters. This ensures
7798 // that all of the template parameter types will have the same depth.
7799 //
7800 // Note that this is only valid when coerce_template_template_parm is
7801 // true for the innermost template parameters of PARM and ARG. In other
7802 // words, because coercion is successful, this conversion will be valid.
7803 if (parm_cons)
7804 {
7805 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7806 parm_cons = tsubst_constraint_info (parm_cons,
7807 INNERMOST_TEMPLATE_ARGS (args),
7808 tf_none, NULL_TREE);
7809 if (parm_cons == error_mark_node)
7810 return false;
7811 }
7812
7813 return subsumes (parm_cons, arg_cons);
7814 }
7815
7816 // Convert a placeholder argument into a binding to the original
7817 // parameter. The original parameter is saved as the TREE_TYPE of
7818 // ARG.
7819 static inline tree
7820 convert_wildcard_argument (tree parm, tree arg)
7821 {
7822 TREE_TYPE (arg) = parm;
7823 return arg;
7824 }
7825
7826 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7827 because one of them is dependent. But we need to represent the
7828 conversion for the benefit of cp_tree_equal. */
7829
7830 static tree
7831 maybe_convert_nontype_argument (tree type, tree arg)
7832 {
7833 /* Auto parms get no conversion. */
7834 if (type_uses_auto (type))
7835 return arg;
7836 /* We don't need or want to add this conversion now if we're going to use the
7837 argument for deduction. */
7838 if (value_dependent_expression_p (arg))
7839 return arg;
7840
7841 type = cv_unqualified (type);
7842 tree argtype = TREE_TYPE (arg);
7843 if (same_type_p (type, argtype))
7844 return arg;
7845
7846 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7847 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7848 return arg;
7849 }
7850
7851 /* Convert the indicated template ARG as necessary to match the
7852 indicated template PARM. Returns the converted ARG, or
7853 error_mark_node if the conversion was unsuccessful. Error and
7854 warning messages are issued under control of COMPLAIN. This
7855 conversion is for the Ith parameter in the parameter list. ARGS is
7856 the full set of template arguments deduced so far. */
7857
7858 static tree
7859 convert_template_argument (tree parm,
7860 tree arg,
7861 tree args,
7862 tsubst_flags_t complain,
7863 int i,
7864 tree in_decl)
7865 {
7866 tree orig_arg;
7867 tree val;
7868 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7869
7870 if (parm == error_mark_node || error_operand_p (arg))
7871 return error_mark_node;
7872
7873 /* Trivially convert placeholders. */
7874 if (TREE_CODE (arg) == WILDCARD_DECL)
7875 return convert_wildcard_argument (parm, arg);
7876
7877 if (arg == any_targ_node)
7878 return arg;
7879
7880 if (TREE_CODE (arg) == TREE_LIST
7881 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7882 {
7883 /* The template argument was the name of some
7884 member function. That's usually
7885 invalid, but static members are OK. In any
7886 case, grab the underlying fields/functions
7887 and issue an error later if required. */
7888 TREE_TYPE (arg) = unknown_type_node;
7889 }
7890
7891 orig_arg = arg;
7892
7893 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7894 requires_type = (TREE_CODE (parm) == TYPE_DECL
7895 || requires_tmpl_type);
7896
7897 /* When determining whether an argument pack expansion is a template,
7898 look at the pattern. */
7899 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7900 arg = PACK_EXPANSION_PATTERN (arg);
7901
7902 /* Deal with an injected-class-name used as a template template arg. */
7903 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7904 {
7905 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7906 if (TREE_CODE (t) == TEMPLATE_DECL)
7907 {
7908 if (cxx_dialect >= cxx11)
7909 /* OK under DR 1004. */;
7910 else if (complain & tf_warning_or_error)
7911 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7912 " used as template template argument", TYPE_NAME (arg));
7913 else if (flag_pedantic_errors)
7914 t = arg;
7915
7916 arg = t;
7917 }
7918 }
7919
7920 is_tmpl_type =
7921 ((TREE_CODE (arg) == TEMPLATE_DECL
7922 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7923 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7924 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7925 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7926
7927 if (is_tmpl_type
7928 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7929 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7930 arg = TYPE_STUB_DECL (arg);
7931
7932 is_type = TYPE_P (arg) || is_tmpl_type;
7933
7934 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7935 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7936 {
7937 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7938 {
7939 if (complain & tf_error)
7940 error ("invalid use of destructor %qE as a type", orig_arg);
7941 return error_mark_node;
7942 }
7943
7944 permerror (input_location,
7945 "to refer to a type member of a template parameter, "
7946 "use %<typename %E%>", orig_arg);
7947
7948 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7949 TREE_OPERAND (arg, 1),
7950 typename_type,
7951 complain);
7952 arg = orig_arg;
7953 is_type = 1;
7954 }
7955 if (is_type != requires_type)
7956 {
7957 if (in_decl)
7958 {
7959 if (complain & tf_error)
7960 {
7961 error ("type/value mismatch at argument %d in template "
7962 "parameter list for %qD",
7963 i + 1, in_decl);
7964 if (is_type)
7965 {
7966 /* The template argument is a type, but we're expecting
7967 an expression. */
7968 inform (input_location,
7969 " expected a constant of type %qT, got %qT",
7970 TREE_TYPE (parm),
7971 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7972 /* [temp.arg]/2: "In a template-argument, an ambiguity
7973 between a type-id and an expression is resolved to a
7974 type-id, regardless of the form of the corresponding
7975 template-parameter." So give the user a clue. */
7976 if (TREE_CODE (arg) == FUNCTION_TYPE)
7977 inform (input_location, " ambiguous template argument "
7978 "for non-type template parameter is treated as "
7979 "function type");
7980 }
7981 else if (requires_tmpl_type)
7982 inform (input_location,
7983 " expected a class template, got %qE", orig_arg);
7984 else
7985 inform (input_location,
7986 " expected a type, got %qE", orig_arg);
7987 }
7988 }
7989 return error_mark_node;
7990 }
7991 if (is_tmpl_type ^ requires_tmpl_type)
7992 {
7993 if (in_decl && (complain & tf_error))
7994 {
7995 error ("type/value mismatch at argument %d in template "
7996 "parameter list for %qD",
7997 i + 1, in_decl);
7998 if (is_tmpl_type)
7999 inform (input_location,
8000 " expected a type, got %qT", DECL_NAME (arg));
8001 else
8002 inform (input_location,
8003 " expected a class template, got %qT", orig_arg);
8004 }
8005 return error_mark_node;
8006 }
8007
8008 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8009 /* We already did the appropriate conversion when packing args. */
8010 val = orig_arg;
8011 else if (is_type)
8012 {
8013 if (requires_tmpl_type)
8014 {
8015 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8016 /* The number of argument required is not known yet.
8017 Just accept it for now. */
8018 val = orig_arg;
8019 else
8020 {
8021 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8022 tree argparm;
8023
8024 /* Strip alias templates that are equivalent to another
8025 template. */
8026 arg = get_underlying_template (arg);
8027 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8028
8029 if (coerce_template_template_parms (parmparm, argparm,
8030 complain, in_decl,
8031 args))
8032 {
8033 val = arg;
8034
8035 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8036 TEMPLATE_DECL. */
8037 if (val != error_mark_node)
8038 {
8039 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8040 val = TREE_TYPE (val);
8041 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8042 val = make_pack_expansion (val, complain);
8043 }
8044 }
8045 else
8046 {
8047 if (in_decl && (complain & tf_error))
8048 {
8049 error ("type/value mismatch at argument %d in "
8050 "template parameter list for %qD",
8051 i + 1, in_decl);
8052 inform (input_location,
8053 " expected a template of type %qD, got %qT",
8054 parm, orig_arg);
8055 }
8056
8057 val = error_mark_node;
8058 }
8059
8060 // Check that the constraints are compatible before allowing the
8061 // substitution.
8062 if (val != error_mark_node)
8063 if (!is_compatible_template_arg (parm, arg))
8064 {
8065 if (in_decl && (complain & tf_error))
8066 {
8067 error ("constraint mismatch at argument %d in "
8068 "template parameter list for %qD",
8069 i + 1, in_decl);
8070 inform (input_location, " expected %qD but got %qD",
8071 parm, arg);
8072 }
8073 val = error_mark_node;
8074 }
8075 }
8076 }
8077 else
8078 val = orig_arg;
8079 /* We only form one instance of each template specialization.
8080 Therefore, if we use a non-canonical variant (i.e., a
8081 typedef), any future messages referring to the type will use
8082 the typedef, which is confusing if those future uses do not
8083 themselves also use the typedef. */
8084 if (TYPE_P (val))
8085 val = canonicalize_type_argument (val, complain);
8086 }
8087 else
8088 {
8089 tree t = TREE_TYPE (parm);
8090
8091 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8092 > TMPL_ARGS_DEPTH (args))
8093 /* We don't have enough levels of args to do any substitution. This
8094 can happen in the context of -fnew-ttp-matching. */;
8095 else if (tree a = type_uses_auto (t))
8096 {
8097 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8098 if (t == error_mark_node)
8099 return error_mark_node;
8100 }
8101 else
8102 t = tsubst (t, args, complain, in_decl);
8103
8104 if (invalid_nontype_parm_type_p (t, complain))
8105 return error_mark_node;
8106
8107 if (t != TREE_TYPE (parm))
8108 t = canonicalize_type_argument (t, complain);
8109
8110 if (!type_dependent_expression_p (orig_arg)
8111 && !uses_template_parms (t))
8112 /* We used to call digest_init here. However, digest_init
8113 will report errors, which we don't want when complain
8114 is zero. More importantly, digest_init will try too
8115 hard to convert things: for example, `0' should not be
8116 converted to pointer type at this point according to
8117 the standard. Accepting this is not merely an
8118 extension, since deciding whether or not these
8119 conversions can occur is part of determining which
8120 function template to call, or whether a given explicit
8121 argument specification is valid. */
8122 val = convert_nontype_argument (t, orig_arg, complain);
8123 else
8124 {
8125 val = canonicalize_expr_argument (orig_arg, complain);
8126 val = maybe_convert_nontype_argument (t, val);
8127 }
8128
8129
8130 if (val == NULL_TREE)
8131 val = error_mark_node;
8132 else if (val == error_mark_node && (complain & tf_error))
8133 error ("could not convert template argument %qE from %qT to %qT",
8134 orig_arg, TREE_TYPE (orig_arg), t);
8135
8136 if (INDIRECT_REF_P (val))
8137 {
8138 /* Reject template arguments that are references to built-in
8139 functions with no library fallbacks. */
8140 const_tree inner = TREE_OPERAND (val, 0);
8141 const_tree innertype = TREE_TYPE (inner);
8142 if (innertype
8143 && TYPE_REF_P (innertype)
8144 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8145 && TREE_OPERAND_LENGTH (inner) > 0
8146 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8147 return error_mark_node;
8148 }
8149
8150 if (TREE_CODE (val) == SCOPE_REF)
8151 {
8152 /* Strip typedefs from the SCOPE_REF. */
8153 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8154 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8155 complain);
8156 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8157 QUALIFIED_NAME_IS_TEMPLATE (val));
8158 }
8159 }
8160
8161 return val;
8162 }
8163
8164 /* Coerces the remaining template arguments in INNER_ARGS (from
8165 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8166 Returns the coerced argument pack. PARM_IDX is the position of this
8167 parameter in the template parameter list. ARGS is the original
8168 template argument list. */
8169 static tree
8170 coerce_template_parameter_pack (tree parms,
8171 int parm_idx,
8172 tree args,
8173 tree inner_args,
8174 int arg_idx,
8175 tree new_args,
8176 int* lost,
8177 tree in_decl,
8178 tsubst_flags_t complain)
8179 {
8180 tree parm = TREE_VEC_ELT (parms, parm_idx);
8181 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8182 tree packed_args;
8183 tree argument_pack;
8184 tree packed_parms = NULL_TREE;
8185
8186 if (arg_idx > nargs)
8187 arg_idx = nargs;
8188
8189 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8190 {
8191 /* When the template parameter is a non-type template parameter pack
8192 or template template parameter pack whose type or template
8193 parameters use parameter packs, we know exactly how many arguments
8194 we are looking for. Build a vector of the instantiated decls for
8195 these template parameters in PACKED_PARMS. */
8196 /* We can't use make_pack_expansion here because it would interpret a
8197 _DECL as a use rather than a declaration. */
8198 tree decl = TREE_VALUE (parm);
8199 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8200 SET_PACK_EXPANSION_PATTERN (exp, decl);
8201 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8202 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8203
8204 TREE_VEC_LENGTH (args)--;
8205 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8206 TREE_VEC_LENGTH (args)++;
8207
8208 if (packed_parms == error_mark_node)
8209 return error_mark_node;
8210
8211 /* If we're doing a partial instantiation of a member template,
8212 verify that all of the types used for the non-type
8213 template parameter pack are, in fact, valid for non-type
8214 template parameters. */
8215 if (arg_idx < nargs
8216 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8217 {
8218 int j, len = TREE_VEC_LENGTH (packed_parms);
8219 for (j = 0; j < len; ++j)
8220 {
8221 tree t = TREE_VEC_ELT (packed_parms, j);
8222 if (TREE_CODE (t) == PARM_DECL
8223 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8224 return error_mark_node;
8225 }
8226 /* We don't know how many args we have yet, just
8227 use the unconverted ones for now. */
8228 return NULL_TREE;
8229 }
8230
8231 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8232 }
8233 /* Check if we have a placeholder pack, which indicates we're
8234 in the context of a introduction list. In that case we want
8235 to match this pack to the single placeholder. */
8236 else if (arg_idx < nargs
8237 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8238 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8239 {
8240 nargs = arg_idx + 1;
8241 packed_args = make_tree_vec (1);
8242 }
8243 else
8244 packed_args = make_tree_vec (nargs - arg_idx);
8245
8246 /* Convert the remaining arguments, which will be a part of the
8247 parameter pack "parm". */
8248 int first_pack_arg = arg_idx;
8249 for (; arg_idx < nargs; ++arg_idx)
8250 {
8251 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8252 tree actual_parm = TREE_VALUE (parm);
8253 int pack_idx = arg_idx - first_pack_arg;
8254
8255 if (packed_parms)
8256 {
8257 /* Once we've packed as many args as we have types, stop. */
8258 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8259 break;
8260 else if (PACK_EXPANSION_P (arg))
8261 /* We don't know how many args we have yet, just
8262 use the unconverted ones for now. */
8263 return NULL_TREE;
8264 else
8265 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8266 }
8267
8268 if (arg == error_mark_node)
8269 {
8270 if (complain & tf_error)
8271 error ("template argument %d is invalid", arg_idx + 1);
8272 }
8273 else
8274 arg = convert_template_argument (actual_parm,
8275 arg, new_args, complain, parm_idx,
8276 in_decl);
8277 if (arg == error_mark_node)
8278 (*lost)++;
8279 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8280 }
8281
8282 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8283 && TREE_VEC_LENGTH (packed_args) > 0)
8284 {
8285 if (complain & tf_error)
8286 error ("wrong number of template arguments (%d, should be %d)",
8287 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8288 return error_mark_node;
8289 }
8290
8291 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8292 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8293 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8294 else
8295 {
8296 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8297 TREE_CONSTANT (argument_pack) = 1;
8298 }
8299
8300 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8301 if (CHECKING_P)
8302 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8303 TREE_VEC_LENGTH (packed_args));
8304 return argument_pack;
8305 }
8306
8307 /* Returns the number of pack expansions in the template argument vector
8308 ARGS. */
8309
8310 static int
8311 pack_expansion_args_count (tree args)
8312 {
8313 int i;
8314 int count = 0;
8315 if (args)
8316 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8317 {
8318 tree elt = TREE_VEC_ELT (args, i);
8319 if (elt && PACK_EXPANSION_P (elt))
8320 ++count;
8321 }
8322 return count;
8323 }
8324
8325 /* Convert all template arguments to their appropriate types, and
8326 return a vector containing the innermost resulting template
8327 arguments. If any error occurs, return error_mark_node. Error and
8328 warning messages are issued under control of COMPLAIN.
8329
8330 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8331 for arguments not specified in ARGS. Otherwise, if
8332 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8333 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8334 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8335 ARGS. */
8336
8337 static tree
8338 coerce_template_parms (tree parms,
8339 tree args,
8340 tree in_decl,
8341 tsubst_flags_t complain,
8342 bool require_all_args,
8343 bool use_default_args)
8344 {
8345 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8346 tree orig_inner_args;
8347 tree inner_args;
8348 tree new_args;
8349 tree new_inner_args;
8350
8351 /* When used as a boolean value, indicates whether this is a
8352 variadic template parameter list. Since it's an int, we can also
8353 subtract it from nparms to get the number of non-variadic
8354 parameters. */
8355 int variadic_p = 0;
8356 int variadic_args_p = 0;
8357 int post_variadic_parms = 0;
8358
8359 /* Adjustment to nparms for fixed parameter packs. */
8360 int fixed_pack_adjust = 0;
8361 int fixed_packs = 0;
8362 int missing = 0;
8363
8364 /* Likewise for parameters with default arguments. */
8365 int default_p = 0;
8366
8367 if (args == error_mark_node)
8368 return error_mark_node;
8369
8370 nparms = TREE_VEC_LENGTH (parms);
8371
8372 /* Determine if there are any parameter packs or default arguments. */
8373 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8374 {
8375 tree parm = TREE_VEC_ELT (parms, parm_idx);
8376 if (variadic_p)
8377 ++post_variadic_parms;
8378 if (template_parameter_pack_p (TREE_VALUE (parm)))
8379 ++variadic_p;
8380 if (TREE_PURPOSE (parm))
8381 ++default_p;
8382 }
8383
8384 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8385 /* If there are no parameters that follow a parameter pack, we need to
8386 expand any argument packs so that we can deduce a parameter pack from
8387 some non-packed args followed by an argument pack, as in variadic85.C.
8388 If there are such parameters, we need to leave argument packs intact
8389 so the arguments are assigned properly. This can happen when dealing
8390 with a nested class inside a partial specialization of a class
8391 template, as in variadic92.C, or when deducing a template parameter pack
8392 from a sub-declarator, as in variadic114.C. */
8393 if (!post_variadic_parms)
8394 inner_args = expand_template_argument_pack (inner_args);
8395
8396 /* Count any pack expansion args. */
8397 variadic_args_p = pack_expansion_args_count (inner_args);
8398
8399 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8400 if ((nargs - variadic_args_p > nparms && !variadic_p)
8401 || (nargs < nparms - variadic_p
8402 && require_all_args
8403 && !variadic_args_p
8404 && (!use_default_args
8405 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8406 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8407 {
8408 bad_nargs:
8409 if (complain & tf_error)
8410 {
8411 if (variadic_p || default_p)
8412 {
8413 nparms -= variadic_p + default_p;
8414 error ("wrong number of template arguments "
8415 "(%d, should be at least %d)", nargs, nparms);
8416 }
8417 else
8418 error ("wrong number of template arguments "
8419 "(%d, should be %d)", nargs, nparms);
8420
8421 if (in_decl)
8422 inform (DECL_SOURCE_LOCATION (in_decl),
8423 "provided for %qD", in_decl);
8424 }
8425
8426 return error_mark_node;
8427 }
8428 /* We can't pass a pack expansion to a non-pack parameter of an alias
8429 template (DR 1430). */
8430 else if (in_decl
8431 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8432 || concept_template_p (in_decl))
8433 && variadic_args_p
8434 && nargs - variadic_args_p < nparms - variadic_p)
8435 {
8436 if (complain & tf_error)
8437 {
8438 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8439 {
8440 tree arg = TREE_VEC_ELT (inner_args, i);
8441 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8442
8443 if (PACK_EXPANSION_P (arg)
8444 && !template_parameter_pack_p (parm))
8445 {
8446 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8447 error_at (location_of (arg),
8448 "pack expansion argument for non-pack parameter "
8449 "%qD of alias template %qD", parm, in_decl);
8450 else
8451 error_at (location_of (arg),
8452 "pack expansion argument for non-pack parameter "
8453 "%qD of concept %qD", parm, in_decl);
8454 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8455 goto found;
8456 }
8457 }
8458 gcc_unreachable ();
8459 found:;
8460 }
8461 return error_mark_node;
8462 }
8463
8464 /* We need to evaluate the template arguments, even though this
8465 template-id may be nested within a "sizeof". */
8466 cp_evaluated ev;
8467
8468 new_inner_args = make_tree_vec (nparms);
8469 new_args = add_outermost_template_args (args, new_inner_args);
8470 int pack_adjust = 0;
8471 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8472 {
8473 tree arg;
8474 tree parm;
8475
8476 /* Get the Ith template parameter. */
8477 parm = TREE_VEC_ELT (parms, parm_idx);
8478
8479 if (parm == error_mark_node)
8480 {
8481 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8482 continue;
8483 }
8484
8485 /* Calculate the next argument. */
8486 if (arg_idx < nargs)
8487 arg = TREE_VEC_ELT (inner_args, arg_idx);
8488 else
8489 arg = NULL_TREE;
8490
8491 if (template_parameter_pack_p (TREE_VALUE (parm))
8492 && (arg || require_all_args || !(complain & tf_partial))
8493 && !(arg && ARGUMENT_PACK_P (arg)))
8494 {
8495 /* Some arguments will be placed in the
8496 template parameter pack PARM. */
8497 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8498 inner_args, arg_idx,
8499 new_args, &lost,
8500 in_decl, complain);
8501
8502 if (arg == NULL_TREE)
8503 {
8504 /* We don't know how many args we have yet, just use the
8505 unconverted (and still packed) ones for now. */
8506 new_inner_args = orig_inner_args;
8507 arg_idx = nargs;
8508 break;
8509 }
8510
8511 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8512
8513 /* Store this argument. */
8514 if (arg == error_mark_node)
8515 {
8516 lost++;
8517 /* We are done with all of the arguments. */
8518 arg_idx = nargs;
8519 break;
8520 }
8521 else
8522 {
8523 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8524 arg_idx += pack_adjust;
8525 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8526 {
8527 ++fixed_packs;
8528 fixed_pack_adjust += pack_adjust;
8529 }
8530 }
8531
8532 continue;
8533 }
8534 else if (arg)
8535 {
8536 if (PACK_EXPANSION_P (arg))
8537 {
8538 /* "If every valid specialization of a variadic template
8539 requires an empty template parameter pack, the template is
8540 ill-formed, no diagnostic required." So check that the
8541 pattern works with this parameter. */
8542 tree pattern = PACK_EXPANSION_PATTERN (arg);
8543 tree conv = convert_template_argument (TREE_VALUE (parm),
8544 pattern, new_args,
8545 complain, parm_idx,
8546 in_decl);
8547 if (conv == error_mark_node)
8548 {
8549 if (complain & tf_error)
8550 inform (input_location, "so any instantiation with a "
8551 "non-empty parameter pack would be ill-formed");
8552 ++lost;
8553 }
8554 else if (TYPE_P (conv) && !TYPE_P (pattern))
8555 /* Recover from missing typename. */
8556 TREE_VEC_ELT (inner_args, arg_idx)
8557 = make_pack_expansion (conv, complain);
8558
8559 /* We don't know how many args we have yet, just
8560 use the unconverted ones for now. */
8561 new_inner_args = inner_args;
8562 arg_idx = nargs;
8563 break;
8564 }
8565 }
8566 else if (require_all_args)
8567 {
8568 /* There must be a default arg in this case. */
8569 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8570 complain, in_decl);
8571 /* The position of the first default template argument,
8572 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8573 Record that. */
8574 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8575 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8576 arg_idx - pack_adjust);
8577 }
8578 else
8579 break;
8580
8581 if (arg == error_mark_node)
8582 {
8583 if (complain & tf_error)
8584 error ("template argument %d is invalid", arg_idx + 1);
8585 }
8586 else if (!arg)
8587 {
8588 /* This can occur if there was an error in the template
8589 parameter list itself (which we would already have
8590 reported) that we are trying to recover from, e.g., a class
8591 template with a parameter list such as
8592 template<typename..., typename> (cpp0x/variadic150.C). */
8593 ++lost;
8594
8595 /* This can also happen with a fixed parameter pack (71834). */
8596 if (arg_idx >= nargs)
8597 ++missing;
8598 }
8599 else
8600 arg = convert_template_argument (TREE_VALUE (parm),
8601 arg, new_args, complain,
8602 parm_idx, in_decl);
8603
8604 if (arg == error_mark_node)
8605 lost++;
8606 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8607 }
8608
8609 if (missing || arg_idx < nargs - variadic_args_p)
8610 {
8611 /* If we had fixed parameter packs, we didn't know how many arguments we
8612 actually needed earlier; now we do. */
8613 nparms += fixed_pack_adjust;
8614 variadic_p -= fixed_packs;
8615 goto bad_nargs;
8616 }
8617
8618 if (arg_idx < nargs)
8619 {
8620 /* We had some pack expansion arguments that will only work if the packs
8621 are empty, but wait until instantiation time to complain.
8622 See variadic-ttp3.C. */
8623 int len = nparms + (nargs - arg_idx);
8624 tree args = make_tree_vec (len);
8625 int i = 0;
8626 for (; i < nparms; ++i)
8627 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8628 for (; i < len; ++i, ++arg_idx)
8629 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8630 arg_idx - pack_adjust);
8631 new_inner_args = args;
8632 }
8633
8634 if (lost)
8635 {
8636 gcc_assert (!(complain & tf_error) || seen_error ());
8637 return error_mark_node;
8638 }
8639
8640 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8641 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8642 TREE_VEC_LENGTH (new_inner_args));
8643
8644 return new_inner_args;
8645 }
8646
8647 /* Convert all template arguments to their appropriate types, and
8648 return a vector containing the innermost resulting template
8649 arguments. If any error occurs, return error_mark_node. Error and
8650 warning messages are not issued.
8651
8652 Note that no function argument deduction is performed, and default
8653 arguments are used to fill in unspecified arguments. */
8654 tree
8655 coerce_template_parms (tree parms, tree args, tree in_decl)
8656 {
8657 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8658 }
8659
8660 /* Convert all template arguments to their appropriate type, and
8661 instantiate default arguments as needed. This returns a vector
8662 containing the innermost resulting template arguments, or
8663 error_mark_node if unsuccessful. */
8664 tree
8665 coerce_template_parms (tree parms, tree args, tree in_decl,
8666 tsubst_flags_t complain)
8667 {
8668 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8669 }
8670
8671 /* Like coerce_template_parms. If PARMS represents all template
8672 parameters levels, this function returns a vector of vectors
8673 representing all the resulting argument levels. Note that in this
8674 case, only the innermost arguments are coerced because the
8675 outermost ones are supposed to have been coerced already.
8676
8677 Otherwise, if PARMS represents only (the innermost) vector of
8678 parameters, this function returns a vector containing just the
8679 innermost resulting arguments. */
8680
8681 static tree
8682 coerce_innermost_template_parms (tree parms,
8683 tree args,
8684 tree in_decl,
8685 tsubst_flags_t complain,
8686 bool require_all_args,
8687 bool use_default_args)
8688 {
8689 int parms_depth = TMPL_PARMS_DEPTH (parms);
8690 int args_depth = TMPL_ARGS_DEPTH (args);
8691 tree coerced_args;
8692
8693 if (parms_depth > 1)
8694 {
8695 coerced_args = make_tree_vec (parms_depth);
8696 tree level;
8697 int cur_depth;
8698
8699 for (level = parms, cur_depth = parms_depth;
8700 parms_depth > 0 && level != NULL_TREE;
8701 level = TREE_CHAIN (level), --cur_depth)
8702 {
8703 tree l;
8704 if (cur_depth == args_depth)
8705 l = coerce_template_parms (TREE_VALUE (level),
8706 args, in_decl, complain,
8707 require_all_args,
8708 use_default_args);
8709 else
8710 l = TMPL_ARGS_LEVEL (args, cur_depth);
8711
8712 if (l == error_mark_node)
8713 return error_mark_node;
8714
8715 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8716 }
8717 }
8718 else
8719 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8720 args, in_decl, complain,
8721 require_all_args,
8722 use_default_args);
8723 return coerced_args;
8724 }
8725
8726 /* Returns 1 if template args OT and NT are equivalent. */
8727
8728 int
8729 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8730 {
8731 if (nt == ot)
8732 return 1;
8733 if (nt == NULL_TREE || ot == NULL_TREE)
8734 return false;
8735 if (nt == any_targ_node || ot == any_targ_node)
8736 return true;
8737
8738 if (TREE_CODE (nt) == TREE_VEC)
8739 /* For member templates */
8740 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8741 else if (PACK_EXPANSION_P (ot))
8742 return (PACK_EXPANSION_P (nt)
8743 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8744 PACK_EXPANSION_PATTERN (nt))
8745 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8746 PACK_EXPANSION_EXTRA_ARGS (nt)));
8747 else if (ARGUMENT_PACK_P (ot))
8748 {
8749 int i, len;
8750 tree opack, npack;
8751
8752 if (!ARGUMENT_PACK_P (nt))
8753 return 0;
8754
8755 opack = ARGUMENT_PACK_ARGS (ot);
8756 npack = ARGUMENT_PACK_ARGS (nt);
8757 len = TREE_VEC_LENGTH (opack);
8758 if (TREE_VEC_LENGTH (npack) != len)
8759 return 0;
8760 for (i = 0; i < len; ++i)
8761 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8762 TREE_VEC_ELT (npack, i)))
8763 return 0;
8764 return 1;
8765 }
8766 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8767 gcc_unreachable ();
8768 else if (TYPE_P (nt))
8769 {
8770 if (!TYPE_P (ot))
8771 return false;
8772 /* Don't treat an alias template specialization with dependent
8773 arguments as equivalent to its underlying type when used as a
8774 template argument; we need them to be distinct so that we
8775 substitute into the specialization arguments at instantiation
8776 time. And aliases can't be equivalent without being ==, so
8777 we don't need to look any deeper.
8778
8779 During partial ordering, however, we need to treat them normally so
8780 that we can order uses of the same alias with different
8781 cv-qualification (79960). */
8782 if (!partial_order
8783 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8784 return false;
8785 else
8786 return same_type_p (ot, nt);
8787 }
8788 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8789 return 0;
8790 else
8791 {
8792 /* Try to treat a template non-type argument that has been converted
8793 to the parameter type as equivalent to one that hasn't yet. */
8794 for (enum tree_code code1 = TREE_CODE (ot);
8795 CONVERT_EXPR_CODE_P (code1)
8796 || code1 == NON_LVALUE_EXPR;
8797 code1 = TREE_CODE (ot))
8798 ot = TREE_OPERAND (ot, 0);
8799 for (enum tree_code code2 = TREE_CODE (nt);
8800 CONVERT_EXPR_CODE_P (code2)
8801 || code2 == NON_LVALUE_EXPR;
8802 code2 = TREE_CODE (nt))
8803 nt = TREE_OPERAND (nt, 0);
8804
8805 return cp_tree_equal (ot, nt);
8806 }
8807 }
8808
8809 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8810 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8811 NEWARG_PTR with the offending arguments if they are non-NULL. */
8812
8813 int
8814 comp_template_args (tree oldargs, tree newargs,
8815 tree *oldarg_ptr, tree *newarg_ptr,
8816 bool partial_order)
8817 {
8818 int i;
8819
8820 if (oldargs == newargs)
8821 return 1;
8822
8823 if (!oldargs || !newargs)
8824 return 0;
8825
8826 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8827 return 0;
8828
8829 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8830 {
8831 tree nt = TREE_VEC_ELT (newargs, i);
8832 tree ot = TREE_VEC_ELT (oldargs, i);
8833
8834 if (! template_args_equal (ot, nt, partial_order))
8835 {
8836 if (oldarg_ptr != NULL)
8837 *oldarg_ptr = ot;
8838 if (newarg_ptr != NULL)
8839 *newarg_ptr = nt;
8840 return 0;
8841 }
8842 }
8843 return 1;
8844 }
8845
8846 inline bool
8847 comp_template_args_porder (tree oargs, tree nargs)
8848 {
8849 return comp_template_args (oargs, nargs, NULL, NULL, true);
8850 }
8851
8852 /* Implement a freelist interface for objects of type T.
8853
8854 Head is a separate object, rather than a regular member, so that we
8855 can define it as a GTY deletable pointer, which is highly
8856 desirable. A data member could be declared that way, but then the
8857 containing object would implicitly get GTY((user)), which would
8858 prevent us from instantiating freelists as global objects.
8859 Although this way we can create freelist global objects, they're
8860 such thin wrappers that instantiating temporaries at every use
8861 loses nothing and saves permanent storage for the freelist object.
8862
8863 Member functions next, anew, poison and reinit have default
8864 implementations that work for most of the types we're interested
8865 in, but if they don't work for some type, they should be explicitly
8866 specialized. See the comments before them for requirements, and
8867 the example specializations for the tree_list_freelist. */
8868 template <typename T>
8869 class freelist
8870 {
8871 /* Return the next object in a chain. We could just do type
8872 punning, but if we access the object with its underlying type, we
8873 avoid strict-aliasing trouble. This needs only work between
8874 poison and reinit. */
8875 static T *&next (T *obj) { return obj->next; }
8876
8877 /* Return a newly allocated, uninitialized or minimally-initialized
8878 object of type T. Any initialization performed by anew should
8879 either remain across the life of the object and the execution of
8880 poison, or be redone by reinit. */
8881 static T *anew () { return ggc_alloc<T> (); }
8882
8883 /* Optionally scribble all over the bits holding the object, so that
8884 they become (mostly?) uninitialized memory. This is called while
8885 preparing to make the object part of the free list. */
8886 static void poison (T *obj) {
8887 T *p ATTRIBUTE_UNUSED = obj;
8888 T **q ATTRIBUTE_UNUSED = &next (obj);
8889
8890 #ifdef ENABLE_GC_CHECKING
8891 /* Poison the data, to indicate the data is garbage. */
8892 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8893 memset (p, 0xa5, sizeof (*p));
8894 #endif
8895 /* Let valgrind know the object is free. */
8896 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8897
8898 /* Let valgrind know the next portion of the object is available,
8899 but uninitialized. */
8900 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8901 }
8902
8903 /* Bring an object that underwent at least one lifecycle after anew
8904 and before the most recent free and poison, back to a usable
8905 state, reinitializing whatever is needed for it to be
8906 functionally equivalent to an object just allocated and returned
8907 by anew. This may poison or clear the next field, used by
8908 freelist housekeeping after poison was called. */
8909 static void reinit (T *obj) {
8910 T **q ATTRIBUTE_UNUSED = &next (obj);
8911
8912 #ifdef ENABLE_GC_CHECKING
8913 memset (q, 0xa5, sizeof (*q));
8914 #endif
8915 /* Let valgrind know the entire object is available, but
8916 uninitialized. */
8917 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8918 }
8919
8920 /* Reference a GTY-deletable pointer that points to the first object
8921 in the free list proper. */
8922 T *&head;
8923 public:
8924 /* Construct a freelist object chaining objects off of HEAD. */
8925 freelist (T *&head) : head(head) {}
8926
8927 /* Add OBJ to the free object list. The former head becomes OBJ's
8928 successor. */
8929 void free (T *obj)
8930 {
8931 poison (obj);
8932 next (obj) = head;
8933 head = obj;
8934 }
8935
8936 /* Take an object from the free list, if one is available, or
8937 allocate a new one. Objects taken from the free list should be
8938 regarded as filled with garbage, except for bits that are
8939 configured to be preserved across free and alloc. */
8940 T *alloc ()
8941 {
8942 if (head)
8943 {
8944 T *obj = head;
8945 head = next (head);
8946 reinit (obj);
8947 return obj;
8948 }
8949 else
8950 return anew ();
8951 }
8952 };
8953
8954 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8955 want to allocate a TREE_LIST using the usual interface, and ensure
8956 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8957 build_tree_list logic in reinit, so this could go out of sync. */
8958 template <>
8959 inline tree &
8960 freelist<tree_node>::next (tree obj)
8961 {
8962 return TREE_CHAIN (obj);
8963 }
8964 template <>
8965 inline tree
8966 freelist<tree_node>::anew ()
8967 {
8968 return build_tree_list (NULL, NULL);
8969 }
8970 template <>
8971 inline void
8972 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8973 {
8974 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8975 tree p ATTRIBUTE_UNUSED = obj;
8976 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8977 tree *q ATTRIBUTE_UNUSED = &next (obj);
8978
8979 #ifdef ENABLE_GC_CHECKING
8980 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8981
8982 /* Poison the data, to indicate the data is garbage. */
8983 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8984 memset (p, 0xa5, size);
8985 #endif
8986 /* Let valgrind know the object is free. */
8987 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8988 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8989 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8990 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8991
8992 #ifdef ENABLE_GC_CHECKING
8993 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8994 /* Keep TREE_CHAIN functional. */
8995 TREE_SET_CODE (obj, TREE_LIST);
8996 #else
8997 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8998 #endif
8999 }
9000 template <>
9001 inline void
9002 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9003 {
9004 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9005
9006 #ifdef ENABLE_GC_CHECKING
9007 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9008 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9009 memset (obj, 0, sizeof (tree_list));
9010 #endif
9011
9012 /* Let valgrind know the entire object is available, but
9013 uninitialized. */
9014 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9015
9016 #ifdef ENABLE_GC_CHECKING
9017 TREE_SET_CODE (obj, TREE_LIST);
9018 #else
9019 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9020 #endif
9021 }
9022
9023 /* Point to the first object in the TREE_LIST freelist. */
9024 static GTY((deletable)) tree tree_list_freelist_head;
9025 /* Return the/an actual TREE_LIST freelist. */
9026 static inline freelist<tree_node>
9027 tree_list_freelist ()
9028 {
9029 return tree_list_freelist_head;
9030 }
9031
9032 /* Point to the first object in the tinst_level freelist. */
9033 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9034 /* Return the/an actual tinst_level freelist. */
9035 static inline freelist<tinst_level>
9036 tinst_level_freelist ()
9037 {
9038 return tinst_level_freelist_head;
9039 }
9040
9041 /* Point to the first object in the pending_template freelist. */
9042 static GTY((deletable)) pending_template *pending_template_freelist_head;
9043 /* Return the/an actual pending_template freelist. */
9044 static inline freelist<pending_template>
9045 pending_template_freelist ()
9046 {
9047 return pending_template_freelist_head;
9048 }
9049
9050 /* Build the TREE_LIST object out of a split list, store it
9051 permanently, and return it. */
9052 tree
9053 tinst_level::to_list ()
9054 {
9055 gcc_assert (split_list_p ());
9056 tree ret = tree_list_freelist ().alloc ();
9057 TREE_PURPOSE (ret) = tldcl;
9058 TREE_VALUE (ret) = targs;
9059 tldcl = ret;
9060 targs = NULL;
9061 gcc_assert (tree_list_p ());
9062 return ret;
9063 }
9064
9065 const unsigned short tinst_level::refcount_infinity;
9066
9067 /* Increment OBJ's refcount unless it is already infinite. */
9068 static tinst_level *
9069 inc_refcount_use (tinst_level *obj)
9070 {
9071 if (obj && obj->refcount != tinst_level::refcount_infinity)
9072 ++obj->refcount;
9073 return obj;
9074 }
9075
9076 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9077 void
9078 tinst_level::free (tinst_level *obj)
9079 {
9080 if (obj->tree_list_p ())
9081 tree_list_freelist ().free (obj->get_node ());
9082 tinst_level_freelist ().free (obj);
9083 }
9084
9085 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9086 OBJ's DECL and OBJ, and start over with the tinst_level object that
9087 used to be referenced by OBJ's NEXT. */
9088 static void
9089 dec_refcount_use (tinst_level *obj)
9090 {
9091 while (obj
9092 && obj->refcount != tinst_level::refcount_infinity
9093 && !--obj->refcount)
9094 {
9095 tinst_level *next = obj->next;
9096 tinst_level::free (obj);
9097 obj = next;
9098 }
9099 }
9100
9101 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9102 and of the former PTR. Omitting the second argument is equivalent
9103 to passing (T*)NULL; this is allowed because passing the
9104 zero-valued integral constant NULL confuses type deduction and/or
9105 overload resolution. */
9106 template <typename T>
9107 static void
9108 set_refcount_ptr (T *& ptr, T *obj = NULL)
9109 {
9110 T *save = ptr;
9111 ptr = inc_refcount_use (obj);
9112 dec_refcount_use (save);
9113 }
9114
9115 static void
9116 add_pending_template (tree d)
9117 {
9118 tree ti = (TYPE_P (d)
9119 ? CLASSTYPE_TEMPLATE_INFO (d)
9120 : DECL_TEMPLATE_INFO (d));
9121 struct pending_template *pt;
9122 int level;
9123
9124 if (TI_PENDING_TEMPLATE_FLAG (ti))
9125 return;
9126
9127 /* We are called both from instantiate_decl, where we've already had a
9128 tinst_level pushed, and instantiate_template, where we haven't.
9129 Compensate. */
9130 gcc_assert (TREE_CODE (d) != TREE_LIST);
9131 level = !current_tinst_level
9132 || current_tinst_level->maybe_get_node () != d;
9133
9134 if (level)
9135 push_tinst_level (d);
9136
9137 pt = pending_template_freelist ().alloc ();
9138 pt->next = NULL;
9139 pt->tinst = NULL;
9140 set_refcount_ptr (pt->tinst, current_tinst_level);
9141 if (last_pending_template)
9142 last_pending_template->next = pt;
9143 else
9144 pending_templates = pt;
9145
9146 last_pending_template = pt;
9147
9148 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9149
9150 if (level)
9151 pop_tinst_level ();
9152 }
9153
9154
9155 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9156 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9157 documentation for TEMPLATE_ID_EXPR. */
9158
9159 tree
9160 lookup_template_function (tree fns, tree arglist)
9161 {
9162 if (fns == error_mark_node || arglist == error_mark_node)
9163 return error_mark_node;
9164
9165 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9166
9167 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9168 {
9169 error ("%q#D is not a function template", fns);
9170 return error_mark_node;
9171 }
9172
9173 if (BASELINK_P (fns))
9174 {
9175 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9176 unknown_type_node,
9177 BASELINK_FUNCTIONS (fns),
9178 arglist);
9179 return fns;
9180 }
9181
9182 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9183 }
9184
9185 /* Within the scope of a template class S<T>, the name S gets bound
9186 (in build_self_reference) to a TYPE_DECL for the class, not a
9187 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9188 or one of its enclosing classes, and that type is a template,
9189 return the associated TEMPLATE_DECL. Otherwise, the original
9190 DECL is returned.
9191
9192 Also handle the case when DECL is a TREE_LIST of ambiguous
9193 injected-class-names from different bases. */
9194
9195 tree
9196 maybe_get_template_decl_from_type_decl (tree decl)
9197 {
9198 if (decl == NULL_TREE)
9199 return decl;
9200
9201 /* DR 176: A lookup that finds an injected-class-name (10.2
9202 [class.member.lookup]) can result in an ambiguity in certain cases
9203 (for example, if it is found in more than one base class). If all of
9204 the injected-class-names that are found refer to specializations of
9205 the same class template, and if the name is followed by a
9206 template-argument-list, the reference refers to the class template
9207 itself and not a specialization thereof, and is not ambiguous. */
9208 if (TREE_CODE (decl) == TREE_LIST)
9209 {
9210 tree t, tmpl = NULL_TREE;
9211 for (t = decl; t; t = TREE_CHAIN (t))
9212 {
9213 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9214 if (!tmpl)
9215 tmpl = elt;
9216 else if (tmpl != elt)
9217 break;
9218 }
9219 if (tmpl && t == NULL_TREE)
9220 return tmpl;
9221 else
9222 return decl;
9223 }
9224
9225 return (decl != NULL_TREE
9226 && DECL_SELF_REFERENCE_P (decl)
9227 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9228 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9229 }
9230
9231 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9232 parameters, find the desired type.
9233
9234 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9235
9236 IN_DECL, if non-NULL, is the template declaration we are trying to
9237 instantiate.
9238
9239 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9240 the class we are looking up.
9241
9242 Issue error and warning messages under control of COMPLAIN.
9243
9244 If the template class is really a local class in a template
9245 function, then the FUNCTION_CONTEXT is the function in which it is
9246 being instantiated.
9247
9248 ??? Note that this function is currently called *twice* for each
9249 template-id: the first time from the parser, while creating the
9250 incomplete type (finish_template_type), and the second type during the
9251 real instantiation (instantiate_template_class). This is surely something
9252 that we want to avoid. It also causes some problems with argument
9253 coercion (see convert_nontype_argument for more information on this). */
9254
9255 static tree
9256 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9257 int entering_scope, tsubst_flags_t complain)
9258 {
9259 tree templ = NULL_TREE, parmlist;
9260 tree t;
9261 spec_entry **slot;
9262 spec_entry *entry;
9263 spec_entry elt;
9264 hashval_t hash;
9265
9266 if (identifier_p (d1))
9267 {
9268 tree value = innermost_non_namespace_value (d1);
9269 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9270 templ = value;
9271 else
9272 {
9273 if (context)
9274 push_decl_namespace (context);
9275 templ = lookup_name (d1);
9276 templ = maybe_get_template_decl_from_type_decl (templ);
9277 if (context)
9278 pop_decl_namespace ();
9279 }
9280 if (templ)
9281 context = DECL_CONTEXT (templ);
9282 }
9283 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9284 {
9285 tree type = TREE_TYPE (d1);
9286
9287 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9288 an implicit typename for the second A. Deal with it. */
9289 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9290 type = TREE_TYPE (type);
9291
9292 if (CLASSTYPE_TEMPLATE_INFO (type))
9293 {
9294 templ = CLASSTYPE_TI_TEMPLATE (type);
9295 d1 = DECL_NAME (templ);
9296 }
9297 }
9298 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9299 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9300 {
9301 templ = TYPE_TI_TEMPLATE (d1);
9302 d1 = DECL_NAME (templ);
9303 }
9304 else if (DECL_TYPE_TEMPLATE_P (d1))
9305 {
9306 templ = d1;
9307 d1 = DECL_NAME (templ);
9308 context = DECL_CONTEXT (templ);
9309 }
9310 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9311 {
9312 templ = d1;
9313 d1 = DECL_NAME (templ);
9314 }
9315
9316 /* Issue an error message if we didn't find a template. */
9317 if (! templ)
9318 {
9319 if (complain & tf_error)
9320 error ("%qT is not a template", d1);
9321 return error_mark_node;
9322 }
9323
9324 if (TREE_CODE (templ) != TEMPLATE_DECL
9325 /* Make sure it's a user visible template, if it was named by
9326 the user. */
9327 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9328 && !PRIMARY_TEMPLATE_P (templ)))
9329 {
9330 if (complain & tf_error)
9331 {
9332 error ("non-template type %qT used as a template", d1);
9333 if (in_decl)
9334 error ("for template declaration %q+D", in_decl);
9335 }
9336 return error_mark_node;
9337 }
9338
9339 complain &= ~tf_user;
9340
9341 /* An alias that just changes the name of a template is equivalent to the
9342 other template, so if any of the arguments are pack expansions, strip
9343 the alias to avoid problems with a pack expansion passed to a non-pack
9344 alias template parameter (DR 1430). */
9345 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9346 templ = get_underlying_template (templ);
9347
9348 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9349 {
9350 tree parm;
9351 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9352 if (arglist2 == error_mark_node
9353 || (!uses_template_parms (arglist2)
9354 && check_instantiated_args (templ, arglist2, complain)))
9355 return error_mark_node;
9356
9357 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9358 return parm;
9359 }
9360 else
9361 {
9362 tree template_type = TREE_TYPE (templ);
9363 tree gen_tmpl;
9364 tree type_decl;
9365 tree found = NULL_TREE;
9366 int arg_depth;
9367 int parm_depth;
9368 int is_dependent_type;
9369 int use_partial_inst_tmpl = false;
9370
9371 if (template_type == error_mark_node)
9372 /* An error occurred while building the template TEMPL, and a
9373 diagnostic has most certainly been emitted for that
9374 already. Let's propagate that error. */
9375 return error_mark_node;
9376
9377 gen_tmpl = most_general_template (templ);
9378 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9379 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9380 arg_depth = TMPL_ARGS_DEPTH (arglist);
9381
9382 if (arg_depth == 1 && parm_depth > 1)
9383 {
9384 /* We've been given an incomplete set of template arguments.
9385 For example, given:
9386
9387 template <class T> struct S1 {
9388 template <class U> struct S2 {};
9389 template <class U> struct S2<U*> {};
9390 };
9391
9392 we will be called with an ARGLIST of `U*', but the
9393 TEMPLATE will be `template <class T> template
9394 <class U> struct S1<T>::S2'. We must fill in the missing
9395 arguments. */
9396 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9397 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9398 arg_depth = TMPL_ARGS_DEPTH (arglist);
9399 }
9400
9401 /* Now we should have enough arguments. */
9402 gcc_assert (parm_depth == arg_depth);
9403
9404 /* From here on, we're only interested in the most general
9405 template. */
9406
9407 /* Calculate the BOUND_ARGS. These will be the args that are
9408 actually tsubst'd into the definition to create the
9409 instantiation. */
9410 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9411 complain,
9412 /*require_all_args=*/true,
9413 /*use_default_args=*/true);
9414
9415 if (arglist == error_mark_node)
9416 /* We were unable to bind the arguments. */
9417 return error_mark_node;
9418
9419 /* In the scope of a template class, explicit references to the
9420 template class refer to the type of the template, not any
9421 instantiation of it. For example, in:
9422
9423 template <class T> class C { void f(C<T>); }
9424
9425 the `C<T>' is just the same as `C'. Outside of the
9426 class, however, such a reference is an instantiation. */
9427 if (entering_scope
9428 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9429 || currently_open_class (template_type))
9430 {
9431 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9432
9433 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9434 return template_type;
9435 }
9436
9437 /* If we already have this specialization, return it. */
9438 elt.tmpl = gen_tmpl;
9439 elt.args = arglist;
9440 elt.spec = NULL_TREE;
9441 hash = spec_hasher::hash (&elt);
9442 entry = type_specializations->find_with_hash (&elt, hash);
9443
9444 if (entry)
9445 return entry->spec;
9446
9447 /* If the the template's constraints are not satisfied,
9448 then we cannot form a valid type.
9449
9450 Note that the check is deferred until after the hash
9451 lookup. This prevents redundant checks on previously
9452 instantiated specializations. */
9453 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9454 {
9455 if (complain & tf_error)
9456 {
9457 auto_diagnostic_group d;
9458 error ("template constraint failure");
9459 diagnose_constraints (input_location, gen_tmpl, arglist);
9460 }
9461 return error_mark_node;
9462 }
9463
9464 is_dependent_type = uses_template_parms (arglist);
9465
9466 /* If the deduced arguments are invalid, then the binding
9467 failed. */
9468 if (!is_dependent_type
9469 && check_instantiated_args (gen_tmpl,
9470 INNERMOST_TEMPLATE_ARGS (arglist),
9471 complain))
9472 return error_mark_node;
9473
9474 if (!is_dependent_type
9475 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9476 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9477 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9478 {
9479 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9480 DECL_NAME (gen_tmpl),
9481 /*tag_scope=*/ts_global);
9482 return found;
9483 }
9484
9485 context = DECL_CONTEXT (gen_tmpl);
9486 if (context && TYPE_P (context))
9487 {
9488 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9489 context = complete_type (context);
9490 }
9491 else
9492 context = tsubst (context, arglist, complain, in_decl);
9493
9494 if (context == error_mark_node)
9495 return error_mark_node;
9496
9497 if (!context)
9498 context = global_namespace;
9499
9500 /* Create the type. */
9501 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9502 {
9503 /* The user referred to a specialization of an alias
9504 template represented by GEN_TMPL.
9505
9506 [temp.alias]/2 says:
9507
9508 When a template-id refers to the specialization of an
9509 alias template, it is equivalent to the associated
9510 type obtained by substitution of its
9511 template-arguments for the template-parameters in the
9512 type-id of the alias template. */
9513
9514 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9515 /* Note that the call above (by indirectly calling
9516 register_specialization in tsubst_decl) registers the
9517 TYPE_DECL representing the specialization of the alias
9518 template. So next time someone substitutes ARGLIST for
9519 the template parms into the alias template (GEN_TMPL),
9520 she'll get that TYPE_DECL back. */
9521
9522 if (t == error_mark_node)
9523 return t;
9524 }
9525 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9526 {
9527 if (!is_dependent_type)
9528 {
9529 set_current_access_from_decl (TYPE_NAME (template_type));
9530 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9531 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9532 arglist, complain, in_decl),
9533 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9534 arglist, complain, in_decl),
9535 SCOPED_ENUM_P (template_type), NULL);
9536
9537 if (t == error_mark_node)
9538 return t;
9539 }
9540 else
9541 {
9542 /* We don't want to call start_enum for this type, since
9543 the values for the enumeration constants may involve
9544 template parameters. And, no one should be interested
9545 in the enumeration constants for such a type. */
9546 t = cxx_make_type (ENUMERAL_TYPE);
9547 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9548 }
9549 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9550 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9551 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9552 }
9553 else if (CLASS_TYPE_P (template_type))
9554 {
9555 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9556 instantiated here. */
9557 gcc_assert (!LAMBDA_TYPE_P (template_type));
9558
9559 t = make_class_type (TREE_CODE (template_type));
9560 CLASSTYPE_DECLARED_CLASS (t)
9561 = CLASSTYPE_DECLARED_CLASS (template_type);
9562 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9563
9564 /* A local class. Make sure the decl gets registered properly. */
9565 if (context == current_function_decl)
9566 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9567 == error_mark_node)
9568 return error_mark_node;
9569
9570 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9571 /* This instantiation is another name for the primary
9572 template type. Set the TYPE_CANONICAL field
9573 appropriately. */
9574 TYPE_CANONICAL (t) = template_type;
9575 else if (any_template_arguments_need_structural_equality_p (arglist))
9576 /* Some of the template arguments require structural
9577 equality testing, so this template class requires
9578 structural equality testing. */
9579 SET_TYPE_STRUCTURAL_EQUALITY (t);
9580 }
9581 else
9582 gcc_unreachable ();
9583
9584 /* If we called start_enum or pushtag above, this information
9585 will already be set up. */
9586 if (!TYPE_NAME (t))
9587 {
9588 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9589
9590 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9591 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9592 DECL_SOURCE_LOCATION (type_decl)
9593 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9594 }
9595 else
9596 type_decl = TYPE_NAME (t);
9597
9598 if (CLASS_TYPE_P (template_type))
9599 {
9600 TREE_PRIVATE (type_decl)
9601 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9602 TREE_PROTECTED (type_decl)
9603 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9604 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9605 {
9606 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9607 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9608 }
9609 }
9610
9611 if (OVERLOAD_TYPE_P (t)
9612 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9613 {
9614 static const char *tags[] = {"abi_tag", "may_alias"};
9615
9616 for (unsigned ix = 0; ix != 2; ix++)
9617 {
9618 tree attributes
9619 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9620
9621 if (attributes)
9622 TYPE_ATTRIBUTES (t)
9623 = tree_cons (TREE_PURPOSE (attributes),
9624 TREE_VALUE (attributes),
9625 TYPE_ATTRIBUTES (t));
9626 }
9627 }
9628
9629 /* Let's consider the explicit specialization of a member
9630 of a class template specialization that is implicitly instantiated,
9631 e.g.:
9632 template<class T>
9633 struct S
9634 {
9635 template<class U> struct M {}; //#0
9636 };
9637
9638 template<>
9639 template<>
9640 struct S<int>::M<char> //#1
9641 {
9642 int i;
9643 };
9644 [temp.expl.spec]/4 says this is valid.
9645
9646 In this case, when we write:
9647 S<int>::M<char> m;
9648
9649 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9650 the one of #0.
9651
9652 When we encounter #1, we want to store the partial instantiation
9653 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9654
9655 For all cases other than this "explicit specialization of member of a
9656 class template", we just want to store the most general template into
9657 the CLASSTYPE_TI_TEMPLATE of M.
9658
9659 This case of "explicit specialization of member of a class template"
9660 only happens when:
9661 1/ the enclosing class is an instantiation of, and therefore not
9662 the same as, the context of the most general template, and
9663 2/ we aren't looking at the partial instantiation itself, i.e.
9664 the innermost arguments are not the same as the innermost parms of
9665 the most general template.
9666
9667 So it's only when 1/ and 2/ happens that we want to use the partial
9668 instantiation of the member template in lieu of its most general
9669 template. */
9670
9671 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9672 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9673 /* the enclosing class must be an instantiation... */
9674 && CLASS_TYPE_P (context)
9675 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9676 {
9677 TREE_VEC_LENGTH (arglist)--;
9678 ++processing_template_decl;
9679 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9680 tree partial_inst_args =
9681 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9682 arglist, complain, NULL_TREE);
9683 --processing_template_decl;
9684 TREE_VEC_LENGTH (arglist)++;
9685 if (partial_inst_args == error_mark_node)
9686 return error_mark_node;
9687 use_partial_inst_tmpl =
9688 /*...and we must not be looking at the partial instantiation
9689 itself. */
9690 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9691 partial_inst_args);
9692 }
9693
9694 if (!use_partial_inst_tmpl)
9695 /* This case is easy; there are no member templates involved. */
9696 found = gen_tmpl;
9697 else
9698 {
9699 /* This is a full instantiation of a member template. Find
9700 the partial instantiation of which this is an instance. */
9701
9702 /* Temporarily reduce by one the number of levels in the ARGLIST
9703 so as to avoid comparing the last set of arguments. */
9704 TREE_VEC_LENGTH (arglist)--;
9705 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9706 TREE_VEC_LENGTH (arglist)++;
9707 /* FOUND is either a proper class type, or an alias
9708 template specialization. In the later case, it's a
9709 TYPE_DECL, resulting from the substituting of arguments
9710 for parameters in the TYPE_DECL of the alias template
9711 done earlier. So be careful while getting the template
9712 of FOUND. */
9713 found = (TREE_CODE (found) == TEMPLATE_DECL
9714 ? found
9715 : (TREE_CODE (found) == TYPE_DECL
9716 ? DECL_TI_TEMPLATE (found)
9717 : CLASSTYPE_TI_TEMPLATE (found)));
9718
9719 if (DECL_CLASS_TEMPLATE_P (found)
9720 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9721 {
9722 /* If this partial instantiation is specialized, we want to
9723 use it for hash table lookup. */
9724 elt.tmpl = found;
9725 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9726 hash = spec_hasher::hash (&elt);
9727 }
9728 }
9729
9730 // Build template info for the new specialization.
9731 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9732
9733 elt.spec = t;
9734 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9735 gcc_checking_assert (*slot == NULL);
9736 entry = ggc_alloc<spec_entry> ();
9737 *entry = elt;
9738 *slot = entry;
9739
9740 /* Note this use of the partial instantiation so we can check it
9741 later in maybe_process_partial_specialization. */
9742 DECL_TEMPLATE_INSTANTIATIONS (found)
9743 = tree_cons (arglist, t,
9744 DECL_TEMPLATE_INSTANTIATIONS (found));
9745
9746 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9747 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9748 /* Now that the type has been registered on the instantiations
9749 list, we set up the enumerators. Because the enumeration
9750 constants may involve the enumeration type itself, we make
9751 sure to register the type first, and then create the
9752 constants. That way, doing tsubst_expr for the enumeration
9753 constants won't result in recursive calls here; we'll find
9754 the instantiation and exit above. */
9755 tsubst_enum (template_type, t, arglist);
9756
9757 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9758 /* If the type makes use of template parameters, the
9759 code that generates debugging information will crash. */
9760 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9761
9762 /* Possibly limit visibility based on template args. */
9763 TREE_PUBLIC (type_decl) = 1;
9764 determine_visibility (type_decl);
9765
9766 inherit_targ_abi_tags (t);
9767
9768 return t;
9769 }
9770 }
9771
9772 /* Wrapper for lookup_template_class_1. */
9773
9774 tree
9775 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9776 int entering_scope, tsubst_flags_t complain)
9777 {
9778 tree ret;
9779 timevar_push (TV_TEMPLATE_INST);
9780 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9781 entering_scope, complain);
9782 timevar_pop (TV_TEMPLATE_INST);
9783 return ret;
9784 }
9785
9786 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9787
9788 tree
9789 lookup_template_variable (tree templ, tree arglist)
9790 {
9791 /* The type of the expression is NULL_TREE since the template-id could refer
9792 to an explicit or partial specialization. */
9793 tree type = NULL_TREE;
9794 if (flag_concepts && variable_concept_p (templ))
9795 /* Except that concepts are always bool. */
9796 type = boolean_type_node;
9797 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9798 }
9799
9800 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9801
9802 tree
9803 finish_template_variable (tree var, tsubst_flags_t complain)
9804 {
9805 tree templ = TREE_OPERAND (var, 0);
9806 tree arglist = TREE_OPERAND (var, 1);
9807
9808 /* We never want to return a VAR_DECL for a variable concept, since they
9809 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9810 bool concept_p = flag_concepts && variable_concept_p (templ);
9811 if (concept_p && processing_template_decl)
9812 return var;
9813
9814 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9815 arglist = add_outermost_template_args (tmpl_args, arglist);
9816
9817 templ = most_general_template (templ);
9818 tree parms = DECL_TEMPLATE_PARMS (templ);
9819 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9820 /*req_all*/true,
9821 /*use_default*/true);
9822
9823 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9824 {
9825 if (complain & tf_error)
9826 {
9827 auto_diagnostic_group d;
9828 error ("use of invalid variable template %qE", var);
9829 diagnose_constraints (location_of (var), templ, arglist);
9830 }
9831 return error_mark_node;
9832 }
9833
9834 /* If a template-id refers to a specialization of a variable
9835 concept, then the expression is true if and only if the
9836 concept's constraints are satisfied by the given template
9837 arguments.
9838
9839 NOTE: This is an extension of Concepts Lite TS that
9840 allows constraints to be used in expressions. */
9841 if (concept_p)
9842 {
9843 tree decl = DECL_TEMPLATE_RESULT (templ);
9844 return evaluate_variable_concept (decl, arglist);
9845 }
9846
9847 return instantiate_template (templ, arglist, complain);
9848 }
9849
9850 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9851 TARGS template args, and instantiate it if it's not dependent. */
9852
9853 tree
9854 lookup_and_finish_template_variable (tree templ, tree targs,
9855 tsubst_flags_t complain)
9856 {
9857 templ = lookup_template_variable (templ, targs);
9858 if (!any_dependent_template_arguments_p (targs))
9859 {
9860 templ = finish_template_variable (templ, complain);
9861 mark_used (templ);
9862 }
9863
9864 return convert_from_reference (templ);
9865 }
9866
9867 \f
9868 struct pair_fn_data
9869 {
9870 tree_fn_t fn;
9871 tree_fn_t any_fn;
9872 void *data;
9873 /* True when we should also visit template parameters that occur in
9874 non-deduced contexts. */
9875 bool include_nondeduced_p;
9876 hash_set<tree> *visited;
9877 };
9878
9879 /* Called from for_each_template_parm via walk_tree. */
9880
9881 static tree
9882 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9883 {
9884 tree t = *tp;
9885 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9886 tree_fn_t fn = pfd->fn;
9887 void *data = pfd->data;
9888 tree result = NULL_TREE;
9889
9890 #define WALK_SUBTREE(NODE) \
9891 do \
9892 { \
9893 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9894 pfd->include_nondeduced_p, \
9895 pfd->any_fn); \
9896 if (result) goto out; \
9897 } \
9898 while (0)
9899
9900 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9901 return t;
9902
9903 if (TYPE_P (t)
9904 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9905 WALK_SUBTREE (TYPE_CONTEXT (t));
9906
9907 switch (TREE_CODE (t))
9908 {
9909 case RECORD_TYPE:
9910 if (TYPE_PTRMEMFUNC_P (t))
9911 break;
9912 /* Fall through. */
9913
9914 case UNION_TYPE:
9915 case ENUMERAL_TYPE:
9916 if (!TYPE_TEMPLATE_INFO (t))
9917 *walk_subtrees = 0;
9918 else
9919 WALK_SUBTREE (TYPE_TI_ARGS (t));
9920 break;
9921
9922 case INTEGER_TYPE:
9923 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9924 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9925 break;
9926
9927 case METHOD_TYPE:
9928 /* Since we're not going to walk subtrees, we have to do this
9929 explicitly here. */
9930 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9931 /* Fall through. */
9932
9933 case FUNCTION_TYPE:
9934 /* Check the return type. */
9935 WALK_SUBTREE (TREE_TYPE (t));
9936
9937 /* Check the parameter types. Since default arguments are not
9938 instantiated until they are needed, the TYPE_ARG_TYPES may
9939 contain expressions that involve template parameters. But,
9940 no-one should be looking at them yet. And, once they're
9941 instantiated, they don't contain template parameters, so
9942 there's no point in looking at them then, either. */
9943 {
9944 tree parm;
9945
9946 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9947 WALK_SUBTREE (TREE_VALUE (parm));
9948
9949 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9950 want walk_tree walking into them itself. */
9951 *walk_subtrees = 0;
9952 }
9953
9954 if (flag_noexcept_type)
9955 {
9956 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9957 if (spec)
9958 WALK_SUBTREE (TREE_PURPOSE (spec));
9959 }
9960 break;
9961
9962 case TYPEOF_TYPE:
9963 case DECLTYPE_TYPE:
9964 case UNDERLYING_TYPE:
9965 if (pfd->include_nondeduced_p
9966 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9967 pfd->visited,
9968 pfd->include_nondeduced_p,
9969 pfd->any_fn))
9970 return error_mark_node;
9971 *walk_subtrees = false;
9972 break;
9973
9974 case FUNCTION_DECL:
9975 case VAR_DECL:
9976 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9977 WALK_SUBTREE (DECL_TI_ARGS (t));
9978 /* Fall through. */
9979
9980 case PARM_DECL:
9981 case CONST_DECL:
9982 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9983 WALK_SUBTREE (DECL_INITIAL (t));
9984 if (DECL_CONTEXT (t)
9985 && pfd->include_nondeduced_p)
9986 WALK_SUBTREE (DECL_CONTEXT (t));
9987 break;
9988
9989 case BOUND_TEMPLATE_TEMPLATE_PARM:
9990 /* Record template parameters such as `T' inside `TT<T>'. */
9991 WALK_SUBTREE (TYPE_TI_ARGS (t));
9992 /* Fall through. */
9993
9994 case TEMPLATE_TEMPLATE_PARM:
9995 case TEMPLATE_TYPE_PARM:
9996 case TEMPLATE_PARM_INDEX:
9997 if (fn && (*fn)(t, data))
9998 return t;
9999 else if (!fn)
10000 return t;
10001 break;
10002
10003 case TEMPLATE_DECL:
10004 /* A template template parameter is encountered. */
10005 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10006 WALK_SUBTREE (TREE_TYPE (t));
10007
10008 /* Already substituted template template parameter */
10009 *walk_subtrees = 0;
10010 break;
10011
10012 case TYPENAME_TYPE:
10013 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10014 partial instantiation. */
10015 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10016 break;
10017
10018 case CONSTRUCTOR:
10019 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10020 && pfd->include_nondeduced_p)
10021 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10022 break;
10023
10024 case INDIRECT_REF:
10025 case COMPONENT_REF:
10026 /* If there's no type, then this thing must be some expression
10027 involving template parameters. */
10028 if (!fn && !TREE_TYPE (t))
10029 return error_mark_node;
10030 break;
10031
10032 case MODOP_EXPR:
10033 case CAST_EXPR:
10034 case IMPLICIT_CONV_EXPR:
10035 case REINTERPRET_CAST_EXPR:
10036 case CONST_CAST_EXPR:
10037 case STATIC_CAST_EXPR:
10038 case DYNAMIC_CAST_EXPR:
10039 case ARROW_EXPR:
10040 case DOTSTAR_EXPR:
10041 case TYPEID_EXPR:
10042 case PSEUDO_DTOR_EXPR:
10043 if (!fn)
10044 return error_mark_node;
10045 break;
10046
10047 default:
10048 break;
10049 }
10050
10051 #undef WALK_SUBTREE
10052
10053 /* We didn't find any template parameters we liked. */
10054 out:
10055 return result;
10056 }
10057
10058 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10059 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10060 call FN with the parameter and the DATA.
10061 If FN returns nonzero, the iteration is terminated, and
10062 for_each_template_parm returns 1. Otherwise, the iteration
10063 continues. If FN never returns a nonzero value, the value
10064 returned by for_each_template_parm is 0. If FN is NULL, it is
10065 considered to be the function which always returns 1.
10066
10067 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10068 parameters that occur in non-deduced contexts. When false, only
10069 visits those template parameters that can be deduced. */
10070
10071 static tree
10072 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10073 hash_set<tree> *visited,
10074 bool include_nondeduced_p,
10075 tree_fn_t any_fn)
10076 {
10077 struct pair_fn_data pfd;
10078 tree result;
10079
10080 /* Set up. */
10081 pfd.fn = fn;
10082 pfd.any_fn = any_fn;
10083 pfd.data = data;
10084 pfd.include_nondeduced_p = include_nondeduced_p;
10085
10086 /* Walk the tree. (Conceptually, we would like to walk without
10087 duplicates, but for_each_template_parm_r recursively calls
10088 for_each_template_parm, so we would need to reorganize a fair
10089 bit to use walk_tree_without_duplicates, so we keep our own
10090 visited list.) */
10091 if (visited)
10092 pfd.visited = visited;
10093 else
10094 pfd.visited = new hash_set<tree>;
10095 result = cp_walk_tree (&t,
10096 for_each_template_parm_r,
10097 &pfd,
10098 pfd.visited);
10099
10100 /* Clean up. */
10101 if (!visited)
10102 {
10103 delete pfd.visited;
10104 pfd.visited = 0;
10105 }
10106
10107 return result;
10108 }
10109
10110 /* Returns true if T depends on any template parameter. */
10111
10112 int
10113 uses_template_parms (tree t)
10114 {
10115 if (t == NULL_TREE)
10116 return false;
10117
10118 bool dependent_p;
10119 int saved_processing_template_decl;
10120
10121 saved_processing_template_decl = processing_template_decl;
10122 if (!saved_processing_template_decl)
10123 processing_template_decl = 1;
10124 if (TYPE_P (t))
10125 dependent_p = dependent_type_p (t);
10126 else if (TREE_CODE (t) == TREE_VEC)
10127 dependent_p = any_dependent_template_arguments_p (t);
10128 else if (TREE_CODE (t) == TREE_LIST)
10129 dependent_p = (uses_template_parms (TREE_VALUE (t))
10130 || uses_template_parms (TREE_CHAIN (t)));
10131 else if (TREE_CODE (t) == TYPE_DECL)
10132 dependent_p = dependent_type_p (TREE_TYPE (t));
10133 else if (DECL_P (t)
10134 || EXPR_P (t)
10135 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10136 || TREE_CODE (t) == OVERLOAD
10137 || BASELINK_P (t)
10138 || identifier_p (t)
10139 || TREE_CODE (t) == TRAIT_EXPR
10140 || TREE_CODE (t) == CONSTRUCTOR
10141 || CONSTANT_CLASS_P (t))
10142 dependent_p = (type_dependent_expression_p (t)
10143 || value_dependent_expression_p (t));
10144 else
10145 {
10146 gcc_assert (t == error_mark_node);
10147 dependent_p = false;
10148 }
10149
10150 processing_template_decl = saved_processing_template_decl;
10151
10152 return dependent_p;
10153 }
10154
10155 /* Returns true iff current_function_decl is an incompletely instantiated
10156 template. Useful instead of processing_template_decl because the latter
10157 is set to 0 during instantiate_non_dependent_expr. */
10158
10159 bool
10160 in_template_function (void)
10161 {
10162 tree fn = current_function_decl;
10163 bool ret;
10164 ++processing_template_decl;
10165 ret = (fn && DECL_LANG_SPECIFIC (fn)
10166 && DECL_TEMPLATE_INFO (fn)
10167 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10168 --processing_template_decl;
10169 return ret;
10170 }
10171
10172 /* Returns true if T depends on any template parameter with level LEVEL. */
10173
10174 bool
10175 uses_template_parms_level (tree t, int level)
10176 {
10177 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10178 /*include_nondeduced_p=*/true);
10179 }
10180
10181 /* Returns true if the signature of DECL depends on any template parameter from
10182 its enclosing class. */
10183
10184 bool
10185 uses_outer_template_parms (tree decl)
10186 {
10187 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10188 if (depth == 0)
10189 return false;
10190 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10191 &depth, NULL, /*include_nondeduced_p=*/true))
10192 return true;
10193 if (PRIMARY_TEMPLATE_P (decl)
10194 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10195 (DECL_TEMPLATE_PARMS (decl)),
10196 template_parm_outer_level,
10197 &depth, NULL, /*include_nondeduced_p=*/true))
10198 return true;
10199 tree ci = get_constraints (decl);
10200 if (ci)
10201 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10202 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10203 &depth, NULL, /*nondeduced*/true))
10204 return true;
10205 return false;
10206 }
10207
10208 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10209 ill-formed translation unit, i.e. a variable or function that isn't
10210 usable in a constant expression. */
10211
10212 static inline bool
10213 neglectable_inst_p (tree d)
10214 {
10215 return (d && DECL_P (d)
10216 && !undeduced_auto_decl (d)
10217 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10218 : decl_maybe_constant_var_p (d)));
10219 }
10220
10221 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10222 neglectable and instantiated from within an erroneous instantiation. */
10223
10224 static bool
10225 limit_bad_template_recursion (tree decl)
10226 {
10227 struct tinst_level *lev = current_tinst_level;
10228 int errs = errorcount + sorrycount;
10229 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10230 return false;
10231
10232 for (; lev; lev = lev->next)
10233 if (neglectable_inst_p (lev->maybe_get_node ()))
10234 break;
10235
10236 return (lev && errs > lev->errors);
10237 }
10238
10239 static int tinst_depth;
10240 extern int max_tinst_depth;
10241 int depth_reached;
10242
10243 static GTY(()) struct tinst_level *last_error_tinst_level;
10244
10245 /* We're starting to instantiate D; record the template instantiation context
10246 at LOC for diagnostics and to restore it later. */
10247
10248 static bool
10249 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10250 {
10251 struct tinst_level *new_level;
10252
10253 if (tinst_depth >= max_tinst_depth)
10254 {
10255 /* Tell error.c not to try to instantiate any templates. */
10256 at_eof = 2;
10257 fatal_error (input_location,
10258 "template instantiation depth exceeds maximum of %d"
10259 " (use %<-ftemplate-depth=%> to increase the maximum)",
10260 max_tinst_depth);
10261 return false;
10262 }
10263
10264 /* If the current instantiation caused problems, don't let it instantiate
10265 anything else. Do allow deduction substitution and decls usable in
10266 constant expressions. */
10267 if (!targs && limit_bad_template_recursion (tldcl))
10268 return false;
10269
10270 /* When not -quiet, dump template instantiations other than functions, since
10271 announce_function will take care of those. */
10272 if (!quiet_flag && !targs
10273 && TREE_CODE (tldcl) != TREE_LIST
10274 && TREE_CODE (tldcl) != FUNCTION_DECL)
10275 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10276
10277 new_level = tinst_level_freelist ().alloc ();
10278 new_level->tldcl = tldcl;
10279 new_level->targs = targs;
10280 new_level->locus = loc;
10281 new_level->errors = errorcount + sorrycount;
10282 new_level->next = NULL;
10283 new_level->refcount = 0;
10284 set_refcount_ptr (new_level->next, current_tinst_level);
10285 set_refcount_ptr (current_tinst_level, new_level);
10286
10287 ++tinst_depth;
10288 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10289 depth_reached = tinst_depth;
10290
10291 return true;
10292 }
10293
10294 /* We're starting substitution of TMPL<ARGS>; record the template
10295 substitution context for diagnostics and to restore it later. */
10296
10297 static bool
10298 push_tinst_level (tree tmpl, tree args)
10299 {
10300 return push_tinst_level_loc (tmpl, args, input_location);
10301 }
10302
10303 /* We're starting to instantiate D; record INPUT_LOCATION and the
10304 template instantiation context for diagnostics and to restore it
10305 later. */
10306
10307 bool
10308 push_tinst_level (tree d)
10309 {
10310 return push_tinst_level_loc (d, input_location);
10311 }
10312
10313 /* Likewise, but record LOC as the program location. */
10314
10315 bool
10316 push_tinst_level_loc (tree d, location_t loc)
10317 {
10318 gcc_assert (TREE_CODE (d) != TREE_LIST);
10319 return push_tinst_level_loc (d, NULL, loc);
10320 }
10321
10322 /* We're done instantiating this template; return to the instantiation
10323 context. */
10324
10325 void
10326 pop_tinst_level (void)
10327 {
10328 /* Restore the filename and line number stashed away when we started
10329 this instantiation. */
10330 input_location = current_tinst_level->locus;
10331 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10332 --tinst_depth;
10333 }
10334
10335 /* We're instantiating a deferred template; restore the template
10336 instantiation context in which the instantiation was requested, which
10337 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10338
10339 static tree
10340 reopen_tinst_level (struct tinst_level *level)
10341 {
10342 struct tinst_level *t;
10343
10344 tinst_depth = 0;
10345 for (t = level; t; t = t->next)
10346 ++tinst_depth;
10347
10348 set_refcount_ptr (current_tinst_level, level);
10349 pop_tinst_level ();
10350 if (current_tinst_level)
10351 current_tinst_level->errors = errorcount+sorrycount;
10352 return level->maybe_get_node ();
10353 }
10354
10355 /* Returns the TINST_LEVEL which gives the original instantiation
10356 context. */
10357
10358 struct tinst_level *
10359 outermost_tinst_level (void)
10360 {
10361 struct tinst_level *level = current_tinst_level;
10362 if (level)
10363 while (level->next)
10364 level = level->next;
10365 return level;
10366 }
10367
10368 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10369 vector of template arguments, as for tsubst.
10370
10371 Returns an appropriate tsubst'd friend declaration. */
10372
10373 static tree
10374 tsubst_friend_function (tree decl, tree args)
10375 {
10376 tree new_friend;
10377
10378 if (TREE_CODE (decl) == FUNCTION_DECL
10379 && DECL_TEMPLATE_INSTANTIATION (decl)
10380 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10381 /* This was a friend declared with an explicit template
10382 argument list, e.g.:
10383
10384 friend void f<>(T);
10385
10386 to indicate that f was a template instantiation, not a new
10387 function declaration. Now, we have to figure out what
10388 instantiation of what template. */
10389 {
10390 tree template_id, arglist, fns;
10391 tree new_args;
10392 tree tmpl;
10393 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10394
10395 /* Friend functions are looked up in the containing namespace scope.
10396 We must enter that scope, to avoid finding member functions of the
10397 current class with same name. */
10398 push_nested_namespace (ns);
10399 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10400 tf_warning_or_error, NULL_TREE,
10401 /*integral_constant_expression_p=*/false);
10402 pop_nested_namespace (ns);
10403 arglist = tsubst (DECL_TI_ARGS (decl), args,
10404 tf_warning_or_error, NULL_TREE);
10405 template_id = lookup_template_function (fns, arglist);
10406
10407 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10408 tmpl = determine_specialization (template_id, new_friend,
10409 &new_args,
10410 /*need_member_template=*/0,
10411 TREE_VEC_LENGTH (args),
10412 tsk_none);
10413 return instantiate_template (tmpl, new_args, tf_error);
10414 }
10415
10416 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10417
10418 /* The NEW_FRIEND will look like an instantiation, to the
10419 compiler, but is not an instantiation from the point of view of
10420 the language. For example, we might have had:
10421
10422 template <class T> struct S {
10423 template <class U> friend void f(T, U);
10424 };
10425
10426 Then, in S<int>, template <class U> void f(int, U) is not an
10427 instantiation of anything. */
10428 if (new_friend == error_mark_node)
10429 return error_mark_node;
10430
10431 DECL_USE_TEMPLATE (new_friend) = 0;
10432 if (TREE_CODE (decl) == TEMPLATE_DECL)
10433 {
10434 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10435 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10436 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10437 }
10438
10439 /* The mangled name for the NEW_FRIEND is incorrect. The function
10440 is not a template instantiation and should not be mangled like
10441 one. Therefore, we forget the mangling here; we'll recompute it
10442 later if we need it. */
10443 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10444 {
10445 SET_DECL_RTL (new_friend, NULL);
10446 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10447 }
10448
10449 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10450 {
10451 tree old_decl;
10452 tree new_friend_template_info;
10453 tree new_friend_result_template_info;
10454 tree ns;
10455 int new_friend_is_defn;
10456
10457 /* We must save some information from NEW_FRIEND before calling
10458 duplicate decls since that function will free NEW_FRIEND if
10459 possible. */
10460 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10461 new_friend_is_defn =
10462 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10463 (template_for_substitution (new_friend)))
10464 != NULL_TREE);
10465 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10466 {
10467 /* This declaration is a `primary' template. */
10468 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10469
10470 new_friend_result_template_info
10471 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10472 }
10473 else
10474 new_friend_result_template_info = NULL_TREE;
10475
10476 /* Inside pushdecl_namespace_level, we will push into the
10477 current namespace. However, the friend function should go
10478 into the namespace of the template. */
10479 ns = decl_namespace_context (new_friend);
10480 push_nested_namespace (ns);
10481 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10482 pop_nested_namespace (ns);
10483
10484 if (old_decl == error_mark_node)
10485 return error_mark_node;
10486
10487 if (old_decl != new_friend)
10488 {
10489 /* This new friend declaration matched an existing
10490 declaration. For example, given:
10491
10492 template <class T> void f(T);
10493 template <class U> class C {
10494 template <class T> friend void f(T) {}
10495 };
10496
10497 the friend declaration actually provides the definition
10498 of `f', once C has been instantiated for some type. So,
10499 old_decl will be the out-of-class template declaration,
10500 while new_friend is the in-class definition.
10501
10502 But, if `f' was called before this point, the
10503 instantiation of `f' will have DECL_TI_ARGS corresponding
10504 to `T' but not to `U', references to which might appear
10505 in the definition of `f'. Previously, the most general
10506 template for an instantiation of `f' was the out-of-class
10507 version; now it is the in-class version. Therefore, we
10508 run through all specialization of `f', adding to their
10509 DECL_TI_ARGS appropriately. In particular, they need a
10510 new set of outer arguments, corresponding to the
10511 arguments for this class instantiation.
10512
10513 The same situation can arise with something like this:
10514
10515 friend void f(int);
10516 template <class T> class C {
10517 friend void f(T) {}
10518 };
10519
10520 when `C<int>' is instantiated. Now, `f(int)' is defined
10521 in the class. */
10522
10523 if (!new_friend_is_defn)
10524 /* On the other hand, if the in-class declaration does
10525 *not* provide a definition, then we don't want to alter
10526 existing definitions. We can just leave everything
10527 alone. */
10528 ;
10529 else
10530 {
10531 tree new_template = TI_TEMPLATE (new_friend_template_info);
10532 tree new_args = TI_ARGS (new_friend_template_info);
10533
10534 /* Overwrite whatever template info was there before, if
10535 any, with the new template information pertaining to
10536 the declaration. */
10537 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10538
10539 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10540 {
10541 /* We should have called reregister_specialization in
10542 duplicate_decls. */
10543 gcc_assert (retrieve_specialization (new_template,
10544 new_args, 0)
10545 == old_decl);
10546
10547 /* Instantiate it if the global has already been used. */
10548 if (DECL_ODR_USED (old_decl))
10549 instantiate_decl (old_decl, /*defer_ok=*/true,
10550 /*expl_inst_class_mem_p=*/false);
10551 }
10552 else
10553 {
10554 tree t;
10555
10556 /* Indicate that the old function template is a partial
10557 instantiation. */
10558 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10559 = new_friend_result_template_info;
10560
10561 gcc_assert (new_template
10562 == most_general_template (new_template));
10563 gcc_assert (new_template != old_decl);
10564
10565 /* Reassign any specializations already in the hash table
10566 to the new more general template, and add the
10567 additional template args. */
10568 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10569 t != NULL_TREE;
10570 t = TREE_CHAIN (t))
10571 {
10572 tree spec = TREE_VALUE (t);
10573 spec_entry elt;
10574
10575 elt.tmpl = old_decl;
10576 elt.args = DECL_TI_ARGS (spec);
10577 elt.spec = NULL_TREE;
10578
10579 decl_specializations->remove_elt (&elt);
10580
10581 DECL_TI_ARGS (spec)
10582 = add_outermost_template_args (new_args,
10583 DECL_TI_ARGS (spec));
10584
10585 register_specialization
10586 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10587
10588 }
10589 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10590 }
10591 }
10592
10593 /* The information from NEW_FRIEND has been merged into OLD_DECL
10594 by duplicate_decls. */
10595 new_friend = old_decl;
10596 }
10597 }
10598 else
10599 {
10600 tree context = DECL_CONTEXT (new_friend);
10601 bool dependent_p;
10602
10603 /* In the code
10604 template <class T> class C {
10605 template <class U> friend void C1<U>::f (); // case 1
10606 friend void C2<T>::f (); // case 2
10607 };
10608 we only need to make sure CONTEXT is a complete type for
10609 case 2. To distinguish between the two cases, we note that
10610 CONTEXT of case 1 remains dependent type after tsubst while
10611 this isn't true for case 2. */
10612 ++processing_template_decl;
10613 dependent_p = dependent_type_p (context);
10614 --processing_template_decl;
10615
10616 if (!dependent_p
10617 && !complete_type_or_else (context, NULL_TREE))
10618 return error_mark_node;
10619
10620 if (COMPLETE_TYPE_P (context))
10621 {
10622 tree fn = new_friend;
10623 /* do_friend adds the TEMPLATE_DECL for any member friend
10624 template even if it isn't a member template, i.e.
10625 template <class T> friend A<T>::f();
10626 Look through it in that case. */
10627 if (TREE_CODE (fn) == TEMPLATE_DECL
10628 && !PRIMARY_TEMPLATE_P (fn))
10629 fn = DECL_TEMPLATE_RESULT (fn);
10630 /* Check to see that the declaration is really present, and,
10631 possibly obtain an improved declaration. */
10632 fn = check_classfn (context, fn, NULL_TREE);
10633
10634 if (fn)
10635 new_friend = fn;
10636 }
10637 }
10638
10639 return new_friend;
10640 }
10641
10642 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10643 template arguments, as for tsubst.
10644
10645 Returns an appropriate tsubst'd friend type or error_mark_node on
10646 failure. */
10647
10648 static tree
10649 tsubst_friend_class (tree friend_tmpl, tree args)
10650 {
10651 tree tmpl;
10652
10653 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10654 {
10655 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10656 return TREE_TYPE (tmpl);
10657 }
10658
10659 tree context = CP_DECL_CONTEXT (friend_tmpl);
10660 if (TREE_CODE (context) == NAMESPACE_DECL)
10661 push_nested_namespace (context);
10662 else
10663 {
10664 context = tsubst (context, args, tf_error, NULL_TREE);
10665 push_nested_class (context);
10666 }
10667
10668 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10669 /*non_class=*/false, /*block_p=*/false,
10670 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10671
10672 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10673 {
10674 /* The friend template has already been declared. Just
10675 check to see that the declarations match, and install any new
10676 default parameters. We must tsubst the default parameters,
10677 of course. We only need the innermost template parameters
10678 because that is all that redeclare_class_template will look
10679 at. */
10680 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10681 > TMPL_ARGS_DEPTH (args))
10682 {
10683 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10684 args, tf_warning_or_error);
10685 location_t saved_input_location = input_location;
10686 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10687 tree cons = get_constraints (tmpl);
10688 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10689 input_location = saved_input_location;
10690 }
10691 }
10692 else
10693 {
10694 /* The friend template has not already been declared. In this
10695 case, the instantiation of the template class will cause the
10696 injection of this template into the namespace scope. */
10697 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10698
10699 if (tmpl != error_mark_node)
10700 {
10701 /* The new TMPL is not an instantiation of anything, so we
10702 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10703 for the new type because that is supposed to be the
10704 corresponding template decl, i.e., TMPL. */
10705 DECL_USE_TEMPLATE (tmpl) = 0;
10706 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10707 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10708 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10709 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10710
10711 /* It is hidden. */
10712 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10713 DECL_ANTICIPATED (tmpl)
10714 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10715
10716 /* Inject this template into the enclosing namspace scope. */
10717 tmpl = pushdecl_namespace_level (tmpl, true);
10718 }
10719 }
10720
10721 if (TREE_CODE (context) == NAMESPACE_DECL)
10722 pop_nested_namespace (context);
10723 else
10724 pop_nested_class ();
10725
10726 return TREE_TYPE (tmpl);
10727 }
10728
10729 /* Returns zero if TYPE cannot be completed later due to circularity.
10730 Otherwise returns one. */
10731
10732 static int
10733 can_complete_type_without_circularity (tree type)
10734 {
10735 if (type == NULL_TREE || type == error_mark_node)
10736 return 0;
10737 else if (COMPLETE_TYPE_P (type))
10738 return 1;
10739 else if (TREE_CODE (type) == ARRAY_TYPE)
10740 return can_complete_type_without_circularity (TREE_TYPE (type));
10741 else if (CLASS_TYPE_P (type)
10742 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10743 return 0;
10744 else
10745 return 1;
10746 }
10747
10748 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10749 tsubst_flags_t, tree);
10750
10751 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10752 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10753
10754 static tree
10755 tsubst_attribute (tree t, tree *decl_p, tree args,
10756 tsubst_flags_t complain, tree in_decl)
10757 {
10758 gcc_assert (ATTR_IS_DEPENDENT (t));
10759
10760 tree val = TREE_VALUE (t);
10761 if (val == NULL_TREE)
10762 /* Nothing to do. */;
10763 else if ((flag_openmp || flag_openmp_simd)
10764 && is_attribute_p ("omp declare simd",
10765 get_attribute_name (t)))
10766 {
10767 tree clauses = TREE_VALUE (val);
10768 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10769 complain, in_decl);
10770 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10771 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10772 tree parms = DECL_ARGUMENTS (*decl_p);
10773 clauses
10774 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10775 if (clauses)
10776 val = build_tree_list (NULL_TREE, clauses);
10777 else
10778 val = NULL_TREE;
10779 }
10780 /* If the first attribute argument is an identifier, don't
10781 pass it through tsubst. Attributes like mode, format,
10782 cleanup and several target specific attributes expect it
10783 unmodified. */
10784 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10785 {
10786 tree chain
10787 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10788 /*integral_constant_expression_p=*/false);
10789 if (chain != TREE_CHAIN (val))
10790 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10791 }
10792 else if (PACK_EXPANSION_P (val))
10793 {
10794 /* An attribute pack expansion. */
10795 tree purp = TREE_PURPOSE (t);
10796 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10797 if (pack == error_mark_node)
10798 return error_mark_node;
10799 int len = TREE_VEC_LENGTH (pack);
10800 tree list = NULL_TREE;
10801 tree *q = &list;
10802 for (int i = 0; i < len; ++i)
10803 {
10804 tree elt = TREE_VEC_ELT (pack, i);
10805 *q = build_tree_list (purp, elt);
10806 q = &TREE_CHAIN (*q);
10807 }
10808 return list;
10809 }
10810 else
10811 val = tsubst_expr (val, args, complain, in_decl,
10812 /*integral_constant_expression_p=*/false);
10813
10814 if (val != TREE_VALUE (t))
10815 return build_tree_list (TREE_PURPOSE (t), val);
10816 return t;
10817 }
10818
10819 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10820 unchanged or a new TREE_LIST chain. */
10821
10822 static tree
10823 tsubst_attributes (tree attributes, tree args,
10824 tsubst_flags_t complain, tree in_decl)
10825 {
10826 tree last_dep = NULL_TREE;
10827
10828 for (tree t = attributes; t; t = TREE_CHAIN (t))
10829 if (ATTR_IS_DEPENDENT (t))
10830 {
10831 last_dep = t;
10832 attributes = copy_list (attributes);
10833 break;
10834 }
10835
10836 if (last_dep)
10837 for (tree *p = &attributes; *p; )
10838 {
10839 tree t = *p;
10840 if (ATTR_IS_DEPENDENT (t))
10841 {
10842 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10843 if (subst != t)
10844 {
10845 *p = subst;
10846 while (*p)
10847 p = &TREE_CHAIN (*p);
10848 *p = TREE_CHAIN (t);
10849 continue;
10850 }
10851 }
10852 p = &TREE_CHAIN (*p);
10853 }
10854
10855 return attributes;
10856 }
10857
10858 /* Apply any attributes which had to be deferred until instantiation
10859 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10860 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10861
10862 static void
10863 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10864 tree args, tsubst_flags_t complain, tree in_decl)
10865 {
10866 tree last_dep = NULL_TREE;
10867 tree t;
10868 tree *p;
10869
10870 if (attributes == NULL_TREE)
10871 return;
10872
10873 if (DECL_P (*decl_p))
10874 {
10875 if (TREE_TYPE (*decl_p) == error_mark_node)
10876 return;
10877 p = &DECL_ATTRIBUTES (*decl_p);
10878 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10879 to our attributes parameter. */
10880 gcc_assert (*p == attributes);
10881 }
10882 else
10883 {
10884 p = &TYPE_ATTRIBUTES (*decl_p);
10885 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10886 lookup_template_class_1, and should be preserved. */
10887 gcc_assert (*p != attributes);
10888 while (*p)
10889 p = &TREE_CHAIN (*p);
10890 }
10891
10892 for (t = attributes; t; t = TREE_CHAIN (t))
10893 if (ATTR_IS_DEPENDENT (t))
10894 {
10895 last_dep = t;
10896 attributes = copy_list (attributes);
10897 break;
10898 }
10899
10900 *p = attributes;
10901 if (last_dep)
10902 {
10903 tree late_attrs = NULL_TREE;
10904 tree *q = &late_attrs;
10905
10906 for (; *p; )
10907 {
10908 t = *p;
10909 if (ATTR_IS_DEPENDENT (t))
10910 {
10911 *p = TREE_CHAIN (t);
10912 TREE_CHAIN (t) = NULL_TREE;
10913 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10914 while (*q)
10915 q = &TREE_CHAIN (*q);
10916 }
10917 else
10918 p = &TREE_CHAIN (t);
10919 }
10920
10921 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10922 }
10923 }
10924
10925 /* Perform (or defer) access check for typedefs that were referenced
10926 from within the template TMPL code.
10927 This is a subroutine of instantiate_decl and instantiate_class_template.
10928 TMPL is the template to consider and TARGS is the list of arguments of
10929 that template. */
10930
10931 static void
10932 perform_typedefs_access_check (tree tmpl, tree targs)
10933 {
10934 location_t saved_location;
10935 unsigned i;
10936 qualified_typedef_usage_t *iter;
10937
10938 if (!tmpl
10939 || (!CLASS_TYPE_P (tmpl)
10940 && TREE_CODE (tmpl) != FUNCTION_DECL))
10941 return;
10942
10943 saved_location = input_location;
10944 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10945 {
10946 tree type_decl = iter->typedef_decl;
10947 tree type_scope = iter->context;
10948
10949 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10950 continue;
10951
10952 if (uses_template_parms (type_decl))
10953 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10954 if (uses_template_parms (type_scope))
10955 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10956
10957 /* Make access check error messages point to the location
10958 of the use of the typedef. */
10959 input_location = iter->locus;
10960 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10961 type_decl, type_decl,
10962 tf_warning_or_error);
10963 }
10964 input_location = saved_location;
10965 }
10966
10967 static tree
10968 instantiate_class_template_1 (tree type)
10969 {
10970 tree templ, args, pattern, t, member;
10971 tree typedecl;
10972 tree pbinfo;
10973 tree base_list;
10974 unsigned int saved_maximum_field_alignment;
10975 tree fn_context;
10976
10977 if (type == error_mark_node)
10978 return error_mark_node;
10979
10980 if (COMPLETE_OR_OPEN_TYPE_P (type)
10981 || uses_template_parms (type))
10982 return type;
10983
10984 /* Figure out which template is being instantiated. */
10985 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10986 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10987
10988 /* Mark the type as in the process of being defined. */
10989 TYPE_BEING_DEFINED (type) = 1;
10990
10991 /* We may be in the middle of deferred access check. Disable
10992 it now. */
10993 deferring_access_check_sentinel acs (dk_no_deferred);
10994
10995 /* Determine what specialization of the original template to
10996 instantiate. */
10997 t = most_specialized_partial_spec (type, tf_warning_or_error);
10998 if (t == error_mark_node)
10999 return error_mark_node;
11000 else if (t)
11001 {
11002 /* This TYPE is actually an instantiation of a partial
11003 specialization. We replace the innermost set of ARGS with
11004 the arguments appropriate for substitution. For example,
11005 given:
11006
11007 template <class T> struct S {};
11008 template <class T> struct S<T*> {};
11009
11010 and supposing that we are instantiating S<int*>, ARGS will
11011 presently be {int*} -- but we need {int}. */
11012 pattern = TREE_TYPE (t);
11013 args = TREE_PURPOSE (t);
11014 }
11015 else
11016 {
11017 pattern = TREE_TYPE (templ);
11018 args = CLASSTYPE_TI_ARGS (type);
11019 }
11020
11021 /* If the template we're instantiating is incomplete, then clearly
11022 there's nothing we can do. */
11023 if (!COMPLETE_TYPE_P (pattern))
11024 {
11025 /* We can try again later. */
11026 TYPE_BEING_DEFINED (type) = 0;
11027 return type;
11028 }
11029
11030 /* If we've recursively instantiated too many templates, stop. */
11031 if (! push_tinst_level (type))
11032 return type;
11033
11034 int saved_unevaluated_operand = cp_unevaluated_operand;
11035 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11036
11037 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11038 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11039 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11040 fn_context = error_mark_node;
11041 if (!fn_context)
11042 push_to_top_level ();
11043 else
11044 {
11045 cp_unevaluated_operand = 0;
11046 c_inhibit_evaluation_warnings = 0;
11047 }
11048 /* Use #pragma pack from the template context. */
11049 saved_maximum_field_alignment = maximum_field_alignment;
11050 maximum_field_alignment = TYPE_PRECISION (pattern);
11051
11052 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11053
11054 /* Set the input location to the most specialized template definition.
11055 This is needed if tsubsting causes an error. */
11056 typedecl = TYPE_MAIN_DECL (pattern);
11057 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11058 DECL_SOURCE_LOCATION (typedecl);
11059
11060 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11061 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11062 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11063 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11064 if (ANON_AGGR_TYPE_P (pattern))
11065 SET_ANON_AGGR_TYPE_P (type);
11066 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11067 {
11068 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11069 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11070 /* Adjust visibility for template arguments. */
11071 determine_visibility (TYPE_MAIN_DECL (type));
11072 }
11073 if (CLASS_TYPE_P (type))
11074 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11075
11076 pbinfo = TYPE_BINFO (pattern);
11077
11078 /* We should never instantiate a nested class before its enclosing
11079 class; we need to look up the nested class by name before we can
11080 instantiate it, and that lookup should instantiate the enclosing
11081 class. */
11082 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11083 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11084
11085 base_list = NULL_TREE;
11086 if (BINFO_N_BASE_BINFOS (pbinfo))
11087 {
11088 tree pbase_binfo;
11089 tree pushed_scope;
11090 int i;
11091
11092 /* We must enter the scope containing the type, as that is where
11093 the accessibility of types named in dependent bases are
11094 looked up from. */
11095 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11096
11097 /* Substitute into each of the bases to determine the actual
11098 basetypes. */
11099 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11100 {
11101 tree base;
11102 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11103 tree expanded_bases = NULL_TREE;
11104 int idx, len = 1;
11105
11106 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11107 {
11108 expanded_bases =
11109 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11110 args, tf_error, NULL_TREE);
11111 if (expanded_bases == error_mark_node)
11112 continue;
11113
11114 len = TREE_VEC_LENGTH (expanded_bases);
11115 }
11116
11117 for (idx = 0; idx < len; idx++)
11118 {
11119 if (expanded_bases)
11120 /* Extract the already-expanded base class. */
11121 base = TREE_VEC_ELT (expanded_bases, idx);
11122 else
11123 /* Substitute to figure out the base class. */
11124 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11125 NULL_TREE);
11126
11127 if (base == error_mark_node)
11128 continue;
11129
11130 base_list = tree_cons (access, base, base_list);
11131 if (BINFO_VIRTUAL_P (pbase_binfo))
11132 TREE_TYPE (base_list) = integer_type_node;
11133 }
11134 }
11135
11136 /* The list is now in reverse order; correct that. */
11137 base_list = nreverse (base_list);
11138
11139 if (pushed_scope)
11140 pop_scope (pushed_scope);
11141 }
11142 /* Now call xref_basetypes to set up all the base-class
11143 information. */
11144 xref_basetypes (type, base_list);
11145
11146 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11147 (int) ATTR_FLAG_TYPE_IN_PLACE,
11148 args, tf_error, NULL_TREE);
11149 fixup_attribute_variants (type);
11150
11151 /* Now that our base classes are set up, enter the scope of the
11152 class, so that name lookups into base classes, etc. will work
11153 correctly. This is precisely analogous to what we do in
11154 begin_class_definition when defining an ordinary non-template
11155 class, except we also need to push the enclosing classes. */
11156 push_nested_class (type);
11157
11158 /* Now members are processed in the order of declaration. */
11159 for (member = CLASSTYPE_DECL_LIST (pattern);
11160 member; member = TREE_CHAIN (member))
11161 {
11162 tree t = TREE_VALUE (member);
11163
11164 if (TREE_PURPOSE (member))
11165 {
11166 if (TYPE_P (t))
11167 {
11168 if (LAMBDA_TYPE_P (t))
11169 /* A closure type for a lambda in an NSDMI or default argument.
11170 Ignore it; it will be regenerated when needed. */
11171 continue;
11172
11173 /* Build new CLASSTYPE_NESTED_UTDS. */
11174
11175 tree newtag;
11176 bool class_template_p;
11177
11178 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11179 && TYPE_LANG_SPECIFIC (t)
11180 && CLASSTYPE_IS_TEMPLATE (t));
11181 /* If the member is a class template, then -- even after
11182 substitution -- there may be dependent types in the
11183 template argument list for the class. We increment
11184 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11185 that function will assume that no types are dependent
11186 when outside of a template. */
11187 if (class_template_p)
11188 ++processing_template_decl;
11189 newtag = tsubst (t, args, tf_error, NULL_TREE);
11190 if (class_template_p)
11191 --processing_template_decl;
11192 if (newtag == error_mark_node)
11193 continue;
11194
11195 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11196 {
11197 tree name = TYPE_IDENTIFIER (t);
11198
11199 if (class_template_p)
11200 /* Unfortunately, lookup_template_class sets
11201 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11202 instantiation (i.e., for the type of a member
11203 template class nested within a template class.)
11204 This behavior is required for
11205 maybe_process_partial_specialization to work
11206 correctly, but is not accurate in this case;
11207 the TAG is not an instantiation of anything.
11208 (The corresponding TEMPLATE_DECL is an
11209 instantiation, but the TYPE is not.) */
11210 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11211
11212 /* Now, we call pushtag to put this NEWTAG into the scope of
11213 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11214 pushtag calling push_template_decl. We don't have to do
11215 this for enums because it will already have been done in
11216 tsubst_enum. */
11217 if (name)
11218 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11219 pushtag (name, newtag, /*tag_scope=*/ts_current);
11220 }
11221 }
11222 else if (DECL_DECLARES_FUNCTION_P (t))
11223 {
11224 tree r;
11225
11226 if (TREE_CODE (t) == TEMPLATE_DECL)
11227 ++processing_template_decl;
11228 r = tsubst (t, args, tf_error, NULL_TREE);
11229 if (TREE_CODE (t) == TEMPLATE_DECL)
11230 --processing_template_decl;
11231 set_current_access_from_decl (r);
11232 finish_member_declaration (r);
11233 /* Instantiate members marked with attribute used. */
11234 if (r != error_mark_node && DECL_PRESERVE_P (r))
11235 mark_used (r);
11236 if (TREE_CODE (r) == FUNCTION_DECL
11237 && DECL_OMP_DECLARE_REDUCTION_P (r))
11238 cp_check_omp_declare_reduction (r);
11239 }
11240 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11241 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11242 /* A closure type for a lambda in an NSDMI or default argument.
11243 Ignore it; it will be regenerated when needed. */;
11244 else
11245 {
11246 /* Build new TYPE_FIELDS. */
11247 if (TREE_CODE (t) == STATIC_ASSERT)
11248 {
11249 tree condition;
11250
11251 ++c_inhibit_evaluation_warnings;
11252 condition =
11253 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11254 tf_warning_or_error, NULL_TREE,
11255 /*integral_constant_expression_p=*/true);
11256 --c_inhibit_evaluation_warnings;
11257
11258 finish_static_assert (condition,
11259 STATIC_ASSERT_MESSAGE (t),
11260 STATIC_ASSERT_SOURCE_LOCATION (t),
11261 /*member_p=*/true);
11262 }
11263 else if (TREE_CODE (t) != CONST_DECL)
11264 {
11265 tree r;
11266 tree vec = NULL_TREE;
11267 int len = 1;
11268
11269 /* The file and line for this declaration, to
11270 assist in error message reporting. Since we
11271 called push_tinst_level above, we don't need to
11272 restore these. */
11273 input_location = DECL_SOURCE_LOCATION (t);
11274
11275 if (TREE_CODE (t) == TEMPLATE_DECL)
11276 ++processing_template_decl;
11277 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11278 if (TREE_CODE (t) == TEMPLATE_DECL)
11279 --processing_template_decl;
11280
11281 if (TREE_CODE (r) == TREE_VEC)
11282 {
11283 /* A capture pack became multiple fields. */
11284 vec = r;
11285 len = TREE_VEC_LENGTH (vec);
11286 }
11287
11288 for (int i = 0; i < len; ++i)
11289 {
11290 if (vec)
11291 r = TREE_VEC_ELT (vec, i);
11292 if (VAR_P (r))
11293 {
11294 /* In [temp.inst]:
11295
11296 [t]he initialization (and any associated
11297 side-effects) of a static data member does
11298 not occur unless the static data member is
11299 itself used in a way that requires the
11300 definition of the static data member to
11301 exist.
11302
11303 Therefore, we do not substitute into the
11304 initialized for the static data member here. */
11305 finish_static_data_member_decl
11306 (r,
11307 /*init=*/NULL_TREE,
11308 /*init_const_expr_p=*/false,
11309 /*asmspec_tree=*/NULL_TREE,
11310 /*flags=*/0);
11311 /* Instantiate members marked with attribute used. */
11312 if (r != error_mark_node && DECL_PRESERVE_P (r))
11313 mark_used (r);
11314 }
11315 else if (TREE_CODE (r) == FIELD_DECL)
11316 {
11317 /* Determine whether R has a valid type and can be
11318 completed later. If R is invalid, then its type
11319 is replaced by error_mark_node. */
11320 tree rtype = TREE_TYPE (r);
11321 if (can_complete_type_without_circularity (rtype))
11322 complete_type (rtype);
11323
11324 if (!complete_or_array_type_p (rtype))
11325 {
11326 /* If R's type couldn't be completed and
11327 it isn't a flexible array member (whose
11328 type is incomplete by definition) give
11329 an error. */
11330 cxx_incomplete_type_error (r, rtype);
11331 TREE_TYPE (r) = error_mark_node;
11332 }
11333 else if (TREE_CODE (rtype) == ARRAY_TYPE
11334 && TYPE_DOMAIN (rtype) == NULL_TREE
11335 && (TREE_CODE (type) == UNION_TYPE
11336 || TREE_CODE (type) == QUAL_UNION_TYPE))
11337 {
11338 error ("flexible array member %qD in union", r);
11339 TREE_TYPE (r) = error_mark_node;
11340 }
11341 }
11342
11343 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11344 such a thing will already have been added to the field
11345 list by tsubst_enum in finish_member_declaration in the
11346 CLASSTYPE_NESTED_UTDS case above. */
11347 if (!(TREE_CODE (r) == TYPE_DECL
11348 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11349 && DECL_ARTIFICIAL (r)))
11350 {
11351 set_current_access_from_decl (r);
11352 finish_member_declaration (r);
11353 }
11354 }
11355 }
11356 }
11357 }
11358 else
11359 {
11360 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11361 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11362 {
11363 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11364
11365 tree friend_type = t;
11366 bool adjust_processing_template_decl = false;
11367
11368 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11369 {
11370 /* template <class T> friend class C; */
11371 friend_type = tsubst_friend_class (friend_type, args);
11372 adjust_processing_template_decl = true;
11373 }
11374 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11375 {
11376 /* template <class T> friend class C::D; */
11377 friend_type = tsubst (friend_type, args,
11378 tf_warning_or_error, NULL_TREE);
11379 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11380 friend_type = TREE_TYPE (friend_type);
11381 adjust_processing_template_decl = true;
11382 }
11383 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11384 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11385 {
11386 /* This could be either
11387
11388 friend class T::C;
11389
11390 when dependent_type_p is false or
11391
11392 template <class U> friend class T::C;
11393
11394 otherwise. */
11395 /* Bump processing_template_decl in case this is something like
11396 template <class T> friend struct A<T>::B. */
11397 ++processing_template_decl;
11398 friend_type = tsubst (friend_type, args,
11399 tf_warning_or_error, NULL_TREE);
11400 if (dependent_type_p (friend_type))
11401 adjust_processing_template_decl = true;
11402 --processing_template_decl;
11403 }
11404 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11405 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11406 && TYPE_HIDDEN_P (friend_type))
11407 {
11408 /* friend class C;
11409
11410 where C hasn't been declared yet. Let's lookup name
11411 from namespace scope directly, bypassing any name that
11412 come from dependent base class. */
11413 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11414
11415 /* The call to xref_tag_from_type does injection for friend
11416 classes. */
11417 push_nested_namespace (ns);
11418 friend_type =
11419 xref_tag_from_type (friend_type, NULL_TREE,
11420 /*tag_scope=*/ts_current);
11421 pop_nested_namespace (ns);
11422 }
11423 else if (uses_template_parms (friend_type))
11424 /* friend class C<T>; */
11425 friend_type = tsubst (friend_type, args,
11426 tf_warning_or_error, NULL_TREE);
11427 /* Otherwise it's
11428
11429 friend class C;
11430
11431 where C is already declared or
11432
11433 friend class C<int>;
11434
11435 We don't have to do anything in these cases. */
11436
11437 if (adjust_processing_template_decl)
11438 /* Trick make_friend_class into realizing that the friend
11439 we're adding is a template, not an ordinary class. It's
11440 important that we use make_friend_class since it will
11441 perform some error-checking and output cross-reference
11442 information. */
11443 ++processing_template_decl;
11444
11445 if (friend_type != error_mark_node)
11446 make_friend_class (type, friend_type, /*complain=*/false);
11447
11448 if (adjust_processing_template_decl)
11449 --processing_template_decl;
11450 }
11451 else
11452 {
11453 /* Build new DECL_FRIENDLIST. */
11454 tree r;
11455
11456 /* The file and line for this declaration, to
11457 assist in error message reporting. Since we
11458 called push_tinst_level above, we don't need to
11459 restore these. */
11460 input_location = DECL_SOURCE_LOCATION (t);
11461
11462 if (TREE_CODE (t) == TEMPLATE_DECL)
11463 {
11464 ++processing_template_decl;
11465 push_deferring_access_checks (dk_no_check);
11466 }
11467
11468 r = tsubst_friend_function (t, args);
11469 add_friend (type, r, /*complain=*/false);
11470 if (TREE_CODE (t) == TEMPLATE_DECL)
11471 {
11472 pop_deferring_access_checks ();
11473 --processing_template_decl;
11474 }
11475 }
11476 }
11477 }
11478
11479 if (fn_context)
11480 {
11481 /* Restore these before substituting into the lambda capture
11482 initializers. */
11483 cp_unevaluated_operand = saved_unevaluated_operand;
11484 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11485 }
11486
11487 /* Set the file and line number information to whatever is given for
11488 the class itself. This puts error messages involving generated
11489 implicit functions at a predictable point, and the same point
11490 that would be used for non-template classes. */
11491 input_location = DECL_SOURCE_LOCATION (typedecl);
11492
11493 unreverse_member_declarations (type);
11494 finish_struct_1 (type);
11495 TYPE_BEING_DEFINED (type) = 0;
11496
11497 /* We don't instantiate default arguments for member functions. 14.7.1:
11498
11499 The implicit instantiation of a class template specialization causes
11500 the implicit instantiation of the declarations, but not of the
11501 definitions or default arguments, of the class member functions,
11502 member classes, static data members and member templates.... */
11503
11504 /* Some typedefs referenced from within the template code need to be access
11505 checked at template instantiation time, i.e now. These types were
11506 added to the template at parsing time. Let's get those and perform
11507 the access checks then. */
11508 perform_typedefs_access_check (pattern, args);
11509 perform_deferred_access_checks (tf_warning_or_error);
11510 pop_nested_class ();
11511 maximum_field_alignment = saved_maximum_field_alignment;
11512 if (!fn_context)
11513 pop_from_top_level ();
11514 pop_tinst_level ();
11515
11516 /* The vtable for a template class can be emitted in any translation
11517 unit in which the class is instantiated. When there is no key
11518 method, however, finish_struct_1 will already have added TYPE to
11519 the keyed_classes. */
11520 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11521 vec_safe_push (keyed_classes, type);
11522
11523 return type;
11524 }
11525
11526 /* Wrapper for instantiate_class_template_1. */
11527
11528 tree
11529 instantiate_class_template (tree type)
11530 {
11531 tree ret;
11532 timevar_push (TV_TEMPLATE_INST);
11533 ret = instantiate_class_template_1 (type);
11534 timevar_pop (TV_TEMPLATE_INST);
11535 return ret;
11536 }
11537
11538 static tree
11539 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11540 {
11541 tree r;
11542
11543 if (!t)
11544 r = t;
11545 else if (TYPE_P (t))
11546 r = tsubst (t, args, complain, in_decl);
11547 else
11548 {
11549 if (!(complain & tf_warning))
11550 ++c_inhibit_evaluation_warnings;
11551 r = tsubst_expr (t, args, complain, in_decl,
11552 /*integral_constant_expression_p=*/true);
11553 if (!(complain & tf_warning))
11554 --c_inhibit_evaluation_warnings;
11555 }
11556 return r;
11557 }
11558
11559 /* Given a function parameter pack TMPL_PARM and some function parameters
11560 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11561 and set *SPEC_P to point at the next point in the list. */
11562
11563 tree
11564 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11565 {
11566 /* Collect all of the extra "packed" parameters into an
11567 argument pack. */
11568 tree parmvec;
11569 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11570 tree spec_parm = *spec_p;
11571 int i, len;
11572
11573 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11574 if (tmpl_parm
11575 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11576 break;
11577
11578 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11579 parmvec = make_tree_vec (len);
11580 spec_parm = *spec_p;
11581 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11582 {
11583 tree elt = spec_parm;
11584 if (DECL_PACK_P (elt))
11585 elt = make_pack_expansion (elt);
11586 TREE_VEC_ELT (parmvec, i) = elt;
11587 }
11588
11589 /* Build the argument packs. */
11590 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11591 *spec_p = spec_parm;
11592
11593 return argpack;
11594 }
11595
11596 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11597 NONTYPE_ARGUMENT_PACK. */
11598
11599 static tree
11600 make_fnparm_pack (tree spec_parm)
11601 {
11602 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11603 }
11604
11605 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11606 pack expansion with no extra args, 2 if it has extra args, or 0
11607 if it is not a pack expansion. */
11608
11609 static int
11610 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11611 {
11612 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11613 /* We're being called before this happens in tsubst_pack_expansion. */
11614 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11615 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11616 if (i >= TREE_VEC_LENGTH (vec))
11617 return 0;
11618 tree elt = TREE_VEC_ELT (vec, i);
11619 if (DECL_P (elt))
11620 /* A decl pack is itself an expansion. */
11621 elt = TREE_TYPE (elt);
11622 if (!PACK_EXPANSION_P (elt))
11623 return 0;
11624 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11625 return 2;
11626 return 1;
11627 }
11628
11629
11630 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11631
11632 static tree
11633 make_argument_pack_select (tree arg_pack, unsigned index)
11634 {
11635 tree aps = make_node (ARGUMENT_PACK_SELECT);
11636
11637 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11638 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11639
11640 return aps;
11641 }
11642
11643 /* This is a subroutine of tsubst_pack_expansion.
11644
11645 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11646 mechanism to store the (non complete list of) arguments of the
11647 substitution and return a non substituted pack expansion, in order
11648 to wait for when we have enough arguments to really perform the
11649 substitution. */
11650
11651 static bool
11652 use_pack_expansion_extra_args_p (tree parm_packs,
11653 int arg_pack_len,
11654 bool has_empty_arg)
11655 {
11656 /* If one pack has an expansion and another pack has a normal
11657 argument or if one pack has an empty argument and an another
11658 one hasn't then tsubst_pack_expansion cannot perform the
11659 substitution and need to fall back on the
11660 PACK_EXPANSION_EXTRA mechanism. */
11661 if (parm_packs == NULL_TREE)
11662 return false;
11663 else if (has_empty_arg)
11664 return true;
11665
11666 bool has_expansion_arg = false;
11667 for (int i = 0 ; i < arg_pack_len; ++i)
11668 {
11669 bool has_non_expansion_arg = false;
11670 for (tree parm_pack = parm_packs;
11671 parm_pack;
11672 parm_pack = TREE_CHAIN (parm_pack))
11673 {
11674 tree arg = TREE_VALUE (parm_pack);
11675
11676 int exp = argument_pack_element_is_expansion_p (arg, i);
11677 if (exp == 2)
11678 /* We can't substitute a pack expansion with extra args into
11679 our pattern. */
11680 return true;
11681 else if (exp)
11682 has_expansion_arg = true;
11683 else
11684 has_non_expansion_arg = true;
11685 }
11686
11687 if (has_expansion_arg && has_non_expansion_arg)
11688 return true;
11689 }
11690 return false;
11691 }
11692
11693 /* [temp.variadic]/6 says that:
11694
11695 The instantiation of a pack expansion [...]
11696 produces a list E1,E2, ..., En, where N is the number of elements
11697 in the pack expansion parameters.
11698
11699 This subroutine of tsubst_pack_expansion produces one of these Ei.
11700
11701 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11702 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11703 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11704 INDEX is the index 'i' of the element Ei to produce. ARGS,
11705 COMPLAIN, and IN_DECL are the same parameters as for the
11706 tsubst_pack_expansion function.
11707
11708 The function returns the resulting Ei upon successful completion,
11709 or error_mark_node.
11710
11711 Note that this function possibly modifies the ARGS parameter, so
11712 it's the responsibility of the caller to restore it. */
11713
11714 static tree
11715 gen_elem_of_pack_expansion_instantiation (tree pattern,
11716 tree parm_packs,
11717 unsigned index,
11718 tree args /* This parm gets
11719 modified. */,
11720 tsubst_flags_t complain,
11721 tree in_decl)
11722 {
11723 tree t;
11724 bool ith_elem_is_expansion = false;
11725
11726 /* For each parameter pack, change the substitution of the parameter
11727 pack to the ith argument in its argument pack, then expand the
11728 pattern. */
11729 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11730 {
11731 tree parm = TREE_PURPOSE (pack);
11732 tree arg_pack = TREE_VALUE (pack);
11733 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11734
11735 ith_elem_is_expansion |=
11736 argument_pack_element_is_expansion_p (arg_pack, index);
11737
11738 /* Select the Ith argument from the pack. */
11739 if (TREE_CODE (parm) == PARM_DECL
11740 || VAR_P (parm)
11741 || TREE_CODE (parm) == FIELD_DECL)
11742 {
11743 if (index == 0)
11744 {
11745 aps = make_argument_pack_select (arg_pack, index);
11746 if (!mark_used (parm, complain) && !(complain & tf_error))
11747 return error_mark_node;
11748 register_local_specialization (aps, parm);
11749 }
11750 else
11751 aps = retrieve_local_specialization (parm);
11752 }
11753 else
11754 {
11755 int idx, level;
11756 template_parm_level_and_index (parm, &level, &idx);
11757
11758 if (index == 0)
11759 {
11760 aps = make_argument_pack_select (arg_pack, index);
11761 /* Update the corresponding argument. */
11762 TMPL_ARG (args, level, idx) = aps;
11763 }
11764 else
11765 /* Re-use the ARGUMENT_PACK_SELECT. */
11766 aps = TMPL_ARG (args, level, idx);
11767 }
11768 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11769 }
11770
11771 // Any local specialization bindings arising from this substitution
11772 // cannot be reused for a different INDEX.
11773 local_specialization_stack lss (lss_copy);
11774
11775 /* Substitute into the PATTERN with the (possibly altered)
11776 arguments. */
11777 if (pattern == in_decl)
11778 /* Expanding a fixed parameter pack from
11779 coerce_template_parameter_pack. */
11780 t = tsubst_decl (pattern, args, complain);
11781 else if (pattern == error_mark_node)
11782 t = error_mark_node;
11783 else if (constraint_p (pattern))
11784 {
11785 if (processing_template_decl)
11786 t = tsubst_constraint (pattern, args, complain, in_decl);
11787 else
11788 t = (constraints_satisfied_p (pattern, args)
11789 ? boolean_true_node : boolean_false_node);
11790 }
11791 else if (!TYPE_P (pattern))
11792 t = tsubst_expr (pattern, args, complain, in_decl,
11793 /*integral_constant_expression_p=*/false);
11794 else
11795 t = tsubst (pattern, args, complain, in_decl);
11796
11797 /* If the Ith argument pack element is a pack expansion, then
11798 the Ith element resulting from the substituting is going to
11799 be a pack expansion as well. */
11800 if (ith_elem_is_expansion)
11801 t = make_pack_expansion (t, complain);
11802
11803 return t;
11804 }
11805
11806 /* When the unexpanded parameter pack in a fold expression expands to an empty
11807 sequence, the value of the expression is as follows; the program is
11808 ill-formed if the operator is not listed in this table.
11809
11810 && true
11811 || false
11812 , void() */
11813
11814 tree
11815 expand_empty_fold (tree t, tsubst_flags_t complain)
11816 {
11817 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11818 if (!FOLD_EXPR_MODIFY_P (t))
11819 switch (code)
11820 {
11821 case TRUTH_ANDIF_EXPR:
11822 return boolean_true_node;
11823 case TRUTH_ORIF_EXPR:
11824 return boolean_false_node;
11825 case COMPOUND_EXPR:
11826 return void_node;
11827 default:
11828 break;
11829 }
11830
11831 if (complain & tf_error)
11832 error_at (location_of (t),
11833 "fold of empty expansion over %O", code);
11834 return error_mark_node;
11835 }
11836
11837 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11838 form an expression that combines the two terms using the
11839 operator of T. */
11840
11841 static tree
11842 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11843 {
11844 tree op = FOLD_EXPR_OP (t);
11845 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11846
11847 // Handle compound assignment operators.
11848 if (FOLD_EXPR_MODIFY_P (t))
11849 return build_x_modify_expr (input_location, left, code, right, complain);
11850
11851 switch (code)
11852 {
11853 case COMPOUND_EXPR:
11854 return build_x_compound_expr (input_location, left, right, complain);
11855 default:
11856 return build_x_binary_op (input_location, code,
11857 left, TREE_CODE (left),
11858 right, TREE_CODE (right),
11859 /*overload=*/NULL,
11860 complain);
11861 }
11862 }
11863
11864 /* Substitute ARGS into the pack of a fold expression T. */
11865
11866 static inline tree
11867 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11868 {
11869 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11870 }
11871
11872 /* Substitute ARGS into the pack of a fold expression T. */
11873
11874 static inline tree
11875 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11876 {
11877 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11878 }
11879
11880 /* Expand a PACK of arguments into a grouped as left fold.
11881 Given a pack containing elements A0, A1, ..., An and an
11882 operator @, this builds the expression:
11883
11884 ((A0 @ A1) @ A2) ... @ An
11885
11886 Note that PACK must not be empty.
11887
11888 The operator is defined by the original fold expression T. */
11889
11890 static tree
11891 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11892 {
11893 tree left = TREE_VEC_ELT (pack, 0);
11894 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11895 {
11896 tree right = TREE_VEC_ELT (pack, i);
11897 left = fold_expression (t, left, right, complain);
11898 }
11899 return left;
11900 }
11901
11902 /* Substitute into a unary left fold expression. */
11903
11904 static tree
11905 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11906 tree in_decl)
11907 {
11908 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11909 if (pack == error_mark_node)
11910 return error_mark_node;
11911 if (PACK_EXPANSION_P (pack))
11912 {
11913 tree r = copy_node (t);
11914 FOLD_EXPR_PACK (r) = pack;
11915 return r;
11916 }
11917 if (TREE_VEC_LENGTH (pack) == 0)
11918 return expand_empty_fold (t, complain);
11919 else
11920 return expand_left_fold (t, pack, complain);
11921 }
11922
11923 /* Substitute into a binary left fold expression.
11924
11925 Do ths by building a single (non-empty) vector of argumnts and
11926 building the expression from those elements. */
11927
11928 static tree
11929 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11930 tree in_decl)
11931 {
11932 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11933 if (pack == error_mark_node)
11934 return error_mark_node;
11935 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11936 if (init == error_mark_node)
11937 return error_mark_node;
11938
11939 if (PACK_EXPANSION_P (pack))
11940 {
11941 tree r = copy_node (t);
11942 FOLD_EXPR_PACK (r) = pack;
11943 FOLD_EXPR_INIT (r) = init;
11944 return r;
11945 }
11946
11947 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11948 TREE_VEC_ELT (vec, 0) = init;
11949 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11950 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11951
11952 return expand_left_fold (t, vec, complain);
11953 }
11954
11955 /* Expand a PACK of arguments into a grouped as right fold.
11956 Given a pack containing elementns A0, A1, ..., and an
11957 operator @, this builds the expression:
11958
11959 A0@ ... (An-2 @ (An-1 @ An))
11960
11961 Note that PACK must not be empty.
11962
11963 The operator is defined by the original fold expression T. */
11964
11965 tree
11966 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11967 {
11968 // Build the expression.
11969 int n = TREE_VEC_LENGTH (pack);
11970 tree right = TREE_VEC_ELT (pack, n - 1);
11971 for (--n; n != 0; --n)
11972 {
11973 tree left = TREE_VEC_ELT (pack, n - 1);
11974 right = fold_expression (t, left, right, complain);
11975 }
11976 return right;
11977 }
11978
11979 /* Substitute into a unary right fold expression. */
11980
11981 static tree
11982 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11983 tree in_decl)
11984 {
11985 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11986 if (pack == error_mark_node)
11987 return error_mark_node;
11988 if (PACK_EXPANSION_P (pack))
11989 {
11990 tree r = copy_node (t);
11991 FOLD_EXPR_PACK (r) = pack;
11992 return r;
11993 }
11994 if (TREE_VEC_LENGTH (pack) == 0)
11995 return expand_empty_fold (t, complain);
11996 else
11997 return expand_right_fold (t, pack, complain);
11998 }
11999
12000 /* Substitute into a binary right fold expression.
12001
12002 Do ths by building a single (non-empty) vector of arguments and
12003 building the expression from those elements. */
12004
12005 static tree
12006 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12007 tree in_decl)
12008 {
12009 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12010 if (pack == error_mark_node)
12011 return error_mark_node;
12012 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12013 if (init == error_mark_node)
12014 return error_mark_node;
12015
12016 if (PACK_EXPANSION_P (pack))
12017 {
12018 tree r = copy_node (t);
12019 FOLD_EXPR_PACK (r) = pack;
12020 FOLD_EXPR_INIT (r) = init;
12021 return r;
12022 }
12023
12024 int n = TREE_VEC_LENGTH (pack);
12025 tree vec = make_tree_vec (n + 1);
12026 for (int i = 0; i < n; ++i)
12027 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12028 TREE_VEC_ELT (vec, n) = init;
12029
12030 return expand_right_fold (t, vec, complain);
12031 }
12032
12033 /* Walk through the pattern of a pack expansion, adding everything in
12034 local_specializations to a list. */
12035
12036 class el_data
12037 {
12038 public:
12039 hash_set<tree> internal;
12040 tree extra;
12041 tsubst_flags_t complain;
12042
12043 el_data (tsubst_flags_t c)
12044 : extra (NULL_TREE), complain (c) {}
12045 };
12046 static tree
12047 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12048 {
12049 el_data &data = *reinterpret_cast<el_data*>(data_);
12050 tree *extra = &data.extra;
12051 tsubst_flags_t complain = data.complain;
12052
12053 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12054 /* Remember local typedefs (85214). */
12055 tp = &TYPE_NAME (*tp);
12056
12057 if (TREE_CODE (*tp) == DECL_EXPR)
12058 data.internal.add (DECL_EXPR_DECL (*tp));
12059 else if (tree spec = retrieve_local_specialization (*tp))
12060 {
12061 if (data.internal.contains (*tp))
12062 /* Don't mess with variables declared within the pattern. */
12063 return NULL_TREE;
12064 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12065 {
12066 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12067 tree args = ARGUMENT_PACK_ARGS (spec);
12068 if (TREE_VEC_LENGTH (args) == 1)
12069 {
12070 tree elt = TREE_VEC_ELT (args, 0);
12071 if (PACK_EXPANSION_P (elt))
12072 elt = PACK_EXPANSION_PATTERN (elt);
12073 if (DECL_PACK_P (elt))
12074 spec = elt;
12075 }
12076 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12077 {
12078 /* Handle lambda capture here, since we aren't doing any
12079 substitution now, and so tsubst_copy won't call
12080 process_outer_var_ref. */
12081 tree args = ARGUMENT_PACK_ARGS (spec);
12082 int len = TREE_VEC_LENGTH (args);
12083 for (int i = 0; i < len; ++i)
12084 {
12085 tree arg = TREE_VEC_ELT (args, i);
12086 tree carg = arg;
12087 if (outer_automatic_var_p (arg))
12088 carg = process_outer_var_ref (arg, complain);
12089 if (carg != arg)
12090 {
12091 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12092 proxies. */
12093 if (i == 0)
12094 {
12095 spec = copy_node (spec);
12096 args = copy_node (args);
12097 SET_ARGUMENT_PACK_ARGS (spec, args);
12098 register_local_specialization (spec, *tp);
12099 }
12100 TREE_VEC_ELT (args, i) = carg;
12101 }
12102 }
12103 }
12104 }
12105 if (outer_automatic_var_p (spec))
12106 spec = process_outer_var_ref (spec, complain);
12107 *extra = tree_cons (*tp, spec, *extra);
12108 }
12109 return NULL_TREE;
12110 }
12111 static tree
12112 extract_local_specs (tree pattern, tsubst_flags_t complain)
12113 {
12114 el_data data (complain);
12115 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12116 return data.extra;
12117 }
12118
12119 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12120 for use in PACK_EXPANSION_EXTRA_ARGS. */
12121
12122 tree
12123 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12124 {
12125 tree extra = args;
12126 if (local_specializations)
12127 if (tree locals = extract_local_specs (pattern, complain))
12128 extra = tree_cons (NULL_TREE, extra, locals);
12129 return extra;
12130 }
12131
12132 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12133 normal template args to ARGS. */
12134
12135 tree
12136 add_extra_args (tree extra, tree args)
12137 {
12138 if (extra && TREE_CODE (extra) == TREE_LIST)
12139 {
12140 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12141 {
12142 /* The partial instantiation involved local declarations collected in
12143 extract_local_specs; map from the general template to our local
12144 context. */
12145 tree gen = TREE_PURPOSE (elt);
12146 tree inst = TREE_VALUE (elt);
12147 if (DECL_P (inst))
12148 if (tree local = retrieve_local_specialization (inst))
12149 inst = local;
12150 /* else inst is already a full instantiation of the pack. */
12151 register_local_specialization (inst, gen);
12152 }
12153 gcc_assert (!TREE_PURPOSE (extra));
12154 extra = TREE_VALUE (extra);
12155 }
12156 return add_to_template_args (extra, args);
12157 }
12158
12159 /* Substitute ARGS into T, which is an pack expansion
12160 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12161 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12162 (if only a partial substitution could be performed) or
12163 ERROR_MARK_NODE if there was an error. */
12164 tree
12165 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12166 tree in_decl)
12167 {
12168 tree pattern;
12169 tree pack, packs = NULL_TREE;
12170 bool unsubstituted_packs = false;
12171 bool unsubstituted_fn_pack = false;
12172 int i, len = -1;
12173 tree result;
12174 hash_map<tree, tree> *saved_local_specializations = NULL;
12175 bool need_local_specializations = false;
12176 int levels;
12177
12178 gcc_assert (PACK_EXPANSION_P (t));
12179 pattern = PACK_EXPANSION_PATTERN (t);
12180
12181 /* Add in any args remembered from an earlier partial instantiation. */
12182 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12183
12184 levels = TMPL_ARGS_DEPTH (args);
12185
12186 /* Determine the argument packs that will instantiate the parameter
12187 packs used in the expansion expression. While we're at it,
12188 compute the number of arguments to be expanded and make sure it
12189 is consistent. */
12190 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12191 pack = TREE_CHAIN (pack))
12192 {
12193 tree parm_pack = TREE_VALUE (pack);
12194 tree arg_pack = NULL_TREE;
12195 tree orig_arg = NULL_TREE;
12196 int level = 0;
12197
12198 if (TREE_CODE (parm_pack) == BASES)
12199 {
12200 gcc_assert (parm_pack == pattern);
12201 if (BASES_DIRECT (parm_pack))
12202 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12203 args, complain,
12204 in_decl, false),
12205 complain);
12206 else
12207 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12208 args, complain, in_decl,
12209 false), complain);
12210 }
12211 else if (builtin_pack_call_p (parm_pack))
12212 {
12213 if (parm_pack != pattern)
12214 {
12215 if (complain & tf_error)
12216 sorry ("%qE is not the entire pattern of the pack expansion",
12217 parm_pack);
12218 return error_mark_node;
12219 }
12220 return expand_builtin_pack_call (parm_pack, args,
12221 complain, in_decl);
12222 }
12223 else if (TREE_CODE (parm_pack) == PARM_DECL)
12224 {
12225 /* We know we have correct local_specializations if this
12226 expansion is at function scope, or if we're dealing with a
12227 local parameter in a requires expression; for the latter,
12228 tsubst_requires_expr set it up appropriately. */
12229 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12230 arg_pack = retrieve_local_specialization (parm_pack);
12231 else
12232 /* We can't rely on local_specializations for a parameter
12233 name used later in a function declaration (such as in a
12234 late-specified return type). Even if it exists, it might
12235 have the wrong value for a recursive call. */
12236 need_local_specializations = true;
12237
12238 if (!arg_pack)
12239 {
12240 /* This parameter pack was used in an unevaluated context. Just
12241 make a dummy decl, since it's only used for its type. */
12242 ++cp_unevaluated_operand;
12243 arg_pack = tsubst_decl (parm_pack, args, complain);
12244 --cp_unevaluated_operand;
12245 if (arg_pack && DECL_PACK_P (arg_pack))
12246 /* Partial instantiation of the parm_pack, we can't build
12247 up an argument pack yet. */
12248 arg_pack = NULL_TREE;
12249 else
12250 arg_pack = make_fnparm_pack (arg_pack);
12251 }
12252 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12253 /* This argument pack isn't fully instantiated yet. We set this
12254 flag rather than clear arg_pack because we do want to do the
12255 optimization below, and we don't want to substitute directly
12256 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12257 where it isn't expected). */
12258 unsubstituted_fn_pack = true;
12259 }
12260 else if (is_capture_proxy (parm_pack))
12261 {
12262 arg_pack = retrieve_local_specialization (parm_pack);
12263 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12264 unsubstituted_fn_pack = true;
12265 }
12266 else
12267 {
12268 int idx;
12269 template_parm_level_and_index (parm_pack, &level, &idx);
12270
12271 if (level <= levels)
12272 arg_pack = TMPL_ARG (args, level, idx);
12273 }
12274
12275 orig_arg = arg_pack;
12276 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12277 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12278
12279 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12280 /* This can only happen if we forget to expand an argument
12281 pack somewhere else. Just return an error, silently. */
12282 {
12283 result = make_tree_vec (1);
12284 TREE_VEC_ELT (result, 0) = error_mark_node;
12285 return result;
12286 }
12287
12288 if (arg_pack)
12289 {
12290 int my_len =
12291 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12292
12293 /* Don't bother trying to do a partial substitution with
12294 incomplete packs; we'll try again after deduction. */
12295 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12296 return t;
12297
12298 if (len < 0)
12299 len = my_len;
12300 else if (len != my_len
12301 && !unsubstituted_fn_pack)
12302 {
12303 if (!(complain & tf_error))
12304 /* Fail quietly. */;
12305 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12306 error ("mismatched argument pack lengths while expanding %qT",
12307 pattern);
12308 else
12309 error ("mismatched argument pack lengths while expanding %qE",
12310 pattern);
12311 return error_mark_node;
12312 }
12313
12314 /* Keep track of the parameter packs and their corresponding
12315 argument packs. */
12316 packs = tree_cons (parm_pack, arg_pack, packs);
12317 TREE_TYPE (packs) = orig_arg;
12318 }
12319 else
12320 {
12321 /* We can't substitute for this parameter pack. We use a flag as
12322 well as the missing_level counter because function parameter
12323 packs don't have a level. */
12324 gcc_assert (processing_template_decl || is_auto (parm_pack));
12325 unsubstituted_packs = true;
12326 }
12327 }
12328
12329 /* If the expansion is just T..., return the matching argument pack, unless
12330 we need to call convert_from_reference on all the elements. This is an
12331 important optimization; see c++/68422. */
12332 if (!unsubstituted_packs
12333 && TREE_PURPOSE (packs) == pattern)
12334 {
12335 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12336
12337 /* If the argument pack is a single pack expansion, pull it out. */
12338 if (TREE_VEC_LENGTH (args) == 1
12339 && pack_expansion_args_count (args))
12340 return TREE_VEC_ELT (args, 0);
12341
12342 /* Types need no adjustment, nor does sizeof..., and if we still have
12343 some pack expansion args we won't do anything yet. */
12344 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12345 || PACK_EXPANSION_SIZEOF_P (t)
12346 || pack_expansion_args_count (args))
12347 return args;
12348 /* Also optimize expression pack expansions if we can tell that the
12349 elements won't have reference type. */
12350 tree type = TREE_TYPE (pattern);
12351 if (type && !TYPE_REF_P (type)
12352 && !PACK_EXPANSION_P (type)
12353 && !WILDCARD_TYPE_P (type))
12354 return args;
12355 /* Otherwise use the normal path so we get convert_from_reference. */
12356 }
12357
12358 /* We cannot expand this expansion expression, because we don't have
12359 all of the argument packs we need. */
12360 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12361 {
12362 /* We got some full packs, but we can't substitute them in until we
12363 have values for all the packs. So remember these until then. */
12364
12365 t = make_pack_expansion (pattern, complain);
12366 PACK_EXPANSION_EXTRA_ARGS (t)
12367 = build_extra_args (pattern, args, complain);
12368 return t;
12369 }
12370 else if (unsubstituted_packs)
12371 {
12372 /* There were no real arguments, we're just replacing a parameter
12373 pack with another version of itself. Substitute into the
12374 pattern and return a PACK_EXPANSION_*. The caller will need to
12375 deal with that. */
12376 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12377 t = tsubst_expr (pattern, args, complain, in_decl,
12378 /*integral_constant_expression_p=*/false);
12379 else
12380 t = tsubst (pattern, args, complain, in_decl);
12381 t = make_pack_expansion (t, complain);
12382 return t;
12383 }
12384
12385 gcc_assert (len >= 0);
12386
12387 if (need_local_specializations)
12388 {
12389 /* We're in a late-specified return type, so create our own local
12390 specializations map; the current map is either NULL or (in the
12391 case of recursive unification) might have bindings that we don't
12392 want to use or alter. */
12393 saved_local_specializations = local_specializations;
12394 local_specializations = new hash_map<tree, tree>;
12395 }
12396
12397 /* For each argument in each argument pack, substitute into the
12398 pattern. */
12399 result = make_tree_vec (len);
12400 tree elem_args = copy_template_args (args);
12401 for (i = 0; i < len; ++i)
12402 {
12403 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12404 i,
12405 elem_args, complain,
12406 in_decl);
12407 TREE_VEC_ELT (result, i) = t;
12408 if (t == error_mark_node)
12409 {
12410 result = error_mark_node;
12411 break;
12412 }
12413 }
12414
12415 /* Update ARGS to restore the substitution from parameter packs to
12416 their argument packs. */
12417 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12418 {
12419 tree parm = TREE_PURPOSE (pack);
12420
12421 if (TREE_CODE (parm) == PARM_DECL
12422 || VAR_P (parm)
12423 || TREE_CODE (parm) == FIELD_DECL)
12424 register_local_specialization (TREE_TYPE (pack), parm);
12425 else
12426 {
12427 int idx, level;
12428
12429 if (TREE_VALUE (pack) == NULL_TREE)
12430 continue;
12431
12432 template_parm_level_and_index (parm, &level, &idx);
12433
12434 /* Update the corresponding argument. */
12435 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12436 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12437 TREE_TYPE (pack);
12438 else
12439 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12440 }
12441 }
12442
12443 if (need_local_specializations)
12444 {
12445 delete local_specializations;
12446 local_specializations = saved_local_specializations;
12447 }
12448
12449 /* If the dependent pack arguments were such that we end up with only a
12450 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12451 if (len == 1 && TREE_CODE (result) == TREE_VEC
12452 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12453 return TREE_VEC_ELT (result, 0);
12454
12455 return result;
12456 }
12457
12458 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12459 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12460 parameter packs; all parms generated from a function parameter pack will
12461 have the same DECL_PARM_INDEX. */
12462
12463 tree
12464 get_pattern_parm (tree parm, tree tmpl)
12465 {
12466 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12467 tree patparm;
12468
12469 if (DECL_ARTIFICIAL (parm))
12470 {
12471 for (patparm = DECL_ARGUMENTS (pattern);
12472 patparm; patparm = DECL_CHAIN (patparm))
12473 if (DECL_ARTIFICIAL (patparm)
12474 && DECL_NAME (parm) == DECL_NAME (patparm))
12475 break;
12476 }
12477 else
12478 {
12479 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12480 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12481 gcc_assert (DECL_PARM_INDEX (patparm)
12482 == DECL_PARM_INDEX (parm));
12483 }
12484
12485 return patparm;
12486 }
12487
12488 /* Make an argument pack out of the TREE_VEC VEC. */
12489
12490 static tree
12491 make_argument_pack (tree vec)
12492 {
12493 tree pack;
12494 tree elt = TREE_VEC_ELT (vec, 0);
12495 if (TYPE_P (elt))
12496 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12497 else
12498 {
12499 pack = make_node (NONTYPE_ARGUMENT_PACK);
12500 TREE_CONSTANT (pack) = 1;
12501 }
12502 SET_ARGUMENT_PACK_ARGS (pack, vec);
12503 return pack;
12504 }
12505
12506 /* Return an exact copy of template args T that can be modified
12507 independently. */
12508
12509 static tree
12510 copy_template_args (tree t)
12511 {
12512 if (t == error_mark_node)
12513 return t;
12514
12515 int len = TREE_VEC_LENGTH (t);
12516 tree new_vec = make_tree_vec (len);
12517
12518 for (int i = 0; i < len; ++i)
12519 {
12520 tree elt = TREE_VEC_ELT (t, i);
12521 if (elt && TREE_CODE (elt) == TREE_VEC)
12522 elt = copy_template_args (elt);
12523 TREE_VEC_ELT (new_vec, i) = elt;
12524 }
12525
12526 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12527 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12528
12529 return new_vec;
12530 }
12531
12532 /* Substitute ARGS into the vector or list of template arguments T. */
12533
12534 static tree
12535 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12536 {
12537 tree orig_t = t;
12538 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12539 tree *elts;
12540
12541 if (t == error_mark_node)
12542 return error_mark_node;
12543
12544 len = TREE_VEC_LENGTH (t);
12545 elts = XALLOCAVEC (tree, len);
12546
12547 for (i = 0; i < len; i++)
12548 {
12549 tree orig_arg = TREE_VEC_ELT (t, i);
12550 tree new_arg;
12551
12552 if (TREE_CODE (orig_arg) == TREE_VEC)
12553 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12554 else if (PACK_EXPANSION_P (orig_arg))
12555 {
12556 /* Substitute into an expansion expression. */
12557 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12558
12559 if (TREE_CODE (new_arg) == TREE_VEC)
12560 /* Add to the expanded length adjustment the number of
12561 expanded arguments. We subtract one from this
12562 measurement, because the argument pack expression
12563 itself is already counted as 1 in
12564 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12565 the argument pack is empty. */
12566 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12567 }
12568 else if (ARGUMENT_PACK_P (orig_arg))
12569 {
12570 /* Substitute into each of the arguments. */
12571 new_arg = TYPE_P (orig_arg)
12572 ? cxx_make_type (TREE_CODE (orig_arg))
12573 : make_node (TREE_CODE (orig_arg));
12574
12575 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12576 args, complain, in_decl);
12577 if (pack_args == error_mark_node)
12578 new_arg = error_mark_node;
12579 else
12580 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12581
12582 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12583 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12584 }
12585 else
12586 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12587
12588 if (new_arg == error_mark_node)
12589 return error_mark_node;
12590
12591 elts[i] = new_arg;
12592 if (new_arg != orig_arg)
12593 need_new = 1;
12594 }
12595
12596 if (!need_new)
12597 return t;
12598
12599 /* Make space for the expanded arguments coming from template
12600 argument packs. */
12601 t = make_tree_vec (len + expanded_len_adjust);
12602 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12603 arguments for a member template.
12604 In that case each TREE_VEC in ORIG_T represents a level of template
12605 arguments, and ORIG_T won't carry any non defaulted argument count.
12606 It will rather be the nested TREE_VECs that will carry one.
12607 In other words, ORIG_T carries a non defaulted argument count only
12608 if it doesn't contain any nested TREE_VEC. */
12609 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12610 {
12611 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12612 count += expanded_len_adjust;
12613 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12614 }
12615 for (i = 0, out = 0; i < len; i++)
12616 {
12617 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12618 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12619 && TREE_CODE (elts[i]) == TREE_VEC)
12620 {
12621 int idx;
12622
12623 /* Now expand the template argument pack "in place". */
12624 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12625 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12626 }
12627 else
12628 {
12629 TREE_VEC_ELT (t, out) = elts[i];
12630 out++;
12631 }
12632 }
12633
12634 return t;
12635 }
12636
12637 /* Substitute ARGS into one level PARMS of template parameters. */
12638
12639 static tree
12640 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12641 {
12642 if (parms == error_mark_node)
12643 return error_mark_node;
12644
12645 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12646
12647 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12648 {
12649 tree tuple = TREE_VEC_ELT (parms, i);
12650
12651 if (tuple == error_mark_node)
12652 continue;
12653
12654 TREE_VEC_ELT (new_vec, i) =
12655 tsubst_template_parm (tuple, args, complain);
12656 }
12657
12658 return new_vec;
12659 }
12660
12661 /* Return the result of substituting ARGS into the template parameters
12662 given by PARMS. If there are m levels of ARGS and m + n levels of
12663 PARMS, then the result will contain n levels of PARMS. For
12664 example, if PARMS is `template <class T> template <class U>
12665 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12666 result will be `template <int*, double, class V>'. */
12667
12668 static tree
12669 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12670 {
12671 tree r = NULL_TREE;
12672 tree* new_parms;
12673
12674 /* When substituting into a template, we must set
12675 PROCESSING_TEMPLATE_DECL as the template parameters may be
12676 dependent if they are based on one-another, and the dependency
12677 predicates are short-circuit outside of templates. */
12678 ++processing_template_decl;
12679
12680 for (new_parms = &r;
12681 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12682 new_parms = &(TREE_CHAIN (*new_parms)),
12683 parms = TREE_CHAIN (parms))
12684 {
12685 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12686 args, complain);
12687 *new_parms =
12688 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12689 - TMPL_ARGS_DEPTH (args)),
12690 new_vec, NULL_TREE);
12691 }
12692
12693 --processing_template_decl;
12694
12695 return r;
12696 }
12697
12698 /* Return the result of substituting ARGS into one template parameter
12699 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12700 parameter and which TREE_PURPOSE is the default argument of the
12701 template parameter. */
12702
12703 static tree
12704 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12705 {
12706 tree default_value, parm_decl;
12707
12708 if (args == NULL_TREE
12709 || t == NULL_TREE
12710 || t == error_mark_node)
12711 return t;
12712
12713 gcc_assert (TREE_CODE (t) == TREE_LIST);
12714
12715 default_value = TREE_PURPOSE (t);
12716 parm_decl = TREE_VALUE (t);
12717
12718 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12719 if (TREE_CODE (parm_decl) == PARM_DECL
12720 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12721 parm_decl = error_mark_node;
12722 default_value = tsubst_template_arg (default_value, args,
12723 complain, NULL_TREE);
12724
12725 return build_tree_list (default_value, parm_decl);
12726 }
12727
12728 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12729 type T. If T is not an aggregate or enumeration type, it is
12730 handled as if by tsubst. IN_DECL is as for tsubst. If
12731 ENTERING_SCOPE is nonzero, T is the context for a template which
12732 we are presently tsubst'ing. Return the substituted value. */
12733
12734 static tree
12735 tsubst_aggr_type (tree t,
12736 tree args,
12737 tsubst_flags_t complain,
12738 tree in_decl,
12739 int entering_scope)
12740 {
12741 if (t == NULL_TREE)
12742 return NULL_TREE;
12743
12744 switch (TREE_CODE (t))
12745 {
12746 case RECORD_TYPE:
12747 if (TYPE_PTRMEMFUNC_P (t))
12748 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12749
12750 /* Fall through. */
12751 case ENUMERAL_TYPE:
12752 case UNION_TYPE:
12753 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12754 {
12755 tree argvec;
12756 tree context;
12757 tree r;
12758
12759 /* In "sizeof(X<I>)" we need to evaluate "I". */
12760 cp_evaluated ev;
12761
12762 /* First, determine the context for the type we are looking
12763 up. */
12764 context = TYPE_CONTEXT (t);
12765 if (context && TYPE_P (context))
12766 {
12767 context = tsubst_aggr_type (context, args, complain,
12768 in_decl, /*entering_scope=*/1);
12769 /* If context is a nested class inside a class template,
12770 it may still need to be instantiated (c++/33959). */
12771 context = complete_type (context);
12772 }
12773
12774 /* Then, figure out what arguments are appropriate for the
12775 type we are trying to find. For example, given:
12776
12777 template <class T> struct S;
12778 template <class T, class U> void f(T, U) { S<U> su; }
12779
12780 and supposing that we are instantiating f<int, double>,
12781 then our ARGS will be {int, double}, but, when looking up
12782 S we only want {double}. */
12783 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12784 complain, in_decl);
12785 if (argvec == error_mark_node)
12786 r = error_mark_node;
12787 else
12788 {
12789 r = lookup_template_class (t, argvec, in_decl, context,
12790 entering_scope, complain);
12791 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12792 }
12793
12794 return r;
12795 }
12796 else
12797 /* This is not a template type, so there's nothing to do. */
12798 return t;
12799
12800 default:
12801 return tsubst (t, args, complain, in_decl);
12802 }
12803 }
12804
12805 static GTY((cache)) tree_cache_map *defarg_inst;
12806
12807 /* Substitute into the default argument ARG (a default argument for
12808 FN), which has the indicated TYPE. */
12809
12810 tree
12811 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12812 tsubst_flags_t complain)
12813 {
12814 int errs = errorcount + sorrycount;
12815
12816 /* This can happen in invalid code. */
12817 if (TREE_CODE (arg) == DEFERRED_PARSE)
12818 return arg;
12819
12820 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12821 parm = chain_index (parmnum, parm);
12822 tree parmtype = TREE_TYPE (parm);
12823 if (DECL_BY_REFERENCE (parm))
12824 parmtype = TREE_TYPE (parmtype);
12825 if (parmtype == error_mark_node)
12826 return error_mark_node;
12827
12828 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12829
12830 tree *slot;
12831 if (defarg_inst && (slot = defarg_inst->get (parm)))
12832 return *slot;
12833
12834 /* This default argument came from a template. Instantiate the
12835 default argument here, not in tsubst. In the case of
12836 something like:
12837
12838 template <class T>
12839 struct S {
12840 static T t();
12841 void f(T = t());
12842 };
12843
12844 we must be careful to do name lookup in the scope of S<T>,
12845 rather than in the current class. */
12846 push_to_top_level ();
12847 push_access_scope (fn);
12848 push_deferring_access_checks (dk_no_deferred);
12849 start_lambda_scope (parm);
12850
12851 /* The default argument expression may cause implicitly defined
12852 member functions to be synthesized, which will result in garbage
12853 collection. We must treat this situation as if we were within
12854 the body of function so as to avoid collecting live data on the
12855 stack. */
12856 ++function_depth;
12857 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12858 complain, NULL_TREE,
12859 /*integral_constant_expression_p=*/false);
12860 --function_depth;
12861
12862 finish_lambda_scope ();
12863
12864 /* Make sure the default argument is reasonable. */
12865 arg = check_default_argument (type, arg, complain);
12866
12867 if (errorcount+sorrycount > errs
12868 && (complain & tf_warning_or_error))
12869 inform (input_location,
12870 " when instantiating default argument for call to %qD", fn);
12871
12872 pop_deferring_access_checks ();
12873 pop_access_scope (fn);
12874 pop_from_top_level ();
12875
12876 if (arg != error_mark_node && !cp_unevaluated_operand)
12877 {
12878 if (!defarg_inst)
12879 defarg_inst = tree_cache_map::create_ggc (37);
12880 defarg_inst->put (parm, arg);
12881 }
12882
12883 return arg;
12884 }
12885
12886 /* Substitute into all the default arguments for FN. */
12887
12888 static void
12889 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12890 {
12891 tree arg;
12892 tree tmpl_args;
12893
12894 tmpl_args = DECL_TI_ARGS (fn);
12895
12896 /* If this function is not yet instantiated, we certainly don't need
12897 its default arguments. */
12898 if (uses_template_parms (tmpl_args))
12899 return;
12900 /* Don't do this again for clones. */
12901 if (DECL_CLONED_FUNCTION_P (fn))
12902 return;
12903
12904 int i = 0;
12905 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12906 arg;
12907 arg = TREE_CHAIN (arg), ++i)
12908 if (TREE_PURPOSE (arg))
12909 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12910 TREE_VALUE (arg),
12911 TREE_PURPOSE (arg),
12912 complain);
12913 }
12914
12915 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12916 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12917
12918 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12919
12920 void
12921 store_explicit_specifier (tree v, tree t)
12922 {
12923 if (!explicit_specifier_map)
12924 explicit_specifier_map = tree_cache_map::create_ggc (37);
12925 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12926 explicit_specifier_map->put (v, t);
12927 }
12928
12929 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12930
12931 static tree
12932 lookup_explicit_specifier (tree v)
12933 {
12934 return *explicit_specifier_map->get (v);
12935 }
12936
12937 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12938
12939 static tree
12940 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12941 tree lambda_fntype)
12942 {
12943 tree gen_tmpl, argvec;
12944 hashval_t hash = 0;
12945 tree in_decl = t;
12946
12947 /* Nobody should be tsubst'ing into non-template functions. */
12948 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12949
12950 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12951 {
12952 /* If T is not dependent, just return it. */
12953 if (!uses_template_parms (DECL_TI_ARGS (t))
12954 && !LAMBDA_FUNCTION_P (t))
12955 return t;
12956
12957 /* Calculate the most general template of which R is a
12958 specialization. */
12959 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12960
12961 /* We're substituting a lambda function under tsubst_lambda_expr but not
12962 directly from it; find the matching function we're already inside.
12963 But don't do this if T is a generic lambda with a single level of
12964 template parms, as in that case we're doing a normal instantiation. */
12965 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12966 && (!generic_lambda_fn_p (t)
12967 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12968 return enclosing_instantiation_of (t);
12969
12970 /* Calculate the complete set of arguments used to
12971 specialize R. */
12972 argvec = tsubst_template_args (DECL_TI_ARGS
12973 (DECL_TEMPLATE_RESULT
12974 (DECL_TI_TEMPLATE (t))),
12975 args, complain, in_decl);
12976 if (argvec == error_mark_node)
12977 return error_mark_node;
12978
12979 /* Check to see if we already have this specialization. */
12980 if (!lambda_fntype)
12981 {
12982 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12983 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12984 return spec;
12985 }
12986
12987 /* We can see more levels of arguments than parameters if
12988 there was a specialization of a member template, like
12989 this:
12990
12991 template <class T> struct S { template <class U> void f(); }
12992 template <> template <class U> void S<int>::f(U);
12993
12994 Here, we'll be substituting into the specialization,
12995 because that's where we can find the code we actually
12996 want to generate, but we'll have enough arguments for
12997 the most general template.
12998
12999 We also deal with the peculiar case:
13000
13001 template <class T> struct S {
13002 template <class U> friend void f();
13003 };
13004 template <class U> void f() {}
13005 template S<int>;
13006 template void f<double>();
13007
13008 Here, the ARGS for the instantiation of will be {int,
13009 double}. But, we only need as many ARGS as there are
13010 levels of template parameters in CODE_PATTERN. We are
13011 careful not to get fooled into reducing the ARGS in
13012 situations like:
13013
13014 template <class T> struct S { template <class U> void f(U); }
13015 template <class T> template <> void S<T>::f(int) {}
13016
13017 which we can spot because the pattern will be a
13018 specialization in this case. */
13019 int args_depth = TMPL_ARGS_DEPTH (args);
13020 int parms_depth =
13021 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13022
13023 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13024 args = get_innermost_template_args (args, parms_depth);
13025 }
13026 else
13027 {
13028 /* This special case arises when we have something like this:
13029
13030 template <class T> struct S {
13031 friend void f<int>(int, double);
13032 };
13033
13034 Here, the DECL_TI_TEMPLATE for the friend declaration
13035 will be an IDENTIFIER_NODE. We are being called from
13036 tsubst_friend_function, and we want only to create a
13037 new decl (R) with appropriate types so that we can call
13038 determine_specialization. */
13039 gen_tmpl = NULL_TREE;
13040 argvec = NULL_TREE;
13041 }
13042
13043 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13044 : NULL_TREE);
13045 tree ctx = closure ? closure : DECL_CONTEXT (t);
13046 bool member = ctx && TYPE_P (ctx);
13047
13048 if (member && !closure)
13049 ctx = tsubst_aggr_type (ctx, args,
13050 complain, t, /*entering_scope=*/1);
13051
13052 tree type = (lambda_fntype ? lambda_fntype
13053 : tsubst (TREE_TYPE (t), args,
13054 complain | tf_fndecl_type, in_decl));
13055 if (type == error_mark_node)
13056 return error_mark_node;
13057
13058 /* If we hit excessive deduction depth, the type is bogus even if
13059 it isn't error_mark_node, so don't build a decl. */
13060 if (excessive_deduction_depth)
13061 return error_mark_node;
13062
13063 /* We do NOT check for matching decls pushed separately at this
13064 point, as they may not represent instantiations of this
13065 template, and in any case are considered separate under the
13066 discrete model. */
13067 tree r = copy_decl (t);
13068 DECL_USE_TEMPLATE (r) = 0;
13069 TREE_TYPE (r) = type;
13070 /* Clear out the mangled name and RTL for the instantiation. */
13071 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13072 SET_DECL_RTL (r, NULL);
13073 /* Leave DECL_INITIAL set on deleted instantiations. */
13074 if (!DECL_DELETED_FN (r))
13075 DECL_INITIAL (r) = NULL_TREE;
13076 DECL_CONTEXT (r) = ctx;
13077
13078 /* Handle explicit(dependent-expr). */
13079 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13080 {
13081 tree spec = lookup_explicit_specifier (t);
13082 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13083 /*function_p=*/false,
13084 /*i_c_e_p=*/true);
13085 spec = build_explicit_specifier (spec, complain);
13086 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13087 }
13088
13089 /* OpenMP UDRs have the only argument a reference to the declared
13090 type. We want to diagnose if the declared type is a reference,
13091 which is invalid, but as references to references are usually
13092 quietly merged, diagnose it here. */
13093 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13094 {
13095 tree argtype
13096 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13097 argtype = tsubst (argtype, args, complain, in_decl);
13098 if (TYPE_REF_P (argtype))
13099 error_at (DECL_SOURCE_LOCATION (t),
13100 "reference type %qT in "
13101 "%<#pragma omp declare reduction%>", argtype);
13102 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13103 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13104 argtype);
13105 }
13106
13107 if (member && DECL_CONV_FN_P (r))
13108 /* Type-conversion operator. Reconstruct the name, in
13109 case it's the name of one of the template's parameters. */
13110 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13111
13112 tree parms = DECL_ARGUMENTS (t);
13113 if (closure)
13114 parms = DECL_CHAIN (parms);
13115 parms = tsubst (parms, args, complain, t);
13116 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13117 DECL_CONTEXT (parm) = r;
13118 if (closure)
13119 {
13120 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13121 DECL_CHAIN (tparm) = parms;
13122 parms = tparm;
13123 }
13124 DECL_ARGUMENTS (r) = parms;
13125 DECL_RESULT (r) = NULL_TREE;
13126
13127 TREE_STATIC (r) = 0;
13128 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13129 DECL_EXTERNAL (r) = 1;
13130 /* If this is an instantiation of a function with internal
13131 linkage, we already know what object file linkage will be
13132 assigned to the instantiation. */
13133 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13134 DECL_DEFER_OUTPUT (r) = 0;
13135 DECL_CHAIN (r) = NULL_TREE;
13136 DECL_PENDING_INLINE_INFO (r) = 0;
13137 DECL_PENDING_INLINE_P (r) = 0;
13138 DECL_SAVED_TREE (r) = NULL_TREE;
13139 DECL_STRUCT_FUNCTION (r) = NULL;
13140 TREE_USED (r) = 0;
13141 /* We'll re-clone as appropriate in instantiate_template. */
13142 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13143
13144 /* If we aren't complaining now, return on error before we register
13145 the specialization so that we'll complain eventually. */
13146 if ((complain & tf_error) == 0
13147 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13148 && !grok_op_properties (r, /*complain=*/false))
13149 return error_mark_node;
13150
13151 /* When instantiating a constrained member, substitute
13152 into the constraints to create a new constraint. */
13153 if (tree ci = get_constraints (t))
13154 if (member)
13155 {
13156 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13157 set_constraints (r, ci);
13158 }
13159
13160 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13161 SET_DECL_FRIEND_CONTEXT (r,
13162 tsubst (DECL_FRIEND_CONTEXT (t),
13163 args, complain, in_decl));
13164
13165 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13166 this in the special friend case mentioned above where
13167 GEN_TMPL is NULL. */
13168 if (gen_tmpl && !closure)
13169 {
13170 DECL_TEMPLATE_INFO (r)
13171 = build_template_info (gen_tmpl, argvec);
13172 SET_DECL_IMPLICIT_INSTANTIATION (r);
13173
13174 tree new_r
13175 = register_specialization (r, gen_tmpl, argvec, false, hash);
13176 if (new_r != r)
13177 /* We instantiated this while substituting into
13178 the type earlier (template/friend54.C). */
13179 return new_r;
13180
13181 /* We're not supposed to instantiate default arguments
13182 until they are called, for a template. But, for a
13183 declaration like:
13184
13185 template <class T> void f ()
13186 { extern void g(int i = T()); }
13187
13188 we should do the substitution when the template is
13189 instantiated. We handle the member function case in
13190 instantiate_class_template since the default arguments
13191 might refer to other members of the class. */
13192 if (!member
13193 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13194 && !uses_template_parms (argvec))
13195 tsubst_default_arguments (r, complain);
13196 }
13197 else
13198 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13199
13200 /* Copy the list of befriending classes. */
13201 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13202 *friends;
13203 friends = &TREE_CHAIN (*friends))
13204 {
13205 *friends = copy_node (*friends);
13206 TREE_VALUE (*friends)
13207 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13208 }
13209
13210 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13211 {
13212 maybe_retrofit_in_chrg (r);
13213 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13214 return error_mark_node;
13215 /* If this is an instantiation of a member template, clone it.
13216 If it isn't, that'll be handled by
13217 clone_constructors_and_destructors. */
13218 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13219 clone_function_decl (r, /*update_methods=*/false);
13220 }
13221 else if ((complain & tf_error) != 0
13222 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13223 && !grok_op_properties (r, /*complain=*/true))
13224 return error_mark_node;
13225
13226 /* Possibly limit visibility based on template args. */
13227 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13228 if (DECL_VISIBILITY_SPECIFIED (t))
13229 {
13230 DECL_VISIBILITY_SPECIFIED (r) = 0;
13231 DECL_ATTRIBUTES (r)
13232 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13233 }
13234 determine_visibility (r);
13235 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13236 && !processing_template_decl)
13237 defaulted_late_check (r);
13238
13239 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13240 args, complain, in_decl);
13241 return r;
13242 }
13243
13244 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13245
13246 static tree
13247 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13248 tree lambda_fntype)
13249 {
13250 /* We can get here when processing a member function template,
13251 member class template, or template template parameter. */
13252 tree decl = DECL_TEMPLATE_RESULT (t);
13253 tree in_decl = t;
13254 tree spec;
13255 tree tmpl_args;
13256 tree full_args;
13257 tree r;
13258 hashval_t hash = 0;
13259
13260 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13261 {
13262 /* Template template parameter is treated here. */
13263 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13264 if (new_type == error_mark_node)
13265 r = error_mark_node;
13266 /* If we get a real template back, return it. This can happen in
13267 the context of most_specialized_partial_spec. */
13268 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13269 r = new_type;
13270 else
13271 /* The new TEMPLATE_DECL was built in
13272 reduce_template_parm_level. */
13273 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13274 return r;
13275 }
13276
13277 if (!lambda_fntype)
13278 {
13279 /* We might already have an instance of this template.
13280 The ARGS are for the surrounding class type, so the
13281 full args contain the tsubst'd args for the context,
13282 plus the innermost args from the template decl. */
13283 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13284 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13285 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13286 /* Because this is a template, the arguments will still be
13287 dependent, even after substitution. If
13288 PROCESSING_TEMPLATE_DECL is not set, the dependency
13289 predicates will short-circuit. */
13290 ++processing_template_decl;
13291 full_args = tsubst_template_args (tmpl_args, args,
13292 complain, in_decl);
13293 --processing_template_decl;
13294 if (full_args == error_mark_node)
13295 return error_mark_node;
13296
13297 /* If this is a default template template argument,
13298 tsubst might not have changed anything. */
13299 if (full_args == tmpl_args)
13300 return t;
13301
13302 hash = hash_tmpl_and_args (t, full_args);
13303 spec = retrieve_specialization (t, full_args, hash);
13304 if (spec != NULL_TREE)
13305 {
13306 if (TYPE_P (spec))
13307 /* Type partial instantiations are stored as the type by
13308 lookup_template_class_1, not here as the template. */
13309 spec = CLASSTYPE_TI_TEMPLATE (spec);
13310 return spec;
13311 }
13312 }
13313
13314 /* Make a new template decl. It will be similar to the
13315 original, but will record the current template arguments.
13316 We also create a new function declaration, which is just
13317 like the old one, but points to this new template, rather
13318 than the old one. */
13319 r = copy_decl (t);
13320 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13321 DECL_CHAIN (r) = NULL_TREE;
13322
13323 // Build new template info linking to the original template decl.
13324 if (!lambda_fntype)
13325 {
13326 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13327 SET_DECL_IMPLICIT_INSTANTIATION (r);
13328 }
13329 else
13330 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13331
13332 /* The template parameters for this new template are all the
13333 template parameters for the old template, except the
13334 outermost level of parameters. */
13335 DECL_TEMPLATE_PARMS (r)
13336 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13337 complain);
13338
13339 if (TREE_CODE (decl) == TYPE_DECL
13340 && !TYPE_DECL_ALIAS_P (decl))
13341 {
13342 tree new_type;
13343 ++processing_template_decl;
13344 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13345 --processing_template_decl;
13346 if (new_type == error_mark_node)
13347 return error_mark_node;
13348
13349 TREE_TYPE (r) = new_type;
13350 /* For a partial specialization, we need to keep pointing to
13351 the primary template. */
13352 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13353 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13354 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13355 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13356 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13357 }
13358 else
13359 {
13360 tree new_decl;
13361 ++processing_template_decl;
13362 if (TREE_CODE (decl) == FUNCTION_DECL)
13363 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13364 else
13365 new_decl = tsubst (decl, args, complain, in_decl);
13366 --processing_template_decl;
13367 if (new_decl == error_mark_node)
13368 return error_mark_node;
13369
13370 DECL_TEMPLATE_RESULT (r) = new_decl;
13371 TREE_TYPE (r) = TREE_TYPE (new_decl);
13372 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13373 if (lambda_fntype)
13374 {
13375 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13376 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13377 }
13378 else
13379 {
13380 DECL_TI_TEMPLATE (new_decl) = r;
13381 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13382 }
13383 }
13384
13385 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13386 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13387
13388 if (PRIMARY_TEMPLATE_P (t))
13389 DECL_PRIMARY_TEMPLATE (r) = r;
13390
13391 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13392 && !lambda_fntype)
13393 /* Record this non-type partial instantiation. */
13394 register_specialization (r, t,
13395 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13396 false, hash);
13397
13398 return r;
13399 }
13400
13401 /* True if FN is the op() for a lambda in an uninstantiated template. */
13402
13403 bool
13404 lambda_fn_in_template_p (tree fn)
13405 {
13406 if (!fn || !LAMBDA_FUNCTION_P (fn))
13407 return false;
13408 tree closure = DECL_CONTEXT (fn);
13409 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13410 }
13411
13412 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13413 which the above is true. */
13414
13415 bool
13416 instantiated_lambda_fn_p (tree fn)
13417 {
13418 if (!fn || !LAMBDA_FUNCTION_P (fn))
13419 return false;
13420 tree closure = DECL_CONTEXT (fn);
13421 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13422 return LAMBDA_EXPR_INSTANTIATED (lam);
13423 }
13424
13425 /* We're instantiating a variable from template function TCTX. Return the
13426 corresponding current enclosing scope. This gets complicated because lambda
13427 functions in templates are regenerated rather than instantiated, but generic
13428 lambda functions are subsequently instantiated. */
13429
13430 static tree
13431 enclosing_instantiation_of (tree otctx)
13432 {
13433 tree tctx = otctx;
13434 tree fn = current_function_decl;
13435 int lambda_count = 0;
13436
13437 for (; tctx && (lambda_fn_in_template_p (tctx)
13438 || instantiated_lambda_fn_p (tctx));
13439 tctx = decl_function_context (tctx))
13440 ++lambda_count;
13441 for (; fn; fn = decl_function_context (fn))
13442 {
13443 tree ofn = fn;
13444 int flambda_count = 0;
13445 for (; fn && instantiated_lambda_fn_p (fn);
13446 fn = decl_function_context (fn))
13447 ++flambda_count;
13448 if ((fn && DECL_TEMPLATE_INFO (fn))
13449 ? most_general_template (fn) != most_general_template (tctx)
13450 : fn != tctx)
13451 continue;
13452 if (flambda_count != lambda_count)
13453 {
13454 gcc_assert (flambda_count > lambda_count);
13455 for (; flambda_count > lambda_count; --flambda_count)
13456 ofn = decl_function_context (ofn);
13457 }
13458 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13459 || DECL_CONV_FN_P (ofn));
13460 return ofn;
13461 }
13462 gcc_unreachable ();
13463 }
13464
13465 /* Substitute the ARGS into the T, which is a _DECL. Return the
13466 result of the substitution. Issue error and warning messages under
13467 control of COMPLAIN. */
13468
13469 static tree
13470 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13471 {
13472 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13473 location_t saved_loc;
13474 tree r = NULL_TREE;
13475 tree in_decl = t;
13476 hashval_t hash = 0;
13477
13478 /* Set the filename and linenumber to improve error-reporting. */
13479 saved_loc = input_location;
13480 input_location = DECL_SOURCE_LOCATION (t);
13481
13482 switch (TREE_CODE (t))
13483 {
13484 case TEMPLATE_DECL:
13485 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13486 break;
13487
13488 case FUNCTION_DECL:
13489 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13490 break;
13491
13492 case PARM_DECL:
13493 {
13494 tree type = NULL_TREE;
13495 int i, len = 1;
13496 tree expanded_types = NULL_TREE;
13497 tree prev_r = NULL_TREE;
13498 tree first_r = NULL_TREE;
13499
13500 if (DECL_PACK_P (t))
13501 {
13502 /* If there is a local specialization that isn't a
13503 parameter pack, it means that we're doing a "simple"
13504 substitution from inside tsubst_pack_expansion. Just
13505 return the local specialization (which will be a single
13506 parm). */
13507 tree spec = retrieve_local_specialization (t);
13508 if (spec
13509 && TREE_CODE (spec) == PARM_DECL
13510 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13511 RETURN (spec);
13512
13513 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13514 the parameters in this function parameter pack. */
13515 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13516 complain, in_decl);
13517 if (TREE_CODE (expanded_types) == TREE_VEC)
13518 {
13519 len = TREE_VEC_LENGTH (expanded_types);
13520
13521 /* Zero-length parameter packs are boring. Just substitute
13522 into the chain. */
13523 if (len == 0)
13524 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13525 TREE_CHAIN (t)));
13526 }
13527 else
13528 {
13529 /* All we did was update the type. Make a note of that. */
13530 type = expanded_types;
13531 expanded_types = NULL_TREE;
13532 }
13533 }
13534
13535 /* Loop through all of the parameters we'll build. When T is
13536 a function parameter pack, LEN is the number of expanded
13537 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13538 r = NULL_TREE;
13539 for (i = 0; i < len; ++i)
13540 {
13541 prev_r = r;
13542 r = copy_node (t);
13543 if (DECL_TEMPLATE_PARM_P (t))
13544 SET_DECL_TEMPLATE_PARM_P (r);
13545
13546 if (expanded_types)
13547 /* We're on the Ith parameter of the function parameter
13548 pack. */
13549 {
13550 /* Get the Ith type. */
13551 type = TREE_VEC_ELT (expanded_types, i);
13552
13553 /* Rename the parameter to include the index. */
13554 DECL_NAME (r)
13555 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13556 }
13557 else if (!type)
13558 /* We're dealing with a normal parameter. */
13559 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13560
13561 type = type_decays_to (type);
13562 TREE_TYPE (r) = type;
13563 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13564
13565 if (DECL_INITIAL (r))
13566 {
13567 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13568 DECL_INITIAL (r) = TREE_TYPE (r);
13569 else
13570 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13571 complain, in_decl);
13572 }
13573
13574 DECL_CONTEXT (r) = NULL_TREE;
13575
13576 if (!DECL_TEMPLATE_PARM_P (r))
13577 DECL_ARG_TYPE (r) = type_passed_as (type);
13578
13579 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13580 args, complain, in_decl);
13581
13582 /* Keep track of the first new parameter we
13583 generate. That's what will be returned to the
13584 caller. */
13585 if (!first_r)
13586 first_r = r;
13587
13588 /* Build a proper chain of parameters when substituting
13589 into a function parameter pack. */
13590 if (prev_r)
13591 DECL_CHAIN (prev_r) = r;
13592 }
13593
13594 /* If cp_unevaluated_operand is set, we're just looking for a
13595 single dummy parameter, so don't keep going. */
13596 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13597 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13598 complain, DECL_CHAIN (t));
13599
13600 /* FIRST_R contains the start of the chain we've built. */
13601 r = first_r;
13602 }
13603 break;
13604
13605 case FIELD_DECL:
13606 {
13607 tree type = NULL_TREE;
13608 tree vec = NULL_TREE;
13609 tree expanded_types = NULL_TREE;
13610 int len = 1;
13611
13612 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13613 {
13614 /* This field is a lambda capture pack. Return a TREE_VEC of
13615 the expanded fields to instantiate_class_template_1. */
13616 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13617 complain, in_decl);
13618 if (TREE_CODE (expanded_types) == TREE_VEC)
13619 {
13620 len = TREE_VEC_LENGTH (expanded_types);
13621 vec = make_tree_vec (len);
13622 }
13623 else
13624 {
13625 /* All we did was update the type. Make a note of that. */
13626 type = expanded_types;
13627 expanded_types = NULL_TREE;
13628 }
13629 }
13630
13631 for (int i = 0; i < len; ++i)
13632 {
13633 r = copy_decl (t);
13634 if (expanded_types)
13635 {
13636 type = TREE_VEC_ELT (expanded_types, i);
13637 DECL_NAME (r)
13638 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13639 }
13640 else if (!type)
13641 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13642
13643 if (type == error_mark_node)
13644 RETURN (error_mark_node);
13645 TREE_TYPE (r) = type;
13646 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13647
13648 if (DECL_C_BIT_FIELD (r))
13649 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13650 number of bits. */
13651 DECL_BIT_FIELD_REPRESENTATIVE (r)
13652 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13653 complain, in_decl,
13654 /*integral_constant_expression_p=*/true);
13655 if (DECL_INITIAL (t))
13656 {
13657 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13658 NSDMI in perform_member_init. Still set DECL_INITIAL
13659 so that we know there is one. */
13660 DECL_INITIAL (r) = void_node;
13661 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13662 retrofit_lang_decl (r);
13663 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13664 }
13665 /* We don't have to set DECL_CONTEXT here; it is set by
13666 finish_member_declaration. */
13667 DECL_CHAIN (r) = NULL_TREE;
13668
13669 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13670 args, complain, in_decl);
13671
13672 if (vec)
13673 TREE_VEC_ELT (vec, i) = r;
13674 }
13675
13676 if (vec)
13677 r = vec;
13678 }
13679 break;
13680
13681 case USING_DECL:
13682 /* We reach here only for member using decls. We also need to check
13683 uses_template_parms because DECL_DEPENDENT_P is not set for a
13684 using-declaration that designates a member of the current
13685 instantiation (c++/53549). */
13686 if (DECL_DEPENDENT_P (t)
13687 || uses_template_parms (USING_DECL_SCOPE (t)))
13688 {
13689 tree scope = USING_DECL_SCOPE (t);
13690 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13691 if (PACK_EXPANSION_P (scope))
13692 {
13693 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13694 int len = TREE_VEC_LENGTH (vec);
13695 r = make_tree_vec (len);
13696 for (int i = 0; i < len; ++i)
13697 {
13698 tree escope = TREE_VEC_ELT (vec, i);
13699 tree elt = do_class_using_decl (escope, name);
13700 if (!elt)
13701 {
13702 r = error_mark_node;
13703 break;
13704 }
13705 else
13706 {
13707 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13708 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13709 }
13710 TREE_VEC_ELT (r, i) = elt;
13711 }
13712 }
13713 else
13714 {
13715 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13716 complain, in_decl);
13717 r = do_class_using_decl (inst_scope, name);
13718 if (!r)
13719 r = error_mark_node;
13720 else
13721 {
13722 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13723 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13724 }
13725 }
13726 }
13727 else
13728 {
13729 r = copy_node (t);
13730 DECL_CHAIN (r) = NULL_TREE;
13731 }
13732 break;
13733
13734 case TYPE_DECL:
13735 case VAR_DECL:
13736 {
13737 tree argvec = NULL_TREE;
13738 tree gen_tmpl = NULL_TREE;
13739 tree spec;
13740 tree tmpl = NULL_TREE;
13741 tree ctx;
13742 tree type = NULL_TREE;
13743 bool local_p;
13744
13745 if (TREE_TYPE (t) == error_mark_node)
13746 RETURN (error_mark_node);
13747
13748 if (TREE_CODE (t) == TYPE_DECL
13749 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13750 {
13751 /* If this is the canonical decl, we don't have to
13752 mess with instantiations, and often we can't (for
13753 typename, template type parms and such). Note that
13754 TYPE_NAME is not correct for the above test if
13755 we've copied the type for a typedef. */
13756 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13757 if (type == error_mark_node)
13758 RETURN (error_mark_node);
13759 r = TYPE_NAME (type);
13760 break;
13761 }
13762
13763 /* Check to see if we already have the specialization we
13764 need. */
13765 spec = NULL_TREE;
13766 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13767 {
13768 /* T is a static data member or namespace-scope entity.
13769 We have to substitute into namespace-scope variables
13770 (not just variable templates) because of cases like:
13771
13772 template <class T> void f() { extern T t; }
13773
13774 where the entity referenced is not known until
13775 instantiation time. */
13776 local_p = false;
13777 ctx = DECL_CONTEXT (t);
13778 if (DECL_CLASS_SCOPE_P (t))
13779 {
13780 ctx = tsubst_aggr_type (ctx, args,
13781 complain,
13782 in_decl, /*entering_scope=*/1);
13783 /* If CTX is unchanged, then T is in fact the
13784 specialization we want. That situation occurs when
13785 referencing a static data member within in its own
13786 class. We can use pointer equality, rather than
13787 same_type_p, because DECL_CONTEXT is always
13788 canonical... */
13789 if (ctx == DECL_CONTEXT (t)
13790 /* ... unless T is a member template; in which
13791 case our caller can be willing to create a
13792 specialization of that template represented
13793 by T. */
13794 && !(DECL_TI_TEMPLATE (t)
13795 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13796 spec = t;
13797 }
13798
13799 if (!spec)
13800 {
13801 tmpl = DECL_TI_TEMPLATE (t);
13802 gen_tmpl = most_general_template (tmpl);
13803 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13804 if (argvec != error_mark_node)
13805 argvec = (coerce_innermost_template_parms
13806 (DECL_TEMPLATE_PARMS (gen_tmpl),
13807 argvec, t, complain,
13808 /*all*/true, /*defarg*/true));
13809 if (argvec == error_mark_node)
13810 RETURN (error_mark_node);
13811 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13812 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13813 }
13814 }
13815 else
13816 {
13817 /* A local variable. */
13818 local_p = true;
13819 /* Subsequent calls to pushdecl will fill this in. */
13820 ctx = NULL_TREE;
13821 /* Unless this is a reference to a static variable from an
13822 enclosing function, in which case we need to fill it in now. */
13823 if (TREE_STATIC (t))
13824 {
13825 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13826 if (fn != current_function_decl)
13827 ctx = fn;
13828 }
13829 spec = retrieve_local_specialization (t);
13830 }
13831 /* If we already have the specialization we need, there is
13832 nothing more to do. */
13833 if (spec)
13834 {
13835 r = spec;
13836 break;
13837 }
13838
13839 /* Create a new node for the specialization we need. */
13840 if (type == NULL_TREE)
13841 {
13842 if (is_typedef_decl (t))
13843 type = DECL_ORIGINAL_TYPE (t);
13844 else
13845 type = TREE_TYPE (t);
13846 if (VAR_P (t)
13847 && VAR_HAD_UNKNOWN_BOUND (t)
13848 && type != error_mark_node)
13849 type = strip_array_domain (type);
13850 tree sub_args = args;
13851 if (tree auto_node = type_uses_auto (type))
13852 {
13853 /* Mask off any template args past the variable's context so we
13854 don't replace the auto with an unrelated argument. */
13855 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13856 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13857 if (extra > 0)
13858 /* This should never happen with the new lambda instantiation
13859 model, but keep the handling just in case. */
13860 gcc_assert (!CHECKING_P),
13861 sub_args = strip_innermost_template_args (args, extra);
13862 }
13863 type = tsubst (type, sub_args, complain, in_decl);
13864 /* Substituting the type might have recursively instantiated this
13865 same alias (c++/86171). */
13866 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13867 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13868 {
13869 r = spec;
13870 break;
13871 }
13872 }
13873 r = copy_decl (t);
13874 if (VAR_P (r))
13875 {
13876 DECL_INITIALIZED_P (r) = 0;
13877 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13878 if (type == error_mark_node)
13879 RETURN (error_mark_node);
13880 if (TREE_CODE (type) == FUNCTION_TYPE)
13881 {
13882 /* It may seem that this case cannot occur, since:
13883
13884 typedef void f();
13885 void g() { f x; }
13886
13887 declares a function, not a variable. However:
13888
13889 typedef void f();
13890 template <typename T> void g() { T t; }
13891 template void g<f>();
13892
13893 is an attempt to declare a variable with function
13894 type. */
13895 error ("variable %qD has function type",
13896 /* R is not yet sufficiently initialized, so we
13897 just use its name. */
13898 DECL_NAME (r));
13899 RETURN (error_mark_node);
13900 }
13901 type = complete_type (type);
13902 /* Wait until cp_finish_decl to set this again, to handle
13903 circular dependency (template/instantiate6.C). */
13904 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13905 type = check_var_type (DECL_NAME (r), type);
13906
13907 if (DECL_HAS_VALUE_EXPR_P (t))
13908 {
13909 tree ve = DECL_VALUE_EXPR (t);
13910 ve = tsubst_expr (ve, args, complain, in_decl,
13911 /*constant_expression_p=*/false);
13912 if (REFERENCE_REF_P (ve))
13913 {
13914 gcc_assert (TYPE_REF_P (type));
13915 ve = TREE_OPERAND (ve, 0);
13916 }
13917 SET_DECL_VALUE_EXPR (r, ve);
13918 }
13919 if (CP_DECL_THREAD_LOCAL_P (r)
13920 && !processing_template_decl)
13921 set_decl_tls_model (r, decl_default_tls_model (r));
13922 }
13923 else if (DECL_SELF_REFERENCE_P (t))
13924 SET_DECL_SELF_REFERENCE_P (r);
13925 TREE_TYPE (r) = type;
13926 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13927 DECL_CONTEXT (r) = ctx;
13928 /* Clear out the mangled name and RTL for the instantiation. */
13929 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13930 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13931 SET_DECL_RTL (r, NULL);
13932 /* The initializer must not be expanded until it is required;
13933 see [temp.inst]. */
13934 DECL_INITIAL (r) = NULL_TREE;
13935 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13936 if (VAR_P (r))
13937 {
13938 if (DECL_LANG_SPECIFIC (r))
13939 SET_DECL_DEPENDENT_INIT_P (r, false);
13940
13941 SET_DECL_MODE (r, VOIDmode);
13942
13943 /* Possibly limit visibility based on template args. */
13944 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13945 if (DECL_VISIBILITY_SPECIFIED (t))
13946 {
13947 DECL_VISIBILITY_SPECIFIED (r) = 0;
13948 DECL_ATTRIBUTES (r)
13949 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13950 }
13951 determine_visibility (r);
13952 }
13953
13954 if (!local_p)
13955 {
13956 /* A static data member declaration is always marked
13957 external when it is declared in-class, even if an
13958 initializer is present. We mimic the non-template
13959 processing here. */
13960 DECL_EXTERNAL (r) = 1;
13961 if (DECL_NAMESPACE_SCOPE_P (t))
13962 DECL_NOT_REALLY_EXTERN (r) = 1;
13963
13964 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13965 SET_DECL_IMPLICIT_INSTANTIATION (r);
13966 if (!error_operand_p (r) || (complain & tf_error))
13967 register_specialization (r, gen_tmpl, argvec, false, hash);
13968 }
13969 else
13970 {
13971 if (DECL_LANG_SPECIFIC (r))
13972 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13973 if (!cp_unevaluated_operand)
13974 register_local_specialization (r, t);
13975 }
13976
13977 DECL_CHAIN (r) = NULL_TREE;
13978
13979 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13980 /*flags=*/0,
13981 args, complain, in_decl);
13982
13983 /* Preserve a typedef that names a type. */
13984 if (is_typedef_decl (r) && type != error_mark_node)
13985 {
13986 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13987 set_underlying_type (r);
13988 if (TYPE_DECL_ALIAS_P (r))
13989 /* An alias template specialization can be dependent
13990 even if its underlying type is not. */
13991 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13992 }
13993
13994 layout_decl (r, 0);
13995 }
13996 break;
13997
13998 default:
13999 gcc_unreachable ();
14000 }
14001 #undef RETURN
14002
14003 out:
14004 /* Restore the file and line information. */
14005 input_location = saved_loc;
14006
14007 return r;
14008 }
14009
14010 /* Substitute into the ARG_TYPES of a function type.
14011 If END is a TREE_CHAIN, leave it and any following types
14012 un-substituted. */
14013
14014 static tree
14015 tsubst_arg_types (tree arg_types,
14016 tree args,
14017 tree end,
14018 tsubst_flags_t complain,
14019 tree in_decl)
14020 {
14021 tree remaining_arg_types;
14022 tree type = NULL_TREE;
14023 int i = 1;
14024 tree expanded_args = NULL_TREE;
14025 tree default_arg;
14026
14027 if (!arg_types || arg_types == void_list_node || arg_types == end)
14028 return arg_types;
14029
14030 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14031 args, end, complain, in_decl);
14032 if (remaining_arg_types == error_mark_node)
14033 return error_mark_node;
14034
14035 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14036 {
14037 /* For a pack expansion, perform substitution on the
14038 entire expression. Later on, we'll handle the arguments
14039 one-by-one. */
14040 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14041 args, complain, in_decl);
14042
14043 if (TREE_CODE (expanded_args) == TREE_VEC)
14044 /* So that we'll spin through the parameters, one by one. */
14045 i = TREE_VEC_LENGTH (expanded_args);
14046 else
14047 {
14048 /* We only partially substituted into the parameter
14049 pack. Our type is TYPE_PACK_EXPANSION. */
14050 type = expanded_args;
14051 expanded_args = NULL_TREE;
14052 }
14053 }
14054
14055 while (i > 0) {
14056 --i;
14057
14058 if (expanded_args)
14059 type = TREE_VEC_ELT (expanded_args, i);
14060 else if (!type)
14061 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14062
14063 if (type == error_mark_node)
14064 return error_mark_node;
14065 if (VOID_TYPE_P (type))
14066 {
14067 if (complain & tf_error)
14068 {
14069 error ("invalid parameter type %qT", type);
14070 if (in_decl)
14071 error ("in declaration %q+D", in_decl);
14072 }
14073 return error_mark_node;
14074 }
14075 /* DR 657. */
14076 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14077 return error_mark_node;
14078
14079 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14080 top-level qualifiers as required. */
14081 type = cv_unqualified (type_decays_to (type));
14082
14083 /* We do not substitute into default arguments here. The standard
14084 mandates that they be instantiated only when needed, which is
14085 done in build_over_call. */
14086 default_arg = TREE_PURPOSE (arg_types);
14087
14088 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14089 since the new op() won't have any associated template arguments for us
14090 to refer to later. */
14091 if (lambda_fn_in_template_p (in_decl))
14092 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14093 false/*fn*/, false/*constexpr*/);
14094
14095 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14096 {
14097 /* We've instantiated a template before its default arguments
14098 have been parsed. This can happen for a nested template
14099 class, and is not an error unless we require the default
14100 argument in a call of this function. */
14101 remaining_arg_types =
14102 tree_cons (default_arg, type, remaining_arg_types);
14103 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14104 remaining_arg_types);
14105 }
14106 else
14107 remaining_arg_types =
14108 hash_tree_cons (default_arg, type, remaining_arg_types);
14109 }
14110
14111 return remaining_arg_types;
14112 }
14113
14114 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14115 *not* handle the exception-specification for FNTYPE, because the
14116 initial substitution of explicitly provided template parameters
14117 during argument deduction forbids substitution into the
14118 exception-specification:
14119
14120 [temp.deduct]
14121
14122 All references in the function type of the function template to the
14123 corresponding template parameters are replaced by the specified tem-
14124 plate argument values. If a substitution in a template parameter or
14125 in the function type of the function template results in an invalid
14126 type, type deduction fails. [Note: The equivalent substitution in
14127 exception specifications is done only when the function is instanti-
14128 ated, at which point a program is ill-formed if the substitution
14129 results in an invalid type.] */
14130
14131 static tree
14132 tsubst_function_type (tree t,
14133 tree args,
14134 tsubst_flags_t complain,
14135 tree in_decl)
14136 {
14137 tree return_type;
14138 tree arg_types = NULL_TREE;
14139 tree fntype;
14140
14141 /* The TYPE_CONTEXT is not used for function/method types. */
14142 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14143
14144 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14145 failure. */
14146 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14147
14148 if (late_return_type_p)
14149 {
14150 /* Substitute the argument types. */
14151 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14152 complain, in_decl);
14153 if (arg_types == error_mark_node)
14154 return error_mark_node;
14155
14156 tree save_ccp = current_class_ptr;
14157 tree save_ccr = current_class_ref;
14158 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14159 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14160 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14161 if (do_inject)
14162 {
14163 /* DR 1207: 'this' is in scope in the trailing return type. */
14164 inject_this_parameter (this_type, cp_type_quals (this_type));
14165 }
14166
14167 /* Substitute the return type. */
14168 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14169
14170 if (do_inject)
14171 {
14172 current_class_ptr = save_ccp;
14173 current_class_ref = save_ccr;
14174 }
14175 }
14176 else
14177 /* Substitute the return type. */
14178 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14179
14180 if (return_type == error_mark_node)
14181 return error_mark_node;
14182 /* DR 486 clarifies that creation of a function type with an
14183 invalid return type is a deduction failure. */
14184 if (TREE_CODE (return_type) == ARRAY_TYPE
14185 || TREE_CODE (return_type) == FUNCTION_TYPE)
14186 {
14187 if (complain & tf_error)
14188 {
14189 if (TREE_CODE (return_type) == ARRAY_TYPE)
14190 error ("function returning an array");
14191 else
14192 error ("function returning a function");
14193 }
14194 return error_mark_node;
14195 }
14196 /* And DR 657. */
14197 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14198 return error_mark_node;
14199
14200 if (!late_return_type_p)
14201 {
14202 /* Substitute the argument types. */
14203 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14204 complain, in_decl);
14205 if (arg_types == error_mark_node)
14206 return error_mark_node;
14207 }
14208
14209 /* Construct a new type node and return it. */
14210 if (TREE_CODE (t) == FUNCTION_TYPE)
14211 {
14212 fntype = build_function_type (return_type, arg_types);
14213 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14214 }
14215 else
14216 {
14217 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14218 /* Don't pick up extra function qualifiers from the basetype. */
14219 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14220 if (! MAYBE_CLASS_TYPE_P (r))
14221 {
14222 /* [temp.deduct]
14223
14224 Type deduction may fail for any of the following
14225 reasons:
14226
14227 -- Attempting to create "pointer to member of T" when T
14228 is not a class type. */
14229 if (complain & tf_error)
14230 error ("creating pointer to member function of non-class type %qT",
14231 r);
14232 return error_mark_node;
14233 }
14234
14235 fntype = build_method_type_directly (r, return_type,
14236 TREE_CHAIN (arg_types));
14237 }
14238 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14239
14240 /* See comment above. */
14241 tree raises = NULL_TREE;
14242 cp_ref_qualifier rqual = type_memfn_rqual (t);
14243 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14244
14245 return fntype;
14246 }
14247
14248 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14249 ARGS into that specification, and return the substituted
14250 specification. If there is no specification, return NULL_TREE. */
14251
14252 static tree
14253 tsubst_exception_specification (tree fntype,
14254 tree args,
14255 tsubst_flags_t complain,
14256 tree in_decl,
14257 bool defer_ok)
14258 {
14259 tree specs;
14260 tree new_specs;
14261
14262 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14263 new_specs = NULL_TREE;
14264 if (specs && TREE_PURPOSE (specs))
14265 {
14266 /* A noexcept-specifier. */
14267 tree expr = TREE_PURPOSE (specs);
14268 if (TREE_CODE (expr) == INTEGER_CST)
14269 new_specs = expr;
14270 else if (defer_ok)
14271 {
14272 /* Defer instantiation of noexcept-specifiers to avoid
14273 excessive instantiations (c++/49107). */
14274 new_specs = make_node (DEFERRED_NOEXCEPT);
14275 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14276 {
14277 /* We already partially instantiated this member template,
14278 so combine the new args with the old. */
14279 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14280 = DEFERRED_NOEXCEPT_PATTERN (expr);
14281 DEFERRED_NOEXCEPT_ARGS (new_specs)
14282 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14283 }
14284 else
14285 {
14286 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14287 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14288 }
14289 }
14290 else
14291 {
14292 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14293 {
14294 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14295 args);
14296 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14297 }
14298 new_specs = tsubst_copy_and_build
14299 (expr, args, complain, in_decl, /*function_p=*/false,
14300 /*integral_constant_expression_p=*/true);
14301 }
14302 new_specs = build_noexcept_spec (new_specs, complain);
14303 }
14304 else if (specs)
14305 {
14306 if (! TREE_VALUE (specs))
14307 new_specs = specs;
14308 else
14309 while (specs)
14310 {
14311 tree spec;
14312 int i, len = 1;
14313 tree expanded_specs = NULL_TREE;
14314
14315 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14316 {
14317 /* Expand the pack expansion type. */
14318 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14319 args, complain,
14320 in_decl);
14321
14322 if (expanded_specs == error_mark_node)
14323 return error_mark_node;
14324 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14325 len = TREE_VEC_LENGTH (expanded_specs);
14326 else
14327 {
14328 /* We're substituting into a member template, so
14329 we got a TYPE_PACK_EXPANSION back. Add that
14330 expansion and move on. */
14331 gcc_assert (TREE_CODE (expanded_specs)
14332 == TYPE_PACK_EXPANSION);
14333 new_specs = add_exception_specifier (new_specs,
14334 expanded_specs,
14335 complain);
14336 specs = TREE_CHAIN (specs);
14337 continue;
14338 }
14339 }
14340
14341 for (i = 0; i < len; ++i)
14342 {
14343 if (expanded_specs)
14344 spec = TREE_VEC_ELT (expanded_specs, i);
14345 else
14346 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14347 if (spec == error_mark_node)
14348 return spec;
14349 new_specs = add_exception_specifier (new_specs, spec,
14350 complain);
14351 }
14352
14353 specs = TREE_CHAIN (specs);
14354 }
14355 }
14356 return new_specs;
14357 }
14358
14359 /* Take the tree structure T and replace template parameters used
14360 therein with the argument vector ARGS. IN_DECL is an associated
14361 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14362 Issue error and warning messages under control of COMPLAIN. Note
14363 that we must be relatively non-tolerant of extensions here, in
14364 order to preserve conformance; if we allow substitutions that
14365 should not be allowed, we may allow argument deductions that should
14366 not succeed, and therefore report ambiguous overload situations
14367 where there are none. In theory, we could allow the substitution,
14368 but indicate that it should have failed, and allow our caller to
14369 make sure that the right thing happens, but we don't try to do this
14370 yet.
14371
14372 This function is used for dealing with types, decls and the like;
14373 for expressions, use tsubst_expr or tsubst_copy. */
14374
14375 tree
14376 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14377 {
14378 enum tree_code code;
14379 tree type, r = NULL_TREE;
14380
14381 if (t == NULL_TREE || t == error_mark_node
14382 || t == integer_type_node
14383 || t == void_type_node
14384 || t == char_type_node
14385 || t == unknown_type_node
14386 || TREE_CODE (t) == NAMESPACE_DECL
14387 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14388 return t;
14389
14390 if (DECL_P (t))
14391 return tsubst_decl (t, args, complain);
14392
14393 if (args == NULL_TREE)
14394 return t;
14395
14396 code = TREE_CODE (t);
14397
14398 if (code == IDENTIFIER_NODE)
14399 type = IDENTIFIER_TYPE_VALUE (t);
14400 else
14401 type = TREE_TYPE (t);
14402
14403 gcc_assert (type != unknown_type_node);
14404
14405 /* Reuse typedefs. We need to do this to handle dependent attributes,
14406 such as attribute aligned. */
14407 if (TYPE_P (t)
14408 && typedef_variant_p (t))
14409 {
14410 tree decl = TYPE_NAME (t);
14411
14412 if (alias_template_specialization_p (t))
14413 {
14414 /* DECL represents an alias template and we want to
14415 instantiate it. */
14416 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14417 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14418 r = instantiate_alias_template (tmpl, gen_args, complain);
14419 }
14420 else if (DECL_CLASS_SCOPE_P (decl)
14421 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14422 && uses_template_parms (DECL_CONTEXT (decl)))
14423 {
14424 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14425 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14426 r = retrieve_specialization (tmpl, gen_args, 0);
14427 }
14428 else if (DECL_FUNCTION_SCOPE_P (decl)
14429 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14430 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14431 r = retrieve_local_specialization (decl);
14432 else
14433 /* The typedef is from a non-template context. */
14434 return t;
14435
14436 if (r)
14437 {
14438 r = TREE_TYPE (r);
14439 r = cp_build_qualified_type_real
14440 (r, cp_type_quals (t) | cp_type_quals (r),
14441 complain | tf_ignore_bad_quals);
14442 return r;
14443 }
14444 else
14445 {
14446 /* We don't have an instantiation yet, so drop the typedef. */
14447 int quals = cp_type_quals (t);
14448 t = DECL_ORIGINAL_TYPE (decl);
14449 t = cp_build_qualified_type_real (t, quals,
14450 complain | tf_ignore_bad_quals);
14451 }
14452 }
14453
14454 bool fndecl_type = (complain & tf_fndecl_type);
14455 complain &= ~tf_fndecl_type;
14456
14457 if (type
14458 && code != TYPENAME_TYPE
14459 && code != TEMPLATE_TYPE_PARM
14460 && code != TEMPLATE_PARM_INDEX
14461 && code != IDENTIFIER_NODE
14462 && code != FUNCTION_TYPE
14463 && code != METHOD_TYPE)
14464 type = tsubst (type, args, complain, in_decl);
14465 if (type == error_mark_node)
14466 return error_mark_node;
14467
14468 switch (code)
14469 {
14470 case RECORD_TYPE:
14471 case UNION_TYPE:
14472 case ENUMERAL_TYPE:
14473 return tsubst_aggr_type (t, args, complain, in_decl,
14474 /*entering_scope=*/0);
14475
14476 case ERROR_MARK:
14477 case IDENTIFIER_NODE:
14478 case VOID_TYPE:
14479 case REAL_TYPE:
14480 case COMPLEX_TYPE:
14481 case VECTOR_TYPE:
14482 case BOOLEAN_TYPE:
14483 case NULLPTR_TYPE:
14484 case LANG_TYPE:
14485 return t;
14486
14487 case INTEGER_TYPE:
14488 if (t == integer_type_node)
14489 return t;
14490
14491 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14492 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14493 return t;
14494
14495 {
14496 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14497
14498 max = tsubst_expr (omax, args, complain, in_decl,
14499 /*integral_constant_expression_p=*/false);
14500
14501 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14502 needed. */
14503 if (TREE_CODE (max) == NOP_EXPR
14504 && TREE_SIDE_EFFECTS (omax)
14505 && !TREE_TYPE (max))
14506 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14507
14508 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14509 with TREE_SIDE_EFFECTS that indicates this is not an integral
14510 constant expression. */
14511 if (processing_template_decl
14512 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14513 {
14514 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14515 TREE_SIDE_EFFECTS (max) = 1;
14516 }
14517
14518 return compute_array_index_type (NULL_TREE, max, complain);
14519 }
14520
14521 case TEMPLATE_TYPE_PARM:
14522 case TEMPLATE_TEMPLATE_PARM:
14523 case BOUND_TEMPLATE_TEMPLATE_PARM:
14524 case TEMPLATE_PARM_INDEX:
14525 {
14526 int idx;
14527 int level;
14528 int levels;
14529 tree arg = NULL_TREE;
14530
14531 /* Early in template argument deduction substitution, we don't
14532 want to reduce the level of 'auto', or it will be confused
14533 with a normal template parm in subsequent deduction. */
14534 if (is_auto (t) && (complain & tf_partial))
14535 return t;
14536
14537 r = NULL_TREE;
14538
14539 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14540 template_parm_level_and_index (t, &level, &idx);
14541
14542 levels = TMPL_ARGS_DEPTH (args);
14543 if (level <= levels
14544 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14545 {
14546 arg = TMPL_ARG (args, level, idx);
14547
14548 /* See through ARGUMENT_PACK_SELECT arguments. */
14549 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14550 arg = argument_pack_select_arg (arg);
14551 }
14552
14553 if (arg == error_mark_node)
14554 return error_mark_node;
14555 else if (arg != NULL_TREE)
14556 {
14557 if (ARGUMENT_PACK_P (arg))
14558 /* If ARG is an argument pack, we don't actually want to
14559 perform a substitution here, because substitutions
14560 for argument packs are only done
14561 element-by-element. We can get to this point when
14562 substituting the type of a non-type template
14563 parameter pack, when that type actually contains
14564 template parameter packs from an outer template, e.g.,
14565
14566 template<typename... Types> struct A {
14567 template<Types... Values> struct B { };
14568 }; */
14569 return t;
14570
14571 if (code == TEMPLATE_TYPE_PARM)
14572 {
14573 int quals;
14574 gcc_assert (TYPE_P (arg));
14575
14576 quals = cp_type_quals (arg) | cp_type_quals (t);
14577
14578 return cp_build_qualified_type_real
14579 (arg, quals, complain | tf_ignore_bad_quals);
14580 }
14581 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14582 {
14583 /* We are processing a type constructed from a
14584 template template parameter. */
14585 tree argvec = tsubst (TYPE_TI_ARGS (t),
14586 args, complain, in_decl);
14587 if (argvec == error_mark_node)
14588 return error_mark_node;
14589
14590 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14591 || TREE_CODE (arg) == TEMPLATE_DECL
14592 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14593
14594 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14595 /* Consider this code:
14596
14597 template <template <class> class Template>
14598 struct Internal {
14599 template <class Arg> using Bind = Template<Arg>;
14600 };
14601
14602 template <template <class> class Template, class Arg>
14603 using Instantiate = Template<Arg>; //#0
14604
14605 template <template <class> class Template,
14606 class Argument>
14607 using Bind =
14608 Instantiate<Internal<Template>::template Bind,
14609 Argument>; //#1
14610
14611 When #1 is parsed, the
14612 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14613 parameter `Template' in #0 matches the
14614 UNBOUND_CLASS_TEMPLATE representing the argument
14615 `Internal<Template>::template Bind'; We then want
14616 to assemble the type `Bind<Argument>' that can't
14617 be fully created right now, because
14618 `Internal<Template>' not being complete, the Bind
14619 template cannot be looked up in that context. So
14620 we need to "store" `Bind<Argument>' for later
14621 when the context of Bind becomes complete. Let's
14622 store that in a TYPENAME_TYPE. */
14623 return make_typename_type (TYPE_CONTEXT (arg),
14624 build_nt (TEMPLATE_ID_EXPR,
14625 TYPE_IDENTIFIER (arg),
14626 argvec),
14627 typename_type,
14628 complain);
14629
14630 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14631 are resolving nested-types in the signature of a
14632 member function templates. Otherwise ARG is a
14633 TEMPLATE_DECL and is the real template to be
14634 instantiated. */
14635 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14636 arg = TYPE_NAME (arg);
14637
14638 r = lookup_template_class (arg,
14639 argvec, in_decl,
14640 DECL_CONTEXT (arg),
14641 /*entering_scope=*/0,
14642 complain);
14643 return cp_build_qualified_type_real
14644 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14645 }
14646 else if (code == TEMPLATE_TEMPLATE_PARM)
14647 return arg;
14648 else
14649 /* TEMPLATE_PARM_INDEX. */
14650 return convert_from_reference (unshare_expr (arg));
14651 }
14652
14653 if (level == 1)
14654 /* This can happen during the attempted tsubst'ing in
14655 unify. This means that we don't yet have any information
14656 about the template parameter in question. */
14657 return t;
14658
14659 /* If we get here, we must have been looking at a parm for a
14660 more deeply nested template. Make a new version of this
14661 template parameter, but with a lower level. */
14662 switch (code)
14663 {
14664 case TEMPLATE_TYPE_PARM:
14665 case TEMPLATE_TEMPLATE_PARM:
14666 case BOUND_TEMPLATE_TEMPLATE_PARM:
14667 if (cp_type_quals (t))
14668 {
14669 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14670 r = cp_build_qualified_type_real
14671 (r, cp_type_quals (t),
14672 complain | (code == TEMPLATE_TYPE_PARM
14673 ? tf_ignore_bad_quals : 0));
14674 }
14675 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14676 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14677 && (r = (TEMPLATE_PARM_DESCENDANTS
14678 (TEMPLATE_TYPE_PARM_INDEX (t))))
14679 && (r = TREE_TYPE (r))
14680 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14681 /* Break infinite recursion when substituting the constraints
14682 of a constrained placeholder. */;
14683 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14684 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14685 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14686 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14687 r = TEMPLATE_PARM_DESCENDANTS (arg))
14688 && (TEMPLATE_PARM_LEVEL (r)
14689 == TEMPLATE_PARM_LEVEL (arg) - levels))
14690 /* Cache the simple case of lowering a type parameter. */
14691 r = TREE_TYPE (r);
14692 else
14693 {
14694 r = copy_type (t);
14695 TEMPLATE_TYPE_PARM_INDEX (r)
14696 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14697 r, levels, args, complain);
14698 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14699 TYPE_MAIN_VARIANT (r) = r;
14700 TYPE_POINTER_TO (r) = NULL_TREE;
14701 TYPE_REFERENCE_TO (r) = NULL_TREE;
14702
14703 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14704 {
14705 /* Propagate constraints on placeholders. */
14706 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14707 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14708 = tsubst_constraint (constr, args, complain, in_decl);
14709 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14710 {
14711 pl = tsubst_copy (pl, args, complain, in_decl);
14712 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14713 }
14714 }
14715
14716 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14717 /* We have reduced the level of the template
14718 template parameter, but not the levels of its
14719 template parameters, so canonical_type_parameter
14720 will not be able to find the canonical template
14721 template parameter for this level. Thus, we
14722 require structural equality checking to compare
14723 TEMPLATE_TEMPLATE_PARMs. */
14724 SET_TYPE_STRUCTURAL_EQUALITY (r);
14725 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14726 SET_TYPE_STRUCTURAL_EQUALITY (r);
14727 else
14728 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14729
14730 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14731 {
14732 tree tinfo = TYPE_TEMPLATE_INFO (t);
14733 /* We might need to substitute into the types of non-type
14734 template parameters. */
14735 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14736 complain, in_decl);
14737 if (tmpl == error_mark_node)
14738 return error_mark_node;
14739 tree argvec = tsubst (TI_ARGS (tinfo), args,
14740 complain, in_decl);
14741 if (argvec == error_mark_node)
14742 return error_mark_node;
14743
14744 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14745 = build_template_info (tmpl, argvec);
14746 }
14747 }
14748 break;
14749
14750 case TEMPLATE_PARM_INDEX:
14751 /* OK, now substitute the type of the non-type parameter. We
14752 couldn't do it earlier because it might be an auto parameter,
14753 and we wouldn't need to if we had an argument. */
14754 type = tsubst (type, args, complain, in_decl);
14755 if (type == error_mark_node)
14756 return error_mark_node;
14757 r = reduce_template_parm_level (t, type, levels, args, complain);
14758 break;
14759
14760 default:
14761 gcc_unreachable ();
14762 }
14763
14764 return r;
14765 }
14766
14767 case TREE_LIST:
14768 {
14769 tree purpose, value, chain;
14770
14771 if (t == void_list_node)
14772 return t;
14773
14774 purpose = TREE_PURPOSE (t);
14775 if (purpose)
14776 {
14777 purpose = tsubst (purpose, args, complain, in_decl);
14778 if (purpose == error_mark_node)
14779 return error_mark_node;
14780 }
14781 value = TREE_VALUE (t);
14782 if (value)
14783 {
14784 value = tsubst (value, args, complain, in_decl);
14785 if (value == error_mark_node)
14786 return error_mark_node;
14787 }
14788 chain = TREE_CHAIN (t);
14789 if (chain && chain != void_type_node)
14790 {
14791 chain = tsubst (chain, args, complain, in_decl);
14792 if (chain == error_mark_node)
14793 return error_mark_node;
14794 }
14795 if (purpose == TREE_PURPOSE (t)
14796 && value == TREE_VALUE (t)
14797 && chain == TREE_CHAIN (t))
14798 return t;
14799 return hash_tree_cons (purpose, value, chain);
14800 }
14801
14802 case TREE_BINFO:
14803 /* We should never be tsubsting a binfo. */
14804 gcc_unreachable ();
14805
14806 case TREE_VEC:
14807 /* A vector of template arguments. */
14808 gcc_assert (!type);
14809 return tsubst_template_args (t, args, complain, in_decl);
14810
14811 case POINTER_TYPE:
14812 case REFERENCE_TYPE:
14813 {
14814 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14815 return t;
14816
14817 /* [temp.deduct]
14818
14819 Type deduction may fail for any of the following
14820 reasons:
14821
14822 -- Attempting to create a pointer to reference type.
14823 -- Attempting to create a reference to a reference type or
14824 a reference to void.
14825
14826 Core issue 106 says that creating a reference to a reference
14827 during instantiation is no longer a cause for failure. We
14828 only enforce this check in strict C++98 mode. */
14829 if ((TYPE_REF_P (type)
14830 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14831 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14832 {
14833 static location_t last_loc;
14834
14835 /* We keep track of the last time we issued this error
14836 message to avoid spewing a ton of messages during a
14837 single bad template instantiation. */
14838 if (complain & tf_error
14839 && last_loc != input_location)
14840 {
14841 if (VOID_TYPE_P (type))
14842 error ("forming reference to void");
14843 else if (code == POINTER_TYPE)
14844 error ("forming pointer to reference type %qT", type);
14845 else
14846 error ("forming reference to reference type %qT", type);
14847 last_loc = input_location;
14848 }
14849
14850 return error_mark_node;
14851 }
14852 else if (TREE_CODE (type) == FUNCTION_TYPE
14853 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14854 || type_memfn_rqual (type) != REF_QUAL_NONE))
14855 {
14856 if (complain & tf_error)
14857 {
14858 if (code == POINTER_TYPE)
14859 error ("forming pointer to qualified function type %qT",
14860 type);
14861 else
14862 error ("forming reference to qualified function type %qT",
14863 type);
14864 }
14865 return error_mark_node;
14866 }
14867 else if (code == POINTER_TYPE)
14868 {
14869 r = build_pointer_type (type);
14870 if (TREE_CODE (type) == METHOD_TYPE)
14871 r = build_ptrmemfunc_type (r);
14872 }
14873 else if (TYPE_REF_P (type))
14874 /* In C++0x, during template argument substitution, when there is an
14875 attempt to create a reference to a reference type, reference
14876 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14877
14878 "If a template-argument for a template-parameter T names a type
14879 that is a reference to a type A, an attempt to create the type
14880 'lvalue reference to cv T' creates the type 'lvalue reference to
14881 A,' while an attempt to create the type type rvalue reference to
14882 cv T' creates the type T"
14883 */
14884 r = cp_build_reference_type
14885 (TREE_TYPE (type),
14886 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14887 else
14888 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14889 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14890
14891 if (r != error_mark_node)
14892 /* Will this ever be needed for TYPE_..._TO values? */
14893 layout_type (r);
14894
14895 return r;
14896 }
14897 case OFFSET_TYPE:
14898 {
14899 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14900 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14901 {
14902 /* [temp.deduct]
14903
14904 Type deduction may fail for any of the following
14905 reasons:
14906
14907 -- Attempting to create "pointer to member of T" when T
14908 is not a class type. */
14909 if (complain & tf_error)
14910 error ("creating pointer to member of non-class type %qT", r);
14911 return error_mark_node;
14912 }
14913 if (TYPE_REF_P (type))
14914 {
14915 if (complain & tf_error)
14916 error ("creating pointer to member reference type %qT", type);
14917 return error_mark_node;
14918 }
14919 if (VOID_TYPE_P (type))
14920 {
14921 if (complain & tf_error)
14922 error ("creating pointer to member of type void");
14923 return error_mark_node;
14924 }
14925 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14926 if (TREE_CODE (type) == FUNCTION_TYPE)
14927 {
14928 /* The type of the implicit object parameter gets its
14929 cv-qualifiers from the FUNCTION_TYPE. */
14930 tree memptr;
14931 tree method_type
14932 = build_memfn_type (type, r, type_memfn_quals (type),
14933 type_memfn_rqual (type));
14934 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14935 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14936 complain);
14937 }
14938 else
14939 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14940 cp_type_quals (t),
14941 complain);
14942 }
14943 case FUNCTION_TYPE:
14944 case METHOD_TYPE:
14945 {
14946 tree fntype;
14947 tree specs;
14948 fntype = tsubst_function_type (t, args, complain, in_decl);
14949 if (fntype == error_mark_node)
14950 return error_mark_node;
14951
14952 /* Substitute the exception specification. */
14953 specs = tsubst_exception_specification (t, args, complain, in_decl,
14954 /*defer_ok*/fndecl_type);
14955 if (specs == error_mark_node)
14956 return error_mark_node;
14957 if (specs)
14958 fntype = build_exception_variant (fntype, specs);
14959 return fntype;
14960 }
14961 case ARRAY_TYPE:
14962 {
14963 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14964 if (domain == error_mark_node)
14965 return error_mark_node;
14966
14967 /* As an optimization, we avoid regenerating the array type if
14968 it will obviously be the same as T. */
14969 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14970 return t;
14971
14972 /* These checks should match the ones in create_array_type_for_decl.
14973
14974 [temp.deduct]
14975
14976 The deduction may fail for any of the following reasons:
14977
14978 -- Attempting to create an array with an element type that
14979 is void, a function type, or a reference type, or [DR337]
14980 an abstract class type. */
14981 if (VOID_TYPE_P (type)
14982 || TREE_CODE (type) == FUNCTION_TYPE
14983 || (TREE_CODE (type) == ARRAY_TYPE
14984 && TYPE_DOMAIN (type) == NULL_TREE)
14985 || TYPE_REF_P (type))
14986 {
14987 if (complain & tf_error)
14988 error ("creating array of %qT", type);
14989 return error_mark_node;
14990 }
14991
14992 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14993 return error_mark_node;
14994
14995 r = build_cplus_array_type (type, domain);
14996
14997 if (!valid_array_size_p (input_location, r, in_decl,
14998 (complain & tf_error)))
14999 return error_mark_node;
15000
15001 if (TYPE_USER_ALIGN (t))
15002 {
15003 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15004 TYPE_USER_ALIGN (r) = 1;
15005 }
15006
15007 return r;
15008 }
15009
15010 case TYPENAME_TYPE:
15011 {
15012 tree ctx = TYPE_CONTEXT (t);
15013 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15014 {
15015 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15016 if (ctx == error_mark_node
15017 || TREE_VEC_LENGTH (ctx) > 1)
15018 return error_mark_node;
15019 if (TREE_VEC_LENGTH (ctx) == 0)
15020 {
15021 if (complain & tf_error)
15022 error ("%qD is instantiated for an empty pack",
15023 TYPENAME_TYPE_FULLNAME (t));
15024 return error_mark_node;
15025 }
15026 ctx = TREE_VEC_ELT (ctx, 0);
15027 }
15028 else
15029 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15030 /*entering_scope=*/1);
15031 if (ctx == error_mark_node)
15032 return error_mark_node;
15033
15034 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15035 complain, in_decl);
15036 if (f == error_mark_node)
15037 return error_mark_node;
15038
15039 if (!MAYBE_CLASS_TYPE_P (ctx))
15040 {
15041 if (complain & tf_error)
15042 error ("%qT is not a class, struct, or union type", ctx);
15043 return error_mark_node;
15044 }
15045 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15046 {
15047 /* Normally, make_typename_type does not require that the CTX
15048 have complete type in order to allow things like:
15049
15050 template <class T> struct S { typename S<T>::X Y; };
15051
15052 But, such constructs have already been resolved by this
15053 point, so here CTX really should have complete type, unless
15054 it's a partial instantiation. */
15055 ctx = complete_type (ctx);
15056 if (!COMPLETE_TYPE_P (ctx))
15057 {
15058 if (complain & tf_error)
15059 cxx_incomplete_type_error (NULL_TREE, ctx);
15060 return error_mark_node;
15061 }
15062 }
15063
15064 f = make_typename_type (ctx, f, typename_type,
15065 complain | tf_keep_type_decl);
15066 if (f == error_mark_node)
15067 return f;
15068 if (TREE_CODE (f) == TYPE_DECL)
15069 {
15070 complain |= tf_ignore_bad_quals;
15071 f = TREE_TYPE (f);
15072 }
15073
15074 if (TREE_CODE (f) != TYPENAME_TYPE)
15075 {
15076 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15077 {
15078 if (complain & tf_error)
15079 error ("%qT resolves to %qT, which is not an enumeration type",
15080 t, f);
15081 else
15082 return error_mark_node;
15083 }
15084 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15085 {
15086 if (complain & tf_error)
15087 error ("%qT resolves to %qT, which is is not a class type",
15088 t, f);
15089 else
15090 return error_mark_node;
15091 }
15092 }
15093
15094 return cp_build_qualified_type_real
15095 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15096 }
15097
15098 case UNBOUND_CLASS_TEMPLATE:
15099 {
15100 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15101 in_decl, /*entering_scope=*/1);
15102 tree name = TYPE_IDENTIFIER (t);
15103 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15104
15105 if (ctx == error_mark_node || name == error_mark_node)
15106 return error_mark_node;
15107
15108 if (parm_list)
15109 parm_list = tsubst_template_parms (parm_list, args, complain);
15110 return make_unbound_class_template (ctx, name, parm_list, complain);
15111 }
15112
15113 case TYPEOF_TYPE:
15114 {
15115 tree type;
15116
15117 ++cp_unevaluated_operand;
15118 ++c_inhibit_evaluation_warnings;
15119
15120 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15121 complain, in_decl,
15122 /*integral_constant_expression_p=*/false);
15123
15124 --cp_unevaluated_operand;
15125 --c_inhibit_evaluation_warnings;
15126
15127 type = finish_typeof (type);
15128 return cp_build_qualified_type_real (type,
15129 cp_type_quals (t)
15130 | cp_type_quals (type),
15131 complain);
15132 }
15133
15134 case DECLTYPE_TYPE:
15135 {
15136 tree type;
15137
15138 ++cp_unevaluated_operand;
15139 ++c_inhibit_evaluation_warnings;
15140
15141 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15142 complain|tf_decltype, in_decl,
15143 /*function_p*/false,
15144 /*integral_constant_expression*/false);
15145
15146 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15147 {
15148 if (type == NULL_TREE)
15149 {
15150 if (complain & tf_error)
15151 error ("empty initializer in lambda init-capture");
15152 type = error_mark_node;
15153 }
15154 else if (TREE_CODE (type) == TREE_LIST)
15155 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15156 }
15157
15158 --cp_unevaluated_operand;
15159 --c_inhibit_evaluation_warnings;
15160
15161 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15162 type = lambda_capture_field_type (type,
15163 DECLTYPE_FOR_INIT_CAPTURE (t),
15164 DECLTYPE_FOR_REF_CAPTURE (t));
15165 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15166 type = lambda_proxy_type (type);
15167 else
15168 {
15169 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15170 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15171 && EXPR_P (type))
15172 /* In a template ~id could be either a complement expression
15173 or an unqualified-id naming a destructor; if instantiating
15174 it produces an expression, it's not an id-expression or
15175 member access. */
15176 id = false;
15177 type = finish_decltype_type (type, id, complain);
15178 }
15179 return cp_build_qualified_type_real (type,
15180 cp_type_quals (t)
15181 | cp_type_quals (type),
15182 complain | tf_ignore_bad_quals);
15183 }
15184
15185 case UNDERLYING_TYPE:
15186 {
15187 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15188 complain, in_decl);
15189 return finish_underlying_type (type);
15190 }
15191
15192 case TYPE_ARGUMENT_PACK:
15193 case NONTYPE_ARGUMENT_PACK:
15194 {
15195 tree r;
15196
15197 if (code == NONTYPE_ARGUMENT_PACK)
15198 r = make_node (code);
15199 else
15200 r = cxx_make_type (code);
15201
15202 tree pack_args = ARGUMENT_PACK_ARGS (t);
15203 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15204 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15205
15206 return r;
15207 }
15208
15209 case VOID_CST:
15210 case INTEGER_CST:
15211 case REAL_CST:
15212 case STRING_CST:
15213 case PLUS_EXPR:
15214 case MINUS_EXPR:
15215 case NEGATE_EXPR:
15216 case NOP_EXPR:
15217 case INDIRECT_REF:
15218 case ADDR_EXPR:
15219 case CALL_EXPR:
15220 case ARRAY_REF:
15221 case SCOPE_REF:
15222 /* We should use one of the expression tsubsts for these codes. */
15223 gcc_unreachable ();
15224
15225 default:
15226 sorry ("use of %qs in template", get_tree_code_name (code));
15227 return error_mark_node;
15228 }
15229 }
15230
15231 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15232 expression on the left-hand side of the "." or "->" operator. We
15233 only do the lookup if we had a dependent BASELINK. Otherwise we
15234 adjust it onto the instantiated heirarchy. */
15235
15236 static tree
15237 tsubst_baselink (tree baselink, tree object_type,
15238 tree args, tsubst_flags_t complain, tree in_decl)
15239 {
15240 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15241 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15242 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15243
15244 tree optype = BASELINK_OPTYPE (baselink);
15245 optype = tsubst (optype, args, complain, in_decl);
15246
15247 tree template_args = NULL_TREE;
15248 bool template_id_p = false;
15249 tree fns = BASELINK_FUNCTIONS (baselink);
15250 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15251 {
15252 template_id_p = true;
15253 template_args = TREE_OPERAND (fns, 1);
15254 fns = TREE_OPERAND (fns, 0);
15255 if (template_args)
15256 template_args = tsubst_template_args (template_args, args,
15257 complain, in_decl);
15258 }
15259
15260 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15261 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15262 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15263
15264 if (dependent_p)
15265 {
15266 tree name = OVL_NAME (fns);
15267 if (IDENTIFIER_CONV_OP_P (name))
15268 name = make_conv_op_name (optype);
15269
15270 if (name == complete_dtor_identifier)
15271 /* Treat as-if non-dependent below. */
15272 dependent_p = false;
15273
15274 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15275 if (!baselink)
15276 {
15277 if ((complain & tf_error)
15278 && constructor_name_p (name, qualifying_scope))
15279 error ("cannot call constructor %<%T::%D%> directly",
15280 qualifying_scope, name);
15281 return error_mark_node;
15282 }
15283
15284 if (BASELINK_P (baselink))
15285 fns = BASELINK_FUNCTIONS (baselink);
15286 }
15287 else
15288 /* We're going to overwrite pieces below, make a duplicate. */
15289 baselink = copy_node (baselink);
15290
15291 /* If lookup found a single function, mark it as used at this point.
15292 (If lookup found multiple functions the one selected later by
15293 overload resolution will be marked as used at that point.) */
15294 if (!template_id_p && !really_overloaded_fn (fns))
15295 {
15296 tree fn = OVL_FIRST (fns);
15297 bool ok = mark_used (fn, complain);
15298 if (!ok && !(complain & tf_error))
15299 return error_mark_node;
15300 if (ok && BASELINK_P (baselink))
15301 /* We might have instantiated an auto function. */
15302 TREE_TYPE (baselink) = TREE_TYPE (fn);
15303 }
15304
15305 if (BASELINK_P (baselink))
15306 {
15307 /* Add back the template arguments, if present. */
15308 if (template_id_p)
15309 BASELINK_FUNCTIONS (baselink)
15310 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15311
15312 /* Update the conversion operator type. */
15313 BASELINK_OPTYPE (baselink) = optype;
15314 }
15315
15316 if (!object_type)
15317 object_type = current_class_type;
15318
15319 if (qualified_p || !dependent_p)
15320 {
15321 baselink = adjust_result_of_qualified_name_lookup (baselink,
15322 qualifying_scope,
15323 object_type);
15324 if (!qualified_p)
15325 /* We need to call adjust_result_of_qualified_name_lookup in case the
15326 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15327 so that we still get virtual function binding. */
15328 BASELINK_QUALIFIED_P (baselink) = false;
15329 }
15330
15331 return baselink;
15332 }
15333
15334 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15335 true if the qualified-id will be a postfix-expression in-and-of
15336 itself; false if more of the postfix-expression follows the
15337 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15338 of "&". */
15339
15340 static tree
15341 tsubst_qualified_id (tree qualified_id, tree args,
15342 tsubst_flags_t complain, tree in_decl,
15343 bool done, bool address_p)
15344 {
15345 tree expr;
15346 tree scope;
15347 tree name;
15348 bool is_template;
15349 tree template_args;
15350 location_t loc = UNKNOWN_LOCATION;
15351
15352 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15353
15354 /* Figure out what name to look up. */
15355 name = TREE_OPERAND (qualified_id, 1);
15356 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15357 {
15358 is_template = true;
15359 loc = EXPR_LOCATION (name);
15360 template_args = TREE_OPERAND (name, 1);
15361 if (template_args)
15362 template_args = tsubst_template_args (template_args, args,
15363 complain, in_decl);
15364 if (template_args == error_mark_node)
15365 return error_mark_node;
15366 name = TREE_OPERAND (name, 0);
15367 }
15368 else
15369 {
15370 is_template = false;
15371 template_args = NULL_TREE;
15372 }
15373
15374 /* Substitute into the qualifying scope. When there are no ARGS, we
15375 are just trying to simplify a non-dependent expression. In that
15376 case the qualifying scope may be dependent, and, in any case,
15377 substituting will not help. */
15378 scope = TREE_OPERAND (qualified_id, 0);
15379 if (args)
15380 {
15381 scope = tsubst (scope, args, complain, in_decl);
15382 expr = tsubst_copy (name, args, complain, in_decl);
15383 }
15384 else
15385 expr = name;
15386
15387 if (dependent_scope_p (scope))
15388 {
15389 if (is_template)
15390 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15391 tree r = build_qualified_name (NULL_TREE, scope, expr,
15392 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15393 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15394 return r;
15395 }
15396
15397 if (!BASELINK_P (name) && !DECL_P (expr))
15398 {
15399 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15400 {
15401 /* A BIT_NOT_EXPR is used to represent a destructor. */
15402 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15403 {
15404 error ("qualifying type %qT does not match destructor name ~%qT",
15405 scope, TREE_OPERAND (expr, 0));
15406 expr = error_mark_node;
15407 }
15408 else
15409 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15410 /*is_type_p=*/0, false);
15411 }
15412 else
15413 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15414 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15415 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15416 {
15417 if (complain & tf_error)
15418 {
15419 error ("dependent-name %qE is parsed as a non-type, but "
15420 "instantiation yields a type", qualified_id);
15421 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15422 }
15423 return error_mark_node;
15424 }
15425 }
15426
15427 if (DECL_P (expr))
15428 {
15429 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15430 scope);
15431 /* Remember that there was a reference to this entity. */
15432 if (!mark_used (expr, complain) && !(complain & tf_error))
15433 return error_mark_node;
15434 }
15435
15436 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15437 {
15438 if (complain & tf_error)
15439 qualified_name_lookup_error (scope,
15440 TREE_OPERAND (qualified_id, 1),
15441 expr, input_location);
15442 return error_mark_node;
15443 }
15444
15445 if (is_template)
15446 {
15447 /* We may be repeating a check already done during parsing, but
15448 if it was well-formed and passed then, it will pass again
15449 now, and if it didn't, we wouldn't have got here. The case
15450 we want to catch is when we couldn't tell then, and can now,
15451 namely when templ prior to substitution was an
15452 identifier. */
15453 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15454 return error_mark_node;
15455
15456 if (variable_template_p (expr))
15457 expr = lookup_and_finish_template_variable (expr, template_args,
15458 complain);
15459 else
15460 expr = lookup_template_function (expr, template_args);
15461 }
15462
15463 if (expr == error_mark_node && complain & tf_error)
15464 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15465 expr, input_location);
15466 else if (TYPE_P (scope))
15467 {
15468 expr = (adjust_result_of_qualified_name_lookup
15469 (expr, scope, current_nonlambda_class_type ()));
15470 expr = (finish_qualified_id_expr
15471 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15472 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15473 /*template_arg_p=*/false, complain));
15474 }
15475
15476 /* Expressions do not generally have reference type. */
15477 if (TREE_CODE (expr) != SCOPE_REF
15478 /* However, if we're about to form a pointer-to-member, we just
15479 want the referenced member referenced. */
15480 && TREE_CODE (expr) != OFFSET_REF)
15481 expr = convert_from_reference (expr);
15482
15483 if (REF_PARENTHESIZED_P (qualified_id))
15484 expr = force_paren_expr (expr);
15485
15486 return expr;
15487 }
15488
15489 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15490 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15491 for tsubst. */
15492
15493 static tree
15494 tsubst_init (tree init, tree decl, tree args,
15495 tsubst_flags_t complain, tree in_decl)
15496 {
15497 if (!init)
15498 return NULL_TREE;
15499
15500 init = tsubst_expr (init, args, complain, in_decl, false);
15501
15502 tree type = TREE_TYPE (decl);
15503
15504 if (!init && type != error_mark_node)
15505 {
15506 if (tree auto_node = type_uses_auto (type))
15507 {
15508 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15509 {
15510 if (complain & tf_error)
15511 error ("initializer for %q#D expands to an empty list "
15512 "of expressions", decl);
15513 return error_mark_node;
15514 }
15515 }
15516 else if (!dependent_type_p (type))
15517 {
15518 /* If we had an initializer but it
15519 instantiated to nothing,
15520 value-initialize the object. This will
15521 only occur when the initializer was a
15522 pack expansion where the parameter packs
15523 used in that expansion were of length
15524 zero. */
15525 init = build_value_init (type, complain);
15526 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15527 init = get_target_expr_sfinae (init, complain);
15528 if (TREE_CODE (init) == TARGET_EXPR)
15529 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15530 }
15531 }
15532
15533 return init;
15534 }
15535
15536 /* Like tsubst, but deals with expressions. This function just replaces
15537 template parms; to finish processing the resultant expression, use
15538 tsubst_copy_and_build or tsubst_expr. */
15539
15540 static tree
15541 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15542 {
15543 enum tree_code code;
15544 tree r;
15545
15546 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15547 return t;
15548
15549 code = TREE_CODE (t);
15550
15551 switch (code)
15552 {
15553 case PARM_DECL:
15554 r = retrieve_local_specialization (t);
15555
15556 if (r == NULL_TREE)
15557 {
15558 /* We get here for a use of 'this' in an NSDMI. */
15559 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15560 return current_class_ptr;
15561
15562 /* This can happen for a parameter name used later in a function
15563 declaration (such as in a late-specified return type). Just
15564 make a dummy decl, since it's only used for its type. */
15565 gcc_assert (cp_unevaluated_operand != 0);
15566 r = tsubst_decl (t, args, complain);
15567 /* Give it the template pattern as its context; its true context
15568 hasn't been instantiated yet and this is good enough for
15569 mangling. */
15570 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15571 }
15572
15573 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15574 r = argument_pack_select_arg (r);
15575 if (!mark_used (r, complain) && !(complain & tf_error))
15576 return error_mark_node;
15577 return r;
15578
15579 case CONST_DECL:
15580 {
15581 tree enum_type;
15582 tree v;
15583
15584 if (DECL_TEMPLATE_PARM_P (t))
15585 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15586 /* There is no need to substitute into namespace-scope
15587 enumerators. */
15588 if (DECL_NAMESPACE_SCOPE_P (t))
15589 return t;
15590 /* If ARGS is NULL, then T is known to be non-dependent. */
15591 if (args == NULL_TREE)
15592 return scalar_constant_value (t);
15593
15594 /* Unfortunately, we cannot just call lookup_name here.
15595 Consider:
15596
15597 template <int I> int f() {
15598 enum E { a = I };
15599 struct S { void g() { E e = a; } };
15600 };
15601
15602 When we instantiate f<7>::S::g(), say, lookup_name is not
15603 clever enough to find f<7>::a. */
15604 enum_type
15605 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15606 /*entering_scope=*/0);
15607
15608 for (v = TYPE_VALUES (enum_type);
15609 v != NULL_TREE;
15610 v = TREE_CHAIN (v))
15611 if (TREE_PURPOSE (v) == DECL_NAME (t))
15612 return TREE_VALUE (v);
15613
15614 /* We didn't find the name. That should never happen; if
15615 name-lookup found it during preliminary parsing, we
15616 should find it again here during instantiation. */
15617 gcc_unreachable ();
15618 }
15619 return t;
15620
15621 case FIELD_DECL:
15622 if (DECL_CONTEXT (t))
15623 {
15624 tree ctx;
15625
15626 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15627 /*entering_scope=*/1);
15628 if (ctx != DECL_CONTEXT (t))
15629 {
15630 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15631 if (!r)
15632 {
15633 if (complain & tf_error)
15634 error ("using invalid field %qD", t);
15635 return error_mark_node;
15636 }
15637 return r;
15638 }
15639 }
15640
15641 return t;
15642
15643 case VAR_DECL:
15644 case FUNCTION_DECL:
15645 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15646 r = tsubst (t, args, complain, in_decl);
15647 else if (local_variable_p (t)
15648 && uses_template_parms (DECL_CONTEXT (t)))
15649 {
15650 r = retrieve_local_specialization (t);
15651 if (r == NULL_TREE)
15652 {
15653 /* First try name lookup to find the instantiation. */
15654 r = lookup_name (DECL_NAME (t));
15655 if (r)
15656 {
15657 if (!VAR_P (r))
15658 {
15659 /* During error-recovery we may find a non-variable,
15660 even an OVERLOAD: just bail out and avoid ICEs and
15661 duplicate diagnostics (c++/62207). */
15662 gcc_assert (seen_error ());
15663 return error_mark_node;
15664 }
15665 if (!is_capture_proxy (r))
15666 {
15667 /* Make sure the one we found is the one we want. */
15668 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15669 if (ctx != DECL_CONTEXT (r))
15670 r = NULL_TREE;
15671 }
15672 }
15673
15674 if (r)
15675 /* OK */;
15676 else
15677 {
15678 /* This can happen for a variable used in a
15679 late-specified return type of a local lambda, or for a
15680 local static or constant. Building a new VAR_DECL
15681 should be OK in all those cases. */
15682 r = tsubst_decl (t, args, complain);
15683 if (local_specializations)
15684 /* Avoid infinite recursion (79640). */
15685 register_local_specialization (r, t);
15686 if (decl_maybe_constant_var_p (r))
15687 {
15688 /* We can't call cp_finish_decl, so handle the
15689 initializer by hand. */
15690 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15691 complain, in_decl);
15692 if (!processing_template_decl)
15693 init = maybe_constant_init (init);
15694 if (processing_template_decl
15695 ? potential_constant_expression (init)
15696 : reduced_constant_expression_p (init))
15697 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15698 = TREE_CONSTANT (r) = true;
15699 DECL_INITIAL (r) = init;
15700 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15701 TREE_TYPE (r)
15702 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15703 complain, adc_variable_type);
15704 }
15705 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15706 || decl_constant_var_p (r)
15707 || seen_error ());
15708 if (!processing_template_decl
15709 && !TREE_STATIC (r))
15710 r = process_outer_var_ref (r, complain);
15711 }
15712 /* Remember this for subsequent uses. */
15713 if (local_specializations)
15714 register_local_specialization (r, t);
15715 }
15716 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15717 r = argument_pack_select_arg (r);
15718 }
15719 else
15720 r = t;
15721 if (!mark_used (r, complain))
15722 return error_mark_node;
15723 return r;
15724
15725 case NAMESPACE_DECL:
15726 return t;
15727
15728 case OVERLOAD:
15729 return t;
15730
15731 case BASELINK:
15732 return tsubst_baselink (t, current_nonlambda_class_type (),
15733 args, complain, in_decl);
15734
15735 case TEMPLATE_DECL:
15736 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15737 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15738 args, complain, in_decl);
15739 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15740 return tsubst (t, args, complain, in_decl);
15741 else if (DECL_CLASS_SCOPE_P (t)
15742 && uses_template_parms (DECL_CONTEXT (t)))
15743 {
15744 /* Template template argument like the following example need
15745 special treatment:
15746
15747 template <template <class> class TT> struct C {};
15748 template <class T> struct D {
15749 template <class U> struct E {};
15750 C<E> c; // #1
15751 };
15752 D<int> d; // #2
15753
15754 We are processing the template argument `E' in #1 for
15755 the template instantiation #2. Originally, `E' is a
15756 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15757 have to substitute this with one having context `D<int>'. */
15758
15759 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15760 if (dependent_scope_p (context))
15761 {
15762 /* When rewriting a constructor into a deduction guide, a
15763 non-dependent name can become dependent, so memtmpl<args>
15764 becomes context::template memtmpl<args>. */
15765 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15766 return build_qualified_name (type, context, DECL_NAME (t),
15767 /*template*/true);
15768 }
15769 return lookup_field (context, DECL_NAME(t), 0, false);
15770 }
15771 else
15772 /* Ordinary template template argument. */
15773 return t;
15774
15775 case NON_LVALUE_EXPR:
15776 case VIEW_CONVERT_EXPR:
15777 {
15778 /* Handle location wrappers by substituting the wrapped node
15779 first, *then* reusing the resulting type. Doing the type
15780 first ensures that we handle template parameters and
15781 parameter pack expansions. */
15782 if (location_wrapper_p (t))
15783 {
15784 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15785 complain, in_decl);
15786 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15787 }
15788 tree op = TREE_OPERAND (t, 0);
15789 if (code == VIEW_CONVERT_EXPR
15790 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15791 {
15792 /* Wrapper to make a C++20 template parameter object const. */
15793 op = tsubst_copy (op, args, complain, in_decl);
15794 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15795 {
15796 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15797 return build1 (code, type, op);
15798 }
15799 else
15800 {
15801 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15802 return op;
15803 }
15804 }
15805 /* We shouldn't see any other uses of these in templates. */
15806 gcc_unreachable ();
15807 }
15808
15809 case CAST_EXPR:
15810 case REINTERPRET_CAST_EXPR:
15811 case CONST_CAST_EXPR:
15812 case STATIC_CAST_EXPR:
15813 case DYNAMIC_CAST_EXPR:
15814 case IMPLICIT_CONV_EXPR:
15815 case CONVERT_EXPR:
15816 case NOP_EXPR:
15817 {
15818 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15819 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15820 return build1 (code, type, op0);
15821 }
15822
15823 case SIZEOF_EXPR:
15824 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15825 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15826 {
15827 tree expanded, op = TREE_OPERAND (t, 0);
15828 int len = 0;
15829
15830 if (SIZEOF_EXPR_TYPE_P (t))
15831 op = TREE_TYPE (op);
15832
15833 ++cp_unevaluated_operand;
15834 ++c_inhibit_evaluation_warnings;
15835 /* We only want to compute the number of arguments. */
15836 if (PACK_EXPANSION_P (op))
15837 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15838 else
15839 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15840 args, complain, in_decl);
15841 --cp_unevaluated_operand;
15842 --c_inhibit_evaluation_warnings;
15843
15844 if (TREE_CODE (expanded) == TREE_VEC)
15845 {
15846 len = TREE_VEC_LENGTH (expanded);
15847 /* Set TREE_USED for the benefit of -Wunused. */
15848 for (int i = 0; i < len; i++)
15849 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15850 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15851 }
15852
15853 if (expanded == error_mark_node)
15854 return error_mark_node;
15855 else if (PACK_EXPANSION_P (expanded)
15856 || (TREE_CODE (expanded) == TREE_VEC
15857 && pack_expansion_args_count (expanded)))
15858
15859 {
15860 if (PACK_EXPANSION_P (expanded))
15861 /* OK. */;
15862 else if (TREE_VEC_LENGTH (expanded) == 1)
15863 expanded = TREE_VEC_ELT (expanded, 0);
15864 else
15865 expanded = make_argument_pack (expanded);
15866
15867 if (TYPE_P (expanded))
15868 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15869 false,
15870 complain & tf_error);
15871 else
15872 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15873 complain & tf_error);
15874 }
15875 else
15876 return build_int_cst (size_type_node, len);
15877 }
15878 if (SIZEOF_EXPR_TYPE_P (t))
15879 {
15880 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15881 args, complain, in_decl);
15882 r = build1 (NOP_EXPR, r, error_mark_node);
15883 r = build1 (SIZEOF_EXPR,
15884 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15885 SIZEOF_EXPR_TYPE_P (r) = 1;
15886 return r;
15887 }
15888 /* Fall through */
15889
15890 case INDIRECT_REF:
15891 case NEGATE_EXPR:
15892 case TRUTH_NOT_EXPR:
15893 case BIT_NOT_EXPR:
15894 case ADDR_EXPR:
15895 case UNARY_PLUS_EXPR: /* Unary + */
15896 case ALIGNOF_EXPR:
15897 case AT_ENCODE_EXPR:
15898 case ARROW_EXPR:
15899 case THROW_EXPR:
15900 case TYPEID_EXPR:
15901 case REALPART_EXPR:
15902 case IMAGPART_EXPR:
15903 case PAREN_EXPR:
15904 {
15905 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15906 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15907 r = build1 (code, type, op0);
15908 if (code == ALIGNOF_EXPR)
15909 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15910 return r;
15911 }
15912
15913 case COMPONENT_REF:
15914 {
15915 tree object;
15916 tree name;
15917
15918 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15919 name = TREE_OPERAND (t, 1);
15920 if (TREE_CODE (name) == BIT_NOT_EXPR)
15921 {
15922 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15923 complain, in_decl);
15924 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15925 }
15926 else if (TREE_CODE (name) == SCOPE_REF
15927 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15928 {
15929 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15930 complain, in_decl);
15931 name = TREE_OPERAND (name, 1);
15932 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15933 complain, in_decl);
15934 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15935 name = build_qualified_name (/*type=*/NULL_TREE,
15936 base, name,
15937 /*template_p=*/false);
15938 }
15939 else if (BASELINK_P (name))
15940 name = tsubst_baselink (name,
15941 non_reference (TREE_TYPE (object)),
15942 args, complain,
15943 in_decl);
15944 else
15945 name = tsubst_copy (name, args, complain, in_decl);
15946 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15947 }
15948
15949 case PLUS_EXPR:
15950 case MINUS_EXPR:
15951 case MULT_EXPR:
15952 case TRUNC_DIV_EXPR:
15953 case CEIL_DIV_EXPR:
15954 case FLOOR_DIV_EXPR:
15955 case ROUND_DIV_EXPR:
15956 case EXACT_DIV_EXPR:
15957 case BIT_AND_EXPR:
15958 case BIT_IOR_EXPR:
15959 case BIT_XOR_EXPR:
15960 case TRUNC_MOD_EXPR:
15961 case FLOOR_MOD_EXPR:
15962 case TRUTH_ANDIF_EXPR:
15963 case TRUTH_ORIF_EXPR:
15964 case TRUTH_AND_EXPR:
15965 case TRUTH_OR_EXPR:
15966 case RSHIFT_EXPR:
15967 case LSHIFT_EXPR:
15968 case RROTATE_EXPR:
15969 case LROTATE_EXPR:
15970 case EQ_EXPR:
15971 case NE_EXPR:
15972 case MAX_EXPR:
15973 case MIN_EXPR:
15974 case LE_EXPR:
15975 case GE_EXPR:
15976 case LT_EXPR:
15977 case GT_EXPR:
15978 case COMPOUND_EXPR:
15979 case DOTSTAR_EXPR:
15980 case MEMBER_REF:
15981 case PREDECREMENT_EXPR:
15982 case PREINCREMENT_EXPR:
15983 case POSTDECREMENT_EXPR:
15984 case POSTINCREMENT_EXPR:
15985 {
15986 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15987 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15988 return build_nt (code, op0, op1);
15989 }
15990
15991 case SCOPE_REF:
15992 {
15993 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15994 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15995 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15996 QUALIFIED_NAME_IS_TEMPLATE (t));
15997 }
15998
15999 case ARRAY_REF:
16000 {
16001 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16002 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16003 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16004 }
16005
16006 case CALL_EXPR:
16007 {
16008 int n = VL_EXP_OPERAND_LENGTH (t);
16009 tree result = build_vl_exp (CALL_EXPR, n);
16010 int i;
16011 for (i = 0; i < n; i++)
16012 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16013 complain, in_decl);
16014 return result;
16015 }
16016
16017 case COND_EXPR:
16018 case MODOP_EXPR:
16019 case PSEUDO_DTOR_EXPR:
16020 case VEC_PERM_EXPR:
16021 {
16022 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16023 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16024 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16025 r = build_nt (code, op0, op1, op2);
16026 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16027 return r;
16028 }
16029
16030 case NEW_EXPR:
16031 {
16032 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16033 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16034 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16035 r = build_nt (code, op0, op1, op2);
16036 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16037 return r;
16038 }
16039
16040 case DELETE_EXPR:
16041 {
16042 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16043 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16044 r = build_nt (code, op0, op1);
16045 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16046 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16047 return r;
16048 }
16049
16050 case TEMPLATE_ID_EXPR:
16051 {
16052 /* Substituted template arguments */
16053 tree fn = TREE_OPERAND (t, 0);
16054 tree targs = TREE_OPERAND (t, 1);
16055
16056 fn = tsubst_copy (fn, args, complain, in_decl);
16057 if (targs)
16058 targs = tsubst_template_args (targs, args, complain, in_decl);
16059
16060 return lookup_template_function (fn, targs);
16061 }
16062
16063 case TREE_LIST:
16064 {
16065 tree purpose, value, chain;
16066
16067 if (t == void_list_node)
16068 return t;
16069
16070 purpose = TREE_PURPOSE (t);
16071 if (purpose)
16072 purpose = tsubst_copy (purpose, args, complain, in_decl);
16073 value = TREE_VALUE (t);
16074 if (value)
16075 value = tsubst_copy (value, args, complain, in_decl);
16076 chain = TREE_CHAIN (t);
16077 if (chain && chain != void_type_node)
16078 chain = tsubst_copy (chain, args, complain, in_decl);
16079 if (purpose == TREE_PURPOSE (t)
16080 && value == TREE_VALUE (t)
16081 && chain == TREE_CHAIN (t))
16082 return t;
16083 return tree_cons (purpose, value, chain);
16084 }
16085
16086 case RECORD_TYPE:
16087 case UNION_TYPE:
16088 case ENUMERAL_TYPE:
16089 case INTEGER_TYPE:
16090 case TEMPLATE_TYPE_PARM:
16091 case TEMPLATE_TEMPLATE_PARM:
16092 case BOUND_TEMPLATE_TEMPLATE_PARM:
16093 case TEMPLATE_PARM_INDEX:
16094 case POINTER_TYPE:
16095 case REFERENCE_TYPE:
16096 case OFFSET_TYPE:
16097 case FUNCTION_TYPE:
16098 case METHOD_TYPE:
16099 case ARRAY_TYPE:
16100 case TYPENAME_TYPE:
16101 case UNBOUND_CLASS_TEMPLATE:
16102 case TYPEOF_TYPE:
16103 case DECLTYPE_TYPE:
16104 case TYPE_DECL:
16105 return tsubst (t, args, complain, in_decl);
16106
16107 case USING_DECL:
16108 t = DECL_NAME (t);
16109 /* Fall through. */
16110 case IDENTIFIER_NODE:
16111 if (IDENTIFIER_CONV_OP_P (t))
16112 {
16113 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16114 return make_conv_op_name (new_type);
16115 }
16116 else
16117 return t;
16118
16119 case CONSTRUCTOR:
16120 /* This is handled by tsubst_copy_and_build. */
16121 gcc_unreachable ();
16122
16123 case VA_ARG_EXPR:
16124 {
16125 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16126 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16127 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16128 }
16129
16130 case CLEANUP_POINT_EXPR:
16131 /* We shouldn't have built any of these during initial template
16132 generation. Instead, they should be built during instantiation
16133 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16134 gcc_unreachable ();
16135
16136 case OFFSET_REF:
16137 {
16138 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16139 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16140 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16141 r = build2 (code, type, op0, op1);
16142 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16143 if (!mark_used (TREE_OPERAND (r, 1), complain)
16144 && !(complain & tf_error))
16145 return error_mark_node;
16146 return r;
16147 }
16148
16149 case EXPR_PACK_EXPANSION:
16150 error ("invalid use of pack expansion expression");
16151 return error_mark_node;
16152
16153 case NONTYPE_ARGUMENT_PACK:
16154 error ("use %<...%> to expand argument pack");
16155 return error_mark_node;
16156
16157 case VOID_CST:
16158 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16159 return t;
16160
16161 case INTEGER_CST:
16162 case REAL_CST:
16163 case STRING_CST:
16164 case COMPLEX_CST:
16165 {
16166 /* Instantiate any typedefs in the type. */
16167 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16168 r = fold_convert (type, t);
16169 gcc_assert (TREE_CODE (r) == code);
16170 return r;
16171 }
16172
16173 case PTRMEM_CST:
16174 /* These can sometimes show up in a partial instantiation, but never
16175 involve template parms. */
16176 gcc_assert (!uses_template_parms (t));
16177 return t;
16178
16179 case UNARY_LEFT_FOLD_EXPR:
16180 return tsubst_unary_left_fold (t, args, complain, in_decl);
16181 case UNARY_RIGHT_FOLD_EXPR:
16182 return tsubst_unary_right_fold (t, args, complain, in_decl);
16183 case BINARY_LEFT_FOLD_EXPR:
16184 return tsubst_binary_left_fold (t, args, complain, in_decl);
16185 case BINARY_RIGHT_FOLD_EXPR:
16186 return tsubst_binary_right_fold (t, args, complain, in_decl);
16187 case PREDICT_EXPR:
16188 return t;
16189
16190 case DEBUG_BEGIN_STMT:
16191 /* ??? There's no point in copying it for now, but maybe some
16192 day it will contain more information, such as a pointer back
16193 to the containing function, inlined copy or so. */
16194 return t;
16195
16196 default:
16197 /* We shouldn't get here, but keep going if !flag_checking. */
16198 if (flag_checking)
16199 gcc_unreachable ();
16200 return t;
16201 }
16202 }
16203
16204 /* Helper function for tsubst_omp_clauses, used for instantiation of
16205 OMP_CLAUSE_DECL of clauses. */
16206
16207 static tree
16208 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16209 tree in_decl, tree *iterator_cache)
16210 {
16211 if (decl == NULL_TREE)
16212 return NULL_TREE;
16213
16214 /* Handle OpenMP iterators. */
16215 if (TREE_CODE (decl) == TREE_LIST
16216 && TREE_PURPOSE (decl)
16217 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16218 {
16219 tree ret;
16220 if (iterator_cache[0] == TREE_PURPOSE (decl))
16221 ret = iterator_cache[1];
16222 else
16223 {
16224 tree *tp = &ret;
16225 begin_scope (sk_omp, NULL);
16226 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16227 {
16228 *tp = copy_node (it);
16229 TREE_VEC_ELT (*tp, 0)
16230 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16231 TREE_VEC_ELT (*tp, 1)
16232 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16233 /*integral_constant_expression_p=*/false);
16234 TREE_VEC_ELT (*tp, 2)
16235 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16236 /*integral_constant_expression_p=*/false);
16237 TREE_VEC_ELT (*tp, 3)
16238 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16239 /*integral_constant_expression_p=*/false);
16240 TREE_CHAIN (*tp) = NULL_TREE;
16241 tp = &TREE_CHAIN (*tp);
16242 }
16243 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16244 iterator_cache[0] = TREE_PURPOSE (decl);
16245 iterator_cache[1] = ret;
16246 }
16247 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16248 args, complain,
16249 in_decl, NULL));
16250 }
16251
16252 /* Handle an OpenMP array section represented as a TREE_LIST (or
16253 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16254 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16255 TREE_LIST. We can handle it exactly the same as an array section
16256 (purpose, value, and a chain), even though the nomenclature
16257 (low_bound, length, etc) is different. */
16258 if (TREE_CODE (decl) == TREE_LIST)
16259 {
16260 tree low_bound
16261 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16262 /*integral_constant_expression_p=*/false);
16263 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16264 /*integral_constant_expression_p=*/false);
16265 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16266 in_decl, NULL);
16267 if (TREE_PURPOSE (decl) == low_bound
16268 && TREE_VALUE (decl) == length
16269 && TREE_CHAIN (decl) == chain)
16270 return decl;
16271 tree ret = tree_cons (low_bound, length, chain);
16272 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16273 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16274 return ret;
16275 }
16276 tree ret = tsubst_expr (decl, args, complain, in_decl,
16277 /*integral_constant_expression_p=*/false);
16278 /* Undo convert_from_reference tsubst_expr could have called. */
16279 if (decl
16280 && REFERENCE_REF_P (ret)
16281 && !REFERENCE_REF_P (decl))
16282 ret = TREE_OPERAND (ret, 0);
16283 return ret;
16284 }
16285
16286 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16287
16288 static tree
16289 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16290 tree args, tsubst_flags_t complain, tree in_decl)
16291 {
16292 tree new_clauses = NULL_TREE, nc, oc;
16293 tree linear_no_step = NULL_TREE;
16294 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16295
16296 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16297 {
16298 nc = copy_node (oc);
16299 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16300 new_clauses = nc;
16301
16302 switch (OMP_CLAUSE_CODE (nc))
16303 {
16304 case OMP_CLAUSE_LASTPRIVATE:
16305 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16306 {
16307 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16308 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16309 in_decl, /*integral_constant_expression_p=*/false);
16310 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16311 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16312 }
16313 /* FALLTHRU */
16314 case OMP_CLAUSE_PRIVATE:
16315 case OMP_CLAUSE_SHARED:
16316 case OMP_CLAUSE_FIRSTPRIVATE:
16317 case OMP_CLAUSE_COPYIN:
16318 case OMP_CLAUSE_COPYPRIVATE:
16319 case OMP_CLAUSE_UNIFORM:
16320 case OMP_CLAUSE_DEPEND:
16321 case OMP_CLAUSE_FROM:
16322 case OMP_CLAUSE_TO:
16323 case OMP_CLAUSE_MAP:
16324 case OMP_CLAUSE_NONTEMPORAL:
16325 case OMP_CLAUSE_USE_DEVICE_PTR:
16326 case OMP_CLAUSE_IS_DEVICE_PTR:
16327 case OMP_CLAUSE_INCLUSIVE:
16328 case OMP_CLAUSE_EXCLUSIVE:
16329 OMP_CLAUSE_DECL (nc)
16330 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16331 in_decl, iterator_cache);
16332 break;
16333 case OMP_CLAUSE_TILE:
16334 case OMP_CLAUSE_IF:
16335 case OMP_CLAUSE_NUM_THREADS:
16336 case OMP_CLAUSE_SCHEDULE:
16337 case OMP_CLAUSE_COLLAPSE:
16338 case OMP_CLAUSE_FINAL:
16339 case OMP_CLAUSE_DEVICE:
16340 case OMP_CLAUSE_DIST_SCHEDULE:
16341 case OMP_CLAUSE_NUM_TEAMS:
16342 case OMP_CLAUSE_THREAD_LIMIT:
16343 case OMP_CLAUSE_SAFELEN:
16344 case OMP_CLAUSE_SIMDLEN:
16345 case OMP_CLAUSE_NUM_TASKS:
16346 case OMP_CLAUSE_GRAINSIZE:
16347 case OMP_CLAUSE_PRIORITY:
16348 case OMP_CLAUSE_ORDERED:
16349 case OMP_CLAUSE_HINT:
16350 case OMP_CLAUSE_NUM_GANGS:
16351 case OMP_CLAUSE_NUM_WORKERS:
16352 case OMP_CLAUSE_VECTOR_LENGTH:
16353 case OMP_CLAUSE_WORKER:
16354 case OMP_CLAUSE_VECTOR:
16355 case OMP_CLAUSE_ASYNC:
16356 case OMP_CLAUSE_WAIT:
16357 OMP_CLAUSE_OPERAND (nc, 0)
16358 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16359 in_decl, /*integral_constant_expression_p=*/false);
16360 break;
16361 case OMP_CLAUSE_REDUCTION:
16362 case OMP_CLAUSE_IN_REDUCTION:
16363 case OMP_CLAUSE_TASK_REDUCTION:
16364 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16365 {
16366 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16367 if (TREE_CODE (placeholder) == SCOPE_REF)
16368 {
16369 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16370 complain, in_decl);
16371 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16372 = build_qualified_name (NULL_TREE, scope,
16373 TREE_OPERAND (placeholder, 1),
16374 false);
16375 }
16376 else
16377 gcc_assert (identifier_p (placeholder));
16378 }
16379 OMP_CLAUSE_DECL (nc)
16380 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16381 in_decl, NULL);
16382 break;
16383 case OMP_CLAUSE_GANG:
16384 case OMP_CLAUSE_ALIGNED:
16385 OMP_CLAUSE_DECL (nc)
16386 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16387 in_decl, NULL);
16388 OMP_CLAUSE_OPERAND (nc, 1)
16389 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16390 in_decl, /*integral_constant_expression_p=*/false);
16391 break;
16392 case OMP_CLAUSE_LINEAR:
16393 OMP_CLAUSE_DECL (nc)
16394 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16395 in_decl, NULL);
16396 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16397 {
16398 gcc_assert (!linear_no_step);
16399 linear_no_step = nc;
16400 }
16401 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16402 OMP_CLAUSE_LINEAR_STEP (nc)
16403 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16404 complain, in_decl, NULL);
16405 else
16406 OMP_CLAUSE_LINEAR_STEP (nc)
16407 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16408 in_decl,
16409 /*integral_constant_expression_p=*/false);
16410 break;
16411 case OMP_CLAUSE_NOWAIT:
16412 case OMP_CLAUSE_DEFAULT:
16413 case OMP_CLAUSE_UNTIED:
16414 case OMP_CLAUSE_MERGEABLE:
16415 case OMP_CLAUSE_INBRANCH:
16416 case OMP_CLAUSE_NOTINBRANCH:
16417 case OMP_CLAUSE_PROC_BIND:
16418 case OMP_CLAUSE_FOR:
16419 case OMP_CLAUSE_PARALLEL:
16420 case OMP_CLAUSE_SECTIONS:
16421 case OMP_CLAUSE_TASKGROUP:
16422 case OMP_CLAUSE_NOGROUP:
16423 case OMP_CLAUSE_THREADS:
16424 case OMP_CLAUSE_SIMD:
16425 case OMP_CLAUSE_DEFAULTMAP:
16426 case OMP_CLAUSE_ORDER:
16427 case OMP_CLAUSE_INDEPENDENT:
16428 case OMP_CLAUSE_AUTO:
16429 case OMP_CLAUSE_SEQ:
16430 case OMP_CLAUSE_IF_PRESENT:
16431 case OMP_CLAUSE_FINALIZE:
16432 break;
16433 default:
16434 gcc_unreachable ();
16435 }
16436 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16437 switch (OMP_CLAUSE_CODE (nc))
16438 {
16439 case OMP_CLAUSE_SHARED:
16440 case OMP_CLAUSE_PRIVATE:
16441 case OMP_CLAUSE_FIRSTPRIVATE:
16442 case OMP_CLAUSE_LASTPRIVATE:
16443 case OMP_CLAUSE_COPYPRIVATE:
16444 case OMP_CLAUSE_LINEAR:
16445 case OMP_CLAUSE_REDUCTION:
16446 case OMP_CLAUSE_IN_REDUCTION:
16447 case OMP_CLAUSE_TASK_REDUCTION:
16448 case OMP_CLAUSE_USE_DEVICE_PTR:
16449 case OMP_CLAUSE_IS_DEVICE_PTR:
16450 case OMP_CLAUSE_INCLUSIVE:
16451 case OMP_CLAUSE_EXCLUSIVE:
16452 /* tsubst_expr on SCOPE_REF results in returning
16453 finish_non_static_data_member result. Undo that here. */
16454 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16455 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16456 == IDENTIFIER_NODE))
16457 {
16458 tree t = OMP_CLAUSE_DECL (nc);
16459 tree v = t;
16460 while (v)
16461 switch (TREE_CODE (v))
16462 {
16463 case COMPONENT_REF:
16464 case MEM_REF:
16465 case INDIRECT_REF:
16466 CASE_CONVERT:
16467 case POINTER_PLUS_EXPR:
16468 v = TREE_OPERAND (v, 0);
16469 continue;
16470 case PARM_DECL:
16471 if (DECL_CONTEXT (v) == current_function_decl
16472 && DECL_ARTIFICIAL (v)
16473 && DECL_NAME (v) == this_identifier)
16474 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16475 /* FALLTHRU */
16476 default:
16477 v = NULL_TREE;
16478 break;
16479 }
16480 }
16481 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16482 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16483 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16484 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16485 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16486 {
16487 tree decl = OMP_CLAUSE_DECL (nc);
16488 if (VAR_P (decl))
16489 {
16490 retrofit_lang_decl (decl);
16491 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16492 }
16493 }
16494 break;
16495 default:
16496 break;
16497 }
16498 }
16499
16500 new_clauses = nreverse (new_clauses);
16501 if (ort != C_ORT_OMP_DECLARE_SIMD)
16502 {
16503 new_clauses = finish_omp_clauses (new_clauses, ort);
16504 if (linear_no_step)
16505 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16506 if (nc == linear_no_step)
16507 {
16508 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16509 break;
16510 }
16511 }
16512 return new_clauses;
16513 }
16514
16515 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16516
16517 static tree
16518 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16519 tree in_decl)
16520 {
16521 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16522
16523 tree purpose, value, chain;
16524
16525 if (t == NULL)
16526 return t;
16527
16528 if (TREE_CODE (t) != TREE_LIST)
16529 return tsubst_copy_and_build (t, args, complain, in_decl,
16530 /*function_p=*/false,
16531 /*integral_constant_expression_p=*/false);
16532
16533 if (t == void_list_node)
16534 return t;
16535
16536 purpose = TREE_PURPOSE (t);
16537 if (purpose)
16538 purpose = RECUR (purpose);
16539 value = TREE_VALUE (t);
16540 if (value)
16541 {
16542 if (TREE_CODE (value) != LABEL_DECL)
16543 value = RECUR (value);
16544 else
16545 {
16546 value = lookup_label (DECL_NAME (value));
16547 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16548 TREE_USED (value) = 1;
16549 }
16550 }
16551 chain = TREE_CHAIN (t);
16552 if (chain && chain != void_type_node)
16553 chain = RECUR (chain);
16554 return tree_cons (purpose, value, chain);
16555 #undef RECUR
16556 }
16557
16558 /* Used to temporarily communicate the list of #pragma omp parallel
16559 clauses to #pragma omp for instantiation if they are combined
16560 together. */
16561
16562 static tree *omp_parallel_combined_clauses;
16563
16564 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16565 tree *, unsigned int *);
16566
16567 /* Substitute one OMP_FOR iterator. */
16568
16569 static bool
16570 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16571 tree initv, tree condv, tree incrv, tree *clauses,
16572 tree args, tsubst_flags_t complain, tree in_decl,
16573 bool integral_constant_expression_p)
16574 {
16575 #define RECUR(NODE) \
16576 tsubst_expr ((NODE), args, complain, in_decl, \
16577 integral_constant_expression_p)
16578 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16579 bool ret = false;
16580
16581 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16582 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16583
16584 decl = TREE_OPERAND (init, 0);
16585 init = TREE_OPERAND (init, 1);
16586 tree decl_expr = NULL_TREE;
16587 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16588 if (range_for)
16589 {
16590 bool decomp = false;
16591 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16592 {
16593 tree v = DECL_VALUE_EXPR (decl);
16594 if (TREE_CODE (v) == ARRAY_REF
16595 && VAR_P (TREE_OPERAND (v, 0))
16596 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16597 {
16598 tree decomp_first = NULL_TREE;
16599 unsigned decomp_cnt = 0;
16600 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16601 maybe_push_decl (d);
16602 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16603 in_decl, &decomp_first, &decomp_cnt);
16604 decomp = true;
16605 if (d == error_mark_node)
16606 decl = error_mark_node;
16607 else
16608 for (unsigned int i = 0; i < decomp_cnt; i++)
16609 {
16610 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16611 {
16612 tree v = build_nt (ARRAY_REF, d,
16613 size_int (decomp_cnt - i - 1),
16614 NULL_TREE, NULL_TREE);
16615 SET_DECL_VALUE_EXPR (decomp_first, v);
16616 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16617 }
16618 fit_decomposition_lang_decl (decomp_first, d);
16619 decomp_first = DECL_CHAIN (decomp_first);
16620 }
16621 }
16622 }
16623 decl = tsubst_decl (decl, args, complain);
16624 if (!decomp)
16625 maybe_push_decl (decl);
16626 }
16627 else if (init && TREE_CODE (init) == DECL_EXPR)
16628 {
16629 /* We need to jump through some hoops to handle declarations in the
16630 init-statement, since we might need to handle auto deduction,
16631 but we need to keep control of initialization. */
16632 decl_expr = init;
16633 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16634 decl = tsubst_decl (decl, args, complain);
16635 }
16636 else
16637 {
16638 if (TREE_CODE (decl) == SCOPE_REF)
16639 {
16640 decl = RECUR (decl);
16641 if (TREE_CODE (decl) == COMPONENT_REF)
16642 {
16643 tree v = decl;
16644 while (v)
16645 switch (TREE_CODE (v))
16646 {
16647 case COMPONENT_REF:
16648 case MEM_REF:
16649 case INDIRECT_REF:
16650 CASE_CONVERT:
16651 case POINTER_PLUS_EXPR:
16652 v = TREE_OPERAND (v, 0);
16653 continue;
16654 case PARM_DECL:
16655 if (DECL_CONTEXT (v) == current_function_decl
16656 && DECL_ARTIFICIAL (v)
16657 && DECL_NAME (v) == this_identifier)
16658 {
16659 decl = TREE_OPERAND (decl, 1);
16660 decl = omp_privatize_field (decl, false);
16661 }
16662 /* FALLTHRU */
16663 default:
16664 v = NULL_TREE;
16665 break;
16666 }
16667 }
16668 }
16669 else
16670 decl = RECUR (decl);
16671 }
16672 init = RECUR (init);
16673
16674 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16675 {
16676 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16677 if (TREE_CODE (o) == TREE_LIST)
16678 TREE_VEC_ELT (orig_declv, i)
16679 = tree_cons (RECUR (TREE_PURPOSE (o)),
16680 RECUR (TREE_VALUE (o)),
16681 NULL_TREE);
16682 else
16683 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16684 }
16685
16686 if (range_for)
16687 {
16688 tree this_pre_body = NULL_TREE;
16689 tree orig_init = NULL_TREE;
16690 tree orig_decl = NULL_TREE;
16691 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16692 orig_init, cond, incr);
16693 if (orig_decl)
16694 {
16695 if (orig_declv == NULL_TREE)
16696 orig_declv = copy_node (declv);
16697 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16698 ret = true;
16699 }
16700 else if (orig_declv)
16701 TREE_VEC_ELT (orig_declv, i) = decl;
16702 }
16703
16704 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16705 if (!range_for && auto_node && init)
16706 TREE_TYPE (decl)
16707 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16708
16709 gcc_assert (!type_dependent_expression_p (decl));
16710
16711 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16712 {
16713 if (decl_expr)
16714 {
16715 /* Declare the variable, but don't let that initialize it. */
16716 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16717 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16718 RECUR (decl_expr);
16719 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16720 }
16721
16722 if (!range_for)
16723 {
16724 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16725 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16726 if (TREE_CODE (incr) == MODIFY_EXPR)
16727 {
16728 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16729 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16730 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16731 NOP_EXPR, rhs, complain);
16732 }
16733 else
16734 incr = RECUR (incr);
16735 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16736 TREE_VEC_ELT (orig_declv, i) = decl;
16737 }
16738 TREE_VEC_ELT (declv, i) = decl;
16739 TREE_VEC_ELT (initv, i) = init;
16740 TREE_VEC_ELT (condv, i) = cond;
16741 TREE_VEC_ELT (incrv, i) = incr;
16742 return ret;
16743 }
16744
16745 if (decl_expr)
16746 {
16747 /* Declare and initialize the variable. */
16748 RECUR (decl_expr);
16749 init = NULL_TREE;
16750 }
16751 else if (init)
16752 {
16753 tree *pc;
16754 int j;
16755 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16756 {
16757 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16758 {
16759 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16760 && OMP_CLAUSE_DECL (*pc) == decl)
16761 break;
16762 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16763 && OMP_CLAUSE_DECL (*pc) == decl)
16764 {
16765 if (j)
16766 break;
16767 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16768 tree c = *pc;
16769 *pc = OMP_CLAUSE_CHAIN (c);
16770 OMP_CLAUSE_CHAIN (c) = *clauses;
16771 *clauses = c;
16772 }
16773 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16774 && OMP_CLAUSE_DECL (*pc) == decl)
16775 {
16776 error ("iteration variable %qD should not be firstprivate",
16777 decl);
16778 *pc = OMP_CLAUSE_CHAIN (*pc);
16779 }
16780 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16781 && OMP_CLAUSE_DECL (*pc) == decl)
16782 {
16783 error ("iteration variable %qD should not be reduction",
16784 decl);
16785 *pc = OMP_CLAUSE_CHAIN (*pc);
16786 }
16787 else
16788 pc = &OMP_CLAUSE_CHAIN (*pc);
16789 }
16790 if (*pc)
16791 break;
16792 }
16793 if (*pc == NULL_TREE)
16794 {
16795 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16796 OMP_CLAUSE_DECL (c) = decl;
16797 c = finish_omp_clauses (c, C_ORT_OMP);
16798 if (c)
16799 {
16800 OMP_CLAUSE_CHAIN (c) = *clauses;
16801 *clauses = c;
16802 }
16803 }
16804 }
16805 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16806 if (COMPARISON_CLASS_P (cond))
16807 {
16808 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16809 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16810 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16811 }
16812 else
16813 cond = RECUR (cond);
16814 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16815 switch (TREE_CODE (incr))
16816 {
16817 case PREINCREMENT_EXPR:
16818 case PREDECREMENT_EXPR:
16819 case POSTINCREMENT_EXPR:
16820 case POSTDECREMENT_EXPR:
16821 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16822 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16823 break;
16824 case MODIFY_EXPR:
16825 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16826 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16827 {
16828 tree rhs = TREE_OPERAND (incr, 1);
16829 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16830 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16831 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16832 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16833 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16834 rhs0, rhs1));
16835 }
16836 else
16837 incr = RECUR (incr);
16838 break;
16839 case MODOP_EXPR:
16840 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16841 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16842 {
16843 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16844 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16845 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16846 TREE_TYPE (decl), lhs,
16847 RECUR (TREE_OPERAND (incr, 2))));
16848 }
16849 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16850 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16851 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16852 {
16853 tree rhs = TREE_OPERAND (incr, 2);
16854 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16855 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16856 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16857 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16858 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16859 rhs0, rhs1));
16860 }
16861 else
16862 incr = RECUR (incr);
16863 break;
16864 default:
16865 incr = RECUR (incr);
16866 break;
16867 }
16868
16869 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16870 TREE_VEC_ELT (orig_declv, i) = decl;
16871 TREE_VEC_ELT (declv, i) = decl;
16872 TREE_VEC_ELT (initv, i) = init;
16873 TREE_VEC_ELT (condv, i) = cond;
16874 TREE_VEC_ELT (incrv, i) = incr;
16875 return false;
16876 #undef RECUR
16877 }
16878
16879 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16880 of OMP_TARGET's body. */
16881
16882 static tree
16883 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16884 {
16885 *walk_subtrees = 0;
16886 switch (TREE_CODE (*tp))
16887 {
16888 case OMP_TEAMS:
16889 return *tp;
16890 case BIND_EXPR:
16891 case STATEMENT_LIST:
16892 *walk_subtrees = 1;
16893 break;
16894 default:
16895 break;
16896 }
16897 return NULL_TREE;
16898 }
16899
16900 /* Helper function for tsubst_expr. For decomposition declaration
16901 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16902 also the corresponding decls representing the identifiers
16903 of the decomposition declaration. Return DECL if successful
16904 or error_mark_node otherwise, set *FIRST to the first decl
16905 in the list chained through DECL_CHAIN and *CNT to the number
16906 of such decls. */
16907
16908 static tree
16909 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16910 tsubst_flags_t complain, tree in_decl, tree *first,
16911 unsigned int *cnt)
16912 {
16913 tree decl2, decl3, prev = decl;
16914 *cnt = 0;
16915 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16916 for (decl2 = DECL_CHAIN (pattern_decl);
16917 decl2
16918 && VAR_P (decl2)
16919 && DECL_DECOMPOSITION_P (decl2)
16920 && DECL_NAME (decl2);
16921 decl2 = DECL_CHAIN (decl2))
16922 {
16923 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16924 {
16925 gcc_assert (errorcount);
16926 return error_mark_node;
16927 }
16928 (*cnt)++;
16929 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16930 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16931 tree v = DECL_VALUE_EXPR (decl2);
16932 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16933 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16934 decl3 = tsubst (decl2, args, complain, in_decl);
16935 SET_DECL_VALUE_EXPR (decl2, v);
16936 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16937 if (VAR_P (decl3))
16938 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16939 else
16940 {
16941 gcc_assert (errorcount);
16942 decl = error_mark_node;
16943 continue;
16944 }
16945 maybe_push_decl (decl3);
16946 if (error_operand_p (decl3))
16947 decl = error_mark_node;
16948 else if (decl != error_mark_node
16949 && DECL_CHAIN (decl3) != prev
16950 && decl != prev)
16951 {
16952 gcc_assert (errorcount);
16953 decl = error_mark_node;
16954 }
16955 else
16956 prev = decl3;
16957 }
16958 *first = prev;
16959 return decl;
16960 }
16961
16962 /* Return the proper local_specialization for init-capture pack DECL. */
16963
16964 static tree
16965 lookup_init_capture_pack (tree decl)
16966 {
16967 /* We handle normal pack captures by forwarding to the specialization of the
16968 captured parameter. We can't do that for pack init-captures; we need them
16969 to have their own local_specialization. We created the individual
16970 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16971 when we process the DECL_EXPR for the pack init-capture in the template.
16972 So, how do we find them? We don't know the capture proxy pack when
16973 building the individual resulting proxies, and we don't know the
16974 individual proxies when instantiating the pack. What we have in common is
16975 the FIELD_DECL.
16976
16977 So...when we instantiate the FIELD_DECL, we stick the result in
16978 local_specializations. Then at the DECL_EXPR we look up that result, see
16979 how many elements it has, synthesize the names, and look them up. */
16980
16981 tree cname = DECL_NAME (decl);
16982 tree val = DECL_VALUE_EXPR (decl);
16983 tree field = TREE_OPERAND (val, 1);
16984 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16985 tree fpack = retrieve_local_specialization (field);
16986 if (fpack == error_mark_node)
16987 return error_mark_node;
16988
16989 int len = 1;
16990 tree vec = NULL_TREE;
16991 tree r = NULL_TREE;
16992 if (TREE_CODE (fpack) == TREE_VEC)
16993 {
16994 len = TREE_VEC_LENGTH (fpack);
16995 vec = make_tree_vec (len);
16996 r = make_node (NONTYPE_ARGUMENT_PACK);
16997 SET_ARGUMENT_PACK_ARGS (r, vec);
16998 }
16999 for (int i = 0; i < len; ++i)
17000 {
17001 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17002 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17003 if (vec)
17004 TREE_VEC_ELT (vec, i) = elt;
17005 else
17006 r = elt;
17007 }
17008 return r;
17009 }
17010
17011 /* Like tsubst_copy for expressions, etc. but also does semantic
17012 processing. */
17013
17014 tree
17015 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17016 bool integral_constant_expression_p)
17017 {
17018 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17019 #define RECUR(NODE) \
17020 tsubst_expr ((NODE), args, complain, in_decl, \
17021 integral_constant_expression_p)
17022
17023 tree stmt, tmp;
17024 tree r;
17025 location_t loc;
17026
17027 if (t == NULL_TREE || t == error_mark_node)
17028 return t;
17029
17030 loc = input_location;
17031 if (location_t eloc = cp_expr_location (t))
17032 input_location = eloc;
17033 if (STATEMENT_CODE_P (TREE_CODE (t)))
17034 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17035
17036 switch (TREE_CODE (t))
17037 {
17038 case STATEMENT_LIST:
17039 {
17040 tree_stmt_iterator i;
17041 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17042 RECUR (tsi_stmt (i));
17043 break;
17044 }
17045
17046 case CTOR_INITIALIZER:
17047 finish_mem_initializers (tsubst_initializer_list
17048 (TREE_OPERAND (t, 0), args));
17049 break;
17050
17051 case RETURN_EXPR:
17052 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17053 break;
17054
17055 case EXPR_STMT:
17056 tmp = RECUR (EXPR_STMT_EXPR (t));
17057 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17058 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17059 else
17060 finish_expr_stmt (tmp);
17061 break;
17062
17063 case USING_STMT:
17064 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17065 break;
17066
17067 case DECL_EXPR:
17068 {
17069 tree decl, pattern_decl;
17070 tree init;
17071
17072 pattern_decl = decl = DECL_EXPR_DECL (t);
17073 if (TREE_CODE (decl) == LABEL_DECL)
17074 finish_label_decl (DECL_NAME (decl));
17075 else if (TREE_CODE (decl) == USING_DECL)
17076 {
17077 tree scope = USING_DECL_SCOPE (decl);
17078 tree name = DECL_NAME (decl);
17079
17080 scope = tsubst (scope, args, complain, in_decl);
17081 finish_nonmember_using_decl (scope, name);
17082 }
17083 else if (is_capture_proxy (decl)
17084 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17085 {
17086 /* We're in tsubst_lambda_expr, we've already inserted a new
17087 capture proxy, so look it up and register it. */
17088 tree inst;
17089 if (!DECL_PACK_P (decl))
17090 {
17091 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17092 /*nonclass*/1, /*block_p=*/true,
17093 /*ns_only*/0, LOOKUP_HIDDEN);
17094 gcc_assert (inst != decl && is_capture_proxy (inst));
17095 }
17096 else if (is_normal_capture_proxy (decl))
17097 {
17098 inst = (retrieve_local_specialization
17099 (DECL_CAPTURED_VARIABLE (decl)));
17100 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17101 }
17102 else
17103 inst = lookup_init_capture_pack (decl);
17104
17105 register_local_specialization (inst, decl);
17106 break;
17107 }
17108 else if (DECL_PRETTY_FUNCTION_P (decl))
17109 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17110 DECL_NAME (decl),
17111 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17112 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17113 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17114 /* Don't copy the old closure; we'll create a new one in
17115 tsubst_lambda_expr. */
17116 break;
17117 else
17118 {
17119 init = DECL_INITIAL (decl);
17120 decl = tsubst (decl, args, complain, in_decl);
17121 if (decl != error_mark_node)
17122 {
17123 /* By marking the declaration as instantiated, we avoid
17124 trying to instantiate it. Since instantiate_decl can't
17125 handle local variables, and since we've already done
17126 all that needs to be done, that's the right thing to
17127 do. */
17128 if (VAR_P (decl))
17129 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17130 if (VAR_P (decl) && !DECL_NAME (decl)
17131 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17132 /* Anonymous aggregates are a special case. */
17133 finish_anon_union (decl);
17134 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17135 {
17136 DECL_CONTEXT (decl) = current_function_decl;
17137 if (DECL_NAME (decl) == this_identifier)
17138 {
17139 tree lam = DECL_CONTEXT (current_function_decl);
17140 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17141 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17142 }
17143 insert_capture_proxy (decl);
17144 }
17145 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17146 /* We already did a pushtag. */;
17147 else if (TREE_CODE (decl) == FUNCTION_DECL
17148 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17149 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17150 {
17151 DECL_CONTEXT (decl) = NULL_TREE;
17152 pushdecl (decl);
17153 DECL_CONTEXT (decl) = current_function_decl;
17154 cp_check_omp_declare_reduction (decl);
17155 }
17156 else
17157 {
17158 int const_init = false;
17159 unsigned int cnt = 0;
17160 tree first = NULL_TREE, ndecl = error_mark_node;
17161 maybe_push_decl (decl);
17162
17163 if (VAR_P (decl)
17164 && DECL_DECOMPOSITION_P (decl)
17165 && TREE_TYPE (pattern_decl) != error_mark_node)
17166 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17167 complain, in_decl, &first,
17168 &cnt);
17169
17170 init = tsubst_init (init, decl, args, complain, in_decl);
17171
17172 if (VAR_P (decl))
17173 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17174 (pattern_decl));
17175
17176 if (ndecl != error_mark_node)
17177 cp_maybe_mangle_decomp (ndecl, first, cnt);
17178
17179 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17180
17181 if (ndecl != error_mark_node)
17182 cp_finish_decomp (ndecl, first, cnt);
17183 }
17184 }
17185 }
17186
17187 break;
17188 }
17189
17190 case FOR_STMT:
17191 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17192 RECUR (FOR_INIT_STMT (t));
17193 finish_init_stmt (stmt);
17194 tmp = RECUR (FOR_COND (t));
17195 finish_for_cond (tmp, stmt, false, 0);
17196 tmp = RECUR (FOR_EXPR (t));
17197 finish_for_expr (tmp, stmt);
17198 {
17199 bool prev = note_iteration_stmt_body_start ();
17200 RECUR (FOR_BODY (t));
17201 note_iteration_stmt_body_end (prev);
17202 }
17203 finish_for_stmt (stmt);
17204 break;
17205
17206 case RANGE_FOR_STMT:
17207 {
17208 /* Construct another range_for, if this is not a final
17209 substitution (for inside inside a generic lambda of a
17210 template). Otherwise convert to a regular for. */
17211 tree decl, expr;
17212 stmt = (processing_template_decl
17213 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17214 : begin_for_stmt (NULL_TREE, NULL_TREE));
17215 RECUR (RANGE_FOR_INIT_STMT (t));
17216 decl = RANGE_FOR_DECL (t);
17217 decl = tsubst (decl, args, complain, in_decl);
17218 maybe_push_decl (decl);
17219 expr = RECUR (RANGE_FOR_EXPR (t));
17220
17221 tree decomp_first = NULL_TREE;
17222 unsigned decomp_cnt = 0;
17223 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17224 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17225 complain, in_decl,
17226 &decomp_first, &decomp_cnt);
17227
17228 if (processing_template_decl)
17229 {
17230 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17231 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17232 finish_range_for_decl (stmt, decl, expr);
17233 if (decomp_first && decl != error_mark_node)
17234 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17235 }
17236 else
17237 {
17238 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17239 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17240 stmt = cp_convert_range_for (stmt, decl, expr,
17241 decomp_first, decomp_cnt,
17242 RANGE_FOR_IVDEP (t), unroll);
17243 }
17244
17245 bool prev = note_iteration_stmt_body_start ();
17246 RECUR (RANGE_FOR_BODY (t));
17247 note_iteration_stmt_body_end (prev);
17248 finish_for_stmt (stmt);
17249 }
17250 break;
17251
17252 case WHILE_STMT:
17253 stmt = begin_while_stmt ();
17254 tmp = RECUR (WHILE_COND (t));
17255 finish_while_stmt_cond (tmp, stmt, false, 0);
17256 {
17257 bool prev = note_iteration_stmt_body_start ();
17258 RECUR (WHILE_BODY (t));
17259 note_iteration_stmt_body_end (prev);
17260 }
17261 finish_while_stmt (stmt);
17262 break;
17263
17264 case DO_STMT:
17265 stmt = begin_do_stmt ();
17266 {
17267 bool prev = note_iteration_stmt_body_start ();
17268 RECUR (DO_BODY (t));
17269 note_iteration_stmt_body_end (prev);
17270 }
17271 finish_do_body (stmt);
17272 tmp = RECUR (DO_COND (t));
17273 finish_do_stmt (tmp, stmt, false, 0);
17274 break;
17275
17276 case IF_STMT:
17277 stmt = begin_if_stmt ();
17278 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17279 if (IF_STMT_CONSTEXPR_P (t))
17280 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17281 tmp = RECUR (IF_COND (t));
17282 tmp = finish_if_stmt_cond (tmp, stmt);
17283 if (IF_STMT_CONSTEXPR_P (t)
17284 && instantiation_dependent_expression_p (tmp))
17285 {
17286 /* We're partially instantiating a generic lambda, but the condition
17287 of the constexpr if is still dependent. Don't substitute into the
17288 branches now, just remember the template arguments. */
17289 do_poplevel (IF_SCOPE (stmt));
17290 IF_COND (stmt) = IF_COND (t);
17291 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17292 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17293 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17294 add_stmt (stmt);
17295 break;
17296 }
17297 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17298 /* Don't instantiate the THEN_CLAUSE. */;
17299 else
17300 {
17301 tree folded = fold_non_dependent_expr (tmp, complain);
17302 bool inhibit = integer_zerop (folded);
17303 if (inhibit)
17304 ++c_inhibit_evaluation_warnings;
17305 RECUR (THEN_CLAUSE (t));
17306 if (inhibit)
17307 --c_inhibit_evaluation_warnings;
17308 }
17309 finish_then_clause (stmt);
17310
17311 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17312 /* Don't instantiate the ELSE_CLAUSE. */;
17313 else if (ELSE_CLAUSE (t))
17314 {
17315 tree folded = fold_non_dependent_expr (tmp, complain);
17316 bool inhibit = integer_nonzerop (folded);
17317 begin_else_clause (stmt);
17318 if (inhibit)
17319 ++c_inhibit_evaluation_warnings;
17320 RECUR (ELSE_CLAUSE (t));
17321 if (inhibit)
17322 --c_inhibit_evaluation_warnings;
17323 finish_else_clause (stmt);
17324 }
17325
17326 finish_if_stmt (stmt);
17327 break;
17328
17329 case BIND_EXPR:
17330 if (BIND_EXPR_BODY_BLOCK (t))
17331 stmt = begin_function_body ();
17332 else
17333 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17334 ? BCS_TRY_BLOCK : 0);
17335
17336 RECUR (BIND_EXPR_BODY (t));
17337
17338 if (BIND_EXPR_BODY_BLOCK (t))
17339 finish_function_body (stmt);
17340 else
17341 finish_compound_stmt (stmt);
17342 break;
17343
17344 case BREAK_STMT:
17345 finish_break_stmt ();
17346 break;
17347
17348 case CONTINUE_STMT:
17349 finish_continue_stmt ();
17350 break;
17351
17352 case SWITCH_STMT:
17353 stmt = begin_switch_stmt ();
17354 tmp = RECUR (SWITCH_STMT_COND (t));
17355 finish_switch_cond (tmp, stmt);
17356 RECUR (SWITCH_STMT_BODY (t));
17357 finish_switch_stmt (stmt);
17358 break;
17359
17360 case CASE_LABEL_EXPR:
17361 {
17362 tree decl = CASE_LABEL (t);
17363 tree low = RECUR (CASE_LOW (t));
17364 tree high = RECUR (CASE_HIGH (t));
17365 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17366 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17367 {
17368 tree label = CASE_LABEL (l);
17369 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17370 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17371 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17372 }
17373 }
17374 break;
17375
17376 case LABEL_EXPR:
17377 {
17378 tree decl = LABEL_EXPR_LABEL (t);
17379 tree label;
17380
17381 label = finish_label_stmt (DECL_NAME (decl));
17382 if (TREE_CODE (label) == LABEL_DECL)
17383 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17384 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17385 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17386 }
17387 break;
17388
17389 case GOTO_EXPR:
17390 tmp = GOTO_DESTINATION (t);
17391 if (TREE_CODE (tmp) != LABEL_DECL)
17392 /* Computed goto's must be tsubst'd into. On the other hand,
17393 non-computed gotos must not be; the identifier in question
17394 will have no binding. */
17395 tmp = RECUR (tmp);
17396 else
17397 tmp = DECL_NAME (tmp);
17398 finish_goto_stmt (tmp);
17399 break;
17400
17401 case ASM_EXPR:
17402 {
17403 tree string = RECUR (ASM_STRING (t));
17404 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17405 complain, in_decl);
17406 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17407 complain, in_decl);
17408 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17409 complain, in_decl);
17410 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17411 complain, in_decl);
17412 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17413 clobbers, labels, ASM_INLINE_P (t));
17414 tree asm_expr = tmp;
17415 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17416 asm_expr = TREE_OPERAND (asm_expr, 0);
17417 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17418 }
17419 break;
17420
17421 case TRY_BLOCK:
17422 if (CLEANUP_P (t))
17423 {
17424 stmt = begin_try_block ();
17425 RECUR (TRY_STMTS (t));
17426 finish_cleanup_try_block (stmt);
17427 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17428 }
17429 else
17430 {
17431 tree compound_stmt = NULL_TREE;
17432
17433 if (FN_TRY_BLOCK_P (t))
17434 stmt = begin_function_try_block (&compound_stmt);
17435 else
17436 stmt = begin_try_block ();
17437
17438 RECUR (TRY_STMTS (t));
17439
17440 if (FN_TRY_BLOCK_P (t))
17441 finish_function_try_block (stmt);
17442 else
17443 finish_try_block (stmt);
17444
17445 RECUR (TRY_HANDLERS (t));
17446 if (FN_TRY_BLOCK_P (t))
17447 finish_function_handler_sequence (stmt, compound_stmt);
17448 else
17449 finish_handler_sequence (stmt);
17450 }
17451 break;
17452
17453 case HANDLER:
17454 {
17455 tree decl = HANDLER_PARMS (t);
17456
17457 if (decl)
17458 {
17459 decl = tsubst (decl, args, complain, in_decl);
17460 /* Prevent instantiate_decl from trying to instantiate
17461 this variable. We've already done all that needs to be
17462 done. */
17463 if (decl != error_mark_node)
17464 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17465 }
17466 stmt = begin_handler ();
17467 finish_handler_parms (decl, stmt);
17468 RECUR (HANDLER_BODY (t));
17469 finish_handler (stmt);
17470 }
17471 break;
17472
17473 case TAG_DEFN:
17474 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17475 if (CLASS_TYPE_P (tmp))
17476 {
17477 /* Local classes are not independent templates; they are
17478 instantiated along with their containing function. And this
17479 way we don't have to deal with pushing out of one local class
17480 to instantiate a member of another local class. */
17481 /* Closures are handled by the LAMBDA_EXPR. */
17482 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17483 complete_type (tmp);
17484 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17485 if ((VAR_P (fld)
17486 || (TREE_CODE (fld) == FUNCTION_DECL
17487 && !DECL_ARTIFICIAL (fld)))
17488 && DECL_TEMPLATE_INSTANTIATION (fld))
17489 instantiate_decl (fld, /*defer_ok=*/false,
17490 /*expl_inst_class=*/false);
17491 }
17492 break;
17493
17494 case STATIC_ASSERT:
17495 {
17496 tree condition;
17497
17498 ++c_inhibit_evaluation_warnings;
17499 condition =
17500 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17501 args,
17502 complain, in_decl,
17503 /*integral_constant_expression_p=*/true);
17504 --c_inhibit_evaluation_warnings;
17505
17506 finish_static_assert (condition,
17507 STATIC_ASSERT_MESSAGE (t),
17508 STATIC_ASSERT_SOURCE_LOCATION (t),
17509 /*member_p=*/false);
17510 }
17511 break;
17512
17513 case OACC_KERNELS:
17514 case OACC_PARALLEL:
17515 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17516 in_decl);
17517 stmt = begin_omp_parallel ();
17518 RECUR (OMP_BODY (t));
17519 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17520 break;
17521
17522 case OMP_PARALLEL:
17523 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17524 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17525 complain, in_decl);
17526 if (OMP_PARALLEL_COMBINED (t))
17527 omp_parallel_combined_clauses = &tmp;
17528 stmt = begin_omp_parallel ();
17529 RECUR (OMP_PARALLEL_BODY (t));
17530 gcc_assert (omp_parallel_combined_clauses == NULL);
17531 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17532 = OMP_PARALLEL_COMBINED (t);
17533 pop_omp_privatization_clauses (r);
17534 break;
17535
17536 case OMP_TASK:
17537 if (OMP_TASK_BODY (t) == NULL_TREE)
17538 {
17539 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17540 complain, in_decl);
17541 t = copy_node (t);
17542 OMP_TASK_CLAUSES (t) = tmp;
17543 add_stmt (t);
17544 break;
17545 }
17546 r = push_omp_privatization_clauses (false);
17547 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17548 complain, in_decl);
17549 stmt = begin_omp_task ();
17550 RECUR (OMP_TASK_BODY (t));
17551 finish_omp_task (tmp, stmt);
17552 pop_omp_privatization_clauses (r);
17553 break;
17554
17555 case OMP_FOR:
17556 case OMP_LOOP:
17557 case OMP_SIMD:
17558 case OMP_DISTRIBUTE:
17559 case OMP_TASKLOOP:
17560 case OACC_LOOP:
17561 {
17562 tree clauses, body, pre_body;
17563 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17564 tree orig_declv = NULL_TREE;
17565 tree incrv = NULL_TREE;
17566 enum c_omp_region_type ort = C_ORT_OMP;
17567 bool any_range_for = false;
17568 int i;
17569
17570 if (TREE_CODE (t) == OACC_LOOP)
17571 ort = C_ORT_ACC;
17572
17573 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17574 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17575 in_decl);
17576 if (OMP_FOR_INIT (t) != NULL_TREE)
17577 {
17578 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17579 if (OMP_FOR_ORIG_DECLS (t))
17580 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17581 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17582 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17583 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17584 }
17585
17586 keep_next_level (true);
17587 stmt = begin_omp_structured_block ();
17588
17589 pre_body = push_stmt_list ();
17590 RECUR (OMP_FOR_PRE_BODY (t));
17591 pre_body = pop_stmt_list (pre_body);
17592
17593 if (OMP_FOR_INIT (t) != NULL_TREE)
17594 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17595 any_range_for
17596 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17597 condv, incrv, &clauses, args,
17598 complain, in_decl,
17599 integral_constant_expression_p);
17600 omp_parallel_combined_clauses = NULL;
17601
17602 if (any_range_for)
17603 {
17604 gcc_assert (orig_declv);
17605 body = begin_omp_structured_block ();
17606 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17607 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17608 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17609 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17610 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17611 TREE_VEC_ELT (declv, i));
17612 }
17613 else
17614 body = push_stmt_list ();
17615 RECUR (OMP_FOR_BODY (t));
17616 if (any_range_for)
17617 body = finish_omp_structured_block (body);
17618 else
17619 body = pop_stmt_list (body);
17620
17621 if (OMP_FOR_INIT (t) != NULL_TREE)
17622 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17623 orig_declv, initv, condv, incrv, body, pre_body,
17624 NULL, clauses);
17625 else
17626 {
17627 t = make_node (TREE_CODE (t));
17628 TREE_TYPE (t) = void_type_node;
17629 OMP_FOR_BODY (t) = body;
17630 OMP_FOR_PRE_BODY (t) = pre_body;
17631 OMP_FOR_CLAUSES (t) = clauses;
17632 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17633 add_stmt (t);
17634 }
17635
17636 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17637 t));
17638 pop_omp_privatization_clauses (r);
17639 }
17640 break;
17641
17642 case OMP_SECTIONS:
17643 omp_parallel_combined_clauses = NULL;
17644 /* FALLTHRU */
17645 case OMP_SINGLE:
17646 case OMP_TEAMS:
17647 case OMP_CRITICAL:
17648 case OMP_TASKGROUP:
17649 case OMP_SCAN:
17650 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17651 && OMP_TEAMS_COMBINED (t));
17652 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17653 in_decl);
17654 if (TREE_CODE (t) == OMP_TEAMS)
17655 {
17656 keep_next_level (true);
17657 stmt = begin_omp_structured_block ();
17658 RECUR (OMP_BODY (t));
17659 stmt = finish_omp_structured_block (stmt);
17660 }
17661 else
17662 {
17663 stmt = push_stmt_list ();
17664 RECUR (OMP_BODY (t));
17665 stmt = pop_stmt_list (stmt);
17666 }
17667
17668 t = copy_node (t);
17669 OMP_BODY (t) = stmt;
17670 OMP_CLAUSES (t) = tmp;
17671 add_stmt (t);
17672 pop_omp_privatization_clauses (r);
17673 break;
17674
17675 case OMP_DEPOBJ:
17676 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17677 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17678 {
17679 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17680 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17681 {
17682 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17683 args, complain, in_decl);
17684 if (tmp == NULL_TREE)
17685 tmp = error_mark_node;
17686 }
17687 else
17688 {
17689 kind = (enum omp_clause_depend_kind)
17690 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17691 tmp = NULL_TREE;
17692 }
17693 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17694 }
17695 else
17696 finish_omp_depobj (EXPR_LOCATION (t), r,
17697 OMP_CLAUSE_DEPEND_SOURCE,
17698 OMP_DEPOBJ_CLAUSES (t));
17699 break;
17700
17701 case OACC_DATA:
17702 case OMP_TARGET_DATA:
17703 case OMP_TARGET:
17704 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17705 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17706 in_decl);
17707 keep_next_level (true);
17708 stmt = begin_omp_structured_block ();
17709
17710 RECUR (OMP_BODY (t));
17711 stmt = finish_omp_structured_block (stmt);
17712
17713 t = copy_node (t);
17714 OMP_BODY (t) = stmt;
17715 OMP_CLAUSES (t) = tmp;
17716 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17717 {
17718 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17719 if (teams)
17720 {
17721 /* For combined target teams, ensure the num_teams and
17722 thread_limit clause expressions are evaluated on the host,
17723 before entering the target construct. */
17724 tree c;
17725 for (c = OMP_TEAMS_CLAUSES (teams);
17726 c; c = OMP_CLAUSE_CHAIN (c))
17727 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17728 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17729 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17730 {
17731 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17732 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17733 if (expr == error_mark_node)
17734 continue;
17735 tmp = TARGET_EXPR_SLOT (expr);
17736 add_stmt (expr);
17737 OMP_CLAUSE_OPERAND (c, 0) = expr;
17738 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17739 OMP_CLAUSE_FIRSTPRIVATE);
17740 OMP_CLAUSE_DECL (tc) = tmp;
17741 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17742 OMP_TARGET_CLAUSES (t) = tc;
17743 }
17744 }
17745 }
17746 add_stmt (t);
17747 break;
17748
17749 case OACC_DECLARE:
17750 t = copy_node (t);
17751 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17752 complain, in_decl);
17753 OACC_DECLARE_CLAUSES (t) = tmp;
17754 add_stmt (t);
17755 break;
17756
17757 case OMP_TARGET_UPDATE:
17758 case OMP_TARGET_ENTER_DATA:
17759 case OMP_TARGET_EXIT_DATA:
17760 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17761 complain, in_decl);
17762 t = copy_node (t);
17763 OMP_STANDALONE_CLAUSES (t) = tmp;
17764 add_stmt (t);
17765 break;
17766
17767 case OACC_ENTER_DATA:
17768 case OACC_EXIT_DATA:
17769 case OACC_UPDATE:
17770 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17771 complain, in_decl);
17772 t = copy_node (t);
17773 OMP_STANDALONE_CLAUSES (t) = tmp;
17774 add_stmt (t);
17775 break;
17776
17777 case OMP_ORDERED:
17778 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17779 complain, in_decl);
17780 stmt = push_stmt_list ();
17781 RECUR (OMP_BODY (t));
17782 stmt = pop_stmt_list (stmt);
17783
17784 t = copy_node (t);
17785 OMP_BODY (t) = stmt;
17786 OMP_ORDERED_CLAUSES (t) = tmp;
17787 add_stmt (t);
17788 break;
17789
17790 case OMP_SECTION:
17791 case OMP_MASTER:
17792 stmt = push_stmt_list ();
17793 RECUR (OMP_BODY (t));
17794 stmt = pop_stmt_list (stmt);
17795
17796 t = copy_node (t);
17797 OMP_BODY (t) = stmt;
17798 add_stmt (t);
17799 break;
17800
17801 case OMP_ATOMIC:
17802 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17803 tmp = NULL_TREE;
17804 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17805 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17806 complain, in_decl);
17807 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17808 {
17809 tree op1 = TREE_OPERAND (t, 1);
17810 tree rhs1 = NULL_TREE;
17811 tree lhs, rhs;
17812 if (TREE_CODE (op1) == COMPOUND_EXPR)
17813 {
17814 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17815 op1 = TREE_OPERAND (op1, 1);
17816 }
17817 lhs = RECUR (TREE_OPERAND (op1, 0));
17818 rhs = RECUR (TREE_OPERAND (op1, 1));
17819 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17820 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17821 OMP_ATOMIC_MEMORY_ORDER (t));
17822 }
17823 else
17824 {
17825 tree op1 = TREE_OPERAND (t, 1);
17826 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17827 tree rhs1 = NULL_TREE;
17828 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17829 enum tree_code opcode = NOP_EXPR;
17830 if (code == OMP_ATOMIC_READ)
17831 {
17832 v = RECUR (TREE_OPERAND (op1, 0));
17833 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17834 }
17835 else if (code == OMP_ATOMIC_CAPTURE_OLD
17836 || code == OMP_ATOMIC_CAPTURE_NEW)
17837 {
17838 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17839 v = RECUR (TREE_OPERAND (op1, 0));
17840 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17841 if (TREE_CODE (op11) == COMPOUND_EXPR)
17842 {
17843 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17844 op11 = TREE_OPERAND (op11, 1);
17845 }
17846 lhs = RECUR (TREE_OPERAND (op11, 0));
17847 rhs = RECUR (TREE_OPERAND (op11, 1));
17848 opcode = TREE_CODE (op11);
17849 if (opcode == MODIFY_EXPR)
17850 opcode = NOP_EXPR;
17851 }
17852 else
17853 {
17854 code = OMP_ATOMIC;
17855 lhs = RECUR (TREE_OPERAND (op1, 0));
17856 rhs = RECUR (TREE_OPERAND (op1, 1));
17857 }
17858 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17859 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17860 }
17861 break;
17862
17863 case TRANSACTION_EXPR:
17864 {
17865 int flags = 0;
17866 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17867 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17868
17869 if (TRANSACTION_EXPR_IS_STMT (t))
17870 {
17871 tree body = TRANSACTION_EXPR_BODY (t);
17872 tree noex = NULL_TREE;
17873 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17874 {
17875 noex = MUST_NOT_THROW_COND (body);
17876 if (noex == NULL_TREE)
17877 noex = boolean_true_node;
17878 body = TREE_OPERAND (body, 0);
17879 }
17880 stmt = begin_transaction_stmt (input_location, NULL, flags);
17881 RECUR (body);
17882 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17883 }
17884 else
17885 {
17886 stmt = build_transaction_expr (EXPR_LOCATION (t),
17887 RECUR (TRANSACTION_EXPR_BODY (t)),
17888 flags, NULL_TREE);
17889 RETURN (stmt);
17890 }
17891 }
17892 break;
17893
17894 case MUST_NOT_THROW_EXPR:
17895 {
17896 tree op0 = RECUR (TREE_OPERAND (t, 0));
17897 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17898 RETURN (build_must_not_throw_expr (op0, cond));
17899 }
17900
17901 case EXPR_PACK_EXPANSION:
17902 error ("invalid use of pack expansion expression");
17903 RETURN (error_mark_node);
17904
17905 case NONTYPE_ARGUMENT_PACK:
17906 error ("use %<...%> to expand argument pack");
17907 RETURN (error_mark_node);
17908
17909 case COMPOUND_EXPR:
17910 tmp = RECUR (TREE_OPERAND (t, 0));
17911 if (tmp == NULL_TREE)
17912 /* If the first operand was a statement, we're done with it. */
17913 RETURN (RECUR (TREE_OPERAND (t, 1)));
17914 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17915 RECUR (TREE_OPERAND (t, 1)),
17916 complain));
17917
17918 case ANNOTATE_EXPR:
17919 tmp = RECUR (TREE_OPERAND (t, 0));
17920 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17921 TREE_TYPE (tmp), tmp,
17922 RECUR (TREE_OPERAND (t, 1)),
17923 RECUR (TREE_OPERAND (t, 2))));
17924
17925 case PREDICT_EXPR:
17926 RETURN (add_stmt (copy_node (t)));
17927
17928 default:
17929 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17930
17931 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17932 /*function_p=*/false,
17933 integral_constant_expression_p));
17934 }
17935
17936 RETURN (NULL_TREE);
17937 out:
17938 input_location = loc;
17939 return r;
17940 #undef RECUR
17941 #undef RETURN
17942 }
17943
17944 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17945 function. For description of the body see comment above
17946 cp_parser_omp_declare_reduction_exprs. */
17947
17948 static void
17949 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17950 {
17951 if (t == NULL_TREE || t == error_mark_node)
17952 return;
17953
17954 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17955
17956 tree_stmt_iterator tsi;
17957 int i;
17958 tree stmts[7];
17959 memset (stmts, 0, sizeof stmts);
17960 for (i = 0, tsi = tsi_start (t);
17961 i < 7 && !tsi_end_p (tsi);
17962 i++, tsi_next (&tsi))
17963 stmts[i] = tsi_stmt (tsi);
17964 gcc_assert (tsi_end_p (tsi));
17965
17966 if (i >= 3)
17967 {
17968 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17969 && TREE_CODE (stmts[1]) == DECL_EXPR);
17970 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17971 args, complain, in_decl);
17972 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17973 args, complain, in_decl);
17974 DECL_CONTEXT (omp_out) = current_function_decl;
17975 DECL_CONTEXT (omp_in) = current_function_decl;
17976 keep_next_level (true);
17977 tree block = begin_omp_structured_block ();
17978 tsubst_expr (stmts[2], args, complain, in_decl, false);
17979 block = finish_omp_structured_block (block);
17980 block = maybe_cleanup_point_expr_void (block);
17981 add_decl_expr (omp_out);
17982 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17983 TREE_NO_WARNING (omp_out) = 1;
17984 add_decl_expr (omp_in);
17985 finish_expr_stmt (block);
17986 }
17987 if (i >= 6)
17988 {
17989 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17990 && TREE_CODE (stmts[4]) == DECL_EXPR);
17991 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17992 args, complain, in_decl);
17993 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17994 args, complain, in_decl);
17995 DECL_CONTEXT (omp_priv) = current_function_decl;
17996 DECL_CONTEXT (omp_orig) = current_function_decl;
17997 keep_next_level (true);
17998 tree block = begin_omp_structured_block ();
17999 tsubst_expr (stmts[5], args, complain, in_decl, false);
18000 block = finish_omp_structured_block (block);
18001 block = maybe_cleanup_point_expr_void (block);
18002 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18003 add_decl_expr (omp_priv);
18004 add_decl_expr (omp_orig);
18005 finish_expr_stmt (block);
18006 if (i == 7)
18007 add_decl_expr (omp_orig);
18008 }
18009 }
18010
18011 /* T is a postfix-expression that is not being used in a function
18012 call. Return the substituted version of T. */
18013
18014 static tree
18015 tsubst_non_call_postfix_expression (tree t, tree args,
18016 tsubst_flags_t complain,
18017 tree in_decl)
18018 {
18019 if (TREE_CODE (t) == SCOPE_REF)
18020 t = tsubst_qualified_id (t, args, complain, in_decl,
18021 /*done=*/false, /*address_p=*/false);
18022 else
18023 t = tsubst_copy_and_build (t, args, complain, in_decl,
18024 /*function_p=*/false,
18025 /*integral_constant_expression_p=*/false);
18026
18027 return t;
18028 }
18029
18030 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18031 instantiation context. Instantiating a pack expansion containing a lambda
18032 might result in multiple lambdas all based on the same lambda in the
18033 template. */
18034
18035 tree
18036 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18037 {
18038 tree oldfn = lambda_function (t);
18039 in_decl = oldfn;
18040
18041 /* If we have already specialized this lambda expr, reuse it. See
18042 PR c++/87322. */
18043 if (local_specializations)
18044 if (tree r = retrieve_local_specialization (t))
18045 return r;
18046
18047 tree r = build_lambda_expr ();
18048
18049 if (local_specializations)
18050 register_local_specialization (r, t);
18051
18052 LAMBDA_EXPR_LOCATION (r)
18053 = LAMBDA_EXPR_LOCATION (t);
18054 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18055 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18056 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18057 LAMBDA_EXPR_INSTANTIATED (r) = true;
18058
18059 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18060 /* A lambda in a default argument outside a class gets no
18061 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18062 tsubst_default_argument calls start_lambda_scope, so we need to
18063 specifically ignore it here, and use the global scope. */
18064 record_null_lambda_scope (r);
18065 else
18066 record_lambda_scope (r);
18067
18068 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18069 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18070
18071 vec<tree,va_gc>* field_packs = NULL;
18072
18073 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18074 cap = TREE_CHAIN (cap))
18075 {
18076 tree ofield = TREE_PURPOSE (cap);
18077 if (PACK_EXPANSION_P (ofield))
18078 ofield = PACK_EXPANSION_PATTERN (ofield);
18079 tree field = tsubst_decl (ofield, args, complain);
18080
18081 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18082 {
18083 /* Remember these for when we've pushed local_specializations. */
18084 vec_safe_push (field_packs, ofield);
18085 vec_safe_push (field_packs, field);
18086 }
18087
18088 if (field == error_mark_node)
18089 return error_mark_node;
18090
18091 tree init = TREE_VALUE (cap);
18092 if (PACK_EXPANSION_P (init))
18093 init = tsubst_pack_expansion (init, args, complain, in_decl);
18094 else
18095 init = tsubst_copy_and_build (init, args, complain, in_decl,
18096 /*fn*/false, /*constexpr*/false);
18097
18098 if (TREE_CODE (field) == TREE_VEC)
18099 {
18100 int len = TREE_VEC_LENGTH (field);
18101 gcc_assert (TREE_CODE (init) == TREE_VEC
18102 && TREE_VEC_LENGTH (init) == len);
18103 for (int i = 0; i < len; ++i)
18104 LAMBDA_EXPR_CAPTURE_LIST (r)
18105 = tree_cons (TREE_VEC_ELT (field, i),
18106 TREE_VEC_ELT (init, i),
18107 LAMBDA_EXPR_CAPTURE_LIST (r));
18108 }
18109 else
18110 {
18111 LAMBDA_EXPR_CAPTURE_LIST (r)
18112 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
18113
18114 if (id_equal (DECL_NAME (field), "__this"))
18115 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18116 }
18117 }
18118
18119 tree type = begin_lambda_type (r);
18120 if (type == error_mark_node)
18121 return error_mark_node;
18122
18123 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18124 determine_visibility (TYPE_NAME (type));
18125
18126 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18127
18128 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18129 ? DECL_TI_TEMPLATE (oldfn)
18130 : NULL_TREE);
18131
18132 tree fntype = static_fn_type (oldfn);
18133 if (oldtmpl)
18134 ++processing_template_decl;
18135 fntype = tsubst (fntype, args, complain, in_decl);
18136 if (oldtmpl)
18137 --processing_template_decl;
18138
18139 if (fntype == error_mark_node)
18140 r = error_mark_node;
18141 else
18142 {
18143 /* The body of a lambda-expression is not a subexpression of the
18144 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18145 which would be skipped if cp_unevaluated_operand. */
18146 cp_evaluated ev;
18147
18148 /* Fix the type of 'this'. */
18149 fntype = build_memfn_type (fntype, type,
18150 type_memfn_quals (fntype),
18151 type_memfn_rqual (fntype));
18152 tree fn, tmpl;
18153 if (oldtmpl)
18154 {
18155 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18156 fn = DECL_TEMPLATE_RESULT (tmpl);
18157 finish_member_declaration (tmpl);
18158 }
18159 else
18160 {
18161 tmpl = NULL_TREE;
18162 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18163 finish_member_declaration (fn);
18164 }
18165
18166 /* Let finish_function set this. */
18167 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18168
18169 bool nested = cfun;
18170 if (nested)
18171 push_function_context ();
18172 else
18173 /* Still increment function_depth so that we don't GC in the
18174 middle of an expression. */
18175 ++function_depth;
18176
18177 local_specialization_stack s (lss_copy);
18178
18179 tree body = start_lambda_function (fn, r);
18180
18181 /* Now record them for lookup_init_capture_pack. */
18182 int fplen = vec_safe_length (field_packs);
18183 for (int i = 0; i < fplen; )
18184 {
18185 tree pack = (*field_packs)[i++];
18186 tree inst = (*field_packs)[i++];
18187 register_local_specialization (inst, pack);
18188 }
18189 release_tree_vector (field_packs);
18190
18191 register_parameter_specializations (oldfn, fn);
18192
18193 if (oldtmpl)
18194 {
18195 /* We might not partially instantiate some parts of the function, so
18196 copy these flags from the original template. */
18197 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18198 current_function_returns_value = ol->returns_value;
18199 current_function_returns_null = ol->returns_null;
18200 current_function_returns_abnormally = ol->returns_abnormally;
18201 current_function_infinite_loop = ol->infinite_loop;
18202 }
18203
18204 /* [temp.deduct] A lambda-expression appearing in a function type or a
18205 template parameter is not considered part of the immediate context for
18206 the purposes of template argument deduction. */
18207 complain = tf_warning_or_error;
18208
18209 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18210 /*constexpr*/false);
18211
18212 finish_lambda_function (body);
18213
18214 if (nested)
18215 pop_function_context ();
18216 else
18217 --function_depth;
18218
18219 /* The capture list was built up in reverse order; fix that now. */
18220 LAMBDA_EXPR_CAPTURE_LIST (r)
18221 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18222
18223 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18224
18225 maybe_add_lambda_conv_op (type);
18226 }
18227
18228 finish_struct (type, /*attr*/NULL_TREE);
18229
18230 insert_pending_capture_proxies ();
18231
18232 return r;
18233 }
18234
18235 /* Like tsubst but deals with expressions and performs semantic
18236 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18237
18238 tree
18239 tsubst_copy_and_build (tree t,
18240 tree args,
18241 tsubst_flags_t complain,
18242 tree in_decl,
18243 bool function_p,
18244 bool integral_constant_expression_p)
18245 {
18246 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18247 #define RECUR(NODE) \
18248 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18249 /*function_p=*/false, \
18250 integral_constant_expression_p)
18251
18252 tree retval, op1;
18253 location_t loc;
18254
18255 if (t == NULL_TREE || t == error_mark_node)
18256 return t;
18257
18258 loc = input_location;
18259 if (location_t eloc = cp_expr_location (t))
18260 input_location = eloc;
18261
18262 /* N3276 decltype magic only applies to calls at the top level or on the
18263 right side of a comma. */
18264 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18265 complain &= ~tf_decltype;
18266
18267 switch (TREE_CODE (t))
18268 {
18269 case USING_DECL:
18270 t = DECL_NAME (t);
18271 /* Fall through. */
18272 case IDENTIFIER_NODE:
18273 {
18274 tree decl;
18275 cp_id_kind idk;
18276 bool non_integral_constant_expression_p;
18277 const char *error_msg;
18278
18279 if (IDENTIFIER_CONV_OP_P (t))
18280 {
18281 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18282 t = make_conv_op_name (new_type);
18283 }
18284
18285 /* Look up the name. */
18286 decl = lookup_name (t);
18287
18288 /* By convention, expressions use ERROR_MARK_NODE to indicate
18289 failure, not NULL_TREE. */
18290 if (decl == NULL_TREE)
18291 decl = error_mark_node;
18292
18293 decl = finish_id_expression (t, decl, NULL_TREE,
18294 &idk,
18295 integral_constant_expression_p,
18296 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18297 &non_integral_constant_expression_p,
18298 /*template_p=*/false,
18299 /*done=*/true,
18300 /*address_p=*/false,
18301 /*template_arg_p=*/false,
18302 &error_msg,
18303 input_location);
18304 if (error_msg)
18305 error (error_msg);
18306 if (!function_p && identifier_p (decl))
18307 {
18308 if (complain & tf_error)
18309 unqualified_name_lookup_error (decl);
18310 decl = error_mark_node;
18311 }
18312 RETURN (decl);
18313 }
18314
18315 case TEMPLATE_ID_EXPR:
18316 {
18317 tree object;
18318 tree templ = RECUR (TREE_OPERAND (t, 0));
18319 tree targs = TREE_OPERAND (t, 1);
18320
18321 if (targs)
18322 targs = tsubst_template_args (targs, args, complain, in_decl);
18323 if (targs == error_mark_node)
18324 RETURN (error_mark_node);
18325
18326 if (TREE_CODE (templ) == SCOPE_REF)
18327 {
18328 tree name = TREE_OPERAND (templ, 1);
18329 tree tid = lookup_template_function (name, targs);
18330 TREE_OPERAND (templ, 1) = tid;
18331 RETURN (templ);
18332 }
18333
18334 if (variable_template_p (templ))
18335 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18336
18337 if (TREE_CODE (templ) == COMPONENT_REF)
18338 {
18339 object = TREE_OPERAND (templ, 0);
18340 templ = TREE_OPERAND (templ, 1);
18341 }
18342 else
18343 object = NULL_TREE;
18344 templ = lookup_template_function (templ, targs);
18345
18346 if (object)
18347 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18348 object, templ, NULL_TREE));
18349 else
18350 RETURN (baselink_for_fns (templ));
18351 }
18352
18353 case INDIRECT_REF:
18354 {
18355 tree r = RECUR (TREE_OPERAND (t, 0));
18356
18357 if (REFERENCE_REF_P (t))
18358 {
18359 /* A type conversion to reference type will be enclosed in
18360 such an indirect ref, but the substitution of the cast
18361 will have also added such an indirect ref. */
18362 r = convert_from_reference (r);
18363 }
18364 else
18365 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18366 complain|decltype_flag);
18367
18368 if (REF_PARENTHESIZED_P (t))
18369 r = force_paren_expr (r);
18370
18371 RETURN (r);
18372 }
18373
18374 case NOP_EXPR:
18375 {
18376 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18377 tree op0 = RECUR (TREE_OPERAND (t, 0));
18378 RETURN (build_nop (type, op0));
18379 }
18380
18381 case IMPLICIT_CONV_EXPR:
18382 {
18383 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18384 tree expr = RECUR (TREE_OPERAND (t, 0));
18385 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18386 {
18387 retval = copy_node (t);
18388 TREE_TYPE (retval) = type;
18389 TREE_OPERAND (retval, 0) = expr;
18390 RETURN (retval);
18391 }
18392 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18393 /* We'll pass this to convert_nontype_argument again, we don't need
18394 to actually perform any conversion here. */
18395 RETURN (expr);
18396 int flags = LOOKUP_IMPLICIT;
18397 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18398 flags = LOOKUP_NORMAL;
18399 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18400 flags |= LOOKUP_NO_NARROWING;
18401 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18402 flags));
18403 }
18404
18405 case CONVERT_EXPR:
18406 {
18407 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18408 tree op0 = RECUR (TREE_OPERAND (t, 0));
18409 if (op0 == error_mark_node)
18410 RETURN (error_mark_node);
18411 RETURN (build1 (CONVERT_EXPR, type, op0));
18412 }
18413
18414 case CAST_EXPR:
18415 case REINTERPRET_CAST_EXPR:
18416 case CONST_CAST_EXPR:
18417 case DYNAMIC_CAST_EXPR:
18418 case STATIC_CAST_EXPR:
18419 {
18420 tree type;
18421 tree op, r = NULL_TREE;
18422
18423 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18424 if (integral_constant_expression_p
18425 && !cast_valid_in_integral_constant_expression_p (type))
18426 {
18427 if (complain & tf_error)
18428 error ("a cast to a type other than an integral or "
18429 "enumeration type cannot appear in a constant-expression");
18430 RETURN (error_mark_node);
18431 }
18432
18433 op = RECUR (TREE_OPERAND (t, 0));
18434
18435 warning_sentinel s(warn_useless_cast);
18436 warning_sentinel s2(warn_ignored_qualifiers);
18437 switch (TREE_CODE (t))
18438 {
18439 case CAST_EXPR:
18440 r = build_functional_cast (type, op, complain);
18441 break;
18442 case REINTERPRET_CAST_EXPR:
18443 r = build_reinterpret_cast (type, op, complain);
18444 break;
18445 case CONST_CAST_EXPR:
18446 r = build_const_cast (type, op, complain);
18447 break;
18448 case DYNAMIC_CAST_EXPR:
18449 r = build_dynamic_cast (type, op, complain);
18450 break;
18451 case STATIC_CAST_EXPR:
18452 r = build_static_cast (type, op, complain);
18453 break;
18454 default:
18455 gcc_unreachable ();
18456 }
18457
18458 RETURN (r);
18459 }
18460
18461 case POSTDECREMENT_EXPR:
18462 case POSTINCREMENT_EXPR:
18463 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18464 args, complain, in_decl);
18465 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18466 complain|decltype_flag));
18467
18468 case PREDECREMENT_EXPR:
18469 case PREINCREMENT_EXPR:
18470 case NEGATE_EXPR:
18471 case BIT_NOT_EXPR:
18472 case ABS_EXPR:
18473 case TRUTH_NOT_EXPR:
18474 case UNARY_PLUS_EXPR: /* Unary + */
18475 case REALPART_EXPR:
18476 case IMAGPART_EXPR:
18477 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18478 RECUR (TREE_OPERAND (t, 0)),
18479 complain|decltype_flag));
18480
18481 case FIX_TRUNC_EXPR:
18482 gcc_unreachable ();
18483
18484 case ADDR_EXPR:
18485 op1 = TREE_OPERAND (t, 0);
18486 if (TREE_CODE (op1) == LABEL_DECL)
18487 RETURN (finish_label_address_expr (DECL_NAME (op1),
18488 EXPR_LOCATION (op1)));
18489 if (TREE_CODE (op1) == SCOPE_REF)
18490 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18491 /*done=*/true, /*address_p=*/true);
18492 else
18493 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18494 in_decl);
18495 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18496 complain|decltype_flag));
18497
18498 case PLUS_EXPR:
18499 case MINUS_EXPR:
18500 case MULT_EXPR:
18501 case TRUNC_DIV_EXPR:
18502 case CEIL_DIV_EXPR:
18503 case FLOOR_DIV_EXPR:
18504 case ROUND_DIV_EXPR:
18505 case EXACT_DIV_EXPR:
18506 case BIT_AND_EXPR:
18507 case BIT_IOR_EXPR:
18508 case BIT_XOR_EXPR:
18509 case TRUNC_MOD_EXPR:
18510 case FLOOR_MOD_EXPR:
18511 case TRUTH_ANDIF_EXPR:
18512 case TRUTH_ORIF_EXPR:
18513 case TRUTH_AND_EXPR:
18514 case TRUTH_OR_EXPR:
18515 case RSHIFT_EXPR:
18516 case LSHIFT_EXPR:
18517 case RROTATE_EXPR:
18518 case LROTATE_EXPR:
18519 case EQ_EXPR:
18520 case NE_EXPR:
18521 case MAX_EXPR:
18522 case MIN_EXPR:
18523 case LE_EXPR:
18524 case GE_EXPR:
18525 case LT_EXPR:
18526 case GT_EXPR:
18527 case MEMBER_REF:
18528 case DOTSTAR_EXPR:
18529 {
18530 warning_sentinel s1(warn_type_limits);
18531 warning_sentinel s2(warn_div_by_zero);
18532 warning_sentinel s3(warn_logical_op);
18533 warning_sentinel s4(warn_tautological_compare);
18534 tree op0 = RECUR (TREE_OPERAND (t, 0));
18535 tree op1 = RECUR (TREE_OPERAND (t, 1));
18536 tree r = build_x_binary_op
18537 (input_location, TREE_CODE (t),
18538 op0,
18539 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18540 ? ERROR_MARK
18541 : TREE_CODE (TREE_OPERAND (t, 0))),
18542 op1,
18543 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18544 ? ERROR_MARK
18545 : TREE_CODE (TREE_OPERAND (t, 1))),
18546 /*overload=*/NULL,
18547 complain|decltype_flag);
18548 if (EXPR_P (r) && TREE_NO_WARNING (t))
18549 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18550
18551 RETURN (r);
18552 }
18553
18554 case POINTER_PLUS_EXPR:
18555 {
18556 tree op0 = RECUR (TREE_OPERAND (t, 0));
18557 tree op1 = RECUR (TREE_OPERAND (t, 1));
18558 RETURN (fold_build_pointer_plus (op0, op1));
18559 }
18560
18561 case SCOPE_REF:
18562 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18563 /*address_p=*/false));
18564 case ARRAY_REF:
18565 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18566 args, complain, in_decl);
18567 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18568 RECUR (TREE_OPERAND (t, 1)),
18569 complain|decltype_flag));
18570
18571 case SIZEOF_EXPR:
18572 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18573 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18574 RETURN (tsubst_copy (t, args, complain, in_decl));
18575 /* Fall through */
18576
18577 case ALIGNOF_EXPR:
18578 {
18579 tree r;
18580
18581 op1 = TREE_OPERAND (t, 0);
18582 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18583 op1 = TREE_TYPE (op1);
18584 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18585 && ALIGNOF_EXPR_STD_P (t));
18586 if (!args)
18587 {
18588 /* When there are no ARGS, we are trying to evaluate a
18589 non-dependent expression from the parser. Trying to do
18590 the substitutions may not work. */
18591 if (!TYPE_P (op1))
18592 op1 = TREE_TYPE (op1);
18593 }
18594 else
18595 {
18596 ++cp_unevaluated_operand;
18597 ++c_inhibit_evaluation_warnings;
18598 if (TYPE_P (op1))
18599 op1 = tsubst (op1, args, complain, in_decl);
18600 else
18601 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18602 /*function_p=*/false,
18603 /*integral_constant_expression_p=*/
18604 false);
18605 --cp_unevaluated_operand;
18606 --c_inhibit_evaluation_warnings;
18607 }
18608 if (TYPE_P (op1))
18609 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18610 complain & tf_error);
18611 else
18612 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18613 complain & tf_error);
18614 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18615 {
18616 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18617 {
18618 if (!processing_template_decl && TYPE_P (op1))
18619 {
18620 r = build_min (SIZEOF_EXPR, size_type_node,
18621 build1 (NOP_EXPR, op1, error_mark_node));
18622 SIZEOF_EXPR_TYPE_P (r) = 1;
18623 }
18624 else
18625 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18626 TREE_SIDE_EFFECTS (r) = 0;
18627 TREE_READONLY (r) = 1;
18628 }
18629 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18630 }
18631 RETURN (r);
18632 }
18633
18634 case AT_ENCODE_EXPR:
18635 {
18636 op1 = TREE_OPERAND (t, 0);
18637 ++cp_unevaluated_operand;
18638 ++c_inhibit_evaluation_warnings;
18639 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18640 /*function_p=*/false,
18641 /*integral_constant_expression_p=*/false);
18642 --cp_unevaluated_operand;
18643 --c_inhibit_evaluation_warnings;
18644 RETURN (objc_build_encode_expr (op1));
18645 }
18646
18647 case NOEXCEPT_EXPR:
18648 op1 = TREE_OPERAND (t, 0);
18649 ++cp_unevaluated_operand;
18650 ++c_inhibit_evaluation_warnings;
18651 ++cp_noexcept_operand;
18652 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18653 /*function_p=*/false,
18654 /*integral_constant_expression_p=*/false);
18655 --cp_unevaluated_operand;
18656 --c_inhibit_evaluation_warnings;
18657 --cp_noexcept_operand;
18658 RETURN (finish_noexcept_expr (op1, complain));
18659
18660 case MODOP_EXPR:
18661 {
18662 warning_sentinel s(warn_div_by_zero);
18663 tree lhs = RECUR (TREE_OPERAND (t, 0));
18664 tree rhs = RECUR (TREE_OPERAND (t, 2));
18665 tree r = build_x_modify_expr
18666 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18667 complain|decltype_flag);
18668 /* TREE_NO_WARNING must be set if either the expression was
18669 parenthesized or it uses an operator such as >>= rather
18670 than plain assignment. In the former case, it was already
18671 set and must be copied. In the latter case,
18672 build_x_modify_expr sets it and it must not be reset
18673 here. */
18674 if (TREE_NO_WARNING (t))
18675 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18676
18677 RETURN (r);
18678 }
18679
18680 case ARROW_EXPR:
18681 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18682 args, complain, in_decl);
18683 /* Remember that there was a reference to this entity. */
18684 if (DECL_P (op1)
18685 && !mark_used (op1, complain) && !(complain & tf_error))
18686 RETURN (error_mark_node);
18687 RETURN (build_x_arrow (input_location, op1, complain));
18688
18689 case NEW_EXPR:
18690 {
18691 tree placement = RECUR (TREE_OPERAND (t, 0));
18692 tree init = RECUR (TREE_OPERAND (t, 3));
18693 vec<tree, va_gc> *placement_vec;
18694 vec<tree, va_gc> *init_vec;
18695 tree ret;
18696
18697 if (placement == NULL_TREE)
18698 placement_vec = NULL;
18699 else
18700 {
18701 placement_vec = make_tree_vector ();
18702 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18703 vec_safe_push (placement_vec, TREE_VALUE (placement));
18704 }
18705
18706 /* If there was an initializer in the original tree, but it
18707 instantiated to an empty list, then we should pass a
18708 non-NULL empty vector to tell build_new that it was an
18709 empty initializer() rather than no initializer. This can
18710 only happen when the initializer is a pack expansion whose
18711 parameter packs are of length zero. */
18712 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18713 init_vec = NULL;
18714 else
18715 {
18716 init_vec = make_tree_vector ();
18717 if (init == void_node)
18718 gcc_assert (init_vec != NULL);
18719 else
18720 {
18721 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18722 vec_safe_push (init_vec, TREE_VALUE (init));
18723 }
18724 }
18725
18726 /* Avoid passing an enclosing decl to valid_array_size_p. */
18727 in_decl = NULL_TREE;
18728
18729 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18730 tree op2 = RECUR (TREE_OPERAND (t, 2));
18731 ret = build_new (&placement_vec, op1, op2, &init_vec,
18732 NEW_EXPR_USE_GLOBAL (t),
18733 complain);
18734
18735 if (placement_vec != NULL)
18736 release_tree_vector (placement_vec);
18737 if (init_vec != NULL)
18738 release_tree_vector (init_vec);
18739
18740 RETURN (ret);
18741 }
18742
18743 case DELETE_EXPR:
18744 {
18745 tree op0 = RECUR (TREE_OPERAND (t, 0));
18746 tree op1 = RECUR (TREE_OPERAND (t, 1));
18747 RETURN (delete_sanity (op0, op1,
18748 DELETE_EXPR_USE_VEC (t),
18749 DELETE_EXPR_USE_GLOBAL (t),
18750 complain));
18751 }
18752
18753 case COMPOUND_EXPR:
18754 {
18755 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18756 complain & ~tf_decltype, in_decl,
18757 /*function_p=*/false,
18758 integral_constant_expression_p);
18759 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18760 op0,
18761 RECUR (TREE_OPERAND (t, 1)),
18762 complain|decltype_flag));
18763 }
18764
18765 case CALL_EXPR:
18766 {
18767 tree function;
18768 unsigned int nargs, i;
18769 bool qualified_p;
18770 bool koenig_p;
18771 tree ret;
18772
18773 function = CALL_EXPR_FN (t);
18774 /* Internal function with no arguments. */
18775 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18776 RETURN (t);
18777
18778 /* When we parsed the expression, we determined whether or
18779 not Koenig lookup should be performed. */
18780 koenig_p = KOENIG_LOOKUP_P (t);
18781 if (function == NULL_TREE)
18782 {
18783 koenig_p = false;
18784 qualified_p = false;
18785 }
18786 else if (TREE_CODE (function) == SCOPE_REF)
18787 {
18788 qualified_p = true;
18789 function = tsubst_qualified_id (function, args, complain, in_decl,
18790 /*done=*/false,
18791 /*address_p=*/false);
18792 }
18793 else if (koenig_p && identifier_p (function))
18794 {
18795 /* Do nothing; calling tsubst_copy_and_build on an identifier
18796 would incorrectly perform unqualified lookup again.
18797
18798 Note that we can also have an IDENTIFIER_NODE if the earlier
18799 unqualified lookup found a member function; in that case
18800 koenig_p will be false and we do want to do the lookup
18801 again to find the instantiated member function.
18802
18803 FIXME but doing that causes c++/15272, so we need to stop
18804 using IDENTIFIER_NODE in that situation. */
18805 qualified_p = false;
18806 }
18807 else
18808 {
18809 if (TREE_CODE (function) == COMPONENT_REF)
18810 {
18811 tree op = TREE_OPERAND (function, 1);
18812
18813 qualified_p = (TREE_CODE (op) == SCOPE_REF
18814 || (BASELINK_P (op)
18815 && BASELINK_QUALIFIED_P (op)));
18816 }
18817 else
18818 qualified_p = false;
18819
18820 if (TREE_CODE (function) == ADDR_EXPR
18821 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18822 /* Avoid error about taking the address of a constructor. */
18823 function = TREE_OPERAND (function, 0);
18824
18825 function = tsubst_copy_and_build (function, args, complain,
18826 in_decl,
18827 !qualified_p,
18828 integral_constant_expression_p);
18829
18830 if (BASELINK_P (function))
18831 qualified_p = true;
18832 }
18833
18834 nargs = call_expr_nargs (t);
18835 releasing_vec call_args;
18836 for (i = 0; i < nargs; ++i)
18837 {
18838 tree arg = CALL_EXPR_ARG (t, i);
18839
18840 if (!PACK_EXPANSION_P (arg))
18841 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18842 else
18843 {
18844 /* Expand the pack expansion and push each entry onto
18845 CALL_ARGS. */
18846 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18847 if (TREE_CODE (arg) == TREE_VEC)
18848 {
18849 unsigned int len, j;
18850
18851 len = TREE_VEC_LENGTH (arg);
18852 for (j = 0; j < len; ++j)
18853 {
18854 tree value = TREE_VEC_ELT (arg, j);
18855 if (value != NULL_TREE)
18856 value = convert_from_reference (value);
18857 vec_safe_push (call_args, value);
18858 }
18859 }
18860 else
18861 {
18862 /* A partial substitution. Add one entry. */
18863 vec_safe_push (call_args, arg);
18864 }
18865 }
18866 }
18867
18868 /* Stripped-down processing for a call in a thunk. Specifically, in
18869 the thunk template for a generic lambda. */
18870 if (CALL_FROM_THUNK_P (t))
18871 {
18872 /* Now that we've expanded any packs, the number of call args
18873 might be different. */
18874 unsigned int cargs = call_args->length ();
18875 tree thisarg = NULL_TREE;
18876 if (TREE_CODE (function) == COMPONENT_REF)
18877 {
18878 thisarg = TREE_OPERAND (function, 0);
18879 if (TREE_CODE (thisarg) == INDIRECT_REF)
18880 thisarg = TREE_OPERAND (thisarg, 0);
18881 function = TREE_OPERAND (function, 1);
18882 if (TREE_CODE (function) == BASELINK)
18883 function = BASELINK_FUNCTIONS (function);
18884 }
18885 /* We aren't going to do normal overload resolution, so force the
18886 template-id to resolve. */
18887 function = resolve_nondeduced_context (function, complain);
18888 for (unsigned i = 0; i < cargs; ++i)
18889 {
18890 /* In a thunk, pass through args directly, without any
18891 conversions. */
18892 tree arg = (*call_args)[i];
18893 while (TREE_CODE (arg) != PARM_DECL)
18894 arg = TREE_OPERAND (arg, 0);
18895 (*call_args)[i] = arg;
18896 }
18897 if (thisarg)
18898 {
18899 /* If there are no other args, just push 'this'. */
18900 if (cargs == 0)
18901 vec_safe_push (call_args, thisarg);
18902 else
18903 {
18904 /* Otherwise, shift the other args over to make room. */
18905 tree last = (*call_args)[cargs - 1];
18906 vec_safe_push (call_args, last);
18907 for (int i = cargs - 1; i > 0; --i)
18908 (*call_args)[i] = (*call_args)[i - 1];
18909 (*call_args)[0] = thisarg;
18910 }
18911 }
18912 ret = build_call_a (function, call_args->length (),
18913 call_args->address ());
18914 /* The thunk location is not interesting. */
18915 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18916 CALL_FROM_THUNK_P (ret) = true;
18917 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18918 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18919
18920 RETURN (ret);
18921 }
18922
18923 /* We do not perform argument-dependent lookup if normal
18924 lookup finds a non-function, in accordance with the
18925 expected resolution of DR 218. */
18926 if (koenig_p
18927 && ((is_overloaded_fn (function)
18928 /* If lookup found a member function, the Koenig lookup is
18929 not appropriate, even if an unqualified-name was used
18930 to denote the function. */
18931 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18932 || identifier_p (function))
18933 /* Only do this when substitution turns a dependent call
18934 into a non-dependent call. */
18935 && type_dependent_expression_p_push (t)
18936 && !any_type_dependent_arguments_p (call_args))
18937 function = perform_koenig_lookup (function, call_args, tf_none);
18938
18939 if (function != NULL_TREE
18940 && identifier_p (function)
18941 && !any_type_dependent_arguments_p (call_args))
18942 {
18943 if (koenig_p && (complain & tf_warning_or_error))
18944 {
18945 /* For backwards compatibility and good diagnostics, try
18946 the unqualified lookup again if we aren't in SFINAE
18947 context. */
18948 tree unq = (tsubst_copy_and_build
18949 (function, args, complain, in_decl, true,
18950 integral_constant_expression_p));
18951 if (unq == error_mark_node)
18952 RETURN (error_mark_node);
18953
18954 if (unq != function)
18955 {
18956 /* In a lambda fn, we have to be careful to not
18957 introduce new this captures. Legacy code can't
18958 be using lambdas anyway, so it's ok to be
18959 stricter. */
18960 bool in_lambda = (current_class_type
18961 && LAMBDA_TYPE_P (current_class_type));
18962 char const *const msg
18963 = G_("%qD was not declared in this scope, "
18964 "and no declarations were found by "
18965 "argument-dependent lookup at the point "
18966 "of instantiation");
18967
18968 bool diag = true;
18969 if (in_lambda)
18970 error_at (cp_expr_loc_or_loc (t, input_location),
18971 msg, function);
18972 else
18973 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18974 msg, function);
18975 if (diag)
18976 {
18977 tree fn = unq;
18978
18979 if (INDIRECT_REF_P (fn))
18980 fn = TREE_OPERAND (fn, 0);
18981 if (is_overloaded_fn (fn))
18982 fn = get_first_fn (fn);
18983
18984 if (!DECL_P (fn))
18985 /* Can't say anything more. */;
18986 else if (DECL_CLASS_SCOPE_P (fn))
18987 {
18988 location_t loc = cp_expr_loc_or_loc (t,
18989 input_location);
18990 inform (loc,
18991 "declarations in dependent base %qT are "
18992 "not found by unqualified lookup",
18993 DECL_CLASS_CONTEXT (fn));
18994 if (current_class_ptr)
18995 inform (loc,
18996 "use %<this->%D%> instead", function);
18997 else
18998 inform (loc,
18999 "use %<%T::%D%> instead",
19000 current_class_name, function);
19001 }
19002 else
19003 inform (DECL_SOURCE_LOCATION (fn),
19004 "%qD declared here, later in the "
19005 "translation unit", fn);
19006 if (in_lambda)
19007 RETURN (error_mark_node);
19008 }
19009
19010 function = unq;
19011 }
19012 }
19013 if (identifier_p (function))
19014 {
19015 if (complain & tf_error)
19016 unqualified_name_lookup_error (function);
19017 RETURN (error_mark_node);
19018 }
19019 }
19020
19021 /* Remember that there was a reference to this entity. */
19022 if (function != NULL_TREE
19023 && DECL_P (function)
19024 && !mark_used (function, complain) && !(complain & tf_error))
19025 RETURN (error_mark_node);
19026
19027 /* Put back tf_decltype for the actual call. */
19028 complain |= decltype_flag;
19029
19030 if (function == NULL_TREE)
19031 switch (CALL_EXPR_IFN (t))
19032 {
19033 case IFN_LAUNDER:
19034 gcc_assert (nargs == 1);
19035 if (vec_safe_length (call_args) != 1)
19036 {
19037 error_at (cp_expr_loc_or_loc (t, input_location),
19038 "wrong number of arguments to "
19039 "%<__builtin_launder%>");
19040 ret = error_mark_node;
19041 }
19042 else
19043 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
19044 input_location),
19045 (*call_args)[0], complain);
19046 break;
19047
19048 case IFN_VEC_CONVERT:
19049 gcc_assert (nargs == 1);
19050 if (vec_safe_length (call_args) != 1)
19051 {
19052 error_at (cp_expr_loc_or_loc (t, input_location),
19053 "wrong number of arguments to "
19054 "%<__builtin_convertvector%>");
19055 ret = error_mark_node;
19056 break;
19057 }
19058 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19059 tsubst (TREE_TYPE (t), args,
19060 complain, in_decl),
19061 complain);
19062 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19063 RETURN (ret);
19064 break;
19065
19066 default:
19067 /* Unsupported internal function with arguments. */
19068 gcc_unreachable ();
19069 }
19070 else if (TREE_CODE (function) == OFFSET_REF
19071 || TREE_CODE (function) == DOTSTAR_EXPR
19072 || TREE_CODE (function) == MEMBER_REF)
19073 ret = build_offset_ref_call_from_tree (function, &call_args,
19074 complain);
19075 else if (TREE_CODE (function) == COMPONENT_REF)
19076 {
19077 tree instance = TREE_OPERAND (function, 0);
19078 tree fn = TREE_OPERAND (function, 1);
19079
19080 if (processing_template_decl
19081 && (type_dependent_expression_p (instance)
19082 || (!BASELINK_P (fn)
19083 && TREE_CODE (fn) != FIELD_DECL)
19084 || type_dependent_expression_p (fn)
19085 || any_type_dependent_arguments_p (call_args)))
19086 ret = build_min_nt_call_vec (function, call_args);
19087 else if (!BASELINK_P (fn))
19088 ret = finish_call_expr (function, &call_args,
19089 /*disallow_virtual=*/false,
19090 /*koenig_p=*/false,
19091 complain);
19092 else
19093 ret = (build_new_method_call
19094 (instance, fn,
19095 &call_args, NULL_TREE,
19096 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19097 /*fn_p=*/NULL,
19098 complain));
19099 }
19100 else
19101 ret = finish_call_expr (function, &call_args,
19102 /*disallow_virtual=*/qualified_p,
19103 koenig_p,
19104 complain);
19105
19106 if (ret != error_mark_node)
19107 {
19108 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19109 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19110 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19111 if (op || ord || rev)
19112 {
19113 function = extract_call_expr (ret);
19114 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19115 CALL_EXPR_ORDERED_ARGS (function) = ord;
19116 CALL_EXPR_REVERSE_ARGS (function) = rev;
19117 }
19118 }
19119
19120 RETURN (ret);
19121 }
19122
19123 case COND_EXPR:
19124 {
19125 tree cond = RECUR (TREE_OPERAND (t, 0));
19126 cond = mark_rvalue_use (cond);
19127 tree folded_cond = fold_non_dependent_expr (cond, complain);
19128 tree exp1, exp2;
19129
19130 if (TREE_CODE (folded_cond) == INTEGER_CST)
19131 {
19132 if (integer_zerop (folded_cond))
19133 {
19134 ++c_inhibit_evaluation_warnings;
19135 exp1 = RECUR (TREE_OPERAND (t, 1));
19136 --c_inhibit_evaluation_warnings;
19137 exp2 = RECUR (TREE_OPERAND (t, 2));
19138 }
19139 else
19140 {
19141 exp1 = RECUR (TREE_OPERAND (t, 1));
19142 ++c_inhibit_evaluation_warnings;
19143 exp2 = RECUR (TREE_OPERAND (t, 2));
19144 --c_inhibit_evaluation_warnings;
19145 }
19146 cond = folded_cond;
19147 }
19148 else
19149 {
19150 exp1 = RECUR (TREE_OPERAND (t, 1));
19151 exp2 = RECUR (TREE_OPERAND (t, 2));
19152 }
19153
19154 warning_sentinel s(warn_duplicated_branches);
19155 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19156 cond, exp1, exp2, complain));
19157 }
19158
19159 case PSEUDO_DTOR_EXPR:
19160 {
19161 tree op0 = RECUR (TREE_OPERAND (t, 0));
19162 tree op1 = RECUR (TREE_OPERAND (t, 1));
19163 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19164 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19165 input_location));
19166 }
19167
19168 case TREE_LIST:
19169 {
19170 tree purpose, value, chain;
19171
19172 if (t == void_list_node)
19173 RETURN (t);
19174
19175 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19176 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19177 {
19178 /* We have pack expansions, so expand those and
19179 create a new list out of it. */
19180 tree purposevec = NULL_TREE;
19181 tree valuevec = NULL_TREE;
19182 tree chain;
19183 int i, len = -1;
19184
19185 /* Expand the argument expressions. */
19186 if (TREE_PURPOSE (t))
19187 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19188 complain, in_decl);
19189 if (TREE_VALUE (t))
19190 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19191 complain, in_decl);
19192
19193 /* Build the rest of the list. */
19194 chain = TREE_CHAIN (t);
19195 if (chain && chain != void_type_node)
19196 chain = RECUR (chain);
19197
19198 /* Determine the number of arguments. */
19199 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19200 {
19201 len = TREE_VEC_LENGTH (purposevec);
19202 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19203 }
19204 else if (TREE_CODE (valuevec) == TREE_VEC)
19205 len = TREE_VEC_LENGTH (valuevec);
19206 else
19207 {
19208 /* Since we only performed a partial substitution into
19209 the argument pack, we only RETURN (a single list
19210 node. */
19211 if (purposevec == TREE_PURPOSE (t)
19212 && valuevec == TREE_VALUE (t)
19213 && chain == TREE_CHAIN (t))
19214 RETURN (t);
19215
19216 RETURN (tree_cons (purposevec, valuevec, chain));
19217 }
19218
19219 /* Convert the argument vectors into a TREE_LIST */
19220 i = len;
19221 while (i > 0)
19222 {
19223 /* Grab the Ith values. */
19224 i--;
19225 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19226 : NULL_TREE;
19227 value
19228 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19229 : NULL_TREE;
19230
19231 /* Build the list (backwards). */
19232 chain = tree_cons (purpose, value, chain);
19233 }
19234
19235 RETURN (chain);
19236 }
19237
19238 purpose = TREE_PURPOSE (t);
19239 if (purpose)
19240 purpose = RECUR (purpose);
19241 value = TREE_VALUE (t);
19242 if (value)
19243 value = RECUR (value);
19244 chain = TREE_CHAIN (t);
19245 if (chain && chain != void_type_node)
19246 chain = RECUR (chain);
19247 if (purpose == TREE_PURPOSE (t)
19248 && value == TREE_VALUE (t)
19249 && chain == TREE_CHAIN (t))
19250 RETURN (t);
19251 RETURN (tree_cons (purpose, value, chain));
19252 }
19253
19254 case COMPONENT_REF:
19255 {
19256 tree object;
19257 tree object_type;
19258 tree member;
19259 tree r;
19260
19261 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19262 args, complain, in_decl);
19263 /* Remember that there was a reference to this entity. */
19264 if (DECL_P (object)
19265 && !mark_used (object, complain) && !(complain & tf_error))
19266 RETURN (error_mark_node);
19267 object_type = TREE_TYPE (object);
19268
19269 member = TREE_OPERAND (t, 1);
19270 if (BASELINK_P (member))
19271 member = tsubst_baselink (member,
19272 non_reference (TREE_TYPE (object)),
19273 args, complain, in_decl);
19274 else
19275 member = tsubst_copy (member, args, complain, in_decl);
19276 if (member == error_mark_node)
19277 RETURN (error_mark_node);
19278
19279 if (TREE_CODE (member) == FIELD_DECL)
19280 {
19281 r = finish_non_static_data_member (member, object, NULL_TREE);
19282 if (TREE_CODE (r) == COMPONENT_REF)
19283 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19284 RETURN (r);
19285 }
19286 else if (type_dependent_expression_p (object))
19287 /* We can't do much here. */;
19288 else if (!CLASS_TYPE_P (object_type))
19289 {
19290 if (scalarish_type_p (object_type))
19291 {
19292 tree s = NULL_TREE;
19293 tree dtor = member;
19294
19295 if (TREE_CODE (dtor) == SCOPE_REF)
19296 {
19297 s = TREE_OPERAND (dtor, 0);
19298 dtor = TREE_OPERAND (dtor, 1);
19299 }
19300 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19301 {
19302 dtor = TREE_OPERAND (dtor, 0);
19303 if (TYPE_P (dtor))
19304 RETURN (finish_pseudo_destructor_expr
19305 (object, s, dtor, input_location));
19306 }
19307 }
19308 }
19309 else if (TREE_CODE (member) == SCOPE_REF
19310 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19311 {
19312 /* Lookup the template functions now that we know what the
19313 scope is. */
19314 tree scope = TREE_OPERAND (member, 0);
19315 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19316 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19317 member = lookup_qualified_name (scope, tmpl,
19318 /*is_type_p=*/false,
19319 /*complain=*/false);
19320 if (BASELINK_P (member))
19321 {
19322 BASELINK_FUNCTIONS (member)
19323 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19324 args);
19325 member = (adjust_result_of_qualified_name_lookup
19326 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19327 object_type));
19328 }
19329 else
19330 {
19331 qualified_name_lookup_error (scope, tmpl, member,
19332 input_location);
19333 RETURN (error_mark_node);
19334 }
19335 }
19336 else if (TREE_CODE (member) == SCOPE_REF
19337 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19338 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19339 {
19340 if (complain & tf_error)
19341 {
19342 if (TYPE_P (TREE_OPERAND (member, 0)))
19343 error ("%qT is not a class or namespace",
19344 TREE_OPERAND (member, 0));
19345 else
19346 error ("%qD is not a class or namespace",
19347 TREE_OPERAND (member, 0));
19348 }
19349 RETURN (error_mark_node);
19350 }
19351
19352 r = finish_class_member_access_expr (object, member,
19353 /*template_p=*/false,
19354 complain);
19355 if (TREE_CODE (r) == COMPONENT_REF)
19356 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19357 RETURN (r);
19358 }
19359
19360 case THROW_EXPR:
19361 RETURN (build_throw
19362 (RECUR (TREE_OPERAND (t, 0))));
19363
19364 case CONSTRUCTOR:
19365 {
19366 vec<constructor_elt, va_gc> *n;
19367 constructor_elt *ce;
19368 unsigned HOST_WIDE_INT idx;
19369 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19370 bool process_index_p;
19371 int newlen;
19372 bool need_copy_p = false;
19373 tree r;
19374
19375 if (type == error_mark_node)
19376 RETURN (error_mark_node);
19377
19378 /* We do not want to process the index of aggregate
19379 initializers as they are identifier nodes which will be
19380 looked up by digest_init. */
19381 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19382
19383 if (null_member_pointer_value_p (t))
19384 {
19385 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19386 RETURN (t);
19387 }
19388
19389 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19390 newlen = vec_safe_length (n);
19391 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19392 {
19393 if (ce->index && process_index_p
19394 /* An identifier index is looked up in the type
19395 being initialized, not the current scope. */
19396 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19397 ce->index = RECUR (ce->index);
19398
19399 if (PACK_EXPANSION_P (ce->value))
19400 {
19401 /* Substitute into the pack expansion. */
19402 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19403 in_decl);
19404
19405 if (ce->value == error_mark_node
19406 || PACK_EXPANSION_P (ce->value))
19407 ;
19408 else if (TREE_VEC_LENGTH (ce->value) == 1)
19409 /* Just move the argument into place. */
19410 ce->value = TREE_VEC_ELT (ce->value, 0);
19411 else
19412 {
19413 /* Update the length of the final CONSTRUCTOR
19414 arguments vector, and note that we will need to
19415 copy.*/
19416 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19417 need_copy_p = true;
19418 }
19419 }
19420 else
19421 ce->value = RECUR (ce->value);
19422 }
19423
19424 if (need_copy_p)
19425 {
19426 vec<constructor_elt, va_gc> *old_n = n;
19427
19428 vec_alloc (n, newlen);
19429 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19430 {
19431 if (TREE_CODE (ce->value) == TREE_VEC)
19432 {
19433 int i, len = TREE_VEC_LENGTH (ce->value);
19434 for (i = 0; i < len; ++i)
19435 CONSTRUCTOR_APPEND_ELT (n, 0,
19436 TREE_VEC_ELT (ce->value, i));
19437 }
19438 else
19439 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19440 }
19441 }
19442
19443 r = build_constructor (init_list_type_node, n);
19444 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19445 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19446 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19447
19448 if (TREE_HAS_CONSTRUCTOR (t))
19449 {
19450 fcl_t cl = fcl_functional;
19451 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19452 cl = fcl_c99;
19453 RETURN (finish_compound_literal (type, r, complain, cl));
19454 }
19455
19456 TREE_TYPE (r) = type;
19457 RETURN (r);
19458 }
19459
19460 case TYPEID_EXPR:
19461 {
19462 tree operand_0 = TREE_OPERAND (t, 0);
19463 if (TYPE_P (operand_0))
19464 {
19465 operand_0 = tsubst (operand_0, args, complain, in_decl);
19466 RETURN (get_typeid (operand_0, complain));
19467 }
19468 else
19469 {
19470 operand_0 = RECUR (operand_0);
19471 RETURN (build_typeid (operand_0, complain));
19472 }
19473 }
19474
19475 case VAR_DECL:
19476 if (!args)
19477 RETURN (t);
19478 /* Fall through */
19479
19480 case PARM_DECL:
19481 {
19482 tree r = tsubst_copy (t, args, complain, in_decl);
19483 /* ??? We're doing a subset of finish_id_expression here. */
19484 if (tree wrap = maybe_get_tls_wrapper_call (r))
19485 /* Replace an evaluated use of the thread_local variable with
19486 a call to its wrapper. */
19487 r = wrap;
19488 else if (outer_automatic_var_p (r))
19489 r = process_outer_var_ref (r, complain);
19490
19491 if (!TYPE_REF_P (TREE_TYPE (t)))
19492 /* If the original type was a reference, we'll be wrapped in
19493 the appropriate INDIRECT_REF. */
19494 r = convert_from_reference (r);
19495 RETURN (r);
19496 }
19497
19498 case VA_ARG_EXPR:
19499 {
19500 tree op0 = RECUR (TREE_OPERAND (t, 0));
19501 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19502 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19503 }
19504
19505 case OFFSETOF_EXPR:
19506 {
19507 tree object_ptr
19508 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19509 in_decl, /*function_p=*/false,
19510 /*integral_constant_expression_p=*/false);
19511 RETURN (finish_offsetof (object_ptr,
19512 RECUR (TREE_OPERAND (t, 0)),
19513 EXPR_LOCATION (t)));
19514 }
19515
19516 case ADDRESSOF_EXPR:
19517 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19518 RECUR (TREE_OPERAND (t, 0)), complain));
19519
19520 case TRAIT_EXPR:
19521 {
19522 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19523 complain, in_decl);
19524
19525 tree type2 = TRAIT_EXPR_TYPE2 (t);
19526 if (type2 && TREE_CODE (type2) == TREE_LIST)
19527 type2 = RECUR (type2);
19528 else if (type2)
19529 type2 = tsubst (type2, args, complain, in_decl);
19530
19531 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19532 }
19533
19534 case STMT_EXPR:
19535 {
19536 tree old_stmt_expr = cur_stmt_expr;
19537 tree stmt_expr = begin_stmt_expr ();
19538
19539 cur_stmt_expr = stmt_expr;
19540 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19541 integral_constant_expression_p);
19542 stmt_expr = finish_stmt_expr (stmt_expr, false);
19543 cur_stmt_expr = old_stmt_expr;
19544
19545 /* If the resulting list of expression statement is empty,
19546 fold it further into void_node. */
19547 if (empty_expr_stmt_p (stmt_expr))
19548 stmt_expr = void_node;
19549
19550 RETURN (stmt_expr);
19551 }
19552
19553 case LAMBDA_EXPR:
19554 {
19555 if (complain & tf_partial)
19556 {
19557 /* We don't have a full set of template arguments yet; don't touch
19558 the lambda at all. */
19559 gcc_assert (processing_template_decl);
19560 return t;
19561 }
19562 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19563
19564 RETURN (build_lambda_object (r));
19565 }
19566
19567 case TARGET_EXPR:
19568 /* We can get here for a constant initializer of non-dependent type.
19569 FIXME stop folding in cp_parser_initializer_clause. */
19570 {
19571 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19572 complain);
19573 RETURN (r);
19574 }
19575
19576 case TRANSACTION_EXPR:
19577 RETURN (tsubst_expr(t, args, complain, in_decl,
19578 integral_constant_expression_p));
19579
19580 case PAREN_EXPR:
19581 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19582
19583 case VEC_PERM_EXPR:
19584 {
19585 tree op0 = RECUR (TREE_OPERAND (t, 0));
19586 tree op1 = RECUR (TREE_OPERAND (t, 1));
19587 tree op2 = RECUR (TREE_OPERAND (t, 2));
19588 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19589 complain));
19590 }
19591
19592 case REQUIRES_EXPR:
19593 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19594
19595 case RANGE_EXPR:
19596 /* No need to substitute further, a RANGE_EXPR will always be built
19597 with constant operands. */
19598 RETURN (t);
19599
19600 case NON_LVALUE_EXPR:
19601 case VIEW_CONVERT_EXPR:
19602 if (location_wrapper_p (t))
19603 /* We need to do this here as well as in tsubst_copy so we get the
19604 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19605 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19606 EXPR_LOCATION (t)));
19607 /* fallthrough. */
19608
19609 default:
19610 /* Handle Objective-C++ constructs, if appropriate. */
19611 {
19612 tree subst
19613 = objcp_tsubst_copy_and_build (t, args, complain,
19614 in_decl, /*function_p=*/false);
19615 if (subst)
19616 RETURN (subst);
19617 }
19618 RETURN (tsubst_copy (t, args, complain, in_decl));
19619 }
19620
19621 #undef RECUR
19622 #undef RETURN
19623 out:
19624 input_location = loc;
19625 return retval;
19626 }
19627
19628 /* Verify that the instantiated ARGS are valid. For type arguments,
19629 make sure that the type's linkage is ok. For non-type arguments,
19630 make sure they are constants if they are integral or enumerations.
19631 Emit an error under control of COMPLAIN, and return TRUE on error. */
19632
19633 static bool
19634 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19635 {
19636 if (dependent_template_arg_p (t))
19637 return false;
19638 if (ARGUMENT_PACK_P (t))
19639 {
19640 tree vec = ARGUMENT_PACK_ARGS (t);
19641 int len = TREE_VEC_LENGTH (vec);
19642 bool result = false;
19643 int i;
19644
19645 for (i = 0; i < len; ++i)
19646 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19647 result = true;
19648 return result;
19649 }
19650 else if (TYPE_P (t))
19651 {
19652 /* [basic.link]: A name with no linkage (notably, the name
19653 of a class or enumeration declared in a local scope)
19654 shall not be used to declare an entity with linkage.
19655 This implies that names with no linkage cannot be used as
19656 template arguments
19657
19658 DR 757 relaxes this restriction for C++0x. */
19659 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19660 : no_linkage_check (t, /*relaxed_p=*/false));
19661
19662 if (nt)
19663 {
19664 /* DR 488 makes use of a type with no linkage cause
19665 type deduction to fail. */
19666 if (complain & tf_error)
19667 {
19668 if (TYPE_UNNAMED_P (nt))
19669 error ("%qT is/uses unnamed type", t);
19670 else
19671 error ("template argument for %qD uses local type %qT",
19672 tmpl, t);
19673 }
19674 return true;
19675 }
19676 /* In order to avoid all sorts of complications, we do not
19677 allow variably-modified types as template arguments. */
19678 else if (variably_modified_type_p (t, NULL_TREE))
19679 {
19680 if (complain & tf_error)
19681 error ("%qT is a variably modified type", t);
19682 return true;
19683 }
19684 }
19685 /* Class template and alias template arguments should be OK. */
19686 else if (DECL_TYPE_TEMPLATE_P (t))
19687 ;
19688 /* A non-type argument of integral or enumerated type must be a
19689 constant. */
19690 else if (TREE_TYPE (t)
19691 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19692 && !REFERENCE_REF_P (t)
19693 && !TREE_CONSTANT (t))
19694 {
19695 if (complain & tf_error)
19696 error ("integral expression %qE is not constant", t);
19697 return true;
19698 }
19699 return false;
19700 }
19701
19702 static bool
19703 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19704 {
19705 int ix, len = DECL_NTPARMS (tmpl);
19706 bool result = false;
19707
19708 for (ix = 0; ix != len; ix++)
19709 {
19710 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19711 result = true;
19712 }
19713 if (result && (complain & tf_error))
19714 error (" trying to instantiate %qD", tmpl);
19715 return result;
19716 }
19717
19718 /* We're out of SFINAE context now, so generate diagnostics for the access
19719 errors we saw earlier when instantiating D from TMPL and ARGS. */
19720
19721 static void
19722 recheck_decl_substitution (tree d, tree tmpl, tree args)
19723 {
19724 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19725 tree type = TREE_TYPE (pattern);
19726 location_t loc = input_location;
19727
19728 push_access_scope (d);
19729 push_deferring_access_checks (dk_no_deferred);
19730 input_location = DECL_SOURCE_LOCATION (pattern);
19731 tsubst (type, args, tf_warning_or_error, d);
19732 input_location = loc;
19733 pop_deferring_access_checks ();
19734 pop_access_scope (d);
19735 }
19736
19737 /* Instantiate the indicated variable, function, or alias template TMPL with
19738 the template arguments in TARG_PTR. */
19739
19740 static tree
19741 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19742 {
19743 tree targ_ptr = orig_args;
19744 tree fndecl;
19745 tree gen_tmpl;
19746 tree spec;
19747 bool access_ok = true;
19748
19749 if (tmpl == error_mark_node)
19750 return error_mark_node;
19751
19752 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19753
19754 /* If this function is a clone, handle it specially. */
19755 if (DECL_CLONED_FUNCTION_P (tmpl))
19756 {
19757 tree spec;
19758 tree clone;
19759
19760 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19761 DECL_CLONED_FUNCTION. */
19762 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19763 targ_ptr, complain);
19764 if (spec == error_mark_node)
19765 return error_mark_node;
19766
19767 /* Look for the clone. */
19768 FOR_EACH_CLONE (clone, spec)
19769 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19770 return clone;
19771 /* We should always have found the clone by now. */
19772 gcc_unreachable ();
19773 return NULL_TREE;
19774 }
19775
19776 if (targ_ptr == error_mark_node)
19777 return error_mark_node;
19778
19779 /* Check to see if we already have this specialization. */
19780 gen_tmpl = most_general_template (tmpl);
19781 if (TMPL_ARGS_DEPTH (targ_ptr)
19782 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19783 /* targ_ptr only has the innermost template args, so add the outer ones
19784 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19785 the case of a non-dependent call within a template definition). */
19786 targ_ptr = (add_outermost_template_args
19787 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19788 targ_ptr));
19789
19790 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19791 but it doesn't seem to be on the hot path. */
19792 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19793
19794 gcc_assert (tmpl == gen_tmpl
19795 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19796 == spec)
19797 || fndecl == NULL_TREE);
19798
19799 if (spec != NULL_TREE)
19800 {
19801 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19802 {
19803 if (complain & tf_error)
19804 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19805 return error_mark_node;
19806 }
19807 return spec;
19808 }
19809
19810 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19811 complain))
19812 return error_mark_node;
19813
19814 /* We are building a FUNCTION_DECL, during which the access of its
19815 parameters and return types have to be checked. However this
19816 FUNCTION_DECL which is the desired context for access checking
19817 is not built yet. We solve this chicken-and-egg problem by
19818 deferring all checks until we have the FUNCTION_DECL. */
19819 push_deferring_access_checks (dk_deferred);
19820
19821 /* Instantiation of the function happens in the context of the function
19822 template, not the context of the overload resolution we're doing. */
19823 push_to_top_level ();
19824 /* If there are dependent arguments, e.g. because we're doing partial
19825 ordering, make sure processing_template_decl stays set. */
19826 if (uses_template_parms (targ_ptr))
19827 ++processing_template_decl;
19828 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19829 {
19830 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19831 complain, gen_tmpl, true);
19832 push_nested_class (ctx);
19833 }
19834
19835 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19836
19837 fndecl = NULL_TREE;
19838 if (VAR_P (pattern))
19839 {
19840 /* We need to determine if we're using a partial or explicit
19841 specialization now, because the type of the variable could be
19842 different. */
19843 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19844 tree elt = most_specialized_partial_spec (tid, complain);
19845 if (elt == error_mark_node)
19846 pattern = error_mark_node;
19847 else if (elt)
19848 {
19849 tree partial_tmpl = TREE_VALUE (elt);
19850 tree partial_args = TREE_PURPOSE (elt);
19851 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19852 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19853 }
19854 }
19855
19856 /* Substitute template parameters to obtain the specialization. */
19857 if (fndecl == NULL_TREE)
19858 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19859 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19860 pop_nested_class ();
19861 pop_from_top_level ();
19862
19863 if (fndecl == error_mark_node)
19864 {
19865 pop_deferring_access_checks ();
19866 return error_mark_node;
19867 }
19868
19869 /* The DECL_TI_TEMPLATE should always be the immediate parent
19870 template, not the most general template. */
19871 DECL_TI_TEMPLATE (fndecl) = tmpl;
19872 DECL_TI_ARGS (fndecl) = targ_ptr;
19873
19874 /* Now we know the specialization, compute access previously
19875 deferred. Do no access control for inheriting constructors,
19876 as we already checked access for the inherited constructor. */
19877 if (!(flag_new_inheriting_ctors
19878 && DECL_INHERITED_CTOR (fndecl)))
19879 {
19880 push_access_scope (fndecl);
19881 if (!perform_deferred_access_checks (complain))
19882 access_ok = false;
19883 pop_access_scope (fndecl);
19884 }
19885 pop_deferring_access_checks ();
19886
19887 /* If we've just instantiated the main entry point for a function,
19888 instantiate all the alternate entry points as well. We do this
19889 by cloning the instantiation of the main entry point, not by
19890 instantiating the template clones. */
19891 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19892 clone_function_decl (fndecl, /*update_methods=*/false);
19893
19894 if (!access_ok)
19895 {
19896 if (!(complain & tf_error))
19897 {
19898 /* Remember to reinstantiate when we're out of SFINAE so the user
19899 can see the errors. */
19900 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19901 }
19902 return error_mark_node;
19903 }
19904 return fndecl;
19905 }
19906
19907 /* Wrapper for instantiate_template_1. */
19908
19909 tree
19910 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19911 {
19912 tree ret;
19913 timevar_push (TV_TEMPLATE_INST);
19914 ret = instantiate_template_1 (tmpl, orig_args, complain);
19915 timevar_pop (TV_TEMPLATE_INST);
19916 return ret;
19917 }
19918
19919 /* Instantiate the alias template TMPL with ARGS. Also push a template
19920 instantiation level, which instantiate_template doesn't do because
19921 functions and variables have sufficient context established by the
19922 callers. */
19923
19924 static tree
19925 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19926 {
19927 if (tmpl == error_mark_node || args == error_mark_node)
19928 return error_mark_node;
19929 if (!push_tinst_level (tmpl, args))
19930 return error_mark_node;
19931
19932 args =
19933 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19934 args, tmpl, complain,
19935 /*require_all_args=*/true,
19936 /*use_default_args=*/true);
19937
19938 tree r = instantiate_template (tmpl, args, complain);
19939 pop_tinst_level ();
19940
19941 return r;
19942 }
19943
19944 /* PARM is a template parameter pack for FN. Returns true iff
19945 PARM is used in a deducible way in the argument list of FN. */
19946
19947 static bool
19948 pack_deducible_p (tree parm, tree fn)
19949 {
19950 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19951 for (; t; t = TREE_CHAIN (t))
19952 {
19953 tree type = TREE_VALUE (t);
19954 tree packs;
19955 if (!PACK_EXPANSION_P (type))
19956 continue;
19957 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19958 packs; packs = TREE_CHAIN (packs))
19959 if (template_args_equal (TREE_VALUE (packs), parm))
19960 {
19961 /* The template parameter pack is used in a function parameter
19962 pack. If this is the end of the parameter list, the
19963 template parameter pack is deducible. */
19964 if (TREE_CHAIN (t) == void_list_node)
19965 return true;
19966 else
19967 /* Otherwise, not. Well, it could be deduced from
19968 a non-pack parameter, but doing so would end up with
19969 a deduction mismatch, so don't bother. */
19970 return false;
19971 }
19972 }
19973 /* The template parameter pack isn't used in any function parameter
19974 packs, but it might be used deeper, e.g. tuple<Args...>. */
19975 return true;
19976 }
19977
19978 /* Subroutine of fn_type_unification: check non-dependent parms for
19979 convertibility. */
19980
19981 static int
19982 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19983 tree fn, unification_kind_t strict, int flags,
19984 struct conversion **convs, bool explain_p)
19985 {
19986 /* Non-constructor methods need to leave a conversion for 'this', which
19987 isn't included in nargs here. */
19988 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19989 && !DECL_CONSTRUCTOR_P (fn));
19990
19991 for (unsigned ia = 0;
19992 parms && parms != void_list_node && ia < nargs; )
19993 {
19994 tree parm = TREE_VALUE (parms);
19995
19996 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19997 && (!TREE_CHAIN (parms)
19998 || TREE_CHAIN (parms) == void_list_node))
19999 /* For a function parameter pack that occurs at the end of the
20000 parameter-declaration-list, the type A of each remaining
20001 argument of the call is compared with the type P of the
20002 declarator-id of the function parameter pack. */
20003 break;
20004
20005 parms = TREE_CHAIN (parms);
20006
20007 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20008 /* For a function parameter pack that does not occur at the
20009 end of the parameter-declaration-list, the type of the
20010 parameter pack is a non-deduced context. */
20011 continue;
20012
20013 if (!uses_template_parms (parm))
20014 {
20015 tree arg = args[ia];
20016 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20017 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20018
20019 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20020 conv_p, explain_p))
20021 return 1;
20022 }
20023
20024 ++ia;
20025 }
20026
20027 return 0;
20028 }
20029
20030 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20031 NARGS elements of the arguments that are being used when calling
20032 it. TARGS is a vector into which the deduced template arguments
20033 are placed.
20034
20035 Returns either a FUNCTION_DECL for the matching specialization of FN or
20036 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20037 true, diagnostics will be printed to explain why it failed.
20038
20039 If FN is a conversion operator, or we are trying to produce a specific
20040 specialization, RETURN_TYPE is the return type desired.
20041
20042 The EXPLICIT_TARGS are explicit template arguments provided via a
20043 template-id.
20044
20045 The parameter STRICT is one of:
20046
20047 DEDUCE_CALL:
20048 We are deducing arguments for a function call, as in
20049 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20050 deducing arguments for a call to the result of a conversion
20051 function template, as in [over.call.object].
20052
20053 DEDUCE_CONV:
20054 We are deducing arguments for a conversion function, as in
20055 [temp.deduct.conv].
20056
20057 DEDUCE_EXACT:
20058 We are deducing arguments when doing an explicit instantiation
20059 as in [temp.explicit], when determining an explicit specialization
20060 as in [temp.expl.spec], or when taking the address of a function
20061 template, as in [temp.deduct.funcaddr]. */
20062
20063 tree
20064 fn_type_unification (tree fn,
20065 tree explicit_targs,
20066 tree targs,
20067 const tree *args,
20068 unsigned int nargs,
20069 tree return_type,
20070 unification_kind_t strict,
20071 int flags,
20072 struct conversion **convs,
20073 bool explain_p,
20074 bool decltype_p)
20075 {
20076 tree parms;
20077 tree fntype;
20078 tree decl = NULL_TREE;
20079 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20080 bool ok;
20081 static int deduction_depth;
20082 /* type_unification_real will pass back any access checks from default
20083 template argument substitution. */
20084 vec<deferred_access_check, va_gc> *checks = NULL;
20085 /* We don't have all the template args yet. */
20086 bool incomplete = true;
20087
20088 tree orig_fn = fn;
20089 if (flag_new_inheriting_ctors)
20090 fn = strip_inheriting_ctors (fn);
20091
20092 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20093 tree r = error_mark_node;
20094
20095 tree full_targs = targs;
20096 if (TMPL_ARGS_DEPTH (targs)
20097 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20098 full_targs = (add_outermost_template_args
20099 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20100 targs));
20101
20102 if (decltype_p)
20103 complain |= tf_decltype;
20104
20105 /* In C++0x, it's possible to have a function template whose type depends
20106 on itself recursively. This is most obvious with decltype, but can also
20107 occur with enumeration scope (c++/48969). So we need to catch infinite
20108 recursion and reject the substitution at deduction time; this function
20109 will return error_mark_node for any repeated substitution.
20110
20111 This also catches excessive recursion such as when f<N> depends on
20112 f<N-1> across all integers, and returns error_mark_node for all the
20113 substitutions back up to the initial one.
20114
20115 This is, of course, not reentrant. */
20116 if (excessive_deduction_depth)
20117 return error_mark_node;
20118 ++deduction_depth;
20119
20120 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20121
20122 fntype = TREE_TYPE (fn);
20123 if (explicit_targs)
20124 {
20125 /* [temp.deduct]
20126
20127 The specified template arguments must match the template
20128 parameters in kind (i.e., type, nontype, template), and there
20129 must not be more arguments than there are parameters;
20130 otherwise type deduction fails.
20131
20132 Nontype arguments must match the types of the corresponding
20133 nontype template parameters, or must be convertible to the
20134 types of the corresponding nontype parameters as specified in
20135 _temp.arg.nontype_, otherwise type deduction fails.
20136
20137 All references in the function type of the function template
20138 to the corresponding template parameters are replaced by the
20139 specified template argument values. If a substitution in a
20140 template parameter or in the function type of the function
20141 template results in an invalid type, type deduction fails. */
20142 int i, len = TREE_VEC_LENGTH (tparms);
20143 location_t loc = input_location;
20144 incomplete = false;
20145
20146 if (explicit_targs == error_mark_node)
20147 goto fail;
20148
20149 if (TMPL_ARGS_DEPTH (explicit_targs)
20150 < TMPL_ARGS_DEPTH (full_targs))
20151 explicit_targs = add_outermost_template_args (full_targs,
20152 explicit_targs);
20153
20154 /* Adjust any explicit template arguments before entering the
20155 substitution context. */
20156 explicit_targs
20157 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20158 complain|tf_partial,
20159 /*require_all_args=*/false,
20160 /*use_default_args=*/false));
20161 if (explicit_targs == error_mark_node)
20162 goto fail;
20163
20164 /* Substitute the explicit args into the function type. This is
20165 necessary so that, for instance, explicitly declared function
20166 arguments can match null pointed constants. If we were given
20167 an incomplete set of explicit args, we must not do semantic
20168 processing during substitution as we could create partial
20169 instantiations. */
20170 for (i = 0; i < len; i++)
20171 {
20172 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20173 bool parameter_pack = false;
20174 tree targ = TREE_VEC_ELT (explicit_targs, i);
20175
20176 /* Dig out the actual parm. */
20177 if (TREE_CODE (parm) == TYPE_DECL
20178 || TREE_CODE (parm) == TEMPLATE_DECL)
20179 {
20180 parm = TREE_TYPE (parm);
20181 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20182 }
20183 else if (TREE_CODE (parm) == PARM_DECL)
20184 {
20185 parm = DECL_INITIAL (parm);
20186 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20187 }
20188
20189 if (targ == NULL_TREE)
20190 /* No explicit argument for this template parameter. */
20191 incomplete = true;
20192 else if (parameter_pack && pack_deducible_p (parm, fn))
20193 {
20194 /* Mark the argument pack as "incomplete". We could
20195 still deduce more arguments during unification.
20196 We remove this mark in type_unification_real. */
20197 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20198 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20199 = ARGUMENT_PACK_ARGS (targ);
20200
20201 /* We have some incomplete argument packs. */
20202 incomplete = true;
20203 }
20204 }
20205
20206 if (incomplete)
20207 {
20208 if (!push_tinst_level (fn, explicit_targs))
20209 {
20210 excessive_deduction_depth = true;
20211 goto fail;
20212 }
20213 ++processing_template_decl;
20214 input_location = DECL_SOURCE_LOCATION (fn);
20215 /* Ignore any access checks; we'll see them again in
20216 instantiate_template and they might have the wrong
20217 access path at this point. */
20218 push_deferring_access_checks (dk_deferred);
20219 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20220 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20221 pop_deferring_access_checks ();
20222 input_location = loc;
20223 --processing_template_decl;
20224 pop_tinst_level ();
20225
20226 if (fntype == error_mark_node)
20227 goto fail;
20228 }
20229
20230 /* Place the explicitly specified arguments in TARGS. */
20231 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20232 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20233 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20234 if (!incomplete && CHECKING_P
20235 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20236 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20237 (targs, NUM_TMPL_ARGS (explicit_targs));
20238 }
20239
20240 if (return_type && strict != DEDUCE_CALL)
20241 {
20242 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20243 new_args[0] = return_type;
20244 memcpy (new_args + 1, args, nargs * sizeof (tree));
20245 args = new_args;
20246 ++nargs;
20247 }
20248
20249 if (!incomplete)
20250 goto deduced;
20251
20252 /* Never do unification on the 'this' parameter. */
20253 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20254
20255 if (return_type && strict == DEDUCE_CALL)
20256 {
20257 /* We're deducing for a call to the result of a template conversion
20258 function. The parms we really want are in return_type. */
20259 if (INDIRECT_TYPE_P (return_type))
20260 return_type = TREE_TYPE (return_type);
20261 parms = TYPE_ARG_TYPES (return_type);
20262 }
20263 else if (return_type)
20264 {
20265 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20266 }
20267
20268 /* We allow incomplete unification without an error message here
20269 because the standard doesn't seem to explicitly prohibit it. Our
20270 callers must be ready to deal with unification failures in any
20271 event. */
20272
20273 /* If we aren't explaining yet, push tinst context so we can see where
20274 any errors (e.g. from class instantiations triggered by instantiation
20275 of default template arguments) come from. If we are explaining, this
20276 context is redundant. */
20277 if (!explain_p && !push_tinst_level (fn, targs))
20278 {
20279 excessive_deduction_depth = true;
20280 goto fail;
20281 }
20282
20283 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20284 full_targs, parms, args, nargs, /*subr=*/0,
20285 strict, &checks, explain_p);
20286 if (!explain_p)
20287 pop_tinst_level ();
20288 if (!ok)
20289 goto fail;
20290
20291 /* Now that we have bindings for all of the template arguments,
20292 ensure that the arguments deduced for the template template
20293 parameters have compatible template parameter lists. We cannot
20294 check this property before we have deduced all template
20295 arguments, because the template parameter types of a template
20296 template parameter might depend on prior template parameters
20297 deduced after the template template parameter. The following
20298 ill-formed example illustrates this issue:
20299
20300 template<typename T, template<T> class C> void f(C<5>, T);
20301
20302 template<int N> struct X {};
20303
20304 void g() {
20305 f(X<5>(), 5l); // error: template argument deduction fails
20306 }
20307
20308 The template parameter list of 'C' depends on the template type
20309 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20310 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20311 time that we deduce 'C'. */
20312 if (!template_template_parm_bindings_ok_p
20313 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20314 {
20315 unify_inconsistent_template_template_parameters (explain_p);
20316 goto fail;
20317 }
20318
20319 /* DR 1391: All parameters have args, now check non-dependent parms for
20320 convertibility. */
20321 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20322 convs, explain_p))
20323 goto fail;
20324
20325 deduced:
20326 /* All is well so far. Now, check:
20327
20328 [temp.deduct]
20329
20330 When all template arguments have been deduced, all uses of
20331 template parameters in nondeduced contexts are replaced with
20332 the corresponding deduced argument values. If the
20333 substitution results in an invalid type, as described above,
20334 type deduction fails. */
20335 if (!push_tinst_level (fn, targs))
20336 {
20337 excessive_deduction_depth = true;
20338 goto fail;
20339 }
20340
20341 /* Also collect access checks from the instantiation. */
20342 reopen_deferring_access_checks (checks);
20343
20344 decl = instantiate_template (fn, targs, complain);
20345
20346 checks = get_deferred_access_checks ();
20347 pop_deferring_access_checks ();
20348
20349 pop_tinst_level ();
20350
20351 if (decl == error_mark_node)
20352 goto fail;
20353
20354 /* Now perform any access checks encountered during substitution. */
20355 push_access_scope (decl);
20356 ok = perform_access_checks (checks, complain);
20357 pop_access_scope (decl);
20358 if (!ok)
20359 goto fail;
20360
20361 /* If we're looking for an exact match, check that what we got
20362 is indeed an exact match. It might not be if some template
20363 parameters are used in non-deduced contexts. But don't check
20364 for an exact match if we have dependent template arguments;
20365 in that case we're doing partial ordering, and we already know
20366 that we have two candidates that will provide the actual type. */
20367 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20368 {
20369 tree substed = TREE_TYPE (decl);
20370 unsigned int i;
20371
20372 tree sarg
20373 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20374 if (return_type)
20375 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20376 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20377 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20378 {
20379 unify_type_mismatch (explain_p, args[i],
20380 TREE_VALUE (sarg));
20381 goto fail;
20382 }
20383 }
20384
20385 /* After doing deduction with the inherited constructor, actually return an
20386 instantiation of the inheriting constructor. */
20387 if (orig_fn != fn)
20388 decl = instantiate_template (orig_fn, targs, complain);
20389
20390 r = decl;
20391
20392 fail:
20393 --deduction_depth;
20394 if (excessive_deduction_depth)
20395 {
20396 if (deduction_depth == 0)
20397 /* Reset once we're all the way out. */
20398 excessive_deduction_depth = false;
20399 }
20400
20401 return r;
20402 }
20403
20404 /* Adjust types before performing type deduction, as described in
20405 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20406 sections are symmetric. PARM is the type of a function parameter
20407 or the return type of the conversion function. ARG is the type of
20408 the argument passed to the call, or the type of the value
20409 initialized with the result of the conversion function.
20410 ARG_EXPR is the original argument expression, which may be null. */
20411
20412 static int
20413 maybe_adjust_types_for_deduction (unification_kind_t strict,
20414 tree* parm,
20415 tree* arg,
20416 tree arg_expr)
20417 {
20418 int result = 0;
20419
20420 switch (strict)
20421 {
20422 case DEDUCE_CALL:
20423 break;
20424
20425 case DEDUCE_CONV:
20426 /* Swap PARM and ARG throughout the remainder of this
20427 function; the handling is precisely symmetric since PARM
20428 will initialize ARG rather than vice versa. */
20429 std::swap (parm, arg);
20430 break;
20431
20432 case DEDUCE_EXACT:
20433 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20434 too, but here handle it by stripping the reference from PARM
20435 rather than by adding it to ARG. */
20436 if (TYPE_REF_P (*parm)
20437 && TYPE_REF_IS_RVALUE (*parm)
20438 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20439 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20440 && TYPE_REF_P (*arg)
20441 && !TYPE_REF_IS_RVALUE (*arg))
20442 *parm = TREE_TYPE (*parm);
20443 /* Nothing else to do in this case. */
20444 return 0;
20445
20446 default:
20447 gcc_unreachable ();
20448 }
20449
20450 if (!TYPE_REF_P (*parm))
20451 {
20452 /* [temp.deduct.call]
20453
20454 If P is not a reference type:
20455
20456 --If A is an array type, the pointer type produced by the
20457 array-to-pointer standard conversion (_conv.array_) is
20458 used in place of A for type deduction; otherwise,
20459
20460 --If A is a function type, the pointer type produced by
20461 the function-to-pointer standard conversion
20462 (_conv.func_) is used in place of A for type deduction;
20463 otherwise,
20464
20465 --If A is a cv-qualified type, the top level
20466 cv-qualifiers of A's type are ignored for type
20467 deduction. */
20468 if (TREE_CODE (*arg) == ARRAY_TYPE)
20469 *arg = build_pointer_type (TREE_TYPE (*arg));
20470 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20471 *arg = build_pointer_type (*arg);
20472 else
20473 *arg = TYPE_MAIN_VARIANT (*arg);
20474 }
20475
20476 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20477 reference to a cv-unqualified template parameter that does not represent a
20478 template parameter of a class template (during class template argument
20479 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20480 an lvalue, the type "lvalue reference to A" is used in place of A for type
20481 deduction. */
20482 if (TYPE_REF_P (*parm)
20483 && TYPE_REF_IS_RVALUE (*parm)
20484 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20485 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20486 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20487 && (arg_expr ? lvalue_p (arg_expr)
20488 /* try_one_overload doesn't provide an arg_expr, but
20489 functions are always lvalues. */
20490 : TREE_CODE (*arg) == FUNCTION_TYPE))
20491 *arg = build_reference_type (*arg);
20492
20493 /* [temp.deduct.call]
20494
20495 If P is a cv-qualified type, the top level cv-qualifiers
20496 of P's type are ignored for type deduction. If P is a
20497 reference type, the type referred to by P is used for
20498 type deduction. */
20499 *parm = TYPE_MAIN_VARIANT (*parm);
20500 if (TYPE_REF_P (*parm))
20501 {
20502 *parm = TREE_TYPE (*parm);
20503 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20504 }
20505
20506 /* DR 322. For conversion deduction, remove a reference type on parm
20507 too (which has been swapped into ARG). */
20508 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20509 *arg = TREE_TYPE (*arg);
20510
20511 return result;
20512 }
20513
20514 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20515 template which doesn't contain any deducible template parameters; check if
20516 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20517 unify_one_argument. */
20518
20519 static int
20520 check_non_deducible_conversion (tree parm, tree arg, int strict,
20521 int flags, struct conversion **conv_p,
20522 bool explain_p)
20523 {
20524 tree type;
20525
20526 if (!TYPE_P (arg))
20527 type = TREE_TYPE (arg);
20528 else
20529 type = arg;
20530
20531 if (same_type_p (parm, type))
20532 return unify_success (explain_p);
20533
20534 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20535 if (strict == DEDUCE_CONV)
20536 {
20537 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20538 return unify_success (explain_p);
20539 }
20540 else if (strict != DEDUCE_EXACT)
20541 {
20542 bool ok = false;
20543 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20544 if (conv_p)
20545 /* Avoid recalculating this in add_function_candidate. */
20546 ok = (*conv_p
20547 = good_conversion (parm, type, conv_arg, flags, complain));
20548 else
20549 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20550 if (ok)
20551 return unify_success (explain_p);
20552 }
20553
20554 if (strict == DEDUCE_EXACT)
20555 return unify_type_mismatch (explain_p, parm, arg);
20556 else
20557 return unify_arg_conversion (explain_p, parm, type, arg);
20558 }
20559
20560 static bool uses_deducible_template_parms (tree type);
20561
20562 /* Returns true iff the expression EXPR is one from which a template
20563 argument can be deduced. In other words, if it's an undecorated
20564 use of a template non-type parameter. */
20565
20566 static bool
20567 deducible_expression (tree expr)
20568 {
20569 /* Strip implicit conversions. */
20570 while (CONVERT_EXPR_P (expr))
20571 expr = TREE_OPERAND (expr, 0);
20572 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20573 }
20574
20575 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20576 deducible way; that is, if it has a max value of <PARM> - 1. */
20577
20578 static bool
20579 deducible_array_bound (tree domain)
20580 {
20581 if (domain == NULL_TREE)
20582 return false;
20583
20584 tree max = TYPE_MAX_VALUE (domain);
20585 if (TREE_CODE (max) != MINUS_EXPR)
20586 return false;
20587
20588 return deducible_expression (TREE_OPERAND (max, 0));
20589 }
20590
20591 /* Returns true iff the template arguments ARGS use a template parameter
20592 in a deducible way. */
20593
20594 static bool
20595 deducible_template_args (tree args)
20596 {
20597 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20598 {
20599 bool deducible;
20600 tree elt = TREE_VEC_ELT (args, i);
20601 if (ARGUMENT_PACK_P (elt))
20602 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20603 else
20604 {
20605 if (PACK_EXPANSION_P (elt))
20606 elt = PACK_EXPANSION_PATTERN (elt);
20607 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20608 deducible = true;
20609 else if (TYPE_P (elt))
20610 deducible = uses_deducible_template_parms (elt);
20611 else
20612 deducible = deducible_expression (elt);
20613 }
20614 if (deducible)
20615 return true;
20616 }
20617 return false;
20618 }
20619
20620 /* Returns true iff TYPE contains any deducible references to template
20621 parameters, as per 14.8.2.5. */
20622
20623 static bool
20624 uses_deducible_template_parms (tree type)
20625 {
20626 if (PACK_EXPANSION_P (type))
20627 type = PACK_EXPANSION_PATTERN (type);
20628
20629 /* T
20630 cv-list T
20631 TT<T>
20632 TT<i>
20633 TT<> */
20634 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20635 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20636 return true;
20637
20638 /* T*
20639 T&
20640 T&& */
20641 if (INDIRECT_TYPE_P (type))
20642 return uses_deducible_template_parms (TREE_TYPE (type));
20643
20644 /* T[integer-constant ]
20645 type [i] */
20646 if (TREE_CODE (type) == ARRAY_TYPE)
20647 return (uses_deducible_template_parms (TREE_TYPE (type))
20648 || deducible_array_bound (TYPE_DOMAIN (type)));
20649
20650 /* T type ::*
20651 type T::*
20652 T T::*
20653 T (type ::*)()
20654 type (T::*)()
20655 type (type ::*)(T)
20656 type (T::*)(T)
20657 T (type ::*)(T)
20658 T (T::*)()
20659 T (T::*)(T) */
20660 if (TYPE_PTRMEM_P (type))
20661 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20662 || (uses_deducible_template_parms
20663 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20664
20665 /* template-name <T> (where template-name refers to a class template)
20666 template-name <i> (where template-name refers to a class template) */
20667 if (CLASS_TYPE_P (type)
20668 && CLASSTYPE_TEMPLATE_INFO (type)
20669 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20670 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20671 (CLASSTYPE_TI_ARGS (type)));
20672
20673 /* type (T)
20674 T()
20675 T(T) */
20676 if (FUNC_OR_METHOD_TYPE_P (type))
20677 {
20678 if (uses_deducible_template_parms (TREE_TYPE (type)))
20679 return true;
20680 tree parm = TYPE_ARG_TYPES (type);
20681 if (TREE_CODE (type) == METHOD_TYPE)
20682 parm = TREE_CHAIN (parm);
20683 for (; parm; parm = TREE_CHAIN (parm))
20684 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20685 return true;
20686 }
20687
20688 return false;
20689 }
20690
20691 /* Subroutine of type_unification_real and unify_pack_expansion to
20692 handle unification of a single P/A pair. Parameters are as
20693 for those functions. */
20694
20695 static int
20696 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20697 int subr, unification_kind_t strict,
20698 bool explain_p)
20699 {
20700 tree arg_expr = NULL_TREE;
20701 int arg_strict;
20702
20703 if (arg == error_mark_node || parm == error_mark_node)
20704 return unify_invalid (explain_p);
20705 if (arg == unknown_type_node)
20706 /* We can't deduce anything from this, but we might get all the
20707 template args from other function args. */
20708 return unify_success (explain_p);
20709
20710 /* Implicit conversions (Clause 4) will be performed on a function
20711 argument to convert it to the type of the corresponding function
20712 parameter if the parameter type contains no template-parameters that
20713 participate in template argument deduction. */
20714 if (strict != DEDUCE_EXACT
20715 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20716 /* For function parameters with no deducible template parameters,
20717 just return. We'll check non-dependent conversions later. */
20718 return unify_success (explain_p);
20719
20720 switch (strict)
20721 {
20722 case DEDUCE_CALL:
20723 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20724 | UNIFY_ALLOW_MORE_CV_QUAL
20725 | UNIFY_ALLOW_DERIVED);
20726 break;
20727
20728 case DEDUCE_CONV:
20729 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20730 break;
20731
20732 case DEDUCE_EXACT:
20733 arg_strict = UNIFY_ALLOW_NONE;
20734 break;
20735
20736 default:
20737 gcc_unreachable ();
20738 }
20739
20740 /* We only do these transformations if this is the top-level
20741 parameter_type_list in a call or declaration matching; in other
20742 situations (nested function declarators, template argument lists) we
20743 won't be comparing a type to an expression, and we don't do any type
20744 adjustments. */
20745 if (!subr)
20746 {
20747 if (!TYPE_P (arg))
20748 {
20749 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20750 if (type_unknown_p (arg))
20751 {
20752 /* [temp.deduct.type] A template-argument can be
20753 deduced from a pointer to function or pointer
20754 to member function argument if the set of
20755 overloaded functions does not contain function
20756 templates and at most one of a set of
20757 overloaded functions provides a unique
20758 match. */
20759 resolve_overloaded_unification (tparms, targs, parm,
20760 arg, strict,
20761 arg_strict, explain_p);
20762 /* If a unique match was not found, this is a
20763 non-deduced context, so we still succeed. */
20764 return unify_success (explain_p);
20765 }
20766
20767 arg_expr = arg;
20768 arg = unlowered_expr_type (arg);
20769 if (arg == error_mark_node)
20770 return unify_invalid (explain_p);
20771 }
20772
20773 arg_strict |=
20774 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20775 }
20776 else
20777 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20778 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20779 return unify_template_argument_mismatch (explain_p, parm, arg);
20780
20781 /* For deduction from an init-list we need the actual list. */
20782 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20783 arg = arg_expr;
20784 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20785 }
20786
20787 /* for_each_template_parm callback that always returns 0. */
20788
20789 static int
20790 zero_r (tree, void *)
20791 {
20792 return 0;
20793 }
20794
20795 /* for_each_template_parm any_fn callback to handle deduction of a template
20796 type argument from the type of an array bound. */
20797
20798 static int
20799 array_deduction_r (tree t, void *data)
20800 {
20801 tree_pair_p d = (tree_pair_p)data;
20802 tree &tparms = d->purpose;
20803 tree &targs = d->value;
20804
20805 if (TREE_CODE (t) == ARRAY_TYPE)
20806 if (tree dom = TYPE_DOMAIN (t))
20807 if (tree max = TYPE_MAX_VALUE (dom))
20808 {
20809 if (TREE_CODE (max) == MINUS_EXPR)
20810 max = TREE_OPERAND (max, 0);
20811 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20812 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20813 UNIFY_ALLOW_NONE, /*explain*/false);
20814 }
20815
20816 /* Keep walking. */
20817 return 0;
20818 }
20819
20820 /* Try to deduce any not-yet-deduced template type arguments from the type of
20821 an array bound. This is handled separately from unify because 14.8.2.5 says
20822 "The type of a type parameter is only deduced from an array bound if it is
20823 not otherwise deduced." */
20824
20825 static void
20826 try_array_deduction (tree tparms, tree targs, tree parm)
20827 {
20828 tree_pair_s data = { tparms, targs };
20829 hash_set<tree> visited;
20830 for_each_template_parm (parm, zero_r, &data, &visited,
20831 /*nondeduced*/false, array_deduction_r);
20832 }
20833
20834 /* Most parms like fn_type_unification.
20835
20836 If SUBR is 1, we're being called recursively (to unify the
20837 arguments of a function or method parameter of a function
20838 template).
20839
20840 CHECKS is a pointer to a vector of access checks encountered while
20841 substituting default template arguments. */
20842
20843 static int
20844 type_unification_real (tree tparms,
20845 tree full_targs,
20846 tree xparms,
20847 const tree *xargs,
20848 unsigned int xnargs,
20849 int subr,
20850 unification_kind_t strict,
20851 vec<deferred_access_check, va_gc> **checks,
20852 bool explain_p)
20853 {
20854 tree parm, arg;
20855 int i;
20856 int ntparms = TREE_VEC_LENGTH (tparms);
20857 int saw_undeduced = 0;
20858 tree parms;
20859 const tree *args;
20860 unsigned int nargs;
20861 unsigned int ia;
20862
20863 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20864 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20865 gcc_assert (ntparms > 0);
20866
20867 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20868
20869 /* Reset the number of non-defaulted template arguments contained
20870 in TARGS. */
20871 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20872
20873 again:
20874 parms = xparms;
20875 args = xargs;
20876 nargs = xnargs;
20877
20878 ia = 0;
20879 while (parms && parms != void_list_node
20880 && ia < nargs)
20881 {
20882 parm = TREE_VALUE (parms);
20883
20884 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20885 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20886 /* For a function parameter pack that occurs at the end of the
20887 parameter-declaration-list, the type A of each remaining
20888 argument of the call is compared with the type P of the
20889 declarator-id of the function parameter pack. */
20890 break;
20891
20892 parms = TREE_CHAIN (parms);
20893
20894 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20895 /* For a function parameter pack that does not occur at the
20896 end of the parameter-declaration-list, the type of the
20897 parameter pack is a non-deduced context. */
20898 continue;
20899
20900 arg = args[ia];
20901 ++ia;
20902
20903 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20904 explain_p))
20905 return 1;
20906 }
20907
20908 if (parms
20909 && parms != void_list_node
20910 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20911 {
20912 /* Unify the remaining arguments with the pack expansion type. */
20913 tree argvec;
20914 tree parmvec = make_tree_vec (1);
20915
20916 /* Allocate a TREE_VEC and copy in all of the arguments */
20917 argvec = make_tree_vec (nargs - ia);
20918 for (i = 0; ia < nargs; ++ia, ++i)
20919 TREE_VEC_ELT (argvec, i) = args[ia];
20920
20921 /* Copy the parameter into parmvec. */
20922 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20923 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20924 /*subr=*/subr, explain_p))
20925 return 1;
20926
20927 /* Advance to the end of the list of parameters. */
20928 parms = TREE_CHAIN (parms);
20929 }
20930
20931 /* Fail if we've reached the end of the parm list, and more args
20932 are present, and the parm list isn't variadic. */
20933 if (ia < nargs && parms == void_list_node)
20934 return unify_too_many_arguments (explain_p, nargs, ia);
20935 /* Fail if parms are left and they don't have default values and
20936 they aren't all deduced as empty packs (c++/57397). This is
20937 consistent with sufficient_parms_p. */
20938 if (parms && parms != void_list_node
20939 && TREE_PURPOSE (parms) == NULL_TREE)
20940 {
20941 unsigned int count = nargs;
20942 tree p = parms;
20943 bool type_pack_p;
20944 do
20945 {
20946 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20947 if (!type_pack_p)
20948 count++;
20949 p = TREE_CHAIN (p);
20950 }
20951 while (p && p != void_list_node);
20952 if (count != nargs)
20953 return unify_too_few_arguments (explain_p, ia, count,
20954 type_pack_p);
20955 }
20956
20957 if (!subr)
20958 {
20959 tsubst_flags_t complain = (explain_p
20960 ? tf_warning_or_error
20961 : tf_none);
20962 bool tried_array_deduction = (cxx_dialect < cxx17);
20963
20964 for (i = 0; i < ntparms; i++)
20965 {
20966 tree targ = TREE_VEC_ELT (targs, i);
20967 tree tparm = TREE_VEC_ELT (tparms, i);
20968
20969 /* Clear the "incomplete" flags on all argument packs now so that
20970 substituting them into later default arguments works. */
20971 if (targ && ARGUMENT_PACK_P (targ))
20972 {
20973 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20974 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20975 }
20976
20977 if (targ || tparm == error_mark_node)
20978 continue;
20979 tparm = TREE_VALUE (tparm);
20980
20981 if (TREE_CODE (tparm) == TYPE_DECL
20982 && !tried_array_deduction)
20983 {
20984 try_array_deduction (tparms, targs, xparms);
20985 tried_array_deduction = true;
20986 if (TREE_VEC_ELT (targs, i))
20987 continue;
20988 }
20989
20990 /* If this is an undeduced nontype parameter that depends on
20991 a type parameter, try another pass; its type may have been
20992 deduced from a later argument than the one from which
20993 this parameter can be deduced. */
20994 if (TREE_CODE (tparm) == PARM_DECL
20995 && uses_template_parms (TREE_TYPE (tparm))
20996 && saw_undeduced < 2)
20997 {
20998 saw_undeduced = 1;
20999 continue;
21000 }
21001
21002 /* Core issue #226 (C++0x) [temp.deduct]:
21003
21004 If a template argument has not been deduced, its
21005 default template argument, if any, is used.
21006
21007 When we are in C++98 mode, TREE_PURPOSE will either
21008 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21009 to explicitly check cxx_dialect here. */
21010 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21011 /* OK, there is a default argument. Wait until after the
21012 conversion check to do substitution. */
21013 continue;
21014
21015 /* If the type parameter is a parameter pack, then it will
21016 be deduced to an empty parameter pack. */
21017 if (template_parameter_pack_p (tparm))
21018 {
21019 tree arg;
21020
21021 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21022 {
21023 arg = make_node (NONTYPE_ARGUMENT_PACK);
21024 TREE_CONSTANT (arg) = 1;
21025 }
21026 else
21027 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21028
21029 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21030
21031 TREE_VEC_ELT (targs, i) = arg;
21032 continue;
21033 }
21034
21035 return unify_parameter_deduction_failure (explain_p, tparm);
21036 }
21037
21038 /* Now substitute into the default template arguments. */
21039 for (i = 0; i < ntparms; i++)
21040 {
21041 tree targ = TREE_VEC_ELT (targs, i);
21042 tree tparm = TREE_VEC_ELT (tparms, i);
21043
21044 if (targ || tparm == error_mark_node)
21045 continue;
21046 tree parm = TREE_VALUE (tparm);
21047 tree arg = TREE_PURPOSE (tparm);
21048 reopen_deferring_access_checks (*checks);
21049 location_t save_loc = input_location;
21050 if (DECL_P (parm))
21051 input_location = DECL_SOURCE_LOCATION (parm);
21052
21053 if (saw_undeduced == 1
21054 && TREE_CODE (parm) == PARM_DECL
21055 && uses_template_parms (TREE_TYPE (parm)))
21056 {
21057 /* The type of this non-type parameter depends on undeduced
21058 parameters. Don't try to use its default argument yet,
21059 since we might deduce an argument for it on the next pass,
21060 but do check whether the arguments we already have cause
21061 substitution failure, so that that happens before we try
21062 later default arguments (78489). */
21063 ++processing_template_decl;
21064 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21065 NULL_TREE);
21066 --processing_template_decl;
21067 if (type == error_mark_node)
21068 arg = error_mark_node;
21069 else
21070 arg = NULL_TREE;
21071 }
21072 else
21073 {
21074 /* Even if the call is happening in template context, getting
21075 here means it's non-dependent, and a default argument is
21076 considered a separate definition under [temp.decls], so we can
21077 do this substitution without processing_template_decl. This
21078 is important if the default argument contains something that
21079 might be instantiation-dependent like access (87480). */
21080 processing_template_decl_sentinel s;
21081 tree substed = NULL_TREE;
21082 if (saw_undeduced == 1)
21083 {
21084 /* First instatiate in template context, in case we still
21085 depend on undeduced template parameters. */
21086 ++processing_template_decl;
21087 substed = tsubst_template_arg (arg, full_targs, complain,
21088 NULL_TREE);
21089 --processing_template_decl;
21090 if (substed != error_mark_node
21091 && !uses_template_parms (substed))
21092 /* We replaced all the tparms, substitute again out of
21093 template context. */
21094 substed = NULL_TREE;
21095 }
21096 if (!substed)
21097 substed = tsubst_template_arg (arg, full_targs, complain,
21098 NULL_TREE);
21099
21100 if (!uses_template_parms (substed))
21101 arg = convert_template_argument (parm, substed, full_targs,
21102 complain, i, NULL_TREE);
21103 else if (saw_undeduced == 1)
21104 arg = NULL_TREE;
21105 else
21106 arg = error_mark_node;
21107 }
21108
21109 input_location = save_loc;
21110 *checks = get_deferred_access_checks ();
21111 pop_deferring_access_checks ();
21112
21113 if (arg == error_mark_node)
21114 return 1;
21115 else if (arg)
21116 {
21117 TREE_VEC_ELT (targs, i) = arg;
21118 /* The position of the first default template argument,
21119 is also the number of non-defaulted arguments in TARGS.
21120 Record that. */
21121 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21122 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21123 }
21124 }
21125
21126 if (saw_undeduced++ == 1)
21127 goto again;
21128 }
21129
21130 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21131 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21132
21133 return unify_success (explain_p);
21134 }
21135
21136 /* Subroutine of type_unification_real. Args are like the variables
21137 at the call site. ARG is an overloaded function (or template-id);
21138 we try deducing template args from each of the overloads, and if
21139 only one succeeds, we go with that. Modifies TARGS and returns
21140 true on success. */
21141
21142 static bool
21143 resolve_overloaded_unification (tree tparms,
21144 tree targs,
21145 tree parm,
21146 tree arg,
21147 unification_kind_t strict,
21148 int sub_strict,
21149 bool explain_p)
21150 {
21151 tree tempargs = copy_node (targs);
21152 int good = 0;
21153 tree goodfn = NULL_TREE;
21154 bool addr_p;
21155
21156 if (TREE_CODE (arg) == ADDR_EXPR)
21157 {
21158 arg = TREE_OPERAND (arg, 0);
21159 addr_p = true;
21160 }
21161 else
21162 addr_p = false;
21163
21164 if (TREE_CODE (arg) == COMPONENT_REF)
21165 /* Handle `&x' where `x' is some static or non-static member
21166 function name. */
21167 arg = TREE_OPERAND (arg, 1);
21168
21169 if (TREE_CODE (arg) == OFFSET_REF)
21170 arg = TREE_OPERAND (arg, 1);
21171
21172 /* Strip baselink information. */
21173 if (BASELINK_P (arg))
21174 arg = BASELINK_FUNCTIONS (arg);
21175
21176 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21177 {
21178 /* If we got some explicit template args, we need to plug them into
21179 the affected templates before we try to unify, in case the
21180 explicit args will completely resolve the templates in question. */
21181
21182 int ok = 0;
21183 tree expl_subargs = TREE_OPERAND (arg, 1);
21184 arg = TREE_OPERAND (arg, 0);
21185
21186 for (lkp_iterator iter (arg); iter; ++iter)
21187 {
21188 tree fn = *iter;
21189 tree subargs, elem;
21190
21191 if (TREE_CODE (fn) != TEMPLATE_DECL)
21192 continue;
21193
21194 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21195 expl_subargs, NULL_TREE, tf_none,
21196 /*require_all_args=*/true,
21197 /*use_default_args=*/true);
21198 if (subargs != error_mark_node
21199 && !any_dependent_template_arguments_p (subargs))
21200 {
21201 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21202 if (try_one_overload (tparms, targs, tempargs, parm,
21203 elem, strict, sub_strict, addr_p, explain_p)
21204 && (!goodfn || !same_type_p (goodfn, elem)))
21205 {
21206 goodfn = elem;
21207 ++good;
21208 }
21209 }
21210 else if (subargs)
21211 ++ok;
21212 }
21213 /* If no templates (or more than one) are fully resolved by the
21214 explicit arguments, this template-id is a non-deduced context; it
21215 could still be OK if we deduce all template arguments for the
21216 enclosing call through other arguments. */
21217 if (good != 1)
21218 good = ok;
21219 }
21220 else if (!OVL_P (arg))
21221 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21222 -- but the deduction does not succeed because the expression is
21223 not just the function on its own. */
21224 return false;
21225 else
21226 for (lkp_iterator iter (arg); iter; ++iter)
21227 {
21228 tree fn = *iter;
21229 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21230 strict, sub_strict, addr_p, explain_p)
21231 && (!goodfn || !decls_match (goodfn, fn)))
21232 {
21233 goodfn = fn;
21234 ++good;
21235 }
21236 }
21237
21238 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21239 to function or pointer to member function argument if the set of
21240 overloaded functions does not contain function templates and at most
21241 one of a set of overloaded functions provides a unique match.
21242
21243 So if we found multiple possibilities, we return success but don't
21244 deduce anything. */
21245
21246 if (good == 1)
21247 {
21248 int i = TREE_VEC_LENGTH (targs);
21249 for (; i--; )
21250 if (TREE_VEC_ELT (tempargs, i))
21251 {
21252 tree old = TREE_VEC_ELT (targs, i);
21253 tree new_ = TREE_VEC_ELT (tempargs, i);
21254 if (new_ && old && ARGUMENT_PACK_P (old)
21255 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21256 /* Don't forget explicit template arguments in a pack. */
21257 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21258 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21259 TREE_VEC_ELT (targs, i) = new_;
21260 }
21261 }
21262 if (good)
21263 return true;
21264
21265 return false;
21266 }
21267
21268 /* Core DR 115: In contexts where deduction is done and fails, or in
21269 contexts where deduction is not done, if a template argument list is
21270 specified and it, along with any default template arguments, identifies
21271 a single function template specialization, then the template-id is an
21272 lvalue for the function template specialization. */
21273
21274 tree
21275 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21276 {
21277 tree expr, offset, baselink;
21278 bool addr;
21279
21280 if (!type_unknown_p (orig_expr))
21281 return orig_expr;
21282
21283 expr = orig_expr;
21284 addr = false;
21285 offset = NULL_TREE;
21286 baselink = NULL_TREE;
21287
21288 if (TREE_CODE (expr) == ADDR_EXPR)
21289 {
21290 expr = TREE_OPERAND (expr, 0);
21291 addr = true;
21292 }
21293 if (TREE_CODE (expr) == OFFSET_REF)
21294 {
21295 offset = expr;
21296 expr = TREE_OPERAND (expr, 1);
21297 }
21298 if (BASELINK_P (expr))
21299 {
21300 baselink = expr;
21301 expr = BASELINK_FUNCTIONS (expr);
21302 }
21303
21304 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21305 {
21306 int good = 0;
21307 tree goodfn = NULL_TREE;
21308
21309 /* If we got some explicit template args, we need to plug them into
21310 the affected templates before we try to unify, in case the
21311 explicit args will completely resolve the templates in question. */
21312
21313 tree expl_subargs = TREE_OPERAND (expr, 1);
21314 tree arg = TREE_OPERAND (expr, 0);
21315 tree badfn = NULL_TREE;
21316 tree badargs = NULL_TREE;
21317
21318 for (lkp_iterator iter (arg); iter; ++iter)
21319 {
21320 tree fn = *iter;
21321 tree subargs, elem;
21322
21323 if (TREE_CODE (fn) != TEMPLATE_DECL)
21324 continue;
21325
21326 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21327 expl_subargs, NULL_TREE, tf_none,
21328 /*require_all_args=*/true,
21329 /*use_default_args=*/true);
21330 if (subargs != error_mark_node
21331 && !any_dependent_template_arguments_p (subargs))
21332 {
21333 elem = instantiate_template (fn, subargs, tf_none);
21334 if (elem == error_mark_node)
21335 {
21336 badfn = fn;
21337 badargs = subargs;
21338 }
21339 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21340 {
21341 goodfn = elem;
21342 ++good;
21343 }
21344 }
21345 }
21346 if (good == 1)
21347 {
21348 mark_used (goodfn);
21349 expr = goodfn;
21350 if (baselink)
21351 expr = build_baselink (BASELINK_BINFO (baselink),
21352 BASELINK_ACCESS_BINFO (baselink),
21353 expr, BASELINK_OPTYPE (baselink));
21354 if (offset)
21355 {
21356 tree base
21357 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21358 expr = build_offset_ref (base, expr, addr, complain);
21359 }
21360 if (addr)
21361 expr = cp_build_addr_expr (expr, complain);
21362 return expr;
21363 }
21364 else if (good == 0 && badargs && (complain & tf_error))
21365 /* There were no good options and at least one bad one, so let the
21366 user know what the problem is. */
21367 instantiate_template (badfn, badargs, complain);
21368 }
21369 return orig_expr;
21370 }
21371
21372 /* As above, but error out if the expression remains overloaded. */
21373
21374 tree
21375 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21376 {
21377 exp = resolve_nondeduced_context (exp, complain);
21378 if (type_unknown_p (exp))
21379 {
21380 if (complain & tf_error)
21381 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21382 return error_mark_node;
21383 }
21384 return exp;
21385 }
21386
21387 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21388 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21389 different overloads deduce different arguments for a given parm.
21390 ADDR_P is true if the expression for which deduction is being
21391 performed was of the form "& fn" rather than simply "fn".
21392
21393 Returns 1 on success. */
21394
21395 static int
21396 try_one_overload (tree tparms,
21397 tree orig_targs,
21398 tree targs,
21399 tree parm,
21400 tree arg,
21401 unification_kind_t strict,
21402 int sub_strict,
21403 bool addr_p,
21404 bool explain_p)
21405 {
21406 int nargs;
21407 tree tempargs;
21408 int i;
21409
21410 if (arg == error_mark_node)
21411 return 0;
21412
21413 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21414 to function or pointer to member function argument if the set of
21415 overloaded functions does not contain function templates and at most
21416 one of a set of overloaded functions provides a unique match.
21417
21418 So if this is a template, just return success. */
21419
21420 if (uses_template_parms (arg))
21421 return 1;
21422
21423 if (TREE_CODE (arg) == METHOD_TYPE)
21424 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21425 else if (addr_p)
21426 arg = build_pointer_type (arg);
21427
21428 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21429
21430 /* We don't copy orig_targs for this because if we have already deduced
21431 some template args from previous args, unify would complain when we
21432 try to deduce a template parameter for the same argument, even though
21433 there isn't really a conflict. */
21434 nargs = TREE_VEC_LENGTH (targs);
21435 tempargs = make_tree_vec (nargs);
21436
21437 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21438 return 0;
21439
21440 /* First make sure we didn't deduce anything that conflicts with
21441 explicitly specified args. */
21442 for (i = nargs; i--; )
21443 {
21444 tree elt = TREE_VEC_ELT (tempargs, i);
21445 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21446
21447 if (!elt)
21448 /*NOP*/;
21449 else if (uses_template_parms (elt))
21450 /* Since we're unifying against ourselves, we will fill in
21451 template args used in the function parm list with our own
21452 template parms. Discard them. */
21453 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21454 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21455 {
21456 /* Check that the argument at each index of the deduced argument pack
21457 is equivalent to the corresponding explicitly specified argument.
21458 We may have deduced more arguments than were explicitly specified,
21459 and that's OK. */
21460
21461 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21462 that's wrong if we deduce the same argument pack from multiple
21463 function arguments: it's only incomplete the first time. */
21464
21465 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21466 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21467
21468 if (TREE_VEC_LENGTH (deduced_pack)
21469 < TREE_VEC_LENGTH (explicit_pack))
21470 return 0;
21471
21472 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21473 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21474 TREE_VEC_ELT (deduced_pack, j)))
21475 return 0;
21476 }
21477 else if (oldelt && !template_args_equal (oldelt, elt))
21478 return 0;
21479 }
21480
21481 for (i = nargs; i--; )
21482 {
21483 tree elt = TREE_VEC_ELT (tempargs, i);
21484
21485 if (elt)
21486 TREE_VEC_ELT (targs, i) = elt;
21487 }
21488
21489 return 1;
21490 }
21491
21492 /* PARM is a template class (perhaps with unbound template
21493 parameters). ARG is a fully instantiated type. If ARG can be
21494 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21495 TARGS are as for unify. */
21496
21497 static tree
21498 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21499 bool explain_p)
21500 {
21501 tree copy_of_targs;
21502
21503 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21504 return NULL_TREE;
21505 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21506 /* Matches anything. */;
21507 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21508 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21509 return NULL_TREE;
21510
21511 /* We need to make a new template argument vector for the call to
21512 unify. If we used TARGS, we'd clutter it up with the result of
21513 the attempted unification, even if this class didn't work out.
21514 We also don't want to commit ourselves to all the unifications
21515 we've already done, since unification is supposed to be done on
21516 an argument-by-argument basis. In other words, consider the
21517 following pathological case:
21518
21519 template <int I, int J, int K>
21520 struct S {};
21521
21522 template <int I, int J>
21523 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21524
21525 template <int I, int J, int K>
21526 void f(S<I, J, K>, S<I, I, I>);
21527
21528 void g() {
21529 S<0, 0, 0> s0;
21530 S<0, 1, 2> s2;
21531
21532 f(s0, s2);
21533 }
21534
21535 Now, by the time we consider the unification involving `s2', we
21536 already know that we must have `f<0, 0, 0>'. But, even though
21537 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21538 because there are two ways to unify base classes of S<0, 1, 2>
21539 with S<I, I, I>. If we kept the already deduced knowledge, we
21540 would reject the possibility I=1. */
21541 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21542
21543 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21544 {
21545 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21546 return NULL_TREE;
21547 return arg;
21548 }
21549
21550 /* If unification failed, we're done. */
21551 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21552 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21553 return NULL_TREE;
21554
21555 return arg;
21556 }
21557
21558 /* Given a template type PARM and a class type ARG, find the unique
21559 base type in ARG that is an instance of PARM. We do not examine
21560 ARG itself; only its base-classes. If there is not exactly one
21561 appropriate base class, return NULL_TREE. PARM may be the type of
21562 a partial specialization, as well as a plain template type. Used
21563 by unify. */
21564
21565 static enum template_base_result
21566 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21567 bool explain_p, tree *result)
21568 {
21569 tree rval = NULL_TREE;
21570 tree binfo;
21571
21572 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21573
21574 binfo = TYPE_BINFO (complete_type (arg));
21575 if (!binfo)
21576 {
21577 /* The type could not be completed. */
21578 *result = NULL_TREE;
21579 return tbr_incomplete_type;
21580 }
21581
21582 /* Walk in inheritance graph order. The search order is not
21583 important, and this avoids multiple walks of virtual bases. */
21584 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21585 {
21586 tree r = try_class_unification (tparms, targs, parm,
21587 BINFO_TYPE (binfo), explain_p);
21588
21589 if (r)
21590 {
21591 /* If there is more than one satisfactory baseclass, then:
21592
21593 [temp.deduct.call]
21594
21595 If they yield more than one possible deduced A, the type
21596 deduction fails.
21597
21598 applies. */
21599 if (rval && !same_type_p (r, rval))
21600 {
21601 *result = NULL_TREE;
21602 return tbr_ambiguous_baseclass;
21603 }
21604
21605 rval = r;
21606 }
21607 }
21608
21609 *result = rval;
21610 return tbr_success;
21611 }
21612
21613 /* Returns the level of DECL, which declares a template parameter. */
21614
21615 static int
21616 template_decl_level (tree decl)
21617 {
21618 switch (TREE_CODE (decl))
21619 {
21620 case TYPE_DECL:
21621 case TEMPLATE_DECL:
21622 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21623
21624 case PARM_DECL:
21625 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21626
21627 default:
21628 gcc_unreachable ();
21629 }
21630 return 0;
21631 }
21632
21633 /* Decide whether ARG can be unified with PARM, considering only the
21634 cv-qualifiers of each type, given STRICT as documented for unify.
21635 Returns nonzero iff the unification is OK on that basis. */
21636
21637 static int
21638 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21639 {
21640 int arg_quals = cp_type_quals (arg);
21641 int parm_quals = cp_type_quals (parm);
21642
21643 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21644 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21645 {
21646 /* Although a CVR qualifier is ignored when being applied to a
21647 substituted template parameter ([8.3.2]/1 for example), that
21648 does not allow us to unify "const T" with "int&" because both
21649 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21650 It is ok when we're allowing additional CV qualifiers
21651 at the outer level [14.8.2.1]/3,1st bullet. */
21652 if ((TYPE_REF_P (arg)
21653 || FUNC_OR_METHOD_TYPE_P (arg))
21654 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21655 return 0;
21656
21657 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21658 && (parm_quals & TYPE_QUAL_RESTRICT))
21659 return 0;
21660 }
21661
21662 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21663 && (arg_quals & parm_quals) != parm_quals)
21664 return 0;
21665
21666 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21667 && (parm_quals & arg_quals) != arg_quals)
21668 return 0;
21669
21670 return 1;
21671 }
21672
21673 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21674 void
21675 template_parm_level_and_index (tree parm, int* level, int* index)
21676 {
21677 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21678 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21679 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21680 {
21681 *index = TEMPLATE_TYPE_IDX (parm);
21682 *level = TEMPLATE_TYPE_LEVEL (parm);
21683 }
21684 else
21685 {
21686 *index = TEMPLATE_PARM_IDX (parm);
21687 *level = TEMPLATE_PARM_LEVEL (parm);
21688 }
21689 }
21690
21691 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21692 do { \
21693 if (unify (TP, TA, P, A, S, EP)) \
21694 return 1; \
21695 } while (0)
21696
21697 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21698 expansion at the end of PACKED_PARMS. Returns 0 if the type
21699 deduction succeeds, 1 otherwise. STRICT is the same as in
21700 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21701 function call argument list. We'll need to adjust the arguments to make them
21702 types. SUBR tells us if this is from a recursive call to
21703 type_unification_real, or for comparing two template argument
21704 lists. */
21705
21706 static int
21707 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21708 tree packed_args, unification_kind_t strict,
21709 bool subr, bool explain_p)
21710 {
21711 tree parm
21712 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21713 tree pattern = PACK_EXPANSION_PATTERN (parm);
21714 tree pack, packs = NULL_TREE;
21715 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21716
21717 /* Add in any args remembered from an earlier partial instantiation. */
21718 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21719 int levels = TMPL_ARGS_DEPTH (targs);
21720
21721 packed_args = expand_template_argument_pack (packed_args);
21722
21723 int len = TREE_VEC_LENGTH (packed_args);
21724
21725 /* Determine the parameter packs we will be deducing from the
21726 pattern, and record their current deductions. */
21727 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21728 pack; pack = TREE_CHAIN (pack))
21729 {
21730 tree parm_pack = TREE_VALUE (pack);
21731 int idx, level;
21732
21733 /* Only template parameter packs can be deduced, not e.g. function
21734 parameter packs or __bases or __integer_pack. */
21735 if (!TEMPLATE_PARM_P (parm_pack))
21736 continue;
21737
21738 /* Determine the index and level of this parameter pack. */
21739 template_parm_level_and_index (parm_pack, &level, &idx);
21740 if (level < levels)
21741 continue;
21742
21743 /* Keep track of the parameter packs and their corresponding
21744 argument packs. */
21745 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21746 TREE_TYPE (packs) = make_tree_vec (len - start);
21747 }
21748
21749 /* Loop through all of the arguments that have not yet been
21750 unified and unify each with the pattern. */
21751 for (i = start; i < len; i++)
21752 {
21753 tree parm;
21754 bool any_explicit = false;
21755 tree arg = TREE_VEC_ELT (packed_args, i);
21756
21757 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21758 or the element of its argument pack at the current index if
21759 this argument was explicitly specified. */
21760 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21761 {
21762 int idx, level;
21763 tree arg, pargs;
21764 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21765
21766 arg = NULL_TREE;
21767 if (TREE_VALUE (pack)
21768 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21769 && (i - start < TREE_VEC_LENGTH (pargs)))
21770 {
21771 any_explicit = true;
21772 arg = TREE_VEC_ELT (pargs, i - start);
21773 }
21774 TMPL_ARG (targs, level, idx) = arg;
21775 }
21776
21777 /* If we had explicit template arguments, substitute them into the
21778 pattern before deduction. */
21779 if (any_explicit)
21780 {
21781 /* Some arguments might still be unspecified or dependent. */
21782 bool dependent;
21783 ++processing_template_decl;
21784 dependent = any_dependent_template_arguments_p (targs);
21785 if (!dependent)
21786 --processing_template_decl;
21787 parm = tsubst (pattern, targs,
21788 explain_p ? tf_warning_or_error : tf_none,
21789 NULL_TREE);
21790 if (dependent)
21791 --processing_template_decl;
21792 if (parm == error_mark_node)
21793 return 1;
21794 }
21795 else
21796 parm = pattern;
21797
21798 /* Unify the pattern with the current argument. */
21799 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21800 explain_p))
21801 return 1;
21802
21803 /* For each parameter pack, collect the deduced value. */
21804 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21805 {
21806 int idx, level;
21807 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21808
21809 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21810 TMPL_ARG (targs, level, idx);
21811 }
21812 }
21813
21814 /* Verify that the results of unification with the parameter packs
21815 produce results consistent with what we've seen before, and make
21816 the deduced argument packs available. */
21817 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21818 {
21819 tree old_pack = TREE_VALUE (pack);
21820 tree new_args = TREE_TYPE (pack);
21821 int i, len = TREE_VEC_LENGTH (new_args);
21822 int idx, level;
21823 bool nondeduced_p = false;
21824
21825 /* By default keep the original deduced argument pack.
21826 If necessary, more specific code is going to update the
21827 resulting deduced argument later down in this function. */
21828 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21829 TMPL_ARG (targs, level, idx) = old_pack;
21830
21831 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21832 actually deduce anything. */
21833 for (i = 0; i < len && !nondeduced_p; ++i)
21834 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21835 nondeduced_p = true;
21836 if (nondeduced_p)
21837 continue;
21838
21839 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21840 {
21841 /* If we had fewer function args than explicit template args,
21842 just use the explicits. */
21843 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21844 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21845 if (len < explicit_len)
21846 new_args = explicit_args;
21847 }
21848
21849 if (!old_pack)
21850 {
21851 tree result;
21852 /* Build the deduced *_ARGUMENT_PACK. */
21853 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21854 {
21855 result = make_node (NONTYPE_ARGUMENT_PACK);
21856 TREE_CONSTANT (result) = 1;
21857 }
21858 else
21859 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21860
21861 SET_ARGUMENT_PACK_ARGS (result, new_args);
21862
21863 /* Note the deduced argument packs for this parameter
21864 pack. */
21865 TMPL_ARG (targs, level, idx) = result;
21866 }
21867 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21868 && (ARGUMENT_PACK_ARGS (old_pack)
21869 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21870 {
21871 /* We only had the explicitly-provided arguments before, but
21872 now we have a complete set of arguments. */
21873 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21874
21875 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21876 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21877 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21878 }
21879 else
21880 {
21881 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21882 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21883
21884 if (!comp_template_args (old_args, new_args,
21885 &bad_old_arg, &bad_new_arg))
21886 /* Inconsistent unification of this parameter pack. */
21887 return unify_parameter_pack_inconsistent (explain_p,
21888 bad_old_arg,
21889 bad_new_arg);
21890 }
21891 }
21892
21893 return unify_success (explain_p);
21894 }
21895
21896 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21897 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21898 parameters and return value are as for unify. */
21899
21900 static int
21901 unify_array_domain (tree tparms, tree targs,
21902 tree parm_dom, tree arg_dom,
21903 bool explain_p)
21904 {
21905 tree parm_max;
21906 tree arg_max;
21907 bool parm_cst;
21908 bool arg_cst;
21909
21910 /* Our representation of array types uses "N - 1" as the
21911 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21912 not an integer constant. We cannot unify arbitrarily
21913 complex expressions, so we eliminate the MINUS_EXPRs
21914 here. */
21915 parm_max = TYPE_MAX_VALUE (parm_dom);
21916 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21917 if (!parm_cst)
21918 {
21919 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21920 parm_max = TREE_OPERAND (parm_max, 0);
21921 }
21922 arg_max = TYPE_MAX_VALUE (arg_dom);
21923 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21924 if (!arg_cst)
21925 {
21926 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21927 trying to unify the type of a variable with the type
21928 of a template parameter. For example:
21929
21930 template <unsigned int N>
21931 void f (char (&) [N]);
21932 int g();
21933 void h(int i) {
21934 char a[g(i)];
21935 f(a);
21936 }
21937
21938 Here, the type of the ARG will be "int [g(i)]", and
21939 may be a SAVE_EXPR, etc. */
21940 if (TREE_CODE (arg_max) != MINUS_EXPR)
21941 return unify_vla_arg (explain_p, arg_dom);
21942 arg_max = TREE_OPERAND (arg_max, 0);
21943 }
21944
21945 /* If only one of the bounds used a MINUS_EXPR, compensate
21946 by adding one to the other bound. */
21947 if (parm_cst && !arg_cst)
21948 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21949 integer_type_node,
21950 parm_max,
21951 integer_one_node);
21952 else if (arg_cst && !parm_cst)
21953 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21954 integer_type_node,
21955 arg_max,
21956 integer_one_node);
21957
21958 return unify (tparms, targs, parm_max, arg_max,
21959 UNIFY_ALLOW_INTEGER, explain_p);
21960 }
21961
21962 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21963
21964 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21965
21966 static pa_kind_t
21967 pa_kind (tree t)
21968 {
21969 if (PACK_EXPANSION_P (t))
21970 t = PACK_EXPANSION_PATTERN (t);
21971 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21972 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21973 || DECL_TYPE_TEMPLATE_P (t))
21974 return pa_tmpl;
21975 else if (TYPE_P (t))
21976 return pa_type;
21977 else
21978 return pa_expr;
21979 }
21980
21981 /* Deduce the value of template parameters. TPARMS is the (innermost)
21982 set of template parameters to a template. TARGS is the bindings
21983 for those template parameters, as determined thus far; TARGS may
21984 include template arguments for outer levels of template parameters
21985 as well. PARM is a parameter to a template function, or a
21986 subcomponent of that parameter; ARG is the corresponding argument.
21987 This function attempts to match PARM with ARG in a manner
21988 consistent with the existing assignments in TARGS. If more values
21989 are deduced, then TARGS is updated.
21990
21991 Returns 0 if the type deduction succeeds, 1 otherwise. The
21992 parameter STRICT is a bitwise or of the following flags:
21993
21994 UNIFY_ALLOW_NONE:
21995 Require an exact match between PARM and ARG.
21996 UNIFY_ALLOW_MORE_CV_QUAL:
21997 Allow the deduced ARG to be more cv-qualified (by qualification
21998 conversion) than ARG.
21999 UNIFY_ALLOW_LESS_CV_QUAL:
22000 Allow the deduced ARG to be less cv-qualified than ARG.
22001 UNIFY_ALLOW_DERIVED:
22002 Allow the deduced ARG to be a template base class of ARG,
22003 or a pointer to a template base class of the type pointed to by
22004 ARG.
22005 UNIFY_ALLOW_INTEGER:
22006 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22007 case for more information.
22008 UNIFY_ALLOW_OUTER_LEVEL:
22009 This is the outermost level of a deduction. Used to determine validity
22010 of qualification conversions. A valid qualification conversion must
22011 have const qualified pointers leading up to the inner type which
22012 requires additional CV quals, except at the outer level, where const
22013 is not required [conv.qual]. It would be normal to set this flag in
22014 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22015 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22016 This is the outermost level of a deduction, and PARM can be more CV
22017 qualified at this point.
22018 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22019 This is the outermost level of a deduction, and PARM can be less CV
22020 qualified at this point. */
22021
22022 static int
22023 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22024 bool explain_p)
22025 {
22026 int idx;
22027 tree targ;
22028 tree tparm;
22029 int strict_in = strict;
22030 tsubst_flags_t complain = (explain_p
22031 ? tf_warning_or_error
22032 : tf_none);
22033
22034 /* I don't think this will do the right thing with respect to types.
22035 But the only case I've seen it in so far has been array bounds, where
22036 signedness is the only information lost, and I think that will be
22037 okay. */
22038 while (CONVERT_EXPR_P (parm))
22039 parm = TREE_OPERAND (parm, 0);
22040
22041 if (arg == error_mark_node)
22042 return unify_invalid (explain_p);
22043 if (arg == unknown_type_node
22044 || arg == init_list_type_node)
22045 /* We can't deduce anything from this, but we might get all the
22046 template args from other function args. */
22047 return unify_success (explain_p);
22048
22049 if (parm == any_targ_node || arg == any_targ_node)
22050 return unify_success (explain_p);
22051
22052 /* If PARM uses template parameters, then we can't bail out here,
22053 even if ARG == PARM, since we won't record unifications for the
22054 template parameters. We might need them if we're trying to
22055 figure out which of two things is more specialized. */
22056 if (arg == parm && !uses_template_parms (parm))
22057 return unify_success (explain_p);
22058
22059 /* Handle init lists early, so the rest of the function can assume
22060 we're dealing with a type. */
22061 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22062 {
22063 tree elt, elttype;
22064 unsigned i;
22065 tree orig_parm = parm;
22066
22067 /* Replace T with std::initializer_list<T> for deduction. */
22068 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22069 && flag_deduce_init_list)
22070 parm = listify (parm);
22071
22072 if (!is_std_init_list (parm)
22073 && TREE_CODE (parm) != ARRAY_TYPE)
22074 /* We can only deduce from an initializer list argument if the
22075 parameter is std::initializer_list or an array; otherwise this
22076 is a non-deduced context. */
22077 return unify_success (explain_p);
22078
22079 if (TREE_CODE (parm) == ARRAY_TYPE)
22080 elttype = TREE_TYPE (parm);
22081 else
22082 {
22083 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22084 /* Deduction is defined in terms of a single type, so just punt
22085 on the (bizarre) std::initializer_list<T...>. */
22086 if (PACK_EXPANSION_P (elttype))
22087 return unify_success (explain_p);
22088 }
22089
22090 if (strict != DEDUCE_EXACT
22091 && TYPE_P (elttype)
22092 && !uses_deducible_template_parms (elttype))
22093 /* If ELTTYPE has no deducible template parms, skip deduction from
22094 the list elements. */;
22095 else
22096 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22097 {
22098 int elt_strict = strict;
22099
22100 if (elt == error_mark_node)
22101 return unify_invalid (explain_p);
22102
22103 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22104 {
22105 tree type = TREE_TYPE (elt);
22106 if (type == error_mark_node)
22107 return unify_invalid (explain_p);
22108 /* It should only be possible to get here for a call. */
22109 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22110 elt_strict |= maybe_adjust_types_for_deduction
22111 (DEDUCE_CALL, &elttype, &type, elt);
22112 elt = type;
22113 }
22114
22115 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22116 explain_p);
22117 }
22118
22119 if (TREE_CODE (parm) == ARRAY_TYPE
22120 && deducible_array_bound (TYPE_DOMAIN (parm)))
22121 {
22122 /* Also deduce from the length of the initializer list. */
22123 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22124 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22125 if (idx == error_mark_node)
22126 return unify_invalid (explain_p);
22127 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22128 idx, explain_p);
22129 }
22130
22131 /* If the std::initializer_list<T> deduction worked, replace the
22132 deduced A with std::initializer_list<A>. */
22133 if (orig_parm != parm)
22134 {
22135 idx = TEMPLATE_TYPE_IDX (orig_parm);
22136 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22137 targ = listify (targ);
22138 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22139 }
22140 return unify_success (explain_p);
22141 }
22142
22143 /* If parm and arg aren't the same kind of thing (template, type, or
22144 expression), fail early. */
22145 if (pa_kind (parm) != pa_kind (arg))
22146 return unify_invalid (explain_p);
22147
22148 /* Immediately reject some pairs that won't unify because of
22149 cv-qualification mismatches. */
22150 if (TREE_CODE (arg) == TREE_CODE (parm)
22151 && TYPE_P (arg)
22152 /* It is the elements of the array which hold the cv quals of an array
22153 type, and the elements might be template type parms. We'll check
22154 when we recurse. */
22155 && TREE_CODE (arg) != ARRAY_TYPE
22156 /* We check the cv-qualifiers when unifying with template type
22157 parameters below. We want to allow ARG `const T' to unify with
22158 PARM `T' for example, when computing which of two templates
22159 is more specialized, for example. */
22160 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22161 && !check_cv_quals_for_unify (strict_in, arg, parm))
22162 return unify_cv_qual_mismatch (explain_p, parm, arg);
22163
22164 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22165 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22166 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22167 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22168 strict &= ~UNIFY_ALLOW_DERIVED;
22169 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22170 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22171
22172 switch (TREE_CODE (parm))
22173 {
22174 case TYPENAME_TYPE:
22175 case SCOPE_REF:
22176 case UNBOUND_CLASS_TEMPLATE:
22177 /* In a type which contains a nested-name-specifier, template
22178 argument values cannot be deduced for template parameters used
22179 within the nested-name-specifier. */
22180 return unify_success (explain_p);
22181
22182 case TEMPLATE_TYPE_PARM:
22183 case TEMPLATE_TEMPLATE_PARM:
22184 case BOUND_TEMPLATE_TEMPLATE_PARM:
22185 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22186 if (error_operand_p (tparm))
22187 return unify_invalid (explain_p);
22188
22189 if (TEMPLATE_TYPE_LEVEL (parm)
22190 != template_decl_level (tparm))
22191 /* The PARM is not one we're trying to unify. Just check
22192 to see if it matches ARG. */
22193 {
22194 if (TREE_CODE (arg) == TREE_CODE (parm)
22195 && (is_auto (parm) ? is_auto (arg)
22196 : same_type_p (parm, arg)))
22197 return unify_success (explain_p);
22198 else
22199 return unify_type_mismatch (explain_p, parm, arg);
22200 }
22201 idx = TEMPLATE_TYPE_IDX (parm);
22202 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22203 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22204 if (error_operand_p (tparm))
22205 return unify_invalid (explain_p);
22206
22207 /* Check for mixed types and values. */
22208 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22209 && TREE_CODE (tparm) != TYPE_DECL)
22210 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22211 && TREE_CODE (tparm) != TEMPLATE_DECL))
22212 gcc_unreachable ();
22213
22214 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22215 {
22216 if ((strict_in & UNIFY_ALLOW_DERIVED)
22217 && CLASS_TYPE_P (arg))
22218 {
22219 /* First try to match ARG directly. */
22220 tree t = try_class_unification (tparms, targs, parm, arg,
22221 explain_p);
22222 if (!t)
22223 {
22224 /* Otherwise, look for a suitable base of ARG, as below. */
22225 enum template_base_result r;
22226 r = get_template_base (tparms, targs, parm, arg,
22227 explain_p, &t);
22228 if (!t)
22229 return unify_no_common_base (explain_p, r, parm, arg);
22230 arg = t;
22231 }
22232 }
22233 /* ARG must be constructed from a template class or a template
22234 template parameter. */
22235 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22236 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22237 return unify_template_deduction_failure (explain_p, parm, arg);
22238
22239 /* Deduce arguments T, i from TT<T> or TT<i>. */
22240 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22241 return 1;
22242
22243 arg = TYPE_TI_TEMPLATE (arg);
22244
22245 /* Fall through to deduce template name. */
22246 }
22247
22248 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22249 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22250 {
22251 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22252
22253 /* Simple cases: Value already set, does match or doesn't. */
22254 if (targ != NULL_TREE && template_args_equal (targ, arg))
22255 return unify_success (explain_p);
22256 else if (targ)
22257 return unify_inconsistency (explain_p, parm, targ, arg);
22258 }
22259 else
22260 {
22261 /* If PARM is `const T' and ARG is only `int', we don't have
22262 a match unless we are allowing additional qualification.
22263 If ARG is `const int' and PARM is just `T' that's OK;
22264 that binds `const int' to `T'. */
22265 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22266 arg, parm))
22267 return unify_cv_qual_mismatch (explain_p, parm, arg);
22268
22269 /* Consider the case where ARG is `const volatile int' and
22270 PARM is `const T'. Then, T should be `volatile int'. */
22271 arg = cp_build_qualified_type_real
22272 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22273 if (arg == error_mark_node)
22274 return unify_invalid (explain_p);
22275
22276 /* Simple cases: Value already set, does match or doesn't. */
22277 if (targ != NULL_TREE && same_type_p (targ, arg))
22278 return unify_success (explain_p);
22279 else if (targ)
22280 return unify_inconsistency (explain_p, parm, targ, arg);
22281
22282 /* Make sure that ARG is not a variable-sized array. (Note
22283 that were talking about variable-sized arrays (like
22284 `int[n]'), rather than arrays of unknown size (like
22285 `int[]').) We'll get very confused by such a type since
22286 the bound of the array is not constant, and therefore
22287 not mangleable. Besides, such types are not allowed in
22288 ISO C++, so we can do as we please here. We do allow
22289 them for 'auto' deduction, since that isn't ABI-exposed. */
22290 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22291 return unify_vla_arg (explain_p, arg);
22292
22293 /* Strip typedefs as in convert_template_argument. */
22294 arg = canonicalize_type_argument (arg, tf_none);
22295 }
22296
22297 /* If ARG is a parameter pack or an expansion, we cannot unify
22298 against it unless PARM is also a parameter pack. */
22299 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22300 && !template_parameter_pack_p (parm))
22301 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22302
22303 /* If the argument deduction results is a METHOD_TYPE,
22304 then there is a problem.
22305 METHOD_TYPE doesn't map to any real C++ type the result of
22306 the deduction cannot be of that type. */
22307 if (TREE_CODE (arg) == METHOD_TYPE)
22308 return unify_method_type_error (explain_p, arg);
22309
22310 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22311 return unify_success (explain_p);
22312
22313 case TEMPLATE_PARM_INDEX:
22314 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22315 if (error_operand_p (tparm))
22316 return unify_invalid (explain_p);
22317
22318 if (TEMPLATE_PARM_LEVEL (parm)
22319 != template_decl_level (tparm))
22320 {
22321 /* The PARM is not one we're trying to unify. Just check
22322 to see if it matches ARG. */
22323 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22324 && cp_tree_equal (parm, arg));
22325 if (result)
22326 unify_expression_unequal (explain_p, parm, arg);
22327 return result;
22328 }
22329
22330 idx = TEMPLATE_PARM_IDX (parm);
22331 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22332
22333 if (targ)
22334 {
22335 if ((strict & UNIFY_ALLOW_INTEGER)
22336 && TREE_TYPE (targ) && TREE_TYPE (arg)
22337 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22338 /* We're deducing from an array bound, the type doesn't matter. */
22339 arg = fold_convert (TREE_TYPE (targ), arg);
22340 int x = !cp_tree_equal (targ, arg);
22341 if (x)
22342 unify_inconsistency (explain_p, parm, targ, arg);
22343 return x;
22344 }
22345
22346 /* [temp.deduct.type] If, in the declaration of a function template
22347 with a non-type template-parameter, the non-type
22348 template-parameter is used in an expression in the function
22349 parameter-list and, if the corresponding template-argument is
22350 deduced, the template-argument type shall match the type of the
22351 template-parameter exactly, except that a template-argument
22352 deduced from an array bound may be of any integral type.
22353 The non-type parameter might use already deduced type parameters. */
22354 tparm = TREE_TYPE (parm);
22355 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22356 /* We don't have enough levels of args to do any substitution. This
22357 can happen in the context of -fnew-ttp-matching. */;
22358 else
22359 {
22360 ++processing_template_decl;
22361 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22362 --processing_template_decl;
22363
22364 if (tree a = type_uses_auto (tparm))
22365 {
22366 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22367 if (tparm == error_mark_node)
22368 return 1;
22369 }
22370 }
22371
22372 if (!TREE_TYPE (arg))
22373 /* Template-parameter dependent expression. Just accept it for now.
22374 It will later be processed in convert_template_argument. */
22375 ;
22376 else if (same_type_ignoring_top_level_qualifiers_p
22377 (non_reference (TREE_TYPE (arg)),
22378 non_reference (tparm)))
22379 /* OK. Ignore top-level quals here because a class-type template
22380 parameter object is const. */;
22381 else if ((strict & UNIFY_ALLOW_INTEGER)
22382 && CP_INTEGRAL_TYPE_P (tparm))
22383 /* Convert the ARG to the type of PARM; the deduced non-type
22384 template argument must exactly match the types of the
22385 corresponding parameter. */
22386 arg = fold (build_nop (tparm, arg));
22387 else if (uses_template_parms (tparm))
22388 {
22389 /* We haven't deduced the type of this parameter yet. */
22390 if (cxx_dialect >= cxx17
22391 /* We deduce from array bounds in try_array_deduction. */
22392 && !(strict & UNIFY_ALLOW_INTEGER))
22393 {
22394 /* Deduce it from the non-type argument. */
22395 tree atype = TREE_TYPE (arg);
22396 RECUR_AND_CHECK_FAILURE (tparms, targs,
22397 tparm, atype,
22398 UNIFY_ALLOW_NONE, explain_p);
22399 }
22400 else
22401 /* Try again later. */
22402 return unify_success (explain_p);
22403 }
22404 else
22405 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22406
22407 /* If ARG is a parameter pack or an expansion, we cannot unify
22408 against it unless PARM is also a parameter pack. */
22409 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22410 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22411 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22412
22413 {
22414 bool removed_attr = false;
22415 arg = strip_typedefs_expr (arg, &removed_attr);
22416 }
22417 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22418 return unify_success (explain_p);
22419
22420 case PTRMEM_CST:
22421 {
22422 /* A pointer-to-member constant can be unified only with
22423 another constant. */
22424 if (TREE_CODE (arg) != PTRMEM_CST)
22425 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22426
22427 /* Just unify the class member. It would be useless (and possibly
22428 wrong, depending on the strict flags) to unify also
22429 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22430 arg refer to the same variable, even if through different
22431 classes. For instance:
22432
22433 struct A { int x; };
22434 struct B : A { };
22435
22436 Unification of &A::x and &B::x must succeed. */
22437 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22438 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22439 }
22440
22441 case POINTER_TYPE:
22442 {
22443 if (!TYPE_PTR_P (arg))
22444 return unify_type_mismatch (explain_p, parm, arg);
22445
22446 /* [temp.deduct.call]
22447
22448 A can be another pointer or pointer to member type that can
22449 be converted to the deduced A via a qualification
22450 conversion (_conv.qual_).
22451
22452 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22453 This will allow for additional cv-qualification of the
22454 pointed-to types if appropriate. */
22455
22456 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22457 /* The derived-to-base conversion only persists through one
22458 level of pointers. */
22459 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22460
22461 return unify (tparms, targs, TREE_TYPE (parm),
22462 TREE_TYPE (arg), strict, explain_p);
22463 }
22464
22465 case REFERENCE_TYPE:
22466 if (!TYPE_REF_P (arg))
22467 return unify_type_mismatch (explain_p, parm, arg);
22468 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22469 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22470
22471 case ARRAY_TYPE:
22472 if (TREE_CODE (arg) != ARRAY_TYPE)
22473 return unify_type_mismatch (explain_p, parm, arg);
22474 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22475 != (TYPE_DOMAIN (arg) == NULL_TREE))
22476 return unify_type_mismatch (explain_p, parm, arg);
22477 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22478 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22479 if (TYPE_DOMAIN (parm) != NULL_TREE)
22480 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22481 TYPE_DOMAIN (arg), explain_p);
22482 return unify_success (explain_p);
22483
22484 case REAL_TYPE:
22485 case COMPLEX_TYPE:
22486 case VECTOR_TYPE:
22487 case INTEGER_TYPE:
22488 case BOOLEAN_TYPE:
22489 case ENUMERAL_TYPE:
22490 case VOID_TYPE:
22491 case NULLPTR_TYPE:
22492 if (TREE_CODE (arg) != TREE_CODE (parm))
22493 return unify_type_mismatch (explain_p, parm, arg);
22494
22495 /* We have already checked cv-qualification at the top of the
22496 function. */
22497 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22498 return unify_type_mismatch (explain_p, parm, arg);
22499
22500 /* As far as unification is concerned, this wins. Later checks
22501 will invalidate it if necessary. */
22502 return unify_success (explain_p);
22503
22504 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22505 /* Type INTEGER_CST can come from ordinary constant template args. */
22506 case INTEGER_CST:
22507 while (CONVERT_EXPR_P (arg))
22508 arg = TREE_OPERAND (arg, 0);
22509
22510 if (TREE_CODE (arg) != INTEGER_CST)
22511 return unify_template_argument_mismatch (explain_p, parm, arg);
22512 return (tree_int_cst_equal (parm, arg)
22513 ? unify_success (explain_p)
22514 : unify_template_argument_mismatch (explain_p, parm, arg));
22515
22516 case TREE_VEC:
22517 {
22518 int i, len, argslen;
22519 int parm_variadic_p = 0;
22520
22521 if (TREE_CODE (arg) != TREE_VEC)
22522 return unify_template_argument_mismatch (explain_p, parm, arg);
22523
22524 len = TREE_VEC_LENGTH (parm);
22525 argslen = TREE_VEC_LENGTH (arg);
22526
22527 /* Check for pack expansions in the parameters. */
22528 for (i = 0; i < len; ++i)
22529 {
22530 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22531 {
22532 if (i == len - 1)
22533 /* We can unify against something with a trailing
22534 parameter pack. */
22535 parm_variadic_p = 1;
22536 else
22537 /* [temp.deduct.type]/9: If the template argument list of
22538 P contains a pack expansion that is not the last
22539 template argument, the entire template argument list
22540 is a non-deduced context. */
22541 return unify_success (explain_p);
22542 }
22543 }
22544
22545 /* If we don't have enough arguments to satisfy the parameters
22546 (not counting the pack expression at the end), or we have
22547 too many arguments for a parameter list that doesn't end in
22548 a pack expression, we can't unify. */
22549 if (parm_variadic_p
22550 ? argslen < len - parm_variadic_p
22551 : argslen != len)
22552 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22553
22554 /* Unify all of the parameters that precede the (optional)
22555 pack expression. */
22556 for (i = 0; i < len - parm_variadic_p; ++i)
22557 {
22558 RECUR_AND_CHECK_FAILURE (tparms, targs,
22559 TREE_VEC_ELT (parm, i),
22560 TREE_VEC_ELT (arg, i),
22561 UNIFY_ALLOW_NONE, explain_p);
22562 }
22563 if (parm_variadic_p)
22564 return unify_pack_expansion (tparms, targs, parm, arg,
22565 DEDUCE_EXACT,
22566 /*subr=*/true, explain_p);
22567 return unify_success (explain_p);
22568 }
22569
22570 case RECORD_TYPE:
22571 case UNION_TYPE:
22572 if (TREE_CODE (arg) != TREE_CODE (parm))
22573 return unify_type_mismatch (explain_p, parm, arg);
22574
22575 if (TYPE_PTRMEMFUNC_P (parm))
22576 {
22577 if (!TYPE_PTRMEMFUNC_P (arg))
22578 return unify_type_mismatch (explain_p, parm, arg);
22579
22580 return unify (tparms, targs,
22581 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22582 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22583 strict, explain_p);
22584 }
22585 else if (TYPE_PTRMEMFUNC_P (arg))
22586 return unify_type_mismatch (explain_p, parm, arg);
22587
22588 if (CLASSTYPE_TEMPLATE_INFO (parm))
22589 {
22590 tree t = NULL_TREE;
22591
22592 if (strict_in & UNIFY_ALLOW_DERIVED)
22593 {
22594 /* First, we try to unify the PARM and ARG directly. */
22595 t = try_class_unification (tparms, targs,
22596 parm, arg, explain_p);
22597
22598 if (!t)
22599 {
22600 /* Fallback to the special case allowed in
22601 [temp.deduct.call]:
22602
22603 If P is a class, and P has the form
22604 template-id, then A can be a derived class of
22605 the deduced A. Likewise, if P is a pointer to
22606 a class of the form template-id, A can be a
22607 pointer to a derived class pointed to by the
22608 deduced A. */
22609 enum template_base_result r;
22610 r = get_template_base (tparms, targs, parm, arg,
22611 explain_p, &t);
22612
22613 if (!t)
22614 {
22615 /* Don't give the derived diagnostic if we're
22616 already dealing with the same template. */
22617 bool same_template
22618 = (CLASSTYPE_TEMPLATE_INFO (arg)
22619 && (CLASSTYPE_TI_TEMPLATE (parm)
22620 == CLASSTYPE_TI_TEMPLATE (arg)));
22621 return unify_no_common_base (explain_p && !same_template,
22622 r, parm, arg);
22623 }
22624 }
22625 }
22626 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22627 && (CLASSTYPE_TI_TEMPLATE (parm)
22628 == CLASSTYPE_TI_TEMPLATE (arg)))
22629 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22630 Then, we should unify `int' and `U'. */
22631 t = arg;
22632 else
22633 /* There's no chance of unification succeeding. */
22634 return unify_type_mismatch (explain_p, parm, arg);
22635
22636 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22637 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22638 }
22639 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22640 return unify_type_mismatch (explain_p, parm, arg);
22641 return unify_success (explain_p);
22642
22643 case METHOD_TYPE:
22644 case FUNCTION_TYPE:
22645 {
22646 unsigned int nargs;
22647 tree *args;
22648 tree a;
22649 unsigned int i;
22650
22651 if (TREE_CODE (arg) != TREE_CODE (parm))
22652 return unify_type_mismatch (explain_p, parm, arg);
22653
22654 /* CV qualifications for methods can never be deduced, they must
22655 match exactly. We need to check them explicitly here,
22656 because type_unification_real treats them as any other
22657 cv-qualified parameter. */
22658 if (TREE_CODE (parm) == METHOD_TYPE
22659 && (!check_cv_quals_for_unify
22660 (UNIFY_ALLOW_NONE,
22661 class_of_this_parm (arg),
22662 class_of_this_parm (parm))))
22663 return unify_cv_qual_mismatch (explain_p, parm, arg);
22664 if (TREE_CODE (arg) == FUNCTION_TYPE
22665 && type_memfn_quals (parm) != type_memfn_quals (arg))
22666 return unify_cv_qual_mismatch (explain_p, parm, arg);
22667 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22668 return unify_type_mismatch (explain_p, parm, arg);
22669
22670 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22671 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22672
22673 nargs = list_length (TYPE_ARG_TYPES (arg));
22674 args = XALLOCAVEC (tree, nargs);
22675 for (a = TYPE_ARG_TYPES (arg), i = 0;
22676 a != NULL_TREE && a != void_list_node;
22677 a = TREE_CHAIN (a), ++i)
22678 args[i] = TREE_VALUE (a);
22679 nargs = i;
22680
22681 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22682 args, nargs, 1, DEDUCE_EXACT,
22683 NULL, explain_p))
22684 return 1;
22685
22686 if (flag_noexcept_type)
22687 {
22688 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22689 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22690 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22691 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22692 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22693 && uses_template_parms (TREE_PURPOSE (pspec)))
22694 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22695 TREE_PURPOSE (aspec),
22696 UNIFY_ALLOW_NONE, explain_p);
22697 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22698 return unify_type_mismatch (explain_p, parm, arg);
22699 }
22700
22701 return 0;
22702 }
22703
22704 case OFFSET_TYPE:
22705 /* Unify a pointer to member with a pointer to member function, which
22706 deduces the type of the member as a function type. */
22707 if (TYPE_PTRMEMFUNC_P (arg))
22708 {
22709 /* Check top-level cv qualifiers */
22710 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22711 return unify_cv_qual_mismatch (explain_p, parm, arg);
22712
22713 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22714 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22715 UNIFY_ALLOW_NONE, explain_p);
22716
22717 /* Determine the type of the function we are unifying against. */
22718 tree fntype = static_fn_type (arg);
22719
22720 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22721 }
22722
22723 if (TREE_CODE (arg) != OFFSET_TYPE)
22724 return unify_type_mismatch (explain_p, parm, arg);
22725 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22726 TYPE_OFFSET_BASETYPE (arg),
22727 UNIFY_ALLOW_NONE, explain_p);
22728 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22729 strict, explain_p);
22730
22731 case CONST_DECL:
22732 if (DECL_TEMPLATE_PARM_P (parm))
22733 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22734 if (arg != scalar_constant_value (parm))
22735 return unify_template_argument_mismatch (explain_p, parm, arg);
22736 return unify_success (explain_p);
22737
22738 case FIELD_DECL:
22739 case TEMPLATE_DECL:
22740 /* Matched cases are handled by the ARG == PARM test above. */
22741 return unify_template_argument_mismatch (explain_p, parm, arg);
22742
22743 case VAR_DECL:
22744 /* We might get a variable as a non-type template argument in parm if the
22745 corresponding parameter is type-dependent. Make any necessary
22746 adjustments based on whether arg is a reference. */
22747 if (CONSTANT_CLASS_P (arg))
22748 parm = fold_non_dependent_expr (parm, complain);
22749 else if (REFERENCE_REF_P (arg))
22750 {
22751 tree sub = TREE_OPERAND (arg, 0);
22752 STRIP_NOPS (sub);
22753 if (TREE_CODE (sub) == ADDR_EXPR)
22754 arg = TREE_OPERAND (sub, 0);
22755 }
22756 /* Now use the normal expression code to check whether they match. */
22757 goto expr;
22758
22759 case TYPE_ARGUMENT_PACK:
22760 case NONTYPE_ARGUMENT_PACK:
22761 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22762 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22763
22764 case TYPEOF_TYPE:
22765 case DECLTYPE_TYPE:
22766 case UNDERLYING_TYPE:
22767 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22768 or UNDERLYING_TYPE nodes. */
22769 return unify_success (explain_p);
22770
22771 case ERROR_MARK:
22772 /* Unification fails if we hit an error node. */
22773 return unify_invalid (explain_p);
22774
22775 case INDIRECT_REF:
22776 if (REFERENCE_REF_P (parm))
22777 {
22778 bool pexp = PACK_EXPANSION_P (arg);
22779 if (pexp)
22780 arg = PACK_EXPANSION_PATTERN (arg);
22781 if (REFERENCE_REF_P (arg))
22782 arg = TREE_OPERAND (arg, 0);
22783 if (pexp)
22784 arg = make_pack_expansion (arg, complain);
22785 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22786 strict, explain_p);
22787 }
22788 /* FALLTHRU */
22789
22790 default:
22791 /* An unresolved overload is a nondeduced context. */
22792 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22793 return unify_success (explain_p);
22794 gcc_assert (EXPR_P (parm)
22795 || COMPOUND_LITERAL_P (parm)
22796 || TREE_CODE (parm) == TRAIT_EXPR);
22797 expr:
22798 /* We must be looking at an expression. This can happen with
22799 something like:
22800
22801 template <int I>
22802 void foo(S<I>, S<I + 2>);
22803
22804 or
22805
22806 template<typename T>
22807 void foo(A<T, T{}>);
22808
22809 This is a "non-deduced context":
22810
22811 [deduct.type]
22812
22813 The non-deduced contexts are:
22814
22815 --A non-type template argument or an array bound in which
22816 a subexpression references a template parameter.
22817
22818 In these cases, we assume deduction succeeded, but don't
22819 actually infer any unifications. */
22820
22821 if (!uses_template_parms (parm)
22822 && !template_args_equal (parm, arg))
22823 return unify_expression_unequal (explain_p, parm, arg);
22824 else
22825 return unify_success (explain_p);
22826 }
22827 }
22828 #undef RECUR_AND_CHECK_FAILURE
22829 \f
22830 /* Note that DECL can be defined in this translation unit, if
22831 required. */
22832
22833 static void
22834 mark_definable (tree decl)
22835 {
22836 tree clone;
22837 DECL_NOT_REALLY_EXTERN (decl) = 1;
22838 FOR_EACH_CLONE (clone, decl)
22839 DECL_NOT_REALLY_EXTERN (clone) = 1;
22840 }
22841
22842 /* Called if RESULT is explicitly instantiated, or is a member of an
22843 explicitly instantiated class. */
22844
22845 void
22846 mark_decl_instantiated (tree result, int extern_p)
22847 {
22848 SET_DECL_EXPLICIT_INSTANTIATION (result);
22849
22850 /* If this entity has already been written out, it's too late to
22851 make any modifications. */
22852 if (TREE_ASM_WRITTEN (result))
22853 return;
22854
22855 /* For anonymous namespace we don't need to do anything. */
22856 if (decl_anon_ns_mem_p (result))
22857 {
22858 gcc_assert (!TREE_PUBLIC (result));
22859 return;
22860 }
22861
22862 if (TREE_CODE (result) != FUNCTION_DECL)
22863 /* The TREE_PUBLIC flag for function declarations will have been
22864 set correctly by tsubst. */
22865 TREE_PUBLIC (result) = 1;
22866
22867 /* This might have been set by an earlier implicit instantiation. */
22868 DECL_COMDAT (result) = 0;
22869
22870 if (extern_p)
22871 DECL_NOT_REALLY_EXTERN (result) = 0;
22872 else
22873 {
22874 mark_definable (result);
22875 mark_needed (result);
22876 /* Always make artificials weak. */
22877 if (DECL_ARTIFICIAL (result) && flag_weak)
22878 comdat_linkage (result);
22879 /* For WIN32 we also want to put explicit instantiations in
22880 linkonce sections. */
22881 else if (TREE_PUBLIC (result))
22882 maybe_make_one_only (result);
22883 if (TREE_CODE (result) == FUNCTION_DECL
22884 && DECL_TEMPLATE_INSTANTIATED (result))
22885 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22886 since start_preparsed_function wouldn't have if we had an earlier
22887 extern explicit instantiation. */
22888 DECL_EXTERNAL (result) = 0;
22889 }
22890
22891 /* If EXTERN_P, then this function will not be emitted -- unless
22892 followed by an explicit instantiation, at which point its linkage
22893 will be adjusted. If !EXTERN_P, then this function will be
22894 emitted here. In neither circumstance do we want
22895 import_export_decl to adjust the linkage. */
22896 DECL_INTERFACE_KNOWN (result) = 1;
22897 }
22898
22899 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22900 important template arguments. If any are missing, we check whether
22901 they're important by using error_mark_node for substituting into any
22902 args that were used for partial ordering (the ones between ARGS and END)
22903 and seeing if it bubbles up. */
22904
22905 static bool
22906 check_undeduced_parms (tree targs, tree args, tree end)
22907 {
22908 bool found = false;
22909 int i;
22910 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22911 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22912 {
22913 found = true;
22914 TREE_VEC_ELT (targs, i) = error_mark_node;
22915 }
22916 if (found)
22917 {
22918 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22919 if (substed == error_mark_node)
22920 return true;
22921 }
22922 return false;
22923 }
22924
22925 /* Given two function templates PAT1 and PAT2, return:
22926
22927 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22928 -1 if PAT2 is more specialized than PAT1.
22929 0 if neither is more specialized.
22930
22931 LEN indicates the number of parameters we should consider
22932 (defaulted parameters should not be considered).
22933
22934 The 1998 std underspecified function template partial ordering, and
22935 DR214 addresses the issue. We take pairs of arguments, one from
22936 each of the templates, and deduce them against each other. One of
22937 the templates will be more specialized if all the *other*
22938 template's arguments deduce against its arguments and at least one
22939 of its arguments *does* *not* deduce against the other template's
22940 corresponding argument. Deduction is done as for class templates.
22941 The arguments used in deduction have reference and top level cv
22942 qualifiers removed. Iff both arguments were originally reference
22943 types *and* deduction succeeds in both directions, an lvalue reference
22944 wins against an rvalue reference and otherwise the template
22945 with the more cv-qualified argument wins for that pairing (if
22946 neither is more cv-qualified, they both are equal). Unlike regular
22947 deduction, after all the arguments have been deduced in this way,
22948 we do *not* verify the deduced template argument values can be
22949 substituted into non-deduced contexts.
22950
22951 The logic can be a bit confusing here, because we look at deduce1 and
22952 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22953 can find template arguments for pat1 to make arg1 look like arg2, that
22954 means that arg2 is at least as specialized as arg1. */
22955
22956 int
22957 more_specialized_fn (tree pat1, tree pat2, int len)
22958 {
22959 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22960 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22961 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22962 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22963 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22964 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22965 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22966 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22967 tree origs1, origs2;
22968 bool lose1 = false;
22969 bool lose2 = false;
22970
22971 /* Remove the this parameter from non-static member functions. If
22972 one is a non-static member function and the other is not a static
22973 member function, remove the first parameter from that function
22974 also. This situation occurs for operator functions where we
22975 locate both a member function (with this pointer) and non-member
22976 operator (with explicit first operand). */
22977 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22978 {
22979 len--; /* LEN is the number of significant arguments for DECL1 */
22980 args1 = TREE_CHAIN (args1);
22981 if (!DECL_STATIC_FUNCTION_P (decl2))
22982 args2 = TREE_CHAIN (args2);
22983 }
22984 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22985 {
22986 args2 = TREE_CHAIN (args2);
22987 if (!DECL_STATIC_FUNCTION_P (decl1))
22988 {
22989 len--;
22990 args1 = TREE_CHAIN (args1);
22991 }
22992 }
22993
22994 /* If only one is a conversion operator, they are unordered. */
22995 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22996 return 0;
22997
22998 /* Consider the return type for a conversion function */
22999 if (DECL_CONV_FN_P (decl1))
23000 {
23001 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23002 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23003 len++;
23004 }
23005
23006 processing_template_decl++;
23007
23008 origs1 = args1;
23009 origs2 = args2;
23010
23011 while (len--
23012 /* Stop when an ellipsis is seen. */
23013 && args1 != NULL_TREE && args2 != NULL_TREE)
23014 {
23015 tree arg1 = TREE_VALUE (args1);
23016 tree arg2 = TREE_VALUE (args2);
23017 int deduce1, deduce2;
23018 int quals1 = -1;
23019 int quals2 = -1;
23020 int ref1 = 0;
23021 int ref2 = 0;
23022
23023 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23024 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23025 {
23026 /* When both arguments are pack expansions, we need only
23027 unify the patterns themselves. */
23028 arg1 = PACK_EXPANSION_PATTERN (arg1);
23029 arg2 = PACK_EXPANSION_PATTERN (arg2);
23030
23031 /* This is the last comparison we need to do. */
23032 len = 0;
23033 }
23034
23035 /* DR 1847: If a particular P contains no template-parameters that
23036 participate in template argument deduction, that P is not used to
23037 determine the ordering. */
23038 if (!uses_deducible_template_parms (arg1)
23039 && !uses_deducible_template_parms (arg2))
23040 goto next;
23041
23042 if (TYPE_REF_P (arg1))
23043 {
23044 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23045 arg1 = TREE_TYPE (arg1);
23046 quals1 = cp_type_quals (arg1);
23047 }
23048
23049 if (TYPE_REF_P (arg2))
23050 {
23051 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23052 arg2 = TREE_TYPE (arg2);
23053 quals2 = cp_type_quals (arg2);
23054 }
23055
23056 arg1 = TYPE_MAIN_VARIANT (arg1);
23057 arg2 = TYPE_MAIN_VARIANT (arg2);
23058
23059 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23060 {
23061 int i, len2 = remaining_arguments (args2);
23062 tree parmvec = make_tree_vec (1);
23063 tree argvec = make_tree_vec (len2);
23064 tree ta = args2;
23065
23066 /* Setup the parameter vector, which contains only ARG1. */
23067 TREE_VEC_ELT (parmvec, 0) = arg1;
23068
23069 /* Setup the argument vector, which contains the remaining
23070 arguments. */
23071 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23072 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23073
23074 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23075 argvec, DEDUCE_EXACT,
23076 /*subr=*/true, /*explain_p=*/false)
23077 == 0);
23078
23079 /* We cannot deduce in the other direction, because ARG1 is
23080 a pack expansion but ARG2 is not. */
23081 deduce2 = 0;
23082 }
23083 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23084 {
23085 int i, len1 = remaining_arguments (args1);
23086 tree parmvec = make_tree_vec (1);
23087 tree argvec = make_tree_vec (len1);
23088 tree ta = args1;
23089
23090 /* Setup the parameter vector, which contains only ARG1. */
23091 TREE_VEC_ELT (parmvec, 0) = arg2;
23092
23093 /* Setup the argument vector, which contains the remaining
23094 arguments. */
23095 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23096 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23097
23098 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23099 argvec, DEDUCE_EXACT,
23100 /*subr=*/true, /*explain_p=*/false)
23101 == 0);
23102
23103 /* We cannot deduce in the other direction, because ARG2 is
23104 a pack expansion but ARG1 is not.*/
23105 deduce1 = 0;
23106 }
23107
23108 else
23109 {
23110 /* The normal case, where neither argument is a pack
23111 expansion. */
23112 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23113 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23114 == 0);
23115 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23116 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23117 == 0);
23118 }
23119
23120 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23121 arg2, then arg2 is not as specialized as arg1. */
23122 if (!deduce1)
23123 lose2 = true;
23124 if (!deduce2)
23125 lose1 = true;
23126
23127 /* "If, for a given type, deduction succeeds in both directions
23128 (i.e., the types are identical after the transformations above)
23129 and both P and A were reference types (before being replaced with
23130 the type referred to above):
23131 - if the type from the argument template was an lvalue reference and
23132 the type from the parameter template was not, the argument type is
23133 considered to be more specialized than the other; otherwise,
23134 - if the type from the argument template is more cv-qualified
23135 than the type from the parameter template (as described above),
23136 the argument type is considered to be more specialized than the other;
23137 otherwise,
23138 - neither type is more specialized than the other." */
23139
23140 if (deduce1 && deduce2)
23141 {
23142 if (ref1 && ref2 && ref1 != ref2)
23143 {
23144 if (ref1 > ref2)
23145 lose1 = true;
23146 else
23147 lose2 = true;
23148 }
23149 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23150 {
23151 if ((quals1 & quals2) == quals2)
23152 lose2 = true;
23153 if ((quals1 & quals2) == quals1)
23154 lose1 = true;
23155 }
23156 }
23157
23158 if (lose1 && lose2)
23159 /* We've failed to deduce something in either direction.
23160 These must be unordered. */
23161 break;
23162
23163 next:
23164
23165 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23166 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23167 /* We have already processed all of the arguments in our
23168 handing of the pack expansion type. */
23169 len = 0;
23170
23171 args1 = TREE_CHAIN (args1);
23172 args2 = TREE_CHAIN (args2);
23173 }
23174
23175 /* "In most cases, all template parameters must have values in order for
23176 deduction to succeed, but for partial ordering purposes a template
23177 parameter may remain without a value provided it is not used in the
23178 types being used for partial ordering."
23179
23180 Thus, if we are missing any of the targs1 we need to substitute into
23181 origs1, then pat2 is not as specialized as pat1. This can happen when
23182 there is a nondeduced context. */
23183 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23184 lose2 = true;
23185 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23186 lose1 = true;
23187
23188 processing_template_decl--;
23189
23190 /* If both deductions succeed, the partial ordering selects the more
23191 constrained template. */
23192 if (!lose1 && !lose2)
23193 {
23194 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23195 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23196 lose1 = !subsumes_constraints (c1, c2);
23197 lose2 = !subsumes_constraints (c2, c1);
23198 }
23199
23200 /* All things being equal, if the next argument is a pack expansion
23201 for one function but not for the other, prefer the
23202 non-variadic function. FIXME this is bogus; see c++/41958. */
23203 if (lose1 == lose2
23204 && args1 && TREE_VALUE (args1)
23205 && args2 && TREE_VALUE (args2))
23206 {
23207 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23208 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23209 }
23210
23211 if (lose1 == lose2)
23212 return 0;
23213 else if (!lose1)
23214 return 1;
23215 else
23216 return -1;
23217 }
23218
23219 /* Determine which of two partial specializations of TMPL is more
23220 specialized.
23221
23222 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23223 to the first partial specialization. The TREE_PURPOSE is the
23224 innermost set of template parameters for the partial
23225 specialization. PAT2 is similar, but for the second template.
23226
23227 Return 1 if the first partial specialization is more specialized;
23228 -1 if the second is more specialized; 0 if neither is more
23229 specialized.
23230
23231 See [temp.class.order] for information about determining which of
23232 two templates is more specialized. */
23233
23234 static int
23235 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23236 {
23237 tree targs;
23238 int winner = 0;
23239 bool any_deductions = false;
23240
23241 tree tmpl1 = TREE_VALUE (pat1);
23242 tree tmpl2 = TREE_VALUE (pat2);
23243 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23244 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23245
23246 /* Just like what happens for functions, if we are ordering between
23247 different template specializations, we may encounter dependent
23248 types in the arguments, and we need our dependency check functions
23249 to behave correctly. */
23250 ++processing_template_decl;
23251 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23252 if (targs)
23253 {
23254 --winner;
23255 any_deductions = true;
23256 }
23257
23258 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23259 if (targs)
23260 {
23261 ++winner;
23262 any_deductions = true;
23263 }
23264 --processing_template_decl;
23265
23266 /* If both deductions succeed, the partial ordering selects the more
23267 constrained template. */
23268 if (!winner && any_deductions)
23269 return more_constrained (tmpl1, tmpl2);
23270
23271 /* In the case of a tie where at least one of the templates
23272 has a parameter pack at the end, the template with the most
23273 non-packed parameters wins. */
23274 if (winner == 0
23275 && any_deductions
23276 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23277 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23278 {
23279 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23280 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23281 int len1 = TREE_VEC_LENGTH (args1);
23282 int len2 = TREE_VEC_LENGTH (args2);
23283
23284 /* We don't count the pack expansion at the end. */
23285 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23286 --len1;
23287 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23288 --len2;
23289
23290 if (len1 > len2)
23291 return 1;
23292 else if (len1 < len2)
23293 return -1;
23294 }
23295
23296 return winner;
23297 }
23298
23299 /* Return the template arguments that will produce the function signature
23300 DECL from the function template FN, with the explicit template
23301 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23302 also match. Return NULL_TREE if no satisfactory arguments could be
23303 found. */
23304
23305 static tree
23306 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23307 {
23308 int ntparms = DECL_NTPARMS (fn);
23309 tree targs = make_tree_vec (ntparms);
23310 tree decl_type = TREE_TYPE (decl);
23311 tree decl_arg_types;
23312 tree *args;
23313 unsigned int nargs, ix;
23314 tree arg;
23315
23316 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23317
23318 /* Never do unification on the 'this' parameter. */
23319 decl_arg_types = skip_artificial_parms_for (decl,
23320 TYPE_ARG_TYPES (decl_type));
23321
23322 nargs = list_length (decl_arg_types);
23323 args = XALLOCAVEC (tree, nargs);
23324 for (arg = decl_arg_types, ix = 0;
23325 arg != NULL_TREE && arg != void_list_node;
23326 arg = TREE_CHAIN (arg), ++ix)
23327 args[ix] = TREE_VALUE (arg);
23328
23329 if (fn_type_unification (fn, explicit_args, targs,
23330 args, ix,
23331 (check_rettype || DECL_CONV_FN_P (fn)
23332 ? TREE_TYPE (decl_type) : NULL_TREE),
23333 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23334 /*explain_p=*/false,
23335 /*decltype*/false)
23336 == error_mark_node)
23337 return NULL_TREE;
23338
23339 return targs;
23340 }
23341
23342 /* Return the innermost template arguments that, when applied to a partial
23343 specialization SPEC_TMPL of TMPL, yield the ARGS.
23344
23345 For example, suppose we have:
23346
23347 template <class T, class U> struct S {};
23348 template <class T> struct S<T*, int> {};
23349
23350 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23351 partial specialization and the ARGS will be {double*, int}. The resulting
23352 vector will be {double}, indicating that `T' is bound to `double'. */
23353
23354 static tree
23355 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23356 {
23357 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23358 tree spec_args
23359 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23360 int i, ntparms = TREE_VEC_LENGTH (tparms);
23361 tree deduced_args;
23362 tree innermost_deduced_args;
23363
23364 innermost_deduced_args = make_tree_vec (ntparms);
23365 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23366 {
23367 deduced_args = copy_node (args);
23368 SET_TMPL_ARGS_LEVEL (deduced_args,
23369 TMPL_ARGS_DEPTH (deduced_args),
23370 innermost_deduced_args);
23371 }
23372 else
23373 deduced_args = innermost_deduced_args;
23374
23375 bool tried_array_deduction = (cxx_dialect < cxx17);
23376 again:
23377 if (unify (tparms, deduced_args,
23378 INNERMOST_TEMPLATE_ARGS (spec_args),
23379 INNERMOST_TEMPLATE_ARGS (args),
23380 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23381 return NULL_TREE;
23382
23383 for (i = 0; i < ntparms; ++i)
23384 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23385 {
23386 if (!tried_array_deduction)
23387 {
23388 try_array_deduction (tparms, innermost_deduced_args,
23389 INNERMOST_TEMPLATE_ARGS (spec_args));
23390 tried_array_deduction = true;
23391 if (TREE_VEC_ELT (innermost_deduced_args, i))
23392 goto again;
23393 }
23394 return NULL_TREE;
23395 }
23396
23397 if (!push_tinst_level (spec_tmpl, deduced_args))
23398 {
23399 excessive_deduction_depth = true;
23400 return NULL_TREE;
23401 }
23402
23403 /* Verify that nondeduced template arguments agree with the type
23404 obtained from argument deduction.
23405
23406 For example:
23407
23408 struct A { typedef int X; };
23409 template <class T, class U> struct C {};
23410 template <class T> struct C<T, typename T::X> {};
23411
23412 Then with the instantiation `C<A, int>', we can deduce that
23413 `T' is `A' but unify () does not check whether `typename T::X'
23414 is `int'. */
23415 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23416
23417 if (spec_args != error_mark_node)
23418 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23419 INNERMOST_TEMPLATE_ARGS (spec_args),
23420 tmpl, tf_none, false, false);
23421
23422 pop_tinst_level ();
23423
23424 if (spec_args == error_mark_node
23425 /* We only need to check the innermost arguments; the other
23426 arguments will always agree. */
23427 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23428 INNERMOST_TEMPLATE_ARGS (args)))
23429 return NULL_TREE;
23430
23431 /* Now that we have bindings for all of the template arguments,
23432 ensure that the arguments deduced for the template template
23433 parameters have compatible template parameter lists. See the use
23434 of template_template_parm_bindings_ok_p in fn_type_unification
23435 for more information. */
23436 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23437 return NULL_TREE;
23438
23439 return deduced_args;
23440 }
23441
23442 // Compare two function templates T1 and T2 by deducing bindings
23443 // from one against the other. If both deductions succeed, compare
23444 // constraints to see which is more constrained.
23445 static int
23446 more_specialized_inst (tree t1, tree t2)
23447 {
23448 int fate = 0;
23449 int count = 0;
23450
23451 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23452 {
23453 --fate;
23454 ++count;
23455 }
23456
23457 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23458 {
23459 ++fate;
23460 ++count;
23461 }
23462
23463 // If both deductions succeed, then one may be more constrained.
23464 if (count == 2 && fate == 0)
23465 fate = more_constrained (t1, t2);
23466
23467 return fate;
23468 }
23469
23470 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23471 Return the TREE_LIST node with the most specialized template, if
23472 any. If there is no most specialized template, the error_mark_node
23473 is returned.
23474
23475 Note that this function does not look at, or modify, the
23476 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23477 returned is one of the elements of INSTANTIATIONS, callers may
23478 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23479 and retrieve it from the value returned. */
23480
23481 tree
23482 most_specialized_instantiation (tree templates)
23483 {
23484 tree fn, champ;
23485
23486 ++processing_template_decl;
23487
23488 champ = templates;
23489 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23490 {
23491 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23492 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23493 if (fate == -1)
23494 champ = fn;
23495 else if (!fate)
23496 {
23497 /* Equally specialized, move to next function. If there
23498 is no next function, nothing's most specialized. */
23499 fn = TREE_CHAIN (fn);
23500 champ = fn;
23501 if (!fn)
23502 break;
23503 }
23504 }
23505
23506 if (champ)
23507 /* Now verify that champ is better than everything earlier in the
23508 instantiation list. */
23509 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23510 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23511 {
23512 champ = NULL_TREE;
23513 break;
23514 }
23515 }
23516
23517 processing_template_decl--;
23518
23519 if (!champ)
23520 return error_mark_node;
23521
23522 return champ;
23523 }
23524
23525 /* If DECL is a specialization of some template, return the most
23526 general such template. Otherwise, returns NULL_TREE.
23527
23528 For example, given:
23529
23530 template <class T> struct S { template <class U> void f(U); };
23531
23532 if TMPL is `template <class U> void S<int>::f(U)' this will return
23533 the full template. This function will not trace past partial
23534 specializations, however. For example, given in addition:
23535
23536 template <class T> struct S<T*> { template <class U> void f(U); };
23537
23538 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23539 `template <class T> template <class U> S<T*>::f(U)'. */
23540
23541 tree
23542 most_general_template (tree decl)
23543 {
23544 if (TREE_CODE (decl) != TEMPLATE_DECL)
23545 {
23546 if (tree tinfo = get_template_info (decl))
23547 decl = TI_TEMPLATE (tinfo);
23548 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23549 template friend, or a FIELD_DECL for a capture pack. */
23550 if (TREE_CODE (decl) != TEMPLATE_DECL)
23551 return NULL_TREE;
23552 }
23553
23554 /* Look for more and more general templates. */
23555 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23556 {
23557 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23558 (See cp-tree.h for details.) */
23559 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23560 break;
23561
23562 if (CLASS_TYPE_P (TREE_TYPE (decl))
23563 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23564 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23565 break;
23566
23567 /* Stop if we run into an explicitly specialized class template. */
23568 if (!DECL_NAMESPACE_SCOPE_P (decl)
23569 && DECL_CONTEXT (decl)
23570 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23571 break;
23572
23573 decl = DECL_TI_TEMPLATE (decl);
23574 }
23575
23576 return decl;
23577 }
23578
23579 /* Return the most specialized of the template partial specializations
23580 which can produce TARGET, a specialization of some class or variable
23581 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23582 a TEMPLATE_DECL node corresponding to the partial specialization, while
23583 the TREE_PURPOSE is the set of template arguments that must be
23584 substituted into the template pattern in order to generate TARGET.
23585
23586 If the choice of partial specialization is ambiguous, a diagnostic
23587 is issued, and the error_mark_node is returned. If there are no
23588 partial specializations matching TARGET, then NULL_TREE is
23589 returned, indicating that the primary template should be used. */
23590
23591 static tree
23592 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23593 {
23594 tree list = NULL_TREE;
23595 tree t;
23596 tree champ;
23597 int fate;
23598 bool ambiguous_p;
23599 tree outer_args = NULL_TREE;
23600 tree tmpl, args;
23601
23602 if (TYPE_P (target))
23603 {
23604 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23605 tmpl = TI_TEMPLATE (tinfo);
23606 args = TI_ARGS (tinfo);
23607 }
23608 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23609 {
23610 tmpl = TREE_OPERAND (target, 0);
23611 args = TREE_OPERAND (target, 1);
23612 }
23613 else if (VAR_P (target))
23614 {
23615 tree tinfo = DECL_TEMPLATE_INFO (target);
23616 tmpl = TI_TEMPLATE (tinfo);
23617 args = TI_ARGS (tinfo);
23618 }
23619 else
23620 gcc_unreachable ();
23621
23622 tree main_tmpl = most_general_template (tmpl);
23623
23624 /* For determining which partial specialization to use, only the
23625 innermost args are interesting. */
23626 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23627 {
23628 outer_args = strip_innermost_template_args (args, 1);
23629 args = INNERMOST_TEMPLATE_ARGS (args);
23630 }
23631
23632 /* The caller hasn't called push_to_top_level yet, but we need
23633 get_partial_spec_bindings to be done in non-template context so that we'll
23634 fully resolve everything. */
23635 processing_template_decl_sentinel ptds;
23636
23637 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23638 {
23639 tree spec_args;
23640 tree spec_tmpl = TREE_VALUE (t);
23641
23642 if (outer_args)
23643 {
23644 /* Substitute in the template args from the enclosing class. */
23645 ++processing_template_decl;
23646 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23647 --processing_template_decl;
23648 }
23649
23650 if (spec_tmpl == error_mark_node)
23651 return error_mark_node;
23652
23653 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23654 if (spec_args)
23655 {
23656 if (outer_args)
23657 spec_args = add_to_template_args (outer_args, spec_args);
23658
23659 /* Keep the candidate only if the constraints are satisfied,
23660 or if we're not compiling with concepts. */
23661 if (!flag_concepts
23662 || constraints_satisfied_p (spec_tmpl, spec_args))
23663 {
23664 list = tree_cons (spec_args, TREE_VALUE (t), list);
23665 TREE_TYPE (list) = TREE_TYPE (t);
23666 }
23667 }
23668 }
23669
23670 if (! list)
23671 return NULL_TREE;
23672
23673 ambiguous_p = false;
23674 t = list;
23675 champ = t;
23676 t = TREE_CHAIN (t);
23677 for (; t; t = TREE_CHAIN (t))
23678 {
23679 fate = more_specialized_partial_spec (tmpl, champ, t);
23680 if (fate == 1)
23681 ;
23682 else
23683 {
23684 if (fate == 0)
23685 {
23686 t = TREE_CHAIN (t);
23687 if (! t)
23688 {
23689 ambiguous_p = true;
23690 break;
23691 }
23692 }
23693 champ = t;
23694 }
23695 }
23696
23697 if (!ambiguous_p)
23698 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23699 {
23700 fate = more_specialized_partial_spec (tmpl, champ, t);
23701 if (fate != 1)
23702 {
23703 ambiguous_p = true;
23704 break;
23705 }
23706 }
23707
23708 if (ambiguous_p)
23709 {
23710 const char *str;
23711 char *spaces = NULL;
23712 if (!(complain & tf_error))
23713 return error_mark_node;
23714 if (TYPE_P (target))
23715 error ("ambiguous template instantiation for %q#T", target);
23716 else
23717 error ("ambiguous template instantiation for %q#D", target);
23718 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23719 for (t = list; t; t = TREE_CHAIN (t))
23720 {
23721 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23722 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23723 "%s %#qS", spaces ? spaces : str, subst);
23724 spaces = spaces ? spaces : get_spaces (str);
23725 }
23726 free (spaces);
23727 return error_mark_node;
23728 }
23729
23730 return champ;
23731 }
23732
23733 /* Explicitly instantiate DECL. */
23734
23735 void
23736 do_decl_instantiation (tree decl, tree storage)
23737 {
23738 tree result = NULL_TREE;
23739 int extern_p = 0;
23740
23741 if (!decl || decl == error_mark_node)
23742 /* An error occurred, for which grokdeclarator has already issued
23743 an appropriate message. */
23744 return;
23745 else if (! DECL_LANG_SPECIFIC (decl))
23746 {
23747 error ("explicit instantiation of non-template %q#D", decl);
23748 return;
23749 }
23750 else if (DECL_DECLARED_CONCEPT_P (decl))
23751 {
23752 if (VAR_P (decl))
23753 error ("explicit instantiation of variable concept %q#D", decl);
23754 else
23755 error ("explicit instantiation of function concept %q#D", decl);
23756 return;
23757 }
23758
23759 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23760 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23761
23762 if (VAR_P (decl) && !var_templ)
23763 {
23764 /* There is an asymmetry here in the way VAR_DECLs and
23765 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23766 the latter, the DECL we get back will be marked as a
23767 template instantiation, and the appropriate
23768 DECL_TEMPLATE_INFO will be set up. This does not happen for
23769 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23770 should handle VAR_DECLs as it currently handles
23771 FUNCTION_DECLs. */
23772 if (!DECL_CLASS_SCOPE_P (decl))
23773 {
23774 error ("%qD is not a static data member of a class template", decl);
23775 return;
23776 }
23777 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23778 if (!result || !VAR_P (result))
23779 {
23780 error ("no matching template for %qD found", decl);
23781 return;
23782 }
23783 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23784 {
23785 error ("type %qT for explicit instantiation %qD does not match "
23786 "declared type %qT", TREE_TYPE (result), decl,
23787 TREE_TYPE (decl));
23788 return;
23789 }
23790 }
23791 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23792 {
23793 error ("explicit instantiation of %q#D", decl);
23794 return;
23795 }
23796 else
23797 result = decl;
23798
23799 /* Check for various error cases. Note that if the explicit
23800 instantiation is valid the RESULT will currently be marked as an
23801 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23802 until we get here. */
23803
23804 if (DECL_TEMPLATE_SPECIALIZATION (result))
23805 {
23806 /* DR 259 [temp.spec].
23807
23808 Both an explicit instantiation and a declaration of an explicit
23809 specialization shall not appear in a program unless the explicit
23810 instantiation follows a declaration of the explicit specialization.
23811
23812 For a given set of template parameters, if an explicit
23813 instantiation of a template appears after a declaration of an
23814 explicit specialization for that template, the explicit
23815 instantiation has no effect. */
23816 return;
23817 }
23818 else if (DECL_EXPLICIT_INSTANTIATION (result))
23819 {
23820 /* [temp.spec]
23821
23822 No program shall explicitly instantiate any template more
23823 than once.
23824
23825 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23826 the first instantiation was `extern' and the second is not,
23827 and EXTERN_P for the opposite case. */
23828 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23829 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23830 /* If an "extern" explicit instantiation follows an ordinary
23831 explicit instantiation, the template is instantiated. */
23832 if (extern_p)
23833 return;
23834 }
23835 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23836 {
23837 error ("no matching template for %qD found", result);
23838 return;
23839 }
23840 else if (!DECL_TEMPLATE_INFO (result))
23841 {
23842 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23843 return;
23844 }
23845
23846 if (storage == NULL_TREE)
23847 ;
23848 else if (storage == ridpointers[(int) RID_EXTERN])
23849 {
23850 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23851 pedwarn (input_location, OPT_Wpedantic,
23852 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23853 "instantiations");
23854 extern_p = 1;
23855 }
23856 else
23857 error ("storage class %qD applied to template instantiation", storage);
23858
23859 check_explicit_instantiation_namespace (result);
23860 mark_decl_instantiated (result, extern_p);
23861 if (! extern_p)
23862 instantiate_decl (result, /*defer_ok=*/true,
23863 /*expl_inst_class_mem_p=*/false);
23864 }
23865
23866 static void
23867 mark_class_instantiated (tree t, int extern_p)
23868 {
23869 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23870 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23871 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23872 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23873 if (! extern_p)
23874 {
23875 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23876 rest_of_type_compilation (t, 1);
23877 }
23878 }
23879
23880 /* Called from do_type_instantiation through binding_table_foreach to
23881 do recursive instantiation for the type bound in ENTRY. */
23882 static void
23883 bt_instantiate_type_proc (binding_entry entry, void *data)
23884 {
23885 tree storage = *(tree *) data;
23886
23887 if (MAYBE_CLASS_TYPE_P (entry->type)
23888 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23889 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23890 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23891 }
23892
23893 /* Perform an explicit instantiation of template class T. STORAGE, if
23894 non-null, is the RID for extern, inline or static. COMPLAIN is
23895 nonzero if this is called from the parser, zero if called recursively,
23896 since the standard is unclear (as detailed below). */
23897
23898 void
23899 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23900 {
23901 int extern_p = 0;
23902 int nomem_p = 0;
23903 int static_p = 0;
23904 int previous_instantiation_extern_p = 0;
23905
23906 if (TREE_CODE (t) == TYPE_DECL)
23907 t = TREE_TYPE (t);
23908
23909 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23910 {
23911 tree tmpl =
23912 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23913 if (tmpl)
23914 error ("explicit instantiation of non-class template %qD", tmpl);
23915 else
23916 error ("explicit instantiation of non-template type %qT", t);
23917 return;
23918 }
23919
23920 complete_type (t);
23921
23922 if (!COMPLETE_TYPE_P (t))
23923 {
23924 if (complain & tf_error)
23925 error ("explicit instantiation of %q#T before definition of template",
23926 t);
23927 return;
23928 }
23929
23930 if (storage != NULL_TREE)
23931 {
23932 if (!in_system_header_at (input_location))
23933 {
23934 if (storage == ridpointers[(int) RID_EXTERN])
23935 {
23936 if (cxx_dialect == cxx98)
23937 pedwarn (input_location, OPT_Wpedantic,
23938 "ISO C++ 1998 forbids the use of %<extern%> on "
23939 "explicit instantiations");
23940 }
23941 else
23942 pedwarn (input_location, OPT_Wpedantic,
23943 "ISO C++ forbids the use of %qE"
23944 " on explicit instantiations", storage);
23945 }
23946
23947 if (storage == ridpointers[(int) RID_INLINE])
23948 nomem_p = 1;
23949 else if (storage == ridpointers[(int) RID_EXTERN])
23950 extern_p = 1;
23951 else if (storage == ridpointers[(int) RID_STATIC])
23952 static_p = 1;
23953 else
23954 {
23955 error ("storage class %qD applied to template instantiation",
23956 storage);
23957 extern_p = 0;
23958 }
23959 }
23960
23961 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23962 {
23963 /* DR 259 [temp.spec].
23964
23965 Both an explicit instantiation and a declaration of an explicit
23966 specialization shall not appear in a program unless the explicit
23967 instantiation follows a declaration of the explicit specialization.
23968
23969 For a given set of template parameters, if an explicit
23970 instantiation of a template appears after a declaration of an
23971 explicit specialization for that template, the explicit
23972 instantiation has no effect. */
23973 return;
23974 }
23975 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23976 {
23977 /* [temp.spec]
23978
23979 No program shall explicitly instantiate any template more
23980 than once.
23981
23982 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23983 instantiation was `extern'. If EXTERN_P then the second is.
23984 These cases are OK. */
23985 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23986
23987 if (!previous_instantiation_extern_p && !extern_p
23988 && (complain & tf_error))
23989 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23990
23991 /* If we've already instantiated the template, just return now. */
23992 if (!CLASSTYPE_INTERFACE_ONLY (t))
23993 return;
23994 }
23995
23996 check_explicit_instantiation_namespace (TYPE_NAME (t));
23997 mark_class_instantiated (t, extern_p);
23998
23999 if (nomem_p)
24000 return;
24001
24002 /* In contrast to implicit instantiation, where only the
24003 declarations, and not the definitions, of members are
24004 instantiated, we have here:
24005
24006 [temp.explicit]
24007
24008 The explicit instantiation of a class template specialization
24009 implies the instantiation of all of its members not
24010 previously explicitly specialized in the translation unit
24011 containing the explicit instantiation.
24012
24013 Of course, we can't instantiate member template classes, since we
24014 don't have any arguments for them. Note that the standard is
24015 unclear on whether the instantiation of the members are
24016 *explicit* instantiations or not. However, the most natural
24017 interpretation is that it should be an explicit
24018 instantiation. */
24019 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24020 if ((VAR_P (fld)
24021 || (TREE_CODE (fld) == FUNCTION_DECL
24022 && !static_p
24023 && user_provided_p (fld)))
24024 && DECL_TEMPLATE_INSTANTIATION (fld))
24025 {
24026 mark_decl_instantiated (fld, extern_p);
24027 if (! extern_p)
24028 instantiate_decl (fld, /*defer_ok=*/true,
24029 /*expl_inst_class_mem_p=*/true);
24030 }
24031
24032 if (CLASSTYPE_NESTED_UTDS (t))
24033 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24034 bt_instantiate_type_proc, &storage);
24035 }
24036
24037 /* Given a function DECL, which is a specialization of TMPL, modify
24038 DECL to be a re-instantiation of TMPL with the same template
24039 arguments. TMPL should be the template into which tsubst'ing
24040 should occur for DECL, not the most general template.
24041
24042 One reason for doing this is a scenario like this:
24043
24044 template <class T>
24045 void f(const T&, int i);
24046
24047 void g() { f(3, 7); }
24048
24049 template <class T>
24050 void f(const T& t, const int i) { }
24051
24052 Note that when the template is first instantiated, with
24053 instantiate_template, the resulting DECL will have no name for the
24054 first parameter, and the wrong type for the second. So, when we go
24055 to instantiate the DECL, we regenerate it. */
24056
24057 static void
24058 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24059 {
24060 /* The arguments used to instantiate DECL, from the most general
24061 template. */
24062 tree code_pattern;
24063
24064 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24065
24066 /* Make sure that we can see identifiers, and compute access
24067 correctly. */
24068 push_access_scope (decl);
24069
24070 if (TREE_CODE (decl) == FUNCTION_DECL)
24071 {
24072 tree decl_parm;
24073 tree pattern_parm;
24074 tree specs;
24075 int args_depth;
24076 int parms_depth;
24077
24078 args_depth = TMPL_ARGS_DEPTH (args);
24079 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24080 if (args_depth > parms_depth)
24081 args = get_innermost_template_args (args, parms_depth);
24082
24083 /* Instantiate a dynamic exception-specification. noexcept will be
24084 handled below. */
24085 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24086 if (TREE_VALUE (raises))
24087 {
24088 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24089 args, tf_error, NULL_TREE,
24090 /*defer_ok*/false);
24091 if (specs && specs != error_mark_node)
24092 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24093 specs);
24094 }
24095
24096 /* Merge parameter declarations. */
24097 decl_parm = skip_artificial_parms_for (decl,
24098 DECL_ARGUMENTS (decl));
24099 pattern_parm
24100 = skip_artificial_parms_for (code_pattern,
24101 DECL_ARGUMENTS (code_pattern));
24102 while (decl_parm && !DECL_PACK_P (pattern_parm))
24103 {
24104 tree parm_type;
24105 tree attributes;
24106
24107 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24108 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24109 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24110 NULL_TREE);
24111 parm_type = type_decays_to (parm_type);
24112 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24113 TREE_TYPE (decl_parm) = parm_type;
24114 attributes = DECL_ATTRIBUTES (pattern_parm);
24115 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24116 {
24117 DECL_ATTRIBUTES (decl_parm) = attributes;
24118 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24119 }
24120 decl_parm = DECL_CHAIN (decl_parm);
24121 pattern_parm = DECL_CHAIN (pattern_parm);
24122 }
24123 /* Merge any parameters that match with the function parameter
24124 pack. */
24125 if (pattern_parm && DECL_PACK_P (pattern_parm))
24126 {
24127 int i, len;
24128 tree expanded_types;
24129 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24130 the parameters in this function parameter pack. */
24131 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24132 args, tf_error, NULL_TREE);
24133 len = TREE_VEC_LENGTH (expanded_types);
24134 for (i = 0; i < len; i++)
24135 {
24136 tree parm_type;
24137 tree attributes;
24138
24139 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24140 /* Rename the parameter to include the index. */
24141 DECL_NAME (decl_parm) =
24142 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24143 parm_type = TREE_VEC_ELT (expanded_types, i);
24144 parm_type = type_decays_to (parm_type);
24145 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24146 TREE_TYPE (decl_parm) = parm_type;
24147 attributes = DECL_ATTRIBUTES (pattern_parm);
24148 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24149 {
24150 DECL_ATTRIBUTES (decl_parm) = attributes;
24151 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24152 }
24153 decl_parm = DECL_CHAIN (decl_parm);
24154 }
24155 }
24156 /* Merge additional specifiers from the CODE_PATTERN. */
24157 if (DECL_DECLARED_INLINE_P (code_pattern)
24158 && !DECL_DECLARED_INLINE_P (decl))
24159 DECL_DECLARED_INLINE_P (decl) = 1;
24160
24161 maybe_instantiate_noexcept (decl, tf_error);
24162 }
24163 else if (VAR_P (decl))
24164 {
24165 start_lambda_scope (decl);
24166 DECL_INITIAL (decl) =
24167 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24168 tf_error, DECL_TI_TEMPLATE (decl));
24169 finish_lambda_scope ();
24170 if (VAR_HAD_UNKNOWN_BOUND (decl))
24171 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24172 tf_error, DECL_TI_TEMPLATE (decl));
24173 }
24174 else
24175 gcc_unreachable ();
24176
24177 pop_access_scope (decl);
24178 }
24179
24180 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24181 substituted to get DECL. */
24182
24183 tree
24184 template_for_substitution (tree decl)
24185 {
24186 tree tmpl = DECL_TI_TEMPLATE (decl);
24187
24188 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24189 for the instantiation. This is not always the most general
24190 template. Consider, for example:
24191
24192 template <class T>
24193 struct S { template <class U> void f();
24194 template <> void f<int>(); };
24195
24196 and an instantiation of S<double>::f<int>. We want TD to be the
24197 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24198 while (/* An instantiation cannot have a definition, so we need a
24199 more general template. */
24200 DECL_TEMPLATE_INSTANTIATION (tmpl)
24201 /* We must also deal with friend templates. Given:
24202
24203 template <class T> struct S {
24204 template <class U> friend void f() {};
24205 };
24206
24207 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24208 so far as the language is concerned, but that's still
24209 where we get the pattern for the instantiation from. On
24210 other hand, if the definition comes outside the class, say:
24211
24212 template <class T> struct S {
24213 template <class U> friend void f();
24214 };
24215 template <class U> friend void f() {}
24216
24217 we don't need to look any further. That's what the check for
24218 DECL_INITIAL is for. */
24219 || (TREE_CODE (decl) == FUNCTION_DECL
24220 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24221 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24222 {
24223 /* The present template, TD, should not be a definition. If it
24224 were a definition, we should be using it! Note that we
24225 cannot restructure the loop to just keep going until we find
24226 a template with a definition, since that might go too far if
24227 a specialization was declared, but not defined. */
24228
24229 /* Fetch the more general template. */
24230 tmpl = DECL_TI_TEMPLATE (tmpl);
24231 }
24232
24233 return tmpl;
24234 }
24235
24236 /* Returns true if we need to instantiate this template instance even if we
24237 know we aren't going to emit it. */
24238
24239 bool
24240 always_instantiate_p (tree decl)
24241 {
24242 /* We always instantiate inline functions so that we can inline them. An
24243 explicit instantiation declaration prohibits implicit instantiation of
24244 non-inline functions. With high levels of optimization, we would
24245 normally inline non-inline functions -- but we're not allowed to do
24246 that for "extern template" functions. Therefore, we check
24247 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24248 return ((TREE_CODE (decl) == FUNCTION_DECL
24249 && (DECL_DECLARED_INLINE_P (decl)
24250 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24251 /* And we need to instantiate static data members so that
24252 their initializers are available in integral constant
24253 expressions. */
24254 || (VAR_P (decl)
24255 && decl_maybe_constant_var_p (decl)));
24256 }
24257
24258 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24259 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24260 error, true otherwise. */
24261
24262 bool
24263 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24264 {
24265 tree fntype, spec, noex, clone;
24266
24267 /* Don't instantiate a noexcept-specification from template context. */
24268 if (processing_template_decl
24269 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24270 return true;
24271
24272 if (DECL_CLONED_FUNCTION_P (fn))
24273 fn = DECL_CLONED_FUNCTION (fn);
24274
24275 tree orig_fn = NULL_TREE;
24276 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24277 its FUNCTION_DECL for the rest of this function -- push_access_scope
24278 doesn't accept TEMPLATE_DECLs. */
24279 if (DECL_FUNCTION_TEMPLATE_P (fn))
24280 {
24281 orig_fn = fn;
24282 fn = DECL_TEMPLATE_RESULT (fn);
24283 }
24284
24285 fntype = TREE_TYPE (fn);
24286 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24287
24288 if (!spec || !TREE_PURPOSE (spec))
24289 return true;
24290
24291 noex = TREE_PURPOSE (spec);
24292
24293 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24294 {
24295 static hash_set<tree>* fns = new hash_set<tree>;
24296 bool added = false;
24297 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24298 {
24299 spec = get_defaulted_eh_spec (fn, complain);
24300 if (spec == error_mark_node)
24301 /* This might have failed because of an unparsed DMI, so
24302 let's try again later. */
24303 return false;
24304 }
24305 else if (!(added = !fns->add (fn)))
24306 {
24307 /* If hash_set::add returns true, the element was already there. */
24308 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24309 DECL_SOURCE_LOCATION (fn));
24310 error_at (loc,
24311 "exception specification of %qD depends on itself",
24312 fn);
24313 spec = noexcept_false_spec;
24314 }
24315 else if (push_tinst_level (fn))
24316 {
24317 push_access_scope (fn);
24318 push_deferring_access_checks (dk_no_deferred);
24319 input_location = DECL_SOURCE_LOCATION (fn);
24320
24321 tree save_ccp = current_class_ptr;
24322 tree save_ccr = current_class_ref;
24323 /* If needed, set current_class_ptr for the benefit of
24324 tsubst_copy/PARM_DECL. */
24325 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24326 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24327 {
24328 tree this_parm = DECL_ARGUMENTS (tdecl);
24329 current_class_ptr = NULL_TREE;
24330 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24331 current_class_ptr = this_parm;
24332 }
24333
24334 /* If this function is represented by a TEMPLATE_DECL, then
24335 the deferred noexcept-specification might still contain
24336 dependent types, even after substitution. And we need the
24337 dependency check functions to work in build_noexcept_spec. */
24338 if (orig_fn)
24339 ++processing_template_decl;
24340
24341 /* Do deferred instantiation of the noexcept-specifier. */
24342 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24343 DEFERRED_NOEXCEPT_ARGS (noex),
24344 tf_warning_or_error, fn,
24345 /*function_p=*/false,
24346 /*i_c_e_p=*/true);
24347
24348 current_class_ptr = save_ccp;
24349 current_class_ref = save_ccr;
24350
24351 /* Build up the noexcept-specification. */
24352 spec = build_noexcept_spec (noex, tf_warning_or_error);
24353
24354 if (orig_fn)
24355 --processing_template_decl;
24356
24357 pop_deferring_access_checks ();
24358 pop_access_scope (fn);
24359 pop_tinst_level ();
24360 }
24361 else
24362 spec = noexcept_false_spec;
24363
24364 if (added)
24365 fns->remove (fn);
24366
24367 if (spec == error_mark_node)
24368 {
24369 /* This failed with a hard error, so let's go with false. */
24370 gcc_assert (seen_error ());
24371 spec = noexcept_false_spec;
24372 }
24373
24374 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24375 if (orig_fn)
24376 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24377 }
24378
24379 FOR_EACH_CLONE (clone, fn)
24380 {
24381 if (TREE_TYPE (clone) == fntype)
24382 TREE_TYPE (clone) = TREE_TYPE (fn);
24383 else
24384 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24385 }
24386
24387 return true;
24388 }
24389
24390 /* We're starting to process the function INST, an instantiation of PATTERN;
24391 add their parameters to local_specializations. */
24392
24393 static void
24394 register_parameter_specializations (tree pattern, tree inst)
24395 {
24396 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24397 tree spec_parm = DECL_ARGUMENTS (inst);
24398 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24399 {
24400 register_local_specialization (spec_parm, tmpl_parm);
24401 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24402 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24403 }
24404 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24405 {
24406 if (!DECL_PACK_P (tmpl_parm))
24407 {
24408 register_local_specialization (spec_parm, tmpl_parm);
24409 spec_parm = DECL_CHAIN (spec_parm);
24410 }
24411 else
24412 {
24413 /* Register the (value) argument pack as a specialization of
24414 TMPL_PARM, then move on. */
24415 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24416 register_local_specialization (argpack, tmpl_parm);
24417 }
24418 }
24419 gcc_assert (!spec_parm);
24420 }
24421
24422 /* Produce the definition of D, a _DECL generated from a template. If
24423 DEFER_OK is true, then we don't have to actually do the
24424 instantiation now; we just have to do it sometime. Normally it is
24425 an error if this is an explicit instantiation but D is undefined.
24426 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24427 instantiated class template. */
24428
24429 tree
24430 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24431 {
24432 tree tmpl = DECL_TI_TEMPLATE (d);
24433 tree gen_args;
24434 tree args;
24435 tree td;
24436 tree code_pattern;
24437 tree spec;
24438 tree gen_tmpl;
24439 bool pattern_defined;
24440 location_t saved_loc = input_location;
24441 int saved_unevaluated_operand = cp_unevaluated_operand;
24442 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24443 bool external_p;
24444 bool deleted_p;
24445
24446 /* This function should only be used to instantiate templates for
24447 functions and static member variables. */
24448 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24449
24450 /* A concept is never instantiated. */
24451 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24452
24453 /* Variables are never deferred; if instantiation is required, they
24454 are instantiated right away. That allows for better code in the
24455 case that an expression refers to the value of the variable --
24456 if the variable has a constant value the referring expression can
24457 take advantage of that fact. */
24458 if (VAR_P (d))
24459 defer_ok = false;
24460
24461 /* Don't instantiate cloned functions. Instead, instantiate the
24462 functions they cloned. */
24463 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24464 d = DECL_CLONED_FUNCTION (d);
24465
24466 if (DECL_TEMPLATE_INSTANTIATED (d)
24467 || (TREE_CODE (d) == FUNCTION_DECL
24468 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24469 || DECL_TEMPLATE_SPECIALIZATION (d))
24470 /* D has already been instantiated or explicitly specialized, so
24471 there's nothing for us to do here.
24472
24473 It might seem reasonable to check whether or not D is an explicit
24474 instantiation, and, if so, stop here. But when an explicit
24475 instantiation is deferred until the end of the compilation,
24476 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24477 the instantiation. */
24478 return d;
24479
24480 /* Check to see whether we know that this template will be
24481 instantiated in some other file, as with "extern template"
24482 extension. */
24483 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24484
24485 /* In general, we do not instantiate such templates. */
24486 if (external_p && !always_instantiate_p (d))
24487 return d;
24488
24489 gen_tmpl = most_general_template (tmpl);
24490 gen_args = DECL_TI_ARGS (d);
24491
24492 if (tmpl != gen_tmpl)
24493 /* We should already have the extra args. */
24494 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24495 == TMPL_ARGS_DEPTH (gen_args));
24496 /* And what's in the hash table should match D. */
24497 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24498 || spec == NULL_TREE);
24499
24500 /* This needs to happen before any tsubsting. */
24501 if (! push_tinst_level (d))
24502 return d;
24503
24504 timevar_push (TV_TEMPLATE_INST);
24505
24506 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24507 for the instantiation. */
24508 td = template_for_substitution (d);
24509 args = gen_args;
24510
24511 if (VAR_P (d))
24512 {
24513 /* Look up an explicit specialization, if any. */
24514 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24515 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24516 if (elt && elt != error_mark_node)
24517 {
24518 td = TREE_VALUE (elt);
24519 args = TREE_PURPOSE (elt);
24520 }
24521 }
24522
24523 code_pattern = DECL_TEMPLATE_RESULT (td);
24524
24525 /* We should never be trying to instantiate a member of a class
24526 template or partial specialization. */
24527 gcc_assert (d != code_pattern);
24528
24529 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24530 || DECL_TEMPLATE_SPECIALIZATION (td))
24531 /* In the case of a friend template whose definition is provided
24532 outside the class, we may have too many arguments. Drop the
24533 ones we don't need. The same is true for specializations. */
24534 args = get_innermost_template_args
24535 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24536
24537 if (TREE_CODE (d) == FUNCTION_DECL)
24538 {
24539 deleted_p = DECL_DELETED_FN (code_pattern);
24540 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24541 && DECL_INITIAL (code_pattern) != error_mark_node)
24542 || DECL_DEFAULTED_FN (code_pattern)
24543 || deleted_p);
24544 }
24545 else
24546 {
24547 deleted_p = false;
24548 if (DECL_CLASS_SCOPE_P (code_pattern))
24549 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24550 else
24551 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24552 }
24553
24554 /* We may be in the middle of deferred access check. Disable it now. */
24555 push_deferring_access_checks (dk_no_deferred);
24556
24557 /* Unless an explicit instantiation directive has already determined
24558 the linkage of D, remember that a definition is available for
24559 this entity. */
24560 if (pattern_defined
24561 && !DECL_INTERFACE_KNOWN (d)
24562 && !DECL_NOT_REALLY_EXTERN (d))
24563 mark_definable (d);
24564
24565 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24566 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24567 input_location = DECL_SOURCE_LOCATION (d);
24568
24569 /* If D is a member of an explicitly instantiated class template,
24570 and no definition is available, treat it like an implicit
24571 instantiation. */
24572 if (!pattern_defined && expl_inst_class_mem_p
24573 && DECL_EXPLICIT_INSTANTIATION (d))
24574 {
24575 /* Leave linkage flags alone on instantiations with anonymous
24576 visibility. */
24577 if (TREE_PUBLIC (d))
24578 {
24579 DECL_NOT_REALLY_EXTERN (d) = 0;
24580 DECL_INTERFACE_KNOWN (d) = 0;
24581 }
24582 SET_DECL_IMPLICIT_INSTANTIATION (d);
24583 }
24584
24585 /* Defer all other templates, unless we have been explicitly
24586 forbidden from doing so. */
24587 if (/* If there is no definition, we cannot instantiate the
24588 template. */
24589 ! pattern_defined
24590 /* If it's OK to postpone instantiation, do so. */
24591 || defer_ok
24592 /* If this is a static data member that will be defined
24593 elsewhere, we don't want to instantiate the entire data
24594 member, but we do want to instantiate the initializer so that
24595 we can substitute that elsewhere. */
24596 || (external_p && VAR_P (d))
24597 /* Handle here a deleted function too, avoid generating
24598 its body (c++/61080). */
24599 || deleted_p)
24600 {
24601 /* The definition of the static data member is now required so
24602 we must substitute the initializer. */
24603 if (VAR_P (d)
24604 && !DECL_INITIAL (d)
24605 && DECL_INITIAL (code_pattern))
24606 {
24607 tree ns;
24608 tree init;
24609 bool const_init = false;
24610 bool enter_context = DECL_CLASS_SCOPE_P (d);
24611
24612 ns = decl_namespace_context (d);
24613 push_nested_namespace (ns);
24614 if (enter_context)
24615 push_nested_class (DECL_CONTEXT (d));
24616 init = tsubst_expr (DECL_INITIAL (code_pattern),
24617 args,
24618 tf_warning_or_error, NULL_TREE,
24619 /*integral_constant_expression_p=*/false);
24620 /* If instantiating the initializer involved instantiating this
24621 again, don't call cp_finish_decl twice. */
24622 if (!DECL_INITIAL (d))
24623 {
24624 /* Make sure the initializer is still constant, in case of
24625 circular dependency (template/instantiate6.C). */
24626 const_init
24627 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24628 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24629 /*asmspec_tree=*/NULL_TREE,
24630 LOOKUP_ONLYCONVERTING);
24631 }
24632 if (enter_context)
24633 pop_nested_class ();
24634 pop_nested_namespace (ns);
24635 }
24636
24637 /* We restore the source position here because it's used by
24638 add_pending_template. */
24639 input_location = saved_loc;
24640
24641 if (at_eof && !pattern_defined
24642 && DECL_EXPLICIT_INSTANTIATION (d)
24643 && DECL_NOT_REALLY_EXTERN (d))
24644 /* [temp.explicit]
24645
24646 The definition of a non-exported function template, a
24647 non-exported member function template, or a non-exported
24648 member function or static data member of a class template
24649 shall be present in every translation unit in which it is
24650 explicitly instantiated. */
24651 permerror (input_location, "explicit instantiation of %qD "
24652 "but no definition available", d);
24653
24654 /* If we're in unevaluated context, we just wanted to get the
24655 constant value; this isn't an odr use, so don't queue
24656 a full instantiation. */
24657 if (cp_unevaluated_operand != 0)
24658 goto out;
24659 /* ??? Historically, we have instantiated inline functions, even
24660 when marked as "extern template". */
24661 if (!(external_p && VAR_P (d)))
24662 add_pending_template (d);
24663 goto out;
24664 }
24665 /* Tell the repository that D is available in this translation unit
24666 -- and see if it is supposed to be instantiated here. */
24667 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24668 {
24669 /* In a PCH file, despite the fact that the repository hasn't
24670 requested instantiation in the PCH it is still possible that
24671 an instantiation will be required in a file that includes the
24672 PCH. */
24673 if (pch_file)
24674 add_pending_template (d);
24675 /* Instantiate inline functions so that the inliner can do its
24676 job, even though we'll not be emitting a copy of this
24677 function. */
24678 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24679 goto out;
24680 }
24681
24682 bool push_to_top, nested;
24683 tree fn_context;
24684 fn_context = decl_function_context (d);
24685 if (LAMBDA_FUNCTION_P (d))
24686 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24687 fn_context = NULL_TREE;
24688 nested = current_function_decl != NULL_TREE;
24689 push_to_top = !(nested && fn_context == current_function_decl);
24690
24691 vec<tree> omp_privatization_save;
24692 if (nested)
24693 save_omp_privatization_clauses (omp_privatization_save);
24694
24695 if (push_to_top)
24696 push_to_top_level ();
24697 else
24698 {
24699 gcc_assert (!processing_template_decl);
24700 push_function_context ();
24701 cp_unevaluated_operand = 0;
24702 c_inhibit_evaluation_warnings = 0;
24703 }
24704
24705 /* Mark D as instantiated so that recursive calls to
24706 instantiate_decl do not try to instantiate it again. */
24707 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24708
24709 /* Regenerate the declaration in case the template has been modified
24710 by a subsequent redeclaration. */
24711 regenerate_decl_from_template (d, td, args);
24712
24713 /* We already set the file and line above. Reset them now in case
24714 they changed as a result of calling regenerate_decl_from_template. */
24715 input_location = DECL_SOURCE_LOCATION (d);
24716
24717 if (VAR_P (d))
24718 {
24719 tree init;
24720 bool const_init = false;
24721
24722 /* Clear out DECL_RTL; whatever was there before may not be right
24723 since we've reset the type of the declaration. */
24724 SET_DECL_RTL (d, NULL);
24725 DECL_IN_AGGR_P (d) = 0;
24726
24727 /* The initializer is placed in DECL_INITIAL by
24728 regenerate_decl_from_template so we don't need to
24729 push/pop_access_scope again here. Pull it out so that
24730 cp_finish_decl can process it. */
24731 init = DECL_INITIAL (d);
24732 DECL_INITIAL (d) = NULL_TREE;
24733 DECL_INITIALIZED_P (d) = 0;
24734
24735 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24736 initializer. That function will defer actual emission until
24737 we have a chance to determine linkage. */
24738 DECL_EXTERNAL (d) = 0;
24739
24740 /* Enter the scope of D so that access-checking works correctly. */
24741 bool enter_context = DECL_CLASS_SCOPE_P (d);
24742 if (enter_context)
24743 push_nested_class (DECL_CONTEXT (d));
24744
24745 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24746 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24747
24748 if (enter_context)
24749 pop_nested_class ();
24750
24751 if (variable_template_p (gen_tmpl))
24752 note_variable_template_instantiation (d);
24753 }
24754 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24755 synthesize_method (d);
24756 else if (TREE_CODE (d) == FUNCTION_DECL)
24757 {
24758 /* Set up the list of local specializations. */
24759 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24760 tree block = NULL_TREE;
24761
24762 /* Set up context. */
24763 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24764 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24765 block = push_stmt_list ();
24766 else
24767 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24768
24769 /* Some typedefs referenced from within the template code need to be
24770 access checked at template instantiation time, i.e now. These
24771 types were added to the template at parsing time. Let's get those
24772 and perform the access checks then. */
24773 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24774 args);
24775
24776 /* Create substitution entries for the parameters. */
24777 register_parameter_specializations (code_pattern, d);
24778
24779 /* Substitute into the body of the function. */
24780 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24781 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24782 tf_warning_or_error, tmpl);
24783 else
24784 {
24785 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24786 tf_warning_or_error, tmpl,
24787 /*integral_constant_expression_p=*/false);
24788
24789 /* Set the current input_location to the end of the function
24790 so that finish_function knows where we are. */
24791 input_location
24792 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24793
24794 /* Remember if we saw an infinite loop in the template. */
24795 current_function_infinite_loop
24796 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24797 }
24798
24799 /* Finish the function. */
24800 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24801 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24802 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24803 else
24804 {
24805 d = finish_function (/*inline_p=*/false);
24806 expand_or_defer_fn (d);
24807 }
24808
24809 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24810 cp_check_omp_declare_reduction (d);
24811 }
24812
24813 /* We're not deferring instantiation any more. */
24814 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24815
24816 if (push_to_top)
24817 pop_from_top_level ();
24818 else
24819 pop_function_context ();
24820
24821 if (nested)
24822 restore_omp_privatization_clauses (omp_privatization_save);
24823
24824 out:
24825 pop_deferring_access_checks ();
24826 timevar_pop (TV_TEMPLATE_INST);
24827 pop_tinst_level ();
24828 input_location = saved_loc;
24829 cp_unevaluated_operand = saved_unevaluated_operand;
24830 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24831
24832 return d;
24833 }
24834
24835 /* Run through the list of templates that we wish we could
24836 instantiate, and instantiate any we can. RETRIES is the
24837 number of times we retry pending template instantiation. */
24838
24839 void
24840 instantiate_pending_templates (int retries)
24841 {
24842 int reconsider;
24843 location_t saved_loc = input_location;
24844
24845 /* Instantiating templates may trigger vtable generation. This in turn
24846 may require further template instantiations. We place a limit here
24847 to avoid infinite loop. */
24848 if (pending_templates && retries >= max_tinst_depth)
24849 {
24850 tree decl = pending_templates->tinst->maybe_get_node ();
24851
24852 fatal_error (input_location,
24853 "template instantiation depth exceeds maximum of %d"
24854 " instantiating %q+D, possibly from virtual table generation"
24855 " (use %<-ftemplate-depth=%> to increase the maximum)",
24856 max_tinst_depth, decl);
24857 if (TREE_CODE (decl) == FUNCTION_DECL)
24858 /* Pretend that we defined it. */
24859 DECL_INITIAL (decl) = error_mark_node;
24860 return;
24861 }
24862
24863 do
24864 {
24865 struct pending_template **t = &pending_templates;
24866 struct pending_template *last = NULL;
24867 reconsider = 0;
24868 while (*t)
24869 {
24870 tree instantiation = reopen_tinst_level ((*t)->tinst);
24871 bool complete = false;
24872
24873 if (TYPE_P (instantiation))
24874 {
24875 if (!COMPLETE_TYPE_P (instantiation))
24876 {
24877 instantiate_class_template (instantiation);
24878 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24879 for (tree fld = TYPE_FIELDS (instantiation);
24880 fld; fld = TREE_CHAIN (fld))
24881 if ((VAR_P (fld)
24882 || (TREE_CODE (fld) == FUNCTION_DECL
24883 && !DECL_ARTIFICIAL (fld)))
24884 && DECL_TEMPLATE_INSTANTIATION (fld))
24885 instantiate_decl (fld,
24886 /*defer_ok=*/false,
24887 /*expl_inst_class_mem_p=*/false);
24888
24889 if (COMPLETE_TYPE_P (instantiation))
24890 reconsider = 1;
24891 }
24892
24893 complete = COMPLETE_TYPE_P (instantiation);
24894 }
24895 else
24896 {
24897 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24898 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24899 {
24900 instantiation
24901 = instantiate_decl (instantiation,
24902 /*defer_ok=*/false,
24903 /*expl_inst_class_mem_p=*/false);
24904 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24905 reconsider = 1;
24906 }
24907
24908 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24909 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24910 }
24911
24912 if (complete)
24913 {
24914 /* If INSTANTIATION has been instantiated, then we don't
24915 need to consider it again in the future. */
24916 struct pending_template *drop = *t;
24917 *t = (*t)->next;
24918 set_refcount_ptr (drop->tinst);
24919 pending_template_freelist ().free (drop);
24920 }
24921 else
24922 {
24923 last = *t;
24924 t = &(*t)->next;
24925 }
24926 tinst_depth = 0;
24927 set_refcount_ptr (current_tinst_level);
24928 }
24929 last_pending_template = last;
24930 }
24931 while (reconsider);
24932
24933 input_location = saved_loc;
24934 }
24935
24936 /* Substitute ARGVEC into T, which is a list of initializers for
24937 either base class or a non-static data member. The TREE_PURPOSEs
24938 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24939 instantiate_decl. */
24940
24941 static tree
24942 tsubst_initializer_list (tree t, tree argvec)
24943 {
24944 tree inits = NULL_TREE;
24945 tree target_ctor = error_mark_node;
24946
24947 for (; t; t = TREE_CHAIN (t))
24948 {
24949 tree decl;
24950 tree init;
24951 tree expanded_bases = NULL_TREE;
24952 tree expanded_arguments = NULL_TREE;
24953 int i, len = 1;
24954
24955 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24956 {
24957 tree expr;
24958 tree arg;
24959
24960 /* Expand the base class expansion type into separate base
24961 classes. */
24962 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24963 tf_warning_or_error,
24964 NULL_TREE);
24965 if (expanded_bases == error_mark_node)
24966 continue;
24967
24968 /* We'll be building separate TREE_LISTs of arguments for
24969 each base. */
24970 len = TREE_VEC_LENGTH (expanded_bases);
24971 expanded_arguments = make_tree_vec (len);
24972 for (i = 0; i < len; i++)
24973 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24974
24975 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24976 expand each argument in the TREE_VALUE of t. */
24977 expr = make_node (EXPR_PACK_EXPANSION);
24978 PACK_EXPANSION_LOCAL_P (expr) = true;
24979 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24980 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24981
24982 if (TREE_VALUE (t) == void_type_node)
24983 /* VOID_TYPE_NODE is used to indicate
24984 value-initialization. */
24985 {
24986 for (i = 0; i < len; i++)
24987 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24988 }
24989 else
24990 {
24991 /* Substitute parameter packs into each argument in the
24992 TREE_LIST. */
24993 in_base_initializer = 1;
24994 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24995 {
24996 tree expanded_exprs;
24997
24998 /* Expand the argument. */
24999 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25000 expanded_exprs
25001 = tsubst_pack_expansion (expr, argvec,
25002 tf_warning_or_error,
25003 NULL_TREE);
25004 if (expanded_exprs == error_mark_node)
25005 continue;
25006
25007 /* Prepend each of the expanded expressions to the
25008 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25009 for (i = 0; i < len; i++)
25010 {
25011 TREE_VEC_ELT (expanded_arguments, i) =
25012 tree_cons (NULL_TREE,
25013 TREE_VEC_ELT (expanded_exprs, i),
25014 TREE_VEC_ELT (expanded_arguments, i));
25015 }
25016 }
25017 in_base_initializer = 0;
25018
25019 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25020 since we built them backwards. */
25021 for (i = 0; i < len; i++)
25022 {
25023 TREE_VEC_ELT (expanded_arguments, i) =
25024 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25025 }
25026 }
25027 }
25028
25029 for (i = 0; i < len; ++i)
25030 {
25031 if (expanded_bases)
25032 {
25033 decl = TREE_VEC_ELT (expanded_bases, i);
25034 decl = expand_member_init (decl);
25035 init = TREE_VEC_ELT (expanded_arguments, i);
25036 }
25037 else
25038 {
25039 tree tmp;
25040 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25041 tf_warning_or_error, NULL_TREE);
25042
25043 decl = expand_member_init (decl);
25044 if (decl && !DECL_P (decl))
25045 in_base_initializer = 1;
25046
25047 init = TREE_VALUE (t);
25048 tmp = init;
25049 if (init != void_type_node)
25050 init = tsubst_expr (init, argvec,
25051 tf_warning_or_error, NULL_TREE,
25052 /*integral_constant_expression_p=*/false);
25053 if (init == NULL_TREE && tmp != NULL_TREE)
25054 /* If we had an initializer but it instantiated to nothing,
25055 value-initialize the object. This will only occur when
25056 the initializer was a pack expansion where the parameter
25057 packs used in that expansion were of length zero. */
25058 init = void_type_node;
25059 in_base_initializer = 0;
25060 }
25061
25062 if (target_ctor != error_mark_node
25063 && init != error_mark_node)
25064 {
25065 error ("mem-initializer for %qD follows constructor delegation",
25066 decl);
25067 return inits;
25068 }
25069 /* Look for a target constructor. */
25070 if (init != error_mark_node
25071 && decl && CLASS_TYPE_P (decl)
25072 && same_type_p (decl, current_class_type))
25073 {
25074 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25075 if (inits)
25076 {
25077 error ("constructor delegation follows mem-initializer for %qD",
25078 TREE_PURPOSE (inits));
25079 continue;
25080 }
25081 target_ctor = init;
25082 }
25083
25084 if (decl)
25085 {
25086 init = build_tree_list (decl, init);
25087 TREE_CHAIN (init) = inits;
25088 inits = init;
25089 }
25090 }
25091 }
25092 return inits;
25093 }
25094
25095 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25096
25097 static void
25098 set_current_access_from_decl (tree decl)
25099 {
25100 if (TREE_PRIVATE (decl))
25101 current_access_specifier = access_private_node;
25102 else if (TREE_PROTECTED (decl))
25103 current_access_specifier = access_protected_node;
25104 else
25105 current_access_specifier = access_public_node;
25106 }
25107
25108 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25109 is the instantiation (which should have been created with
25110 start_enum) and ARGS are the template arguments to use. */
25111
25112 static void
25113 tsubst_enum (tree tag, tree newtag, tree args)
25114 {
25115 tree e;
25116
25117 if (SCOPED_ENUM_P (newtag))
25118 begin_scope (sk_scoped_enum, newtag);
25119
25120 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25121 {
25122 tree value;
25123 tree decl;
25124
25125 decl = TREE_VALUE (e);
25126 /* Note that in a template enum, the TREE_VALUE is the
25127 CONST_DECL, not the corresponding INTEGER_CST. */
25128 value = tsubst_expr (DECL_INITIAL (decl),
25129 args, tf_warning_or_error, NULL_TREE,
25130 /*integral_constant_expression_p=*/true);
25131
25132 /* Give this enumeration constant the correct access. */
25133 set_current_access_from_decl (decl);
25134
25135 /* Actually build the enumerator itself. Here we're assuming that
25136 enumerators can't have dependent attributes. */
25137 build_enumerator (DECL_NAME (decl), value, newtag,
25138 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25139 }
25140
25141 if (SCOPED_ENUM_P (newtag))
25142 finish_scope ();
25143
25144 finish_enum_value_list (newtag);
25145 finish_enum (newtag);
25146
25147 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25148 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25149 }
25150
25151 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25152 its type -- but without substituting the innermost set of template
25153 arguments. So, innermost set of template parameters will appear in
25154 the type. */
25155
25156 tree
25157 get_mostly_instantiated_function_type (tree decl)
25158 {
25159 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25160 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25161 }
25162
25163 /* Return truthvalue if we're processing a template different from
25164 the last one involved in diagnostics. */
25165 bool
25166 problematic_instantiation_changed (void)
25167 {
25168 return current_tinst_level != last_error_tinst_level;
25169 }
25170
25171 /* Remember current template involved in diagnostics. */
25172 void
25173 record_last_problematic_instantiation (void)
25174 {
25175 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25176 }
25177
25178 struct tinst_level *
25179 current_instantiation (void)
25180 {
25181 return current_tinst_level;
25182 }
25183
25184 /* Return TRUE if current_function_decl is being instantiated, false
25185 otherwise. */
25186
25187 bool
25188 instantiating_current_function_p (void)
25189 {
25190 return (current_instantiation ()
25191 && (current_instantiation ()->maybe_get_node ()
25192 == current_function_decl));
25193 }
25194
25195 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25196 type. Return false for ok, true for disallowed. Issue error and
25197 inform messages under control of COMPLAIN. */
25198
25199 static bool
25200 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25201 {
25202 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25203 return false;
25204 else if (TYPE_PTR_P (type))
25205 return false;
25206 else if (TYPE_REF_P (type)
25207 && !TYPE_REF_IS_RVALUE (type))
25208 return false;
25209 else if (TYPE_PTRMEM_P (type))
25210 return false;
25211 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25212 return false;
25213 else if (TREE_CODE (type) == TYPENAME_TYPE)
25214 return false;
25215 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25216 return false;
25217 else if (TREE_CODE (type) == NULLPTR_TYPE)
25218 return false;
25219 /* A bound template template parm could later be instantiated to have a valid
25220 nontype parm type via an alias template. */
25221 else if (cxx_dialect >= cxx11
25222 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25223 return false;
25224 else if (CLASS_TYPE_P (type))
25225 {
25226 if (cxx_dialect < cxx2a)
25227 {
25228 error ("non-type template parameters of class type only available "
25229 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25230 return true;
25231 }
25232 if (dependent_type_p (type))
25233 return false;
25234 if (!complete_type_or_else (type, NULL_TREE))
25235 return true;
25236 if (!literal_type_p (type))
25237 {
25238 error ("%qT is not a valid type for a template non-type parameter "
25239 "because it is not literal", type);
25240 explain_non_literal_class (type);
25241 return true;
25242 }
25243 if (cp_has_mutable_p (type))
25244 {
25245 error ("%qT is not a valid type for a template non-type parameter "
25246 "because it has a mutable member", type);
25247 return true;
25248 }
25249 /* FIXME check op<=> and strong structural equality once spaceship is
25250 implemented. */
25251 return false;
25252 }
25253
25254 if (complain & tf_error)
25255 {
25256 if (type == error_mark_node)
25257 inform (input_location, "invalid template non-type parameter");
25258 else
25259 error ("%q#T is not a valid type for a template non-type parameter",
25260 type);
25261 }
25262 return true;
25263 }
25264
25265 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25266 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25267
25268 static bool
25269 dependent_type_p_r (tree type)
25270 {
25271 tree scope;
25272
25273 /* [temp.dep.type]
25274
25275 A type is dependent if it is:
25276
25277 -- a template parameter. Template template parameters are types
25278 for us (since TYPE_P holds true for them) so we handle
25279 them here. */
25280 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25281 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25282 return true;
25283 /* -- a qualified-id with a nested-name-specifier which contains a
25284 class-name that names a dependent type or whose unqualified-id
25285 names a dependent type. */
25286 if (TREE_CODE (type) == TYPENAME_TYPE)
25287 return true;
25288
25289 /* An alias template specialization can be dependent even if the
25290 resulting type is not. */
25291 if (dependent_alias_template_spec_p (type))
25292 return true;
25293
25294 /* -- a cv-qualified type where the cv-unqualified type is
25295 dependent.
25296 No code is necessary for this bullet; the code below handles
25297 cv-qualified types, and we don't want to strip aliases with
25298 TYPE_MAIN_VARIANT because of DR 1558. */
25299 /* -- a compound type constructed from any dependent type. */
25300 if (TYPE_PTRMEM_P (type))
25301 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25302 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25303 (type)));
25304 else if (INDIRECT_TYPE_P (type))
25305 return dependent_type_p (TREE_TYPE (type));
25306 else if (FUNC_OR_METHOD_TYPE_P (type))
25307 {
25308 tree arg_type;
25309
25310 if (dependent_type_p (TREE_TYPE (type)))
25311 return true;
25312 for (arg_type = TYPE_ARG_TYPES (type);
25313 arg_type;
25314 arg_type = TREE_CHAIN (arg_type))
25315 if (dependent_type_p (TREE_VALUE (arg_type)))
25316 return true;
25317 if (cxx_dialect >= cxx17)
25318 /* A value-dependent noexcept-specifier makes the type dependent. */
25319 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25320 if (tree noex = TREE_PURPOSE (spec))
25321 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25322 affect overload resolution and treating it as dependent breaks
25323 things. Same for an unparsed noexcept expression. */
25324 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25325 && TREE_CODE (noex) != DEFERRED_PARSE
25326 && value_dependent_expression_p (noex))
25327 return true;
25328 return false;
25329 }
25330 /* -- an array type constructed from any dependent type or whose
25331 size is specified by a constant expression that is
25332 value-dependent.
25333
25334 We checked for type- and value-dependence of the bounds in
25335 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25336 if (TREE_CODE (type) == ARRAY_TYPE)
25337 {
25338 if (TYPE_DOMAIN (type)
25339 && dependent_type_p (TYPE_DOMAIN (type)))
25340 return true;
25341 return dependent_type_p (TREE_TYPE (type));
25342 }
25343
25344 /* -- a template-id in which either the template name is a template
25345 parameter ... */
25346 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25347 return true;
25348 /* ... or any of the template arguments is a dependent type or
25349 an expression that is type-dependent or value-dependent. */
25350 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25351 && (any_dependent_template_arguments_p
25352 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25353 return true;
25354
25355 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25356 dependent; if the argument of the `typeof' expression is not
25357 type-dependent, then it should already been have resolved. */
25358 if (TREE_CODE (type) == TYPEOF_TYPE
25359 || TREE_CODE (type) == DECLTYPE_TYPE
25360 || TREE_CODE (type) == UNDERLYING_TYPE)
25361 return true;
25362
25363 /* A template argument pack is dependent if any of its packed
25364 arguments are. */
25365 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25366 {
25367 tree args = ARGUMENT_PACK_ARGS (type);
25368 int i, len = TREE_VEC_LENGTH (args);
25369 for (i = 0; i < len; ++i)
25370 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25371 return true;
25372 }
25373
25374 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25375 be template parameters. */
25376 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25377 return true;
25378
25379 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25380 return true;
25381
25382 /* The standard does not specifically mention types that are local
25383 to template functions or local classes, but they should be
25384 considered dependent too. For example:
25385
25386 template <int I> void f() {
25387 enum E { a = I };
25388 S<sizeof (E)> s;
25389 }
25390
25391 The size of `E' cannot be known until the value of `I' has been
25392 determined. Therefore, `E' must be considered dependent. */
25393 scope = TYPE_CONTEXT (type);
25394 if (scope && TYPE_P (scope))
25395 return dependent_type_p (scope);
25396 /* Don't use type_dependent_expression_p here, as it can lead
25397 to infinite recursion trying to determine whether a lambda
25398 nested in a lambda is dependent (c++/47687). */
25399 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25400 && DECL_LANG_SPECIFIC (scope)
25401 && DECL_TEMPLATE_INFO (scope)
25402 && (any_dependent_template_arguments_p
25403 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25404 return true;
25405
25406 /* Other types are non-dependent. */
25407 return false;
25408 }
25409
25410 /* Returns TRUE if TYPE is dependent, in the sense of
25411 [temp.dep.type]. Note that a NULL type is considered dependent. */
25412
25413 bool
25414 dependent_type_p (tree type)
25415 {
25416 /* If there are no template parameters in scope, then there can't be
25417 any dependent types. */
25418 if (!processing_template_decl)
25419 {
25420 /* If we are not processing a template, then nobody should be
25421 providing us with a dependent type. */
25422 gcc_assert (type);
25423 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25424 return false;
25425 }
25426
25427 /* If the type is NULL, we have not computed a type for the entity
25428 in question; in that case, the type is dependent. */
25429 if (!type)
25430 return true;
25431
25432 /* Erroneous types can be considered non-dependent. */
25433 if (type == error_mark_node)
25434 return false;
25435
25436 /* Getting here with global_type_node means we improperly called this
25437 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25438 gcc_checking_assert (type != global_type_node);
25439
25440 /* If we have not already computed the appropriate value for TYPE,
25441 do so now. */
25442 if (!TYPE_DEPENDENT_P_VALID (type))
25443 {
25444 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25445 TYPE_DEPENDENT_P_VALID (type) = 1;
25446 }
25447
25448 return TYPE_DEPENDENT_P (type);
25449 }
25450
25451 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25452 lookup. In other words, a dependent type that is not the current
25453 instantiation. */
25454
25455 bool
25456 dependent_scope_p (tree scope)
25457 {
25458 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25459 && !currently_open_class (scope));
25460 }
25461
25462 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25463 an unknown base of 'this' (and is therefore instantiation-dependent). */
25464
25465 static bool
25466 unknown_base_ref_p (tree t)
25467 {
25468 if (!current_class_ptr)
25469 return false;
25470
25471 tree mem = TREE_OPERAND (t, 1);
25472 if (shared_member_p (mem))
25473 return false;
25474
25475 tree cur = current_nonlambda_class_type ();
25476 if (!any_dependent_bases_p (cur))
25477 return false;
25478
25479 tree ctx = TREE_OPERAND (t, 0);
25480 if (DERIVED_FROM_P (ctx, cur))
25481 return false;
25482
25483 return true;
25484 }
25485
25486 /* T is a SCOPE_REF; return whether we need to consider it
25487 instantiation-dependent so that we can check access at instantiation
25488 time even though we know which member it resolves to. */
25489
25490 static bool
25491 instantiation_dependent_scope_ref_p (tree t)
25492 {
25493 if (DECL_P (TREE_OPERAND (t, 1))
25494 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25495 && !unknown_base_ref_p (t)
25496 && accessible_in_template_p (TREE_OPERAND (t, 0),
25497 TREE_OPERAND (t, 1)))
25498 return false;
25499 else
25500 return true;
25501 }
25502
25503 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25504 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25505 expression. */
25506
25507 /* Note that this predicate is not appropriate for general expressions;
25508 only constant expressions (that satisfy potential_constant_expression)
25509 can be tested for value dependence. */
25510
25511 bool
25512 value_dependent_expression_p (tree expression)
25513 {
25514 if (!processing_template_decl || expression == NULL_TREE)
25515 return false;
25516
25517 /* A type-dependent expression is also value-dependent. */
25518 if (type_dependent_expression_p (expression))
25519 return true;
25520
25521 switch (TREE_CODE (expression))
25522 {
25523 case BASELINK:
25524 /* A dependent member function of the current instantiation. */
25525 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25526
25527 case FUNCTION_DECL:
25528 /* A dependent member function of the current instantiation. */
25529 if (DECL_CLASS_SCOPE_P (expression)
25530 && dependent_type_p (DECL_CONTEXT (expression)))
25531 return true;
25532 break;
25533
25534 case IDENTIFIER_NODE:
25535 /* A name that has not been looked up -- must be dependent. */
25536 return true;
25537
25538 case TEMPLATE_PARM_INDEX:
25539 /* A non-type template parm. */
25540 return true;
25541
25542 case CONST_DECL:
25543 /* A non-type template parm. */
25544 if (DECL_TEMPLATE_PARM_P (expression))
25545 return true;
25546 return value_dependent_expression_p (DECL_INITIAL (expression));
25547
25548 case VAR_DECL:
25549 /* A constant with literal type and is initialized
25550 with an expression that is value-dependent. */
25551 if (DECL_DEPENDENT_INIT_P (expression)
25552 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25553 || TYPE_REF_P (TREE_TYPE (expression)))
25554 return true;
25555 if (DECL_HAS_VALUE_EXPR_P (expression))
25556 {
25557 tree value_expr = DECL_VALUE_EXPR (expression);
25558 if (value_dependent_expression_p (value_expr))
25559 return true;
25560 }
25561 return false;
25562
25563 case DYNAMIC_CAST_EXPR:
25564 case STATIC_CAST_EXPR:
25565 case CONST_CAST_EXPR:
25566 case REINTERPRET_CAST_EXPR:
25567 case CAST_EXPR:
25568 case IMPLICIT_CONV_EXPR:
25569 /* These expressions are value-dependent if the type to which
25570 the cast occurs is dependent or the expression being casted
25571 is value-dependent. */
25572 {
25573 tree type = TREE_TYPE (expression);
25574
25575 if (dependent_type_p (type))
25576 return true;
25577
25578 /* A functional cast has a list of operands. */
25579 expression = TREE_OPERAND (expression, 0);
25580 if (!expression)
25581 {
25582 /* If there are no operands, it must be an expression such
25583 as "int()". This should not happen for aggregate types
25584 because it would form non-constant expressions. */
25585 gcc_assert (cxx_dialect >= cxx11
25586 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25587
25588 return false;
25589 }
25590
25591 if (TREE_CODE (expression) == TREE_LIST)
25592 return any_value_dependent_elements_p (expression);
25593
25594 return value_dependent_expression_p (expression);
25595 }
25596
25597 case SIZEOF_EXPR:
25598 if (SIZEOF_EXPR_TYPE_P (expression))
25599 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25600 /* FALLTHRU */
25601 case ALIGNOF_EXPR:
25602 case TYPEID_EXPR:
25603 /* A `sizeof' expression is value-dependent if the operand is
25604 type-dependent or is a pack expansion. */
25605 expression = TREE_OPERAND (expression, 0);
25606 if (PACK_EXPANSION_P (expression))
25607 return true;
25608 else if (TYPE_P (expression))
25609 return dependent_type_p (expression);
25610 return instantiation_dependent_uneval_expression_p (expression);
25611
25612 case AT_ENCODE_EXPR:
25613 /* An 'encode' expression is value-dependent if the operand is
25614 type-dependent. */
25615 expression = TREE_OPERAND (expression, 0);
25616 return dependent_type_p (expression);
25617
25618 case NOEXCEPT_EXPR:
25619 expression = TREE_OPERAND (expression, 0);
25620 return instantiation_dependent_uneval_expression_p (expression);
25621
25622 case SCOPE_REF:
25623 /* All instantiation-dependent expressions should also be considered
25624 value-dependent. */
25625 return instantiation_dependent_scope_ref_p (expression);
25626
25627 case COMPONENT_REF:
25628 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25629 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25630
25631 case NONTYPE_ARGUMENT_PACK:
25632 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25633 is value-dependent. */
25634 {
25635 tree values = ARGUMENT_PACK_ARGS (expression);
25636 int i, len = TREE_VEC_LENGTH (values);
25637
25638 for (i = 0; i < len; ++i)
25639 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25640 return true;
25641
25642 return false;
25643 }
25644
25645 case TRAIT_EXPR:
25646 {
25647 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25648
25649 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25650 return true;
25651
25652 if (!type2)
25653 return false;
25654
25655 if (TREE_CODE (type2) != TREE_LIST)
25656 return dependent_type_p (type2);
25657
25658 for (; type2; type2 = TREE_CHAIN (type2))
25659 if (dependent_type_p (TREE_VALUE (type2)))
25660 return true;
25661
25662 return false;
25663 }
25664
25665 case MODOP_EXPR:
25666 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25667 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25668
25669 case ARRAY_REF:
25670 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25671 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25672
25673 case ADDR_EXPR:
25674 {
25675 tree op = TREE_OPERAND (expression, 0);
25676 return (value_dependent_expression_p (op)
25677 || has_value_dependent_address (op));
25678 }
25679
25680 case REQUIRES_EXPR:
25681 /* Treat all requires-expressions as value-dependent so
25682 we don't try to fold them. */
25683 return true;
25684
25685 case TYPE_REQ:
25686 return dependent_type_p (TREE_OPERAND (expression, 0));
25687
25688 case CALL_EXPR:
25689 {
25690 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25691 return true;
25692 tree fn = get_callee_fndecl (expression);
25693 int i, nargs;
25694 nargs = call_expr_nargs (expression);
25695 for (i = 0; i < nargs; ++i)
25696 {
25697 tree op = CALL_EXPR_ARG (expression, i);
25698 /* In a call to a constexpr member function, look through the
25699 implicit ADDR_EXPR on the object argument so that it doesn't
25700 cause the call to be considered value-dependent. We also
25701 look through it in potential_constant_expression. */
25702 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25703 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25704 && TREE_CODE (op) == ADDR_EXPR)
25705 op = TREE_OPERAND (op, 0);
25706 if (value_dependent_expression_p (op))
25707 return true;
25708 }
25709 return false;
25710 }
25711
25712 case TEMPLATE_ID_EXPR:
25713 return variable_concept_p (TREE_OPERAND (expression, 0));
25714
25715 case CONSTRUCTOR:
25716 {
25717 unsigned ix;
25718 tree val;
25719 if (dependent_type_p (TREE_TYPE (expression)))
25720 return true;
25721 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25722 if (value_dependent_expression_p (val))
25723 return true;
25724 return false;
25725 }
25726
25727 case STMT_EXPR:
25728 /* Treat a GNU statement expression as dependent to avoid crashing
25729 under instantiate_non_dependent_expr; it can't be constant. */
25730 return true;
25731
25732 default:
25733 /* A constant expression is value-dependent if any subexpression is
25734 value-dependent. */
25735 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25736 {
25737 case tcc_reference:
25738 case tcc_unary:
25739 case tcc_comparison:
25740 case tcc_binary:
25741 case tcc_expression:
25742 case tcc_vl_exp:
25743 {
25744 int i, len = cp_tree_operand_length (expression);
25745
25746 for (i = 0; i < len; i++)
25747 {
25748 tree t = TREE_OPERAND (expression, i);
25749
25750 /* In some cases, some of the operands may be missing.
25751 (For example, in the case of PREDECREMENT_EXPR, the
25752 amount to increment by may be missing.) That doesn't
25753 make the expression dependent. */
25754 if (t && value_dependent_expression_p (t))
25755 return true;
25756 }
25757 }
25758 break;
25759 default:
25760 break;
25761 }
25762 break;
25763 }
25764
25765 /* The expression is not value-dependent. */
25766 return false;
25767 }
25768
25769 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25770 [temp.dep.expr]. Note that an expression with no type is
25771 considered dependent. Other parts of the compiler arrange for an
25772 expression with type-dependent subexpressions to have no type, so
25773 this function doesn't have to be fully recursive. */
25774
25775 bool
25776 type_dependent_expression_p (tree expression)
25777 {
25778 if (!processing_template_decl)
25779 return false;
25780
25781 if (expression == NULL_TREE || expression == error_mark_node)
25782 return false;
25783
25784 STRIP_ANY_LOCATION_WRAPPER (expression);
25785
25786 /* An unresolved name is always dependent. */
25787 if (identifier_p (expression)
25788 || TREE_CODE (expression) == USING_DECL
25789 || TREE_CODE (expression) == WILDCARD_DECL)
25790 return true;
25791
25792 /* A lambda-expression in template context is dependent. dependent_type_p is
25793 true for a lambda in the scope of a class or function template, but that
25794 doesn't cover all template contexts, like a default template argument. */
25795 if (TREE_CODE (expression) == LAMBDA_EXPR)
25796 return true;
25797
25798 /* A fold expression is type-dependent. */
25799 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25800 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25801 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25802 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25803 return true;
25804
25805 /* Some expression forms are never type-dependent. */
25806 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25807 || TREE_CODE (expression) == SIZEOF_EXPR
25808 || TREE_CODE (expression) == ALIGNOF_EXPR
25809 || TREE_CODE (expression) == AT_ENCODE_EXPR
25810 || TREE_CODE (expression) == NOEXCEPT_EXPR
25811 || TREE_CODE (expression) == TRAIT_EXPR
25812 || TREE_CODE (expression) == TYPEID_EXPR
25813 || TREE_CODE (expression) == DELETE_EXPR
25814 || TREE_CODE (expression) == VEC_DELETE_EXPR
25815 || TREE_CODE (expression) == THROW_EXPR
25816 || TREE_CODE (expression) == REQUIRES_EXPR)
25817 return false;
25818
25819 /* The types of these expressions depends only on the type to which
25820 the cast occurs. */
25821 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25822 || TREE_CODE (expression) == STATIC_CAST_EXPR
25823 || TREE_CODE (expression) == CONST_CAST_EXPR
25824 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25825 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25826 || TREE_CODE (expression) == CAST_EXPR)
25827 return dependent_type_p (TREE_TYPE (expression));
25828
25829 /* The types of these expressions depends only on the type created
25830 by the expression. */
25831 if (TREE_CODE (expression) == NEW_EXPR
25832 || TREE_CODE (expression) == VEC_NEW_EXPR)
25833 {
25834 /* For NEW_EXPR tree nodes created inside a template, either
25835 the object type itself or a TREE_LIST may appear as the
25836 operand 1. */
25837 tree type = TREE_OPERAND (expression, 1);
25838 if (TREE_CODE (type) == TREE_LIST)
25839 /* This is an array type. We need to check array dimensions
25840 as well. */
25841 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25842 || value_dependent_expression_p
25843 (TREE_OPERAND (TREE_VALUE (type), 1));
25844 else
25845 return dependent_type_p (type);
25846 }
25847
25848 if (TREE_CODE (expression) == SCOPE_REF)
25849 {
25850 tree scope = TREE_OPERAND (expression, 0);
25851 tree name = TREE_OPERAND (expression, 1);
25852
25853 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25854 contains an identifier associated by name lookup with one or more
25855 declarations declared with a dependent type, or...a
25856 nested-name-specifier or qualified-id that names a member of an
25857 unknown specialization. */
25858 return (type_dependent_expression_p (name)
25859 || dependent_scope_p (scope));
25860 }
25861
25862 if (TREE_CODE (expression) == TEMPLATE_DECL
25863 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25864 return uses_outer_template_parms (expression);
25865
25866 if (TREE_CODE (expression) == STMT_EXPR)
25867 expression = stmt_expr_value_expr (expression);
25868
25869 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25870 {
25871 tree elt;
25872 unsigned i;
25873
25874 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25875 {
25876 if (type_dependent_expression_p (elt))
25877 return true;
25878 }
25879 return false;
25880 }
25881
25882 /* A static data member of the current instantiation with incomplete
25883 array type is type-dependent, as the definition and specializations
25884 can have different bounds. */
25885 if (VAR_P (expression)
25886 && DECL_CLASS_SCOPE_P (expression)
25887 && dependent_type_p (DECL_CONTEXT (expression))
25888 && VAR_HAD_UNKNOWN_BOUND (expression))
25889 return true;
25890
25891 /* An array of unknown bound depending on a variadic parameter, eg:
25892
25893 template<typename... Args>
25894 void foo (Args... args)
25895 {
25896 int arr[] = { args... };
25897 }
25898
25899 template<int... vals>
25900 void bar ()
25901 {
25902 int arr[] = { vals... };
25903 }
25904
25905 If the array has no length and has an initializer, it must be that
25906 we couldn't determine its length in cp_complete_array_type because
25907 it is dependent. */
25908 if (VAR_P (expression)
25909 && TREE_TYPE (expression) != NULL_TREE
25910 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25911 && !TYPE_DOMAIN (TREE_TYPE (expression))
25912 && DECL_INITIAL (expression))
25913 return true;
25914
25915 /* A function or variable template-id is type-dependent if it has any
25916 dependent template arguments. */
25917 if (VAR_OR_FUNCTION_DECL_P (expression)
25918 && DECL_LANG_SPECIFIC (expression)
25919 && DECL_TEMPLATE_INFO (expression))
25920 {
25921 /* Consider the innermost template arguments, since those are the ones
25922 that come from the template-id; the template arguments for the
25923 enclosing class do not make it type-dependent unless they are used in
25924 the type of the decl. */
25925 if (instantiates_primary_template_p (expression)
25926 && (any_dependent_template_arguments_p
25927 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25928 return true;
25929 }
25930
25931 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25932 type-dependent. Checking this is important for functions with auto return
25933 type, which looks like a dependent type. */
25934 if (TREE_CODE (expression) == FUNCTION_DECL
25935 && !(DECL_CLASS_SCOPE_P (expression)
25936 && dependent_type_p (DECL_CONTEXT (expression)))
25937 && !(DECL_LANG_SPECIFIC (expression)
25938 && DECL_FRIEND_P (expression)
25939 && (!DECL_FRIEND_CONTEXT (expression)
25940 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25941 && !DECL_LOCAL_FUNCTION_P (expression))
25942 {
25943 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25944 || undeduced_auto_decl (expression));
25945 return false;
25946 }
25947
25948 /* Always dependent, on the number of arguments if nothing else. */
25949 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25950 return true;
25951
25952 if (TREE_TYPE (expression) == unknown_type_node)
25953 {
25954 if (TREE_CODE (expression) == ADDR_EXPR)
25955 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25956 if (TREE_CODE (expression) == COMPONENT_REF
25957 || TREE_CODE (expression) == OFFSET_REF)
25958 {
25959 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25960 return true;
25961 expression = TREE_OPERAND (expression, 1);
25962 if (identifier_p (expression))
25963 return false;
25964 }
25965 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25966 if (TREE_CODE (expression) == SCOPE_REF)
25967 return false;
25968
25969 if (BASELINK_P (expression))
25970 {
25971 if (BASELINK_OPTYPE (expression)
25972 && dependent_type_p (BASELINK_OPTYPE (expression)))
25973 return true;
25974 expression = BASELINK_FUNCTIONS (expression);
25975 }
25976
25977 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25978 {
25979 if (any_dependent_template_arguments_p
25980 (TREE_OPERAND (expression, 1)))
25981 return true;
25982 expression = TREE_OPERAND (expression, 0);
25983 if (identifier_p (expression))
25984 return true;
25985 }
25986
25987 gcc_assert (OVL_P (expression));
25988
25989 for (lkp_iterator iter (expression); iter; ++iter)
25990 if (type_dependent_expression_p (*iter))
25991 return true;
25992
25993 return false;
25994 }
25995
25996 /* The type of a non-type template parm declared with a placeholder type
25997 depends on the corresponding template argument, even though
25998 placeholders are not normally considered dependent. */
25999 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26000 && is_auto (TREE_TYPE (expression)))
26001 return true;
26002
26003 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26004
26005 /* Dependent type attributes might not have made it from the decl to
26006 the type yet. */
26007 if (DECL_P (expression)
26008 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26009 return true;
26010
26011 return (dependent_type_p (TREE_TYPE (expression)));
26012 }
26013
26014 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26015 type-dependent if the expression refers to a member of the current
26016 instantiation and the type of the referenced member is dependent, or the
26017 class member access expression refers to a member of an unknown
26018 specialization.
26019
26020 This function returns true if the OBJECT in such a class member access
26021 expression is of an unknown specialization. */
26022
26023 bool
26024 type_dependent_object_expression_p (tree object)
26025 {
26026 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26027 dependent. */
26028 if (TREE_CODE (object) == IDENTIFIER_NODE)
26029 return true;
26030 tree scope = TREE_TYPE (object);
26031 return (!scope || dependent_scope_p (scope));
26032 }
26033
26034 /* walk_tree callback function for instantiation_dependent_expression_p,
26035 below. Returns non-zero if a dependent subexpression is found. */
26036
26037 static tree
26038 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26039 void * /*data*/)
26040 {
26041 if (TYPE_P (*tp))
26042 {
26043 /* We don't have to worry about decltype currently because decltype
26044 of an instantiation-dependent expr is a dependent type. This
26045 might change depending on the resolution of DR 1172. */
26046 *walk_subtrees = false;
26047 return NULL_TREE;
26048 }
26049 enum tree_code code = TREE_CODE (*tp);
26050 switch (code)
26051 {
26052 /* Don't treat an argument list as dependent just because it has no
26053 TREE_TYPE. */
26054 case TREE_LIST:
26055 case TREE_VEC:
26056 case NONTYPE_ARGUMENT_PACK:
26057 return NULL_TREE;
26058
26059 case TEMPLATE_PARM_INDEX:
26060 if (dependent_type_p (TREE_TYPE (*tp)))
26061 return *tp;
26062 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26063 return *tp;
26064 /* We'll check value-dependence separately. */
26065 return NULL_TREE;
26066
26067 /* Handle expressions with type operands. */
26068 case SIZEOF_EXPR:
26069 case ALIGNOF_EXPR:
26070 case TYPEID_EXPR:
26071 case AT_ENCODE_EXPR:
26072 {
26073 tree op = TREE_OPERAND (*tp, 0);
26074 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26075 op = TREE_TYPE (op);
26076 if (TYPE_P (op))
26077 {
26078 if (dependent_type_p (op))
26079 return *tp;
26080 else
26081 {
26082 *walk_subtrees = false;
26083 return NULL_TREE;
26084 }
26085 }
26086 break;
26087 }
26088
26089 case COMPONENT_REF:
26090 if (identifier_p (TREE_OPERAND (*tp, 1)))
26091 /* In a template, finish_class_member_access_expr creates a
26092 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26093 type-dependent, so that we can check access control at
26094 instantiation time (PR 42277). See also Core issue 1273. */
26095 return *tp;
26096 break;
26097
26098 case SCOPE_REF:
26099 if (instantiation_dependent_scope_ref_p (*tp))
26100 return *tp;
26101 else
26102 break;
26103
26104 /* Treat statement-expressions as dependent. */
26105 case BIND_EXPR:
26106 return *tp;
26107
26108 /* Treat requires-expressions as dependent. */
26109 case REQUIRES_EXPR:
26110 return *tp;
26111
26112 case CALL_EXPR:
26113 /* Treat calls to function concepts as dependent. */
26114 if (function_concept_check_p (*tp))
26115 return *tp;
26116 break;
26117
26118 case TEMPLATE_ID_EXPR:
26119 /* And variable concepts. */
26120 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26121 return *tp;
26122 break;
26123
26124 case CONSTRUCTOR:
26125 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26126 return *tp;
26127 break;
26128
26129 default:
26130 break;
26131 }
26132
26133 if (type_dependent_expression_p (*tp))
26134 return *tp;
26135 else
26136 return NULL_TREE;
26137 }
26138
26139 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26140 sense defined by the ABI:
26141
26142 "An expression is instantiation-dependent if it is type-dependent
26143 or value-dependent, or it has a subexpression that is type-dependent
26144 or value-dependent."
26145
26146 Except don't actually check value-dependence for unevaluated expressions,
26147 because in sizeof(i) we don't care about the value of i. Checking
26148 type-dependence will in turn check value-dependence of array bounds/template
26149 arguments as needed. */
26150
26151 bool
26152 instantiation_dependent_uneval_expression_p (tree expression)
26153 {
26154 tree result;
26155
26156 if (!processing_template_decl)
26157 return false;
26158
26159 if (expression == error_mark_node)
26160 return false;
26161
26162 result = cp_walk_tree_without_duplicates (&expression,
26163 instantiation_dependent_r, NULL);
26164 return result != NULL_TREE;
26165 }
26166
26167 /* As above, but also check value-dependence of the expression as a whole. */
26168
26169 bool
26170 instantiation_dependent_expression_p (tree expression)
26171 {
26172 return (instantiation_dependent_uneval_expression_p (expression)
26173 || value_dependent_expression_p (expression));
26174 }
26175
26176 /* Like type_dependent_expression_p, but it also works while not processing
26177 a template definition, i.e. during substitution or mangling. */
26178
26179 bool
26180 type_dependent_expression_p_push (tree expr)
26181 {
26182 bool b;
26183 ++processing_template_decl;
26184 b = type_dependent_expression_p (expr);
26185 --processing_template_decl;
26186 return b;
26187 }
26188
26189 /* Returns TRUE if ARGS contains a type-dependent expression. */
26190
26191 bool
26192 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26193 {
26194 unsigned int i;
26195 tree arg;
26196
26197 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26198 {
26199 if (type_dependent_expression_p (arg))
26200 return true;
26201 }
26202 return false;
26203 }
26204
26205 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26206 expressions) contains any type-dependent expressions. */
26207
26208 bool
26209 any_type_dependent_elements_p (const_tree list)
26210 {
26211 for (; list; list = TREE_CHAIN (list))
26212 if (type_dependent_expression_p (TREE_VALUE (list)))
26213 return true;
26214
26215 return false;
26216 }
26217
26218 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26219 expressions) contains any value-dependent expressions. */
26220
26221 bool
26222 any_value_dependent_elements_p (const_tree list)
26223 {
26224 for (; list; list = TREE_CHAIN (list))
26225 if (value_dependent_expression_p (TREE_VALUE (list)))
26226 return true;
26227
26228 return false;
26229 }
26230
26231 /* Returns TRUE if the ARG (a template argument) is dependent. */
26232
26233 bool
26234 dependent_template_arg_p (tree arg)
26235 {
26236 if (!processing_template_decl)
26237 return false;
26238
26239 /* Assume a template argument that was wrongly written by the user
26240 is dependent. This is consistent with what
26241 any_dependent_template_arguments_p [that calls this function]
26242 does. */
26243 if (!arg || arg == error_mark_node)
26244 return true;
26245
26246 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26247 arg = argument_pack_select_arg (arg);
26248
26249 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26250 return true;
26251 if (TREE_CODE (arg) == TEMPLATE_DECL)
26252 {
26253 if (DECL_TEMPLATE_PARM_P (arg))
26254 return true;
26255 /* A member template of a dependent class is not necessarily
26256 type-dependent, but it is a dependent template argument because it
26257 will be a member of an unknown specialization to that template. */
26258 tree scope = CP_DECL_CONTEXT (arg);
26259 return TYPE_P (scope) && dependent_type_p (scope);
26260 }
26261 else if (ARGUMENT_PACK_P (arg))
26262 {
26263 tree args = ARGUMENT_PACK_ARGS (arg);
26264 int i, len = TREE_VEC_LENGTH (args);
26265 for (i = 0; i < len; ++i)
26266 {
26267 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26268 return true;
26269 }
26270
26271 return false;
26272 }
26273 else if (TYPE_P (arg))
26274 return dependent_type_p (arg);
26275 else
26276 return (type_dependent_expression_p (arg)
26277 || value_dependent_expression_p (arg));
26278 }
26279
26280 /* Returns true if ARGS (a collection of template arguments) contains
26281 any types that require structural equality testing. */
26282
26283 bool
26284 any_template_arguments_need_structural_equality_p (tree args)
26285 {
26286 int i;
26287 int j;
26288
26289 if (!args)
26290 return false;
26291 if (args == error_mark_node)
26292 return true;
26293
26294 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26295 {
26296 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26297 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26298 {
26299 tree arg = TREE_VEC_ELT (level, j);
26300 tree packed_args = NULL_TREE;
26301 int k, len = 1;
26302
26303 if (ARGUMENT_PACK_P (arg))
26304 {
26305 /* Look inside the argument pack. */
26306 packed_args = ARGUMENT_PACK_ARGS (arg);
26307 len = TREE_VEC_LENGTH (packed_args);
26308 }
26309
26310 for (k = 0; k < len; ++k)
26311 {
26312 if (packed_args)
26313 arg = TREE_VEC_ELT (packed_args, k);
26314
26315 if (error_operand_p (arg))
26316 return true;
26317 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26318 continue;
26319 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26320 return true;
26321 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26322 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26323 return true;
26324 }
26325 }
26326 }
26327
26328 return false;
26329 }
26330
26331 /* Returns true if ARGS (a collection of template arguments) contains
26332 any dependent arguments. */
26333
26334 bool
26335 any_dependent_template_arguments_p (const_tree args)
26336 {
26337 int i;
26338 int j;
26339
26340 if (!args)
26341 return false;
26342 if (args == error_mark_node)
26343 return true;
26344
26345 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26346 {
26347 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26348 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26349 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26350 return true;
26351 }
26352
26353 return false;
26354 }
26355
26356 /* Returns true if ARGS contains any errors. */
26357
26358 bool
26359 any_erroneous_template_args_p (const_tree args)
26360 {
26361 int i;
26362 int j;
26363
26364 if (args == error_mark_node)
26365 return true;
26366
26367 if (args && TREE_CODE (args) != TREE_VEC)
26368 {
26369 if (tree ti = get_template_info (args))
26370 args = TI_ARGS (ti);
26371 else
26372 args = NULL_TREE;
26373 }
26374
26375 if (!args)
26376 return false;
26377
26378 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26379 {
26380 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26381 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26382 if (error_operand_p (TREE_VEC_ELT (level, j)))
26383 return true;
26384 }
26385
26386 return false;
26387 }
26388
26389 /* Returns TRUE if the template TMPL is type-dependent. */
26390
26391 bool
26392 dependent_template_p (tree tmpl)
26393 {
26394 if (TREE_CODE (tmpl) == OVERLOAD)
26395 {
26396 for (lkp_iterator iter (tmpl); iter; ++iter)
26397 if (dependent_template_p (*iter))
26398 return true;
26399 return false;
26400 }
26401
26402 /* Template template parameters are dependent. */
26403 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26404 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26405 return true;
26406 /* So are names that have not been looked up. */
26407 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26408 return true;
26409 return false;
26410 }
26411
26412 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26413
26414 bool
26415 dependent_template_id_p (tree tmpl, tree args)
26416 {
26417 return (dependent_template_p (tmpl)
26418 || any_dependent_template_arguments_p (args));
26419 }
26420
26421 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26422 are dependent. */
26423
26424 bool
26425 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26426 {
26427 int i;
26428
26429 if (!processing_template_decl)
26430 return false;
26431
26432 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26433 {
26434 tree decl = TREE_VEC_ELT (declv, i);
26435 tree init = TREE_VEC_ELT (initv, i);
26436 tree cond = TREE_VEC_ELT (condv, i);
26437 tree incr = TREE_VEC_ELT (incrv, i);
26438
26439 if (type_dependent_expression_p (decl)
26440 || TREE_CODE (decl) == SCOPE_REF)
26441 return true;
26442
26443 if (init && type_dependent_expression_p (init))
26444 return true;
26445
26446 if (cond == global_namespace)
26447 return true;
26448
26449 if (type_dependent_expression_p (cond))
26450 return true;
26451
26452 if (COMPARISON_CLASS_P (cond)
26453 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26454 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26455 return true;
26456
26457 if (TREE_CODE (incr) == MODOP_EXPR)
26458 {
26459 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26460 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26461 return true;
26462 }
26463 else if (type_dependent_expression_p (incr))
26464 return true;
26465 else if (TREE_CODE (incr) == MODIFY_EXPR)
26466 {
26467 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26468 return true;
26469 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26470 {
26471 tree t = TREE_OPERAND (incr, 1);
26472 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26473 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26474 return true;
26475
26476 /* If this loop has a class iterator with != comparison
26477 with increment other than i++/++i/i--/--i, make sure the
26478 increment is constant. */
26479 if (CLASS_TYPE_P (TREE_TYPE (decl))
26480 && TREE_CODE (cond) == NE_EXPR)
26481 {
26482 if (TREE_OPERAND (t, 0) == decl)
26483 t = TREE_OPERAND (t, 1);
26484 else
26485 t = TREE_OPERAND (t, 0);
26486 if (TREE_CODE (t) != INTEGER_CST)
26487 return true;
26488 }
26489 }
26490 }
26491 }
26492
26493 return false;
26494 }
26495
26496 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26497 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26498 no such TYPE can be found. Note that this function peers inside
26499 uninstantiated templates and therefore should be used only in
26500 extremely limited situations. ONLY_CURRENT_P restricts this
26501 peering to the currently open classes hierarchy (which is required
26502 when comparing types). */
26503
26504 tree
26505 resolve_typename_type (tree type, bool only_current_p)
26506 {
26507 tree scope;
26508 tree name;
26509 tree decl;
26510 int quals;
26511 tree pushed_scope;
26512 tree result;
26513
26514 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26515
26516 scope = TYPE_CONTEXT (type);
26517 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26518 gcc_checking_assert (uses_template_parms (scope));
26519
26520 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26521 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26522 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26523 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26524 identifier of the TYPENAME_TYPE anymore.
26525 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26526 TYPENAME_TYPE instead, we avoid messing up with a possible
26527 typedef variant case. */
26528 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26529
26530 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26531 it first before we can figure out what NAME refers to. */
26532 if (TREE_CODE (scope) == TYPENAME_TYPE)
26533 {
26534 if (TYPENAME_IS_RESOLVING_P (scope))
26535 /* Given a class template A with a dependent base with nested type C,
26536 typedef typename A::C::C C will land us here, as trying to resolve
26537 the initial A::C leads to the local C typedef, which leads back to
26538 A::C::C. So we break the recursion now. */
26539 return type;
26540 else
26541 scope = resolve_typename_type (scope, only_current_p);
26542 }
26543 /* If we don't know what SCOPE refers to, then we cannot resolve the
26544 TYPENAME_TYPE. */
26545 if (!CLASS_TYPE_P (scope))
26546 return type;
26547 /* If this is a typedef, we don't want to look inside (c++/11987). */
26548 if (typedef_variant_p (type))
26549 return type;
26550 /* If SCOPE isn't the template itself, it will not have a valid
26551 TYPE_FIELDS list. */
26552 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26553 /* scope is either the template itself or a compatible instantiation
26554 like X<T>, so look up the name in the original template. */
26555 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26556 /* If scope has no fields, it can't be a current instantiation. Check this
26557 before currently_open_class to avoid infinite recursion (71515). */
26558 if (!TYPE_FIELDS (scope))
26559 return type;
26560 /* If the SCOPE is not the current instantiation, there's no reason
26561 to look inside it. */
26562 if (only_current_p && !currently_open_class (scope))
26563 return type;
26564 /* Enter the SCOPE so that name lookup will be resolved as if we
26565 were in the class definition. In particular, SCOPE will no
26566 longer be considered a dependent type. */
26567 pushed_scope = push_scope (scope);
26568 /* Look up the declaration. */
26569 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26570 tf_warning_or_error);
26571
26572 result = NULL_TREE;
26573
26574 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26575 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26576 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26577 if (!decl)
26578 /*nop*/;
26579 else if (identifier_p (fullname)
26580 && TREE_CODE (decl) == TYPE_DECL)
26581 {
26582 result = TREE_TYPE (decl);
26583 if (result == error_mark_node)
26584 result = NULL_TREE;
26585 }
26586 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26587 && DECL_CLASS_TEMPLATE_P (decl))
26588 {
26589 /* Obtain the template and the arguments. */
26590 tree tmpl = TREE_OPERAND (fullname, 0);
26591 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26592 {
26593 /* We get here with a plain identifier because a previous tentative
26594 parse of the nested-name-specifier as part of a ptr-operator saw
26595 ::template X<A>. The use of ::template is necessary in a
26596 ptr-operator, but wrong in a declarator-id.
26597
26598 [temp.names]: In a qualified-id of a declarator-id, the keyword
26599 template shall not appear at the top level. */
26600 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26601 "keyword %<template%> not allowed in declarator-id");
26602 tmpl = decl;
26603 }
26604 tree args = TREE_OPERAND (fullname, 1);
26605 /* Instantiate the template. */
26606 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26607 /*entering_scope=*/true,
26608 tf_error | tf_user);
26609 if (result == error_mark_node)
26610 result = NULL_TREE;
26611 }
26612
26613 /* Leave the SCOPE. */
26614 if (pushed_scope)
26615 pop_scope (pushed_scope);
26616
26617 /* If we failed to resolve it, return the original typename. */
26618 if (!result)
26619 return type;
26620
26621 /* If lookup found a typename type, resolve that too. */
26622 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26623 {
26624 /* Ill-formed programs can cause infinite recursion here, so we
26625 must catch that. */
26626 TYPENAME_IS_RESOLVING_P (result) = 1;
26627 result = resolve_typename_type (result, only_current_p);
26628 TYPENAME_IS_RESOLVING_P (result) = 0;
26629 }
26630
26631 /* Qualify the resulting type. */
26632 quals = cp_type_quals (type);
26633 if (quals)
26634 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26635
26636 return result;
26637 }
26638
26639 /* EXPR is an expression which is not type-dependent. Return a proxy
26640 for EXPR that can be used to compute the types of larger
26641 expressions containing EXPR. */
26642
26643 tree
26644 build_non_dependent_expr (tree expr)
26645 {
26646 tree orig_expr = expr;
26647 tree inner_expr;
26648
26649 /* When checking, try to get a constant value for all non-dependent
26650 expressions in order to expose bugs in *_dependent_expression_p
26651 and constexpr. This can affect code generation, see PR70704, so
26652 only do this for -fchecking=2. */
26653 if (flag_checking > 1
26654 && cxx_dialect >= cxx11
26655 /* Don't do this during nsdmi parsing as it can lead to
26656 unexpected recursive instantiations. */
26657 && !parsing_nsdmi ()
26658 /* Don't do this during concept expansion either and for
26659 the same reason. */
26660 && !expanding_concept ())
26661 fold_non_dependent_expr (expr, tf_none);
26662
26663 STRIP_ANY_LOCATION_WRAPPER (expr);
26664
26665 /* Preserve OVERLOADs; the functions must be available to resolve
26666 types. */
26667 inner_expr = expr;
26668 if (TREE_CODE (inner_expr) == STMT_EXPR)
26669 inner_expr = stmt_expr_value_expr (inner_expr);
26670 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26671 inner_expr = TREE_OPERAND (inner_expr, 0);
26672 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26673 inner_expr = TREE_OPERAND (inner_expr, 1);
26674 if (is_overloaded_fn (inner_expr)
26675 || TREE_CODE (inner_expr) == OFFSET_REF)
26676 return orig_expr;
26677 /* There is no need to return a proxy for a variable or enumerator. */
26678 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26679 return orig_expr;
26680 /* Preserve string constants; conversions from string constants to
26681 "char *" are allowed, even though normally a "const char *"
26682 cannot be used to initialize a "char *". */
26683 if (TREE_CODE (expr) == STRING_CST)
26684 return orig_expr;
26685 /* Preserve void and arithmetic constants, as an optimization -- there is no
26686 reason to create a new node. */
26687 if (TREE_CODE (expr) == VOID_CST
26688 || TREE_CODE (expr) == INTEGER_CST
26689 || TREE_CODE (expr) == REAL_CST)
26690 return orig_expr;
26691 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26692 There is at least one place where we want to know that a
26693 particular expression is a throw-expression: when checking a ?:
26694 expression, there are special rules if the second or third
26695 argument is a throw-expression. */
26696 if (TREE_CODE (expr) == THROW_EXPR)
26697 return orig_expr;
26698
26699 /* Don't wrap an initializer list, we need to be able to look inside. */
26700 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26701 return orig_expr;
26702
26703 /* Don't wrap a dummy object, we need to be able to test for it. */
26704 if (is_dummy_object (expr))
26705 return orig_expr;
26706
26707 if (TREE_CODE (expr) == COND_EXPR)
26708 return build3 (COND_EXPR,
26709 TREE_TYPE (expr),
26710 TREE_OPERAND (expr, 0),
26711 (TREE_OPERAND (expr, 1)
26712 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26713 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26714 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26715 if (TREE_CODE (expr) == COMPOUND_EXPR
26716 && !COMPOUND_EXPR_OVERLOADED (expr))
26717 return build2 (COMPOUND_EXPR,
26718 TREE_TYPE (expr),
26719 TREE_OPERAND (expr, 0),
26720 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26721
26722 /* If the type is unknown, it can't really be non-dependent */
26723 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26724
26725 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26726 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26727 TREE_TYPE (expr), expr);
26728 }
26729
26730 /* ARGS is a vector of expressions as arguments to a function call.
26731 Replace the arguments with equivalent non-dependent expressions.
26732 This modifies ARGS in place. */
26733
26734 void
26735 make_args_non_dependent (vec<tree, va_gc> *args)
26736 {
26737 unsigned int ix;
26738 tree arg;
26739
26740 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26741 {
26742 tree newarg = build_non_dependent_expr (arg);
26743 if (newarg != arg)
26744 (*args)[ix] = newarg;
26745 }
26746 }
26747
26748 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26749 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26750 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26751
26752 static tree
26753 make_auto_1 (tree name, bool set_canonical)
26754 {
26755 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26756 TYPE_NAME (au) = build_decl (input_location,
26757 TYPE_DECL, name, au);
26758 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26759 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26760 (0, processing_template_decl + 1, processing_template_decl + 1,
26761 TYPE_NAME (au), NULL_TREE);
26762 if (set_canonical)
26763 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26764 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26765 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26766
26767 return au;
26768 }
26769
26770 tree
26771 make_decltype_auto (void)
26772 {
26773 return make_auto_1 (decltype_auto_identifier, true);
26774 }
26775
26776 tree
26777 make_auto (void)
26778 {
26779 return make_auto_1 (auto_identifier, true);
26780 }
26781
26782 /* Return a C++17 deduction placeholder for class template TMPL. */
26783
26784 tree
26785 make_template_placeholder (tree tmpl)
26786 {
26787 tree t = make_auto_1 (auto_identifier, false);
26788 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26789 /* Our canonical type depends on the placeholder. */
26790 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26791 return t;
26792 }
26793
26794 /* True iff T is a C++17 class template deduction placeholder. */
26795
26796 bool
26797 template_placeholder_p (tree t)
26798 {
26799 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26800 }
26801
26802 /* Make a "constrained auto" type-specifier. This is an
26803 auto type with constraints that must be associated after
26804 deduction. The constraint is formed from the given
26805 CONC and its optional sequence of arguments, which are
26806 non-null if written as partial-concept-id. */
26807
26808 tree
26809 make_constrained_auto (tree con, tree args)
26810 {
26811 tree type = make_auto_1 (auto_identifier, false);
26812
26813 /* Build the constraint. */
26814 tree tmpl = DECL_TI_TEMPLATE (con);
26815 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26816 expr = build_concept_check (expr, type, args);
26817
26818 tree constr = normalize_expression (expr);
26819 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26820
26821 /* Our canonical type depends on the constraint. */
26822 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26823
26824 /* Attach the constraint to the type declaration. */
26825 tree decl = TYPE_NAME (type);
26826 return decl;
26827 }
26828
26829 /* Given type ARG, return std::initializer_list<ARG>. */
26830
26831 static tree
26832 listify (tree arg)
26833 {
26834 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26835
26836 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26837 {
26838 gcc_rich_location richloc (input_location);
26839 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26840 error_at (&richloc,
26841 "deducing from brace-enclosed initializer list"
26842 " requires %<#include <initializer_list>%>");
26843
26844 return error_mark_node;
26845 }
26846 tree argvec = make_tree_vec (1);
26847 TREE_VEC_ELT (argvec, 0) = arg;
26848
26849 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26850 NULL_TREE, 0, tf_warning_or_error);
26851 }
26852
26853 /* Replace auto in TYPE with std::initializer_list<auto>. */
26854
26855 static tree
26856 listify_autos (tree type, tree auto_node)
26857 {
26858 tree init_auto = listify (strip_top_quals (auto_node));
26859 tree argvec = make_tree_vec (1);
26860 TREE_VEC_ELT (argvec, 0) = init_auto;
26861 if (processing_template_decl)
26862 argvec = add_to_template_args (current_template_args (), argvec);
26863 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26864 }
26865
26866 /* Hash traits for hashing possibly constrained 'auto'
26867 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26868
26869 struct auto_hash : default_hash_traits<tree>
26870 {
26871 static inline hashval_t hash (tree);
26872 static inline bool equal (tree, tree);
26873 };
26874
26875 /* Hash the 'auto' T. */
26876
26877 inline hashval_t
26878 auto_hash::hash (tree t)
26879 {
26880 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26881 /* Matching constrained-type-specifiers denote the same template
26882 parameter, so hash the constraint. */
26883 return hash_placeholder_constraint (c);
26884 else
26885 /* But unconstrained autos are all separate, so just hash the pointer. */
26886 return iterative_hash_object (t, 0);
26887 }
26888
26889 /* Compare two 'auto's. */
26890
26891 inline bool
26892 auto_hash::equal (tree t1, tree t2)
26893 {
26894 if (t1 == t2)
26895 return true;
26896
26897 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26898 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26899
26900 /* Two unconstrained autos are distinct. */
26901 if (!c1 || !c2)
26902 return false;
26903
26904 return equivalent_placeholder_constraints (c1, c2);
26905 }
26906
26907 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26908 constrained) auto, add it to the vector. */
26909
26910 static int
26911 extract_autos_r (tree t, void *data)
26912 {
26913 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26914 if (is_auto (t))
26915 {
26916 /* All the autos were built with index 0; fix that up now. */
26917 tree *p = hash.find_slot (t, INSERT);
26918 unsigned idx;
26919 if (*p)
26920 /* If this is a repeated constrained-type-specifier, use the index we
26921 chose before. */
26922 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26923 else
26924 {
26925 /* Otherwise this is new, so use the current count. */
26926 *p = t;
26927 idx = hash.elements () - 1;
26928 }
26929 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26930 }
26931
26932 /* Always keep walking. */
26933 return 0;
26934 }
26935
26936 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26937 says they can appear anywhere in the type. */
26938
26939 static tree
26940 extract_autos (tree type)
26941 {
26942 hash_set<tree> visited;
26943 hash_table<auto_hash> hash (2);
26944
26945 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26946
26947 tree tree_vec = make_tree_vec (hash.elements());
26948 for (hash_table<auto_hash>::iterator iter = hash.begin();
26949 iter != hash.end(); ++iter)
26950 {
26951 tree elt = *iter;
26952 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26953 TREE_VEC_ELT (tree_vec, i)
26954 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26955 }
26956
26957 return tree_vec;
26958 }
26959
26960 /* The stem for deduction guide names. */
26961 const char *const dguide_base = "__dguide_";
26962
26963 /* Return the name for a deduction guide for class template TMPL. */
26964
26965 tree
26966 dguide_name (tree tmpl)
26967 {
26968 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26969 tree tname = TYPE_IDENTIFIER (type);
26970 char *buf = (char *) alloca (1 + strlen (dguide_base)
26971 + IDENTIFIER_LENGTH (tname));
26972 memcpy (buf, dguide_base, strlen (dguide_base));
26973 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26974 IDENTIFIER_LENGTH (tname) + 1);
26975 tree dname = get_identifier (buf);
26976 TREE_TYPE (dname) = type;
26977 return dname;
26978 }
26979
26980 /* True if NAME is the name of a deduction guide. */
26981
26982 bool
26983 dguide_name_p (tree name)
26984 {
26985 return (TREE_CODE (name) == IDENTIFIER_NODE
26986 && TREE_TYPE (name)
26987 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26988 strlen (dguide_base)));
26989 }
26990
26991 /* True if FN is a deduction guide. */
26992
26993 bool
26994 deduction_guide_p (const_tree fn)
26995 {
26996 if (DECL_P (fn))
26997 if (tree name = DECL_NAME (fn))
26998 return dguide_name_p (name);
26999 return false;
27000 }
27001
27002 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27003
27004 bool
27005 copy_guide_p (const_tree fn)
27006 {
27007 gcc_assert (deduction_guide_p (fn));
27008 if (!DECL_ARTIFICIAL (fn))
27009 return false;
27010 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27011 return (TREE_CHAIN (parms) == void_list_node
27012 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27013 }
27014
27015 /* True if FN is a guide generated from a constructor template. */
27016
27017 bool
27018 template_guide_p (const_tree fn)
27019 {
27020 gcc_assert (deduction_guide_p (fn));
27021 if (!DECL_ARTIFICIAL (fn))
27022 return false;
27023 tree tmpl = DECL_TI_TEMPLATE (fn);
27024 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27025 return PRIMARY_TEMPLATE_P (org);
27026 return false;
27027 }
27028
27029 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27030 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27031 template parameter types. Note that the handling of template template
27032 parameters relies on current_template_parms being set appropriately for the
27033 new template. */
27034
27035 static tree
27036 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27037 tree tsubst_args, tsubst_flags_t complain)
27038 {
27039 if (olddecl == error_mark_node)
27040 return error_mark_node;
27041
27042 tree oldidx = get_template_parm_index (olddecl);
27043
27044 tree newtype;
27045 if (TREE_CODE (olddecl) == TYPE_DECL
27046 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27047 {
27048 tree oldtype = TREE_TYPE (olddecl);
27049 newtype = cxx_make_type (TREE_CODE (oldtype));
27050 TYPE_MAIN_VARIANT (newtype) = newtype;
27051 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27052 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27053 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27054 }
27055 else
27056 {
27057 newtype = TREE_TYPE (olddecl);
27058 if (type_uses_auto (newtype))
27059 {
27060 // Substitute once to fix references to other template parameters.
27061 newtype = tsubst (newtype, tsubst_args,
27062 complain|tf_partial, NULL_TREE);
27063 // Now substitute again to reduce the level of the auto.
27064 newtype = tsubst (newtype, current_template_args (),
27065 complain, NULL_TREE);
27066 }
27067 else
27068 newtype = tsubst (newtype, tsubst_args,
27069 complain, NULL_TREE);
27070 }
27071
27072 tree newdecl
27073 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27074 DECL_NAME (olddecl), newtype);
27075 SET_DECL_TEMPLATE_PARM_P (newdecl);
27076
27077 tree newidx;
27078 if (TREE_CODE (olddecl) == TYPE_DECL
27079 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27080 {
27081 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27082 = build_template_parm_index (index, level, level,
27083 newdecl, newtype);
27084 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27085 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27086 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27087 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27088
27089 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27090 {
27091 DECL_TEMPLATE_RESULT (newdecl)
27092 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27093 DECL_NAME (olddecl), newtype);
27094 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27095 // First create a copy (ttargs) of tsubst_args with an
27096 // additional level for the template template parameter's own
27097 // template parameters (ttparms).
27098 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27099 (DECL_TEMPLATE_PARMS (olddecl)));
27100 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27101 tree ttargs = make_tree_vec (depth + 1);
27102 for (int i = 0; i < depth; ++i)
27103 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27104 TREE_VEC_ELT (ttargs, depth)
27105 = template_parms_level_to_args (ttparms);
27106 // Substitute ttargs into ttparms to fix references to
27107 // other template parameters.
27108 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27109 complain|tf_partial);
27110 // Now substitute again with args based on tparms, to reduce
27111 // the level of the ttparms.
27112 ttargs = current_template_args ();
27113 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27114 complain);
27115 // Finally, tack the adjusted parms onto tparms.
27116 ttparms = tree_cons (size_int (depth), ttparms,
27117 current_template_parms);
27118 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27119 }
27120 }
27121 else
27122 {
27123 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27124 tree newconst
27125 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27126 TREE_CODE (oldconst),
27127 DECL_NAME (oldconst), newtype);
27128 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27129 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27130 SET_DECL_TEMPLATE_PARM_P (newconst);
27131 newidx = build_template_parm_index (index, level, level,
27132 newconst, newtype);
27133 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27134 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27135 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27136 }
27137
27138 return newdecl;
27139 }
27140
27141 /* Returns a C++17 class deduction guide template based on the constructor
27142 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27143 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27144
27145 static tree
27146 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27147 {
27148 tree type, tparms, targs, fparms, fargs, ci;
27149 bool memtmpl = false;
27150 bool explicit_p;
27151 location_t loc;
27152 tree fn_tmpl = NULL_TREE;
27153
27154 if (TYPE_P (ctor))
27155 {
27156 type = ctor;
27157 bool copy_p = TYPE_REF_P (type);
27158 if (copy_p)
27159 {
27160 type = TREE_TYPE (type);
27161 fparms = tree_cons (NULL_TREE, type, void_list_node);
27162 }
27163 else
27164 fparms = void_list_node;
27165
27166 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27167 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27168 targs = CLASSTYPE_TI_ARGS (type);
27169 ci = NULL_TREE;
27170 fargs = NULL_TREE;
27171 loc = DECL_SOURCE_LOCATION (ctmpl);
27172 explicit_p = false;
27173 }
27174 else
27175 {
27176 ++processing_template_decl;
27177 bool ok = true;
27178
27179 fn_tmpl
27180 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27181 : DECL_TI_TEMPLATE (ctor));
27182 if (outer_args)
27183 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27184 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27185
27186 type = DECL_CONTEXT (ctor);
27187
27188 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27189 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27190 fully specialized args for the enclosing class. Strip those off, as
27191 the deduction guide won't have those template parameters. */
27192 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27193 TMPL_PARMS_DEPTH (tparms));
27194 /* Discard the 'this' parameter. */
27195 fparms = FUNCTION_ARG_CHAIN (ctor);
27196 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27197 ci = get_constraints (ctor);
27198 loc = DECL_SOURCE_LOCATION (ctor);
27199 explicit_p = DECL_NONCONVERTING_P (ctor);
27200
27201 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27202 {
27203 memtmpl = true;
27204
27205 /* For a member template constructor, we need to flatten the two
27206 template parameter lists into one, and then adjust the function
27207 signature accordingly. This gets...complicated. */
27208 tree save_parms = current_template_parms;
27209
27210 /* For a member template we should have two levels of parms/args, one
27211 for the class and one for the constructor. We stripped
27212 specialized args for further enclosing classes above. */
27213 const int depth = 2;
27214 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27215
27216 /* Template args for translating references to the two-level template
27217 parameters into references to the one-level template parameters we
27218 are creating. */
27219 tree tsubst_args = copy_node (targs);
27220 TMPL_ARGS_LEVEL (tsubst_args, depth)
27221 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27222
27223 /* Template parms for the constructor template. */
27224 tree ftparms = TREE_VALUE (tparms);
27225 unsigned flen = TREE_VEC_LENGTH (ftparms);
27226 /* Template parms for the class template. */
27227 tparms = TREE_CHAIN (tparms);
27228 tree ctparms = TREE_VALUE (tparms);
27229 unsigned clen = TREE_VEC_LENGTH (ctparms);
27230 /* Template parms for the deduction guide start as a copy of the
27231 template parms for the class. We set current_template_parms for
27232 lookup_template_class_1. */
27233 current_template_parms = tparms = copy_node (tparms);
27234 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27235 for (unsigned i = 0; i < clen; ++i)
27236 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27237
27238 /* Now we need to rewrite the constructor parms to append them to the
27239 class parms. */
27240 for (unsigned i = 0; i < flen; ++i)
27241 {
27242 unsigned index = i + clen;
27243 unsigned level = 1;
27244 tree oldelt = TREE_VEC_ELT (ftparms, i);
27245 tree olddecl = TREE_VALUE (oldelt);
27246 tree newdecl = rewrite_template_parm (olddecl, index, level,
27247 tsubst_args, complain);
27248 if (newdecl == error_mark_node)
27249 ok = false;
27250 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27251 tsubst_args, complain, ctor);
27252 tree list = build_tree_list (newdef, newdecl);
27253 TEMPLATE_PARM_CONSTRAINTS (list)
27254 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27255 tsubst_args, complain, ctor);
27256 TREE_VEC_ELT (new_vec, index) = list;
27257 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27258 }
27259
27260 /* Now we have a final set of template parms to substitute into the
27261 function signature. */
27262 targs = template_parms_to_args (tparms);
27263 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27264 complain, ctor);
27265 if (fparms == error_mark_node)
27266 ok = false;
27267 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27268 if (ci)
27269 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27270
27271 current_template_parms = save_parms;
27272 }
27273
27274 --processing_template_decl;
27275 if (!ok)
27276 return error_mark_node;
27277 }
27278
27279 if (!memtmpl)
27280 {
27281 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27282 tparms = copy_node (tparms);
27283 INNERMOST_TEMPLATE_PARMS (tparms)
27284 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27285 }
27286
27287 tree fntype = build_function_type (type, fparms);
27288 tree ded_fn = build_lang_decl_loc (loc,
27289 FUNCTION_DECL,
27290 dguide_name (type), fntype);
27291 DECL_ARGUMENTS (ded_fn) = fargs;
27292 DECL_ARTIFICIAL (ded_fn) = true;
27293 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27294 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27295 DECL_ARTIFICIAL (ded_tmpl) = true;
27296 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27297 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27298 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27299 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27300 if (DECL_P (ctor))
27301 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27302 if (ci)
27303 set_constraints (ded_tmpl, ci);
27304
27305 return ded_tmpl;
27306 }
27307
27308 /* Deduce template arguments for the class template placeholder PTYPE for
27309 template TMPL based on the initializer INIT, and return the resulting
27310 type. */
27311
27312 static tree
27313 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27314 tsubst_flags_t complain)
27315 {
27316 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27317 {
27318 /* We should have handled this in the caller. */
27319 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27320 return ptype;
27321 if (complain & tf_error)
27322 error ("non-class template %qT used without template arguments", tmpl);
27323 return error_mark_node;
27324 }
27325 if (init && TREE_TYPE (init) == ptype)
27326 /* Using the template parm as its own argument. */
27327 return ptype;
27328
27329 tree type = TREE_TYPE (tmpl);
27330
27331 bool try_list_ctor = false;
27332
27333 releasing_vec rv_args = NULL;
27334 vec<tree,va_gc> *&args = *&rv_args;
27335 if (init == NULL_TREE
27336 || TREE_CODE (init) == TREE_LIST)
27337 args = make_tree_vector_from_list (init);
27338 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27339 {
27340 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27341 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27342 {
27343 /* As an exception, the first phase in 16.3.1.7 (considering the
27344 initializer list as a single argument) is omitted if the
27345 initializer list consists of a single expression of type cv U,
27346 where U is a specialization of C or a class derived from a
27347 specialization of C. */
27348 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27349 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27350 {
27351 tree etype = TREE_TYPE (elt);
27352 tree tparms = (INNERMOST_TEMPLATE_PARMS
27353 (DECL_TEMPLATE_PARMS (tmpl)));
27354 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27355 int err = unify (tparms, targs, type, etype,
27356 UNIFY_ALLOW_DERIVED, /*explain*/false);
27357 if (err == 0)
27358 try_list_ctor = false;
27359 ggc_free (targs);
27360 }
27361 }
27362 if (try_list_ctor || is_std_init_list (type))
27363 args = make_tree_vector_single (init);
27364 else
27365 args = make_tree_vector_from_ctor (init);
27366 }
27367 else
27368 args = make_tree_vector_single (init);
27369
27370 tree dname = dguide_name (tmpl);
27371 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27372 /*type*/false, /*complain*/false,
27373 /*hidden*/false);
27374 bool elided = false;
27375 if (cands == error_mark_node)
27376 cands = NULL_TREE;
27377
27378 /* Prune explicit deduction guides in copy-initialization context. */
27379 if (flags & LOOKUP_ONLYCONVERTING)
27380 {
27381 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27382 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27383 elided = true;
27384
27385 if (elided)
27386 {
27387 /* Found a nonconverting guide, prune the candidates. */
27388 tree pruned = NULL_TREE;
27389 for (lkp_iterator iter (cands); iter; ++iter)
27390 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27391 pruned = lookup_add (*iter, pruned);
27392
27393 cands = pruned;
27394 }
27395 }
27396
27397 tree outer_args = NULL_TREE;
27398 if (DECL_CLASS_SCOPE_P (tmpl)
27399 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27400 {
27401 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27402 type = TREE_TYPE (most_general_template (tmpl));
27403 }
27404
27405 bool saw_ctor = false;
27406 // FIXME cache artificial deduction guides
27407 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27408 {
27409 /* Skip inherited constructors. */
27410 if (iter.using_p ())
27411 continue;
27412
27413 tree guide = build_deduction_guide (*iter, outer_args, complain);
27414 if (guide == error_mark_node)
27415 return error_mark_node;
27416 if ((flags & LOOKUP_ONLYCONVERTING)
27417 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27418 elided = true;
27419 else
27420 cands = lookup_add (guide, cands);
27421
27422 saw_ctor = true;
27423 }
27424
27425 tree call = error_mark_node;
27426
27427 /* If this is list-initialization and the class has a list constructor, first
27428 try deducing from the list as a single argument, as [over.match.list]. */
27429 tree list_cands = NULL_TREE;
27430 if (try_list_ctor && cands)
27431 for (lkp_iterator iter (cands); iter; ++iter)
27432 {
27433 tree dg = *iter;
27434 if (is_list_ctor (dg))
27435 list_cands = lookup_add (dg, list_cands);
27436 }
27437 if (list_cands)
27438 {
27439 ++cp_unevaluated_operand;
27440 call = build_new_function_call (list_cands, &args, tf_decltype);
27441 --cp_unevaluated_operand;
27442
27443 if (call == error_mark_node)
27444 {
27445 /* That didn't work, now try treating the list as a sequence of
27446 arguments. */
27447 release_tree_vector (args);
27448 args = make_tree_vector_from_ctor (init);
27449 }
27450 }
27451
27452 /* Maybe generate an implicit deduction guide. */
27453 if (call == error_mark_node && args->length () < 2)
27454 {
27455 tree gtype = NULL_TREE;
27456
27457 if (args->length () == 1)
27458 /* Generate a copy guide. */
27459 gtype = build_reference_type (type);
27460 else if (!saw_ctor)
27461 /* Generate a default guide. */
27462 gtype = type;
27463
27464 if (gtype)
27465 {
27466 tree guide = build_deduction_guide (gtype, outer_args, complain);
27467 if (guide == error_mark_node)
27468 return error_mark_node;
27469 cands = lookup_add (guide, cands);
27470 }
27471 }
27472
27473 if (elided && !cands)
27474 {
27475 error ("cannot deduce template arguments for copy-initialization"
27476 " of %qT, as it has no non-explicit deduction guides or "
27477 "user-declared constructors", type);
27478 return error_mark_node;
27479 }
27480 else if (!cands && call == error_mark_node)
27481 {
27482 error ("cannot deduce template arguments of %qT, as it has no viable "
27483 "deduction guides", type);
27484 return error_mark_node;
27485 }
27486
27487 if (call == error_mark_node)
27488 {
27489 ++cp_unevaluated_operand;
27490 call = build_new_function_call (cands, &args, tf_decltype);
27491 --cp_unevaluated_operand;
27492 }
27493
27494 if (call == error_mark_node && (complain & tf_warning_or_error))
27495 {
27496 error ("class template argument deduction failed:");
27497
27498 ++cp_unevaluated_operand;
27499 call = build_new_function_call (cands, &args, complain | tf_decltype);
27500 --cp_unevaluated_operand;
27501
27502 if (elided)
27503 inform (input_location, "explicit deduction guides not considered "
27504 "for copy-initialization");
27505 }
27506
27507 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27508 }
27509
27510 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27511 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27512 The CONTEXT determines the context in which auto deduction is performed
27513 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27514 OUTER_TARGS are used during template argument deduction
27515 (context == adc_unify) to properly substitute the result, and is ignored
27516 in other contexts.
27517
27518 For partial-concept-ids, extra args may be appended to the list of deduced
27519 template arguments prior to determining constraint satisfaction. */
27520
27521 tree
27522 do_auto_deduction (tree type, tree init, tree auto_node,
27523 tsubst_flags_t complain, auto_deduction_context context,
27524 tree outer_targs, int flags)
27525 {
27526 tree targs;
27527
27528 if (init == error_mark_node)
27529 return error_mark_node;
27530
27531 if (init && type_dependent_expression_p (init)
27532 && context != adc_unify)
27533 /* Defining a subset of type-dependent expressions that we can deduce
27534 from ahead of time isn't worth the trouble. */
27535 return type;
27536
27537 /* Similarly, we can't deduce from another undeduced decl. */
27538 if (init && undeduced_auto_decl (init))
27539 return type;
27540
27541 /* We may be doing a partial substitution, but we still want to replace
27542 auto_node. */
27543 complain &= ~tf_partial;
27544
27545 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27546 /* C++17 class template argument deduction. */
27547 return do_class_deduction (type, tmpl, init, flags, complain);
27548
27549 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27550 /* Nothing we can do with this, even in deduction context. */
27551 return type;
27552
27553 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27554 with either a new invented type template parameter U or, if the
27555 initializer is a braced-init-list (8.5.4), with
27556 std::initializer_list<U>. */
27557 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27558 {
27559 if (!DIRECT_LIST_INIT_P (init))
27560 type = listify_autos (type, auto_node);
27561 else if (CONSTRUCTOR_NELTS (init) == 1)
27562 init = CONSTRUCTOR_ELT (init, 0)->value;
27563 else
27564 {
27565 if (complain & tf_warning_or_error)
27566 {
27567 if (permerror (input_location, "direct-list-initialization of "
27568 "%<auto%> requires exactly one element"))
27569 inform (input_location,
27570 "for deduction to %<std::initializer_list%>, use copy-"
27571 "list-initialization (i.e. add %<=%> before the %<{%>)");
27572 }
27573 type = listify_autos (type, auto_node);
27574 }
27575 }
27576
27577 if (type == error_mark_node)
27578 return error_mark_node;
27579
27580 init = resolve_nondeduced_context (init, complain);
27581
27582 if (context == adc_decomp_type
27583 && auto_node == type
27584 && init != error_mark_node
27585 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27586 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27587 and initializer has array type, deduce cv-qualified array type. */
27588 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27589 complain);
27590 else if (AUTO_IS_DECLTYPE (auto_node))
27591 {
27592 tree stripped_init = tree_strip_any_location_wrapper (init);
27593 bool id = (DECL_P (stripped_init)
27594 || ((TREE_CODE (init) == COMPONENT_REF
27595 || TREE_CODE (init) == SCOPE_REF)
27596 && !REF_PARENTHESIZED_P (init)));
27597 targs = make_tree_vec (1);
27598 TREE_VEC_ELT (targs, 0)
27599 = finish_decltype_type (init, id, tf_warning_or_error);
27600 if (type != auto_node)
27601 {
27602 if (complain & tf_error)
27603 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27604 return error_mark_node;
27605 }
27606 }
27607 else
27608 {
27609 tree parms = build_tree_list (NULL_TREE, type);
27610 tree tparms;
27611
27612 if (flag_concepts)
27613 tparms = extract_autos (type);
27614 else
27615 {
27616 tparms = make_tree_vec (1);
27617 TREE_VEC_ELT (tparms, 0)
27618 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27619 }
27620
27621 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27622 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27623 DEDUCE_CALL,
27624 NULL, /*explain_p=*/false);
27625 if (val > 0)
27626 {
27627 if (processing_template_decl)
27628 /* Try again at instantiation time. */
27629 return type;
27630 if (type && type != error_mark_node
27631 && (complain & tf_error))
27632 /* If type is error_mark_node a diagnostic must have been
27633 emitted by now. Also, having a mention to '<type error>'
27634 in the diagnostic is not really useful to the user. */
27635 {
27636 if (cfun
27637 && FNDECL_USED_AUTO (current_function_decl)
27638 && (auto_node
27639 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27640 && LAMBDA_FUNCTION_P (current_function_decl))
27641 error ("unable to deduce lambda return type from %qE", init);
27642 else
27643 error ("unable to deduce %qT from %qE", type, init);
27644 type_unification_real (tparms, targs, parms, &init, 1, 0,
27645 DEDUCE_CALL,
27646 NULL, /*explain_p=*/true);
27647 }
27648 return error_mark_node;
27649 }
27650 }
27651
27652 /* Check any placeholder constraints against the deduced type. */
27653 if (flag_concepts && !processing_template_decl)
27654 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27655 {
27656 /* Use the deduced type to check the associated constraints. If we
27657 have a partial-concept-id, rebuild the argument list so that
27658 we check using the extra arguments. */
27659 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27660 tree cargs = CHECK_CONSTR_ARGS (constr);
27661 if (TREE_VEC_LENGTH (cargs) > 1)
27662 {
27663 cargs = copy_node (cargs);
27664 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27665 }
27666 else
27667 cargs = targs;
27668 if (!constraints_satisfied_p (constr, cargs))
27669 {
27670 if (complain & tf_warning_or_error)
27671 {
27672 auto_diagnostic_group d;
27673 switch (context)
27674 {
27675 case adc_unspecified:
27676 case adc_unify:
27677 error("placeholder constraints not satisfied");
27678 break;
27679 case adc_variable_type:
27680 case adc_decomp_type:
27681 error ("deduced initializer does not satisfy "
27682 "placeholder constraints");
27683 break;
27684 case adc_return_type:
27685 error ("deduced return type does not satisfy "
27686 "placeholder constraints");
27687 break;
27688 case adc_requirement:
27689 error ("deduced expression type does not satisfy "
27690 "placeholder constraints");
27691 break;
27692 }
27693 diagnose_constraints (input_location, constr, targs);
27694 }
27695 return error_mark_node;
27696 }
27697 }
27698
27699 if (processing_template_decl && context != adc_unify)
27700 outer_targs = current_template_args ();
27701 targs = add_to_template_args (outer_targs, targs);
27702 return tsubst (type, targs, complain, NULL_TREE);
27703 }
27704
27705 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27706 result. */
27707
27708 tree
27709 splice_late_return_type (tree type, tree late_return_type)
27710 {
27711 if (is_auto (type))
27712 {
27713 if (late_return_type)
27714 return late_return_type;
27715
27716 tree idx = get_template_parm_index (type);
27717 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27718 /* In an abbreviated function template we didn't know we were dealing
27719 with a function template when we saw the auto return type, so update
27720 it to have the correct level. */
27721 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27722 }
27723 return type;
27724 }
27725
27726 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27727 'decltype(auto)' or a deduced class template. */
27728
27729 bool
27730 is_auto (const_tree type)
27731 {
27732 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27733 && (TYPE_IDENTIFIER (type) == auto_identifier
27734 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27735 return true;
27736 else
27737 return false;
27738 }
27739
27740 /* for_each_template_parm callback for type_uses_auto. */
27741
27742 int
27743 is_auto_r (tree tp, void */*data*/)
27744 {
27745 return is_auto (tp);
27746 }
27747
27748 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27749 a use of `auto'. Returns NULL_TREE otherwise. */
27750
27751 tree
27752 type_uses_auto (tree type)
27753 {
27754 if (type == NULL_TREE)
27755 return NULL_TREE;
27756 else if (flag_concepts)
27757 {
27758 /* The Concepts TS allows multiple autos in one type-specifier; just
27759 return the first one we find, do_auto_deduction will collect all of
27760 them. */
27761 if (uses_template_parms (type))
27762 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27763 /*visited*/NULL, /*nondeduced*/false);
27764 else
27765 return NULL_TREE;
27766 }
27767 else
27768 return find_type_usage (type, is_auto);
27769 }
27770
27771 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27772 concepts are enabled, auto is acceptable in template arguments, but
27773 only when TEMPL identifies a template class. Return TRUE if any
27774 such errors were reported. */
27775
27776 bool
27777 check_auto_in_tmpl_args (tree tmpl, tree args)
27778 {
27779 /* If there were previous errors, nevermind. */
27780 if (!args || TREE_CODE (args) != TREE_VEC)
27781 return false;
27782
27783 /* If TMPL is an identifier, we're parsing and we can't tell yet
27784 whether TMPL is supposed to be a type, a function or a variable.
27785 We'll only be able to tell during template substitution, so we
27786 expect to be called again then. If concepts are enabled and we
27787 know we have a type, we're ok. */
27788 if (flag_concepts
27789 && (identifier_p (tmpl)
27790 || (DECL_P (tmpl)
27791 && (DECL_TYPE_TEMPLATE_P (tmpl)
27792 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27793 return false;
27794
27795 /* Quickly search for any occurrences of auto; usually there won't
27796 be any, and then we'll avoid allocating the vector. */
27797 if (!type_uses_auto (args))
27798 return false;
27799
27800 bool errors = false;
27801
27802 tree vec = extract_autos (args);
27803 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27804 {
27805 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27806 error_at (DECL_SOURCE_LOCATION (xauto),
27807 "invalid use of %qT in template argument", xauto);
27808 errors = true;
27809 }
27810
27811 return errors;
27812 }
27813
27814 /* For a given template T, return the vector of typedefs referenced
27815 in T for which access check is needed at T instantiation time.
27816 T is either a FUNCTION_DECL or a RECORD_TYPE.
27817 Those typedefs were added to T by the function
27818 append_type_to_template_for_access_check. */
27819
27820 vec<qualified_typedef_usage_t, va_gc> *
27821 get_types_needing_access_check (tree t)
27822 {
27823 tree ti;
27824 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27825
27826 if (!t || t == error_mark_node)
27827 return NULL;
27828
27829 if (!(ti = get_template_info (t)))
27830 return NULL;
27831
27832 if (CLASS_TYPE_P (t)
27833 || TREE_CODE (t) == FUNCTION_DECL)
27834 {
27835 if (!TI_TEMPLATE (ti))
27836 return NULL;
27837
27838 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27839 }
27840
27841 return result;
27842 }
27843
27844 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27845 tied to T. That list of typedefs will be access checked at
27846 T instantiation time.
27847 T is either a FUNCTION_DECL or a RECORD_TYPE.
27848 TYPE_DECL is a TYPE_DECL node representing a typedef.
27849 SCOPE is the scope through which TYPE_DECL is accessed.
27850 LOCATION is the location of the usage point of TYPE_DECL.
27851
27852 This function is a subroutine of
27853 append_type_to_template_for_access_check. */
27854
27855 static void
27856 append_type_to_template_for_access_check_1 (tree t,
27857 tree type_decl,
27858 tree scope,
27859 location_t location)
27860 {
27861 qualified_typedef_usage_t typedef_usage;
27862 tree ti;
27863
27864 if (!t || t == error_mark_node)
27865 return;
27866
27867 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27868 || CLASS_TYPE_P (t))
27869 && type_decl
27870 && TREE_CODE (type_decl) == TYPE_DECL
27871 && scope);
27872
27873 if (!(ti = get_template_info (t)))
27874 return;
27875
27876 gcc_assert (TI_TEMPLATE (ti));
27877
27878 typedef_usage.typedef_decl = type_decl;
27879 typedef_usage.context = scope;
27880 typedef_usage.locus = location;
27881
27882 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27883 }
27884
27885 /* Append TYPE_DECL to the template TEMPL.
27886 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27887 At TEMPL instanciation time, TYPE_DECL will be checked to see
27888 if it can be accessed through SCOPE.
27889 LOCATION is the location of the usage point of TYPE_DECL.
27890
27891 e.g. consider the following code snippet:
27892
27893 class C
27894 {
27895 typedef int myint;
27896 };
27897
27898 template<class U> struct S
27899 {
27900 C::myint mi; // <-- usage point of the typedef C::myint
27901 };
27902
27903 S<char> s;
27904
27905 At S<char> instantiation time, we need to check the access of C::myint
27906 In other words, we need to check the access of the myint typedef through
27907 the C scope. For that purpose, this function will add the myint typedef
27908 and the scope C through which its being accessed to a list of typedefs
27909 tied to the template S. That list will be walked at template instantiation
27910 time and access check performed on each typedefs it contains.
27911 Note that this particular code snippet should yield an error because
27912 myint is private to C. */
27913
27914 void
27915 append_type_to_template_for_access_check (tree templ,
27916 tree type_decl,
27917 tree scope,
27918 location_t location)
27919 {
27920 qualified_typedef_usage_t *iter;
27921 unsigned i;
27922
27923 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27924
27925 /* Make sure we don't append the type to the template twice. */
27926 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27927 if (iter->typedef_decl == type_decl && scope == iter->context)
27928 return;
27929
27930 append_type_to_template_for_access_check_1 (templ, type_decl,
27931 scope, location);
27932 }
27933
27934 /* Convert the generic type parameters in PARM that match the types given in the
27935 range [START_IDX, END_IDX) from the current_template_parms into generic type
27936 packs. */
27937
27938 tree
27939 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27940 {
27941 tree current = current_template_parms;
27942 int depth = TMPL_PARMS_DEPTH (current);
27943 current = INNERMOST_TEMPLATE_PARMS (current);
27944 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27945
27946 for (int i = 0; i < start_idx; ++i)
27947 TREE_VEC_ELT (replacement, i)
27948 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27949
27950 for (int i = start_idx; i < end_idx; ++i)
27951 {
27952 /* Create a distinct parameter pack type from the current parm and add it
27953 to the replacement args to tsubst below into the generic function
27954 parameter. */
27955
27956 tree o = TREE_TYPE (TREE_VALUE
27957 (TREE_VEC_ELT (current, i)));
27958 tree t = copy_type (o);
27959 TEMPLATE_TYPE_PARM_INDEX (t)
27960 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27961 o, 0, 0, tf_none);
27962 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27963 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27964 TYPE_MAIN_VARIANT (t) = t;
27965 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27966 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27967 TREE_VEC_ELT (replacement, i) = t;
27968 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27969 }
27970
27971 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27972 TREE_VEC_ELT (replacement, i)
27973 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27974
27975 /* If there are more levels then build up the replacement with the outer
27976 template parms. */
27977 if (depth > 1)
27978 replacement = add_to_template_args (template_parms_to_args
27979 (TREE_CHAIN (current_template_parms)),
27980 replacement);
27981
27982 return tsubst (parm, replacement, tf_none, NULL_TREE);
27983 }
27984
27985 /* Entries in the decl_constraint hash table. */
27986 struct GTY((for_user)) constr_entry
27987 {
27988 tree decl;
27989 tree ci;
27990 };
27991
27992 /* Hashing function and equality for constraint entries. */
27993 struct constr_hasher : ggc_ptr_hash<constr_entry>
27994 {
27995 static hashval_t hash (constr_entry *e)
27996 {
27997 return (hashval_t)DECL_UID (e->decl);
27998 }
27999
28000 static bool equal (constr_entry *e1, constr_entry *e2)
28001 {
28002 return e1->decl == e2->decl;
28003 }
28004 };
28005
28006 /* A mapping from declarations to constraint information. Note that
28007 both templates and their underlying declarations are mapped to the
28008 same constraint information.
28009
28010 FIXME: This is defined in pt.c because garbage collection
28011 code is not being generated for constraint.cc. */
28012
28013 static GTY (()) hash_table<constr_hasher> *decl_constraints;
28014
28015 /* Returns the template constraints of declaration T. If T is not
28016 constrained, return NULL_TREE. Note that T must be non-null. */
28017
28018 tree
28019 get_constraints (tree t)
28020 {
28021 if (!flag_concepts)
28022 return NULL_TREE;
28023
28024 gcc_assert (DECL_P (t));
28025 if (TREE_CODE (t) == TEMPLATE_DECL)
28026 t = DECL_TEMPLATE_RESULT (t);
28027 constr_entry elt = { t, NULL_TREE };
28028 constr_entry* found = decl_constraints->find (&elt);
28029 if (found)
28030 return found->ci;
28031 else
28032 return NULL_TREE;
28033 }
28034
28035 /* Associate the given constraint information CI with the declaration
28036 T. If T is a template, then the constraints are associated with
28037 its underlying declaration. Don't build associations if CI is
28038 NULL_TREE. */
28039
28040 void
28041 set_constraints (tree t, tree ci)
28042 {
28043 if (!ci)
28044 return;
28045 gcc_assert (t && flag_concepts);
28046 if (TREE_CODE (t) == TEMPLATE_DECL)
28047 t = DECL_TEMPLATE_RESULT (t);
28048 gcc_assert (!get_constraints (t));
28049 constr_entry elt = {t, ci};
28050 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28051 constr_entry* entry = ggc_alloc<constr_entry> ();
28052 *entry = elt;
28053 *slot = entry;
28054 }
28055
28056 /* Remove the associated constraints of the declaration T. */
28057
28058 void
28059 remove_constraints (tree t)
28060 {
28061 gcc_assert (DECL_P (t));
28062 if (TREE_CODE (t) == TEMPLATE_DECL)
28063 t = DECL_TEMPLATE_RESULT (t);
28064
28065 constr_entry elt = {t, NULL_TREE};
28066 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28067 if (slot)
28068 decl_constraints->clear_slot (slot);
28069 }
28070
28071 /* Memoized satisfaction results for declarations. This
28072 maps the pair (constraint_info, arguments) to the result computed
28073 by constraints_satisfied_p. */
28074
28075 struct GTY((for_user)) constraint_sat_entry
28076 {
28077 tree ci;
28078 tree args;
28079 tree result;
28080 };
28081
28082 /* Hashing function and equality for constraint entries. */
28083
28084 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28085 {
28086 static hashval_t hash (constraint_sat_entry *e)
28087 {
28088 hashval_t val = iterative_hash_object(e->ci, 0);
28089 return iterative_hash_template_arg (e->args, val);
28090 }
28091
28092 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28093 {
28094 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28095 }
28096 };
28097
28098 /* Memoized satisfaction results for concept checks. */
28099
28100 struct GTY((for_user)) concept_spec_entry
28101 {
28102 tree tmpl;
28103 tree args;
28104 tree result;
28105 };
28106
28107 /* Hashing function and equality for constraint entries. */
28108
28109 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28110 {
28111 static hashval_t hash (concept_spec_entry *e)
28112 {
28113 return hash_tmpl_and_args (e->tmpl, e->args);
28114 }
28115
28116 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28117 {
28118 ++comparing_specializations;
28119 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28120 --comparing_specializations;
28121 return eq;
28122 }
28123 };
28124
28125 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28126 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28127
28128 /* Search for a memoized satisfaction result. Returns one of the
28129 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28130
28131 tree
28132 lookup_constraint_satisfaction (tree ci, tree args)
28133 {
28134 constraint_sat_entry elt = { ci, args, NULL_TREE };
28135 constraint_sat_entry* found = constraint_memos->find (&elt);
28136 if (found)
28137 return found->result;
28138 else
28139 return NULL_TREE;
28140 }
28141
28142 /* Memoize the result of a satisfication test. Returns the saved result. */
28143
28144 tree
28145 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28146 {
28147 constraint_sat_entry elt = {ci, args, result};
28148 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28149 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28150 *entry = elt;
28151 *slot = entry;
28152 return result;
28153 }
28154
28155 /* Search for a memoized satisfaction result for a concept. */
28156
28157 tree
28158 lookup_concept_satisfaction (tree tmpl, tree args)
28159 {
28160 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28161 concept_spec_entry* found = concept_memos->find (&elt);
28162 if (found)
28163 return found->result;
28164 else
28165 return NULL_TREE;
28166 }
28167
28168 /* Memoize the result of a concept check. Returns the saved result. */
28169
28170 tree
28171 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28172 {
28173 concept_spec_entry elt = {tmpl, args, result};
28174 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28175 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28176 *entry = elt;
28177 *slot = entry;
28178 return result;
28179 }
28180
28181 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28182
28183 /* Returns a prior concept specialization. This returns the substituted
28184 and normalized constraints defined by the concept. */
28185
28186 tree
28187 get_concept_expansion (tree tmpl, tree args)
28188 {
28189 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28190 concept_spec_entry* found = concept_expansions->find (&elt);
28191 if (found)
28192 return found->result;
28193 else
28194 return NULL_TREE;
28195 }
28196
28197 /* Save a concept expansion for later. */
28198
28199 tree
28200 save_concept_expansion (tree tmpl, tree args, tree def)
28201 {
28202 concept_spec_entry elt = {tmpl, args, def};
28203 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28204 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28205 *entry = elt;
28206 *slot = entry;
28207 return def;
28208 }
28209
28210 static hashval_t
28211 hash_subsumption_args (tree t1, tree t2)
28212 {
28213 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28214 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28215 int val = 0;
28216 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28217 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28218 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28219 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28220 return val;
28221 }
28222
28223 /* Compare the constraints of two subsumption entries. The LEFT1 and
28224 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28225 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28226
28227 static bool
28228 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28229 {
28230 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28231 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28232 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28233 CHECK_CONSTR_ARGS (right1)))
28234 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28235 CHECK_CONSTR_ARGS (right2));
28236 return false;
28237 }
28238
28239 /* Key/value pair for learning and memoizing subsumption results. This
28240 associates a pair of check constraints (including arguments) with
28241 a boolean value indicating the result. */
28242
28243 struct GTY((for_user)) subsumption_entry
28244 {
28245 tree t1;
28246 tree t2;
28247 bool result;
28248 };
28249
28250 /* Hashing function and equality for constraint entries. */
28251
28252 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28253 {
28254 static hashval_t hash (subsumption_entry *e)
28255 {
28256 return hash_subsumption_args (e->t1, e->t2);
28257 }
28258
28259 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28260 {
28261 ++comparing_specializations;
28262 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28263 --comparing_specializations;
28264 return eq;
28265 }
28266 };
28267
28268 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28269
28270 /* Search for a previously cached subsumption result. */
28271
28272 bool*
28273 lookup_subsumption_result (tree t1, tree t2)
28274 {
28275 subsumption_entry elt = { t1, t2, false };
28276 subsumption_entry* found = subsumption_table->find (&elt);
28277 if (found)
28278 return &found->result;
28279 else
28280 return 0;
28281 }
28282
28283 /* Save a subsumption result. */
28284
28285 bool
28286 save_subsumption_result (tree t1, tree t2, bool result)
28287 {
28288 subsumption_entry elt = {t1, t2, result};
28289 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28290 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28291 *entry = elt;
28292 *slot = entry;
28293 return result;
28294 }
28295
28296 /* Set up the hash table for constraint association. */
28297
28298 void
28299 init_constraint_processing (void)
28300 {
28301 if (!flag_concepts)
28302 return;
28303
28304 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28305 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28306 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28307 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28308 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28309 }
28310
28311 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28312 0..N-1. */
28313
28314 void
28315 declare_integer_pack (void)
28316 {
28317 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28318 build_function_type_list (integer_type_node,
28319 integer_type_node,
28320 NULL_TREE),
28321 NULL_TREE, ECF_CONST);
28322 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28323 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28324 DECL_FUNCTION_CODE (ipfn)
28325 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28326 }
28327
28328 /* Set up the hash tables for template instantiations. */
28329
28330 void
28331 init_template_processing (void)
28332 {
28333 /* FIXME: enable sanitization (PR87847) */
28334 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28335 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28336
28337 if (cxx_dialect >= cxx11)
28338 declare_integer_pack ();
28339 }
28340
28341 /* Print stats about the template hash tables for -fstats. */
28342
28343 void
28344 print_template_statistics (void)
28345 {
28346 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28347 "%f collisions\n", (long) decl_specializations->size (),
28348 (long) decl_specializations->elements (),
28349 decl_specializations->collisions ());
28350 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28351 "%f collisions\n", (long) type_specializations->size (),
28352 (long) type_specializations->elements (),
28353 type_specializations->collisions ());
28354 }
28355
28356 #if CHECKING_P
28357
28358 namespace selftest {
28359
28360 /* Verify that build_non_dependent_expr () works, for various expressions,
28361 and that location wrappers don't affect the results. */
28362
28363 static void
28364 test_build_non_dependent_expr ()
28365 {
28366 location_t loc = BUILTINS_LOCATION;
28367
28368 /* Verify constants, without and with location wrappers. */
28369 tree int_cst = build_int_cst (integer_type_node, 42);
28370 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28371
28372 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28373 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28374 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28375
28376 tree string_lit = build_string (4, "foo");
28377 TREE_TYPE (string_lit) = char_array_type_node;
28378 string_lit = fix_string_type (string_lit);
28379 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28380
28381 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28382 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28383 ASSERT_EQ (wrapped_string_lit,
28384 build_non_dependent_expr (wrapped_string_lit));
28385 }
28386
28387 /* Verify that type_dependent_expression_p () works correctly, even
28388 in the presence of location wrapper nodes. */
28389
28390 static void
28391 test_type_dependent_expression_p ()
28392 {
28393 location_t loc = BUILTINS_LOCATION;
28394
28395 tree name = get_identifier ("foo");
28396
28397 /* If no templates are involved, nothing is type-dependent. */
28398 gcc_assert (!processing_template_decl);
28399 ASSERT_FALSE (type_dependent_expression_p (name));
28400
28401 ++processing_template_decl;
28402
28403 /* Within a template, an unresolved name is always type-dependent. */
28404 ASSERT_TRUE (type_dependent_expression_p (name));
28405
28406 /* Ensure it copes with NULL_TREE and errors. */
28407 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28408 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28409
28410 /* A USING_DECL in a template should be type-dependent, even if wrapped
28411 with a location wrapper (PR c++/83799). */
28412 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28413 TREE_TYPE (using_decl) = integer_type_node;
28414 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28415 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28416 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28417 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28418
28419 --processing_template_decl;
28420 }
28421
28422 /* Run all of the selftests within this file. */
28423
28424 void
28425 cp_pt_c_tests ()
28426 {
28427 test_build_non_dependent_expr ();
28428 test_type_dependent_expression_p ();
28429 }
28430
28431 } // namespace selftest
28432
28433 #endif /* #if CHECKING_P */
28434
28435 #include "gt-cp-pt.h"