]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Remove quite obvious dead assignments.
[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 (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4959 {
4960 if ((!packed_args && tpd.arg_uses_template_parms[i])
4961 || (packed_args && uses_template_parms (arg)))
4962 error ("template argument %qE involves template parameter(s)",
4963 arg);
4964 else
4965 {
4966 /* Look at the corresponding template parameter,
4967 marking which template parameters its type depends
4968 upon. */
4969 tree type = TREE_TYPE (parm);
4970
4971 if (!tpd2.parms)
4972 {
4973 /* We haven't yet initialized TPD2. Do so now. */
4974 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4975 /* The number of parameters here is the number in the
4976 main template, which, as checked in the assertion
4977 above, is NARGS. */
4978 tpd2.parms = XALLOCAVEC (int, nargs);
4979 tpd2.level =
4980 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4981 }
4982
4983 /* Mark the template parameters. But this time, we're
4984 looking for the template parameters of the main
4985 template, not in the specialization. */
4986 tpd2.current_arg = i;
4987 tpd2.arg_uses_template_parms[i] = 0;
4988 memset (tpd2.parms, 0, sizeof (int) * nargs);
4989 for_each_template_parm (type,
4990 &mark_template_parm,
4991 &tpd2,
4992 NULL,
4993 /*include_nondeduced_p=*/false);
4994
4995 if (tpd2.arg_uses_template_parms [i])
4996 {
4997 /* The type depended on some template parameters.
4998 If they are fully specialized in the
4999 specialization, that's OK. */
5000 int j;
5001 int count = 0;
5002 for (j = 0; j < nargs; ++j)
5003 if (tpd2.parms[j] != 0
5004 && tpd.arg_uses_template_parms [j])
5005 ++count;
5006 if (count != 0)
5007 error_n (input_location, count,
5008 "type %qT of template argument %qE depends "
5009 "on a template parameter",
5010 "type %qT of template argument %qE depends "
5011 "on template parameters",
5012 type,
5013 arg);
5014 }
5015 }
5016 }
5017 }
5018 }
5019
5020 /* We should only get here once. */
5021 if (TREE_CODE (decl) == TYPE_DECL)
5022 gcc_assert (!COMPLETE_TYPE_P (type));
5023
5024 // Build the template decl.
5025 tree tmpl = build_template_decl (decl, current_template_parms,
5026 DECL_MEMBER_TEMPLATE_P (maintmpl));
5027 TREE_TYPE (tmpl) = type;
5028 DECL_TEMPLATE_RESULT (tmpl) = decl;
5029 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5030 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5031 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5032
5033 /* Give template template parms a DECL_CONTEXT of the template
5034 for which they are a parameter. */
5035 for (i = 0; i < ntparms; ++i)
5036 {
5037 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5038 if (TREE_CODE (parm) == TEMPLATE_DECL)
5039 DECL_CONTEXT (parm) = tmpl;
5040 }
5041
5042 if (VAR_P (decl))
5043 /* We didn't register this in check_explicit_specialization so we could
5044 wait until the constraints were set. */
5045 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5046 else
5047 associate_classtype_constraints (type);
5048
5049 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5050 = tree_cons (specargs, tmpl,
5051 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5052 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5053
5054 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5055 inst = TREE_CHAIN (inst))
5056 {
5057 tree instance = TREE_VALUE (inst);
5058 if (TYPE_P (instance)
5059 ? (COMPLETE_TYPE_P (instance)
5060 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5061 : DECL_TEMPLATE_INSTANTIATION (instance))
5062 {
5063 tree spec = most_specialized_partial_spec (instance, tf_none);
5064 tree inst_decl = (DECL_P (instance)
5065 ? instance : TYPE_NAME (instance));
5066 if (!spec)
5067 /* OK */;
5068 else if (spec == error_mark_node)
5069 permerror (input_location,
5070 "declaration of %qD ambiguates earlier template "
5071 "instantiation for %qD", decl, inst_decl);
5072 else if (TREE_VALUE (spec) == tmpl)
5073 permerror (input_location,
5074 "partial specialization of %qD after instantiation "
5075 "of %qD", decl, inst_decl);
5076 }
5077 }
5078
5079 return decl;
5080 }
5081
5082 /* PARM is a template parameter of some form; return the corresponding
5083 TEMPLATE_PARM_INDEX. */
5084
5085 static tree
5086 get_template_parm_index (tree parm)
5087 {
5088 if (TREE_CODE (parm) == PARM_DECL
5089 || TREE_CODE (parm) == CONST_DECL)
5090 parm = DECL_INITIAL (parm);
5091 else if (TREE_CODE (parm) == TYPE_DECL
5092 || TREE_CODE (parm) == TEMPLATE_DECL)
5093 parm = TREE_TYPE (parm);
5094 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5095 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5096 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5097 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5098 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5099 return parm;
5100 }
5101
5102 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5103 parameter packs used by the template parameter PARM. */
5104
5105 static void
5106 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5107 {
5108 /* A type parm can't refer to another parm. */
5109 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5110 return;
5111 else if (TREE_CODE (parm) == PARM_DECL)
5112 {
5113 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5114 ppd, ppd->visited);
5115 return;
5116 }
5117
5118 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5119
5120 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5121 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5122 {
5123 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5124 if (template_parameter_pack_p (p))
5125 /* Any packs in the type are expanded by this parameter. */;
5126 else
5127 fixed_parameter_pack_p_1 (p, ppd);
5128 }
5129 }
5130
5131 /* PARM is a template parameter pack. Return any parameter packs used in
5132 its type or the type of any of its template parameters. If there are
5133 any such packs, it will be instantiated into a fixed template parameter
5134 list by partial instantiation rather than be fully deduced. */
5135
5136 tree
5137 fixed_parameter_pack_p (tree parm)
5138 {
5139 /* This can only be true in a member template. */
5140 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5141 return NULL_TREE;
5142 /* This can only be true for a parameter pack. */
5143 if (!template_parameter_pack_p (parm))
5144 return NULL_TREE;
5145 /* A type parm can't refer to another parm. */
5146 if (TREE_CODE (parm) == TYPE_DECL)
5147 return NULL_TREE;
5148
5149 tree parameter_packs = NULL_TREE;
5150 struct find_parameter_pack_data ppd;
5151 ppd.parameter_packs = &parameter_packs;
5152 ppd.visited = new hash_set<tree>;
5153 ppd.type_pack_expansion_p = false;
5154
5155 fixed_parameter_pack_p_1 (parm, &ppd);
5156
5157 delete ppd.visited;
5158 return parameter_packs;
5159 }
5160
5161 /* Check that a template declaration's use of default arguments and
5162 parameter packs is not invalid. Here, PARMS are the template
5163 parameters. IS_PRIMARY is true if DECL is the thing declared by
5164 a primary template. IS_PARTIAL is true if DECL is a partial
5165 specialization.
5166
5167 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5168 function template declaration or a friend class template
5169 declaration. In the function case, 1 indicates a declaration, 2
5170 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5171 emitted for extraneous default arguments.
5172
5173 Returns TRUE if there were no errors found, FALSE otherwise. */
5174
5175 bool
5176 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5177 bool is_partial, int is_friend_decl)
5178 {
5179 const char *msg;
5180 int last_level_to_check;
5181 tree parm_level;
5182 bool no_errors = true;
5183
5184 /* [temp.param]
5185
5186 A default template-argument shall not be specified in a
5187 function template declaration or a function template definition, nor
5188 in the template-parameter-list of the definition of a member of a
5189 class template. */
5190
5191 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5192 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5193 /* You can't have a function template declaration in a local
5194 scope, nor you can you define a member of a class template in a
5195 local scope. */
5196 return true;
5197
5198 if ((TREE_CODE (decl) == TYPE_DECL
5199 && TREE_TYPE (decl)
5200 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5201 || (TREE_CODE (decl) == FUNCTION_DECL
5202 && LAMBDA_FUNCTION_P (decl)))
5203 /* A lambda doesn't have an explicit declaration; don't complain
5204 about the parms of the enclosing class. */
5205 return true;
5206
5207 if (current_class_type
5208 && !TYPE_BEING_DEFINED (current_class_type)
5209 && DECL_LANG_SPECIFIC (decl)
5210 && DECL_DECLARES_FUNCTION_P (decl)
5211 /* If this is either a friend defined in the scope of the class
5212 or a member function. */
5213 && (DECL_FUNCTION_MEMBER_P (decl)
5214 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5215 : DECL_FRIEND_CONTEXT (decl)
5216 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5217 : false)
5218 /* And, if it was a member function, it really was defined in
5219 the scope of the class. */
5220 && (!DECL_FUNCTION_MEMBER_P (decl)
5221 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5222 /* We already checked these parameters when the template was
5223 declared, so there's no need to do it again now. This function
5224 was defined in class scope, but we're processing its body now
5225 that the class is complete. */
5226 return true;
5227
5228 /* Core issue 226 (C++0x only): the following only applies to class
5229 templates. */
5230 if (is_primary
5231 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5232 {
5233 /* [temp.param]
5234
5235 If a template-parameter has a default template-argument, all
5236 subsequent template-parameters shall have a default
5237 template-argument supplied. */
5238 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5239 {
5240 tree inner_parms = TREE_VALUE (parm_level);
5241 int ntparms = TREE_VEC_LENGTH (inner_parms);
5242 int seen_def_arg_p = 0;
5243 int i;
5244
5245 for (i = 0; i < ntparms; ++i)
5246 {
5247 tree parm = TREE_VEC_ELT (inner_parms, i);
5248
5249 if (parm == error_mark_node)
5250 continue;
5251
5252 if (TREE_PURPOSE (parm))
5253 seen_def_arg_p = 1;
5254 else if (seen_def_arg_p
5255 && !template_parameter_pack_p (TREE_VALUE (parm)))
5256 {
5257 error ("no default argument for %qD", TREE_VALUE (parm));
5258 /* For better subsequent error-recovery, we indicate that
5259 there should have been a default argument. */
5260 TREE_PURPOSE (parm) = error_mark_node;
5261 no_errors = false;
5262 }
5263 else if (!is_partial
5264 && !is_friend_decl
5265 /* Don't complain about an enclosing partial
5266 specialization. */
5267 && parm_level == parms
5268 && TREE_CODE (decl) == TYPE_DECL
5269 && i < ntparms - 1
5270 && template_parameter_pack_p (TREE_VALUE (parm))
5271 /* A fixed parameter pack will be partially
5272 instantiated into a fixed length list. */
5273 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5274 {
5275 /* A primary class template can only have one
5276 parameter pack, at the end of the template
5277 parameter list. */
5278
5279 error ("parameter pack %q+D must be at the end of the"
5280 " template parameter list", TREE_VALUE (parm));
5281
5282 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5283 = error_mark_node;
5284 no_errors = false;
5285 }
5286 }
5287 }
5288 }
5289
5290 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5291 || is_partial
5292 || !is_primary
5293 || is_friend_decl)
5294 /* For an ordinary class template, default template arguments are
5295 allowed at the innermost level, e.g.:
5296 template <class T = int>
5297 struct S {};
5298 but, in a partial specialization, they're not allowed even
5299 there, as we have in [temp.class.spec]:
5300
5301 The template parameter list of a specialization shall not
5302 contain default template argument values.
5303
5304 So, for a partial specialization, or for a function template
5305 (in C++98/C++03), we look at all of them. */
5306 ;
5307 else
5308 /* But, for a primary class template that is not a partial
5309 specialization we look at all template parameters except the
5310 innermost ones. */
5311 parms = TREE_CHAIN (parms);
5312
5313 /* Figure out what error message to issue. */
5314 if (is_friend_decl == 2)
5315 msg = G_("default template arguments may not be used in function template "
5316 "friend re-declaration");
5317 else if (is_friend_decl)
5318 msg = G_("default template arguments may not be used in template "
5319 "friend declarations");
5320 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5321 msg = G_("default template arguments may not be used in function templates "
5322 "without %<-std=c++11%> or %<-std=gnu++11%>");
5323 else if (is_partial)
5324 msg = G_("default template arguments may not be used in "
5325 "partial specializations");
5326 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5327 msg = G_("default argument for template parameter for class enclosing %qD");
5328 else
5329 /* Per [temp.param]/9, "A default template-argument shall not be
5330 specified in the template-parameter-lists of the definition of
5331 a member of a class template that appears outside of the member's
5332 class.", thus if we aren't handling a member of a class template
5333 there is no need to examine the parameters. */
5334 return true;
5335
5336 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5337 /* If we're inside a class definition, there's no need to
5338 examine the parameters to the class itself. On the one
5339 hand, they will be checked when the class is defined, and,
5340 on the other, default arguments are valid in things like:
5341 template <class T = double>
5342 struct S { template <class U> void f(U); };
5343 Here the default argument for `S' has no bearing on the
5344 declaration of `f'. */
5345 last_level_to_check = template_class_depth (current_class_type) + 1;
5346 else
5347 /* Check everything. */
5348 last_level_to_check = 0;
5349
5350 for (parm_level = parms;
5351 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5352 parm_level = TREE_CHAIN (parm_level))
5353 {
5354 tree inner_parms = TREE_VALUE (parm_level);
5355 int i;
5356 int ntparms;
5357
5358 ntparms = TREE_VEC_LENGTH (inner_parms);
5359 for (i = 0; i < ntparms; ++i)
5360 {
5361 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5362 continue;
5363
5364 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5365 {
5366 if (msg)
5367 {
5368 no_errors = false;
5369 if (is_friend_decl == 2)
5370 return no_errors;
5371
5372 error (msg, decl);
5373 msg = 0;
5374 }
5375
5376 /* Clear out the default argument so that we are not
5377 confused later. */
5378 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5379 }
5380 }
5381
5382 /* At this point, if we're still interested in issuing messages,
5383 they must apply to classes surrounding the object declared. */
5384 if (msg)
5385 msg = G_("default argument for template parameter for class "
5386 "enclosing %qD");
5387 }
5388
5389 return no_errors;
5390 }
5391
5392 /* Worker for push_template_decl_real, called via
5393 for_each_template_parm. DATA is really an int, indicating the
5394 level of the parameters we are interested in. If T is a template
5395 parameter of that level, return nonzero. */
5396
5397 static int
5398 template_parm_this_level_p (tree t, void* data)
5399 {
5400 int this_level = *(int *)data;
5401 int level;
5402
5403 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5404 level = TEMPLATE_PARM_LEVEL (t);
5405 else
5406 level = TEMPLATE_TYPE_LEVEL (t);
5407 return level == this_level;
5408 }
5409
5410 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5411 DATA is really an int, indicating the innermost outer level of parameters.
5412 If T is a template parameter of that level or further out, return
5413 nonzero. */
5414
5415 static int
5416 template_parm_outer_level (tree t, void *data)
5417 {
5418 int this_level = *(int *)data;
5419 int level;
5420
5421 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5422 level = TEMPLATE_PARM_LEVEL (t);
5423 else
5424 level = TEMPLATE_TYPE_LEVEL (t);
5425 return level <= this_level;
5426 }
5427
5428 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5429 parameters given by current_template_args, or reuses a
5430 previously existing one, if appropriate. Returns the DECL, or an
5431 equivalent one, if it is replaced via a call to duplicate_decls.
5432
5433 If IS_FRIEND is true, DECL is a friend declaration. */
5434
5435 tree
5436 push_template_decl_real (tree decl, bool is_friend)
5437 {
5438 tree tmpl;
5439 tree args;
5440 tree info;
5441 tree ctx;
5442 bool is_primary;
5443 bool is_partial;
5444 int new_template_p = 0;
5445 /* True if the template is a member template, in the sense of
5446 [temp.mem]. */
5447 bool member_template_p = false;
5448
5449 if (decl == error_mark_node || !current_template_parms)
5450 return error_mark_node;
5451
5452 /* See if this is a partial specialization. */
5453 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5454 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5455 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5456 || (VAR_P (decl)
5457 && DECL_LANG_SPECIFIC (decl)
5458 && DECL_TEMPLATE_SPECIALIZATION (decl)
5459 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5460
5461 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5462 is_friend = true;
5463
5464 if (is_friend)
5465 /* For a friend, we want the context of the friend, not
5466 the type of which it is a friend. */
5467 ctx = CP_DECL_CONTEXT (decl);
5468 else if (CP_DECL_CONTEXT (decl)
5469 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5470 /* In the case of a virtual function, we want the class in which
5471 it is defined. */
5472 ctx = CP_DECL_CONTEXT (decl);
5473 else
5474 /* Otherwise, if we're currently defining some class, the DECL
5475 is assumed to be a member of the class. */
5476 ctx = current_scope ();
5477
5478 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5479 ctx = NULL_TREE;
5480
5481 if (!DECL_CONTEXT (decl))
5482 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5483
5484 /* See if this is a primary template. */
5485 if (is_friend && ctx
5486 && uses_template_parms_level (ctx, processing_template_decl))
5487 /* A friend template that specifies a class context, i.e.
5488 template <typename T> friend void A<T>::f();
5489 is not primary. */
5490 is_primary = false;
5491 else if (TREE_CODE (decl) == TYPE_DECL
5492 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5493 is_primary = false;
5494 else
5495 is_primary = template_parm_scope_p ();
5496
5497 if (is_primary)
5498 {
5499 warning (OPT_Wtemplates, "template %qD declared", decl);
5500
5501 if (DECL_CLASS_SCOPE_P (decl))
5502 member_template_p = true;
5503 if (TREE_CODE (decl) == TYPE_DECL
5504 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5505 {
5506 error ("template class without a name");
5507 return error_mark_node;
5508 }
5509 else if (TREE_CODE (decl) == FUNCTION_DECL)
5510 {
5511 if (member_template_p)
5512 {
5513 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5514 error ("member template %qD may not have virt-specifiers", decl);
5515 }
5516 if (DECL_DESTRUCTOR_P (decl))
5517 {
5518 /* [temp.mem]
5519
5520 A destructor shall not be a member template. */
5521 error ("destructor %qD declared as member template", decl);
5522 return error_mark_node;
5523 }
5524 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5525 && (!prototype_p (TREE_TYPE (decl))
5526 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5527 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5528 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5529 == void_list_node)))
5530 {
5531 /* [basic.stc.dynamic.allocation]
5532
5533 An allocation function can be a function
5534 template. ... Template allocation functions shall
5535 have two or more parameters. */
5536 error ("invalid template declaration of %qD", decl);
5537 return error_mark_node;
5538 }
5539 }
5540 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5541 && CLASS_TYPE_P (TREE_TYPE (decl)))
5542 {
5543 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5544 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5545 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5546 {
5547 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5548 if (TREE_CODE (t) == TYPE_DECL)
5549 t = TREE_TYPE (t);
5550 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5551 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5552 }
5553 }
5554 else if (TREE_CODE (decl) == TYPE_DECL
5555 && TYPE_DECL_ALIAS_P (decl))
5556 /* alias-declaration */
5557 gcc_assert (!DECL_ARTIFICIAL (decl));
5558 else if (VAR_P (decl))
5559 /* C++14 variable template. */;
5560 else
5561 {
5562 error ("template declaration of %q#D", decl);
5563 return error_mark_node;
5564 }
5565 }
5566
5567 /* Check to see that the rules regarding the use of default
5568 arguments are not being violated. We check args for a friend
5569 functions when we know whether it's a definition, introducing
5570 declaration or re-declaration. */
5571 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5572 check_default_tmpl_args (decl, current_template_parms,
5573 is_primary, is_partial, is_friend);
5574
5575 /* Ensure that there are no parameter packs in the type of this
5576 declaration that have not been expanded. */
5577 if (TREE_CODE (decl) == FUNCTION_DECL)
5578 {
5579 /* Check each of the arguments individually to see if there are
5580 any bare parameter packs. */
5581 tree type = TREE_TYPE (decl);
5582 tree arg = DECL_ARGUMENTS (decl);
5583 tree argtype = TYPE_ARG_TYPES (type);
5584
5585 while (arg && argtype)
5586 {
5587 if (!DECL_PACK_P (arg)
5588 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5589 {
5590 /* This is a PARM_DECL that contains unexpanded parameter
5591 packs. We have already complained about this in the
5592 check_for_bare_parameter_packs call, so just replace
5593 these types with ERROR_MARK_NODE. */
5594 TREE_TYPE (arg) = error_mark_node;
5595 TREE_VALUE (argtype) = error_mark_node;
5596 }
5597
5598 arg = DECL_CHAIN (arg);
5599 argtype = TREE_CHAIN (argtype);
5600 }
5601
5602 /* Check for bare parameter packs in the return type and the
5603 exception specifiers. */
5604 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5605 /* Errors were already issued, set return type to int
5606 as the frontend doesn't expect error_mark_node as
5607 the return type. */
5608 TREE_TYPE (type) = integer_type_node;
5609 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5610 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5611 }
5612 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5613 && TYPE_DECL_ALIAS_P (decl))
5614 ? DECL_ORIGINAL_TYPE (decl)
5615 : TREE_TYPE (decl)))
5616 {
5617 TREE_TYPE (decl) = error_mark_node;
5618 return error_mark_node;
5619 }
5620
5621 if (is_partial)
5622 return process_partial_specialization (decl);
5623
5624 args = current_template_args ();
5625
5626 if (!ctx
5627 || TREE_CODE (ctx) == FUNCTION_DECL
5628 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5629 || (TREE_CODE (decl) == TYPE_DECL
5630 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5631 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5632 {
5633 if (DECL_LANG_SPECIFIC (decl)
5634 && DECL_TEMPLATE_INFO (decl)
5635 && DECL_TI_TEMPLATE (decl))
5636 tmpl = DECL_TI_TEMPLATE (decl);
5637 /* If DECL is a TYPE_DECL for a class-template, then there won't
5638 be DECL_LANG_SPECIFIC. The information equivalent to
5639 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5640 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5641 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5642 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5643 {
5644 /* Since a template declaration already existed for this
5645 class-type, we must be redeclaring it here. Make sure
5646 that the redeclaration is valid. */
5647 redeclare_class_template (TREE_TYPE (decl),
5648 current_template_parms,
5649 current_template_constraints ());
5650 /* We don't need to create a new TEMPLATE_DECL; just use the
5651 one we already had. */
5652 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5653 }
5654 else
5655 {
5656 tmpl = build_template_decl (decl, current_template_parms,
5657 member_template_p);
5658 new_template_p = 1;
5659
5660 if (DECL_LANG_SPECIFIC (decl)
5661 && DECL_TEMPLATE_SPECIALIZATION (decl))
5662 {
5663 /* A specialization of a member template of a template
5664 class. */
5665 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5666 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5667 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5668 }
5669 }
5670 }
5671 else
5672 {
5673 tree a, t, current, parms;
5674 int i;
5675 tree tinfo = get_template_info (decl);
5676
5677 if (!tinfo)
5678 {
5679 error ("template definition of non-template %q#D", decl);
5680 return error_mark_node;
5681 }
5682
5683 tmpl = TI_TEMPLATE (tinfo);
5684
5685 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5686 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5687 && DECL_TEMPLATE_SPECIALIZATION (decl)
5688 && DECL_MEMBER_TEMPLATE_P (tmpl))
5689 {
5690 tree new_tmpl;
5691
5692 /* The declaration is a specialization of a member
5693 template, declared outside the class. Therefore, the
5694 innermost template arguments will be NULL, so we
5695 replace them with the arguments determined by the
5696 earlier call to check_explicit_specialization. */
5697 args = DECL_TI_ARGS (decl);
5698
5699 new_tmpl
5700 = build_template_decl (decl, current_template_parms,
5701 member_template_p);
5702 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5703 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5704 DECL_TI_TEMPLATE (decl) = new_tmpl;
5705 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5706 DECL_TEMPLATE_INFO (new_tmpl)
5707 = build_template_info (tmpl, args);
5708
5709 register_specialization (new_tmpl,
5710 most_general_template (tmpl),
5711 args,
5712 is_friend, 0);
5713 return decl;
5714 }
5715
5716 /* Make sure the template headers we got make sense. */
5717
5718 parms = DECL_TEMPLATE_PARMS (tmpl);
5719 i = TMPL_PARMS_DEPTH (parms);
5720 if (TMPL_ARGS_DEPTH (args) != i)
5721 {
5722 error ("expected %d levels of template parms for %q#D, got %d",
5723 i, decl, TMPL_ARGS_DEPTH (args));
5724 DECL_INTERFACE_KNOWN (decl) = 1;
5725 return error_mark_node;
5726 }
5727 else
5728 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5729 {
5730 a = TMPL_ARGS_LEVEL (args, i);
5731 t = INNERMOST_TEMPLATE_PARMS (parms);
5732
5733 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5734 {
5735 if (current == decl)
5736 error ("got %d template parameters for %q#D",
5737 TREE_VEC_LENGTH (a), decl);
5738 else
5739 error ("got %d template parameters for %q#T",
5740 TREE_VEC_LENGTH (a), current);
5741 error (" but %d required", TREE_VEC_LENGTH (t));
5742 /* Avoid crash in import_export_decl. */
5743 DECL_INTERFACE_KNOWN (decl) = 1;
5744 return error_mark_node;
5745 }
5746
5747 if (current == decl)
5748 current = ctx;
5749 else if (current == NULL_TREE)
5750 /* Can happen in erroneous input. */
5751 break;
5752 else
5753 current = get_containing_scope (current);
5754 }
5755
5756 /* Check that the parms are used in the appropriate qualifying scopes
5757 in the declarator. */
5758 if (!comp_template_args
5759 (TI_ARGS (tinfo),
5760 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5761 {
5762 error ("template arguments to %qD do not match original "
5763 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5764 if (!uses_template_parms (TI_ARGS (tinfo)))
5765 inform (input_location, "use %<template<>%> for"
5766 " an explicit specialization");
5767 /* Avoid crash in import_export_decl. */
5768 DECL_INTERFACE_KNOWN (decl) = 1;
5769 return error_mark_node;
5770 }
5771 }
5772
5773 DECL_TEMPLATE_RESULT (tmpl) = decl;
5774 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5775
5776 /* Push template declarations for global functions and types. Note
5777 that we do not try to push a global template friend declared in a
5778 template class; such a thing may well depend on the template
5779 parameters of the class. */
5780 if (new_template_p && !ctx
5781 && !(is_friend && template_class_depth (current_class_type) > 0))
5782 {
5783 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5784 if (tmpl == error_mark_node)
5785 return error_mark_node;
5786
5787 /* Hide template friend classes that haven't been declared yet. */
5788 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5789 {
5790 DECL_ANTICIPATED (tmpl) = 1;
5791 DECL_FRIEND_P (tmpl) = 1;
5792 }
5793 }
5794
5795 if (is_primary)
5796 {
5797 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5798
5799 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5800
5801 /* Give template template parms a DECL_CONTEXT of the template
5802 for which they are a parameter. */
5803 parms = INNERMOST_TEMPLATE_PARMS (parms);
5804 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5805 {
5806 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5807 if (TREE_CODE (parm) == TEMPLATE_DECL)
5808 DECL_CONTEXT (parm) = tmpl;
5809 }
5810
5811 if (TREE_CODE (decl) == TYPE_DECL
5812 && TYPE_DECL_ALIAS_P (decl)
5813 && complex_alias_template_p (tmpl))
5814 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5815 }
5816
5817 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5818 back to its most general template. If TMPL is a specialization,
5819 ARGS may only have the innermost set of arguments. Add the missing
5820 argument levels if necessary. */
5821 if (DECL_TEMPLATE_INFO (tmpl))
5822 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5823
5824 info = build_template_info (tmpl, args);
5825
5826 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5827 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5828 else
5829 {
5830 if (is_primary)
5831 retrofit_lang_decl (decl);
5832 if (DECL_LANG_SPECIFIC (decl))
5833 DECL_TEMPLATE_INFO (decl) = info;
5834 }
5835
5836 if (flag_implicit_templates
5837 && !is_friend
5838 && TREE_PUBLIC (decl)
5839 && VAR_OR_FUNCTION_DECL_P (decl))
5840 /* Set DECL_COMDAT on template instantiations; if we force
5841 them to be emitted by explicit instantiation or -frepo,
5842 mark_needed will tell cgraph to do the right thing. */
5843 DECL_COMDAT (decl) = true;
5844
5845 return DECL_TEMPLATE_RESULT (tmpl);
5846 }
5847
5848 tree
5849 push_template_decl (tree decl)
5850 {
5851 return push_template_decl_real (decl, false);
5852 }
5853
5854 /* FN is an inheriting constructor that inherits from the constructor
5855 template INHERITED; turn FN into a constructor template with a matching
5856 template header. */
5857
5858 tree
5859 add_inherited_template_parms (tree fn, tree inherited)
5860 {
5861 tree inner_parms
5862 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5863 inner_parms = copy_node (inner_parms);
5864 tree parms
5865 = tree_cons (size_int (processing_template_decl + 1),
5866 inner_parms, current_template_parms);
5867 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5868 tree args = template_parms_to_args (parms);
5869 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5870 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5871 DECL_TEMPLATE_RESULT (tmpl) = fn;
5872 DECL_ARTIFICIAL (tmpl) = true;
5873 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5874 return tmpl;
5875 }
5876
5877 /* Called when a class template TYPE is redeclared with the indicated
5878 template PARMS, e.g.:
5879
5880 template <class T> struct S;
5881 template <class T> struct S {}; */
5882
5883 bool
5884 redeclare_class_template (tree type, tree parms, tree cons)
5885 {
5886 tree tmpl;
5887 tree tmpl_parms;
5888 int i;
5889
5890 if (!TYPE_TEMPLATE_INFO (type))
5891 {
5892 error ("%qT is not a template type", type);
5893 return false;
5894 }
5895
5896 tmpl = TYPE_TI_TEMPLATE (type);
5897 if (!PRIMARY_TEMPLATE_P (tmpl))
5898 /* The type is nested in some template class. Nothing to worry
5899 about here; there are no new template parameters for the nested
5900 type. */
5901 return true;
5902
5903 if (!parms)
5904 {
5905 error ("template specifiers not specified in declaration of %qD",
5906 tmpl);
5907 return false;
5908 }
5909
5910 parms = INNERMOST_TEMPLATE_PARMS (parms);
5911 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5912
5913 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5914 {
5915 error_n (input_location, TREE_VEC_LENGTH (parms),
5916 "redeclared with %d template parameter",
5917 "redeclared with %d template parameters",
5918 TREE_VEC_LENGTH (parms));
5919 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5920 "previous declaration %qD used %d template parameter",
5921 "previous declaration %qD used %d template parameters",
5922 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5923 return false;
5924 }
5925
5926 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5927 {
5928 tree tmpl_parm;
5929 tree parm;
5930 tree tmpl_default;
5931 tree parm_default;
5932
5933 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5934 || TREE_VEC_ELT (parms, i) == error_mark_node)
5935 continue;
5936
5937 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5938 if (error_operand_p (tmpl_parm))
5939 return false;
5940
5941 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5942 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5943 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5944
5945 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5946 TEMPLATE_DECL. */
5947 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5948 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5949 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5950 || (TREE_CODE (tmpl_parm) != PARM_DECL
5951 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5952 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5953 || (TREE_CODE (tmpl_parm) == PARM_DECL
5954 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5955 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5956 {
5957 error ("template parameter %q+#D", tmpl_parm);
5958 error ("redeclared here as %q#D", parm);
5959 return false;
5960 }
5961
5962 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5963 {
5964 /* We have in [temp.param]:
5965
5966 A template-parameter may not be given default arguments
5967 by two different declarations in the same scope. */
5968 error_at (input_location, "redefinition of default argument for %q#D", parm);
5969 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5970 "original definition appeared here");
5971 return false;
5972 }
5973
5974 if (parm_default != NULL_TREE)
5975 /* Update the previous template parameters (which are the ones
5976 that will really count) with the new default value. */
5977 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5978 else if (tmpl_default != NULL_TREE)
5979 /* Update the new parameters, too; they'll be used as the
5980 parameters for any members. */
5981 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5982
5983 /* Give each template template parm in this redeclaration a
5984 DECL_CONTEXT of the template for which they are a parameter. */
5985 if (TREE_CODE (parm) == TEMPLATE_DECL)
5986 {
5987 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5988 DECL_CONTEXT (parm) = tmpl;
5989 }
5990
5991 if (TREE_CODE (parm) == TYPE_DECL)
5992 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5993 }
5994
5995 // Cannot redeclare a class template with a different set of constraints.
5996 if (!equivalent_constraints (get_constraints (tmpl), cons))
5997 {
5998 error_at (input_location, "redeclaration %q#D with different "
5999 "constraints", tmpl);
6000 inform (DECL_SOURCE_LOCATION (tmpl),
6001 "original declaration appeared here");
6002 }
6003
6004 return true;
6005 }
6006
6007 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6008 to be used when the caller has already checked
6009 (processing_template_decl
6010 && !instantiation_dependent_expression_p (expr)
6011 && potential_constant_expression (expr))
6012 and cleared processing_template_decl. */
6013
6014 tree
6015 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6016 {
6017 return tsubst_copy_and_build (expr,
6018 /*args=*/NULL_TREE,
6019 complain,
6020 /*in_decl=*/NULL_TREE,
6021 /*function_p=*/false,
6022 /*integral_constant_expression_p=*/true);
6023 }
6024
6025 /* Simplify EXPR if it is a non-dependent expression. Returns the
6026 (possibly simplified) expression. */
6027
6028 tree
6029 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6030 {
6031 if (expr == NULL_TREE)
6032 return NULL_TREE;
6033
6034 /* If we're in a template, but EXPR isn't value dependent, simplify
6035 it. We're supposed to treat:
6036
6037 template <typename T> void f(T[1 + 1]);
6038 template <typename T> void f(T[2]);
6039
6040 as two declarations of the same function, for example. */
6041 if (processing_template_decl
6042 && is_nondependent_constant_expression (expr))
6043 {
6044 processing_template_decl_sentinel s;
6045 expr = instantiate_non_dependent_expr_internal (expr, complain);
6046 }
6047 return expr;
6048 }
6049
6050 tree
6051 instantiate_non_dependent_expr (tree expr)
6052 {
6053 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6054 }
6055
6056 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6057 an uninstantiated expression. */
6058
6059 tree
6060 instantiate_non_dependent_or_null (tree expr)
6061 {
6062 if (expr == NULL_TREE)
6063 return NULL_TREE;
6064 if (processing_template_decl)
6065 {
6066 if (!is_nondependent_constant_expression (expr))
6067 expr = NULL_TREE;
6068 else
6069 {
6070 processing_template_decl_sentinel s;
6071 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6072 }
6073 }
6074 return expr;
6075 }
6076
6077 /* True iff T is a specialization of a variable template. */
6078
6079 bool
6080 variable_template_specialization_p (tree t)
6081 {
6082 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6083 return false;
6084 tree tmpl = DECL_TI_TEMPLATE (t);
6085 return variable_template_p (tmpl);
6086 }
6087
6088 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6089 template declaration, or a TYPE_DECL for an alias declaration. */
6090
6091 bool
6092 alias_type_or_template_p (tree t)
6093 {
6094 if (t == NULL_TREE)
6095 return false;
6096 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6097 || (TYPE_P (t)
6098 && TYPE_NAME (t)
6099 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6100 || DECL_ALIAS_TEMPLATE_P (t));
6101 }
6102
6103 /* Return TRUE iff T is a specialization of an alias template. */
6104
6105 bool
6106 alias_template_specialization_p (const_tree t)
6107 {
6108 /* It's an alias template specialization if it's an alias and its
6109 TYPE_NAME is a specialization of a primary template. */
6110 if (TYPE_ALIAS_P (t))
6111 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6112 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6113
6114 return false;
6115 }
6116
6117 /* An alias template is complex from a SFINAE perspective if a template-id
6118 using that alias can be ill-formed when the expansion is not, as with
6119 the void_t template. We determine this by checking whether the
6120 expansion for the alias template uses all its template parameters. */
6121
6122 struct uses_all_template_parms_data
6123 {
6124 int level;
6125 bool *seen;
6126 };
6127
6128 static int
6129 uses_all_template_parms_r (tree t, void *data_)
6130 {
6131 struct uses_all_template_parms_data &data
6132 = *(struct uses_all_template_parms_data*)data_;
6133 tree idx = get_template_parm_index (t);
6134
6135 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6136 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6137 return 0;
6138 }
6139
6140 static bool
6141 complex_alias_template_p (const_tree tmpl)
6142 {
6143 struct uses_all_template_parms_data data;
6144 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6145 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6146 data.level = TMPL_PARMS_DEPTH (parms);
6147 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6148 data.seen = XALLOCAVEC (bool, len);
6149 for (int i = 0; i < len; ++i)
6150 data.seen[i] = false;
6151
6152 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6153 for (int i = 0; i < len; ++i)
6154 if (!data.seen[i])
6155 return true;
6156 return false;
6157 }
6158
6159 /* Return TRUE iff T is a specialization of a complex alias template with
6160 dependent template-arguments. */
6161
6162 bool
6163 dependent_alias_template_spec_p (const_tree t)
6164 {
6165 if (!alias_template_specialization_p (t))
6166 return false;
6167
6168 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6169 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6170 return false;
6171
6172 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6173 if (!any_dependent_template_arguments_p (args))
6174 return false;
6175
6176 return true;
6177 }
6178
6179 /* Return the number of innermost template parameters in TMPL. */
6180
6181 static int
6182 num_innermost_template_parms (tree tmpl)
6183 {
6184 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6185 return TREE_VEC_LENGTH (parms);
6186 }
6187
6188 /* Return either TMPL or another template that it is equivalent to under DR
6189 1286: An alias that just changes the name of a template is equivalent to
6190 the other template. */
6191
6192 static tree
6193 get_underlying_template (tree tmpl)
6194 {
6195 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6196 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6197 {
6198 /* Determine if the alias is equivalent to an underlying template. */
6199 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6200 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6201 if (!tinfo)
6202 break;
6203
6204 tree underlying = TI_TEMPLATE (tinfo);
6205 if (!PRIMARY_TEMPLATE_P (underlying)
6206 || (num_innermost_template_parms (tmpl)
6207 != num_innermost_template_parms (underlying)))
6208 break;
6209
6210 tree alias_args = INNERMOST_TEMPLATE_ARGS
6211 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6212 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6213 break;
6214
6215 /* Alias is equivalent. Strip it and repeat. */
6216 tmpl = underlying;
6217 }
6218
6219 return tmpl;
6220 }
6221
6222 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6223 must be a reference-to-function or a pointer-to-function type, as specified
6224 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6225 and check that the resulting function has external linkage. */
6226
6227 static tree
6228 convert_nontype_argument_function (tree type, tree expr,
6229 tsubst_flags_t complain)
6230 {
6231 tree fns = expr;
6232 tree fn, fn_no_ptr;
6233 linkage_kind linkage;
6234
6235 fn = instantiate_type (type, fns, tf_none);
6236 if (fn == error_mark_node)
6237 return error_mark_node;
6238
6239 if (value_dependent_expression_p (fn))
6240 goto accept;
6241
6242 fn_no_ptr = strip_fnptr_conv (fn);
6243 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6244 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6245 if (BASELINK_P (fn_no_ptr))
6246 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6247
6248 /* [temp.arg.nontype]/1
6249
6250 A template-argument for a non-type, non-template template-parameter
6251 shall be one of:
6252 [...]
6253 -- the address of an object or function with external [C++11: or
6254 internal] linkage. */
6255
6256 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6257 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6258 {
6259 if (complain & tf_error)
6260 {
6261 error ("%qE is not a valid template argument for type %qT",
6262 expr, type);
6263 if (TYPE_PTR_P (type))
6264 inform (input_location, "it must be the address of a function "
6265 "with external linkage");
6266 else
6267 inform (input_location, "it must be the name of a function with "
6268 "external linkage");
6269 }
6270 return NULL_TREE;
6271 }
6272
6273 linkage = decl_linkage (fn_no_ptr);
6274 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6275 {
6276 if (complain & tf_error)
6277 {
6278 if (cxx_dialect >= cxx11)
6279 error ("%qE is not a valid template argument for type %qT "
6280 "because %qD has no linkage",
6281 expr, type, fn_no_ptr);
6282 else
6283 error ("%qE is not a valid template argument for type %qT "
6284 "because %qD does not have external linkage",
6285 expr, type, fn_no_ptr);
6286 }
6287 return NULL_TREE;
6288 }
6289
6290 accept:
6291 if (TYPE_REF_P (type))
6292 {
6293 if (REFERENCE_REF_P (fn))
6294 fn = TREE_OPERAND (fn, 0);
6295 else
6296 fn = build_address (fn);
6297 }
6298 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6299 fn = build_nop (type, fn);
6300
6301 return fn;
6302 }
6303
6304 /* Subroutine of convert_nontype_argument.
6305 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6306 Emit an error otherwise. */
6307
6308 static bool
6309 check_valid_ptrmem_cst_expr (tree type, tree expr,
6310 tsubst_flags_t complain)
6311 {
6312 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6313 tree orig_expr = expr;
6314 STRIP_NOPS (expr);
6315 if (null_ptr_cst_p (expr))
6316 return true;
6317 if (TREE_CODE (expr) == PTRMEM_CST
6318 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6319 PTRMEM_CST_CLASS (expr)))
6320 return true;
6321 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6322 return true;
6323 if (processing_template_decl
6324 && TREE_CODE (expr) == ADDR_EXPR
6325 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6326 return true;
6327 if (complain & tf_error)
6328 {
6329 error_at (loc, "%qE is not a valid template argument for type %qT",
6330 orig_expr, type);
6331 if (TREE_CODE (expr) != PTRMEM_CST)
6332 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6333 else
6334 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6335 }
6336 return false;
6337 }
6338
6339 /* Returns TRUE iff the address of OP is value-dependent.
6340
6341 14.6.2.4 [temp.dep.temp]:
6342 A non-integral non-type template-argument is dependent if its type is
6343 dependent or it has either of the following forms
6344 qualified-id
6345 & qualified-id
6346 and contains a nested-name-specifier which specifies a class-name that
6347 names a dependent type.
6348
6349 We generalize this to just say that the address of a member of a
6350 dependent class is value-dependent; the above doesn't cover the
6351 address of a static data member named with an unqualified-id. */
6352
6353 static bool
6354 has_value_dependent_address (tree op)
6355 {
6356 /* We could use get_inner_reference here, but there's no need;
6357 this is only relevant for template non-type arguments, which
6358 can only be expressed as &id-expression. */
6359 if (DECL_P (op))
6360 {
6361 tree ctx = CP_DECL_CONTEXT (op);
6362 if (TYPE_P (ctx) && dependent_type_p (ctx))
6363 return true;
6364 }
6365
6366 return false;
6367 }
6368
6369 /* The next set of functions are used for providing helpful explanatory
6370 diagnostics for failed overload resolution. Their messages should be
6371 indented by two spaces for consistency with the messages in
6372 call.c */
6373
6374 static int
6375 unify_success (bool /*explain_p*/)
6376 {
6377 return 0;
6378 }
6379
6380 /* Other failure functions should call this one, to provide a single function
6381 for setting a breakpoint on. */
6382
6383 static int
6384 unify_invalid (bool /*explain_p*/)
6385 {
6386 return 1;
6387 }
6388
6389 static int
6390 unify_parameter_deduction_failure (bool explain_p, tree parm)
6391 {
6392 if (explain_p)
6393 inform (input_location,
6394 " couldn%'t deduce template parameter %qD", parm);
6395 return unify_invalid (explain_p);
6396 }
6397
6398 static int
6399 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6400 {
6401 if (explain_p)
6402 inform (input_location,
6403 " types %qT and %qT have incompatible cv-qualifiers",
6404 parm, arg);
6405 return unify_invalid (explain_p);
6406 }
6407
6408 static int
6409 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6410 {
6411 if (explain_p)
6412 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6413 return unify_invalid (explain_p);
6414 }
6415
6416 static int
6417 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6418 {
6419 if (explain_p)
6420 inform (input_location,
6421 " template parameter %qD is not a parameter pack, but "
6422 "argument %qD is",
6423 parm, arg);
6424 return unify_invalid (explain_p);
6425 }
6426
6427 static int
6428 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6429 {
6430 if (explain_p)
6431 inform (input_location,
6432 " template argument %qE does not match "
6433 "pointer-to-member constant %qE",
6434 arg, parm);
6435 return unify_invalid (explain_p);
6436 }
6437
6438 static int
6439 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6440 {
6441 if (explain_p)
6442 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6443 return unify_invalid (explain_p);
6444 }
6445
6446 static int
6447 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6448 {
6449 if (explain_p)
6450 inform (input_location,
6451 " inconsistent parameter pack deduction with %qT and %qT",
6452 old_arg, new_arg);
6453 return unify_invalid (explain_p);
6454 }
6455
6456 static int
6457 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6458 {
6459 if (explain_p)
6460 {
6461 if (TYPE_P (parm))
6462 inform (input_location,
6463 " deduced conflicting types for parameter %qT (%qT and %qT)",
6464 parm, first, second);
6465 else
6466 inform (input_location,
6467 " deduced conflicting values for non-type parameter "
6468 "%qE (%qE and %qE)", parm, first, second);
6469 }
6470 return unify_invalid (explain_p);
6471 }
6472
6473 static int
6474 unify_vla_arg (bool explain_p, tree arg)
6475 {
6476 if (explain_p)
6477 inform (input_location,
6478 " variable-sized array type %qT is not "
6479 "a valid template argument",
6480 arg);
6481 return unify_invalid (explain_p);
6482 }
6483
6484 static int
6485 unify_method_type_error (bool explain_p, tree arg)
6486 {
6487 if (explain_p)
6488 inform (input_location,
6489 " member function type %qT is not a valid template argument",
6490 arg);
6491 return unify_invalid (explain_p);
6492 }
6493
6494 static int
6495 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6496 {
6497 if (explain_p)
6498 {
6499 if (least_p)
6500 inform_n (input_location, wanted,
6501 " candidate expects at least %d argument, %d provided",
6502 " candidate expects at least %d arguments, %d provided",
6503 wanted, have);
6504 else
6505 inform_n (input_location, wanted,
6506 " candidate expects %d argument, %d provided",
6507 " candidate expects %d arguments, %d provided",
6508 wanted, have);
6509 }
6510 return unify_invalid (explain_p);
6511 }
6512
6513 static int
6514 unify_too_many_arguments (bool explain_p, int have, int wanted)
6515 {
6516 return unify_arity (explain_p, have, wanted);
6517 }
6518
6519 static int
6520 unify_too_few_arguments (bool explain_p, int have, int wanted,
6521 bool least_p = false)
6522 {
6523 return unify_arity (explain_p, have, wanted, least_p);
6524 }
6525
6526 static int
6527 unify_arg_conversion (bool explain_p, tree to_type,
6528 tree from_type, tree arg)
6529 {
6530 if (explain_p)
6531 inform (cp_expr_loc_or_loc (arg, input_location),
6532 " cannot convert %qE (type %qT) to type %qT",
6533 arg, from_type, to_type);
6534 return unify_invalid (explain_p);
6535 }
6536
6537 static int
6538 unify_no_common_base (bool explain_p, enum template_base_result r,
6539 tree parm, tree arg)
6540 {
6541 if (explain_p)
6542 switch (r)
6543 {
6544 case tbr_ambiguous_baseclass:
6545 inform (input_location, " %qT is an ambiguous base class of %qT",
6546 parm, arg);
6547 break;
6548 default:
6549 inform (input_location, " %qT is not derived from %qT", arg, parm);
6550 break;
6551 }
6552 return unify_invalid (explain_p);
6553 }
6554
6555 static int
6556 unify_inconsistent_template_template_parameters (bool explain_p)
6557 {
6558 if (explain_p)
6559 inform (input_location,
6560 " template parameters of a template template argument are "
6561 "inconsistent with other deduced template arguments");
6562 return unify_invalid (explain_p);
6563 }
6564
6565 static int
6566 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6567 {
6568 if (explain_p)
6569 inform (input_location,
6570 " cannot deduce a template for %qT from non-template type %qT",
6571 parm, arg);
6572 return unify_invalid (explain_p);
6573 }
6574
6575 static int
6576 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6577 {
6578 if (explain_p)
6579 inform (input_location,
6580 " template argument %qE does not match %qE", arg, parm);
6581 return unify_invalid (explain_p);
6582 }
6583
6584 /* True if T is a C++20 template parameter object to store the argument for a
6585 template parameter of class type. */
6586
6587 bool
6588 template_parm_object_p (const_tree t)
6589 {
6590 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6591 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6592 }
6593
6594 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6595 argument for TYPE, points to an unsuitable object. */
6596
6597 static bool
6598 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6599 {
6600 switch (TREE_CODE (expr))
6601 {
6602 CASE_CONVERT:
6603 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6604 complain);
6605
6606 case TARGET_EXPR:
6607 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6608 complain);
6609
6610 case CONSTRUCTOR:
6611 {
6612 unsigned i; tree elt;
6613 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6614 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6615 return true;
6616 }
6617 break;
6618
6619 case ADDR_EXPR:
6620 {
6621 tree decl = TREE_OPERAND (expr, 0);
6622
6623 if (!VAR_P (decl))
6624 {
6625 if (complain & tf_error)
6626 error ("%qE is not a valid template argument of type %qT "
6627 "because %qE is not a variable", expr, type, decl);
6628 return true;
6629 }
6630 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6631 {
6632 if (complain & tf_error)
6633 error ("%qE is not a valid template argument of type %qT "
6634 "in C++98 because %qD does not have external linkage",
6635 expr, type, decl);
6636 return true;
6637 }
6638 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6639 && decl_linkage (decl) == lk_none)
6640 {
6641 if (complain & tf_error)
6642 error ("%qE is not a valid template argument of type %qT "
6643 "because %qD has no linkage", expr, type, decl);
6644 return true;
6645 }
6646 /* C++17: For a non-type template-parameter of reference or pointer
6647 type, the value of the constant expression shall not refer to (or
6648 for a pointer type, shall not be the address of):
6649 * a subobject (4.5),
6650 * a temporary object (15.2),
6651 * a string literal (5.13.5),
6652 * the result of a typeid expression (8.2.8), or
6653 * a predefined __func__ variable (11.4.1). */
6654 else if (DECL_ARTIFICIAL (decl))
6655 {
6656 if (complain & tf_error)
6657 error ("the address of %qD is not a valid template argument",
6658 decl);
6659 return true;
6660 }
6661 else if (!same_type_ignoring_top_level_qualifiers_p
6662 (strip_array_types (TREE_TYPE (type)),
6663 strip_array_types (TREE_TYPE (decl))))
6664 {
6665 if (complain & tf_error)
6666 error ("the address of the %qT subobject of %qD is not a "
6667 "valid template argument", TREE_TYPE (type), decl);
6668 return true;
6669 }
6670 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6671 {
6672 if (complain & tf_error)
6673 error ("the address of %qD is not a valid template argument "
6674 "because it does not have static storage duration",
6675 decl);
6676 return true;
6677 }
6678 }
6679 break;
6680
6681 default:
6682 if (!INDIRECT_TYPE_P (type))
6683 /* We're only concerned about pointers and references here. */;
6684 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6685 /* Null pointer values are OK in C++11. */;
6686 else
6687 {
6688 if (VAR_P (expr))
6689 {
6690 if (complain & tf_error)
6691 error ("%qD is not a valid template argument "
6692 "because %qD is a variable, not the address of "
6693 "a variable", expr, expr);
6694 return true;
6695 }
6696 else
6697 {
6698 if (complain & tf_error)
6699 error ("%qE is not a valid template argument for %qT "
6700 "because it is not the address of a variable",
6701 expr, type);
6702 return true;
6703 }
6704 }
6705 }
6706 return false;
6707
6708 }
6709
6710 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6711 template argument EXPR. */
6712
6713 static tree
6714 get_template_parm_object (tree expr, tsubst_flags_t complain)
6715 {
6716 if (TREE_CODE (expr) == TARGET_EXPR)
6717 expr = TARGET_EXPR_INITIAL (expr);
6718
6719 if (!TREE_CONSTANT (expr))
6720 {
6721 if ((complain & tf_error)
6722 && require_rvalue_constant_expression (expr))
6723 cxx_constant_value (expr);
6724 return error_mark_node;
6725 }
6726 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6727 return error_mark_node;
6728
6729 tree name = mangle_template_parm_object (expr);
6730 tree decl = get_global_binding (name);
6731 if (decl)
6732 return decl;
6733
6734 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6735 decl = create_temporary_var (type);
6736 TREE_STATIC (decl) = true;
6737 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6738 TREE_READONLY (decl) = true;
6739 DECL_NAME (decl) = name;
6740 SET_DECL_ASSEMBLER_NAME (decl, name);
6741 DECL_CONTEXT (decl) = global_namespace;
6742 comdat_linkage (decl);
6743 pushdecl_top_level_and_finish (decl, expr);
6744 return decl;
6745 }
6746
6747 /* Attempt to convert the non-type template parameter EXPR to the
6748 indicated TYPE. If the conversion is successful, return the
6749 converted value. If the conversion is unsuccessful, return
6750 NULL_TREE if we issued an error message, or error_mark_node if we
6751 did not. We issue error messages for out-and-out bad template
6752 parameters, but not simply because the conversion failed, since we
6753 might be just trying to do argument deduction. Both TYPE and EXPR
6754 must be non-dependent.
6755
6756 The conversion follows the special rules described in
6757 [temp.arg.nontype], and it is much more strict than an implicit
6758 conversion.
6759
6760 This function is called twice for each template argument (see
6761 lookup_template_class for a more accurate description of this
6762 problem). This means that we need to handle expressions which
6763 are not valid in a C++ source, but can be created from the
6764 first call (for instance, casts to perform conversions). These
6765 hacks can go away after we fix the double coercion problem. */
6766
6767 static tree
6768 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6769 {
6770 tree expr_type;
6771 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6772
6773 /* Detect immediately string literals as invalid non-type argument.
6774 This special-case is not needed for correctness (we would easily
6775 catch this later), but only to provide better diagnostic for this
6776 common user mistake. As suggested by DR 100, we do not mention
6777 linkage issues in the diagnostic as this is not the point. */
6778 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6779 {
6780 if (complain & tf_error)
6781 error ("%qE is not a valid template argument for type %qT "
6782 "because string literals can never be used in this context",
6783 expr, type);
6784 return NULL_TREE;
6785 }
6786
6787 /* Add the ADDR_EXPR now for the benefit of
6788 value_dependent_expression_p. */
6789 if (TYPE_PTROBV_P (type)
6790 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6791 {
6792 expr = decay_conversion (expr, complain);
6793 if (expr == error_mark_node)
6794 return error_mark_node;
6795 }
6796
6797 /* If we are in a template, EXPR may be non-dependent, but still
6798 have a syntactic, rather than semantic, form. For example, EXPR
6799 might be a SCOPE_REF, rather than the VAR_DECL to which the
6800 SCOPE_REF refers. Preserving the qualifying scope is necessary
6801 so that access checking can be performed when the template is
6802 instantiated -- but here we need the resolved form so that we can
6803 convert the argument. */
6804 bool non_dep = false;
6805 if (TYPE_REF_OBJ_P (type)
6806 && has_value_dependent_address (expr))
6807 /* If we want the address and it's value-dependent, don't fold. */;
6808 else if (processing_template_decl
6809 && is_nondependent_constant_expression (expr))
6810 non_dep = true;
6811 if (error_operand_p (expr))
6812 return error_mark_node;
6813 expr_type = TREE_TYPE (expr);
6814
6815 /* If the argument is non-dependent, perform any conversions in
6816 non-dependent context as well. */
6817 processing_template_decl_sentinel s (non_dep);
6818 if (non_dep)
6819 expr = instantiate_non_dependent_expr_internal (expr, complain);
6820
6821 if (value_dependent_expression_p (expr))
6822 expr = canonicalize_expr_argument (expr, complain);
6823
6824 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6825 to a non-type argument of "nullptr". */
6826 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6827 expr = fold_simple (convert (type, expr));
6828
6829 /* In C++11, integral or enumeration non-type template arguments can be
6830 arbitrary constant expressions. Pointer and pointer to
6831 member arguments can be general constant expressions that evaluate
6832 to a null value, but otherwise still need to be of a specific form. */
6833 if (cxx_dialect >= cxx11)
6834 {
6835 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6836 /* A PTRMEM_CST is already constant, and a valid template
6837 argument for a parameter of pointer to member type, we just want
6838 to leave it in that form rather than lower it to a
6839 CONSTRUCTOR. */;
6840 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6841 || cxx_dialect >= cxx17)
6842 {
6843 /* Calling build_converted_constant_expr might create a call to
6844 a conversion function with a value-dependent argument, which
6845 could invoke taking the address of a temporary representing
6846 the result of the conversion. */
6847 if (COMPOUND_LITERAL_P (expr)
6848 && CONSTRUCTOR_IS_DEPENDENT (expr)
6849 && MAYBE_CLASS_TYPE_P (expr_type)
6850 && TYPE_HAS_CONVERSION (expr_type))
6851 {
6852 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6853 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6854 return expr;
6855 }
6856 /* C++17: A template-argument for a non-type template-parameter shall
6857 be a converted constant expression (8.20) of the type of the
6858 template-parameter. */
6859 expr = build_converted_constant_expr (type, expr, complain);
6860 if (expr == error_mark_node)
6861 /* Make sure we return NULL_TREE only if we have really issued
6862 an error, as described above. */
6863 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6864 expr = maybe_constant_value (expr, NULL_TREE,
6865 /*manifestly_const_eval=*/true);
6866 expr = convert_from_reference (expr);
6867 }
6868 else if (TYPE_PTR_OR_PTRMEM_P (type))
6869 {
6870 tree folded = maybe_constant_value (expr, NULL_TREE,
6871 /*manifestly_const_eval=*/true);
6872 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6873 : null_member_pointer_value_p (folded))
6874 expr = folded;
6875 }
6876 }
6877
6878 if (TYPE_REF_P (type))
6879 expr = mark_lvalue_use (expr);
6880 else
6881 expr = mark_rvalue_use (expr);
6882
6883 /* HACK: Due to double coercion, we can get a
6884 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6885 which is the tree that we built on the first call (see
6886 below when coercing to reference to object or to reference to
6887 function). We just strip everything and get to the arg.
6888 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6889 for examples. */
6890 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6891 {
6892 tree probe_type, probe = expr;
6893 if (REFERENCE_REF_P (probe))
6894 probe = TREE_OPERAND (probe, 0);
6895 probe_type = TREE_TYPE (probe);
6896 if (TREE_CODE (probe) == NOP_EXPR)
6897 {
6898 /* ??? Maybe we could use convert_from_reference here, but we
6899 would need to relax its constraints because the NOP_EXPR
6900 could actually change the type to something more cv-qualified,
6901 and this is not folded by convert_from_reference. */
6902 tree addr = TREE_OPERAND (probe, 0);
6903 if (TYPE_REF_P (probe_type)
6904 && TREE_CODE (addr) == ADDR_EXPR
6905 && TYPE_PTR_P (TREE_TYPE (addr))
6906 && (same_type_ignoring_top_level_qualifiers_p
6907 (TREE_TYPE (probe_type),
6908 TREE_TYPE (TREE_TYPE (addr)))))
6909 {
6910 expr = TREE_OPERAND (addr, 0);
6911 expr_type = TREE_TYPE (probe_type);
6912 }
6913 }
6914 }
6915
6916 /* [temp.arg.nontype]/5, bullet 1
6917
6918 For a non-type template-parameter of integral or enumeration type,
6919 integral promotions (_conv.prom_) and integral conversions
6920 (_conv.integral_) are applied. */
6921 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6922 {
6923 if (cxx_dialect < cxx11)
6924 {
6925 tree t = build_converted_constant_expr (type, expr, complain);
6926 t = maybe_constant_value (t);
6927 if (t != error_mark_node)
6928 expr = t;
6929 }
6930
6931 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6932 return error_mark_node;
6933
6934 /* Notice that there are constant expressions like '4 % 0' which
6935 do not fold into integer constants. */
6936 if (TREE_CODE (expr) != INTEGER_CST
6937 && !value_dependent_expression_p (expr))
6938 {
6939 if (complain & tf_error)
6940 {
6941 int errs = errorcount, warns = warningcount + werrorcount;
6942 if (!require_potential_constant_expression (expr))
6943 expr = error_mark_node;
6944 else
6945 expr = cxx_constant_value (expr);
6946 if (errorcount > errs || warningcount + werrorcount > warns)
6947 inform (loc, "in template argument for type %qT", type);
6948 if (expr == error_mark_node)
6949 return NULL_TREE;
6950 /* else cxx_constant_value complained but gave us
6951 a real constant, so go ahead. */
6952 if (TREE_CODE (expr) != INTEGER_CST)
6953 {
6954 /* Some assemble time constant expressions like
6955 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6956 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6957 as we can emit them into .rodata initializers of
6958 variables, yet they can't fold into an INTEGER_CST at
6959 compile time. Refuse them here. */
6960 gcc_checking_assert (reduced_constant_expression_p (expr));
6961 error_at (loc, "template argument %qE for type %qT not "
6962 "a constant integer", expr, type);
6963 return NULL_TREE;
6964 }
6965 }
6966 else
6967 return NULL_TREE;
6968 }
6969
6970 /* Avoid typedef problems. */
6971 if (TREE_TYPE (expr) != type)
6972 expr = fold_convert (type, expr);
6973 }
6974 /* [temp.arg.nontype]/5, bullet 2
6975
6976 For a non-type template-parameter of type pointer to object,
6977 qualification conversions (_conv.qual_) and the array-to-pointer
6978 conversion (_conv.array_) are applied. */
6979 else if (TYPE_PTROBV_P (type))
6980 {
6981 tree decayed = expr;
6982
6983 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6984 decay_conversion or an explicit cast. If it's a problematic cast,
6985 we'll complain about it below. */
6986 if (TREE_CODE (expr) == NOP_EXPR)
6987 {
6988 tree probe = expr;
6989 STRIP_NOPS (probe);
6990 if (TREE_CODE (probe) == ADDR_EXPR
6991 && TYPE_PTR_P (TREE_TYPE (probe)))
6992 {
6993 expr = probe;
6994 expr_type = TREE_TYPE (expr);
6995 }
6996 }
6997
6998 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6999
7000 A template-argument for a non-type, non-template template-parameter
7001 shall be one of: [...]
7002
7003 -- the name of a non-type template-parameter;
7004 -- the address of an object or function with external linkage, [...]
7005 expressed as "& id-expression" where the & is optional if the name
7006 refers to a function or array, or if the corresponding
7007 template-parameter is a reference.
7008
7009 Here, we do not care about functions, as they are invalid anyway
7010 for a parameter of type pointer-to-object. */
7011
7012 if (value_dependent_expression_p (expr))
7013 /* Non-type template parameters are OK. */
7014 ;
7015 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7016 /* Null pointer values are OK in C++11. */;
7017 else if (TREE_CODE (expr) != ADDR_EXPR
7018 && !INDIRECT_TYPE_P (expr_type))
7019 /* Other values, like integer constants, might be valid
7020 non-type arguments of some other type. */
7021 return error_mark_node;
7022 else if (invalid_tparm_referent_p (type, expr, complain))
7023 return NULL_TREE;
7024
7025 expr = decayed;
7026
7027 expr = perform_qualification_conversions (type, expr);
7028 if (expr == error_mark_node)
7029 return error_mark_node;
7030 }
7031 /* [temp.arg.nontype]/5, bullet 3
7032
7033 For a non-type template-parameter of type reference to object, no
7034 conversions apply. The type referred to by the reference may be more
7035 cv-qualified than the (otherwise identical) type of the
7036 template-argument. The template-parameter is bound directly to the
7037 template-argument, which must be an lvalue. */
7038 else if (TYPE_REF_OBJ_P (type))
7039 {
7040 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7041 expr_type))
7042 return error_mark_node;
7043
7044 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7045 {
7046 if (complain & tf_error)
7047 error ("%qE is not a valid template argument for type %qT "
7048 "because of conflicts in cv-qualification", expr, type);
7049 return NULL_TREE;
7050 }
7051
7052 if (!lvalue_p (expr))
7053 {
7054 if (complain & tf_error)
7055 error ("%qE is not a valid template argument for type %qT "
7056 "because it is not an lvalue", expr, type);
7057 return NULL_TREE;
7058 }
7059
7060 /* [temp.arg.nontype]/1
7061
7062 A template-argument for a non-type, non-template template-parameter
7063 shall be one of: [...]
7064
7065 -- the address of an object or function with external linkage. */
7066 if (INDIRECT_REF_P (expr)
7067 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7068 {
7069 expr = TREE_OPERAND (expr, 0);
7070 if (DECL_P (expr))
7071 {
7072 if (complain & tf_error)
7073 error ("%q#D is not a valid template argument for type %qT "
7074 "because a reference variable does not have a constant "
7075 "address", expr, type);
7076 return NULL_TREE;
7077 }
7078 }
7079
7080 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7081 && value_dependent_expression_p (expr))
7082 /* OK, dependent reference. We don't want to ask whether a DECL is
7083 itself value-dependent, since what we want here is its address. */;
7084 else
7085 {
7086 expr = build_address (expr);
7087
7088 if (invalid_tparm_referent_p (type, expr, complain))
7089 return NULL_TREE;
7090 }
7091
7092 if (!same_type_p (type, TREE_TYPE (expr)))
7093 expr = build_nop (type, expr);
7094 }
7095 /* [temp.arg.nontype]/5, bullet 4
7096
7097 For a non-type template-parameter of type pointer to function, only
7098 the function-to-pointer conversion (_conv.func_) is applied. If the
7099 template-argument represents a set of overloaded functions (or a
7100 pointer to such), the matching function is selected from the set
7101 (_over.over_). */
7102 else if (TYPE_PTRFN_P (type))
7103 {
7104 /* If the argument is a template-id, we might not have enough
7105 context information to decay the pointer. */
7106 if (!type_unknown_p (expr_type))
7107 {
7108 expr = decay_conversion (expr, complain);
7109 if (expr == error_mark_node)
7110 return error_mark_node;
7111 }
7112
7113 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7114 /* Null pointer values are OK in C++11. */
7115 return perform_qualification_conversions (type, expr);
7116
7117 expr = convert_nontype_argument_function (type, expr, complain);
7118 if (!expr || expr == error_mark_node)
7119 return expr;
7120 }
7121 /* [temp.arg.nontype]/5, bullet 5
7122
7123 For a non-type template-parameter of type reference to function, no
7124 conversions apply. If the template-argument represents a set of
7125 overloaded functions, the matching function is selected from the set
7126 (_over.over_). */
7127 else if (TYPE_REFFN_P (type))
7128 {
7129 if (TREE_CODE (expr) == ADDR_EXPR)
7130 {
7131 if (complain & tf_error)
7132 {
7133 error ("%qE is not a valid template argument for type %qT "
7134 "because it is a pointer", expr, type);
7135 inform (input_location, "try using %qE instead",
7136 TREE_OPERAND (expr, 0));
7137 }
7138 return NULL_TREE;
7139 }
7140
7141 expr = convert_nontype_argument_function (type, expr, complain);
7142 if (!expr || expr == error_mark_node)
7143 return expr;
7144 }
7145 /* [temp.arg.nontype]/5, bullet 6
7146
7147 For a non-type template-parameter of type pointer to member function,
7148 no conversions apply. If the template-argument represents a set of
7149 overloaded member functions, the matching member function is selected
7150 from the set (_over.over_). */
7151 else if (TYPE_PTRMEMFUNC_P (type))
7152 {
7153 expr = instantiate_type (type, expr, tf_none);
7154 if (expr == error_mark_node)
7155 return error_mark_node;
7156
7157 /* [temp.arg.nontype] bullet 1 says the pointer to member
7158 expression must be a pointer-to-member constant. */
7159 if (!value_dependent_expression_p (expr)
7160 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7161 return NULL_TREE;
7162
7163 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7164 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7165 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7166 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7167 }
7168 /* [temp.arg.nontype]/5, bullet 7
7169
7170 For a non-type template-parameter of type pointer to data member,
7171 qualification conversions (_conv.qual_) are applied. */
7172 else if (TYPE_PTRDATAMEM_P (type))
7173 {
7174 /* [temp.arg.nontype] bullet 1 says the pointer to member
7175 expression must be a pointer-to-member constant. */
7176 if (!value_dependent_expression_p (expr)
7177 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7178 return NULL_TREE;
7179
7180 expr = perform_qualification_conversions (type, expr);
7181 if (expr == error_mark_node)
7182 return expr;
7183 }
7184 else if (NULLPTR_TYPE_P (type))
7185 {
7186 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7187 {
7188 if (complain & tf_error)
7189 error ("%qE is not a valid template argument for type %qT "
7190 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7191 return NULL_TREE;
7192 }
7193 return expr;
7194 }
7195 else if (CLASS_TYPE_P (type))
7196 {
7197 /* Replace the argument with a reference to the corresponding template
7198 parameter object. */
7199 if (!value_dependent_expression_p (expr))
7200 expr = get_template_parm_object (expr, complain);
7201 if (expr == error_mark_node)
7202 return NULL_TREE;
7203 }
7204 /* A template non-type parameter must be one of the above. */
7205 else
7206 gcc_unreachable ();
7207
7208 /* Sanity check: did we actually convert the argument to the
7209 right type? */
7210 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7211 (type, TREE_TYPE (expr)));
7212 return convert_from_reference (expr);
7213 }
7214
7215 /* Subroutine of coerce_template_template_parms, which returns 1 if
7216 PARM_PARM and ARG_PARM match using the rule for the template
7217 parameters of template template parameters. Both PARM and ARG are
7218 template parameters; the rest of the arguments are the same as for
7219 coerce_template_template_parms.
7220 */
7221 static int
7222 coerce_template_template_parm (tree parm,
7223 tree arg,
7224 tsubst_flags_t complain,
7225 tree in_decl,
7226 tree outer_args)
7227 {
7228 if (arg == NULL_TREE || error_operand_p (arg)
7229 || parm == NULL_TREE || error_operand_p (parm))
7230 return 0;
7231
7232 if (TREE_CODE (arg) != TREE_CODE (parm))
7233 return 0;
7234
7235 switch (TREE_CODE (parm))
7236 {
7237 case TEMPLATE_DECL:
7238 /* We encounter instantiations of templates like
7239 template <template <template <class> class> class TT>
7240 class C; */
7241 {
7242 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7243 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7244
7245 if (!coerce_template_template_parms
7246 (parmparm, argparm, complain, in_decl, outer_args))
7247 return 0;
7248 }
7249 /* Fall through. */
7250
7251 case TYPE_DECL:
7252 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7253 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7254 /* Argument is a parameter pack but parameter is not. */
7255 return 0;
7256 break;
7257
7258 case PARM_DECL:
7259 /* The tsubst call is used to handle cases such as
7260
7261 template <int> class C {};
7262 template <class T, template <T> class TT> class D {};
7263 D<int, C> d;
7264
7265 i.e. the parameter list of TT depends on earlier parameters. */
7266 if (!uses_template_parms (TREE_TYPE (arg)))
7267 {
7268 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7269 if (!uses_template_parms (t)
7270 && !same_type_p (t, TREE_TYPE (arg)))
7271 return 0;
7272 }
7273
7274 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7275 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7276 /* Argument is a parameter pack but parameter is not. */
7277 return 0;
7278
7279 break;
7280
7281 default:
7282 gcc_unreachable ();
7283 }
7284
7285 return 1;
7286 }
7287
7288 /* Coerce template argument list ARGLIST for use with template
7289 template-parameter TEMPL. */
7290
7291 static tree
7292 coerce_template_args_for_ttp (tree templ, tree arglist,
7293 tsubst_flags_t complain)
7294 {
7295 /* Consider an example where a template template parameter declared as
7296
7297 template <class T, class U = std::allocator<T> > class TT
7298
7299 The template parameter level of T and U are one level larger than
7300 of TT. To proper process the default argument of U, say when an
7301 instantiation `TT<int>' is seen, we need to build the full
7302 arguments containing {int} as the innermost level. Outer levels,
7303 available when not appearing as default template argument, can be
7304 obtained from the arguments of the enclosing template.
7305
7306 Suppose that TT is later substituted with std::vector. The above
7307 instantiation is `TT<int, std::allocator<T> >' with TT at
7308 level 1, and T at level 2, while the template arguments at level 1
7309 becomes {std::vector} and the inner level 2 is {int}. */
7310
7311 tree outer = DECL_CONTEXT (templ);
7312 if (outer)
7313 {
7314 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7315 /* We want arguments for the partial specialization, not arguments for
7316 the primary template. */
7317 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7318 else
7319 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7320 }
7321 else if (current_template_parms)
7322 {
7323 /* This is an argument of the current template, so we haven't set
7324 DECL_CONTEXT yet. */
7325 tree relevant_template_parms;
7326
7327 /* Parameter levels that are greater than the level of the given
7328 template template parm are irrelevant. */
7329 relevant_template_parms = current_template_parms;
7330 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7331 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7332 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7333
7334 outer = template_parms_to_args (relevant_template_parms);
7335 }
7336
7337 if (outer)
7338 arglist = add_to_template_args (outer, arglist);
7339
7340 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7341 return coerce_template_parms (parmlist, arglist, templ,
7342 complain,
7343 /*require_all_args=*/true,
7344 /*use_default_args=*/true);
7345 }
7346
7347 /* A cache of template template parameters with match-all default
7348 arguments. */
7349 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7350 static void
7351 store_defaulted_ttp (tree v, tree t)
7352 {
7353 if (!defaulted_ttp_cache)
7354 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7355 defaulted_ttp_cache->put (v, t);
7356 }
7357 static tree
7358 lookup_defaulted_ttp (tree v)
7359 {
7360 if (defaulted_ttp_cache)
7361 if (tree *p = defaulted_ttp_cache->get (v))
7362 return *p;
7363 return NULL_TREE;
7364 }
7365
7366 /* T is a bound template template-parameter. Copy its arguments into default
7367 arguments of the template template-parameter's template parameters. */
7368
7369 static tree
7370 add_defaults_to_ttp (tree otmpl)
7371 {
7372 if (tree c = lookup_defaulted_ttp (otmpl))
7373 return c;
7374
7375 tree ntmpl = copy_node (otmpl);
7376
7377 tree ntype = copy_node (TREE_TYPE (otmpl));
7378 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7379 TYPE_MAIN_VARIANT (ntype) = ntype;
7380 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7381 TYPE_NAME (ntype) = ntmpl;
7382 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7383
7384 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7385 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7386 TEMPLATE_PARM_DECL (idx) = ntmpl;
7387 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7388
7389 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7390 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7391 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7392 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7393 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7394 {
7395 tree o = TREE_VEC_ELT (vec, i);
7396 if (!template_parameter_pack_p (TREE_VALUE (o)))
7397 {
7398 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7399 TREE_PURPOSE (n) = any_targ_node;
7400 }
7401 }
7402
7403 store_defaulted_ttp (otmpl, ntmpl);
7404 return ntmpl;
7405 }
7406
7407 /* ARG is a bound potential template template-argument, and PARGS is a list
7408 of arguments for the corresponding template template-parameter. Adjust
7409 PARGS as appropriate for application to ARG's template, and if ARG is a
7410 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7411 arguments to the template template parameter. */
7412
7413 static tree
7414 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7415 {
7416 ++processing_template_decl;
7417 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7418 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7419 {
7420 /* When comparing two template template-parameters in partial ordering,
7421 rewrite the one currently being used as an argument to have default
7422 arguments for all parameters. */
7423 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7424 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7425 if (pargs != error_mark_node)
7426 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7427 TYPE_TI_ARGS (arg));
7428 }
7429 else
7430 {
7431 tree aparms
7432 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7433 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7434 /*require_all*/true,
7435 /*use_default*/true);
7436 }
7437 --processing_template_decl;
7438 return pargs;
7439 }
7440
7441 /* Subroutine of unify for the case when PARM is a
7442 BOUND_TEMPLATE_TEMPLATE_PARM. */
7443
7444 static int
7445 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7446 bool explain_p)
7447 {
7448 tree parmvec = TYPE_TI_ARGS (parm);
7449 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7450
7451 /* The template template parm might be variadic and the argument
7452 not, so flatten both argument lists. */
7453 parmvec = expand_template_argument_pack (parmvec);
7454 argvec = expand_template_argument_pack (argvec);
7455
7456 if (flag_new_ttp)
7457 {
7458 /* In keeping with P0522R0, adjust P's template arguments
7459 to apply to A's template; then flatten it again. */
7460 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7461 nparmvec = expand_template_argument_pack (nparmvec);
7462
7463 if (unify (tparms, targs, nparmvec, argvec,
7464 UNIFY_ALLOW_NONE, explain_p))
7465 return 1;
7466
7467 /* If the P0522 adjustment eliminated a pack expansion, deduce
7468 empty packs. */
7469 if (flag_new_ttp
7470 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7471 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7472 DEDUCE_EXACT, /*sub*/true, explain_p))
7473 return 1;
7474 }
7475 else
7476 {
7477 /* Deduce arguments T, i from TT<T> or TT<i>.
7478 We check each element of PARMVEC and ARGVEC individually
7479 rather than the whole TREE_VEC since they can have
7480 different number of elements, which is allowed under N2555. */
7481
7482 int len = TREE_VEC_LENGTH (parmvec);
7483
7484 /* Check if the parameters end in a pack, making them
7485 variadic. */
7486 int parm_variadic_p = 0;
7487 if (len > 0
7488 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7489 parm_variadic_p = 1;
7490
7491 for (int i = 0; i < len - parm_variadic_p; ++i)
7492 /* If the template argument list of P contains a pack
7493 expansion that is not the last template argument, the
7494 entire template argument list is a non-deduced
7495 context. */
7496 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7497 return unify_success (explain_p);
7498
7499 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7500 return unify_too_few_arguments (explain_p,
7501 TREE_VEC_LENGTH (argvec), len);
7502
7503 for (int i = 0; i < len - parm_variadic_p; ++i)
7504 if (unify (tparms, targs,
7505 TREE_VEC_ELT (parmvec, i),
7506 TREE_VEC_ELT (argvec, i),
7507 UNIFY_ALLOW_NONE, explain_p))
7508 return 1;
7509
7510 if (parm_variadic_p
7511 && unify_pack_expansion (tparms, targs,
7512 parmvec, argvec,
7513 DEDUCE_EXACT,
7514 /*subr=*/true, explain_p))
7515 return 1;
7516 }
7517
7518 return 0;
7519 }
7520
7521 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7522 template template parameters. Both PARM_PARMS and ARG_PARMS are
7523 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7524 or PARM_DECL.
7525
7526 Consider the example:
7527 template <class T> class A;
7528 template<template <class U> class TT> class B;
7529
7530 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7531 the parameters to A, and OUTER_ARGS contains A. */
7532
7533 static int
7534 coerce_template_template_parms (tree parm_parms,
7535 tree arg_parms,
7536 tsubst_flags_t complain,
7537 tree in_decl,
7538 tree outer_args)
7539 {
7540 int nparms, nargs, i;
7541 tree parm, arg;
7542 int variadic_p = 0;
7543
7544 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7545 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7546
7547 nparms = TREE_VEC_LENGTH (parm_parms);
7548 nargs = TREE_VEC_LENGTH (arg_parms);
7549
7550 if (flag_new_ttp)
7551 {
7552 /* P0522R0: A template template-parameter P is at least as specialized as
7553 a template template-argument A if, given the following rewrite to two
7554 function templates, the function template corresponding to P is at
7555 least as specialized as the function template corresponding to A
7556 according to the partial ordering rules for function templates
7557 ([temp.func.order]). Given an invented class template X with the
7558 template parameter list of A (including default arguments):
7559
7560 * Each of the two function templates has the same template parameters,
7561 respectively, as P or A.
7562
7563 * Each function template has a single function parameter whose type is
7564 a specialization of X with template arguments corresponding to the
7565 template parameters from the respective function template where, for
7566 each template parameter PP in the template parameter list of the
7567 function template, a corresponding template argument AA is formed. If
7568 PP declares a parameter pack, then AA is the pack expansion
7569 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7570
7571 If the rewrite produces an invalid type, then P is not at least as
7572 specialized as A. */
7573
7574 /* So coerce P's args to apply to A's parms, and then deduce between A's
7575 args and the converted args. If that succeeds, A is at least as
7576 specialized as P, so they match.*/
7577 tree pargs = template_parms_level_to_args (parm_parms);
7578 pargs = add_outermost_template_args (outer_args, pargs);
7579 ++processing_template_decl;
7580 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7581 /*require_all*/true, /*use_default*/true);
7582 --processing_template_decl;
7583 if (pargs != error_mark_node)
7584 {
7585 tree targs = make_tree_vec (nargs);
7586 tree aargs = template_parms_level_to_args (arg_parms);
7587 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7588 /*explain*/false))
7589 return 1;
7590 }
7591 }
7592
7593 /* Determine whether we have a parameter pack at the end of the
7594 template template parameter's template parameter list. */
7595 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7596 {
7597 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7598
7599 if (error_operand_p (parm))
7600 return 0;
7601
7602 switch (TREE_CODE (parm))
7603 {
7604 case TEMPLATE_DECL:
7605 case TYPE_DECL:
7606 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7607 variadic_p = 1;
7608 break;
7609
7610 case PARM_DECL:
7611 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7612 variadic_p = 1;
7613 break;
7614
7615 default:
7616 gcc_unreachable ();
7617 }
7618 }
7619
7620 if (nargs != nparms
7621 && !(variadic_p && nargs >= nparms - 1))
7622 return 0;
7623
7624 /* Check all of the template parameters except the parameter pack at
7625 the end (if any). */
7626 for (i = 0; i < nparms - variadic_p; ++i)
7627 {
7628 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7629 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7630 continue;
7631
7632 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7633 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7634
7635 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7636 outer_args))
7637 return 0;
7638
7639 }
7640
7641 if (variadic_p)
7642 {
7643 /* Check each of the template parameters in the template
7644 argument against the template parameter pack at the end of
7645 the template template parameter. */
7646 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7647 return 0;
7648
7649 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7650
7651 for (; i < nargs; ++i)
7652 {
7653 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7654 continue;
7655
7656 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7657
7658 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7659 outer_args))
7660 return 0;
7661 }
7662 }
7663
7664 return 1;
7665 }
7666
7667 /* Verifies that the deduced template arguments (in TARGS) for the
7668 template template parameters (in TPARMS) represent valid bindings,
7669 by comparing the template parameter list of each template argument
7670 to the template parameter list of its corresponding template
7671 template parameter, in accordance with DR150. This
7672 routine can only be called after all template arguments have been
7673 deduced. It will return TRUE if all of the template template
7674 parameter bindings are okay, FALSE otherwise. */
7675 bool
7676 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7677 {
7678 int i, ntparms = TREE_VEC_LENGTH (tparms);
7679 bool ret = true;
7680
7681 /* We're dealing with template parms in this process. */
7682 ++processing_template_decl;
7683
7684 targs = INNERMOST_TEMPLATE_ARGS (targs);
7685
7686 for (i = 0; i < ntparms; ++i)
7687 {
7688 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7689 tree targ = TREE_VEC_ELT (targs, i);
7690
7691 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7692 {
7693 tree packed_args = NULL_TREE;
7694 int idx, len = 1;
7695
7696 if (ARGUMENT_PACK_P (targ))
7697 {
7698 /* Look inside the argument pack. */
7699 packed_args = ARGUMENT_PACK_ARGS (targ);
7700 len = TREE_VEC_LENGTH (packed_args);
7701 }
7702
7703 for (idx = 0; idx < len; ++idx)
7704 {
7705 tree targ_parms = NULL_TREE;
7706
7707 if (packed_args)
7708 /* Extract the next argument from the argument
7709 pack. */
7710 targ = TREE_VEC_ELT (packed_args, idx);
7711
7712 if (PACK_EXPANSION_P (targ))
7713 /* Look at the pattern of the pack expansion. */
7714 targ = PACK_EXPANSION_PATTERN (targ);
7715
7716 /* Extract the template parameters from the template
7717 argument. */
7718 if (TREE_CODE (targ) == TEMPLATE_DECL)
7719 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7720 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7721 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7722
7723 /* Verify that we can coerce the template template
7724 parameters from the template argument to the template
7725 parameter. This requires an exact match. */
7726 if (targ_parms
7727 && !coerce_template_template_parms
7728 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7729 targ_parms,
7730 tf_none,
7731 tparm,
7732 targs))
7733 {
7734 ret = false;
7735 goto out;
7736 }
7737 }
7738 }
7739 }
7740
7741 out:
7742
7743 --processing_template_decl;
7744 return ret;
7745 }
7746
7747 /* Since type attributes aren't mangled, we need to strip them from
7748 template type arguments. */
7749
7750 static tree
7751 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7752 {
7753 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7754 return arg;
7755 bool removed_attributes = false;
7756 tree canon = strip_typedefs (arg, &removed_attributes);
7757 if (removed_attributes
7758 && (complain & tf_warning))
7759 warning (OPT_Wignored_attributes,
7760 "ignoring attributes on template argument %qT", arg);
7761 return canon;
7762 }
7763
7764 /* And from inside dependent non-type arguments like sizeof(Type). */
7765
7766 static tree
7767 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7768 {
7769 if (!arg || arg == error_mark_node)
7770 return arg;
7771 bool removed_attributes = false;
7772 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7773 if (removed_attributes
7774 && (complain & tf_warning))
7775 warning (OPT_Wignored_attributes,
7776 "ignoring attributes in template argument %qE", arg);
7777 return canon;
7778 }
7779
7780 // A template declaration can be substituted for a constrained
7781 // template template parameter only when the argument is more
7782 // constrained than the parameter.
7783 static bool
7784 is_compatible_template_arg (tree parm, tree arg)
7785 {
7786 tree parm_cons = get_constraints (parm);
7787
7788 /* For now, allow constrained template template arguments
7789 and unconstrained template template parameters. */
7790 if (parm_cons == NULL_TREE)
7791 return true;
7792
7793 tree arg_cons = get_constraints (arg);
7794
7795 // If the template parameter is constrained, we need to rewrite its
7796 // constraints in terms of the ARG's template parameters. This ensures
7797 // that all of the template parameter types will have the same depth.
7798 //
7799 // Note that this is only valid when coerce_template_template_parm is
7800 // true for the innermost template parameters of PARM and ARG. In other
7801 // words, because coercion is successful, this conversion will be valid.
7802 if (parm_cons)
7803 {
7804 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7805 parm_cons = tsubst_constraint_info (parm_cons,
7806 INNERMOST_TEMPLATE_ARGS (args),
7807 tf_none, NULL_TREE);
7808 if (parm_cons == error_mark_node)
7809 return false;
7810 }
7811
7812 return subsumes (parm_cons, arg_cons);
7813 }
7814
7815 // Convert a placeholder argument into a binding to the original
7816 // parameter. The original parameter is saved as the TREE_TYPE of
7817 // ARG.
7818 static inline tree
7819 convert_wildcard_argument (tree parm, tree arg)
7820 {
7821 TREE_TYPE (arg) = parm;
7822 return arg;
7823 }
7824
7825 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7826 because one of them is dependent. But we need to represent the
7827 conversion for the benefit of cp_tree_equal. */
7828
7829 static tree
7830 maybe_convert_nontype_argument (tree type, tree arg)
7831 {
7832 /* Auto parms get no conversion. */
7833 if (type_uses_auto (type))
7834 return arg;
7835 /* We don't need or want to add this conversion now if we're going to use the
7836 argument for deduction. */
7837 if (value_dependent_expression_p (arg))
7838 return arg;
7839
7840 type = cv_unqualified (type);
7841 tree argtype = TREE_TYPE (arg);
7842 if (same_type_p (type, argtype))
7843 return arg;
7844
7845 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7846 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7847 return arg;
7848 }
7849
7850 /* Convert the indicated template ARG as necessary to match the
7851 indicated template PARM. Returns the converted ARG, or
7852 error_mark_node if the conversion was unsuccessful. Error and
7853 warning messages are issued under control of COMPLAIN. This
7854 conversion is for the Ith parameter in the parameter list. ARGS is
7855 the full set of template arguments deduced so far. */
7856
7857 static tree
7858 convert_template_argument (tree parm,
7859 tree arg,
7860 tree args,
7861 tsubst_flags_t complain,
7862 int i,
7863 tree in_decl)
7864 {
7865 tree orig_arg;
7866 tree val;
7867 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7868
7869 if (parm == error_mark_node || error_operand_p (arg))
7870 return error_mark_node;
7871
7872 /* Trivially convert placeholders. */
7873 if (TREE_CODE (arg) == WILDCARD_DECL)
7874 return convert_wildcard_argument (parm, arg);
7875
7876 if (arg == any_targ_node)
7877 return arg;
7878
7879 if (TREE_CODE (arg) == TREE_LIST
7880 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7881 {
7882 /* The template argument was the name of some
7883 member function. That's usually
7884 invalid, but static members are OK. In any
7885 case, grab the underlying fields/functions
7886 and issue an error later if required. */
7887 TREE_TYPE (arg) = unknown_type_node;
7888 }
7889
7890 orig_arg = arg;
7891
7892 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7893 requires_type = (TREE_CODE (parm) == TYPE_DECL
7894 || requires_tmpl_type);
7895
7896 /* When determining whether an argument pack expansion is a template,
7897 look at the pattern. */
7898 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7899 arg = PACK_EXPANSION_PATTERN (arg);
7900
7901 /* Deal with an injected-class-name used as a template template arg. */
7902 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7903 {
7904 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7905 if (TREE_CODE (t) == TEMPLATE_DECL)
7906 {
7907 if (cxx_dialect >= cxx11)
7908 /* OK under DR 1004. */;
7909 else if (complain & tf_warning_or_error)
7910 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7911 " used as template template argument", TYPE_NAME (arg));
7912 else if (flag_pedantic_errors)
7913 t = arg;
7914
7915 arg = t;
7916 }
7917 }
7918
7919 is_tmpl_type =
7920 ((TREE_CODE (arg) == TEMPLATE_DECL
7921 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7922 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7923 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7924 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7925
7926 if (is_tmpl_type
7927 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7928 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7929 arg = TYPE_STUB_DECL (arg);
7930
7931 is_type = TYPE_P (arg) || is_tmpl_type;
7932
7933 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7934 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7935 {
7936 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7937 {
7938 if (complain & tf_error)
7939 error ("invalid use of destructor %qE as a type", orig_arg);
7940 return error_mark_node;
7941 }
7942
7943 permerror (input_location,
7944 "to refer to a type member of a template parameter, "
7945 "use %<typename %E%>", orig_arg);
7946
7947 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7948 TREE_OPERAND (arg, 1),
7949 typename_type,
7950 complain);
7951 arg = orig_arg;
7952 is_type = 1;
7953 }
7954 if (is_type != requires_type)
7955 {
7956 if (in_decl)
7957 {
7958 if (complain & tf_error)
7959 {
7960 error ("type/value mismatch at argument %d in template "
7961 "parameter list for %qD",
7962 i + 1, in_decl);
7963 if (is_type)
7964 {
7965 /* The template argument is a type, but we're expecting
7966 an expression. */
7967 inform (input_location,
7968 " expected a constant of type %qT, got %qT",
7969 TREE_TYPE (parm),
7970 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7971 /* [temp.arg]/2: "In a template-argument, an ambiguity
7972 between a type-id and an expression is resolved to a
7973 type-id, regardless of the form of the corresponding
7974 template-parameter." So give the user a clue. */
7975 if (TREE_CODE (arg) == FUNCTION_TYPE)
7976 inform (input_location, " ambiguous template argument "
7977 "for non-type template parameter is treated as "
7978 "function type");
7979 }
7980 else if (requires_tmpl_type)
7981 inform (input_location,
7982 " expected a class template, got %qE", orig_arg);
7983 else
7984 inform (input_location,
7985 " expected a type, got %qE", orig_arg);
7986 }
7987 }
7988 return error_mark_node;
7989 }
7990 if (is_tmpl_type ^ requires_tmpl_type)
7991 {
7992 if (in_decl && (complain & tf_error))
7993 {
7994 error ("type/value mismatch at argument %d in template "
7995 "parameter list for %qD",
7996 i + 1, in_decl);
7997 if (is_tmpl_type)
7998 inform (input_location,
7999 " expected a type, got %qT", DECL_NAME (arg));
8000 else
8001 inform (input_location,
8002 " expected a class template, got %qT", orig_arg);
8003 }
8004 return error_mark_node;
8005 }
8006
8007 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8008 /* We already did the appropriate conversion when packing args. */
8009 val = orig_arg;
8010 else if (is_type)
8011 {
8012 if (requires_tmpl_type)
8013 {
8014 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8015 /* The number of argument required is not known yet.
8016 Just accept it for now. */
8017 val = orig_arg;
8018 else
8019 {
8020 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8021 tree argparm;
8022
8023 /* Strip alias templates that are equivalent to another
8024 template. */
8025 arg = get_underlying_template (arg);
8026 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8027
8028 if (coerce_template_template_parms (parmparm, argparm,
8029 complain, in_decl,
8030 args))
8031 {
8032 val = arg;
8033
8034 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8035 TEMPLATE_DECL. */
8036 if (val != error_mark_node)
8037 {
8038 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8039 val = TREE_TYPE (val);
8040 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8041 val = make_pack_expansion (val, complain);
8042 }
8043 }
8044 else
8045 {
8046 if (in_decl && (complain & tf_error))
8047 {
8048 error ("type/value mismatch at argument %d in "
8049 "template parameter list for %qD",
8050 i + 1, in_decl);
8051 inform (input_location,
8052 " expected a template of type %qD, got %qT",
8053 parm, orig_arg);
8054 }
8055
8056 val = error_mark_node;
8057 }
8058
8059 // Check that the constraints are compatible before allowing the
8060 // substitution.
8061 if (val != error_mark_node)
8062 if (!is_compatible_template_arg (parm, arg))
8063 {
8064 if (in_decl && (complain & tf_error))
8065 {
8066 error ("constraint mismatch at argument %d in "
8067 "template parameter list for %qD",
8068 i + 1, in_decl);
8069 inform (input_location, " expected %qD but got %qD",
8070 parm, arg);
8071 }
8072 val = error_mark_node;
8073 }
8074 }
8075 }
8076 else
8077 val = orig_arg;
8078 /* We only form one instance of each template specialization.
8079 Therefore, if we use a non-canonical variant (i.e., a
8080 typedef), any future messages referring to the type will use
8081 the typedef, which is confusing if those future uses do not
8082 themselves also use the typedef. */
8083 if (TYPE_P (val))
8084 val = canonicalize_type_argument (val, complain);
8085 }
8086 else
8087 {
8088 tree t = TREE_TYPE (parm);
8089
8090 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8091 > TMPL_ARGS_DEPTH (args))
8092 /* We don't have enough levels of args to do any substitution. This
8093 can happen in the context of -fnew-ttp-matching. */;
8094 else if (tree a = type_uses_auto (t))
8095 {
8096 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8097 if (t == error_mark_node)
8098 return error_mark_node;
8099 }
8100 else
8101 t = tsubst (t, args, complain, in_decl);
8102
8103 if (invalid_nontype_parm_type_p (t, complain))
8104 return error_mark_node;
8105
8106 if (t != TREE_TYPE (parm))
8107 t = canonicalize_type_argument (t, complain);
8108
8109 if (!type_dependent_expression_p (orig_arg)
8110 && !uses_template_parms (t))
8111 /* We used to call digest_init here. However, digest_init
8112 will report errors, which we don't want when complain
8113 is zero. More importantly, digest_init will try too
8114 hard to convert things: for example, `0' should not be
8115 converted to pointer type at this point according to
8116 the standard. Accepting this is not merely an
8117 extension, since deciding whether or not these
8118 conversions can occur is part of determining which
8119 function template to call, or whether a given explicit
8120 argument specification is valid. */
8121 val = convert_nontype_argument (t, orig_arg, complain);
8122 else
8123 {
8124 val = canonicalize_expr_argument (orig_arg, complain);
8125 val = maybe_convert_nontype_argument (t, val);
8126 }
8127
8128
8129 if (val == NULL_TREE)
8130 val = error_mark_node;
8131 else if (val == error_mark_node && (complain & tf_error))
8132 error ("could not convert template argument %qE from %qT to %qT",
8133 orig_arg, TREE_TYPE (orig_arg), t);
8134
8135 if (INDIRECT_REF_P (val))
8136 {
8137 /* Reject template arguments that are references to built-in
8138 functions with no library fallbacks. */
8139 const_tree inner = TREE_OPERAND (val, 0);
8140 const_tree innertype = TREE_TYPE (inner);
8141 if (innertype
8142 && TYPE_REF_P (innertype)
8143 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8144 && TREE_OPERAND_LENGTH (inner) > 0
8145 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8146 return error_mark_node;
8147 }
8148
8149 if (TREE_CODE (val) == SCOPE_REF)
8150 {
8151 /* Strip typedefs from the SCOPE_REF. */
8152 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8153 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8154 complain);
8155 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8156 QUALIFIED_NAME_IS_TEMPLATE (val));
8157 }
8158 }
8159
8160 return val;
8161 }
8162
8163 /* Coerces the remaining template arguments in INNER_ARGS (from
8164 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8165 Returns the coerced argument pack. PARM_IDX is the position of this
8166 parameter in the template parameter list. ARGS is the original
8167 template argument list. */
8168 static tree
8169 coerce_template_parameter_pack (tree parms,
8170 int parm_idx,
8171 tree args,
8172 tree inner_args,
8173 int arg_idx,
8174 tree new_args,
8175 int* lost,
8176 tree in_decl,
8177 tsubst_flags_t complain)
8178 {
8179 tree parm = TREE_VEC_ELT (parms, parm_idx);
8180 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8181 tree packed_args;
8182 tree argument_pack;
8183 tree packed_parms = NULL_TREE;
8184
8185 if (arg_idx > nargs)
8186 arg_idx = nargs;
8187
8188 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8189 {
8190 /* When the template parameter is a non-type template parameter pack
8191 or template template parameter pack whose type or template
8192 parameters use parameter packs, we know exactly how many arguments
8193 we are looking for. Build a vector of the instantiated decls for
8194 these template parameters in PACKED_PARMS. */
8195 /* We can't use make_pack_expansion here because it would interpret a
8196 _DECL as a use rather than a declaration. */
8197 tree decl = TREE_VALUE (parm);
8198 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8199 SET_PACK_EXPANSION_PATTERN (exp, decl);
8200 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8201 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8202
8203 TREE_VEC_LENGTH (args)--;
8204 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8205 TREE_VEC_LENGTH (args)++;
8206
8207 if (packed_parms == error_mark_node)
8208 return error_mark_node;
8209
8210 /* If we're doing a partial instantiation of a member template,
8211 verify that all of the types used for the non-type
8212 template parameter pack are, in fact, valid for non-type
8213 template parameters. */
8214 if (arg_idx < nargs
8215 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8216 {
8217 int j, len = TREE_VEC_LENGTH (packed_parms);
8218 for (j = 0; j < len; ++j)
8219 {
8220 tree t = TREE_VEC_ELT (packed_parms, j);
8221 if (TREE_CODE (t) == PARM_DECL
8222 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8223 return error_mark_node;
8224 }
8225 /* We don't know how many args we have yet, just
8226 use the unconverted ones for now. */
8227 return NULL_TREE;
8228 }
8229
8230 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8231 }
8232 /* Check if we have a placeholder pack, which indicates we're
8233 in the context of a introduction list. In that case we want
8234 to match this pack to the single placeholder. */
8235 else if (arg_idx < nargs
8236 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8237 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8238 {
8239 nargs = arg_idx + 1;
8240 packed_args = make_tree_vec (1);
8241 }
8242 else
8243 packed_args = make_tree_vec (nargs - arg_idx);
8244
8245 /* Convert the remaining arguments, which will be a part of the
8246 parameter pack "parm". */
8247 int first_pack_arg = arg_idx;
8248 for (; arg_idx < nargs; ++arg_idx)
8249 {
8250 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8251 tree actual_parm = TREE_VALUE (parm);
8252 int pack_idx = arg_idx - first_pack_arg;
8253
8254 if (packed_parms)
8255 {
8256 /* Once we've packed as many args as we have types, stop. */
8257 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8258 break;
8259 else if (PACK_EXPANSION_P (arg))
8260 /* We don't know how many args we have yet, just
8261 use the unconverted ones for now. */
8262 return NULL_TREE;
8263 else
8264 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8265 }
8266
8267 if (arg == error_mark_node)
8268 {
8269 if (complain & tf_error)
8270 error ("template argument %d is invalid", arg_idx + 1);
8271 }
8272 else
8273 arg = convert_template_argument (actual_parm,
8274 arg, new_args, complain, parm_idx,
8275 in_decl);
8276 if (arg == error_mark_node)
8277 (*lost)++;
8278 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8279 }
8280
8281 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8282 && TREE_VEC_LENGTH (packed_args) > 0)
8283 {
8284 if (complain & tf_error)
8285 error ("wrong number of template arguments (%d, should be %d)",
8286 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8287 return error_mark_node;
8288 }
8289
8290 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8291 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8292 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8293 else
8294 {
8295 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8296 TREE_CONSTANT (argument_pack) = 1;
8297 }
8298
8299 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8300 if (CHECKING_P)
8301 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8302 TREE_VEC_LENGTH (packed_args));
8303 return argument_pack;
8304 }
8305
8306 /* Returns the number of pack expansions in the template argument vector
8307 ARGS. */
8308
8309 static int
8310 pack_expansion_args_count (tree args)
8311 {
8312 int i;
8313 int count = 0;
8314 if (args)
8315 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8316 {
8317 tree elt = TREE_VEC_ELT (args, i);
8318 if (elt && PACK_EXPANSION_P (elt))
8319 ++count;
8320 }
8321 return count;
8322 }
8323
8324 /* Convert all template arguments to their appropriate types, and
8325 return a vector containing the innermost resulting template
8326 arguments. If any error occurs, return error_mark_node. Error and
8327 warning messages are issued under control of COMPLAIN.
8328
8329 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8330 for arguments not specified in ARGS. Otherwise, if
8331 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8332 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8333 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8334 ARGS. */
8335
8336 static tree
8337 coerce_template_parms (tree parms,
8338 tree args,
8339 tree in_decl,
8340 tsubst_flags_t complain,
8341 bool require_all_args,
8342 bool use_default_args)
8343 {
8344 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8345 tree orig_inner_args;
8346 tree inner_args;
8347 tree new_args;
8348 tree new_inner_args;
8349
8350 /* When used as a boolean value, indicates whether this is a
8351 variadic template parameter list. Since it's an int, we can also
8352 subtract it from nparms to get the number of non-variadic
8353 parameters. */
8354 int variadic_p = 0;
8355 int variadic_args_p = 0;
8356 int post_variadic_parms = 0;
8357
8358 /* Adjustment to nparms for fixed parameter packs. */
8359 int fixed_pack_adjust = 0;
8360 int fixed_packs = 0;
8361 int missing = 0;
8362
8363 /* Likewise for parameters with default arguments. */
8364 int default_p = 0;
8365
8366 if (args == error_mark_node)
8367 return error_mark_node;
8368
8369 nparms = TREE_VEC_LENGTH (parms);
8370
8371 /* Determine if there are any parameter packs or default arguments. */
8372 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8373 {
8374 tree parm = TREE_VEC_ELT (parms, parm_idx);
8375 if (variadic_p)
8376 ++post_variadic_parms;
8377 if (template_parameter_pack_p (TREE_VALUE (parm)))
8378 ++variadic_p;
8379 if (TREE_PURPOSE (parm))
8380 ++default_p;
8381 }
8382
8383 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8384 /* If there are no parameters that follow a parameter pack, we need to
8385 expand any argument packs so that we can deduce a parameter pack from
8386 some non-packed args followed by an argument pack, as in variadic85.C.
8387 If there are such parameters, we need to leave argument packs intact
8388 so the arguments are assigned properly. This can happen when dealing
8389 with a nested class inside a partial specialization of a class
8390 template, as in variadic92.C, or when deducing a template parameter pack
8391 from a sub-declarator, as in variadic114.C. */
8392 if (!post_variadic_parms)
8393 inner_args = expand_template_argument_pack (inner_args);
8394
8395 /* Count any pack expansion args. */
8396 variadic_args_p = pack_expansion_args_count (inner_args);
8397
8398 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8399 if ((nargs - variadic_args_p > nparms && !variadic_p)
8400 || (nargs < nparms - variadic_p
8401 && require_all_args
8402 && !variadic_args_p
8403 && (!use_default_args
8404 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8405 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8406 {
8407 bad_nargs:
8408 if (complain & tf_error)
8409 {
8410 if (variadic_p || default_p)
8411 {
8412 nparms -= variadic_p + default_p;
8413 error ("wrong number of template arguments "
8414 "(%d, should be at least %d)", nargs, nparms);
8415 }
8416 else
8417 error ("wrong number of template arguments "
8418 "(%d, should be %d)", nargs, nparms);
8419
8420 if (in_decl)
8421 inform (DECL_SOURCE_LOCATION (in_decl),
8422 "provided for %qD", in_decl);
8423 }
8424
8425 return error_mark_node;
8426 }
8427 /* We can't pass a pack expansion to a non-pack parameter of an alias
8428 template (DR 1430). */
8429 else if (in_decl
8430 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8431 || concept_template_p (in_decl))
8432 && variadic_args_p
8433 && nargs - variadic_args_p < nparms - variadic_p)
8434 {
8435 if (complain & tf_error)
8436 {
8437 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8438 {
8439 tree arg = TREE_VEC_ELT (inner_args, i);
8440 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8441
8442 if (PACK_EXPANSION_P (arg)
8443 && !template_parameter_pack_p (parm))
8444 {
8445 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8446 error_at (location_of (arg),
8447 "pack expansion argument for non-pack parameter "
8448 "%qD of alias template %qD", parm, in_decl);
8449 else
8450 error_at (location_of (arg),
8451 "pack expansion argument for non-pack parameter "
8452 "%qD of concept %qD", parm, in_decl);
8453 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8454 goto found;
8455 }
8456 }
8457 gcc_unreachable ();
8458 found:;
8459 }
8460 return error_mark_node;
8461 }
8462
8463 /* We need to evaluate the template arguments, even though this
8464 template-id may be nested within a "sizeof". */
8465 cp_evaluated ev;
8466
8467 new_inner_args = make_tree_vec (nparms);
8468 new_args = add_outermost_template_args (args, new_inner_args);
8469 int pack_adjust = 0;
8470 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8471 {
8472 tree arg;
8473 tree parm;
8474
8475 /* Get the Ith template parameter. */
8476 parm = TREE_VEC_ELT (parms, parm_idx);
8477
8478 if (parm == error_mark_node)
8479 {
8480 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8481 continue;
8482 }
8483
8484 /* Calculate the next argument. */
8485 if (arg_idx < nargs)
8486 arg = TREE_VEC_ELT (inner_args, arg_idx);
8487 else
8488 arg = NULL_TREE;
8489
8490 if (template_parameter_pack_p (TREE_VALUE (parm))
8491 && (arg || require_all_args || !(complain & tf_partial))
8492 && !(arg && ARGUMENT_PACK_P (arg)))
8493 {
8494 /* Some arguments will be placed in the
8495 template parameter pack PARM. */
8496 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8497 inner_args, arg_idx,
8498 new_args, &lost,
8499 in_decl, complain);
8500
8501 if (arg == NULL_TREE)
8502 {
8503 /* We don't know how many args we have yet, just use the
8504 unconverted (and still packed) ones for now. */
8505 new_inner_args = orig_inner_args;
8506 arg_idx = nargs;
8507 break;
8508 }
8509
8510 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8511
8512 /* Store this argument. */
8513 if (arg == error_mark_node)
8514 {
8515 lost++;
8516 /* We are done with all of the arguments. */
8517 arg_idx = nargs;
8518 break;
8519 }
8520 else
8521 {
8522 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8523 arg_idx += pack_adjust;
8524 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8525 {
8526 ++fixed_packs;
8527 fixed_pack_adjust += pack_adjust;
8528 }
8529 }
8530
8531 continue;
8532 }
8533 else if (arg)
8534 {
8535 if (PACK_EXPANSION_P (arg))
8536 {
8537 /* "If every valid specialization of a variadic template
8538 requires an empty template parameter pack, the template is
8539 ill-formed, no diagnostic required." So check that the
8540 pattern works with this parameter. */
8541 tree pattern = PACK_EXPANSION_PATTERN (arg);
8542 tree conv = convert_template_argument (TREE_VALUE (parm),
8543 pattern, new_args,
8544 complain, parm_idx,
8545 in_decl);
8546 if (conv == error_mark_node)
8547 {
8548 if (complain & tf_error)
8549 inform (input_location, "so any instantiation with a "
8550 "non-empty parameter pack would be ill-formed");
8551 ++lost;
8552 }
8553 else if (TYPE_P (conv) && !TYPE_P (pattern))
8554 /* Recover from missing typename. */
8555 TREE_VEC_ELT (inner_args, arg_idx)
8556 = make_pack_expansion (conv, complain);
8557
8558 /* We don't know how many args we have yet, just
8559 use the unconverted ones for now. */
8560 new_inner_args = inner_args;
8561 arg_idx = nargs;
8562 break;
8563 }
8564 }
8565 else if (require_all_args)
8566 {
8567 /* There must be a default arg in this case. */
8568 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8569 complain, in_decl);
8570 /* The position of the first default template argument,
8571 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8572 Record that. */
8573 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8574 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8575 arg_idx - pack_adjust);
8576 }
8577 else
8578 break;
8579
8580 if (arg == error_mark_node)
8581 {
8582 if (complain & tf_error)
8583 error ("template argument %d is invalid", arg_idx + 1);
8584 }
8585 else if (!arg)
8586 {
8587 /* This can occur if there was an error in the template
8588 parameter list itself (which we would already have
8589 reported) that we are trying to recover from, e.g., a class
8590 template with a parameter list such as
8591 template<typename..., typename> (cpp0x/variadic150.C). */
8592 ++lost;
8593
8594 /* This can also happen with a fixed parameter pack (71834). */
8595 if (arg_idx >= nargs)
8596 ++missing;
8597 }
8598 else
8599 arg = convert_template_argument (TREE_VALUE (parm),
8600 arg, new_args, complain,
8601 parm_idx, in_decl);
8602
8603 if (arg == error_mark_node)
8604 lost++;
8605 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8606 }
8607
8608 if (missing || arg_idx < nargs - variadic_args_p)
8609 {
8610 /* If we had fixed parameter packs, we didn't know how many arguments we
8611 actually needed earlier; now we do. */
8612 nparms += fixed_pack_adjust;
8613 variadic_p -= fixed_packs;
8614 goto bad_nargs;
8615 }
8616
8617 if (arg_idx < nargs)
8618 {
8619 /* We had some pack expansion arguments that will only work if the packs
8620 are empty, but wait until instantiation time to complain.
8621 See variadic-ttp3.C. */
8622 int len = nparms + (nargs - arg_idx);
8623 tree args = make_tree_vec (len);
8624 int i = 0;
8625 for (; i < nparms; ++i)
8626 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8627 for (; i < len; ++i, ++arg_idx)
8628 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8629 arg_idx - pack_adjust);
8630 new_inner_args = args;
8631 }
8632
8633 if (lost)
8634 {
8635 gcc_assert (!(complain & tf_error) || seen_error ());
8636 return error_mark_node;
8637 }
8638
8639 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8640 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8641 TREE_VEC_LENGTH (new_inner_args));
8642
8643 return new_inner_args;
8644 }
8645
8646 /* Convert all template arguments to their appropriate types, and
8647 return a vector containing the innermost resulting template
8648 arguments. If any error occurs, return error_mark_node. Error and
8649 warning messages are not issued.
8650
8651 Note that no function argument deduction is performed, and default
8652 arguments are used to fill in unspecified arguments. */
8653 tree
8654 coerce_template_parms (tree parms, tree args, tree in_decl)
8655 {
8656 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8657 }
8658
8659 /* Convert all template arguments to their appropriate type, and
8660 instantiate default arguments as needed. This returns a vector
8661 containing the innermost resulting template arguments, or
8662 error_mark_node if unsuccessful. */
8663 tree
8664 coerce_template_parms (tree parms, tree args, tree in_decl,
8665 tsubst_flags_t complain)
8666 {
8667 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8668 }
8669
8670 /* Like coerce_template_parms. If PARMS represents all template
8671 parameters levels, this function returns a vector of vectors
8672 representing all the resulting argument levels. Note that in this
8673 case, only the innermost arguments are coerced because the
8674 outermost ones are supposed to have been coerced already.
8675
8676 Otherwise, if PARMS represents only (the innermost) vector of
8677 parameters, this function returns a vector containing just the
8678 innermost resulting arguments. */
8679
8680 static tree
8681 coerce_innermost_template_parms (tree parms,
8682 tree args,
8683 tree in_decl,
8684 tsubst_flags_t complain,
8685 bool require_all_args,
8686 bool use_default_args)
8687 {
8688 int parms_depth = TMPL_PARMS_DEPTH (parms);
8689 int args_depth = TMPL_ARGS_DEPTH (args);
8690 tree coerced_args;
8691
8692 if (parms_depth > 1)
8693 {
8694 coerced_args = make_tree_vec (parms_depth);
8695 tree level;
8696 int cur_depth;
8697
8698 for (level = parms, cur_depth = parms_depth;
8699 parms_depth > 0 && level != NULL_TREE;
8700 level = TREE_CHAIN (level), --cur_depth)
8701 {
8702 tree l;
8703 if (cur_depth == args_depth)
8704 l = coerce_template_parms (TREE_VALUE (level),
8705 args, in_decl, complain,
8706 require_all_args,
8707 use_default_args);
8708 else
8709 l = TMPL_ARGS_LEVEL (args, cur_depth);
8710
8711 if (l == error_mark_node)
8712 return error_mark_node;
8713
8714 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8715 }
8716 }
8717 else
8718 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8719 args, in_decl, complain,
8720 require_all_args,
8721 use_default_args);
8722 return coerced_args;
8723 }
8724
8725 /* Returns 1 if template args OT and NT are equivalent. */
8726
8727 int
8728 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8729 {
8730 if (nt == ot)
8731 return 1;
8732 if (nt == NULL_TREE || ot == NULL_TREE)
8733 return false;
8734 if (nt == any_targ_node || ot == any_targ_node)
8735 return true;
8736
8737 if (TREE_CODE (nt) == TREE_VEC)
8738 /* For member templates */
8739 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8740 else if (PACK_EXPANSION_P (ot))
8741 return (PACK_EXPANSION_P (nt)
8742 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8743 PACK_EXPANSION_PATTERN (nt))
8744 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8745 PACK_EXPANSION_EXTRA_ARGS (nt)));
8746 else if (ARGUMENT_PACK_P (ot))
8747 {
8748 int i, len;
8749 tree opack, npack;
8750
8751 if (!ARGUMENT_PACK_P (nt))
8752 return 0;
8753
8754 opack = ARGUMENT_PACK_ARGS (ot);
8755 npack = ARGUMENT_PACK_ARGS (nt);
8756 len = TREE_VEC_LENGTH (opack);
8757 if (TREE_VEC_LENGTH (npack) != len)
8758 return 0;
8759 for (i = 0; i < len; ++i)
8760 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8761 TREE_VEC_ELT (npack, i)))
8762 return 0;
8763 return 1;
8764 }
8765 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8766 gcc_unreachable ();
8767 else if (TYPE_P (nt))
8768 {
8769 if (!TYPE_P (ot))
8770 return false;
8771 /* Don't treat an alias template specialization with dependent
8772 arguments as equivalent to its underlying type when used as a
8773 template argument; we need them to be distinct so that we
8774 substitute into the specialization arguments at instantiation
8775 time. And aliases can't be equivalent without being ==, so
8776 we don't need to look any deeper.
8777
8778 During partial ordering, however, we need to treat them normally so
8779 that we can order uses of the same alias with different
8780 cv-qualification (79960). */
8781 if (!partial_order
8782 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8783 return false;
8784 else
8785 return same_type_p (ot, nt);
8786 }
8787 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8788 return 0;
8789 else
8790 {
8791 /* Try to treat a template non-type argument that has been converted
8792 to the parameter type as equivalent to one that hasn't yet. */
8793 for (enum tree_code code1 = TREE_CODE (ot);
8794 CONVERT_EXPR_CODE_P (code1)
8795 || code1 == NON_LVALUE_EXPR;
8796 code1 = TREE_CODE (ot))
8797 ot = TREE_OPERAND (ot, 0);
8798 for (enum tree_code code2 = TREE_CODE (nt);
8799 CONVERT_EXPR_CODE_P (code2)
8800 || code2 == NON_LVALUE_EXPR;
8801 code2 = TREE_CODE (nt))
8802 nt = TREE_OPERAND (nt, 0);
8803
8804 return cp_tree_equal (ot, nt);
8805 }
8806 }
8807
8808 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8809 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8810 NEWARG_PTR with the offending arguments if they are non-NULL. */
8811
8812 int
8813 comp_template_args (tree oldargs, tree newargs,
8814 tree *oldarg_ptr, tree *newarg_ptr,
8815 bool partial_order)
8816 {
8817 int i;
8818
8819 if (oldargs == newargs)
8820 return 1;
8821
8822 if (!oldargs || !newargs)
8823 return 0;
8824
8825 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8826 return 0;
8827
8828 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8829 {
8830 tree nt = TREE_VEC_ELT (newargs, i);
8831 tree ot = TREE_VEC_ELT (oldargs, i);
8832
8833 if (! template_args_equal (ot, nt, partial_order))
8834 {
8835 if (oldarg_ptr != NULL)
8836 *oldarg_ptr = ot;
8837 if (newarg_ptr != NULL)
8838 *newarg_ptr = nt;
8839 return 0;
8840 }
8841 }
8842 return 1;
8843 }
8844
8845 inline bool
8846 comp_template_args_porder (tree oargs, tree nargs)
8847 {
8848 return comp_template_args (oargs, nargs, NULL, NULL, true);
8849 }
8850
8851 /* Implement a freelist interface for objects of type T.
8852
8853 Head is a separate object, rather than a regular member, so that we
8854 can define it as a GTY deletable pointer, which is highly
8855 desirable. A data member could be declared that way, but then the
8856 containing object would implicitly get GTY((user)), which would
8857 prevent us from instantiating freelists as global objects.
8858 Although this way we can create freelist global objects, they're
8859 such thin wrappers that instantiating temporaries at every use
8860 loses nothing and saves permanent storage for the freelist object.
8861
8862 Member functions next, anew, poison and reinit have default
8863 implementations that work for most of the types we're interested
8864 in, but if they don't work for some type, they should be explicitly
8865 specialized. See the comments before them for requirements, and
8866 the example specializations for the tree_list_freelist. */
8867 template <typename T>
8868 class freelist
8869 {
8870 /* Return the next object in a chain. We could just do type
8871 punning, but if we access the object with its underlying type, we
8872 avoid strict-aliasing trouble. This needs only work between
8873 poison and reinit. */
8874 static T *&next (T *obj) { return obj->next; }
8875
8876 /* Return a newly allocated, uninitialized or minimally-initialized
8877 object of type T. Any initialization performed by anew should
8878 either remain across the life of the object and the execution of
8879 poison, or be redone by reinit. */
8880 static T *anew () { return ggc_alloc<T> (); }
8881
8882 /* Optionally scribble all over the bits holding the object, so that
8883 they become (mostly?) uninitialized memory. This is called while
8884 preparing to make the object part of the free list. */
8885 static void poison (T *obj) {
8886 T *p ATTRIBUTE_UNUSED = obj;
8887 T **q ATTRIBUTE_UNUSED = &next (obj);
8888
8889 #ifdef ENABLE_GC_CHECKING
8890 /* Poison the data, to indicate the data is garbage. */
8891 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8892 memset (p, 0xa5, sizeof (*p));
8893 #endif
8894 /* Let valgrind know the object is free. */
8895 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8896
8897 /* Let valgrind know the next portion of the object is available,
8898 but uninitialized. */
8899 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8900 }
8901
8902 /* Bring an object that underwent at least one lifecycle after anew
8903 and before the most recent free and poison, back to a usable
8904 state, reinitializing whatever is needed for it to be
8905 functionally equivalent to an object just allocated and returned
8906 by anew. This may poison or clear the next field, used by
8907 freelist housekeeping after poison was called. */
8908 static void reinit (T *obj) {
8909 T **q ATTRIBUTE_UNUSED = &next (obj);
8910
8911 #ifdef ENABLE_GC_CHECKING
8912 memset (q, 0xa5, sizeof (*q));
8913 #endif
8914 /* Let valgrind know the entire object is available, but
8915 uninitialized. */
8916 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8917 }
8918
8919 /* Reference a GTY-deletable pointer that points to the first object
8920 in the free list proper. */
8921 T *&head;
8922 public:
8923 /* Construct a freelist object chaining objects off of HEAD. */
8924 freelist (T *&head) : head(head) {}
8925
8926 /* Add OBJ to the free object list. The former head becomes OBJ's
8927 successor. */
8928 void free (T *obj)
8929 {
8930 poison (obj);
8931 next (obj) = head;
8932 head = obj;
8933 }
8934
8935 /* Take an object from the free list, if one is available, or
8936 allocate a new one. Objects taken from the free list should be
8937 regarded as filled with garbage, except for bits that are
8938 configured to be preserved across free and alloc. */
8939 T *alloc ()
8940 {
8941 if (head)
8942 {
8943 T *obj = head;
8944 head = next (head);
8945 reinit (obj);
8946 return obj;
8947 }
8948 else
8949 return anew ();
8950 }
8951 };
8952
8953 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8954 want to allocate a TREE_LIST using the usual interface, and ensure
8955 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8956 build_tree_list logic in reinit, so this could go out of sync. */
8957 template <>
8958 inline tree &
8959 freelist<tree_node>::next (tree obj)
8960 {
8961 return TREE_CHAIN (obj);
8962 }
8963 template <>
8964 inline tree
8965 freelist<tree_node>::anew ()
8966 {
8967 return build_tree_list (NULL, NULL);
8968 }
8969 template <>
8970 inline void
8971 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8972 {
8973 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8974 tree p ATTRIBUTE_UNUSED = obj;
8975 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8976 tree *q ATTRIBUTE_UNUSED = &next (obj);
8977
8978 #ifdef ENABLE_GC_CHECKING
8979 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8980
8981 /* Poison the data, to indicate the data is garbage. */
8982 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8983 memset (p, 0xa5, size);
8984 #endif
8985 /* Let valgrind know the object is free. */
8986 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8987 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8988 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8989 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8990
8991 #ifdef ENABLE_GC_CHECKING
8992 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8993 /* Keep TREE_CHAIN functional. */
8994 TREE_SET_CODE (obj, TREE_LIST);
8995 #else
8996 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8997 #endif
8998 }
8999 template <>
9000 inline void
9001 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9002 {
9003 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9004
9005 #ifdef ENABLE_GC_CHECKING
9006 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9007 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9008 memset (obj, 0, sizeof (tree_list));
9009 #endif
9010
9011 /* Let valgrind know the entire object is available, but
9012 uninitialized. */
9013 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9014
9015 #ifdef ENABLE_GC_CHECKING
9016 TREE_SET_CODE (obj, TREE_LIST);
9017 #else
9018 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9019 #endif
9020 }
9021
9022 /* Point to the first object in the TREE_LIST freelist. */
9023 static GTY((deletable)) tree tree_list_freelist_head;
9024 /* Return the/an actual TREE_LIST freelist. */
9025 static inline freelist<tree_node>
9026 tree_list_freelist ()
9027 {
9028 return tree_list_freelist_head;
9029 }
9030
9031 /* Point to the first object in the tinst_level freelist. */
9032 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9033 /* Return the/an actual tinst_level freelist. */
9034 static inline freelist<tinst_level>
9035 tinst_level_freelist ()
9036 {
9037 return tinst_level_freelist_head;
9038 }
9039
9040 /* Point to the first object in the pending_template freelist. */
9041 static GTY((deletable)) pending_template *pending_template_freelist_head;
9042 /* Return the/an actual pending_template freelist. */
9043 static inline freelist<pending_template>
9044 pending_template_freelist ()
9045 {
9046 return pending_template_freelist_head;
9047 }
9048
9049 /* Build the TREE_LIST object out of a split list, store it
9050 permanently, and return it. */
9051 tree
9052 tinst_level::to_list ()
9053 {
9054 gcc_assert (split_list_p ());
9055 tree ret = tree_list_freelist ().alloc ();
9056 TREE_PURPOSE (ret) = tldcl;
9057 TREE_VALUE (ret) = targs;
9058 tldcl = ret;
9059 targs = NULL;
9060 gcc_assert (tree_list_p ());
9061 return ret;
9062 }
9063
9064 const unsigned short tinst_level::refcount_infinity;
9065
9066 /* Increment OBJ's refcount unless it is already infinite. */
9067 static tinst_level *
9068 inc_refcount_use (tinst_level *obj)
9069 {
9070 if (obj && obj->refcount != tinst_level::refcount_infinity)
9071 ++obj->refcount;
9072 return obj;
9073 }
9074
9075 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9076 void
9077 tinst_level::free (tinst_level *obj)
9078 {
9079 if (obj->tree_list_p ())
9080 tree_list_freelist ().free (obj->get_node ());
9081 tinst_level_freelist ().free (obj);
9082 }
9083
9084 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9085 OBJ's DECL and OBJ, and start over with the tinst_level object that
9086 used to be referenced by OBJ's NEXT. */
9087 static void
9088 dec_refcount_use (tinst_level *obj)
9089 {
9090 while (obj
9091 && obj->refcount != tinst_level::refcount_infinity
9092 && !--obj->refcount)
9093 {
9094 tinst_level *next = obj->next;
9095 tinst_level::free (obj);
9096 obj = next;
9097 }
9098 }
9099
9100 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9101 and of the former PTR. Omitting the second argument is equivalent
9102 to passing (T*)NULL; this is allowed because passing the
9103 zero-valued integral constant NULL confuses type deduction and/or
9104 overload resolution. */
9105 template <typename T>
9106 static void
9107 set_refcount_ptr (T *& ptr, T *obj = NULL)
9108 {
9109 T *save = ptr;
9110 ptr = inc_refcount_use (obj);
9111 dec_refcount_use (save);
9112 }
9113
9114 static void
9115 add_pending_template (tree d)
9116 {
9117 tree ti = (TYPE_P (d)
9118 ? CLASSTYPE_TEMPLATE_INFO (d)
9119 : DECL_TEMPLATE_INFO (d));
9120 struct pending_template *pt;
9121 int level;
9122
9123 if (TI_PENDING_TEMPLATE_FLAG (ti))
9124 return;
9125
9126 /* We are called both from instantiate_decl, where we've already had a
9127 tinst_level pushed, and instantiate_template, where we haven't.
9128 Compensate. */
9129 gcc_assert (TREE_CODE (d) != TREE_LIST);
9130 level = !current_tinst_level
9131 || current_tinst_level->maybe_get_node () != d;
9132
9133 if (level)
9134 push_tinst_level (d);
9135
9136 pt = pending_template_freelist ().alloc ();
9137 pt->next = NULL;
9138 pt->tinst = NULL;
9139 set_refcount_ptr (pt->tinst, current_tinst_level);
9140 if (last_pending_template)
9141 last_pending_template->next = pt;
9142 else
9143 pending_templates = pt;
9144
9145 last_pending_template = pt;
9146
9147 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9148
9149 if (level)
9150 pop_tinst_level ();
9151 }
9152
9153
9154 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9155 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9156 documentation for TEMPLATE_ID_EXPR. */
9157
9158 tree
9159 lookup_template_function (tree fns, tree arglist)
9160 {
9161 if (fns == error_mark_node || arglist == error_mark_node)
9162 return error_mark_node;
9163
9164 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9165
9166 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9167 {
9168 error ("%q#D is not a function template", fns);
9169 return error_mark_node;
9170 }
9171
9172 if (BASELINK_P (fns))
9173 {
9174 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9175 unknown_type_node,
9176 BASELINK_FUNCTIONS (fns),
9177 arglist);
9178 return fns;
9179 }
9180
9181 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9182 }
9183
9184 /* Within the scope of a template class S<T>, the name S gets bound
9185 (in build_self_reference) to a TYPE_DECL for the class, not a
9186 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9187 or one of its enclosing classes, and that type is a template,
9188 return the associated TEMPLATE_DECL. Otherwise, the original
9189 DECL is returned.
9190
9191 Also handle the case when DECL is a TREE_LIST of ambiguous
9192 injected-class-names from different bases. */
9193
9194 tree
9195 maybe_get_template_decl_from_type_decl (tree decl)
9196 {
9197 if (decl == NULL_TREE)
9198 return decl;
9199
9200 /* DR 176: A lookup that finds an injected-class-name (10.2
9201 [class.member.lookup]) can result in an ambiguity in certain cases
9202 (for example, if it is found in more than one base class). If all of
9203 the injected-class-names that are found refer to specializations of
9204 the same class template, and if the name is followed by a
9205 template-argument-list, the reference refers to the class template
9206 itself and not a specialization thereof, and is not ambiguous. */
9207 if (TREE_CODE (decl) == TREE_LIST)
9208 {
9209 tree t, tmpl = NULL_TREE;
9210 for (t = decl; t; t = TREE_CHAIN (t))
9211 {
9212 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9213 if (!tmpl)
9214 tmpl = elt;
9215 else if (tmpl != elt)
9216 break;
9217 }
9218 if (tmpl && t == NULL_TREE)
9219 return tmpl;
9220 else
9221 return decl;
9222 }
9223
9224 return (decl != NULL_TREE
9225 && DECL_SELF_REFERENCE_P (decl)
9226 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9227 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9228 }
9229
9230 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9231 parameters, find the desired type.
9232
9233 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9234
9235 IN_DECL, if non-NULL, is the template declaration we are trying to
9236 instantiate.
9237
9238 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9239 the class we are looking up.
9240
9241 Issue error and warning messages under control of COMPLAIN.
9242
9243 If the template class is really a local class in a template
9244 function, then the FUNCTION_CONTEXT is the function in which it is
9245 being instantiated.
9246
9247 ??? Note that this function is currently called *twice* for each
9248 template-id: the first time from the parser, while creating the
9249 incomplete type (finish_template_type), and the second type during the
9250 real instantiation (instantiate_template_class). This is surely something
9251 that we want to avoid. It also causes some problems with argument
9252 coercion (see convert_nontype_argument for more information on this). */
9253
9254 static tree
9255 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9256 int entering_scope, tsubst_flags_t complain)
9257 {
9258 tree templ = NULL_TREE, parmlist;
9259 tree t;
9260 spec_entry **slot;
9261 spec_entry *entry;
9262 spec_entry elt;
9263 hashval_t hash;
9264
9265 if (identifier_p (d1))
9266 {
9267 tree value = innermost_non_namespace_value (d1);
9268 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9269 templ = value;
9270 else
9271 {
9272 if (context)
9273 push_decl_namespace (context);
9274 templ = lookup_name (d1);
9275 templ = maybe_get_template_decl_from_type_decl (templ);
9276 if (context)
9277 pop_decl_namespace ();
9278 }
9279 if (templ)
9280 context = DECL_CONTEXT (templ);
9281 }
9282 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9283 {
9284 tree type = TREE_TYPE (d1);
9285
9286 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9287 an implicit typename for the second A. Deal with it. */
9288 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9289 type = TREE_TYPE (type);
9290
9291 if (CLASSTYPE_TEMPLATE_INFO (type))
9292 {
9293 templ = CLASSTYPE_TI_TEMPLATE (type);
9294 d1 = DECL_NAME (templ);
9295 }
9296 }
9297 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9298 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9299 {
9300 templ = TYPE_TI_TEMPLATE (d1);
9301 d1 = DECL_NAME (templ);
9302 }
9303 else if (DECL_TYPE_TEMPLATE_P (d1))
9304 {
9305 templ = d1;
9306 d1 = DECL_NAME (templ);
9307 context = DECL_CONTEXT (templ);
9308 }
9309 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9310 {
9311 templ = d1;
9312 d1 = DECL_NAME (templ);
9313 }
9314
9315 /* Issue an error message if we didn't find a template. */
9316 if (! templ)
9317 {
9318 if (complain & tf_error)
9319 error ("%qT is not a template", d1);
9320 return error_mark_node;
9321 }
9322
9323 if (TREE_CODE (templ) != TEMPLATE_DECL
9324 /* Make sure it's a user visible template, if it was named by
9325 the user. */
9326 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9327 && !PRIMARY_TEMPLATE_P (templ)))
9328 {
9329 if (complain & tf_error)
9330 {
9331 error ("non-template type %qT used as a template", d1);
9332 if (in_decl)
9333 error ("for template declaration %q+D", in_decl);
9334 }
9335 return error_mark_node;
9336 }
9337
9338 complain &= ~tf_user;
9339
9340 /* An alias that just changes the name of a template is equivalent to the
9341 other template, so if any of the arguments are pack expansions, strip
9342 the alias to avoid problems with a pack expansion passed to a non-pack
9343 alias template parameter (DR 1430). */
9344 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9345 templ = get_underlying_template (templ);
9346
9347 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9348 {
9349 tree parm;
9350 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9351 if (arglist2 == error_mark_node
9352 || (!uses_template_parms (arglist2)
9353 && check_instantiated_args (templ, arglist2, complain)))
9354 return error_mark_node;
9355
9356 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9357 return parm;
9358 }
9359 else
9360 {
9361 tree template_type = TREE_TYPE (templ);
9362 tree gen_tmpl;
9363 tree type_decl;
9364 tree found = NULL_TREE;
9365 int arg_depth;
9366 int parm_depth;
9367 int is_dependent_type;
9368 int use_partial_inst_tmpl = false;
9369
9370 if (template_type == error_mark_node)
9371 /* An error occurred while building the template TEMPL, and a
9372 diagnostic has most certainly been emitted for that
9373 already. Let's propagate that error. */
9374 return error_mark_node;
9375
9376 gen_tmpl = most_general_template (templ);
9377 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9378 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9379 arg_depth = TMPL_ARGS_DEPTH (arglist);
9380
9381 if (arg_depth == 1 && parm_depth > 1)
9382 {
9383 /* We've been given an incomplete set of template arguments.
9384 For example, given:
9385
9386 template <class T> struct S1 {
9387 template <class U> struct S2 {};
9388 template <class U> struct S2<U*> {};
9389 };
9390
9391 we will be called with an ARGLIST of `U*', but the
9392 TEMPLATE will be `template <class T> template
9393 <class U> struct S1<T>::S2'. We must fill in the missing
9394 arguments. */
9395 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9396 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9397 arg_depth = TMPL_ARGS_DEPTH (arglist);
9398 }
9399
9400 /* Now we should have enough arguments. */
9401 gcc_assert (parm_depth == arg_depth);
9402
9403 /* From here on, we're only interested in the most general
9404 template. */
9405
9406 /* Calculate the BOUND_ARGS. These will be the args that are
9407 actually tsubst'd into the definition to create the
9408 instantiation. */
9409 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9410 complain,
9411 /*require_all_args=*/true,
9412 /*use_default_args=*/true);
9413
9414 if (arglist == error_mark_node)
9415 /* We were unable to bind the arguments. */
9416 return error_mark_node;
9417
9418 /* In the scope of a template class, explicit references to the
9419 template class refer to the type of the template, not any
9420 instantiation of it. For example, in:
9421
9422 template <class T> class C { void f(C<T>); }
9423
9424 the `C<T>' is just the same as `C'. Outside of the
9425 class, however, such a reference is an instantiation. */
9426 if (entering_scope
9427 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9428 || currently_open_class (template_type))
9429 {
9430 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9431
9432 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9433 return template_type;
9434 }
9435
9436 /* If we already have this specialization, return it. */
9437 elt.tmpl = gen_tmpl;
9438 elt.args = arglist;
9439 elt.spec = NULL_TREE;
9440 hash = spec_hasher::hash (&elt);
9441 entry = type_specializations->find_with_hash (&elt, hash);
9442
9443 if (entry)
9444 return entry->spec;
9445
9446 /* If the the template's constraints are not satisfied,
9447 then we cannot form a valid type.
9448
9449 Note that the check is deferred until after the hash
9450 lookup. This prevents redundant checks on previously
9451 instantiated specializations. */
9452 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9453 {
9454 if (complain & tf_error)
9455 {
9456 auto_diagnostic_group d;
9457 error ("template constraint failure");
9458 diagnose_constraints (input_location, gen_tmpl, arglist);
9459 }
9460 return error_mark_node;
9461 }
9462
9463 is_dependent_type = uses_template_parms (arglist);
9464
9465 /* If the deduced arguments are invalid, then the binding
9466 failed. */
9467 if (!is_dependent_type
9468 && check_instantiated_args (gen_tmpl,
9469 INNERMOST_TEMPLATE_ARGS (arglist),
9470 complain))
9471 return error_mark_node;
9472
9473 if (!is_dependent_type
9474 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9475 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9476 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9477 {
9478 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9479 DECL_NAME (gen_tmpl),
9480 /*tag_scope=*/ts_global);
9481 return found;
9482 }
9483
9484 context = DECL_CONTEXT (gen_tmpl);
9485 if (context && TYPE_P (context))
9486 {
9487 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9488 context = complete_type (context);
9489 }
9490 else
9491 context = tsubst (context, arglist, complain, in_decl);
9492
9493 if (context == error_mark_node)
9494 return error_mark_node;
9495
9496 if (!context)
9497 context = global_namespace;
9498
9499 /* Create the type. */
9500 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9501 {
9502 /* The user referred to a specialization of an alias
9503 template represented by GEN_TMPL.
9504
9505 [temp.alias]/2 says:
9506
9507 When a template-id refers to the specialization of an
9508 alias template, it is equivalent to the associated
9509 type obtained by substitution of its
9510 template-arguments for the template-parameters in the
9511 type-id of the alias template. */
9512
9513 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9514 /* Note that the call above (by indirectly calling
9515 register_specialization in tsubst_decl) registers the
9516 TYPE_DECL representing the specialization of the alias
9517 template. So next time someone substitutes ARGLIST for
9518 the template parms into the alias template (GEN_TMPL),
9519 she'll get that TYPE_DECL back. */
9520
9521 if (t == error_mark_node)
9522 return t;
9523 }
9524 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9525 {
9526 if (!is_dependent_type)
9527 {
9528 set_current_access_from_decl (TYPE_NAME (template_type));
9529 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9530 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9531 arglist, complain, in_decl),
9532 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9533 arglist, complain, in_decl),
9534 SCOPED_ENUM_P (template_type), NULL);
9535
9536 if (t == error_mark_node)
9537 return t;
9538 }
9539 else
9540 {
9541 /* We don't want to call start_enum for this type, since
9542 the values for the enumeration constants may involve
9543 template parameters. And, no one should be interested
9544 in the enumeration constants for such a type. */
9545 t = cxx_make_type (ENUMERAL_TYPE);
9546 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9547 }
9548 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9549 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9550 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9551 }
9552 else if (CLASS_TYPE_P (template_type))
9553 {
9554 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9555 instantiated here. */
9556 gcc_assert (!LAMBDA_TYPE_P (template_type));
9557
9558 t = make_class_type (TREE_CODE (template_type));
9559 CLASSTYPE_DECLARED_CLASS (t)
9560 = CLASSTYPE_DECLARED_CLASS (template_type);
9561 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9562
9563 /* A local class. Make sure the decl gets registered properly. */
9564 if (context == current_function_decl)
9565 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9566 == error_mark_node)
9567 return error_mark_node;
9568
9569 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9570 /* This instantiation is another name for the primary
9571 template type. Set the TYPE_CANONICAL field
9572 appropriately. */
9573 TYPE_CANONICAL (t) = template_type;
9574 else if (any_template_arguments_need_structural_equality_p (arglist))
9575 /* Some of the template arguments require structural
9576 equality testing, so this template class requires
9577 structural equality testing. */
9578 SET_TYPE_STRUCTURAL_EQUALITY (t);
9579 }
9580 else
9581 gcc_unreachable ();
9582
9583 /* If we called start_enum or pushtag above, this information
9584 will already be set up. */
9585 if (!TYPE_NAME (t))
9586 {
9587 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9588
9589 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9590 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9591 DECL_SOURCE_LOCATION (type_decl)
9592 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9593 }
9594 else
9595 type_decl = TYPE_NAME (t);
9596
9597 if (CLASS_TYPE_P (template_type))
9598 {
9599 TREE_PRIVATE (type_decl)
9600 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9601 TREE_PROTECTED (type_decl)
9602 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9603 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9604 {
9605 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9606 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9607 }
9608 }
9609
9610 if (OVERLOAD_TYPE_P (t)
9611 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9612 {
9613 static const char *tags[] = {"abi_tag", "may_alias"};
9614
9615 for (unsigned ix = 0; ix != 2; ix++)
9616 {
9617 tree attributes
9618 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9619
9620 if (attributes)
9621 TYPE_ATTRIBUTES (t)
9622 = tree_cons (TREE_PURPOSE (attributes),
9623 TREE_VALUE (attributes),
9624 TYPE_ATTRIBUTES (t));
9625 }
9626 }
9627
9628 /* Let's consider the explicit specialization of a member
9629 of a class template specialization that is implicitly instantiated,
9630 e.g.:
9631 template<class T>
9632 struct S
9633 {
9634 template<class U> struct M {}; //#0
9635 };
9636
9637 template<>
9638 template<>
9639 struct S<int>::M<char> //#1
9640 {
9641 int i;
9642 };
9643 [temp.expl.spec]/4 says this is valid.
9644
9645 In this case, when we write:
9646 S<int>::M<char> m;
9647
9648 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9649 the one of #0.
9650
9651 When we encounter #1, we want to store the partial instantiation
9652 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9653
9654 For all cases other than this "explicit specialization of member of a
9655 class template", we just want to store the most general template into
9656 the CLASSTYPE_TI_TEMPLATE of M.
9657
9658 This case of "explicit specialization of member of a class template"
9659 only happens when:
9660 1/ the enclosing class is an instantiation of, and therefore not
9661 the same as, the context of the most general template, and
9662 2/ we aren't looking at the partial instantiation itself, i.e.
9663 the innermost arguments are not the same as the innermost parms of
9664 the most general template.
9665
9666 So it's only when 1/ and 2/ happens that we want to use the partial
9667 instantiation of the member template in lieu of its most general
9668 template. */
9669
9670 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9671 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9672 /* the enclosing class must be an instantiation... */
9673 && CLASS_TYPE_P (context)
9674 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9675 {
9676 TREE_VEC_LENGTH (arglist)--;
9677 ++processing_template_decl;
9678 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9679 tree partial_inst_args =
9680 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9681 arglist, complain, NULL_TREE);
9682 --processing_template_decl;
9683 TREE_VEC_LENGTH (arglist)++;
9684 if (partial_inst_args == error_mark_node)
9685 return error_mark_node;
9686 use_partial_inst_tmpl =
9687 /*...and we must not be looking at the partial instantiation
9688 itself. */
9689 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9690 partial_inst_args);
9691 }
9692
9693 if (!use_partial_inst_tmpl)
9694 /* This case is easy; there are no member templates involved. */
9695 found = gen_tmpl;
9696 else
9697 {
9698 /* This is a full instantiation of a member template. Find
9699 the partial instantiation of which this is an instance. */
9700
9701 /* Temporarily reduce by one the number of levels in the ARGLIST
9702 so as to avoid comparing the last set of arguments. */
9703 TREE_VEC_LENGTH (arglist)--;
9704 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9705 TREE_VEC_LENGTH (arglist)++;
9706 /* FOUND is either a proper class type, or an alias
9707 template specialization. In the later case, it's a
9708 TYPE_DECL, resulting from the substituting of arguments
9709 for parameters in the TYPE_DECL of the alias template
9710 done earlier. So be careful while getting the template
9711 of FOUND. */
9712 found = (TREE_CODE (found) == TEMPLATE_DECL
9713 ? found
9714 : (TREE_CODE (found) == TYPE_DECL
9715 ? DECL_TI_TEMPLATE (found)
9716 : CLASSTYPE_TI_TEMPLATE (found)));
9717
9718 if (DECL_CLASS_TEMPLATE_P (found)
9719 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9720 {
9721 /* If this partial instantiation is specialized, we want to
9722 use it for hash table lookup. */
9723 elt.tmpl = found;
9724 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9725 hash = spec_hasher::hash (&elt);
9726 }
9727 }
9728
9729 // Build template info for the new specialization.
9730 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9731
9732 elt.spec = t;
9733 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9734 gcc_checking_assert (*slot == NULL);
9735 entry = ggc_alloc<spec_entry> ();
9736 *entry = elt;
9737 *slot = entry;
9738
9739 /* Note this use of the partial instantiation so we can check it
9740 later in maybe_process_partial_specialization. */
9741 DECL_TEMPLATE_INSTANTIATIONS (found)
9742 = tree_cons (arglist, t,
9743 DECL_TEMPLATE_INSTANTIATIONS (found));
9744
9745 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9746 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9747 /* Now that the type has been registered on the instantiations
9748 list, we set up the enumerators. Because the enumeration
9749 constants may involve the enumeration type itself, we make
9750 sure to register the type first, and then create the
9751 constants. That way, doing tsubst_expr for the enumeration
9752 constants won't result in recursive calls here; we'll find
9753 the instantiation and exit above. */
9754 tsubst_enum (template_type, t, arglist);
9755
9756 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9757 /* If the type makes use of template parameters, the
9758 code that generates debugging information will crash. */
9759 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9760
9761 /* Possibly limit visibility based on template args. */
9762 TREE_PUBLIC (type_decl) = 1;
9763 determine_visibility (type_decl);
9764
9765 inherit_targ_abi_tags (t);
9766
9767 return t;
9768 }
9769 }
9770
9771 /* Wrapper for lookup_template_class_1. */
9772
9773 tree
9774 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9775 int entering_scope, tsubst_flags_t complain)
9776 {
9777 tree ret;
9778 timevar_push (TV_TEMPLATE_INST);
9779 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9780 entering_scope, complain);
9781 timevar_pop (TV_TEMPLATE_INST);
9782 return ret;
9783 }
9784
9785 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9786
9787 tree
9788 lookup_template_variable (tree templ, tree arglist)
9789 {
9790 /* The type of the expression is NULL_TREE since the template-id could refer
9791 to an explicit or partial specialization. */
9792 tree type = NULL_TREE;
9793 if (flag_concepts && variable_concept_p (templ))
9794 /* Except that concepts are always bool. */
9795 type = boolean_type_node;
9796 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9797 }
9798
9799 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9800
9801 tree
9802 finish_template_variable (tree var, tsubst_flags_t complain)
9803 {
9804 tree templ = TREE_OPERAND (var, 0);
9805 tree arglist = TREE_OPERAND (var, 1);
9806
9807 /* We never want to return a VAR_DECL for a variable concept, since they
9808 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9809 bool concept_p = flag_concepts && variable_concept_p (templ);
9810 if (concept_p && processing_template_decl)
9811 return var;
9812
9813 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9814 arglist = add_outermost_template_args (tmpl_args, arglist);
9815
9816 templ = most_general_template (templ);
9817 tree parms = DECL_TEMPLATE_PARMS (templ);
9818 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9819 /*req_all*/true,
9820 /*use_default*/true);
9821
9822 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9823 {
9824 if (complain & tf_error)
9825 {
9826 auto_diagnostic_group d;
9827 error ("use of invalid variable template %qE", var);
9828 diagnose_constraints (location_of (var), templ, arglist);
9829 }
9830 return error_mark_node;
9831 }
9832
9833 /* If a template-id refers to a specialization of a variable
9834 concept, then the expression is true if and only if the
9835 concept's constraints are satisfied by the given template
9836 arguments.
9837
9838 NOTE: This is an extension of Concepts Lite TS that
9839 allows constraints to be used in expressions. */
9840 if (concept_p)
9841 {
9842 tree decl = DECL_TEMPLATE_RESULT (templ);
9843 return evaluate_variable_concept (decl, arglist);
9844 }
9845
9846 return instantiate_template (templ, arglist, complain);
9847 }
9848
9849 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9850 TARGS template args, and instantiate it if it's not dependent. */
9851
9852 tree
9853 lookup_and_finish_template_variable (tree templ, tree targs,
9854 tsubst_flags_t complain)
9855 {
9856 templ = lookup_template_variable (templ, targs);
9857 if (!any_dependent_template_arguments_p (targs))
9858 {
9859 templ = finish_template_variable (templ, complain);
9860 mark_used (templ);
9861 }
9862
9863 return convert_from_reference (templ);
9864 }
9865
9866 \f
9867 struct pair_fn_data
9868 {
9869 tree_fn_t fn;
9870 tree_fn_t any_fn;
9871 void *data;
9872 /* True when we should also visit template parameters that occur in
9873 non-deduced contexts. */
9874 bool include_nondeduced_p;
9875 hash_set<tree> *visited;
9876 };
9877
9878 /* Called from for_each_template_parm via walk_tree. */
9879
9880 static tree
9881 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9882 {
9883 tree t = *tp;
9884 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9885 tree_fn_t fn = pfd->fn;
9886 void *data = pfd->data;
9887 tree result = NULL_TREE;
9888
9889 #define WALK_SUBTREE(NODE) \
9890 do \
9891 { \
9892 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9893 pfd->include_nondeduced_p, \
9894 pfd->any_fn); \
9895 if (result) goto out; \
9896 } \
9897 while (0)
9898
9899 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9900 return t;
9901
9902 if (TYPE_P (t)
9903 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9904 WALK_SUBTREE (TYPE_CONTEXT (t));
9905
9906 switch (TREE_CODE (t))
9907 {
9908 case RECORD_TYPE:
9909 if (TYPE_PTRMEMFUNC_P (t))
9910 break;
9911 /* Fall through. */
9912
9913 case UNION_TYPE:
9914 case ENUMERAL_TYPE:
9915 if (!TYPE_TEMPLATE_INFO (t))
9916 *walk_subtrees = 0;
9917 else
9918 WALK_SUBTREE (TYPE_TI_ARGS (t));
9919 break;
9920
9921 case INTEGER_TYPE:
9922 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9923 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9924 break;
9925
9926 case METHOD_TYPE:
9927 /* Since we're not going to walk subtrees, we have to do this
9928 explicitly here. */
9929 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9930 /* Fall through. */
9931
9932 case FUNCTION_TYPE:
9933 /* Check the return type. */
9934 WALK_SUBTREE (TREE_TYPE (t));
9935
9936 /* Check the parameter types. Since default arguments are not
9937 instantiated until they are needed, the TYPE_ARG_TYPES may
9938 contain expressions that involve template parameters. But,
9939 no-one should be looking at them yet. And, once they're
9940 instantiated, they don't contain template parameters, so
9941 there's no point in looking at them then, either. */
9942 {
9943 tree parm;
9944
9945 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9946 WALK_SUBTREE (TREE_VALUE (parm));
9947
9948 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9949 want walk_tree walking into them itself. */
9950 *walk_subtrees = 0;
9951 }
9952
9953 if (flag_noexcept_type)
9954 {
9955 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9956 if (spec)
9957 WALK_SUBTREE (TREE_PURPOSE (spec));
9958 }
9959 break;
9960
9961 case TYPEOF_TYPE:
9962 case DECLTYPE_TYPE:
9963 case UNDERLYING_TYPE:
9964 if (pfd->include_nondeduced_p
9965 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9966 pfd->visited,
9967 pfd->include_nondeduced_p,
9968 pfd->any_fn))
9969 return error_mark_node;
9970 *walk_subtrees = false;
9971 break;
9972
9973 case FUNCTION_DECL:
9974 case VAR_DECL:
9975 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9976 WALK_SUBTREE (DECL_TI_ARGS (t));
9977 /* Fall through. */
9978
9979 case PARM_DECL:
9980 case CONST_DECL:
9981 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9982 WALK_SUBTREE (DECL_INITIAL (t));
9983 if (DECL_CONTEXT (t)
9984 && pfd->include_nondeduced_p)
9985 WALK_SUBTREE (DECL_CONTEXT (t));
9986 break;
9987
9988 case BOUND_TEMPLATE_TEMPLATE_PARM:
9989 /* Record template parameters such as `T' inside `TT<T>'. */
9990 WALK_SUBTREE (TYPE_TI_ARGS (t));
9991 /* Fall through. */
9992
9993 case TEMPLATE_TEMPLATE_PARM:
9994 case TEMPLATE_TYPE_PARM:
9995 case TEMPLATE_PARM_INDEX:
9996 if (fn && (*fn)(t, data))
9997 return t;
9998 else if (!fn)
9999 return t;
10000 break;
10001
10002 case TEMPLATE_DECL:
10003 /* A template template parameter is encountered. */
10004 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10005 WALK_SUBTREE (TREE_TYPE (t));
10006
10007 /* Already substituted template template parameter */
10008 *walk_subtrees = 0;
10009 break;
10010
10011 case TYPENAME_TYPE:
10012 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10013 partial instantiation. */
10014 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10015 break;
10016
10017 case CONSTRUCTOR:
10018 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10019 && pfd->include_nondeduced_p)
10020 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10021 break;
10022
10023 case INDIRECT_REF:
10024 case COMPONENT_REF:
10025 /* If there's no type, then this thing must be some expression
10026 involving template parameters. */
10027 if (!fn && !TREE_TYPE (t))
10028 return error_mark_node;
10029 break;
10030
10031 case MODOP_EXPR:
10032 case CAST_EXPR:
10033 case IMPLICIT_CONV_EXPR:
10034 case REINTERPRET_CAST_EXPR:
10035 case CONST_CAST_EXPR:
10036 case STATIC_CAST_EXPR:
10037 case DYNAMIC_CAST_EXPR:
10038 case ARROW_EXPR:
10039 case DOTSTAR_EXPR:
10040 case TYPEID_EXPR:
10041 case PSEUDO_DTOR_EXPR:
10042 if (!fn)
10043 return error_mark_node;
10044 break;
10045
10046 default:
10047 break;
10048 }
10049
10050 #undef WALK_SUBTREE
10051
10052 /* We didn't find any template parameters we liked. */
10053 out:
10054 return result;
10055 }
10056
10057 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10058 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10059 call FN with the parameter and the DATA.
10060 If FN returns nonzero, the iteration is terminated, and
10061 for_each_template_parm returns 1. Otherwise, the iteration
10062 continues. If FN never returns a nonzero value, the value
10063 returned by for_each_template_parm is 0. If FN is NULL, it is
10064 considered to be the function which always returns 1.
10065
10066 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10067 parameters that occur in non-deduced contexts. When false, only
10068 visits those template parameters that can be deduced. */
10069
10070 static tree
10071 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10072 hash_set<tree> *visited,
10073 bool include_nondeduced_p,
10074 tree_fn_t any_fn)
10075 {
10076 struct pair_fn_data pfd;
10077 tree result;
10078
10079 /* Set up. */
10080 pfd.fn = fn;
10081 pfd.any_fn = any_fn;
10082 pfd.data = data;
10083 pfd.include_nondeduced_p = include_nondeduced_p;
10084
10085 /* Walk the tree. (Conceptually, we would like to walk without
10086 duplicates, but for_each_template_parm_r recursively calls
10087 for_each_template_parm, so we would need to reorganize a fair
10088 bit to use walk_tree_without_duplicates, so we keep our own
10089 visited list.) */
10090 if (visited)
10091 pfd.visited = visited;
10092 else
10093 pfd.visited = new hash_set<tree>;
10094 result = cp_walk_tree (&t,
10095 for_each_template_parm_r,
10096 &pfd,
10097 pfd.visited);
10098
10099 /* Clean up. */
10100 if (!visited)
10101 {
10102 delete pfd.visited;
10103 pfd.visited = 0;
10104 }
10105
10106 return result;
10107 }
10108
10109 /* Returns true if T depends on any template parameter. */
10110
10111 int
10112 uses_template_parms (tree t)
10113 {
10114 if (t == NULL_TREE)
10115 return false;
10116
10117 bool dependent_p;
10118 int saved_processing_template_decl;
10119
10120 saved_processing_template_decl = processing_template_decl;
10121 if (!saved_processing_template_decl)
10122 processing_template_decl = 1;
10123 if (TYPE_P (t))
10124 dependent_p = dependent_type_p (t);
10125 else if (TREE_CODE (t) == TREE_VEC)
10126 dependent_p = any_dependent_template_arguments_p (t);
10127 else if (TREE_CODE (t) == TREE_LIST)
10128 dependent_p = (uses_template_parms (TREE_VALUE (t))
10129 || uses_template_parms (TREE_CHAIN (t)));
10130 else if (TREE_CODE (t) == TYPE_DECL)
10131 dependent_p = dependent_type_p (TREE_TYPE (t));
10132 else if (DECL_P (t)
10133 || EXPR_P (t)
10134 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10135 || TREE_CODE (t) == OVERLOAD
10136 || BASELINK_P (t)
10137 || identifier_p (t)
10138 || TREE_CODE (t) == TRAIT_EXPR
10139 || TREE_CODE (t) == CONSTRUCTOR
10140 || CONSTANT_CLASS_P (t))
10141 dependent_p = (type_dependent_expression_p (t)
10142 || value_dependent_expression_p (t));
10143 else
10144 {
10145 gcc_assert (t == error_mark_node);
10146 dependent_p = false;
10147 }
10148
10149 processing_template_decl = saved_processing_template_decl;
10150
10151 return dependent_p;
10152 }
10153
10154 /* Returns true iff current_function_decl is an incompletely instantiated
10155 template. Useful instead of processing_template_decl because the latter
10156 is set to 0 during instantiate_non_dependent_expr. */
10157
10158 bool
10159 in_template_function (void)
10160 {
10161 tree fn = current_function_decl;
10162 bool ret;
10163 ++processing_template_decl;
10164 ret = (fn && DECL_LANG_SPECIFIC (fn)
10165 && DECL_TEMPLATE_INFO (fn)
10166 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10167 --processing_template_decl;
10168 return ret;
10169 }
10170
10171 /* Returns true if T depends on any template parameter with level LEVEL. */
10172
10173 bool
10174 uses_template_parms_level (tree t, int level)
10175 {
10176 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10177 /*include_nondeduced_p=*/true);
10178 }
10179
10180 /* Returns true if the signature of DECL depends on any template parameter from
10181 its enclosing class. */
10182
10183 bool
10184 uses_outer_template_parms (tree decl)
10185 {
10186 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10187 if (depth == 0)
10188 return false;
10189 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10190 &depth, NULL, /*include_nondeduced_p=*/true))
10191 return true;
10192 if (PRIMARY_TEMPLATE_P (decl)
10193 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10194 (DECL_TEMPLATE_PARMS (decl)),
10195 template_parm_outer_level,
10196 &depth, NULL, /*include_nondeduced_p=*/true))
10197 return true;
10198 tree ci = get_constraints (decl);
10199 if (ci)
10200 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10201 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10202 &depth, NULL, /*nondeduced*/true))
10203 return true;
10204 return false;
10205 }
10206
10207 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10208 ill-formed translation unit, i.e. a variable or function that isn't
10209 usable in a constant expression. */
10210
10211 static inline bool
10212 neglectable_inst_p (tree d)
10213 {
10214 return (d && DECL_P (d)
10215 && !undeduced_auto_decl (d)
10216 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10217 : decl_maybe_constant_var_p (d)));
10218 }
10219
10220 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10221 neglectable and instantiated from within an erroneous instantiation. */
10222
10223 static bool
10224 limit_bad_template_recursion (tree decl)
10225 {
10226 struct tinst_level *lev = current_tinst_level;
10227 int errs = errorcount + sorrycount;
10228 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10229 return false;
10230
10231 for (; lev; lev = lev->next)
10232 if (neglectable_inst_p (lev->maybe_get_node ()))
10233 break;
10234
10235 return (lev && errs > lev->errors);
10236 }
10237
10238 static int tinst_depth;
10239 extern int max_tinst_depth;
10240 int depth_reached;
10241
10242 static GTY(()) struct tinst_level *last_error_tinst_level;
10243
10244 /* We're starting to instantiate D; record the template instantiation context
10245 at LOC for diagnostics and to restore it later. */
10246
10247 static bool
10248 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10249 {
10250 struct tinst_level *new_level;
10251
10252 if (tinst_depth >= max_tinst_depth)
10253 {
10254 /* Tell error.c not to try to instantiate any templates. */
10255 at_eof = 2;
10256 fatal_error (input_location,
10257 "template instantiation depth exceeds maximum of %d"
10258 " (use %<-ftemplate-depth=%> to increase the maximum)",
10259 max_tinst_depth);
10260 return false;
10261 }
10262
10263 /* If the current instantiation caused problems, don't let it instantiate
10264 anything else. Do allow deduction substitution and decls usable in
10265 constant expressions. */
10266 if (!targs && limit_bad_template_recursion (tldcl))
10267 return false;
10268
10269 /* When not -quiet, dump template instantiations other than functions, since
10270 announce_function will take care of those. */
10271 if (!quiet_flag && !targs
10272 && TREE_CODE (tldcl) != TREE_LIST
10273 && TREE_CODE (tldcl) != FUNCTION_DECL)
10274 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10275
10276 new_level = tinst_level_freelist ().alloc ();
10277 new_level->tldcl = tldcl;
10278 new_level->targs = targs;
10279 new_level->locus = loc;
10280 new_level->errors = errorcount + sorrycount;
10281 new_level->next = NULL;
10282 new_level->refcount = 0;
10283 set_refcount_ptr (new_level->next, current_tinst_level);
10284 set_refcount_ptr (current_tinst_level, new_level);
10285
10286 ++tinst_depth;
10287 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10288 depth_reached = tinst_depth;
10289
10290 return true;
10291 }
10292
10293 /* We're starting substitution of TMPL<ARGS>; record the template
10294 substitution context for diagnostics and to restore it later. */
10295
10296 static bool
10297 push_tinst_level (tree tmpl, tree args)
10298 {
10299 return push_tinst_level_loc (tmpl, args, input_location);
10300 }
10301
10302 /* We're starting to instantiate D; record INPUT_LOCATION and the
10303 template instantiation context for diagnostics and to restore it
10304 later. */
10305
10306 bool
10307 push_tinst_level (tree d)
10308 {
10309 return push_tinst_level_loc (d, input_location);
10310 }
10311
10312 /* Likewise, but record LOC as the program location. */
10313
10314 bool
10315 push_tinst_level_loc (tree d, location_t loc)
10316 {
10317 gcc_assert (TREE_CODE (d) != TREE_LIST);
10318 return push_tinst_level_loc (d, NULL, loc);
10319 }
10320
10321 /* We're done instantiating this template; return to the instantiation
10322 context. */
10323
10324 void
10325 pop_tinst_level (void)
10326 {
10327 /* Restore the filename and line number stashed away when we started
10328 this instantiation. */
10329 input_location = current_tinst_level->locus;
10330 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10331 --tinst_depth;
10332 }
10333
10334 /* We're instantiating a deferred template; restore the template
10335 instantiation context in which the instantiation was requested, which
10336 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10337
10338 static tree
10339 reopen_tinst_level (struct tinst_level *level)
10340 {
10341 struct tinst_level *t;
10342
10343 tinst_depth = 0;
10344 for (t = level; t; t = t->next)
10345 ++tinst_depth;
10346
10347 set_refcount_ptr (current_tinst_level, level);
10348 pop_tinst_level ();
10349 if (current_tinst_level)
10350 current_tinst_level->errors = errorcount+sorrycount;
10351 return level->maybe_get_node ();
10352 }
10353
10354 /* Returns the TINST_LEVEL which gives the original instantiation
10355 context. */
10356
10357 struct tinst_level *
10358 outermost_tinst_level (void)
10359 {
10360 struct tinst_level *level = current_tinst_level;
10361 if (level)
10362 while (level->next)
10363 level = level->next;
10364 return level;
10365 }
10366
10367 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10368 vector of template arguments, as for tsubst.
10369
10370 Returns an appropriate tsubst'd friend declaration. */
10371
10372 static tree
10373 tsubst_friend_function (tree decl, tree args)
10374 {
10375 tree new_friend;
10376
10377 if (TREE_CODE (decl) == FUNCTION_DECL
10378 && DECL_TEMPLATE_INSTANTIATION (decl)
10379 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10380 /* This was a friend declared with an explicit template
10381 argument list, e.g.:
10382
10383 friend void f<>(T);
10384
10385 to indicate that f was a template instantiation, not a new
10386 function declaration. Now, we have to figure out what
10387 instantiation of what template. */
10388 {
10389 tree template_id, arglist, fns;
10390 tree new_args;
10391 tree tmpl;
10392 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10393
10394 /* Friend functions are looked up in the containing namespace scope.
10395 We must enter that scope, to avoid finding member functions of the
10396 current class with same name. */
10397 push_nested_namespace (ns);
10398 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10399 tf_warning_or_error, NULL_TREE,
10400 /*integral_constant_expression_p=*/false);
10401 pop_nested_namespace (ns);
10402 arglist = tsubst (DECL_TI_ARGS (decl), args,
10403 tf_warning_or_error, NULL_TREE);
10404 template_id = lookup_template_function (fns, arglist);
10405
10406 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10407 tmpl = determine_specialization (template_id, new_friend,
10408 &new_args,
10409 /*need_member_template=*/0,
10410 TREE_VEC_LENGTH (args),
10411 tsk_none);
10412 return instantiate_template (tmpl, new_args, tf_error);
10413 }
10414
10415 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10416
10417 /* The NEW_FRIEND will look like an instantiation, to the
10418 compiler, but is not an instantiation from the point of view of
10419 the language. For example, we might have had:
10420
10421 template <class T> struct S {
10422 template <class U> friend void f(T, U);
10423 };
10424
10425 Then, in S<int>, template <class U> void f(int, U) is not an
10426 instantiation of anything. */
10427 if (new_friend == error_mark_node)
10428 return error_mark_node;
10429
10430 DECL_USE_TEMPLATE (new_friend) = 0;
10431 if (TREE_CODE (decl) == TEMPLATE_DECL)
10432 {
10433 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10434 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10435 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10436 }
10437
10438 /* The mangled name for the NEW_FRIEND is incorrect. The function
10439 is not a template instantiation and should not be mangled like
10440 one. Therefore, we forget the mangling here; we'll recompute it
10441 later if we need it. */
10442 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10443 {
10444 SET_DECL_RTL (new_friend, NULL);
10445 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10446 }
10447
10448 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10449 {
10450 tree old_decl;
10451 tree new_friend_template_info;
10452 tree new_friend_result_template_info;
10453 tree ns;
10454 int new_friend_is_defn;
10455
10456 /* We must save some information from NEW_FRIEND before calling
10457 duplicate decls since that function will free NEW_FRIEND if
10458 possible. */
10459 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10460 new_friend_is_defn =
10461 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10462 (template_for_substitution (new_friend)))
10463 != NULL_TREE);
10464 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10465 {
10466 /* This declaration is a `primary' template. */
10467 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10468
10469 new_friend_result_template_info
10470 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10471 }
10472 else
10473 new_friend_result_template_info = NULL_TREE;
10474
10475 /* Inside pushdecl_namespace_level, we will push into the
10476 current namespace. However, the friend function should go
10477 into the namespace of the template. */
10478 ns = decl_namespace_context (new_friend);
10479 push_nested_namespace (ns);
10480 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10481 pop_nested_namespace (ns);
10482
10483 if (old_decl == error_mark_node)
10484 return error_mark_node;
10485
10486 if (old_decl != new_friend)
10487 {
10488 /* This new friend declaration matched an existing
10489 declaration. For example, given:
10490
10491 template <class T> void f(T);
10492 template <class U> class C {
10493 template <class T> friend void f(T) {}
10494 };
10495
10496 the friend declaration actually provides the definition
10497 of `f', once C has been instantiated for some type. So,
10498 old_decl will be the out-of-class template declaration,
10499 while new_friend is the in-class definition.
10500
10501 But, if `f' was called before this point, the
10502 instantiation of `f' will have DECL_TI_ARGS corresponding
10503 to `T' but not to `U', references to which might appear
10504 in the definition of `f'. Previously, the most general
10505 template for an instantiation of `f' was the out-of-class
10506 version; now it is the in-class version. Therefore, we
10507 run through all specialization of `f', adding to their
10508 DECL_TI_ARGS appropriately. In particular, they need a
10509 new set of outer arguments, corresponding to the
10510 arguments for this class instantiation.
10511
10512 The same situation can arise with something like this:
10513
10514 friend void f(int);
10515 template <class T> class C {
10516 friend void f(T) {}
10517 };
10518
10519 when `C<int>' is instantiated. Now, `f(int)' is defined
10520 in the class. */
10521
10522 if (!new_friend_is_defn)
10523 /* On the other hand, if the in-class declaration does
10524 *not* provide a definition, then we don't want to alter
10525 existing definitions. We can just leave everything
10526 alone. */
10527 ;
10528 else
10529 {
10530 tree new_template = TI_TEMPLATE (new_friend_template_info);
10531 tree new_args = TI_ARGS (new_friend_template_info);
10532
10533 /* Overwrite whatever template info was there before, if
10534 any, with the new template information pertaining to
10535 the declaration. */
10536 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10537
10538 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10539 {
10540 /* We should have called reregister_specialization in
10541 duplicate_decls. */
10542 gcc_assert (retrieve_specialization (new_template,
10543 new_args, 0)
10544 == old_decl);
10545
10546 /* Instantiate it if the global has already been used. */
10547 if (DECL_ODR_USED (old_decl))
10548 instantiate_decl (old_decl, /*defer_ok=*/true,
10549 /*expl_inst_class_mem_p=*/false);
10550 }
10551 else
10552 {
10553 tree t;
10554
10555 /* Indicate that the old function template is a partial
10556 instantiation. */
10557 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10558 = new_friend_result_template_info;
10559
10560 gcc_assert (new_template
10561 == most_general_template (new_template));
10562 gcc_assert (new_template != old_decl);
10563
10564 /* Reassign any specializations already in the hash table
10565 to the new more general template, and add the
10566 additional template args. */
10567 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10568 t != NULL_TREE;
10569 t = TREE_CHAIN (t))
10570 {
10571 tree spec = TREE_VALUE (t);
10572 spec_entry elt;
10573
10574 elt.tmpl = old_decl;
10575 elt.args = DECL_TI_ARGS (spec);
10576 elt.spec = NULL_TREE;
10577
10578 decl_specializations->remove_elt (&elt);
10579
10580 DECL_TI_ARGS (spec)
10581 = add_outermost_template_args (new_args,
10582 DECL_TI_ARGS (spec));
10583
10584 register_specialization
10585 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10586
10587 }
10588 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10589 }
10590 }
10591
10592 /* The information from NEW_FRIEND has been merged into OLD_DECL
10593 by duplicate_decls. */
10594 new_friend = old_decl;
10595 }
10596 }
10597 else
10598 {
10599 tree context = DECL_CONTEXT (new_friend);
10600 bool dependent_p;
10601
10602 /* In the code
10603 template <class T> class C {
10604 template <class U> friend void C1<U>::f (); // case 1
10605 friend void C2<T>::f (); // case 2
10606 };
10607 we only need to make sure CONTEXT is a complete type for
10608 case 2. To distinguish between the two cases, we note that
10609 CONTEXT of case 1 remains dependent type after tsubst while
10610 this isn't true for case 2. */
10611 ++processing_template_decl;
10612 dependent_p = dependent_type_p (context);
10613 --processing_template_decl;
10614
10615 if (!dependent_p
10616 && !complete_type_or_else (context, NULL_TREE))
10617 return error_mark_node;
10618
10619 if (COMPLETE_TYPE_P (context))
10620 {
10621 tree fn = new_friend;
10622 /* do_friend adds the TEMPLATE_DECL for any member friend
10623 template even if it isn't a member template, i.e.
10624 template <class T> friend A<T>::f();
10625 Look through it in that case. */
10626 if (TREE_CODE (fn) == TEMPLATE_DECL
10627 && !PRIMARY_TEMPLATE_P (fn))
10628 fn = DECL_TEMPLATE_RESULT (fn);
10629 /* Check to see that the declaration is really present, and,
10630 possibly obtain an improved declaration. */
10631 fn = check_classfn (context, fn, NULL_TREE);
10632
10633 if (fn)
10634 new_friend = fn;
10635 }
10636 }
10637
10638 return new_friend;
10639 }
10640
10641 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10642 template arguments, as for tsubst.
10643
10644 Returns an appropriate tsubst'd friend type or error_mark_node on
10645 failure. */
10646
10647 static tree
10648 tsubst_friend_class (tree friend_tmpl, tree args)
10649 {
10650 tree tmpl;
10651
10652 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10653 {
10654 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10655 return TREE_TYPE (tmpl);
10656 }
10657
10658 tree context = CP_DECL_CONTEXT (friend_tmpl);
10659 if (TREE_CODE (context) == NAMESPACE_DECL)
10660 push_nested_namespace (context);
10661 else
10662 {
10663 context = tsubst (context, args, tf_error, NULL_TREE);
10664 push_nested_class (context);
10665 }
10666
10667 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10668 /*non_class=*/false, /*block_p=*/false,
10669 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10670
10671 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10672 {
10673 /* The friend template has already been declared. Just
10674 check to see that the declarations match, and install any new
10675 default parameters. We must tsubst the default parameters,
10676 of course. We only need the innermost template parameters
10677 because that is all that redeclare_class_template will look
10678 at. */
10679 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10680 > TMPL_ARGS_DEPTH (args))
10681 {
10682 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10683 args, tf_warning_or_error);
10684 location_t saved_input_location = input_location;
10685 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10686 tree cons = get_constraints (tmpl);
10687 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10688 input_location = saved_input_location;
10689 }
10690 }
10691 else
10692 {
10693 /* The friend template has not already been declared. In this
10694 case, the instantiation of the template class will cause the
10695 injection of this template into the namespace scope. */
10696 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10697
10698 if (tmpl != error_mark_node)
10699 {
10700 /* The new TMPL is not an instantiation of anything, so we
10701 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10702 for the new type because that is supposed to be the
10703 corresponding template decl, i.e., TMPL. */
10704 DECL_USE_TEMPLATE (tmpl) = 0;
10705 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10706 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10707 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10708 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10709
10710 /* It is hidden. */
10711 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10712 DECL_ANTICIPATED (tmpl)
10713 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10714
10715 /* Inject this template into the enclosing namspace scope. */
10716 tmpl = pushdecl_namespace_level (tmpl, true);
10717 }
10718 }
10719
10720 if (TREE_CODE (context) == NAMESPACE_DECL)
10721 pop_nested_namespace (context);
10722 else
10723 pop_nested_class ();
10724
10725 return TREE_TYPE (tmpl);
10726 }
10727
10728 /* Returns zero if TYPE cannot be completed later due to circularity.
10729 Otherwise returns one. */
10730
10731 static int
10732 can_complete_type_without_circularity (tree type)
10733 {
10734 if (type == NULL_TREE || type == error_mark_node)
10735 return 0;
10736 else if (COMPLETE_TYPE_P (type))
10737 return 1;
10738 else if (TREE_CODE (type) == ARRAY_TYPE)
10739 return can_complete_type_without_circularity (TREE_TYPE (type));
10740 else if (CLASS_TYPE_P (type)
10741 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10742 return 0;
10743 else
10744 return 1;
10745 }
10746
10747 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10748 tsubst_flags_t, tree);
10749
10750 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10751 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10752
10753 static tree
10754 tsubst_attribute (tree t, tree *decl_p, tree args,
10755 tsubst_flags_t complain, tree in_decl)
10756 {
10757 gcc_assert (ATTR_IS_DEPENDENT (t));
10758
10759 tree val = TREE_VALUE (t);
10760 if (val == NULL_TREE)
10761 /* Nothing to do. */;
10762 else if ((flag_openmp || flag_openmp_simd)
10763 && is_attribute_p ("omp declare simd",
10764 get_attribute_name (t)))
10765 {
10766 tree clauses = TREE_VALUE (val);
10767 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10768 complain, in_decl);
10769 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10770 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10771 tree parms = DECL_ARGUMENTS (*decl_p);
10772 clauses
10773 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10774 if (clauses)
10775 val = build_tree_list (NULL_TREE, clauses);
10776 else
10777 val = NULL_TREE;
10778 }
10779 /* If the first attribute argument is an identifier, don't
10780 pass it through tsubst. Attributes like mode, format,
10781 cleanup and several target specific attributes expect it
10782 unmodified. */
10783 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10784 {
10785 tree chain
10786 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10787 /*integral_constant_expression_p=*/false);
10788 if (chain != TREE_CHAIN (val))
10789 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10790 }
10791 else if (PACK_EXPANSION_P (val))
10792 {
10793 /* An attribute pack expansion. */
10794 tree purp = TREE_PURPOSE (t);
10795 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10796 if (pack == error_mark_node)
10797 return error_mark_node;
10798 int len = TREE_VEC_LENGTH (pack);
10799 tree list = NULL_TREE;
10800 tree *q = &list;
10801 for (int i = 0; i < len; ++i)
10802 {
10803 tree elt = TREE_VEC_ELT (pack, i);
10804 *q = build_tree_list (purp, elt);
10805 q = &TREE_CHAIN (*q);
10806 }
10807 return list;
10808 }
10809 else
10810 val = tsubst_expr (val, args, complain, in_decl,
10811 /*integral_constant_expression_p=*/false);
10812
10813 if (val != TREE_VALUE (t))
10814 return build_tree_list (TREE_PURPOSE (t), val);
10815 return t;
10816 }
10817
10818 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10819 unchanged or a new TREE_LIST chain. */
10820
10821 static tree
10822 tsubst_attributes (tree attributes, tree args,
10823 tsubst_flags_t complain, tree in_decl)
10824 {
10825 tree last_dep = NULL_TREE;
10826
10827 for (tree t = attributes; t; t = TREE_CHAIN (t))
10828 if (ATTR_IS_DEPENDENT (t))
10829 {
10830 last_dep = t;
10831 attributes = copy_list (attributes);
10832 break;
10833 }
10834
10835 if (last_dep)
10836 for (tree *p = &attributes; *p; )
10837 {
10838 tree t = *p;
10839 if (ATTR_IS_DEPENDENT (t))
10840 {
10841 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10842 if (subst != t)
10843 {
10844 *p = subst;
10845 while (*p)
10846 p = &TREE_CHAIN (*p);
10847 *p = TREE_CHAIN (t);
10848 continue;
10849 }
10850 }
10851 p = &TREE_CHAIN (*p);
10852 }
10853
10854 return attributes;
10855 }
10856
10857 /* Apply any attributes which had to be deferred until instantiation
10858 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10859 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10860
10861 static void
10862 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10863 tree args, tsubst_flags_t complain, tree in_decl)
10864 {
10865 tree last_dep = NULL_TREE;
10866 tree t;
10867 tree *p;
10868
10869 if (attributes == NULL_TREE)
10870 return;
10871
10872 if (DECL_P (*decl_p))
10873 {
10874 if (TREE_TYPE (*decl_p) == error_mark_node)
10875 return;
10876 p = &DECL_ATTRIBUTES (*decl_p);
10877 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10878 to our attributes parameter. */
10879 gcc_assert (*p == attributes);
10880 }
10881 else
10882 {
10883 p = &TYPE_ATTRIBUTES (*decl_p);
10884 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10885 lookup_template_class_1, and should be preserved. */
10886 gcc_assert (*p != attributes);
10887 while (*p)
10888 p = &TREE_CHAIN (*p);
10889 }
10890
10891 for (t = attributes; t; t = TREE_CHAIN (t))
10892 if (ATTR_IS_DEPENDENT (t))
10893 {
10894 last_dep = t;
10895 attributes = copy_list (attributes);
10896 break;
10897 }
10898
10899 *p = attributes;
10900 if (last_dep)
10901 {
10902 tree late_attrs = NULL_TREE;
10903 tree *q = &late_attrs;
10904
10905 for (; *p; )
10906 {
10907 t = *p;
10908 if (ATTR_IS_DEPENDENT (t))
10909 {
10910 *p = TREE_CHAIN (t);
10911 TREE_CHAIN (t) = NULL_TREE;
10912 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10913 while (*q)
10914 q = &TREE_CHAIN (*q);
10915 }
10916 else
10917 p = &TREE_CHAIN (t);
10918 }
10919
10920 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10921 }
10922 }
10923
10924 /* Perform (or defer) access check for typedefs that were referenced
10925 from within the template TMPL code.
10926 This is a subroutine of instantiate_decl and instantiate_class_template.
10927 TMPL is the template to consider and TARGS is the list of arguments of
10928 that template. */
10929
10930 static void
10931 perform_typedefs_access_check (tree tmpl, tree targs)
10932 {
10933 location_t saved_location;
10934 unsigned i;
10935 qualified_typedef_usage_t *iter;
10936
10937 if (!tmpl
10938 || (!CLASS_TYPE_P (tmpl)
10939 && TREE_CODE (tmpl) != FUNCTION_DECL))
10940 return;
10941
10942 saved_location = input_location;
10943 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10944 {
10945 tree type_decl = iter->typedef_decl;
10946 tree type_scope = iter->context;
10947
10948 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10949 continue;
10950
10951 if (uses_template_parms (type_decl))
10952 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10953 if (uses_template_parms (type_scope))
10954 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10955
10956 /* Make access check error messages point to the location
10957 of the use of the typedef. */
10958 input_location = iter->locus;
10959 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10960 type_decl, type_decl,
10961 tf_warning_or_error);
10962 }
10963 input_location = saved_location;
10964 }
10965
10966 static tree
10967 instantiate_class_template_1 (tree type)
10968 {
10969 tree templ, args, pattern, t, member;
10970 tree typedecl;
10971 tree pbinfo;
10972 tree base_list;
10973 unsigned int saved_maximum_field_alignment;
10974 tree fn_context;
10975
10976 if (type == error_mark_node)
10977 return error_mark_node;
10978
10979 if (COMPLETE_OR_OPEN_TYPE_P (type)
10980 || uses_template_parms (type))
10981 return type;
10982
10983 /* Figure out which template is being instantiated. */
10984 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10985 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10986
10987 /* Mark the type as in the process of being defined. */
10988 TYPE_BEING_DEFINED (type) = 1;
10989
10990 /* We may be in the middle of deferred access check. Disable
10991 it now. */
10992 deferring_access_check_sentinel acs (dk_no_deferred);
10993
10994 /* Determine what specialization of the original template to
10995 instantiate. */
10996 t = most_specialized_partial_spec (type, tf_warning_or_error);
10997 if (t == error_mark_node)
10998 return error_mark_node;
10999 else if (t)
11000 {
11001 /* This TYPE is actually an instantiation of a partial
11002 specialization. We replace the innermost set of ARGS with
11003 the arguments appropriate for substitution. For example,
11004 given:
11005
11006 template <class T> struct S {};
11007 template <class T> struct S<T*> {};
11008
11009 and supposing that we are instantiating S<int*>, ARGS will
11010 presently be {int*} -- but we need {int}. */
11011 pattern = TREE_TYPE (t);
11012 args = TREE_PURPOSE (t);
11013 }
11014 else
11015 {
11016 pattern = TREE_TYPE (templ);
11017 args = CLASSTYPE_TI_ARGS (type);
11018 }
11019
11020 /* If the template we're instantiating is incomplete, then clearly
11021 there's nothing we can do. */
11022 if (!COMPLETE_TYPE_P (pattern))
11023 {
11024 /* We can try again later. */
11025 TYPE_BEING_DEFINED (type) = 0;
11026 return type;
11027 }
11028
11029 /* If we've recursively instantiated too many templates, stop. */
11030 if (! push_tinst_level (type))
11031 return type;
11032
11033 int saved_unevaluated_operand = cp_unevaluated_operand;
11034 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11035
11036 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11037 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11038 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11039 fn_context = error_mark_node;
11040 if (!fn_context)
11041 push_to_top_level ();
11042 else
11043 {
11044 cp_unevaluated_operand = 0;
11045 c_inhibit_evaluation_warnings = 0;
11046 }
11047 /* Use #pragma pack from the template context. */
11048 saved_maximum_field_alignment = maximum_field_alignment;
11049 maximum_field_alignment = TYPE_PRECISION (pattern);
11050
11051 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11052
11053 /* Set the input location to the most specialized template definition.
11054 This is needed if tsubsting causes an error. */
11055 typedecl = TYPE_MAIN_DECL (pattern);
11056 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11057 DECL_SOURCE_LOCATION (typedecl);
11058
11059 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11060 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11061 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11062 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11063 if (ANON_AGGR_TYPE_P (pattern))
11064 SET_ANON_AGGR_TYPE_P (type);
11065 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11066 {
11067 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11068 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11069 /* Adjust visibility for template arguments. */
11070 determine_visibility (TYPE_MAIN_DECL (type));
11071 }
11072 if (CLASS_TYPE_P (type))
11073 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11074
11075 pbinfo = TYPE_BINFO (pattern);
11076
11077 /* We should never instantiate a nested class before its enclosing
11078 class; we need to look up the nested class by name before we can
11079 instantiate it, and that lookup should instantiate the enclosing
11080 class. */
11081 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11082 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11083
11084 base_list = NULL_TREE;
11085 if (BINFO_N_BASE_BINFOS (pbinfo))
11086 {
11087 tree pbase_binfo;
11088 tree pushed_scope;
11089 int i;
11090
11091 /* We must enter the scope containing the type, as that is where
11092 the accessibility of types named in dependent bases are
11093 looked up from. */
11094 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11095
11096 /* Substitute into each of the bases to determine the actual
11097 basetypes. */
11098 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11099 {
11100 tree base;
11101 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11102 tree expanded_bases = NULL_TREE;
11103 int idx, len = 1;
11104
11105 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11106 {
11107 expanded_bases =
11108 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11109 args, tf_error, NULL_TREE);
11110 if (expanded_bases == error_mark_node)
11111 continue;
11112
11113 len = TREE_VEC_LENGTH (expanded_bases);
11114 }
11115
11116 for (idx = 0; idx < len; idx++)
11117 {
11118 if (expanded_bases)
11119 /* Extract the already-expanded base class. */
11120 base = TREE_VEC_ELT (expanded_bases, idx);
11121 else
11122 /* Substitute to figure out the base class. */
11123 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11124 NULL_TREE);
11125
11126 if (base == error_mark_node)
11127 continue;
11128
11129 base_list = tree_cons (access, base, base_list);
11130 if (BINFO_VIRTUAL_P (pbase_binfo))
11131 TREE_TYPE (base_list) = integer_type_node;
11132 }
11133 }
11134
11135 /* The list is now in reverse order; correct that. */
11136 base_list = nreverse (base_list);
11137
11138 if (pushed_scope)
11139 pop_scope (pushed_scope);
11140 }
11141 /* Now call xref_basetypes to set up all the base-class
11142 information. */
11143 xref_basetypes (type, base_list);
11144
11145 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11146 (int) ATTR_FLAG_TYPE_IN_PLACE,
11147 args, tf_error, NULL_TREE);
11148 fixup_attribute_variants (type);
11149
11150 /* Now that our base classes are set up, enter the scope of the
11151 class, so that name lookups into base classes, etc. will work
11152 correctly. This is precisely analogous to what we do in
11153 begin_class_definition when defining an ordinary non-template
11154 class, except we also need to push the enclosing classes. */
11155 push_nested_class (type);
11156
11157 /* Now members are processed in the order of declaration. */
11158 for (member = CLASSTYPE_DECL_LIST (pattern);
11159 member; member = TREE_CHAIN (member))
11160 {
11161 tree t = TREE_VALUE (member);
11162
11163 if (TREE_PURPOSE (member))
11164 {
11165 if (TYPE_P (t))
11166 {
11167 if (LAMBDA_TYPE_P (t))
11168 /* A closure type for a lambda in an NSDMI or default argument.
11169 Ignore it; it will be regenerated when needed. */
11170 continue;
11171
11172 /* Build new CLASSTYPE_NESTED_UTDS. */
11173
11174 tree newtag;
11175 bool class_template_p;
11176
11177 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11178 && TYPE_LANG_SPECIFIC (t)
11179 && CLASSTYPE_IS_TEMPLATE (t));
11180 /* If the member is a class template, then -- even after
11181 substitution -- there may be dependent types in the
11182 template argument list for the class. We increment
11183 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11184 that function will assume that no types are dependent
11185 when outside of a template. */
11186 if (class_template_p)
11187 ++processing_template_decl;
11188 newtag = tsubst (t, args, tf_error, NULL_TREE);
11189 if (class_template_p)
11190 --processing_template_decl;
11191 if (newtag == error_mark_node)
11192 continue;
11193
11194 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11195 {
11196 tree name = TYPE_IDENTIFIER (t);
11197
11198 if (class_template_p)
11199 /* Unfortunately, lookup_template_class sets
11200 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11201 instantiation (i.e., for the type of a member
11202 template class nested within a template class.)
11203 This behavior is required for
11204 maybe_process_partial_specialization to work
11205 correctly, but is not accurate in this case;
11206 the TAG is not an instantiation of anything.
11207 (The corresponding TEMPLATE_DECL is an
11208 instantiation, but the TYPE is not.) */
11209 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11210
11211 /* Now, we call pushtag to put this NEWTAG into the scope of
11212 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11213 pushtag calling push_template_decl. We don't have to do
11214 this for enums because it will already have been done in
11215 tsubst_enum. */
11216 if (name)
11217 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11218 pushtag (name, newtag, /*tag_scope=*/ts_current);
11219 }
11220 }
11221 else if (DECL_DECLARES_FUNCTION_P (t))
11222 {
11223 tree r;
11224
11225 if (TREE_CODE (t) == TEMPLATE_DECL)
11226 ++processing_template_decl;
11227 r = tsubst (t, args, tf_error, NULL_TREE);
11228 if (TREE_CODE (t) == TEMPLATE_DECL)
11229 --processing_template_decl;
11230 set_current_access_from_decl (r);
11231 finish_member_declaration (r);
11232 /* Instantiate members marked with attribute used. */
11233 if (r != error_mark_node && DECL_PRESERVE_P (r))
11234 mark_used (r);
11235 if (TREE_CODE (r) == FUNCTION_DECL
11236 && DECL_OMP_DECLARE_REDUCTION_P (r))
11237 cp_check_omp_declare_reduction (r);
11238 }
11239 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11240 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11241 /* A closure type for a lambda in an NSDMI or default argument.
11242 Ignore it; it will be regenerated when needed. */;
11243 else
11244 {
11245 /* Build new TYPE_FIELDS. */
11246 if (TREE_CODE (t) == STATIC_ASSERT)
11247 {
11248 tree condition;
11249
11250 ++c_inhibit_evaluation_warnings;
11251 condition =
11252 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11253 tf_warning_or_error, NULL_TREE,
11254 /*integral_constant_expression_p=*/true);
11255 --c_inhibit_evaluation_warnings;
11256
11257 finish_static_assert (condition,
11258 STATIC_ASSERT_MESSAGE (t),
11259 STATIC_ASSERT_SOURCE_LOCATION (t),
11260 /*member_p=*/true);
11261 }
11262 else if (TREE_CODE (t) != CONST_DECL)
11263 {
11264 tree r;
11265 tree vec = NULL_TREE;
11266 int len = 1;
11267
11268 /* The file and line for this declaration, to
11269 assist in error message reporting. Since we
11270 called push_tinst_level above, we don't need to
11271 restore these. */
11272 input_location = DECL_SOURCE_LOCATION (t);
11273
11274 if (TREE_CODE (t) == TEMPLATE_DECL)
11275 ++processing_template_decl;
11276 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11277 if (TREE_CODE (t) == TEMPLATE_DECL)
11278 --processing_template_decl;
11279
11280 if (TREE_CODE (r) == TREE_VEC)
11281 {
11282 /* A capture pack became multiple fields. */
11283 vec = r;
11284 len = TREE_VEC_LENGTH (vec);
11285 }
11286
11287 for (int i = 0; i < len; ++i)
11288 {
11289 if (vec)
11290 r = TREE_VEC_ELT (vec, i);
11291 if (VAR_P (r))
11292 {
11293 /* In [temp.inst]:
11294
11295 [t]he initialization (and any associated
11296 side-effects) of a static data member does
11297 not occur unless the static data member is
11298 itself used in a way that requires the
11299 definition of the static data member to
11300 exist.
11301
11302 Therefore, we do not substitute into the
11303 initialized for the static data member here. */
11304 finish_static_data_member_decl
11305 (r,
11306 /*init=*/NULL_TREE,
11307 /*init_const_expr_p=*/false,
11308 /*asmspec_tree=*/NULL_TREE,
11309 /*flags=*/0);
11310 /* Instantiate members marked with attribute used. */
11311 if (r != error_mark_node && DECL_PRESERVE_P (r))
11312 mark_used (r);
11313 }
11314 else if (TREE_CODE (r) == FIELD_DECL)
11315 {
11316 /* Determine whether R has a valid type and can be
11317 completed later. If R is invalid, then its type
11318 is replaced by error_mark_node. */
11319 tree rtype = TREE_TYPE (r);
11320 if (can_complete_type_without_circularity (rtype))
11321 complete_type (rtype);
11322
11323 if (!complete_or_array_type_p (rtype))
11324 {
11325 /* If R's type couldn't be completed and
11326 it isn't a flexible array member (whose
11327 type is incomplete by definition) give
11328 an error. */
11329 cxx_incomplete_type_error (r, rtype);
11330 TREE_TYPE (r) = error_mark_node;
11331 }
11332 else if (TREE_CODE (rtype) == ARRAY_TYPE
11333 && TYPE_DOMAIN (rtype) == NULL_TREE
11334 && (TREE_CODE (type) == UNION_TYPE
11335 || TREE_CODE (type) == QUAL_UNION_TYPE))
11336 {
11337 error ("flexible array member %qD in union", r);
11338 TREE_TYPE (r) = error_mark_node;
11339 }
11340 }
11341
11342 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11343 such a thing will already have been added to the field
11344 list by tsubst_enum in finish_member_declaration in the
11345 CLASSTYPE_NESTED_UTDS case above. */
11346 if (!(TREE_CODE (r) == TYPE_DECL
11347 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11348 && DECL_ARTIFICIAL (r)))
11349 {
11350 set_current_access_from_decl (r);
11351 finish_member_declaration (r);
11352 }
11353 }
11354 }
11355 }
11356 }
11357 else
11358 {
11359 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11360 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11361 {
11362 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11363
11364 tree friend_type = t;
11365 bool adjust_processing_template_decl = false;
11366
11367 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11368 {
11369 /* template <class T> friend class C; */
11370 friend_type = tsubst_friend_class (friend_type, args);
11371 adjust_processing_template_decl = true;
11372 }
11373 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11374 {
11375 /* template <class T> friend class C::D; */
11376 friend_type = tsubst (friend_type, args,
11377 tf_warning_or_error, NULL_TREE);
11378 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11379 friend_type = TREE_TYPE (friend_type);
11380 adjust_processing_template_decl = true;
11381 }
11382 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11383 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11384 {
11385 /* This could be either
11386
11387 friend class T::C;
11388
11389 when dependent_type_p is false or
11390
11391 template <class U> friend class T::C;
11392
11393 otherwise. */
11394 /* Bump processing_template_decl in case this is something like
11395 template <class T> friend struct A<T>::B. */
11396 ++processing_template_decl;
11397 friend_type = tsubst (friend_type, args,
11398 tf_warning_or_error, NULL_TREE);
11399 if (dependent_type_p (friend_type))
11400 adjust_processing_template_decl = true;
11401 --processing_template_decl;
11402 }
11403 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11404 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11405 && TYPE_HIDDEN_P (friend_type))
11406 {
11407 /* friend class C;
11408
11409 where C hasn't been declared yet. Let's lookup name
11410 from namespace scope directly, bypassing any name that
11411 come from dependent base class. */
11412 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11413
11414 /* The call to xref_tag_from_type does injection for friend
11415 classes. */
11416 push_nested_namespace (ns);
11417 friend_type =
11418 xref_tag_from_type (friend_type, NULL_TREE,
11419 /*tag_scope=*/ts_current);
11420 pop_nested_namespace (ns);
11421 }
11422 else if (uses_template_parms (friend_type))
11423 /* friend class C<T>; */
11424 friend_type = tsubst (friend_type, args,
11425 tf_warning_or_error, NULL_TREE);
11426 /* Otherwise it's
11427
11428 friend class C;
11429
11430 where C is already declared or
11431
11432 friend class C<int>;
11433
11434 We don't have to do anything in these cases. */
11435
11436 if (adjust_processing_template_decl)
11437 /* Trick make_friend_class into realizing that the friend
11438 we're adding is a template, not an ordinary class. It's
11439 important that we use make_friend_class since it will
11440 perform some error-checking and output cross-reference
11441 information. */
11442 ++processing_template_decl;
11443
11444 if (friend_type != error_mark_node)
11445 make_friend_class (type, friend_type, /*complain=*/false);
11446
11447 if (adjust_processing_template_decl)
11448 --processing_template_decl;
11449 }
11450 else
11451 {
11452 /* Build new DECL_FRIENDLIST. */
11453 tree r;
11454
11455 /* The file and line for this declaration, to
11456 assist in error message reporting. Since we
11457 called push_tinst_level above, we don't need to
11458 restore these. */
11459 input_location = DECL_SOURCE_LOCATION (t);
11460
11461 if (TREE_CODE (t) == TEMPLATE_DECL)
11462 {
11463 ++processing_template_decl;
11464 push_deferring_access_checks (dk_no_check);
11465 }
11466
11467 r = tsubst_friend_function (t, args);
11468 add_friend (type, r, /*complain=*/false);
11469 if (TREE_CODE (t) == TEMPLATE_DECL)
11470 {
11471 pop_deferring_access_checks ();
11472 --processing_template_decl;
11473 }
11474 }
11475 }
11476 }
11477
11478 if (fn_context)
11479 {
11480 /* Restore these before substituting into the lambda capture
11481 initializers. */
11482 cp_unevaluated_operand = saved_unevaluated_operand;
11483 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11484 }
11485
11486 /* Set the file and line number information to whatever is given for
11487 the class itself. This puts error messages involving generated
11488 implicit functions at a predictable point, and the same point
11489 that would be used for non-template classes. */
11490 input_location = DECL_SOURCE_LOCATION (typedecl);
11491
11492 unreverse_member_declarations (type);
11493 finish_struct_1 (type);
11494 TYPE_BEING_DEFINED (type) = 0;
11495
11496 /* We don't instantiate default arguments for member functions. 14.7.1:
11497
11498 The implicit instantiation of a class template specialization causes
11499 the implicit instantiation of the declarations, but not of the
11500 definitions or default arguments, of the class member functions,
11501 member classes, static data members and member templates.... */
11502
11503 /* Some typedefs referenced from within the template code need to be access
11504 checked at template instantiation time, i.e now. These types were
11505 added to the template at parsing time. Let's get those and perform
11506 the access checks then. */
11507 perform_typedefs_access_check (pattern, args);
11508 perform_deferred_access_checks (tf_warning_or_error);
11509 pop_nested_class ();
11510 maximum_field_alignment = saved_maximum_field_alignment;
11511 if (!fn_context)
11512 pop_from_top_level ();
11513 pop_tinst_level ();
11514
11515 /* The vtable for a template class can be emitted in any translation
11516 unit in which the class is instantiated. When there is no key
11517 method, however, finish_struct_1 will already have added TYPE to
11518 the keyed_classes. */
11519 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11520 vec_safe_push (keyed_classes, type);
11521
11522 return type;
11523 }
11524
11525 /* Wrapper for instantiate_class_template_1. */
11526
11527 tree
11528 instantiate_class_template (tree type)
11529 {
11530 tree ret;
11531 timevar_push (TV_TEMPLATE_INST);
11532 ret = instantiate_class_template_1 (type);
11533 timevar_pop (TV_TEMPLATE_INST);
11534 return ret;
11535 }
11536
11537 static tree
11538 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11539 {
11540 tree r;
11541
11542 if (!t)
11543 r = t;
11544 else if (TYPE_P (t))
11545 r = tsubst (t, args, complain, in_decl);
11546 else
11547 {
11548 if (!(complain & tf_warning))
11549 ++c_inhibit_evaluation_warnings;
11550 r = tsubst_expr (t, args, complain, in_decl,
11551 /*integral_constant_expression_p=*/true);
11552 if (!(complain & tf_warning))
11553 --c_inhibit_evaluation_warnings;
11554 }
11555 return r;
11556 }
11557
11558 /* Given a function parameter pack TMPL_PARM and some function parameters
11559 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11560 and set *SPEC_P to point at the next point in the list. */
11561
11562 tree
11563 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11564 {
11565 /* Collect all of the extra "packed" parameters into an
11566 argument pack. */
11567 tree parmvec;
11568 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11569 tree spec_parm = *spec_p;
11570 int i, len;
11571
11572 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11573 if (tmpl_parm
11574 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11575 break;
11576
11577 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11578 parmvec = make_tree_vec (len);
11579 spec_parm = *spec_p;
11580 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11581 {
11582 tree elt = spec_parm;
11583 if (DECL_PACK_P (elt))
11584 elt = make_pack_expansion (elt);
11585 TREE_VEC_ELT (parmvec, i) = elt;
11586 }
11587
11588 /* Build the argument packs. */
11589 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11590 *spec_p = spec_parm;
11591
11592 return argpack;
11593 }
11594
11595 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11596 NONTYPE_ARGUMENT_PACK. */
11597
11598 static tree
11599 make_fnparm_pack (tree spec_parm)
11600 {
11601 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11602 }
11603
11604 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11605 pack expansion with no extra args, 2 if it has extra args, or 0
11606 if it is not a pack expansion. */
11607
11608 static int
11609 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11610 {
11611 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11612 /* We're being called before this happens in tsubst_pack_expansion. */
11613 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11614 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11615 if (i >= TREE_VEC_LENGTH (vec))
11616 return 0;
11617 tree elt = TREE_VEC_ELT (vec, i);
11618 if (DECL_P (elt))
11619 /* A decl pack is itself an expansion. */
11620 elt = TREE_TYPE (elt);
11621 if (!PACK_EXPANSION_P (elt))
11622 return 0;
11623 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11624 return 2;
11625 return 1;
11626 }
11627
11628
11629 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11630
11631 static tree
11632 make_argument_pack_select (tree arg_pack, unsigned index)
11633 {
11634 tree aps = make_node (ARGUMENT_PACK_SELECT);
11635
11636 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11637 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11638
11639 return aps;
11640 }
11641
11642 /* This is a subroutine of tsubst_pack_expansion.
11643
11644 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11645 mechanism to store the (non complete list of) arguments of the
11646 substitution and return a non substituted pack expansion, in order
11647 to wait for when we have enough arguments to really perform the
11648 substitution. */
11649
11650 static bool
11651 use_pack_expansion_extra_args_p (tree parm_packs,
11652 int arg_pack_len,
11653 bool has_empty_arg)
11654 {
11655 /* If one pack has an expansion and another pack has a normal
11656 argument or if one pack has an empty argument and an another
11657 one hasn't then tsubst_pack_expansion cannot perform the
11658 substitution and need to fall back on the
11659 PACK_EXPANSION_EXTRA mechanism. */
11660 if (parm_packs == NULL_TREE)
11661 return false;
11662 else if (has_empty_arg)
11663 return true;
11664
11665 bool has_expansion_arg = false;
11666 for (int i = 0 ; i < arg_pack_len; ++i)
11667 {
11668 bool has_non_expansion_arg = false;
11669 for (tree parm_pack = parm_packs;
11670 parm_pack;
11671 parm_pack = TREE_CHAIN (parm_pack))
11672 {
11673 tree arg = TREE_VALUE (parm_pack);
11674
11675 int exp = argument_pack_element_is_expansion_p (arg, i);
11676 if (exp == 2)
11677 /* We can't substitute a pack expansion with extra args into
11678 our pattern. */
11679 return true;
11680 else if (exp)
11681 has_expansion_arg = true;
11682 else
11683 has_non_expansion_arg = true;
11684 }
11685
11686 if (has_expansion_arg && has_non_expansion_arg)
11687 return true;
11688 }
11689 return false;
11690 }
11691
11692 /* [temp.variadic]/6 says that:
11693
11694 The instantiation of a pack expansion [...]
11695 produces a list E1,E2, ..., En, where N is the number of elements
11696 in the pack expansion parameters.
11697
11698 This subroutine of tsubst_pack_expansion produces one of these Ei.
11699
11700 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11701 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11702 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11703 INDEX is the index 'i' of the element Ei to produce. ARGS,
11704 COMPLAIN, and IN_DECL are the same parameters as for the
11705 tsubst_pack_expansion function.
11706
11707 The function returns the resulting Ei upon successful completion,
11708 or error_mark_node.
11709
11710 Note that this function possibly modifies the ARGS parameter, so
11711 it's the responsibility of the caller to restore it. */
11712
11713 static tree
11714 gen_elem_of_pack_expansion_instantiation (tree pattern,
11715 tree parm_packs,
11716 unsigned index,
11717 tree args /* This parm gets
11718 modified. */,
11719 tsubst_flags_t complain,
11720 tree in_decl)
11721 {
11722 tree t;
11723 bool ith_elem_is_expansion = false;
11724
11725 /* For each parameter pack, change the substitution of the parameter
11726 pack to the ith argument in its argument pack, then expand the
11727 pattern. */
11728 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11729 {
11730 tree parm = TREE_PURPOSE (pack);
11731 tree arg_pack = TREE_VALUE (pack);
11732 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11733
11734 ith_elem_is_expansion |=
11735 argument_pack_element_is_expansion_p (arg_pack, index);
11736
11737 /* Select the Ith argument from the pack. */
11738 if (TREE_CODE (parm) == PARM_DECL
11739 || VAR_P (parm)
11740 || TREE_CODE (parm) == FIELD_DECL)
11741 {
11742 if (index == 0)
11743 {
11744 aps = make_argument_pack_select (arg_pack, index);
11745 if (!mark_used (parm, complain) && !(complain & tf_error))
11746 return error_mark_node;
11747 register_local_specialization (aps, parm);
11748 }
11749 else
11750 aps = retrieve_local_specialization (parm);
11751 }
11752 else
11753 {
11754 int idx, level;
11755 template_parm_level_and_index (parm, &level, &idx);
11756
11757 if (index == 0)
11758 {
11759 aps = make_argument_pack_select (arg_pack, index);
11760 /* Update the corresponding argument. */
11761 TMPL_ARG (args, level, idx) = aps;
11762 }
11763 else
11764 /* Re-use the ARGUMENT_PACK_SELECT. */
11765 aps = TMPL_ARG (args, level, idx);
11766 }
11767 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11768 }
11769
11770 // Any local specialization bindings arising from this substitution
11771 // cannot be reused for a different INDEX.
11772 local_specialization_stack lss (lss_copy);
11773
11774 /* Substitute into the PATTERN with the (possibly altered)
11775 arguments. */
11776 if (pattern == in_decl)
11777 /* Expanding a fixed parameter pack from
11778 coerce_template_parameter_pack. */
11779 t = tsubst_decl (pattern, args, complain);
11780 else if (pattern == error_mark_node)
11781 t = error_mark_node;
11782 else if (constraint_p (pattern))
11783 {
11784 if (processing_template_decl)
11785 t = tsubst_constraint (pattern, args, complain, in_decl);
11786 else
11787 t = (constraints_satisfied_p (pattern, args)
11788 ? boolean_true_node : boolean_false_node);
11789 }
11790 else if (!TYPE_P (pattern))
11791 t = tsubst_expr (pattern, args, complain, in_decl,
11792 /*integral_constant_expression_p=*/false);
11793 else
11794 t = tsubst (pattern, args, complain, in_decl);
11795
11796 /* If the Ith argument pack element is a pack expansion, then
11797 the Ith element resulting from the substituting is going to
11798 be a pack expansion as well. */
11799 if (ith_elem_is_expansion)
11800 t = make_pack_expansion (t, complain);
11801
11802 return t;
11803 }
11804
11805 /* When the unexpanded parameter pack in a fold expression expands to an empty
11806 sequence, the value of the expression is as follows; the program is
11807 ill-formed if the operator is not listed in this table.
11808
11809 && true
11810 || false
11811 , void() */
11812
11813 tree
11814 expand_empty_fold (tree t, tsubst_flags_t complain)
11815 {
11816 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11817 if (!FOLD_EXPR_MODIFY_P (t))
11818 switch (code)
11819 {
11820 case TRUTH_ANDIF_EXPR:
11821 return boolean_true_node;
11822 case TRUTH_ORIF_EXPR:
11823 return boolean_false_node;
11824 case COMPOUND_EXPR:
11825 return void_node;
11826 default:
11827 break;
11828 }
11829
11830 if (complain & tf_error)
11831 error_at (location_of (t),
11832 "fold of empty expansion over %O", code);
11833 return error_mark_node;
11834 }
11835
11836 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11837 form an expression that combines the two terms using the
11838 operator of T. */
11839
11840 static tree
11841 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11842 {
11843 tree op = FOLD_EXPR_OP (t);
11844 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11845
11846 // Handle compound assignment operators.
11847 if (FOLD_EXPR_MODIFY_P (t))
11848 return build_x_modify_expr (input_location, left, code, right, complain);
11849
11850 switch (code)
11851 {
11852 case COMPOUND_EXPR:
11853 return build_x_compound_expr (input_location, left, right, complain);
11854 default:
11855 return build_x_binary_op (input_location, code,
11856 left, TREE_CODE (left),
11857 right, TREE_CODE (right),
11858 /*overload=*/NULL,
11859 complain);
11860 }
11861 }
11862
11863 /* Substitute ARGS into the pack of a fold expression T. */
11864
11865 static inline tree
11866 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11867 {
11868 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11869 }
11870
11871 /* Substitute ARGS into the pack of a fold expression T. */
11872
11873 static inline tree
11874 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11875 {
11876 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11877 }
11878
11879 /* Expand a PACK of arguments into a grouped as left fold.
11880 Given a pack containing elements A0, A1, ..., An and an
11881 operator @, this builds the expression:
11882
11883 ((A0 @ A1) @ A2) ... @ An
11884
11885 Note that PACK must not be empty.
11886
11887 The operator is defined by the original fold expression T. */
11888
11889 static tree
11890 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11891 {
11892 tree left = TREE_VEC_ELT (pack, 0);
11893 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11894 {
11895 tree right = TREE_VEC_ELT (pack, i);
11896 left = fold_expression (t, left, right, complain);
11897 }
11898 return left;
11899 }
11900
11901 /* Substitute into a unary left fold expression. */
11902
11903 static tree
11904 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11905 tree in_decl)
11906 {
11907 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11908 if (pack == error_mark_node)
11909 return error_mark_node;
11910 if (PACK_EXPANSION_P (pack))
11911 {
11912 tree r = copy_node (t);
11913 FOLD_EXPR_PACK (r) = pack;
11914 return r;
11915 }
11916 if (TREE_VEC_LENGTH (pack) == 0)
11917 return expand_empty_fold (t, complain);
11918 else
11919 return expand_left_fold (t, pack, complain);
11920 }
11921
11922 /* Substitute into a binary left fold expression.
11923
11924 Do ths by building a single (non-empty) vector of argumnts and
11925 building the expression from those elements. */
11926
11927 static tree
11928 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11929 tree in_decl)
11930 {
11931 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11932 if (pack == error_mark_node)
11933 return error_mark_node;
11934 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11935 if (init == error_mark_node)
11936 return error_mark_node;
11937
11938 if (PACK_EXPANSION_P (pack))
11939 {
11940 tree r = copy_node (t);
11941 FOLD_EXPR_PACK (r) = pack;
11942 FOLD_EXPR_INIT (r) = init;
11943 return r;
11944 }
11945
11946 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11947 TREE_VEC_ELT (vec, 0) = init;
11948 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11949 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11950
11951 return expand_left_fold (t, vec, complain);
11952 }
11953
11954 /* Expand a PACK of arguments into a grouped as right fold.
11955 Given a pack containing elementns A0, A1, ..., and an
11956 operator @, this builds the expression:
11957
11958 A0@ ... (An-2 @ (An-1 @ An))
11959
11960 Note that PACK must not be empty.
11961
11962 The operator is defined by the original fold expression T. */
11963
11964 tree
11965 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11966 {
11967 // Build the expression.
11968 int n = TREE_VEC_LENGTH (pack);
11969 tree right = TREE_VEC_ELT (pack, n - 1);
11970 for (--n; n != 0; --n)
11971 {
11972 tree left = TREE_VEC_ELT (pack, n - 1);
11973 right = fold_expression (t, left, right, complain);
11974 }
11975 return right;
11976 }
11977
11978 /* Substitute into a unary right fold expression. */
11979
11980 static tree
11981 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11982 tree in_decl)
11983 {
11984 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11985 if (pack == error_mark_node)
11986 return error_mark_node;
11987 if (PACK_EXPANSION_P (pack))
11988 {
11989 tree r = copy_node (t);
11990 FOLD_EXPR_PACK (r) = pack;
11991 return r;
11992 }
11993 if (TREE_VEC_LENGTH (pack) == 0)
11994 return expand_empty_fold (t, complain);
11995 else
11996 return expand_right_fold (t, pack, complain);
11997 }
11998
11999 /* Substitute into a binary right fold expression.
12000
12001 Do ths by building a single (non-empty) vector of arguments and
12002 building the expression from those elements. */
12003
12004 static tree
12005 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12006 tree in_decl)
12007 {
12008 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12009 if (pack == error_mark_node)
12010 return error_mark_node;
12011 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12012 if (init == error_mark_node)
12013 return error_mark_node;
12014
12015 if (PACK_EXPANSION_P (pack))
12016 {
12017 tree r = copy_node (t);
12018 FOLD_EXPR_PACK (r) = pack;
12019 FOLD_EXPR_INIT (r) = init;
12020 return r;
12021 }
12022
12023 int n = TREE_VEC_LENGTH (pack);
12024 tree vec = make_tree_vec (n + 1);
12025 for (int i = 0; i < n; ++i)
12026 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12027 TREE_VEC_ELT (vec, n) = init;
12028
12029 return expand_right_fold (t, vec, complain);
12030 }
12031
12032 /* Walk through the pattern of a pack expansion, adding everything in
12033 local_specializations to a list. */
12034
12035 struct el_data
12036 {
12037 hash_set<tree> internal;
12038 tree extra;
12039 tsubst_flags_t complain;
12040
12041 el_data (tsubst_flags_t c)
12042 : extra (NULL_TREE), complain (c) {}
12043 };
12044 static tree
12045 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12046 {
12047 el_data &data = *reinterpret_cast<el_data*>(data_);
12048 tree *extra = &data.extra;
12049 tsubst_flags_t complain = data.complain;
12050
12051 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12052 /* Remember local typedefs (85214). */
12053 tp = &TYPE_NAME (*tp);
12054
12055 if (TREE_CODE (*tp) == DECL_EXPR)
12056 data.internal.add (DECL_EXPR_DECL (*tp));
12057 else if (tree spec = retrieve_local_specialization (*tp))
12058 {
12059 if (data.internal.contains (*tp))
12060 /* Don't mess with variables declared within the pattern. */
12061 return NULL_TREE;
12062 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12063 {
12064 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12065 tree args = ARGUMENT_PACK_ARGS (spec);
12066 if (TREE_VEC_LENGTH (args) == 1)
12067 {
12068 tree elt = TREE_VEC_ELT (args, 0);
12069 if (PACK_EXPANSION_P (elt))
12070 elt = PACK_EXPANSION_PATTERN (elt);
12071 if (DECL_PACK_P (elt))
12072 spec = elt;
12073 }
12074 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12075 {
12076 /* Handle lambda capture here, since we aren't doing any
12077 substitution now, and so tsubst_copy won't call
12078 process_outer_var_ref. */
12079 tree args = ARGUMENT_PACK_ARGS (spec);
12080 int len = TREE_VEC_LENGTH (args);
12081 for (int i = 0; i < len; ++i)
12082 {
12083 tree arg = TREE_VEC_ELT (args, i);
12084 tree carg = arg;
12085 if (outer_automatic_var_p (arg))
12086 carg = process_outer_var_ref (arg, complain);
12087 if (carg != arg)
12088 {
12089 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12090 proxies. */
12091 if (i == 0)
12092 {
12093 spec = copy_node (spec);
12094 args = copy_node (args);
12095 SET_ARGUMENT_PACK_ARGS (spec, args);
12096 register_local_specialization (spec, *tp);
12097 }
12098 TREE_VEC_ELT (args, i) = carg;
12099 }
12100 }
12101 }
12102 }
12103 if (outer_automatic_var_p (spec))
12104 spec = process_outer_var_ref (spec, complain);
12105 *extra = tree_cons (*tp, spec, *extra);
12106 }
12107 return NULL_TREE;
12108 }
12109 static tree
12110 extract_local_specs (tree pattern, tsubst_flags_t complain)
12111 {
12112 el_data data (complain);
12113 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12114 return data.extra;
12115 }
12116
12117 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12118 for use in PACK_EXPANSION_EXTRA_ARGS. */
12119
12120 tree
12121 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12122 {
12123 tree extra = args;
12124 if (local_specializations)
12125 if (tree locals = extract_local_specs (pattern, complain))
12126 extra = tree_cons (NULL_TREE, extra, locals);
12127 return extra;
12128 }
12129
12130 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12131 normal template args to ARGS. */
12132
12133 tree
12134 add_extra_args (tree extra, tree args)
12135 {
12136 if (extra && TREE_CODE (extra) == TREE_LIST)
12137 {
12138 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12139 {
12140 /* The partial instantiation involved local declarations collected in
12141 extract_local_specs; map from the general template to our local
12142 context. */
12143 tree gen = TREE_PURPOSE (elt);
12144 tree inst = TREE_VALUE (elt);
12145 if (DECL_P (inst))
12146 if (tree local = retrieve_local_specialization (inst))
12147 inst = local;
12148 /* else inst is already a full instantiation of the pack. */
12149 register_local_specialization (inst, gen);
12150 }
12151 gcc_assert (!TREE_PURPOSE (extra));
12152 extra = TREE_VALUE (extra);
12153 }
12154 return add_to_template_args (extra, args);
12155 }
12156
12157 /* Substitute ARGS into T, which is an pack expansion
12158 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12159 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12160 (if only a partial substitution could be performed) or
12161 ERROR_MARK_NODE if there was an error. */
12162 tree
12163 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12164 tree in_decl)
12165 {
12166 tree pattern;
12167 tree pack, packs = NULL_TREE;
12168 bool unsubstituted_packs = false;
12169 bool unsubstituted_fn_pack = false;
12170 int i, len = -1;
12171 tree result;
12172 hash_map<tree, tree> *saved_local_specializations = NULL;
12173 bool need_local_specializations = false;
12174 int levels;
12175
12176 gcc_assert (PACK_EXPANSION_P (t));
12177 pattern = PACK_EXPANSION_PATTERN (t);
12178
12179 /* Add in any args remembered from an earlier partial instantiation. */
12180 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12181
12182 levels = TMPL_ARGS_DEPTH (args);
12183
12184 /* Determine the argument packs that will instantiate the parameter
12185 packs used in the expansion expression. While we're at it,
12186 compute the number of arguments to be expanded and make sure it
12187 is consistent. */
12188 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12189 pack = TREE_CHAIN (pack))
12190 {
12191 tree parm_pack = TREE_VALUE (pack);
12192 tree arg_pack = NULL_TREE;
12193 tree orig_arg = NULL_TREE;
12194 int level = 0;
12195
12196 if (TREE_CODE (parm_pack) == BASES)
12197 {
12198 gcc_assert (parm_pack == pattern);
12199 if (BASES_DIRECT (parm_pack))
12200 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12201 args, complain,
12202 in_decl, false),
12203 complain);
12204 else
12205 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12206 args, complain, in_decl,
12207 false), complain);
12208 }
12209 else if (builtin_pack_call_p (parm_pack))
12210 {
12211 if (parm_pack != pattern)
12212 {
12213 if (complain & tf_error)
12214 sorry ("%qE is not the entire pattern of the pack expansion",
12215 parm_pack);
12216 return error_mark_node;
12217 }
12218 return expand_builtin_pack_call (parm_pack, args,
12219 complain, in_decl);
12220 }
12221 else if (TREE_CODE (parm_pack) == PARM_DECL)
12222 {
12223 /* We know we have correct local_specializations if this
12224 expansion is at function scope, or if we're dealing with a
12225 local parameter in a requires expression; for the latter,
12226 tsubst_requires_expr set it up appropriately. */
12227 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12228 arg_pack = retrieve_local_specialization (parm_pack);
12229 else
12230 /* We can't rely on local_specializations for a parameter
12231 name used later in a function declaration (such as in a
12232 late-specified return type). Even if it exists, it might
12233 have the wrong value for a recursive call. */
12234 need_local_specializations = true;
12235
12236 if (!arg_pack)
12237 {
12238 /* This parameter pack was used in an unevaluated context. Just
12239 make a dummy decl, since it's only used for its type. */
12240 ++cp_unevaluated_operand;
12241 arg_pack = tsubst_decl (parm_pack, args, complain);
12242 --cp_unevaluated_operand;
12243 if (arg_pack && DECL_PACK_P (arg_pack))
12244 /* Partial instantiation of the parm_pack, we can't build
12245 up an argument pack yet. */
12246 arg_pack = NULL_TREE;
12247 else
12248 arg_pack = make_fnparm_pack (arg_pack);
12249 }
12250 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12251 /* This argument pack isn't fully instantiated yet. We set this
12252 flag rather than clear arg_pack because we do want to do the
12253 optimization below, and we don't want to substitute directly
12254 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12255 where it isn't expected). */
12256 unsubstituted_fn_pack = true;
12257 }
12258 else if (is_capture_proxy (parm_pack))
12259 {
12260 arg_pack = retrieve_local_specialization (parm_pack);
12261 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12262 unsubstituted_fn_pack = true;
12263 }
12264 else
12265 {
12266 int idx;
12267 template_parm_level_and_index (parm_pack, &level, &idx);
12268
12269 if (level <= levels)
12270 arg_pack = TMPL_ARG (args, level, idx);
12271 }
12272
12273 orig_arg = arg_pack;
12274 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12275 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12276
12277 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12278 /* This can only happen if we forget to expand an argument
12279 pack somewhere else. Just return an error, silently. */
12280 {
12281 result = make_tree_vec (1);
12282 TREE_VEC_ELT (result, 0) = error_mark_node;
12283 return result;
12284 }
12285
12286 if (arg_pack)
12287 {
12288 int my_len =
12289 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12290
12291 /* Don't bother trying to do a partial substitution with
12292 incomplete packs; we'll try again after deduction. */
12293 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12294 return t;
12295
12296 if (len < 0)
12297 len = my_len;
12298 else if (len != my_len
12299 && !unsubstituted_fn_pack)
12300 {
12301 if (!(complain & tf_error))
12302 /* Fail quietly. */;
12303 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12304 error ("mismatched argument pack lengths while expanding %qT",
12305 pattern);
12306 else
12307 error ("mismatched argument pack lengths while expanding %qE",
12308 pattern);
12309 return error_mark_node;
12310 }
12311
12312 /* Keep track of the parameter packs and their corresponding
12313 argument packs. */
12314 packs = tree_cons (parm_pack, arg_pack, packs);
12315 TREE_TYPE (packs) = orig_arg;
12316 }
12317 else
12318 {
12319 /* We can't substitute for this parameter pack. We use a flag as
12320 well as the missing_level counter because function parameter
12321 packs don't have a level. */
12322 gcc_assert (processing_template_decl || is_auto (parm_pack));
12323 unsubstituted_packs = true;
12324 }
12325 }
12326
12327 /* If the expansion is just T..., return the matching argument pack, unless
12328 we need to call convert_from_reference on all the elements. This is an
12329 important optimization; see c++/68422. */
12330 if (!unsubstituted_packs
12331 && TREE_PURPOSE (packs) == pattern)
12332 {
12333 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12334
12335 /* If the argument pack is a single pack expansion, pull it out. */
12336 if (TREE_VEC_LENGTH (args) == 1
12337 && pack_expansion_args_count (args))
12338 return TREE_VEC_ELT (args, 0);
12339
12340 /* Types need no adjustment, nor does sizeof..., and if we still have
12341 some pack expansion args we won't do anything yet. */
12342 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12343 || PACK_EXPANSION_SIZEOF_P (t)
12344 || pack_expansion_args_count (args))
12345 return args;
12346 /* Also optimize expression pack expansions if we can tell that the
12347 elements won't have reference type. */
12348 tree type = TREE_TYPE (pattern);
12349 if (type && !TYPE_REF_P (type)
12350 && !PACK_EXPANSION_P (type)
12351 && !WILDCARD_TYPE_P (type))
12352 return args;
12353 /* Otherwise use the normal path so we get convert_from_reference. */
12354 }
12355
12356 /* We cannot expand this expansion expression, because we don't have
12357 all of the argument packs we need. */
12358 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12359 {
12360 /* We got some full packs, but we can't substitute them in until we
12361 have values for all the packs. So remember these until then. */
12362
12363 t = make_pack_expansion (pattern, complain);
12364 PACK_EXPANSION_EXTRA_ARGS (t)
12365 = build_extra_args (pattern, args, complain);
12366 return t;
12367 }
12368 else if (unsubstituted_packs)
12369 {
12370 /* There were no real arguments, we're just replacing a parameter
12371 pack with another version of itself. Substitute into the
12372 pattern and return a PACK_EXPANSION_*. The caller will need to
12373 deal with that. */
12374 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12375 t = tsubst_expr (pattern, args, complain, in_decl,
12376 /*integral_constant_expression_p=*/false);
12377 else
12378 t = tsubst (pattern, args, complain, in_decl);
12379 t = make_pack_expansion (t, complain);
12380 return t;
12381 }
12382
12383 gcc_assert (len >= 0);
12384
12385 if (need_local_specializations)
12386 {
12387 /* We're in a late-specified return type, so create our own local
12388 specializations map; the current map is either NULL or (in the
12389 case of recursive unification) might have bindings that we don't
12390 want to use or alter. */
12391 saved_local_specializations = local_specializations;
12392 local_specializations = new hash_map<tree, tree>;
12393 }
12394
12395 /* For each argument in each argument pack, substitute into the
12396 pattern. */
12397 result = make_tree_vec (len);
12398 tree elem_args = copy_template_args (args);
12399 for (i = 0; i < len; ++i)
12400 {
12401 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12402 i,
12403 elem_args, complain,
12404 in_decl);
12405 TREE_VEC_ELT (result, i) = t;
12406 if (t == error_mark_node)
12407 {
12408 result = error_mark_node;
12409 break;
12410 }
12411 }
12412
12413 /* Update ARGS to restore the substitution from parameter packs to
12414 their argument packs. */
12415 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12416 {
12417 tree parm = TREE_PURPOSE (pack);
12418
12419 if (TREE_CODE (parm) == PARM_DECL
12420 || VAR_P (parm)
12421 || TREE_CODE (parm) == FIELD_DECL)
12422 register_local_specialization (TREE_TYPE (pack), parm);
12423 else
12424 {
12425 int idx, level;
12426
12427 if (TREE_VALUE (pack) == NULL_TREE)
12428 continue;
12429
12430 template_parm_level_and_index (parm, &level, &idx);
12431
12432 /* Update the corresponding argument. */
12433 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12434 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12435 TREE_TYPE (pack);
12436 else
12437 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12438 }
12439 }
12440
12441 if (need_local_specializations)
12442 {
12443 delete local_specializations;
12444 local_specializations = saved_local_specializations;
12445 }
12446
12447 /* If the dependent pack arguments were such that we end up with only a
12448 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12449 if (len == 1 && TREE_CODE (result) == TREE_VEC
12450 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12451 return TREE_VEC_ELT (result, 0);
12452
12453 return result;
12454 }
12455
12456 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12457 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12458 parameter packs; all parms generated from a function parameter pack will
12459 have the same DECL_PARM_INDEX. */
12460
12461 tree
12462 get_pattern_parm (tree parm, tree tmpl)
12463 {
12464 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12465 tree patparm;
12466
12467 if (DECL_ARTIFICIAL (parm))
12468 {
12469 for (patparm = DECL_ARGUMENTS (pattern);
12470 patparm; patparm = DECL_CHAIN (patparm))
12471 if (DECL_ARTIFICIAL (patparm)
12472 && DECL_NAME (parm) == DECL_NAME (patparm))
12473 break;
12474 }
12475 else
12476 {
12477 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12478 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12479 gcc_assert (DECL_PARM_INDEX (patparm)
12480 == DECL_PARM_INDEX (parm));
12481 }
12482
12483 return patparm;
12484 }
12485
12486 /* Make an argument pack out of the TREE_VEC VEC. */
12487
12488 static tree
12489 make_argument_pack (tree vec)
12490 {
12491 tree pack;
12492 tree elt = TREE_VEC_ELT (vec, 0);
12493 if (TYPE_P (elt))
12494 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12495 else
12496 {
12497 pack = make_node (NONTYPE_ARGUMENT_PACK);
12498 TREE_CONSTANT (pack) = 1;
12499 }
12500 SET_ARGUMENT_PACK_ARGS (pack, vec);
12501 return pack;
12502 }
12503
12504 /* Return an exact copy of template args T that can be modified
12505 independently. */
12506
12507 static tree
12508 copy_template_args (tree t)
12509 {
12510 if (t == error_mark_node)
12511 return t;
12512
12513 int len = TREE_VEC_LENGTH (t);
12514 tree new_vec = make_tree_vec (len);
12515
12516 for (int i = 0; i < len; ++i)
12517 {
12518 tree elt = TREE_VEC_ELT (t, i);
12519 if (elt && TREE_CODE (elt) == TREE_VEC)
12520 elt = copy_template_args (elt);
12521 TREE_VEC_ELT (new_vec, i) = elt;
12522 }
12523
12524 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12525 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12526
12527 return new_vec;
12528 }
12529
12530 /* Substitute ARGS into the vector or list of template arguments T. */
12531
12532 static tree
12533 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12534 {
12535 tree orig_t = t;
12536 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12537 tree *elts;
12538
12539 if (t == error_mark_node)
12540 return error_mark_node;
12541
12542 len = TREE_VEC_LENGTH (t);
12543 elts = XALLOCAVEC (tree, len);
12544
12545 for (i = 0; i < len; i++)
12546 {
12547 tree orig_arg = TREE_VEC_ELT (t, i);
12548 tree new_arg;
12549
12550 if (TREE_CODE (orig_arg) == TREE_VEC)
12551 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12552 else if (PACK_EXPANSION_P (orig_arg))
12553 {
12554 /* Substitute into an expansion expression. */
12555 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12556
12557 if (TREE_CODE (new_arg) == TREE_VEC)
12558 /* Add to the expanded length adjustment the number of
12559 expanded arguments. We subtract one from this
12560 measurement, because the argument pack expression
12561 itself is already counted as 1 in
12562 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12563 the argument pack is empty. */
12564 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12565 }
12566 else if (ARGUMENT_PACK_P (orig_arg))
12567 {
12568 /* Substitute into each of the arguments. */
12569 new_arg = TYPE_P (orig_arg)
12570 ? cxx_make_type (TREE_CODE (orig_arg))
12571 : make_node (TREE_CODE (orig_arg));
12572
12573 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12574 args, complain, in_decl);
12575 if (pack_args == error_mark_node)
12576 new_arg = error_mark_node;
12577 else
12578 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12579
12580 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12581 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12582 }
12583 else
12584 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12585
12586 if (new_arg == error_mark_node)
12587 return error_mark_node;
12588
12589 elts[i] = new_arg;
12590 if (new_arg != orig_arg)
12591 need_new = 1;
12592 }
12593
12594 if (!need_new)
12595 return t;
12596
12597 /* Make space for the expanded arguments coming from template
12598 argument packs. */
12599 t = make_tree_vec (len + expanded_len_adjust);
12600 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12601 arguments for a member template.
12602 In that case each TREE_VEC in ORIG_T represents a level of template
12603 arguments, and ORIG_T won't carry any non defaulted argument count.
12604 It will rather be the nested TREE_VECs that will carry one.
12605 In other words, ORIG_T carries a non defaulted argument count only
12606 if it doesn't contain any nested TREE_VEC. */
12607 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12608 {
12609 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12610 count += expanded_len_adjust;
12611 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12612 }
12613 for (i = 0, out = 0; i < len; i++)
12614 {
12615 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12616 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12617 && TREE_CODE (elts[i]) == TREE_VEC)
12618 {
12619 int idx;
12620
12621 /* Now expand the template argument pack "in place". */
12622 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12623 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12624 }
12625 else
12626 {
12627 TREE_VEC_ELT (t, out) = elts[i];
12628 out++;
12629 }
12630 }
12631
12632 return t;
12633 }
12634
12635 /* Substitute ARGS into one level PARMS of template parameters. */
12636
12637 static tree
12638 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12639 {
12640 if (parms == error_mark_node)
12641 return error_mark_node;
12642
12643 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12644
12645 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12646 {
12647 tree tuple = TREE_VEC_ELT (parms, i);
12648
12649 if (tuple == error_mark_node)
12650 continue;
12651
12652 TREE_VEC_ELT (new_vec, i) =
12653 tsubst_template_parm (tuple, args, complain);
12654 }
12655
12656 return new_vec;
12657 }
12658
12659 /* Return the result of substituting ARGS into the template parameters
12660 given by PARMS. If there are m levels of ARGS and m + n levels of
12661 PARMS, then the result will contain n levels of PARMS. For
12662 example, if PARMS is `template <class T> template <class U>
12663 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12664 result will be `template <int*, double, class V>'. */
12665
12666 static tree
12667 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12668 {
12669 tree r = NULL_TREE;
12670 tree* new_parms;
12671
12672 /* When substituting into a template, we must set
12673 PROCESSING_TEMPLATE_DECL as the template parameters may be
12674 dependent if they are based on one-another, and the dependency
12675 predicates are short-circuit outside of templates. */
12676 ++processing_template_decl;
12677
12678 for (new_parms = &r;
12679 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12680 new_parms = &(TREE_CHAIN (*new_parms)),
12681 parms = TREE_CHAIN (parms))
12682 {
12683 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12684 args, complain);
12685 *new_parms =
12686 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12687 - TMPL_ARGS_DEPTH (args)),
12688 new_vec, NULL_TREE);
12689 }
12690
12691 --processing_template_decl;
12692
12693 return r;
12694 }
12695
12696 /* Return the result of substituting ARGS into one template parameter
12697 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12698 parameter and which TREE_PURPOSE is the default argument of the
12699 template parameter. */
12700
12701 static tree
12702 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12703 {
12704 tree default_value, parm_decl;
12705
12706 if (args == NULL_TREE
12707 || t == NULL_TREE
12708 || t == error_mark_node)
12709 return t;
12710
12711 gcc_assert (TREE_CODE (t) == TREE_LIST);
12712
12713 default_value = TREE_PURPOSE (t);
12714 parm_decl = TREE_VALUE (t);
12715
12716 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12717 if (TREE_CODE (parm_decl) == PARM_DECL
12718 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12719 parm_decl = error_mark_node;
12720 default_value = tsubst_template_arg (default_value, args,
12721 complain, NULL_TREE);
12722
12723 return build_tree_list (default_value, parm_decl);
12724 }
12725
12726 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12727 type T. If T is not an aggregate or enumeration type, it is
12728 handled as if by tsubst. IN_DECL is as for tsubst. If
12729 ENTERING_SCOPE is nonzero, T is the context for a template which
12730 we are presently tsubst'ing. Return the substituted value. */
12731
12732 static tree
12733 tsubst_aggr_type (tree t,
12734 tree args,
12735 tsubst_flags_t complain,
12736 tree in_decl,
12737 int entering_scope)
12738 {
12739 if (t == NULL_TREE)
12740 return NULL_TREE;
12741
12742 switch (TREE_CODE (t))
12743 {
12744 case RECORD_TYPE:
12745 if (TYPE_PTRMEMFUNC_P (t))
12746 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12747
12748 /* Fall through. */
12749 case ENUMERAL_TYPE:
12750 case UNION_TYPE:
12751 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12752 {
12753 tree argvec;
12754 tree context;
12755 tree r;
12756
12757 /* In "sizeof(X<I>)" we need to evaluate "I". */
12758 cp_evaluated ev;
12759
12760 /* First, determine the context for the type we are looking
12761 up. */
12762 context = TYPE_CONTEXT (t);
12763 if (context && TYPE_P (context))
12764 {
12765 context = tsubst_aggr_type (context, args, complain,
12766 in_decl, /*entering_scope=*/1);
12767 /* If context is a nested class inside a class template,
12768 it may still need to be instantiated (c++/33959). */
12769 context = complete_type (context);
12770 }
12771
12772 /* Then, figure out what arguments are appropriate for the
12773 type we are trying to find. For example, given:
12774
12775 template <class T> struct S;
12776 template <class T, class U> void f(T, U) { S<U> su; }
12777
12778 and supposing that we are instantiating f<int, double>,
12779 then our ARGS will be {int, double}, but, when looking up
12780 S we only want {double}. */
12781 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12782 complain, in_decl);
12783 if (argvec == error_mark_node)
12784 r = error_mark_node;
12785 else
12786 {
12787 r = lookup_template_class (t, argvec, in_decl, context,
12788 entering_scope, complain);
12789 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12790 }
12791
12792 return r;
12793 }
12794 else
12795 /* This is not a template type, so there's nothing to do. */
12796 return t;
12797
12798 default:
12799 return tsubst (t, args, complain, in_decl);
12800 }
12801 }
12802
12803 static GTY((cache)) tree_cache_map *defarg_inst;
12804
12805 /* Substitute into the default argument ARG (a default argument for
12806 FN), which has the indicated TYPE. */
12807
12808 tree
12809 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12810 tsubst_flags_t complain)
12811 {
12812 int errs = errorcount + sorrycount;
12813
12814 /* This can happen in invalid code. */
12815 if (TREE_CODE (arg) == DEFERRED_PARSE)
12816 return arg;
12817
12818 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12819 parm = chain_index (parmnum, parm);
12820 tree parmtype = TREE_TYPE (parm);
12821 if (DECL_BY_REFERENCE (parm))
12822 parmtype = TREE_TYPE (parmtype);
12823 if (parmtype == error_mark_node)
12824 return error_mark_node;
12825
12826 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12827
12828 tree *slot;
12829 if (defarg_inst && (slot = defarg_inst->get (parm)))
12830 return *slot;
12831
12832 /* This default argument came from a template. Instantiate the
12833 default argument here, not in tsubst. In the case of
12834 something like:
12835
12836 template <class T>
12837 struct S {
12838 static T t();
12839 void f(T = t());
12840 };
12841
12842 we must be careful to do name lookup in the scope of S<T>,
12843 rather than in the current class. */
12844 push_to_top_level ();
12845 push_access_scope (fn);
12846 push_deferring_access_checks (dk_no_deferred);
12847 start_lambda_scope (parm);
12848
12849 /* The default argument expression may cause implicitly defined
12850 member functions to be synthesized, which will result in garbage
12851 collection. We must treat this situation as if we were within
12852 the body of function so as to avoid collecting live data on the
12853 stack. */
12854 ++function_depth;
12855 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12856 complain, NULL_TREE,
12857 /*integral_constant_expression_p=*/false);
12858 --function_depth;
12859
12860 finish_lambda_scope ();
12861
12862 /* Make sure the default argument is reasonable. */
12863 arg = check_default_argument (type, arg, complain);
12864
12865 if (errorcount+sorrycount > errs
12866 && (complain & tf_warning_or_error))
12867 inform (input_location,
12868 " when instantiating default argument for call to %qD", fn);
12869
12870 pop_deferring_access_checks ();
12871 pop_access_scope (fn);
12872 pop_from_top_level ();
12873
12874 if (arg != error_mark_node && !cp_unevaluated_operand)
12875 {
12876 if (!defarg_inst)
12877 defarg_inst = tree_cache_map::create_ggc (37);
12878 defarg_inst->put (parm, arg);
12879 }
12880
12881 return arg;
12882 }
12883
12884 /* Substitute into all the default arguments for FN. */
12885
12886 static void
12887 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12888 {
12889 tree arg;
12890 tree tmpl_args;
12891
12892 tmpl_args = DECL_TI_ARGS (fn);
12893
12894 /* If this function is not yet instantiated, we certainly don't need
12895 its default arguments. */
12896 if (uses_template_parms (tmpl_args))
12897 return;
12898 /* Don't do this again for clones. */
12899 if (DECL_CLONED_FUNCTION_P (fn))
12900 return;
12901
12902 int i = 0;
12903 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12904 arg;
12905 arg = TREE_CHAIN (arg), ++i)
12906 if (TREE_PURPOSE (arg))
12907 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12908 TREE_VALUE (arg),
12909 TREE_PURPOSE (arg),
12910 complain);
12911 }
12912
12913 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12914 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12915
12916 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12917
12918 void
12919 store_explicit_specifier (tree v, tree t)
12920 {
12921 if (!explicit_specifier_map)
12922 explicit_specifier_map = tree_cache_map::create_ggc (37);
12923 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12924 explicit_specifier_map->put (v, t);
12925 }
12926
12927 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12928
12929 static tree
12930 lookup_explicit_specifier (tree v)
12931 {
12932 return *explicit_specifier_map->get (v);
12933 }
12934
12935 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12936
12937 static tree
12938 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12939 tree lambda_fntype)
12940 {
12941 tree gen_tmpl, argvec;
12942 hashval_t hash = 0;
12943 tree in_decl = t;
12944
12945 /* Nobody should be tsubst'ing into non-template functions. */
12946 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12947
12948 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12949 {
12950 /* If T is not dependent, just return it. */
12951 if (!uses_template_parms (DECL_TI_ARGS (t))
12952 && !LAMBDA_FUNCTION_P (t))
12953 return t;
12954
12955 /* Calculate the most general template of which R is a
12956 specialization. */
12957 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12958
12959 /* We're substituting a lambda function under tsubst_lambda_expr but not
12960 directly from it; find the matching function we're already inside.
12961 But don't do this if T is a generic lambda with a single level of
12962 template parms, as in that case we're doing a normal instantiation. */
12963 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12964 && (!generic_lambda_fn_p (t)
12965 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12966 return enclosing_instantiation_of (t);
12967
12968 /* Calculate the complete set of arguments used to
12969 specialize R. */
12970 argvec = tsubst_template_args (DECL_TI_ARGS
12971 (DECL_TEMPLATE_RESULT
12972 (DECL_TI_TEMPLATE (t))),
12973 args, complain, in_decl);
12974 if (argvec == error_mark_node)
12975 return error_mark_node;
12976
12977 /* Check to see if we already have this specialization. */
12978 if (!lambda_fntype)
12979 {
12980 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12981 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12982 return spec;
12983 }
12984
12985 /* We can see more levels of arguments than parameters if
12986 there was a specialization of a member template, like
12987 this:
12988
12989 template <class T> struct S { template <class U> void f(); }
12990 template <> template <class U> void S<int>::f(U);
12991
12992 Here, we'll be substituting into the specialization,
12993 because that's where we can find the code we actually
12994 want to generate, but we'll have enough arguments for
12995 the most general template.
12996
12997 We also deal with the peculiar case:
12998
12999 template <class T> struct S {
13000 template <class U> friend void f();
13001 };
13002 template <class U> void f() {}
13003 template S<int>;
13004 template void f<double>();
13005
13006 Here, the ARGS for the instantiation of will be {int,
13007 double}. But, we only need as many ARGS as there are
13008 levels of template parameters in CODE_PATTERN. We are
13009 careful not to get fooled into reducing the ARGS in
13010 situations like:
13011
13012 template <class T> struct S { template <class U> void f(U); }
13013 template <class T> template <> void S<T>::f(int) {}
13014
13015 which we can spot because the pattern will be a
13016 specialization in this case. */
13017 int args_depth = TMPL_ARGS_DEPTH (args);
13018 int parms_depth =
13019 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13020
13021 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13022 args = get_innermost_template_args (args, parms_depth);
13023 }
13024 else
13025 {
13026 /* This special case arises when we have something like this:
13027
13028 template <class T> struct S {
13029 friend void f<int>(int, double);
13030 };
13031
13032 Here, the DECL_TI_TEMPLATE for the friend declaration
13033 will be an IDENTIFIER_NODE. We are being called from
13034 tsubst_friend_function, and we want only to create a
13035 new decl (R) with appropriate types so that we can call
13036 determine_specialization. */
13037 gen_tmpl = NULL_TREE;
13038 argvec = NULL_TREE;
13039 }
13040
13041 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13042 : NULL_TREE);
13043 tree ctx = closure ? closure : DECL_CONTEXT (t);
13044 bool member = ctx && TYPE_P (ctx);
13045
13046 if (member && !closure)
13047 ctx = tsubst_aggr_type (ctx, args,
13048 complain, t, /*entering_scope=*/1);
13049
13050 tree type = (lambda_fntype ? lambda_fntype
13051 : tsubst (TREE_TYPE (t), args,
13052 complain | tf_fndecl_type, in_decl));
13053 if (type == error_mark_node)
13054 return error_mark_node;
13055
13056 /* If we hit excessive deduction depth, the type is bogus even if
13057 it isn't error_mark_node, so don't build a decl. */
13058 if (excessive_deduction_depth)
13059 return error_mark_node;
13060
13061 /* We do NOT check for matching decls pushed separately at this
13062 point, as they may not represent instantiations of this
13063 template, and in any case are considered separate under the
13064 discrete model. */
13065 tree r = copy_decl (t);
13066 DECL_USE_TEMPLATE (r) = 0;
13067 TREE_TYPE (r) = type;
13068 /* Clear out the mangled name and RTL for the instantiation. */
13069 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13070 SET_DECL_RTL (r, NULL);
13071 /* Leave DECL_INITIAL set on deleted instantiations. */
13072 if (!DECL_DELETED_FN (r))
13073 DECL_INITIAL (r) = NULL_TREE;
13074 DECL_CONTEXT (r) = ctx;
13075
13076 /* Handle explicit(dependent-expr). */
13077 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13078 {
13079 tree spec = lookup_explicit_specifier (t);
13080 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13081 /*function_p=*/false,
13082 /*i_c_e_p=*/true);
13083 spec = build_explicit_specifier (spec, complain);
13084 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13085 }
13086
13087 /* OpenMP UDRs have the only argument a reference to the declared
13088 type. We want to diagnose if the declared type is a reference,
13089 which is invalid, but as references to references are usually
13090 quietly merged, diagnose it here. */
13091 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13092 {
13093 tree argtype
13094 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13095 argtype = tsubst (argtype, args, complain, in_decl);
13096 if (TYPE_REF_P (argtype))
13097 error_at (DECL_SOURCE_LOCATION (t),
13098 "reference type %qT in "
13099 "%<#pragma omp declare reduction%>", argtype);
13100 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13101 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13102 argtype);
13103 }
13104
13105 if (member && DECL_CONV_FN_P (r))
13106 /* Type-conversion operator. Reconstruct the name, in
13107 case it's the name of one of the template's parameters. */
13108 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13109
13110 tree parms = DECL_ARGUMENTS (t);
13111 if (closure)
13112 parms = DECL_CHAIN (parms);
13113 parms = tsubst (parms, args, complain, t);
13114 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13115 DECL_CONTEXT (parm) = r;
13116 if (closure)
13117 {
13118 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13119 DECL_CHAIN (tparm) = parms;
13120 parms = tparm;
13121 }
13122 DECL_ARGUMENTS (r) = parms;
13123 DECL_RESULT (r) = NULL_TREE;
13124
13125 TREE_STATIC (r) = 0;
13126 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13127 DECL_EXTERNAL (r) = 1;
13128 /* If this is an instantiation of a function with internal
13129 linkage, we already know what object file linkage will be
13130 assigned to the instantiation. */
13131 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13132 DECL_DEFER_OUTPUT (r) = 0;
13133 DECL_CHAIN (r) = NULL_TREE;
13134 DECL_PENDING_INLINE_INFO (r) = 0;
13135 DECL_PENDING_INLINE_P (r) = 0;
13136 DECL_SAVED_TREE (r) = NULL_TREE;
13137 DECL_STRUCT_FUNCTION (r) = NULL;
13138 TREE_USED (r) = 0;
13139 /* We'll re-clone as appropriate in instantiate_template. */
13140 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13141
13142 /* If we aren't complaining now, return on error before we register
13143 the specialization so that we'll complain eventually. */
13144 if ((complain & tf_error) == 0
13145 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13146 && !grok_op_properties (r, /*complain=*/false))
13147 return error_mark_node;
13148
13149 /* When instantiating a constrained member, substitute
13150 into the constraints to create a new constraint. */
13151 if (tree ci = get_constraints (t))
13152 if (member)
13153 {
13154 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13155 set_constraints (r, ci);
13156 }
13157
13158 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13159 SET_DECL_FRIEND_CONTEXT (r,
13160 tsubst (DECL_FRIEND_CONTEXT (t),
13161 args, complain, in_decl));
13162
13163 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13164 this in the special friend case mentioned above where
13165 GEN_TMPL is NULL. */
13166 if (gen_tmpl && !closure)
13167 {
13168 DECL_TEMPLATE_INFO (r)
13169 = build_template_info (gen_tmpl, argvec);
13170 SET_DECL_IMPLICIT_INSTANTIATION (r);
13171
13172 tree new_r
13173 = register_specialization (r, gen_tmpl, argvec, false, hash);
13174 if (new_r != r)
13175 /* We instantiated this while substituting into
13176 the type earlier (template/friend54.C). */
13177 return new_r;
13178
13179 /* We're not supposed to instantiate default arguments
13180 until they are called, for a template. But, for a
13181 declaration like:
13182
13183 template <class T> void f ()
13184 { extern void g(int i = T()); }
13185
13186 we should do the substitution when the template is
13187 instantiated. We handle the member function case in
13188 instantiate_class_template since the default arguments
13189 might refer to other members of the class. */
13190 if (!member
13191 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13192 && !uses_template_parms (argvec))
13193 tsubst_default_arguments (r, complain);
13194 }
13195 else
13196 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13197
13198 /* Copy the list of befriending classes. */
13199 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13200 *friends;
13201 friends = &TREE_CHAIN (*friends))
13202 {
13203 *friends = copy_node (*friends);
13204 TREE_VALUE (*friends)
13205 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13206 }
13207
13208 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13209 {
13210 maybe_retrofit_in_chrg (r);
13211 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13212 return error_mark_node;
13213 /* If this is an instantiation of a member template, clone it.
13214 If it isn't, that'll be handled by
13215 clone_constructors_and_destructors. */
13216 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13217 clone_function_decl (r, /*update_methods=*/false);
13218 }
13219 else if ((complain & tf_error) != 0
13220 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13221 && !grok_op_properties (r, /*complain=*/true))
13222 return error_mark_node;
13223
13224 /* Possibly limit visibility based on template args. */
13225 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13226 if (DECL_VISIBILITY_SPECIFIED (t))
13227 {
13228 DECL_VISIBILITY_SPECIFIED (r) = 0;
13229 DECL_ATTRIBUTES (r)
13230 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13231 }
13232 determine_visibility (r);
13233 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13234 && !processing_template_decl)
13235 defaulted_late_check (r);
13236
13237 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13238 args, complain, in_decl);
13239 return r;
13240 }
13241
13242 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13243
13244 static tree
13245 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13246 tree lambda_fntype)
13247 {
13248 /* We can get here when processing a member function template,
13249 member class template, or template template parameter. */
13250 tree decl = DECL_TEMPLATE_RESULT (t);
13251 tree in_decl = t;
13252 tree spec;
13253 tree tmpl_args;
13254 tree full_args;
13255 tree r;
13256 hashval_t hash = 0;
13257
13258 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13259 {
13260 /* Template template parameter is treated here. */
13261 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13262 if (new_type == error_mark_node)
13263 r = error_mark_node;
13264 /* If we get a real template back, return it. This can happen in
13265 the context of most_specialized_partial_spec. */
13266 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13267 r = new_type;
13268 else
13269 /* The new TEMPLATE_DECL was built in
13270 reduce_template_parm_level. */
13271 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13272 return r;
13273 }
13274
13275 if (!lambda_fntype)
13276 {
13277 /* We might already have an instance of this template.
13278 The ARGS are for the surrounding class type, so the
13279 full args contain the tsubst'd args for the context,
13280 plus the innermost args from the template decl. */
13281 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13282 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13283 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13284 /* Because this is a template, the arguments will still be
13285 dependent, even after substitution. If
13286 PROCESSING_TEMPLATE_DECL is not set, the dependency
13287 predicates will short-circuit. */
13288 ++processing_template_decl;
13289 full_args = tsubst_template_args (tmpl_args, args,
13290 complain, in_decl);
13291 --processing_template_decl;
13292 if (full_args == error_mark_node)
13293 return error_mark_node;
13294
13295 /* If this is a default template template argument,
13296 tsubst might not have changed anything. */
13297 if (full_args == tmpl_args)
13298 return t;
13299
13300 hash = hash_tmpl_and_args (t, full_args);
13301 spec = retrieve_specialization (t, full_args, hash);
13302 if (spec != NULL_TREE)
13303 {
13304 if (TYPE_P (spec))
13305 /* Type partial instantiations are stored as the type by
13306 lookup_template_class_1, not here as the template. */
13307 spec = CLASSTYPE_TI_TEMPLATE (spec);
13308 return spec;
13309 }
13310 }
13311
13312 /* Make a new template decl. It will be similar to the
13313 original, but will record the current template arguments.
13314 We also create a new function declaration, which is just
13315 like the old one, but points to this new template, rather
13316 than the old one. */
13317 r = copy_decl (t);
13318 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13319 DECL_CHAIN (r) = NULL_TREE;
13320
13321 // Build new template info linking to the original template decl.
13322 if (!lambda_fntype)
13323 {
13324 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13325 SET_DECL_IMPLICIT_INSTANTIATION (r);
13326 }
13327 else
13328 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13329
13330 /* The template parameters for this new template are all the
13331 template parameters for the old template, except the
13332 outermost level of parameters. */
13333 DECL_TEMPLATE_PARMS (r)
13334 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13335 complain);
13336
13337 if (TREE_CODE (decl) == TYPE_DECL
13338 && !TYPE_DECL_ALIAS_P (decl))
13339 {
13340 tree new_type;
13341 ++processing_template_decl;
13342 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13343 --processing_template_decl;
13344 if (new_type == error_mark_node)
13345 return error_mark_node;
13346
13347 TREE_TYPE (r) = new_type;
13348 /* For a partial specialization, we need to keep pointing to
13349 the primary template. */
13350 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13351 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13352 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13353 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13354 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13355 }
13356 else
13357 {
13358 tree new_decl;
13359 ++processing_template_decl;
13360 if (TREE_CODE (decl) == FUNCTION_DECL)
13361 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13362 else
13363 new_decl = tsubst (decl, args, complain, in_decl);
13364 --processing_template_decl;
13365 if (new_decl == error_mark_node)
13366 return error_mark_node;
13367
13368 DECL_TEMPLATE_RESULT (r) = new_decl;
13369 TREE_TYPE (r) = TREE_TYPE (new_decl);
13370 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13371 if (lambda_fntype)
13372 {
13373 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13374 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13375 }
13376 else
13377 {
13378 DECL_TI_TEMPLATE (new_decl) = r;
13379 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13380 }
13381 }
13382
13383 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13384 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13385
13386 if (PRIMARY_TEMPLATE_P (t))
13387 DECL_PRIMARY_TEMPLATE (r) = r;
13388
13389 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13390 && !lambda_fntype)
13391 /* Record this non-type partial instantiation. */
13392 register_specialization (r, t,
13393 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13394 false, hash);
13395
13396 return r;
13397 }
13398
13399 /* True if FN is the op() for a lambda in an uninstantiated template. */
13400
13401 bool
13402 lambda_fn_in_template_p (tree fn)
13403 {
13404 if (!fn || !LAMBDA_FUNCTION_P (fn))
13405 return false;
13406 tree closure = DECL_CONTEXT (fn);
13407 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13408 }
13409
13410 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13411 which the above is true. */
13412
13413 bool
13414 instantiated_lambda_fn_p (tree fn)
13415 {
13416 if (!fn || !LAMBDA_FUNCTION_P (fn))
13417 return false;
13418 tree closure = DECL_CONTEXT (fn);
13419 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13420 return LAMBDA_EXPR_INSTANTIATED (lam);
13421 }
13422
13423 /* We're instantiating a variable from template function TCTX. Return the
13424 corresponding current enclosing scope. This gets complicated because lambda
13425 functions in templates are regenerated rather than instantiated, but generic
13426 lambda functions are subsequently instantiated. */
13427
13428 static tree
13429 enclosing_instantiation_of (tree otctx)
13430 {
13431 tree tctx = otctx;
13432 tree fn = current_function_decl;
13433 int lambda_count = 0;
13434
13435 for (; tctx && (lambda_fn_in_template_p (tctx)
13436 || instantiated_lambda_fn_p (tctx));
13437 tctx = decl_function_context (tctx))
13438 ++lambda_count;
13439 for (; fn; fn = decl_function_context (fn))
13440 {
13441 tree ofn = fn;
13442 int flambda_count = 0;
13443 for (; fn && instantiated_lambda_fn_p (fn);
13444 fn = decl_function_context (fn))
13445 ++flambda_count;
13446 if ((fn && DECL_TEMPLATE_INFO (fn))
13447 ? most_general_template (fn) != most_general_template (tctx)
13448 : fn != tctx)
13449 continue;
13450 if (flambda_count != lambda_count)
13451 {
13452 gcc_assert (flambda_count > lambda_count);
13453 for (; flambda_count > lambda_count; --flambda_count)
13454 ofn = decl_function_context (ofn);
13455 }
13456 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13457 || DECL_CONV_FN_P (ofn));
13458 return ofn;
13459 }
13460 gcc_unreachable ();
13461 }
13462
13463 /* Substitute the ARGS into the T, which is a _DECL. Return the
13464 result of the substitution. Issue error and warning messages under
13465 control of COMPLAIN. */
13466
13467 static tree
13468 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13469 {
13470 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13471 location_t saved_loc;
13472 tree r = NULL_TREE;
13473 tree in_decl = t;
13474 hashval_t hash = 0;
13475
13476 /* Set the filename and linenumber to improve error-reporting. */
13477 saved_loc = input_location;
13478 input_location = DECL_SOURCE_LOCATION (t);
13479
13480 switch (TREE_CODE (t))
13481 {
13482 case TEMPLATE_DECL:
13483 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13484 break;
13485
13486 case FUNCTION_DECL:
13487 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13488 break;
13489
13490 case PARM_DECL:
13491 {
13492 tree type = NULL_TREE;
13493 int i, len = 1;
13494 tree expanded_types = NULL_TREE;
13495 tree prev_r = NULL_TREE;
13496 tree first_r = NULL_TREE;
13497
13498 if (DECL_PACK_P (t))
13499 {
13500 /* If there is a local specialization that isn't a
13501 parameter pack, it means that we're doing a "simple"
13502 substitution from inside tsubst_pack_expansion. Just
13503 return the local specialization (which will be a single
13504 parm). */
13505 tree spec = retrieve_local_specialization (t);
13506 if (spec
13507 && TREE_CODE (spec) == PARM_DECL
13508 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13509 RETURN (spec);
13510
13511 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13512 the parameters in this function parameter pack. */
13513 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13514 complain, in_decl);
13515 if (TREE_CODE (expanded_types) == TREE_VEC)
13516 {
13517 len = TREE_VEC_LENGTH (expanded_types);
13518
13519 /* Zero-length parameter packs are boring. Just substitute
13520 into the chain. */
13521 if (len == 0)
13522 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13523 TREE_CHAIN (t)));
13524 }
13525 else
13526 {
13527 /* All we did was update the type. Make a note of that. */
13528 type = expanded_types;
13529 expanded_types = NULL_TREE;
13530 }
13531 }
13532
13533 /* Loop through all of the parameters we'll build. When T is
13534 a function parameter pack, LEN is the number of expanded
13535 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13536 r = NULL_TREE;
13537 for (i = 0; i < len; ++i)
13538 {
13539 prev_r = r;
13540 r = copy_node (t);
13541 if (DECL_TEMPLATE_PARM_P (t))
13542 SET_DECL_TEMPLATE_PARM_P (r);
13543
13544 if (expanded_types)
13545 /* We're on the Ith parameter of the function parameter
13546 pack. */
13547 {
13548 /* Get the Ith type. */
13549 type = TREE_VEC_ELT (expanded_types, i);
13550
13551 /* Rename the parameter to include the index. */
13552 DECL_NAME (r)
13553 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13554 }
13555 else if (!type)
13556 /* We're dealing with a normal parameter. */
13557 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13558
13559 type = type_decays_to (type);
13560 TREE_TYPE (r) = type;
13561 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13562
13563 if (DECL_INITIAL (r))
13564 {
13565 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13566 DECL_INITIAL (r) = TREE_TYPE (r);
13567 else
13568 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13569 complain, in_decl);
13570 }
13571
13572 DECL_CONTEXT (r) = NULL_TREE;
13573
13574 if (!DECL_TEMPLATE_PARM_P (r))
13575 DECL_ARG_TYPE (r) = type_passed_as (type);
13576
13577 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13578 args, complain, in_decl);
13579
13580 /* Keep track of the first new parameter we
13581 generate. That's what will be returned to the
13582 caller. */
13583 if (!first_r)
13584 first_r = r;
13585
13586 /* Build a proper chain of parameters when substituting
13587 into a function parameter pack. */
13588 if (prev_r)
13589 DECL_CHAIN (prev_r) = r;
13590 }
13591
13592 /* If cp_unevaluated_operand is set, we're just looking for a
13593 single dummy parameter, so don't keep going. */
13594 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13595 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13596 complain, DECL_CHAIN (t));
13597
13598 /* FIRST_R contains the start of the chain we've built. */
13599 r = first_r;
13600 }
13601 break;
13602
13603 case FIELD_DECL:
13604 {
13605 tree type = NULL_TREE;
13606 tree vec = NULL_TREE;
13607 tree expanded_types = NULL_TREE;
13608 int len = 1;
13609
13610 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13611 {
13612 /* This field is a lambda capture pack. Return a TREE_VEC of
13613 the expanded fields to instantiate_class_template_1. */
13614 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13615 complain, in_decl);
13616 if (TREE_CODE (expanded_types) == TREE_VEC)
13617 {
13618 len = TREE_VEC_LENGTH (expanded_types);
13619 vec = make_tree_vec (len);
13620 }
13621 else
13622 {
13623 /* All we did was update the type. Make a note of that. */
13624 type = expanded_types;
13625 expanded_types = NULL_TREE;
13626 }
13627 }
13628
13629 for (int i = 0; i < len; ++i)
13630 {
13631 r = copy_decl (t);
13632 if (expanded_types)
13633 {
13634 type = TREE_VEC_ELT (expanded_types, i);
13635 DECL_NAME (r)
13636 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13637 }
13638 else if (!type)
13639 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13640
13641 if (type == error_mark_node)
13642 RETURN (error_mark_node);
13643 TREE_TYPE (r) = type;
13644 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13645
13646 if (DECL_C_BIT_FIELD (r))
13647 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13648 number of bits. */
13649 DECL_BIT_FIELD_REPRESENTATIVE (r)
13650 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13651 complain, in_decl,
13652 /*integral_constant_expression_p=*/true);
13653 if (DECL_INITIAL (t))
13654 {
13655 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13656 NSDMI in perform_member_init. Still set DECL_INITIAL
13657 so that we know there is one. */
13658 DECL_INITIAL (r) = void_node;
13659 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13660 retrofit_lang_decl (r);
13661 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13662 }
13663 /* We don't have to set DECL_CONTEXT here; it is set by
13664 finish_member_declaration. */
13665 DECL_CHAIN (r) = NULL_TREE;
13666
13667 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13668 args, complain, in_decl);
13669
13670 if (vec)
13671 TREE_VEC_ELT (vec, i) = r;
13672 }
13673
13674 if (vec)
13675 r = vec;
13676 }
13677 break;
13678
13679 case USING_DECL:
13680 /* We reach here only for member using decls. We also need to check
13681 uses_template_parms because DECL_DEPENDENT_P is not set for a
13682 using-declaration that designates a member of the current
13683 instantiation (c++/53549). */
13684 if (DECL_DEPENDENT_P (t)
13685 || uses_template_parms (USING_DECL_SCOPE (t)))
13686 {
13687 tree scope = USING_DECL_SCOPE (t);
13688 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13689 if (PACK_EXPANSION_P (scope))
13690 {
13691 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13692 int len = TREE_VEC_LENGTH (vec);
13693 r = make_tree_vec (len);
13694 for (int i = 0; i < len; ++i)
13695 {
13696 tree escope = TREE_VEC_ELT (vec, i);
13697 tree elt = do_class_using_decl (escope, name);
13698 if (!elt)
13699 {
13700 r = error_mark_node;
13701 break;
13702 }
13703 else
13704 {
13705 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13706 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13707 }
13708 TREE_VEC_ELT (r, i) = elt;
13709 }
13710 }
13711 else
13712 {
13713 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13714 complain, in_decl);
13715 r = do_class_using_decl (inst_scope, name);
13716 if (!r)
13717 r = error_mark_node;
13718 else
13719 {
13720 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13721 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13722 }
13723 }
13724 }
13725 else
13726 {
13727 r = copy_node (t);
13728 DECL_CHAIN (r) = NULL_TREE;
13729 }
13730 break;
13731
13732 case TYPE_DECL:
13733 case VAR_DECL:
13734 {
13735 tree argvec = NULL_TREE;
13736 tree gen_tmpl = NULL_TREE;
13737 tree spec;
13738 tree tmpl = NULL_TREE;
13739 tree ctx;
13740 tree type = NULL_TREE;
13741 bool local_p;
13742
13743 if (TREE_TYPE (t) == error_mark_node)
13744 RETURN (error_mark_node);
13745
13746 if (TREE_CODE (t) == TYPE_DECL
13747 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13748 {
13749 /* If this is the canonical decl, we don't have to
13750 mess with instantiations, and often we can't (for
13751 typename, template type parms and such). Note that
13752 TYPE_NAME is not correct for the above test if
13753 we've copied the type for a typedef. */
13754 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13755 if (type == error_mark_node)
13756 RETURN (error_mark_node);
13757 r = TYPE_NAME (type);
13758 break;
13759 }
13760
13761 /* Check to see if we already have the specialization we
13762 need. */
13763 spec = NULL_TREE;
13764 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13765 {
13766 /* T is a static data member or namespace-scope entity.
13767 We have to substitute into namespace-scope variables
13768 (not just variable templates) because of cases like:
13769
13770 template <class T> void f() { extern T t; }
13771
13772 where the entity referenced is not known until
13773 instantiation time. */
13774 local_p = false;
13775 ctx = DECL_CONTEXT (t);
13776 if (DECL_CLASS_SCOPE_P (t))
13777 {
13778 ctx = tsubst_aggr_type (ctx, args,
13779 complain,
13780 in_decl, /*entering_scope=*/1);
13781 /* If CTX is unchanged, then T is in fact the
13782 specialization we want. That situation occurs when
13783 referencing a static data member within in its own
13784 class. We can use pointer equality, rather than
13785 same_type_p, because DECL_CONTEXT is always
13786 canonical... */
13787 if (ctx == DECL_CONTEXT (t)
13788 /* ... unless T is a member template; in which
13789 case our caller can be willing to create a
13790 specialization of that template represented
13791 by T. */
13792 && !(DECL_TI_TEMPLATE (t)
13793 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13794 spec = t;
13795 }
13796
13797 if (!spec)
13798 {
13799 tmpl = DECL_TI_TEMPLATE (t);
13800 gen_tmpl = most_general_template (tmpl);
13801 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13802 if (argvec != error_mark_node)
13803 argvec = (coerce_innermost_template_parms
13804 (DECL_TEMPLATE_PARMS (gen_tmpl),
13805 argvec, t, complain,
13806 /*all*/true, /*defarg*/true));
13807 if (argvec == error_mark_node)
13808 RETURN (error_mark_node);
13809 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13810 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13811 }
13812 }
13813 else
13814 {
13815 /* A local variable. */
13816 local_p = true;
13817 /* Subsequent calls to pushdecl will fill this in. */
13818 ctx = NULL_TREE;
13819 /* Unless this is a reference to a static variable from an
13820 enclosing function, in which case we need to fill it in now. */
13821 if (TREE_STATIC (t))
13822 {
13823 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13824 if (fn != current_function_decl)
13825 ctx = fn;
13826 }
13827 spec = retrieve_local_specialization (t);
13828 }
13829 /* If we already have the specialization we need, there is
13830 nothing more to do. */
13831 if (spec)
13832 {
13833 r = spec;
13834 break;
13835 }
13836
13837 /* Create a new node for the specialization we need. */
13838 if (type == NULL_TREE)
13839 {
13840 if (is_typedef_decl (t))
13841 type = DECL_ORIGINAL_TYPE (t);
13842 else
13843 type = TREE_TYPE (t);
13844 if (VAR_P (t)
13845 && VAR_HAD_UNKNOWN_BOUND (t)
13846 && type != error_mark_node)
13847 type = strip_array_domain (type);
13848 tree sub_args = args;
13849 if (tree auto_node = type_uses_auto (type))
13850 {
13851 /* Mask off any template args past the variable's context so we
13852 don't replace the auto with an unrelated argument. */
13853 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13854 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13855 if (extra > 0)
13856 /* This should never happen with the new lambda instantiation
13857 model, but keep the handling just in case. */
13858 gcc_assert (!CHECKING_P),
13859 sub_args = strip_innermost_template_args (args, extra);
13860 }
13861 type = tsubst (type, sub_args, complain, in_decl);
13862 /* Substituting the type might have recursively instantiated this
13863 same alias (c++/86171). */
13864 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13865 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13866 {
13867 r = spec;
13868 break;
13869 }
13870 }
13871 r = copy_decl (t);
13872 if (VAR_P (r))
13873 {
13874 DECL_INITIALIZED_P (r) = 0;
13875 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13876 if (type == error_mark_node)
13877 RETURN (error_mark_node);
13878 if (TREE_CODE (type) == FUNCTION_TYPE)
13879 {
13880 /* It may seem that this case cannot occur, since:
13881
13882 typedef void f();
13883 void g() { f x; }
13884
13885 declares a function, not a variable. However:
13886
13887 typedef void f();
13888 template <typename T> void g() { T t; }
13889 template void g<f>();
13890
13891 is an attempt to declare a variable with function
13892 type. */
13893 error ("variable %qD has function type",
13894 /* R is not yet sufficiently initialized, so we
13895 just use its name. */
13896 DECL_NAME (r));
13897 RETURN (error_mark_node);
13898 }
13899 type = complete_type (type);
13900 /* Wait until cp_finish_decl to set this again, to handle
13901 circular dependency (template/instantiate6.C). */
13902 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13903 type = check_var_type (DECL_NAME (r), type);
13904
13905 if (DECL_HAS_VALUE_EXPR_P (t))
13906 {
13907 tree ve = DECL_VALUE_EXPR (t);
13908 ve = tsubst_expr (ve, args, complain, in_decl,
13909 /*constant_expression_p=*/false);
13910 if (REFERENCE_REF_P (ve))
13911 {
13912 gcc_assert (TYPE_REF_P (type));
13913 ve = TREE_OPERAND (ve, 0);
13914 }
13915 SET_DECL_VALUE_EXPR (r, ve);
13916 }
13917 if (CP_DECL_THREAD_LOCAL_P (r)
13918 && !processing_template_decl)
13919 set_decl_tls_model (r, decl_default_tls_model (r));
13920 }
13921 else if (DECL_SELF_REFERENCE_P (t))
13922 SET_DECL_SELF_REFERENCE_P (r);
13923 TREE_TYPE (r) = type;
13924 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13925 DECL_CONTEXT (r) = ctx;
13926 /* Clear out the mangled name and RTL for the instantiation. */
13927 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13928 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13929 SET_DECL_RTL (r, NULL);
13930 /* The initializer must not be expanded until it is required;
13931 see [temp.inst]. */
13932 DECL_INITIAL (r) = NULL_TREE;
13933 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13934 if (VAR_P (r))
13935 {
13936 if (DECL_LANG_SPECIFIC (r))
13937 SET_DECL_DEPENDENT_INIT_P (r, false);
13938
13939 SET_DECL_MODE (r, VOIDmode);
13940
13941 /* Possibly limit visibility based on template args. */
13942 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13943 if (DECL_VISIBILITY_SPECIFIED (t))
13944 {
13945 DECL_VISIBILITY_SPECIFIED (r) = 0;
13946 DECL_ATTRIBUTES (r)
13947 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13948 }
13949 determine_visibility (r);
13950 }
13951
13952 if (!local_p)
13953 {
13954 /* A static data member declaration is always marked
13955 external when it is declared in-class, even if an
13956 initializer is present. We mimic the non-template
13957 processing here. */
13958 DECL_EXTERNAL (r) = 1;
13959 if (DECL_NAMESPACE_SCOPE_P (t))
13960 DECL_NOT_REALLY_EXTERN (r) = 1;
13961
13962 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13963 SET_DECL_IMPLICIT_INSTANTIATION (r);
13964 if (!error_operand_p (r) || (complain & tf_error))
13965 register_specialization (r, gen_tmpl, argvec, false, hash);
13966 }
13967 else
13968 {
13969 if (DECL_LANG_SPECIFIC (r))
13970 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13971 if (!cp_unevaluated_operand)
13972 register_local_specialization (r, t);
13973 }
13974
13975 DECL_CHAIN (r) = NULL_TREE;
13976
13977 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13978 /*flags=*/0,
13979 args, complain, in_decl);
13980
13981 /* Preserve a typedef that names a type. */
13982 if (is_typedef_decl (r) && type != error_mark_node)
13983 {
13984 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13985 set_underlying_type (r);
13986 if (TYPE_DECL_ALIAS_P (r))
13987 /* An alias template specialization can be dependent
13988 even if its underlying type is not. */
13989 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13990 }
13991
13992 layout_decl (r, 0);
13993 }
13994 break;
13995
13996 default:
13997 gcc_unreachable ();
13998 }
13999 #undef RETURN
14000
14001 out:
14002 /* Restore the file and line information. */
14003 input_location = saved_loc;
14004
14005 return r;
14006 }
14007
14008 /* Substitute into the ARG_TYPES of a function type.
14009 If END is a TREE_CHAIN, leave it and any following types
14010 un-substituted. */
14011
14012 static tree
14013 tsubst_arg_types (tree arg_types,
14014 tree args,
14015 tree end,
14016 tsubst_flags_t complain,
14017 tree in_decl)
14018 {
14019 tree remaining_arg_types;
14020 tree type = NULL_TREE;
14021 int i = 1;
14022 tree expanded_args = NULL_TREE;
14023 tree default_arg;
14024
14025 if (!arg_types || arg_types == void_list_node || arg_types == end)
14026 return arg_types;
14027
14028 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14029 args, end, complain, in_decl);
14030 if (remaining_arg_types == error_mark_node)
14031 return error_mark_node;
14032
14033 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14034 {
14035 /* For a pack expansion, perform substitution on the
14036 entire expression. Later on, we'll handle the arguments
14037 one-by-one. */
14038 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14039 args, complain, in_decl);
14040
14041 if (TREE_CODE (expanded_args) == TREE_VEC)
14042 /* So that we'll spin through the parameters, one by one. */
14043 i = TREE_VEC_LENGTH (expanded_args);
14044 else
14045 {
14046 /* We only partially substituted into the parameter
14047 pack. Our type is TYPE_PACK_EXPANSION. */
14048 type = expanded_args;
14049 expanded_args = NULL_TREE;
14050 }
14051 }
14052
14053 while (i > 0) {
14054 --i;
14055
14056 if (expanded_args)
14057 type = TREE_VEC_ELT (expanded_args, i);
14058 else if (!type)
14059 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14060
14061 if (type == error_mark_node)
14062 return error_mark_node;
14063 if (VOID_TYPE_P (type))
14064 {
14065 if (complain & tf_error)
14066 {
14067 error ("invalid parameter type %qT", type);
14068 if (in_decl)
14069 error ("in declaration %q+D", in_decl);
14070 }
14071 return error_mark_node;
14072 }
14073 /* DR 657. */
14074 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14075 return error_mark_node;
14076
14077 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14078 top-level qualifiers as required. */
14079 type = cv_unqualified (type_decays_to (type));
14080
14081 /* We do not substitute into default arguments here. The standard
14082 mandates that they be instantiated only when needed, which is
14083 done in build_over_call. */
14084 default_arg = TREE_PURPOSE (arg_types);
14085
14086 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14087 since the new op() won't have any associated template arguments for us
14088 to refer to later. */
14089 if (lambda_fn_in_template_p (in_decl))
14090 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14091 false/*fn*/, false/*constexpr*/);
14092
14093 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14094 {
14095 /* We've instantiated a template before its default arguments
14096 have been parsed. This can happen for a nested template
14097 class, and is not an error unless we require the default
14098 argument in a call of this function. */
14099 remaining_arg_types =
14100 tree_cons (default_arg, type, remaining_arg_types);
14101 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14102 remaining_arg_types);
14103 }
14104 else
14105 remaining_arg_types =
14106 hash_tree_cons (default_arg, type, remaining_arg_types);
14107 }
14108
14109 return remaining_arg_types;
14110 }
14111
14112 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14113 *not* handle the exception-specification for FNTYPE, because the
14114 initial substitution of explicitly provided template parameters
14115 during argument deduction forbids substitution into the
14116 exception-specification:
14117
14118 [temp.deduct]
14119
14120 All references in the function type of the function template to the
14121 corresponding template parameters are replaced by the specified tem-
14122 plate argument values. If a substitution in a template parameter or
14123 in the function type of the function template results in an invalid
14124 type, type deduction fails. [Note: The equivalent substitution in
14125 exception specifications is done only when the function is instanti-
14126 ated, at which point a program is ill-formed if the substitution
14127 results in an invalid type.] */
14128
14129 static tree
14130 tsubst_function_type (tree t,
14131 tree args,
14132 tsubst_flags_t complain,
14133 tree in_decl)
14134 {
14135 tree return_type;
14136 tree arg_types = NULL_TREE;
14137 tree fntype;
14138
14139 /* The TYPE_CONTEXT is not used for function/method types. */
14140 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14141
14142 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14143 failure. */
14144 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14145
14146 if (late_return_type_p)
14147 {
14148 /* Substitute the argument types. */
14149 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14150 complain, in_decl);
14151 if (arg_types == error_mark_node)
14152 return error_mark_node;
14153
14154 tree save_ccp = current_class_ptr;
14155 tree save_ccr = current_class_ref;
14156 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14157 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14158 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14159 if (do_inject)
14160 {
14161 /* DR 1207: 'this' is in scope in the trailing return type. */
14162 inject_this_parameter (this_type, cp_type_quals (this_type));
14163 }
14164
14165 /* Substitute the return type. */
14166 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14167
14168 if (do_inject)
14169 {
14170 current_class_ptr = save_ccp;
14171 current_class_ref = save_ccr;
14172 }
14173 }
14174 else
14175 /* Substitute the return type. */
14176 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14177
14178 if (return_type == error_mark_node)
14179 return error_mark_node;
14180 /* DR 486 clarifies that creation of a function type with an
14181 invalid return type is a deduction failure. */
14182 if (TREE_CODE (return_type) == ARRAY_TYPE
14183 || TREE_CODE (return_type) == FUNCTION_TYPE)
14184 {
14185 if (complain & tf_error)
14186 {
14187 if (TREE_CODE (return_type) == ARRAY_TYPE)
14188 error ("function returning an array");
14189 else
14190 error ("function returning a function");
14191 }
14192 return error_mark_node;
14193 }
14194 /* And DR 657. */
14195 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14196 return error_mark_node;
14197
14198 if (!late_return_type_p)
14199 {
14200 /* Substitute the argument types. */
14201 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14202 complain, in_decl);
14203 if (arg_types == error_mark_node)
14204 return error_mark_node;
14205 }
14206
14207 /* Construct a new type node and return it. */
14208 if (TREE_CODE (t) == FUNCTION_TYPE)
14209 {
14210 fntype = build_function_type (return_type, arg_types);
14211 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14212 }
14213 else
14214 {
14215 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14216 /* Don't pick up extra function qualifiers from the basetype. */
14217 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14218 if (! MAYBE_CLASS_TYPE_P (r))
14219 {
14220 /* [temp.deduct]
14221
14222 Type deduction may fail for any of the following
14223 reasons:
14224
14225 -- Attempting to create "pointer to member of T" when T
14226 is not a class type. */
14227 if (complain & tf_error)
14228 error ("creating pointer to member function of non-class type %qT",
14229 r);
14230 return error_mark_node;
14231 }
14232
14233 fntype = build_method_type_directly (r, return_type,
14234 TREE_CHAIN (arg_types));
14235 }
14236 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14237
14238 /* See comment above. */
14239 tree raises = NULL_TREE;
14240 cp_ref_qualifier rqual = type_memfn_rqual (t);
14241 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14242
14243 return fntype;
14244 }
14245
14246 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14247 ARGS into that specification, and return the substituted
14248 specification. If there is no specification, return NULL_TREE. */
14249
14250 static tree
14251 tsubst_exception_specification (tree fntype,
14252 tree args,
14253 tsubst_flags_t complain,
14254 tree in_decl,
14255 bool defer_ok)
14256 {
14257 tree specs;
14258 tree new_specs;
14259
14260 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14261 new_specs = NULL_TREE;
14262 if (specs && TREE_PURPOSE (specs))
14263 {
14264 /* A noexcept-specifier. */
14265 tree expr = TREE_PURPOSE (specs);
14266 if (TREE_CODE (expr) == INTEGER_CST)
14267 new_specs = expr;
14268 else if (defer_ok)
14269 {
14270 /* Defer instantiation of noexcept-specifiers to avoid
14271 excessive instantiations (c++/49107). */
14272 new_specs = make_node (DEFERRED_NOEXCEPT);
14273 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14274 {
14275 /* We already partially instantiated this member template,
14276 so combine the new args with the old. */
14277 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14278 = DEFERRED_NOEXCEPT_PATTERN (expr);
14279 DEFERRED_NOEXCEPT_ARGS (new_specs)
14280 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14281 }
14282 else
14283 {
14284 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14285 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14286 }
14287 }
14288 else
14289 {
14290 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14291 {
14292 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14293 args);
14294 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14295 }
14296 new_specs = tsubst_copy_and_build
14297 (expr, args, complain, in_decl, /*function_p=*/false,
14298 /*integral_constant_expression_p=*/true);
14299 }
14300 new_specs = build_noexcept_spec (new_specs, complain);
14301 }
14302 else if (specs)
14303 {
14304 if (! TREE_VALUE (specs))
14305 new_specs = specs;
14306 else
14307 while (specs)
14308 {
14309 tree spec;
14310 int i, len = 1;
14311 tree expanded_specs = NULL_TREE;
14312
14313 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14314 {
14315 /* Expand the pack expansion type. */
14316 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14317 args, complain,
14318 in_decl);
14319
14320 if (expanded_specs == error_mark_node)
14321 return error_mark_node;
14322 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14323 len = TREE_VEC_LENGTH (expanded_specs);
14324 else
14325 {
14326 /* We're substituting into a member template, so
14327 we got a TYPE_PACK_EXPANSION back. Add that
14328 expansion and move on. */
14329 gcc_assert (TREE_CODE (expanded_specs)
14330 == TYPE_PACK_EXPANSION);
14331 new_specs = add_exception_specifier (new_specs,
14332 expanded_specs,
14333 complain);
14334 specs = TREE_CHAIN (specs);
14335 continue;
14336 }
14337 }
14338
14339 for (i = 0; i < len; ++i)
14340 {
14341 if (expanded_specs)
14342 spec = TREE_VEC_ELT (expanded_specs, i);
14343 else
14344 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14345 if (spec == error_mark_node)
14346 return spec;
14347 new_specs = add_exception_specifier (new_specs, spec,
14348 complain);
14349 }
14350
14351 specs = TREE_CHAIN (specs);
14352 }
14353 }
14354 return new_specs;
14355 }
14356
14357 /* Take the tree structure T and replace template parameters used
14358 therein with the argument vector ARGS. IN_DECL is an associated
14359 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14360 Issue error and warning messages under control of COMPLAIN. Note
14361 that we must be relatively non-tolerant of extensions here, in
14362 order to preserve conformance; if we allow substitutions that
14363 should not be allowed, we may allow argument deductions that should
14364 not succeed, and therefore report ambiguous overload situations
14365 where there are none. In theory, we could allow the substitution,
14366 but indicate that it should have failed, and allow our caller to
14367 make sure that the right thing happens, but we don't try to do this
14368 yet.
14369
14370 This function is used for dealing with types, decls and the like;
14371 for expressions, use tsubst_expr or tsubst_copy. */
14372
14373 tree
14374 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14375 {
14376 enum tree_code code;
14377 tree type, r = NULL_TREE;
14378
14379 if (t == NULL_TREE || t == error_mark_node
14380 || t == integer_type_node
14381 || t == void_type_node
14382 || t == char_type_node
14383 || t == unknown_type_node
14384 || TREE_CODE (t) == NAMESPACE_DECL
14385 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14386 return t;
14387
14388 if (DECL_P (t))
14389 return tsubst_decl (t, args, complain);
14390
14391 if (args == NULL_TREE)
14392 return t;
14393
14394 code = TREE_CODE (t);
14395
14396 if (code == IDENTIFIER_NODE)
14397 type = IDENTIFIER_TYPE_VALUE (t);
14398 else
14399 type = TREE_TYPE (t);
14400
14401 gcc_assert (type != unknown_type_node);
14402
14403 /* Reuse typedefs. We need to do this to handle dependent attributes,
14404 such as attribute aligned. */
14405 if (TYPE_P (t)
14406 && typedef_variant_p (t))
14407 {
14408 tree decl = TYPE_NAME (t);
14409
14410 if (alias_template_specialization_p (t))
14411 {
14412 /* DECL represents an alias template and we want to
14413 instantiate it. */
14414 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14415 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14416 r = instantiate_alias_template (tmpl, gen_args, complain);
14417 }
14418 else if (DECL_CLASS_SCOPE_P (decl)
14419 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14420 && uses_template_parms (DECL_CONTEXT (decl)))
14421 {
14422 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14423 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14424 r = retrieve_specialization (tmpl, gen_args, 0);
14425 }
14426 else if (DECL_FUNCTION_SCOPE_P (decl)
14427 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14428 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14429 r = retrieve_local_specialization (decl);
14430 else
14431 /* The typedef is from a non-template context. */
14432 return t;
14433
14434 if (r)
14435 {
14436 r = TREE_TYPE (r);
14437 r = cp_build_qualified_type_real
14438 (r, cp_type_quals (t) | cp_type_quals (r),
14439 complain | tf_ignore_bad_quals);
14440 return r;
14441 }
14442 else
14443 {
14444 /* We don't have an instantiation yet, so drop the typedef. */
14445 int quals = cp_type_quals (t);
14446 t = DECL_ORIGINAL_TYPE (decl);
14447 t = cp_build_qualified_type_real (t, quals,
14448 complain | tf_ignore_bad_quals);
14449 }
14450 }
14451
14452 bool fndecl_type = (complain & tf_fndecl_type);
14453 complain &= ~tf_fndecl_type;
14454
14455 if (type
14456 && code != TYPENAME_TYPE
14457 && code != TEMPLATE_TYPE_PARM
14458 && code != TEMPLATE_PARM_INDEX
14459 && code != IDENTIFIER_NODE
14460 && code != FUNCTION_TYPE
14461 && code != METHOD_TYPE)
14462 type = tsubst (type, args, complain, in_decl);
14463 if (type == error_mark_node)
14464 return error_mark_node;
14465
14466 switch (code)
14467 {
14468 case RECORD_TYPE:
14469 case UNION_TYPE:
14470 case ENUMERAL_TYPE:
14471 return tsubst_aggr_type (t, args, complain, in_decl,
14472 /*entering_scope=*/0);
14473
14474 case ERROR_MARK:
14475 case IDENTIFIER_NODE:
14476 case VOID_TYPE:
14477 case REAL_TYPE:
14478 case COMPLEX_TYPE:
14479 case VECTOR_TYPE:
14480 case BOOLEAN_TYPE:
14481 case NULLPTR_TYPE:
14482 case LANG_TYPE:
14483 return t;
14484
14485 case INTEGER_TYPE:
14486 if (t == integer_type_node)
14487 return t;
14488
14489 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14490 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14491 return t;
14492
14493 {
14494 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14495
14496 max = tsubst_expr (omax, args, complain, in_decl,
14497 /*integral_constant_expression_p=*/false);
14498
14499 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14500 needed. */
14501 if (TREE_CODE (max) == NOP_EXPR
14502 && TREE_SIDE_EFFECTS (omax)
14503 && !TREE_TYPE (max))
14504 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14505
14506 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14507 with TREE_SIDE_EFFECTS that indicates this is not an integral
14508 constant expression. */
14509 if (processing_template_decl
14510 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14511 {
14512 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14513 TREE_SIDE_EFFECTS (max) = 1;
14514 }
14515
14516 return compute_array_index_type (NULL_TREE, max, complain);
14517 }
14518
14519 case TEMPLATE_TYPE_PARM:
14520 case TEMPLATE_TEMPLATE_PARM:
14521 case BOUND_TEMPLATE_TEMPLATE_PARM:
14522 case TEMPLATE_PARM_INDEX:
14523 {
14524 int idx;
14525 int level;
14526 int levels;
14527 tree arg = NULL_TREE;
14528
14529 /* Early in template argument deduction substitution, we don't
14530 want to reduce the level of 'auto', or it will be confused
14531 with a normal template parm in subsequent deduction. */
14532 if (is_auto (t) && (complain & tf_partial))
14533 return t;
14534
14535 r = NULL_TREE;
14536
14537 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14538 template_parm_level_and_index (t, &level, &idx);
14539
14540 levels = TMPL_ARGS_DEPTH (args);
14541 if (level <= levels
14542 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14543 {
14544 arg = TMPL_ARG (args, level, idx);
14545
14546 /* See through ARGUMENT_PACK_SELECT arguments. */
14547 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14548 arg = argument_pack_select_arg (arg);
14549 }
14550
14551 if (arg == error_mark_node)
14552 return error_mark_node;
14553 else if (arg != NULL_TREE)
14554 {
14555 if (ARGUMENT_PACK_P (arg))
14556 /* If ARG is an argument pack, we don't actually want to
14557 perform a substitution here, because substitutions
14558 for argument packs are only done
14559 element-by-element. We can get to this point when
14560 substituting the type of a non-type template
14561 parameter pack, when that type actually contains
14562 template parameter packs from an outer template, e.g.,
14563
14564 template<typename... Types> struct A {
14565 template<Types... Values> struct B { };
14566 }; */
14567 return t;
14568
14569 if (code == TEMPLATE_TYPE_PARM)
14570 {
14571 int quals;
14572 gcc_assert (TYPE_P (arg));
14573
14574 quals = cp_type_quals (arg) | cp_type_quals (t);
14575
14576 return cp_build_qualified_type_real
14577 (arg, quals, complain | tf_ignore_bad_quals);
14578 }
14579 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14580 {
14581 /* We are processing a type constructed from a
14582 template template parameter. */
14583 tree argvec = tsubst (TYPE_TI_ARGS (t),
14584 args, complain, in_decl);
14585 if (argvec == error_mark_node)
14586 return error_mark_node;
14587
14588 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14589 || TREE_CODE (arg) == TEMPLATE_DECL
14590 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14591
14592 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14593 /* Consider this code:
14594
14595 template <template <class> class Template>
14596 struct Internal {
14597 template <class Arg> using Bind = Template<Arg>;
14598 };
14599
14600 template <template <class> class Template, class Arg>
14601 using Instantiate = Template<Arg>; //#0
14602
14603 template <template <class> class Template,
14604 class Argument>
14605 using Bind =
14606 Instantiate<Internal<Template>::template Bind,
14607 Argument>; //#1
14608
14609 When #1 is parsed, the
14610 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14611 parameter `Template' in #0 matches the
14612 UNBOUND_CLASS_TEMPLATE representing the argument
14613 `Internal<Template>::template Bind'; We then want
14614 to assemble the type `Bind<Argument>' that can't
14615 be fully created right now, because
14616 `Internal<Template>' not being complete, the Bind
14617 template cannot be looked up in that context. So
14618 we need to "store" `Bind<Argument>' for later
14619 when the context of Bind becomes complete. Let's
14620 store that in a TYPENAME_TYPE. */
14621 return make_typename_type (TYPE_CONTEXT (arg),
14622 build_nt (TEMPLATE_ID_EXPR,
14623 TYPE_IDENTIFIER (arg),
14624 argvec),
14625 typename_type,
14626 complain);
14627
14628 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14629 are resolving nested-types in the signature of a
14630 member function templates. Otherwise ARG is a
14631 TEMPLATE_DECL and is the real template to be
14632 instantiated. */
14633 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14634 arg = TYPE_NAME (arg);
14635
14636 r = lookup_template_class (arg,
14637 argvec, in_decl,
14638 DECL_CONTEXT (arg),
14639 /*entering_scope=*/0,
14640 complain);
14641 return cp_build_qualified_type_real
14642 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14643 }
14644 else if (code == TEMPLATE_TEMPLATE_PARM)
14645 return arg;
14646 else
14647 /* TEMPLATE_PARM_INDEX. */
14648 return convert_from_reference (unshare_expr (arg));
14649 }
14650
14651 if (level == 1)
14652 /* This can happen during the attempted tsubst'ing in
14653 unify. This means that we don't yet have any information
14654 about the template parameter in question. */
14655 return t;
14656
14657 /* If we get here, we must have been looking at a parm for a
14658 more deeply nested template. Make a new version of this
14659 template parameter, but with a lower level. */
14660 switch (code)
14661 {
14662 case TEMPLATE_TYPE_PARM:
14663 case TEMPLATE_TEMPLATE_PARM:
14664 case BOUND_TEMPLATE_TEMPLATE_PARM:
14665 if (cp_type_quals (t))
14666 {
14667 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14668 r = cp_build_qualified_type_real
14669 (r, cp_type_quals (t),
14670 complain | (code == TEMPLATE_TYPE_PARM
14671 ? tf_ignore_bad_quals : 0));
14672 }
14673 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14674 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14675 && (r = (TEMPLATE_PARM_DESCENDANTS
14676 (TEMPLATE_TYPE_PARM_INDEX (t))))
14677 && (r = TREE_TYPE (r))
14678 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14679 /* Break infinite recursion when substituting the constraints
14680 of a constrained placeholder. */;
14681 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14682 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14683 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14684 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14685 r = TEMPLATE_PARM_DESCENDANTS (arg))
14686 && (TEMPLATE_PARM_LEVEL (r)
14687 == TEMPLATE_PARM_LEVEL (arg) - levels))
14688 /* Cache the simple case of lowering a type parameter. */
14689 r = TREE_TYPE (r);
14690 else
14691 {
14692 r = copy_type (t);
14693 TEMPLATE_TYPE_PARM_INDEX (r)
14694 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14695 r, levels, args, complain);
14696 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14697 TYPE_MAIN_VARIANT (r) = r;
14698 TYPE_POINTER_TO (r) = NULL_TREE;
14699 TYPE_REFERENCE_TO (r) = NULL_TREE;
14700
14701 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14702 {
14703 /* Propagate constraints on placeholders. */
14704 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14705 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14706 = tsubst_constraint (constr, args, complain, in_decl);
14707 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14708 {
14709 pl = tsubst_copy (pl, args, complain, in_decl);
14710 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14711 }
14712 }
14713
14714 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14715 /* We have reduced the level of the template
14716 template parameter, but not the levels of its
14717 template parameters, so canonical_type_parameter
14718 will not be able to find the canonical template
14719 template parameter for this level. Thus, we
14720 require structural equality checking to compare
14721 TEMPLATE_TEMPLATE_PARMs. */
14722 SET_TYPE_STRUCTURAL_EQUALITY (r);
14723 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14724 SET_TYPE_STRUCTURAL_EQUALITY (r);
14725 else
14726 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14727
14728 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14729 {
14730 tree tinfo = TYPE_TEMPLATE_INFO (t);
14731 /* We might need to substitute into the types of non-type
14732 template parameters. */
14733 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14734 complain, in_decl);
14735 if (tmpl == error_mark_node)
14736 return error_mark_node;
14737 tree argvec = tsubst (TI_ARGS (tinfo), args,
14738 complain, in_decl);
14739 if (argvec == error_mark_node)
14740 return error_mark_node;
14741
14742 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14743 = build_template_info (tmpl, argvec);
14744 }
14745 }
14746 break;
14747
14748 case TEMPLATE_PARM_INDEX:
14749 /* OK, now substitute the type of the non-type parameter. We
14750 couldn't do it earlier because it might be an auto parameter,
14751 and we wouldn't need to if we had an argument. */
14752 type = tsubst (type, args, complain, in_decl);
14753 if (type == error_mark_node)
14754 return error_mark_node;
14755 r = reduce_template_parm_level (t, type, levels, args, complain);
14756 break;
14757
14758 default:
14759 gcc_unreachable ();
14760 }
14761
14762 return r;
14763 }
14764
14765 case TREE_LIST:
14766 {
14767 tree purpose, value, chain;
14768
14769 if (t == void_list_node)
14770 return t;
14771
14772 purpose = TREE_PURPOSE (t);
14773 if (purpose)
14774 {
14775 purpose = tsubst (purpose, args, complain, in_decl);
14776 if (purpose == error_mark_node)
14777 return error_mark_node;
14778 }
14779 value = TREE_VALUE (t);
14780 if (value)
14781 {
14782 value = tsubst (value, args, complain, in_decl);
14783 if (value == error_mark_node)
14784 return error_mark_node;
14785 }
14786 chain = TREE_CHAIN (t);
14787 if (chain && chain != void_type_node)
14788 {
14789 chain = tsubst (chain, args, complain, in_decl);
14790 if (chain == error_mark_node)
14791 return error_mark_node;
14792 }
14793 if (purpose == TREE_PURPOSE (t)
14794 && value == TREE_VALUE (t)
14795 && chain == TREE_CHAIN (t))
14796 return t;
14797 return hash_tree_cons (purpose, value, chain);
14798 }
14799
14800 case TREE_BINFO:
14801 /* We should never be tsubsting a binfo. */
14802 gcc_unreachable ();
14803
14804 case TREE_VEC:
14805 /* A vector of template arguments. */
14806 gcc_assert (!type);
14807 return tsubst_template_args (t, args, complain, in_decl);
14808
14809 case POINTER_TYPE:
14810 case REFERENCE_TYPE:
14811 {
14812 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14813 return t;
14814
14815 /* [temp.deduct]
14816
14817 Type deduction may fail for any of the following
14818 reasons:
14819
14820 -- Attempting to create a pointer to reference type.
14821 -- Attempting to create a reference to a reference type or
14822 a reference to void.
14823
14824 Core issue 106 says that creating a reference to a reference
14825 during instantiation is no longer a cause for failure. We
14826 only enforce this check in strict C++98 mode. */
14827 if ((TYPE_REF_P (type)
14828 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14829 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14830 {
14831 static location_t last_loc;
14832
14833 /* We keep track of the last time we issued this error
14834 message to avoid spewing a ton of messages during a
14835 single bad template instantiation. */
14836 if (complain & tf_error
14837 && last_loc != input_location)
14838 {
14839 if (VOID_TYPE_P (type))
14840 error ("forming reference to void");
14841 else if (code == POINTER_TYPE)
14842 error ("forming pointer to reference type %qT", type);
14843 else
14844 error ("forming reference to reference type %qT", type);
14845 last_loc = input_location;
14846 }
14847
14848 return error_mark_node;
14849 }
14850 else if (TREE_CODE (type) == FUNCTION_TYPE
14851 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14852 || type_memfn_rqual (type) != REF_QUAL_NONE))
14853 {
14854 if (complain & tf_error)
14855 {
14856 if (code == POINTER_TYPE)
14857 error ("forming pointer to qualified function type %qT",
14858 type);
14859 else
14860 error ("forming reference to qualified function type %qT",
14861 type);
14862 }
14863 return error_mark_node;
14864 }
14865 else if (code == POINTER_TYPE)
14866 {
14867 r = build_pointer_type (type);
14868 if (TREE_CODE (type) == METHOD_TYPE)
14869 r = build_ptrmemfunc_type (r);
14870 }
14871 else if (TYPE_REF_P (type))
14872 /* In C++0x, during template argument substitution, when there is an
14873 attempt to create a reference to a reference type, reference
14874 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14875
14876 "If a template-argument for a template-parameter T names a type
14877 that is a reference to a type A, an attempt to create the type
14878 'lvalue reference to cv T' creates the type 'lvalue reference to
14879 A,' while an attempt to create the type type rvalue reference to
14880 cv T' creates the type T"
14881 */
14882 r = cp_build_reference_type
14883 (TREE_TYPE (type),
14884 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14885 else
14886 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14887 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14888
14889 if (r != error_mark_node)
14890 /* Will this ever be needed for TYPE_..._TO values? */
14891 layout_type (r);
14892
14893 return r;
14894 }
14895 case OFFSET_TYPE:
14896 {
14897 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14898 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14899 {
14900 /* [temp.deduct]
14901
14902 Type deduction may fail for any of the following
14903 reasons:
14904
14905 -- Attempting to create "pointer to member of T" when T
14906 is not a class type. */
14907 if (complain & tf_error)
14908 error ("creating pointer to member of non-class type %qT", r);
14909 return error_mark_node;
14910 }
14911 if (TYPE_REF_P (type))
14912 {
14913 if (complain & tf_error)
14914 error ("creating pointer to member reference type %qT", type);
14915 return error_mark_node;
14916 }
14917 if (VOID_TYPE_P (type))
14918 {
14919 if (complain & tf_error)
14920 error ("creating pointer to member of type void");
14921 return error_mark_node;
14922 }
14923 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14924 if (TREE_CODE (type) == FUNCTION_TYPE)
14925 {
14926 /* The type of the implicit object parameter gets its
14927 cv-qualifiers from the FUNCTION_TYPE. */
14928 tree memptr;
14929 tree method_type
14930 = build_memfn_type (type, r, type_memfn_quals (type),
14931 type_memfn_rqual (type));
14932 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14933 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14934 complain);
14935 }
14936 else
14937 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14938 cp_type_quals (t),
14939 complain);
14940 }
14941 case FUNCTION_TYPE:
14942 case METHOD_TYPE:
14943 {
14944 tree fntype;
14945 tree specs;
14946 fntype = tsubst_function_type (t, args, complain, in_decl);
14947 if (fntype == error_mark_node)
14948 return error_mark_node;
14949
14950 /* Substitute the exception specification. */
14951 specs = tsubst_exception_specification (t, args, complain, in_decl,
14952 /*defer_ok*/fndecl_type);
14953 if (specs == error_mark_node)
14954 return error_mark_node;
14955 if (specs)
14956 fntype = build_exception_variant (fntype, specs);
14957 return fntype;
14958 }
14959 case ARRAY_TYPE:
14960 {
14961 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14962 if (domain == error_mark_node)
14963 return error_mark_node;
14964
14965 /* As an optimization, we avoid regenerating the array type if
14966 it will obviously be the same as T. */
14967 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14968 return t;
14969
14970 /* These checks should match the ones in create_array_type_for_decl.
14971
14972 [temp.deduct]
14973
14974 The deduction may fail for any of the following reasons:
14975
14976 -- Attempting to create an array with an element type that
14977 is void, a function type, or a reference type, or [DR337]
14978 an abstract class type. */
14979 if (VOID_TYPE_P (type)
14980 || TREE_CODE (type) == FUNCTION_TYPE
14981 || (TREE_CODE (type) == ARRAY_TYPE
14982 && TYPE_DOMAIN (type) == NULL_TREE)
14983 || TYPE_REF_P (type))
14984 {
14985 if (complain & tf_error)
14986 error ("creating array of %qT", type);
14987 return error_mark_node;
14988 }
14989
14990 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14991 return error_mark_node;
14992
14993 r = build_cplus_array_type (type, domain);
14994
14995 if (!valid_array_size_p (input_location, r, in_decl,
14996 (complain & tf_error)))
14997 return error_mark_node;
14998
14999 if (TYPE_USER_ALIGN (t))
15000 {
15001 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15002 TYPE_USER_ALIGN (r) = 1;
15003 }
15004
15005 return r;
15006 }
15007
15008 case TYPENAME_TYPE:
15009 {
15010 tree ctx = TYPE_CONTEXT (t);
15011 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15012 {
15013 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15014 if (ctx == error_mark_node
15015 || TREE_VEC_LENGTH (ctx) > 1)
15016 return error_mark_node;
15017 if (TREE_VEC_LENGTH (ctx) == 0)
15018 {
15019 if (complain & tf_error)
15020 error ("%qD is instantiated for an empty pack",
15021 TYPENAME_TYPE_FULLNAME (t));
15022 return error_mark_node;
15023 }
15024 ctx = TREE_VEC_ELT (ctx, 0);
15025 }
15026 else
15027 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15028 /*entering_scope=*/1);
15029 if (ctx == error_mark_node)
15030 return error_mark_node;
15031
15032 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15033 complain, in_decl);
15034 if (f == error_mark_node)
15035 return error_mark_node;
15036
15037 if (!MAYBE_CLASS_TYPE_P (ctx))
15038 {
15039 if (complain & tf_error)
15040 error ("%qT is not a class, struct, or union type", ctx);
15041 return error_mark_node;
15042 }
15043 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15044 {
15045 /* Normally, make_typename_type does not require that the CTX
15046 have complete type in order to allow things like:
15047
15048 template <class T> struct S { typename S<T>::X Y; };
15049
15050 But, such constructs have already been resolved by this
15051 point, so here CTX really should have complete type, unless
15052 it's a partial instantiation. */
15053 ctx = complete_type (ctx);
15054 if (!COMPLETE_TYPE_P (ctx))
15055 {
15056 if (complain & tf_error)
15057 cxx_incomplete_type_error (NULL_TREE, ctx);
15058 return error_mark_node;
15059 }
15060 }
15061
15062 f = make_typename_type (ctx, f, typename_type,
15063 complain | tf_keep_type_decl);
15064 if (f == error_mark_node)
15065 return f;
15066 if (TREE_CODE (f) == TYPE_DECL)
15067 {
15068 complain |= tf_ignore_bad_quals;
15069 f = TREE_TYPE (f);
15070 }
15071
15072 if (TREE_CODE (f) != TYPENAME_TYPE)
15073 {
15074 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15075 {
15076 if (complain & tf_error)
15077 error ("%qT resolves to %qT, which is not an enumeration type",
15078 t, f);
15079 else
15080 return error_mark_node;
15081 }
15082 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15083 {
15084 if (complain & tf_error)
15085 error ("%qT resolves to %qT, which is is not a class type",
15086 t, f);
15087 else
15088 return error_mark_node;
15089 }
15090 }
15091
15092 return cp_build_qualified_type_real
15093 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15094 }
15095
15096 case UNBOUND_CLASS_TEMPLATE:
15097 {
15098 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15099 in_decl, /*entering_scope=*/1);
15100 tree name = TYPE_IDENTIFIER (t);
15101 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15102
15103 if (ctx == error_mark_node || name == error_mark_node)
15104 return error_mark_node;
15105
15106 if (parm_list)
15107 parm_list = tsubst_template_parms (parm_list, args, complain);
15108 return make_unbound_class_template (ctx, name, parm_list, complain);
15109 }
15110
15111 case TYPEOF_TYPE:
15112 {
15113 tree type;
15114
15115 ++cp_unevaluated_operand;
15116 ++c_inhibit_evaluation_warnings;
15117
15118 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15119 complain, in_decl,
15120 /*integral_constant_expression_p=*/false);
15121
15122 --cp_unevaluated_operand;
15123 --c_inhibit_evaluation_warnings;
15124
15125 type = finish_typeof (type);
15126 return cp_build_qualified_type_real (type,
15127 cp_type_quals (t)
15128 | cp_type_quals (type),
15129 complain);
15130 }
15131
15132 case DECLTYPE_TYPE:
15133 {
15134 tree type;
15135
15136 ++cp_unevaluated_operand;
15137 ++c_inhibit_evaluation_warnings;
15138
15139 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15140 complain|tf_decltype, in_decl,
15141 /*function_p*/false,
15142 /*integral_constant_expression*/false);
15143
15144 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15145 {
15146 if (type == NULL_TREE)
15147 {
15148 if (complain & tf_error)
15149 error ("empty initializer in lambda init-capture");
15150 type = error_mark_node;
15151 }
15152 else if (TREE_CODE (type) == TREE_LIST)
15153 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15154 }
15155
15156 --cp_unevaluated_operand;
15157 --c_inhibit_evaluation_warnings;
15158
15159 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15160 type = lambda_capture_field_type (type,
15161 DECLTYPE_FOR_INIT_CAPTURE (t),
15162 DECLTYPE_FOR_REF_CAPTURE (t));
15163 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15164 type = lambda_proxy_type (type);
15165 else
15166 {
15167 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15168 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15169 && EXPR_P (type))
15170 /* In a template ~id could be either a complement expression
15171 or an unqualified-id naming a destructor; if instantiating
15172 it produces an expression, it's not an id-expression or
15173 member access. */
15174 id = false;
15175 type = finish_decltype_type (type, id, complain);
15176 }
15177 return cp_build_qualified_type_real (type,
15178 cp_type_quals (t)
15179 | cp_type_quals (type),
15180 complain | tf_ignore_bad_quals);
15181 }
15182
15183 case UNDERLYING_TYPE:
15184 {
15185 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15186 complain, in_decl);
15187 return finish_underlying_type (type);
15188 }
15189
15190 case TYPE_ARGUMENT_PACK:
15191 case NONTYPE_ARGUMENT_PACK:
15192 {
15193 tree r;
15194
15195 if (code == NONTYPE_ARGUMENT_PACK)
15196 r = make_node (code);
15197 else
15198 r = cxx_make_type (code);
15199
15200 tree pack_args = ARGUMENT_PACK_ARGS (t);
15201 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15202 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15203
15204 return r;
15205 }
15206
15207 case VOID_CST:
15208 case INTEGER_CST:
15209 case REAL_CST:
15210 case STRING_CST:
15211 case PLUS_EXPR:
15212 case MINUS_EXPR:
15213 case NEGATE_EXPR:
15214 case NOP_EXPR:
15215 case INDIRECT_REF:
15216 case ADDR_EXPR:
15217 case CALL_EXPR:
15218 case ARRAY_REF:
15219 case SCOPE_REF:
15220 /* We should use one of the expression tsubsts for these codes. */
15221 gcc_unreachable ();
15222
15223 default:
15224 sorry ("use of %qs in template", get_tree_code_name (code));
15225 return error_mark_node;
15226 }
15227 }
15228
15229 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15230 expression on the left-hand side of the "." or "->" operator. We
15231 only do the lookup if we had a dependent BASELINK. Otherwise we
15232 adjust it onto the instantiated heirarchy. */
15233
15234 static tree
15235 tsubst_baselink (tree baselink, tree object_type,
15236 tree args, tsubst_flags_t complain, tree in_decl)
15237 {
15238 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15239 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15240 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15241
15242 tree optype = BASELINK_OPTYPE (baselink);
15243 optype = tsubst (optype, args, complain, in_decl);
15244
15245 tree template_args = NULL_TREE;
15246 bool template_id_p = false;
15247 tree fns = BASELINK_FUNCTIONS (baselink);
15248 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15249 {
15250 template_id_p = true;
15251 template_args = TREE_OPERAND (fns, 1);
15252 fns = TREE_OPERAND (fns, 0);
15253 if (template_args)
15254 template_args = tsubst_template_args (template_args, args,
15255 complain, in_decl);
15256 }
15257
15258 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15259 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15260 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15261
15262 if (dependent_p)
15263 {
15264 tree name = OVL_NAME (fns);
15265 if (IDENTIFIER_CONV_OP_P (name))
15266 name = make_conv_op_name (optype);
15267
15268 if (name == complete_dtor_identifier)
15269 /* Treat as-if non-dependent below. */
15270 dependent_p = false;
15271
15272 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15273 if (!baselink)
15274 {
15275 if ((complain & tf_error)
15276 && constructor_name_p (name, qualifying_scope))
15277 error ("cannot call constructor %<%T::%D%> directly",
15278 qualifying_scope, name);
15279 return error_mark_node;
15280 }
15281
15282 if (BASELINK_P (baselink))
15283 fns = BASELINK_FUNCTIONS (baselink);
15284 }
15285 else
15286 /* We're going to overwrite pieces below, make a duplicate. */
15287 baselink = copy_node (baselink);
15288
15289 /* If lookup found a single function, mark it as used at this point.
15290 (If lookup found multiple functions the one selected later by
15291 overload resolution will be marked as used at that point.) */
15292 if (!template_id_p && !really_overloaded_fn (fns))
15293 {
15294 tree fn = OVL_FIRST (fns);
15295 bool ok = mark_used (fn, complain);
15296 if (!ok && !(complain & tf_error))
15297 return error_mark_node;
15298 if (ok && BASELINK_P (baselink))
15299 /* We might have instantiated an auto function. */
15300 TREE_TYPE (baselink) = TREE_TYPE (fn);
15301 }
15302
15303 if (BASELINK_P (baselink))
15304 {
15305 /* Add back the template arguments, if present. */
15306 if (template_id_p)
15307 BASELINK_FUNCTIONS (baselink)
15308 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15309
15310 /* Update the conversion operator type. */
15311 BASELINK_OPTYPE (baselink) = optype;
15312 }
15313
15314 if (!object_type)
15315 object_type = current_class_type;
15316
15317 if (qualified_p || !dependent_p)
15318 {
15319 baselink = adjust_result_of_qualified_name_lookup (baselink,
15320 qualifying_scope,
15321 object_type);
15322 if (!qualified_p)
15323 /* We need to call adjust_result_of_qualified_name_lookup in case the
15324 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15325 so that we still get virtual function binding. */
15326 BASELINK_QUALIFIED_P (baselink) = false;
15327 }
15328
15329 return baselink;
15330 }
15331
15332 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15333 true if the qualified-id will be a postfix-expression in-and-of
15334 itself; false if more of the postfix-expression follows the
15335 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15336 of "&". */
15337
15338 static tree
15339 tsubst_qualified_id (tree qualified_id, tree args,
15340 tsubst_flags_t complain, tree in_decl,
15341 bool done, bool address_p)
15342 {
15343 tree expr;
15344 tree scope;
15345 tree name;
15346 bool is_template;
15347 tree template_args;
15348 location_t loc = UNKNOWN_LOCATION;
15349
15350 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15351
15352 /* Figure out what name to look up. */
15353 name = TREE_OPERAND (qualified_id, 1);
15354 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15355 {
15356 is_template = true;
15357 loc = EXPR_LOCATION (name);
15358 template_args = TREE_OPERAND (name, 1);
15359 if (template_args)
15360 template_args = tsubst_template_args (template_args, args,
15361 complain, in_decl);
15362 if (template_args == error_mark_node)
15363 return error_mark_node;
15364 name = TREE_OPERAND (name, 0);
15365 }
15366 else
15367 {
15368 is_template = false;
15369 template_args = NULL_TREE;
15370 }
15371
15372 /* Substitute into the qualifying scope. When there are no ARGS, we
15373 are just trying to simplify a non-dependent expression. In that
15374 case the qualifying scope may be dependent, and, in any case,
15375 substituting will not help. */
15376 scope = TREE_OPERAND (qualified_id, 0);
15377 if (args)
15378 {
15379 scope = tsubst (scope, args, complain, in_decl);
15380 expr = tsubst_copy (name, args, complain, in_decl);
15381 }
15382 else
15383 expr = name;
15384
15385 if (dependent_scope_p (scope))
15386 {
15387 if (is_template)
15388 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15389 tree r = build_qualified_name (NULL_TREE, scope, expr,
15390 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15391 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15392 return r;
15393 }
15394
15395 if (!BASELINK_P (name) && !DECL_P (expr))
15396 {
15397 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15398 {
15399 /* A BIT_NOT_EXPR is used to represent a destructor. */
15400 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15401 {
15402 error ("qualifying type %qT does not match destructor name ~%qT",
15403 scope, TREE_OPERAND (expr, 0));
15404 expr = error_mark_node;
15405 }
15406 else
15407 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15408 /*is_type_p=*/0, false);
15409 }
15410 else
15411 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15412 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15413 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15414 {
15415 if (complain & tf_error)
15416 {
15417 error ("dependent-name %qE is parsed as a non-type, but "
15418 "instantiation yields a type", qualified_id);
15419 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15420 }
15421 return error_mark_node;
15422 }
15423 }
15424
15425 if (DECL_P (expr))
15426 {
15427 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15428 scope);
15429 /* Remember that there was a reference to this entity. */
15430 if (!mark_used (expr, complain) && !(complain & tf_error))
15431 return error_mark_node;
15432 }
15433
15434 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15435 {
15436 if (complain & tf_error)
15437 qualified_name_lookup_error (scope,
15438 TREE_OPERAND (qualified_id, 1),
15439 expr, input_location);
15440 return error_mark_node;
15441 }
15442
15443 if (is_template)
15444 {
15445 /* We may be repeating a check already done during parsing, but
15446 if it was well-formed and passed then, it will pass again
15447 now, and if it didn't, we wouldn't have got here. The case
15448 we want to catch is when we couldn't tell then, and can now,
15449 namely when templ prior to substitution was an
15450 identifier. */
15451 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15452 return error_mark_node;
15453
15454 if (variable_template_p (expr))
15455 expr = lookup_and_finish_template_variable (expr, template_args,
15456 complain);
15457 else
15458 expr = lookup_template_function (expr, template_args);
15459 }
15460
15461 if (expr == error_mark_node && complain & tf_error)
15462 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15463 expr, input_location);
15464 else if (TYPE_P (scope))
15465 {
15466 expr = (adjust_result_of_qualified_name_lookup
15467 (expr, scope, current_nonlambda_class_type ()));
15468 expr = (finish_qualified_id_expr
15469 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15470 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15471 /*template_arg_p=*/false, complain));
15472 }
15473
15474 /* Expressions do not generally have reference type. */
15475 if (TREE_CODE (expr) != SCOPE_REF
15476 /* However, if we're about to form a pointer-to-member, we just
15477 want the referenced member referenced. */
15478 && TREE_CODE (expr) != OFFSET_REF)
15479 expr = convert_from_reference (expr);
15480
15481 if (REF_PARENTHESIZED_P (qualified_id))
15482 expr = force_paren_expr (expr);
15483
15484 return expr;
15485 }
15486
15487 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15488 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15489 for tsubst. */
15490
15491 static tree
15492 tsubst_init (tree init, tree decl, tree args,
15493 tsubst_flags_t complain, tree in_decl)
15494 {
15495 if (!init)
15496 return NULL_TREE;
15497
15498 init = tsubst_expr (init, args, complain, in_decl, false);
15499
15500 tree type = TREE_TYPE (decl);
15501
15502 if (!init && type != error_mark_node)
15503 {
15504 if (tree auto_node = type_uses_auto (type))
15505 {
15506 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15507 {
15508 if (complain & tf_error)
15509 error ("initializer for %q#D expands to an empty list "
15510 "of expressions", decl);
15511 return error_mark_node;
15512 }
15513 }
15514 else if (!dependent_type_p (type))
15515 {
15516 /* If we had an initializer but it
15517 instantiated to nothing,
15518 value-initialize the object. This will
15519 only occur when the initializer was a
15520 pack expansion where the parameter packs
15521 used in that expansion were of length
15522 zero. */
15523 init = build_value_init (type, complain);
15524 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15525 init = get_target_expr_sfinae (init, complain);
15526 if (TREE_CODE (init) == TARGET_EXPR)
15527 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15528 }
15529 }
15530
15531 return init;
15532 }
15533
15534 /* Like tsubst, but deals with expressions. This function just replaces
15535 template parms; to finish processing the resultant expression, use
15536 tsubst_copy_and_build or tsubst_expr. */
15537
15538 static tree
15539 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15540 {
15541 enum tree_code code;
15542 tree r;
15543
15544 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15545 return t;
15546
15547 code = TREE_CODE (t);
15548
15549 switch (code)
15550 {
15551 case PARM_DECL:
15552 r = retrieve_local_specialization (t);
15553
15554 if (r == NULL_TREE)
15555 {
15556 /* We get here for a use of 'this' in an NSDMI. */
15557 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15558 return current_class_ptr;
15559
15560 /* This can happen for a parameter name used later in a function
15561 declaration (such as in a late-specified return type). Just
15562 make a dummy decl, since it's only used for its type. */
15563 gcc_assert (cp_unevaluated_operand != 0);
15564 r = tsubst_decl (t, args, complain);
15565 /* Give it the template pattern as its context; its true context
15566 hasn't been instantiated yet and this is good enough for
15567 mangling. */
15568 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15569 }
15570
15571 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15572 r = argument_pack_select_arg (r);
15573 if (!mark_used (r, complain) && !(complain & tf_error))
15574 return error_mark_node;
15575 return r;
15576
15577 case CONST_DECL:
15578 {
15579 tree enum_type;
15580 tree v;
15581
15582 if (DECL_TEMPLATE_PARM_P (t))
15583 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15584 /* There is no need to substitute into namespace-scope
15585 enumerators. */
15586 if (DECL_NAMESPACE_SCOPE_P (t))
15587 return t;
15588 /* If ARGS is NULL, then T is known to be non-dependent. */
15589 if (args == NULL_TREE)
15590 return scalar_constant_value (t);
15591
15592 /* Unfortunately, we cannot just call lookup_name here.
15593 Consider:
15594
15595 template <int I> int f() {
15596 enum E { a = I };
15597 struct S { void g() { E e = a; } };
15598 };
15599
15600 When we instantiate f<7>::S::g(), say, lookup_name is not
15601 clever enough to find f<7>::a. */
15602 enum_type
15603 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15604 /*entering_scope=*/0);
15605
15606 for (v = TYPE_VALUES (enum_type);
15607 v != NULL_TREE;
15608 v = TREE_CHAIN (v))
15609 if (TREE_PURPOSE (v) == DECL_NAME (t))
15610 return TREE_VALUE (v);
15611
15612 /* We didn't find the name. That should never happen; if
15613 name-lookup found it during preliminary parsing, we
15614 should find it again here during instantiation. */
15615 gcc_unreachable ();
15616 }
15617 return t;
15618
15619 case FIELD_DECL:
15620 if (DECL_CONTEXT (t))
15621 {
15622 tree ctx;
15623
15624 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15625 /*entering_scope=*/1);
15626 if (ctx != DECL_CONTEXT (t))
15627 {
15628 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15629 if (!r)
15630 {
15631 if (complain & tf_error)
15632 error ("using invalid field %qD", t);
15633 return error_mark_node;
15634 }
15635 return r;
15636 }
15637 }
15638
15639 return t;
15640
15641 case VAR_DECL:
15642 case FUNCTION_DECL:
15643 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15644 r = tsubst (t, args, complain, in_decl);
15645 else if (local_variable_p (t)
15646 && uses_template_parms (DECL_CONTEXT (t)))
15647 {
15648 r = retrieve_local_specialization (t);
15649 if (r == NULL_TREE)
15650 {
15651 /* First try name lookup to find the instantiation. */
15652 r = lookup_name (DECL_NAME (t));
15653 if (r)
15654 {
15655 if (!VAR_P (r))
15656 {
15657 /* During error-recovery we may find a non-variable,
15658 even an OVERLOAD: just bail out and avoid ICEs and
15659 duplicate diagnostics (c++/62207). */
15660 gcc_assert (seen_error ());
15661 return error_mark_node;
15662 }
15663 if (!is_capture_proxy (r))
15664 {
15665 /* Make sure the one we found is the one we want. */
15666 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15667 if (ctx != DECL_CONTEXT (r))
15668 r = NULL_TREE;
15669 }
15670 }
15671
15672 if (r)
15673 /* OK */;
15674 else
15675 {
15676 /* This can happen for a variable used in a
15677 late-specified return type of a local lambda, or for a
15678 local static or constant. Building a new VAR_DECL
15679 should be OK in all those cases. */
15680 r = tsubst_decl (t, args, complain);
15681 if (local_specializations)
15682 /* Avoid infinite recursion (79640). */
15683 register_local_specialization (r, t);
15684 if (decl_maybe_constant_var_p (r))
15685 {
15686 /* We can't call cp_finish_decl, so handle the
15687 initializer by hand. */
15688 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15689 complain, in_decl);
15690 if (!processing_template_decl)
15691 init = maybe_constant_init (init);
15692 if (processing_template_decl
15693 ? potential_constant_expression (init)
15694 : reduced_constant_expression_p (init))
15695 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15696 = TREE_CONSTANT (r) = true;
15697 DECL_INITIAL (r) = init;
15698 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15699 TREE_TYPE (r)
15700 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15701 complain, adc_variable_type);
15702 }
15703 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15704 || decl_constant_var_p (r)
15705 || seen_error ());
15706 if (!processing_template_decl
15707 && !TREE_STATIC (r))
15708 r = process_outer_var_ref (r, complain);
15709 }
15710 /* Remember this for subsequent uses. */
15711 if (local_specializations)
15712 register_local_specialization (r, t);
15713 }
15714 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15715 r = argument_pack_select_arg (r);
15716 }
15717 else
15718 r = t;
15719 if (!mark_used (r, complain))
15720 return error_mark_node;
15721 return r;
15722
15723 case NAMESPACE_DECL:
15724 return t;
15725
15726 case OVERLOAD:
15727 return t;
15728
15729 case BASELINK:
15730 return tsubst_baselink (t, current_nonlambda_class_type (),
15731 args, complain, in_decl);
15732
15733 case TEMPLATE_DECL:
15734 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15735 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15736 args, complain, in_decl);
15737 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15738 return tsubst (t, args, complain, in_decl);
15739 else if (DECL_CLASS_SCOPE_P (t)
15740 && uses_template_parms (DECL_CONTEXT (t)))
15741 {
15742 /* Template template argument like the following example need
15743 special treatment:
15744
15745 template <template <class> class TT> struct C {};
15746 template <class T> struct D {
15747 template <class U> struct E {};
15748 C<E> c; // #1
15749 };
15750 D<int> d; // #2
15751
15752 We are processing the template argument `E' in #1 for
15753 the template instantiation #2. Originally, `E' is a
15754 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15755 have to substitute this with one having context `D<int>'. */
15756
15757 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15758 if (dependent_scope_p (context))
15759 {
15760 /* When rewriting a constructor into a deduction guide, a
15761 non-dependent name can become dependent, so memtmpl<args>
15762 becomes context::template memtmpl<args>. */
15763 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15764 return build_qualified_name (type, context, DECL_NAME (t),
15765 /*template*/true);
15766 }
15767 return lookup_field (context, DECL_NAME(t), 0, false);
15768 }
15769 else
15770 /* Ordinary template template argument. */
15771 return t;
15772
15773 case NON_LVALUE_EXPR:
15774 case VIEW_CONVERT_EXPR:
15775 {
15776 /* Handle location wrappers by substituting the wrapped node
15777 first, *then* reusing the resulting type. Doing the type
15778 first ensures that we handle template parameters and
15779 parameter pack expansions. */
15780 if (location_wrapper_p (t))
15781 {
15782 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15783 complain, in_decl);
15784 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15785 }
15786 tree op = TREE_OPERAND (t, 0);
15787 if (code == VIEW_CONVERT_EXPR
15788 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15789 {
15790 /* Wrapper to make a C++20 template parameter object const. */
15791 op = tsubst_copy (op, args, complain, in_decl);
15792 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15793 {
15794 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15795 return build1 (code, type, op);
15796 }
15797 else
15798 {
15799 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15800 return op;
15801 }
15802 }
15803 /* We shouldn't see any other uses of these in templates. */
15804 gcc_unreachable ();
15805 }
15806
15807 case CAST_EXPR:
15808 case REINTERPRET_CAST_EXPR:
15809 case CONST_CAST_EXPR:
15810 case STATIC_CAST_EXPR:
15811 case DYNAMIC_CAST_EXPR:
15812 case IMPLICIT_CONV_EXPR:
15813 case CONVERT_EXPR:
15814 case NOP_EXPR:
15815 {
15816 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15817 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15818 return build1 (code, type, op0);
15819 }
15820
15821 case SIZEOF_EXPR:
15822 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15823 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15824 {
15825 tree expanded, op = TREE_OPERAND (t, 0);
15826 int len = 0;
15827
15828 if (SIZEOF_EXPR_TYPE_P (t))
15829 op = TREE_TYPE (op);
15830
15831 ++cp_unevaluated_operand;
15832 ++c_inhibit_evaluation_warnings;
15833 /* We only want to compute the number of arguments. */
15834 if (PACK_EXPANSION_P (op))
15835 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15836 else
15837 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15838 args, complain, in_decl);
15839 --cp_unevaluated_operand;
15840 --c_inhibit_evaluation_warnings;
15841
15842 if (TREE_CODE (expanded) == TREE_VEC)
15843 {
15844 len = TREE_VEC_LENGTH (expanded);
15845 /* Set TREE_USED for the benefit of -Wunused. */
15846 for (int i = 0; i < len; i++)
15847 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15848 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15849 }
15850
15851 if (expanded == error_mark_node)
15852 return error_mark_node;
15853 else if (PACK_EXPANSION_P (expanded)
15854 || (TREE_CODE (expanded) == TREE_VEC
15855 && pack_expansion_args_count (expanded)))
15856
15857 {
15858 if (PACK_EXPANSION_P (expanded))
15859 /* OK. */;
15860 else if (TREE_VEC_LENGTH (expanded) == 1)
15861 expanded = TREE_VEC_ELT (expanded, 0);
15862 else
15863 expanded = make_argument_pack (expanded);
15864
15865 if (TYPE_P (expanded))
15866 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15867 false,
15868 complain & tf_error);
15869 else
15870 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15871 complain & tf_error);
15872 }
15873 else
15874 return build_int_cst (size_type_node, len);
15875 }
15876 if (SIZEOF_EXPR_TYPE_P (t))
15877 {
15878 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15879 args, complain, in_decl);
15880 r = build1 (NOP_EXPR, r, error_mark_node);
15881 r = build1 (SIZEOF_EXPR,
15882 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15883 SIZEOF_EXPR_TYPE_P (r) = 1;
15884 return r;
15885 }
15886 /* Fall through */
15887
15888 case INDIRECT_REF:
15889 case NEGATE_EXPR:
15890 case TRUTH_NOT_EXPR:
15891 case BIT_NOT_EXPR:
15892 case ADDR_EXPR:
15893 case UNARY_PLUS_EXPR: /* Unary + */
15894 case ALIGNOF_EXPR:
15895 case AT_ENCODE_EXPR:
15896 case ARROW_EXPR:
15897 case THROW_EXPR:
15898 case TYPEID_EXPR:
15899 case REALPART_EXPR:
15900 case IMAGPART_EXPR:
15901 case PAREN_EXPR:
15902 {
15903 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15904 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15905 r = build1 (code, type, op0);
15906 if (code == ALIGNOF_EXPR)
15907 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15908 return r;
15909 }
15910
15911 case COMPONENT_REF:
15912 {
15913 tree object;
15914 tree name;
15915
15916 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15917 name = TREE_OPERAND (t, 1);
15918 if (TREE_CODE (name) == BIT_NOT_EXPR)
15919 {
15920 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15921 complain, in_decl);
15922 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15923 }
15924 else if (TREE_CODE (name) == SCOPE_REF
15925 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15926 {
15927 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15928 complain, in_decl);
15929 name = TREE_OPERAND (name, 1);
15930 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15931 complain, in_decl);
15932 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15933 name = build_qualified_name (/*type=*/NULL_TREE,
15934 base, name,
15935 /*template_p=*/false);
15936 }
15937 else if (BASELINK_P (name))
15938 name = tsubst_baselink (name,
15939 non_reference (TREE_TYPE (object)),
15940 args, complain,
15941 in_decl);
15942 else
15943 name = tsubst_copy (name, args, complain, in_decl);
15944 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15945 }
15946
15947 case PLUS_EXPR:
15948 case MINUS_EXPR:
15949 case MULT_EXPR:
15950 case TRUNC_DIV_EXPR:
15951 case CEIL_DIV_EXPR:
15952 case FLOOR_DIV_EXPR:
15953 case ROUND_DIV_EXPR:
15954 case EXACT_DIV_EXPR:
15955 case BIT_AND_EXPR:
15956 case BIT_IOR_EXPR:
15957 case BIT_XOR_EXPR:
15958 case TRUNC_MOD_EXPR:
15959 case FLOOR_MOD_EXPR:
15960 case TRUTH_ANDIF_EXPR:
15961 case TRUTH_ORIF_EXPR:
15962 case TRUTH_AND_EXPR:
15963 case TRUTH_OR_EXPR:
15964 case RSHIFT_EXPR:
15965 case LSHIFT_EXPR:
15966 case RROTATE_EXPR:
15967 case LROTATE_EXPR:
15968 case EQ_EXPR:
15969 case NE_EXPR:
15970 case MAX_EXPR:
15971 case MIN_EXPR:
15972 case LE_EXPR:
15973 case GE_EXPR:
15974 case LT_EXPR:
15975 case GT_EXPR:
15976 case COMPOUND_EXPR:
15977 case DOTSTAR_EXPR:
15978 case MEMBER_REF:
15979 case PREDECREMENT_EXPR:
15980 case PREINCREMENT_EXPR:
15981 case POSTDECREMENT_EXPR:
15982 case POSTINCREMENT_EXPR:
15983 {
15984 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15985 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15986 return build_nt (code, op0, op1);
15987 }
15988
15989 case SCOPE_REF:
15990 {
15991 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15992 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15993 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15994 QUALIFIED_NAME_IS_TEMPLATE (t));
15995 }
15996
15997 case ARRAY_REF:
15998 {
15999 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16000 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16001 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
16002 }
16003
16004 case CALL_EXPR:
16005 {
16006 int n = VL_EXP_OPERAND_LENGTH (t);
16007 tree result = build_vl_exp (CALL_EXPR, n);
16008 int i;
16009 for (i = 0; i < n; i++)
16010 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16011 complain, in_decl);
16012 return result;
16013 }
16014
16015 case COND_EXPR:
16016 case MODOP_EXPR:
16017 case PSEUDO_DTOR_EXPR:
16018 case VEC_PERM_EXPR:
16019 {
16020 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16021 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16022 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16023 r = build_nt (code, op0, op1, op2);
16024 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16025 return r;
16026 }
16027
16028 case NEW_EXPR:
16029 {
16030 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16031 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16032 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16033 r = build_nt (code, op0, op1, op2);
16034 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16035 return r;
16036 }
16037
16038 case DELETE_EXPR:
16039 {
16040 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16041 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16042 r = build_nt (code, op0, op1);
16043 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16044 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16045 return r;
16046 }
16047
16048 case TEMPLATE_ID_EXPR:
16049 {
16050 /* Substituted template arguments */
16051 tree fn = TREE_OPERAND (t, 0);
16052 tree targs = TREE_OPERAND (t, 1);
16053
16054 fn = tsubst_copy (fn, args, complain, in_decl);
16055 if (targs)
16056 targs = tsubst_template_args (targs, args, complain, in_decl);
16057
16058 return lookup_template_function (fn, targs);
16059 }
16060
16061 case TREE_LIST:
16062 {
16063 tree purpose, value, chain;
16064
16065 if (t == void_list_node)
16066 return t;
16067
16068 purpose = TREE_PURPOSE (t);
16069 if (purpose)
16070 purpose = tsubst_copy (purpose, args, complain, in_decl);
16071 value = TREE_VALUE (t);
16072 if (value)
16073 value = tsubst_copy (value, args, complain, in_decl);
16074 chain = TREE_CHAIN (t);
16075 if (chain && chain != void_type_node)
16076 chain = tsubst_copy (chain, args, complain, in_decl);
16077 if (purpose == TREE_PURPOSE (t)
16078 && value == TREE_VALUE (t)
16079 && chain == TREE_CHAIN (t))
16080 return t;
16081 return tree_cons (purpose, value, chain);
16082 }
16083
16084 case RECORD_TYPE:
16085 case UNION_TYPE:
16086 case ENUMERAL_TYPE:
16087 case INTEGER_TYPE:
16088 case TEMPLATE_TYPE_PARM:
16089 case TEMPLATE_TEMPLATE_PARM:
16090 case BOUND_TEMPLATE_TEMPLATE_PARM:
16091 case TEMPLATE_PARM_INDEX:
16092 case POINTER_TYPE:
16093 case REFERENCE_TYPE:
16094 case OFFSET_TYPE:
16095 case FUNCTION_TYPE:
16096 case METHOD_TYPE:
16097 case ARRAY_TYPE:
16098 case TYPENAME_TYPE:
16099 case UNBOUND_CLASS_TEMPLATE:
16100 case TYPEOF_TYPE:
16101 case DECLTYPE_TYPE:
16102 case TYPE_DECL:
16103 return tsubst (t, args, complain, in_decl);
16104
16105 case USING_DECL:
16106 t = DECL_NAME (t);
16107 /* Fall through. */
16108 case IDENTIFIER_NODE:
16109 if (IDENTIFIER_CONV_OP_P (t))
16110 {
16111 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16112 return make_conv_op_name (new_type);
16113 }
16114 else
16115 return t;
16116
16117 case CONSTRUCTOR:
16118 /* This is handled by tsubst_copy_and_build. */
16119 gcc_unreachable ();
16120
16121 case VA_ARG_EXPR:
16122 {
16123 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16124 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16125 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16126 }
16127
16128 case CLEANUP_POINT_EXPR:
16129 /* We shouldn't have built any of these during initial template
16130 generation. Instead, they should be built during instantiation
16131 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16132 gcc_unreachable ();
16133
16134 case OFFSET_REF:
16135 {
16136 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16137 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16138 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16139 r = build2 (code, type, op0, op1);
16140 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16141 if (!mark_used (TREE_OPERAND (r, 1), complain)
16142 && !(complain & tf_error))
16143 return error_mark_node;
16144 return r;
16145 }
16146
16147 case EXPR_PACK_EXPANSION:
16148 error ("invalid use of pack expansion expression");
16149 return error_mark_node;
16150
16151 case NONTYPE_ARGUMENT_PACK:
16152 error ("use %<...%> to expand argument pack");
16153 return error_mark_node;
16154
16155 case VOID_CST:
16156 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16157 return t;
16158
16159 case INTEGER_CST:
16160 case REAL_CST:
16161 case STRING_CST:
16162 case COMPLEX_CST:
16163 {
16164 /* Instantiate any typedefs in the type. */
16165 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16166 r = fold_convert (type, t);
16167 gcc_assert (TREE_CODE (r) == code);
16168 return r;
16169 }
16170
16171 case PTRMEM_CST:
16172 /* These can sometimes show up in a partial instantiation, but never
16173 involve template parms. */
16174 gcc_assert (!uses_template_parms (t));
16175 return t;
16176
16177 case UNARY_LEFT_FOLD_EXPR:
16178 return tsubst_unary_left_fold (t, args, complain, in_decl);
16179 case UNARY_RIGHT_FOLD_EXPR:
16180 return tsubst_unary_right_fold (t, args, complain, in_decl);
16181 case BINARY_LEFT_FOLD_EXPR:
16182 return tsubst_binary_left_fold (t, args, complain, in_decl);
16183 case BINARY_RIGHT_FOLD_EXPR:
16184 return tsubst_binary_right_fold (t, args, complain, in_decl);
16185 case PREDICT_EXPR:
16186 return t;
16187
16188 case DEBUG_BEGIN_STMT:
16189 /* ??? There's no point in copying it for now, but maybe some
16190 day it will contain more information, such as a pointer back
16191 to the containing function, inlined copy or so. */
16192 return t;
16193
16194 default:
16195 /* We shouldn't get here, but keep going if !flag_checking. */
16196 if (flag_checking)
16197 gcc_unreachable ();
16198 return t;
16199 }
16200 }
16201
16202 /* Helper function for tsubst_omp_clauses, used for instantiation of
16203 OMP_CLAUSE_DECL of clauses. */
16204
16205 static tree
16206 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16207 tree in_decl, tree *iterator_cache)
16208 {
16209 if (decl == NULL_TREE)
16210 return NULL_TREE;
16211
16212 /* Handle OpenMP iterators. */
16213 if (TREE_CODE (decl) == TREE_LIST
16214 && TREE_PURPOSE (decl)
16215 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16216 {
16217 tree ret;
16218 if (iterator_cache[0] == TREE_PURPOSE (decl))
16219 ret = iterator_cache[1];
16220 else
16221 {
16222 tree *tp = &ret;
16223 begin_scope (sk_omp, NULL);
16224 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16225 {
16226 *tp = copy_node (it);
16227 TREE_VEC_ELT (*tp, 0)
16228 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16229 TREE_VEC_ELT (*tp, 1)
16230 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16231 /*integral_constant_expression_p=*/false);
16232 TREE_VEC_ELT (*tp, 2)
16233 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16234 /*integral_constant_expression_p=*/false);
16235 TREE_VEC_ELT (*tp, 3)
16236 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16237 /*integral_constant_expression_p=*/false);
16238 TREE_CHAIN (*tp) = NULL_TREE;
16239 tp = &TREE_CHAIN (*tp);
16240 }
16241 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16242 iterator_cache[0] = TREE_PURPOSE (decl);
16243 iterator_cache[1] = ret;
16244 }
16245 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16246 args, complain,
16247 in_decl, NULL));
16248 }
16249
16250 /* Handle an OpenMP array section represented as a TREE_LIST (or
16251 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16252 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16253 TREE_LIST. We can handle it exactly the same as an array section
16254 (purpose, value, and a chain), even though the nomenclature
16255 (low_bound, length, etc) is different. */
16256 if (TREE_CODE (decl) == TREE_LIST)
16257 {
16258 tree low_bound
16259 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16260 /*integral_constant_expression_p=*/false);
16261 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16262 /*integral_constant_expression_p=*/false);
16263 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16264 in_decl, NULL);
16265 if (TREE_PURPOSE (decl) == low_bound
16266 && TREE_VALUE (decl) == length
16267 && TREE_CHAIN (decl) == chain)
16268 return decl;
16269 tree ret = tree_cons (low_bound, length, chain);
16270 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16271 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16272 return ret;
16273 }
16274 tree ret = tsubst_expr (decl, args, complain, in_decl,
16275 /*integral_constant_expression_p=*/false);
16276 /* Undo convert_from_reference tsubst_expr could have called. */
16277 if (decl
16278 && REFERENCE_REF_P (ret)
16279 && !REFERENCE_REF_P (decl))
16280 ret = TREE_OPERAND (ret, 0);
16281 return ret;
16282 }
16283
16284 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16285
16286 static tree
16287 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16288 tree args, tsubst_flags_t complain, tree in_decl)
16289 {
16290 tree new_clauses = NULL_TREE, nc, oc;
16291 tree linear_no_step = NULL_TREE;
16292 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16293
16294 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16295 {
16296 nc = copy_node (oc);
16297 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16298 new_clauses = nc;
16299
16300 switch (OMP_CLAUSE_CODE (nc))
16301 {
16302 case OMP_CLAUSE_LASTPRIVATE:
16303 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16304 {
16305 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16306 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16307 in_decl, /*integral_constant_expression_p=*/false);
16308 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16309 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16310 }
16311 /* FALLTHRU */
16312 case OMP_CLAUSE_PRIVATE:
16313 case OMP_CLAUSE_SHARED:
16314 case OMP_CLAUSE_FIRSTPRIVATE:
16315 case OMP_CLAUSE_COPYIN:
16316 case OMP_CLAUSE_COPYPRIVATE:
16317 case OMP_CLAUSE_UNIFORM:
16318 case OMP_CLAUSE_DEPEND:
16319 case OMP_CLAUSE_FROM:
16320 case OMP_CLAUSE_TO:
16321 case OMP_CLAUSE_MAP:
16322 case OMP_CLAUSE_NONTEMPORAL:
16323 case OMP_CLAUSE_USE_DEVICE_PTR:
16324 case OMP_CLAUSE_IS_DEVICE_PTR:
16325 case OMP_CLAUSE_INCLUSIVE:
16326 case OMP_CLAUSE_EXCLUSIVE:
16327 OMP_CLAUSE_DECL (nc)
16328 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16329 in_decl, iterator_cache);
16330 break;
16331 case OMP_CLAUSE_TILE:
16332 case OMP_CLAUSE_IF:
16333 case OMP_CLAUSE_NUM_THREADS:
16334 case OMP_CLAUSE_SCHEDULE:
16335 case OMP_CLAUSE_COLLAPSE:
16336 case OMP_CLAUSE_FINAL:
16337 case OMP_CLAUSE_DEVICE:
16338 case OMP_CLAUSE_DIST_SCHEDULE:
16339 case OMP_CLAUSE_NUM_TEAMS:
16340 case OMP_CLAUSE_THREAD_LIMIT:
16341 case OMP_CLAUSE_SAFELEN:
16342 case OMP_CLAUSE_SIMDLEN:
16343 case OMP_CLAUSE_NUM_TASKS:
16344 case OMP_CLAUSE_GRAINSIZE:
16345 case OMP_CLAUSE_PRIORITY:
16346 case OMP_CLAUSE_ORDERED:
16347 case OMP_CLAUSE_HINT:
16348 case OMP_CLAUSE_NUM_GANGS:
16349 case OMP_CLAUSE_NUM_WORKERS:
16350 case OMP_CLAUSE_VECTOR_LENGTH:
16351 case OMP_CLAUSE_WORKER:
16352 case OMP_CLAUSE_VECTOR:
16353 case OMP_CLAUSE_ASYNC:
16354 case OMP_CLAUSE_WAIT:
16355 OMP_CLAUSE_OPERAND (nc, 0)
16356 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16357 in_decl, /*integral_constant_expression_p=*/false);
16358 break;
16359 case OMP_CLAUSE_REDUCTION:
16360 case OMP_CLAUSE_IN_REDUCTION:
16361 case OMP_CLAUSE_TASK_REDUCTION:
16362 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16363 {
16364 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16365 if (TREE_CODE (placeholder) == SCOPE_REF)
16366 {
16367 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16368 complain, in_decl);
16369 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16370 = build_qualified_name (NULL_TREE, scope,
16371 TREE_OPERAND (placeholder, 1),
16372 false);
16373 }
16374 else
16375 gcc_assert (identifier_p (placeholder));
16376 }
16377 OMP_CLAUSE_DECL (nc)
16378 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16379 in_decl, NULL);
16380 break;
16381 case OMP_CLAUSE_GANG:
16382 case OMP_CLAUSE_ALIGNED:
16383 OMP_CLAUSE_DECL (nc)
16384 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16385 in_decl, NULL);
16386 OMP_CLAUSE_OPERAND (nc, 1)
16387 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16388 in_decl, /*integral_constant_expression_p=*/false);
16389 break;
16390 case OMP_CLAUSE_LINEAR:
16391 OMP_CLAUSE_DECL (nc)
16392 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16393 in_decl, NULL);
16394 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16395 {
16396 gcc_assert (!linear_no_step);
16397 linear_no_step = nc;
16398 }
16399 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16400 OMP_CLAUSE_LINEAR_STEP (nc)
16401 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16402 complain, in_decl, NULL);
16403 else
16404 OMP_CLAUSE_LINEAR_STEP (nc)
16405 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16406 in_decl,
16407 /*integral_constant_expression_p=*/false);
16408 break;
16409 case OMP_CLAUSE_NOWAIT:
16410 case OMP_CLAUSE_DEFAULT:
16411 case OMP_CLAUSE_UNTIED:
16412 case OMP_CLAUSE_MERGEABLE:
16413 case OMP_CLAUSE_INBRANCH:
16414 case OMP_CLAUSE_NOTINBRANCH:
16415 case OMP_CLAUSE_PROC_BIND:
16416 case OMP_CLAUSE_FOR:
16417 case OMP_CLAUSE_PARALLEL:
16418 case OMP_CLAUSE_SECTIONS:
16419 case OMP_CLAUSE_TASKGROUP:
16420 case OMP_CLAUSE_NOGROUP:
16421 case OMP_CLAUSE_THREADS:
16422 case OMP_CLAUSE_SIMD:
16423 case OMP_CLAUSE_DEFAULTMAP:
16424 case OMP_CLAUSE_INDEPENDENT:
16425 case OMP_CLAUSE_AUTO:
16426 case OMP_CLAUSE_SEQ:
16427 case OMP_CLAUSE_IF_PRESENT:
16428 case OMP_CLAUSE_FINALIZE:
16429 break;
16430 default:
16431 gcc_unreachable ();
16432 }
16433 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16434 switch (OMP_CLAUSE_CODE (nc))
16435 {
16436 case OMP_CLAUSE_SHARED:
16437 case OMP_CLAUSE_PRIVATE:
16438 case OMP_CLAUSE_FIRSTPRIVATE:
16439 case OMP_CLAUSE_LASTPRIVATE:
16440 case OMP_CLAUSE_COPYPRIVATE:
16441 case OMP_CLAUSE_LINEAR:
16442 case OMP_CLAUSE_REDUCTION:
16443 case OMP_CLAUSE_IN_REDUCTION:
16444 case OMP_CLAUSE_TASK_REDUCTION:
16445 case OMP_CLAUSE_USE_DEVICE_PTR:
16446 case OMP_CLAUSE_IS_DEVICE_PTR:
16447 case OMP_CLAUSE_INCLUSIVE:
16448 case OMP_CLAUSE_EXCLUSIVE:
16449 /* tsubst_expr on SCOPE_REF results in returning
16450 finish_non_static_data_member result. Undo that here. */
16451 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16452 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16453 == IDENTIFIER_NODE))
16454 {
16455 tree t = OMP_CLAUSE_DECL (nc);
16456 tree v = t;
16457 while (v)
16458 switch (TREE_CODE (v))
16459 {
16460 case COMPONENT_REF:
16461 case MEM_REF:
16462 case INDIRECT_REF:
16463 CASE_CONVERT:
16464 case POINTER_PLUS_EXPR:
16465 v = TREE_OPERAND (v, 0);
16466 continue;
16467 case PARM_DECL:
16468 if (DECL_CONTEXT (v) == current_function_decl
16469 && DECL_ARTIFICIAL (v)
16470 && DECL_NAME (v) == this_identifier)
16471 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16472 /* FALLTHRU */
16473 default:
16474 v = NULL_TREE;
16475 break;
16476 }
16477 }
16478 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16479 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16480 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16481 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16482 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16483 {
16484 tree decl = OMP_CLAUSE_DECL (nc);
16485 if (VAR_P (decl))
16486 {
16487 retrofit_lang_decl (decl);
16488 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16489 }
16490 }
16491 break;
16492 default:
16493 break;
16494 }
16495 }
16496
16497 new_clauses = nreverse (new_clauses);
16498 if (ort != C_ORT_OMP_DECLARE_SIMD)
16499 {
16500 new_clauses = finish_omp_clauses (new_clauses, ort);
16501 if (linear_no_step)
16502 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16503 if (nc == linear_no_step)
16504 {
16505 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16506 break;
16507 }
16508 }
16509 return new_clauses;
16510 }
16511
16512 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16513
16514 static tree
16515 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16516 tree in_decl)
16517 {
16518 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16519
16520 tree purpose, value, chain;
16521
16522 if (t == NULL)
16523 return t;
16524
16525 if (TREE_CODE (t) != TREE_LIST)
16526 return tsubst_copy_and_build (t, args, complain, in_decl,
16527 /*function_p=*/false,
16528 /*integral_constant_expression_p=*/false);
16529
16530 if (t == void_list_node)
16531 return t;
16532
16533 purpose = TREE_PURPOSE (t);
16534 if (purpose)
16535 purpose = RECUR (purpose);
16536 value = TREE_VALUE (t);
16537 if (value)
16538 {
16539 if (TREE_CODE (value) != LABEL_DECL)
16540 value = RECUR (value);
16541 else
16542 {
16543 value = lookup_label (DECL_NAME (value));
16544 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16545 TREE_USED (value) = 1;
16546 }
16547 }
16548 chain = TREE_CHAIN (t);
16549 if (chain && chain != void_type_node)
16550 chain = RECUR (chain);
16551 return tree_cons (purpose, value, chain);
16552 #undef RECUR
16553 }
16554
16555 /* Used to temporarily communicate the list of #pragma omp parallel
16556 clauses to #pragma omp for instantiation if they are combined
16557 together. */
16558
16559 static tree *omp_parallel_combined_clauses;
16560
16561 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16562 tree *, unsigned int *);
16563
16564 /* Substitute one OMP_FOR iterator. */
16565
16566 static bool
16567 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16568 tree initv, tree condv, tree incrv, tree *clauses,
16569 tree args, tsubst_flags_t complain, tree in_decl,
16570 bool integral_constant_expression_p)
16571 {
16572 #define RECUR(NODE) \
16573 tsubst_expr ((NODE), args, complain, in_decl, \
16574 integral_constant_expression_p)
16575 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16576 bool ret = false;
16577
16578 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16579 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16580
16581 decl = TREE_OPERAND (init, 0);
16582 init = TREE_OPERAND (init, 1);
16583 tree decl_expr = NULL_TREE;
16584 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16585 if (range_for)
16586 {
16587 bool decomp = false;
16588 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16589 {
16590 tree v = DECL_VALUE_EXPR (decl);
16591 if (TREE_CODE (v) == ARRAY_REF
16592 && VAR_P (TREE_OPERAND (v, 0))
16593 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16594 {
16595 tree decomp_first = NULL_TREE;
16596 unsigned decomp_cnt = 0;
16597 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16598 maybe_push_decl (d);
16599 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16600 in_decl, &decomp_first, &decomp_cnt);
16601 decomp = true;
16602 if (d == error_mark_node)
16603 decl = error_mark_node;
16604 else
16605 for (unsigned int i = 0; i < decomp_cnt; i++)
16606 {
16607 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16608 {
16609 tree v = build_nt (ARRAY_REF, d,
16610 size_int (decomp_cnt - i - 1),
16611 NULL_TREE, NULL_TREE);
16612 SET_DECL_VALUE_EXPR (decomp_first, v);
16613 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16614 }
16615 fit_decomposition_lang_decl (decomp_first, d);
16616 decomp_first = DECL_CHAIN (decomp_first);
16617 }
16618 }
16619 }
16620 decl = tsubst_decl (decl, args, complain);
16621 if (!decomp)
16622 maybe_push_decl (decl);
16623 }
16624 else if (init && TREE_CODE (init) == DECL_EXPR)
16625 {
16626 /* We need to jump through some hoops to handle declarations in the
16627 init-statement, since we might need to handle auto deduction,
16628 but we need to keep control of initialization. */
16629 decl_expr = init;
16630 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16631 decl = tsubst_decl (decl, args, complain);
16632 }
16633 else
16634 {
16635 if (TREE_CODE (decl) == SCOPE_REF)
16636 {
16637 decl = RECUR (decl);
16638 if (TREE_CODE (decl) == COMPONENT_REF)
16639 {
16640 tree v = decl;
16641 while (v)
16642 switch (TREE_CODE (v))
16643 {
16644 case COMPONENT_REF:
16645 case MEM_REF:
16646 case INDIRECT_REF:
16647 CASE_CONVERT:
16648 case POINTER_PLUS_EXPR:
16649 v = TREE_OPERAND (v, 0);
16650 continue;
16651 case PARM_DECL:
16652 if (DECL_CONTEXT (v) == current_function_decl
16653 && DECL_ARTIFICIAL (v)
16654 && DECL_NAME (v) == this_identifier)
16655 {
16656 decl = TREE_OPERAND (decl, 1);
16657 decl = omp_privatize_field (decl, false);
16658 }
16659 /* FALLTHRU */
16660 default:
16661 v = NULL_TREE;
16662 break;
16663 }
16664 }
16665 }
16666 else
16667 decl = RECUR (decl);
16668 }
16669 init = RECUR (init);
16670
16671 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16672 {
16673 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16674 if (TREE_CODE (o) == TREE_LIST)
16675 TREE_VEC_ELT (orig_declv, i)
16676 = tree_cons (RECUR (TREE_PURPOSE (o)),
16677 RECUR (TREE_VALUE (o)),
16678 NULL_TREE);
16679 else
16680 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16681 }
16682
16683 if (range_for)
16684 {
16685 tree this_pre_body = NULL_TREE;
16686 tree orig_init = NULL_TREE;
16687 tree orig_decl = NULL_TREE;
16688 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16689 orig_init, cond, incr);
16690 if (orig_decl)
16691 {
16692 if (orig_declv == NULL_TREE)
16693 orig_declv = copy_node (declv);
16694 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16695 ret = true;
16696 }
16697 else if (orig_declv)
16698 TREE_VEC_ELT (orig_declv, i) = decl;
16699 }
16700
16701 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16702 if (!range_for && auto_node && init)
16703 TREE_TYPE (decl)
16704 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16705
16706 gcc_assert (!type_dependent_expression_p (decl));
16707
16708 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16709 {
16710 if (decl_expr)
16711 {
16712 /* Declare the variable, but don't let that initialize it. */
16713 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16714 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16715 RECUR (decl_expr);
16716 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16717 }
16718
16719 if (!range_for)
16720 {
16721 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16722 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16723 if (TREE_CODE (incr) == MODIFY_EXPR)
16724 {
16725 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16726 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16727 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16728 NOP_EXPR, rhs, complain);
16729 }
16730 else
16731 incr = RECUR (incr);
16732 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16733 TREE_VEC_ELT (orig_declv, i) = decl;
16734 }
16735 TREE_VEC_ELT (declv, i) = decl;
16736 TREE_VEC_ELT (initv, i) = init;
16737 TREE_VEC_ELT (condv, i) = cond;
16738 TREE_VEC_ELT (incrv, i) = incr;
16739 return ret;
16740 }
16741
16742 if (decl_expr)
16743 {
16744 /* Declare and initialize the variable. */
16745 RECUR (decl_expr);
16746 init = NULL_TREE;
16747 }
16748 else if (init)
16749 {
16750 tree *pc;
16751 int j;
16752 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16753 {
16754 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16755 {
16756 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16757 && OMP_CLAUSE_DECL (*pc) == decl)
16758 break;
16759 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16760 && OMP_CLAUSE_DECL (*pc) == decl)
16761 {
16762 if (j)
16763 break;
16764 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16765 tree c = *pc;
16766 *pc = OMP_CLAUSE_CHAIN (c);
16767 OMP_CLAUSE_CHAIN (c) = *clauses;
16768 *clauses = c;
16769 }
16770 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16771 && OMP_CLAUSE_DECL (*pc) == decl)
16772 {
16773 error ("iteration variable %qD should not be firstprivate",
16774 decl);
16775 *pc = OMP_CLAUSE_CHAIN (*pc);
16776 }
16777 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16778 && OMP_CLAUSE_DECL (*pc) == decl)
16779 {
16780 error ("iteration variable %qD should not be reduction",
16781 decl);
16782 *pc = OMP_CLAUSE_CHAIN (*pc);
16783 }
16784 else
16785 pc = &OMP_CLAUSE_CHAIN (*pc);
16786 }
16787 if (*pc)
16788 break;
16789 }
16790 if (*pc == NULL_TREE)
16791 {
16792 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16793 OMP_CLAUSE_DECL (c) = decl;
16794 c = finish_omp_clauses (c, C_ORT_OMP);
16795 if (c)
16796 {
16797 OMP_CLAUSE_CHAIN (c) = *clauses;
16798 *clauses = c;
16799 }
16800 }
16801 }
16802 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16803 if (COMPARISON_CLASS_P (cond))
16804 {
16805 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16806 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16807 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16808 }
16809 else
16810 cond = RECUR (cond);
16811 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16812 switch (TREE_CODE (incr))
16813 {
16814 case PREINCREMENT_EXPR:
16815 case PREDECREMENT_EXPR:
16816 case POSTINCREMENT_EXPR:
16817 case POSTDECREMENT_EXPR:
16818 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16819 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16820 break;
16821 case MODIFY_EXPR:
16822 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16823 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16824 {
16825 tree rhs = TREE_OPERAND (incr, 1);
16826 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16827 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16828 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16829 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16830 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16831 rhs0, rhs1));
16832 }
16833 else
16834 incr = RECUR (incr);
16835 break;
16836 case MODOP_EXPR:
16837 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16838 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16839 {
16840 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16841 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16842 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16843 TREE_TYPE (decl), lhs,
16844 RECUR (TREE_OPERAND (incr, 2))));
16845 }
16846 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16847 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16848 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16849 {
16850 tree rhs = TREE_OPERAND (incr, 2);
16851 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16852 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16853 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16854 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16855 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16856 rhs0, rhs1));
16857 }
16858 else
16859 incr = RECUR (incr);
16860 break;
16861 default:
16862 incr = RECUR (incr);
16863 break;
16864 }
16865
16866 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16867 TREE_VEC_ELT (orig_declv, i) = decl;
16868 TREE_VEC_ELT (declv, i) = decl;
16869 TREE_VEC_ELT (initv, i) = init;
16870 TREE_VEC_ELT (condv, i) = cond;
16871 TREE_VEC_ELT (incrv, i) = incr;
16872 return false;
16873 #undef RECUR
16874 }
16875
16876 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16877 of OMP_TARGET's body. */
16878
16879 static tree
16880 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16881 {
16882 *walk_subtrees = 0;
16883 switch (TREE_CODE (*tp))
16884 {
16885 case OMP_TEAMS:
16886 return *tp;
16887 case BIND_EXPR:
16888 case STATEMENT_LIST:
16889 *walk_subtrees = 1;
16890 break;
16891 default:
16892 break;
16893 }
16894 return NULL_TREE;
16895 }
16896
16897 /* Helper function for tsubst_expr. For decomposition declaration
16898 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16899 also the corresponding decls representing the identifiers
16900 of the decomposition declaration. Return DECL if successful
16901 or error_mark_node otherwise, set *FIRST to the first decl
16902 in the list chained through DECL_CHAIN and *CNT to the number
16903 of such decls. */
16904
16905 static tree
16906 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16907 tsubst_flags_t complain, tree in_decl, tree *first,
16908 unsigned int *cnt)
16909 {
16910 tree decl2, decl3, prev = decl;
16911 *cnt = 0;
16912 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16913 for (decl2 = DECL_CHAIN (pattern_decl);
16914 decl2
16915 && VAR_P (decl2)
16916 && DECL_DECOMPOSITION_P (decl2)
16917 && DECL_NAME (decl2);
16918 decl2 = DECL_CHAIN (decl2))
16919 {
16920 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16921 {
16922 gcc_assert (errorcount);
16923 return error_mark_node;
16924 }
16925 (*cnt)++;
16926 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16927 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16928 tree v = DECL_VALUE_EXPR (decl2);
16929 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16930 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16931 decl3 = tsubst (decl2, args, complain, in_decl);
16932 SET_DECL_VALUE_EXPR (decl2, v);
16933 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16934 if (VAR_P (decl3))
16935 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16936 else
16937 {
16938 gcc_assert (errorcount);
16939 decl = error_mark_node;
16940 continue;
16941 }
16942 maybe_push_decl (decl3);
16943 if (error_operand_p (decl3))
16944 decl = error_mark_node;
16945 else if (decl != error_mark_node
16946 && DECL_CHAIN (decl3) != prev
16947 && decl != prev)
16948 {
16949 gcc_assert (errorcount);
16950 decl = error_mark_node;
16951 }
16952 else
16953 prev = decl3;
16954 }
16955 *first = prev;
16956 return decl;
16957 }
16958
16959 /* Return the proper local_specialization for init-capture pack DECL. */
16960
16961 static tree
16962 lookup_init_capture_pack (tree decl)
16963 {
16964 /* We handle normal pack captures by forwarding to the specialization of the
16965 captured parameter. We can't do that for pack init-captures; we need them
16966 to have their own local_specialization. We created the individual
16967 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16968 when we process the DECL_EXPR for the pack init-capture in the template.
16969 So, how do we find them? We don't know the capture proxy pack when
16970 building the individual resulting proxies, and we don't know the
16971 individual proxies when instantiating the pack. What we have in common is
16972 the FIELD_DECL.
16973
16974 So...when we instantiate the FIELD_DECL, we stick the result in
16975 local_specializations. Then at the DECL_EXPR we look up that result, see
16976 how many elements it has, synthesize the names, and look them up. */
16977
16978 tree cname = DECL_NAME (decl);
16979 tree val = DECL_VALUE_EXPR (decl);
16980 tree field = TREE_OPERAND (val, 1);
16981 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16982 tree fpack = retrieve_local_specialization (field);
16983 if (fpack == error_mark_node)
16984 return error_mark_node;
16985
16986 int len = 1;
16987 tree vec = NULL_TREE;
16988 tree r = NULL_TREE;
16989 if (TREE_CODE (fpack) == TREE_VEC)
16990 {
16991 len = TREE_VEC_LENGTH (fpack);
16992 vec = make_tree_vec (len);
16993 r = make_node (NONTYPE_ARGUMENT_PACK);
16994 SET_ARGUMENT_PACK_ARGS (r, vec);
16995 }
16996 for (int i = 0; i < len; ++i)
16997 {
16998 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16999 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17000 if (vec)
17001 TREE_VEC_ELT (vec, i) = elt;
17002 else
17003 r = elt;
17004 }
17005 return r;
17006 }
17007
17008 /* Like tsubst_copy for expressions, etc. but also does semantic
17009 processing. */
17010
17011 tree
17012 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17013 bool integral_constant_expression_p)
17014 {
17015 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17016 #define RECUR(NODE) \
17017 tsubst_expr ((NODE), args, complain, in_decl, \
17018 integral_constant_expression_p)
17019
17020 tree stmt, tmp;
17021 tree r;
17022 location_t loc;
17023
17024 if (t == NULL_TREE || t == error_mark_node)
17025 return t;
17026
17027 loc = input_location;
17028 if (location_t eloc = cp_expr_location (t))
17029 input_location = eloc;
17030 if (STATEMENT_CODE_P (TREE_CODE (t)))
17031 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17032
17033 switch (TREE_CODE (t))
17034 {
17035 case STATEMENT_LIST:
17036 {
17037 tree_stmt_iterator i;
17038 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17039 RECUR (tsi_stmt (i));
17040 break;
17041 }
17042
17043 case CTOR_INITIALIZER:
17044 finish_mem_initializers (tsubst_initializer_list
17045 (TREE_OPERAND (t, 0), args));
17046 break;
17047
17048 case RETURN_EXPR:
17049 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17050 break;
17051
17052 case EXPR_STMT:
17053 tmp = RECUR (EXPR_STMT_EXPR (t));
17054 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17055 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17056 else
17057 finish_expr_stmt (tmp);
17058 break;
17059
17060 case USING_STMT:
17061 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17062 break;
17063
17064 case DECL_EXPR:
17065 {
17066 tree decl, pattern_decl;
17067 tree init;
17068
17069 pattern_decl = decl = DECL_EXPR_DECL (t);
17070 if (TREE_CODE (decl) == LABEL_DECL)
17071 finish_label_decl (DECL_NAME (decl));
17072 else if (TREE_CODE (decl) == USING_DECL)
17073 {
17074 tree scope = USING_DECL_SCOPE (decl);
17075 tree name = DECL_NAME (decl);
17076
17077 scope = tsubst (scope, args, complain, in_decl);
17078 finish_nonmember_using_decl (scope, name);
17079 }
17080 else if (is_capture_proxy (decl)
17081 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17082 {
17083 /* We're in tsubst_lambda_expr, we've already inserted a new
17084 capture proxy, so look it up and register it. */
17085 tree inst;
17086 if (!DECL_PACK_P (decl))
17087 {
17088 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17089 /*nonclass*/1, /*block_p=*/true,
17090 /*ns_only*/0, LOOKUP_HIDDEN);
17091 gcc_assert (inst != decl && is_capture_proxy (inst));
17092 }
17093 else if (is_normal_capture_proxy (decl))
17094 {
17095 inst = (retrieve_local_specialization
17096 (DECL_CAPTURED_VARIABLE (decl)));
17097 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17098 }
17099 else
17100 inst = lookup_init_capture_pack (decl);
17101
17102 register_local_specialization (inst, decl);
17103 break;
17104 }
17105 else if (DECL_PRETTY_FUNCTION_P (decl))
17106 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17107 DECL_NAME (decl),
17108 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17109 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17110 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17111 /* Don't copy the old closure; we'll create a new one in
17112 tsubst_lambda_expr. */
17113 break;
17114 else
17115 {
17116 init = DECL_INITIAL (decl);
17117 decl = tsubst (decl, args, complain, in_decl);
17118 if (decl != error_mark_node)
17119 {
17120 /* By marking the declaration as instantiated, we avoid
17121 trying to instantiate it. Since instantiate_decl can't
17122 handle local variables, and since we've already done
17123 all that needs to be done, that's the right thing to
17124 do. */
17125 if (VAR_P (decl))
17126 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17127 if (VAR_P (decl) && !DECL_NAME (decl)
17128 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17129 /* Anonymous aggregates are a special case. */
17130 finish_anon_union (decl);
17131 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17132 {
17133 DECL_CONTEXT (decl) = current_function_decl;
17134 if (DECL_NAME (decl) == this_identifier)
17135 {
17136 tree lam = DECL_CONTEXT (current_function_decl);
17137 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17138 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17139 }
17140 insert_capture_proxy (decl);
17141 }
17142 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17143 /* We already did a pushtag. */;
17144 else if (TREE_CODE (decl) == FUNCTION_DECL
17145 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17146 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17147 {
17148 DECL_CONTEXT (decl) = NULL_TREE;
17149 pushdecl (decl);
17150 DECL_CONTEXT (decl) = current_function_decl;
17151 cp_check_omp_declare_reduction (decl);
17152 }
17153 else
17154 {
17155 int const_init = false;
17156 unsigned int cnt = 0;
17157 tree first = NULL_TREE, ndecl = error_mark_node;
17158 maybe_push_decl (decl);
17159
17160 if (VAR_P (decl)
17161 && DECL_DECOMPOSITION_P (decl)
17162 && TREE_TYPE (pattern_decl) != error_mark_node)
17163 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17164 complain, in_decl, &first,
17165 &cnt);
17166
17167 init = tsubst_init (init, decl, args, complain, in_decl);
17168
17169 if (VAR_P (decl))
17170 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17171 (pattern_decl));
17172
17173 if (ndecl != error_mark_node)
17174 cp_maybe_mangle_decomp (ndecl, first, cnt);
17175
17176 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17177
17178 if (ndecl != error_mark_node)
17179 cp_finish_decomp (ndecl, first, cnt);
17180 }
17181 }
17182 }
17183
17184 break;
17185 }
17186
17187 case FOR_STMT:
17188 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17189 RECUR (FOR_INIT_STMT (t));
17190 finish_init_stmt (stmt);
17191 tmp = RECUR (FOR_COND (t));
17192 finish_for_cond (tmp, stmt, false, 0);
17193 tmp = RECUR (FOR_EXPR (t));
17194 finish_for_expr (tmp, stmt);
17195 {
17196 bool prev = note_iteration_stmt_body_start ();
17197 RECUR (FOR_BODY (t));
17198 note_iteration_stmt_body_end (prev);
17199 }
17200 finish_for_stmt (stmt);
17201 break;
17202
17203 case RANGE_FOR_STMT:
17204 {
17205 /* Construct another range_for, if this is not a final
17206 substitution (for inside inside a generic lambda of a
17207 template). Otherwise convert to a regular for. */
17208 tree decl, expr;
17209 stmt = (processing_template_decl
17210 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17211 : begin_for_stmt (NULL_TREE, NULL_TREE));
17212 RECUR (RANGE_FOR_INIT_STMT (t));
17213 decl = RANGE_FOR_DECL (t);
17214 decl = tsubst (decl, args, complain, in_decl);
17215 maybe_push_decl (decl);
17216 expr = RECUR (RANGE_FOR_EXPR (t));
17217
17218 tree decomp_first = NULL_TREE;
17219 unsigned decomp_cnt = 0;
17220 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17221 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17222 complain, in_decl,
17223 &decomp_first, &decomp_cnt);
17224
17225 if (processing_template_decl)
17226 {
17227 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17228 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17229 finish_range_for_decl (stmt, decl, expr);
17230 if (decomp_first && decl != error_mark_node)
17231 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17232 }
17233 else
17234 {
17235 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17236 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17237 stmt = cp_convert_range_for (stmt, decl, expr,
17238 decomp_first, decomp_cnt,
17239 RANGE_FOR_IVDEP (t), unroll);
17240 }
17241
17242 bool prev = note_iteration_stmt_body_start ();
17243 RECUR (RANGE_FOR_BODY (t));
17244 note_iteration_stmt_body_end (prev);
17245 finish_for_stmt (stmt);
17246 }
17247 break;
17248
17249 case WHILE_STMT:
17250 stmt = begin_while_stmt ();
17251 tmp = RECUR (WHILE_COND (t));
17252 finish_while_stmt_cond (tmp, stmt, false, 0);
17253 {
17254 bool prev = note_iteration_stmt_body_start ();
17255 RECUR (WHILE_BODY (t));
17256 note_iteration_stmt_body_end (prev);
17257 }
17258 finish_while_stmt (stmt);
17259 break;
17260
17261 case DO_STMT:
17262 stmt = begin_do_stmt ();
17263 {
17264 bool prev = note_iteration_stmt_body_start ();
17265 RECUR (DO_BODY (t));
17266 note_iteration_stmt_body_end (prev);
17267 }
17268 finish_do_body (stmt);
17269 tmp = RECUR (DO_COND (t));
17270 finish_do_stmt (tmp, stmt, false, 0);
17271 break;
17272
17273 case IF_STMT:
17274 stmt = begin_if_stmt ();
17275 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17276 if (IF_STMT_CONSTEXPR_P (t))
17277 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17278 tmp = RECUR (IF_COND (t));
17279 tmp = finish_if_stmt_cond (tmp, stmt);
17280 if (IF_STMT_CONSTEXPR_P (t)
17281 && instantiation_dependent_expression_p (tmp))
17282 {
17283 /* We're partially instantiating a generic lambda, but the condition
17284 of the constexpr if is still dependent. Don't substitute into the
17285 branches now, just remember the template arguments. */
17286 do_poplevel (IF_SCOPE (stmt));
17287 IF_COND (stmt) = IF_COND (t);
17288 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17289 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17290 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17291 add_stmt (stmt);
17292 break;
17293 }
17294 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17295 /* Don't instantiate the THEN_CLAUSE. */;
17296 else
17297 {
17298 tree folded = fold_non_dependent_expr (tmp, complain);
17299 bool inhibit = integer_zerop (folded);
17300 if (inhibit)
17301 ++c_inhibit_evaluation_warnings;
17302 RECUR (THEN_CLAUSE (t));
17303 if (inhibit)
17304 --c_inhibit_evaluation_warnings;
17305 }
17306 finish_then_clause (stmt);
17307
17308 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17309 /* Don't instantiate the ELSE_CLAUSE. */;
17310 else if (ELSE_CLAUSE (t))
17311 {
17312 tree folded = fold_non_dependent_expr (tmp, complain);
17313 bool inhibit = integer_nonzerop (folded);
17314 begin_else_clause (stmt);
17315 if (inhibit)
17316 ++c_inhibit_evaluation_warnings;
17317 RECUR (ELSE_CLAUSE (t));
17318 if (inhibit)
17319 --c_inhibit_evaluation_warnings;
17320 finish_else_clause (stmt);
17321 }
17322
17323 finish_if_stmt (stmt);
17324 break;
17325
17326 case BIND_EXPR:
17327 if (BIND_EXPR_BODY_BLOCK (t))
17328 stmt = begin_function_body ();
17329 else
17330 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17331 ? BCS_TRY_BLOCK : 0);
17332
17333 RECUR (BIND_EXPR_BODY (t));
17334
17335 if (BIND_EXPR_BODY_BLOCK (t))
17336 finish_function_body (stmt);
17337 else
17338 finish_compound_stmt (stmt);
17339 break;
17340
17341 case BREAK_STMT:
17342 finish_break_stmt ();
17343 break;
17344
17345 case CONTINUE_STMT:
17346 finish_continue_stmt ();
17347 break;
17348
17349 case SWITCH_STMT:
17350 stmt = begin_switch_stmt ();
17351 tmp = RECUR (SWITCH_STMT_COND (t));
17352 finish_switch_cond (tmp, stmt);
17353 RECUR (SWITCH_STMT_BODY (t));
17354 finish_switch_stmt (stmt);
17355 break;
17356
17357 case CASE_LABEL_EXPR:
17358 {
17359 tree decl = CASE_LABEL (t);
17360 tree low = RECUR (CASE_LOW (t));
17361 tree high = RECUR (CASE_HIGH (t));
17362 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17363 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17364 {
17365 tree label = CASE_LABEL (l);
17366 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17367 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17368 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17369 }
17370 }
17371 break;
17372
17373 case LABEL_EXPR:
17374 {
17375 tree decl = LABEL_EXPR_LABEL (t);
17376 tree label;
17377
17378 label = finish_label_stmt (DECL_NAME (decl));
17379 if (TREE_CODE (label) == LABEL_DECL)
17380 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17381 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17382 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17383 }
17384 break;
17385
17386 case GOTO_EXPR:
17387 tmp = GOTO_DESTINATION (t);
17388 if (TREE_CODE (tmp) != LABEL_DECL)
17389 /* Computed goto's must be tsubst'd into. On the other hand,
17390 non-computed gotos must not be; the identifier in question
17391 will have no binding. */
17392 tmp = RECUR (tmp);
17393 else
17394 tmp = DECL_NAME (tmp);
17395 finish_goto_stmt (tmp);
17396 break;
17397
17398 case ASM_EXPR:
17399 {
17400 tree string = RECUR (ASM_STRING (t));
17401 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17402 complain, in_decl);
17403 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17404 complain, in_decl);
17405 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17406 complain, in_decl);
17407 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17408 complain, in_decl);
17409 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17410 clobbers, labels, ASM_INLINE_P (t));
17411 tree asm_expr = tmp;
17412 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17413 asm_expr = TREE_OPERAND (asm_expr, 0);
17414 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17415 }
17416 break;
17417
17418 case TRY_BLOCK:
17419 if (CLEANUP_P (t))
17420 {
17421 stmt = begin_try_block ();
17422 RECUR (TRY_STMTS (t));
17423 finish_cleanup_try_block (stmt);
17424 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17425 }
17426 else
17427 {
17428 tree compound_stmt = NULL_TREE;
17429
17430 if (FN_TRY_BLOCK_P (t))
17431 stmt = begin_function_try_block (&compound_stmt);
17432 else
17433 stmt = begin_try_block ();
17434
17435 RECUR (TRY_STMTS (t));
17436
17437 if (FN_TRY_BLOCK_P (t))
17438 finish_function_try_block (stmt);
17439 else
17440 finish_try_block (stmt);
17441
17442 RECUR (TRY_HANDLERS (t));
17443 if (FN_TRY_BLOCK_P (t))
17444 finish_function_handler_sequence (stmt, compound_stmt);
17445 else
17446 finish_handler_sequence (stmt);
17447 }
17448 break;
17449
17450 case HANDLER:
17451 {
17452 tree decl = HANDLER_PARMS (t);
17453
17454 if (decl)
17455 {
17456 decl = tsubst (decl, args, complain, in_decl);
17457 /* Prevent instantiate_decl from trying to instantiate
17458 this variable. We've already done all that needs to be
17459 done. */
17460 if (decl != error_mark_node)
17461 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17462 }
17463 stmt = begin_handler ();
17464 finish_handler_parms (decl, stmt);
17465 RECUR (HANDLER_BODY (t));
17466 finish_handler (stmt);
17467 }
17468 break;
17469
17470 case TAG_DEFN:
17471 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17472 if (CLASS_TYPE_P (tmp))
17473 {
17474 /* Local classes are not independent templates; they are
17475 instantiated along with their containing function. And this
17476 way we don't have to deal with pushing out of one local class
17477 to instantiate a member of another local class. */
17478 /* Closures are handled by the LAMBDA_EXPR. */
17479 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17480 complete_type (tmp);
17481 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17482 if ((VAR_P (fld)
17483 || (TREE_CODE (fld) == FUNCTION_DECL
17484 && !DECL_ARTIFICIAL (fld)))
17485 && DECL_TEMPLATE_INSTANTIATION (fld))
17486 instantiate_decl (fld, /*defer_ok=*/false,
17487 /*expl_inst_class=*/false);
17488 }
17489 break;
17490
17491 case STATIC_ASSERT:
17492 {
17493 tree condition;
17494
17495 ++c_inhibit_evaluation_warnings;
17496 condition =
17497 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17498 args,
17499 complain, in_decl,
17500 /*integral_constant_expression_p=*/true);
17501 --c_inhibit_evaluation_warnings;
17502
17503 finish_static_assert (condition,
17504 STATIC_ASSERT_MESSAGE (t),
17505 STATIC_ASSERT_SOURCE_LOCATION (t),
17506 /*member_p=*/false);
17507 }
17508 break;
17509
17510 case OACC_KERNELS:
17511 case OACC_PARALLEL:
17512 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17513 in_decl);
17514 stmt = begin_omp_parallel ();
17515 RECUR (OMP_BODY (t));
17516 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17517 break;
17518
17519 case OMP_PARALLEL:
17520 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17521 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17522 complain, in_decl);
17523 if (OMP_PARALLEL_COMBINED (t))
17524 omp_parallel_combined_clauses = &tmp;
17525 stmt = begin_omp_parallel ();
17526 RECUR (OMP_PARALLEL_BODY (t));
17527 gcc_assert (omp_parallel_combined_clauses == NULL);
17528 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17529 = OMP_PARALLEL_COMBINED (t);
17530 pop_omp_privatization_clauses (r);
17531 break;
17532
17533 case OMP_TASK:
17534 if (OMP_TASK_BODY (t) == NULL_TREE)
17535 {
17536 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17537 complain, in_decl);
17538 t = copy_node (t);
17539 OMP_TASK_CLAUSES (t) = tmp;
17540 add_stmt (t);
17541 break;
17542 }
17543 r = push_omp_privatization_clauses (false);
17544 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17545 complain, in_decl);
17546 stmt = begin_omp_task ();
17547 RECUR (OMP_TASK_BODY (t));
17548 finish_omp_task (tmp, stmt);
17549 pop_omp_privatization_clauses (r);
17550 break;
17551
17552 case OMP_FOR:
17553 case OMP_SIMD:
17554 case OMP_DISTRIBUTE:
17555 case OMP_TASKLOOP:
17556 case OACC_LOOP:
17557 {
17558 tree clauses, body, pre_body;
17559 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17560 tree orig_declv = NULL_TREE;
17561 tree incrv = NULL_TREE;
17562 enum c_omp_region_type ort = C_ORT_OMP;
17563 bool any_range_for = false;
17564 int i;
17565
17566 if (TREE_CODE (t) == OACC_LOOP)
17567 ort = C_ORT_ACC;
17568
17569 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17570 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17571 in_decl);
17572 if (OMP_FOR_INIT (t) != NULL_TREE)
17573 {
17574 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17575 if (OMP_FOR_ORIG_DECLS (t))
17576 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17577 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17578 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17579 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17580 }
17581
17582 keep_next_level (true);
17583 stmt = begin_omp_structured_block ();
17584
17585 pre_body = push_stmt_list ();
17586 RECUR (OMP_FOR_PRE_BODY (t));
17587 pre_body = pop_stmt_list (pre_body);
17588
17589 if (OMP_FOR_INIT (t) != NULL_TREE)
17590 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17591 any_range_for
17592 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17593 condv, incrv, &clauses, args,
17594 complain, in_decl,
17595 integral_constant_expression_p);
17596 omp_parallel_combined_clauses = NULL;
17597
17598 if (any_range_for)
17599 {
17600 gcc_assert (orig_declv);
17601 body = begin_omp_structured_block ();
17602 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17603 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17604 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17605 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17606 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17607 TREE_VEC_ELT (declv, i));
17608 }
17609 else
17610 body = push_stmt_list ();
17611 RECUR (OMP_FOR_BODY (t));
17612 if (any_range_for)
17613 body = finish_omp_structured_block (body);
17614 else
17615 body = pop_stmt_list (body);
17616
17617 if (OMP_FOR_INIT (t) != NULL_TREE)
17618 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17619 orig_declv, initv, condv, incrv, body, pre_body,
17620 NULL, clauses);
17621 else
17622 {
17623 t = make_node (TREE_CODE (t));
17624 TREE_TYPE (t) = void_type_node;
17625 OMP_FOR_BODY (t) = body;
17626 OMP_FOR_PRE_BODY (t) = pre_body;
17627 OMP_FOR_CLAUSES (t) = clauses;
17628 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17629 add_stmt (t);
17630 }
17631
17632 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17633 t));
17634 pop_omp_privatization_clauses (r);
17635 }
17636 break;
17637
17638 case OMP_SECTIONS:
17639 omp_parallel_combined_clauses = NULL;
17640 /* FALLTHRU */
17641 case OMP_SINGLE:
17642 case OMP_TEAMS:
17643 case OMP_CRITICAL:
17644 case OMP_TASKGROUP:
17645 case OMP_SCAN:
17646 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17647 && OMP_TEAMS_COMBINED (t));
17648 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17649 in_decl);
17650 if (TREE_CODE (t) == OMP_TEAMS)
17651 {
17652 keep_next_level (true);
17653 stmt = begin_omp_structured_block ();
17654 RECUR (OMP_BODY (t));
17655 stmt = finish_omp_structured_block (stmt);
17656 }
17657 else
17658 {
17659 stmt = push_stmt_list ();
17660 RECUR (OMP_BODY (t));
17661 stmt = pop_stmt_list (stmt);
17662 }
17663
17664 t = copy_node (t);
17665 OMP_BODY (t) = stmt;
17666 OMP_CLAUSES (t) = tmp;
17667 add_stmt (t);
17668 pop_omp_privatization_clauses (r);
17669 break;
17670
17671 case OMP_DEPOBJ:
17672 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17673 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17674 {
17675 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17676 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17677 {
17678 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17679 args, complain, in_decl);
17680 if (tmp == NULL_TREE)
17681 tmp = error_mark_node;
17682 }
17683 else
17684 {
17685 kind = (enum omp_clause_depend_kind)
17686 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17687 tmp = NULL_TREE;
17688 }
17689 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17690 }
17691 else
17692 finish_omp_depobj (EXPR_LOCATION (t), r,
17693 OMP_CLAUSE_DEPEND_SOURCE,
17694 OMP_DEPOBJ_CLAUSES (t));
17695 break;
17696
17697 case OACC_DATA:
17698 case OMP_TARGET_DATA:
17699 case OMP_TARGET:
17700 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17701 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17702 in_decl);
17703 keep_next_level (true);
17704 stmt = begin_omp_structured_block ();
17705
17706 RECUR (OMP_BODY (t));
17707 stmt = finish_omp_structured_block (stmt);
17708
17709 t = copy_node (t);
17710 OMP_BODY (t) = stmt;
17711 OMP_CLAUSES (t) = tmp;
17712 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17713 {
17714 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17715 if (teams)
17716 {
17717 /* For combined target teams, ensure the num_teams and
17718 thread_limit clause expressions are evaluated on the host,
17719 before entering the target construct. */
17720 tree c;
17721 for (c = OMP_TEAMS_CLAUSES (teams);
17722 c; c = OMP_CLAUSE_CHAIN (c))
17723 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17724 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17725 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17726 {
17727 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17728 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17729 if (expr == error_mark_node)
17730 continue;
17731 tmp = TARGET_EXPR_SLOT (expr);
17732 add_stmt (expr);
17733 OMP_CLAUSE_OPERAND (c, 0) = expr;
17734 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17735 OMP_CLAUSE_FIRSTPRIVATE);
17736 OMP_CLAUSE_DECL (tc) = tmp;
17737 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17738 OMP_TARGET_CLAUSES (t) = tc;
17739 }
17740 }
17741 }
17742 add_stmt (t);
17743 break;
17744
17745 case OACC_DECLARE:
17746 t = copy_node (t);
17747 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17748 complain, in_decl);
17749 OACC_DECLARE_CLAUSES (t) = tmp;
17750 add_stmt (t);
17751 break;
17752
17753 case OMP_TARGET_UPDATE:
17754 case OMP_TARGET_ENTER_DATA:
17755 case OMP_TARGET_EXIT_DATA:
17756 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17757 complain, in_decl);
17758 t = copy_node (t);
17759 OMP_STANDALONE_CLAUSES (t) = tmp;
17760 add_stmt (t);
17761 break;
17762
17763 case OACC_ENTER_DATA:
17764 case OACC_EXIT_DATA:
17765 case OACC_UPDATE:
17766 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17767 complain, in_decl);
17768 t = copy_node (t);
17769 OMP_STANDALONE_CLAUSES (t) = tmp;
17770 add_stmt (t);
17771 break;
17772
17773 case OMP_ORDERED:
17774 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17775 complain, in_decl);
17776 stmt = push_stmt_list ();
17777 RECUR (OMP_BODY (t));
17778 stmt = pop_stmt_list (stmt);
17779
17780 t = copy_node (t);
17781 OMP_BODY (t) = stmt;
17782 OMP_ORDERED_CLAUSES (t) = tmp;
17783 add_stmt (t);
17784 break;
17785
17786 case OMP_SECTION:
17787 case OMP_MASTER:
17788 stmt = push_stmt_list ();
17789 RECUR (OMP_BODY (t));
17790 stmt = pop_stmt_list (stmt);
17791
17792 t = copy_node (t);
17793 OMP_BODY (t) = stmt;
17794 add_stmt (t);
17795 break;
17796
17797 case OMP_ATOMIC:
17798 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17799 tmp = NULL_TREE;
17800 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17801 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17802 complain, in_decl);
17803 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17804 {
17805 tree op1 = TREE_OPERAND (t, 1);
17806 tree rhs1 = NULL_TREE;
17807 tree lhs, rhs;
17808 if (TREE_CODE (op1) == COMPOUND_EXPR)
17809 {
17810 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17811 op1 = TREE_OPERAND (op1, 1);
17812 }
17813 lhs = RECUR (TREE_OPERAND (op1, 0));
17814 rhs = RECUR (TREE_OPERAND (op1, 1));
17815 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17816 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17817 OMP_ATOMIC_MEMORY_ORDER (t));
17818 }
17819 else
17820 {
17821 tree op1 = TREE_OPERAND (t, 1);
17822 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17823 tree rhs1 = NULL_TREE;
17824 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17825 enum tree_code opcode = NOP_EXPR;
17826 if (code == OMP_ATOMIC_READ)
17827 {
17828 v = RECUR (TREE_OPERAND (op1, 0));
17829 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17830 }
17831 else if (code == OMP_ATOMIC_CAPTURE_OLD
17832 || code == OMP_ATOMIC_CAPTURE_NEW)
17833 {
17834 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17835 v = RECUR (TREE_OPERAND (op1, 0));
17836 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17837 if (TREE_CODE (op11) == COMPOUND_EXPR)
17838 {
17839 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17840 op11 = TREE_OPERAND (op11, 1);
17841 }
17842 lhs = RECUR (TREE_OPERAND (op11, 0));
17843 rhs = RECUR (TREE_OPERAND (op11, 1));
17844 opcode = TREE_CODE (op11);
17845 if (opcode == MODIFY_EXPR)
17846 opcode = NOP_EXPR;
17847 }
17848 else
17849 {
17850 code = OMP_ATOMIC;
17851 lhs = RECUR (TREE_OPERAND (op1, 0));
17852 rhs = RECUR (TREE_OPERAND (op1, 1));
17853 }
17854 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17855 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17856 }
17857 break;
17858
17859 case TRANSACTION_EXPR:
17860 {
17861 int flags = 0;
17862 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17863 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17864
17865 if (TRANSACTION_EXPR_IS_STMT (t))
17866 {
17867 tree body = TRANSACTION_EXPR_BODY (t);
17868 tree noex = NULL_TREE;
17869 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17870 {
17871 noex = MUST_NOT_THROW_COND (body);
17872 if (noex == NULL_TREE)
17873 noex = boolean_true_node;
17874 body = TREE_OPERAND (body, 0);
17875 }
17876 stmt = begin_transaction_stmt (input_location, NULL, flags);
17877 RECUR (body);
17878 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17879 }
17880 else
17881 {
17882 stmt = build_transaction_expr (EXPR_LOCATION (t),
17883 RECUR (TRANSACTION_EXPR_BODY (t)),
17884 flags, NULL_TREE);
17885 RETURN (stmt);
17886 }
17887 }
17888 break;
17889
17890 case MUST_NOT_THROW_EXPR:
17891 {
17892 tree op0 = RECUR (TREE_OPERAND (t, 0));
17893 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17894 RETURN (build_must_not_throw_expr (op0, cond));
17895 }
17896
17897 case EXPR_PACK_EXPANSION:
17898 error ("invalid use of pack expansion expression");
17899 RETURN (error_mark_node);
17900
17901 case NONTYPE_ARGUMENT_PACK:
17902 error ("use %<...%> to expand argument pack");
17903 RETURN (error_mark_node);
17904
17905 case COMPOUND_EXPR:
17906 tmp = RECUR (TREE_OPERAND (t, 0));
17907 if (tmp == NULL_TREE)
17908 /* If the first operand was a statement, we're done with it. */
17909 RETURN (RECUR (TREE_OPERAND (t, 1)));
17910 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17911 RECUR (TREE_OPERAND (t, 1)),
17912 complain));
17913
17914 case ANNOTATE_EXPR:
17915 tmp = RECUR (TREE_OPERAND (t, 0));
17916 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17917 TREE_TYPE (tmp), tmp,
17918 RECUR (TREE_OPERAND (t, 1)),
17919 RECUR (TREE_OPERAND (t, 2))));
17920
17921 case PREDICT_EXPR:
17922 RETURN (add_stmt (copy_node (t)));
17923
17924 default:
17925 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17926
17927 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17928 /*function_p=*/false,
17929 integral_constant_expression_p));
17930 }
17931
17932 RETURN (NULL_TREE);
17933 out:
17934 input_location = loc;
17935 return r;
17936 #undef RECUR
17937 #undef RETURN
17938 }
17939
17940 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17941 function. For description of the body see comment above
17942 cp_parser_omp_declare_reduction_exprs. */
17943
17944 static void
17945 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17946 {
17947 if (t == NULL_TREE || t == error_mark_node)
17948 return;
17949
17950 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17951
17952 tree_stmt_iterator tsi;
17953 int i;
17954 tree stmts[7];
17955 memset (stmts, 0, sizeof stmts);
17956 for (i = 0, tsi = tsi_start (t);
17957 i < 7 && !tsi_end_p (tsi);
17958 i++, tsi_next (&tsi))
17959 stmts[i] = tsi_stmt (tsi);
17960 gcc_assert (tsi_end_p (tsi));
17961
17962 if (i >= 3)
17963 {
17964 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17965 && TREE_CODE (stmts[1]) == DECL_EXPR);
17966 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17967 args, complain, in_decl);
17968 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17969 args, complain, in_decl);
17970 DECL_CONTEXT (omp_out) = current_function_decl;
17971 DECL_CONTEXT (omp_in) = current_function_decl;
17972 keep_next_level (true);
17973 tree block = begin_omp_structured_block ();
17974 tsubst_expr (stmts[2], args, complain, in_decl, false);
17975 block = finish_omp_structured_block (block);
17976 block = maybe_cleanup_point_expr_void (block);
17977 add_decl_expr (omp_out);
17978 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17979 TREE_NO_WARNING (omp_out) = 1;
17980 add_decl_expr (omp_in);
17981 finish_expr_stmt (block);
17982 }
17983 if (i >= 6)
17984 {
17985 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17986 && TREE_CODE (stmts[4]) == DECL_EXPR);
17987 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17988 args, complain, in_decl);
17989 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17990 args, complain, in_decl);
17991 DECL_CONTEXT (omp_priv) = current_function_decl;
17992 DECL_CONTEXT (omp_orig) = current_function_decl;
17993 keep_next_level (true);
17994 tree block = begin_omp_structured_block ();
17995 tsubst_expr (stmts[5], args, complain, in_decl, false);
17996 block = finish_omp_structured_block (block);
17997 block = maybe_cleanup_point_expr_void (block);
17998 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17999 add_decl_expr (omp_priv);
18000 add_decl_expr (omp_orig);
18001 finish_expr_stmt (block);
18002 if (i == 7)
18003 add_decl_expr (omp_orig);
18004 }
18005 }
18006
18007 /* T is a postfix-expression that is not being used in a function
18008 call. Return the substituted version of T. */
18009
18010 static tree
18011 tsubst_non_call_postfix_expression (tree t, tree args,
18012 tsubst_flags_t complain,
18013 tree in_decl)
18014 {
18015 if (TREE_CODE (t) == SCOPE_REF)
18016 t = tsubst_qualified_id (t, args, complain, in_decl,
18017 /*done=*/false, /*address_p=*/false);
18018 else
18019 t = tsubst_copy_and_build (t, args, complain, in_decl,
18020 /*function_p=*/false,
18021 /*integral_constant_expression_p=*/false);
18022
18023 return t;
18024 }
18025
18026 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18027 instantiation context. Instantiating a pack expansion containing a lambda
18028 might result in multiple lambdas all based on the same lambda in the
18029 template. */
18030
18031 tree
18032 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18033 {
18034 tree oldfn = lambda_function (t);
18035 in_decl = oldfn;
18036
18037 /* If we have already specialized this lambda expr, reuse it. See
18038 PR c++/87322. */
18039 if (local_specializations)
18040 if (tree r = retrieve_local_specialization (t))
18041 return r;
18042
18043 tree r = build_lambda_expr ();
18044
18045 if (local_specializations)
18046 register_local_specialization (r, t);
18047
18048 LAMBDA_EXPR_LOCATION (r)
18049 = LAMBDA_EXPR_LOCATION (t);
18050 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18051 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18052 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18053 LAMBDA_EXPR_INSTANTIATED (r) = true;
18054
18055 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18056 /* A lambda in a default argument outside a class gets no
18057 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18058 tsubst_default_argument calls start_lambda_scope, so we need to
18059 specifically ignore it here, and use the global scope. */
18060 record_null_lambda_scope (r);
18061 else
18062 record_lambda_scope (r);
18063
18064 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18065 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18066
18067 vec<tree,va_gc>* field_packs = NULL;
18068
18069 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18070 cap = TREE_CHAIN (cap))
18071 {
18072 tree ofield = TREE_PURPOSE (cap);
18073 if (PACK_EXPANSION_P (ofield))
18074 ofield = PACK_EXPANSION_PATTERN (ofield);
18075 tree field = tsubst_decl (ofield, args, complain);
18076
18077 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18078 {
18079 /* Remember these for when we've pushed local_specializations. */
18080 vec_safe_push (field_packs, ofield);
18081 vec_safe_push (field_packs, field);
18082 }
18083
18084 if (field == error_mark_node)
18085 return error_mark_node;
18086
18087 tree init = TREE_VALUE (cap);
18088 if (PACK_EXPANSION_P (init))
18089 init = tsubst_pack_expansion (init, args, complain, in_decl);
18090 else
18091 init = tsubst_copy_and_build (init, args, complain, in_decl,
18092 /*fn*/false, /*constexpr*/false);
18093
18094 if (TREE_CODE (field) == TREE_VEC)
18095 {
18096 int len = TREE_VEC_LENGTH (field);
18097 gcc_assert (TREE_CODE (init) == TREE_VEC
18098 && TREE_VEC_LENGTH (init) == len);
18099 for (int i = 0; i < len; ++i)
18100 LAMBDA_EXPR_CAPTURE_LIST (r)
18101 = tree_cons (TREE_VEC_ELT (field, i),
18102 TREE_VEC_ELT (init, i),
18103 LAMBDA_EXPR_CAPTURE_LIST (r));
18104 }
18105 else
18106 {
18107 LAMBDA_EXPR_CAPTURE_LIST (r)
18108 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
18109
18110 if (id_equal (DECL_NAME (field), "__this"))
18111 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18112 }
18113 }
18114
18115 tree type = begin_lambda_type (r);
18116 if (type == error_mark_node)
18117 return error_mark_node;
18118
18119 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18120 determine_visibility (TYPE_NAME (type));
18121
18122 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18123
18124 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18125 ? DECL_TI_TEMPLATE (oldfn)
18126 : NULL_TREE);
18127
18128 tree fntype = static_fn_type (oldfn);
18129 if (oldtmpl)
18130 ++processing_template_decl;
18131 fntype = tsubst (fntype, args, complain, in_decl);
18132 if (oldtmpl)
18133 --processing_template_decl;
18134
18135 if (fntype == error_mark_node)
18136 r = error_mark_node;
18137 else
18138 {
18139 /* The body of a lambda-expression is not a subexpression of the
18140 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18141 which would be skipped if cp_unevaluated_operand. */
18142 cp_evaluated ev;
18143
18144 /* Fix the type of 'this'. */
18145 fntype = build_memfn_type (fntype, type,
18146 type_memfn_quals (fntype),
18147 type_memfn_rqual (fntype));
18148 tree fn, tmpl;
18149 if (oldtmpl)
18150 {
18151 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18152 fn = DECL_TEMPLATE_RESULT (tmpl);
18153 finish_member_declaration (tmpl);
18154 }
18155 else
18156 {
18157 tmpl = NULL_TREE;
18158 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18159 finish_member_declaration (fn);
18160 }
18161
18162 /* Let finish_function set this. */
18163 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18164
18165 bool nested = cfun;
18166 if (nested)
18167 push_function_context ();
18168 else
18169 /* Still increment function_depth so that we don't GC in the
18170 middle of an expression. */
18171 ++function_depth;
18172
18173 local_specialization_stack s (lss_copy);
18174
18175 tree body = start_lambda_function (fn, r);
18176
18177 /* Now record them for lookup_init_capture_pack. */
18178 int fplen = vec_safe_length (field_packs);
18179 for (int i = 0; i < fplen; )
18180 {
18181 tree pack = (*field_packs)[i++];
18182 tree inst = (*field_packs)[i++];
18183 register_local_specialization (inst, pack);
18184 }
18185 release_tree_vector (field_packs);
18186
18187 register_parameter_specializations (oldfn, fn);
18188
18189 if (oldtmpl)
18190 {
18191 /* We might not partially instantiate some parts of the function, so
18192 copy these flags from the original template. */
18193 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18194 current_function_returns_value = ol->returns_value;
18195 current_function_returns_null = ol->returns_null;
18196 current_function_returns_abnormally = ol->returns_abnormally;
18197 current_function_infinite_loop = ol->infinite_loop;
18198 }
18199
18200 /* [temp.deduct] A lambda-expression appearing in a function type or a
18201 template parameter is not considered part of the immediate context for
18202 the purposes of template argument deduction. */
18203 complain = tf_warning_or_error;
18204
18205 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18206 /*constexpr*/false);
18207
18208 finish_lambda_function (body);
18209
18210 if (nested)
18211 pop_function_context ();
18212 else
18213 --function_depth;
18214
18215 /* The capture list was built up in reverse order; fix that now. */
18216 LAMBDA_EXPR_CAPTURE_LIST (r)
18217 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18218
18219 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18220
18221 maybe_add_lambda_conv_op (type);
18222 }
18223
18224 finish_struct (type, /*attr*/NULL_TREE);
18225
18226 insert_pending_capture_proxies ();
18227
18228 return r;
18229 }
18230
18231 /* Like tsubst but deals with expressions and performs semantic
18232 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18233
18234 tree
18235 tsubst_copy_and_build (tree t,
18236 tree args,
18237 tsubst_flags_t complain,
18238 tree in_decl,
18239 bool function_p,
18240 bool integral_constant_expression_p)
18241 {
18242 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18243 #define RECUR(NODE) \
18244 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18245 /*function_p=*/false, \
18246 integral_constant_expression_p)
18247
18248 tree retval, op1;
18249 location_t loc;
18250
18251 if (t == NULL_TREE || t == error_mark_node)
18252 return t;
18253
18254 loc = input_location;
18255 if (location_t eloc = cp_expr_location (t))
18256 input_location = eloc;
18257
18258 /* N3276 decltype magic only applies to calls at the top level or on the
18259 right side of a comma. */
18260 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18261 complain &= ~tf_decltype;
18262
18263 switch (TREE_CODE (t))
18264 {
18265 case USING_DECL:
18266 t = DECL_NAME (t);
18267 /* Fall through. */
18268 case IDENTIFIER_NODE:
18269 {
18270 tree decl;
18271 cp_id_kind idk;
18272 bool non_integral_constant_expression_p;
18273 const char *error_msg;
18274
18275 if (IDENTIFIER_CONV_OP_P (t))
18276 {
18277 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18278 t = make_conv_op_name (new_type);
18279 }
18280
18281 /* Look up the name. */
18282 decl = lookup_name (t);
18283
18284 /* By convention, expressions use ERROR_MARK_NODE to indicate
18285 failure, not NULL_TREE. */
18286 if (decl == NULL_TREE)
18287 decl = error_mark_node;
18288
18289 decl = finish_id_expression (t, decl, NULL_TREE,
18290 &idk,
18291 integral_constant_expression_p,
18292 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18293 &non_integral_constant_expression_p,
18294 /*template_p=*/false,
18295 /*done=*/true,
18296 /*address_p=*/false,
18297 /*template_arg_p=*/false,
18298 &error_msg,
18299 input_location);
18300 if (error_msg)
18301 error (error_msg);
18302 if (!function_p && identifier_p (decl))
18303 {
18304 if (complain & tf_error)
18305 unqualified_name_lookup_error (decl);
18306 decl = error_mark_node;
18307 }
18308 RETURN (decl);
18309 }
18310
18311 case TEMPLATE_ID_EXPR:
18312 {
18313 tree object;
18314 tree templ = RECUR (TREE_OPERAND (t, 0));
18315 tree targs = TREE_OPERAND (t, 1);
18316
18317 if (targs)
18318 targs = tsubst_template_args (targs, args, complain, in_decl);
18319 if (targs == error_mark_node)
18320 RETURN (error_mark_node);
18321
18322 if (TREE_CODE (templ) == SCOPE_REF)
18323 {
18324 tree name = TREE_OPERAND (templ, 1);
18325 tree tid = lookup_template_function (name, targs);
18326 TREE_OPERAND (templ, 1) = tid;
18327 RETURN (templ);
18328 }
18329
18330 if (variable_template_p (templ))
18331 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18332
18333 if (TREE_CODE (templ) == COMPONENT_REF)
18334 {
18335 object = TREE_OPERAND (templ, 0);
18336 templ = TREE_OPERAND (templ, 1);
18337 }
18338 else
18339 object = NULL_TREE;
18340 templ = lookup_template_function (templ, targs);
18341
18342 if (object)
18343 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18344 object, templ, NULL_TREE));
18345 else
18346 RETURN (baselink_for_fns (templ));
18347 }
18348
18349 case INDIRECT_REF:
18350 {
18351 tree r = RECUR (TREE_OPERAND (t, 0));
18352
18353 if (REFERENCE_REF_P (t))
18354 {
18355 /* A type conversion to reference type will be enclosed in
18356 such an indirect ref, but the substitution of the cast
18357 will have also added such an indirect ref. */
18358 r = convert_from_reference (r);
18359 }
18360 else
18361 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18362 complain|decltype_flag);
18363
18364 if (REF_PARENTHESIZED_P (t))
18365 r = force_paren_expr (r);
18366
18367 RETURN (r);
18368 }
18369
18370 case NOP_EXPR:
18371 {
18372 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18373 tree op0 = RECUR (TREE_OPERAND (t, 0));
18374 RETURN (build_nop (type, op0));
18375 }
18376
18377 case IMPLICIT_CONV_EXPR:
18378 {
18379 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18380 tree expr = RECUR (TREE_OPERAND (t, 0));
18381 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18382 {
18383 retval = copy_node (t);
18384 TREE_TYPE (retval) = type;
18385 TREE_OPERAND (retval, 0) = expr;
18386 RETURN (retval);
18387 }
18388 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18389 /* We'll pass this to convert_nontype_argument again, we don't need
18390 to actually perform any conversion here. */
18391 RETURN (expr);
18392 int flags = LOOKUP_IMPLICIT;
18393 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18394 flags = LOOKUP_NORMAL;
18395 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18396 flags |= LOOKUP_NO_NARROWING;
18397 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18398 flags));
18399 }
18400
18401 case CONVERT_EXPR:
18402 {
18403 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18404 tree op0 = RECUR (TREE_OPERAND (t, 0));
18405 if (op0 == error_mark_node)
18406 RETURN (error_mark_node);
18407 RETURN (build1 (CONVERT_EXPR, type, op0));
18408 }
18409
18410 case CAST_EXPR:
18411 case REINTERPRET_CAST_EXPR:
18412 case CONST_CAST_EXPR:
18413 case DYNAMIC_CAST_EXPR:
18414 case STATIC_CAST_EXPR:
18415 {
18416 tree type;
18417 tree op, r = NULL_TREE;
18418
18419 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18420 if (integral_constant_expression_p
18421 && !cast_valid_in_integral_constant_expression_p (type))
18422 {
18423 if (complain & tf_error)
18424 error ("a cast to a type other than an integral or "
18425 "enumeration type cannot appear in a constant-expression");
18426 RETURN (error_mark_node);
18427 }
18428
18429 op = RECUR (TREE_OPERAND (t, 0));
18430
18431 warning_sentinel s(warn_useless_cast);
18432 warning_sentinel s2(warn_ignored_qualifiers);
18433 switch (TREE_CODE (t))
18434 {
18435 case CAST_EXPR:
18436 r = build_functional_cast (type, op, complain);
18437 break;
18438 case REINTERPRET_CAST_EXPR:
18439 r = build_reinterpret_cast (type, op, complain);
18440 break;
18441 case CONST_CAST_EXPR:
18442 r = build_const_cast (type, op, complain);
18443 break;
18444 case DYNAMIC_CAST_EXPR:
18445 r = build_dynamic_cast (type, op, complain);
18446 break;
18447 case STATIC_CAST_EXPR:
18448 r = build_static_cast (type, op, complain);
18449 break;
18450 default:
18451 gcc_unreachable ();
18452 }
18453
18454 RETURN (r);
18455 }
18456
18457 case POSTDECREMENT_EXPR:
18458 case POSTINCREMENT_EXPR:
18459 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18460 args, complain, in_decl);
18461 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18462 complain|decltype_flag));
18463
18464 case PREDECREMENT_EXPR:
18465 case PREINCREMENT_EXPR:
18466 case NEGATE_EXPR:
18467 case BIT_NOT_EXPR:
18468 case ABS_EXPR:
18469 case TRUTH_NOT_EXPR:
18470 case UNARY_PLUS_EXPR: /* Unary + */
18471 case REALPART_EXPR:
18472 case IMAGPART_EXPR:
18473 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18474 RECUR (TREE_OPERAND (t, 0)),
18475 complain|decltype_flag));
18476
18477 case FIX_TRUNC_EXPR:
18478 gcc_unreachable ();
18479
18480 case ADDR_EXPR:
18481 op1 = TREE_OPERAND (t, 0);
18482 if (TREE_CODE (op1) == LABEL_DECL)
18483 RETURN (finish_label_address_expr (DECL_NAME (op1),
18484 EXPR_LOCATION (op1)));
18485 if (TREE_CODE (op1) == SCOPE_REF)
18486 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18487 /*done=*/true, /*address_p=*/true);
18488 else
18489 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18490 in_decl);
18491 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18492 complain|decltype_flag));
18493
18494 case PLUS_EXPR:
18495 case MINUS_EXPR:
18496 case MULT_EXPR:
18497 case TRUNC_DIV_EXPR:
18498 case CEIL_DIV_EXPR:
18499 case FLOOR_DIV_EXPR:
18500 case ROUND_DIV_EXPR:
18501 case EXACT_DIV_EXPR:
18502 case BIT_AND_EXPR:
18503 case BIT_IOR_EXPR:
18504 case BIT_XOR_EXPR:
18505 case TRUNC_MOD_EXPR:
18506 case FLOOR_MOD_EXPR:
18507 case TRUTH_ANDIF_EXPR:
18508 case TRUTH_ORIF_EXPR:
18509 case TRUTH_AND_EXPR:
18510 case TRUTH_OR_EXPR:
18511 case RSHIFT_EXPR:
18512 case LSHIFT_EXPR:
18513 case RROTATE_EXPR:
18514 case LROTATE_EXPR:
18515 case EQ_EXPR:
18516 case NE_EXPR:
18517 case MAX_EXPR:
18518 case MIN_EXPR:
18519 case LE_EXPR:
18520 case GE_EXPR:
18521 case LT_EXPR:
18522 case GT_EXPR:
18523 case MEMBER_REF:
18524 case DOTSTAR_EXPR:
18525 {
18526 warning_sentinel s1(warn_type_limits);
18527 warning_sentinel s2(warn_div_by_zero);
18528 warning_sentinel s3(warn_logical_op);
18529 warning_sentinel s4(warn_tautological_compare);
18530 tree op0 = RECUR (TREE_OPERAND (t, 0));
18531 tree op1 = RECUR (TREE_OPERAND (t, 1));
18532 tree r = build_x_binary_op
18533 (input_location, TREE_CODE (t),
18534 op0,
18535 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18536 ? ERROR_MARK
18537 : TREE_CODE (TREE_OPERAND (t, 0))),
18538 op1,
18539 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18540 ? ERROR_MARK
18541 : TREE_CODE (TREE_OPERAND (t, 1))),
18542 /*overload=*/NULL,
18543 complain|decltype_flag);
18544 if (EXPR_P (r) && TREE_NO_WARNING (t))
18545 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18546
18547 RETURN (r);
18548 }
18549
18550 case POINTER_PLUS_EXPR:
18551 {
18552 tree op0 = RECUR (TREE_OPERAND (t, 0));
18553 tree op1 = RECUR (TREE_OPERAND (t, 1));
18554 RETURN (fold_build_pointer_plus (op0, op1));
18555 }
18556
18557 case SCOPE_REF:
18558 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18559 /*address_p=*/false));
18560 case ARRAY_REF:
18561 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18562 args, complain, in_decl);
18563 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18564 RECUR (TREE_OPERAND (t, 1)),
18565 complain|decltype_flag));
18566
18567 case SIZEOF_EXPR:
18568 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18569 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18570 RETURN (tsubst_copy (t, args, complain, in_decl));
18571 /* Fall through */
18572
18573 case ALIGNOF_EXPR:
18574 {
18575 tree r;
18576
18577 op1 = TREE_OPERAND (t, 0);
18578 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18579 op1 = TREE_TYPE (op1);
18580 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18581 && ALIGNOF_EXPR_STD_P (t));
18582 if (!args)
18583 {
18584 /* When there are no ARGS, we are trying to evaluate a
18585 non-dependent expression from the parser. Trying to do
18586 the substitutions may not work. */
18587 if (!TYPE_P (op1))
18588 op1 = TREE_TYPE (op1);
18589 }
18590 else
18591 {
18592 ++cp_unevaluated_operand;
18593 ++c_inhibit_evaluation_warnings;
18594 if (TYPE_P (op1))
18595 op1 = tsubst (op1, args, complain, in_decl);
18596 else
18597 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18598 /*function_p=*/false,
18599 /*integral_constant_expression_p=*/
18600 false);
18601 --cp_unevaluated_operand;
18602 --c_inhibit_evaluation_warnings;
18603 }
18604 if (TYPE_P (op1))
18605 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18606 complain & tf_error);
18607 else
18608 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18609 complain & tf_error);
18610 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18611 {
18612 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18613 {
18614 if (!processing_template_decl && TYPE_P (op1))
18615 {
18616 r = build_min (SIZEOF_EXPR, size_type_node,
18617 build1 (NOP_EXPR, op1, error_mark_node));
18618 SIZEOF_EXPR_TYPE_P (r) = 1;
18619 }
18620 else
18621 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18622 TREE_SIDE_EFFECTS (r) = 0;
18623 TREE_READONLY (r) = 1;
18624 }
18625 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18626 }
18627 RETURN (r);
18628 }
18629
18630 case AT_ENCODE_EXPR:
18631 {
18632 op1 = TREE_OPERAND (t, 0);
18633 ++cp_unevaluated_operand;
18634 ++c_inhibit_evaluation_warnings;
18635 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18636 /*function_p=*/false,
18637 /*integral_constant_expression_p=*/false);
18638 --cp_unevaluated_operand;
18639 --c_inhibit_evaluation_warnings;
18640 RETURN (objc_build_encode_expr (op1));
18641 }
18642
18643 case NOEXCEPT_EXPR:
18644 op1 = TREE_OPERAND (t, 0);
18645 ++cp_unevaluated_operand;
18646 ++c_inhibit_evaluation_warnings;
18647 ++cp_noexcept_operand;
18648 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18649 /*function_p=*/false,
18650 /*integral_constant_expression_p=*/false);
18651 --cp_unevaluated_operand;
18652 --c_inhibit_evaluation_warnings;
18653 --cp_noexcept_operand;
18654 RETURN (finish_noexcept_expr (op1, complain));
18655
18656 case MODOP_EXPR:
18657 {
18658 warning_sentinel s(warn_div_by_zero);
18659 tree lhs = RECUR (TREE_OPERAND (t, 0));
18660 tree rhs = RECUR (TREE_OPERAND (t, 2));
18661 tree r = build_x_modify_expr
18662 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18663 complain|decltype_flag);
18664 /* TREE_NO_WARNING must be set if either the expression was
18665 parenthesized or it uses an operator such as >>= rather
18666 than plain assignment. In the former case, it was already
18667 set and must be copied. In the latter case,
18668 build_x_modify_expr sets it and it must not be reset
18669 here. */
18670 if (TREE_NO_WARNING (t))
18671 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18672
18673 RETURN (r);
18674 }
18675
18676 case ARROW_EXPR:
18677 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18678 args, complain, in_decl);
18679 /* Remember that there was a reference to this entity. */
18680 if (DECL_P (op1)
18681 && !mark_used (op1, complain) && !(complain & tf_error))
18682 RETURN (error_mark_node);
18683 RETURN (build_x_arrow (input_location, op1, complain));
18684
18685 case NEW_EXPR:
18686 {
18687 tree placement = RECUR (TREE_OPERAND (t, 0));
18688 tree init = RECUR (TREE_OPERAND (t, 3));
18689 vec<tree, va_gc> *placement_vec;
18690 vec<tree, va_gc> *init_vec;
18691 tree ret;
18692
18693 if (placement == NULL_TREE)
18694 placement_vec = NULL;
18695 else
18696 {
18697 placement_vec = make_tree_vector ();
18698 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18699 vec_safe_push (placement_vec, TREE_VALUE (placement));
18700 }
18701
18702 /* If there was an initializer in the original tree, but it
18703 instantiated to an empty list, then we should pass a
18704 non-NULL empty vector to tell build_new that it was an
18705 empty initializer() rather than no initializer. This can
18706 only happen when the initializer is a pack expansion whose
18707 parameter packs are of length zero. */
18708 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18709 init_vec = NULL;
18710 else
18711 {
18712 init_vec = make_tree_vector ();
18713 if (init == void_node)
18714 gcc_assert (init_vec != NULL);
18715 else
18716 {
18717 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18718 vec_safe_push (init_vec, TREE_VALUE (init));
18719 }
18720 }
18721
18722 /* Avoid passing an enclosing decl to valid_array_size_p. */
18723 in_decl = NULL_TREE;
18724
18725 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18726 tree op2 = RECUR (TREE_OPERAND (t, 2));
18727 ret = build_new (&placement_vec, op1, op2, &init_vec,
18728 NEW_EXPR_USE_GLOBAL (t),
18729 complain);
18730
18731 if (placement_vec != NULL)
18732 release_tree_vector (placement_vec);
18733 if (init_vec != NULL)
18734 release_tree_vector (init_vec);
18735
18736 RETURN (ret);
18737 }
18738
18739 case DELETE_EXPR:
18740 {
18741 tree op0 = RECUR (TREE_OPERAND (t, 0));
18742 tree op1 = RECUR (TREE_OPERAND (t, 1));
18743 RETURN (delete_sanity (op0, op1,
18744 DELETE_EXPR_USE_VEC (t),
18745 DELETE_EXPR_USE_GLOBAL (t),
18746 complain));
18747 }
18748
18749 case COMPOUND_EXPR:
18750 {
18751 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18752 complain & ~tf_decltype, in_decl,
18753 /*function_p=*/false,
18754 integral_constant_expression_p);
18755 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18756 op0,
18757 RECUR (TREE_OPERAND (t, 1)),
18758 complain|decltype_flag));
18759 }
18760
18761 case CALL_EXPR:
18762 {
18763 tree function;
18764 unsigned int nargs, i;
18765 bool qualified_p;
18766 bool koenig_p;
18767 tree ret;
18768
18769 function = CALL_EXPR_FN (t);
18770 /* Internal function with no arguments. */
18771 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18772 RETURN (t);
18773
18774 /* When we parsed the expression, we determined whether or
18775 not Koenig lookup should be performed. */
18776 koenig_p = KOENIG_LOOKUP_P (t);
18777 if (function == NULL_TREE)
18778 {
18779 koenig_p = false;
18780 qualified_p = false;
18781 }
18782 else if (TREE_CODE (function) == SCOPE_REF)
18783 {
18784 qualified_p = true;
18785 function = tsubst_qualified_id (function, args, complain, in_decl,
18786 /*done=*/false,
18787 /*address_p=*/false);
18788 }
18789 else if (koenig_p && identifier_p (function))
18790 {
18791 /* Do nothing; calling tsubst_copy_and_build on an identifier
18792 would incorrectly perform unqualified lookup again.
18793
18794 Note that we can also have an IDENTIFIER_NODE if the earlier
18795 unqualified lookup found a member function; in that case
18796 koenig_p will be false and we do want to do the lookup
18797 again to find the instantiated member function.
18798
18799 FIXME but doing that causes c++/15272, so we need to stop
18800 using IDENTIFIER_NODE in that situation. */
18801 qualified_p = false;
18802 }
18803 else
18804 {
18805 if (TREE_CODE (function) == COMPONENT_REF)
18806 {
18807 tree op = TREE_OPERAND (function, 1);
18808
18809 qualified_p = (TREE_CODE (op) == SCOPE_REF
18810 || (BASELINK_P (op)
18811 && BASELINK_QUALIFIED_P (op)));
18812 }
18813 else
18814 qualified_p = false;
18815
18816 if (TREE_CODE (function) == ADDR_EXPR
18817 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18818 /* Avoid error about taking the address of a constructor. */
18819 function = TREE_OPERAND (function, 0);
18820
18821 function = tsubst_copy_and_build (function, args, complain,
18822 in_decl,
18823 !qualified_p,
18824 integral_constant_expression_p);
18825
18826 if (BASELINK_P (function))
18827 qualified_p = true;
18828 }
18829
18830 nargs = call_expr_nargs (t);
18831 releasing_vec call_args;
18832 for (i = 0; i < nargs; ++i)
18833 {
18834 tree arg = CALL_EXPR_ARG (t, i);
18835
18836 if (!PACK_EXPANSION_P (arg))
18837 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18838 else
18839 {
18840 /* Expand the pack expansion and push each entry onto
18841 CALL_ARGS. */
18842 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18843 if (TREE_CODE (arg) == TREE_VEC)
18844 {
18845 unsigned int len, j;
18846
18847 len = TREE_VEC_LENGTH (arg);
18848 for (j = 0; j < len; ++j)
18849 {
18850 tree value = TREE_VEC_ELT (arg, j);
18851 if (value != NULL_TREE)
18852 value = convert_from_reference (value);
18853 vec_safe_push (call_args, value);
18854 }
18855 }
18856 else
18857 {
18858 /* A partial substitution. Add one entry. */
18859 vec_safe_push (call_args, arg);
18860 }
18861 }
18862 }
18863
18864 /* Stripped-down processing for a call in a thunk. Specifically, in
18865 the thunk template for a generic lambda. */
18866 if (CALL_FROM_THUNK_P (t))
18867 {
18868 /* Now that we've expanded any packs, the number of call args
18869 might be different. */
18870 unsigned int cargs = call_args->length ();
18871 tree thisarg = NULL_TREE;
18872 if (TREE_CODE (function) == COMPONENT_REF)
18873 {
18874 thisarg = TREE_OPERAND (function, 0);
18875 if (TREE_CODE (thisarg) == INDIRECT_REF)
18876 thisarg = TREE_OPERAND (thisarg, 0);
18877 function = TREE_OPERAND (function, 1);
18878 if (TREE_CODE (function) == BASELINK)
18879 function = BASELINK_FUNCTIONS (function);
18880 }
18881 /* We aren't going to do normal overload resolution, so force the
18882 template-id to resolve. */
18883 function = resolve_nondeduced_context (function, complain);
18884 for (unsigned i = 0; i < cargs; ++i)
18885 {
18886 /* In a thunk, pass through args directly, without any
18887 conversions. */
18888 tree arg = (*call_args)[i];
18889 while (TREE_CODE (arg) != PARM_DECL)
18890 arg = TREE_OPERAND (arg, 0);
18891 (*call_args)[i] = arg;
18892 }
18893 if (thisarg)
18894 {
18895 /* If there are no other args, just push 'this'. */
18896 if (cargs == 0)
18897 vec_safe_push (call_args, thisarg);
18898 else
18899 {
18900 /* Otherwise, shift the other args over to make room. */
18901 tree last = (*call_args)[cargs - 1];
18902 vec_safe_push (call_args, last);
18903 for (int i = cargs - 1; i > 0; --i)
18904 (*call_args)[i] = (*call_args)[i - 1];
18905 (*call_args)[0] = thisarg;
18906 }
18907 }
18908 ret = build_call_a (function, call_args->length (),
18909 call_args->address ());
18910 /* The thunk location is not interesting. */
18911 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18912 CALL_FROM_THUNK_P (ret) = true;
18913 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18914 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18915
18916 RETURN (ret);
18917 }
18918
18919 /* We do not perform argument-dependent lookup if normal
18920 lookup finds a non-function, in accordance with the
18921 expected resolution of DR 218. */
18922 if (koenig_p
18923 && ((is_overloaded_fn (function)
18924 /* If lookup found a member function, the Koenig lookup is
18925 not appropriate, even if an unqualified-name was used
18926 to denote the function. */
18927 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18928 || identifier_p (function))
18929 /* Only do this when substitution turns a dependent call
18930 into a non-dependent call. */
18931 && type_dependent_expression_p_push (t)
18932 && !any_type_dependent_arguments_p (call_args))
18933 function = perform_koenig_lookup (function, call_args, tf_none);
18934
18935 if (function != NULL_TREE
18936 && identifier_p (function)
18937 && !any_type_dependent_arguments_p (call_args))
18938 {
18939 if (koenig_p && (complain & tf_warning_or_error))
18940 {
18941 /* For backwards compatibility and good diagnostics, try
18942 the unqualified lookup again if we aren't in SFINAE
18943 context. */
18944 tree unq = (tsubst_copy_and_build
18945 (function, args, complain, in_decl, true,
18946 integral_constant_expression_p));
18947 if (unq == error_mark_node)
18948 RETURN (error_mark_node);
18949
18950 if (unq != function)
18951 {
18952 /* In a lambda fn, we have to be careful to not
18953 introduce new this captures. Legacy code can't
18954 be using lambdas anyway, so it's ok to be
18955 stricter. */
18956 bool in_lambda = (current_class_type
18957 && LAMBDA_TYPE_P (current_class_type));
18958 char const *const msg
18959 = G_("%qD was not declared in this scope, "
18960 "and no declarations were found by "
18961 "argument-dependent lookup at the point "
18962 "of instantiation");
18963
18964 bool diag = true;
18965 if (in_lambda)
18966 error_at (cp_expr_loc_or_loc (t, input_location),
18967 msg, function);
18968 else
18969 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18970 msg, function);
18971 if (diag)
18972 {
18973 tree fn = unq;
18974
18975 if (INDIRECT_REF_P (fn))
18976 fn = TREE_OPERAND (fn, 0);
18977 if (is_overloaded_fn (fn))
18978 fn = get_first_fn (fn);
18979
18980 if (!DECL_P (fn))
18981 /* Can't say anything more. */;
18982 else if (DECL_CLASS_SCOPE_P (fn))
18983 {
18984 location_t loc = cp_expr_loc_or_loc (t,
18985 input_location);
18986 inform (loc,
18987 "declarations in dependent base %qT are "
18988 "not found by unqualified lookup",
18989 DECL_CLASS_CONTEXT (fn));
18990 if (current_class_ptr)
18991 inform (loc,
18992 "use %<this->%D%> instead", function);
18993 else
18994 inform (loc,
18995 "use %<%T::%D%> instead",
18996 current_class_name, function);
18997 }
18998 else
18999 inform (DECL_SOURCE_LOCATION (fn),
19000 "%qD declared here, later in the "
19001 "translation unit", fn);
19002 if (in_lambda)
19003 RETURN (error_mark_node);
19004 }
19005
19006 function = unq;
19007 }
19008 }
19009 if (identifier_p (function))
19010 {
19011 if (complain & tf_error)
19012 unqualified_name_lookup_error (function);
19013 RETURN (error_mark_node);
19014 }
19015 }
19016
19017 /* Remember that there was a reference to this entity. */
19018 if (function != NULL_TREE
19019 && DECL_P (function)
19020 && !mark_used (function, complain) && !(complain & tf_error))
19021 RETURN (error_mark_node);
19022
19023 /* Put back tf_decltype for the actual call. */
19024 complain |= decltype_flag;
19025
19026 if (function == NULL_TREE)
19027 switch (CALL_EXPR_IFN (t))
19028 {
19029 case IFN_LAUNDER:
19030 gcc_assert (nargs == 1);
19031 if (vec_safe_length (call_args) != 1)
19032 {
19033 error_at (cp_expr_loc_or_loc (t, input_location),
19034 "wrong number of arguments to "
19035 "%<__builtin_launder%>");
19036 ret = error_mark_node;
19037 }
19038 else
19039 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
19040 input_location),
19041 (*call_args)[0], complain);
19042 break;
19043
19044 case IFN_VEC_CONVERT:
19045 gcc_assert (nargs == 1);
19046 if (vec_safe_length (call_args) != 1)
19047 {
19048 error_at (cp_expr_loc_or_loc (t, input_location),
19049 "wrong number of arguments to "
19050 "%<__builtin_convertvector%>");
19051 ret = error_mark_node;
19052 break;
19053 }
19054 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19055 tsubst (TREE_TYPE (t), args,
19056 complain, in_decl),
19057 complain);
19058 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19059 RETURN (ret);
19060 break;
19061
19062 default:
19063 /* Unsupported internal function with arguments. */
19064 gcc_unreachable ();
19065 }
19066 else if (TREE_CODE (function) == OFFSET_REF
19067 || TREE_CODE (function) == DOTSTAR_EXPR
19068 || TREE_CODE (function) == MEMBER_REF)
19069 ret = build_offset_ref_call_from_tree (function, &call_args,
19070 complain);
19071 else if (TREE_CODE (function) == COMPONENT_REF)
19072 {
19073 tree instance = TREE_OPERAND (function, 0);
19074 tree fn = TREE_OPERAND (function, 1);
19075
19076 if (processing_template_decl
19077 && (type_dependent_expression_p (instance)
19078 || (!BASELINK_P (fn)
19079 && TREE_CODE (fn) != FIELD_DECL)
19080 || type_dependent_expression_p (fn)
19081 || any_type_dependent_arguments_p (call_args)))
19082 ret = build_min_nt_call_vec (function, call_args);
19083 else if (!BASELINK_P (fn))
19084 ret = finish_call_expr (function, &call_args,
19085 /*disallow_virtual=*/false,
19086 /*koenig_p=*/false,
19087 complain);
19088 else
19089 ret = (build_new_method_call
19090 (instance, fn,
19091 &call_args, NULL_TREE,
19092 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19093 /*fn_p=*/NULL,
19094 complain));
19095 }
19096 else
19097 ret = finish_call_expr (function, &call_args,
19098 /*disallow_virtual=*/qualified_p,
19099 koenig_p,
19100 complain);
19101
19102 if (ret != error_mark_node)
19103 {
19104 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19105 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19106 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19107 if (op || ord || rev)
19108 {
19109 function = extract_call_expr (ret);
19110 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19111 CALL_EXPR_ORDERED_ARGS (function) = ord;
19112 CALL_EXPR_REVERSE_ARGS (function) = rev;
19113 }
19114 }
19115
19116 RETURN (ret);
19117 }
19118
19119 case COND_EXPR:
19120 {
19121 tree cond = RECUR (TREE_OPERAND (t, 0));
19122 cond = mark_rvalue_use (cond);
19123 tree folded_cond = fold_non_dependent_expr (cond, complain);
19124 tree exp1, exp2;
19125
19126 if (TREE_CODE (folded_cond) == INTEGER_CST)
19127 {
19128 if (integer_zerop (folded_cond))
19129 {
19130 ++c_inhibit_evaluation_warnings;
19131 exp1 = RECUR (TREE_OPERAND (t, 1));
19132 --c_inhibit_evaluation_warnings;
19133 exp2 = RECUR (TREE_OPERAND (t, 2));
19134 }
19135 else
19136 {
19137 exp1 = RECUR (TREE_OPERAND (t, 1));
19138 ++c_inhibit_evaluation_warnings;
19139 exp2 = RECUR (TREE_OPERAND (t, 2));
19140 --c_inhibit_evaluation_warnings;
19141 }
19142 cond = folded_cond;
19143 }
19144 else
19145 {
19146 exp1 = RECUR (TREE_OPERAND (t, 1));
19147 exp2 = RECUR (TREE_OPERAND (t, 2));
19148 }
19149
19150 warning_sentinel s(warn_duplicated_branches);
19151 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19152 cond, exp1, exp2, complain));
19153 }
19154
19155 case PSEUDO_DTOR_EXPR:
19156 {
19157 tree op0 = RECUR (TREE_OPERAND (t, 0));
19158 tree op1 = RECUR (TREE_OPERAND (t, 1));
19159 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19160 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19161 input_location));
19162 }
19163
19164 case TREE_LIST:
19165 {
19166 tree purpose, value, chain;
19167
19168 if (t == void_list_node)
19169 RETURN (t);
19170
19171 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19172 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19173 {
19174 /* We have pack expansions, so expand those and
19175 create a new list out of it. */
19176 tree purposevec = NULL_TREE;
19177 tree valuevec = NULL_TREE;
19178 tree chain;
19179 int i, len = -1;
19180
19181 /* Expand the argument expressions. */
19182 if (TREE_PURPOSE (t))
19183 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19184 complain, in_decl);
19185 if (TREE_VALUE (t))
19186 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19187 complain, in_decl);
19188
19189 /* Build the rest of the list. */
19190 chain = TREE_CHAIN (t);
19191 if (chain && chain != void_type_node)
19192 chain = RECUR (chain);
19193
19194 /* Determine the number of arguments. */
19195 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19196 {
19197 len = TREE_VEC_LENGTH (purposevec);
19198 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19199 }
19200 else if (TREE_CODE (valuevec) == TREE_VEC)
19201 len = TREE_VEC_LENGTH (valuevec);
19202 else
19203 {
19204 /* Since we only performed a partial substitution into
19205 the argument pack, we only RETURN (a single list
19206 node. */
19207 if (purposevec == TREE_PURPOSE (t)
19208 && valuevec == TREE_VALUE (t)
19209 && chain == TREE_CHAIN (t))
19210 RETURN (t);
19211
19212 RETURN (tree_cons (purposevec, valuevec, chain));
19213 }
19214
19215 /* Convert the argument vectors into a TREE_LIST */
19216 i = len;
19217 while (i > 0)
19218 {
19219 /* Grab the Ith values. */
19220 i--;
19221 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19222 : NULL_TREE;
19223 value
19224 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19225 : NULL_TREE;
19226
19227 /* Build the list (backwards). */
19228 chain = tree_cons (purpose, value, chain);
19229 }
19230
19231 RETURN (chain);
19232 }
19233
19234 purpose = TREE_PURPOSE (t);
19235 if (purpose)
19236 purpose = RECUR (purpose);
19237 value = TREE_VALUE (t);
19238 if (value)
19239 value = RECUR (value);
19240 chain = TREE_CHAIN (t);
19241 if (chain && chain != void_type_node)
19242 chain = RECUR (chain);
19243 if (purpose == TREE_PURPOSE (t)
19244 && value == TREE_VALUE (t)
19245 && chain == TREE_CHAIN (t))
19246 RETURN (t);
19247 RETURN (tree_cons (purpose, value, chain));
19248 }
19249
19250 case COMPONENT_REF:
19251 {
19252 tree object;
19253 tree object_type;
19254 tree member;
19255 tree r;
19256
19257 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19258 args, complain, in_decl);
19259 /* Remember that there was a reference to this entity. */
19260 if (DECL_P (object)
19261 && !mark_used (object, complain) && !(complain & tf_error))
19262 RETURN (error_mark_node);
19263 object_type = TREE_TYPE (object);
19264
19265 member = TREE_OPERAND (t, 1);
19266 if (BASELINK_P (member))
19267 member = tsubst_baselink (member,
19268 non_reference (TREE_TYPE (object)),
19269 args, complain, in_decl);
19270 else
19271 member = tsubst_copy (member, args, complain, in_decl);
19272 if (member == error_mark_node)
19273 RETURN (error_mark_node);
19274
19275 if (TREE_CODE (member) == FIELD_DECL)
19276 {
19277 r = finish_non_static_data_member (member, object, NULL_TREE);
19278 if (TREE_CODE (r) == COMPONENT_REF)
19279 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19280 RETURN (r);
19281 }
19282 else if (type_dependent_expression_p (object))
19283 /* We can't do much here. */;
19284 else if (!CLASS_TYPE_P (object_type))
19285 {
19286 if (scalarish_type_p (object_type))
19287 {
19288 tree s = NULL_TREE;
19289 tree dtor = member;
19290
19291 if (TREE_CODE (dtor) == SCOPE_REF)
19292 {
19293 s = TREE_OPERAND (dtor, 0);
19294 dtor = TREE_OPERAND (dtor, 1);
19295 }
19296 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19297 {
19298 dtor = TREE_OPERAND (dtor, 0);
19299 if (TYPE_P (dtor))
19300 RETURN (finish_pseudo_destructor_expr
19301 (object, s, dtor, input_location));
19302 }
19303 }
19304 }
19305 else if (TREE_CODE (member) == SCOPE_REF
19306 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19307 {
19308 /* Lookup the template functions now that we know what the
19309 scope is. */
19310 tree scope = TREE_OPERAND (member, 0);
19311 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19312 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19313 member = lookup_qualified_name (scope, tmpl,
19314 /*is_type_p=*/false,
19315 /*complain=*/false);
19316 if (BASELINK_P (member))
19317 {
19318 BASELINK_FUNCTIONS (member)
19319 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19320 args);
19321 member = (adjust_result_of_qualified_name_lookup
19322 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19323 object_type));
19324 }
19325 else
19326 {
19327 qualified_name_lookup_error (scope, tmpl, member,
19328 input_location);
19329 RETURN (error_mark_node);
19330 }
19331 }
19332 else if (TREE_CODE (member) == SCOPE_REF
19333 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19334 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19335 {
19336 if (complain & tf_error)
19337 {
19338 if (TYPE_P (TREE_OPERAND (member, 0)))
19339 error ("%qT is not a class or namespace",
19340 TREE_OPERAND (member, 0));
19341 else
19342 error ("%qD is not a class or namespace",
19343 TREE_OPERAND (member, 0));
19344 }
19345 RETURN (error_mark_node);
19346 }
19347
19348 r = finish_class_member_access_expr (object, member,
19349 /*template_p=*/false,
19350 complain);
19351 if (TREE_CODE (r) == COMPONENT_REF)
19352 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19353 RETURN (r);
19354 }
19355
19356 case THROW_EXPR:
19357 RETURN (build_throw
19358 (RECUR (TREE_OPERAND (t, 0))));
19359
19360 case CONSTRUCTOR:
19361 {
19362 vec<constructor_elt, va_gc> *n;
19363 constructor_elt *ce;
19364 unsigned HOST_WIDE_INT idx;
19365 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19366 bool process_index_p;
19367 int newlen;
19368 bool need_copy_p = false;
19369 tree r;
19370
19371 if (type == error_mark_node)
19372 RETURN (error_mark_node);
19373
19374 /* We do not want to process the index of aggregate
19375 initializers as they are identifier nodes which will be
19376 looked up by digest_init. */
19377 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19378
19379 if (null_member_pointer_value_p (t))
19380 {
19381 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19382 RETURN (t);
19383 }
19384
19385 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19386 newlen = vec_safe_length (n);
19387 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19388 {
19389 if (ce->index && process_index_p
19390 /* An identifier index is looked up in the type
19391 being initialized, not the current scope. */
19392 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19393 ce->index = RECUR (ce->index);
19394
19395 if (PACK_EXPANSION_P (ce->value))
19396 {
19397 /* Substitute into the pack expansion. */
19398 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19399 in_decl);
19400
19401 if (ce->value == error_mark_node
19402 || PACK_EXPANSION_P (ce->value))
19403 ;
19404 else if (TREE_VEC_LENGTH (ce->value) == 1)
19405 /* Just move the argument into place. */
19406 ce->value = TREE_VEC_ELT (ce->value, 0);
19407 else
19408 {
19409 /* Update the length of the final CONSTRUCTOR
19410 arguments vector, and note that we will need to
19411 copy.*/
19412 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19413 need_copy_p = true;
19414 }
19415 }
19416 else
19417 ce->value = RECUR (ce->value);
19418 }
19419
19420 if (need_copy_p)
19421 {
19422 vec<constructor_elt, va_gc> *old_n = n;
19423
19424 vec_alloc (n, newlen);
19425 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19426 {
19427 if (TREE_CODE (ce->value) == TREE_VEC)
19428 {
19429 int i, len = TREE_VEC_LENGTH (ce->value);
19430 for (i = 0; i < len; ++i)
19431 CONSTRUCTOR_APPEND_ELT (n, 0,
19432 TREE_VEC_ELT (ce->value, i));
19433 }
19434 else
19435 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19436 }
19437 }
19438
19439 r = build_constructor (init_list_type_node, n);
19440 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19441 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19442 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19443
19444 if (TREE_HAS_CONSTRUCTOR (t))
19445 {
19446 fcl_t cl = fcl_functional;
19447 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19448 cl = fcl_c99;
19449 RETURN (finish_compound_literal (type, r, complain, cl));
19450 }
19451
19452 TREE_TYPE (r) = type;
19453 RETURN (r);
19454 }
19455
19456 case TYPEID_EXPR:
19457 {
19458 tree operand_0 = TREE_OPERAND (t, 0);
19459 if (TYPE_P (operand_0))
19460 {
19461 operand_0 = tsubst (operand_0, args, complain, in_decl);
19462 RETURN (get_typeid (operand_0, complain));
19463 }
19464 else
19465 {
19466 operand_0 = RECUR (operand_0);
19467 RETURN (build_typeid (operand_0, complain));
19468 }
19469 }
19470
19471 case VAR_DECL:
19472 if (!args)
19473 RETURN (t);
19474 /* Fall through */
19475
19476 case PARM_DECL:
19477 {
19478 tree r = tsubst_copy (t, args, complain, in_decl);
19479 /* ??? We're doing a subset of finish_id_expression here. */
19480 if (tree wrap = maybe_get_tls_wrapper_call (r))
19481 /* Replace an evaluated use of the thread_local variable with
19482 a call to its wrapper. */
19483 r = wrap;
19484 else if (outer_automatic_var_p (r))
19485 r = process_outer_var_ref (r, complain);
19486
19487 if (!TYPE_REF_P (TREE_TYPE (t)))
19488 /* If the original type was a reference, we'll be wrapped in
19489 the appropriate INDIRECT_REF. */
19490 r = convert_from_reference (r);
19491 RETURN (r);
19492 }
19493
19494 case VA_ARG_EXPR:
19495 {
19496 tree op0 = RECUR (TREE_OPERAND (t, 0));
19497 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19498 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19499 }
19500
19501 case OFFSETOF_EXPR:
19502 {
19503 tree object_ptr
19504 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19505 in_decl, /*function_p=*/false,
19506 /*integral_constant_expression_p=*/false);
19507 RETURN (finish_offsetof (object_ptr,
19508 RECUR (TREE_OPERAND (t, 0)),
19509 EXPR_LOCATION (t)));
19510 }
19511
19512 case ADDRESSOF_EXPR:
19513 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19514 RECUR (TREE_OPERAND (t, 0)), complain));
19515
19516 case TRAIT_EXPR:
19517 {
19518 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19519 complain, in_decl);
19520
19521 tree type2 = TRAIT_EXPR_TYPE2 (t);
19522 if (type2 && TREE_CODE (type2) == TREE_LIST)
19523 type2 = RECUR (type2);
19524 else if (type2)
19525 type2 = tsubst (type2, args, complain, in_decl);
19526
19527 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19528 }
19529
19530 case STMT_EXPR:
19531 {
19532 tree old_stmt_expr = cur_stmt_expr;
19533 tree stmt_expr = begin_stmt_expr ();
19534
19535 cur_stmt_expr = stmt_expr;
19536 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19537 integral_constant_expression_p);
19538 stmt_expr = finish_stmt_expr (stmt_expr, false);
19539 cur_stmt_expr = old_stmt_expr;
19540
19541 /* If the resulting list of expression statement is empty,
19542 fold it further into void_node. */
19543 if (empty_expr_stmt_p (stmt_expr))
19544 stmt_expr = void_node;
19545
19546 RETURN (stmt_expr);
19547 }
19548
19549 case LAMBDA_EXPR:
19550 {
19551 if (complain & tf_partial)
19552 {
19553 /* We don't have a full set of template arguments yet; don't touch
19554 the lambda at all. */
19555 gcc_assert (processing_template_decl);
19556 return t;
19557 }
19558 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19559
19560 RETURN (build_lambda_object (r));
19561 }
19562
19563 case TARGET_EXPR:
19564 /* We can get here for a constant initializer of non-dependent type.
19565 FIXME stop folding in cp_parser_initializer_clause. */
19566 {
19567 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19568 complain);
19569 RETURN (r);
19570 }
19571
19572 case TRANSACTION_EXPR:
19573 RETURN (tsubst_expr(t, args, complain, in_decl,
19574 integral_constant_expression_p));
19575
19576 case PAREN_EXPR:
19577 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19578
19579 case VEC_PERM_EXPR:
19580 {
19581 tree op0 = RECUR (TREE_OPERAND (t, 0));
19582 tree op1 = RECUR (TREE_OPERAND (t, 1));
19583 tree op2 = RECUR (TREE_OPERAND (t, 2));
19584 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19585 complain));
19586 }
19587
19588 case REQUIRES_EXPR:
19589 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19590
19591 case RANGE_EXPR:
19592 /* No need to substitute further, a RANGE_EXPR will always be built
19593 with constant operands. */
19594 RETURN (t);
19595
19596 case NON_LVALUE_EXPR:
19597 case VIEW_CONVERT_EXPR:
19598 if (location_wrapper_p (t))
19599 /* We need to do this here as well as in tsubst_copy so we get the
19600 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19601 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19602 EXPR_LOCATION (t)));
19603 /* fallthrough. */
19604
19605 default:
19606 /* Handle Objective-C++ constructs, if appropriate. */
19607 {
19608 tree subst
19609 = objcp_tsubst_copy_and_build (t, args, complain,
19610 in_decl, /*function_p=*/false);
19611 if (subst)
19612 RETURN (subst);
19613 }
19614 RETURN (tsubst_copy (t, args, complain, in_decl));
19615 }
19616
19617 #undef RECUR
19618 #undef RETURN
19619 out:
19620 input_location = loc;
19621 return retval;
19622 }
19623
19624 /* Verify that the instantiated ARGS are valid. For type arguments,
19625 make sure that the type's linkage is ok. For non-type arguments,
19626 make sure they are constants if they are integral or enumerations.
19627 Emit an error under control of COMPLAIN, and return TRUE on error. */
19628
19629 static bool
19630 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19631 {
19632 if (dependent_template_arg_p (t))
19633 return false;
19634 if (ARGUMENT_PACK_P (t))
19635 {
19636 tree vec = ARGUMENT_PACK_ARGS (t);
19637 int len = TREE_VEC_LENGTH (vec);
19638 bool result = false;
19639 int i;
19640
19641 for (i = 0; i < len; ++i)
19642 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19643 result = true;
19644 return result;
19645 }
19646 else if (TYPE_P (t))
19647 {
19648 /* [basic.link]: A name with no linkage (notably, the name
19649 of a class or enumeration declared in a local scope)
19650 shall not be used to declare an entity with linkage.
19651 This implies that names with no linkage cannot be used as
19652 template arguments
19653
19654 DR 757 relaxes this restriction for C++0x. */
19655 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19656 : no_linkage_check (t, /*relaxed_p=*/false));
19657
19658 if (nt)
19659 {
19660 /* DR 488 makes use of a type with no linkage cause
19661 type deduction to fail. */
19662 if (complain & tf_error)
19663 {
19664 if (TYPE_UNNAMED_P (nt))
19665 error ("%qT is/uses unnamed type", t);
19666 else
19667 error ("template argument for %qD uses local type %qT",
19668 tmpl, t);
19669 }
19670 return true;
19671 }
19672 /* In order to avoid all sorts of complications, we do not
19673 allow variably-modified types as template arguments. */
19674 else if (variably_modified_type_p (t, NULL_TREE))
19675 {
19676 if (complain & tf_error)
19677 error ("%qT is a variably modified type", t);
19678 return true;
19679 }
19680 }
19681 /* Class template and alias template arguments should be OK. */
19682 else if (DECL_TYPE_TEMPLATE_P (t))
19683 ;
19684 /* A non-type argument of integral or enumerated type must be a
19685 constant. */
19686 else if (TREE_TYPE (t)
19687 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19688 && !REFERENCE_REF_P (t)
19689 && !TREE_CONSTANT (t))
19690 {
19691 if (complain & tf_error)
19692 error ("integral expression %qE is not constant", t);
19693 return true;
19694 }
19695 return false;
19696 }
19697
19698 static bool
19699 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19700 {
19701 int ix, len = DECL_NTPARMS (tmpl);
19702 bool result = false;
19703
19704 for (ix = 0; ix != len; ix++)
19705 {
19706 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19707 result = true;
19708 }
19709 if (result && (complain & tf_error))
19710 error (" trying to instantiate %qD", tmpl);
19711 return result;
19712 }
19713
19714 /* We're out of SFINAE context now, so generate diagnostics for the access
19715 errors we saw earlier when instantiating D from TMPL and ARGS. */
19716
19717 static void
19718 recheck_decl_substitution (tree d, tree tmpl, tree args)
19719 {
19720 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19721 tree type = TREE_TYPE (pattern);
19722 location_t loc = input_location;
19723
19724 push_access_scope (d);
19725 push_deferring_access_checks (dk_no_deferred);
19726 input_location = DECL_SOURCE_LOCATION (pattern);
19727 tsubst (type, args, tf_warning_or_error, d);
19728 input_location = loc;
19729 pop_deferring_access_checks ();
19730 pop_access_scope (d);
19731 }
19732
19733 /* Instantiate the indicated variable, function, or alias template TMPL with
19734 the template arguments in TARG_PTR. */
19735
19736 static tree
19737 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19738 {
19739 tree targ_ptr = orig_args;
19740 tree fndecl;
19741 tree gen_tmpl;
19742 tree spec;
19743 bool access_ok = true;
19744
19745 if (tmpl == error_mark_node)
19746 return error_mark_node;
19747
19748 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19749
19750 /* If this function is a clone, handle it specially. */
19751 if (DECL_CLONED_FUNCTION_P (tmpl))
19752 {
19753 tree spec;
19754 tree clone;
19755
19756 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19757 DECL_CLONED_FUNCTION. */
19758 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19759 targ_ptr, complain);
19760 if (spec == error_mark_node)
19761 return error_mark_node;
19762
19763 /* Look for the clone. */
19764 FOR_EACH_CLONE (clone, spec)
19765 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19766 return clone;
19767 /* We should always have found the clone by now. */
19768 gcc_unreachable ();
19769 return NULL_TREE;
19770 }
19771
19772 if (targ_ptr == error_mark_node)
19773 return error_mark_node;
19774
19775 /* Check to see if we already have this specialization. */
19776 gen_tmpl = most_general_template (tmpl);
19777 if (TMPL_ARGS_DEPTH (targ_ptr)
19778 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19779 /* targ_ptr only has the innermost template args, so add the outer ones
19780 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19781 the case of a non-dependent call within a template definition). */
19782 targ_ptr = (add_outermost_template_args
19783 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19784 targ_ptr));
19785
19786 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19787 but it doesn't seem to be on the hot path. */
19788 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19789
19790 gcc_assert (tmpl == gen_tmpl
19791 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19792 == spec)
19793 || fndecl == NULL_TREE);
19794
19795 if (spec != NULL_TREE)
19796 {
19797 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19798 {
19799 if (complain & tf_error)
19800 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19801 return error_mark_node;
19802 }
19803 return spec;
19804 }
19805
19806 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19807 complain))
19808 return error_mark_node;
19809
19810 /* We are building a FUNCTION_DECL, during which the access of its
19811 parameters and return types have to be checked. However this
19812 FUNCTION_DECL which is the desired context for access checking
19813 is not built yet. We solve this chicken-and-egg problem by
19814 deferring all checks until we have the FUNCTION_DECL. */
19815 push_deferring_access_checks (dk_deferred);
19816
19817 /* Instantiation of the function happens in the context of the function
19818 template, not the context of the overload resolution we're doing. */
19819 push_to_top_level ();
19820 /* If there are dependent arguments, e.g. because we're doing partial
19821 ordering, make sure processing_template_decl stays set. */
19822 if (uses_template_parms (targ_ptr))
19823 ++processing_template_decl;
19824 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19825 {
19826 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19827 complain, gen_tmpl, true);
19828 push_nested_class (ctx);
19829 }
19830
19831 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19832
19833 fndecl = NULL_TREE;
19834 if (VAR_P (pattern))
19835 {
19836 /* We need to determine if we're using a partial or explicit
19837 specialization now, because the type of the variable could be
19838 different. */
19839 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19840 tree elt = most_specialized_partial_spec (tid, complain);
19841 if (elt == error_mark_node)
19842 pattern = error_mark_node;
19843 else if (elt)
19844 {
19845 tree partial_tmpl = TREE_VALUE (elt);
19846 tree partial_args = TREE_PURPOSE (elt);
19847 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19848 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19849 }
19850 }
19851
19852 /* Substitute template parameters to obtain the specialization. */
19853 if (fndecl == NULL_TREE)
19854 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19855 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19856 pop_nested_class ();
19857 pop_from_top_level ();
19858
19859 if (fndecl == error_mark_node)
19860 {
19861 pop_deferring_access_checks ();
19862 return error_mark_node;
19863 }
19864
19865 /* The DECL_TI_TEMPLATE should always be the immediate parent
19866 template, not the most general template. */
19867 DECL_TI_TEMPLATE (fndecl) = tmpl;
19868 DECL_TI_ARGS (fndecl) = targ_ptr;
19869
19870 /* Now we know the specialization, compute access previously
19871 deferred. Do no access control for inheriting constructors,
19872 as we already checked access for the inherited constructor. */
19873 if (!(flag_new_inheriting_ctors
19874 && DECL_INHERITED_CTOR (fndecl)))
19875 {
19876 push_access_scope (fndecl);
19877 if (!perform_deferred_access_checks (complain))
19878 access_ok = false;
19879 pop_access_scope (fndecl);
19880 }
19881 pop_deferring_access_checks ();
19882
19883 /* If we've just instantiated the main entry point for a function,
19884 instantiate all the alternate entry points as well. We do this
19885 by cloning the instantiation of the main entry point, not by
19886 instantiating the template clones. */
19887 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19888 clone_function_decl (fndecl, /*update_methods=*/false);
19889
19890 if (!access_ok)
19891 {
19892 if (!(complain & tf_error))
19893 {
19894 /* Remember to reinstantiate when we're out of SFINAE so the user
19895 can see the errors. */
19896 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19897 }
19898 return error_mark_node;
19899 }
19900 return fndecl;
19901 }
19902
19903 /* Wrapper for instantiate_template_1. */
19904
19905 tree
19906 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19907 {
19908 tree ret;
19909 timevar_push (TV_TEMPLATE_INST);
19910 ret = instantiate_template_1 (tmpl, orig_args, complain);
19911 timevar_pop (TV_TEMPLATE_INST);
19912 return ret;
19913 }
19914
19915 /* Instantiate the alias template TMPL with ARGS. Also push a template
19916 instantiation level, which instantiate_template doesn't do because
19917 functions and variables have sufficient context established by the
19918 callers. */
19919
19920 static tree
19921 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19922 {
19923 if (tmpl == error_mark_node || args == error_mark_node)
19924 return error_mark_node;
19925 if (!push_tinst_level (tmpl, args))
19926 return error_mark_node;
19927
19928 args =
19929 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19930 args, tmpl, complain,
19931 /*require_all_args=*/true,
19932 /*use_default_args=*/true);
19933
19934 tree r = instantiate_template (tmpl, args, complain);
19935 pop_tinst_level ();
19936
19937 return r;
19938 }
19939
19940 /* PARM is a template parameter pack for FN. Returns true iff
19941 PARM is used in a deducible way in the argument list of FN. */
19942
19943 static bool
19944 pack_deducible_p (tree parm, tree fn)
19945 {
19946 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19947 for (; t; t = TREE_CHAIN (t))
19948 {
19949 tree type = TREE_VALUE (t);
19950 tree packs;
19951 if (!PACK_EXPANSION_P (type))
19952 continue;
19953 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19954 packs; packs = TREE_CHAIN (packs))
19955 if (template_args_equal (TREE_VALUE (packs), parm))
19956 {
19957 /* The template parameter pack is used in a function parameter
19958 pack. If this is the end of the parameter list, the
19959 template parameter pack is deducible. */
19960 if (TREE_CHAIN (t) == void_list_node)
19961 return true;
19962 else
19963 /* Otherwise, not. Well, it could be deduced from
19964 a non-pack parameter, but doing so would end up with
19965 a deduction mismatch, so don't bother. */
19966 return false;
19967 }
19968 }
19969 /* The template parameter pack isn't used in any function parameter
19970 packs, but it might be used deeper, e.g. tuple<Args...>. */
19971 return true;
19972 }
19973
19974 /* Subroutine of fn_type_unification: check non-dependent parms for
19975 convertibility. */
19976
19977 static int
19978 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19979 tree fn, unification_kind_t strict, int flags,
19980 struct conversion **convs, bool explain_p)
19981 {
19982 /* Non-constructor methods need to leave a conversion for 'this', which
19983 isn't included in nargs here. */
19984 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19985 && !DECL_CONSTRUCTOR_P (fn));
19986
19987 for (unsigned ia = 0;
19988 parms && parms != void_list_node && ia < nargs; )
19989 {
19990 tree parm = TREE_VALUE (parms);
19991
19992 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19993 && (!TREE_CHAIN (parms)
19994 || TREE_CHAIN (parms) == void_list_node))
19995 /* For a function parameter pack that occurs at the end of the
19996 parameter-declaration-list, the type A of each remaining
19997 argument of the call is compared with the type P of the
19998 declarator-id of the function parameter pack. */
19999 break;
20000
20001 parms = TREE_CHAIN (parms);
20002
20003 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20004 /* For a function parameter pack that does not occur at the
20005 end of the parameter-declaration-list, the type of the
20006 parameter pack is a non-deduced context. */
20007 continue;
20008
20009 if (!uses_template_parms (parm))
20010 {
20011 tree arg = args[ia];
20012 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20013 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20014
20015 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20016 conv_p, explain_p))
20017 return 1;
20018 }
20019
20020 ++ia;
20021 }
20022
20023 return 0;
20024 }
20025
20026 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20027 NARGS elements of the arguments that are being used when calling
20028 it. TARGS is a vector into which the deduced template arguments
20029 are placed.
20030
20031 Returns either a FUNCTION_DECL for the matching specialization of FN or
20032 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20033 true, diagnostics will be printed to explain why it failed.
20034
20035 If FN is a conversion operator, or we are trying to produce a specific
20036 specialization, RETURN_TYPE is the return type desired.
20037
20038 The EXPLICIT_TARGS are explicit template arguments provided via a
20039 template-id.
20040
20041 The parameter STRICT is one of:
20042
20043 DEDUCE_CALL:
20044 We are deducing arguments for a function call, as in
20045 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20046 deducing arguments for a call to the result of a conversion
20047 function template, as in [over.call.object].
20048
20049 DEDUCE_CONV:
20050 We are deducing arguments for a conversion function, as in
20051 [temp.deduct.conv].
20052
20053 DEDUCE_EXACT:
20054 We are deducing arguments when doing an explicit instantiation
20055 as in [temp.explicit], when determining an explicit specialization
20056 as in [temp.expl.spec], or when taking the address of a function
20057 template, as in [temp.deduct.funcaddr]. */
20058
20059 tree
20060 fn_type_unification (tree fn,
20061 tree explicit_targs,
20062 tree targs,
20063 const tree *args,
20064 unsigned int nargs,
20065 tree return_type,
20066 unification_kind_t strict,
20067 int flags,
20068 struct conversion **convs,
20069 bool explain_p,
20070 bool decltype_p)
20071 {
20072 tree parms;
20073 tree fntype;
20074 tree decl = NULL_TREE;
20075 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20076 bool ok;
20077 static int deduction_depth;
20078 /* type_unification_real will pass back any access checks from default
20079 template argument substitution. */
20080 vec<deferred_access_check, va_gc> *checks = NULL;
20081 /* We don't have all the template args yet. */
20082 bool incomplete = true;
20083
20084 tree orig_fn = fn;
20085 if (flag_new_inheriting_ctors)
20086 fn = strip_inheriting_ctors (fn);
20087
20088 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20089 tree r = error_mark_node;
20090
20091 tree full_targs = targs;
20092 if (TMPL_ARGS_DEPTH (targs)
20093 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20094 full_targs = (add_outermost_template_args
20095 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20096 targs));
20097
20098 if (decltype_p)
20099 complain |= tf_decltype;
20100
20101 /* In C++0x, it's possible to have a function template whose type depends
20102 on itself recursively. This is most obvious with decltype, but can also
20103 occur with enumeration scope (c++/48969). So we need to catch infinite
20104 recursion and reject the substitution at deduction time; this function
20105 will return error_mark_node for any repeated substitution.
20106
20107 This also catches excessive recursion such as when f<N> depends on
20108 f<N-1> across all integers, and returns error_mark_node for all the
20109 substitutions back up to the initial one.
20110
20111 This is, of course, not reentrant. */
20112 if (excessive_deduction_depth)
20113 return error_mark_node;
20114 ++deduction_depth;
20115
20116 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20117
20118 fntype = TREE_TYPE (fn);
20119 if (explicit_targs)
20120 {
20121 /* [temp.deduct]
20122
20123 The specified template arguments must match the template
20124 parameters in kind (i.e., type, nontype, template), and there
20125 must not be more arguments than there are parameters;
20126 otherwise type deduction fails.
20127
20128 Nontype arguments must match the types of the corresponding
20129 nontype template parameters, or must be convertible to the
20130 types of the corresponding nontype parameters as specified in
20131 _temp.arg.nontype_, otherwise type deduction fails.
20132
20133 All references in the function type of the function template
20134 to the corresponding template parameters are replaced by the
20135 specified template argument values. If a substitution in a
20136 template parameter or in the function type of the function
20137 template results in an invalid type, type deduction fails. */
20138 int i, len = TREE_VEC_LENGTH (tparms);
20139 location_t loc = input_location;
20140 incomplete = false;
20141
20142 if (explicit_targs == error_mark_node)
20143 goto fail;
20144
20145 if (TMPL_ARGS_DEPTH (explicit_targs)
20146 < TMPL_ARGS_DEPTH (full_targs))
20147 explicit_targs = add_outermost_template_args (full_targs,
20148 explicit_targs);
20149
20150 /* Adjust any explicit template arguments before entering the
20151 substitution context. */
20152 explicit_targs
20153 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20154 complain|tf_partial,
20155 /*require_all_args=*/false,
20156 /*use_default_args=*/false));
20157 if (explicit_targs == error_mark_node)
20158 goto fail;
20159
20160 /* Substitute the explicit args into the function type. This is
20161 necessary so that, for instance, explicitly declared function
20162 arguments can match null pointed constants. If we were given
20163 an incomplete set of explicit args, we must not do semantic
20164 processing during substitution as we could create partial
20165 instantiations. */
20166 for (i = 0; i < len; i++)
20167 {
20168 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20169 bool parameter_pack = false;
20170 tree targ = TREE_VEC_ELT (explicit_targs, i);
20171
20172 /* Dig out the actual parm. */
20173 if (TREE_CODE (parm) == TYPE_DECL
20174 || TREE_CODE (parm) == TEMPLATE_DECL)
20175 {
20176 parm = TREE_TYPE (parm);
20177 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20178 }
20179 else if (TREE_CODE (parm) == PARM_DECL)
20180 {
20181 parm = DECL_INITIAL (parm);
20182 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20183 }
20184
20185 if (targ == NULL_TREE)
20186 /* No explicit argument for this template parameter. */
20187 incomplete = true;
20188 else if (parameter_pack && pack_deducible_p (parm, fn))
20189 {
20190 /* Mark the argument pack as "incomplete". We could
20191 still deduce more arguments during unification.
20192 We remove this mark in type_unification_real. */
20193 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20194 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20195 = ARGUMENT_PACK_ARGS (targ);
20196
20197 /* We have some incomplete argument packs. */
20198 incomplete = true;
20199 }
20200 }
20201
20202 if (incomplete)
20203 {
20204 if (!push_tinst_level (fn, explicit_targs))
20205 {
20206 excessive_deduction_depth = true;
20207 goto fail;
20208 }
20209 ++processing_template_decl;
20210 input_location = DECL_SOURCE_LOCATION (fn);
20211 /* Ignore any access checks; we'll see them again in
20212 instantiate_template and they might have the wrong
20213 access path at this point. */
20214 push_deferring_access_checks (dk_deferred);
20215 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20216 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20217 pop_deferring_access_checks ();
20218 input_location = loc;
20219 --processing_template_decl;
20220 pop_tinst_level ();
20221
20222 if (fntype == error_mark_node)
20223 goto fail;
20224 }
20225
20226 /* Place the explicitly specified arguments in TARGS. */
20227 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20228 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20229 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20230 if (!incomplete && CHECKING_P
20231 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20232 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20233 (targs, NUM_TMPL_ARGS (explicit_targs));
20234 }
20235
20236 if (return_type && strict != DEDUCE_CALL)
20237 {
20238 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20239 new_args[0] = return_type;
20240 memcpy (new_args + 1, args, nargs * sizeof (tree));
20241 args = new_args;
20242 ++nargs;
20243 }
20244
20245 if (!incomplete)
20246 goto deduced;
20247
20248 /* Never do unification on the 'this' parameter. */
20249 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20250
20251 if (return_type && strict == DEDUCE_CALL)
20252 {
20253 /* We're deducing for a call to the result of a template conversion
20254 function. The parms we really want are in return_type. */
20255 if (INDIRECT_TYPE_P (return_type))
20256 return_type = TREE_TYPE (return_type);
20257 parms = TYPE_ARG_TYPES (return_type);
20258 }
20259 else if (return_type)
20260 {
20261 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20262 }
20263
20264 /* We allow incomplete unification without an error message here
20265 because the standard doesn't seem to explicitly prohibit it. Our
20266 callers must be ready to deal with unification failures in any
20267 event. */
20268
20269 /* If we aren't explaining yet, push tinst context so we can see where
20270 any errors (e.g. from class instantiations triggered by instantiation
20271 of default template arguments) come from. If we are explaining, this
20272 context is redundant. */
20273 if (!explain_p && !push_tinst_level (fn, targs))
20274 {
20275 excessive_deduction_depth = true;
20276 goto fail;
20277 }
20278
20279 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20280 full_targs, parms, args, nargs, /*subr=*/0,
20281 strict, &checks, explain_p);
20282 if (!explain_p)
20283 pop_tinst_level ();
20284 if (!ok)
20285 goto fail;
20286
20287 /* Now that we have bindings for all of the template arguments,
20288 ensure that the arguments deduced for the template template
20289 parameters have compatible template parameter lists. We cannot
20290 check this property before we have deduced all template
20291 arguments, because the template parameter types of a template
20292 template parameter might depend on prior template parameters
20293 deduced after the template template parameter. The following
20294 ill-formed example illustrates this issue:
20295
20296 template<typename T, template<T> class C> void f(C<5>, T);
20297
20298 template<int N> struct X {};
20299
20300 void g() {
20301 f(X<5>(), 5l); // error: template argument deduction fails
20302 }
20303
20304 The template parameter list of 'C' depends on the template type
20305 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20306 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20307 time that we deduce 'C'. */
20308 if (!template_template_parm_bindings_ok_p
20309 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20310 {
20311 unify_inconsistent_template_template_parameters (explain_p);
20312 goto fail;
20313 }
20314
20315 /* DR 1391: All parameters have args, now check non-dependent parms for
20316 convertibility. */
20317 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20318 convs, explain_p))
20319 goto fail;
20320
20321 deduced:
20322 /* All is well so far. Now, check:
20323
20324 [temp.deduct]
20325
20326 When all template arguments have been deduced, all uses of
20327 template parameters in nondeduced contexts are replaced with
20328 the corresponding deduced argument values. If the
20329 substitution results in an invalid type, as described above,
20330 type deduction fails. */
20331 if (!push_tinst_level (fn, targs))
20332 {
20333 excessive_deduction_depth = true;
20334 goto fail;
20335 }
20336
20337 /* Also collect access checks from the instantiation. */
20338 reopen_deferring_access_checks (checks);
20339
20340 decl = instantiate_template (fn, targs, complain);
20341
20342 checks = get_deferred_access_checks ();
20343 pop_deferring_access_checks ();
20344
20345 pop_tinst_level ();
20346
20347 if (decl == error_mark_node)
20348 goto fail;
20349
20350 /* Now perform any access checks encountered during substitution. */
20351 push_access_scope (decl);
20352 ok = perform_access_checks (checks, complain);
20353 pop_access_scope (decl);
20354 if (!ok)
20355 goto fail;
20356
20357 /* If we're looking for an exact match, check that what we got
20358 is indeed an exact match. It might not be if some template
20359 parameters are used in non-deduced contexts. But don't check
20360 for an exact match if we have dependent template arguments;
20361 in that case we're doing partial ordering, and we already know
20362 that we have two candidates that will provide the actual type. */
20363 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20364 {
20365 tree substed = TREE_TYPE (decl);
20366 unsigned int i;
20367
20368 tree sarg
20369 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20370 if (return_type)
20371 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20372 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20373 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20374 {
20375 unify_type_mismatch (explain_p, args[i],
20376 TREE_VALUE (sarg));
20377 goto fail;
20378 }
20379 }
20380
20381 /* After doing deduction with the inherited constructor, actually return an
20382 instantiation of the inheriting constructor. */
20383 if (orig_fn != fn)
20384 decl = instantiate_template (orig_fn, targs, complain);
20385
20386 r = decl;
20387
20388 fail:
20389 --deduction_depth;
20390 if (excessive_deduction_depth)
20391 {
20392 if (deduction_depth == 0)
20393 /* Reset once we're all the way out. */
20394 excessive_deduction_depth = false;
20395 }
20396
20397 return r;
20398 }
20399
20400 /* Adjust types before performing type deduction, as described in
20401 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20402 sections are symmetric. PARM is the type of a function parameter
20403 or the return type of the conversion function. ARG is the type of
20404 the argument passed to the call, or the type of the value
20405 initialized with the result of the conversion function.
20406 ARG_EXPR is the original argument expression, which may be null. */
20407
20408 static int
20409 maybe_adjust_types_for_deduction (unification_kind_t strict,
20410 tree* parm,
20411 tree* arg,
20412 tree arg_expr)
20413 {
20414 int result = 0;
20415
20416 switch (strict)
20417 {
20418 case DEDUCE_CALL:
20419 break;
20420
20421 case DEDUCE_CONV:
20422 /* Swap PARM and ARG throughout the remainder of this
20423 function; the handling is precisely symmetric since PARM
20424 will initialize ARG rather than vice versa. */
20425 std::swap (parm, arg);
20426 break;
20427
20428 case DEDUCE_EXACT:
20429 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20430 too, but here handle it by stripping the reference from PARM
20431 rather than by adding it to ARG. */
20432 if (TYPE_REF_P (*parm)
20433 && TYPE_REF_IS_RVALUE (*parm)
20434 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20435 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20436 && TYPE_REF_P (*arg)
20437 && !TYPE_REF_IS_RVALUE (*arg))
20438 *parm = TREE_TYPE (*parm);
20439 /* Nothing else to do in this case. */
20440 return 0;
20441
20442 default:
20443 gcc_unreachable ();
20444 }
20445
20446 if (!TYPE_REF_P (*parm))
20447 {
20448 /* [temp.deduct.call]
20449
20450 If P is not a reference type:
20451
20452 --If A is an array type, the pointer type produced by the
20453 array-to-pointer standard conversion (_conv.array_) is
20454 used in place of A for type deduction; otherwise,
20455
20456 --If A is a function type, the pointer type produced by
20457 the function-to-pointer standard conversion
20458 (_conv.func_) is used in place of A for type deduction;
20459 otherwise,
20460
20461 --If A is a cv-qualified type, the top level
20462 cv-qualifiers of A's type are ignored for type
20463 deduction. */
20464 if (TREE_CODE (*arg) == ARRAY_TYPE)
20465 *arg = build_pointer_type (TREE_TYPE (*arg));
20466 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20467 *arg = build_pointer_type (*arg);
20468 else
20469 *arg = TYPE_MAIN_VARIANT (*arg);
20470 }
20471
20472 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20473 reference to a cv-unqualified template parameter that does not represent a
20474 template parameter of a class template (during class template argument
20475 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20476 an lvalue, the type "lvalue reference to A" is used in place of A for type
20477 deduction. */
20478 if (TYPE_REF_P (*parm)
20479 && TYPE_REF_IS_RVALUE (*parm)
20480 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20481 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20482 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20483 && (arg_expr ? lvalue_p (arg_expr)
20484 /* try_one_overload doesn't provide an arg_expr, but
20485 functions are always lvalues. */
20486 : TREE_CODE (*arg) == FUNCTION_TYPE))
20487 *arg = build_reference_type (*arg);
20488
20489 /* [temp.deduct.call]
20490
20491 If P is a cv-qualified type, the top level cv-qualifiers
20492 of P's type are ignored for type deduction. If P is a
20493 reference type, the type referred to by P is used for
20494 type deduction. */
20495 *parm = TYPE_MAIN_VARIANT (*parm);
20496 if (TYPE_REF_P (*parm))
20497 {
20498 *parm = TREE_TYPE (*parm);
20499 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20500 }
20501
20502 /* DR 322. For conversion deduction, remove a reference type on parm
20503 too (which has been swapped into ARG). */
20504 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20505 *arg = TREE_TYPE (*arg);
20506
20507 return result;
20508 }
20509
20510 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20511 template which doesn't contain any deducible template parameters; check if
20512 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20513 unify_one_argument. */
20514
20515 static int
20516 check_non_deducible_conversion (tree parm, tree arg, int strict,
20517 int flags, struct conversion **conv_p,
20518 bool explain_p)
20519 {
20520 tree type;
20521
20522 if (!TYPE_P (arg))
20523 type = TREE_TYPE (arg);
20524 else
20525 type = arg;
20526
20527 if (same_type_p (parm, type))
20528 return unify_success (explain_p);
20529
20530 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20531 if (strict == DEDUCE_CONV)
20532 {
20533 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20534 return unify_success (explain_p);
20535 }
20536 else if (strict != DEDUCE_EXACT)
20537 {
20538 bool ok = false;
20539 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20540 if (conv_p)
20541 /* Avoid recalculating this in add_function_candidate. */
20542 ok = (*conv_p
20543 = good_conversion (parm, type, conv_arg, flags, complain));
20544 else
20545 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20546 if (ok)
20547 return unify_success (explain_p);
20548 }
20549
20550 if (strict == DEDUCE_EXACT)
20551 return unify_type_mismatch (explain_p, parm, arg);
20552 else
20553 return unify_arg_conversion (explain_p, parm, type, arg);
20554 }
20555
20556 static bool uses_deducible_template_parms (tree type);
20557
20558 /* Returns true iff the expression EXPR is one from which a template
20559 argument can be deduced. In other words, if it's an undecorated
20560 use of a template non-type parameter. */
20561
20562 static bool
20563 deducible_expression (tree expr)
20564 {
20565 /* Strip implicit conversions. */
20566 while (CONVERT_EXPR_P (expr))
20567 expr = TREE_OPERAND (expr, 0);
20568 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20569 }
20570
20571 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20572 deducible way; that is, if it has a max value of <PARM> - 1. */
20573
20574 static bool
20575 deducible_array_bound (tree domain)
20576 {
20577 if (domain == NULL_TREE)
20578 return false;
20579
20580 tree max = TYPE_MAX_VALUE (domain);
20581 if (TREE_CODE (max) != MINUS_EXPR)
20582 return false;
20583
20584 return deducible_expression (TREE_OPERAND (max, 0));
20585 }
20586
20587 /* Returns true iff the template arguments ARGS use a template parameter
20588 in a deducible way. */
20589
20590 static bool
20591 deducible_template_args (tree args)
20592 {
20593 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20594 {
20595 bool deducible;
20596 tree elt = TREE_VEC_ELT (args, i);
20597 if (ARGUMENT_PACK_P (elt))
20598 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20599 else
20600 {
20601 if (PACK_EXPANSION_P (elt))
20602 elt = PACK_EXPANSION_PATTERN (elt);
20603 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20604 deducible = true;
20605 else if (TYPE_P (elt))
20606 deducible = uses_deducible_template_parms (elt);
20607 else
20608 deducible = deducible_expression (elt);
20609 }
20610 if (deducible)
20611 return true;
20612 }
20613 return false;
20614 }
20615
20616 /* Returns true iff TYPE contains any deducible references to template
20617 parameters, as per 14.8.2.5. */
20618
20619 static bool
20620 uses_deducible_template_parms (tree type)
20621 {
20622 if (PACK_EXPANSION_P (type))
20623 type = PACK_EXPANSION_PATTERN (type);
20624
20625 /* T
20626 cv-list T
20627 TT<T>
20628 TT<i>
20629 TT<> */
20630 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20631 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20632 return true;
20633
20634 /* T*
20635 T&
20636 T&& */
20637 if (INDIRECT_TYPE_P (type))
20638 return uses_deducible_template_parms (TREE_TYPE (type));
20639
20640 /* T[integer-constant ]
20641 type [i] */
20642 if (TREE_CODE (type) == ARRAY_TYPE)
20643 return (uses_deducible_template_parms (TREE_TYPE (type))
20644 || deducible_array_bound (TYPE_DOMAIN (type)));
20645
20646 /* T type ::*
20647 type T::*
20648 T T::*
20649 T (type ::*)()
20650 type (T::*)()
20651 type (type ::*)(T)
20652 type (T::*)(T)
20653 T (type ::*)(T)
20654 T (T::*)()
20655 T (T::*)(T) */
20656 if (TYPE_PTRMEM_P (type))
20657 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20658 || (uses_deducible_template_parms
20659 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20660
20661 /* template-name <T> (where template-name refers to a class template)
20662 template-name <i> (where template-name refers to a class template) */
20663 if (CLASS_TYPE_P (type)
20664 && CLASSTYPE_TEMPLATE_INFO (type)
20665 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20666 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20667 (CLASSTYPE_TI_ARGS (type)));
20668
20669 /* type (T)
20670 T()
20671 T(T) */
20672 if (FUNC_OR_METHOD_TYPE_P (type))
20673 {
20674 if (uses_deducible_template_parms (TREE_TYPE (type)))
20675 return true;
20676 tree parm = TYPE_ARG_TYPES (type);
20677 if (TREE_CODE (type) == METHOD_TYPE)
20678 parm = TREE_CHAIN (parm);
20679 for (; parm; parm = TREE_CHAIN (parm))
20680 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20681 return true;
20682 }
20683
20684 return false;
20685 }
20686
20687 /* Subroutine of type_unification_real and unify_pack_expansion to
20688 handle unification of a single P/A pair. Parameters are as
20689 for those functions. */
20690
20691 static int
20692 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20693 int subr, unification_kind_t strict,
20694 bool explain_p)
20695 {
20696 tree arg_expr = NULL_TREE;
20697 int arg_strict;
20698
20699 if (arg == error_mark_node || parm == error_mark_node)
20700 return unify_invalid (explain_p);
20701 if (arg == unknown_type_node)
20702 /* We can't deduce anything from this, but we might get all the
20703 template args from other function args. */
20704 return unify_success (explain_p);
20705
20706 /* Implicit conversions (Clause 4) will be performed on a function
20707 argument to convert it to the type of the corresponding function
20708 parameter if the parameter type contains no template-parameters that
20709 participate in template argument deduction. */
20710 if (strict != DEDUCE_EXACT
20711 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20712 /* For function parameters with no deducible template parameters,
20713 just return. We'll check non-dependent conversions later. */
20714 return unify_success (explain_p);
20715
20716 switch (strict)
20717 {
20718 case DEDUCE_CALL:
20719 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20720 | UNIFY_ALLOW_MORE_CV_QUAL
20721 | UNIFY_ALLOW_DERIVED);
20722 break;
20723
20724 case DEDUCE_CONV:
20725 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20726 break;
20727
20728 case DEDUCE_EXACT:
20729 arg_strict = UNIFY_ALLOW_NONE;
20730 break;
20731
20732 default:
20733 gcc_unreachable ();
20734 }
20735
20736 /* We only do these transformations if this is the top-level
20737 parameter_type_list in a call or declaration matching; in other
20738 situations (nested function declarators, template argument lists) we
20739 won't be comparing a type to an expression, and we don't do any type
20740 adjustments. */
20741 if (!subr)
20742 {
20743 if (!TYPE_P (arg))
20744 {
20745 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20746 if (type_unknown_p (arg))
20747 {
20748 /* [temp.deduct.type] A template-argument can be
20749 deduced from a pointer to function or pointer
20750 to member function argument if the set of
20751 overloaded functions does not contain function
20752 templates and at most one of a set of
20753 overloaded functions provides a unique
20754 match. */
20755 resolve_overloaded_unification (tparms, targs, parm,
20756 arg, strict,
20757 arg_strict, explain_p);
20758 /* If a unique match was not found, this is a
20759 non-deduced context, so we still succeed. */
20760 return unify_success (explain_p);
20761 }
20762
20763 arg_expr = arg;
20764 arg = unlowered_expr_type (arg);
20765 if (arg == error_mark_node)
20766 return unify_invalid (explain_p);
20767 }
20768
20769 arg_strict |=
20770 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20771 }
20772 else
20773 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20774 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20775 return unify_template_argument_mismatch (explain_p, parm, arg);
20776
20777 /* For deduction from an init-list we need the actual list. */
20778 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20779 arg = arg_expr;
20780 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20781 }
20782
20783 /* for_each_template_parm callback that always returns 0. */
20784
20785 static int
20786 zero_r (tree, void *)
20787 {
20788 return 0;
20789 }
20790
20791 /* for_each_template_parm any_fn callback to handle deduction of a template
20792 type argument from the type of an array bound. */
20793
20794 static int
20795 array_deduction_r (tree t, void *data)
20796 {
20797 tree_pair_p d = (tree_pair_p)data;
20798 tree &tparms = d->purpose;
20799 tree &targs = d->value;
20800
20801 if (TREE_CODE (t) == ARRAY_TYPE)
20802 if (tree dom = TYPE_DOMAIN (t))
20803 if (tree max = TYPE_MAX_VALUE (dom))
20804 {
20805 if (TREE_CODE (max) == MINUS_EXPR)
20806 max = TREE_OPERAND (max, 0);
20807 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20808 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20809 UNIFY_ALLOW_NONE, /*explain*/false);
20810 }
20811
20812 /* Keep walking. */
20813 return 0;
20814 }
20815
20816 /* Try to deduce any not-yet-deduced template type arguments from the type of
20817 an array bound. This is handled separately from unify because 14.8.2.5 says
20818 "The type of a type parameter is only deduced from an array bound if it is
20819 not otherwise deduced." */
20820
20821 static void
20822 try_array_deduction (tree tparms, tree targs, tree parm)
20823 {
20824 tree_pair_s data = { tparms, targs };
20825 hash_set<tree> visited;
20826 for_each_template_parm (parm, zero_r, &data, &visited,
20827 /*nondeduced*/false, array_deduction_r);
20828 }
20829
20830 /* Most parms like fn_type_unification.
20831
20832 If SUBR is 1, we're being called recursively (to unify the
20833 arguments of a function or method parameter of a function
20834 template).
20835
20836 CHECKS is a pointer to a vector of access checks encountered while
20837 substituting default template arguments. */
20838
20839 static int
20840 type_unification_real (tree tparms,
20841 tree full_targs,
20842 tree xparms,
20843 const tree *xargs,
20844 unsigned int xnargs,
20845 int subr,
20846 unification_kind_t strict,
20847 vec<deferred_access_check, va_gc> **checks,
20848 bool explain_p)
20849 {
20850 tree parm, arg;
20851 int i;
20852 int ntparms = TREE_VEC_LENGTH (tparms);
20853 int saw_undeduced = 0;
20854 tree parms;
20855 const tree *args;
20856 unsigned int nargs;
20857 unsigned int ia;
20858
20859 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20860 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20861 gcc_assert (ntparms > 0);
20862
20863 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20864
20865 /* Reset the number of non-defaulted template arguments contained
20866 in TARGS. */
20867 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20868
20869 again:
20870 parms = xparms;
20871 args = xargs;
20872 nargs = xnargs;
20873
20874 ia = 0;
20875 while (parms && parms != void_list_node
20876 && ia < nargs)
20877 {
20878 parm = TREE_VALUE (parms);
20879
20880 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20881 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20882 /* For a function parameter pack that occurs at the end of the
20883 parameter-declaration-list, the type A of each remaining
20884 argument of the call is compared with the type P of the
20885 declarator-id of the function parameter pack. */
20886 break;
20887
20888 parms = TREE_CHAIN (parms);
20889
20890 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20891 /* For a function parameter pack that does not occur at the
20892 end of the parameter-declaration-list, the type of the
20893 parameter pack is a non-deduced context. */
20894 continue;
20895
20896 arg = args[ia];
20897 ++ia;
20898
20899 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20900 explain_p))
20901 return 1;
20902 }
20903
20904 if (parms
20905 && parms != void_list_node
20906 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20907 {
20908 /* Unify the remaining arguments with the pack expansion type. */
20909 tree argvec;
20910 tree parmvec = make_tree_vec (1);
20911
20912 /* Allocate a TREE_VEC and copy in all of the arguments */
20913 argvec = make_tree_vec (nargs - ia);
20914 for (i = 0; ia < nargs; ++ia, ++i)
20915 TREE_VEC_ELT (argvec, i) = args[ia];
20916
20917 /* Copy the parameter into parmvec. */
20918 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20919 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20920 /*subr=*/subr, explain_p))
20921 return 1;
20922
20923 /* Advance to the end of the list of parameters. */
20924 parms = TREE_CHAIN (parms);
20925 }
20926
20927 /* Fail if we've reached the end of the parm list, and more args
20928 are present, and the parm list isn't variadic. */
20929 if (ia < nargs && parms == void_list_node)
20930 return unify_too_many_arguments (explain_p, nargs, ia);
20931 /* Fail if parms are left and they don't have default values and
20932 they aren't all deduced as empty packs (c++/57397). This is
20933 consistent with sufficient_parms_p. */
20934 if (parms && parms != void_list_node
20935 && TREE_PURPOSE (parms) == NULL_TREE)
20936 {
20937 unsigned int count = nargs;
20938 tree p = parms;
20939 bool type_pack_p;
20940 do
20941 {
20942 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20943 if (!type_pack_p)
20944 count++;
20945 p = TREE_CHAIN (p);
20946 }
20947 while (p && p != void_list_node);
20948 if (count != nargs)
20949 return unify_too_few_arguments (explain_p, ia, count,
20950 type_pack_p);
20951 }
20952
20953 if (!subr)
20954 {
20955 tsubst_flags_t complain = (explain_p
20956 ? tf_warning_or_error
20957 : tf_none);
20958 bool tried_array_deduction = (cxx_dialect < cxx17);
20959
20960 for (i = 0; i < ntparms; i++)
20961 {
20962 tree targ = TREE_VEC_ELT (targs, i);
20963 tree tparm = TREE_VEC_ELT (tparms, i);
20964
20965 /* Clear the "incomplete" flags on all argument packs now so that
20966 substituting them into later default arguments works. */
20967 if (targ && ARGUMENT_PACK_P (targ))
20968 {
20969 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20970 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20971 }
20972
20973 if (targ || tparm == error_mark_node)
20974 continue;
20975 tparm = TREE_VALUE (tparm);
20976
20977 if (TREE_CODE (tparm) == TYPE_DECL
20978 && !tried_array_deduction)
20979 {
20980 try_array_deduction (tparms, targs, xparms);
20981 tried_array_deduction = true;
20982 if (TREE_VEC_ELT (targs, i))
20983 continue;
20984 }
20985
20986 /* If this is an undeduced nontype parameter that depends on
20987 a type parameter, try another pass; its type may have been
20988 deduced from a later argument than the one from which
20989 this parameter can be deduced. */
20990 if (TREE_CODE (tparm) == PARM_DECL
20991 && uses_template_parms (TREE_TYPE (tparm))
20992 && saw_undeduced < 2)
20993 {
20994 saw_undeduced = 1;
20995 continue;
20996 }
20997
20998 /* Core issue #226 (C++0x) [temp.deduct]:
20999
21000 If a template argument has not been deduced, its
21001 default template argument, if any, is used.
21002
21003 When we are in C++98 mode, TREE_PURPOSE will either
21004 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21005 to explicitly check cxx_dialect here. */
21006 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21007 /* OK, there is a default argument. Wait until after the
21008 conversion check to do substitution. */
21009 continue;
21010
21011 /* If the type parameter is a parameter pack, then it will
21012 be deduced to an empty parameter pack. */
21013 if (template_parameter_pack_p (tparm))
21014 {
21015 tree arg;
21016
21017 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21018 {
21019 arg = make_node (NONTYPE_ARGUMENT_PACK);
21020 TREE_CONSTANT (arg) = 1;
21021 }
21022 else
21023 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21024
21025 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21026
21027 TREE_VEC_ELT (targs, i) = arg;
21028 continue;
21029 }
21030
21031 return unify_parameter_deduction_failure (explain_p, tparm);
21032 }
21033
21034 /* Now substitute into the default template arguments. */
21035 for (i = 0; i < ntparms; i++)
21036 {
21037 tree targ = TREE_VEC_ELT (targs, i);
21038 tree tparm = TREE_VEC_ELT (tparms, i);
21039
21040 if (targ || tparm == error_mark_node)
21041 continue;
21042 tree parm = TREE_VALUE (tparm);
21043 tree arg = TREE_PURPOSE (tparm);
21044 reopen_deferring_access_checks (*checks);
21045 location_t save_loc = input_location;
21046 if (DECL_P (parm))
21047 input_location = DECL_SOURCE_LOCATION (parm);
21048
21049 if (saw_undeduced == 1
21050 && TREE_CODE (parm) == PARM_DECL
21051 && uses_template_parms (TREE_TYPE (parm)))
21052 {
21053 /* The type of this non-type parameter depends on undeduced
21054 parameters. Don't try to use its default argument yet,
21055 since we might deduce an argument for it on the next pass,
21056 but do check whether the arguments we already have cause
21057 substitution failure, so that that happens before we try
21058 later default arguments (78489). */
21059 ++processing_template_decl;
21060 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21061 NULL_TREE);
21062 --processing_template_decl;
21063 if (type == error_mark_node)
21064 arg = error_mark_node;
21065 else
21066 arg = NULL_TREE;
21067 }
21068 else
21069 {
21070 /* Even if the call is happening in template context, getting
21071 here means it's non-dependent, and a default argument is
21072 considered a separate definition under [temp.decls], so we can
21073 do this substitution without processing_template_decl. This
21074 is important if the default argument contains something that
21075 might be instantiation-dependent like access (87480). */
21076 processing_template_decl_sentinel s;
21077 tree substed = NULL_TREE;
21078 if (saw_undeduced == 1)
21079 {
21080 /* First instatiate in template context, in case we still
21081 depend on undeduced template parameters. */
21082 ++processing_template_decl;
21083 substed = tsubst_template_arg (arg, full_targs, complain,
21084 NULL_TREE);
21085 --processing_template_decl;
21086 if (substed != error_mark_node
21087 && !uses_template_parms (substed))
21088 /* We replaced all the tparms, substitute again out of
21089 template context. */
21090 substed = NULL_TREE;
21091 }
21092 if (!substed)
21093 substed = tsubst_template_arg (arg, full_targs, complain,
21094 NULL_TREE);
21095
21096 if (!uses_template_parms (substed))
21097 arg = convert_template_argument (parm, substed, full_targs,
21098 complain, i, NULL_TREE);
21099 else if (saw_undeduced == 1)
21100 arg = NULL_TREE;
21101 else
21102 arg = error_mark_node;
21103 }
21104
21105 input_location = save_loc;
21106 *checks = get_deferred_access_checks ();
21107 pop_deferring_access_checks ();
21108
21109 if (arg == error_mark_node)
21110 return 1;
21111 else if (arg)
21112 {
21113 TREE_VEC_ELT (targs, i) = arg;
21114 /* The position of the first default template argument,
21115 is also the number of non-defaulted arguments in TARGS.
21116 Record that. */
21117 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21118 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21119 }
21120 }
21121
21122 if (saw_undeduced++ == 1)
21123 goto again;
21124 }
21125
21126 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21127 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21128
21129 return unify_success (explain_p);
21130 }
21131
21132 /* Subroutine of type_unification_real. Args are like the variables
21133 at the call site. ARG is an overloaded function (or template-id);
21134 we try deducing template args from each of the overloads, and if
21135 only one succeeds, we go with that. Modifies TARGS and returns
21136 true on success. */
21137
21138 static bool
21139 resolve_overloaded_unification (tree tparms,
21140 tree targs,
21141 tree parm,
21142 tree arg,
21143 unification_kind_t strict,
21144 int sub_strict,
21145 bool explain_p)
21146 {
21147 tree tempargs = copy_node (targs);
21148 int good = 0;
21149 tree goodfn = NULL_TREE;
21150 bool addr_p;
21151
21152 if (TREE_CODE (arg) == ADDR_EXPR)
21153 {
21154 arg = TREE_OPERAND (arg, 0);
21155 addr_p = true;
21156 }
21157 else
21158 addr_p = false;
21159
21160 if (TREE_CODE (arg) == COMPONENT_REF)
21161 /* Handle `&x' where `x' is some static or non-static member
21162 function name. */
21163 arg = TREE_OPERAND (arg, 1);
21164
21165 if (TREE_CODE (arg) == OFFSET_REF)
21166 arg = TREE_OPERAND (arg, 1);
21167
21168 /* Strip baselink information. */
21169 if (BASELINK_P (arg))
21170 arg = BASELINK_FUNCTIONS (arg);
21171
21172 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21173 {
21174 /* If we got some explicit template args, we need to plug them into
21175 the affected templates before we try to unify, in case the
21176 explicit args will completely resolve the templates in question. */
21177
21178 int ok = 0;
21179 tree expl_subargs = TREE_OPERAND (arg, 1);
21180 arg = TREE_OPERAND (arg, 0);
21181
21182 for (lkp_iterator iter (arg); iter; ++iter)
21183 {
21184 tree fn = *iter;
21185 tree subargs, elem;
21186
21187 if (TREE_CODE (fn) != TEMPLATE_DECL)
21188 continue;
21189
21190 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21191 expl_subargs, NULL_TREE, tf_none,
21192 /*require_all_args=*/true,
21193 /*use_default_args=*/true);
21194 if (subargs != error_mark_node
21195 && !any_dependent_template_arguments_p (subargs))
21196 {
21197 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21198 if (try_one_overload (tparms, targs, tempargs, parm,
21199 elem, strict, sub_strict, addr_p, explain_p)
21200 && (!goodfn || !same_type_p (goodfn, elem)))
21201 {
21202 goodfn = elem;
21203 ++good;
21204 }
21205 }
21206 else if (subargs)
21207 ++ok;
21208 }
21209 /* If no templates (or more than one) are fully resolved by the
21210 explicit arguments, this template-id is a non-deduced context; it
21211 could still be OK if we deduce all template arguments for the
21212 enclosing call through other arguments. */
21213 if (good != 1)
21214 good = ok;
21215 }
21216 else if (!OVL_P (arg))
21217 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21218 -- but the deduction does not succeed because the expression is
21219 not just the function on its own. */
21220 return false;
21221 else
21222 for (lkp_iterator iter (arg); iter; ++iter)
21223 {
21224 tree fn = *iter;
21225 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21226 strict, sub_strict, addr_p, explain_p)
21227 && (!goodfn || !decls_match (goodfn, fn)))
21228 {
21229 goodfn = fn;
21230 ++good;
21231 }
21232 }
21233
21234 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21235 to function or pointer to member function argument if the set of
21236 overloaded functions does not contain function templates and at most
21237 one of a set of overloaded functions provides a unique match.
21238
21239 So if we found multiple possibilities, we return success but don't
21240 deduce anything. */
21241
21242 if (good == 1)
21243 {
21244 int i = TREE_VEC_LENGTH (targs);
21245 for (; i--; )
21246 if (TREE_VEC_ELT (tempargs, i))
21247 {
21248 tree old = TREE_VEC_ELT (targs, i);
21249 tree new_ = TREE_VEC_ELT (tempargs, i);
21250 if (new_ && old && ARGUMENT_PACK_P (old)
21251 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21252 /* Don't forget explicit template arguments in a pack. */
21253 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21254 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21255 TREE_VEC_ELT (targs, i) = new_;
21256 }
21257 }
21258 if (good)
21259 return true;
21260
21261 return false;
21262 }
21263
21264 /* Core DR 115: In contexts where deduction is done and fails, or in
21265 contexts where deduction is not done, if a template argument list is
21266 specified and it, along with any default template arguments, identifies
21267 a single function template specialization, then the template-id is an
21268 lvalue for the function template specialization. */
21269
21270 tree
21271 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21272 {
21273 tree expr, offset, baselink;
21274 bool addr;
21275
21276 if (!type_unknown_p (orig_expr))
21277 return orig_expr;
21278
21279 expr = orig_expr;
21280 addr = false;
21281 offset = NULL_TREE;
21282 baselink = NULL_TREE;
21283
21284 if (TREE_CODE (expr) == ADDR_EXPR)
21285 {
21286 expr = TREE_OPERAND (expr, 0);
21287 addr = true;
21288 }
21289 if (TREE_CODE (expr) == OFFSET_REF)
21290 {
21291 offset = expr;
21292 expr = TREE_OPERAND (expr, 1);
21293 }
21294 if (BASELINK_P (expr))
21295 {
21296 baselink = expr;
21297 expr = BASELINK_FUNCTIONS (expr);
21298 }
21299
21300 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21301 {
21302 int good = 0;
21303 tree goodfn = NULL_TREE;
21304
21305 /* If we got some explicit template args, we need to plug them into
21306 the affected templates before we try to unify, in case the
21307 explicit args will completely resolve the templates in question. */
21308
21309 tree expl_subargs = TREE_OPERAND (expr, 1);
21310 tree arg = TREE_OPERAND (expr, 0);
21311 tree badfn = NULL_TREE;
21312 tree badargs = NULL_TREE;
21313
21314 for (lkp_iterator iter (arg); iter; ++iter)
21315 {
21316 tree fn = *iter;
21317 tree subargs, elem;
21318
21319 if (TREE_CODE (fn) != TEMPLATE_DECL)
21320 continue;
21321
21322 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21323 expl_subargs, NULL_TREE, tf_none,
21324 /*require_all_args=*/true,
21325 /*use_default_args=*/true);
21326 if (subargs != error_mark_node
21327 && !any_dependent_template_arguments_p (subargs))
21328 {
21329 elem = instantiate_template (fn, subargs, tf_none);
21330 if (elem == error_mark_node)
21331 {
21332 badfn = fn;
21333 badargs = subargs;
21334 }
21335 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21336 {
21337 goodfn = elem;
21338 ++good;
21339 }
21340 }
21341 }
21342 if (good == 1)
21343 {
21344 mark_used (goodfn);
21345 expr = goodfn;
21346 if (baselink)
21347 expr = build_baselink (BASELINK_BINFO (baselink),
21348 BASELINK_ACCESS_BINFO (baselink),
21349 expr, BASELINK_OPTYPE (baselink));
21350 if (offset)
21351 {
21352 tree base
21353 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21354 expr = build_offset_ref (base, expr, addr, complain);
21355 }
21356 if (addr)
21357 expr = cp_build_addr_expr (expr, complain);
21358 return expr;
21359 }
21360 else if (good == 0 && badargs && (complain & tf_error))
21361 /* There were no good options and at least one bad one, so let the
21362 user know what the problem is. */
21363 instantiate_template (badfn, badargs, complain);
21364 }
21365 return orig_expr;
21366 }
21367
21368 /* As above, but error out if the expression remains overloaded. */
21369
21370 tree
21371 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21372 {
21373 exp = resolve_nondeduced_context (exp, complain);
21374 if (type_unknown_p (exp))
21375 {
21376 if (complain & tf_error)
21377 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21378 return error_mark_node;
21379 }
21380 return exp;
21381 }
21382
21383 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21384 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21385 different overloads deduce different arguments for a given parm.
21386 ADDR_P is true if the expression for which deduction is being
21387 performed was of the form "& fn" rather than simply "fn".
21388
21389 Returns 1 on success. */
21390
21391 static int
21392 try_one_overload (tree tparms,
21393 tree orig_targs,
21394 tree targs,
21395 tree parm,
21396 tree arg,
21397 unification_kind_t strict,
21398 int sub_strict,
21399 bool addr_p,
21400 bool explain_p)
21401 {
21402 int nargs;
21403 tree tempargs;
21404 int i;
21405
21406 if (arg == error_mark_node)
21407 return 0;
21408
21409 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21410 to function or pointer to member function argument if the set of
21411 overloaded functions does not contain function templates and at most
21412 one of a set of overloaded functions provides a unique match.
21413
21414 So if this is a template, just return success. */
21415
21416 if (uses_template_parms (arg))
21417 return 1;
21418
21419 if (TREE_CODE (arg) == METHOD_TYPE)
21420 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21421 else if (addr_p)
21422 arg = build_pointer_type (arg);
21423
21424 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21425
21426 /* We don't copy orig_targs for this because if we have already deduced
21427 some template args from previous args, unify would complain when we
21428 try to deduce a template parameter for the same argument, even though
21429 there isn't really a conflict. */
21430 nargs = TREE_VEC_LENGTH (targs);
21431 tempargs = make_tree_vec (nargs);
21432
21433 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21434 return 0;
21435
21436 /* First make sure we didn't deduce anything that conflicts with
21437 explicitly specified args. */
21438 for (i = nargs; i--; )
21439 {
21440 tree elt = TREE_VEC_ELT (tempargs, i);
21441 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21442
21443 if (!elt)
21444 /*NOP*/;
21445 else if (uses_template_parms (elt))
21446 /* Since we're unifying against ourselves, we will fill in
21447 template args used in the function parm list with our own
21448 template parms. Discard them. */
21449 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21450 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21451 {
21452 /* Check that the argument at each index of the deduced argument pack
21453 is equivalent to the corresponding explicitly specified argument.
21454 We may have deduced more arguments than were explicitly specified,
21455 and that's OK. */
21456
21457 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21458 that's wrong if we deduce the same argument pack from multiple
21459 function arguments: it's only incomplete the first time. */
21460
21461 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21462 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21463
21464 if (TREE_VEC_LENGTH (deduced_pack)
21465 < TREE_VEC_LENGTH (explicit_pack))
21466 return 0;
21467
21468 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21469 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21470 TREE_VEC_ELT (deduced_pack, j)))
21471 return 0;
21472 }
21473 else if (oldelt && !template_args_equal (oldelt, elt))
21474 return 0;
21475 }
21476
21477 for (i = nargs; i--; )
21478 {
21479 tree elt = TREE_VEC_ELT (tempargs, i);
21480
21481 if (elt)
21482 TREE_VEC_ELT (targs, i) = elt;
21483 }
21484
21485 return 1;
21486 }
21487
21488 /* PARM is a template class (perhaps with unbound template
21489 parameters). ARG is a fully instantiated type. If ARG can be
21490 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21491 TARGS are as for unify. */
21492
21493 static tree
21494 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21495 bool explain_p)
21496 {
21497 tree copy_of_targs;
21498
21499 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21500 return NULL_TREE;
21501 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21502 /* Matches anything. */;
21503 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21504 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21505 return NULL_TREE;
21506
21507 /* We need to make a new template argument vector for the call to
21508 unify. If we used TARGS, we'd clutter it up with the result of
21509 the attempted unification, even if this class didn't work out.
21510 We also don't want to commit ourselves to all the unifications
21511 we've already done, since unification is supposed to be done on
21512 an argument-by-argument basis. In other words, consider the
21513 following pathological case:
21514
21515 template <int I, int J, int K>
21516 struct S {};
21517
21518 template <int I, int J>
21519 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21520
21521 template <int I, int J, int K>
21522 void f(S<I, J, K>, S<I, I, I>);
21523
21524 void g() {
21525 S<0, 0, 0> s0;
21526 S<0, 1, 2> s2;
21527
21528 f(s0, s2);
21529 }
21530
21531 Now, by the time we consider the unification involving `s2', we
21532 already know that we must have `f<0, 0, 0>'. But, even though
21533 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21534 because there are two ways to unify base classes of S<0, 1, 2>
21535 with S<I, I, I>. If we kept the already deduced knowledge, we
21536 would reject the possibility I=1. */
21537 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21538
21539 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21540 {
21541 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21542 return NULL_TREE;
21543 return arg;
21544 }
21545
21546 /* If unification failed, we're done. */
21547 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21548 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21549 return NULL_TREE;
21550
21551 return arg;
21552 }
21553
21554 /* Given a template type PARM and a class type ARG, find the unique
21555 base type in ARG that is an instance of PARM. We do not examine
21556 ARG itself; only its base-classes. If there is not exactly one
21557 appropriate base class, return NULL_TREE. PARM may be the type of
21558 a partial specialization, as well as a plain template type. Used
21559 by unify. */
21560
21561 static enum template_base_result
21562 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21563 bool explain_p, tree *result)
21564 {
21565 tree rval = NULL_TREE;
21566 tree binfo;
21567
21568 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21569
21570 binfo = TYPE_BINFO (complete_type (arg));
21571 if (!binfo)
21572 {
21573 /* The type could not be completed. */
21574 *result = NULL_TREE;
21575 return tbr_incomplete_type;
21576 }
21577
21578 /* Walk in inheritance graph order. The search order is not
21579 important, and this avoids multiple walks of virtual bases. */
21580 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21581 {
21582 tree r = try_class_unification (tparms, targs, parm,
21583 BINFO_TYPE (binfo), explain_p);
21584
21585 if (r)
21586 {
21587 /* If there is more than one satisfactory baseclass, then:
21588
21589 [temp.deduct.call]
21590
21591 If they yield more than one possible deduced A, the type
21592 deduction fails.
21593
21594 applies. */
21595 if (rval && !same_type_p (r, rval))
21596 {
21597 *result = NULL_TREE;
21598 return tbr_ambiguous_baseclass;
21599 }
21600
21601 rval = r;
21602 }
21603 }
21604
21605 *result = rval;
21606 return tbr_success;
21607 }
21608
21609 /* Returns the level of DECL, which declares a template parameter. */
21610
21611 static int
21612 template_decl_level (tree decl)
21613 {
21614 switch (TREE_CODE (decl))
21615 {
21616 case TYPE_DECL:
21617 case TEMPLATE_DECL:
21618 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21619
21620 case PARM_DECL:
21621 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21622
21623 default:
21624 gcc_unreachable ();
21625 }
21626 return 0;
21627 }
21628
21629 /* Decide whether ARG can be unified with PARM, considering only the
21630 cv-qualifiers of each type, given STRICT as documented for unify.
21631 Returns nonzero iff the unification is OK on that basis. */
21632
21633 static int
21634 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21635 {
21636 int arg_quals = cp_type_quals (arg);
21637 int parm_quals = cp_type_quals (parm);
21638
21639 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21640 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21641 {
21642 /* Although a CVR qualifier is ignored when being applied to a
21643 substituted template parameter ([8.3.2]/1 for example), that
21644 does not allow us to unify "const T" with "int&" because both
21645 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21646 It is ok when we're allowing additional CV qualifiers
21647 at the outer level [14.8.2.1]/3,1st bullet. */
21648 if ((TYPE_REF_P (arg)
21649 || FUNC_OR_METHOD_TYPE_P (arg))
21650 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21651 return 0;
21652
21653 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21654 && (parm_quals & TYPE_QUAL_RESTRICT))
21655 return 0;
21656 }
21657
21658 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21659 && (arg_quals & parm_quals) != parm_quals)
21660 return 0;
21661
21662 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21663 && (parm_quals & arg_quals) != arg_quals)
21664 return 0;
21665
21666 return 1;
21667 }
21668
21669 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21670 void
21671 template_parm_level_and_index (tree parm, int* level, int* index)
21672 {
21673 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21674 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21675 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21676 {
21677 *index = TEMPLATE_TYPE_IDX (parm);
21678 *level = TEMPLATE_TYPE_LEVEL (parm);
21679 }
21680 else
21681 {
21682 *index = TEMPLATE_PARM_IDX (parm);
21683 *level = TEMPLATE_PARM_LEVEL (parm);
21684 }
21685 }
21686
21687 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21688 do { \
21689 if (unify (TP, TA, P, A, S, EP)) \
21690 return 1; \
21691 } while (0)
21692
21693 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21694 expansion at the end of PACKED_PARMS. Returns 0 if the type
21695 deduction succeeds, 1 otherwise. STRICT is the same as in
21696 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21697 function call argument list. We'll need to adjust the arguments to make them
21698 types. SUBR tells us if this is from a recursive call to
21699 type_unification_real, or for comparing two template argument
21700 lists. */
21701
21702 static int
21703 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21704 tree packed_args, unification_kind_t strict,
21705 bool subr, bool explain_p)
21706 {
21707 tree parm
21708 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21709 tree pattern = PACK_EXPANSION_PATTERN (parm);
21710 tree pack, packs = NULL_TREE;
21711 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21712
21713 /* Add in any args remembered from an earlier partial instantiation. */
21714 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21715 int levels = TMPL_ARGS_DEPTH (targs);
21716
21717 packed_args = expand_template_argument_pack (packed_args);
21718
21719 int len = TREE_VEC_LENGTH (packed_args);
21720
21721 /* Determine the parameter packs we will be deducing from the
21722 pattern, and record their current deductions. */
21723 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21724 pack; pack = TREE_CHAIN (pack))
21725 {
21726 tree parm_pack = TREE_VALUE (pack);
21727 int idx, level;
21728
21729 /* Only template parameter packs can be deduced, not e.g. function
21730 parameter packs or __bases or __integer_pack. */
21731 if (!TEMPLATE_PARM_P (parm_pack))
21732 continue;
21733
21734 /* Determine the index and level of this parameter pack. */
21735 template_parm_level_and_index (parm_pack, &level, &idx);
21736 if (level < levels)
21737 continue;
21738
21739 /* Keep track of the parameter packs and their corresponding
21740 argument packs. */
21741 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21742 TREE_TYPE (packs) = make_tree_vec (len - start);
21743 }
21744
21745 /* Loop through all of the arguments that have not yet been
21746 unified and unify each with the pattern. */
21747 for (i = start; i < len; i++)
21748 {
21749 tree parm;
21750 bool any_explicit = false;
21751 tree arg = TREE_VEC_ELT (packed_args, i);
21752
21753 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21754 or the element of its argument pack at the current index if
21755 this argument was explicitly specified. */
21756 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21757 {
21758 int idx, level;
21759 tree arg, pargs;
21760 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21761
21762 arg = NULL_TREE;
21763 if (TREE_VALUE (pack)
21764 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21765 && (i - start < TREE_VEC_LENGTH (pargs)))
21766 {
21767 any_explicit = true;
21768 arg = TREE_VEC_ELT (pargs, i - start);
21769 }
21770 TMPL_ARG (targs, level, idx) = arg;
21771 }
21772
21773 /* If we had explicit template arguments, substitute them into the
21774 pattern before deduction. */
21775 if (any_explicit)
21776 {
21777 /* Some arguments might still be unspecified or dependent. */
21778 bool dependent;
21779 ++processing_template_decl;
21780 dependent = any_dependent_template_arguments_p (targs);
21781 if (!dependent)
21782 --processing_template_decl;
21783 parm = tsubst (pattern, targs,
21784 explain_p ? tf_warning_or_error : tf_none,
21785 NULL_TREE);
21786 if (dependent)
21787 --processing_template_decl;
21788 if (parm == error_mark_node)
21789 return 1;
21790 }
21791 else
21792 parm = pattern;
21793
21794 /* Unify the pattern with the current argument. */
21795 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21796 explain_p))
21797 return 1;
21798
21799 /* For each parameter pack, collect the deduced value. */
21800 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21801 {
21802 int idx, level;
21803 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21804
21805 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21806 TMPL_ARG (targs, level, idx);
21807 }
21808 }
21809
21810 /* Verify that the results of unification with the parameter packs
21811 produce results consistent with what we've seen before, and make
21812 the deduced argument packs available. */
21813 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21814 {
21815 tree old_pack = TREE_VALUE (pack);
21816 tree new_args = TREE_TYPE (pack);
21817 int i, len = TREE_VEC_LENGTH (new_args);
21818 int idx, level;
21819 bool nondeduced_p = false;
21820
21821 /* By default keep the original deduced argument pack.
21822 If necessary, more specific code is going to update the
21823 resulting deduced argument later down in this function. */
21824 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21825 TMPL_ARG (targs, level, idx) = old_pack;
21826
21827 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21828 actually deduce anything. */
21829 for (i = 0; i < len && !nondeduced_p; ++i)
21830 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21831 nondeduced_p = true;
21832 if (nondeduced_p)
21833 continue;
21834
21835 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21836 {
21837 /* If we had fewer function args than explicit template args,
21838 just use the explicits. */
21839 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21840 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21841 if (len < explicit_len)
21842 new_args = explicit_args;
21843 }
21844
21845 if (!old_pack)
21846 {
21847 tree result;
21848 /* Build the deduced *_ARGUMENT_PACK. */
21849 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21850 {
21851 result = make_node (NONTYPE_ARGUMENT_PACK);
21852 TREE_CONSTANT (result) = 1;
21853 }
21854 else
21855 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21856
21857 SET_ARGUMENT_PACK_ARGS (result, new_args);
21858
21859 /* Note the deduced argument packs for this parameter
21860 pack. */
21861 TMPL_ARG (targs, level, idx) = result;
21862 }
21863 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21864 && (ARGUMENT_PACK_ARGS (old_pack)
21865 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21866 {
21867 /* We only had the explicitly-provided arguments before, but
21868 now we have a complete set of arguments. */
21869 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21870
21871 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21872 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21873 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21874 }
21875 else
21876 {
21877 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21878 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21879
21880 if (!comp_template_args (old_args, new_args,
21881 &bad_old_arg, &bad_new_arg))
21882 /* Inconsistent unification of this parameter pack. */
21883 return unify_parameter_pack_inconsistent (explain_p,
21884 bad_old_arg,
21885 bad_new_arg);
21886 }
21887 }
21888
21889 return unify_success (explain_p);
21890 }
21891
21892 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21893 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21894 parameters and return value are as for unify. */
21895
21896 static int
21897 unify_array_domain (tree tparms, tree targs,
21898 tree parm_dom, tree arg_dom,
21899 bool explain_p)
21900 {
21901 tree parm_max;
21902 tree arg_max;
21903 bool parm_cst;
21904 bool arg_cst;
21905
21906 /* Our representation of array types uses "N - 1" as the
21907 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21908 not an integer constant. We cannot unify arbitrarily
21909 complex expressions, so we eliminate the MINUS_EXPRs
21910 here. */
21911 parm_max = TYPE_MAX_VALUE (parm_dom);
21912 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21913 if (!parm_cst)
21914 {
21915 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21916 parm_max = TREE_OPERAND (parm_max, 0);
21917 }
21918 arg_max = TYPE_MAX_VALUE (arg_dom);
21919 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21920 if (!arg_cst)
21921 {
21922 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21923 trying to unify the type of a variable with the type
21924 of a template parameter. For example:
21925
21926 template <unsigned int N>
21927 void f (char (&) [N]);
21928 int g();
21929 void h(int i) {
21930 char a[g(i)];
21931 f(a);
21932 }
21933
21934 Here, the type of the ARG will be "int [g(i)]", and
21935 may be a SAVE_EXPR, etc. */
21936 if (TREE_CODE (arg_max) != MINUS_EXPR)
21937 return unify_vla_arg (explain_p, arg_dom);
21938 arg_max = TREE_OPERAND (arg_max, 0);
21939 }
21940
21941 /* If only one of the bounds used a MINUS_EXPR, compensate
21942 by adding one to the other bound. */
21943 if (parm_cst && !arg_cst)
21944 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21945 integer_type_node,
21946 parm_max,
21947 integer_one_node);
21948 else if (arg_cst && !parm_cst)
21949 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21950 integer_type_node,
21951 arg_max,
21952 integer_one_node);
21953
21954 return unify (tparms, targs, parm_max, arg_max,
21955 UNIFY_ALLOW_INTEGER, explain_p);
21956 }
21957
21958 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21959
21960 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21961
21962 static pa_kind_t
21963 pa_kind (tree t)
21964 {
21965 if (PACK_EXPANSION_P (t))
21966 t = PACK_EXPANSION_PATTERN (t);
21967 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21968 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21969 || DECL_TYPE_TEMPLATE_P (t))
21970 return pa_tmpl;
21971 else if (TYPE_P (t))
21972 return pa_type;
21973 else
21974 return pa_expr;
21975 }
21976
21977 /* Deduce the value of template parameters. TPARMS is the (innermost)
21978 set of template parameters to a template. TARGS is the bindings
21979 for those template parameters, as determined thus far; TARGS may
21980 include template arguments for outer levels of template parameters
21981 as well. PARM is a parameter to a template function, or a
21982 subcomponent of that parameter; ARG is the corresponding argument.
21983 This function attempts to match PARM with ARG in a manner
21984 consistent with the existing assignments in TARGS. If more values
21985 are deduced, then TARGS is updated.
21986
21987 Returns 0 if the type deduction succeeds, 1 otherwise. The
21988 parameter STRICT is a bitwise or of the following flags:
21989
21990 UNIFY_ALLOW_NONE:
21991 Require an exact match between PARM and ARG.
21992 UNIFY_ALLOW_MORE_CV_QUAL:
21993 Allow the deduced ARG to be more cv-qualified (by qualification
21994 conversion) than ARG.
21995 UNIFY_ALLOW_LESS_CV_QUAL:
21996 Allow the deduced ARG to be less cv-qualified than ARG.
21997 UNIFY_ALLOW_DERIVED:
21998 Allow the deduced ARG to be a template base class of ARG,
21999 or a pointer to a template base class of the type pointed to by
22000 ARG.
22001 UNIFY_ALLOW_INTEGER:
22002 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22003 case for more information.
22004 UNIFY_ALLOW_OUTER_LEVEL:
22005 This is the outermost level of a deduction. Used to determine validity
22006 of qualification conversions. A valid qualification conversion must
22007 have const qualified pointers leading up to the inner type which
22008 requires additional CV quals, except at the outer level, where const
22009 is not required [conv.qual]. It would be normal to set this flag in
22010 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22011 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22012 This is the outermost level of a deduction, and PARM can be more CV
22013 qualified at this point.
22014 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22015 This is the outermost level of a deduction, and PARM can be less CV
22016 qualified at this point. */
22017
22018 static int
22019 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22020 bool explain_p)
22021 {
22022 int idx;
22023 tree targ;
22024 tree tparm;
22025 int strict_in = strict;
22026 tsubst_flags_t complain = (explain_p
22027 ? tf_warning_or_error
22028 : tf_none);
22029
22030 /* I don't think this will do the right thing with respect to types.
22031 But the only case I've seen it in so far has been array bounds, where
22032 signedness is the only information lost, and I think that will be
22033 okay. */
22034 while (CONVERT_EXPR_P (parm))
22035 parm = TREE_OPERAND (parm, 0);
22036
22037 if (arg == error_mark_node)
22038 return unify_invalid (explain_p);
22039 if (arg == unknown_type_node
22040 || arg == init_list_type_node)
22041 /* We can't deduce anything from this, but we might get all the
22042 template args from other function args. */
22043 return unify_success (explain_p);
22044
22045 if (parm == any_targ_node || arg == any_targ_node)
22046 return unify_success (explain_p);
22047
22048 /* If PARM uses template parameters, then we can't bail out here,
22049 even if ARG == PARM, since we won't record unifications for the
22050 template parameters. We might need them if we're trying to
22051 figure out which of two things is more specialized. */
22052 if (arg == parm && !uses_template_parms (parm))
22053 return unify_success (explain_p);
22054
22055 /* Handle init lists early, so the rest of the function can assume
22056 we're dealing with a type. */
22057 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22058 {
22059 tree elt, elttype;
22060 unsigned i;
22061 tree orig_parm = parm;
22062
22063 /* Replace T with std::initializer_list<T> for deduction. */
22064 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22065 && flag_deduce_init_list)
22066 parm = listify (parm);
22067
22068 if (!is_std_init_list (parm)
22069 && TREE_CODE (parm) != ARRAY_TYPE)
22070 /* We can only deduce from an initializer list argument if the
22071 parameter is std::initializer_list or an array; otherwise this
22072 is a non-deduced context. */
22073 return unify_success (explain_p);
22074
22075 if (TREE_CODE (parm) == ARRAY_TYPE)
22076 elttype = TREE_TYPE (parm);
22077 else
22078 {
22079 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22080 /* Deduction is defined in terms of a single type, so just punt
22081 on the (bizarre) std::initializer_list<T...>. */
22082 if (PACK_EXPANSION_P (elttype))
22083 return unify_success (explain_p);
22084 }
22085
22086 if (strict != DEDUCE_EXACT
22087 && TYPE_P (elttype)
22088 && !uses_deducible_template_parms (elttype))
22089 /* If ELTTYPE has no deducible template parms, skip deduction from
22090 the list elements. */;
22091 else
22092 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22093 {
22094 int elt_strict = strict;
22095
22096 if (elt == error_mark_node)
22097 return unify_invalid (explain_p);
22098
22099 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22100 {
22101 tree type = TREE_TYPE (elt);
22102 if (type == error_mark_node)
22103 return unify_invalid (explain_p);
22104 /* It should only be possible to get here for a call. */
22105 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22106 elt_strict |= maybe_adjust_types_for_deduction
22107 (DEDUCE_CALL, &elttype, &type, elt);
22108 elt = type;
22109 }
22110
22111 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22112 explain_p);
22113 }
22114
22115 if (TREE_CODE (parm) == ARRAY_TYPE
22116 && deducible_array_bound (TYPE_DOMAIN (parm)))
22117 {
22118 /* Also deduce from the length of the initializer list. */
22119 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22120 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22121 if (idx == error_mark_node)
22122 return unify_invalid (explain_p);
22123 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22124 idx, explain_p);
22125 }
22126
22127 /* If the std::initializer_list<T> deduction worked, replace the
22128 deduced A with std::initializer_list<A>. */
22129 if (orig_parm != parm)
22130 {
22131 idx = TEMPLATE_TYPE_IDX (orig_parm);
22132 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22133 targ = listify (targ);
22134 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22135 }
22136 return unify_success (explain_p);
22137 }
22138
22139 /* If parm and arg aren't the same kind of thing (template, type, or
22140 expression), fail early. */
22141 if (pa_kind (parm) != pa_kind (arg))
22142 return unify_invalid (explain_p);
22143
22144 /* Immediately reject some pairs that won't unify because of
22145 cv-qualification mismatches. */
22146 if (TREE_CODE (arg) == TREE_CODE (parm)
22147 && TYPE_P (arg)
22148 /* It is the elements of the array which hold the cv quals of an array
22149 type, and the elements might be template type parms. We'll check
22150 when we recurse. */
22151 && TREE_CODE (arg) != ARRAY_TYPE
22152 /* We check the cv-qualifiers when unifying with template type
22153 parameters below. We want to allow ARG `const T' to unify with
22154 PARM `T' for example, when computing which of two templates
22155 is more specialized, for example. */
22156 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22157 && !check_cv_quals_for_unify (strict_in, arg, parm))
22158 return unify_cv_qual_mismatch (explain_p, parm, arg);
22159
22160 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22161 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22162 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22163 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22164 strict &= ~UNIFY_ALLOW_DERIVED;
22165 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22166 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22167
22168 switch (TREE_CODE (parm))
22169 {
22170 case TYPENAME_TYPE:
22171 case SCOPE_REF:
22172 case UNBOUND_CLASS_TEMPLATE:
22173 /* In a type which contains a nested-name-specifier, template
22174 argument values cannot be deduced for template parameters used
22175 within the nested-name-specifier. */
22176 return unify_success (explain_p);
22177
22178 case TEMPLATE_TYPE_PARM:
22179 case TEMPLATE_TEMPLATE_PARM:
22180 case BOUND_TEMPLATE_TEMPLATE_PARM:
22181 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22182 if (error_operand_p (tparm))
22183 return unify_invalid (explain_p);
22184
22185 if (TEMPLATE_TYPE_LEVEL (parm)
22186 != template_decl_level (tparm))
22187 /* The PARM is not one we're trying to unify. Just check
22188 to see if it matches ARG. */
22189 {
22190 if (TREE_CODE (arg) == TREE_CODE (parm)
22191 && (is_auto (parm) ? is_auto (arg)
22192 : same_type_p (parm, arg)))
22193 return unify_success (explain_p);
22194 else
22195 return unify_type_mismatch (explain_p, parm, arg);
22196 }
22197 idx = TEMPLATE_TYPE_IDX (parm);
22198 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22199 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22200 if (error_operand_p (tparm))
22201 return unify_invalid (explain_p);
22202
22203 /* Check for mixed types and values. */
22204 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22205 && TREE_CODE (tparm) != TYPE_DECL)
22206 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22207 && TREE_CODE (tparm) != TEMPLATE_DECL))
22208 gcc_unreachable ();
22209
22210 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22211 {
22212 if ((strict_in & UNIFY_ALLOW_DERIVED)
22213 && CLASS_TYPE_P (arg))
22214 {
22215 /* First try to match ARG directly. */
22216 tree t = try_class_unification (tparms, targs, parm, arg,
22217 explain_p);
22218 if (!t)
22219 {
22220 /* Otherwise, look for a suitable base of ARG, as below. */
22221 enum template_base_result r;
22222 r = get_template_base (tparms, targs, parm, arg,
22223 explain_p, &t);
22224 if (!t)
22225 return unify_no_common_base (explain_p, r, parm, arg);
22226 arg = t;
22227 }
22228 }
22229 /* ARG must be constructed from a template class or a template
22230 template parameter. */
22231 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22232 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22233 return unify_template_deduction_failure (explain_p, parm, arg);
22234
22235 /* Deduce arguments T, i from TT<T> or TT<i>. */
22236 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22237 return 1;
22238
22239 arg = TYPE_TI_TEMPLATE (arg);
22240
22241 /* Fall through to deduce template name. */
22242 }
22243
22244 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22245 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22246 {
22247 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22248
22249 /* Simple cases: Value already set, does match or doesn't. */
22250 if (targ != NULL_TREE && template_args_equal (targ, arg))
22251 return unify_success (explain_p);
22252 else if (targ)
22253 return unify_inconsistency (explain_p, parm, targ, arg);
22254 }
22255 else
22256 {
22257 /* If PARM is `const T' and ARG is only `int', we don't have
22258 a match unless we are allowing additional qualification.
22259 If ARG is `const int' and PARM is just `T' that's OK;
22260 that binds `const int' to `T'. */
22261 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22262 arg, parm))
22263 return unify_cv_qual_mismatch (explain_p, parm, arg);
22264
22265 /* Consider the case where ARG is `const volatile int' and
22266 PARM is `const T'. Then, T should be `volatile int'. */
22267 arg = cp_build_qualified_type_real
22268 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22269 if (arg == error_mark_node)
22270 return unify_invalid (explain_p);
22271
22272 /* Simple cases: Value already set, does match or doesn't. */
22273 if (targ != NULL_TREE && same_type_p (targ, arg))
22274 return unify_success (explain_p);
22275 else if (targ)
22276 return unify_inconsistency (explain_p, parm, targ, arg);
22277
22278 /* Make sure that ARG is not a variable-sized array. (Note
22279 that were talking about variable-sized arrays (like
22280 `int[n]'), rather than arrays of unknown size (like
22281 `int[]').) We'll get very confused by such a type since
22282 the bound of the array is not constant, and therefore
22283 not mangleable. Besides, such types are not allowed in
22284 ISO C++, so we can do as we please here. We do allow
22285 them for 'auto' deduction, since that isn't ABI-exposed. */
22286 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22287 return unify_vla_arg (explain_p, arg);
22288
22289 /* Strip typedefs as in convert_template_argument. */
22290 arg = canonicalize_type_argument (arg, tf_none);
22291 }
22292
22293 /* If ARG is a parameter pack or an expansion, we cannot unify
22294 against it unless PARM is also a parameter pack. */
22295 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22296 && !template_parameter_pack_p (parm))
22297 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22298
22299 /* If the argument deduction results is a METHOD_TYPE,
22300 then there is a problem.
22301 METHOD_TYPE doesn't map to any real C++ type the result of
22302 the deduction cannot be of that type. */
22303 if (TREE_CODE (arg) == METHOD_TYPE)
22304 return unify_method_type_error (explain_p, arg);
22305
22306 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22307 return unify_success (explain_p);
22308
22309 case TEMPLATE_PARM_INDEX:
22310 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22311 if (error_operand_p (tparm))
22312 return unify_invalid (explain_p);
22313
22314 if (TEMPLATE_PARM_LEVEL (parm)
22315 != template_decl_level (tparm))
22316 {
22317 /* The PARM is not one we're trying to unify. Just check
22318 to see if it matches ARG. */
22319 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22320 && cp_tree_equal (parm, arg));
22321 if (result)
22322 unify_expression_unequal (explain_p, parm, arg);
22323 return result;
22324 }
22325
22326 idx = TEMPLATE_PARM_IDX (parm);
22327 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22328
22329 if (targ)
22330 {
22331 if ((strict & UNIFY_ALLOW_INTEGER)
22332 && TREE_TYPE (targ) && TREE_TYPE (arg)
22333 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22334 /* We're deducing from an array bound, the type doesn't matter. */
22335 arg = fold_convert (TREE_TYPE (targ), arg);
22336 int x = !cp_tree_equal (targ, arg);
22337 if (x)
22338 unify_inconsistency (explain_p, parm, targ, arg);
22339 return x;
22340 }
22341
22342 /* [temp.deduct.type] If, in the declaration of a function template
22343 with a non-type template-parameter, the non-type
22344 template-parameter is used in an expression in the function
22345 parameter-list and, if the corresponding template-argument is
22346 deduced, the template-argument type shall match the type of the
22347 template-parameter exactly, except that a template-argument
22348 deduced from an array bound may be of any integral type.
22349 The non-type parameter might use already deduced type parameters. */
22350 tparm = TREE_TYPE (parm);
22351 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22352 /* We don't have enough levels of args to do any substitution. This
22353 can happen in the context of -fnew-ttp-matching. */;
22354 else
22355 {
22356 ++processing_template_decl;
22357 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22358 --processing_template_decl;
22359
22360 if (tree a = type_uses_auto (tparm))
22361 {
22362 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22363 if (tparm == error_mark_node)
22364 return 1;
22365 }
22366 }
22367
22368 if (!TREE_TYPE (arg))
22369 /* Template-parameter dependent expression. Just accept it for now.
22370 It will later be processed in convert_template_argument. */
22371 ;
22372 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22373 non_reference (tparm)))
22374 /* OK */;
22375 else if ((strict & UNIFY_ALLOW_INTEGER)
22376 && CP_INTEGRAL_TYPE_P (tparm))
22377 /* Convert the ARG to the type of PARM; the deduced non-type
22378 template argument must exactly match the types of the
22379 corresponding parameter. */
22380 arg = fold (build_nop (tparm, arg));
22381 else if (uses_template_parms (tparm))
22382 {
22383 /* We haven't deduced the type of this parameter yet. */
22384 if (cxx_dialect >= cxx17
22385 /* We deduce from array bounds in try_array_deduction. */
22386 && !(strict & UNIFY_ALLOW_INTEGER))
22387 {
22388 /* Deduce it from the non-type argument. */
22389 tree atype = TREE_TYPE (arg);
22390 RECUR_AND_CHECK_FAILURE (tparms, targs,
22391 tparm, atype,
22392 UNIFY_ALLOW_NONE, explain_p);
22393 }
22394 else
22395 /* Try again later. */
22396 return unify_success (explain_p);
22397 }
22398 else
22399 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22400
22401 /* If ARG is a parameter pack or an expansion, we cannot unify
22402 against it unless PARM is also a parameter pack. */
22403 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22404 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22405 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22406
22407 {
22408 bool removed_attr = false;
22409 arg = strip_typedefs_expr (arg, &removed_attr);
22410 }
22411 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22412 return unify_success (explain_p);
22413
22414 case PTRMEM_CST:
22415 {
22416 /* A pointer-to-member constant can be unified only with
22417 another constant. */
22418 if (TREE_CODE (arg) != PTRMEM_CST)
22419 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22420
22421 /* Just unify the class member. It would be useless (and possibly
22422 wrong, depending on the strict flags) to unify also
22423 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22424 arg refer to the same variable, even if through different
22425 classes. For instance:
22426
22427 struct A { int x; };
22428 struct B : A { };
22429
22430 Unification of &A::x and &B::x must succeed. */
22431 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22432 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22433 }
22434
22435 case POINTER_TYPE:
22436 {
22437 if (!TYPE_PTR_P (arg))
22438 return unify_type_mismatch (explain_p, parm, arg);
22439
22440 /* [temp.deduct.call]
22441
22442 A can be another pointer or pointer to member type that can
22443 be converted to the deduced A via a qualification
22444 conversion (_conv.qual_).
22445
22446 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22447 This will allow for additional cv-qualification of the
22448 pointed-to types if appropriate. */
22449
22450 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22451 /* The derived-to-base conversion only persists through one
22452 level of pointers. */
22453 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22454
22455 return unify (tparms, targs, TREE_TYPE (parm),
22456 TREE_TYPE (arg), strict, explain_p);
22457 }
22458
22459 case REFERENCE_TYPE:
22460 if (!TYPE_REF_P (arg))
22461 return unify_type_mismatch (explain_p, parm, arg);
22462 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22463 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22464
22465 case ARRAY_TYPE:
22466 if (TREE_CODE (arg) != ARRAY_TYPE)
22467 return unify_type_mismatch (explain_p, parm, arg);
22468 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22469 != (TYPE_DOMAIN (arg) == NULL_TREE))
22470 return unify_type_mismatch (explain_p, parm, arg);
22471 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22472 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22473 if (TYPE_DOMAIN (parm) != NULL_TREE)
22474 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22475 TYPE_DOMAIN (arg), explain_p);
22476 return unify_success (explain_p);
22477
22478 case REAL_TYPE:
22479 case COMPLEX_TYPE:
22480 case VECTOR_TYPE:
22481 case INTEGER_TYPE:
22482 case BOOLEAN_TYPE:
22483 case ENUMERAL_TYPE:
22484 case VOID_TYPE:
22485 case NULLPTR_TYPE:
22486 if (TREE_CODE (arg) != TREE_CODE (parm))
22487 return unify_type_mismatch (explain_p, parm, arg);
22488
22489 /* We have already checked cv-qualification at the top of the
22490 function. */
22491 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22492 return unify_type_mismatch (explain_p, parm, arg);
22493
22494 /* As far as unification is concerned, this wins. Later checks
22495 will invalidate it if necessary. */
22496 return unify_success (explain_p);
22497
22498 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22499 /* Type INTEGER_CST can come from ordinary constant template args. */
22500 case INTEGER_CST:
22501 while (CONVERT_EXPR_P (arg))
22502 arg = TREE_OPERAND (arg, 0);
22503
22504 if (TREE_CODE (arg) != INTEGER_CST)
22505 return unify_template_argument_mismatch (explain_p, parm, arg);
22506 return (tree_int_cst_equal (parm, arg)
22507 ? unify_success (explain_p)
22508 : unify_template_argument_mismatch (explain_p, parm, arg));
22509
22510 case TREE_VEC:
22511 {
22512 int i, len, argslen;
22513 int parm_variadic_p = 0;
22514
22515 if (TREE_CODE (arg) != TREE_VEC)
22516 return unify_template_argument_mismatch (explain_p, parm, arg);
22517
22518 len = TREE_VEC_LENGTH (parm);
22519 argslen = TREE_VEC_LENGTH (arg);
22520
22521 /* Check for pack expansions in the parameters. */
22522 for (i = 0; i < len; ++i)
22523 {
22524 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22525 {
22526 if (i == len - 1)
22527 /* We can unify against something with a trailing
22528 parameter pack. */
22529 parm_variadic_p = 1;
22530 else
22531 /* [temp.deduct.type]/9: If the template argument list of
22532 P contains a pack expansion that is not the last
22533 template argument, the entire template argument list
22534 is a non-deduced context. */
22535 return unify_success (explain_p);
22536 }
22537 }
22538
22539 /* If we don't have enough arguments to satisfy the parameters
22540 (not counting the pack expression at the end), or we have
22541 too many arguments for a parameter list that doesn't end in
22542 a pack expression, we can't unify. */
22543 if (parm_variadic_p
22544 ? argslen < len - parm_variadic_p
22545 : argslen != len)
22546 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22547
22548 /* Unify all of the parameters that precede the (optional)
22549 pack expression. */
22550 for (i = 0; i < len - parm_variadic_p; ++i)
22551 {
22552 RECUR_AND_CHECK_FAILURE (tparms, targs,
22553 TREE_VEC_ELT (parm, i),
22554 TREE_VEC_ELT (arg, i),
22555 UNIFY_ALLOW_NONE, explain_p);
22556 }
22557 if (parm_variadic_p)
22558 return unify_pack_expansion (tparms, targs, parm, arg,
22559 DEDUCE_EXACT,
22560 /*subr=*/true, explain_p);
22561 return unify_success (explain_p);
22562 }
22563
22564 case RECORD_TYPE:
22565 case UNION_TYPE:
22566 if (TREE_CODE (arg) != TREE_CODE (parm))
22567 return unify_type_mismatch (explain_p, parm, arg);
22568
22569 if (TYPE_PTRMEMFUNC_P (parm))
22570 {
22571 if (!TYPE_PTRMEMFUNC_P (arg))
22572 return unify_type_mismatch (explain_p, parm, arg);
22573
22574 return unify (tparms, targs,
22575 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22576 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22577 strict, explain_p);
22578 }
22579 else if (TYPE_PTRMEMFUNC_P (arg))
22580 return unify_type_mismatch (explain_p, parm, arg);
22581
22582 if (CLASSTYPE_TEMPLATE_INFO (parm))
22583 {
22584 tree t = NULL_TREE;
22585
22586 if (strict_in & UNIFY_ALLOW_DERIVED)
22587 {
22588 /* First, we try to unify the PARM and ARG directly. */
22589 t = try_class_unification (tparms, targs,
22590 parm, arg, explain_p);
22591
22592 if (!t)
22593 {
22594 /* Fallback to the special case allowed in
22595 [temp.deduct.call]:
22596
22597 If P is a class, and P has the form
22598 template-id, then A can be a derived class of
22599 the deduced A. Likewise, if P is a pointer to
22600 a class of the form template-id, A can be a
22601 pointer to a derived class pointed to by the
22602 deduced A. */
22603 enum template_base_result r;
22604 r = get_template_base (tparms, targs, parm, arg,
22605 explain_p, &t);
22606
22607 if (!t)
22608 {
22609 /* Don't give the derived diagnostic if we're
22610 already dealing with the same template. */
22611 bool same_template
22612 = (CLASSTYPE_TEMPLATE_INFO (arg)
22613 && (CLASSTYPE_TI_TEMPLATE (parm)
22614 == CLASSTYPE_TI_TEMPLATE (arg)));
22615 return unify_no_common_base (explain_p && !same_template,
22616 r, parm, arg);
22617 }
22618 }
22619 }
22620 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22621 && (CLASSTYPE_TI_TEMPLATE (parm)
22622 == CLASSTYPE_TI_TEMPLATE (arg)))
22623 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22624 Then, we should unify `int' and `U'. */
22625 t = arg;
22626 else
22627 /* There's no chance of unification succeeding. */
22628 return unify_type_mismatch (explain_p, parm, arg);
22629
22630 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22631 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22632 }
22633 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22634 return unify_type_mismatch (explain_p, parm, arg);
22635 return unify_success (explain_p);
22636
22637 case METHOD_TYPE:
22638 case FUNCTION_TYPE:
22639 {
22640 unsigned int nargs;
22641 tree *args;
22642 tree a;
22643 unsigned int i;
22644
22645 if (TREE_CODE (arg) != TREE_CODE (parm))
22646 return unify_type_mismatch (explain_p, parm, arg);
22647
22648 /* CV qualifications for methods can never be deduced, they must
22649 match exactly. We need to check them explicitly here,
22650 because type_unification_real treats them as any other
22651 cv-qualified parameter. */
22652 if (TREE_CODE (parm) == METHOD_TYPE
22653 && (!check_cv_quals_for_unify
22654 (UNIFY_ALLOW_NONE,
22655 class_of_this_parm (arg),
22656 class_of_this_parm (parm))))
22657 return unify_cv_qual_mismatch (explain_p, parm, arg);
22658 if (TREE_CODE (arg) == FUNCTION_TYPE
22659 && type_memfn_quals (parm) != type_memfn_quals (arg))
22660 return unify_cv_qual_mismatch (explain_p, parm, arg);
22661 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22662 return unify_type_mismatch (explain_p, parm, arg);
22663
22664 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22665 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22666
22667 nargs = list_length (TYPE_ARG_TYPES (arg));
22668 args = XALLOCAVEC (tree, nargs);
22669 for (a = TYPE_ARG_TYPES (arg), i = 0;
22670 a != NULL_TREE && a != void_list_node;
22671 a = TREE_CHAIN (a), ++i)
22672 args[i] = TREE_VALUE (a);
22673 nargs = i;
22674
22675 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22676 args, nargs, 1, DEDUCE_EXACT,
22677 NULL, explain_p))
22678 return 1;
22679
22680 if (flag_noexcept_type)
22681 {
22682 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22683 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22684 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22685 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22686 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22687 && uses_template_parms (TREE_PURPOSE (pspec)))
22688 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22689 TREE_PURPOSE (aspec),
22690 UNIFY_ALLOW_NONE, explain_p);
22691 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22692 return unify_type_mismatch (explain_p, parm, arg);
22693 }
22694
22695 return 0;
22696 }
22697
22698 case OFFSET_TYPE:
22699 /* Unify a pointer to member with a pointer to member function, which
22700 deduces the type of the member as a function type. */
22701 if (TYPE_PTRMEMFUNC_P (arg))
22702 {
22703 /* Check top-level cv qualifiers */
22704 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22705 return unify_cv_qual_mismatch (explain_p, parm, arg);
22706
22707 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22708 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22709 UNIFY_ALLOW_NONE, explain_p);
22710
22711 /* Determine the type of the function we are unifying against. */
22712 tree fntype = static_fn_type (arg);
22713
22714 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22715 }
22716
22717 if (TREE_CODE (arg) != OFFSET_TYPE)
22718 return unify_type_mismatch (explain_p, parm, arg);
22719 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22720 TYPE_OFFSET_BASETYPE (arg),
22721 UNIFY_ALLOW_NONE, explain_p);
22722 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22723 strict, explain_p);
22724
22725 case CONST_DECL:
22726 if (DECL_TEMPLATE_PARM_P (parm))
22727 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22728 if (arg != scalar_constant_value (parm))
22729 return unify_template_argument_mismatch (explain_p, parm, arg);
22730 return unify_success (explain_p);
22731
22732 case FIELD_DECL:
22733 case TEMPLATE_DECL:
22734 /* Matched cases are handled by the ARG == PARM test above. */
22735 return unify_template_argument_mismatch (explain_p, parm, arg);
22736
22737 case VAR_DECL:
22738 /* We might get a variable as a non-type template argument in parm if the
22739 corresponding parameter is type-dependent. Make any necessary
22740 adjustments based on whether arg is a reference. */
22741 if (CONSTANT_CLASS_P (arg))
22742 parm = fold_non_dependent_expr (parm, complain);
22743 else if (REFERENCE_REF_P (arg))
22744 {
22745 tree sub = TREE_OPERAND (arg, 0);
22746 STRIP_NOPS (sub);
22747 if (TREE_CODE (sub) == ADDR_EXPR)
22748 arg = TREE_OPERAND (sub, 0);
22749 }
22750 /* Now use the normal expression code to check whether they match. */
22751 goto expr;
22752
22753 case TYPE_ARGUMENT_PACK:
22754 case NONTYPE_ARGUMENT_PACK:
22755 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22756 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22757
22758 case TYPEOF_TYPE:
22759 case DECLTYPE_TYPE:
22760 case UNDERLYING_TYPE:
22761 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22762 or UNDERLYING_TYPE nodes. */
22763 return unify_success (explain_p);
22764
22765 case ERROR_MARK:
22766 /* Unification fails if we hit an error node. */
22767 return unify_invalid (explain_p);
22768
22769 case INDIRECT_REF:
22770 if (REFERENCE_REF_P (parm))
22771 {
22772 bool pexp = PACK_EXPANSION_P (arg);
22773 if (pexp)
22774 arg = PACK_EXPANSION_PATTERN (arg);
22775 if (REFERENCE_REF_P (arg))
22776 arg = TREE_OPERAND (arg, 0);
22777 if (pexp)
22778 arg = make_pack_expansion (arg, complain);
22779 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22780 strict, explain_p);
22781 }
22782 /* FALLTHRU */
22783
22784 default:
22785 /* An unresolved overload is a nondeduced context. */
22786 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22787 return unify_success (explain_p);
22788 gcc_assert (EXPR_P (parm)
22789 || COMPOUND_LITERAL_P (parm)
22790 || TREE_CODE (parm) == TRAIT_EXPR);
22791 expr:
22792 /* We must be looking at an expression. This can happen with
22793 something like:
22794
22795 template <int I>
22796 void foo(S<I>, S<I + 2>);
22797
22798 or
22799
22800 template<typename T>
22801 void foo(A<T, T{}>);
22802
22803 This is a "non-deduced context":
22804
22805 [deduct.type]
22806
22807 The non-deduced contexts are:
22808
22809 --A non-type template argument or an array bound in which
22810 a subexpression references a template parameter.
22811
22812 In these cases, we assume deduction succeeded, but don't
22813 actually infer any unifications. */
22814
22815 if (!uses_template_parms (parm)
22816 && !template_args_equal (parm, arg))
22817 return unify_expression_unequal (explain_p, parm, arg);
22818 else
22819 return unify_success (explain_p);
22820 }
22821 }
22822 #undef RECUR_AND_CHECK_FAILURE
22823 \f
22824 /* Note that DECL can be defined in this translation unit, if
22825 required. */
22826
22827 static void
22828 mark_definable (tree decl)
22829 {
22830 tree clone;
22831 DECL_NOT_REALLY_EXTERN (decl) = 1;
22832 FOR_EACH_CLONE (clone, decl)
22833 DECL_NOT_REALLY_EXTERN (clone) = 1;
22834 }
22835
22836 /* Called if RESULT is explicitly instantiated, or is a member of an
22837 explicitly instantiated class. */
22838
22839 void
22840 mark_decl_instantiated (tree result, int extern_p)
22841 {
22842 SET_DECL_EXPLICIT_INSTANTIATION (result);
22843
22844 /* If this entity has already been written out, it's too late to
22845 make any modifications. */
22846 if (TREE_ASM_WRITTEN (result))
22847 return;
22848
22849 /* For anonymous namespace we don't need to do anything. */
22850 if (decl_anon_ns_mem_p (result))
22851 {
22852 gcc_assert (!TREE_PUBLIC (result));
22853 return;
22854 }
22855
22856 if (TREE_CODE (result) != FUNCTION_DECL)
22857 /* The TREE_PUBLIC flag for function declarations will have been
22858 set correctly by tsubst. */
22859 TREE_PUBLIC (result) = 1;
22860
22861 /* This might have been set by an earlier implicit instantiation. */
22862 DECL_COMDAT (result) = 0;
22863
22864 if (extern_p)
22865 DECL_NOT_REALLY_EXTERN (result) = 0;
22866 else
22867 {
22868 mark_definable (result);
22869 mark_needed (result);
22870 /* Always make artificials weak. */
22871 if (DECL_ARTIFICIAL (result) && flag_weak)
22872 comdat_linkage (result);
22873 /* For WIN32 we also want to put explicit instantiations in
22874 linkonce sections. */
22875 else if (TREE_PUBLIC (result))
22876 maybe_make_one_only (result);
22877 if (TREE_CODE (result) == FUNCTION_DECL
22878 && DECL_TEMPLATE_INSTANTIATED (result))
22879 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22880 since start_preparsed_function wouldn't have if we had an earlier
22881 extern explicit instantiation. */
22882 DECL_EXTERNAL (result) = 0;
22883 }
22884
22885 /* If EXTERN_P, then this function will not be emitted -- unless
22886 followed by an explicit instantiation, at which point its linkage
22887 will be adjusted. If !EXTERN_P, then this function will be
22888 emitted here. In neither circumstance do we want
22889 import_export_decl to adjust the linkage. */
22890 DECL_INTERFACE_KNOWN (result) = 1;
22891 }
22892
22893 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22894 important template arguments. If any are missing, we check whether
22895 they're important by using error_mark_node for substituting into any
22896 args that were used for partial ordering (the ones between ARGS and END)
22897 and seeing if it bubbles up. */
22898
22899 static bool
22900 check_undeduced_parms (tree targs, tree args, tree end)
22901 {
22902 bool found = false;
22903 int i;
22904 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22905 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22906 {
22907 found = true;
22908 TREE_VEC_ELT (targs, i) = error_mark_node;
22909 }
22910 if (found)
22911 {
22912 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22913 if (substed == error_mark_node)
22914 return true;
22915 }
22916 return false;
22917 }
22918
22919 /* Given two function templates PAT1 and PAT2, return:
22920
22921 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22922 -1 if PAT2 is more specialized than PAT1.
22923 0 if neither is more specialized.
22924
22925 LEN indicates the number of parameters we should consider
22926 (defaulted parameters should not be considered).
22927
22928 The 1998 std underspecified function template partial ordering, and
22929 DR214 addresses the issue. We take pairs of arguments, one from
22930 each of the templates, and deduce them against each other. One of
22931 the templates will be more specialized if all the *other*
22932 template's arguments deduce against its arguments and at least one
22933 of its arguments *does* *not* deduce against the other template's
22934 corresponding argument. Deduction is done as for class templates.
22935 The arguments used in deduction have reference and top level cv
22936 qualifiers removed. Iff both arguments were originally reference
22937 types *and* deduction succeeds in both directions, an lvalue reference
22938 wins against an rvalue reference and otherwise the template
22939 with the more cv-qualified argument wins for that pairing (if
22940 neither is more cv-qualified, they both are equal). Unlike regular
22941 deduction, after all the arguments have been deduced in this way,
22942 we do *not* verify the deduced template argument values can be
22943 substituted into non-deduced contexts.
22944
22945 The logic can be a bit confusing here, because we look at deduce1 and
22946 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22947 can find template arguments for pat1 to make arg1 look like arg2, that
22948 means that arg2 is at least as specialized as arg1. */
22949
22950 int
22951 more_specialized_fn (tree pat1, tree pat2, int len)
22952 {
22953 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22954 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22955 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22956 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22957 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22958 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22959 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22960 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22961 tree origs1, origs2;
22962 bool lose1 = false;
22963 bool lose2 = false;
22964
22965 /* Remove the this parameter from non-static member functions. If
22966 one is a non-static member function and the other is not a static
22967 member function, remove the first parameter from that function
22968 also. This situation occurs for operator functions where we
22969 locate both a member function (with this pointer) and non-member
22970 operator (with explicit first operand). */
22971 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22972 {
22973 len--; /* LEN is the number of significant arguments for DECL1 */
22974 args1 = TREE_CHAIN (args1);
22975 if (!DECL_STATIC_FUNCTION_P (decl2))
22976 args2 = TREE_CHAIN (args2);
22977 }
22978 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22979 {
22980 args2 = TREE_CHAIN (args2);
22981 if (!DECL_STATIC_FUNCTION_P (decl1))
22982 {
22983 len--;
22984 args1 = TREE_CHAIN (args1);
22985 }
22986 }
22987
22988 /* If only one is a conversion operator, they are unordered. */
22989 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22990 return 0;
22991
22992 /* Consider the return type for a conversion function */
22993 if (DECL_CONV_FN_P (decl1))
22994 {
22995 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22996 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22997 len++;
22998 }
22999
23000 processing_template_decl++;
23001
23002 origs1 = args1;
23003 origs2 = args2;
23004
23005 while (len--
23006 /* Stop when an ellipsis is seen. */
23007 && args1 != NULL_TREE && args2 != NULL_TREE)
23008 {
23009 tree arg1 = TREE_VALUE (args1);
23010 tree arg2 = TREE_VALUE (args2);
23011 int deduce1, deduce2;
23012 int quals1 = -1;
23013 int quals2 = -1;
23014 int ref1 = 0;
23015 int ref2 = 0;
23016
23017 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23018 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23019 {
23020 /* When both arguments are pack expansions, we need only
23021 unify the patterns themselves. */
23022 arg1 = PACK_EXPANSION_PATTERN (arg1);
23023 arg2 = PACK_EXPANSION_PATTERN (arg2);
23024
23025 /* This is the last comparison we need to do. */
23026 len = 0;
23027 }
23028
23029 /* DR 1847: If a particular P contains no template-parameters that
23030 participate in template argument deduction, that P is not used to
23031 determine the ordering. */
23032 if (!uses_deducible_template_parms (arg1)
23033 && !uses_deducible_template_parms (arg2))
23034 goto next;
23035
23036 if (TYPE_REF_P (arg1))
23037 {
23038 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23039 arg1 = TREE_TYPE (arg1);
23040 quals1 = cp_type_quals (arg1);
23041 }
23042
23043 if (TYPE_REF_P (arg2))
23044 {
23045 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23046 arg2 = TREE_TYPE (arg2);
23047 quals2 = cp_type_quals (arg2);
23048 }
23049
23050 arg1 = TYPE_MAIN_VARIANT (arg1);
23051 arg2 = TYPE_MAIN_VARIANT (arg2);
23052
23053 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23054 {
23055 int i, len2 = remaining_arguments (args2);
23056 tree parmvec = make_tree_vec (1);
23057 tree argvec = make_tree_vec (len2);
23058 tree ta = args2;
23059
23060 /* Setup the parameter vector, which contains only ARG1. */
23061 TREE_VEC_ELT (parmvec, 0) = arg1;
23062
23063 /* Setup the argument vector, which contains the remaining
23064 arguments. */
23065 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23066 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23067
23068 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23069 argvec, DEDUCE_EXACT,
23070 /*subr=*/true, /*explain_p=*/false)
23071 == 0);
23072
23073 /* We cannot deduce in the other direction, because ARG1 is
23074 a pack expansion but ARG2 is not. */
23075 deduce2 = 0;
23076 }
23077 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23078 {
23079 int i, len1 = remaining_arguments (args1);
23080 tree parmvec = make_tree_vec (1);
23081 tree argvec = make_tree_vec (len1);
23082 tree ta = args1;
23083
23084 /* Setup the parameter vector, which contains only ARG1. */
23085 TREE_VEC_ELT (parmvec, 0) = arg2;
23086
23087 /* Setup the argument vector, which contains the remaining
23088 arguments. */
23089 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23090 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23091
23092 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23093 argvec, DEDUCE_EXACT,
23094 /*subr=*/true, /*explain_p=*/false)
23095 == 0);
23096
23097 /* We cannot deduce in the other direction, because ARG2 is
23098 a pack expansion but ARG1 is not.*/
23099 deduce1 = 0;
23100 }
23101
23102 else
23103 {
23104 /* The normal case, where neither argument is a pack
23105 expansion. */
23106 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23107 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23108 == 0);
23109 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23110 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23111 == 0);
23112 }
23113
23114 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23115 arg2, then arg2 is not as specialized as arg1. */
23116 if (!deduce1)
23117 lose2 = true;
23118 if (!deduce2)
23119 lose1 = true;
23120
23121 /* "If, for a given type, deduction succeeds in both directions
23122 (i.e., the types are identical after the transformations above)
23123 and both P and A were reference types (before being replaced with
23124 the type referred to above):
23125 - if the type from the argument template was an lvalue reference and
23126 the type from the parameter template was not, the argument type is
23127 considered to be more specialized than the other; otherwise,
23128 - if the type from the argument template is more cv-qualified
23129 than the type from the parameter template (as described above),
23130 the argument type is considered to be more specialized than the other;
23131 otherwise,
23132 - neither type is more specialized than the other." */
23133
23134 if (deduce1 && deduce2)
23135 {
23136 if (ref1 && ref2 && ref1 != ref2)
23137 {
23138 if (ref1 > ref2)
23139 lose1 = true;
23140 else
23141 lose2 = true;
23142 }
23143 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23144 {
23145 if ((quals1 & quals2) == quals2)
23146 lose2 = true;
23147 if ((quals1 & quals2) == quals1)
23148 lose1 = true;
23149 }
23150 }
23151
23152 if (lose1 && lose2)
23153 /* We've failed to deduce something in either direction.
23154 These must be unordered. */
23155 break;
23156
23157 next:
23158
23159 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23160 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23161 /* We have already processed all of the arguments in our
23162 handing of the pack expansion type. */
23163 len = 0;
23164
23165 args1 = TREE_CHAIN (args1);
23166 args2 = TREE_CHAIN (args2);
23167 }
23168
23169 /* "In most cases, all template parameters must have values in order for
23170 deduction to succeed, but for partial ordering purposes a template
23171 parameter may remain without a value provided it is not used in the
23172 types being used for partial ordering."
23173
23174 Thus, if we are missing any of the targs1 we need to substitute into
23175 origs1, then pat2 is not as specialized as pat1. This can happen when
23176 there is a nondeduced context. */
23177 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23178 lose2 = true;
23179 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23180 lose1 = true;
23181
23182 processing_template_decl--;
23183
23184 /* If both deductions succeed, the partial ordering selects the more
23185 constrained template. */
23186 if (!lose1 && !lose2)
23187 {
23188 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23189 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23190 lose1 = !subsumes_constraints (c1, c2);
23191 lose2 = !subsumes_constraints (c2, c1);
23192 }
23193
23194 /* All things being equal, if the next argument is a pack expansion
23195 for one function but not for the other, prefer the
23196 non-variadic function. FIXME this is bogus; see c++/41958. */
23197 if (lose1 == lose2
23198 && args1 && TREE_VALUE (args1)
23199 && args2 && TREE_VALUE (args2))
23200 {
23201 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23202 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23203 }
23204
23205 if (lose1 == lose2)
23206 return 0;
23207 else if (!lose1)
23208 return 1;
23209 else
23210 return -1;
23211 }
23212
23213 /* Determine which of two partial specializations of TMPL is more
23214 specialized.
23215
23216 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23217 to the first partial specialization. The TREE_PURPOSE is the
23218 innermost set of template parameters for the partial
23219 specialization. PAT2 is similar, but for the second template.
23220
23221 Return 1 if the first partial specialization is more specialized;
23222 -1 if the second is more specialized; 0 if neither is more
23223 specialized.
23224
23225 See [temp.class.order] for information about determining which of
23226 two templates is more specialized. */
23227
23228 static int
23229 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23230 {
23231 tree targs;
23232 int winner = 0;
23233 bool any_deductions = false;
23234
23235 tree tmpl1 = TREE_VALUE (pat1);
23236 tree tmpl2 = TREE_VALUE (pat2);
23237 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23238 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23239
23240 /* Just like what happens for functions, if we are ordering between
23241 different template specializations, we may encounter dependent
23242 types in the arguments, and we need our dependency check functions
23243 to behave correctly. */
23244 ++processing_template_decl;
23245 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23246 if (targs)
23247 {
23248 --winner;
23249 any_deductions = true;
23250 }
23251
23252 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23253 if (targs)
23254 {
23255 ++winner;
23256 any_deductions = true;
23257 }
23258 --processing_template_decl;
23259
23260 /* If both deductions succeed, the partial ordering selects the more
23261 constrained template. */
23262 if (!winner && any_deductions)
23263 return more_constrained (tmpl1, tmpl2);
23264
23265 /* In the case of a tie where at least one of the templates
23266 has a parameter pack at the end, the template with the most
23267 non-packed parameters wins. */
23268 if (winner == 0
23269 && any_deductions
23270 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23271 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23272 {
23273 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23274 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23275 int len1 = TREE_VEC_LENGTH (args1);
23276 int len2 = TREE_VEC_LENGTH (args2);
23277
23278 /* We don't count the pack expansion at the end. */
23279 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23280 --len1;
23281 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23282 --len2;
23283
23284 if (len1 > len2)
23285 return 1;
23286 else if (len1 < len2)
23287 return -1;
23288 }
23289
23290 return winner;
23291 }
23292
23293 /* Return the template arguments that will produce the function signature
23294 DECL from the function template FN, with the explicit template
23295 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23296 also match. Return NULL_TREE if no satisfactory arguments could be
23297 found. */
23298
23299 static tree
23300 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23301 {
23302 int ntparms = DECL_NTPARMS (fn);
23303 tree targs = make_tree_vec (ntparms);
23304 tree decl_type = TREE_TYPE (decl);
23305 tree decl_arg_types;
23306 tree *args;
23307 unsigned int nargs, ix;
23308 tree arg;
23309
23310 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23311
23312 /* Never do unification on the 'this' parameter. */
23313 decl_arg_types = skip_artificial_parms_for (decl,
23314 TYPE_ARG_TYPES (decl_type));
23315
23316 nargs = list_length (decl_arg_types);
23317 args = XALLOCAVEC (tree, nargs);
23318 for (arg = decl_arg_types, ix = 0;
23319 arg != NULL_TREE && arg != void_list_node;
23320 arg = TREE_CHAIN (arg), ++ix)
23321 args[ix] = TREE_VALUE (arg);
23322
23323 if (fn_type_unification (fn, explicit_args, targs,
23324 args, ix,
23325 (check_rettype || DECL_CONV_FN_P (fn)
23326 ? TREE_TYPE (decl_type) : NULL_TREE),
23327 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23328 /*explain_p=*/false,
23329 /*decltype*/false)
23330 == error_mark_node)
23331 return NULL_TREE;
23332
23333 return targs;
23334 }
23335
23336 /* Return the innermost template arguments that, when applied to a partial
23337 specialization SPEC_TMPL of TMPL, yield the ARGS.
23338
23339 For example, suppose we have:
23340
23341 template <class T, class U> struct S {};
23342 template <class T> struct S<T*, int> {};
23343
23344 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23345 partial specialization and the ARGS will be {double*, int}. The resulting
23346 vector will be {double}, indicating that `T' is bound to `double'. */
23347
23348 static tree
23349 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23350 {
23351 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23352 tree spec_args
23353 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23354 int i, ntparms = TREE_VEC_LENGTH (tparms);
23355 tree deduced_args;
23356 tree innermost_deduced_args;
23357
23358 innermost_deduced_args = make_tree_vec (ntparms);
23359 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23360 {
23361 deduced_args = copy_node (args);
23362 SET_TMPL_ARGS_LEVEL (deduced_args,
23363 TMPL_ARGS_DEPTH (deduced_args),
23364 innermost_deduced_args);
23365 }
23366 else
23367 deduced_args = innermost_deduced_args;
23368
23369 bool tried_array_deduction = (cxx_dialect < cxx17);
23370 again:
23371 if (unify (tparms, deduced_args,
23372 INNERMOST_TEMPLATE_ARGS (spec_args),
23373 INNERMOST_TEMPLATE_ARGS (args),
23374 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23375 return NULL_TREE;
23376
23377 for (i = 0; i < ntparms; ++i)
23378 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23379 {
23380 if (!tried_array_deduction)
23381 {
23382 try_array_deduction (tparms, innermost_deduced_args,
23383 INNERMOST_TEMPLATE_ARGS (spec_args));
23384 tried_array_deduction = true;
23385 if (TREE_VEC_ELT (innermost_deduced_args, i))
23386 goto again;
23387 }
23388 return NULL_TREE;
23389 }
23390
23391 if (!push_tinst_level (spec_tmpl, deduced_args))
23392 {
23393 excessive_deduction_depth = true;
23394 return NULL_TREE;
23395 }
23396
23397 /* Verify that nondeduced template arguments agree with the type
23398 obtained from argument deduction.
23399
23400 For example:
23401
23402 struct A { typedef int X; };
23403 template <class T, class U> struct C {};
23404 template <class T> struct C<T, typename T::X> {};
23405
23406 Then with the instantiation `C<A, int>', we can deduce that
23407 `T' is `A' but unify () does not check whether `typename T::X'
23408 is `int'. */
23409 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23410
23411 if (spec_args != error_mark_node)
23412 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23413 INNERMOST_TEMPLATE_ARGS (spec_args),
23414 tmpl, tf_none, false, false);
23415
23416 pop_tinst_level ();
23417
23418 if (spec_args == error_mark_node
23419 /* We only need to check the innermost arguments; the other
23420 arguments will always agree. */
23421 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23422 INNERMOST_TEMPLATE_ARGS (args)))
23423 return NULL_TREE;
23424
23425 /* Now that we have bindings for all of the template arguments,
23426 ensure that the arguments deduced for the template template
23427 parameters have compatible template parameter lists. See the use
23428 of template_template_parm_bindings_ok_p in fn_type_unification
23429 for more information. */
23430 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23431 return NULL_TREE;
23432
23433 return deduced_args;
23434 }
23435
23436 // Compare two function templates T1 and T2 by deducing bindings
23437 // from one against the other. If both deductions succeed, compare
23438 // constraints to see which is more constrained.
23439 static int
23440 more_specialized_inst (tree t1, tree t2)
23441 {
23442 int fate = 0;
23443 int count = 0;
23444
23445 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23446 {
23447 --fate;
23448 ++count;
23449 }
23450
23451 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23452 {
23453 ++fate;
23454 ++count;
23455 }
23456
23457 // If both deductions succeed, then one may be more constrained.
23458 if (count == 2 && fate == 0)
23459 fate = more_constrained (t1, t2);
23460
23461 return fate;
23462 }
23463
23464 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23465 Return the TREE_LIST node with the most specialized template, if
23466 any. If there is no most specialized template, the error_mark_node
23467 is returned.
23468
23469 Note that this function does not look at, or modify, the
23470 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23471 returned is one of the elements of INSTANTIATIONS, callers may
23472 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23473 and retrieve it from the value returned. */
23474
23475 tree
23476 most_specialized_instantiation (tree templates)
23477 {
23478 tree fn, champ;
23479
23480 ++processing_template_decl;
23481
23482 champ = templates;
23483 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23484 {
23485 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23486 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23487 if (fate == -1)
23488 champ = fn;
23489 else if (!fate)
23490 {
23491 /* Equally specialized, move to next function. If there
23492 is no next function, nothing's most specialized. */
23493 fn = TREE_CHAIN (fn);
23494 champ = fn;
23495 if (!fn)
23496 break;
23497 }
23498 }
23499
23500 if (champ)
23501 /* Now verify that champ is better than everything earlier in the
23502 instantiation list. */
23503 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23504 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23505 {
23506 champ = NULL_TREE;
23507 break;
23508 }
23509 }
23510
23511 processing_template_decl--;
23512
23513 if (!champ)
23514 return error_mark_node;
23515
23516 return champ;
23517 }
23518
23519 /* If DECL is a specialization of some template, return the most
23520 general such template. Otherwise, returns NULL_TREE.
23521
23522 For example, given:
23523
23524 template <class T> struct S { template <class U> void f(U); };
23525
23526 if TMPL is `template <class U> void S<int>::f(U)' this will return
23527 the full template. This function will not trace past partial
23528 specializations, however. For example, given in addition:
23529
23530 template <class T> struct S<T*> { template <class U> void f(U); };
23531
23532 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23533 `template <class T> template <class U> S<T*>::f(U)'. */
23534
23535 tree
23536 most_general_template (tree decl)
23537 {
23538 if (TREE_CODE (decl) != TEMPLATE_DECL)
23539 {
23540 if (tree tinfo = get_template_info (decl))
23541 decl = TI_TEMPLATE (tinfo);
23542 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23543 template friend, or a FIELD_DECL for a capture pack. */
23544 if (TREE_CODE (decl) != TEMPLATE_DECL)
23545 return NULL_TREE;
23546 }
23547
23548 /* Look for more and more general templates. */
23549 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23550 {
23551 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23552 (See cp-tree.h for details.) */
23553 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23554 break;
23555
23556 if (CLASS_TYPE_P (TREE_TYPE (decl))
23557 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23558 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23559 break;
23560
23561 /* Stop if we run into an explicitly specialized class template. */
23562 if (!DECL_NAMESPACE_SCOPE_P (decl)
23563 && DECL_CONTEXT (decl)
23564 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23565 break;
23566
23567 decl = DECL_TI_TEMPLATE (decl);
23568 }
23569
23570 return decl;
23571 }
23572
23573 /* Return the most specialized of the template partial specializations
23574 which can produce TARGET, a specialization of some class or variable
23575 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23576 a TEMPLATE_DECL node corresponding to the partial specialization, while
23577 the TREE_PURPOSE is the set of template arguments that must be
23578 substituted into the template pattern in order to generate TARGET.
23579
23580 If the choice of partial specialization is ambiguous, a diagnostic
23581 is issued, and the error_mark_node is returned. If there are no
23582 partial specializations matching TARGET, then NULL_TREE is
23583 returned, indicating that the primary template should be used. */
23584
23585 static tree
23586 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23587 {
23588 tree list = NULL_TREE;
23589 tree t;
23590 tree champ;
23591 int fate;
23592 bool ambiguous_p;
23593 tree outer_args = NULL_TREE;
23594 tree tmpl, args;
23595
23596 if (TYPE_P (target))
23597 {
23598 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23599 tmpl = TI_TEMPLATE (tinfo);
23600 args = TI_ARGS (tinfo);
23601 }
23602 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23603 {
23604 tmpl = TREE_OPERAND (target, 0);
23605 args = TREE_OPERAND (target, 1);
23606 }
23607 else if (VAR_P (target))
23608 {
23609 tree tinfo = DECL_TEMPLATE_INFO (target);
23610 tmpl = TI_TEMPLATE (tinfo);
23611 args = TI_ARGS (tinfo);
23612 }
23613 else
23614 gcc_unreachable ();
23615
23616 tree main_tmpl = most_general_template (tmpl);
23617
23618 /* For determining which partial specialization to use, only the
23619 innermost args are interesting. */
23620 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23621 {
23622 outer_args = strip_innermost_template_args (args, 1);
23623 args = INNERMOST_TEMPLATE_ARGS (args);
23624 }
23625
23626 /* The caller hasn't called push_to_top_level yet, but we need
23627 get_partial_spec_bindings to be done in non-template context so that we'll
23628 fully resolve everything. */
23629 processing_template_decl_sentinel ptds;
23630
23631 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23632 {
23633 tree spec_args;
23634 tree spec_tmpl = TREE_VALUE (t);
23635
23636 if (outer_args)
23637 {
23638 /* Substitute in the template args from the enclosing class. */
23639 ++processing_template_decl;
23640 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23641 --processing_template_decl;
23642 }
23643
23644 if (spec_tmpl == error_mark_node)
23645 return error_mark_node;
23646
23647 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23648 if (spec_args)
23649 {
23650 if (outer_args)
23651 spec_args = add_to_template_args (outer_args, spec_args);
23652
23653 /* Keep the candidate only if the constraints are satisfied,
23654 or if we're not compiling with concepts. */
23655 if (!flag_concepts
23656 || constraints_satisfied_p (spec_tmpl, spec_args))
23657 {
23658 list = tree_cons (spec_args, TREE_VALUE (t), list);
23659 TREE_TYPE (list) = TREE_TYPE (t);
23660 }
23661 }
23662 }
23663
23664 if (! list)
23665 return NULL_TREE;
23666
23667 ambiguous_p = false;
23668 t = list;
23669 champ = t;
23670 t = TREE_CHAIN (t);
23671 for (; t; t = TREE_CHAIN (t))
23672 {
23673 fate = more_specialized_partial_spec (tmpl, champ, t);
23674 if (fate == 1)
23675 ;
23676 else
23677 {
23678 if (fate == 0)
23679 {
23680 t = TREE_CHAIN (t);
23681 if (! t)
23682 {
23683 ambiguous_p = true;
23684 break;
23685 }
23686 }
23687 champ = t;
23688 }
23689 }
23690
23691 if (!ambiguous_p)
23692 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23693 {
23694 fate = more_specialized_partial_spec (tmpl, champ, t);
23695 if (fate != 1)
23696 {
23697 ambiguous_p = true;
23698 break;
23699 }
23700 }
23701
23702 if (ambiguous_p)
23703 {
23704 const char *str;
23705 char *spaces = NULL;
23706 if (!(complain & tf_error))
23707 return error_mark_node;
23708 if (TYPE_P (target))
23709 error ("ambiguous template instantiation for %q#T", target);
23710 else
23711 error ("ambiguous template instantiation for %q#D", target);
23712 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23713 for (t = list; t; t = TREE_CHAIN (t))
23714 {
23715 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23716 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23717 "%s %#qS", spaces ? spaces : str, subst);
23718 spaces = spaces ? spaces : get_spaces (str);
23719 }
23720 free (spaces);
23721 return error_mark_node;
23722 }
23723
23724 return champ;
23725 }
23726
23727 /* Explicitly instantiate DECL. */
23728
23729 void
23730 do_decl_instantiation (tree decl, tree storage)
23731 {
23732 tree result = NULL_TREE;
23733 int extern_p = 0;
23734
23735 if (!decl || decl == error_mark_node)
23736 /* An error occurred, for which grokdeclarator has already issued
23737 an appropriate message. */
23738 return;
23739 else if (! DECL_LANG_SPECIFIC (decl))
23740 {
23741 error ("explicit instantiation of non-template %q#D", decl);
23742 return;
23743 }
23744 else if (DECL_DECLARED_CONCEPT_P (decl))
23745 {
23746 if (VAR_P (decl))
23747 error ("explicit instantiation of variable concept %q#D", decl);
23748 else
23749 error ("explicit instantiation of function concept %q#D", decl);
23750 return;
23751 }
23752
23753 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23754 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23755
23756 if (VAR_P (decl) && !var_templ)
23757 {
23758 /* There is an asymmetry here in the way VAR_DECLs and
23759 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23760 the latter, the DECL we get back will be marked as a
23761 template instantiation, and the appropriate
23762 DECL_TEMPLATE_INFO will be set up. This does not happen for
23763 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23764 should handle VAR_DECLs as it currently handles
23765 FUNCTION_DECLs. */
23766 if (!DECL_CLASS_SCOPE_P (decl))
23767 {
23768 error ("%qD is not a static data member of a class template", decl);
23769 return;
23770 }
23771 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23772 if (!result || !VAR_P (result))
23773 {
23774 error ("no matching template for %qD found", decl);
23775 return;
23776 }
23777 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23778 {
23779 error ("type %qT for explicit instantiation %qD does not match "
23780 "declared type %qT", TREE_TYPE (result), decl,
23781 TREE_TYPE (decl));
23782 return;
23783 }
23784 }
23785 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23786 {
23787 error ("explicit instantiation of %q#D", decl);
23788 return;
23789 }
23790 else
23791 result = decl;
23792
23793 /* Check for various error cases. Note that if the explicit
23794 instantiation is valid the RESULT will currently be marked as an
23795 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23796 until we get here. */
23797
23798 if (DECL_TEMPLATE_SPECIALIZATION (result))
23799 {
23800 /* DR 259 [temp.spec].
23801
23802 Both an explicit instantiation and a declaration of an explicit
23803 specialization shall not appear in a program unless the explicit
23804 instantiation follows a declaration of the explicit specialization.
23805
23806 For a given set of template parameters, if an explicit
23807 instantiation of a template appears after a declaration of an
23808 explicit specialization for that template, the explicit
23809 instantiation has no effect. */
23810 return;
23811 }
23812 else if (DECL_EXPLICIT_INSTANTIATION (result))
23813 {
23814 /* [temp.spec]
23815
23816 No program shall explicitly instantiate any template more
23817 than once.
23818
23819 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23820 the first instantiation was `extern' and the second is not,
23821 and EXTERN_P for the opposite case. */
23822 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23823 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23824 /* If an "extern" explicit instantiation follows an ordinary
23825 explicit instantiation, the template is instantiated. */
23826 if (extern_p)
23827 return;
23828 }
23829 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23830 {
23831 error ("no matching template for %qD found", result);
23832 return;
23833 }
23834 else if (!DECL_TEMPLATE_INFO (result))
23835 {
23836 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23837 return;
23838 }
23839
23840 if (storage == NULL_TREE)
23841 ;
23842 else if (storage == ridpointers[(int) RID_EXTERN])
23843 {
23844 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23845 pedwarn (input_location, OPT_Wpedantic,
23846 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23847 "instantiations");
23848 extern_p = 1;
23849 }
23850 else
23851 error ("storage class %qD applied to template instantiation", storage);
23852
23853 check_explicit_instantiation_namespace (result);
23854 mark_decl_instantiated (result, extern_p);
23855 if (! extern_p)
23856 instantiate_decl (result, /*defer_ok=*/true,
23857 /*expl_inst_class_mem_p=*/false);
23858 }
23859
23860 static void
23861 mark_class_instantiated (tree t, int extern_p)
23862 {
23863 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23864 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23865 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23866 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23867 if (! extern_p)
23868 {
23869 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23870 rest_of_type_compilation (t, 1);
23871 }
23872 }
23873
23874 /* Called from do_type_instantiation through binding_table_foreach to
23875 do recursive instantiation for the type bound in ENTRY. */
23876 static void
23877 bt_instantiate_type_proc (binding_entry entry, void *data)
23878 {
23879 tree storage = *(tree *) data;
23880
23881 if (MAYBE_CLASS_TYPE_P (entry->type)
23882 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23883 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23884 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23885 }
23886
23887 /* Perform an explicit instantiation of template class T. STORAGE, if
23888 non-null, is the RID for extern, inline or static. COMPLAIN is
23889 nonzero if this is called from the parser, zero if called recursively,
23890 since the standard is unclear (as detailed below). */
23891
23892 void
23893 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23894 {
23895 int extern_p = 0;
23896 int nomem_p = 0;
23897 int static_p = 0;
23898 int previous_instantiation_extern_p = 0;
23899
23900 if (TREE_CODE (t) == TYPE_DECL)
23901 t = TREE_TYPE (t);
23902
23903 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23904 {
23905 tree tmpl =
23906 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23907 if (tmpl)
23908 error ("explicit instantiation of non-class template %qD", tmpl);
23909 else
23910 error ("explicit instantiation of non-template type %qT", t);
23911 return;
23912 }
23913
23914 complete_type (t);
23915
23916 if (!COMPLETE_TYPE_P (t))
23917 {
23918 if (complain & tf_error)
23919 error ("explicit instantiation of %q#T before definition of template",
23920 t);
23921 return;
23922 }
23923
23924 if (storage != NULL_TREE)
23925 {
23926 if (!in_system_header_at (input_location))
23927 {
23928 if (storage == ridpointers[(int) RID_EXTERN])
23929 {
23930 if (cxx_dialect == cxx98)
23931 pedwarn (input_location, OPT_Wpedantic,
23932 "ISO C++ 1998 forbids the use of %<extern%> on "
23933 "explicit instantiations");
23934 }
23935 else
23936 pedwarn (input_location, OPT_Wpedantic,
23937 "ISO C++ forbids the use of %qE"
23938 " on explicit instantiations", storage);
23939 }
23940
23941 if (storage == ridpointers[(int) RID_INLINE])
23942 nomem_p = 1;
23943 else if (storage == ridpointers[(int) RID_EXTERN])
23944 extern_p = 1;
23945 else if (storage == ridpointers[(int) RID_STATIC])
23946 static_p = 1;
23947 else
23948 {
23949 error ("storage class %qD applied to template instantiation",
23950 storage);
23951 extern_p = 0;
23952 }
23953 }
23954
23955 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23956 {
23957 /* DR 259 [temp.spec].
23958
23959 Both an explicit instantiation and a declaration of an explicit
23960 specialization shall not appear in a program unless the explicit
23961 instantiation follows a declaration of the explicit specialization.
23962
23963 For a given set of template parameters, if an explicit
23964 instantiation of a template appears after a declaration of an
23965 explicit specialization for that template, the explicit
23966 instantiation has no effect. */
23967 return;
23968 }
23969 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23970 {
23971 /* [temp.spec]
23972
23973 No program shall explicitly instantiate any template more
23974 than once.
23975
23976 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23977 instantiation was `extern'. If EXTERN_P then the second is.
23978 These cases are OK. */
23979 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23980
23981 if (!previous_instantiation_extern_p && !extern_p
23982 && (complain & tf_error))
23983 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23984
23985 /* If we've already instantiated the template, just return now. */
23986 if (!CLASSTYPE_INTERFACE_ONLY (t))
23987 return;
23988 }
23989
23990 check_explicit_instantiation_namespace (TYPE_NAME (t));
23991 mark_class_instantiated (t, extern_p);
23992
23993 if (nomem_p)
23994 return;
23995
23996 /* In contrast to implicit instantiation, where only the
23997 declarations, and not the definitions, of members are
23998 instantiated, we have here:
23999
24000 [temp.explicit]
24001
24002 The explicit instantiation of a class template specialization
24003 implies the instantiation of all of its members not
24004 previously explicitly specialized in the translation unit
24005 containing the explicit instantiation.
24006
24007 Of course, we can't instantiate member template classes, since we
24008 don't have any arguments for them. Note that the standard is
24009 unclear on whether the instantiation of the members are
24010 *explicit* instantiations or not. However, the most natural
24011 interpretation is that it should be an explicit
24012 instantiation. */
24013 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24014 if ((VAR_P (fld)
24015 || (TREE_CODE (fld) == FUNCTION_DECL
24016 && !static_p
24017 && user_provided_p (fld)))
24018 && DECL_TEMPLATE_INSTANTIATION (fld))
24019 {
24020 mark_decl_instantiated (fld, extern_p);
24021 if (! extern_p)
24022 instantiate_decl (fld, /*defer_ok=*/true,
24023 /*expl_inst_class_mem_p=*/true);
24024 }
24025
24026 if (CLASSTYPE_NESTED_UTDS (t))
24027 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24028 bt_instantiate_type_proc, &storage);
24029 }
24030
24031 /* Given a function DECL, which is a specialization of TMPL, modify
24032 DECL to be a re-instantiation of TMPL with the same template
24033 arguments. TMPL should be the template into which tsubst'ing
24034 should occur for DECL, not the most general template.
24035
24036 One reason for doing this is a scenario like this:
24037
24038 template <class T>
24039 void f(const T&, int i);
24040
24041 void g() { f(3, 7); }
24042
24043 template <class T>
24044 void f(const T& t, const int i) { }
24045
24046 Note that when the template is first instantiated, with
24047 instantiate_template, the resulting DECL will have no name for the
24048 first parameter, and the wrong type for the second. So, when we go
24049 to instantiate the DECL, we regenerate it. */
24050
24051 static void
24052 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24053 {
24054 /* The arguments used to instantiate DECL, from the most general
24055 template. */
24056 tree code_pattern;
24057
24058 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24059
24060 /* Make sure that we can see identifiers, and compute access
24061 correctly. */
24062 push_access_scope (decl);
24063
24064 if (TREE_CODE (decl) == FUNCTION_DECL)
24065 {
24066 tree decl_parm;
24067 tree pattern_parm;
24068 tree specs;
24069 int args_depth;
24070 int parms_depth;
24071
24072 args_depth = TMPL_ARGS_DEPTH (args);
24073 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24074 if (args_depth > parms_depth)
24075 args = get_innermost_template_args (args, parms_depth);
24076
24077 /* Instantiate a dynamic exception-specification. noexcept will be
24078 handled below. */
24079 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24080 if (TREE_VALUE (raises))
24081 {
24082 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24083 args, tf_error, NULL_TREE,
24084 /*defer_ok*/false);
24085 if (specs && specs != error_mark_node)
24086 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24087 specs);
24088 }
24089
24090 /* Merge parameter declarations. */
24091 decl_parm = skip_artificial_parms_for (decl,
24092 DECL_ARGUMENTS (decl));
24093 pattern_parm
24094 = skip_artificial_parms_for (code_pattern,
24095 DECL_ARGUMENTS (code_pattern));
24096 while (decl_parm && !DECL_PACK_P (pattern_parm))
24097 {
24098 tree parm_type;
24099 tree attributes;
24100
24101 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24102 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24103 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24104 NULL_TREE);
24105 parm_type = type_decays_to (parm_type);
24106 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24107 TREE_TYPE (decl_parm) = parm_type;
24108 attributes = DECL_ATTRIBUTES (pattern_parm);
24109 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24110 {
24111 DECL_ATTRIBUTES (decl_parm) = attributes;
24112 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24113 }
24114 decl_parm = DECL_CHAIN (decl_parm);
24115 pattern_parm = DECL_CHAIN (pattern_parm);
24116 }
24117 /* Merge any parameters that match with the function parameter
24118 pack. */
24119 if (pattern_parm && DECL_PACK_P (pattern_parm))
24120 {
24121 int i, len;
24122 tree expanded_types;
24123 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24124 the parameters in this function parameter pack. */
24125 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24126 args, tf_error, NULL_TREE);
24127 len = TREE_VEC_LENGTH (expanded_types);
24128 for (i = 0; i < len; i++)
24129 {
24130 tree parm_type;
24131 tree attributes;
24132
24133 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24134 /* Rename the parameter to include the index. */
24135 DECL_NAME (decl_parm) =
24136 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24137 parm_type = TREE_VEC_ELT (expanded_types, i);
24138 parm_type = type_decays_to (parm_type);
24139 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24140 TREE_TYPE (decl_parm) = parm_type;
24141 attributes = DECL_ATTRIBUTES (pattern_parm);
24142 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24143 {
24144 DECL_ATTRIBUTES (decl_parm) = attributes;
24145 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24146 }
24147 decl_parm = DECL_CHAIN (decl_parm);
24148 }
24149 }
24150 /* Merge additional specifiers from the CODE_PATTERN. */
24151 if (DECL_DECLARED_INLINE_P (code_pattern)
24152 && !DECL_DECLARED_INLINE_P (decl))
24153 DECL_DECLARED_INLINE_P (decl) = 1;
24154
24155 maybe_instantiate_noexcept (decl, tf_error);
24156 }
24157 else if (VAR_P (decl))
24158 {
24159 start_lambda_scope (decl);
24160 DECL_INITIAL (decl) =
24161 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24162 tf_error, DECL_TI_TEMPLATE (decl));
24163 finish_lambda_scope ();
24164 if (VAR_HAD_UNKNOWN_BOUND (decl))
24165 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24166 tf_error, DECL_TI_TEMPLATE (decl));
24167 }
24168 else
24169 gcc_unreachable ();
24170
24171 pop_access_scope (decl);
24172 }
24173
24174 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24175 substituted to get DECL. */
24176
24177 tree
24178 template_for_substitution (tree decl)
24179 {
24180 tree tmpl = DECL_TI_TEMPLATE (decl);
24181
24182 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24183 for the instantiation. This is not always the most general
24184 template. Consider, for example:
24185
24186 template <class T>
24187 struct S { template <class U> void f();
24188 template <> void f<int>(); };
24189
24190 and an instantiation of S<double>::f<int>. We want TD to be the
24191 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24192 while (/* An instantiation cannot have a definition, so we need a
24193 more general template. */
24194 DECL_TEMPLATE_INSTANTIATION (tmpl)
24195 /* We must also deal with friend templates. Given:
24196
24197 template <class T> struct S {
24198 template <class U> friend void f() {};
24199 };
24200
24201 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24202 so far as the language is concerned, but that's still
24203 where we get the pattern for the instantiation from. On
24204 other hand, if the definition comes outside the class, say:
24205
24206 template <class T> struct S {
24207 template <class U> friend void f();
24208 };
24209 template <class U> friend void f() {}
24210
24211 we don't need to look any further. That's what the check for
24212 DECL_INITIAL is for. */
24213 || (TREE_CODE (decl) == FUNCTION_DECL
24214 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24215 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24216 {
24217 /* The present template, TD, should not be a definition. If it
24218 were a definition, we should be using it! Note that we
24219 cannot restructure the loop to just keep going until we find
24220 a template with a definition, since that might go too far if
24221 a specialization was declared, but not defined. */
24222
24223 /* Fetch the more general template. */
24224 tmpl = DECL_TI_TEMPLATE (tmpl);
24225 }
24226
24227 return tmpl;
24228 }
24229
24230 /* Returns true if we need to instantiate this template instance even if we
24231 know we aren't going to emit it. */
24232
24233 bool
24234 always_instantiate_p (tree decl)
24235 {
24236 /* We always instantiate inline functions so that we can inline them. An
24237 explicit instantiation declaration prohibits implicit instantiation of
24238 non-inline functions. With high levels of optimization, we would
24239 normally inline non-inline functions -- but we're not allowed to do
24240 that for "extern template" functions. Therefore, we check
24241 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24242 return ((TREE_CODE (decl) == FUNCTION_DECL
24243 && (DECL_DECLARED_INLINE_P (decl)
24244 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24245 /* And we need to instantiate static data members so that
24246 their initializers are available in integral constant
24247 expressions. */
24248 || (VAR_P (decl)
24249 && decl_maybe_constant_var_p (decl)));
24250 }
24251
24252 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24253 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24254 error, true otherwise. */
24255
24256 bool
24257 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24258 {
24259 tree fntype, spec, noex, clone;
24260
24261 /* Don't instantiate a noexcept-specification from template context. */
24262 if (processing_template_decl
24263 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24264 return true;
24265
24266 if (DECL_CLONED_FUNCTION_P (fn))
24267 fn = DECL_CLONED_FUNCTION (fn);
24268
24269 tree orig_fn = NULL_TREE;
24270 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24271 its FUNCTION_DECL for the rest of this function -- push_access_scope
24272 doesn't accept TEMPLATE_DECLs. */
24273 if (DECL_FUNCTION_TEMPLATE_P (fn))
24274 {
24275 orig_fn = fn;
24276 fn = DECL_TEMPLATE_RESULT (fn);
24277 }
24278
24279 fntype = TREE_TYPE (fn);
24280 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24281
24282 if (!spec || !TREE_PURPOSE (spec))
24283 return true;
24284
24285 noex = TREE_PURPOSE (spec);
24286
24287 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24288 {
24289 static hash_set<tree>* fns = new hash_set<tree>;
24290 bool added = false;
24291 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24292 {
24293 spec = get_defaulted_eh_spec (fn, complain);
24294 if (spec == error_mark_node)
24295 /* This might have failed because of an unparsed DMI, so
24296 let's try again later. */
24297 return false;
24298 }
24299 else if (!(added = !fns->add (fn)))
24300 {
24301 /* If hash_set::add returns true, the element was already there. */
24302 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24303 DECL_SOURCE_LOCATION (fn));
24304 error_at (loc,
24305 "exception specification of %qD depends on itself",
24306 fn);
24307 spec = noexcept_false_spec;
24308 }
24309 else if (push_tinst_level (fn))
24310 {
24311 push_access_scope (fn);
24312 push_deferring_access_checks (dk_no_deferred);
24313 input_location = DECL_SOURCE_LOCATION (fn);
24314
24315 tree save_ccp = current_class_ptr;
24316 tree save_ccr = current_class_ref;
24317 /* If needed, set current_class_ptr for the benefit of
24318 tsubst_copy/PARM_DECL. */
24319 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24320 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24321 {
24322 tree this_parm = DECL_ARGUMENTS (tdecl);
24323 current_class_ptr = NULL_TREE;
24324 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24325 current_class_ptr = this_parm;
24326 }
24327
24328 /* If this function is represented by a TEMPLATE_DECL, then
24329 the deferred noexcept-specification might still contain
24330 dependent types, even after substitution. And we need the
24331 dependency check functions to work in build_noexcept_spec. */
24332 if (orig_fn)
24333 ++processing_template_decl;
24334
24335 /* Do deferred instantiation of the noexcept-specifier. */
24336 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24337 DEFERRED_NOEXCEPT_ARGS (noex),
24338 tf_warning_or_error, fn,
24339 /*function_p=*/false,
24340 /*i_c_e_p=*/true);
24341
24342 current_class_ptr = save_ccp;
24343 current_class_ref = save_ccr;
24344
24345 /* Build up the noexcept-specification. */
24346 spec = build_noexcept_spec (noex, tf_warning_or_error);
24347
24348 if (orig_fn)
24349 --processing_template_decl;
24350
24351 pop_deferring_access_checks ();
24352 pop_access_scope (fn);
24353 pop_tinst_level ();
24354 }
24355 else
24356 spec = noexcept_false_spec;
24357
24358 if (added)
24359 fns->remove (fn);
24360
24361 if (spec == error_mark_node)
24362 {
24363 /* This failed with a hard error, so let's go with false. */
24364 gcc_assert (seen_error ());
24365 spec = noexcept_false_spec;
24366 }
24367
24368 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24369 if (orig_fn)
24370 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24371 }
24372
24373 FOR_EACH_CLONE (clone, fn)
24374 {
24375 if (TREE_TYPE (clone) == fntype)
24376 TREE_TYPE (clone) = TREE_TYPE (fn);
24377 else
24378 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24379 }
24380
24381 return true;
24382 }
24383
24384 /* We're starting to process the function INST, an instantiation of PATTERN;
24385 add their parameters to local_specializations. */
24386
24387 static void
24388 register_parameter_specializations (tree pattern, tree inst)
24389 {
24390 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24391 tree spec_parm = DECL_ARGUMENTS (inst);
24392 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24393 {
24394 register_local_specialization (spec_parm, tmpl_parm);
24395 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24396 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24397 }
24398 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24399 {
24400 if (!DECL_PACK_P (tmpl_parm))
24401 {
24402 register_local_specialization (spec_parm, tmpl_parm);
24403 spec_parm = DECL_CHAIN (spec_parm);
24404 }
24405 else
24406 {
24407 /* Register the (value) argument pack as a specialization of
24408 TMPL_PARM, then move on. */
24409 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24410 register_local_specialization (argpack, tmpl_parm);
24411 }
24412 }
24413 gcc_assert (!spec_parm);
24414 }
24415
24416 /* Produce the definition of D, a _DECL generated from a template. If
24417 DEFER_OK is true, then we don't have to actually do the
24418 instantiation now; we just have to do it sometime. Normally it is
24419 an error if this is an explicit instantiation but D is undefined.
24420 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24421 instantiated class template. */
24422
24423 tree
24424 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24425 {
24426 tree tmpl = DECL_TI_TEMPLATE (d);
24427 tree gen_args;
24428 tree args;
24429 tree td;
24430 tree code_pattern;
24431 tree spec;
24432 tree gen_tmpl;
24433 bool pattern_defined;
24434 location_t saved_loc = input_location;
24435 int saved_unevaluated_operand = cp_unevaluated_operand;
24436 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24437 bool external_p;
24438 bool deleted_p;
24439
24440 /* This function should only be used to instantiate templates for
24441 functions and static member variables. */
24442 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24443
24444 /* A concept is never instantiated. */
24445 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24446
24447 /* Variables are never deferred; if instantiation is required, they
24448 are instantiated right away. That allows for better code in the
24449 case that an expression refers to the value of the variable --
24450 if the variable has a constant value the referring expression can
24451 take advantage of that fact. */
24452 if (VAR_P (d))
24453 defer_ok = false;
24454
24455 /* Don't instantiate cloned functions. Instead, instantiate the
24456 functions they cloned. */
24457 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24458 d = DECL_CLONED_FUNCTION (d);
24459
24460 if (DECL_TEMPLATE_INSTANTIATED (d)
24461 || (TREE_CODE (d) == FUNCTION_DECL
24462 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24463 || DECL_TEMPLATE_SPECIALIZATION (d))
24464 /* D has already been instantiated or explicitly specialized, so
24465 there's nothing for us to do here.
24466
24467 It might seem reasonable to check whether or not D is an explicit
24468 instantiation, and, if so, stop here. But when an explicit
24469 instantiation is deferred until the end of the compilation,
24470 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24471 the instantiation. */
24472 return d;
24473
24474 /* Check to see whether we know that this template will be
24475 instantiated in some other file, as with "extern template"
24476 extension. */
24477 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24478
24479 /* In general, we do not instantiate such templates. */
24480 if (external_p && !always_instantiate_p (d))
24481 return d;
24482
24483 gen_tmpl = most_general_template (tmpl);
24484 gen_args = DECL_TI_ARGS (d);
24485
24486 if (tmpl != gen_tmpl)
24487 /* We should already have the extra args. */
24488 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24489 == TMPL_ARGS_DEPTH (gen_args));
24490 /* And what's in the hash table should match D. */
24491 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24492 || spec == NULL_TREE);
24493
24494 /* This needs to happen before any tsubsting. */
24495 if (! push_tinst_level (d))
24496 return d;
24497
24498 timevar_push (TV_TEMPLATE_INST);
24499
24500 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24501 for the instantiation. */
24502 td = template_for_substitution (d);
24503 args = gen_args;
24504
24505 if (VAR_P (d))
24506 {
24507 /* Look up an explicit specialization, if any. */
24508 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24509 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24510 if (elt && elt != error_mark_node)
24511 {
24512 td = TREE_VALUE (elt);
24513 args = TREE_PURPOSE (elt);
24514 }
24515 }
24516
24517 code_pattern = DECL_TEMPLATE_RESULT (td);
24518
24519 /* We should never be trying to instantiate a member of a class
24520 template or partial specialization. */
24521 gcc_assert (d != code_pattern);
24522
24523 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24524 || DECL_TEMPLATE_SPECIALIZATION (td))
24525 /* In the case of a friend template whose definition is provided
24526 outside the class, we may have too many arguments. Drop the
24527 ones we don't need. The same is true for specializations. */
24528 args = get_innermost_template_args
24529 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24530
24531 if (TREE_CODE (d) == FUNCTION_DECL)
24532 {
24533 deleted_p = DECL_DELETED_FN (code_pattern);
24534 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24535 && DECL_INITIAL (code_pattern) != error_mark_node)
24536 || DECL_DEFAULTED_FN (code_pattern)
24537 || deleted_p);
24538 }
24539 else
24540 {
24541 deleted_p = false;
24542 if (DECL_CLASS_SCOPE_P (code_pattern))
24543 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24544 else
24545 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24546 }
24547
24548 /* We may be in the middle of deferred access check. Disable it now. */
24549 push_deferring_access_checks (dk_no_deferred);
24550
24551 /* Unless an explicit instantiation directive has already determined
24552 the linkage of D, remember that a definition is available for
24553 this entity. */
24554 if (pattern_defined
24555 && !DECL_INTERFACE_KNOWN (d)
24556 && !DECL_NOT_REALLY_EXTERN (d))
24557 mark_definable (d);
24558
24559 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24560 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24561 input_location = DECL_SOURCE_LOCATION (d);
24562
24563 /* If D is a member of an explicitly instantiated class template,
24564 and no definition is available, treat it like an implicit
24565 instantiation. */
24566 if (!pattern_defined && expl_inst_class_mem_p
24567 && DECL_EXPLICIT_INSTANTIATION (d))
24568 {
24569 /* Leave linkage flags alone on instantiations with anonymous
24570 visibility. */
24571 if (TREE_PUBLIC (d))
24572 {
24573 DECL_NOT_REALLY_EXTERN (d) = 0;
24574 DECL_INTERFACE_KNOWN (d) = 0;
24575 }
24576 SET_DECL_IMPLICIT_INSTANTIATION (d);
24577 }
24578
24579 /* Defer all other templates, unless we have been explicitly
24580 forbidden from doing so. */
24581 if (/* If there is no definition, we cannot instantiate the
24582 template. */
24583 ! pattern_defined
24584 /* If it's OK to postpone instantiation, do so. */
24585 || defer_ok
24586 /* If this is a static data member that will be defined
24587 elsewhere, we don't want to instantiate the entire data
24588 member, but we do want to instantiate the initializer so that
24589 we can substitute that elsewhere. */
24590 || (external_p && VAR_P (d))
24591 /* Handle here a deleted function too, avoid generating
24592 its body (c++/61080). */
24593 || deleted_p)
24594 {
24595 /* The definition of the static data member is now required so
24596 we must substitute the initializer. */
24597 if (VAR_P (d)
24598 && !DECL_INITIAL (d)
24599 && DECL_INITIAL (code_pattern))
24600 {
24601 tree ns;
24602 tree init;
24603 bool const_init = false;
24604 bool enter_context = DECL_CLASS_SCOPE_P (d);
24605
24606 ns = decl_namespace_context (d);
24607 push_nested_namespace (ns);
24608 if (enter_context)
24609 push_nested_class (DECL_CONTEXT (d));
24610 init = tsubst_expr (DECL_INITIAL (code_pattern),
24611 args,
24612 tf_warning_or_error, NULL_TREE,
24613 /*integral_constant_expression_p=*/false);
24614 /* If instantiating the initializer involved instantiating this
24615 again, don't call cp_finish_decl twice. */
24616 if (!DECL_INITIAL (d))
24617 {
24618 /* Make sure the initializer is still constant, in case of
24619 circular dependency (template/instantiate6.C). */
24620 const_init
24621 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24622 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24623 /*asmspec_tree=*/NULL_TREE,
24624 LOOKUP_ONLYCONVERTING);
24625 }
24626 if (enter_context)
24627 pop_nested_class ();
24628 pop_nested_namespace (ns);
24629 }
24630
24631 /* We restore the source position here because it's used by
24632 add_pending_template. */
24633 input_location = saved_loc;
24634
24635 if (at_eof && !pattern_defined
24636 && DECL_EXPLICIT_INSTANTIATION (d)
24637 && DECL_NOT_REALLY_EXTERN (d))
24638 /* [temp.explicit]
24639
24640 The definition of a non-exported function template, a
24641 non-exported member function template, or a non-exported
24642 member function or static data member of a class template
24643 shall be present in every translation unit in which it is
24644 explicitly instantiated. */
24645 permerror (input_location, "explicit instantiation of %qD "
24646 "but no definition available", d);
24647
24648 /* If we're in unevaluated context, we just wanted to get the
24649 constant value; this isn't an odr use, so don't queue
24650 a full instantiation. */
24651 if (cp_unevaluated_operand != 0)
24652 goto out;
24653 /* ??? Historically, we have instantiated inline functions, even
24654 when marked as "extern template". */
24655 if (!(external_p && VAR_P (d)))
24656 add_pending_template (d);
24657 goto out;
24658 }
24659 /* Tell the repository that D is available in this translation unit
24660 -- and see if it is supposed to be instantiated here. */
24661 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24662 {
24663 /* In a PCH file, despite the fact that the repository hasn't
24664 requested instantiation in the PCH it is still possible that
24665 an instantiation will be required in a file that includes the
24666 PCH. */
24667 if (pch_file)
24668 add_pending_template (d);
24669 /* Instantiate inline functions so that the inliner can do its
24670 job, even though we'll not be emitting a copy of this
24671 function. */
24672 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24673 goto out;
24674 }
24675
24676 bool push_to_top, nested;
24677 tree fn_context;
24678 fn_context = decl_function_context (d);
24679 if (LAMBDA_FUNCTION_P (d))
24680 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24681 fn_context = NULL_TREE;
24682 nested = current_function_decl != NULL_TREE;
24683 push_to_top = !(nested && fn_context == current_function_decl);
24684
24685 vec<tree> omp_privatization_save;
24686 if (nested)
24687 save_omp_privatization_clauses (omp_privatization_save);
24688
24689 if (push_to_top)
24690 push_to_top_level ();
24691 else
24692 {
24693 gcc_assert (!processing_template_decl);
24694 push_function_context ();
24695 cp_unevaluated_operand = 0;
24696 c_inhibit_evaluation_warnings = 0;
24697 }
24698
24699 /* Mark D as instantiated so that recursive calls to
24700 instantiate_decl do not try to instantiate it again. */
24701 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24702
24703 /* Regenerate the declaration in case the template has been modified
24704 by a subsequent redeclaration. */
24705 regenerate_decl_from_template (d, td, args);
24706
24707 /* We already set the file and line above. Reset them now in case
24708 they changed as a result of calling regenerate_decl_from_template. */
24709 input_location = DECL_SOURCE_LOCATION (d);
24710
24711 if (VAR_P (d))
24712 {
24713 tree init;
24714 bool const_init = false;
24715
24716 /* Clear out DECL_RTL; whatever was there before may not be right
24717 since we've reset the type of the declaration. */
24718 SET_DECL_RTL (d, NULL);
24719 DECL_IN_AGGR_P (d) = 0;
24720
24721 /* The initializer is placed in DECL_INITIAL by
24722 regenerate_decl_from_template so we don't need to
24723 push/pop_access_scope again here. Pull it out so that
24724 cp_finish_decl can process it. */
24725 init = DECL_INITIAL (d);
24726 DECL_INITIAL (d) = NULL_TREE;
24727 DECL_INITIALIZED_P (d) = 0;
24728
24729 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24730 initializer. That function will defer actual emission until
24731 we have a chance to determine linkage. */
24732 DECL_EXTERNAL (d) = 0;
24733
24734 /* Enter the scope of D so that access-checking works correctly. */
24735 bool enter_context = DECL_CLASS_SCOPE_P (d);
24736 if (enter_context)
24737 push_nested_class (DECL_CONTEXT (d));
24738
24739 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24740 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24741
24742 if (enter_context)
24743 pop_nested_class ();
24744
24745 if (variable_template_p (gen_tmpl))
24746 note_variable_template_instantiation (d);
24747 }
24748 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24749 synthesize_method (d);
24750 else if (TREE_CODE (d) == FUNCTION_DECL)
24751 {
24752 /* Set up the list of local specializations. */
24753 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24754 tree block = NULL_TREE;
24755
24756 /* Set up context. */
24757 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24758 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24759 block = push_stmt_list ();
24760 else
24761 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24762
24763 /* Some typedefs referenced from within the template code need to be
24764 access checked at template instantiation time, i.e now. These
24765 types were added to the template at parsing time. Let's get those
24766 and perform the access checks then. */
24767 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24768 args);
24769
24770 /* Create substitution entries for the parameters. */
24771 register_parameter_specializations (code_pattern, d);
24772
24773 /* Substitute into the body of the function. */
24774 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24775 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24776 tf_warning_or_error, tmpl);
24777 else
24778 {
24779 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24780 tf_warning_or_error, tmpl,
24781 /*integral_constant_expression_p=*/false);
24782
24783 /* Set the current input_location to the end of the function
24784 so that finish_function knows where we are. */
24785 input_location
24786 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24787
24788 /* Remember if we saw an infinite loop in the template. */
24789 current_function_infinite_loop
24790 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24791 }
24792
24793 /* Finish the function. */
24794 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24795 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24796 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24797 else
24798 {
24799 d = finish_function (/*inline_p=*/false);
24800 expand_or_defer_fn (d);
24801 }
24802
24803 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24804 cp_check_omp_declare_reduction (d);
24805 }
24806
24807 /* We're not deferring instantiation any more. */
24808 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24809
24810 if (push_to_top)
24811 pop_from_top_level ();
24812 else
24813 pop_function_context ();
24814
24815 if (nested)
24816 restore_omp_privatization_clauses (omp_privatization_save);
24817
24818 out:
24819 pop_deferring_access_checks ();
24820 timevar_pop (TV_TEMPLATE_INST);
24821 pop_tinst_level ();
24822 input_location = saved_loc;
24823 cp_unevaluated_operand = saved_unevaluated_operand;
24824 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24825
24826 return d;
24827 }
24828
24829 /* Run through the list of templates that we wish we could
24830 instantiate, and instantiate any we can. RETRIES is the
24831 number of times we retry pending template instantiation. */
24832
24833 void
24834 instantiate_pending_templates (int retries)
24835 {
24836 int reconsider;
24837 location_t saved_loc = input_location;
24838
24839 /* Instantiating templates may trigger vtable generation. This in turn
24840 may require further template instantiations. We place a limit here
24841 to avoid infinite loop. */
24842 if (pending_templates && retries >= max_tinst_depth)
24843 {
24844 tree decl = pending_templates->tinst->maybe_get_node ();
24845
24846 fatal_error (input_location,
24847 "template instantiation depth exceeds maximum of %d"
24848 " instantiating %q+D, possibly from virtual table generation"
24849 " (use %<-ftemplate-depth=%> to increase the maximum)",
24850 max_tinst_depth, decl);
24851 if (TREE_CODE (decl) == FUNCTION_DECL)
24852 /* Pretend that we defined it. */
24853 DECL_INITIAL (decl) = error_mark_node;
24854 return;
24855 }
24856
24857 do
24858 {
24859 struct pending_template **t = &pending_templates;
24860 struct pending_template *last = NULL;
24861 reconsider = 0;
24862 while (*t)
24863 {
24864 tree instantiation = reopen_tinst_level ((*t)->tinst);
24865 bool complete = false;
24866
24867 if (TYPE_P (instantiation))
24868 {
24869 if (!COMPLETE_TYPE_P (instantiation))
24870 {
24871 instantiate_class_template (instantiation);
24872 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24873 for (tree fld = TYPE_FIELDS (instantiation);
24874 fld; fld = TREE_CHAIN (fld))
24875 if ((VAR_P (fld)
24876 || (TREE_CODE (fld) == FUNCTION_DECL
24877 && !DECL_ARTIFICIAL (fld)))
24878 && DECL_TEMPLATE_INSTANTIATION (fld))
24879 instantiate_decl (fld,
24880 /*defer_ok=*/false,
24881 /*expl_inst_class_mem_p=*/false);
24882
24883 if (COMPLETE_TYPE_P (instantiation))
24884 reconsider = 1;
24885 }
24886
24887 complete = COMPLETE_TYPE_P (instantiation);
24888 }
24889 else
24890 {
24891 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24892 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24893 {
24894 instantiation
24895 = instantiate_decl (instantiation,
24896 /*defer_ok=*/false,
24897 /*expl_inst_class_mem_p=*/false);
24898 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24899 reconsider = 1;
24900 }
24901
24902 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24903 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24904 }
24905
24906 if (complete)
24907 {
24908 /* If INSTANTIATION has been instantiated, then we don't
24909 need to consider it again in the future. */
24910 struct pending_template *drop = *t;
24911 *t = (*t)->next;
24912 set_refcount_ptr (drop->tinst);
24913 pending_template_freelist ().free (drop);
24914 }
24915 else
24916 {
24917 last = *t;
24918 t = &(*t)->next;
24919 }
24920 tinst_depth = 0;
24921 set_refcount_ptr (current_tinst_level);
24922 }
24923 last_pending_template = last;
24924 }
24925 while (reconsider);
24926
24927 input_location = saved_loc;
24928 }
24929
24930 /* Substitute ARGVEC into T, which is a list of initializers for
24931 either base class or a non-static data member. The TREE_PURPOSEs
24932 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24933 instantiate_decl. */
24934
24935 static tree
24936 tsubst_initializer_list (tree t, tree argvec)
24937 {
24938 tree inits = NULL_TREE;
24939 tree target_ctor = error_mark_node;
24940
24941 for (; t; t = TREE_CHAIN (t))
24942 {
24943 tree decl;
24944 tree init;
24945 tree expanded_bases = NULL_TREE;
24946 tree expanded_arguments = NULL_TREE;
24947 int i, len = 1;
24948
24949 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24950 {
24951 tree expr;
24952 tree arg;
24953
24954 /* Expand the base class expansion type into separate base
24955 classes. */
24956 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24957 tf_warning_or_error,
24958 NULL_TREE);
24959 if (expanded_bases == error_mark_node)
24960 continue;
24961
24962 /* We'll be building separate TREE_LISTs of arguments for
24963 each base. */
24964 len = TREE_VEC_LENGTH (expanded_bases);
24965 expanded_arguments = make_tree_vec (len);
24966 for (i = 0; i < len; i++)
24967 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24968
24969 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24970 expand each argument in the TREE_VALUE of t. */
24971 expr = make_node (EXPR_PACK_EXPANSION);
24972 PACK_EXPANSION_LOCAL_P (expr) = true;
24973 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24974 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24975
24976 if (TREE_VALUE (t) == void_type_node)
24977 /* VOID_TYPE_NODE is used to indicate
24978 value-initialization. */
24979 {
24980 for (i = 0; i < len; i++)
24981 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24982 }
24983 else
24984 {
24985 /* Substitute parameter packs into each argument in the
24986 TREE_LIST. */
24987 in_base_initializer = 1;
24988 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24989 {
24990 tree expanded_exprs;
24991
24992 /* Expand the argument. */
24993 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24994 expanded_exprs
24995 = tsubst_pack_expansion (expr, argvec,
24996 tf_warning_or_error,
24997 NULL_TREE);
24998 if (expanded_exprs == error_mark_node)
24999 continue;
25000
25001 /* Prepend each of the expanded expressions to the
25002 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25003 for (i = 0; i < len; i++)
25004 {
25005 TREE_VEC_ELT (expanded_arguments, i) =
25006 tree_cons (NULL_TREE,
25007 TREE_VEC_ELT (expanded_exprs, i),
25008 TREE_VEC_ELT (expanded_arguments, i));
25009 }
25010 }
25011 in_base_initializer = 0;
25012
25013 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25014 since we built them backwards. */
25015 for (i = 0; i < len; i++)
25016 {
25017 TREE_VEC_ELT (expanded_arguments, i) =
25018 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25019 }
25020 }
25021 }
25022
25023 for (i = 0; i < len; ++i)
25024 {
25025 if (expanded_bases)
25026 {
25027 decl = TREE_VEC_ELT (expanded_bases, i);
25028 decl = expand_member_init (decl);
25029 init = TREE_VEC_ELT (expanded_arguments, i);
25030 }
25031 else
25032 {
25033 tree tmp;
25034 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25035 tf_warning_or_error, NULL_TREE);
25036
25037 decl = expand_member_init (decl);
25038 if (decl && !DECL_P (decl))
25039 in_base_initializer = 1;
25040
25041 init = TREE_VALUE (t);
25042 tmp = init;
25043 if (init != void_type_node)
25044 init = tsubst_expr (init, argvec,
25045 tf_warning_or_error, NULL_TREE,
25046 /*integral_constant_expression_p=*/false);
25047 if (init == NULL_TREE && tmp != NULL_TREE)
25048 /* If we had an initializer but it instantiated to nothing,
25049 value-initialize the object. This will only occur when
25050 the initializer was a pack expansion where the parameter
25051 packs used in that expansion were of length zero. */
25052 init = void_type_node;
25053 in_base_initializer = 0;
25054 }
25055
25056 if (target_ctor != error_mark_node
25057 && init != error_mark_node)
25058 {
25059 error ("mem-initializer for %qD follows constructor delegation",
25060 decl);
25061 return inits;
25062 }
25063 /* Look for a target constructor. */
25064 if (init != error_mark_node
25065 && decl && CLASS_TYPE_P (decl)
25066 && same_type_p (decl, current_class_type))
25067 {
25068 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25069 if (inits)
25070 {
25071 error ("constructor delegation follows mem-initializer for %qD",
25072 TREE_PURPOSE (inits));
25073 continue;
25074 }
25075 target_ctor = init;
25076 }
25077
25078 if (decl)
25079 {
25080 init = build_tree_list (decl, init);
25081 TREE_CHAIN (init) = inits;
25082 inits = init;
25083 }
25084 }
25085 }
25086 return inits;
25087 }
25088
25089 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25090
25091 static void
25092 set_current_access_from_decl (tree decl)
25093 {
25094 if (TREE_PRIVATE (decl))
25095 current_access_specifier = access_private_node;
25096 else if (TREE_PROTECTED (decl))
25097 current_access_specifier = access_protected_node;
25098 else
25099 current_access_specifier = access_public_node;
25100 }
25101
25102 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25103 is the instantiation (which should have been created with
25104 start_enum) and ARGS are the template arguments to use. */
25105
25106 static void
25107 tsubst_enum (tree tag, tree newtag, tree args)
25108 {
25109 tree e;
25110
25111 if (SCOPED_ENUM_P (newtag))
25112 begin_scope (sk_scoped_enum, newtag);
25113
25114 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25115 {
25116 tree value;
25117 tree decl;
25118
25119 decl = TREE_VALUE (e);
25120 /* Note that in a template enum, the TREE_VALUE is the
25121 CONST_DECL, not the corresponding INTEGER_CST. */
25122 value = tsubst_expr (DECL_INITIAL (decl),
25123 args, tf_warning_or_error, NULL_TREE,
25124 /*integral_constant_expression_p=*/true);
25125
25126 /* Give this enumeration constant the correct access. */
25127 set_current_access_from_decl (decl);
25128
25129 /* Actually build the enumerator itself. Here we're assuming that
25130 enumerators can't have dependent attributes. */
25131 build_enumerator (DECL_NAME (decl), value, newtag,
25132 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25133 }
25134
25135 if (SCOPED_ENUM_P (newtag))
25136 finish_scope ();
25137
25138 finish_enum_value_list (newtag);
25139 finish_enum (newtag);
25140
25141 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25142 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25143 }
25144
25145 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25146 its type -- but without substituting the innermost set of template
25147 arguments. So, innermost set of template parameters will appear in
25148 the type. */
25149
25150 tree
25151 get_mostly_instantiated_function_type (tree decl)
25152 {
25153 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25154 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25155 }
25156
25157 /* Return truthvalue if we're processing a template different from
25158 the last one involved in diagnostics. */
25159 bool
25160 problematic_instantiation_changed (void)
25161 {
25162 return current_tinst_level != last_error_tinst_level;
25163 }
25164
25165 /* Remember current template involved in diagnostics. */
25166 void
25167 record_last_problematic_instantiation (void)
25168 {
25169 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25170 }
25171
25172 struct tinst_level *
25173 current_instantiation (void)
25174 {
25175 return current_tinst_level;
25176 }
25177
25178 /* Return TRUE if current_function_decl is being instantiated, false
25179 otherwise. */
25180
25181 bool
25182 instantiating_current_function_p (void)
25183 {
25184 return (current_instantiation ()
25185 && (current_instantiation ()->maybe_get_node ()
25186 == current_function_decl));
25187 }
25188
25189 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25190 type. Return false for ok, true for disallowed. Issue error and
25191 inform messages under control of COMPLAIN. */
25192
25193 static bool
25194 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25195 {
25196 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25197 return false;
25198 else if (TYPE_PTR_P (type))
25199 return false;
25200 else if (TYPE_REF_P (type)
25201 && !TYPE_REF_IS_RVALUE (type))
25202 return false;
25203 else if (TYPE_PTRMEM_P (type))
25204 return false;
25205 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25206 return false;
25207 else if (TREE_CODE (type) == TYPENAME_TYPE)
25208 return false;
25209 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25210 return false;
25211 else if (TREE_CODE (type) == NULLPTR_TYPE)
25212 return false;
25213 /* A bound template template parm could later be instantiated to have a valid
25214 nontype parm type via an alias template. */
25215 else if (cxx_dialect >= cxx11
25216 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25217 return false;
25218 else if (CLASS_TYPE_P (type))
25219 {
25220 if (cxx_dialect < cxx2a)
25221 {
25222 error ("non-type template parameters of class type only available "
25223 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25224 return true;
25225 }
25226 if (!complete_type_or_else (type, NULL_TREE))
25227 return true;
25228 if (!literal_type_p (type))
25229 {
25230 error ("%qT is not a valid type for a template non-type parameter "
25231 "because it is not literal", type);
25232 explain_non_literal_class (type);
25233 return true;
25234 }
25235 if (cp_has_mutable_p (type))
25236 {
25237 error ("%qT is not a valid type for a template non-type parameter "
25238 "because it has a mutable member", type);
25239 return true;
25240 }
25241 /* FIXME check op<=> and strong structural equality once spaceship is
25242 implemented. */
25243 return false;
25244 }
25245
25246 if (complain & tf_error)
25247 {
25248 if (type == error_mark_node)
25249 inform (input_location, "invalid template non-type parameter");
25250 else
25251 error ("%q#T is not a valid type for a template non-type parameter",
25252 type);
25253 }
25254 return true;
25255 }
25256
25257 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25258 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25259
25260 static bool
25261 dependent_type_p_r (tree type)
25262 {
25263 tree scope;
25264
25265 /* [temp.dep.type]
25266
25267 A type is dependent if it is:
25268
25269 -- a template parameter. Template template parameters are types
25270 for us (since TYPE_P holds true for them) so we handle
25271 them here. */
25272 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25273 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25274 return true;
25275 /* -- a qualified-id with a nested-name-specifier which contains a
25276 class-name that names a dependent type or whose unqualified-id
25277 names a dependent type. */
25278 if (TREE_CODE (type) == TYPENAME_TYPE)
25279 return true;
25280
25281 /* An alias template specialization can be dependent even if the
25282 resulting type is not. */
25283 if (dependent_alias_template_spec_p (type))
25284 return true;
25285
25286 /* -- a cv-qualified type where the cv-unqualified type is
25287 dependent.
25288 No code is necessary for this bullet; the code below handles
25289 cv-qualified types, and we don't want to strip aliases with
25290 TYPE_MAIN_VARIANT because of DR 1558. */
25291 /* -- a compound type constructed from any dependent type. */
25292 if (TYPE_PTRMEM_P (type))
25293 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25294 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25295 (type)));
25296 else if (INDIRECT_TYPE_P (type))
25297 return dependent_type_p (TREE_TYPE (type));
25298 else if (FUNC_OR_METHOD_TYPE_P (type))
25299 {
25300 tree arg_type;
25301
25302 if (dependent_type_p (TREE_TYPE (type)))
25303 return true;
25304 for (arg_type = TYPE_ARG_TYPES (type);
25305 arg_type;
25306 arg_type = TREE_CHAIN (arg_type))
25307 if (dependent_type_p (TREE_VALUE (arg_type)))
25308 return true;
25309 if (cxx_dialect >= cxx17)
25310 /* A value-dependent noexcept-specifier makes the type dependent. */
25311 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25312 if (tree noex = TREE_PURPOSE (spec))
25313 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25314 affect overload resolution and treating it as dependent breaks
25315 things. Same for an unparsed noexcept expression. */
25316 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25317 && TREE_CODE (noex) != DEFERRED_PARSE
25318 && value_dependent_expression_p (noex))
25319 return true;
25320 return false;
25321 }
25322 /* -- an array type constructed from any dependent type or whose
25323 size is specified by a constant expression that is
25324 value-dependent.
25325
25326 We checked for type- and value-dependence of the bounds in
25327 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25328 if (TREE_CODE (type) == ARRAY_TYPE)
25329 {
25330 if (TYPE_DOMAIN (type)
25331 && dependent_type_p (TYPE_DOMAIN (type)))
25332 return true;
25333 return dependent_type_p (TREE_TYPE (type));
25334 }
25335
25336 /* -- a template-id in which either the template name is a template
25337 parameter ... */
25338 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25339 return true;
25340 /* ... or any of the template arguments is a dependent type or
25341 an expression that is type-dependent or value-dependent. */
25342 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25343 && (any_dependent_template_arguments_p
25344 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25345 return true;
25346
25347 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25348 dependent; if the argument of the `typeof' expression is not
25349 type-dependent, then it should already been have resolved. */
25350 if (TREE_CODE (type) == TYPEOF_TYPE
25351 || TREE_CODE (type) == DECLTYPE_TYPE
25352 || TREE_CODE (type) == UNDERLYING_TYPE)
25353 return true;
25354
25355 /* A template argument pack is dependent if any of its packed
25356 arguments are. */
25357 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25358 {
25359 tree args = ARGUMENT_PACK_ARGS (type);
25360 int i, len = TREE_VEC_LENGTH (args);
25361 for (i = 0; i < len; ++i)
25362 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25363 return true;
25364 }
25365
25366 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25367 be template parameters. */
25368 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25369 return true;
25370
25371 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25372 return true;
25373
25374 /* The standard does not specifically mention types that are local
25375 to template functions or local classes, but they should be
25376 considered dependent too. For example:
25377
25378 template <int I> void f() {
25379 enum E { a = I };
25380 S<sizeof (E)> s;
25381 }
25382
25383 The size of `E' cannot be known until the value of `I' has been
25384 determined. Therefore, `E' must be considered dependent. */
25385 scope = TYPE_CONTEXT (type);
25386 if (scope && TYPE_P (scope))
25387 return dependent_type_p (scope);
25388 /* Don't use type_dependent_expression_p here, as it can lead
25389 to infinite recursion trying to determine whether a lambda
25390 nested in a lambda is dependent (c++/47687). */
25391 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25392 && DECL_LANG_SPECIFIC (scope)
25393 && DECL_TEMPLATE_INFO (scope)
25394 && (any_dependent_template_arguments_p
25395 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25396 return true;
25397
25398 /* Other types are non-dependent. */
25399 return false;
25400 }
25401
25402 /* Returns TRUE if TYPE is dependent, in the sense of
25403 [temp.dep.type]. Note that a NULL type is considered dependent. */
25404
25405 bool
25406 dependent_type_p (tree type)
25407 {
25408 /* If there are no template parameters in scope, then there can't be
25409 any dependent types. */
25410 if (!processing_template_decl)
25411 {
25412 /* If we are not processing a template, then nobody should be
25413 providing us with a dependent type. */
25414 gcc_assert (type);
25415 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25416 return false;
25417 }
25418
25419 /* If the type is NULL, we have not computed a type for the entity
25420 in question; in that case, the type is dependent. */
25421 if (!type)
25422 return true;
25423
25424 /* Erroneous types can be considered non-dependent. */
25425 if (type == error_mark_node)
25426 return false;
25427
25428 /* Getting here with global_type_node means we improperly called this
25429 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25430 gcc_checking_assert (type != global_type_node);
25431
25432 /* If we have not already computed the appropriate value for TYPE,
25433 do so now. */
25434 if (!TYPE_DEPENDENT_P_VALID (type))
25435 {
25436 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25437 TYPE_DEPENDENT_P_VALID (type) = 1;
25438 }
25439
25440 return TYPE_DEPENDENT_P (type);
25441 }
25442
25443 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25444 lookup. In other words, a dependent type that is not the current
25445 instantiation. */
25446
25447 bool
25448 dependent_scope_p (tree scope)
25449 {
25450 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25451 && !currently_open_class (scope));
25452 }
25453
25454 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25455 an unknown base of 'this' (and is therefore instantiation-dependent). */
25456
25457 static bool
25458 unknown_base_ref_p (tree t)
25459 {
25460 if (!current_class_ptr)
25461 return false;
25462
25463 tree mem = TREE_OPERAND (t, 1);
25464 if (shared_member_p (mem))
25465 return false;
25466
25467 tree cur = current_nonlambda_class_type ();
25468 if (!any_dependent_bases_p (cur))
25469 return false;
25470
25471 tree ctx = TREE_OPERAND (t, 0);
25472 if (DERIVED_FROM_P (ctx, cur))
25473 return false;
25474
25475 return true;
25476 }
25477
25478 /* T is a SCOPE_REF; return whether we need to consider it
25479 instantiation-dependent so that we can check access at instantiation
25480 time even though we know which member it resolves to. */
25481
25482 static bool
25483 instantiation_dependent_scope_ref_p (tree t)
25484 {
25485 if (DECL_P (TREE_OPERAND (t, 1))
25486 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25487 && !unknown_base_ref_p (t)
25488 && accessible_in_template_p (TREE_OPERAND (t, 0),
25489 TREE_OPERAND (t, 1)))
25490 return false;
25491 else
25492 return true;
25493 }
25494
25495 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25496 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25497 expression. */
25498
25499 /* Note that this predicate is not appropriate for general expressions;
25500 only constant expressions (that satisfy potential_constant_expression)
25501 can be tested for value dependence. */
25502
25503 bool
25504 value_dependent_expression_p (tree expression)
25505 {
25506 if (!processing_template_decl || expression == NULL_TREE)
25507 return false;
25508
25509 /* A type-dependent expression is also value-dependent. */
25510 if (type_dependent_expression_p (expression))
25511 return true;
25512
25513 switch (TREE_CODE (expression))
25514 {
25515 case BASELINK:
25516 /* A dependent member function of the current instantiation. */
25517 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25518
25519 case FUNCTION_DECL:
25520 /* A dependent member function of the current instantiation. */
25521 if (DECL_CLASS_SCOPE_P (expression)
25522 && dependent_type_p (DECL_CONTEXT (expression)))
25523 return true;
25524 break;
25525
25526 case IDENTIFIER_NODE:
25527 /* A name that has not been looked up -- must be dependent. */
25528 return true;
25529
25530 case TEMPLATE_PARM_INDEX:
25531 /* A non-type template parm. */
25532 return true;
25533
25534 case CONST_DECL:
25535 /* A non-type template parm. */
25536 if (DECL_TEMPLATE_PARM_P (expression))
25537 return true;
25538 return value_dependent_expression_p (DECL_INITIAL (expression));
25539
25540 case VAR_DECL:
25541 /* A constant with literal type and is initialized
25542 with an expression that is value-dependent. */
25543 if (DECL_DEPENDENT_INIT_P (expression)
25544 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25545 || TYPE_REF_P (TREE_TYPE (expression)))
25546 return true;
25547 if (DECL_HAS_VALUE_EXPR_P (expression))
25548 {
25549 tree value_expr = DECL_VALUE_EXPR (expression);
25550 if (value_dependent_expression_p (value_expr))
25551 return true;
25552 }
25553 return false;
25554
25555 case DYNAMIC_CAST_EXPR:
25556 case STATIC_CAST_EXPR:
25557 case CONST_CAST_EXPR:
25558 case REINTERPRET_CAST_EXPR:
25559 case CAST_EXPR:
25560 case IMPLICIT_CONV_EXPR:
25561 /* These expressions are value-dependent if the type to which
25562 the cast occurs is dependent or the expression being casted
25563 is value-dependent. */
25564 {
25565 tree type = TREE_TYPE (expression);
25566
25567 if (dependent_type_p (type))
25568 return true;
25569
25570 /* A functional cast has a list of operands. */
25571 expression = TREE_OPERAND (expression, 0);
25572 if (!expression)
25573 {
25574 /* If there are no operands, it must be an expression such
25575 as "int()". This should not happen for aggregate types
25576 because it would form non-constant expressions. */
25577 gcc_assert (cxx_dialect >= cxx11
25578 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25579
25580 return false;
25581 }
25582
25583 if (TREE_CODE (expression) == TREE_LIST)
25584 return any_value_dependent_elements_p (expression);
25585
25586 return value_dependent_expression_p (expression);
25587 }
25588
25589 case SIZEOF_EXPR:
25590 if (SIZEOF_EXPR_TYPE_P (expression))
25591 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25592 /* FALLTHRU */
25593 case ALIGNOF_EXPR:
25594 case TYPEID_EXPR:
25595 /* A `sizeof' expression is value-dependent if the operand is
25596 type-dependent or is a pack expansion. */
25597 expression = TREE_OPERAND (expression, 0);
25598 if (PACK_EXPANSION_P (expression))
25599 return true;
25600 else if (TYPE_P (expression))
25601 return dependent_type_p (expression);
25602 return instantiation_dependent_uneval_expression_p (expression);
25603
25604 case AT_ENCODE_EXPR:
25605 /* An 'encode' expression is value-dependent if the operand is
25606 type-dependent. */
25607 expression = TREE_OPERAND (expression, 0);
25608 return dependent_type_p (expression);
25609
25610 case NOEXCEPT_EXPR:
25611 expression = TREE_OPERAND (expression, 0);
25612 return instantiation_dependent_uneval_expression_p (expression);
25613
25614 case SCOPE_REF:
25615 /* All instantiation-dependent expressions should also be considered
25616 value-dependent. */
25617 return instantiation_dependent_scope_ref_p (expression);
25618
25619 case COMPONENT_REF:
25620 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25621 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25622
25623 case NONTYPE_ARGUMENT_PACK:
25624 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25625 is value-dependent. */
25626 {
25627 tree values = ARGUMENT_PACK_ARGS (expression);
25628 int i, len = TREE_VEC_LENGTH (values);
25629
25630 for (i = 0; i < len; ++i)
25631 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25632 return true;
25633
25634 return false;
25635 }
25636
25637 case TRAIT_EXPR:
25638 {
25639 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25640
25641 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25642 return true;
25643
25644 if (!type2)
25645 return false;
25646
25647 if (TREE_CODE (type2) != TREE_LIST)
25648 return dependent_type_p (type2);
25649
25650 for (; type2; type2 = TREE_CHAIN (type2))
25651 if (dependent_type_p (TREE_VALUE (type2)))
25652 return true;
25653
25654 return false;
25655 }
25656
25657 case MODOP_EXPR:
25658 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25659 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25660
25661 case ARRAY_REF:
25662 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25663 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25664
25665 case ADDR_EXPR:
25666 {
25667 tree op = TREE_OPERAND (expression, 0);
25668 return (value_dependent_expression_p (op)
25669 || has_value_dependent_address (op));
25670 }
25671
25672 case REQUIRES_EXPR:
25673 /* Treat all requires-expressions as value-dependent so
25674 we don't try to fold them. */
25675 return true;
25676
25677 case TYPE_REQ:
25678 return dependent_type_p (TREE_OPERAND (expression, 0));
25679
25680 case CALL_EXPR:
25681 {
25682 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25683 return true;
25684 tree fn = get_callee_fndecl (expression);
25685 int i, nargs;
25686 nargs = call_expr_nargs (expression);
25687 for (i = 0; i < nargs; ++i)
25688 {
25689 tree op = CALL_EXPR_ARG (expression, i);
25690 /* In a call to a constexpr member function, look through the
25691 implicit ADDR_EXPR on the object argument so that it doesn't
25692 cause the call to be considered value-dependent. We also
25693 look through it in potential_constant_expression. */
25694 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25695 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25696 && TREE_CODE (op) == ADDR_EXPR)
25697 op = TREE_OPERAND (op, 0);
25698 if (value_dependent_expression_p (op))
25699 return true;
25700 }
25701 return false;
25702 }
25703
25704 case TEMPLATE_ID_EXPR:
25705 return variable_concept_p (TREE_OPERAND (expression, 0));
25706
25707 case CONSTRUCTOR:
25708 {
25709 unsigned ix;
25710 tree val;
25711 if (dependent_type_p (TREE_TYPE (expression)))
25712 return true;
25713 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25714 if (value_dependent_expression_p (val))
25715 return true;
25716 return false;
25717 }
25718
25719 case STMT_EXPR:
25720 /* Treat a GNU statement expression as dependent to avoid crashing
25721 under instantiate_non_dependent_expr; it can't be constant. */
25722 return true;
25723
25724 default:
25725 /* A constant expression is value-dependent if any subexpression is
25726 value-dependent. */
25727 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25728 {
25729 case tcc_reference:
25730 case tcc_unary:
25731 case tcc_comparison:
25732 case tcc_binary:
25733 case tcc_expression:
25734 case tcc_vl_exp:
25735 {
25736 int i, len = cp_tree_operand_length (expression);
25737
25738 for (i = 0; i < len; i++)
25739 {
25740 tree t = TREE_OPERAND (expression, i);
25741
25742 /* In some cases, some of the operands may be missing.
25743 (For example, in the case of PREDECREMENT_EXPR, the
25744 amount to increment by may be missing.) That doesn't
25745 make the expression dependent. */
25746 if (t && value_dependent_expression_p (t))
25747 return true;
25748 }
25749 }
25750 break;
25751 default:
25752 break;
25753 }
25754 break;
25755 }
25756
25757 /* The expression is not value-dependent. */
25758 return false;
25759 }
25760
25761 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25762 [temp.dep.expr]. Note that an expression with no type is
25763 considered dependent. Other parts of the compiler arrange for an
25764 expression with type-dependent subexpressions to have no type, so
25765 this function doesn't have to be fully recursive. */
25766
25767 bool
25768 type_dependent_expression_p (tree expression)
25769 {
25770 if (!processing_template_decl)
25771 return false;
25772
25773 if (expression == NULL_TREE || expression == error_mark_node)
25774 return false;
25775
25776 STRIP_ANY_LOCATION_WRAPPER (expression);
25777
25778 /* An unresolved name is always dependent. */
25779 if (identifier_p (expression)
25780 || TREE_CODE (expression) == USING_DECL
25781 || TREE_CODE (expression) == WILDCARD_DECL)
25782 return true;
25783
25784 /* A lambda-expression in template context is dependent. dependent_type_p is
25785 true for a lambda in the scope of a class or function template, but that
25786 doesn't cover all template contexts, like a default template argument. */
25787 if (TREE_CODE (expression) == LAMBDA_EXPR)
25788 return true;
25789
25790 /* A fold expression is type-dependent. */
25791 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25792 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25793 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25794 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25795 return true;
25796
25797 /* Some expression forms are never type-dependent. */
25798 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25799 || TREE_CODE (expression) == SIZEOF_EXPR
25800 || TREE_CODE (expression) == ALIGNOF_EXPR
25801 || TREE_CODE (expression) == AT_ENCODE_EXPR
25802 || TREE_CODE (expression) == NOEXCEPT_EXPR
25803 || TREE_CODE (expression) == TRAIT_EXPR
25804 || TREE_CODE (expression) == TYPEID_EXPR
25805 || TREE_CODE (expression) == DELETE_EXPR
25806 || TREE_CODE (expression) == VEC_DELETE_EXPR
25807 || TREE_CODE (expression) == THROW_EXPR
25808 || TREE_CODE (expression) == REQUIRES_EXPR)
25809 return false;
25810
25811 /* The types of these expressions depends only on the type to which
25812 the cast occurs. */
25813 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25814 || TREE_CODE (expression) == STATIC_CAST_EXPR
25815 || TREE_CODE (expression) == CONST_CAST_EXPR
25816 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25817 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25818 || TREE_CODE (expression) == CAST_EXPR)
25819 return dependent_type_p (TREE_TYPE (expression));
25820
25821 /* The types of these expressions depends only on the type created
25822 by the expression. */
25823 if (TREE_CODE (expression) == NEW_EXPR
25824 || TREE_CODE (expression) == VEC_NEW_EXPR)
25825 {
25826 /* For NEW_EXPR tree nodes created inside a template, either
25827 the object type itself or a TREE_LIST may appear as the
25828 operand 1. */
25829 tree type = TREE_OPERAND (expression, 1);
25830 if (TREE_CODE (type) == TREE_LIST)
25831 /* This is an array type. We need to check array dimensions
25832 as well. */
25833 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25834 || value_dependent_expression_p
25835 (TREE_OPERAND (TREE_VALUE (type), 1));
25836 else
25837 return dependent_type_p (type);
25838 }
25839
25840 if (TREE_CODE (expression) == SCOPE_REF)
25841 {
25842 tree scope = TREE_OPERAND (expression, 0);
25843 tree name = TREE_OPERAND (expression, 1);
25844
25845 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25846 contains an identifier associated by name lookup with one or more
25847 declarations declared with a dependent type, or...a
25848 nested-name-specifier or qualified-id that names a member of an
25849 unknown specialization. */
25850 return (type_dependent_expression_p (name)
25851 || dependent_scope_p (scope));
25852 }
25853
25854 if (TREE_CODE (expression) == TEMPLATE_DECL
25855 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25856 return uses_outer_template_parms (expression);
25857
25858 if (TREE_CODE (expression) == STMT_EXPR)
25859 expression = stmt_expr_value_expr (expression);
25860
25861 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25862 {
25863 tree elt;
25864 unsigned i;
25865
25866 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25867 {
25868 if (type_dependent_expression_p (elt))
25869 return true;
25870 }
25871 return false;
25872 }
25873
25874 /* A static data member of the current instantiation with incomplete
25875 array type is type-dependent, as the definition and specializations
25876 can have different bounds. */
25877 if (VAR_P (expression)
25878 && DECL_CLASS_SCOPE_P (expression)
25879 && dependent_type_p (DECL_CONTEXT (expression))
25880 && VAR_HAD_UNKNOWN_BOUND (expression))
25881 return true;
25882
25883 /* An array of unknown bound depending on a variadic parameter, eg:
25884
25885 template<typename... Args>
25886 void foo (Args... args)
25887 {
25888 int arr[] = { args... };
25889 }
25890
25891 template<int... vals>
25892 void bar ()
25893 {
25894 int arr[] = { vals... };
25895 }
25896
25897 If the array has no length and has an initializer, it must be that
25898 we couldn't determine its length in cp_complete_array_type because
25899 it is dependent. */
25900 if (VAR_P (expression)
25901 && TREE_TYPE (expression) != NULL_TREE
25902 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25903 && !TYPE_DOMAIN (TREE_TYPE (expression))
25904 && DECL_INITIAL (expression))
25905 return true;
25906
25907 /* A function or variable template-id is type-dependent if it has any
25908 dependent template arguments. */
25909 if (VAR_OR_FUNCTION_DECL_P (expression)
25910 && DECL_LANG_SPECIFIC (expression)
25911 && DECL_TEMPLATE_INFO (expression))
25912 {
25913 /* Consider the innermost template arguments, since those are the ones
25914 that come from the template-id; the template arguments for the
25915 enclosing class do not make it type-dependent unless they are used in
25916 the type of the decl. */
25917 if (instantiates_primary_template_p (expression)
25918 && (any_dependent_template_arguments_p
25919 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25920 return true;
25921 }
25922
25923 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25924 type-dependent. Checking this is important for functions with auto return
25925 type, which looks like a dependent type. */
25926 if (TREE_CODE (expression) == FUNCTION_DECL
25927 && !(DECL_CLASS_SCOPE_P (expression)
25928 && dependent_type_p (DECL_CONTEXT (expression)))
25929 && !(DECL_LANG_SPECIFIC (expression)
25930 && DECL_FRIEND_P (expression)
25931 && (!DECL_FRIEND_CONTEXT (expression)
25932 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25933 && !DECL_LOCAL_FUNCTION_P (expression))
25934 {
25935 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25936 || undeduced_auto_decl (expression));
25937 return false;
25938 }
25939
25940 /* Always dependent, on the number of arguments if nothing else. */
25941 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25942 return true;
25943
25944 if (TREE_TYPE (expression) == unknown_type_node)
25945 {
25946 if (TREE_CODE (expression) == ADDR_EXPR)
25947 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25948 if (TREE_CODE (expression) == COMPONENT_REF
25949 || TREE_CODE (expression) == OFFSET_REF)
25950 {
25951 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25952 return true;
25953 expression = TREE_OPERAND (expression, 1);
25954 if (identifier_p (expression))
25955 return false;
25956 }
25957 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25958 if (TREE_CODE (expression) == SCOPE_REF)
25959 return false;
25960
25961 if (BASELINK_P (expression))
25962 {
25963 if (BASELINK_OPTYPE (expression)
25964 && dependent_type_p (BASELINK_OPTYPE (expression)))
25965 return true;
25966 expression = BASELINK_FUNCTIONS (expression);
25967 }
25968
25969 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25970 {
25971 if (any_dependent_template_arguments_p
25972 (TREE_OPERAND (expression, 1)))
25973 return true;
25974 expression = TREE_OPERAND (expression, 0);
25975 if (identifier_p (expression))
25976 return true;
25977 }
25978
25979 gcc_assert (OVL_P (expression));
25980
25981 for (lkp_iterator iter (expression); iter; ++iter)
25982 if (type_dependent_expression_p (*iter))
25983 return true;
25984
25985 return false;
25986 }
25987
25988 /* The type of a non-type template parm declared with a placeholder type
25989 depends on the corresponding template argument, even though
25990 placeholders are not normally considered dependent. */
25991 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
25992 && is_auto (TREE_TYPE (expression)))
25993 return true;
25994
25995 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25996
25997 /* Dependent type attributes might not have made it from the decl to
25998 the type yet. */
25999 if (DECL_P (expression)
26000 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26001 return true;
26002
26003 return (dependent_type_p (TREE_TYPE (expression)));
26004 }
26005
26006 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26007 type-dependent if the expression refers to a member of the current
26008 instantiation and the type of the referenced member is dependent, or the
26009 class member access expression refers to a member of an unknown
26010 specialization.
26011
26012 This function returns true if the OBJECT in such a class member access
26013 expression is of an unknown specialization. */
26014
26015 bool
26016 type_dependent_object_expression_p (tree object)
26017 {
26018 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26019 dependent. */
26020 if (TREE_CODE (object) == IDENTIFIER_NODE)
26021 return true;
26022 tree scope = TREE_TYPE (object);
26023 return (!scope || dependent_scope_p (scope));
26024 }
26025
26026 /* walk_tree callback function for instantiation_dependent_expression_p,
26027 below. Returns non-zero if a dependent subexpression is found. */
26028
26029 static tree
26030 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26031 void * /*data*/)
26032 {
26033 if (TYPE_P (*tp))
26034 {
26035 /* We don't have to worry about decltype currently because decltype
26036 of an instantiation-dependent expr is a dependent type. This
26037 might change depending on the resolution of DR 1172. */
26038 *walk_subtrees = false;
26039 return NULL_TREE;
26040 }
26041 enum tree_code code = TREE_CODE (*tp);
26042 switch (code)
26043 {
26044 /* Don't treat an argument list as dependent just because it has no
26045 TREE_TYPE. */
26046 case TREE_LIST:
26047 case TREE_VEC:
26048 case NONTYPE_ARGUMENT_PACK:
26049 return NULL_TREE;
26050
26051 case TEMPLATE_PARM_INDEX:
26052 if (dependent_type_p (TREE_TYPE (*tp)))
26053 return *tp;
26054 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26055 return *tp;
26056 /* We'll check value-dependence separately. */
26057 return NULL_TREE;
26058
26059 /* Handle expressions with type operands. */
26060 case SIZEOF_EXPR:
26061 case ALIGNOF_EXPR:
26062 case TYPEID_EXPR:
26063 case AT_ENCODE_EXPR:
26064 {
26065 tree op = TREE_OPERAND (*tp, 0);
26066 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26067 op = TREE_TYPE (op);
26068 if (TYPE_P (op))
26069 {
26070 if (dependent_type_p (op))
26071 return *tp;
26072 else
26073 {
26074 *walk_subtrees = false;
26075 return NULL_TREE;
26076 }
26077 }
26078 break;
26079 }
26080
26081 case COMPONENT_REF:
26082 if (identifier_p (TREE_OPERAND (*tp, 1)))
26083 /* In a template, finish_class_member_access_expr creates a
26084 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26085 type-dependent, so that we can check access control at
26086 instantiation time (PR 42277). See also Core issue 1273. */
26087 return *tp;
26088 break;
26089
26090 case SCOPE_REF:
26091 if (instantiation_dependent_scope_ref_p (*tp))
26092 return *tp;
26093 else
26094 break;
26095
26096 /* Treat statement-expressions as dependent. */
26097 case BIND_EXPR:
26098 return *tp;
26099
26100 /* Treat requires-expressions as dependent. */
26101 case REQUIRES_EXPR:
26102 return *tp;
26103
26104 case CALL_EXPR:
26105 /* Treat calls to function concepts as dependent. */
26106 if (function_concept_check_p (*tp))
26107 return *tp;
26108 break;
26109
26110 case TEMPLATE_ID_EXPR:
26111 /* And variable concepts. */
26112 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26113 return *tp;
26114 break;
26115
26116 case CONSTRUCTOR:
26117 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26118 return *tp;
26119 break;
26120
26121 default:
26122 break;
26123 }
26124
26125 if (type_dependent_expression_p (*tp))
26126 return *tp;
26127 else
26128 return NULL_TREE;
26129 }
26130
26131 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26132 sense defined by the ABI:
26133
26134 "An expression is instantiation-dependent if it is type-dependent
26135 or value-dependent, or it has a subexpression that is type-dependent
26136 or value-dependent."
26137
26138 Except don't actually check value-dependence for unevaluated expressions,
26139 because in sizeof(i) we don't care about the value of i. Checking
26140 type-dependence will in turn check value-dependence of array bounds/template
26141 arguments as needed. */
26142
26143 bool
26144 instantiation_dependent_uneval_expression_p (tree expression)
26145 {
26146 tree result;
26147
26148 if (!processing_template_decl)
26149 return false;
26150
26151 if (expression == error_mark_node)
26152 return false;
26153
26154 result = cp_walk_tree_without_duplicates (&expression,
26155 instantiation_dependent_r, NULL);
26156 return result != NULL_TREE;
26157 }
26158
26159 /* As above, but also check value-dependence of the expression as a whole. */
26160
26161 bool
26162 instantiation_dependent_expression_p (tree expression)
26163 {
26164 return (instantiation_dependent_uneval_expression_p (expression)
26165 || value_dependent_expression_p (expression));
26166 }
26167
26168 /* Like type_dependent_expression_p, but it also works while not processing
26169 a template definition, i.e. during substitution or mangling. */
26170
26171 bool
26172 type_dependent_expression_p_push (tree expr)
26173 {
26174 bool b;
26175 ++processing_template_decl;
26176 b = type_dependent_expression_p (expr);
26177 --processing_template_decl;
26178 return b;
26179 }
26180
26181 /* Returns TRUE if ARGS contains a type-dependent expression. */
26182
26183 bool
26184 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26185 {
26186 unsigned int i;
26187 tree arg;
26188
26189 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26190 {
26191 if (type_dependent_expression_p (arg))
26192 return true;
26193 }
26194 return false;
26195 }
26196
26197 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26198 expressions) contains any type-dependent expressions. */
26199
26200 bool
26201 any_type_dependent_elements_p (const_tree list)
26202 {
26203 for (; list; list = TREE_CHAIN (list))
26204 if (type_dependent_expression_p (TREE_VALUE (list)))
26205 return true;
26206
26207 return false;
26208 }
26209
26210 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26211 expressions) contains any value-dependent expressions. */
26212
26213 bool
26214 any_value_dependent_elements_p (const_tree list)
26215 {
26216 for (; list; list = TREE_CHAIN (list))
26217 if (value_dependent_expression_p (TREE_VALUE (list)))
26218 return true;
26219
26220 return false;
26221 }
26222
26223 /* Returns TRUE if the ARG (a template argument) is dependent. */
26224
26225 bool
26226 dependent_template_arg_p (tree arg)
26227 {
26228 if (!processing_template_decl)
26229 return false;
26230
26231 /* Assume a template argument that was wrongly written by the user
26232 is dependent. This is consistent with what
26233 any_dependent_template_arguments_p [that calls this function]
26234 does. */
26235 if (!arg || arg == error_mark_node)
26236 return true;
26237
26238 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26239 arg = argument_pack_select_arg (arg);
26240
26241 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26242 return true;
26243 if (TREE_CODE (arg) == TEMPLATE_DECL)
26244 {
26245 if (DECL_TEMPLATE_PARM_P (arg))
26246 return true;
26247 /* A member template of a dependent class is not necessarily
26248 type-dependent, but it is a dependent template argument because it
26249 will be a member of an unknown specialization to that template. */
26250 tree scope = CP_DECL_CONTEXT (arg);
26251 return TYPE_P (scope) && dependent_type_p (scope);
26252 }
26253 else if (ARGUMENT_PACK_P (arg))
26254 {
26255 tree args = ARGUMENT_PACK_ARGS (arg);
26256 int i, len = TREE_VEC_LENGTH (args);
26257 for (i = 0; i < len; ++i)
26258 {
26259 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26260 return true;
26261 }
26262
26263 return false;
26264 }
26265 else if (TYPE_P (arg))
26266 return dependent_type_p (arg);
26267 else
26268 return (type_dependent_expression_p (arg)
26269 || value_dependent_expression_p (arg));
26270 }
26271
26272 /* Returns true if ARGS (a collection of template arguments) contains
26273 any types that require structural equality testing. */
26274
26275 bool
26276 any_template_arguments_need_structural_equality_p (tree args)
26277 {
26278 int i;
26279 int j;
26280
26281 if (!args)
26282 return false;
26283 if (args == error_mark_node)
26284 return true;
26285
26286 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26287 {
26288 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26289 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26290 {
26291 tree arg = TREE_VEC_ELT (level, j);
26292 tree packed_args = NULL_TREE;
26293 int k, len = 1;
26294
26295 if (ARGUMENT_PACK_P (arg))
26296 {
26297 /* Look inside the argument pack. */
26298 packed_args = ARGUMENT_PACK_ARGS (arg);
26299 len = TREE_VEC_LENGTH (packed_args);
26300 }
26301
26302 for (k = 0; k < len; ++k)
26303 {
26304 if (packed_args)
26305 arg = TREE_VEC_ELT (packed_args, k);
26306
26307 if (error_operand_p (arg))
26308 return true;
26309 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26310 continue;
26311 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26312 return true;
26313 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26314 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26315 return true;
26316 }
26317 }
26318 }
26319
26320 return false;
26321 }
26322
26323 /* Returns true if ARGS (a collection of template arguments) contains
26324 any dependent arguments. */
26325
26326 bool
26327 any_dependent_template_arguments_p (const_tree args)
26328 {
26329 int i;
26330 int j;
26331
26332 if (!args)
26333 return false;
26334 if (args == error_mark_node)
26335 return true;
26336
26337 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26338 {
26339 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26340 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26341 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26342 return true;
26343 }
26344
26345 return false;
26346 }
26347
26348 /* Returns true if ARGS contains any errors. */
26349
26350 bool
26351 any_erroneous_template_args_p (const_tree args)
26352 {
26353 int i;
26354 int j;
26355
26356 if (args == error_mark_node)
26357 return true;
26358
26359 if (args && TREE_CODE (args) != TREE_VEC)
26360 {
26361 if (tree ti = get_template_info (args))
26362 args = TI_ARGS (ti);
26363 else
26364 args = NULL_TREE;
26365 }
26366
26367 if (!args)
26368 return false;
26369
26370 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26371 {
26372 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26373 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26374 if (error_operand_p (TREE_VEC_ELT (level, j)))
26375 return true;
26376 }
26377
26378 return false;
26379 }
26380
26381 /* Returns TRUE if the template TMPL is type-dependent. */
26382
26383 bool
26384 dependent_template_p (tree tmpl)
26385 {
26386 if (TREE_CODE (tmpl) == OVERLOAD)
26387 {
26388 for (lkp_iterator iter (tmpl); iter; ++iter)
26389 if (dependent_template_p (*iter))
26390 return true;
26391 return false;
26392 }
26393
26394 /* Template template parameters are dependent. */
26395 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26396 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26397 return true;
26398 /* So are names that have not been looked up. */
26399 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26400 return true;
26401 return false;
26402 }
26403
26404 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26405
26406 bool
26407 dependent_template_id_p (tree tmpl, tree args)
26408 {
26409 return (dependent_template_p (tmpl)
26410 || any_dependent_template_arguments_p (args));
26411 }
26412
26413 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26414 are dependent. */
26415
26416 bool
26417 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26418 {
26419 int i;
26420
26421 if (!processing_template_decl)
26422 return false;
26423
26424 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26425 {
26426 tree decl = TREE_VEC_ELT (declv, i);
26427 tree init = TREE_VEC_ELT (initv, i);
26428 tree cond = TREE_VEC_ELT (condv, i);
26429 tree incr = TREE_VEC_ELT (incrv, i);
26430
26431 if (type_dependent_expression_p (decl)
26432 || TREE_CODE (decl) == SCOPE_REF)
26433 return true;
26434
26435 if (init && type_dependent_expression_p (init))
26436 return true;
26437
26438 if (cond == global_namespace)
26439 return true;
26440
26441 if (type_dependent_expression_p (cond))
26442 return true;
26443
26444 if (COMPARISON_CLASS_P (cond)
26445 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26446 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26447 return true;
26448
26449 if (TREE_CODE (incr) == MODOP_EXPR)
26450 {
26451 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26452 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26453 return true;
26454 }
26455 else if (type_dependent_expression_p (incr))
26456 return true;
26457 else if (TREE_CODE (incr) == MODIFY_EXPR)
26458 {
26459 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26460 return true;
26461 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26462 {
26463 tree t = TREE_OPERAND (incr, 1);
26464 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26465 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26466 return true;
26467
26468 /* If this loop has a class iterator with != comparison
26469 with increment other than i++/++i/i--/--i, make sure the
26470 increment is constant. */
26471 if (CLASS_TYPE_P (TREE_TYPE (decl))
26472 && TREE_CODE (cond) == NE_EXPR)
26473 {
26474 if (TREE_OPERAND (t, 0) == decl)
26475 t = TREE_OPERAND (t, 1);
26476 else
26477 t = TREE_OPERAND (t, 0);
26478 if (TREE_CODE (t) != INTEGER_CST)
26479 return true;
26480 }
26481 }
26482 }
26483 }
26484
26485 return false;
26486 }
26487
26488 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26489 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26490 no such TYPE can be found. Note that this function peers inside
26491 uninstantiated templates and therefore should be used only in
26492 extremely limited situations. ONLY_CURRENT_P restricts this
26493 peering to the currently open classes hierarchy (which is required
26494 when comparing types). */
26495
26496 tree
26497 resolve_typename_type (tree type, bool only_current_p)
26498 {
26499 tree scope;
26500 tree name;
26501 tree decl;
26502 int quals;
26503 tree pushed_scope;
26504 tree result;
26505
26506 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26507
26508 scope = TYPE_CONTEXT (type);
26509 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26510 gcc_checking_assert (uses_template_parms (scope));
26511
26512 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26513 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26514 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26515 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26516 identifier of the TYPENAME_TYPE anymore.
26517 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26518 TYPENAME_TYPE instead, we avoid messing up with a possible
26519 typedef variant case. */
26520 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26521
26522 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26523 it first before we can figure out what NAME refers to. */
26524 if (TREE_CODE (scope) == TYPENAME_TYPE)
26525 {
26526 if (TYPENAME_IS_RESOLVING_P (scope))
26527 /* Given a class template A with a dependent base with nested type C,
26528 typedef typename A::C::C C will land us here, as trying to resolve
26529 the initial A::C leads to the local C typedef, which leads back to
26530 A::C::C. So we break the recursion now. */
26531 return type;
26532 else
26533 scope = resolve_typename_type (scope, only_current_p);
26534 }
26535 /* If we don't know what SCOPE refers to, then we cannot resolve the
26536 TYPENAME_TYPE. */
26537 if (!CLASS_TYPE_P (scope))
26538 return type;
26539 /* If this is a typedef, we don't want to look inside (c++/11987). */
26540 if (typedef_variant_p (type))
26541 return type;
26542 /* If SCOPE isn't the template itself, it will not have a valid
26543 TYPE_FIELDS list. */
26544 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26545 /* scope is either the template itself or a compatible instantiation
26546 like X<T>, so look up the name in the original template. */
26547 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26548 /* If scope has no fields, it can't be a current instantiation. Check this
26549 before currently_open_class to avoid infinite recursion (71515). */
26550 if (!TYPE_FIELDS (scope))
26551 return type;
26552 /* If the SCOPE is not the current instantiation, there's no reason
26553 to look inside it. */
26554 if (only_current_p && !currently_open_class (scope))
26555 return type;
26556 /* Enter the SCOPE so that name lookup will be resolved as if we
26557 were in the class definition. In particular, SCOPE will no
26558 longer be considered a dependent type. */
26559 pushed_scope = push_scope (scope);
26560 /* Look up the declaration. */
26561 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26562 tf_warning_or_error);
26563
26564 result = NULL_TREE;
26565
26566 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26567 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26568 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26569 if (!decl)
26570 /*nop*/;
26571 else if (identifier_p (fullname)
26572 && TREE_CODE (decl) == TYPE_DECL)
26573 {
26574 result = TREE_TYPE (decl);
26575 if (result == error_mark_node)
26576 result = NULL_TREE;
26577 }
26578 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26579 && DECL_CLASS_TEMPLATE_P (decl))
26580 {
26581 /* Obtain the template and the arguments. */
26582 tree tmpl = TREE_OPERAND (fullname, 0);
26583 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26584 {
26585 /* We get here with a plain identifier because a previous tentative
26586 parse of the nested-name-specifier as part of a ptr-operator saw
26587 ::template X<A>. The use of ::template is necessary in a
26588 ptr-operator, but wrong in a declarator-id.
26589
26590 [temp.names]: In a qualified-id of a declarator-id, the keyword
26591 template shall not appear at the top level. */
26592 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26593 "keyword %<template%> not allowed in declarator-id");
26594 tmpl = decl;
26595 }
26596 tree args = TREE_OPERAND (fullname, 1);
26597 /* Instantiate the template. */
26598 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26599 /*entering_scope=*/true,
26600 tf_error | tf_user);
26601 if (result == error_mark_node)
26602 result = NULL_TREE;
26603 }
26604
26605 /* Leave the SCOPE. */
26606 if (pushed_scope)
26607 pop_scope (pushed_scope);
26608
26609 /* If we failed to resolve it, return the original typename. */
26610 if (!result)
26611 return type;
26612
26613 /* If lookup found a typename type, resolve that too. */
26614 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26615 {
26616 /* Ill-formed programs can cause infinite recursion here, so we
26617 must catch that. */
26618 TYPENAME_IS_RESOLVING_P (result) = 1;
26619 result = resolve_typename_type (result, only_current_p);
26620 TYPENAME_IS_RESOLVING_P (result) = 0;
26621 }
26622
26623 /* Qualify the resulting type. */
26624 quals = cp_type_quals (type);
26625 if (quals)
26626 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26627
26628 return result;
26629 }
26630
26631 /* EXPR is an expression which is not type-dependent. Return a proxy
26632 for EXPR that can be used to compute the types of larger
26633 expressions containing EXPR. */
26634
26635 tree
26636 build_non_dependent_expr (tree expr)
26637 {
26638 tree orig_expr = expr;
26639 tree inner_expr;
26640
26641 /* When checking, try to get a constant value for all non-dependent
26642 expressions in order to expose bugs in *_dependent_expression_p
26643 and constexpr. This can affect code generation, see PR70704, so
26644 only do this for -fchecking=2. */
26645 if (flag_checking > 1
26646 && cxx_dialect >= cxx11
26647 /* Don't do this during nsdmi parsing as it can lead to
26648 unexpected recursive instantiations. */
26649 && !parsing_nsdmi ()
26650 /* Don't do this during concept expansion either and for
26651 the same reason. */
26652 && !expanding_concept ())
26653 fold_non_dependent_expr (expr, tf_none);
26654
26655 STRIP_ANY_LOCATION_WRAPPER (expr);
26656
26657 /* Preserve OVERLOADs; the functions must be available to resolve
26658 types. */
26659 inner_expr = expr;
26660 if (TREE_CODE (inner_expr) == STMT_EXPR)
26661 inner_expr = stmt_expr_value_expr (inner_expr);
26662 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26663 inner_expr = TREE_OPERAND (inner_expr, 0);
26664 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26665 inner_expr = TREE_OPERAND (inner_expr, 1);
26666 if (is_overloaded_fn (inner_expr)
26667 || TREE_CODE (inner_expr) == OFFSET_REF)
26668 return orig_expr;
26669 /* There is no need to return a proxy for a variable or enumerator. */
26670 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26671 return orig_expr;
26672 /* Preserve string constants; conversions from string constants to
26673 "char *" are allowed, even though normally a "const char *"
26674 cannot be used to initialize a "char *". */
26675 if (TREE_CODE (expr) == STRING_CST)
26676 return orig_expr;
26677 /* Preserve void and arithmetic constants, as an optimization -- there is no
26678 reason to create a new node. */
26679 if (TREE_CODE (expr) == VOID_CST
26680 || TREE_CODE (expr) == INTEGER_CST
26681 || TREE_CODE (expr) == REAL_CST)
26682 return orig_expr;
26683 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26684 There is at least one place where we want to know that a
26685 particular expression is a throw-expression: when checking a ?:
26686 expression, there are special rules if the second or third
26687 argument is a throw-expression. */
26688 if (TREE_CODE (expr) == THROW_EXPR)
26689 return orig_expr;
26690
26691 /* Don't wrap an initializer list, we need to be able to look inside. */
26692 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26693 return orig_expr;
26694
26695 /* Don't wrap a dummy object, we need to be able to test for it. */
26696 if (is_dummy_object (expr))
26697 return orig_expr;
26698
26699 if (TREE_CODE (expr) == COND_EXPR)
26700 return build3 (COND_EXPR,
26701 TREE_TYPE (expr),
26702 TREE_OPERAND (expr, 0),
26703 (TREE_OPERAND (expr, 1)
26704 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26705 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26706 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26707 if (TREE_CODE (expr) == COMPOUND_EXPR
26708 && !COMPOUND_EXPR_OVERLOADED (expr))
26709 return build2 (COMPOUND_EXPR,
26710 TREE_TYPE (expr),
26711 TREE_OPERAND (expr, 0),
26712 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26713
26714 /* If the type is unknown, it can't really be non-dependent */
26715 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26716
26717 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26718 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26719 TREE_TYPE (expr), expr);
26720 }
26721
26722 /* ARGS is a vector of expressions as arguments to a function call.
26723 Replace the arguments with equivalent non-dependent expressions.
26724 This modifies ARGS in place. */
26725
26726 void
26727 make_args_non_dependent (vec<tree, va_gc> *args)
26728 {
26729 unsigned int ix;
26730 tree arg;
26731
26732 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26733 {
26734 tree newarg = build_non_dependent_expr (arg);
26735 if (newarg != arg)
26736 (*args)[ix] = newarg;
26737 }
26738 }
26739
26740 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26741 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26742 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26743
26744 static tree
26745 make_auto_1 (tree name, bool set_canonical)
26746 {
26747 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26748 TYPE_NAME (au) = build_decl (input_location,
26749 TYPE_DECL, name, au);
26750 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26751 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26752 (0, processing_template_decl + 1, processing_template_decl + 1,
26753 TYPE_NAME (au), NULL_TREE);
26754 if (set_canonical)
26755 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26756 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26757 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26758
26759 return au;
26760 }
26761
26762 tree
26763 make_decltype_auto (void)
26764 {
26765 return make_auto_1 (decltype_auto_identifier, true);
26766 }
26767
26768 tree
26769 make_auto (void)
26770 {
26771 return make_auto_1 (auto_identifier, true);
26772 }
26773
26774 /* Return a C++17 deduction placeholder for class template TMPL. */
26775
26776 tree
26777 make_template_placeholder (tree tmpl)
26778 {
26779 tree t = make_auto_1 (auto_identifier, false);
26780 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26781 /* Our canonical type depends on the placeholder. */
26782 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26783 return t;
26784 }
26785
26786 /* True iff T is a C++17 class template deduction placeholder. */
26787
26788 bool
26789 template_placeholder_p (tree t)
26790 {
26791 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26792 }
26793
26794 /* Make a "constrained auto" type-specifier. This is an
26795 auto type with constraints that must be associated after
26796 deduction. The constraint is formed from the given
26797 CONC and its optional sequence of arguments, which are
26798 non-null if written as partial-concept-id. */
26799
26800 tree
26801 make_constrained_auto (tree con, tree args)
26802 {
26803 tree type = make_auto_1 (auto_identifier, false);
26804
26805 /* Build the constraint. */
26806 tree tmpl = DECL_TI_TEMPLATE (con);
26807 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26808 expr = build_concept_check (expr, type, args);
26809
26810 tree constr = normalize_expression (expr);
26811 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26812
26813 /* Our canonical type depends on the constraint. */
26814 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26815
26816 /* Attach the constraint to the type declaration. */
26817 tree decl = TYPE_NAME (type);
26818 return decl;
26819 }
26820
26821 /* Given type ARG, return std::initializer_list<ARG>. */
26822
26823 static tree
26824 listify (tree arg)
26825 {
26826 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26827
26828 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26829 {
26830 gcc_rich_location richloc (input_location);
26831 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26832 error_at (&richloc,
26833 "deducing from brace-enclosed initializer list"
26834 " requires %<#include <initializer_list>%>");
26835
26836 return error_mark_node;
26837 }
26838 tree argvec = make_tree_vec (1);
26839 TREE_VEC_ELT (argvec, 0) = arg;
26840
26841 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26842 NULL_TREE, 0, tf_warning_or_error);
26843 }
26844
26845 /* Replace auto in TYPE with std::initializer_list<auto>. */
26846
26847 static tree
26848 listify_autos (tree type, tree auto_node)
26849 {
26850 tree init_auto = listify (strip_top_quals (auto_node));
26851 tree argvec = make_tree_vec (1);
26852 TREE_VEC_ELT (argvec, 0) = init_auto;
26853 if (processing_template_decl)
26854 argvec = add_to_template_args (current_template_args (), argvec);
26855 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26856 }
26857
26858 /* Hash traits for hashing possibly constrained 'auto'
26859 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26860
26861 struct auto_hash : default_hash_traits<tree>
26862 {
26863 static inline hashval_t hash (tree);
26864 static inline bool equal (tree, tree);
26865 };
26866
26867 /* Hash the 'auto' T. */
26868
26869 inline hashval_t
26870 auto_hash::hash (tree t)
26871 {
26872 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26873 /* Matching constrained-type-specifiers denote the same template
26874 parameter, so hash the constraint. */
26875 return hash_placeholder_constraint (c);
26876 else
26877 /* But unconstrained autos are all separate, so just hash the pointer. */
26878 return iterative_hash_object (t, 0);
26879 }
26880
26881 /* Compare two 'auto's. */
26882
26883 inline bool
26884 auto_hash::equal (tree t1, tree t2)
26885 {
26886 if (t1 == t2)
26887 return true;
26888
26889 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26890 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26891
26892 /* Two unconstrained autos are distinct. */
26893 if (!c1 || !c2)
26894 return false;
26895
26896 return equivalent_placeholder_constraints (c1, c2);
26897 }
26898
26899 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26900 constrained) auto, add it to the vector. */
26901
26902 static int
26903 extract_autos_r (tree t, void *data)
26904 {
26905 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26906 if (is_auto (t))
26907 {
26908 /* All the autos were built with index 0; fix that up now. */
26909 tree *p = hash.find_slot (t, INSERT);
26910 unsigned idx;
26911 if (*p)
26912 /* If this is a repeated constrained-type-specifier, use the index we
26913 chose before. */
26914 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26915 else
26916 {
26917 /* Otherwise this is new, so use the current count. */
26918 *p = t;
26919 idx = hash.elements () - 1;
26920 }
26921 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26922 }
26923
26924 /* Always keep walking. */
26925 return 0;
26926 }
26927
26928 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26929 says they can appear anywhere in the type. */
26930
26931 static tree
26932 extract_autos (tree type)
26933 {
26934 hash_set<tree> visited;
26935 hash_table<auto_hash> hash (2);
26936
26937 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26938
26939 tree tree_vec = make_tree_vec (hash.elements());
26940 for (hash_table<auto_hash>::iterator iter = hash.begin();
26941 iter != hash.end(); ++iter)
26942 {
26943 tree elt = *iter;
26944 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26945 TREE_VEC_ELT (tree_vec, i)
26946 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26947 }
26948
26949 return tree_vec;
26950 }
26951
26952 /* The stem for deduction guide names. */
26953 const char *const dguide_base = "__dguide_";
26954
26955 /* Return the name for a deduction guide for class template TMPL. */
26956
26957 tree
26958 dguide_name (tree tmpl)
26959 {
26960 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26961 tree tname = TYPE_IDENTIFIER (type);
26962 char *buf = (char *) alloca (1 + strlen (dguide_base)
26963 + IDENTIFIER_LENGTH (tname));
26964 memcpy (buf, dguide_base, strlen (dguide_base));
26965 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26966 IDENTIFIER_LENGTH (tname) + 1);
26967 tree dname = get_identifier (buf);
26968 TREE_TYPE (dname) = type;
26969 return dname;
26970 }
26971
26972 /* True if NAME is the name of a deduction guide. */
26973
26974 bool
26975 dguide_name_p (tree name)
26976 {
26977 return (TREE_CODE (name) == IDENTIFIER_NODE
26978 && TREE_TYPE (name)
26979 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26980 strlen (dguide_base)));
26981 }
26982
26983 /* True if FN is a deduction guide. */
26984
26985 bool
26986 deduction_guide_p (const_tree fn)
26987 {
26988 if (DECL_P (fn))
26989 if (tree name = DECL_NAME (fn))
26990 return dguide_name_p (name);
26991 return false;
26992 }
26993
26994 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26995
26996 bool
26997 copy_guide_p (const_tree fn)
26998 {
26999 gcc_assert (deduction_guide_p (fn));
27000 if (!DECL_ARTIFICIAL (fn))
27001 return false;
27002 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27003 return (TREE_CHAIN (parms) == void_list_node
27004 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27005 }
27006
27007 /* True if FN is a guide generated from a constructor template. */
27008
27009 bool
27010 template_guide_p (const_tree fn)
27011 {
27012 gcc_assert (deduction_guide_p (fn));
27013 if (!DECL_ARTIFICIAL (fn))
27014 return false;
27015 tree tmpl = DECL_TI_TEMPLATE (fn);
27016 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27017 return PRIMARY_TEMPLATE_P (org);
27018 return false;
27019 }
27020
27021 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27022 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27023 template parameter types. Note that the handling of template template
27024 parameters relies on current_template_parms being set appropriately for the
27025 new template. */
27026
27027 static tree
27028 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27029 tree tsubst_args, tsubst_flags_t complain)
27030 {
27031 if (olddecl == error_mark_node)
27032 return error_mark_node;
27033
27034 tree oldidx = get_template_parm_index (olddecl);
27035
27036 tree newtype;
27037 if (TREE_CODE (olddecl) == TYPE_DECL
27038 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27039 {
27040 tree oldtype = TREE_TYPE (olddecl);
27041 newtype = cxx_make_type (TREE_CODE (oldtype));
27042 TYPE_MAIN_VARIANT (newtype) = newtype;
27043 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27044 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27045 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27046 }
27047 else
27048 {
27049 newtype = TREE_TYPE (olddecl);
27050 if (type_uses_auto (newtype))
27051 {
27052 // Substitute once to fix references to other template parameters.
27053 newtype = tsubst (newtype, tsubst_args,
27054 complain|tf_partial, NULL_TREE);
27055 // Now substitute again to reduce the level of the auto.
27056 newtype = tsubst (newtype, current_template_args (),
27057 complain, NULL_TREE);
27058 }
27059 else
27060 newtype = tsubst (newtype, tsubst_args,
27061 complain, NULL_TREE);
27062 }
27063
27064 tree newdecl
27065 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27066 DECL_NAME (olddecl), newtype);
27067 SET_DECL_TEMPLATE_PARM_P (newdecl);
27068
27069 tree newidx;
27070 if (TREE_CODE (olddecl) == TYPE_DECL
27071 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27072 {
27073 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27074 = build_template_parm_index (index, level, level,
27075 newdecl, newtype);
27076 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27077 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27078 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27079 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27080
27081 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27082 {
27083 DECL_TEMPLATE_RESULT (newdecl)
27084 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27085 DECL_NAME (olddecl), newtype);
27086 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27087 // First create a copy (ttargs) of tsubst_args with an
27088 // additional level for the template template parameter's own
27089 // template parameters (ttparms).
27090 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27091 (DECL_TEMPLATE_PARMS (olddecl)));
27092 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27093 tree ttargs = make_tree_vec (depth + 1);
27094 for (int i = 0; i < depth; ++i)
27095 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27096 TREE_VEC_ELT (ttargs, depth)
27097 = template_parms_level_to_args (ttparms);
27098 // Substitute ttargs into ttparms to fix references to
27099 // other template parameters.
27100 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27101 complain|tf_partial);
27102 // Now substitute again with args based on tparms, to reduce
27103 // the level of the ttparms.
27104 ttargs = current_template_args ();
27105 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27106 complain);
27107 // Finally, tack the adjusted parms onto tparms.
27108 ttparms = tree_cons (size_int (depth), ttparms,
27109 current_template_parms);
27110 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27111 }
27112 }
27113 else
27114 {
27115 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27116 tree newconst
27117 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27118 TREE_CODE (oldconst),
27119 DECL_NAME (oldconst), newtype);
27120 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27121 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27122 SET_DECL_TEMPLATE_PARM_P (newconst);
27123 newidx = build_template_parm_index (index, level, level,
27124 newconst, newtype);
27125 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27126 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27127 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27128 }
27129
27130 return newdecl;
27131 }
27132
27133 /* Returns a C++17 class deduction guide template based on the constructor
27134 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27135 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27136
27137 static tree
27138 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27139 {
27140 tree type, tparms, targs, fparms, fargs, ci;
27141 bool memtmpl = false;
27142 bool explicit_p;
27143 location_t loc;
27144 tree fn_tmpl = NULL_TREE;
27145
27146 if (TYPE_P (ctor))
27147 {
27148 type = ctor;
27149 bool copy_p = TYPE_REF_P (type);
27150 if (copy_p)
27151 {
27152 type = TREE_TYPE (type);
27153 fparms = tree_cons (NULL_TREE, type, void_list_node);
27154 }
27155 else
27156 fparms = void_list_node;
27157
27158 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27159 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27160 targs = CLASSTYPE_TI_ARGS (type);
27161 ci = NULL_TREE;
27162 fargs = NULL_TREE;
27163 loc = DECL_SOURCE_LOCATION (ctmpl);
27164 explicit_p = false;
27165 }
27166 else
27167 {
27168 ++processing_template_decl;
27169 bool ok = true;
27170
27171 fn_tmpl
27172 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27173 : DECL_TI_TEMPLATE (ctor));
27174 if (outer_args)
27175 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27176 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27177
27178 type = DECL_CONTEXT (ctor);
27179
27180 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27181 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27182 fully specialized args for the enclosing class. Strip those off, as
27183 the deduction guide won't have those template parameters. */
27184 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27185 TMPL_PARMS_DEPTH (tparms));
27186 /* Discard the 'this' parameter. */
27187 fparms = FUNCTION_ARG_CHAIN (ctor);
27188 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27189 ci = get_constraints (ctor);
27190 loc = DECL_SOURCE_LOCATION (ctor);
27191 explicit_p = DECL_NONCONVERTING_P (ctor);
27192
27193 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27194 {
27195 memtmpl = true;
27196
27197 /* For a member template constructor, we need to flatten the two
27198 template parameter lists into one, and then adjust the function
27199 signature accordingly. This gets...complicated. */
27200 tree save_parms = current_template_parms;
27201
27202 /* For a member template we should have two levels of parms/args, one
27203 for the class and one for the constructor. We stripped
27204 specialized args for further enclosing classes above. */
27205 const int depth = 2;
27206 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27207
27208 /* Template args for translating references to the two-level template
27209 parameters into references to the one-level template parameters we
27210 are creating. */
27211 tree tsubst_args = copy_node (targs);
27212 TMPL_ARGS_LEVEL (tsubst_args, depth)
27213 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27214
27215 /* Template parms for the constructor template. */
27216 tree ftparms = TREE_VALUE (tparms);
27217 unsigned flen = TREE_VEC_LENGTH (ftparms);
27218 /* Template parms for the class template. */
27219 tparms = TREE_CHAIN (tparms);
27220 tree ctparms = TREE_VALUE (tparms);
27221 unsigned clen = TREE_VEC_LENGTH (ctparms);
27222 /* Template parms for the deduction guide start as a copy of the
27223 template parms for the class. We set current_template_parms for
27224 lookup_template_class_1. */
27225 current_template_parms = tparms = copy_node (tparms);
27226 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27227 for (unsigned i = 0; i < clen; ++i)
27228 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27229
27230 /* Now we need to rewrite the constructor parms to append them to the
27231 class parms. */
27232 for (unsigned i = 0; i < flen; ++i)
27233 {
27234 unsigned index = i + clen;
27235 unsigned level = 1;
27236 tree oldelt = TREE_VEC_ELT (ftparms, i);
27237 tree olddecl = TREE_VALUE (oldelt);
27238 tree newdecl = rewrite_template_parm (olddecl, index, level,
27239 tsubst_args, complain);
27240 if (newdecl == error_mark_node)
27241 ok = false;
27242 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27243 tsubst_args, complain, ctor);
27244 tree list = build_tree_list (newdef, newdecl);
27245 TEMPLATE_PARM_CONSTRAINTS (list)
27246 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27247 tsubst_args, complain, ctor);
27248 TREE_VEC_ELT (new_vec, index) = list;
27249 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27250 }
27251
27252 /* Now we have a final set of template parms to substitute into the
27253 function signature. */
27254 targs = template_parms_to_args (tparms);
27255 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27256 complain, ctor);
27257 if (fparms == error_mark_node)
27258 ok = false;
27259 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27260 if (ci)
27261 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27262
27263 current_template_parms = save_parms;
27264 }
27265
27266 --processing_template_decl;
27267 if (!ok)
27268 return error_mark_node;
27269 }
27270
27271 if (!memtmpl)
27272 {
27273 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27274 tparms = copy_node (tparms);
27275 INNERMOST_TEMPLATE_PARMS (tparms)
27276 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27277 }
27278
27279 tree fntype = build_function_type (type, fparms);
27280 tree ded_fn = build_lang_decl_loc (loc,
27281 FUNCTION_DECL,
27282 dguide_name (type), fntype);
27283 DECL_ARGUMENTS (ded_fn) = fargs;
27284 DECL_ARTIFICIAL (ded_fn) = true;
27285 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27286 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27287 DECL_ARTIFICIAL (ded_tmpl) = true;
27288 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27289 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27290 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27291 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27292 if (DECL_P (ctor))
27293 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27294 if (ci)
27295 set_constraints (ded_tmpl, ci);
27296
27297 return ded_tmpl;
27298 }
27299
27300 /* Deduce template arguments for the class template placeholder PTYPE for
27301 template TMPL based on the initializer INIT, and return the resulting
27302 type. */
27303
27304 static tree
27305 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27306 tsubst_flags_t complain)
27307 {
27308 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27309 {
27310 /* We should have handled this in the caller. */
27311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27312 return ptype;
27313 if (complain & tf_error)
27314 error ("non-class template %qT used without template arguments", tmpl);
27315 return error_mark_node;
27316 }
27317 if (init && TREE_TYPE (init) == ptype)
27318 /* Using the template parm as its own argument. */
27319 return ptype;
27320
27321 tree type = TREE_TYPE (tmpl);
27322
27323 bool try_list_ctor = false;
27324
27325 releasing_vec rv_args = NULL;
27326 vec<tree,va_gc> *&args = *&rv_args;
27327 if (init == NULL_TREE
27328 || TREE_CODE (init) == TREE_LIST)
27329 args = make_tree_vector_from_list (init);
27330 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27331 {
27332 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27333 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27334 {
27335 /* As an exception, the first phase in 16.3.1.7 (considering the
27336 initializer list as a single argument) is omitted if the
27337 initializer list consists of a single expression of type cv U,
27338 where U is a specialization of C or a class derived from a
27339 specialization of C. */
27340 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27341 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27342 {
27343 tree etype = TREE_TYPE (elt);
27344 tree tparms = (INNERMOST_TEMPLATE_PARMS
27345 (DECL_TEMPLATE_PARMS (tmpl)));
27346 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27347 int err = unify (tparms, targs, type, etype,
27348 UNIFY_ALLOW_DERIVED, /*explain*/false);
27349 if (err == 0)
27350 try_list_ctor = false;
27351 ggc_free (targs);
27352 }
27353 }
27354 if (try_list_ctor || is_std_init_list (type))
27355 args = make_tree_vector_single (init);
27356 else
27357 args = make_tree_vector_from_ctor (init);
27358 }
27359 else
27360 args = make_tree_vector_single (init);
27361
27362 tree dname = dguide_name (tmpl);
27363 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27364 /*type*/false, /*complain*/false,
27365 /*hidden*/false);
27366 bool elided = false;
27367 if (cands == error_mark_node)
27368 cands = NULL_TREE;
27369
27370 /* Prune explicit deduction guides in copy-initialization context. */
27371 if (flags & LOOKUP_ONLYCONVERTING)
27372 {
27373 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27374 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27375 elided = true;
27376
27377 if (elided)
27378 {
27379 /* Found a nonconverting guide, prune the candidates. */
27380 tree pruned = NULL_TREE;
27381 for (lkp_iterator iter (cands); iter; ++iter)
27382 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27383 pruned = lookup_add (*iter, pruned);
27384
27385 cands = pruned;
27386 }
27387 }
27388
27389 tree outer_args = NULL_TREE;
27390 if (DECL_CLASS_SCOPE_P (tmpl)
27391 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27392 {
27393 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27394 type = TREE_TYPE (most_general_template (tmpl));
27395 }
27396
27397 bool saw_ctor = false;
27398 // FIXME cache artificial deduction guides
27399 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27400 {
27401 /* Skip inherited constructors. */
27402 if (iter.using_p ())
27403 continue;
27404
27405 tree guide = build_deduction_guide (*iter, outer_args, complain);
27406 if (guide == error_mark_node)
27407 return error_mark_node;
27408 if ((flags & LOOKUP_ONLYCONVERTING)
27409 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27410 elided = true;
27411 else
27412 cands = lookup_add (guide, cands);
27413
27414 saw_ctor = true;
27415 }
27416
27417 tree call = error_mark_node;
27418
27419 /* If this is list-initialization and the class has a list constructor, first
27420 try deducing from the list as a single argument, as [over.match.list]. */
27421 tree list_cands = NULL_TREE;
27422 if (try_list_ctor && cands)
27423 for (lkp_iterator iter (cands); iter; ++iter)
27424 {
27425 tree dg = *iter;
27426 if (is_list_ctor (dg))
27427 list_cands = lookup_add (dg, list_cands);
27428 }
27429 if (list_cands)
27430 {
27431 ++cp_unevaluated_operand;
27432 call = build_new_function_call (list_cands, &args, tf_decltype);
27433 --cp_unevaluated_operand;
27434
27435 if (call == error_mark_node)
27436 {
27437 /* That didn't work, now try treating the list as a sequence of
27438 arguments. */
27439 release_tree_vector (args);
27440 args = make_tree_vector_from_ctor (init);
27441 }
27442 }
27443
27444 /* Maybe generate an implicit deduction guide. */
27445 if (call == error_mark_node && args->length () < 2)
27446 {
27447 tree gtype = NULL_TREE;
27448
27449 if (args->length () == 1)
27450 /* Generate a copy guide. */
27451 gtype = build_reference_type (type);
27452 else if (!saw_ctor)
27453 /* Generate a default guide. */
27454 gtype = type;
27455
27456 if (gtype)
27457 {
27458 tree guide = build_deduction_guide (gtype, outer_args, complain);
27459 if (guide == error_mark_node)
27460 return error_mark_node;
27461 cands = lookup_add (guide, cands);
27462 }
27463 }
27464
27465 if (elided && !cands)
27466 {
27467 error ("cannot deduce template arguments for copy-initialization"
27468 " of %qT, as it has no non-explicit deduction guides or "
27469 "user-declared constructors", type);
27470 return error_mark_node;
27471 }
27472 else if (!cands && call == error_mark_node)
27473 {
27474 error ("cannot deduce template arguments of %qT, as it has no viable "
27475 "deduction guides", type);
27476 return error_mark_node;
27477 }
27478
27479 if (call == error_mark_node)
27480 {
27481 ++cp_unevaluated_operand;
27482 call = build_new_function_call (cands, &args, tf_decltype);
27483 --cp_unevaluated_operand;
27484 }
27485
27486 if (call == error_mark_node && (complain & tf_warning_or_error))
27487 {
27488 error ("class template argument deduction failed:");
27489
27490 ++cp_unevaluated_operand;
27491 call = build_new_function_call (cands, &args, complain | tf_decltype);
27492 --cp_unevaluated_operand;
27493
27494 if (elided)
27495 inform (input_location, "explicit deduction guides not considered "
27496 "for copy-initialization");
27497 }
27498
27499 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27500 }
27501
27502 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27503 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27504 The CONTEXT determines the context in which auto deduction is performed
27505 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27506 OUTER_TARGS are used during template argument deduction
27507 (context == adc_unify) to properly substitute the result, and is ignored
27508 in other contexts.
27509
27510 For partial-concept-ids, extra args may be appended to the list of deduced
27511 template arguments prior to determining constraint satisfaction. */
27512
27513 tree
27514 do_auto_deduction (tree type, tree init, tree auto_node,
27515 tsubst_flags_t complain, auto_deduction_context context,
27516 tree outer_targs, int flags)
27517 {
27518 tree targs;
27519
27520 if (init == error_mark_node)
27521 return error_mark_node;
27522
27523 if (init && type_dependent_expression_p (init)
27524 && context != adc_unify)
27525 /* Defining a subset of type-dependent expressions that we can deduce
27526 from ahead of time isn't worth the trouble. */
27527 return type;
27528
27529 /* Similarly, we can't deduce from another undeduced decl. */
27530 if (init && undeduced_auto_decl (init))
27531 return type;
27532
27533 /* We may be doing a partial substitution, but we still want to replace
27534 auto_node. */
27535 complain &= ~tf_partial;
27536
27537 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27538 /* C++17 class template argument deduction. */
27539 return do_class_deduction (type, tmpl, init, flags, complain);
27540
27541 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27542 /* Nothing we can do with this, even in deduction context. */
27543 return type;
27544
27545 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27546 with either a new invented type template parameter U or, if the
27547 initializer is a braced-init-list (8.5.4), with
27548 std::initializer_list<U>. */
27549 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27550 {
27551 if (!DIRECT_LIST_INIT_P (init))
27552 type = listify_autos (type, auto_node);
27553 else if (CONSTRUCTOR_NELTS (init) == 1)
27554 init = CONSTRUCTOR_ELT (init, 0)->value;
27555 else
27556 {
27557 if (complain & tf_warning_or_error)
27558 {
27559 if (permerror (input_location, "direct-list-initialization of "
27560 "%<auto%> requires exactly one element"))
27561 inform (input_location,
27562 "for deduction to %<std::initializer_list%>, use copy-"
27563 "list-initialization (i.e. add %<=%> before the %<{%>)");
27564 }
27565 type = listify_autos (type, auto_node);
27566 }
27567 }
27568
27569 if (type == error_mark_node)
27570 return error_mark_node;
27571
27572 init = resolve_nondeduced_context (init, complain);
27573
27574 if (context == adc_decomp_type
27575 && auto_node == type
27576 && init != error_mark_node
27577 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27578 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27579 and initializer has array type, deduce cv-qualified array type. */
27580 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27581 complain);
27582 else if (AUTO_IS_DECLTYPE (auto_node))
27583 {
27584 tree stripped_init = tree_strip_any_location_wrapper (init);
27585 bool id = (DECL_P (stripped_init)
27586 || ((TREE_CODE (init) == COMPONENT_REF
27587 || TREE_CODE (init) == SCOPE_REF)
27588 && !REF_PARENTHESIZED_P (init)));
27589 targs = make_tree_vec (1);
27590 TREE_VEC_ELT (targs, 0)
27591 = finish_decltype_type (init, id, tf_warning_or_error);
27592 if (type != auto_node)
27593 {
27594 if (complain & tf_error)
27595 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27596 return error_mark_node;
27597 }
27598 }
27599 else
27600 {
27601 tree parms = build_tree_list (NULL_TREE, type);
27602 tree tparms;
27603
27604 if (flag_concepts)
27605 tparms = extract_autos (type);
27606 else
27607 {
27608 tparms = make_tree_vec (1);
27609 TREE_VEC_ELT (tparms, 0)
27610 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27611 }
27612
27613 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27614 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27615 DEDUCE_CALL,
27616 NULL, /*explain_p=*/false);
27617 if (val > 0)
27618 {
27619 if (processing_template_decl)
27620 /* Try again at instantiation time. */
27621 return type;
27622 if (type && type != error_mark_node
27623 && (complain & tf_error))
27624 /* If type is error_mark_node a diagnostic must have been
27625 emitted by now. Also, having a mention to '<type error>'
27626 in the diagnostic is not really useful to the user. */
27627 {
27628 if (cfun
27629 && FNDECL_USED_AUTO (current_function_decl)
27630 && (auto_node
27631 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27632 && LAMBDA_FUNCTION_P (current_function_decl))
27633 error ("unable to deduce lambda return type from %qE", init);
27634 else
27635 error ("unable to deduce %qT from %qE", type, init);
27636 type_unification_real (tparms, targs, parms, &init, 1, 0,
27637 DEDUCE_CALL,
27638 NULL, /*explain_p=*/true);
27639 }
27640 return error_mark_node;
27641 }
27642 }
27643
27644 /* Check any placeholder constraints against the deduced type. */
27645 if (flag_concepts && !processing_template_decl)
27646 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27647 {
27648 /* Use the deduced type to check the associated constraints. If we
27649 have a partial-concept-id, rebuild the argument list so that
27650 we check using the extra arguments. */
27651 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27652 tree cargs = CHECK_CONSTR_ARGS (constr);
27653 if (TREE_VEC_LENGTH (cargs) > 1)
27654 {
27655 cargs = copy_node (cargs);
27656 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27657 }
27658 else
27659 cargs = targs;
27660 if (!constraints_satisfied_p (constr, cargs))
27661 {
27662 if (complain & tf_warning_or_error)
27663 {
27664 auto_diagnostic_group d;
27665 switch (context)
27666 {
27667 case adc_unspecified:
27668 case adc_unify:
27669 error("placeholder constraints not satisfied");
27670 break;
27671 case adc_variable_type:
27672 case adc_decomp_type:
27673 error ("deduced initializer does not satisfy "
27674 "placeholder constraints");
27675 break;
27676 case adc_return_type:
27677 error ("deduced return type does not satisfy "
27678 "placeholder constraints");
27679 break;
27680 case adc_requirement:
27681 error ("deduced expression type does not satisfy "
27682 "placeholder constraints");
27683 break;
27684 }
27685 diagnose_constraints (input_location, constr, targs);
27686 }
27687 return error_mark_node;
27688 }
27689 }
27690
27691 if (processing_template_decl && context != adc_unify)
27692 outer_targs = current_template_args ();
27693 targs = add_to_template_args (outer_targs, targs);
27694 return tsubst (type, targs, complain, NULL_TREE);
27695 }
27696
27697 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27698 result. */
27699
27700 tree
27701 splice_late_return_type (tree type, tree late_return_type)
27702 {
27703 if (is_auto (type))
27704 {
27705 if (late_return_type)
27706 return late_return_type;
27707
27708 tree idx = get_template_parm_index (type);
27709 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27710 /* In an abbreviated function template we didn't know we were dealing
27711 with a function template when we saw the auto return type, so update
27712 it to have the correct level. */
27713 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27714 }
27715 return type;
27716 }
27717
27718 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27719 'decltype(auto)' or a deduced class template. */
27720
27721 bool
27722 is_auto (const_tree type)
27723 {
27724 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27725 && (TYPE_IDENTIFIER (type) == auto_identifier
27726 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27727 return true;
27728 else
27729 return false;
27730 }
27731
27732 /* for_each_template_parm callback for type_uses_auto. */
27733
27734 int
27735 is_auto_r (tree tp, void */*data*/)
27736 {
27737 return is_auto (tp);
27738 }
27739
27740 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27741 a use of `auto'. Returns NULL_TREE otherwise. */
27742
27743 tree
27744 type_uses_auto (tree type)
27745 {
27746 if (type == NULL_TREE)
27747 return NULL_TREE;
27748 else if (flag_concepts)
27749 {
27750 /* The Concepts TS allows multiple autos in one type-specifier; just
27751 return the first one we find, do_auto_deduction will collect all of
27752 them. */
27753 if (uses_template_parms (type))
27754 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27755 /*visited*/NULL, /*nondeduced*/false);
27756 else
27757 return NULL_TREE;
27758 }
27759 else
27760 return find_type_usage (type, is_auto);
27761 }
27762
27763 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27764 concepts are enabled, auto is acceptable in template arguments, but
27765 only when TEMPL identifies a template class. Return TRUE if any
27766 such errors were reported. */
27767
27768 bool
27769 check_auto_in_tmpl_args (tree tmpl, tree args)
27770 {
27771 /* If there were previous errors, nevermind. */
27772 if (!args || TREE_CODE (args) != TREE_VEC)
27773 return false;
27774
27775 /* If TMPL is an identifier, we're parsing and we can't tell yet
27776 whether TMPL is supposed to be a type, a function or a variable.
27777 We'll only be able to tell during template substitution, so we
27778 expect to be called again then. If concepts are enabled and we
27779 know we have a type, we're ok. */
27780 if (flag_concepts
27781 && (identifier_p (tmpl)
27782 || (DECL_P (tmpl)
27783 && (DECL_TYPE_TEMPLATE_P (tmpl)
27784 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27785 return false;
27786
27787 /* Quickly search for any occurrences of auto; usually there won't
27788 be any, and then we'll avoid allocating the vector. */
27789 if (!type_uses_auto (args))
27790 return false;
27791
27792 bool errors = false;
27793
27794 tree vec = extract_autos (args);
27795 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27796 {
27797 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27798 error_at (DECL_SOURCE_LOCATION (xauto),
27799 "invalid use of %qT in template argument", xauto);
27800 errors = true;
27801 }
27802
27803 return errors;
27804 }
27805
27806 /* For a given template T, return the vector of typedefs referenced
27807 in T for which access check is needed at T instantiation time.
27808 T is either a FUNCTION_DECL or a RECORD_TYPE.
27809 Those typedefs were added to T by the function
27810 append_type_to_template_for_access_check. */
27811
27812 vec<qualified_typedef_usage_t, va_gc> *
27813 get_types_needing_access_check (tree t)
27814 {
27815 tree ti;
27816 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27817
27818 if (!t || t == error_mark_node)
27819 return NULL;
27820
27821 if (!(ti = get_template_info (t)))
27822 return NULL;
27823
27824 if (CLASS_TYPE_P (t)
27825 || TREE_CODE (t) == FUNCTION_DECL)
27826 {
27827 if (!TI_TEMPLATE (ti))
27828 return NULL;
27829
27830 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27831 }
27832
27833 return result;
27834 }
27835
27836 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27837 tied to T. That list of typedefs will be access checked at
27838 T instantiation time.
27839 T is either a FUNCTION_DECL or a RECORD_TYPE.
27840 TYPE_DECL is a TYPE_DECL node representing a typedef.
27841 SCOPE is the scope through which TYPE_DECL is accessed.
27842 LOCATION is the location of the usage point of TYPE_DECL.
27843
27844 This function is a subroutine of
27845 append_type_to_template_for_access_check. */
27846
27847 static void
27848 append_type_to_template_for_access_check_1 (tree t,
27849 tree type_decl,
27850 tree scope,
27851 location_t location)
27852 {
27853 qualified_typedef_usage_t typedef_usage;
27854 tree ti;
27855
27856 if (!t || t == error_mark_node)
27857 return;
27858
27859 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27860 || CLASS_TYPE_P (t))
27861 && type_decl
27862 && TREE_CODE (type_decl) == TYPE_DECL
27863 && scope);
27864
27865 if (!(ti = get_template_info (t)))
27866 return;
27867
27868 gcc_assert (TI_TEMPLATE (ti));
27869
27870 typedef_usage.typedef_decl = type_decl;
27871 typedef_usage.context = scope;
27872 typedef_usage.locus = location;
27873
27874 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27875 }
27876
27877 /* Append TYPE_DECL to the template TEMPL.
27878 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27879 At TEMPL instanciation time, TYPE_DECL will be checked to see
27880 if it can be accessed through SCOPE.
27881 LOCATION is the location of the usage point of TYPE_DECL.
27882
27883 e.g. consider the following code snippet:
27884
27885 class C
27886 {
27887 typedef int myint;
27888 };
27889
27890 template<class U> struct S
27891 {
27892 C::myint mi; // <-- usage point of the typedef C::myint
27893 };
27894
27895 S<char> s;
27896
27897 At S<char> instantiation time, we need to check the access of C::myint
27898 In other words, we need to check the access of the myint typedef through
27899 the C scope. For that purpose, this function will add the myint typedef
27900 and the scope C through which its being accessed to a list of typedefs
27901 tied to the template S. That list will be walked at template instantiation
27902 time and access check performed on each typedefs it contains.
27903 Note that this particular code snippet should yield an error because
27904 myint is private to C. */
27905
27906 void
27907 append_type_to_template_for_access_check (tree templ,
27908 tree type_decl,
27909 tree scope,
27910 location_t location)
27911 {
27912 qualified_typedef_usage_t *iter;
27913 unsigned i;
27914
27915 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27916
27917 /* Make sure we don't append the type to the template twice. */
27918 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27919 if (iter->typedef_decl == type_decl && scope == iter->context)
27920 return;
27921
27922 append_type_to_template_for_access_check_1 (templ, type_decl,
27923 scope, location);
27924 }
27925
27926 /* Convert the generic type parameters in PARM that match the types given in the
27927 range [START_IDX, END_IDX) from the current_template_parms into generic type
27928 packs. */
27929
27930 tree
27931 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27932 {
27933 tree current = current_template_parms;
27934 int depth = TMPL_PARMS_DEPTH (current);
27935 current = INNERMOST_TEMPLATE_PARMS (current);
27936 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27937
27938 for (int i = 0; i < start_idx; ++i)
27939 TREE_VEC_ELT (replacement, i)
27940 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27941
27942 for (int i = start_idx; i < end_idx; ++i)
27943 {
27944 /* Create a distinct parameter pack type from the current parm and add it
27945 to the replacement args to tsubst below into the generic function
27946 parameter. */
27947
27948 tree o = TREE_TYPE (TREE_VALUE
27949 (TREE_VEC_ELT (current, i)));
27950 tree t = copy_type (o);
27951 TEMPLATE_TYPE_PARM_INDEX (t)
27952 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27953 o, 0, 0, tf_none);
27954 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27955 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27956 TYPE_MAIN_VARIANT (t) = t;
27957 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27958 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27959 TREE_VEC_ELT (replacement, i) = t;
27960 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27961 }
27962
27963 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27964 TREE_VEC_ELT (replacement, i)
27965 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27966
27967 /* If there are more levels then build up the replacement with the outer
27968 template parms. */
27969 if (depth > 1)
27970 replacement = add_to_template_args (template_parms_to_args
27971 (TREE_CHAIN (current_template_parms)),
27972 replacement);
27973
27974 return tsubst (parm, replacement, tf_none, NULL_TREE);
27975 }
27976
27977 /* Entries in the decl_constraint hash table. */
27978 struct GTY((for_user)) constr_entry
27979 {
27980 tree decl;
27981 tree ci;
27982 };
27983
27984 /* Hashing function and equality for constraint entries. */
27985 struct constr_hasher : ggc_ptr_hash<constr_entry>
27986 {
27987 static hashval_t hash (constr_entry *e)
27988 {
27989 return (hashval_t)DECL_UID (e->decl);
27990 }
27991
27992 static bool equal (constr_entry *e1, constr_entry *e2)
27993 {
27994 return e1->decl == e2->decl;
27995 }
27996 };
27997
27998 /* A mapping from declarations to constraint information. Note that
27999 both templates and their underlying declarations are mapped to the
28000 same constraint information.
28001
28002 FIXME: This is defined in pt.c because garbage collection
28003 code is not being generated for constraint.cc. */
28004
28005 static GTY (()) hash_table<constr_hasher> *decl_constraints;
28006
28007 /* Returns the template constraints of declaration T. If T is not
28008 constrained, return NULL_TREE. Note that T must be non-null. */
28009
28010 tree
28011 get_constraints (tree t)
28012 {
28013 if (!flag_concepts)
28014 return NULL_TREE;
28015
28016 gcc_assert (DECL_P (t));
28017 if (TREE_CODE (t) == TEMPLATE_DECL)
28018 t = DECL_TEMPLATE_RESULT (t);
28019 constr_entry elt = { t, NULL_TREE };
28020 constr_entry* found = decl_constraints->find (&elt);
28021 if (found)
28022 return found->ci;
28023 else
28024 return NULL_TREE;
28025 }
28026
28027 /* Associate the given constraint information CI with the declaration
28028 T. If T is a template, then the constraints are associated with
28029 its underlying declaration. Don't build associations if CI is
28030 NULL_TREE. */
28031
28032 void
28033 set_constraints (tree t, tree ci)
28034 {
28035 if (!ci)
28036 return;
28037 gcc_assert (t && flag_concepts);
28038 if (TREE_CODE (t) == TEMPLATE_DECL)
28039 t = DECL_TEMPLATE_RESULT (t);
28040 gcc_assert (!get_constraints (t));
28041 constr_entry elt = {t, ci};
28042 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28043 constr_entry* entry = ggc_alloc<constr_entry> ();
28044 *entry = elt;
28045 *slot = entry;
28046 }
28047
28048 /* Remove the associated constraints of the declaration T. */
28049
28050 void
28051 remove_constraints (tree t)
28052 {
28053 gcc_assert (DECL_P (t));
28054 if (TREE_CODE (t) == TEMPLATE_DECL)
28055 t = DECL_TEMPLATE_RESULT (t);
28056
28057 constr_entry elt = {t, NULL_TREE};
28058 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28059 if (slot)
28060 decl_constraints->clear_slot (slot);
28061 }
28062
28063 /* Memoized satisfaction results for declarations. This
28064 maps the pair (constraint_info, arguments) to the result computed
28065 by constraints_satisfied_p. */
28066
28067 struct GTY((for_user)) constraint_sat_entry
28068 {
28069 tree ci;
28070 tree args;
28071 tree result;
28072 };
28073
28074 /* Hashing function and equality for constraint entries. */
28075
28076 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28077 {
28078 static hashval_t hash (constraint_sat_entry *e)
28079 {
28080 hashval_t val = iterative_hash_object(e->ci, 0);
28081 return iterative_hash_template_arg (e->args, val);
28082 }
28083
28084 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28085 {
28086 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28087 }
28088 };
28089
28090 /* Memoized satisfaction results for concept checks. */
28091
28092 struct GTY((for_user)) concept_spec_entry
28093 {
28094 tree tmpl;
28095 tree args;
28096 tree result;
28097 };
28098
28099 /* Hashing function and equality for constraint entries. */
28100
28101 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28102 {
28103 static hashval_t hash (concept_spec_entry *e)
28104 {
28105 return hash_tmpl_and_args (e->tmpl, e->args);
28106 }
28107
28108 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28109 {
28110 ++comparing_specializations;
28111 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28112 --comparing_specializations;
28113 return eq;
28114 }
28115 };
28116
28117 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28118 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28119
28120 /* Search for a memoized satisfaction result. Returns one of the
28121 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28122
28123 tree
28124 lookup_constraint_satisfaction (tree ci, tree args)
28125 {
28126 constraint_sat_entry elt = { ci, args, NULL_TREE };
28127 constraint_sat_entry* found = constraint_memos->find (&elt);
28128 if (found)
28129 return found->result;
28130 else
28131 return NULL_TREE;
28132 }
28133
28134 /* Memoize the result of a satisfication test. Returns the saved result. */
28135
28136 tree
28137 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28138 {
28139 constraint_sat_entry elt = {ci, args, result};
28140 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28141 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28142 *entry = elt;
28143 *slot = entry;
28144 return result;
28145 }
28146
28147 /* Search for a memoized satisfaction result for a concept. */
28148
28149 tree
28150 lookup_concept_satisfaction (tree tmpl, tree args)
28151 {
28152 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28153 concept_spec_entry* found = concept_memos->find (&elt);
28154 if (found)
28155 return found->result;
28156 else
28157 return NULL_TREE;
28158 }
28159
28160 /* Memoize the result of a concept check. Returns the saved result. */
28161
28162 tree
28163 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28164 {
28165 concept_spec_entry elt = {tmpl, args, result};
28166 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28167 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28168 *entry = elt;
28169 *slot = entry;
28170 return result;
28171 }
28172
28173 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28174
28175 /* Returns a prior concept specialization. This returns the substituted
28176 and normalized constraints defined by the concept. */
28177
28178 tree
28179 get_concept_expansion (tree tmpl, tree args)
28180 {
28181 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28182 concept_spec_entry* found = concept_expansions->find (&elt);
28183 if (found)
28184 return found->result;
28185 else
28186 return NULL_TREE;
28187 }
28188
28189 /* Save a concept expansion for later. */
28190
28191 tree
28192 save_concept_expansion (tree tmpl, tree args, tree def)
28193 {
28194 concept_spec_entry elt = {tmpl, args, def};
28195 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28196 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28197 *entry = elt;
28198 *slot = entry;
28199 return def;
28200 }
28201
28202 static hashval_t
28203 hash_subsumption_args (tree t1, tree t2)
28204 {
28205 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28206 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28207 int val = 0;
28208 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28209 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28210 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28211 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28212 return val;
28213 }
28214
28215 /* Compare the constraints of two subsumption entries. The LEFT1 and
28216 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28217 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28218
28219 static bool
28220 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28221 {
28222 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28223 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28224 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28225 CHECK_CONSTR_ARGS (right1)))
28226 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28227 CHECK_CONSTR_ARGS (right2));
28228 return false;
28229 }
28230
28231 /* Key/value pair for learning and memoizing subsumption results. This
28232 associates a pair of check constraints (including arguments) with
28233 a boolean value indicating the result. */
28234
28235 struct GTY((for_user)) subsumption_entry
28236 {
28237 tree t1;
28238 tree t2;
28239 bool result;
28240 };
28241
28242 /* Hashing function and equality for constraint entries. */
28243
28244 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28245 {
28246 static hashval_t hash (subsumption_entry *e)
28247 {
28248 return hash_subsumption_args (e->t1, e->t2);
28249 }
28250
28251 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28252 {
28253 ++comparing_specializations;
28254 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28255 --comparing_specializations;
28256 return eq;
28257 }
28258 };
28259
28260 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28261
28262 /* Search for a previously cached subsumption result. */
28263
28264 bool*
28265 lookup_subsumption_result (tree t1, tree t2)
28266 {
28267 subsumption_entry elt = { t1, t2, false };
28268 subsumption_entry* found = subsumption_table->find (&elt);
28269 if (found)
28270 return &found->result;
28271 else
28272 return 0;
28273 }
28274
28275 /* Save a subsumption result. */
28276
28277 bool
28278 save_subsumption_result (tree t1, tree t2, bool result)
28279 {
28280 subsumption_entry elt = {t1, t2, result};
28281 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28282 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28283 *entry = elt;
28284 *slot = entry;
28285 return result;
28286 }
28287
28288 /* Set up the hash table for constraint association. */
28289
28290 void
28291 init_constraint_processing (void)
28292 {
28293 if (!flag_concepts)
28294 return;
28295
28296 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28297 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28298 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28299 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28300 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28301 }
28302
28303 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28304 0..N-1. */
28305
28306 void
28307 declare_integer_pack (void)
28308 {
28309 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28310 build_function_type_list (integer_type_node,
28311 integer_type_node,
28312 NULL_TREE),
28313 NULL_TREE, ECF_CONST);
28314 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28315 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28316 DECL_FUNCTION_CODE (ipfn)
28317 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28318 }
28319
28320 /* Set up the hash tables for template instantiations. */
28321
28322 void
28323 init_template_processing (void)
28324 {
28325 /* FIXME: enable sanitization (PR87847) */
28326 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28327 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28328
28329 if (cxx_dialect >= cxx11)
28330 declare_integer_pack ();
28331 }
28332
28333 /* Print stats about the template hash tables for -fstats. */
28334
28335 void
28336 print_template_statistics (void)
28337 {
28338 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28339 "%f collisions\n", (long) decl_specializations->size (),
28340 (long) decl_specializations->elements (),
28341 decl_specializations->collisions ());
28342 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28343 "%f collisions\n", (long) type_specializations->size (),
28344 (long) type_specializations->elements (),
28345 type_specializations->collisions ());
28346 }
28347
28348 #if CHECKING_P
28349
28350 namespace selftest {
28351
28352 /* Verify that build_non_dependent_expr () works, for various expressions,
28353 and that location wrappers don't affect the results. */
28354
28355 static void
28356 test_build_non_dependent_expr ()
28357 {
28358 location_t loc = BUILTINS_LOCATION;
28359
28360 /* Verify constants, without and with location wrappers. */
28361 tree int_cst = build_int_cst (integer_type_node, 42);
28362 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28363
28364 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28365 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28366 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28367
28368 tree string_lit = build_string (4, "foo");
28369 TREE_TYPE (string_lit) = char_array_type_node;
28370 string_lit = fix_string_type (string_lit);
28371 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28372
28373 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28374 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28375 ASSERT_EQ (wrapped_string_lit,
28376 build_non_dependent_expr (wrapped_string_lit));
28377 }
28378
28379 /* Verify that type_dependent_expression_p () works correctly, even
28380 in the presence of location wrapper nodes. */
28381
28382 static void
28383 test_type_dependent_expression_p ()
28384 {
28385 location_t loc = BUILTINS_LOCATION;
28386
28387 tree name = get_identifier ("foo");
28388
28389 /* If no templates are involved, nothing is type-dependent. */
28390 gcc_assert (!processing_template_decl);
28391 ASSERT_FALSE (type_dependent_expression_p (name));
28392
28393 ++processing_template_decl;
28394
28395 /* Within a template, an unresolved name is always type-dependent. */
28396 ASSERT_TRUE (type_dependent_expression_p (name));
28397
28398 /* Ensure it copes with NULL_TREE and errors. */
28399 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28400 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28401
28402 /* A USING_DECL in a template should be type-dependent, even if wrapped
28403 with a location wrapper (PR c++/83799). */
28404 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28405 TREE_TYPE (using_decl) = integer_type_node;
28406 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28407 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28408 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28409 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28410
28411 --processing_template_decl;
28412 }
28413
28414 /* Run all of the selftests within this file. */
28415
28416 void
28417 cp_pt_c_tests ()
28418 {
28419 test_build_non_dependent_expr ();
28420 test_type_dependent_expression_p ();
28421 }
28422
28423 } // namespace selftest
28424
28425 #endif /* #if CHECKING_P */
28426
28427 #include "gt-cp-pt.h"