b8fbf4046f0790c0262fec99ddf0c2d2cd411f43
[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 permerror (input_location,
1062 "specializing %q#T in different namespace", type);
1063 permerror (DECL_SOURCE_LOCATION (tmpl),
1064 " from definition of %q#D", tmpl);
1065 }
1066
1067 /* Check for invalid specialization after instantiation:
1068
1069 template <> template <> class C<int>::D<int>;
1070 template <> template <class U> class C<int>::D; */
1071
1072 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1073 t; t = TREE_CHAIN (t))
1074 {
1075 tree inst = TREE_VALUE (t);
1076 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1077 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1078 {
1079 /* We already have a full specialization of this partial
1080 instantiation, or a full specialization has been
1081 looked up but not instantiated. Reassign it to the
1082 new member specialization template. */
1083 spec_entry elt;
1084 spec_entry *entry;
1085
1086 elt.tmpl = most_general_template (tmpl);
1087 elt.args = CLASSTYPE_TI_ARGS (inst);
1088 elt.spec = inst;
1089
1090 type_specializations->remove_elt (&elt);
1091
1092 elt.tmpl = tmpl;
1093 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1094
1095 spec_entry **slot
1096 = type_specializations->find_slot (&elt, INSERT);
1097 entry = ggc_alloc<spec_entry> ();
1098 *entry = elt;
1099 *slot = entry;
1100 }
1101 else
1102 /* But if we've had an implicit instantiation, that's a
1103 problem ([temp.expl.spec]/6). */
1104 error ("specialization %qT after instantiation %qT",
1105 type, inst);
1106 }
1107
1108 /* Mark TYPE as a specialization. And as a result, we only
1109 have one level of template argument for the innermost
1110 class template. */
1111 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1112 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1113 CLASSTYPE_TI_ARGS (type)
1114 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1115 }
1116 }
1117 else if (processing_specialization)
1118 {
1119 /* Someday C++0x may allow for enum template specialization. */
1120 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1121 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1122 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1123 "of %qD not allowed by ISO C++", type);
1124 else
1125 {
1126 error ("explicit specialization of non-template %qT", type);
1127 return error_mark_node;
1128 }
1129 }
1130
1131 return type;
1132 }
1133
1134 /* Returns nonzero if we can optimize the retrieval of specializations
1135 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1136 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1137
1138 static inline bool
1139 optimize_specialization_lookup_p (tree tmpl)
1140 {
1141 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1142 && DECL_CLASS_SCOPE_P (tmpl)
1143 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1144 parameter. */
1145 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1146 /* The optimized lookup depends on the fact that the
1147 template arguments for the member function template apply
1148 purely to the containing class, which is not true if the
1149 containing class is an explicit or partial
1150 specialization. */
1151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1152 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1153 && !DECL_CONV_FN_P (tmpl)
1154 /* It is possible to have a template that is not a member
1155 template and is not a member of a template class:
1156
1157 template <typename T>
1158 struct S { friend A::f(); };
1159
1160 Here, the friend function is a template, but the context does
1161 not have template information. The optimized lookup relies
1162 on having ARGS be the template arguments for both the class
1163 and the function template. */
1164 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1165 }
1166
1167 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1168 gone through coerce_template_parms by now. */
1169
1170 static void
1171 verify_unstripped_args_1 (tree inner)
1172 {
1173 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1174 {
1175 tree arg = TREE_VEC_ELT (inner, i);
1176 if (TREE_CODE (arg) == TEMPLATE_DECL)
1177 /* OK */;
1178 else if (TYPE_P (arg))
1179 gcc_assert (strip_typedefs (arg, NULL) == arg);
1180 else if (ARGUMENT_PACK_P (arg))
1181 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1182 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1183 /* Allow typedefs on the type of a non-type argument, since a
1184 parameter can have them. */;
1185 else
1186 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1187 }
1188 }
1189
1190 static void
1191 verify_unstripped_args (tree args)
1192 {
1193 ++processing_template_decl;
1194 if (!any_dependent_template_arguments_p (args))
1195 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1196 --processing_template_decl;
1197 }
1198
1199 /* Retrieve the specialization (in the sense of [temp.spec] - a
1200 specialization is either an instantiation or an explicit
1201 specialization) of TMPL for the given template ARGS. If there is
1202 no such specialization, return NULL_TREE. The ARGS are a vector of
1203 arguments, or a vector of vectors of arguments, in the case of
1204 templates with more than one level of parameters.
1205
1206 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1207 then we search for a partial specialization matching ARGS. This
1208 parameter is ignored if TMPL is not a class template.
1209
1210 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1211 result is a NONTYPE_ARGUMENT_PACK. */
1212
1213 static tree
1214 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1215 {
1216 if (tmpl == NULL_TREE)
1217 return NULL_TREE;
1218
1219 if (args == error_mark_node)
1220 return NULL_TREE;
1221
1222 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1223 || TREE_CODE (tmpl) == FIELD_DECL);
1224
1225 /* There should be as many levels of arguments as there are
1226 levels of parameters. */
1227 gcc_assert (TMPL_ARGS_DEPTH (args)
1228 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1229 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1230 : template_class_depth (DECL_CONTEXT (tmpl))));
1231
1232 if (flag_checking)
1233 verify_unstripped_args (args);
1234
1235 /* Lambda functions in templates aren't instantiated normally, but through
1236 tsubst_lambda_expr. */
1237 if (lambda_fn_in_template_p (tmpl))
1238 return NULL_TREE;
1239
1240 if (optimize_specialization_lookup_p (tmpl))
1241 {
1242 /* The template arguments actually apply to the containing
1243 class. Find the class specialization with those
1244 arguments. */
1245 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1246 tree class_specialization
1247 = retrieve_specialization (class_template, args, 0);
1248 if (!class_specialization)
1249 return NULL_TREE;
1250
1251 /* Find the instance of TMPL. */
1252 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1253 for (ovl_iterator iter (fns); iter; ++iter)
1254 {
1255 tree fn = *iter;
1256 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1257 /* using-declarations can add base methods to the method vec,
1258 and we don't want those here. */
1259 && DECL_CONTEXT (fn) == class_specialization)
1260 return fn;
1261 }
1262 return NULL_TREE;
1263 }
1264 else
1265 {
1266 spec_entry *found;
1267 spec_entry elt;
1268 hash_table<spec_hasher> *specializations;
1269
1270 elt.tmpl = tmpl;
1271 elt.args = args;
1272 elt.spec = NULL_TREE;
1273
1274 if (DECL_CLASS_TEMPLATE_P (tmpl))
1275 specializations = type_specializations;
1276 else
1277 specializations = decl_specializations;
1278
1279 if (hash == 0)
1280 hash = spec_hasher::hash (&elt);
1281 found = specializations->find_with_hash (&elt, hash);
1282 if (found)
1283 return found->spec;
1284 }
1285
1286 return NULL_TREE;
1287 }
1288
1289 /* Like retrieve_specialization, but for local declarations. */
1290
1291 tree
1292 retrieve_local_specialization (tree tmpl)
1293 {
1294 if (local_specializations == NULL)
1295 return NULL_TREE;
1296
1297 tree *slot = local_specializations->get (tmpl);
1298 return slot ? *slot : NULL_TREE;
1299 }
1300
1301 /* Returns nonzero iff DECL is a specialization of TMPL. */
1302
1303 int
1304 is_specialization_of (tree decl, tree tmpl)
1305 {
1306 tree t;
1307
1308 if (TREE_CODE (decl) == FUNCTION_DECL)
1309 {
1310 for (t = decl;
1311 t != NULL_TREE;
1312 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1313 if (t == tmpl)
1314 return 1;
1315 }
1316 else
1317 {
1318 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1319
1320 for (t = TREE_TYPE (decl);
1321 t != NULL_TREE;
1322 t = CLASSTYPE_USE_TEMPLATE (t)
1323 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1324 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1325 return 1;
1326 }
1327
1328 return 0;
1329 }
1330
1331 /* Returns nonzero iff DECL is a specialization of friend declaration
1332 FRIEND_DECL according to [temp.friend]. */
1333
1334 bool
1335 is_specialization_of_friend (tree decl, tree friend_decl)
1336 {
1337 bool need_template = true;
1338 int template_depth;
1339
1340 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1341 || TREE_CODE (decl) == TYPE_DECL);
1342
1343 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1344 of a template class, we want to check if DECL is a specialization
1345 if this. */
1346 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1347 && DECL_TEMPLATE_INFO (friend_decl)
1348 && !DECL_USE_TEMPLATE (friend_decl))
1349 {
1350 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1351 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1352 need_template = false;
1353 }
1354 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1355 && !PRIMARY_TEMPLATE_P (friend_decl))
1356 need_template = false;
1357
1358 /* There is nothing to do if this is not a template friend. */
1359 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1360 return false;
1361
1362 if (is_specialization_of (decl, friend_decl))
1363 return true;
1364
1365 /* [temp.friend/6]
1366 A member of a class template may be declared to be a friend of a
1367 non-template class. In this case, the corresponding member of
1368 every specialization of the class template is a friend of the
1369 class granting friendship.
1370
1371 For example, given a template friend declaration
1372
1373 template <class T> friend void A<T>::f();
1374
1375 the member function below is considered a friend
1376
1377 template <> struct A<int> {
1378 void f();
1379 };
1380
1381 For this type of template friend, TEMPLATE_DEPTH below will be
1382 nonzero. To determine if DECL is a friend of FRIEND, we first
1383 check if the enclosing class is a specialization of another. */
1384
1385 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1386 if (template_depth
1387 && DECL_CLASS_SCOPE_P (decl)
1388 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1389 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1390 {
1391 /* Next, we check the members themselves. In order to handle
1392 a few tricky cases, such as when FRIEND_DECL's are
1393
1394 template <class T> friend void A<T>::g(T t);
1395 template <class T> template <T t> friend void A<T>::h();
1396
1397 and DECL's are
1398
1399 void A<int>::g(int);
1400 template <int> void A<int>::h();
1401
1402 we need to figure out ARGS, the template arguments from
1403 the context of DECL. This is required for template substitution
1404 of `T' in the function parameter of `g' and template parameter
1405 of `h' in the above examples. Here ARGS corresponds to `int'. */
1406
1407 tree context = DECL_CONTEXT (decl);
1408 tree args = NULL_TREE;
1409 int current_depth = 0;
1410
1411 while (current_depth < template_depth)
1412 {
1413 if (CLASSTYPE_TEMPLATE_INFO (context))
1414 {
1415 if (current_depth == 0)
1416 args = TYPE_TI_ARGS (context);
1417 else
1418 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1419 current_depth++;
1420 }
1421 context = TYPE_CONTEXT (context);
1422 }
1423
1424 if (TREE_CODE (decl) == FUNCTION_DECL)
1425 {
1426 bool is_template;
1427 tree friend_type;
1428 tree decl_type;
1429 tree friend_args_type;
1430 tree decl_args_type;
1431
1432 /* Make sure that both DECL and FRIEND_DECL are templates or
1433 non-templates. */
1434 is_template = DECL_TEMPLATE_INFO (decl)
1435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1436 if (need_template ^ is_template)
1437 return false;
1438 else if (is_template)
1439 {
1440 /* If both are templates, check template parameter list. */
1441 tree friend_parms
1442 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1443 args, tf_none);
1444 if (!comp_template_parms
1445 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1446 friend_parms))
1447 return false;
1448
1449 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1450 }
1451 else
1452 decl_type = TREE_TYPE (decl);
1453
1454 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1455 tf_none, NULL_TREE);
1456 if (friend_type == error_mark_node)
1457 return false;
1458
1459 /* Check if return types match. */
1460 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1461 return false;
1462
1463 /* Check if function parameter types match, ignoring the
1464 `this' parameter. */
1465 friend_args_type = TYPE_ARG_TYPES (friend_type);
1466 decl_args_type = TYPE_ARG_TYPES (decl_type);
1467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1468 friend_args_type = TREE_CHAIN (friend_args_type);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1470 decl_args_type = TREE_CHAIN (decl_args_type);
1471
1472 return compparms (decl_args_type, friend_args_type);
1473 }
1474 else
1475 {
1476 /* DECL is a TYPE_DECL */
1477 bool is_template;
1478 tree decl_type = TREE_TYPE (decl);
1479
1480 /* Make sure that both DECL and FRIEND_DECL are templates or
1481 non-templates. */
1482 is_template
1483 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1485
1486 if (need_template ^ is_template)
1487 return false;
1488 else if (is_template)
1489 {
1490 tree friend_parms;
1491 /* If both are templates, check the name of the two
1492 TEMPLATE_DECL's first because is_friend didn't. */
1493 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1494 != DECL_NAME (friend_decl))
1495 return false;
1496
1497 /* Now check template parameter list. */
1498 friend_parms
1499 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1500 args, tf_none);
1501 return comp_template_parms
1502 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1503 friend_parms);
1504 }
1505 else
1506 return (DECL_NAME (decl)
1507 == DECL_NAME (friend_decl));
1508 }
1509 }
1510 return false;
1511 }
1512
1513 /* Register the specialization SPEC as a specialization of TMPL with
1514 the indicated ARGS. IS_FRIEND indicates whether the specialization
1515 is actually just a friend declaration. ATTRLIST is the list of
1516 attributes that the specialization is declared with or NULL when
1517 it isn't. Returns SPEC, or an equivalent prior declaration, if
1518 available.
1519
1520 We also store instantiations of field packs in the hash table, even
1521 though they are not themselves templates, to make lookup easier. */
1522
1523 static tree
1524 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1525 hashval_t hash)
1526 {
1527 tree fn;
1528 spec_entry **slot = NULL;
1529 spec_entry elt;
1530
1531 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1532 || (TREE_CODE (tmpl) == FIELD_DECL
1533 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1534
1535 if (TREE_CODE (spec) == FUNCTION_DECL
1536 && uses_template_parms (DECL_TI_ARGS (spec)))
1537 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1538 register it; we want the corresponding TEMPLATE_DECL instead.
1539 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1540 the more obvious `uses_template_parms (spec)' to avoid problems
1541 with default function arguments. In particular, given
1542 something like this:
1543
1544 template <class T> void f(T t1, T t = T())
1545
1546 the default argument expression is not substituted for in an
1547 instantiation unless and until it is actually needed. */
1548 return spec;
1549
1550 if (optimize_specialization_lookup_p (tmpl))
1551 /* We don't put these specializations in the hash table, but we might
1552 want to give an error about a mismatch. */
1553 fn = retrieve_specialization (tmpl, args, 0);
1554 else
1555 {
1556 elt.tmpl = tmpl;
1557 elt.args = args;
1558 elt.spec = spec;
1559
1560 if (hash == 0)
1561 hash = spec_hasher::hash (&elt);
1562
1563 slot =
1564 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1565 if (*slot)
1566 fn = ((spec_entry *) *slot)->spec;
1567 else
1568 fn = NULL_TREE;
1569 }
1570
1571 /* We can sometimes try to re-register a specialization that we've
1572 already got. In particular, regenerate_decl_from_template calls
1573 duplicate_decls which will update the specialization list. But,
1574 we'll still get called again here anyhow. It's more convenient
1575 to simply allow this than to try to prevent it. */
1576 if (fn == spec)
1577 return spec;
1578 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1579 {
1580 if (DECL_TEMPLATE_INSTANTIATION (fn))
1581 {
1582 if (DECL_ODR_USED (fn)
1583 || DECL_EXPLICIT_INSTANTIATION (fn))
1584 {
1585 error ("specialization of %qD after instantiation",
1586 fn);
1587 return error_mark_node;
1588 }
1589 else
1590 {
1591 tree clone;
1592 /* This situation should occur only if the first
1593 specialization is an implicit instantiation, the
1594 second is an explicit specialization, and the
1595 implicit instantiation has not yet been used. That
1596 situation can occur if we have implicitly
1597 instantiated a member function and then specialized
1598 it later.
1599
1600 We can also wind up here if a friend declaration that
1601 looked like an instantiation turns out to be a
1602 specialization:
1603
1604 template <class T> void foo(T);
1605 class S { friend void foo<>(int) };
1606 template <> void foo(int);
1607
1608 We transform the existing DECL in place so that any
1609 pointers to it become pointers to the updated
1610 declaration.
1611
1612 If there was a definition for the template, but not
1613 for the specialization, we want this to look as if
1614 there were no definition, and vice versa. */
1615 DECL_INITIAL (fn) = NULL_TREE;
1616 duplicate_decls (spec, fn, is_friend);
1617 /* The call to duplicate_decls will have applied
1618 [temp.expl.spec]:
1619
1620 An explicit specialization of a function template
1621 is inline only if it is explicitly declared to be,
1622 and independently of whether its function template
1623 is.
1624
1625 to the primary function; now copy the inline bits to
1626 the various clones. */
1627 FOR_EACH_CLONE (clone, fn)
1628 {
1629 DECL_DECLARED_INLINE_P (clone)
1630 = DECL_DECLARED_INLINE_P (fn);
1631 DECL_SOURCE_LOCATION (clone)
1632 = DECL_SOURCE_LOCATION (fn);
1633 DECL_DELETED_FN (clone)
1634 = DECL_DELETED_FN (fn);
1635 }
1636 check_specialization_namespace (tmpl);
1637
1638 return fn;
1639 }
1640 }
1641 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1642 {
1643 tree dd = duplicate_decls (spec, fn, is_friend);
1644 if (dd == error_mark_node)
1645 /* We've already complained in duplicate_decls. */
1646 return error_mark_node;
1647
1648 if (dd == NULL_TREE && DECL_INITIAL (spec))
1649 /* Dup decl failed, but this is a new definition. Set the
1650 line number so any errors match this new
1651 definition. */
1652 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1653
1654 return fn;
1655 }
1656 }
1657 else if (fn)
1658 return duplicate_decls (spec, fn, is_friend);
1659
1660 /* A specialization must be declared in the same namespace as the
1661 template it is specializing. */
1662 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1663 && !check_specialization_namespace (tmpl))
1664 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1665
1666 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1667 {
1668 spec_entry *entry = ggc_alloc<spec_entry> ();
1669 gcc_assert (tmpl && args && spec);
1670 *entry = elt;
1671 *slot = entry;
1672 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1673 && PRIMARY_TEMPLATE_P (tmpl)
1674 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1675 || variable_template_p (tmpl))
1676 /* If TMPL is a forward declaration of a template function, keep a list
1677 of all specializations in case we need to reassign them to a friend
1678 template later in tsubst_friend_function.
1679
1680 Also keep a list of all variable template instantiations so that
1681 process_partial_specialization can check whether a later partial
1682 specialization would have used it. */
1683 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1684 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1685 }
1686
1687 return spec;
1688 }
1689
1690 /* Returns true iff two spec_entry nodes are equivalent. */
1691
1692 int comparing_specializations;
1693
1694 bool
1695 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1696 {
1697 int equal;
1698
1699 ++comparing_specializations;
1700 equal = (e1->tmpl == e2->tmpl
1701 && comp_template_args (e1->args, e2->args));
1702 if (equal && flag_concepts
1703 /* tmpl could be a FIELD_DECL for a capture pack. */
1704 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1705 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1706 && uses_template_parms (e1->args))
1707 {
1708 /* Partial specializations of a variable template can be distinguished by
1709 constraints. */
1710 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1711 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1712 equal = equivalent_constraints (c1, c2);
1713 }
1714 --comparing_specializations;
1715
1716 return equal;
1717 }
1718
1719 /* Returns a hash for a template TMPL and template arguments ARGS. */
1720
1721 static hashval_t
1722 hash_tmpl_and_args (tree tmpl, tree args)
1723 {
1724 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1725 return iterative_hash_template_arg (args, val);
1726 }
1727
1728 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1729 ignoring SPEC. */
1730
1731 hashval_t
1732 spec_hasher::hash (spec_entry *e)
1733 {
1734 return hash_tmpl_and_args (e->tmpl, e->args);
1735 }
1736
1737 /* Recursively calculate a hash value for a template argument ARG, for use
1738 in the hash tables of template specializations. */
1739
1740 hashval_t
1741 iterative_hash_template_arg (tree arg, hashval_t val)
1742 {
1743 unsigned HOST_WIDE_INT i;
1744 enum tree_code code;
1745 char tclass;
1746
1747 if (arg == NULL_TREE)
1748 return iterative_hash_object (arg, val);
1749
1750 if (!TYPE_P (arg))
1751 STRIP_NOPS (arg);
1752
1753 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1754 gcc_unreachable ();
1755
1756 code = TREE_CODE (arg);
1757 tclass = TREE_CODE_CLASS (code);
1758
1759 val = iterative_hash_object (code, val);
1760
1761 switch (code)
1762 {
1763 case ERROR_MARK:
1764 return val;
1765
1766 case IDENTIFIER_NODE:
1767 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1768
1769 case TREE_VEC:
1770 {
1771 int i, len = TREE_VEC_LENGTH (arg);
1772 for (i = 0; i < len; ++i)
1773 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1774 return val;
1775 }
1776
1777 case TYPE_PACK_EXPANSION:
1778 case EXPR_PACK_EXPANSION:
1779 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1780 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1781
1782 case TYPE_ARGUMENT_PACK:
1783 case NONTYPE_ARGUMENT_PACK:
1784 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1785
1786 case TREE_LIST:
1787 for (; arg; arg = TREE_CHAIN (arg))
1788 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1789 return val;
1790
1791 case OVERLOAD:
1792 for (lkp_iterator iter (arg); iter; ++iter)
1793 val = iterative_hash_template_arg (*iter, val);
1794 return val;
1795
1796 case CONSTRUCTOR:
1797 {
1798 tree field, value;
1799 iterative_hash_template_arg (TREE_TYPE (arg), val);
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1801 {
1802 val = iterative_hash_template_arg (field, val);
1803 val = iterative_hash_template_arg (value, val);
1804 }
1805 return val;
1806 }
1807
1808 case PARM_DECL:
1809 if (!DECL_ARTIFICIAL (arg))
1810 {
1811 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1812 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1813 }
1814 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1815
1816 case TARGET_EXPR:
1817 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1818
1819 case PTRMEM_CST:
1820 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1821 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1822
1823 case TEMPLATE_PARM_INDEX:
1824 val = iterative_hash_template_arg
1825 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1826 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1827 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1828
1829 case TRAIT_EXPR:
1830 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1831 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1832 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1833
1834 case BASELINK:
1835 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1836 val);
1837 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1838 val);
1839
1840 case MODOP_EXPR:
1841 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1842 code = TREE_CODE (TREE_OPERAND (arg, 1));
1843 val = iterative_hash_object (code, val);
1844 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1845
1846 case LAMBDA_EXPR:
1847 /* [temp.over.link] Two lambda-expressions are never considered
1848 equivalent.
1849
1850 So just hash the closure type. */
1851 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1852
1853 case CAST_EXPR:
1854 case IMPLICIT_CONV_EXPR:
1855 case STATIC_CAST_EXPR:
1856 case REINTERPRET_CAST_EXPR:
1857 case CONST_CAST_EXPR:
1858 case DYNAMIC_CAST_EXPR:
1859 case NEW_EXPR:
1860 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1861 /* Now hash operands as usual. */
1862 break;
1863
1864 default:
1865 break;
1866 }
1867
1868 switch (tclass)
1869 {
1870 case tcc_type:
1871 if (alias_template_specialization_p (arg))
1872 {
1873 // We want an alias specialization that survived strip_typedefs
1874 // to hash differently from its TYPE_CANONICAL, to avoid hash
1875 // collisions that compare as different in template_args_equal.
1876 // These could be dependent specializations that strip_typedefs
1877 // left alone, or untouched specializations because
1878 // coerce_template_parms returns the unconverted template
1879 // arguments if it sees incomplete argument packs.
1880 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1881 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1882 }
1883 if (TYPE_CANONICAL (arg))
1884 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1885 val);
1886 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1887 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1888 /* Otherwise just compare the types during lookup. */
1889 return val;
1890
1891 case tcc_declaration:
1892 case tcc_constant:
1893 return iterative_hash_expr (arg, val);
1894
1895 default:
1896 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1897 {
1898 unsigned n = cp_tree_operand_length (arg);
1899 for (i = 0; i < n; ++i)
1900 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1901 return val;
1902 }
1903 }
1904 gcc_unreachable ();
1905 return 0;
1906 }
1907
1908 /* Unregister the specialization SPEC as a specialization of TMPL.
1909 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1910 if the SPEC was listed as a specialization of TMPL.
1911
1912 Note that SPEC has been ggc_freed, so we can't look inside it. */
1913
1914 bool
1915 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1916 {
1917 spec_entry *entry;
1918 spec_entry elt;
1919
1920 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1921 elt.args = TI_ARGS (tinfo);
1922 elt.spec = NULL_TREE;
1923
1924 entry = decl_specializations->find (&elt);
1925 if (entry != NULL)
1926 {
1927 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1928 gcc_assert (new_spec != NULL_TREE);
1929 entry->spec = new_spec;
1930 return 1;
1931 }
1932
1933 return 0;
1934 }
1935
1936 /* Like register_specialization, but for local declarations. We are
1937 registering SPEC, an instantiation of TMPL. */
1938
1939 void
1940 register_local_specialization (tree spec, tree tmpl)
1941 {
1942 gcc_assert (tmpl != spec);
1943 local_specializations->put (tmpl, spec);
1944 }
1945
1946 /* TYPE is a class type. Returns true if TYPE is an explicitly
1947 specialized class. */
1948
1949 bool
1950 explicit_class_specialization_p (tree type)
1951 {
1952 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1953 return false;
1954 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1955 }
1956
1957 /* Print the list of functions at FNS, going through all the overloads
1958 for each element of the list. Alternatively, FNS cannot be a
1959 TREE_LIST, in which case it will be printed together with all the
1960 overloads.
1961
1962 MORE and *STR should respectively be FALSE and NULL when the function
1963 is called from the outside. They are used internally on recursive
1964 calls. print_candidates manages the two parameters and leaves NULL
1965 in *STR when it ends. */
1966
1967 static void
1968 print_candidates_1 (tree fns, char **str, bool more = false)
1969 {
1970 if (TREE_CODE (fns) == TREE_LIST)
1971 for (; fns; fns = TREE_CHAIN (fns))
1972 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1973 else
1974 for (lkp_iterator iter (fns); iter;)
1975 {
1976 tree cand = *iter;
1977 ++iter;
1978
1979 const char *pfx = *str;
1980 if (!pfx)
1981 {
1982 if (more || iter)
1983 pfx = _("candidates are:");
1984 else
1985 pfx = _("candidate is:");
1986 *str = get_spaces (pfx);
1987 }
1988 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1989 }
1990 }
1991
1992 /* Print the list of candidate FNS in an error message. FNS can also
1993 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1994
1995 void
1996 print_candidates (tree fns)
1997 {
1998 char *str = NULL;
1999 print_candidates_1 (fns, &str);
2000 free (str);
2001 }
2002
2003 /* Get a (possibly) constrained template declaration for the
2004 purpose of ordering candidates. */
2005 static tree
2006 get_template_for_ordering (tree list)
2007 {
2008 gcc_assert (TREE_CODE (list) == TREE_LIST);
2009 tree f = TREE_VALUE (list);
2010 if (tree ti = DECL_TEMPLATE_INFO (f))
2011 return TI_TEMPLATE (ti);
2012 return f;
2013 }
2014
2015 /* Among candidates having the same signature, return the
2016 most constrained or NULL_TREE if there is no best candidate.
2017 If the signatures of candidates vary (e.g., template
2018 specialization vs. member function), then there can be no
2019 most constrained.
2020
2021 Note that we don't compare constraints on the functions
2022 themselves, but rather those of their templates. */
2023 static tree
2024 most_constrained_function (tree candidates)
2025 {
2026 // Try to find the best candidate in a first pass.
2027 tree champ = candidates;
2028 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2029 {
2030 int winner = more_constrained (get_template_for_ordering (champ),
2031 get_template_for_ordering (c));
2032 if (winner == -1)
2033 champ = c; // The candidate is more constrained
2034 else if (winner == 0)
2035 return NULL_TREE; // Neither is more constrained
2036 }
2037
2038 // Verify that the champ is better than previous candidates.
2039 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2040 if (!more_constrained (get_template_for_ordering (champ),
2041 get_template_for_ordering (c)))
2042 return NULL_TREE;
2043 }
2044
2045 return champ;
2046 }
2047
2048
2049 /* Returns the template (one of the functions given by TEMPLATE_ID)
2050 which can be specialized to match the indicated DECL with the
2051 explicit template args given in TEMPLATE_ID. The DECL may be
2052 NULL_TREE if none is available. In that case, the functions in
2053 TEMPLATE_ID are non-members.
2054
2055 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2056 specialization of a member template.
2057
2058 The TEMPLATE_COUNT is the number of references to qualifying
2059 template classes that appeared in the name of the function. See
2060 check_explicit_specialization for a more accurate description.
2061
2062 TSK indicates what kind of template declaration (if any) is being
2063 declared. TSK_TEMPLATE indicates that the declaration given by
2064 DECL, though a FUNCTION_DECL, has template parameters, and is
2065 therefore a template function.
2066
2067 The template args (those explicitly specified and those deduced)
2068 are output in a newly created vector *TARGS_OUT.
2069
2070 If it is impossible to determine the result, an error message is
2071 issued. The error_mark_node is returned to indicate failure. */
2072
2073 static tree
2074 determine_specialization (tree template_id,
2075 tree decl,
2076 tree* targs_out,
2077 int need_member_template,
2078 int template_count,
2079 tmpl_spec_kind tsk)
2080 {
2081 tree fns;
2082 tree targs;
2083 tree explicit_targs;
2084 tree candidates = NULL_TREE;
2085
2086 /* A TREE_LIST of templates of which DECL may be a specialization.
2087 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2088 corresponding TREE_PURPOSE is the set of template arguments that,
2089 when used to instantiate the template, would produce a function
2090 with the signature of DECL. */
2091 tree templates = NULL_TREE;
2092 int header_count;
2093 cp_binding_level *b;
2094
2095 *targs_out = NULL_TREE;
2096
2097 if (template_id == error_mark_node || decl == error_mark_node)
2098 return error_mark_node;
2099
2100 /* We shouldn't be specializing a member template of an
2101 unspecialized class template; we already gave an error in
2102 check_specialization_scope, now avoid crashing. */
2103 if (!VAR_P (decl)
2104 && template_count && DECL_CLASS_SCOPE_P (decl)
2105 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2106 {
2107 gcc_assert (errorcount);
2108 return error_mark_node;
2109 }
2110
2111 fns = TREE_OPERAND (template_id, 0);
2112 explicit_targs = TREE_OPERAND (template_id, 1);
2113
2114 if (fns == error_mark_node)
2115 return error_mark_node;
2116
2117 /* Check for baselinks. */
2118 if (BASELINK_P (fns))
2119 fns = BASELINK_FUNCTIONS (fns);
2120
2121 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2122 {
2123 error ("%qD is not a function template", fns);
2124 return error_mark_node;
2125 }
2126 else if (VAR_P (decl) && !variable_template_p (fns))
2127 {
2128 error ("%qD is not a variable template", fns);
2129 return error_mark_node;
2130 }
2131
2132 /* Count the number of template headers specified for this
2133 specialization. */
2134 header_count = 0;
2135 for (b = current_binding_level;
2136 b->kind == sk_template_parms;
2137 b = b->level_chain)
2138 ++header_count;
2139
2140 tree orig_fns = fns;
2141
2142 if (variable_template_p (fns))
2143 {
2144 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2145 targs = coerce_template_parms (parms, explicit_targs, fns,
2146 tf_warning_or_error,
2147 /*req_all*/true, /*use_defarg*/true);
2148 if (targs != error_mark_node)
2149 templates = tree_cons (targs, fns, templates);
2150 }
2151 else for (lkp_iterator iter (fns); iter; ++iter)
2152 {
2153 tree fn = *iter;
2154
2155 if (TREE_CODE (fn) == TEMPLATE_DECL)
2156 {
2157 tree decl_arg_types;
2158 tree fn_arg_types;
2159 tree insttype;
2160
2161 /* In case of explicit specialization, we need to check if
2162 the number of template headers appearing in the specialization
2163 is correct. This is usually done in check_explicit_specialization,
2164 but the check done there cannot be exhaustive when specializing
2165 member functions. Consider the following code:
2166
2167 template <> void A<int>::f(int);
2168 template <> template <> void A<int>::f(int);
2169
2170 Assuming that A<int> is not itself an explicit specialization
2171 already, the first line specializes "f" which is a non-template
2172 member function, whilst the second line specializes "f" which
2173 is a template member function. So both lines are syntactically
2174 correct, and check_explicit_specialization does not reject
2175 them.
2176
2177 Here, we can do better, as we are matching the specialization
2178 against the declarations. We count the number of template
2179 headers, and we check if they match TEMPLATE_COUNT + 1
2180 (TEMPLATE_COUNT is the number of qualifying template classes,
2181 plus there must be another header for the member template
2182 itself).
2183
2184 Notice that if header_count is zero, this is not a
2185 specialization but rather a template instantiation, so there
2186 is no check we can perform here. */
2187 if (header_count && header_count != template_count + 1)
2188 continue;
2189
2190 /* Check that the number of template arguments at the
2191 innermost level for DECL is the same as for FN. */
2192 if (current_binding_level->kind == sk_template_parms
2193 && !current_binding_level->explicit_spec_p
2194 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2195 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2196 (current_template_parms))))
2197 continue;
2198
2199 /* DECL might be a specialization of FN. */
2200 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2201 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2202
2203 /* For a non-static member function, we need to make sure
2204 that the const qualification is the same. Since
2205 get_bindings does not try to merge the "this" parameter,
2206 we must do the comparison explicitly. */
2207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2208 {
2209 if (!same_type_p (TREE_VALUE (fn_arg_types),
2210 TREE_VALUE (decl_arg_types)))
2211 continue;
2212
2213 /* And the ref-qualification. */
2214 if (type_memfn_rqual (TREE_TYPE (decl))
2215 != type_memfn_rqual (TREE_TYPE (fn)))
2216 continue;
2217 }
2218
2219 /* Skip the "this" parameter and, for constructors of
2220 classes with virtual bases, the VTT parameter. A
2221 full specialization of a constructor will have a VTT
2222 parameter, but a template never will. */
2223 decl_arg_types
2224 = skip_artificial_parms_for (decl, decl_arg_types);
2225 fn_arg_types
2226 = skip_artificial_parms_for (fn, fn_arg_types);
2227
2228 /* Function templates cannot be specializations; there are
2229 no partial specializations of functions. Therefore, if
2230 the type of DECL does not match FN, there is no
2231 match.
2232
2233 Note that it should never be the case that we have both
2234 candidates added here, and for regular member functions
2235 below. */
2236 if (tsk == tsk_template)
2237 {
2238 if (compparms (fn_arg_types, decl_arg_types))
2239 candidates = tree_cons (NULL_TREE, fn, candidates);
2240 continue;
2241 }
2242
2243 /* See whether this function might be a specialization of this
2244 template. Suppress access control because we might be trying
2245 to make this specialization a friend, and we have already done
2246 access control for the declaration of the specialization. */
2247 push_deferring_access_checks (dk_no_check);
2248 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2249 pop_deferring_access_checks ();
2250
2251 if (!targs)
2252 /* We cannot deduce template arguments that when used to
2253 specialize TMPL will produce DECL. */
2254 continue;
2255
2256 if (uses_template_parms (targs))
2257 /* We deduced something involving 'auto', which isn't a valid
2258 template argument. */
2259 continue;
2260
2261 /* Remove, from the set of candidates, all those functions
2262 whose constraints are not satisfied. */
2263 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2264 continue;
2265
2266 // Then, try to form the new function type.
2267 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2268 if (insttype == error_mark_node)
2269 continue;
2270 fn_arg_types
2271 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2272 if (!compparms (fn_arg_types, decl_arg_types))
2273 continue;
2274
2275 /* Save this template, and the arguments deduced. */
2276 templates = tree_cons (targs, fn, templates);
2277 }
2278 else if (need_member_template)
2279 /* FN is an ordinary member function, and we need a
2280 specialization of a member template. */
2281 ;
2282 else if (TREE_CODE (fn) != FUNCTION_DECL)
2283 /* We can get IDENTIFIER_NODEs here in certain erroneous
2284 cases. */
2285 ;
2286 else if (!DECL_FUNCTION_MEMBER_P (fn))
2287 /* This is just an ordinary non-member function. Nothing can
2288 be a specialization of that. */
2289 ;
2290 else if (DECL_ARTIFICIAL (fn))
2291 /* Cannot specialize functions that are created implicitly. */
2292 ;
2293 else
2294 {
2295 tree decl_arg_types;
2296
2297 /* This is an ordinary member function. However, since
2298 we're here, we can assume its enclosing class is a
2299 template class. For example,
2300
2301 template <typename T> struct S { void f(); };
2302 template <> void S<int>::f() {}
2303
2304 Here, S<int>::f is a non-template, but S<int> is a
2305 template class. If FN has the same type as DECL, we
2306 might be in business. */
2307
2308 if (!DECL_TEMPLATE_INFO (fn))
2309 /* Its enclosing class is an explicit specialization
2310 of a template class. This is not a candidate. */
2311 continue;
2312
2313 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2314 TREE_TYPE (TREE_TYPE (fn))))
2315 /* The return types differ. */
2316 continue;
2317
2318 /* Adjust the type of DECL in case FN is a static member. */
2319 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2320 if (DECL_STATIC_FUNCTION_P (fn)
2321 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2322 decl_arg_types = TREE_CHAIN (decl_arg_types);
2323
2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2325 decl_arg_types))
2326 continue;
2327
2328 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2329 && (type_memfn_rqual (TREE_TYPE (decl))
2330 != type_memfn_rqual (TREE_TYPE (fn))))
2331 continue;
2332
2333 // If the deduced arguments do not satisfy the constraints,
2334 // this is not a candidate.
2335 if (flag_concepts && !constraints_satisfied_p (fn))
2336 continue;
2337
2338 // Add the candidate.
2339 candidates = tree_cons (NULL_TREE, fn, candidates);
2340 }
2341 }
2342
2343 if (templates && TREE_CHAIN (templates))
2344 {
2345 /* We have:
2346
2347 [temp.expl.spec]
2348
2349 It is possible for a specialization with a given function
2350 signature to be instantiated from more than one function
2351 template. In such cases, explicit specification of the
2352 template arguments must be used to uniquely identify the
2353 function template specialization being specialized.
2354
2355 Note that here, there's no suggestion that we're supposed to
2356 determine which of the candidate templates is most
2357 specialized. However, we, also have:
2358
2359 [temp.func.order]
2360
2361 Partial ordering of overloaded function template
2362 declarations is used in the following contexts to select
2363 the function template to which a function template
2364 specialization refers:
2365
2366 -- when an explicit specialization refers to a function
2367 template.
2368
2369 So, we do use the partial ordering rules, at least for now.
2370 This extension can only serve to make invalid programs valid,
2371 so it's safe. And, there is strong anecdotal evidence that
2372 the committee intended the partial ordering rules to apply;
2373 the EDG front end has that behavior, and John Spicer claims
2374 that the committee simply forgot to delete the wording in
2375 [temp.expl.spec]. */
2376 tree tmpl = most_specialized_instantiation (templates);
2377 if (tmpl != error_mark_node)
2378 {
2379 templates = tmpl;
2380 TREE_CHAIN (templates) = NULL_TREE;
2381 }
2382 }
2383
2384 // Concepts allows multiple declarations of member functions
2385 // with the same signature. Like above, we need to rely on
2386 // on the partial ordering of those candidates to determine which
2387 // is the best.
2388 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2389 {
2390 if (tree cand = most_constrained_function (candidates))
2391 {
2392 candidates = cand;
2393 TREE_CHAIN (cand) = NULL_TREE;
2394 }
2395 }
2396
2397 if (templates == NULL_TREE && candidates == NULL_TREE)
2398 {
2399 error ("template-id %qD for %q+D does not match any template "
2400 "declaration", template_id, decl);
2401 if (header_count && header_count != template_count + 1)
2402 inform (input_location, "saw %d %<template<>%>, need %d for "
2403 "specializing a member function template",
2404 header_count, template_count + 1);
2405 else
2406 print_candidates (orig_fns);
2407 return error_mark_node;
2408 }
2409 else if ((templates && TREE_CHAIN (templates))
2410 || (candidates && TREE_CHAIN (candidates))
2411 || (templates && candidates))
2412 {
2413 error ("ambiguous template specialization %qD for %q+D",
2414 template_id, decl);
2415 candidates = chainon (candidates, templates);
2416 print_candidates (candidates);
2417 return error_mark_node;
2418 }
2419
2420 /* We have one, and exactly one, match. */
2421 if (candidates)
2422 {
2423 tree fn = TREE_VALUE (candidates);
2424 *targs_out = copy_node (DECL_TI_ARGS (fn));
2425
2426 // Propagate the candidate's constraints to the declaration.
2427 set_constraints (decl, get_constraints (fn));
2428
2429 /* DECL is a re-declaration or partial instantiation of a template
2430 function. */
2431 if (TREE_CODE (fn) == TEMPLATE_DECL)
2432 return fn;
2433 /* It was a specialization of an ordinary member function in a
2434 template class. */
2435 return DECL_TI_TEMPLATE (fn);
2436 }
2437
2438 /* It was a specialization of a template. */
2439 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2440 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2441 {
2442 *targs_out = copy_node (targs);
2443 SET_TMPL_ARGS_LEVEL (*targs_out,
2444 TMPL_ARGS_DEPTH (*targs_out),
2445 TREE_PURPOSE (templates));
2446 }
2447 else
2448 *targs_out = TREE_PURPOSE (templates);
2449 return TREE_VALUE (templates);
2450 }
2451
2452 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2453 but with the default argument values filled in from those in the
2454 TMPL_TYPES. */
2455
2456 static tree
2457 copy_default_args_to_explicit_spec_1 (tree spec_types,
2458 tree tmpl_types)
2459 {
2460 tree new_spec_types;
2461
2462 if (!spec_types)
2463 return NULL_TREE;
2464
2465 if (spec_types == void_list_node)
2466 return void_list_node;
2467
2468 /* Substitute into the rest of the list. */
2469 new_spec_types =
2470 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2471 TREE_CHAIN (tmpl_types));
2472
2473 /* Add the default argument for this parameter. */
2474 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2475 TREE_VALUE (spec_types),
2476 new_spec_types);
2477 }
2478
2479 /* DECL is an explicit specialization. Replicate default arguments
2480 from the template it specializes. (That way, code like:
2481
2482 template <class T> void f(T = 3);
2483 template <> void f(double);
2484 void g () { f (); }
2485
2486 works, as required.) An alternative approach would be to look up
2487 the correct default arguments at the call-site, but this approach
2488 is consistent with how implicit instantiations are handled. */
2489
2490 static void
2491 copy_default_args_to_explicit_spec (tree decl)
2492 {
2493 tree tmpl;
2494 tree spec_types;
2495 tree tmpl_types;
2496 tree new_spec_types;
2497 tree old_type;
2498 tree new_type;
2499 tree t;
2500 tree object_type = NULL_TREE;
2501 tree in_charge = NULL_TREE;
2502 tree vtt = NULL_TREE;
2503
2504 /* See if there's anything we need to do. */
2505 tmpl = DECL_TI_TEMPLATE (decl);
2506 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2507 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2508 if (TREE_PURPOSE (t))
2509 break;
2510 if (!t)
2511 return;
2512
2513 old_type = TREE_TYPE (decl);
2514 spec_types = TYPE_ARG_TYPES (old_type);
2515
2516 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2517 {
2518 /* Remove the this pointer, but remember the object's type for
2519 CV quals. */
2520 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2521 spec_types = TREE_CHAIN (spec_types);
2522 tmpl_types = TREE_CHAIN (tmpl_types);
2523
2524 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2525 {
2526 /* DECL may contain more parameters than TMPL due to the extra
2527 in-charge parameter in constructors and destructors. */
2528 in_charge = spec_types;
2529 spec_types = TREE_CHAIN (spec_types);
2530 }
2531 if (DECL_HAS_VTT_PARM_P (decl))
2532 {
2533 vtt = spec_types;
2534 spec_types = TREE_CHAIN (spec_types);
2535 }
2536 }
2537
2538 /* Compute the merged default arguments. */
2539 new_spec_types =
2540 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2541
2542 /* Compute the new FUNCTION_TYPE. */
2543 if (object_type)
2544 {
2545 if (vtt)
2546 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2547 TREE_VALUE (vtt),
2548 new_spec_types);
2549
2550 if (in_charge)
2551 /* Put the in-charge parameter back. */
2552 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2553 TREE_VALUE (in_charge),
2554 new_spec_types);
2555
2556 new_type = build_method_type_directly (object_type,
2557 TREE_TYPE (old_type),
2558 new_spec_types);
2559 }
2560 else
2561 new_type = build_function_type (TREE_TYPE (old_type),
2562 new_spec_types);
2563 new_type = cp_build_type_attribute_variant (new_type,
2564 TYPE_ATTRIBUTES (old_type));
2565 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2566
2567 TREE_TYPE (decl) = new_type;
2568 }
2569
2570 /* Return the number of template headers we expect to see for a definition
2571 or specialization of CTYPE or one of its non-template members. */
2572
2573 int
2574 num_template_headers_for_class (tree ctype)
2575 {
2576 int num_templates = 0;
2577
2578 while (ctype && CLASS_TYPE_P (ctype))
2579 {
2580 /* You're supposed to have one `template <...>' for every
2581 template class, but you don't need one for a full
2582 specialization. For example:
2583
2584 template <class T> struct S{};
2585 template <> struct S<int> { void f(); };
2586 void S<int>::f () {}
2587
2588 is correct; there shouldn't be a `template <>' for the
2589 definition of `S<int>::f'. */
2590 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2591 /* If CTYPE does not have template information of any
2592 kind, then it is not a template, nor is it nested
2593 within a template. */
2594 break;
2595 if (explicit_class_specialization_p (ctype))
2596 break;
2597 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2598 ++num_templates;
2599
2600 ctype = TYPE_CONTEXT (ctype);
2601 }
2602
2603 return num_templates;
2604 }
2605
2606 /* Do a simple sanity check on the template headers that precede the
2607 variable declaration DECL. */
2608
2609 void
2610 check_template_variable (tree decl)
2611 {
2612 tree ctx = CP_DECL_CONTEXT (decl);
2613 int wanted = num_template_headers_for_class (ctx);
2614 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2615 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2616 {
2617 if (cxx_dialect < cxx14)
2618 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2619 "variable templates only available with "
2620 "-std=c++14 or -std=gnu++14");
2621
2622 // Namespace-scope variable templates should have a template header.
2623 ++wanted;
2624 }
2625 if (template_header_count > wanted)
2626 {
2627 auto_diagnostic_group d;
2628 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2629 "too many template headers for %qD "
2630 "(should be %d)",
2631 decl, wanted);
2632 if (warned && CLASS_TYPE_P (ctx)
2633 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2634 inform (DECL_SOURCE_LOCATION (decl),
2635 "members of an explicitly specialized class are defined "
2636 "without a template header");
2637 }
2638 }
2639
2640 /* An explicit specialization whose declarator-id or class-head-name is not
2641 qualified shall be declared in the nearest enclosing namespace of the
2642 template, or, if the namespace is inline (7.3.1), any namespace from its
2643 enclosing namespace set.
2644
2645 If the name declared in the explicit instantiation is an unqualified name,
2646 the explicit instantiation shall appear in the namespace where its template
2647 is declared or, if that namespace is inline (7.3.1), any namespace from its
2648 enclosing namespace set. */
2649
2650 void
2651 check_unqualified_spec_or_inst (tree t, location_t loc)
2652 {
2653 tree tmpl = most_general_template (t);
2654 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2655 && !is_nested_namespace (current_namespace,
2656 CP_DECL_CONTEXT (tmpl), true))
2657 {
2658 if (processing_specialization)
2659 permerror (loc, "explicit specialization of %qD outside its "
2660 "namespace must use a nested-name-specifier", tmpl);
2661 else if (processing_explicit_instantiation
2662 && cxx_dialect >= cxx11)
2663 /* This was allowed in C++98, so only pedwarn. */
2664 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2665 "outside its namespace must use a nested-name-"
2666 "specifier", tmpl);
2667 }
2668 }
2669
2670 /* Warn for a template specialization SPEC that is missing some of a set
2671 of function or type attributes that the template TEMPL is declared with.
2672 ATTRLIST is a list of additional attributes that SPEC should be taken
2673 to ultimately be declared with. */
2674
2675 static void
2676 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2677 {
2678 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2679 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2680
2681 /* Avoid warning if the difference between the primary and
2682 the specialization is not in one of the attributes below. */
2683 const char* const blacklist[] = {
2684 "alloc_align", "alloc_size", "assume_aligned", "format",
2685 "format_arg", "malloc", "nonnull", NULL
2686 };
2687
2688 /* Put together a list of the black listed attributes that the primary
2689 template is declared with that the specialization is not, in case
2690 it's not apparent from the most recent declaration of the primary. */
2691 pretty_printer str;
2692 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2693 blacklist, &str);
2694
2695 if (!nattrs)
2696 return;
2697
2698 auto_diagnostic_group d;
2699 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2700 "explicit specialization %q#D may be missing attributes",
2701 spec))
2702 inform (DECL_SOURCE_LOCATION (tmpl),
2703 nattrs > 1
2704 ? G_("missing primary template attributes %s")
2705 : G_("missing primary template attribute %s"),
2706 pp_formatted_text (&str));
2707 }
2708
2709 /* Check to see if the function just declared, as indicated in
2710 DECLARATOR, and in DECL, is a specialization of a function
2711 template. We may also discover that the declaration is an explicit
2712 instantiation at this point.
2713
2714 Returns DECL, or an equivalent declaration that should be used
2715 instead if all goes well. Issues an error message if something is
2716 amiss. Returns error_mark_node if the error is not easily
2717 recoverable.
2718
2719 FLAGS is a bitmask consisting of the following flags:
2720
2721 2: The function has a definition.
2722 4: The function is a friend.
2723
2724 The TEMPLATE_COUNT is the number of references to qualifying
2725 template classes that appeared in the name of the function. For
2726 example, in
2727
2728 template <class T> struct S { void f(); };
2729 void S<int>::f();
2730
2731 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2732 classes are not counted in the TEMPLATE_COUNT, so that in
2733
2734 template <class T> struct S {};
2735 template <> struct S<int> { void f(); }
2736 template <> void S<int>::f();
2737
2738 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2739 invalid; there should be no template <>.)
2740
2741 If the function is a specialization, it is marked as such via
2742 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2743 is set up correctly, and it is added to the list of specializations
2744 for that template. */
2745
2746 tree
2747 check_explicit_specialization (tree declarator,
2748 tree decl,
2749 int template_count,
2750 int flags,
2751 tree attrlist)
2752 {
2753 int have_def = flags & 2;
2754 int is_friend = flags & 4;
2755 bool is_concept = flags & 8;
2756 int specialization = 0;
2757 int explicit_instantiation = 0;
2758 int member_specialization = 0;
2759 tree ctype = DECL_CLASS_CONTEXT (decl);
2760 tree dname = DECL_NAME (decl);
2761 tmpl_spec_kind tsk;
2762
2763 if (is_friend)
2764 {
2765 if (!processing_specialization)
2766 tsk = tsk_none;
2767 else
2768 tsk = tsk_excessive_parms;
2769 }
2770 else
2771 tsk = current_tmpl_spec_kind (template_count);
2772
2773 switch (tsk)
2774 {
2775 case tsk_none:
2776 if (processing_specialization && !VAR_P (decl))
2777 {
2778 specialization = 1;
2779 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2780 }
2781 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2782 {
2783 if (is_friend)
2784 /* This could be something like:
2785
2786 template <class T> void f(T);
2787 class S { friend void f<>(int); } */
2788 specialization = 1;
2789 else
2790 {
2791 /* This case handles bogus declarations like template <>
2792 template <class T> void f<int>(); */
2793
2794 error ("template-id %qD in declaration of primary template",
2795 declarator);
2796 return decl;
2797 }
2798 }
2799 break;
2800
2801 case tsk_invalid_member_spec:
2802 /* The error has already been reported in
2803 check_specialization_scope. */
2804 return error_mark_node;
2805
2806 case tsk_invalid_expl_inst:
2807 error ("template parameter list used in explicit instantiation");
2808
2809 /* Fall through. */
2810
2811 case tsk_expl_inst:
2812 if (have_def)
2813 error ("definition provided for explicit instantiation");
2814
2815 explicit_instantiation = 1;
2816 break;
2817
2818 case tsk_excessive_parms:
2819 case tsk_insufficient_parms:
2820 if (tsk == tsk_excessive_parms)
2821 error ("too many template parameter lists in declaration of %qD",
2822 decl);
2823 else if (template_header_count)
2824 error("too few template parameter lists in declaration of %qD", decl);
2825 else
2826 error("explicit specialization of %qD must be introduced by "
2827 "%<template <>%>", decl);
2828
2829 /* Fall through. */
2830 case tsk_expl_spec:
2831 if (is_concept)
2832 error ("explicit specialization declared %<concept%>");
2833
2834 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2835 /* In cases like template<> constexpr bool v = true;
2836 We'll give an error in check_template_variable. */
2837 break;
2838
2839 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2840 if (ctype)
2841 member_specialization = 1;
2842 else
2843 specialization = 1;
2844 break;
2845
2846 case tsk_template:
2847 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2848 {
2849 /* This case handles bogus declarations like template <>
2850 template <class T> void f<int>(); */
2851
2852 if (!uses_template_parms (declarator))
2853 error ("template-id %qD in declaration of primary template",
2854 declarator);
2855 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2856 {
2857 /* Partial specialization of variable template. */
2858 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2859 specialization = 1;
2860 goto ok;
2861 }
2862 else if (cxx_dialect < cxx14)
2863 error ("non-type partial specialization %qD "
2864 "is not allowed", declarator);
2865 else
2866 error ("non-class, non-variable partial specialization %qD "
2867 "is not allowed", declarator);
2868 return decl;
2869 ok:;
2870 }
2871
2872 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2873 /* This is a specialization of a member template, without
2874 specialization the containing class. Something like:
2875
2876 template <class T> struct S {
2877 template <class U> void f (U);
2878 };
2879 template <> template <class U> void S<int>::f(U) {}
2880
2881 That's a specialization -- but of the entire template. */
2882 specialization = 1;
2883 break;
2884
2885 default:
2886 gcc_unreachable ();
2887 }
2888
2889 if ((specialization || member_specialization)
2890 /* This doesn't apply to variable templates. */
2891 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2892 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2893 {
2894 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2895 for (; t; t = TREE_CHAIN (t))
2896 if (TREE_PURPOSE (t))
2897 {
2898 permerror (input_location,
2899 "default argument specified in explicit specialization");
2900 break;
2901 }
2902 }
2903
2904 if (specialization || member_specialization || explicit_instantiation)
2905 {
2906 tree tmpl = NULL_TREE;
2907 tree targs = NULL_TREE;
2908 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2909
2910 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2911 if (!was_template_id)
2912 {
2913 tree fns;
2914
2915 gcc_assert (identifier_p (declarator));
2916 if (ctype)
2917 fns = dname;
2918 else
2919 {
2920 /* If there is no class context, the explicit instantiation
2921 must be at namespace scope. */
2922 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2923
2924 /* Find the namespace binding, using the declaration
2925 context. */
2926 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2927 false, true);
2928 if (fns == error_mark_node)
2929 /* If lookup fails, look for a friend declaration so we can
2930 give a better diagnostic. */
2931 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2932 /*type*/false, /*complain*/true,
2933 /*hidden*/true);
2934
2935 if (fns == error_mark_node || !is_overloaded_fn (fns))
2936 {
2937 error ("%qD is not a template function", dname);
2938 fns = error_mark_node;
2939 }
2940 }
2941
2942 declarator = lookup_template_function (fns, NULL_TREE);
2943 }
2944
2945 if (declarator == error_mark_node)
2946 return error_mark_node;
2947
2948 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2949 {
2950 if (!explicit_instantiation)
2951 /* A specialization in class scope. This is invalid,
2952 but the error will already have been flagged by
2953 check_specialization_scope. */
2954 return error_mark_node;
2955 else
2956 {
2957 /* It's not valid to write an explicit instantiation in
2958 class scope, e.g.:
2959
2960 class C { template void f(); }
2961
2962 This case is caught by the parser. However, on
2963 something like:
2964
2965 template class C { void f(); };
2966
2967 (which is invalid) we can get here. The error will be
2968 issued later. */
2969 ;
2970 }
2971
2972 return decl;
2973 }
2974 else if (ctype != NULL_TREE
2975 && (identifier_p (TREE_OPERAND (declarator, 0))))
2976 {
2977 // We'll match variable templates in start_decl.
2978 if (VAR_P (decl))
2979 return decl;
2980
2981 /* Find the list of functions in ctype that have the same
2982 name as the declared function. */
2983 tree name = TREE_OPERAND (declarator, 0);
2984
2985 if (constructor_name_p (name, ctype))
2986 {
2987 if (DECL_CONSTRUCTOR_P (decl)
2988 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2989 : !CLASSTYPE_DESTRUCTOR (ctype))
2990 {
2991 /* From [temp.expl.spec]:
2992
2993 If such an explicit specialization for the member
2994 of a class template names an implicitly-declared
2995 special member function (clause _special_), the
2996 program is ill-formed.
2997
2998 Similar language is found in [temp.explicit]. */
2999 error ("specialization of implicitly-declared special member function");
3000 return error_mark_node;
3001 }
3002
3003 name = DECL_NAME (decl);
3004 }
3005
3006 /* For a type-conversion operator, We might be looking for
3007 `operator int' which will be a specialization of
3008 `operator T'. Grab all the conversion operators, and
3009 then select from them. */
3010 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3011 ? conv_op_identifier : name);
3012
3013 if (fns == NULL_TREE)
3014 {
3015 error ("no member function %qD declared in %qT", name, ctype);
3016 return error_mark_node;
3017 }
3018 else
3019 TREE_OPERAND (declarator, 0) = fns;
3020 }
3021
3022 /* Figure out what exactly is being specialized at this point.
3023 Note that for an explicit instantiation, even one for a
3024 member function, we cannot tell a priori whether the
3025 instantiation is for a member template, or just a member
3026 function of a template class. Even if a member template is
3027 being instantiated, the member template arguments may be
3028 elided if they can be deduced from the rest of the
3029 declaration. */
3030 tmpl = determine_specialization (declarator, decl,
3031 &targs,
3032 member_specialization,
3033 template_count,
3034 tsk);
3035
3036 if (!tmpl || tmpl == error_mark_node)
3037 /* We couldn't figure out what this declaration was
3038 specializing. */
3039 return error_mark_node;
3040 else
3041 {
3042 if (TREE_CODE (decl) == FUNCTION_DECL
3043 && DECL_HIDDEN_FRIEND_P (tmpl))
3044 {
3045 auto_diagnostic_group d;
3046 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3047 "friend declaration %qD is not visible to "
3048 "explicit specialization", tmpl))
3049 inform (DECL_SOURCE_LOCATION (tmpl),
3050 "friend declaration here");
3051 }
3052 else if (!ctype && !is_friend
3053 && CP_DECL_CONTEXT (decl) == current_namespace)
3054 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3055
3056 tree gen_tmpl = most_general_template (tmpl);
3057
3058 if (explicit_instantiation)
3059 {
3060 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3061 is done by do_decl_instantiation later. */
3062
3063 int arg_depth = TMPL_ARGS_DEPTH (targs);
3064 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3065
3066 if (arg_depth > parm_depth)
3067 {
3068 /* If TMPL is not the most general template (for
3069 example, if TMPL is a friend template that is
3070 injected into namespace scope), then there will
3071 be too many levels of TARGS. Remove some of them
3072 here. */
3073 int i;
3074 tree new_targs;
3075
3076 new_targs = make_tree_vec (parm_depth);
3077 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3078 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3079 = TREE_VEC_ELT (targs, i);
3080 targs = new_targs;
3081 }
3082
3083 return instantiate_template (tmpl, targs, tf_error);
3084 }
3085
3086 /* If we thought that the DECL was a member function, but it
3087 turns out to be specializing a static member function,
3088 make DECL a static member function as well. */
3089 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3090 && DECL_STATIC_FUNCTION_P (tmpl)
3091 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3092 revert_static_member_fn (decl);
3093
3094 /* If this is a specialization of a member template of a
3095 template class, we want to return the TEMPLATE_DECL, not
3096 the specialization of it. */
3097 if (tsk == tsk_template && !was_template_id)
3098 {
3099 tree result = DECL_TEMPLATE_RESULT (tmpl);
3100 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3101 DECL_INITIAL (result) = NULL_TREE;
3102 if (have_def)
3103 {
3104 tree parm;
3105 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3106 DECL_SOURCE_LOCATION (result)
3107 = DECL_SOURCE_LOCATION (decl);
3108 /* We want to use the argument list specified in the
3109 definition, not in the original declaration. */
3110 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3111 for (parm = DECL_ARGUMENTS (result); parm;
3112 parm = DECL_CHAIN (parm))
3113 DECL_CONTEXT (parm) = result;
3114 }
3115 return register_specialization (tmpl, gen_tmpl, targs,
3116 is_friend, 0);
3117 }
3118
3119 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3120 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3121
3122 if (was_template_id)
3123 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3124
3125 /* Inherit default function arguments from the template
3126 DECL is specializing. */
3127 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3128 copy_default_args_to_explicit_spec (decl);
3129
3130 /* This specialization has the same protection as the
3131 template it specializes. */
3132 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3133 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3134
3135 /* 7.1.1-1 [dcl.stc]
3136
3137 A storage-class-specifier shall not be specified in an
3138 explicit specialization...
3139
3140 The parser rejects these, so unless action is taken here,
3141 explicit function specializations will always appear with
3142 global linkage.
3143
3144 The action recommended by the C++ CWG in response to C++
3145 defect report 605 is to make the storage class and linkage
3146 of the explicit specialization match the templated function:
3147
3148 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3149 */
3150 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3151 {
3152 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3153 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3154
3155 /* A concept cannot be specialized. */
3156 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3157 {
3158 error ("explicit specialization of function concept %qD",
3159 gen_tmpl);
3160 return error_mark_node;
3161 }
3162
3163 /* This specialization has the same linkage and visibility as
3164 the function template it specializes. */
3165 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3166 if (! TREE_PUBLIC (decl))
3167 {
3168 DECL_INTERFACE_KNOWN (decl) = 1;
3169 DECL_NOT_REALLY_EXTERN (decl) = 1;
3170 }
3171 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3172 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3173 {
3174 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3175 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3176 }
3177 }
3178
3179 /* If DECL is a friend declaration, declared using an
3180 unqualified name, the namespace associated with DECL may
3181 have been set incorrectly. For example, in:
3182
3183 template <typename T> void f(T);
3184 namespace N {
3185 struct S { friend void f<int>(int); }
3186 }
3187
3188 we will have set the DECL_CONTEXT for the friend
3189 declaration to N, rather than to the global namespace. */
3190 if (DECL_NAMESPACE_SCOPE_P (decl))
3191 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3192
3193 if (is_friend && !have_def)
3194 /* This is not really a declaration of a specialization.
3195 It's just the name of an instantiation. But, it's not
3196 a request for an instantiation, either. */
3197 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3198 else if (TREE_CODE (decl) == FUNCTION_DECL)
3199 /* A specialization is not necessarily COMDAT. */
3200 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3201 && DECL_DECLARED_INLINE_P (decl));
3202 else if (VAR_P (decl))
3203 DECL_COMDAT (decl) = false;
3204
3205 /* If this is a full specialization, register it so that we can find
3206 it again. Partial specializations will be registered in
3207 process_partial_specialization. */
3208 if (!processing_template_decl)
3209 {
3210 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3211
3212 decl = register_specialization (decl, gen_tmpl, targs,
3213 is_friend, 0);
3214 }
3215
3216
3217 /* A 'structor should already have clones. */
3218 gcc_assert (decl == error_mark_node
3219 || variable_template_p (tmpl)
3220 || !(DECL_CONSTRUCTOR_P (decl)
3221 || DECL_DESTRUCTOR_P (decl))
3222 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3223 }
3224 }
3225
3226 return decl;
3227 }
3228
3229 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3230 parameters. These are represented in the same format used for
3231 DECL_TEMPLATE_PARMS. */
3232
3233 int
3234 comp_template_parms (const_tree parms1, const_tree parms2)
3235 {
3236 const_tree p1;
3237 const_tree p2;
3238
3239 if (parms1 == parms2)
3240 return 1;
3241
3242 for (p1 = parms1, p2 = parms2;
3243 p1 != NULL_TREE && p2 != NULL_TREE;
3244 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3245 {
3246 tree t1 = TREE_VALUE (p1);
3247 tree t2 = TREE_VALUE (p2);
3248 int i;
3249
3250 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3251 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3252
3253 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3254 return 0;
3255
3256 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3257 {
3258 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3259 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3260
3261 /* If either of the template parameters are invalid, assume
3262 they match for the sake of error recovery. */
3263 if (error_operand_p (parm1) || error_operand_p (parm2))
3264 return 1;
3265
3266 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3267 return 0;
3268
3269 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3270 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3271 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3272 continue;
3273 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3274 return 0;
3275 }
3276 }
3277
3278 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3279 /* One set of parameters has more parameters lists than the
3280 other. */
3281 return 0;
3282
3283 return 1;
3284 }
3285
3286 /* Determine whether PARM is a parameter pack. */
3287
3288 bool
3289 template_parameter_pack_p (const_tree parm)
3290 {
3291 /* Determine if we have a non-type template parameter pack. */
3292 if (TREE_CODE (parm) == PARM_DECL)
3293 return (DECL_TEMPLATE_PARM_P (parm)
3294 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3295 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3296 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3297
3298 /* If this is a list of template parameters, we could get a
3299 TYPE_DECL or a TEMPLATE_DECL. */
3300 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3301 parm = TREE_TYPE (parm);
3302
3303 /* Otherwise it must be a type template parameter. */
3304 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3305 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3306 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3307 }
3308
3309 /* Determine if T is a function parameter pack. */
3310
3311 bool
3312 function_parameter_pack_p (const_tree t)
3313 {
3314 if (t && TREE_CODE (t) == PARM_DECL)
3315 return DECL_PACK_P (t);
3316 return false;
3317 }
3318
3319 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3320 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3321
3322 tree
3323 get_function_template_decl (const_tree primary_func_tmpl_inst)
3324 {
3325 if (! primary_func_tmpl_inst
3326 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3327 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3328 return NULL;
3329
3330 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3331 }
3332
3333 /* Return true iff the function parameter PARAM_DECL was expanded
3334 from the function parameter pack PACK. */
3335
3336 bool
3337 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3338 {
3339 if (DECL_ARTIFICIAL (param_decl)
3340 || !function_parameter_pack_p (pack))
3341 return false;
3342
3343 /* The parameter pack and its pack arguments have the same
3344 DECL_PARM_INDEX. */
3345 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3346 }
3347
3348 /* Determine whether ARGS describes a variadic template args list,
3349 i.e., one that is terminated by a template argument pack. */
3350
3351 static bool
3352 template_args_variadic_p (tree args)
3353 {
3354 int nargs;
3355 tree last_parm;
3356
3357 if (args == NULL_TREE)
3358 return false;
3359
3360 args = INNERMOST_TEMPLATE_ARGS (args);
3361 nargs = TREE_VEC_LENGTH (args);
3362
3363 if (nargs == 0)
3364 return false;
3365
3366 last_parm = TREE_VEC_ELT (args, nargs - 1);
3367
3368 return ARGUMENT_PACK_P (last_parm);
3369 }
3370
3371 /* Generate a new name for the parameter pack name NAME (an
3372 IDENTIFIER_NODE) that incorporates its */
3373
3374 static tree
3375 make_ith_pack_parameter_name (tree name, int i)
3376 {
3377 /* Munge the name to include the parameter index. */
3378 #define NUMBUF_LEN 128
3379 char numbuf[NUMBUF_LEN];
3380 char* newname;
3381 int newname_len;
3382
3383 if (name == NULL_TREE)
3384 return name;
3385 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3386 newname_len = IDENTIFIER_LENGTH (name)
3387 + strlen (numbuf) + 2;
3388 newname = (char*)alloca (newname_len);
3389 snprintf (newname, newname_len,
3390 "%s#%i", IDENTIFIER_POINTER (name), i);
3391 return get_identifier (newname);
3392 }
3393
3394 /* Return true if T is a primary function, class or alias template
3395 specialization, not including the template pattern. */
3396
3397 bool
3398 primary_template_specialization_p (const_tree t)
3399 {
3400 if (!t)
3401 return false;
3402
3403 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3404 return (DECL_LANG_SPECIFIC (t)
3405 && DECL_USE_TEMPLATE (t)
3406 && DECL_TEMPLATE_INFO (t)
3407 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3408 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3409 return (CLASSTYPE_TEMPLATE_INFO (t)
3410 && CLASSTYPE_USE_TEMPLATE (t)
3411 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3412 else if (alias_template_specialization_p (t))
3413 return true;
3414 return false;
3415 }
3416
3417 /* Return true if PARM is a template template parameter. */
3418
3419 bool
3420 template_template_parameter_p (const_tree parm)
3421 {
3422 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3423 }
3424
3425 /* Return true iff PARM is a DECL representing a type template
3426 parameter. */
3427
3428 bool
3429 template_type_parameter_p (const_tree parm)
3430 {
3431 return (parm
3432 && (TREE_CODE (parm) == TYPE_DECL
3433 || TREE_CODE (parm) == TEMPLATE_DECL)
3434 && DECL_TEMPLATE_PARM_P (parm));
3435 }
3436
3437 /* Return the template parameters of T if T is a
3438 primary template instantiation, NULL otherwise. */
3439
3440 tree
3441 get_primary_template_innermost_parameters (const_tree t)
3442 {
3443 tree parms = NULL, template_info = NULL;
3444
3445 if ((template_info = get_template_info (t))
3446 && primary_template_specialization_p (t))
3447 parms = INNERMOST_TEMPLATE_PARMS
3448 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3449
3450 return parms;
3451 }
3452
3453 /* Return the template parameters of the LEVELth level from the full list
3454 of template parameters PARMS. */
3455
3456 tree
3457 get_template_parms_at_level (tree parms, int level)
3458 {
3459 tree p;
3460 if (!parms
3461 || TREE_CODE (parms) != TREE_LIST
3462 || level > TMPL_PARMS_DEPTH (parms))
3463 return NULL_TREE;
3464
3465 for (p = parms; p; p = TREE_CHAIN (p))
3466 if (TMPL_PARMS_DEPTH (p) == level)
3467 return p;
3468
3469 return NULL_TREE;
3470 }
3471
3472 /* Returns the template arguments of T if T is a template instantiation,
3473 NULL otherwise. */
3474
3475 tree
3476 get_template_innermost_arguments (const_tree t)
3477 {
3478 tree args = NULL, template_info = NULL;
3479
3480 if ((template_info = get_template_info (t))
3481 && TI_ARGS (template_info))
3482 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3483
3484 return args;
3485 }
3486
3487 /* Return the argument pack elements of T if T is a template argument pack,
3488 NULL otherwise. */
3489
3490 tree
3491 get_template_argument_pack_elems (const_tree t)
3492 {
3493 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3494 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3495 return NULL;
3496
3497 return ARGUMENT_PACK_ARGS (t);
3498 }
3499
3500 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3501 ARGUMENT_PACK_SELECT represents. */
3502
3503 static tree
3504 argument_pack_select_arg (tree t)
3505 {
3506 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3507 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3508
3509 /* If the selected argument is an expansion E, that most likely means we were
3510 called from gen_elem_of_pack_expansion_instantiation during the
3511 substituting of an argument pack (of which the Ith element is a pack
3512 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3513 In this case, the Ith element resulting from this substituting is going to
3514 be a pack expansion, which pattern is the pattern of E. Let's return the
3515 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3516 resulting pack expansion from it. */
3517 if (PACK_EXPANSION_P (arg))
3518 {
3519 /* Make sure we aren't throwing away arg info. */
3520 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3521 arg = PACK_EXPANSION_PATTERN (arg);
3522 }
3523
3524 return arg;
3525 }
3526
3527
3528 /* True iff FN is a function representing a built-in variadic parameter
3529 pack. */
3530
3531 bool
3532 builtin_pack_fn_p (tree fn)
3533 {
3534 if (!fn
3535 || TREE_CODE (fn) != FUNCTION_DECL
3536 || !DECL_IS_BUILTIN (fn))
3537 return false;
3538
3539 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3540 return true;
3541
3542 return false;
3543 }
3544
3545 /* True iff CALL is a call to a function representing a built-in variadic
3546 parameter pack. */
3547
3548 static bool
3549 builtin_pack_call_p (tree call)
3550 {
3551 if (TREE_CODE (call) != CALL_EXPR)
3552 return false;
3553 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3554 }
3555
3556 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3557
3558 static tree
3559 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3560 tree in_decl)
3561 {
3562 tree ohi = CALL_EXPR_ARG (call, 0);
3563 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3564 false/*fn*/, true/*int_cst*/);
3565
3566 if (value_dependent_expression_p (hi))
3567 {
3568 if (hi != ohi)
3569 {
3570 call = copy_node (call);
3571 CALL_EXPR_ARG (call, 0) = hi;
3572 }
3573 tree ex = make_pack_expansion (call, complain);
3574 tree vec = make_tree_vec (1);
3575 TREE_VEC_ELT (vec, 0) = ex;
3576 return vec;
3577 }
3578 else
3579 {
3580 hi = cxx_constant_value (hi);
3581 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3582
3583 /* Calculate the largest value of len that won't make the size of the vec
3584 overflow an int. The compiler will exceed resource limits long before
3585 this, but it seems a decent place to diagnose. */
3586 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3587
3588 if (len < 0 || len > max)
3589 {
3590 if ((complain & tf_error)
3591 && hi != error_mark_node)
3592 error ("argument to __integer_pack must be between 0 and %d", max);
3593 return error_mark_node;
3594 }
3595
3596 tree vec = make_tree_vec (len);
3597
3598 for (int i = 0; i < len; ++i)
3599 TREE_VEC_ELT (vec, i) = size_int (i);
3600
3601 return vec;
3602 }
3603 }
3604
3605 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3606 CALL. */
3607
3608 static tree
3609 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3610 tree in_decl)
3611 {
3612 if (!builtin_pack_call_p (call))
3613 return NULL_TREE;
3614
3615 tree fn = CALL_EXPR_FN (call);
3616
3617 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3618 return expand_integer_pack (call, args, complain, in_decl);
3619
3620 return NULL_TREE;
3621 }
3622
3623 /* Structure used to track the progress of find_parameter_packs_r. */
3624 struct find_parameter_pack_data
3625 {
3626 /* TREE_LIST that will contain all of the parameter packs found by
3627 the traversal. */
3628 tree* parameter_packs;
3629
3630 /* Set of AST nodes that have been visited by the traversal. */
3631 hash_set<tree> *visited;
3632
3633 /* True iff we're making a type pack expansion. */
3634 bool type_pack_expansion_p;
3635 };
3636
3637 /* Identifies all of the argument packs that occur in a template
3638 argument and appends them to the TREE_LIST inside DATA, which is a
3639 find_parameter_pack_data structure. This is a subroutine of
3640 make_pack_expansion and uses_parameter_packs. */
3641 static tree
3642 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3643 {
3644 tree t = *tp;
3645 struct find_parameter_pack_data* ppd =
3646 (struct find_parameter_pack_data*)data;
3647 bool parameter_pack_p = false;
3648
3649 /* Handle type aliases/typedefs. */
3650 if (TYPE_ALIAS_P (t))
3651 {
3652 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3653 cp_walk_tree (&TI_ARGS (tinfo),
3654 &find_parameter_packs_r,
3655 ppd, ppd->visited);
3656 *walk_subtrees = 0;
3657 return NULL_TREE;
3658 }
3659
3660 /* Identify whether this is a parameter pack or not. */
3661 switch (TREE_CODE (t))
3662 {
3663 case TEMPLATE_PARM_INDEX:
3664 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3665 parameter_pack_p = true;
3666 break;
3667
3668 case TEMPLATE_TYPE_PARM:
3669 t = TYPE_MAIN_VARIANT (t);
3670 /* FALLTHRU */
3671 case TEMPLATE_TEMPLATE_PARM:
3672 /* If the placeholder appears in the decl-specifier-seq of a function
3673 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3674 is a pack expansion, the invented template parameter is a template
3675 parameter pack. */
3676 if (ppd->type_pack_expansion_p && is_auto (t))
3677 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3678 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3679 parameter_pack_p = true;
3680 break;
3681
3682 case FIELD_DECL:
3683 case PARM_DECL:
3684 if (DECL_PACK_P (t))
3685 {
3686 /* We don't want to walk into the type of a PARM_DECL,
3687 because we don't want to see the type parameter pack. */
3688 *walk_subtrees = 0;
3689 parameter_pack_p = true;
3690 }
3691 break;
3692
3693 case VAR_DECL:
3694 if (DECL_PACK_P (t))
3695 {
3696 /* We don't want to walk into the type of a variadic capture proxy,
3697 because we don't want to see the type parameter pack. */
3698 *walk_subtrees = 0;
3699 parameter_pack_p = true;
3700 }
3701 else if (variable_template_specialization_p (t))
3702 {
3703 cp_walk_tree (&DECL_TI_ARGS (t),
3704 find_parameter_packs_r,
3705 ppd, ppd->visited);
3706 *walk_subtrees = 0;
3707 }
3708 break;
3709
3710 case CALL_EXPR:
3711 if (builtin_pack_call_p (t))
3712 parameter_pack_p = true;
3713 break;
3714
3715 case BASES:
3716 parameter_pack_p = true;
3717 break;
3718 default:
3719 /* Not a parameter pack. */
3720 break;
3721 }
3722
3723 if (parameter_pack_p)
3724 {
3725 /* Add this parameter pack to the list. */
3726 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3727 }
3728
3729 if (TYPE_P (t))
3730 cp_walk_tree (&TYPE_CONTEXT (t),
3731 &find_parameter_packs_r, ppd, ppd->visited);
3732
3733 /* This switch statement will return immediately if we don't find a
3734 parameter pack. */
3735 switch (TREE_CODE (t))
3736 {
3737 case TEMPLATE_PARM_INDEX:
3738 return NULL_TREE;
3739
3740 case BOUND_TEMPLATE_TEMPLATE_PARM:
3741 /* Check the template itself. */
3742 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3743 &find_parameter_packs_r, ppd, ppd->visited);
3744 /* Check the template arguments. */
3745 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3746 ppd->visited);
3747 *walk_subtrees = 0;
3748 return NULL_TREE;
3749
3750 case TEMPLATE_TYPE_PARM:
3751 case TEMPLATE_TEMPLATE_PARM:
3752 return NULL_TREE;
3753
3754 case PARM_DECL:
3755 return NULL_TREE;
3756
3757 case DECL_EXPR:
3758 /* Ignore the declaration of a capture proxy for a parameter pack. */
3759 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3760 *walk_subtrees = 0;
3761 return NULL_TREE;
3762
3763 case RECORD_TYPE:
3764 if (TYPE_PTRMEMFUNC_P (t))
3765 return NULL_TREE;
3766 /* Fall through. */
3767
3768 case UNION_TYPE:
3769 case ENUMERAL_TYPE:
3770 if (TYPE_TEMPLATE_INFO (t))
3771 cp_walk_tree (&TYPE_TI_ARGS (t),
3772 &find_parameter_packs_r, ppd, ppd->visited);
3773
3774 *walk_subtrees = 0;
3775 return NULL_TREE;
3776
3777 case TEMPLATE_DECL:
3778 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3779 return NULL_TREE;
3780 gcc_fallthrough();
3781
3782 case CONSTRUCTOR:
3783 cp_walk_tree (&TREE_TYPE (t),
3784 &find_parameter_packs_r, ppd, ppd->visited);
3785 return NULL_TREE;
3786
3787 case TYPENAME_TYPE:
3788 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3789 ppd, ppd->visited);
3790 *walk_subtrees = 0;
3791 return NULL_TREE;
3792
3793 case TYPE_PACK_EXPANSION:
3794 case EXPR_PACK_EXPANSION:
3795 *walk_subtrees = 0;
3796 return NULL_TREE;
3797
3798 case INTEGER_TYPE:
3799 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3800 ppd, ppd->visited);
3801 *walk_subtrees = 0;
3802 return NULL_TREE;
3803
3804 case IDENTIFIER_NODE:
3805 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3806 ppd->visited);
3807 *walk_subtrees = 0;
3808 return NULL_TREE;
3809
3810 case LAMBDA_EXPR:
3811 {
3812 /* Look at explicit captures. */
3813 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3814 cap; cap = TREE_CHAIN (cap))
3815 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3816 ppd->visited);
3817 /* Since we defer implicit capture, look in the parms and body. */
3818 tree fn = lambda_function (t);
3819 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3820 ppd->visited);
3821 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3822 ppd->visited);
3823 *walk_subtrees = 0;
3824 return NULL_TREE;
3825 }
3826
3827 case DECLTYPE_TYPE:
3828 {
3829 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3830 type_pack_expansion_p to false so that any placeholders
3831 within the expression don't get marked as parameter packs. */
3832 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3833 ppd->type_pack_expansion_p = false;
3834 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3835 ppd, ppd->visited);
3836 ppd->type_pack_expansion_p = type_pack_expansion_p;
3837 *walk_subtrees = 0;
3838 return NULL_TREE;
3839 }
3840
3841 case IF_STMT:
3842 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3843 ppd, ppd->visited);
3844 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3845 ppd, ppd->visited);
3846 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3847 ppd, ppd->visited);
3848 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3849 *walk_subtrees = 0;
3850 return NULL_TREE;
3851
3852 default:
3853 return NULL_TREE;
3854 }
3855
3856 return NULL_TREE;
3857 }
3858
3859 /* Determines if the expression or type T uses any parameter packs. */
3860 bool
3861 uses_parameter_packs (tree t)
3862 {
3863 tree parameter_packs = NULL_TREE;
3864 struct find_parameter_pack_data ppd;
3865 ppd.parameter_packs = &parameter_packs;
3866 ppd.visited = new hash_set<tree>;
3867 ppd.type_pack_expansion_p = false;
3868 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3869 delete ppd.visited;
3870 return parameter_packs != NULL_TREE;
3871 }
3872
3873 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3874 representation a base-class initializer into a parameter pack
3875 expansion. If all goes well, the resulting node will be an
3876 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3877 respectively. */
3878 tree
3879 make_pack_expansion (tree arg, tsubst_flags_t complain)
3880 {
3881 tree result;
3882 tree parameter_packs = NULL_TREE;
3883 bool for_types = false;
3884 struct find_parameter_pack_data ppd;
3885
3886 if (!arg || arg == error_mark_node)
3887 return arg;
3888
3889 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3890 {
3891 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3892 class initializer. In this case, the TREE_PURPOSE will be a
3893 _TYPE node (representing the base class expansion we're
3894 initializing) and the TREE_VALUE will be a TREE_LIST
3895 containing the initialization arguments.
3896
3897 The resulting expansion looks somewhat different from most
3898 expansions. Rather than returning just one _EXPANSION, we
3899 return a TREE_LIST whose TREE_PURPOSE is a
3900 TYPE_PACK_EXPANSION containing the bases that will be
3901 initialized. The TREE_VALUE will be identical to the
3902 original TREE_VALUE, which is a list of arguments that will
3903 be passed to each base. We do not introduce any new pack
3904 expansion nodes into the TREE_VALUE (although it is possible
3905 that some already exist), because the TREE_PURPOSE and
3906 TREE_VALUE all need to be expanded together with the same
3907 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3908 resulting TREE_PURPOSE will mention the parameter packs in
3909 both the bases and the arguments to the bases. */
3910 tree purpose;
3911 tree value;
3912 tree parameter_packs = NULL_TREE;
3913
3914 /* Determine which parameter packs will be used by the base
3915 class expansion. */
3916 ppd.visited = new hash_set<tree>;
3917 ppd.parameter_packs = &parameter_packs;
3918 ppd.type_pack_expansion_p = true;
3919 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3920 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3921 &ppd, ppd.visited);
3922
3923 if (parameter_packs == NULL_TREE)
3924 {
3925 if (complain & tf_error)
3926 error ("base initializer expansion %qT contains no parameter packs",
3927 arg);
3928 delete ppd.visited;
3929 return error_mark_node;
3930 }
3931
3932 if (TREE_VALUE (arg) != void_type_node)
3933 {
3934 /* Collect the sets of parameter packs used in each of the
3935 initialization arguments. */
3936 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3937 {
3938 /* Determine which parameter packs will be expanded in this
3939 argument. */
3940 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3941 &ppd, ppd.visited);
3942 }
3943 }
3944
3945 delete ppd.visited;
3946
3947 /* Create the pack expansion type for the base type. */
3948 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3949 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3950 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3951 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3952
3953 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3954 they will rarely be compared to anything. */
3955 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3956
3957 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3958 }
3959
3960 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3961 for_types = true;
3962
3963 /* Build the PACK_EXPANSION_* node. */
3964 result = for_types
3965 ? cxx_make_type (TYPE_PACK_EXPANSION)
3966 : make_node (EXPR_PACK_EXPANSION);
3967 SET_PACK_EXPANSION_PATTERN (result, arg);
3968 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3969 {
3970 /* Propagate type and const-expression information. */
3971 TREE_TYPE (result) = TREE_TYPE (arg);
3972 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3973 /* Mark this read now, since the expansion might be length 0. */
3974 mark_exp_read (arg);
3975 }
3976 else
3977 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3978 they will rarely be compared to anything. */
3979 SET_TYPE_STRUCTURAL_EQUALITY (result);
3980
3981 /* Determine which parameter packs will be expanded. */
3982 ppd.parameter_packs = &parameter_packs;
3983 ppd.visited = new hash_set<tree>;
3984 ppd.type_pack_expansion_p = TYPE_P (arg);
3985 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3986 delete ppd.visited;
3987
3988 /* Make sure we found some parameter packs. */
3989 if (parameter_packs == NULL_TREE)
3990 {
3991 if (complain & tf_error)
3992 {
3993 if (TYPE_P (arg))
3994 error ("expansion pattern %qT contains no parameter packs", arg);
3995 else
3996 error ("expansion pattern %qE contains no parameter packs", arg);
3997 }
3998 return error_mark_node;
3999 }
4000 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4001
4002 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4003
4004 return result;
4005 }
4006
4007 /* Checks T for any "bare" parameter packs, which have not yet been
4008 expanded, and issues an error if any are found. This operation can
4009 only be done on full expressions or types (e.g., an expression
4010 statement, "if" condition, etc.), because we could have expressions like:
4011
4012 foo(f(g(h(args)))...)
4013
4014 where "args" is a parameter pack. check_for_bare_parameter_packs
4015 should not be called for the subexpressions args, h(args),
4016 g(h(args)), or f(g(h(args))), because we would produce erroneous
4017 error messages.
4018
4019 Returns TRUE and emits an error if there were bare parameter packs,
4020 returns FALSE otherwise. */
4021 bool
4022 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4023 {
4024 tree parameter_packs = NULL_TREE;
4025 struct find_parameter_pack_data ppd;
4026
4027 if (!processing_template_decl || !t || t == error_mark_node)
4028 return false;
4029
4030 /* A lambda might use a parameter pack from the containing context. */
4031 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4032 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4033 return false;
4034
4035 if (TREE_CODE (t) == TYPE_DECL)
4036 t = TREE_TYPE (t);
4037
4038 ppd.parameter_packs = &parameter_packs;
4039 ppd.visited = new hash_set<tree>;
4040 ppd.type_pack_expansion_p = false;
4041 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4042 delete ppd.visited;
4043
4044 if (parameter_packs)
4045 {
4046 if (loc == UNKNOWN_LOCATION)
4047 loc = cp_expr_loc_or_loc (t, input_location);
4048 error_at (loc, "parameter packs not expanded with %<...%>:");
4049 while (parameter_packs)
4050 {
4051 tree pack = TREE_VALUE (parameter_packs);
4052 tree name = NULL_TREE;
4053
4054 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4055 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4056 name = TYPE_NAME (pack);
4057 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4058 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4059 else if (TREE_CODE (pack) == CALL_EXPR)
4060 name = DECL_NAME (CALL_EXPR_FN (pack));
4061 else
4062 name = DECL_NAME (pack);
4063
4064 if (name)
4065 inform (loc, " %qD", name);
4066 else
4067 inform (loc, " <anonymous>");
4068
4069 parameter_packs = TREE_CHAIN (parameter_packs);
4070 }
4071
4072 return true;
4073 }
4074
4075 return false;
4076 }
4077
4078 /* Expand any parameter packs that occur in the template arguments in
4079 ARGS. */
4080 tree
4081 expand_template_argument_pack (tree args)
4082 {
4083 if (args == error_mark_node)
4084 return error_mark_node;
4085
4086 tree result_args = NULL_TREE;
4087 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4088 int num_result_args = -1;
4089 int non_default_args_count = -1;
4090
4091 /* First, determine if we need to expand anything, and the number of
4092 slots we'll need. */
4093 for (in_arg = 0; in_arg < nargs; ++in_arg)
4094 {
4095 tree arg = TREE_VEC_ELT (args, in_arg);
4096 if (arg == NULL_TREE)
4097 return args;
4098 if (ARGUMENT_PACK_P (arg))
4099 {
4100 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4101 if (num_result_args < 0)
4102 num_result_args = in_arg + num_packed;
4103 else
4104 num_result_args += num_packed;
4105 }
4106 else
4107 {
4108 if (num_result_args >= 0)
4109 num_result_args++;
4110 }
4111 }
4112
4113 /* If no expansion is necessary, we're done. */
4114 if (num_result_args < 0)
4115 return args;
4116
4117 /* Expand arguments. */
4118 result_args = make_tree_vec (num_result_args);
4119 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4120 non_default_args_count =
4121 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4122 for (in_arg = 0; in_arg < nargs; ++in_arg)
4123 {
4124 tree arg = TREE_VEC_ELT (args, in_arg);
4125 if (ARGUMENT_PACK_P (arg))
4126 {
4127 tree packed = ARGUMENT_PACK_ARGS (arg);
4128 int i, num_packed = TREE_VEC_LENGTH (packed);
4129 for (i = 0; i < num_packed; ++i, ++out_arg)
4130 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4131 if (non_default_args_count > 0)
4132 non_default_args_count += num_packed - 1;
4133 }
4134 else
4135 {
4136 TREE_VEC_ELT (result_args, out_arg) = arg;
4137 ++out_arg;
4138 }
4139 }
4140 if (non_default_args_count >= 0)
4141 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4142 return result_args;
4143 }
4144
4145 /* Checks if DECL shadows a template parameter.
4146
4147 [temp.local]: A template-parameter shall not be redeclared within its
4148 scope (including nested scopes).
4149
4150 Emits an error and returns TRUE if the DECL shadows a parameter,
4151 returns FALSE otherwise. */
4152
4153 bool
4154 check_template_shadow (tree decl)
4155 {
4156 tree olddecl;
4157
4158 /* If we're not in a template, we can't possibly shadow a template
4159 parameter. */
4160 if (!current_template_parms)
4161 return true;
4162
4163 /* Figure out what we're shadowing. */
4164 decl = OVL_FIRST (decl);
4165 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4166
4167 /* If there's no previous binding for this name, we're not shadowing
4168 anything, let alone a template parameter. */
4169 if (!olddecl)
4170 return true;
4171
4172 /* If we're not shadowing a template parameter, we're done. Note
4173 that OLDDECL might be an OVERLOAD (or perhaps even an
4174 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4175 node. */
4176 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4177 return true;
4178
4179 /* We check for decl != olddecl to avoid bogus errors for using a
4180 name inside a class. We check TPFI to avoid duplicate errors for
4181 inline member templates. */
4182 if (decl == olddecl
4183 || (DECL_TEMPLATE_PARM_P (decl)
4184 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4185 return true;
4186
4187 /* Don't complain about the injected class name, as we've already
4188 complained about the class itself. */
4189 if (DECL_SELF_REFERENCE_P (decl))
4190 return false;
4191
4192 if (DECL_TEMPLATE_PARM_P (decl))
4193 error ("declaration of template parameter %q+D shadows "
4194 "template parameter", decl);
4195 else
4196 error ("declaration of %q+#D shadows template parameter", decl);
4197 inform (DECL_SOURCE_LOCATION (olddecl),
4198 "template parameter %qD declared here", olddecl);
4199 return false;
4200 }
4201
4202 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4203 ORIG_LEVEL, DECL, and TYPE. */
4204
4205 static tree
4206 build_template_parm_index (int index,
4207 int level,
4208 int orig_level,
4209 tree decl,
4210 tree type)
4211 {
4212 tree t = make_node (TEMPLATE_PARM_INDEX);
4213 TEMPLATE_PARM_IDX (t) = index;
4214 TEMPLATE_PARM_LEVEL (t) = level;
4215 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4216 TEMPLATE_PARM_DECL (t) = decl;
4217 TREE_TYPE (t) = type;
4218 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4219 TREE_READONLY (t) = TREE_READONLY (decl);
4220
4221 return t;
4222 }
4223
4224 /* Find the canonical type parameter for the given template type
4225 parameter. Returns the canonical type parameter, which may be TYPE
4226 if no such parameter existed. */
4227
4228 static tree
4229 canonical_type_parameter (tree type)
4230 {
4231 tree list;
4232 int idx = TEMPLATE_TYPE_IDX (type);
4233 if (!canonical_template_parms)
4234 vec_alloc (canonical_template_parms, idx + 1);
4235
4236 if (canonical_template_parms->length () <= (unsigned) idx)
4237 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4238
4239 list = (*canonical_template_parms)[idx];
4240 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4241 list = TREE_CHAIN (list);
4242
4243 if (list)
4244 return TREE_VALUE (list);
4245 else
4246 {
4247 (*canonical_template_parms)[idx]
4248 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4249 return type;
4250 }
4251 }
4252
4253 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4254 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4255 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4256 new one is created. */
4257
4258 static tree
4259 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4260 tsubst_flags_t complain)
4261 {
4262 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4263 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4264 != TEMPLATE_PARM_LEVEL (index) - levels)
4265 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4266 {
4267 tree orig_decl = TEMPLATE_PARM_DECL (index);
4268 tree decl, t;
4269
4270 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4271 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4272 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4273 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4274 DECL_ARTIFICIAL (decl) = 1;
4275 SET_DECL_TEMPLATE_PARM_P (decl);
4276
4277 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4278 TEMPLATE_PARM_LEVEL (index) - levels,
4279 TEMPLATE_PARM_ORIG_LEVEL (index),
4280 decl, type);
4281 TEMPLATE_PARM_DESCENDANTS (index) = t;
4282 TEMPLATE_PARM_PARAMETER_PACK (t)
4283 = TEMPLATE_PARM_PARAMETER_PACK (index);
4284
4285 /* Template template parameters need this. */
4286 if (TREE_CODE (decl) == TEMPLATE_DECL)
4287 {
4288 DECL_TEMPLATE_RESULT (decl)
4289 = build_decl (DECL_SOURCE_LOCATION (decl),
4290 TYPE_DECL, DECL_NAME (decl), type);
4291 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4292 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4293 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4294 }
4295 }
4296
4297 return TEMPLATE_PARM_DESCENDANTS (index);
4298 }
4299
4300 /* Process information from new template parameter PARM and append it
4301 to the LIST being built. This new parameter is a non-type
4302 parameter iff IS_NON_TYPE is true. This new parameter is a
4303 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4304 is in PARM_LOC. */
4305
4306 tree
4307 process_template_parm (tree list, location_t parm_loc, tree parm,
4308 bool is_non_type, bool is_parameter_pack)
4309 {
4310 tree decl = 0;
4311 int idx = 0;
4312
4313 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4314 tree defval = TREE_PURPOSE (parm);
4315 tree constr = TREE_TYPE (parm);
4316
4317 if (list)
4318 {
4319 tree p = tree_last (list);
4320
4321 if (p && TREE_VALUE (p) != error_mark_node)
4322 {
4323 p = TREE_VALUE (p);
4324 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4325 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4326 else
4327 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4328 }
4329
4330 ++idx;
4331 }
4332
4333 if (is_non_type)
4334 {
4335 parm = TREE_VALUE (parm);
4336
4337 SET_DECL_TEMPLATE_PARM_P (parm);
4338
4339 if (TREE_TYPE (parm) != error_mark_node)
4340 {
4341 /* [temp.param]
4342
4343 The top-level cv-qualifiers on the template-parameter are
4344 ignored when determining its type. */
4345 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4346 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4347 TREE_TYPE (parm) = error_mark_node;
4348 else if (uses_parameter_packs (TREE_TYPE (parm))
4349 && !is_parameter_pack
4350 /* If we're in a nested template parameter list, the template
4351 template parameter could be a parameter pack. */
4352 && processing_template_parmlist == 1)
4353 {
4354 /* This template parameter is not a parameter pack, but it
4355 should be. Complain about "bare" parameter packs. */
4356 check_for_bare_parameter_packs (TREE_TYPE (parm));
4357
4358 /* Recover by calling this a parameter pack. */
4359 is_parameter_pack = true;
4360 }
4361 }
4362
4363 /* A template parameter is not modifiable. */
4364 TREE_CONSTANT (parm) = 1;
4365 TREE_READONLY (parm) = 1;
4366 decl = build_decl (parm_loc,
4367 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4368 TREE_CONSTANT (decl) = 1;
4369 TREE_READONLY (decl) = 1;
4370 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4371 = build_template_parm_index (idx, processing_template_decl,
4372 processing_template_decl,
4373 decl, TREE_TYPE (parm));
4374
4375 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4376 = is_parameter_pack;
4377 }
4378 else
4379 {
4380 tree t;
4381 parm = TREE_VALUE (TREE_VALUE (parm));
4382
4383 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4384 {
4385 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4386 /* This is for distinguishing between real templates and template
4387 template parameters */
4388 TREE_TYPE (parm) = t;
4389 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4390 decl = parm;
4391 }
4392 else
4393 {
4394 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4395 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4396 decl = build_decl (parm_loc,
4397 TYPE_DECL, parm, t);
4398 }
4399
4400 TYPE_NAME (t) = decl;
4401 TYPE_STUB_DECL (t) = decl;
4402 parm = decl;
4403 TEMPLATE_TYPE_PARM_INDEX (t)
4404 = build_template_parm_index (idx, processing_template_decl,
4405 processing_template_decl,
4406 decl, TREE_TYPE (parm));
4407 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4408 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4409 }
4410 DECL_ARTIFICIAL (decl) = 1;
4411 SET_DECL_TEMPLATE_PARM_P (decl);
4412
4413 /* Build requirements for the type/template parameter.
4414 This must be done after SET_DECL_TEMPLATE_PARM_P or
4415 process_template_parm could fail. */
4416 tree reqs = finish_shorthand_constraint (parm, constr);
4417
4418 pushdecl (decl);
4419
4420 /* Build the parameter node linking the parameter declaration,
4421 its default argument (if any), and its constraints (if any). */
4422 parm = build_tree_list (defval, parm);
4423 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4424
4425 return chainon (list, parm);
4426 }
4427
4428 /* The end of a template parameter list has been reached. Process the
4429 tree list into a parameter vector, converting each parameter into a more
4430 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4431 as PARM_DECLs. */
4432
4433 tree
4434 end_template_parm_list (tree parms)
4435 {
4436 int nparms;
4437 tree parm, next;
4438 tree saved_parmlist = make_tree_vec (list_length (parms));
4439
4440 /* Pop the dummy parameter level and add the real one. */
4441 current_template_parms = TREE_CHAIN (current_template_parms);
4442
4443 current_template_parms
4444 = tree_cons (size_int (processing_template_decl),
4445 saved_parmlist, current_template_parms);
4446
4447 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4448 {
4449 next = TREE_CHAIN (parm);
4450 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4451 TREE_CHAIN (parm) = NULL_TREE;
4452 }
4453
4454 --processing_template_parmlist;
4455
4456 return saved_parmlist;
4457 }
4458
4459 // Explicitly indicate the end of the template parameter list. We assume
4460 // that the current template parameters have been constructed and/or
4461 // managed explicitly, as when creating new template template parameters
4462 // from a shorthand constraint.
4463 void
4464 end_template_parm_list ()
4465 {
4466 --processing_template_parmlist;
4467 }
4468
4469 /* end_template_decl is called after a template declaration is seen. */
4470
4471 void
4472 end_template_decl (void)
4473 {
4474 reset_specialization ();
4475
4476 if (! processing_template_decl)
4477 return;
4478
4479 /* This matches the pushlevel in begin_template_parm_list. */
4480 finish_scope ();
4481
4482 --processing_template_decl;
4483 current_template_parms = TREE_CHAIN (current_template_parms);
4484 }
4485
4486 /* Takes a TREE_LIST representing a template parameter and convert it
4487 into an argument suitable to be passed to the type substitution
4488 functions. Note that If the TREE_LIST contains an error_mark
4489 node, the returned argument is error_mark_node. */
4490
4491 tree
4492 template_parm_to_arg (tree t)
4493 {
4494
4495 if (t == NULL_TREE
4496 || TREE_CODE (t) != TREE_LIST)
4497 return t;
4498
4499 if (error_operand_p (TREE_VALUE (t)))
4500 return error_mark_node;
4501
4502 t = TREE_VALUE (t);
4503
4504 if (TREE_CODE (t) == TYPE_DECL
4505 || TREE_CODE (t) == TEMPLATE_DECL)
4506 {
4507 t = TREE_TYPE (t);
4508
4509 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4510 {
4511 /* Turn this argument into a TYPE_ARGUMENT_PACK
4512 with a single element, which expands T. */
4513 tree vec = make_tree_vec (1);
4514 if (CHECKING_P)
4515 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4516
4517 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4518
4519 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4520 SET_ARGUMENT_PACK_ARGS (t, vec);
4521 }
4522 }
4523 else
4524 {
4525 t = DECL_INITIAL (t);
4526
4527 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4528 {
4529 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4530 with a single element, which expands T. */
4531 tree vec = make_tree_vec (1);
4532 if (CHECKING_P)
4533 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4534
4535 t = convert_from_reference (t);
4536 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4537
4538 t = make_node (NONTYPE_ARGUMENT_PACK);
4539 SET_ARGUMENT_PACK_ARGS (t, vec);
4540 }
4541 else
4542 t = convert_from_reference (t);
4543 }
4544 return t;
4545 }
4546
4547 /* Given a single level of template parameters (a TREE_VEC), return it
4548 as a set of template arguments. */
4549
4550 static tree
4551 template_parms_level_to_args (tree parms)
4552 {
4553 tree a = copy_node (parms);
4554 TREE_TYPE (a) = NULL_TREE;
4555 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4556 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4557
4558 if (CHECKING_P)
4559 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4560
4561 return a;
4562 }
4563
4564 /* Given a set of template parameters, return them as a set of template
4565 arguments. The template parameters are represented as a TREE_VEC, in
4566 the form documented in cp-tree.h for template arguments. */
4567
4568 static tree
4569 template_parms_to_args (tree parms)
4570 {
4571 tree header;
4572 tree args = NULL_TREE;
4573 int length = TMPL_PARMS_DEPTH (parms);
4574 int l = length;
4575
4576 /* If there is only one level of template parameters, we do not
4577 create a TREE_VEC of TREE_VECs. Instead, we return a single
4578 TREE_VEC containing the arguments. */
4579 if (length > 1)
4580 args = make_tree_vec (length);
4581
4582 for (header = parms; header; header = TREE_CHAIN (header))
4583 {
4584 tree a = template_parms_level_to_args (TREE_VALUE (header));
4585
4586 if (length > 1)
4587 TREE_VEC_ELT (args, --l) = a;
4588 else
4589 args = a;
4590 }
4591
4592 return args;
4593 }
4594
4595 /* Within the declaration of a template, return the currently active
4596 template parameters as an argument TREE_VEC. */
4597
4598 static tree
4599 current_template_args (void)
4600 {
4601 return template_parms_to_args (current_template_parms);
4602 }
4603
4604 /* Update the declared TYPE by doing any lookups which were thought to be
4605 dependent, but are not now that we know the SCOPE of the declarator. */
4606
4607 tree
4608 maybe_update_decl_type (tree orig_type, tree scope)
4609 {
4610 tree type = orig_type;
4611
4612 if (type == NULL_TREE)
4613 return type;
4614
4615 if (TREE_CODE (orig_type) == TYPE_DECL)
4616 type = TREE_TYPE (type);
4617
4618 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4619 && dependent_type_p (type)
4620 /* Don't bother building up the args in this case. */
4621 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4622 {
4623 /* tsubst in the args corresponding to the template parameters,
4624 including auto if present. Most things will be unchanged, but
4625 make_typename_type and tsubst_qualified_id will resolve
4626 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4627 tree args = current_template_args ();
4628 tree auto_node = type_uses_auto (type);
4629 tree pushed;
4630 if (auto_node)
4631 {
4632 tree auto_vec = make_tree_vec (1);
4633 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4634 args = add_to_template_args (args, auto_vec);
4635 }
4636 pushed = push_scope (scope);
4637 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4638 if (pushed)
4639 pop_scope (scope);
4640 }
4641
4642 if (type == error_mark_node)
4643 return orig_type;
4644
4645 if (TREE_CODE (orig_type) == TYPE_DECL)
4646 {
4647 if (same_type_p (type, TREE_TYPE (orig_type)))
4648 type = orig_type;
4649 else
4650 type = TYPE_NAME (type);
4651 }
4652 return type;
4653 }
4654
4655 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4656 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4657 the new template is a member template. */
4658
4659 static tree
4660 build_template_decl (tree decl, tree parms, bool member_template_p)
4661 {
4662 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4663 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4664 DECL_TEMPLATE_PARMS (tmpl) = parms;
4665 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4666 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4667 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4668
4669 return tmpl;
4670 }
4671
4672 struct template_parm_data
4673 {
4674 /* The level of the template parameters we are currently
4675 processing. */
4676 int level;
4677
4678 /* The index of the specialization argument we are currently
4679 processing. */
4680 int current_arg;
4681
4682 /* An array whose size is the number of template parameters. The
4683 elements are nonzero if the parameter has been used in any one
4684 of the arguments processed so far. */
4685 int* parms;
4686
4687 /* An array whose size is the number of template arguments. The
4688 elements are nonzero if the argument makes use of template
4689 parameters of this level. */
4690 int* arg_uses_template_parms;
4691 };
4692
4693 /* Subroutine of push_template_decl used to see if each template
4694 parameter in a partial specialization is used in the explicit
4695 argument list. If T is of the LEVEL given in DATA (which is
4696 treated as a template_parm_data*), then DATA->PARMS is marked
4697 appropriately. */
4698
4699 static int
4700 mark_template_parm (tree t, void* data)
4701 {
4702 int level;
4703 int idx;
4704 struct template_parm_data* tpd = (struct template_parm_data*) data;
4705
4706 template_parm_level_and_index (t, &level, &idx);
4707
4708 if (level == tpd->level)
4709 {
4710 tpd->parms[idx] = 1;
4711 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4712 }
4713
4714 /* In C++17 the type of a non-type argument is a deduced context. */
4715 if (cxx_dialect >= cxx17
4716 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4717 for_each_template_parm (TREE_TYPE (t),
4718 &mark_template_parm,
4719 data,
4720 NULL,
4721 /*include_nondeduced_p=*/false);
4722
4723 /* Return zero so that for_each_template_parm will continue the
4724 traversal of the tree; we want to mark *every* template parm. */
4725 return 0;
4726 }
4727
4728 /* Process the partial specialization DECL. */
4729
4730 static tree
4731 process_partial_specialization (tree decl)
4732 {
4733 tree type = TREE_TYPE (decl);
4734 tree tinfo = get_template_info (decl);
4735 tree maintmpl = TI_TEMPLATE (tinfo);
4736 tree specargs = TI_ARGS (tinfo);
4737 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4738 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4739 tree inner_parms;
4740 tree inst;
4741 int nargs = TREE_VEC_LENGTH (inner_args);
4742 int ntparms;
4743 int i;
4744 bool did_error_intro = false;
4745 struct template_parm_data tpd;
4746 struct template_parm_data tpd2;
4747
4748 gcc_assert (current_template_parms);
4749
4750 /* A concept cannot be specialized. */
4751 if (flag_concepts && variable_concept_p (maintmpl))
4752 {
4753 error ("specialization of variable concept %q#D", maintmpl);
4754 return error_mark_node;
4755 }
4756
4757 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4758 ntparms = TREE_VEC_LENGTH (inner_parms);
4759
4760 /* We check that each of the template parameters given in the
4761 partial specialization is used in the argument list to the
4762 specialization. For example:
4763
4764 template <class T> struct S;
4765 template <class T> struct S<T*>;
4766
4767 The second declaration is OK because `T*' uses the template
4768 parameter T, whereas
4769
4770 template <class T> struct S<int>;
4771
4772 is no good. Even trickier is:
4773
4774 template <class T>
4775 struct S1
4776 {
4777 template <class U>
4778 struct S2;
4779 template <class U>
4780 struct S2<T>;
4781 };
4782
4783 The S2<T> declaration is actually invalid; it is a
4784 full-specialization. Of course,
4785
4786 template <class U>
4787 struct S2<T (*)(U)>;
4788
4789 or some such would have been OK. */
4790 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4791 tpd.parms = XALLOCAVEC (int, ntparms);
4792 memset (tpd.parms, 0, sizeof (int) * ntparms);
4793
4794 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4795 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4796 for (i = 0; i < nargs; ++i)
4797 {
4798 tpd.current_arg = i;
4799 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4800 &mark_template_parm,
4801 &tpd,
4802 NULL,
4803 /*include_nondeduced_p=*/false);
4804 }
4805 for (i = 0; i < ntparms; ++i)
4806 if (tpd.parms[i] == 0)
4807 {
4808 /* One of the template parms was not used in a deduced context in the
4809 specialization. */
4810 if (!did_error_intro)
4811 {
4812 error ("template parameters not deducible in "
4813 "partial specialization:");
4814 did_error_intro = true;
4815 }
4816
4817 inform (input_location, " %qD",
4818 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4819 }
4820
4821 if (did_error_intro)
4822 return error_mark_node;
4823
4824 /* [temp.class.spec]
4825
4826 The argument list of the specialization shall not be identical to
4827 the implicit argument list of the primary template. */
4828 tree main_args
4829 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4830 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4831 && (!flag_concepts
4832 || !strictly_subsumes (current_template_constraints (),
4833 get_constraints (maintmpl))))
4834 {
4835 if (!flag_concepts)
4836 error ("partial specialization %q+D does not specialize "
4837 "any template arguments; to define the primary template, "
4838 "remove the template argument list", decl);
4839 else
4840 error ("partial specialization %q+D does not specialize any "
4841 "template arguments and is not more constrained than "
4842 "the primary template; to define the primary template, "
4843 "remove the template argument list", decl);
4844 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4845 }
4846
4847 /* A partial specialization that replaces multiple parameters of the
4848 primary template with a pack expansion is less specialized for those
4849 parameters. */
4850 if (nargs < DECL_NTPARMS (maintmpl))
4851 {
4852 error ("partial specialization is not more specialized than the "
4853 "primary template because it replaces multiple parameters "
4854 "with a pack expansion");
4855 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4856 /* Avoid crash in process_partial_specialization. */
4857 return decl;
4858 }
4859
4860 /* If we aren't in a dependent class, we can actually try deduction. */
4861 else if (tpd.level == 1
4862 /* FIXME we should be able to handle a partial specialization of a
4863 partial instantiation, but currently we can't (c++/41727). */
4864 && TMPL_ARGS_DEPTH (specargs) == 1
4865 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4866 {
4867 auto_diagnostic_group d;
4868 if (permerror (input_location, "partial specialization %qD is not "
4869 "more specialized than", decl))
4870 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4871 maintmpl);
4872 }
4873
4874 /* [temp.class.spec]
4875
4876 A partially specialized non-type argument expression shall not
4877 involve template parameters of the partial specialization except
4878 when the argument expression is a simple identifier.
4879
4880 The type of a template parameter corresponding to a specialized
4881 non-type argument shall not be dependent on a parameter of the
4882 specialization.
4883
4884 Also, we verify that pack expansions only occur at the
4885 end of the argument list. */
4886 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4887 tpd2.parms = 0;
4888 for (i = 0; i < nargs; ++i)
4889 {
4890 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4891 tree arg = TREE_VEC_ELT (inner_args, i);
4892 tree packed_args = NULL_TREE;
4893 int j, len = 1;
4894
4895 if (ARGUMENT_PACK_P (arg))
4896 {
4897 /* Extract the arguments from the argument pack. We'll be
4898 iterating over these in the following loop. */
4899 packed_args = ARGUMENT_PACK_ARGS (arg);
4900 len = TREE_VEC_LENGTH (packed_args);
4901 }
4902
4903 for (j = 0; j < len; j++)
4904 {
4905 if (packed_args)
4906 /* Get the Jth argument in the parameter pack. */
4907 arg = TREE_VEC_ELT (packed_args, j);
4908
4909 if (PACK_EXPANSION_P (arg))
4910 {
4911 /* Pack expansions must come at the end of the
4912 argument list. */
4913 if ((packed_args && j < len - 1)
4914 || (!packed_args && i < nargs - 1))
4915 {
4916 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4917 error ("parameter pack argument %qE must be at the "
4918 "end of the template argument list", arg);
4919 else
4920 error ("parameter pack argument %qT must be at the "
4921 "end of the template argument list", arg);
4922 }
4923 }
4924
4925 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4926 /* We only care about the pattern. */
4927 arg = PACK_EXPANSION_PATTERN (arg);
4928
4929 if (/* These first two lines are the `non-type' bit. */
4930 !TYPE_P (arg)
4931 && TREE_CODE (arg) != TEMPLATE_DECL
4932 /* This next two lines are the `argument expression is not just a
4933 simple identifier' condition and also the `specialized
4934 non-type argument' bit. */
4935 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4936 && !(REFERENCE_REF_P (arg)
4937 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4938 {
4939 if ((!packed_args && tpd.arg_uses_template_parms[i])
4940 || (packed_args && uses_template_parms (arg)))
4941 error ("template argument %qE involves template parameter(s)",
4942 arg);
4943 else
4944 {
4945 /* Look at the corresponding template parameter,
4946 marking which template parameters its type depends
4947 upon. */
4948 tree type = TREE_TYPE (parm);
4949
4950 if (!tpd2.parms)
4951 {
4952 /* We haven't yet initialized TPD2. Do so now. */
4953 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4954 /* The number of parameters here is the number in the
4955 main template, which, as checked in the assertion
4956 above, is NARGS. */
4957 tpd2.parms = XALLOCAVEC (int, nargs);
4958 tpd2.level =
4959 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4960 }
4961
4962 /* Mark the template parameters. But this time, we're
4963 looking for the template parameters of the main
4964 template, not in the specialization. */
4965 tpd2.current_arg = i;
4966 tpd2.arg_uses_template_parms[i] = 0;
4967 memset (tpd2.parms, 0, sizeof (int) * nargs);
4968 for_each_template_parm (type,
4969 &mark_template_parm,
4970 &tpd2,
4971 NULL,
4972 /*include_nondeduced_p=*/false);
4973
4974 if (tpd2.arg_uses_template_parms [i])
4975 {
4976 /* The type depended on some template parameters.
4977 If they are fully specialized in the
4978 specialization, that's OK. */
4979 int j;
4980 int count = 0;
4981 for (j = 0; j < nargs; ++j)
4982 if (tpd2.parms[j] != 0
4983 && tpd.arg_uses_template_parms [j])
4984 ++count;
4985 if (count != 0)
4986 error_n (input_location, count,
4987 "type %qT of template argument %qE depends "
4988 "on a template parameter",
4989 "type %qT of template argument %qE depends "
4990 "on template parameters",
4991 type,
4992 arg);
4993 }
4994 }
4995 }
4996 }
4997 }
4998
4999 /* We should only get here once. */
5000 if (TREE_CODE (decl) == TYPE_DECL)
5001 gcc_assert (!COMPLETE_TYPE_P (type));
5002
5003 // Build the template decl.
5004 tree tmpl = build_template_decl (decl, current_template_parms,
5005 DECL_MEMBER_TEMPLATE_P (maintmpl));
5006 TREE_TYPE (tmpl) = type;
5007 DECL_TEMPLATE_RESULT (tmpl) = decl;
5008 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5009 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5010 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5011
5012 /* Give template template parms a DECL_CONTEXT of the template
5013 for which they are a parameter. */
5014 for (i = 0; i < ntparms; ++i)
5015 {
5016 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5017 if (TREE_CODE (parm) == TEMPLATE_DECL)
5018 DECL_CONTEXT (parm) = tmpl;
5019 }
5020
5021 if (VAR_P (decl))
5022 /* We didn't register this in check_explicit_specialization so we could
5023 wait until the constraints were set. */
5024 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5025 else
5026 associate_classtype_constraints (type);
5027
5028 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5029 = tree_cons (specargs, tmpl,
5030 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5031 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5032
5033 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5034 inst = TREE_CHAIN (inst))
5035 {
5036 tree instance = TREE_VALUE (inst);
5037 if (TYPE_P (instance)
5038 ? (COMPLETE_TYPE_P (instance)
5039 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5040 : DECL_TEMPLATE_INSTANTIATION (instance))
5041 {
5042 tree spec = most_specialized_partial_spec (instance, tf_none);
5043 tree inst_decl = (DECL_P (instance)
5044 ? instance : TYPE_NAME (instance));
5045 if (!spec)
5046 /* OK */;
5047 else if (spec == error_mark_node)
5048 permerror (input_location,
5049 "declaration of %qD ambiguates earlier template "
5050 "instantiation for %qD", decl, inst_decl);
5051 else if (TREE_VALUE (spec) == tmpl)
5052 permerror (input_location,
5053 "partial specialization of %qD after instantiation "
5054 "of %qD", decl, inst_decl);
5055 }
5056 }
5057
5058 return decl;
5059 }
5060
5061 /* PARM is a template parameter of some form; return the corresponding
5062 TEMPLATE_PARM_INDEX. */
5063
5064 static tree
5065 get_template_parm_index (tree parm)
5066 {
5067 if (TREE_CODE (parm) == PARM_DECL
5068 || TREE_CODE (parm) == CONST_DECL)
5069 parm = DECL_INITIAL (parm);
5070 else if (TREE_CODE (parm) == TYPE_DECL
5071 || TREE_CODE (parm) == TEMPLATE_DECL)
5072 parm = TREE_TYPE (parm);
5073 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5074 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5075 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5076 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5077 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5078 return parm;
5079 }
5080
5081 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5082 parameter packs used by the template parameter PARM. */
5083
5084 static void
5085 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5086 {
5087 /* A type parm can't refer to another parm. */
5088 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5089 return;
5090 else if (TREE_CODE (parm) == PARM_DECL)
5091 {
5092 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5093 ppd, ppd->visited);
5094 return;
5095 }
5096
5097 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5098
5099 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5100 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5101 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5102 }
5103
5104 /* PARM is a template parameter pack. Return any parameter packs used in
5105 its type or the type of any of its template parameters. If there are
5106 any such packs, it will be instantiated into a fixed template parameter
5107 list by partial instantiation rather than be fully deduced. */
5108
5109 tree
5110 fixed_parameter_pack_p (tree parm)
5111 {
5112 /* This can only be true in a member template. */
5113 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5114 return NULL_TREE;
5115 /* This can only be true for a parameter pack. */
5116 if (!template_parameter_pack_p (parm))
5117 return NULL_TREE;
5118 /* A type parm can't refer to another parm. */
5119 if (TREE_CODE (parm) == TYPE_DECL)
5120 return NULL_TREE;
5121
5122 tree parameter_packs = NULL_TREE;
5123 struct find_parameter_pack_data ppd;
5124 ppd.parameter_packs = &parameter_packs;
5125 ppd.visited = new hash_set<tree>;
5126 ppd.type_pack_expansion_p = false;
5127
5128 fixed_parameter_pack_p_1 (parm, &ppd);
5129
5130 delete ppd.visited;
5131 return parameter_packs;
5132 }
5133
5134 /* Check that a template declaration's use of default arguments and
5135 parameter packs is not invalid. Here, PARMS are the template
5136 parameters. IS_PRIMARY is true if DECL is the thing declared by
5137 a primary template. IS_PARTIAL is true if DECL is a partial
5138 specialization.
5139
5140 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5141 function template declaration or a friend class template
5142 declaration. In the function case, 1 indicates a declaration, 2
5143 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5144 emitted for extraneous default arguments.
5145
5146 Returns TRUE if there were no errors found, FALSE otherwise. */
5147
5148 bool
5149 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5150 bool is_partial, int is_friend_decl)
5151 {
5152 const char *msg;
5153 int last_level_to_check;
5154 tree parm_level;
5155 bool no_errors = true;
5156
5157 /* [temp.param]
5158
5159 A default template-argument shall not be specified in a
5160 function template declaration or a function template definition, nor
5161 in the template-parameter-list of the definition of a member of a
5162 class template. */
5163
5164 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5165 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5166 /* You can't have a function template declaration in a local
5167 scope, nor you can you define a member of a class template in a
5168 local scope. */
5169 return true;
5170
5171 if ((TREE_CODE (decl) == TYPE_DECL
5172 && TREE_TYPE (decl)
5173 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5174 || (TREE_CODE (decl) == FUNCTION_DECL
5175 && LAMBDA_FUNCTION_P (decl)))
5176 /* A lambda doesn't have an explicit declaration; don't complain
5177 about the parms of the enclosing class. */
5178 return true;
5179
5180 if (current_class_type
5181 && !TYPE_BEING_DEFINED (current_class_type)
5182 && DECL_LANG_SPECIFIC (decl)
5183 && DECL_DECLARES_FUNCTION_P (decl)
5184 /* If this is either a friend defined in the scope of the class
5185 or a member function. */
5186 && (DECL_FUNCTION_MEMBER_P (decl)
5187 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5188 : DECL_FRIEND_CONTEXT (decl)
5189 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5190 : false)
5191 /* And, if it was a member function, it really was defined in
5192 the scope of the class. */
5193 && (!DECL_FUNCTION_MEMBER_P (decl)
5194 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5195 /* We already checked these parameters when the template was
5196 declared, so there's no need to do it again now. This function
5197 was defined in class scope, but we're processing its body now
5198 that the class is complete. */
5199 return true;
5200
5201 /* Core issue 226 (C++0x only): the following only applies to class
5202 templates. */
5203 if (is_primary
5204 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5205 {
5206 /* [temp.param]
5207
5208 If a template-parameter has a default template-argument, all
5209 subsequent template-parameters shall have a default
5210 template-argument supplied. */
5211 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5212 {
5213 tree inner_parms = TREE_VALUE (parm_level);
5214 int ntparms = TREE_VEC_LENGTH (inner_parms);
5215 int seen_def_arg_p = 0;
5216 int i;
5217
5218 for (i = 0; i < ntparms; ++i)
5219 {
5220 tree parm = TREE_VEC_ELT (inner_parms, i);
5221
5222 if (parm == error_mark_node)
5223 continue;
5224
5225 if (TREE_PURPOSE (parm))
5226 seen_def_arg_p = 1;
5227 else if (seen_def_arg_p
5228 && !template_parameter_pack_p (TREE_VALUE (parm)))
5229 {
5230 error ("no default argument for %qD", TREE_VALUE (parm));
5231 /* For better subsequent error-recovery, we indicate that
5232 there should have been a default argument. */
5233 TREE_PURPOSE (parm) = error_mark_node;
5234 no_errors = false;
5235 }
5236 else if (!is_partial
5237 && !is_friend_decl
5238 /* Don't complain about an enclosing partial
5239 specialization. */
5240 && parm_level == parms
5241 && TREE_CODE (decl) == TYPE_DECL
5242 && i < ntparms - 1
5243 && template_parameter_pack_p (TREE_VALUE (parm))
5244 /* A fixed parameter pack will be partially
5245 instantiated into a fixed length list. */
5246 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5247 {
5248 /* A primary class template can only have one
5249 parameter pack, at the end of the template
5250 parameter list. */
5251
5252 error ("parameter pack %q+D must be at the end of the"
5253 " template parameter list", TREE_VALUE (parm));
5254
5255 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5256 = error_mark_node;
5257 no_errors = false;
5258 }
5259 }
5260 }
5261 }
5262
5263 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5264 || is_partial
5265 || !is_primary
5266 || is_friend_decl)
5267 /* For an ordinary class template, default template arguments are
5268 allowed at the innermost level, e.g.:
5269 template <class T = int>
5270 struct S {};
5271 but, in a partial specialization, they're not allowed even
5272 there, as we have in [temp.class.spec]:
5273
5274 The template parameter list of a specialization shall not
5275 contain default template argument values.
5276
5277 So, for a partial specialization, or for a function template
5278 (in C++98/C++03), we look at all of them. */
5279 ;
5280 else
5281 /* But, for a primary class template that is not a partial
5282 specialization we look at all template parameters except the
5283 innermost ones. */
5284 parms = TREE_CHAIN (parms);
5285
5286 /* Figure out what error message to issue. */
5287 if (is_friend_decl == 2)
5288 msg = G_("default template arguments may not be used in function template "
5289 "friend re-declaration");
5290 else if (is_friend_decl)
5291 msg = G_("default template arguments may not be used in template "
5292 "friend declarations");
5293 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5294 msg = G_("default template arguments may not be used in function templates "
5295 "without -std=c++11 or -std=gnu++11");
5296 else if (is_partial)
5297 msg = G_("default template arguments may not be used in "
5298 "partial specializations");
5299 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5300 msg = G_("default argument for template parameter for class enclosing %qD");
5301 else
5302 /* Per [temp.param]/9, "A default template-argument shall not be
5303 specified in the template-parameter-lists of the definition of
5304 a member of a class template that appears outside of the member's
5305 class.", thus if we aren't handling a member of a class template
5306 there is no need to examine the parameters. */
5307 return true;
5308
5309 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5310 /* If we're inside a class definition, there's no need to
5311 examine the parameters to the class itself. On the one
5312 hand, they will be checked when the class is defined, and,
5313 on the other, default arguments are valid in things like:
5314 template <class T = double>
5315 struct S { template <class U> void f(U); };
5316 Here the default argument for `S' has no bearing on the
5317 declaration of `f'. */
5318 last_level_to_check = template_class_depth (current_class_type) + 1;
5319 else
5320 /* Check everything. */
5321 last_level_to_check = 0;
5322
5323 for (parm_level = parms;
5324 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5325 parm_level = TREE_CHAIN (parm_level))
5326 {
5327 tree inner_parms = TREE_VALUE (parm_level);
5328 int i;
5329 int ntparms;
5330
5331 ntparms = TREE_VEC_LENGTH (inner_parms);
5332 for (i = 0; i < ntparms; ++i)
5333 {
5334 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5335 continue;
5336
5337 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5338 {
5339 if (msg)
5340 {
5341 no_errors = false;
5342 if (is_friend_decl == 2)
5343 return no_errors;
5344
5345 error (msg, decl);
5346 msg = 0;
5347 }
5348
5349 /* Clear out the default argument so that we are not
5350 confused later. */
5351 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5352 }
5353 }
5354
5355 /* At this point, if we're still interested in issuing messages,
5356 they must apply to classes surrounding the object declared. */
5357 if (msg)
5358 msg = G_("default argument for template parameter for class "
5359 "enclosing %qD");
5360 }
5361
5362 return no_errors;
5363 }
5364
5365 /* Worker for push_template_decl_real, called via
5366 for_each_template_parm. DATA is really an int, indicating the
5367 level of the parameters we are interested in. If T is a template
5368 parameter of that level, return nonzero. */
5369
5370 static int
5371 template_parm_this_level_p (tree t, void* data)
5372 {
5373 int this_level = *(int *)data;
5374 int level;
5375
5376 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5377 level = TEMPLATE_PARM_LEVEL (t);
5378 else
5379 level = TEMPLATE_TYPE_LEVEL (t);
5380 return level == this_level;
5381 }
5382
5383 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5384 DATA is really an int, indicating the innermost outer level of parameters.
5385 If T is a template parameter of that level or further out, return
5386 nonzero. */
5387
5388 static int
5389 template_parm_outer_level (tree t, void *data)
5390 {
5391 int this_level = *(int *)data;
5392 int level;
5393
5394 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5395 level = TEMPLATE_PARM_LEVEL (t);
5396 else
5397 level = TEMPLATE_TYPE_LEVEL (t);
5398 return level <= this_level;
5399 }
5400
5401 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5402 parameters given by current_template_args, or reuses a
5403 previously existing one, if appropriate. Returns the DECL, or an
5404 equivalent one, if it is replaced via a call to duplicate_decls.
5405
5406 If IS_FRIEND is true, DECL is a friend declaration. */
5407
5408 tree
5409 push_template_decl_real (tree decl, bool is_friend)
5410 {
5411 tree tmpl;
5412 tree args;
5413 tree info;
5414 tree ctx;
5415 bool is_primary;
5416 bool is_partial;
5417 int new_template_p = 0;
5418 /* True if the template is a member template, in the sense of
5419 [temp.mem]. */
5420 bool member_template_p = false;
5421
5422 if (decl == error_mark_node || !current_template_parms)
5423 return error_mark_node;
5424
5425 /* See if this is a partial specialization. */
5426 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5427 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5428 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5429 || (VAR_P (decl)
5430 && DECL_LANG_SPECIFIC (decl)
5431 && DECL_TEMPLATE_SPECIALIZATION (decl)
5432 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5433
5434 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5435 is_friend = true;
5436
5437 if (is_friend)
5438 /* For a friend, we want the context of the friend, not
5439 the type of which it is a friend. */
5440 ctx = CP_DECL_CONTEXT (decl);
5441 else if (CP_DECL_CONTEXT (decl)
5442 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5443 /* In the case of a virtual function, we want the class in which
5444 it is defined. */
5445 ctx = CP_DECL_CONTEXT (decl);
5446 else
5447 /* Otherwise, if we're currently defining some class, the DECL
5448 is assumed to be a member of the class. */
5449 ctx = current_scope ();
5450
5451 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5452 ctx = NULL_TREE;
5453
5454 if (!DECL_CONTEXT (decl))
5455 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5456
5457 /* See if this is a primary template. */
5458 if (is_friend && ctx
5459 && uses_template_parms_level (ctx, processing_template_decl))
5460 /* A friend template that specifies a class context, i.e.
5461 template <typename T> friend void A<T>::f();
5462 is not primary. */
5463 is_primary = false;
5464 else if (TREE_CODE (decl) == TYPE_DECL
5465 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5466 is_primary = false;
5467 else
5468 is_primary = template_parm_scope_p ();
5469
5470 if (is_primary)
5471 {
5472 warning (OPT_Wtemplates, "template %qD declared", decl);
5473
5474 if (DECL_CLASS_SCOPE_P (decl))
5475 member_template_p = true;
5476 if (TREE_CODE (decl) == TYPE_DECL
5477 && anon_aggrname_p (DECL_NAME (decl)))
5478 {
5479 error ("template class without a name");
5480 return error_mark_node;
5481 }
5482 else if (TREE_CODE (decl) == FUNCTION_DECL)
5483 {
5484 if (member_template_p)
5485 {
5486 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5487 error ("member template %qD may not have virt-specifiers", decl);
5488 }
5489 if (DECL_DESTRUCTOR_P (decl))
5490 {
5491 /* [temp.mem]
5492
5493 A destructor shall not be a member template. */
5494 error ("destructor %qD declared as member template", decl);
5495 return error_mark_node;
5496 }
5497 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5498 && (!prototype_p (TREE_TYPE (decl))
5499 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5500 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5501 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5502 == void_list_node)))
5503 {
5504 /* [basic.stc.dynamic.allocation]
5505
5506 An allocation function can be a function
5507 template. ... Template allocation functions shall
5508 have two or more parameters. */
5509 error ("invalid template declaration of %qD", decl);
5510 return error_mark_node;
5511 }
5512 }
5513 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5514 && CLASS_TYPE_P (TREE_TYPE (decl)))
5515 {
5516 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5517 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5518 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5519 {
5520 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5521 if (TREE_CODE (t) == TYPE_DECL)
5522 t = TREE_TYPE (t);
5523 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5524 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5525 }
5526 }
5527 else if (TREE_CODE (decl) == TYPE_DECL
5528 && TYPE_DECL_ALIAS_P (decl))
5529 /* alias-declaration */
5530 gcc_assert (!DECL_ARTIFICIAL (decl));
5531 else if (VAR_P (decl))
5532 /* C++14 variable template. */;
5533 else
5534 {
5535 error ("template declaration of %q#D", decl);
5536 return error_mark_node;
5537 }
5538 }
5539
5540 /* Check to see that the rules regarding the use of default
5541 arguments are not being violated. We check args for a friend
5542 functions when we know whether it's a definition, introducing
5543 declaration or re-declaration. */
5544 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5545 check_default_tmpl_args (decl, current_template_parms,
5546 is_primary, is_partial, is_friend);
5547
5548 /* Ensure that there are no parameter packs in the type of this
5549 declaration that have not been expanded. */
5550 if (TREE_CODE (decl) == FUNCTION_DECL)
5551 {
5552 /* Check each of the arguments individually to see if there are
5553 any bare parameter packs. */
5554 tree type = TREE_TYPE (decl);
5555 tree arg = DECL_ARGUMENTS (decl);
5556 tree argtype = TYPE_ARG_TYPES (type);
5557
5558 while (arg && argtype)
5559 {
5560 if (!DECL_PACK_P (arg)
5561 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5562 {
5563 /* This is a PARM_DECL that contains unexpanded parameter
5564 packs. We have already complained about this in the
5565 check_for_bare_parameter_packs call, so just replace
5566 these types with ERROR_MARK_NODE. */
5567 TREE_TYPE (arg) = error_mark_node;
5568 TREE_VALUE (argtype) = error_mark_node;
5569 }
5570
5571 arg = DECL_CHAIN (arg);
5572 argtype = TREE_CHAIN (argtype);
5573 }
5574
5575 /* Check for bare parameter packs in the return type and the
5576 exception specifiers. */
5577 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5578 /* Errors were already issued, set return type to int
5579 as the frontend doesn't expect error_mark_node as
5580 the return type. */
5581 TREE_TYPE (type) = integer_type_node;
5582 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5583 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5584 }
5585 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5586 && TYPE_DECL_ALIAS_P (decl))
5587 ? DECL_ORIGINAL_TYPE (decl)
5588 : TREE_TYPE (decl)))
5589 {
5590 TREE_TYPE (decl) = error_mark_node;
5591 return error_mark_node;
5592 }
5593
5594 if (is_partial)
5595 return process_partial_specialization (decl);
5596
5597 args = current_template_args ();
5598
5599 if (!ctx
5600 || TREE_CODE (ctx) == FUNCTION_DECL
5601 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5602 || (TREE_CODE (decl) == TYPE_DECL
5603 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5604 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5605 {
5606 if (DECL_LANG_SPECIFIC (decl)
5607 && DECL_TEMPLATE_INFO (decl)
5608 && DECL_TI_TEMPLATE (decl))
5609 tmpl = DECL_TI_TEMPLATE (decl);
5610 /* If DECL is a TYPE_DECL for a class-template, then there won't
5611 be DECL_LANG_SPECIFIC. The information equivalent to
5612 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5613 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5614 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5615 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5616 {
5617 /* Since a template declaration already existed for this
5618 class-type, we must be redeclaring it here. Make sure
5619 that the redeclaration is valid. */
5620 redeclare_class_template (TREE_TYPE (decl),
5621 current_template_parms,
5622 current_template_constraints ());
5623 /* We don't need to create a new TEMPLATE_DECL; just use the
5624 one we already had. */
5625 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5626 }
5627 else
5628 {
5629 tmpl = build_template_decl (decl, current_template_parms,
5630 member_template_p);
5631 new_template_p = 1;
5632
5633 if (DECL_LANG_SPECIFIC (decl)
5634 && DECL_TEMPLATE_SPECIALIZATION (decl))
5635 {
5636 /* A specialization of a member template of a template
5637 class. */
5638 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5639 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5640 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5641 }
5642 }
5643 }
5644 else
5645 {
5646 tree a, t, current, parms;
5647 int i;
5648 tree tinfo = get_template_info (decl);
5649
5650 if (!tinfo)
5651 {
5652 error ("template definition of non-template %q#D", decl);
5653 return error_mark_node;
5654 }
5655
5656 tmpl = TI_TEMPLATE (tinfo);
5657
5658 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5659 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5660 && DECL_TEMPLATE_SPECIALIZATION (decl)
5661 && DECL_MEMBER_TEMPLATE_P (tmpl))
5662 {
5663 tree new_tmpl;
5664
5665 /* The declaration is a specialization of a member
5666 template, declared outside the class. Therefore, the
5667 innermost template arguments will be NULL, so we
5668 replace them with the arguments determined by the
5669 earlier call to check_explicit_specialization. */
5670 args = DECL_TI_ARGS (decl);
5671
5672 new_tmpl
5673 = build_template_decl (decl, current_template_parms,
5674 member_template_p);
5675 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5676 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5677 DECL_TI_TEMPLATE (decl) = new_tmpl;
5678 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5679 DECL_TEMPLATE_INFO (new_tmpl)
5680 = build_template_info (tmpl, args);
5681
5682 register_specialization (new_tmpl,
5683 most_general_template (tmpl),
5684 args,
5685 is_friend, 0);
5686 return decl;
5687 }
5688
5689 /* Make sure the template headers we got make sense. */
5690
5691 parms = DECL_TEMPLATE_PARMS (tmpl);
5692 i = TMPL_PARMS_DEPTH (parms);
5693 if (TMPL_ARGS_DEPTH (args) != i)
5694 {
5695 error ("expected %d levels of template parms for %q#D, got %d",
5696 i, decl, TMPL_ARGS_DEPTH (args));
5697 DECL_INTERFACE_KNOWN (decl) = 1;
5698 return error_mark_node;
5699 }
5700 else
5701 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5702 {
5703 a = TMPL_ARGS_LEVEL (args, i);
5704 t = INNERMOST_TEMPLATE_PARMS (parms);
5705
5706 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5707 {
5708 if (current == decl)
5709 error ("got %d template parameters for %q#D",
5710 TREE_VEC_LENGTH (a), decl);
5711 else
5712 error ("got %d template parameters for %q#T",
5713 TREE_VEC_LENGTH (a), current);
5714 error (" but %d required", TREE_VEC_LENGTH (t));
5715 /* Avoid crash in import_export_decl. */
5716 DECL_INTERFACE_KNOWN (decl) = 1;
5717 return error_mark_node;
5718 }
5719
5720 if (current == decl)
5721 current = ctx;
5722 else if (current == NULL_TREE)
5723 /* Can happen in erroneous input. */
5724 break;
5725 else
5726 current = get_containing_scope (current);
5727 }
5728
5729 /* Check that the parms are used in the appropriate qualifying scopes
5730 in the declarator. */
5731 if (!comp_template_args
5732 (TI_ARGS (tinfo),
5733 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5734 {
5735 error ("template arguments to %qD do not match original "
5736 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5737 if (!uses_template_parms (TI_ARGS (tinfo)))
5738 inform (input_location, "use %<template<>%> for"
5739 " an explicit specialization");
5740 /* Avoid crash in import_export_decl. */
5741 DECL_INTERFACE_KNOWN (decl) = 1;
5742 return error_mark_node;
5743 }
5744 }
5745
5746 DECL_TEMPLATE_RESULT (tmpl) = decl;
5747 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5748
5749 /* Push template declarations for global functions and types. Note
5750 that we do not try to push a global template friend declared in a
5751 template class; such a thing may well depend on the template
5752 parameters of the class. */
5753 if (new_template_p && !ctx
5754 && !(is_friend && template_class_depth (current_class_type) > 0))
5755 {
5756 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5757 if (tmpl == error_mark_node)
5758 return error_mark_node;
5759
5760 /* Hide template friend classes that haven't been declared yet. */
5761 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5762 {
5763 DECL_ANTICIPATED (tmpl) = 1;
5764 DECL_FRIEND_P (tmpl) = 1;
5765 }
5766 }
5767
5768 if (is_primary)
5769 {
5770 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5771
5772 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5773
5774 /* Give template template parms a DECL_CONTEXT of the template
5775 for which they are a parameter. */
5776 parms = INNERMOST_TEMPLATE_PARMS (parms);
5777 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5778 {
5779 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5780 if (TREE_CODE (parm) == TEMPLATE_DECL)
5781 DECL_CONTEXT (parm) = tmpl;
5782 }
5783
5784 if (TREE_CODE (decl) == TYPE_DECL
5785 && TYPE_DECL_ALIAS_P (decl)
5786 && complex_alias_template_p (tmpl))
5787 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5788 }
5789
5790 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5791 back to its most general template. If TMPL is a specialization,
5792 ARGS may only have the innermost set of arguments. Add the missing
5793 argument levels if necessary. */
5794 if (DECL_TEMPLATE_INFO (tmpl))
5795 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5796
5797 info = build_template_info (tmpl, args);
5798
5799 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5800 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5801 else
5802 {
5803 if (is_primary)
5804 retrofit_lang_decl (decl);
5805 if (DECL_LANG_SPECIFIC (decl))
5806 DECL_TEMPLATE_INFO (decl) = info;
5807 }
5808
5809 if (flag_implicit_templates
5810 && !is_friend
5811 && TREE_PUBLIC (decl)
5812 && VAR_OR_FUNCTION_DECL_P (decl))
5813 /* Set DECL_COMDAT on template instantiations; if we force
5814 them to be emitted by explicit instantiation or -frepo,
5815 mark_needed will tell cgraph to do the right thing. */
5816 DECL_COMDAT (decl) = true;
5817
5818 return DECL_TEMPLATE_RESULT (tmpl);
5819 }
5820
5821 tree
5822 push_template_decl (tree decl)
5823 {
5824 return push_template_decl_real (decl, false);
5825 }
5826
5827 /* FN is an inheriting constructor that inherits from the constructor
5828 template INHERITED; turn FN into a constructor template with a matching
5829 template header. */
5830
5831 tree
5832 add_inherited_template_parms (tree fn, tree inherited)
5833 {
5834 tree inner_parms
5835 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5836 inner_parms = copy_node (inner_parms);
5837 tree parms
5838 = tree_cons (size_int (processing_template_decl + 1),
5839 inner_parms, current_template_parms);
5840 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5841 tree args = template_parms_to_args (parms);
5842 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5843 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5844 DECL_TEMPLATE_RESULT (tmpl) = fn;
5845 DECL_ARTIFICIAL (tmpl) = true;
5846 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5847 return tmpl;
5848 }
5849
5850 /* Called when a class template TYPE is redeclared with the indicated
5851 template PARMS, e.g.:
5852
5853 template <class T> struct S;
5854 template <class T> struct S {}; */
5855
5856 bool
5857 redeclare_class_template (tree type, tree parms, tree cons)
5858 {
5859 tree tmpl;
5860 tree tmpl_parms;
5861 int i;
5862
5863 if (!TYPE_TEMPLATE_INFO (type))
5864 {
5865 error ("%qT is not a template type", type);
5866 return false;
5867 }
5868
5869 tmpl = TYPE_TI_TEMPLATE (type);
5870 if (!PRIMARY_TEMPLATE_P (tmpl))
5871 /* The type is nested in some template class. Nothing to worry
5872 about here; there are no new template parameters for the nested
5873 type. */
5874 return true;
5875
5876 if (!parms)
5877 {
5878 error ("template specifiers not specified in declaration of %qD",
5879 tmpl);
5880 return false;
5881 }
5882
5883 parms = INNERMOST_TEMPLATE_PARMS (parms);
5884 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5885
5886 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5887 {
5888 error_n (input_location, TREE_VEC_LENGTH (parms),
5889 "redeclared with %d template parameter",
5890 "redeclared with %d template parameters",
5891 TREE_VEC_LENGTH (parms));
5892 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5893 "previous declaration %qD used %d template parameter",
5894 "previous declaration %qD used %d template parameters",
5895 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5896 return false;
5897 }
5898
5899 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5900 {
5901 tree tmpl_parm;
5902 tree parm;
5903 tree tmpl_default;
5904 tree parm_default;
5905
5906 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5907 || TREE_VEC_ELT (parms, i) == error_mark_node)
5908 continue;
5909
5910 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5911 if (error_operand_p (tmpl_parm))
5912 return false;
5913
5914 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5915 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5916 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5917
5918 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5919 TEMPLATE_DECL. */
5920 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5921 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5922 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5923 || (TREE_CODE (tmpl_parm) != PARM_DECL
5924 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5925 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5926 || (TREE_CODE (tmpl_parm) == PARM_DECL
5927 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5928 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5929 {
5930 error ("template parameter %q+#D", tmpl_parm);
5931 error ("redeclared here as %q#D", parm);
5932 return false;
5933 }
5934
5935 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5936 {
5937 /* We have in [temp.param]:
5938
5939 A template-parameter may not be given default arguments
5940 by two different declarations in the same scope. */
5941 error_at (input_location, "redefinition of default argument for %q#D", parm);
5942 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5943 "original definition appeared here");
5944 return false;
5945 }
5946
5947 if (parm_default != NULL_TREE)
5948 /* Update the previous template parameters (which are the ones
5949 that will really count) with the new default value. */
5950 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5951 else if (tmpl_default != NULL_TREE)
5952 /* Update the new parameters, too; they'll be used as the
5953 parameters for any members. */
5954 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5955
5956 /* Give each template template parm in this redeclaration a
5957 DECL_CONTEXT of the template for which they are a parameter. */
5958 if (TREE_CODE (parm) == TEMPLATE_DECL)
5959 {
5960 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5961 DECL_CONTEXT (parm) = tmpl;
5962 }
5963
5964 if (TREE_CODE (parm) == TYPE_DECL)
5965 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5966 }
5967
5968 // Cannot redeclare a class template with a different set of constraints.
5969 if (!equivalent_constraints (get_constraints (tmpl), cons))
5970 {
5971 error_at (input_location, "redeclaration %q#D with different "
5972 "constraints", tmpl);
5973 inform (DECL_SOURCE_LOCATION (tmpl),
5974 "original declaration appeared here");
5975 }
5976
5977 return true;
5978 }
5979
5980 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5981 to be used when the caller has already checked
5982 (processing_template_decl
5983 && !instantiation_dependent_expression_p (expr)
5984 && potential_constant_expression (expr))
5985 and cleared processing_template_decl. */
5986
5987 tree
5988 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5989 {
5990 return tsubst_copy_and_build (expr,
5991 /*args=*/NULL_TREE,
5992 complain,
5993 /*in_decl=*/NULL_TREE,
5994 /*function_p=*/false,
5995 /*integral_constant_expression_p=*/true);
5996 }
5997
5998 /* Simplify EXPR if it is a non-dependent expression. Returns the
5999 (possibly simplified) expression. */
6000
6001 tree
6002 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6003 {
6004 if (expr == NULL_TREE)
6005 return NULL_TREE;
6006
6007 /* If we're in a template, but EXPR isn't value dependent, simplify
6008 it. We're supposed to treat:
6009
6010 template <typename T> void f(T[1 + 1]);
6011 template <typename T> void f(T[2]);
6012
6013 as two declarations of the same function, for example. */
6014 if (processing_template_decl
6015 && is_nondependent_constant_expression (expr))
6016 {
6017 processing_template_decl_sentinel s;
6018 expr = instantiate_non_dependent_expr_internal (expr, complain);
6019 }
6020 return expr;
6021 }
6022
6023 tree
6024 instantiate_non_dependent_expr (tree expr)
6025 {
6026 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6027 }
6028
6029 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6030 an uninstantiated expression. */
6031
6032 tree
6033 instantiate_non_dependent_or_null (tree expr)
6034 {
6035 if (expr == NULL_TREE)
6036 return NULL_TREE;
6037 if (processing_template_decl)
6038 {
6039 if (!is_nondependent_constant_expression (expr))
6040 expr = NULL_TREE;
6041 else
6042 {
6043 processing_template_decl_sentinel s;
6044 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6045 }
6046 }
6047 return expr;
6048 }
6049
6050 /* True iff T is a specialization of a variable template. */
6051
6052 bool
6053 variable_template_specialization_p (tree t)
6054 {
6055 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6056 return false;
6057 tree tmpl = DECL_TI_TEMPLATE (t);
6058 return variable_template_p (tmpl);
6059 }
6060
6061 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6062 template declaration, or a TYPE_DECL for an alias declaration. */
6063
6064 bool
6065 alias_type_or_template_p (tree t)
6066 {
6067 if (t == NULL_TREE)
6068 return false;
6069 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6070 || (TYPE_P (t)
6071 && TYPE_NAME (t)
6072 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6073 || DECL_ALIAS_TEMPLATE_P (t));
6074 }
6075
6076 /* Return TRUE iff T is a specialization of an alias template. */
6077
6078 bool
6079 alias_template_specialization_p (const_tree t)
6080 {
6081 /* It's an alias template specialization if it's an alias and its
6082 TYPE_NAME is a specialization of a primary template. */
6083 if (TYPE_ALIAS_P (t))
6084 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6085 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6086
6087 return false;
6088 }
6089
6090 /* An alias template is complex from a SFINAE perspective if a template-id
6091 using that alias can be ill-formed when the expansion is not, as with
6092 the void_t template. We determine this by checking whether the
6093 expansion for the alias template uses all its template parameters. */
6094
6095 struct uses_all_template_parms_data
6096 {
6097 int level;
6098 bool *seen;
6099 };
6100
6101 static int
6102 uses_all_template_parms_r (tree t, void *data_)
6103 {
6104 struct uses_all_template_parms_data &data
6105 = *(struct uses_all_template_parms_data*)data_;
6106 tree idx = get_template_parm_index (t);
6107
6108 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6109 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6110 return 0;
6111 }
6112
6113 static bool
6114 complex_alias_template_p (const_tree tmpl)
6115 {
6116 struct uses_all_template_parms_data data;
6117 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6118 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6119 data.level = TMPL_PARMS_DEPTH (parms);
6120 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6121 data.seen = XALLOCAVEC (bool, len);
6122 for (int i = 0; i < len; ++i)
6123 data.seen[i] = false;
6124
6125 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6126 for (int i = 0; i < len; ++i)
6127 if (!data.seen[i])
6128 return true;
6129 return false;
6130 }
6131
6132 /* Return TRUE iff T is a specialization of a complex alias template with
6133 dependent template-arguments. */
6134
6135 bool
6136 dependent_alias_template_spec_p (const_tree t)
6137 {
6138 if (!alias_template_specialization_p (t))
6139 return false;
6140
6141 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6142 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6143 return false;
6144
6145 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6146 if (!any_dependent_template_arguments_p (args))
6147 return false;
6148
6149 return true;
6150 }
6151
6152 /* Return the number of innermost template parameters in TMPL. */
6153
6154 static int
6155 num_innermost_template_parms (tree tmpl)
6156 {
6157 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6158 return TREE_VEC_LENGTH (parms);
6159 }
6160
6161 /* Return either TMPL or another template that it is equivalent to under DR
6162 1286: An alias that just changes the name of a template is equivalent to
6163 the other template. */
6164
6165 static tree
6166 get_underlying_template (tree tmpl)
6167 {
6168 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6169 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6170 {
6171 /* Determine if the alias is equivalent to an underlying template. */
6172 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6173 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6174 if (!tinfo)
6175 break;
6176
6177 tree underlying = TI_TEMPLATE (tinfo);
6178 if (!PRIMARY_TEMPLATE_P (underlying)
6179 || (num_innermost_template_parms (tmpl)
6180 != num_innermost_template_parms (underlying)))
6181 break;
6182
6183 tree alias_args = INNERMOST_TEMPLATE_ARGS
6184 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6185 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6186 break;
6187
6188 /* Alias is equivalent. Strip it and repeat. */
6189 tmpl = underlying;
6190 }
6191
6192 return tmpl;
6193 }
6194
6195 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6196 must be a reference-to-function or a pointer-to-function type, as specified
6197 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6198 and check that the resulting function has external linkage. */
6199
6200 static tree
6201 convert_nontype_argument_function (tree type, tree expr,
6202 tsubst_flags_t complain)
6203 {
6204 tree fns = expr;
6205 tree fn, fn_no_ptr;
6206 linkage_kind linkage;
6207
6208 fn = instantiate_type (type, fns, tf_none);
6209 if (fn == error_mark_node)
6210 return error_mark_node;
6211
6212 if (value_dependent_expression_p (fn))
6213 goto accept;
6214
6215 fn_no_ptr = strip_fnptr_conv (fn);
6216 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6217 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6218 if (BASELINK_P (fn_no_ptr))
6219 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6220
6221 /* [temp.arg.nontype]/1
6222
6223 A template-argument for a non-type, non-template template-parameter
6224 shall be one of:
6225 [...]
6226 -- the address of an object or function with external [C++11: or
6227 internal] linkage. */
6228
6229 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6230 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6231 {
6232 if (complain & tf_error)
6233 {
6234 error ("%qE is not a valid template argument for type %qT",
6235 expr, type);
6236 if (TYPE_PTR_P (type))
6237 inform (input_location, "it must be the address of a function "
6238 "with external linkage");
6239 else
6240 inform (input_location, "it must be the name of a function with "
6241 "external linkage");
6242 }
6243 return NULL_TREE;
6244 }
6245
6246 linkage = decl_linkage (fn_no_ptr);
6247 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6248 {
6249 if (complain & tf_error)
6250 {
6251 if (cxx_dialect >= cxx11)
6252 error ("%qE is not a valid template argument for type %qT "
6253 "because %qD has no linkage",
6254 expr, type, fn_no_ptr);
6255 else
6256 error ("%qE is not a valid template argument for type %qT "
6257 "because %qD does not have external linkage",
6258 expr, type, fn_no_ptr);
6259 }
6260 return NULL_TREE;
6261 }
6262
6263 accept:
6264 if (TYPE_REF_P (type))
6265 {
6266 if (REFERENCE_REF_P (fn))
6267 fn = TREE_OPERAND (fn, 0);
6268 else
6269 fn = build_address (fn);
6270 }
6271 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6272 fn = build_nop (type, fn);
6273
6274 return fn;
6275 }
6276
6277 /* Subroutine of convert_nontype_argument.
6278 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6279 Emit an error otherwise. */
6280
6281 static bool
6282 check_valid_ptrmem_cst_expr (tree type, tree expr,
6283 tsubst_flags_t complain)
6284 {
6285 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6286 tree orig_expr = expr;
6287 STRIP_NOPS (expr);
6288 if (null_ptr_cst_p (expr))
6289 return true;
6290 if (TREE_CODE (expr) == PTRMEM_CST
6291 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6292 PTRMEM_CST_CLASS (expr)))
6293 return true;
6294 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6295 return true;
6296 if (processing_template_decl
6297 && TREE_CODE (expr) == ADDR_EXPR
6298 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6299 return true;
6300 if (complain & tf_error)
6301 {
6302 error_at (loc, "%qE is not a valid template argument for type %qT",
6303 orig_expr, type);
6304 if (TREE_CODE (expr) != PTRMEM_CST)
6305 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6306 else
6307 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6308 }
6309 return false;
6310 }
6311
6312 /* Returns TRUE iff the address of OP is value-dependent.
6313
6314 14.6.2.4 [temp.dep.temp]:
6315 A non-integral non-type template-argument is dependent if its type is
6316 dependent or it has either of the following forms
6317 qualified-id
6318 & qualified-id
6319 and contains a nested-name-specifier which specifies a class-name that
6320 names a dependent type.
6321
6322 We generalize this to just say that the address of a member of a
6323 dependent class is value-dependent; the above doesn't cover the
6324 address of a static data member named with an unqualified-id. */
6325
6326 static bool
6327 has_value_dependent_address (tree op)
6328 {
6329 /* We could use get_inner_reference here, but there's no need;
6330 this is only relevant for template non-type arguments, which
6331 can only be expressed as &id-expression. */
6332 if (DECL_P (op))
6333 {
6334 tree ctx = CP_DECL_CONTEXT (op);
6335 if (TYPE_P (ctx) && dependent_type_p (ctx))
6336 return true;
6337 }
6338
6339 return false;
6340 }
6341
6342 /* The next set of functions are used for providing helpful explanatory
6343 diagnostics for failed overload resolution. Their messages should be
6344 indented by two spaces for consistency with the messages in
6345 call.c */
6346
6347 static int
6348 unify_success (bool /*explain_p*/)
6349 {
6350 return 0;
6351 }
6352
6353 /* Other failure functions should call this one, to provide a single function
6354 for setting a breakpoint on. */
6355
6356 static int
6357 unify_invalid (bool /*explain_p*/)
6358 {
6359 return 1;
6360 }
6361
6362 static int
6363 unify_parameter_deduction_failure (bool explain_p, tree parm)
6364 {
6365 if (explain_p)
6366 inform (input_location,
6367 " couldn't deduce template parameter %qD", parm);
6368 return unify_invalid (explain_p);
6369 }
6370
6371 static int
6372 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6373 {
6374 if (explain_p)
6375 inform (input_location,
6376 " types %qT and %qT have incompatible cv-qualifiers",
6377 parm, arg);
6378 return unify_invalid (explain_p);
6379 }
6380
6381 static int
6382 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6383 {
6384 if (explain_p)
6385 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6386 return unify_invalid (explain_p);
6387 }
6388
6389 static int
6390 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6391 {
6392 if (explain_p)
6393 inform (input_location,
6394 " template parameter %qD is not a parameter pack, but "
6395 "argument %qD is",
6396 parm, arg);
6397 return unify_invalid (explain_p);
6398 }
6399
6400 static int
6401 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6402 {
6403 if (explain_p)
6404 inform (input_location,
6405 " template argument %qE does not match "
6406 "pointer-to-member constant %qE",
6407 arg, parm);
6408 return unify_invalid (explain_p);
6409 }
6410
6411 static int
6412 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6413 {
6414 if (explain_p)
6415 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6416 return unify_invalid (explain_p);
6417 }
6418
6419 static int
6420 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6421 {
6422 if (explain_p)
6423 inform (input_location,
6424 " inconsistent parameter pack deduction with %qT and %qT",
6425 old_arg, new_arg);
6426 return unify_invalid (explain_p);
6427 }
6428
6429 static int
6430 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6431 {
6432 if (explain_p)
6433 {
6434 if (TYPE_P (parm))
6435 inform (input_location,
6436 " deduced conflicting types for parameter %qT (%qT and %qT)",
6437 parm, first, second);
6438 else
6439 inform (input_location,
6440 " deduced conflicting values for non-type parameter "
6441 "%qE (%qE and %qE)", parm, first, second);
6442 }
6443 return unify_invalid (explain_p);
6444 }
6445
6446 static int
6447 unify_vla_arg (bool explain_p, tree arg)
6448 {
6449 if (explain_p)
6450 inform (input_location,
6451 " variable-sized array type %qT is not "
6452 "a valid template argument",
6453 arg);
6454 return unify_invalid (explain_p);
6455 }
6456
6457 static int
6458 unify_method_type_error (bool explain_p, tree arg)
6459 {
6460 if (explain_p)
6461 inform (input_location,
6462 " member function type %qT is not a valid template argument",
6463 arg);
6464 return unify_invalid (explain_p);
6465 }
6466
6467 static int
6468 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6469 {
6470 if (explain_p)
6471 {
6472 if (least_p)
6473 inform_n (input_location, wanted,
6474 " candidate expects at least %d argument, %d provided",
6475 " candidate expects at least %d arguments, %d provided",
6476 wanted, have);
6477 else
6478 inform_n (input_location, wanted,
6479 " candidate expects %d argument, %d provided",
6480 " candidate expects %d arguments, %d provided",
6481 wanted, have);
6482 }
6483 return unify_invalid (explain_p);
6484 }
6485
6486 static int
6487 unify_too_many_arguments (bool explain_p, int have, int wanted)
6488 {
6489 return unify_arity (explain_p, have, wanted);
6490 }
6491
6492 static int
6493 unify_too_few_arguments (bool explain_p, int have, int wanted,
6494 bool least_p = false)
6495 {
6496 return unify_arity (explain_p, have, wanted, least_p);
6497 }
6498
6499 static int
6500 unify_arg_conversion (bool explain_p, tree to_type,
6501 tree from_type, tree arg)
6502 {
6503 if (explain_p)
6504 inform (cp_expr_loc_or_loc (arg, input_location),
6505 " cannot convert %qE (type %qT) to type %qT",
6506 arg, from_type, to_type);
6507 return unify_invalid (explain_p);
6508 }
6509
6510 static int
6511 unify_no_common_base (bool explain_p, enum template_base_result r,
6512 tree parm, tree arg)
6513 {
6514 if (explain_p)
6515 switch (r)
6516 {
6517 case tbr_ambiguous_baseclass:
6518 inform (input_location, " %qT is an ambiguous base class of %qT",
6519 parm, arg);
6520 break;
6521 default:
6522 inform (input_location, " %qT is not derived from %qT", arg, parm);
6523 break;
6524 }
6525 return unify_invalid (explain_p);
6526 }
6527
6528 static int
6529 unify_inconsistent_template_template_parameters (bool explain_p)
6530 {
6531 if (explain_p)
6532 inform (input_location,
6533 " template parameters of a template template argument are "
6534 "inconsistent with other deduced template arguments");
6535 return unify_invalid (explain_p);
6536 }
6537
6538 static int
6539 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6540 {
6541 if (explain_p)
6542 inform (input_location,
6543 " can't deduce a template for %qT from non-template type %qT",
6544 parm, arg);
6545 return unify_invalid (explain_p);
6546 }
6547
6548 static int
6549 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6550 {
6551 if (explain_p)
6552 inform (input_location,
6553 " template argument %qE does not match %qE", arg, parm);
6554 return unify_invalid (explain_p);
6555 }
6556
6557 /* True if T is a C++20 template parameter object to store the argument for a
6558 template parameter of class type. */
6559
6560 bool
6561 template_parm_object_p (const_tree t)
6562 {
6563 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6564 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6565 }
6566
6567 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6568 argument for TYPE, points to an unsuitable object. */
6569
6570 static bool
6571 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6572 {
6573 switch (TREE_CODE (expr))
6574 {
6575 CASE_CONVERT:
6576 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6577 complain);
6578
6579 case TARGET_EXPR:
6580 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6581 complain);
6582
6583 case CONSTRUCTOR:
6584 {
6585 unsigned i; tree elt;
6586 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6587 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6588 return true;
6589 }
6590 break;
6591
6592 case ADDR_EXPR:
6593 {
6594 tree decl = TREE_OPERAND (expr, 0);
6595
6596 if (!VAR_P (decl))
6597 {
6598 if (complain & tf_error)
6599 error ("%qE is not a valid template argument of type %qT "
6600 "because %qE is not a variable", expr, type, decl);
6601 return true;
6602 }
6603 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6604 {
6605 if (complain & tf_error)
6606 error ("%qE is not a valid template argument of type %qT "
6607 "in C++98 because %qD does not have external linkage",
6608 expr, type, decl);
6609 return true;
6610 }
6611 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6612 && decl_linkage (decl) == lk_none)
6613 {
6614 if (complain & tf_error)
6615 error ("%qE is not a valid template argument of type %qT "
6616 "because %qD has no linkage", expr, type, decl);
6617 return true;
6618 }
6619 /* C++17: For a non-type template-parameter of reference or pointer
6620 type, the value of the constant expression shall not refer to (or
6621 for a pointer type, shall not be the address of):
6622 * a subobject (4.5),
6623 * a temporary object (15.2),
6624 * a string literal (5.13.5),
6625 * the result of a typeid expression (8.2.8), or
6626 * a predefined __func__ variable (11.4.1). */
6627 else if (DECL_ARTIFICIAL (decl))
6628 {
6629 if (complain & tf_error)
6630 error ("the address of %qD is not a valid template argument",
6631 decl);
6632 return true;
6633 }
6634 else if (!same_type_ignoring_top_level_qualifiers_p
6635 (strip_array_types (TREE_TYPE (type)),
6636 strip_array_types (TREE_TYPE (decl))))
6637 {
6638 if (complain & tf_error)
6639 error ("the address of the %qT subobject of %qD is not a "
6640 "valid template argument", TREE_TYPE (type), decl);
6641 return true;
6642 }
6643 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6644 {
6645 if (complain & tf_error)
6646 error ("the address of %qD is not a valid template argument "
6647 "because it does not have static storage duration",
6648 decl);
6649 return true;
6650 }
6651 }
6652 break;
6653
6654 default:
6655 if (!INDIRECT_TYPE_P (type))
6656 /* We're only concerned about pointers and references here. */;
6657 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6658 /* Null pointer values are OK in C++11. */;
6659 else
6660 {
6661 if (VAR_P (expr))
6662 {
6663 if (complain & tf_error)
6664 error ("%qD is not a valid template argument "
6665 "because %qD is a variable, not the address of "
6666 "a variable", expr, expr);
6667 return true;
6668 }
6669 else
6670 {
6671 if (complain & tf_error)
6672 error ("%qE is not a valid template argument for %qT "
6673 "because it is not the address of a variable",
6674 expr, type);
6675 return true;
6676 }
6677 }
6678 }
6679 return false;
6680
6681 }
6682
6683 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6684 template argument EXPR. */
6685
6686 static tree
6687 get_template_parm_object (tree expr, tsubst_flags_t complain)
6688 {
6689 if (TREE_CODE (expr) == TARGET_EXPR)
6690 expr = TARGET_EXPR_INITIAL (expr);
6691
6692 if (!TREE_CONSTANT (expr))
6693 {
6694 if ((complain & tf_error)
6695 && require_rvalue_constant_expression (expr))
6696 cxx_constant_value (expr);
6697 return error_mark_node;
6698 }
6699 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6700 return error_mark_node;
6701
6702 tree name = mangle_template_parm_object (expr);
6703 tree decl = get_global_binding (name);
6704 if (decl)
6705 return decl;
6706
6707 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6708 decl = create_temporary_var (type);
6709 TREE_STATIC (decl) = true;
6710 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6711 TREE_READONLY (decl) = true;
6712 DECL_NAME (decl) = name;
6713 SET_DECL_ASSEMBLER_NAME (decl, name);
6714 DECL_CONTEXT (decl) = global_namespace;
6715 comdat_linkage (decl);
6716 pushdecl_top_level_and_finish (decl, expr);
6717 return decl;
6718 }
6719
6720 /* Attempt to convert the non-type template parameter EXPR to the
6721 indicated TYPE. If the conversion is successful, return the
6722 converted value. If the conversion is unsuccessful, return
6723 NULL_TREE if we issued an error message, or error_mark_node if we
6724 did not. We issue error messages for out-and-out bad template
6725 parameters, but not simply because the conversion failed, since we
6726 might be just trying to do argument deduction. Both TYPE and EXPR
6727 must be non-dependent.
6728
6729 The conversion follows the special rules described in
6730 [temp.arg.nontype], and it is much more strict than an implicit
6731 conversion.
6732
6733 This function is called twice for each template argument (see
6734 lookup_template_class for a more accurate description of this
6735 problem). This means that we need to handle expressions which
6736 are not valid in a C++ source, but can be created from the
6737 first call (for instance, casts to perform conversions). These
6738 hacks can go away after we fix the double coercion problem. */
6739
6740 static tree
6741 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6742 {
6743 tree expr_type;
6744 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6745
6746 /* Detect immediately string literals as invalid non-type argument.
6747 This special-case is not needed for correctness (we would easily
6748 catch this later), but only to provide better diagnostic for this
6749 common user mistake. As suggested by DR 100, we do not mention
6750 linkage issues in the diagnostic as this is not the point. */
6751 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6752 {
6753 if (complain & tf_error)
6754 error ("%qE is not a valid template argument for type %qT "
6755 "because string literals can never be used in this context",
6756 expr, type);
6757 return NULL_TREE;
6758 }
6759
6760 /* Add the ADDR_EXPR now for the benefit of
6761 value_dependent_expression_p. */
6762 if (TYPE_PTROBV_P (type)
6763 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6764 {
6765 expr = decay_conversion (expr, complain);
6766 if (expr == error_mark_node)
6767 return error_mark_node;
6768 }
6769
6770 /* If we are in a template, EXPR may be non-dependent, but still
6771 have a syntactic, rather than semantic, form. For example, EXPR
6772 might be a SCOPE_REF, rather than the VAR_DECL to which the
6773 SCOPE_REF refers. Preserving the qualifying scope is necessary
6774 so that access checking can be performed when the template is
6775 instantiated -- but here we need the resolved form so that we can
6776 convert the argument. */
6777 bool non_dep = false;
6778 if (TYPE_REF_OBJ_P (type)
6779 && has_value_dependent_address (expr))
6780 /* If we want the address and it's value-dependent, don't fold. */;
6781 else if (processing_template_decl
6782 && is_nondependent_constant_expression (expr))
6783 non_dep = true;
6784 if (error_operand_p (expr))
6785 return error_mark_node;
6786 expr_type = TREE_TYPE (expr);
6787
6788 /* If the argument is non-dependent, perform any conversions in
6789 non-dependent context as well. */
6790 processing_template_decl_sentinel s (non_dep);
6791 if (non_dep)
6792 expr = instantiate_non_dependent_expr_internal (expr, complain);
6793
6794 if (value_dependent_expression_p (expr))
6795 expr = canonicalize_expr_argument (expr, complain);
6796
6797 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6798 to a non-type argument of "nullptr". */
6799 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6800 expr = fold_simple (convert (type, expr));
6801
6802 /* In C++11, integral or enumeration non-type template arguments can be
6803 arbitrary constant expressions. Pointer and pointer to
6804 member arguments can be general constant expressions that evaluate
6805 to a null value, but otherwise still need to be of a specific form. */
6806 if (cxx_dialect >= cxx11)
6807 {
6808 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6809 /* A PTRMEM_CST is already constant, and a valid template
6810 argument for a parameter of pointer to member type, we just want
6811 to leave it in that form rather than lower it to a
6812 CONSTRUCTOR. */;
6813 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6814 || cxx_dialect >= cxx17)
6815 {
6816 /* C++17: A template-argument for a non-type template-parameter shall
6817 be a converted constant expression (8.20) of the type of the
6818 template-parameter. */
6819 expr = build_converted_constant_expr (type, expr, complain);
6820 if (expr == error_mark_node)
6821 /* Make sure we return NULL_TREE only if we have really issued
6822 an error, as described above. */
6823 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6824 expr = maybe_constant_value (expr);
6825 expr = convert_from_reference (expr);
6826 }
6827 else if (TYPE_PTR_OR_PTRMEM_P (type))
6828 {
6829 tree folded = maybe_constant_value (expr);
6830 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6831 : null_member_pointer_value_p (folded))
6832 expr = folded;
6833 }
6834 }
6835
6836 if (TYPE_REF_P (type))
6837 expr = mark_lvalue_use (expr);
6838 else
6839 expr = mark_rvalue_use (expr);
6840
6841 /* HACK: Due to double coercion, we can get a
6842 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6843 which is the tree that we built on the first call (see
6844 below when coercing to reference to object or to reference to
6845 function). We just strip everything and get to the arg.
6846 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6847 for examples. */
6848 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6849 {
6850 tree probe_type, probe = expr;
6851 if (REFERENCE_REF_P (probe))
6852 probe = TREE_OPERAND (probe, 0);
6853 probe_type = TREE_TYPE (probe);
6854 if (TREE_CODE (probe) == NOP_EXPR)
6855 {
6856 /* ??? Maybe we could use convert_from_reference here, but we
6857 would need to relax its constraints because the NOP_EXPR
6858 could actually change the type to something more cv-qualified,
6859 and this is not folded by convert_from_reference. */
6860 tree addr = TREE_OPERAND (probe, 0);
6861 if (TYPE_REF_P (probe_type)
6862 && TREE_CODE (addr) == ADDR_EXPR
6863 && TYPE_PTR_P (TREE_TYPE (addr))
6864 && (same_type_ignoring_top_level_qualifiers_p
6865 (TREE_TYPE (probe_type),
6866 TREE_TYPE (TREE_TYPE (addr)))))
6867 {
6868 expr = TREE_OPERAND (addr, 0);
6869 expr_type = TREE_TYPE (probe_type);
6870 }
6871 }
6872 }
6873
6874 /* [temp.arg.nontype]/5, bullet 1
6875
6876 For a non-type template-parameter of integral or enumeration type,
6877 integral promotions (_conv.prom_) and integral conversions
6878 (_conv.integral_) are applied. */
6879 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6880 {
6881 if (cxx_dialect < cxx11)
6882 {
6883 tree t = build_converted_constant_expr (type, expr, complain);
6884 t = maybe_constant_value (t);
6885 if (t != error_mark_node)
6886 expr = t;
6887 }
6888
6889 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6890 return error_mark_node;
6891
6892 /* Notice that there are constant expressions like '4 % 0' which
6893 do not fold into integer constants. */
6894 if (TREE_CODE (expr) != INTEGER_CST
6895 && !value_dependent_expression_p (expr))
6896 {
6897 if (complain & tf_error)
6898 {
6899 int errs = errorcount, warns = warningcount + werrorcount;
6900 if (!require_potential_constant_expression (expr))
6901 expr = error_mark_node;
6902 else
6903 expr = cxx_constant_value (expr);
6904 if (errorcount > errs || warningcount + werrorcount > warns)
6905 inform (loc, "in template argument for type %qT", type);
6906 if (expr == error_mark_node)
6907 return NULL_TREE;
6908 /* else cxx_constant_value complained but gave us
6909 a real constant, so go ahead. */
6910 if (TREE_CODE (expr) != INTEGER_CST)
6911 {
6912 /* Some assemble time constant expressions like
6913 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6914 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6915 as we can emit them into .rodata initializers of
6916 variables, yet they can't fold into an INTEGER_CST at
6917 compile time. Refuse them here. */
6918 gcc_checking_assert (reduced_constant_expression_p (expr));
6919 error_at (loc, "template argument %qE for type %qT not "
6920 "a constant integer", expr, type);
6921 return NULL_TREE;
6922 }
6923 }
6924 else
6925 return NULL_TREE;
6926 }
6927
6928 /* Avoid typedef problems. */
6929 if (TREE_TYPE (expr) != type)
6930 expr = fold_convert (type, expr);
6931 }
6932 /* [temp.arg.nontype]/5, bullet 2
6933
6934 For a non-type template-parameter of type pointer to object,
6935 qualification conversions (_conv.qual_) and the array-to-pointer
6936 conversion (_conv.array_) are applied. */
6937 else if (TYPE_PTROBV_P (type))
6938 {
6939 tree decayed = expr;
6940
6941 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6942 decay_conversion or an explicit cast. If it's a problematic cast,
6943 we'll complain about it below. */
6944 if (TREE_CODE (expr) == NOP_EXPR)
6945 {
6946 tree probe = expr;
6947 STRIP_NOPS (probe);
6948 if (TREE_CODE (probe) == ADDR_EXPR
6949 && TYPE_PTR_P (TREE_TYPE (probe)))
6950 {
6951 expr = probe;
6952 expr_type = TREE_TYPE (expr);
6953 }
6954 }
6955
6956 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6957
6958 A template-argument for a non-type, non-template template-parameter
6959 shall be one of: [...]
6960
6961 -- the name of a non-type template-parameter;
6962 -- the address of an object or function with external linkage, [...]
6963 expressed as "& id-expression" where the & is optional if the name
6964 refers to a function or array, or if the corresponding
6965 template-parameter is a reference.
6966
6967 Here, we do not care about functions, as they are invalid anyway
6968 for a parameter of type pointer-to-object. */
6969
6970 if (value_dependent_expression_p (expr))
6971 /* Non-type template parameters are OK. */
6972 ;
6973 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6974 /* Null pointer values are OK in C++11. */;
6975 else if (TREE_CODE (expr) != ADDR_EXPR
6976 && !INDIRECT_TYPE_P (expr_type))
6977 /* Other values, like integer constants, might be valid
6978 non-type arguments of some other type. */
6979 return error_mark_node;
6980 else if (invalid_tparm_referent_p (type, expr, complain))
6981 return NULL_TREE;
6982
6983 expr = decayed;
6984
6985 expr = perform_qualification_conversions (type, expr);
6986 if (expr == error_mark_node)
6987 return error_mark_node;
6988 }
6989 /* [temp.arg.nontype]/5, bullet 3
6990
6991 For a non-type template-parameter of type reference to object, no
6992 conversions apply. The type referred to by the reference may be more
6993 cv-qualified than the (otherwise identical) type of the
6994 template-argument. The template-parameter is bound directly to the
6995 template-argument, which must be an lvalue. */
6996 else if (TYPE_REF_OBJ_P (type))
6997 {
6998 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6999 expr_type))
7000 return error_mark_node;
7001
7002 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7003 {
7004 if (complain & tf_error)
7005 error ("%qE is not a valid template argument for type %qT "
7006 "because of conflicts in cv-qualification", expr, type);
7007 return NULL_TREE;
7008 }
7009
7010 if (!lvalue_p (expr))
7011 {
7012 if (complain & tf_error)
7013 error ("%qE is not a valid template argument for type %qT "
7014 "because it is not an lvalue", expr, type);
7015 return NULL_TREE;
7016 }
7017
7018 /* [temp.arg.nontype]/1
7019
7020 A template-argument for a non-type, non-template template-parameter
7021 shall be one of: [...]
7022
7023 -- the address of an object or function with external linkage. */
7024 if (INDIRECT_REF_P (expr)
7025 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7026 {
7027 expr = TREE_OPERAND (expr, 0);
7028 if (DECL_P (expr))
7029 {
7030 if (complain & tf_error)
7031 error ("%q#D is not a valid template argument for type %qT "
7032 "because a reference variable does not have a constant "
7033 "address", expr, type);
7034 return NULL_TREE;
7035 }
7036 }
7037
7038 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7039 && value_dependent_expression_p (expr))
7040 /* OK, dependent reference. We don't want to ask whether a DECL is
7041 itself value-dependent, since what we want here is its address. */;
7042 else
7043 {
7044 expr = build_address (expr);
7045
7046 if (invalid_tparm_referent_p (type, expr, complain))
7047 return NULL_TREE;
7048 }
7049
7050 if (!same_type_p (type, TREE_TYPE (expr)))
7051 expr = build_nop (type, expr);
7052 }
7053 /* [temp.arg.nontype]/5, bullet 4
7054
7055 For a non-type template-parameter of type pointer to function, only
7056 the function-to-pointer conversion (_conv.func_) is applied. If the
7057 template-argument represents a set of overloaded functions (or a
7058 pointer to such), the matching function is selected from the set
7059 (_over.over_). */
7060 else if (TYPE_PTRFN_P (type))
7061 {
7062 /* If the argument is a template-id, we might not have enough
7063 context information to decay the pointer. */
7064 if (!type_unknown_p (expr_type))
7065 {
7066 expr = decay_conversion (expr, complain);
7067 if (expr == error_mark_node)
7068 return error_mark_node;
7069 }
7070
7071 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7072 /* Null pointer values are OK in C++11. */
7073 return perform_qualification_conversions (type, expr);
7074
7075 expr = convert_nontype_argument_function (type, expr, complain);
7076 if (!expr || expr == error_mark_node)
7077 return expr;
7078 }
7079 /* [temp.arg.nontype]/5, bullet 5
7080
7081 For a non-type template-parameter of type reference to function, no
7082 conversions apply. If the template-argument represents a set of
7083 overloaded functions, the matching function is selected from the set
7084 (_over.over_). */
7085 else if (TYPE_REFFN_P (type))
7086 {
7087 if (TREE_CODE (expr) == ADDR_EXPR)
7088 {
7089 if (complain & tf_error)
7090 {
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is a pointer", expr, type);
7093 inform (input_location, "try using %qE instead",
7094 TREE_OPERAND (expr, 0));
7095 }
7096 return NULL_TREE;
7097 }
7098
7099 expr = convert_nontype_argument_function (type, expr, complain);
7100 if (!expr || expr == error_mark_node)
7101 return expr;
7102 }
7103 /* [temp.arg.nontype]/5, bullet 6
7104
7105 For a non-type template-parameter of type pointer to member function,
7106 no conversions apply. If the template-argument represents a set of
7107 overloaded member functions, the matching member function is selected
7108 from the set (_over.over_). */
7109 else if (TYPE_PTRMEMFUNC_P (type))
7110 {
7111 expr = instantiate_type (type, expr, tf_none);
7112 if (expr == error_mark_node)
7113 return error_mark_node;
7114
7115 /* [temp.arg.nontype] bullet 1 says the pointer to member
7116 expression must be a pointer-to-member constant. */
7117 if (!value_dependent_expression_p (expr)
7118 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7119 return NULL_TREE;
7120
7121 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7122 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7123 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7124 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7125 }
7126 /* [temp.arg.nontype]/5, bullet 7
7127
7128 For a non-type template-parameter of type pointer to data member,
7129 qualification conversions (_conv.qual_) are applied. */
7130 else if (TYPE_PTRDATAMEM_P (type))
7131 {
7132 /* [temp.arg.nontype] bullet 1 says the pointer to member
7133 expression must be a pointer-to-member constant. */
7134 if (!value_dependent_expression_p (expr)
7135 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7136 return NULL_TREE;
7137
7138 expr = perform_qualification_conversions (type, expr);
7139 if (expr == error_mark_node)
7140 return expr;
7141 }
7142 else if (NULLPTR_TYPE_P (type))
7143 {
7144 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7145 {
7146 if (complain & tf_error)
7147 error ("%qE is not a valid template argument for type %qT "
7148 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7149 return NULL_TREE;
7150 }
7151 return expr;
7152 }
7153 else if (CLASS_TYPE_P (type))
7154 {
7155 /* Replace the argument with a reference to the corresponding template
7156 parameter object. */
7157 if (!value_dependent_expression_p (expr))
7158 expr = get_template_parm_object (expr, complain);
7159 if (expr == error_mark_node)
7160 return NULL_TREE;
7161 }
7162 /* A template non-type parameter must be one of the above. */
7163 else
7164 gcc_unreachable ();
7165
7166 /* Sanity check: did we actually convert the argument to the
7167 right type? */
7168 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7169 (type, TREE_TYPE (expr)));
7170 return convert_from_reference (expr);
7171 }
7172
7173 /* Subroutine of coerce_template_template_parms, which returns 1 if
7174 PARM_PARM and ARG_PARM match using the rule for the template
7175 parameters of template template parameters. Both PARM and ARG are
7176 template parameters; the rest of the arguments are the same as for
7177 coerce_template_template_parms.
7178 */
7179 static int
7180 coerce_template_template_parm (tree parm,
7181 tree arg,
7182 tsubst_flags_t complain,
7183 tree in_decl,
7184 tree outer_args)
7185 {
7186 if (arg == NULL_TREE || error_operand_p (arg)
7187 || parm == NULL_TREE || error_operand_p (parm))
7188 return 0;
7189
7190 if (TREE_CODE (arg) != TREE_CODE (parm))
7191 return 0;
7192
7193 switch (TREE_CODE (parm))
7194 {
7195 case TEMPLATE_DECL:
7196 /* We encounter instantiations of templates like
7197 template <template <template <class> class> class TT>
7198 class C; */
7199 {
7200 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7201 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7202
7203 if (!coerce_template_template_parms
7204 (parmparm, argparm, complain, in_decl, outer_args))
7205 return 0;
7206 }
7207 /* Fall through. */
7208
7209 case TYPE_DECL:
7210 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7211 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7212 /* Argument is a parameter pack but parameter is not. */
7213 return 0;
7214 break;
7215
7216 case PARM_DECL:
7217 /* The tsubst call is used to handle cases such as
7218
7219 template <int> class C {};
7220 template <class T, template <T> class TT> class D {};
7221 D<int, C> d;
7222
7223 i.e. the parameter list of TT depends on earlier parameters. */
7224 if (!uses_template_parms (TREE_TYPE (arg)))
7225 {
7226 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7227 if (!uses_template_parms (t)
7228 && !same_type_p (t, TREE_TYPE (arg)))
7229 return 0;
7230 }
7231
7232 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7233 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7234 /* Argument is a parameter pack but parameter is not. */
7235 return 0;
7236
7237 break;
7238
7239 default:
7240 gcc_unreachable ();
7241 }
7242
7243 return 1;
7244 }
7245
7246 /* Coerce template argument list ARGLIST for use with template
7247 template-parameter TEMPL. */
7248
7249 static tree
7250 coerce_template_args_for_ttp (tree templ, tree arglist,
7251 tsubst_flags_t complain)
7252 {
7253 /* Consider an example where a template template parameter declared as
7254
7255 template <class T, class U = std::allocator<T> > class TT
7256
7257 The template parameter level of T and U are one level larger than
7258 of TT. To proper process the default argument of U, say when an
7259 instantiation `TT<int>' is seen, we need to build the full
7260 arguments containing {int} as the innermost level. Outer levels,
7261 available when not appearing as default template argument, can be
7262 obtained from the arguments of the enclosing template.
7263
7264 Suppose that TT is later substituted with std::vector. The above
7265 instantiation is `TT<int, std::allocator<T> >' with TT at
7266 level 1, and T at level 2, while the template arguments at level 1
7267 becomes {std::vector} and the inner level 2 is {int}. */
7268
7269 tree outer = DECL_CONTEXT (templ);
7270 if (outer)
7271 {
7272 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7273 /* We want arguments for the partial specialization, not arguments for
7274 the primary template. */
7275 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7276 else
7277 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7278 }
7279 else if (current_template_parms)
7280 {
7281 /* This is an argument of the current template, so we haven't set
7282 DECL_CONTEXT yet. */
7283 tree relevant_template_parms;
7284
7285 /* Parameter levels that are greater than the level of the given
7286 template template parm are irrelevant. */
7287 relevant_template_parms = current_template_parms;
7288 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7289 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7290 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7291
7292 outer = template_parms_to_args (relevant_template_parms);
7293 }
7294
7295 if (outer)
7296 arglist = add_to_template_args (outer, arglist);
7297
7298 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7299 return coerce_template_parms (parmlist, arglist, templ,
7300 complain,
7301 /*require_all_args=*/true,
7302 /*use_default_args=*/true);
7303 }
7304
7305 /* A cache of template template parameters with match-all default
7306 arguments. */
7307 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7308 static void
7309 store_defaulted_ttp (tree v, tree t)
7310 {
7311 if (!defaulted_ttp_cache)
7312 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7313 defaulted_ttp_cache->put (v, t);
7314 }
7315 static tree
7316 lookup_defaulted_ttp (tree v)
7317 {
7318 if (defaulted_ttp_cache)
7319 if (tree *p = defaulted_ttp_cache->get (v))
7320 return *p;
7321 return NULL_TREE;
7322 }
7323
7324 /* T is a bound template template-parameter. Copy its arguments into default
7325 arguments of the template template-parameter's template parameters. */
7326
7327 static tree
7328 add_defaults_to_ttp (tree otmpl)
7329 {
7330 if (tree c = lookup_defaulted_ttp (otmpl))
7331 return c;
7332
7333 tree ntmpl = copy_node (otmpl);
7334
7335 tree ntype = copy_node (TREE_TYPE (otmpl));
7336 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7337 TYPE_MAIN_VARIANT (ntype) = ntype;
7338 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7339 TYPE_NAME (ntype) = ntmpl;
7340 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7341
7342 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7343 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7344 TEMPLATE_PARM_DECL (idx) = ntmpl;
7345 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7346
7347 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7348 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7349 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7350 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7351 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7352 {
7353 tree o = TREE_VEC_ELT (vec, i);
7354 if (!template_parameter_pack_p (TREE_VALUE (o)))
7355 {
7356 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7357 TREE_PURPOSE (n) = any_targ_node;
7358 }
7359 }
7360
7361 store_defaulted_ttp (otmpl, ntmpl);
7362 return ntmpl;
7363 }
7364
7365 /* ARG is a bound potential template template-argument, and PARGS is a list
7366 of arguments for the corresponding template template-parameter. Adjust
7367 PARGS as appropriate for application to ARG's template, and if ARG is a
7368 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7369 arguments to the template template parameter. */
7370
7371 static tree
7372 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7373 {
7374 ++processing_template_decl;
7375 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7376 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7377 {
7378 /* When comparing two template template-parameters in partial ordering,
7379 rewrite the one currently being used as an argument to have default
7380 arguments for all parameters. */
7381 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7382 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7383 if (pargs != error_mark_node)
7384 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7385 TYPE_TI_ARGS (arg));
7386 }
7387 else
7388 {
7389 tree aparms
7390 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7391 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7392 /*require_all*/true,
7393 /*use_default*/true);
7394 }
7395 --processing_template_decl;
7396 return pargs;
7397 }
7398
7399 /* Subroutine of unify for the case when PARM is a
7400 BOUND_TEMPLATE_TEMPLATE_PARM. */
7401
7402 static int
7403 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7404 bool explain_p)
7405 {
7406 tree parmvec = TYPE_TI_ARGS (parm);
7407 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7408
7409 /* The template template parm might be variadic and the argument
7410 not, so flatten both argument lists. */
7411 parmvec = expand_template_argument_pack (parmvec);
7412 argvec = expand_template_argument_pack (argvec);
7413
7414 if (flag_new_ttp)
7415 {
7416 /* In keeping with P0522R0, adjust P's template arguments
7417 to apply to A's template; then flatten it again. */
7418 tree nparmvec = parmvec;
7419 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7420 nparmvec = expand_template_argument_pack (nparmvec);
7421
7422 if (unify (tparms, targs, nparmvec, argvec,
7423 UNIFY_ALLOW_NONE, explain_p))
7424 return 1;
7425
7426 /* If the P0522 adjustment eliminated a pack expansion, deduce
7427 empty packs. */
7428 if (flag_new_ttp
7429 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7430 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7431 DEDUCE_EXACT, /*sub*/true, explain_p))
7432 return 1;
7433 }
7434 else
7435 {
7436 /* Deduce arguments T, i from TT<T> or TT<i>.
7437 We check each element of PARMVEC and ARGVEC individually
7438 rather than the whole TREE_VEC since they can have
7439 different number of elements, which is allowed under N2555. */
7440
7441 int len = TREE_VEC_LENGTH (parmvec);
7442
7443 /* Check if the parameters end in a pack, making them
7444 variadic. */
7445 int parm_variadic_p = 0;
7446 if (len > 0
7447 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7448 parm_variadic_p = 1;
7449
7450 for (int i = 0; i < len - parm_variadic_p; ++i)
7451 /* If the template argument list of P contains a pack
7452 expansion that is not the last template argument, the
7453 entire template argument list is a non-deduced
7454 context. */
7455 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7456 return unify_success (explain_p);
7457
7458 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7459 return unify_too_few_arguments (explain_p,
7460 TREE_VEC_LENGTH (argvec), len);
7461
7462 for (int i = 0; i < len - parm_variadic_p; ++i)
7463 if (unify (tparms, targs,
7464 TREE_VEC_ELT (parmvec, i),
7465 TREE_VEC_ELT (argvec, i),
7466 UNIFY_ALLOW_NONE, explain_p))
7467 return 1;
7468
7469 if (parm_variadic_p
7470 && unify_pack_expansion (tparms, targs,
7471 parmvec, argvec,
7472 DEDUCE_EXACT,
7473 /*subr=*/true, explain_p))
7474 return 1;
7475 }
7476
7477 return 0;
7478 }
7479
7480 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7481 template template parameters. Both PARM_PARMS and ARG_PARMS are
7482 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7483 or PARM_DECL.
7484
7485 Consider the example:
7486 template <class T> class A;
7487 template<template <class U> class TT> class B;
7488
7489 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7490 the parameters to A, and OUTER_ARGS contains A. */
7491
7492 static int
7493 coerce_template_template_parms (tree parm_parms,
7494 tree arg_parms,
7495 tsubst_flags_t complain,
7496 tree in_decl,
7497 tree outer_args)
7498 {
7499 int nparms, nargs, i;
7500 tree parm, arg;
7501 int variadic_p = 0;
7502
7503 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7504 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7505
7506 nparms = TREE_VEC_LENGTH (parm_parms);
7507 nargs = TREE_VEC_LENGTH (arg_parms);
7508
7509 if (flag_new_ttp)
7510 {
7511 /* P0522R0: A template template-parameter P is at least as specialized as
7512 a template template-argument A if, given the following rewrite to two
7513 function templates, the function template corresponding to P is at
7514 least as specialized as the function template corresponding to A
7515 according to the partial ordering rules for function templates
7516 ([temp.func.order]). Given an invented class template X with the
7517 template parameter list of A (including default arguments):
7518
7519 * Each of the two function templates has the same template parameters,
7520 respectively, as P or A.
7521
7522 * Each function template has a single function parameter whose type is
7523 a specialization of X with template arguments corresponding to the
7524 template parameters from the respective function template where, for
7525 each template parameter PP in the template parameter list of the
7526 function template, a corresponding template argument AA is formed. If
7527 PP declares a parameter pack, then AA is the pack expansion
7528 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7529
7530 If the rewrite produces an invalid type, then P is not at least as
7531 specialized as A. */
7532
7533 /* So coerce P's args to apply to A's parms, and then deduce between A's
7534 args and the converted args. If that succeeds, A is at least as
7535 specialized as P, so they match.*/
7536 tree pargs = template_parms_level_to_args (parm_parms);
7537 ++processing_template_decl;
7538 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7539 /*require_all*/true, /*use_default*/true);
7540 --processing_template_decl;
7541 if (pargs != error_mark_node)
7542 {
7543 tree targs = make_tree_vec (nargs);
7544 tree aargs = template_parms_level_to_args (arg_parms);
7545 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7546 /*explain*/false))
7547 return 1;
7548 }
7549 }
7550
7551 /* Determine whether we have a parameter pack at the end of the
7552 template template parameter's template parameter list. */
7553 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7554 {
7555 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7556
7557 if (error_operand_p (parm))
7558 return 0;
7559
7560 switch (TREE_CODE (parm))
7561 {
7562 case TEMPLATE_DECL:
7563 case TYPE_DECL:
7564 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7565 variadic_p = 1;
7566 break;
7567
7568 case PARM_DECL:
7569 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7570 variadic_p = 1;
7571 break;
7572
7573 default:
7574 gcc_unreachable ();
7575 }
7576 }
7577
7578 if (nargs != nparms
7579 && !(variadic_p && nargs >= nparms - 1))
7580 return 0;
7581
7582 /* Check all of the template parameters except the parameter pack at
7583 the end (if any). */
7584 for (i = 0; i < nparms - variadic_p; ++i)
7585 {
7586 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7587 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7588 continue;
7589
7590 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7591 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7592
7593 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7594 outer_args))
7595 return 0;
7596
7597 }
7598
7599 if (variadic_p)
7600 {
7601 /* Check each of the template parameters in the template
7602 argument against the template parameter pack at the end of
7603 the template template parameter. */
7604 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7605 return 0;
7606
7607 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7608
7609 for (; i < nargs; ++i)
7610 {
7611 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7612 continue;
7613
7614 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7615
7616 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7617 outer_args))
7618 return 0;
7619 }
7620 }
7621
7622 return 1;
7623 }
7624
7625 /* Verifies that the deduced template arguments (in TARGS) for the
7626 template template parameters (in TPARMS) represent valid bindings,
7627 by comparing the template parameter list of each template argument
7628 to the template parameter list of its corresponding template
7629 template parameter, in accordance with DR150. This
7630 routine can only be called after all template arguments have been
7631 deduced. It will return TRUE if all of the template template
7632 parameter bindings are okay, FALSE otherwise. */
7633 bool
7634 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7635 {
7636 int i, ntparms = TREE_VEC_LENGTH (tparms);
7637 bool ret = true;
7638
7639 /* We're dealing with template parms in this process. */
7640 ++processing_template_decl;
7641
7642 targs = INNERMOST_TEMPLATE_ARGS (targs);
7643
7644 for (i = 0; i < ntparms; ++i)
7645 {
7646 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7647 tree targ = TREE_VEC_ELT (targs, i);
7648
7649 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7650 {
7651 tree packed_args = NULL_TREE;
7652 int idx, len = 1;
7653
7654 if (ARGUMENT_PACK_P (targ))
7655 {
7656 /* Look inside the argument pack. */
7657 packed_args = ARGUMENT_PACK_ARGS (targ);
7658 len = TREE_VEC_LENGTH (packed_args);
7659 }
7660
7661 for (idx = 0; idx < len; ++idx)
7662 {
7663 tree targ_parms = NULL_TREE;
7664
7665 if (packed_args)
7666 /* Extract the next argument from the argument
7667 pack. */
7668 targ = TREE_VEC_ELT (packed_args, idx);
7669
7670 if (PACK_EXPANSION_P (targ))
7671 /* Look at the pattern of the pack expansion. */
7672 targ = PACK_EXPANSION_PATTERN (targ);
7673
7674 /* Extract the template parameters from the template
7675 argument. */
7676 if (TREE_CODE (targ) == TEMPLATE_DECL)
7677 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7678 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7679 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7680
7681 /* Verify that we can coerce the template template
7682 parameters from the template argument to the template
7683 parameter. This requires an exact match. */
7684 if (targ_parms
7685 && !coerce_template_template_parms
7686 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7687 targ_parms,
7688 tf_none,
7689 tparm,
7690 targs))
7691 {
7692 ret = false;
7693 goto out;
7694 }
7695 }
7696 }
7697 }
7698
7699 out:
7700
7701 --processing_template_decl;
7702 return ret;
7703 }
7704
7705 /* Since type attributes aren't mangled, we need to strip them from
7706 template type arguments. */
7707
7708 static tree
7709 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7710 {
7711 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7712 return arg;
7713 bool removed_attributes = false;
7714 tree canon = strip_typedefs (arg, &removed_attributes);
7715 if (removed_attributes
7716 && (complain & tf_warning))
7717 warning (OPT_Wignored_attributes,
7718 "ignoring attributes on template argument %qT", arg);
7719 return canon;
7720 }
7721
7722 /* And from inside dependent non-type arguments like sizeof(Type). */
7723
7724 static tree
7725 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7726 {
7727 if (!arg || arg == error_mark_node)
7728 return arg;
7729 bool removed_attributes = false;
7730 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7731 if (removed_attributes
7732 && (complain & tf_warning))
7733 warning (OPT_Wignored_attributes,
7734 "ignoring attributes in template argument %qE", arg);
7735 return canon;
7736 }
7737
7738 // A template declaration can be substituted for a constrained
7739 // template template parameter only when the argument is more
7740 // constrained than the parameter.
7741 static bool
7742 is_compatible_template_arg (tree parm, tree arg)
7743 {
7744 tree parm_cons = get_constraints (parm);
7745
7746 /* For now, allow constrained template template arguments
7747 and unconstrained template template parameters. */
7748 if (parm_cons == NULL_TREE)
7749 return true;
7750
7751 tree arg_cons = get_constraints (arg);
7752
7753 // If the template parameter is constrained, we need to rewrite its
7754 // constraints in terms of the ARG's template parameters. This ensures
7755 // that all of the template parameter types will have the same depth.
7756 //
7757 // Note that this is only valid when coerce_template_template_parm is
7758 // true for the innermost template parameters of PARM and ARG. In other
7759 // words, because coercion is successful, this conversion will be valid.
7760 if (parm_cons)
7761 {
7762 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7763 parm_cons = tsubst_constraint_info (parm_cons,
7764 INNERMOST_TEMPLATE_ARGS (args),
7765 tf_none, NULL_TREE);
7766 if (parm_cons == error_mark_node)
7767 return false;
7768 }
7769
7770 return subsumes (parm_cons, arg_cons);
7771 }
7772
7773 // Convert a placeholder argument into a binding to the original
7774 // parameter. The original parameter is saved as the TREE_TYPE of
7775 // ARG.
7776 static inline tree
7777 convert_wildcard_argument (tree parm, tree arg)
7778 {
7779 TREE_TYPE (arg) = parm;
7780 return arg;
7781 }
7782
7783 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7784 because one of them is dependent. But we need to represent the
7785 conversion for the benefit of cp_tree_equal. */
7786
7787 static tree
7788 maybe_convert_nontype_argument (tree type, tree arg)
7789 {
7790 /* Auto parms get no conversion. */
7791 if (type_uses_auto (type))
7792 return arg;
7793 /* We don't need or want to add this conversion now if we're going to use the
7794 argument for deduction. */
7795 if (value_dependent_expression_p (arg))
7796 return arg;
7797
7798 type = cv_unqualified (type);
7799 tree argtype = TREE_TYPE (arg);
7800 if (same_type_p (type, argtype))
7801 return arg;
7802
7803 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7804 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7805 return arg;
7806 }
7807
7808 /* Convert the indicated template ARG as necessary to match the
7809 indicated template PARM. Returns the converted ARG, or
7810 error_mark_node if the conversion was unsuccessful. Error and
7811 warning messages are issued under control of COMPLAIN. This
7812 conversion is for the Ith parameter in the parameter list. ARGS is
7813 the full set of template arguments deduced so far. */
7814
7815 static tree
7816 convert_template_argument (tree parm,
7817 tree arg,
7818 tree args,
7819 tsubst_flags_t complain,
7820 int i,
7821 tree in_decl)
7822 {
7823 tree orig_arg;
7824 tree val;
7825 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7826
7827 if (parm == error_mark_node || error_operand_p (arg))
7828 return error_mark_node;
7829
7830 /* Trivially convert placeholders. */
7831 if (TREE_CODE (arg) == WILDCARD_DECL)
7832 return convert_wildcard_argument (parm, arg);
7833
7834 if (arg == any_targ_node)
7835 return arg;
7836
7837 if (TREE_CODE (arg) == TREE_LIST
7838 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7839 {
7840 /* The template argument was the name of some
7841 member function. That's usually
7842 invalid, but static members are OK. In any
7843 case, grab the underlying fields/functions
7844 and issue an error later if required. */
7845 orig_arg = TREE_VALUE (arg);
7846 TREE_TYPE (arg) = unknown_type_node;
7847 }
7848
7849 orig_arg = arg;
7850
7851 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7852 requires_type = (TREE_CODE (parm) == TYPE_DECL
7853 || requires_tmpl_type);
7854
7855 /* When determining whether an argument pack expansion is a template,
7856 look at the pattern. */
7857 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7858 arg = PACK_EXPANSION_PATTERN (arg);
7859
7860 /* Deal with an injected-class-name used as a template template arg. */
7861 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7862 {
7863 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7864 if (TREE_CODE (t) == TEMPLATE_DECL)
7865 {
7866 if (cxx_dialect >= cxx11)
7867 /* OK under DR 1004. */;
7868 else if (complain & tf_warning_or_error)
7869 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7870 " used as template template argument", TYPE_NAME (arg));
7871 else if (flag_pedantic_errors)
7872 t = arg;
7873
7874 arg = t;
7875 }
7876 }
7877
7878 is_tmpl_type =
7879 ((TREE_CODE (arg) == TEMPLATE_DECL
7880 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7881 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7882 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7883 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7884
7885 if (is_tmpl_type
7886 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7887 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7888 arg = TYPE_STUB_DECL (arg);
7889
7890 is_type = TYPE_P (arg) || is_tmpl_type;
7891
7892 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7893 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7894 {
7895 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7896 {
7897 if (complain & tf_error)
7898 error ("invalid use of destructor %qE as a type", orig_arg);
7899 return error_mark_node;
7900 }
7901
7902 permerror (input_location,
7903 "to refer to a type member of a template parameter, "
7904 "use %<typename %E%>", orig_arg);
7905
7906 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7907 TREE_OPERAND (arg, 1),
7908 typename_type,
7909 complain);
7910 arg = orig_arg;
7911 is_type = 1;
7912 }
7913 if (is_type != requires_type)
7914 {
7915 if (in_decl)
7916 {
7917 if (complain & tf_error)
7918 {
7919 error ("type/value mismatch at argument %d in template "
7920 "parameter list for %qD",
7921 i + 1, in_decl);
7922 if (is_type)
7923 inform (input_location,
7924 " expected a constant of type %qT, got %qT",
7925 TREE_TYPE (parm),
7926 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7927 else if (requires_tmpl_type)
7928 inform (input_location,
7929 " expected a class template, got %qE", orig_arg);
7930 else
7931 inform (input_location,
7932 " expected a type, got %qE", orig_arg);
7933 }
7934 }
7935 return error_mark_node;
7936 }
7937 if (is_tmpl_type ^ requires_tmpl_type)
7938 {
7939 if (in_decl && (complain & tf_error))
7940 {
7941 error ("type/value mismatch at argument %d in template "
7942 "parameter list for %qD",
7943 i + 1, in_decl);
7944 if (is_tmpl_type)
7945 inform (input_location,
7946 " expected a type, got %qT", DECL_NAME (arg));
7947 else
7948 inform (input_location,
7949 " expected a class template, got %qT", orig_arg);
7950 }
7951 return error_mark_node;
7952 }
7953
7954 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7955 /* We already did the appropriate conversion when packing args. */
7956 val = orig_arg;
7957 else if (is_type)
7958 {
7959 if (requires_tmpl_type)
7960 {
7961 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7962 /* The number of argument required is not known yet.
7963 Just accept it for now. */
7964 val = orig_arg;
7965 else
7966 {
7967 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7968 tree argparm;
7969
7970 /* Strip alias templates that are equivalent to another
7971 template. */
7972 arg = get_underlying_template (arg);
7973 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7974
7975 if (coerce_template_template_parms (parmparm, argparm,
7976 complain, in_decl,
7977 args))
7978 {
7979 val = arg;
7980
7981 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7982 TEMPLATE_DECL. */
7983 if (val != error_mark_node)
7984 {
7985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7986 val = TREE_TYPE (val);
7987 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7988 val = make_pack_expansion (val, complain);
7989 }
7990 }
7991 else
7992 {
7993 if (in_decl && (complain & tf_error))
7994 {
7995 error ("type/value mismatch at argument %d in "
7996 "template parameter list for %qD",
7997 i + 1, in_decl);
7998 inform (input_location,
7999 " expected a template of type %qD, got %qT",
8000 parm, orig_arg);
8001 }
8002
8003 val = error_mark_node;
8004 }
8005
8006 // Check that the constraints are compatible before allowing the
8007 // substitution.
8008 if (val != error_mark_node)
8009 if (!is_compatible_template_arg (parm, arg))
8010 {
8011 if (in_decl && (complain & tf_error))
8012 {
8013 error ("constraint mismatch at argument %d in "
8014 "template parameter list for %qD",
8015 i + 1, in_decl);
8016 inform (input_location, " expected %qD but got %qD",
8017 parm, arg);
8018 }
8019 val = error_mark_node;
8020 }
8021 }
8022 }
8023 else
8024 val = orig_arg;
8025 /* We only form one instance of each template specialization.
8026 Therefore, if we use a non-canonical variant (i.e., a
8027 typedef), any future messages referring to the type will use
8028 the typedef, which is confusing if those future uses do not
8029 themselves also use the typedef. */
8030 if (TYPE_P (val))
8031 val = canonicalize_type_argument (val, complain);
8032 }
8033 else
8034 {
8035 tree t = TREE_TYPE (parm);
8036
8037 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8038 > TMPL_ARGS_DEPTH (args))
8039 /* We don't have enough levels of args to do any substitution. This
8040 can happen in the context of -fnew-ttp-matching. */;
8041 else if (tree a = type_uses_auto (t))
8042 {
8043 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8044 if (t == error_mark_node)
8045 return error_mark_node;
8046 }
8047 else
8048 t = tsubst (t, args, complain, in_decl);
8049
8050 if (invalid_nontype_parm_type_p (t, complain))
8051 return error_mark_node;
8052
8053 if (t != TREE_TYPE (parm))
8054 t = canonicalize_type_argument (t, complain);
8055
8056 if (!type_dependent_expression_p (orig_arg)
8057 && !uses_template_parms (t))
8058 /* We used to call digest_init here. However, digest_init
8059 will report errors, which we don't want when complain
8060 is zero. More importantly, digest_init will try too
8061 hard to convert things: for example, `0' should not be
8062 converted to pointer type at this point according to
8063 the standard. Accepting this is not merely an
8064 extension, since deciding whether or not these
8065 conversions can occur is part of determining which
8066 function template to call, or whether a given explicit
8067 argument specification is valid. */
8068 val = convert_nontype_argument (t, orig_arg, complain);
8069 else
8070 {
8071 val = canonicalize_expr_argument (orig_arg, complain);
8072 val = maybe_convert_nontype_argument (t, val);
8073 }
8074
8075
8076 if (val == NULL_TREE)
8077 val = error_mark_node;
8078 else if (val == error_mark_node && (complain & tf_error))
8079 error ("could not convert template argument %qE from %qT to %qT",
8080 orig_arg, TREE_TYPE (orig_arg), t);
8081
8082 if (INDIRECT_REF_P (val))
8083 {
8084 /* Reject template arguments that are references to built-in
8085 functions with no library fallbacks. */
8086 const_tree inner = TREE_OPERAND (val, 0);
8087 const_tree innertype = TREE_TYPE (inner);
8088 if (innertype
8089 && TYPE_REF_P (innertype)
8090 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8091 && TREE_OPERAND_LENGTH (inner) > 0
8092 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8093 return error_mark_node;
8094 }
8095
8096 if (TREE_CODE (val) == SCOPE_REF)
8097 {
8098 /* Strip typedefs from the SCOPE_REF. */
8099 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8100 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8101 complain);
8102 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8103 QUALIFIED_NAME_IS_TEMPLATE (val));
8104 }
8105 }
8106
8107 return val;
8108 }
8109
8110 /* Coerces the remaining template arguments in INNER_ARGS (from
8111 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8112 Returns the coerced argument pack. PARM_IDX is the position of this
8113 parameter in the template parameter list. ARGS is the original
8114 template argument list. */
8115 static tree
8116 coerce_template_parameter_pack (tree parms,
8117 int parm_idx,
8118 tree args,
8119 tree inner_args,
8120 int arg_idx,
8121 tree new_args,
8122 int* lost,
8123 tree in_decl,
8124 tsubst_flags_t complain)
8125 {
8126 tree parm = TREE_VEC_ELT (parms, parm_idx);
8127 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8128 tree packed_args;
8129 tree argument_pack;
8130 tree packed_parms = NULL_TREE;
8131
8132 if (arg_idx > nargs)
8133 arg_idx = nargs;
8134
8135 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8136 {
8137 /* When the template parameter is a non-type template parameter pack
8138 or template template parameter pack whose type or template
8139 parameters use parameter packs, we know exactly how many arguments
8140 we are looking for. Build a vector of the instantiated decls for
8141 these template parameters in PACKED_PARMS. */
8142 /* We can't use make_pack_expansion here because it would interpret a
8143 _DECL as a use rather than a declaration. */
8144 tree decl = TREE_VALUE (parm);
8145 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8146 SET_PACK_EXPANSION_PATTERN (exp, decl);
8147 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8148 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8149
8150 TREE_VEC_LENGTH (args)--;
8151 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8152 TREE_VEC_LENGTH (args)++;
8153
8154 if (packed_parms == error_mark_node)
8155 return error_mark_node;
8156
8157 /* If we're doing a partial instantiation of a member template,
8158 verify that all of the types used for the non-type
8159 template parameter pack are, in fact, valid for non-type
8160 template parameters. */
8161 if (arg_idx < nargs
8162 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8163 {
8164 int j, len = TREE_VEC_LENGTH (packed_parms);
8165 for (j = 0; j < len; ++j)
8166 {
8167 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8168 if (invalid_nontype_parm_type_p (t, complain))
8169 return error_mark_node;
8170 }
8171 /* We don't know how many args we have yet, just
8172 use the unconverted ones for now. */
8173 return NULL_TREE;
8174 }
8175
8176 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8177 }
8178 /* Check if we have a placeholder pack, which indicates we're
8179 in the context of a introduction list. In that case we want
8180 to match this pack to the single placeholder. */
8181 else if (arg_idx < nargs
8182 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8183 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8184 {
8185 nargs = arg_idx + 1;
8186 packed_args = make_tree_vec (1);
8187 }
8188 else
8189 packed_args = make_tree_vec (nargs - arg_idx);
8190
8191 /* Convert the remaining arguments, which will be a part of the
8192 parameter pack "parm". */
8193 int first_pack_arg = arg_idx;
8194 for (; arg_idx < nargs; ++arg_idx)
8195 {
8196 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8197 tree actual_parm = TREE_VALUE (parm);
8198 int pack_idx = arg_idx - first_pack_arg;
8199
8200 if (packed_parms)
8201 {
8202 /* Once we've packed as many args as we have types, stop. */
8203 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8204 break;
8205 else if (PACK_EXPANSION_P (arg))
8206 /* We don't know how many args we have yet, just
8207 use the unconverted ones for now. */
8208 return NULL_TREE;
8209 else
8210 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8211 }
8212
8213 if (arg == error_mark_node)
8214 {
8215 if (complain & tf_error)
8216 error ("template argument %d is invalid", arg_idx + 1);
8217 }
8218 else
8219 arg = convert_template_argument (actual_parm,
8220 arg, new_args, complain, parm_idx,
8221 in_decl);
8222 if (arg == error_mark_node)
8223 (*lost)++;
8224 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8225 }
8226
8227 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8228 && TREE_VEC_LENGTH (packed_args) > 0)
8229 {
8230 if (complain & tf_error)
8231 error ("wrong number of template arguments (%d, should be %d)",
8232 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8233 return error_mark_node;
8234 }
8235
8236 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8237 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8238 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8239 else
8240 {
8241 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8242 TREE_CONSTANT (argument_pack) = 1;
8243 }
8244
8245 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8246 if (CHECKING_P)
8247 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8248 TREE_VEC_LENGTH (packed_args));
8249 return argument_pack;
8250 }
8251
8252 /* Returns the number of pack expansions in the template argument vector
8253 ARGS. */
8254
8255 static int
8256 pack_expansion_args_count (tree args)
8257 {
8258 int i;
8259 int count = 0;
8260 if (args)
8261 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8262 {
8263 tree elt = TREE_VEC_ELT (args, i);
8264 if (elt && PACK_EXPANSION_P (elt))
8265 ++count;
8266 }
8267 return count;
8268 }
8269
8270 /* Convert all template arguments to their appropriate types, and
8271 return a vector containing the innermost resulting template
8272 arguments. If any error occurs, return error_mark_node. Error and
8273 warning messages are issued under control of COMPLAIN.
8274
8275 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8276 for arguments not specified in ARGS. Otherwise, if
8277 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8278 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8279 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8280 ARGS. */
8281
8282 static tree
8283 coerce_template_parms (tree parms,
8284 tree args,
8285 tree in_decl,
8286 tsubst_flags_t complain,
8287 bool require_all_args,
8288 bool use_default_args)
8289 {
8290 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8291 tree orig_inner_args;
8292 tree inner_args;
8293 tree new_args;
8294 tree new_inner_args;
8295
8296 /* When used as a boolean value, indicates whether this is a
8297 variadic template parameter list. Since it's an int, we can also
8298 subtract it from nparms to get the number of non-variadic
8299 parameters. */
8300 int variadic_p = 0;
8301 int variadic_args_p = 0;
8302 int post_variadic_parms = 0;
8303
8304 /* Adjustment to nparms for fixed parameter packs. */
8305 int fixed_pack_adjust = 0;
8306 int fixed_packs = 0;
8307 int missing = 0;
8308
8309 /* Likewise for parameters with default arguments. */
8310 int default_p = 0;
8311
8312 if (args == error_mark_node)
8313 return error_mark_node;
8314
8315 nparms = TREE_VEC_LENGTH (parms);
8316
8317 /* Determine if there are any parameter packs or default arguments. */
8318 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8319 {
8320 tree parm = TREE_VEC_ELT (parms, parm_idx);
8321 if (variadic_p)
8322 ++post_variadic_parms;
8323 if (template_parameter_pack_p (TREE_VALUE (parm)))
8324 ++variadic_p;
8325 if (TREE_PURPOSE (parm))
8326 ++default_p;
8327 }
8328
8329 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8330 /* If there are no parameters that follow a parameter pack, we need to
8331 expand any argument packs so that we can deduce a parameter pack from
8332 some non-packed args followed by an argument pack, as in variadic85.C.
8333 If there are such parameters, we need to leave argument packs intact
8334 so the arguments are assigned properly. This can happen when dealing
8335 with a nested class inside a partial specialization of a class
8336 template, as in variadic92.C, or when deducing a template parameter pack
8337 from a sub-declarator, as in variadic114.C. */
8338 if (!post_variadic_parms)
8339 inner_args = expand_template_argument_pack (inner_args);
8340
8341 /* Count any pack expansion args. */
8342 variadic_args_p = pack_expansion_args_count (inner_args);
8343
8344 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8345 if ((nargs - variadic_args_p > nparms && !variadic_p)
8346 || (nargs < nparms - variadic_p
8347 && require_all_args
8348 && !variadic_args_p
8349 && (!use_default_args
8350 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8351 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8352 {
8353 bad_nargs:
8354 if (complain & tf_error)
8355 {
8356 if (variadic_p || default_p)
8357 {
8358 nparms -= variadic_p + default_p;
8359 error ("wrong number of template arguments "
8360 "(%d, should be at least %d)", nargs, nparms);
8361 }
8362 else
8363 error ("wrong number of template arguments "
8364 "(%d, should be %d)", nargs, nparms);
8365
8366 if (in_decl)
8367 inform (DECL_SOURCE_LOCATION (in_decl),
8368 "provided for %qD", in_decl);
8369 }
8370
8371 return error_mark_node;
8372 }
8373 /* We can't pass a pack expansion to a non-pack parameter of an alias
8374 template (DR 1430). */
8375 else if (in_decl
8376 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8377 || concept_template_p (in_decl))
8378 && variadic_args_p
8379 && nargs - variadic_args_p < nparms - variadic_p)
8380 {
8381 if (complain & tf_error)
8382 {
8383 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8384 {
8385 tree arg = TREE_VEC_ELT (inner_args, i);
8386 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8387
8388 if (PACK_EXPANSION_P (arg)
8389 && !template_parameter_pack_p (parm))
8390 {
8391 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8392 error_at (location_of (arg),
8393 "pack expansion argument for non-pack parameter "
8394 "%qD of alias template %qD", parm, in_decl);
8395 else
8396 error_at (location_of (arg),
8397 "pack expansion argument for non-pack parameter "
8398 "%qD of concept %qD", parm, in_decl);
8399 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8400 goto found;
8401 }
8402 }
8403 gcc_unreachable ();
8404 found:;
8405 }
8406 return error_mark_node;
8407 }
8408
8409 /* We need to evaluate the template arguments, even though this
8410 template-id may be nested within a "sizeof". */
8411 cp_evaluated ev;
8412
8413 new_inner_args = make_tree_vec (nparms);
8414 new_args = add_outermost_template_args (args, new_inner_args);
8415 int pack_adjust = 0;
8416 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8417 {
8418 tree arg;
8419 tree parm;
8420
8421 /* Get the Ith template parameter. */
8422 parm = TREE_VEC_ELT (parms, parm_idx);
8423
8424 if (parm == error_mark_node)
8425 {
8426 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8427 continue;
8428 }
8429
8430 /* Calculate the next argument. */
8431 if (arg_idx < nargs)
8432 arg = TREE_VEC_ELT (inner_args, arg_idx);
8433 else
8434 arg = NULL_TREE;
8435
8436 if (template_parameter_pack_p (TREE_VALUE (parm))
8437 && !(arg && ARGUMENT_PACK_P (arg)))
8438 {
8439 /* Some arguments will be placed in the
8440 template parameter pack PARM. */
8441 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8442 inner_args, arg_idx,
8443 new_args, &lost,
8444 in_decl, complain);
8445
8446 if (arg == NULL_TREE)
8447 {
8448 /* We don't know how many args we have yet, just use the
8449 unconverted (and still packed) ones for now. */
8450 new_inner_args = orig_inner_args;
8451 arg_idx = nargs;
8452 break;
8453 }
8454
8455 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8456
8457 /* Store this argument. */
8458 if (arg == error_mark_node)
8459 {
8460 lost++;
8461 /* We are done with all of the arguments. */
8462 arg_idx = nargs;
8463 break;
8464 }
8465 else
8466 {
8467 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8468 arg_idx += pack_adjust;
8469 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8470 {
8471 ++fixed_packs;
8472 fixed_pack_adjust += pack_adjust;
8473 }
8474 }
8475
8476 continue;
8477 }
8478 else if (arg)
8479 {
8480 if (PACK_EXPANSION_P (arg))
8481 {
8482 /* "If every valid specialization of a variadic template
8483 requires an empty template parameter pack, the template is
8484 ill-formed, no diagnostic required." So check that the
8485 pattern works with this parameter. */
8486 tree pattern = PACK_EXPANSION_PATTERN (arg);
8487 tree conv = convert_template_argument (TREE_VALUE (parm),
8488 pattern, new_args,
8489 complain, parm_idx,
8490 in_decl);
8491 if (conv == error_mark_node)
8492 {
8493 if (complain & tf_error)
8494 inform (input_location, "so any instantiation with a "
8495 "non-empty parameter pack would be ill-formed");
8496 ++lost;
8497 }
8498 else if (TYPE_P (conv) && !TYPE_P (pattern))
8499 /* Recover from missing typename. */
8500 TREE_VEC_ELT (inner_args, arg_idx)
8501 = make_pack_expansion (conv, complain);
8502
8503 /* We don't know how many args we have yet, just
8504 use the unconverted ones for now. */
8505 new_inner_args = inner_args;
8506 arg_idx = nargs;
8507 break;
8508 }
8509 }
8510 else if (require_all_args)
8511 {
8512 /* There must be a default arg in this case. */
8513 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8514 complain, in_decl);
8515 /* The position of the first default template argument,
8516 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8517 Record that. */
8518 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8519 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8520 arg_idx - pack_adjust);
8521 }
8522 else
8523 break;
8524
8525 if (arg == error_mark_node)
8526 {
8527 if (complain & tf_error)
8528 error ("template argument %d is invalid", arg_idx + 1);
8529 }
8530 else if (!arg)
8531 {
8532 /* This can occur if there was an error in the template
8533 parameter list itself (which we would already have
8534 reported) that we are trying to recover from, e.g., a class
8535 template with a parameter list such as
8536 template<typename..., typename> (cpp0x/variadic150.C). */
8537 ++lost;
8538
8539 /* This can also happen with a fixed parameter pack (71834). */
8540 if (arg_idx >= nargs)
8541 ++missing;
8542 }
8543 else
8544 arg = convert_template_argument (TREE_VALUE (parm),
8545 arg, new_args, complain,
8546 parm_idx, in_decl);
8547
8548 if (arg == error_mark_node)
8549 lost++;
8550 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8551 }
8552
8553 if (missing || arg_idx < nargs - variadic_args_p)
8554 {
8555 /* If we had fixed parameter packs, we didn't know how many arguments we
8556 actually needed earlier; now we do. */
8557 nparms += fixed_pack_adjust;
8558 variadic_p -= fixed_packs;
8559 goto bad_nargs;
8560 }
8561
8562 if (arg_idx < nargs)
8563 {
8564 /* We had some pack expansion arguments that will only work if the packs
8565 are empty, but wait until instantiation time to complain.
8566 See variadic-ttp3.C. */
8567 int len = nparms + (nargs - arg_idx);
8568 tree args = make_tree_vec (len);
8569 int i = 0;
8570 for (; i < nparms; ++i)
8571 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8572 for (; i < len; ++i, ++arg_idx)
8573 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8574 arg_idx - pack_adjust);
8575 new_inner_args = args;
8576 }
8577
8578 if (lost)
8579 {
8580 gcc_assert (!(complain & tf_error) || seen_error ());
8581 return error_mark_node;
8582 }
8583
8584 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8585 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8586 TREE_VEC_LENGTH (new_inner_args));
8587
8588 return new_inner_args;
8589 }
8590
8591 /* Convert all template arguments to their appropriate types, and
8592 return a vector containing the innermost resulting template
8593 arguments. If any error occurs, return error_mark_node. Error and
8594 warning messages are not issued.
8595
8596 Note that no function argument deduction is performed, and default
8597 arguments are used to fill in unspecified arguments. */
8598 tree
8599 coerce_template_parms (tree parms, tree args, tree in_decl)
8600 {
8601 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8602 }
8603
8604 /* Convert all template arguments to their appropriate type, and
8605 instantiate default arguments as needed. This returns a vector
8606 containing the innermost resulting template arguments, or
8607 error_mark_node if unsuccessful. */
8608 tree
8609 coerce_template_parms (tree parms, tree args, tree in_decl,
8610 tsubst_flags_t complain)
8611 {
8612 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8613 }
8614
8615 /* Like coerce_template_parms. If PARMS represents all template
8616 parameters levels, this function returns a vector of vectors
8617 representing all the resulting argument levels. Note that in this
8618 case, only the innermost arguments are coerced because the
8619 outermost ones are supposed to have been coerced already.
8620
8621 Otherwise, if PARMS represents only (the innermost) vector of
8622 parameters, this function returns a vector containing just the
8623 innermost resulting arguments. */
8624
8625 static tree
8626 coerce_innermost_template_parms (tree parms,
8627 tree args,
8628 tree in_decl,
8629 tsubst_flags_t complain,
8630 bool require_all_args,
8631 bool use_default_args)
8632 {
8633 int parms_depth = TMPL_PARMS_DEPTH (parms);
8634 int args_depth = TMPL_ARGS_DEPTH (args);
8635 tree coerced_args;
8636
8637 if (parms_depth > 1)
8638 {
8639 coerced_args = make_tree_vec (parms_depth);
8640 tree level;
8641 int cur_depth;
8642
8643 for (level = parms, cur_depth = parms_depth;
8644 parms_depth > 0 && level != NULL_TREE;
8645 level = TREE_CHAIN (level), --cur_depth)
8646 {
8647 tree l;
8648 if (cur_depth == args_depth)
8649 l = coerce_template_parms (TREE_VALUE (level),
8650 args, in_decl, complain,
8651 require_all_args,
8652 use_default_args);
8653 else
8654 l = TMPL_ARGS_LEVEL (args, cur_depth);
8655
8656 if (l == error_mark_node)
8657 return error_mark_node;
8658
8659 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8660 }
8661 }
8662 else
8663 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8664 args, in_decl, complain,
8665 require_all_args,
8666 use_default_args);
8667 return coerced_args;
8668 }
8669
8670 /* Returns 1 if template args OT and NT are equivalent. */
8671
8672 int
8673 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8674 {
8675 if (nt == ot)
8676 return 1;
8677 if (nt == NULL_TREE || ot == NULL_TREE)
8678 return false;
8679 if (nt == any_targ_node || ot == any_targ_node)
8680 return true;
8681
8682 if (TREE_CODE (nt) == TREE_VEC)
8683 /* For member templates */
8684 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8685 else if (PACK_EXPANSION_P (ot))
8686 return (PACK_EXPANSION_P (nt)
8687 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8688 PACK_EXPANSION_PATTERN (nt))
8689 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8690 PACK_EXPANSION_EXTRA_ARGS (nt)));
8691 else if (ARGUMENT_PACK_P (ot))
8692 {
8693 int i, len;
8694 tree opack, npack;
8695
8696 if (!ARGUMENT_PACK_P (nt))
8697 return 0;
8698
8699 opack = ARGUMENT_PACK_ARGS (ot);
8700 npack = ARGUMENT_PACK_ARGS (nt);
8701 len = TREE_VEC_LENGTH (opack);
8702 if (TREE_VEC_LENGTH (npack) != len)
8703 return 0;
8704 for (i = 0; i < len; ++i)
8705 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8706 TREE_VEC_ELT (npack, i)))
8707 return 0;
8708 return 1;
8709 }
8710 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8711 gcc_unreachable ();
8712 else if (TYPE_P (nt))
8713 {
8714 if (!TYPE_P (ot))
8715 return false;
8716 /* Don't treat an alias template specialization with dependent
8717 arguments as equivalent to its underlying type when used as a
8718 template argument; we need them to be distinct so that we
8719 substitute into the specialization arguments at instantiation
8720 time. And aliases can't be equivalent without being ==, so
8721 we don't need to look any deeper.
8722
8723 During partial ordering, however, we need to treat them normally so
8724 that we can order uses of the same alias with different
8725 cv-qualification (79960). */
8726 if (!partial_order
8727 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8728 return false;
8729 else
8730 return same_type_p (ot, nt);
8731 }
8732 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8733 return 0;
8734 else
8735 {
8736 /* Try to treat a template non-type argument that has been converted
8737 to the parameter type as equivalent to one that hasn't yet. */
8738 for (enum tree_code code1 = TREE_CODE (ot);
8739 CONVERT_EXPR_CODE_P (code1)
8740 || code1 == NON_LVALUE_EXPR;
8741 code1 = TREE_CODE (ot))
8742 ot = TREE_OPERAND (ot, 0);
8743 for (enum tree_code code2 = TREE_CODE (nt);
8744 CONVERT_EXPR_CODE_P (code2)
8745 || code2 == NON_LVALUE_EXPR;
8746 code2 = TREE_CODE (nt))
8747 nt = TREE_OPERAND (nt, 0);
8748
8749 return cp_tree_equal (ot, nt);
8750 }
8751 }
8752
8753 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8754 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8755 NEWARG_PTR with the offending arguments if they are non-NULL. */
8756
8757 int
8758 comp_template_args (tree oldargs, tree newargs,
8759 tree *oldarg_ptr, tree *newarg_ptr,
8760 bool partial_order)
8761 {
8762 int i;
8763
8764 if (oldargs == newargs)
8765 return 1;
8766
8767 if (!oldargs || !newargs)
8768 return 0;
8769
8770 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8771 return 0;
8772
8773 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8774 {
8775 tree nt = TREE_VEC_ELT (newargs, i);
8776 tree ot = TREE_VEC_ELT (oldargs, i);
8777
8778 if (! template_args_equal (ot, nt, partial_order))
8779 {
8780 if (oldarg_ptr != NULL)
8781 *oldarg_ptr = ot;
8782 if (newarg_ptr != NULL)
8783 *newarg_ptr = nt;
8784 return 0;
8785 }
8786 }
8787 return 1;
8788 }
8789
8790 inline bool
8791 comp_template_args_porder (tree oargs, tree nargs)
8792 {
8793 return comp_template_args (oargs, nargs, NULL, NULL, true);
8794 }
8795
8796 /* Implement a freelist interface for objects of type T.
8797
8798 Head is a separate object, rather than a regular member, so that we
8799 can define it as a GTY deletable pointer, which is highly
8800 desirable. A data member could be declared that way, but then the
8801 containing object would implicitly get GTY((user)), which would
8802 prevent us from instantiating freelists as global objects.
8803 Although this way we can create freelist global objects, they're
8804 such thin wrappers that instantiating temporaries at every use
8805 loses nothing and saves permanent storage for the freelist object.
8806
8807 Member functions next, anew, poison and reinit have default
8808 implementations that work for most of the types we're interested
8809 in, but if they don't work for some type, they should be explicitly
8810 specialized. See the comments before them for requirements, and
8811 the example specializations for the tree_list_freelist. */
8812 template <typename T>
8813 class freelist
8814 {
8815 /* Return the next object in a chain. We could just do type
8816 punning, but if we access the object with its underlying type, we
8817 avoid strict-aliasing trouble. This needs only work between
8818 poison and reinit. */
8819 static T *&next (T *obj) { return obj->next; }
8820
8821 /* Return a newly allocated, uninitialized or minimally-initialized
8822 object of type T. Any initialization performed by anew should
8823 either remain across the life of the object and the execution of
8824 poison, or be redone by reinit. */
8825 static T *anew () { return ggc_alloc<T> (); }
8826
8827 /* Optionally scribble all over the bits holding the object, so that
8828 they become (mostly?) uninitialized memory. This is called while
8829 preparing to make the object part of the free list. */
8830 static void poison (T *obj) {
8831 T *p ATTRIBUTE_UNUSED = obj;
8832 T **q ATTRIBUTE_UNUSED = &next (obj);
8833
8834 #ifdef ENABLE_GC_CHECKING
8835 /* Poison the data, to indicate the data is garbage. */
8836 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8837 memset (p, 0xa5, sizeof (*p));
8838 #endif
8839 /* Let valgrind know the object is free. */
8840 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8841
8842 /* Let valgrind know the next portion of the object is available,
8843 but uninitialized. */
8844 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8845 }
8846
8847 /* Bring an object that underwent at least one lifecycle after anew
8848 and before the most recent free and poison, back to a usable
8849 state, reinitializing whatever is needed for it to be
8850 functionally equivalent to an object just allocated and returned
8851 by anew. This may poison or clear the next field, used by
8852 freelist housekeeping after poison was called. */
8853 static void reinit (T *obj) {
8854 T **q ATTRIBUTE_UNUSED = &next (obj);
8855
8856 #ifdef ENABLE_GC_CHECKING
8857 memset (q, 0xa5, sizeof (*q));
8858 #endif
8859 /* Let valgrind know the entire object is available, but
8860 uninitialized. */
8861 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8862 }
8863
8864 /* Reference a GTY-deletable pointer that points to the first object
8865 in the free list proper. */
8866 T *&head;
8867 public:
8868 /* Construct a freelist object chaining objects off of HEAD. */
8869 freelist (T *&head) : head(head) {}
8870
8871 /* Add OBJ to the free object list. The former head becomes OBJ's
8872 successor. */
8873 void free (T *obj)
8874 {
8875 poison (obj);
8876 next (obj) = head;
8877 head = obj;
8878 }
8879
8880 /* Take an object from the free list, if one is available, or
8881 allocate a new one. Objects taken from the free list should be
8882 regarded as filled with garbage, except for bits that are
8883 configured to be preserved across free and alloc. */
8884 T *alloc ()
8885 {
8886 if (head)
8887 {
8888 T *obj = head;
8889 head = next (head);
8890 reinit (obj);
8891 return obj;
8892 }
8893 else
8894 return anew ();
8895 }
8896 };
8897
8898 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8899 want to allocate a TREE_LIST using the usual interface, and ensure
8900 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8901 build_tree_list logic in reinit, so this could go out of sync. */
8902 template <>
8903 inline tree &
8904 freelist<tree_node>::next (tree obj)
8905 {
8906 return TREE_CHAIN (obj);
8907 }
8908 template <>
8909 inline tree
8910 freelist<tree_node>::anew ()
8911 {
8912 return build_tree_list (NULL, NULL);
8913 }
8914 template <>
8915 inline void
8916 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8917 {
8918 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8919 tree p ATTRIBUTE_UNUSED = obj;
8920 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8921 tree *q ATTRIBUTE_UNUSED = &next (obj);
8922
8923 #ifdef ENABLE_GC_CHECKING
8924 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8925
8926 /* Poison the data, to indicate the data is garbage. */
8927 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8928 memset (p, 0xa5, size);
8929 #endif
8930 /* Let valgrind know the object is free. */
8931 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8932 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8933 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8934 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8935
8936 #ifdef ENABLE_GC_CHECKING
8937 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8938 /* Keep TREE_CHAIN functional. */
8939 TREE_SET_CODE (obj, TREE_LIST);
8940 #else
8941 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8942 #endif
8943 }
8944 template <>
8945 inline void
8946 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8947 {
8948 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8949
8950 #ifdef ENABLE_GC_CHECKING
8951 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8952 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8953 memset (obj, 0, sizeof (tree_list));
8954 #endif
8955
8956 /* Let valgrind know the entire object is available, but
8957 uninitialized. */
8958 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8959
8960 #ifdef ENABLE_GC_CHECKING
8961 TREE_SET_CODE (obj, TREE_LIST);
8962 #else
8963 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8964 #endif
8965 }
8966
8967 /* Point to the first object in the TREE_LIST freelist. */
8968 static GTY((deletable)) tree tree_list_freelist_head;
8969 /* Return the/an actual TREE_LIST freelist. */
8970 static inline freelist<tree_node>
8971 tree_list_freelist ()
8972 {
8973 return tree_list_freelist_head;
8974 }
8975
8976 /* Point to the first object in the tinst_level freelist. */
8977 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
8978 /* Return the/an actual tinst_level freelist. */
8979 static inline freelist<tinst_level>
8980 tinst_level_freelist ()
8981 {
8982 return tinst_level_freelist_head;
8983 }
8984
8985 /* Point to the first object in the pending_template freelist. */
8986 static GTY((deletable)) pending_template *pending_template_freelist_head;
8987 /* Return the/an actual pending_template freelist. */
8988 static inline freelist<pending_template>
8989 pending_template_freelist ()
8990 {
8991 return pending_template_freelist_head;
8992 }
8993
8994 /* Build the TREE_LIST object out of a split list, store it
8995 permanently, and return it. */
8996 tree
8997 tinst_level::to_list ()
8998 {
8999 gcc_assert (split_list_p ());
9000 tree ret = tree_list_freelist ().alloc ();
9001 TREE_PURPOSE (ret) = tldcl;
9002 TREE_VALUE (ret) = targs;
9003 tldcl = ret;
9004 targs = NULL;
9005 gcc_assert (tree_list_p ());
9006 return ret;
9007 }
9008
9009 const unsigned short tinst_level::refcount_infinity;
9010
9011 /* Increment OBJ's refcount unless it is already infinite. */
9012 static tinst_level *
9013 inc_refcount_use (tinst_level *obj)
9014 {
9015 if (obj && obj->refcount != tinst_level::refcount_infinity)
9016 ++obj->refcount;
9017 return obj;
9018 }
9019
9020 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9021 void
9022 tinst_level::free (tinst_level *obj)
9023 {
9024 if (obj->tree_list_p ())
9025 tree_list_freelist ().free (obj->get_node ());
9026 tinst_level_freelist ().free (obj);
9027 }
9028
9029 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9030 OBJ's DECL and OBJ, and start over with the tinst_level object that
9031 used to be referenced by OBJ's NEXT. */
9032 static void
9033 dec_refcount_use (tinst_level *obj)
9034 {
9035 while (obj
9036 && obj->refcount != tinst_level::refcount_infinity
9037 && !--obj->refcount)
9038 {
9039 tinst_level *next = obj->next;
9040 tinst_level::free (obj);
9041 obj = next;
9042 }
9043 }
9044
9045 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9046 and of the former PTR. Omitting the second argument is equivalent
9047 to passing (T*)NULL; this is allowed because passing the
9048 zero-valued integral constant NULL confuses type deduction and/or
9049 overload resolution. */
9050 template <typename T>
9051 static void
9052 set_refcount_ptr (T *& ptr, T *obj = NULL)
9053 {
9054 T *save = ptr;
9055 ptr = inc_refcount_use (obj);
9056 dec_refcount_use (save);
9057 }
9058
9059 static void
9060 add_pending_template (tree d)
9061 {
9062 tree ti = (TYPE_P (d)
9063 ? CLASSTYPE_TEMPLATE_INFO (d)
9064 : DECL_TEMPLATE_INFO (d));
9065 struct pending_template *pt;
9066 int level;
9067
9068 if (TI_PENDING_TEMPLATE_FLAG (ti))
9069 return;
9070
9071 /* We are called both from instantiate_decl, where we've already had a
9072 tinst_level pushed, and instantiate_template, where we haven't.
9073 Compensate. */
9074 gcc_assert (TREE_CODE (d) != TREE_LIST);
9075 level = !current_tinst_level
9076 || current_tinst_level->maybe_get_node () != d;
9077
9078 if (level)
9079 push_tinst_level (d);
9080
9081 pt = pending_template_freelist ().alloc ();
9082 pt->next = NULL;
9083 pt->tinst = NULL;
9084 set_refcount_ptr (pt->tinst, current_tinst_level);
9085 if (last_pending_template)
9086 last_pending_template->next = pt;
9087 else
9088 pending_templates = pt;
9089
9090 last_pending_template = pt;
9091
9092 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9093
9094 if (level)
9095 pop_tinst_level ();
9096 }
9097
9098
9099 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9100 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9101 documentation for TEMPLATE_ID_EXPR. */
9102
9103 tree
9104 lookup_template_function (tree fns, tree arglist)
9105 {
9106 if (fns == error_mark_node || arglist == error_mark_node)
9107 return error_mark_node;
9108
9109 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9110
9111 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9112 {
9113 error ("%q#D is not a function template", fns);
9114 return error_mark_node;
9115 }
9116
9117 if (BASELINK_P (fns))
9118 {
9119 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9120 unknown_type_node,
9121 BASELINK_FUNCTIONS (fns),
9122 arglist);
9123 return fns;
9124 }
9125
9126 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9127 }
9128
9129 /* Within the scope of a template class S<T>, the name S gets bound
9130 (in build_self_reference) to a TYPE_DECL for the class, not a
9131 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9132 or one of its enclosing classes, and that type is a template,
9133 return the associated TEMPLATE_DECL. Otherwise, the original
9134 DECL is returned.
9135
9136 Also handle the case when DECL is a TREE_LIST of ambiguous
9137 injected-class-names from different bases. */
9138
9139 tree
9140 maybe_get_template_decl_from_type_decl (tree decl)
9141 {
9142 if (decl == NULL_TREE)
9143 return decl;
9144
9145 /* DR 176: A lookup that finds an injected-class-name (10.2
9146 [class.member.lookup]) can result in an ambiguity in certain cases
9147 (for example, if it is found in more than one base class). If all of
9148 the injected-class-names that are found refer to specializations of
9149 the same class template, and if the name is followed by a
9150 template-argument-list, the reference refers to the class template
9151 itself and not a specialization thereof, and is not ambiguous. */
9152 if (TREE_CODE (decl) == TREE_LIST)
9153 {
9154 tree t, tmpl = NULL_TREE;
9155 for (t = decl; t; t = TREE_CHAIN (t))
9156 {
9157 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9158 if (!tmpl)
9159 tmpl = elt;
9160 else if (tmpl != elt)
9161 break;
9162 }
9163 if (tmpl && t == NULL_TREE)
9164 return tmpl;
9165 else
9166 return decl;
9167 }
9168
9169 return (decl != NULL_TREE
9170 && DECL_SELF_REFERENCE_P (decl)
9171 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9172 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9173 }
9174
9175 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9176 parameters, find the desired type.
9177
9178 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9179
9180 IN_DECL, if non-NULL, is the template declaration we are trying to
9181 instantiate.
9182
9183 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9184 the class we are looking up.
9185
9186 Issue error and warning messages under control of COMPLAIN.
9187
9188 If the template class is really a local class in a template
9189 function, then the FUNCTION_CONTEXT is the function in which it is
9190 being instantiated.
9191
9192 ??? Note that this function is currently called *twice* for each
9193 template-id: the first time from the parser, while creating the
9194 incomplete type (finish_template_type), and the second type during the
9195 real instantiation (instantiate_template_class). This is surely something
9196 that we want to avoid. It also causes some problems with argument
9197 coercion (see convert_nontype_argument for more information on this). */
9198
9199 static tree
9200 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9201 int entering_scope, tsubst_flags_t complain)
9202 {
9203 tree templ = NULL_TREE, parmlist;
9204 tree t;
9205 spec_entry **slot;
9206 spec_entry *entry;
9207 spec_entry elt;
9208 hashval_t hash;
9209
9210 if (identifier_p (d1))
9211 {
9212 tree value = innermost_non_namespace_value (d1);
9213 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9214 templ = value;
9215 else
9216 {
9217 if (context)
9218 push_decl_namespace (context);
9219 templ = lookup_name (d1);
9220 templ = maybe_get_template_decl_from_type_decl (templ);
9221 if (context)
9222 pop_decl_namespace ();
9223 }
9224 if (templ)
9225 context = DECL_CONTEXT (templ);
9226 }
9227 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9228 {
9229 tree type = TREE_TYPE (d1);
9230
9231 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9232 an implicit typename for the second A. Deal with it. */
9233 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9234 type = TREE_TYPE (type);
9235
9236 if (CLASSTYPE_TEMPLATE_INFO (type))
9237 {
9238 templ = CLASSTYPE_TI_TEMPLATE (type);
9239 d1 = DECL_NAME (templ);
9240 }
9241 }
9242 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9243 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9244 {
9245 templ = TYPE_TI_TEMPLATE (d1);
9246 d1 = DECL_NAME (templ);
9247 }
9248 else if (DECL_TYPE_TEMPLATE_P (d1))
9249 {
9250 templ = d1;
9251 d1 = DECL_NAME (templ);
9252 context = DECL_CONTEXT (templ);
9253 }
9254 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9255 {
9256 templ = d1;
9257 d1 = DECL_NAME (templ);
9258 }
9259
9260 /* Issue an error message if we didn't find a template. */
9261 if (! templ)
9262 {
9263 if (complain & tf_error)
9264 error ("%qT is not a template", d1);
9265 return error_mark_node;
9266 }
9267
9268 if (TREE_CODE (templ) != TEMPLATE_DECL
9269 /* Make sure it's a user visible template, if it was named by
9270 the user. */
9271 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9272 && !PRIMARY_TEMPLATE_P (templ)))
9273 {
9274 if (complain & tf_error)
9275 {
9276 error ("non-template type %qT used as a template", d1);
9277 if (in_decl)
9278 error ("for template declaration %q+D", in_decl);
9279 }
9280 return error_mark_node;
9281 }
9282
9283 complain &= ~tf_user;
9284
9285 /* An alias that just changes the name of a template is equivalent to the
9286 other template, so if any of the arguments are pack expansions, strip
9287 the alias to avoid problems with a pack expansion passed to a non-pack
9288 alias template parameter (DR 1430). */
9289 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9290 templ = get_underlying_template (templ);
9291
9292 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9293 {
9294 tree parm;
9295 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9296 if (arglist2 == error_mark_node
9297 || (!uses_template_parms (arglist2)
9298 && check_instantiated_args (templ, arglist2, complain)))
9299 return error_mark_node;
9300
9301 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9302 return parm;
9303 }
9304 else
9305 {
9306 tree template_type = TREE_TYPE (templ);
9307 tree gen_tmpl;
9308 tree type_decl;
9309 tree found = NULL_TREE;
9310 int arg_depth;
9311 int parm_depth;
9312 int is_dependent_type;
9313 int use_partial_inst_tmpl = false;
9314
9315 if (template_type == error_mark_node)
9316 /* An error occurred while building the template TEMPL, and a
9317 diagnostic has most certainly been emitted for that
9318 already. Let's propagate that error. */
9319 return error_mark_node;
9320
9321 gen_tmpl = most_general_template (templ);
9322 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9323 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9324 arg_depth = TMPL_ARGS_DEPTH (arglist);
9325
9326 if (arg_depth == 1 && parm_depth > 1)
9327 {
9328 /* We've been given an incomplete set of template arguments.
9329 For example, given:
9330
9331 template <class T> struct S1 {
9332 template <class U> struct S2 {};
9333 template <class U> struct S2<U*> {};
9334 };
9335
9336 we will be called with an ARGLIST of `U*', but the
9337 TEMPLATE will be `template <class T> template
9338 <class U> struct S1<T>::S2'. We must fill in the missing
9339 arguments. */
9340 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9341 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9342 arg_depth = TMPL_ARGS_DEPTH (arglist);
9343 }
9344
9345 /* Now we should have enough arguments. */
9346 gcc_assert (parm_depth == arg_depth);
9347
9348 /* From here on, we're only interested in the most general
9349 template. */
9350
9351 /* Calculate the BOUND_ARGS. These will be the args that are
9352 actually tsubst'd into the definition to create the
9353 instantiation. */
9354 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9355 complain,
9356 /*require_all_args=*/true,
9357 /*use_default_args=*/true);
9358
9359 if (arglist == error_mark_node)
9360 /* We were unable to bind the arguments. */
9361 return error_mark_node;
9362
9363 /* In the scope of a template class, explicit references to the
9364 template class refer to the type of the template, not any
9365 instantiation of it. For example, in:
9366
9367 template <class T> class C { void f(C<T>); }
9368
9369 the `C<T>' is just the same as `C'. Outside of the
9370 class, however, such a reference is an instantiation. */
9371 if (entering_scope
9372 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9373 || currently_open_class (template_type))
9374 {
9375 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9376
9377 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9378 return template_type;
9379 }
9380
9381 /* If we already have this specialization, return it. */
9382 elt.tmpl = gen_tmpl;
9383 elt.args = arglist;
9384 elt.spec = NULL_TREE;
9385 hash = spec_hasher::hash (&elt);
9386 entry = type_specializations->find_with_hash (&elt, hash);
9387
9388 if (entry)
9389 return entry->spec;
9390
9391 /* If the the template's constraints are not satisfied,
9392 then we cannot form a valid type.
9393
9394 Note that the check is deferred until after the hash
9395 lookup. This prevents redundant checks on previously
9396 instantiated specializations. */
9397 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9398 {
9399 if (complain & tf_error)
9400 {
9401 auto_diagnostic_group d;
9402 error ("template constraint failure");
9403 diagnose_constraints (input_location, gen_tmpl, arglist);
9404 }
9405 return error_mark_node;
9406 }
9407
9408 is_dependent_type = uses_template_parms (arglist);
9409
9410 /* If the deduced arguments are invalid, then the binding
9411 failed. */
9412 if (!is_dependent_type
9413 && check_instantiated_args (gen_tmpl,
9414 INNERMOST_TEMPLATE_ARGS (arglist),
9415 complain))
9416 return error_mark_node;
9417
9418 if (!is_dependent_type
9419 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9420 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9421 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9422 {
9423 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9424 DECL_NAME (gen_tmpl),
9425 /*tag_scope=*/ts_global);
9426 return found;
9427 }
9428
9429 context = DECL_CONTEXT (gen_tmpl);
9430 if (context && TYPE_P (context))
9431 {
9432 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9433 context = complete_type (context);
9434 }
9435 else
9436 context = tsubst (context, arglist, complain, in_decl);
9437
9438 if (context == error_mark_node)
9439 return error_mark_node;
9440
9441 if (!context)
9442 context = global_namespace;
9443
9444 /* Create the type. */
9445 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9446 {
9447 /* The user referred to a specialization of an alias
9448 template represented by GEN_TMPL.
9449
9450 [temp.alias]/2 says:
9451
9452 When a template-id refers to the specialization of an
9453 alias template, it is equivalent to the associated
9454 type obtained by substitution of its
9455 template-arguments for the template-parameters in the
9456 type-id of the alias template. */
9457
9458 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9459 /* Note that the call above (by indirectly calling
9460 register_specialization in tsubst_decl) registers the
9461 TYPE_DECL representing the specialization of the alias
9462 template. So next time someone substitutes ARGLIST for
9463 the template parms into the alias template (GEN_TMPL),
9464 she'll get that TYPE_DECL back. */
9465
9466 if (t == error_mark_node)
9467 return t;
9468 }
9469 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9470 {
9471 if (!is_dependent_type)
9472 {
9473 set_current_access_from_decl (TYPE_NAME (template_type));
9474 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9475 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9476 arglist, complain, in_decl),
9477 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9478 arglist, complain, in_decl),
9479 SCOPED_ENUM_P (template_type), NULL);
9480
9481 if (t == error_mark_node)
9482 return t;
9483 }
9484 else
9485 {
9486 /* We don't want to call start_enum for this type, since
9487 the values for the enumeration constants may involve
9488 template parameters. And, no one should be interested
9489 in the enumeration constants for such a type. */
9490 t = cxx_make_type (ENUMERAL_TYPE);
9491 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9492 }
9493 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9494 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9495 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9496 }
9497 else if (CLASS_TYPE_P (template_type))
9498 {
9499 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9500 instantiated here. */
9501 gcc_assert (!LAMBDA_TYPE_P (template_type));
9502
9503 t = make_class_type (TREE_CODE (template_type));
9504 CLASSTYPE_DECLARED_CLASS (t)
9505 = CLASSTYPE_DECLARED_CLASS (template_type);
9506 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9507
9508 /* A local class. Make sure the decl gets registered properly. */
9509 if (context == current_function_decl)
9510 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9511 == error_mark_node)
9512 return error_mark_node;
9513
9514 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9515 /* This instantiation is another name for the primary
9516 template type. Set the TYPE_CANONICAL field
9517 appropriately. */
9518 TYPE_CANONICAL (t) = template_type;
9519 else if (any_template_arguments_need_structural_equality_p (arglist))
9520 /* Some of the template arguments require structural
9521 equality testing, so this template class requires
9522 structural equality testing. */
9523 SET_TYPE_STRUCTURAL_EQUALITY (t);
9524 }
9525 else
9526 gcc_unreachable ();
9527
9528 /* If we called start_enum or pushtag above, this information
9529 will already be set up. */
9530 if (!TYPE_NAME (t))
9531 {
9532 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9533
9534 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9535 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9536 DECL_SOURCE_LOCATION (type_decl)
9537 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9538 }
9539 else
9540 type_decl = TYPE_NAME (t);
9541
9542 if (CLASS_TYPE_P (template_type))
9543 {
9544 TREE_PRIVATE (type_decl)
9545 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9546 TREE_PROTECTED (type_decl)
9547 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9548 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9549 {
9550 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9551 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9552 }
9553 }
9554
9555 if (OVERLOAD_TYPE_P (t)
9556 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9557 {
9558 static const char *tags[] = {"abi_tag", "may_alias"};
9559
9560 for (unsigned ix = 0; ix != 2; ix++)
9561 {
9562 tree attributes
9563 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9564
9565 if (attributes)
9566 TYPE_ATTRIBUTES (t)
9567 = tree_cons (TREE_PURPOSE (attributes),
9568 TREE_VALUE (attributes),
9569 TYPE_ATTRIBUTES (t));
9570 }
9571 }
9572
9573 /* Let's consider the explicit specialization of a member
9574 of a class template specialization that is implicitly instantiated,
9575 e.g.:
9576 template<class T>
9577 struct S
9578 {
9579 template<class U> struct M {}; //#0
9580 };
9581
9582 template<>
9583 template<>
9584 struct S<int>::M<char> //#1
9585 {
9586 int i;
9587 };
9588 [temp.expl.spec]/4 says this is valid.
9589
9590 In this case, when we write:
9591 S<int>::M<char> m;
9592
9593 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9594 the one of #0.
9595
9596 When we encounter #1, we want to store the partial instantiation
9597 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9598
9599 For all cases other than this "explicit specialization of member of a
9600 class template", we just want to store the most general template into
9601 the CLASSTYPE_TI_TEMPLATE of M.
9602
9603 This case of "explicit specialization of member of a class template"
9604 only happens when:
9605 1/ the enclosing class is an instantiation of, and therefore not
9606 the same as, the context of the most general template, and
9607 2/ we aren't looking at the partial instantiation itself, i.e.
9608 the innermost arguments are not the same as the innermost parms of
9609 the most general template.
9610
9611 So it's only when 1/ and 2/ happens that we want to use the partial
9612 instantiation of the member template in lieu of its most general
9613 template. */
9614
9615 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9616 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9617 /* the enclosing class must be an instantiation... */
9618 && CLASS_TYPE_P (context)
9619 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9620 {
9621 TREE_VEC_LENGTH (arglist)--;
9622 ++processing_template_decl;
9623 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9624 tree partial_inst_args =
9625 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9626 arglist, complain, NULL_TREE);
9627 --processing_template_decl;
9628 TREE_VEC_LENGTH (arglist)++;
9629 if (partial_inst_args == error_mark_node)
9630 return error_mark_node;
9631 use_partial_inst_tmpl =
9632 /*...and we must not be looking at the partial instantiation
9633 itself. */
9634 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9635 partial_inst_args);
9636 }
9637
9638 if (!use_partial_inst_tmpl)
9639 /* This case is easy; there are no member templates involved. */
9640 found = gen_tmpl;
9641 else
9642 {
9643 /* This is a full instantiation of a member template. Find
9644 the partial instantiation of which this is an instance. */
9645
9646 /* Temporarily reduce by one the number of levels in the ARGLIST
9647 so as to avoid comparing the last set of arguments. */
9648 TREE_VEC_LENGTH (arglist)--;
9649 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9650 TREE_VEC_LENGTH (arglist)++;
9651 /* FOUND is either a proper class type, or an alias
9652 template specialization. In the later case, it's a
9653 TYPE_DECL, resulting from the substituting of arguments
9654 for parameters in the TYPE_DECL of the alias template
9655 done earlier. So be careful while getting the template
9656 of FOUND. */
9657 found = (TREE_CODE (found) == TEMPLATE_DECL
9658 ? found
9659 : (TREE_CODE (found) == TYPE_DECL
9660 ? DECL_TI_TEMPLATE (found)
9661 : CLASSTYPE_TI_TEMPLATE (found)));
9662 }
9663
9664 // Build template info for the new specialization.
9665 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9666
9667 elt.spec = t;
9668 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9669 entry = ggc_alloc<spec_entry> ();
9670 *entry = elt;
9671 *slot = entry;
9672
9673 /* Note this use of the partial instantiation so we can check it
9674 later in maybe_process_partial_specialization. */
9675 DECL_TEMPLATE_INSTANTIATIONS (found)
9676 = tree_cons (arglist, t,
9677 DECL_TEMPLATE_INSTANTIATIONS (found));
9678
9679 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9680 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9681 /* Now that the type has been registered on the instantiations
9682 list, we set up the enumerators. Because the enumeration
9683 constants may involve the enumeration type itself, we make
9684 sure to register the type first, and then create the
9685 constants. That way, doing tsubst_expr for the enumeration
9686 constants won't result in recursive calls here; we'll find
9687 the instantiation and exit above. */
9688 tsubst_enum (template_type, t, arglist);
9689
9690 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9691 /* If the type makes use of template parameters, the
9692 code that generates debugging information will crash. */
9693 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9694
9695 /* Possibly limit visibility based on template args. */
9696 TREE_PUBLIC (type_decl) = 1;
9697 determine_visibility (type_decl);
9698
9699 inherit_targ_abi_tags (t);
9700
9701 return t;
9702 }
9703 }
9704
9705 /* Wrapper for lookup_template_class_1. */
9706
9707 tree
9708 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9709 int entering_scope, tsubst_flags_t complain)
9710 {
9711 tree ret;
9712 timevar_push (TV_TEMPLATE_INST);
9713 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9714 entering_scope, complain);
9715 timevar_pop (TV_TEMPLATE_INST);
9716 return ret;
9717 }
9718
9719 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9720
9721 tree
9722 lookup_template_variable (tree templ, tree arglist)
9723 {
9724 /* The type of the expression is NULL_TREE since the template-id could refer
9725 to an explicit or partial specialization. */
9726 tree type = NULL_TREE;
9727 if (flag_concepts && variable_concept_p (templ))
9728 /* Except that concepts are always bool. */
9729 type = boolean_type_node;
9730 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9731 }
9732
9733 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9734
9735 tree
9736 finish_template_variable (tree var, tsubst_flags_t complain)
9737 {
9738 tree templ = TREE_OPERAND (var, 0);
9739 tree arglist = TREE_OPERAND (var, 1);
9740
9741 /* We never want to return a VAR_DECL for a variable concept, since they
9742 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9743 bool concept_p = flag_concepts && variable_concept_p (templ);
9744 if (concept_p && processing_template_decl)
9745 return var;
9746
9747 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9748 arglist = add_outermost_template_args (tmpl_args, arglist);
9749
9750 templ = most_general_template (templ);
9751 tree parms = DECL_TEMPLATE_PARMS (templ);
9752 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9753 /*req_all*/true,
9754 /*use_default*/true);
9755
9756 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9757 {
9758 if (complain & tf_error)
9759 {
9760 auto_diagnostic_group d;
9761 error ("use of invalid variable template %qE", var);
9762 diagnose_constraints (location_of (var), templ, arglist);
9763 }
9764 return error_mark_node;
9765 }
9766
9767 /* If a template-id refers to a specialization of a variable
9768 concept, then the expression is true if and only if the
9769 concept's constraints are satisfied by the given template
9770 arguments.
9771
9772 NOTE: This is an extension of Concepts Lite TS that
9773 allows constraints to be used in expressions. */
9774 if (concept_p)
9775 {
9776 tree decl = DECL_TEMPLATE_RESULT (templ);
9777 return evaluate_variable_concept (decl, arglist);
9778 }
9779
9780 return instantiate_template (templ, arglist, complain);
9781 }
9782
9783 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9784 TARGS template args, and instantiate it if it's not dependent. */
9785
9786 tree
9787 lookup_and_finish_template_variable (tree templ, tree targs,
9788 tsubst_flags_t complain)
9789 {
9790 templ = lookup_template_variable (templ, targs);
9791 if (!any_dependent_template_arguments_p (targs))
9792 {
9793 templ = finish_template_variable (templ, complain);
9794 mark_used (templ);
9795 }
9796
9797 return convert_from_reference (templ);
9798 }
9799
9800 \f
9801 struct pair_fn_data
9802 {
9803 tree_fn_t fn;
9804 tree_fn_t any_fn;
9805 void *data;
9806 /* True when we should also visit template parameters that occur in
9807 non-deduced contexts. */
9808 bool include_nondeduced_p;
9809 hash_set<tree> *visited;
9810 };
9811
9812 /* Called from for_each_template_parm via walk_tree. */
9813
9814 static tree
9815 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9816 {
9817 tree t = *tp;
9818 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9819 tree_fn_t fn = pfd->fn;
9820 void *data = pfd->data;
9821 tree result = NULL_TREE;
9822
9823 #define WALK_SUBTREE(NODE) \
9824 do \
9825 { \
9826 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9827 pfd->include_nondeduced_p, \
9828 pfd->any_fn); \
9829 if (result) goto out; \
9830 } \
9831 while (0)
9832
9833 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9834 return t;
9835
9836 if (TYPE_P (t)
9837 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9838 WALK_SUBTREE (TYPE_CONTEXT (t));
9839
9840 switch (TREE_CODE (t))
9841 {
9842 case RECORD_TYPE:
9843 if (TYPE_PTRMEMFUNC_P (t))
9844 break;
9845 /* Fall through. */
9846
9847 case UNION_TYPE:
9848 case ENUMERAL_TYPE:
9849 if (!TYPE_TEMPLATE_INFO (t))
9850 *walk_subtrees = 0;
9851 else
9852 WALK_SUBTREE (TYPE_TI_ARGS (t));
9853 break;
9854
9855 case INTEGER_TYPE:
9856 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9857 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9858 break;
9859
9860 case METHOD_TYPE:
9861 /* Since we're not going to walk subtrees, we have to do this
9862 explicitly here. */
9863 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9864 /* Fall through. */
9865
9866 case FUNCTION_TYPE:
9867 /* Check the return type. */
9868 WALK_SUBTREE (TREE_TYPE (t));
9869
9870 /* Check the parameter types. Since default arguments are not
9871 instantiated until they are needed, the TYPE_ARG_TYPES may
9872 contain expressions that involve template parameters. But,
9873 no-one should be looking at them yet. And, once they're
9874 instantiated, they don't contain template parameters, so
9875 there's no point in looking at them then, either. */
9876 {
9877 tree parm;
9878
9879 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9880 WALK_SUBTREE (TREE_VALUE (parm));
9881
9882 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9883 want walk_tree walking into them itself. */
9884 *walk_subtrees = 0;
9885 }
9886
9887 if (flag_noexcept_type)
9888 {
9889 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9890 if (spec)
9891 WALK_SUBTREE (TREE_PURPOSE (spec));
9892 }
9893 break;
9894
9895 case TYPEOF_TYPE:
9896 case DECLTYPE_TYPE:
9897 case UNDERLYING_TYPE:
9898 if (pfd->include_nondeduced_p
9899 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9900 pfd->visited,
9901 pfd->include_nondeduced_p,
9902 pfd->any_fn))
9903 return error_mark_node;
9904 *walk_subtrees = false;
9905 break;
9906
9907 case FUNCTION_DECL:
9908 case VAR_DECL:
9909 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9910 WALK_SUBTREE (DECL_TI_ARGS (t));
9911 /* Fall through. */
9912
9913 case PARM_DECL:
9914 case CONST_DECL:
9915 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9916 WALK_SUBTREE (DECL_INITIAL (t));
9917 if (DECL_CONTEXT (t)
9918 && pfd->include_nondeduced_p)
9919 WALK_SUBTREE (DECL_CONTEXT (t));
9920 break;
9921
9922 case BOUND_TEMPLATE_TEMPLATE_PARM:
9923 /* Record template parameters such as `T' inside `TT<T>'. */
9924 WALK_SUBTREE (TYPE_TI_ARGS (t));
9925 /* Fall through. */
9926
9927 case TEMPLATE_TEMPLATE_PARM:
9928 case TEMPLATE_TYPE_PARM:
9929 case TEMPLATE_PARM_INDEX:
9930 if (fn && (*fn)(t, data))
9931 return t;
9932 else if (!fn)
9933 return t;
9934 break;
9935
9936 case TEMPLATE_DECL:
9937 /* A template template parameter is encountered. */
9938 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9939 WALK_SUBTREE (TREE_TYPE (t));
9940
9941 /* Already substituted template template parameter */
9942 *walk_subtrees = 0;
9943 break;
9944
9945 case TYPENAME_TYPE:
9946 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9947 partial instantiation. */
9948 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9949 break;
9950
9951 case CONSTRUCTOR:
9952 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9953 && pfd->include_nondeduced_p)
9954 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9955 break;
9956
9957 case INDIRECT_REF:
9958 case COMPONENT_REF:
9959 /* If there's no type, then this thing must be some expression
9960 involving template parameters. */
9961 if (!fn && !TREE_TYPE (t))
9962 return error_mark_node;
9963 break;
9964
9965 case MODOP_EXPR:
9966 case CAST_EXPR:
9967 case IMPLICIT_CONV_EXPR:
9968 case REINTERPRET_CAST_EXPR:
9969 case CONST_CAST_EXPR:
9970 case STATIC_CAST_EXPR:
9971 case DYNAMIC_CAST_EXPR:
9972 case ARROW_EXPR:
9973 case DOTSTAR_EXPR:
9974 case TYPEID_EXPR:
9975 case PSEUDO_DTOR_EXPR:
9976 if (!fn)
9977 return error_mark_node;
9978 break;
9979
9980 default:
9981 break;
9982 }
9983
9984 #undef WALK_SUBTREE
9985
9986 /* We didn't find any template parameters we liked. */
9987 out:
9988 return result;
9989 }
9990
9991 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9992 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9993 call FN with the parameter and the DATA.
9994 If FN returns nonzero, the iteration is terminated, and
9995 for_each_template_parm returns 1. Otherwise, the iteration
9996 continues. If FN never returns a nonzero value, the value
9997 returned by for_each_template_parm is 0. If FN is NULL, it is
9998 considered to be the function which always returns 1.
9999
10000 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10001 parameters that occur in non-deduced contexts. When false, only
10002 visits those template parameters that can be deduced. */
10003
10004 static tree
10005 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10006 hash_set<tree> *visited,
10007 bool include_nondeduced_p,
10008 tree_fn_t any_fn)
10009 {
10010 struct pair_fn_data pfd;
10011 tree result;
10012
10013 /* Set up. */
10014 pfd.fn = fn;
10015 pfd.any_fn = any_fn;
10016 pfd.data = data;
10017 pfd.include_nondeduced_p = include_nondeduced_p;
10018
10019 /* Walk the tree. (Conceptually, we would like to walk without
10020 duplicates, but for_each_template_parm_r recursively calls
10021 for_each_template_parm, so we would need to reorganize a fair
10022 bit to use walk_tree_without_duplicates, so we keep our own
10023 visited list.) */
10024 if (visited)
10025 pfd.visited = visited;
10026 else
10027 pfd.visited = new hash_set<tree>;
10028 result = cp_walk_tree (&t,
10029 for_each_template_parm_r,
10030 &pfd,
10031 pfd.visited);
10032
10033 /* Clean up. */
10034 if (!visited)
10035 {
10036 delete pfd.visited;
10037 pfd.visited = 0;
10038 }
10039
10040 return result;
10041 }
10042
10043 /* Returns true if T depends on any template parameter. */
10044
10045 int
10046 uses_template_parms (tree t)
10047 {
10048 if (t == NULL_TREE)
10049 return false;
10050
10051 bool dependent_p;
10052 int saved_processing_template_decl;
10053
10054 saved_processing_template_decl = processing_template_decl;
10055 if (!saved_processing_template_decl)
10056 processing_template_decl = 1;
10057 if (TYPE_P (t))
10058 dependent_p = dependent_type_p (t);
10059 else if (TREE_CODE (t) == TREE_VEC)
10060 dependent_p = any_dependent_template_arguments_p (t);
10061 else if (TREE_CODE (t) == TREE_LIST)
10062 dependent_p = (uses_template_parms (TREE_VALUE (t))
10063 || uses_template_parms (TREE_CHAIN (t)));
10064 else if (TREE_CODE (t) == TYPE_DECL)
10065 dependent_p = dependent_type_p (TREE_TYPE (t));
10066 else if (DECL_P (t)
10067 || EXPR_P (t)
10068 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10069 || TREE_CODE (t) == OVERLOAD
10070 || BASELINK_P (t)
10071 || identifier_p (t)
10072 || TREE_CODE (t) == TRAIT_EXPR
10073 || TREE_CODE (t) == CONSTRUCTOR
10074 || CONSTANT_CLASS_P (t))
10075 dependent_p = (type_dependent_expression_p (t)
10076 || value_dependent_expression_p (t));
10077 else
10078 {
10079 gcc_assert (t == error_mark_node);
10080 dependent_p = false;
10081 }
10082
10083 processing_template_decl = saved_processing_template_decl;
10084
10085 return dependent_p;
10086 }
10087
10088 /* Returns true iff current_function_decl is an incompletely instantiated
10089 template. Useful instead of processing_template_decl because the latter
10090 is set to 0 during instantiate_non_dependent_expr. */
10091
10092 bool
10093 in_template_function (void)
10094 {
10095 tree fn = current_function_decl;
10096 bool ret;
10097 ++processing_template_decl;
10098 ret = (fn && DECL_LANG_SPECIFIC (fn)
10099 && DECL_TEMPLATE_INFO (fn)
10100 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10101 --processing_template_decl;
10102 return ret;
10103 }
10104
10105 /* Returns true if T depends on any template parameter with level LEVEL. */
10106
10107 bool
10108 uses_template_parms_level (tree t, int level)
10109 {
10110 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10111 /*include_nondeduced_p=*/true);
10112 }
10113
10114 /* Returns true if the signature of DECL depends on any template parameter from
10115 its enclosing class. */
10116
10117 bool
10118 uses_outer_template_parms (tree decl)
10119 {
10120 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10121 if (depth == 0)
10122 return false;
10123 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10124 &depth, NULL, /*include_nondeduced_p=*/true))
10125 return true;
10126 if (PRIMARY_TEMPLATE_P (decl)
10127 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10128 (DECL_TEMPLATE_PARMS (decl)),
10129 template_parm_outer_level,
10130 &depth, NULL, /*include_nondeduced_p=*/true))
10131 return true;
10132 tree ci = get_constraints (decl);
10133 if (ci)
10134 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10135 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10136 &depth, NULL, /*nondeduced*/true))
10137 return true;
10138 return false;
10139 }
10140
10141 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10142 ill-formed translation unit, i.e. a variable or function that isn't
10143 usable in a constant expression. */
10144
10145 static inline bool
10146 neglectable_inst_p (tree d)
10147 {
10148 return (d && DECL_P (d)
10149 && !undeduced_auto_decl (d)
10150 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10151 : decl_maybe_constant_var_p (d)));
10152 }
10153
10154 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10155 neglectable and instantiated from within an erroneous instantiation. */
10156
10157 static bool
10158 limit_bad_template_recursion (tree decl)
10159 {
10160 struct tinst_level *lev = current_tinst_level;
10161 int errs = errorcount + sorrycount;
10162 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10163 return false;
10164
10165 for (; lev; lev = lev->next)
10166 if (neglectable_inst_p (lev->maybe_get_node ()))
10167 break;
10168
10169 return (lev && errs > lev->errors);
10170 }
10171
10172 static int tinst_depth;
10173 extern int max_tinst_depth;
10174 int depth_reached;
10175
10176 static GTY(()) struct tinst_level *last_error_tinst_level;
10177
10178 /* We're starting to instantiate D; record the template instantiation context
10179 at LOC for diagnostics and to restore it later. */
10180
10181 static bool
10182 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10183 {
10184 struct tinst_level *new_level;
10185
10186 if (tinst_depth >= max_tinst_depth)
10187 {
10188 /* Tell error.c not to try to instantiate any templates. */
10189 at_eof = 2;
10190 fatal_error (input_location,
10191 "template instantiation depth exceeds maximum of %d"
10192 " (use -ftemplate-depth= to increase the maximum)",
10193 max_tinst_depth);
10194 return false;
10195 }
10196
10197 /* If the current instantiation caused problems, don't let it instantiate
10198 anything else. Do allow deduction substitution and decls usable in
10199 constant expressions. */
10200 if (!targs && limit_bad_template_recursion (tldcl))
10201 return false;
10202
10203 /* When not -quiet, dump template instantiations other than functions, since
10204 announce_function will take care of those. */
10205 if (!quiet_flag && !targs
10206 && TREE_CODE (tldcl) != TREE_LIST
10207 && TREE_CODE (tldcl) != FUNCTION_DECL)
10208 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10209
10210 new_level = tinst_level_freelist ().alloc ();
10211 new_level->tldcl = tldcl;
10212 new_level->targs = targs;
10213 new_level->locus = loc;
10214 new_level->errors = errorcount + sorrycount;
10215 new_level->next = NULL;
10216 new_level->refcount = 0;
10217 set_refcount_ptr (new_level->next, current_tinst_level);
10218 set_refcount_ptr (current_tinst_level, new_level);
10219
10220 ++tinst_depth;
10221 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10222 depth_reached = tinst_depth;
10223
10224 return true;
10225 }
10226
10227 /* We're starting substitution of TMPL<ARGS>; record the template
10228 substitution context for diagnostics and to restore it later. */
10229
10230 static bool
10231 push_tinst_level (tree tmpl, tree args)
10232 {
10233 return push_tinst_level_loc (tmpl, args, input_location);
10234 }
10235
10236 /* We're starting to instantiate D; record INPUT_LOCATION and the
10237 template instantiation context for diagnostics and to restore it
10238 later. */
10239
10240 bool
10241 push_tinst_level (tree d)
10242 {
10243 return push_tinst_level_loc (d, input_location);
10244 }
10245
10246 /* Likewise, but record LOC as the program location. */
10247
10248 bool
10249 push_tinst_level_loc (tree d, location_t loc)
10250 {
10251 gcc_assert (TREE_CODE (d) != TREE_LIST);
10252 return push_tinst_level_loc (d, NULL, loc);
10253 }
10254
10255 /* We're done instantiating this template; return to the instantiation
10256 context. */
10257
10258 void
10259 pop_tinst_level (void)
10260 {
10261 /* Restore the filename and line number stashed away when we started
10262 this instantiation. */
10263 input_location = current_tinst_level->locus;
10264 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10265 --tinst_depth;
10266 }
10267
10268 /* We're instantiating a deferred template; restore the template
10269 instantiation context in which the instantiation was requested, which
10270 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10271
10272 static tree
10273 reopen_tinst_level (struct tinst_level *level)
10274 {
10275 struct tinst_level *t;
10276
10277 tinst_depth = 0;
10278 for (t = level; t; t = t->next)
10279 ++tinst_depth;
10280
10281 set_refcount_ptr (current_tinst_level, level);
10282 pop_tinst_level ();
10283 if (current_tinst_level)
10284 current_tinst_level->errors = errorcount+sorrycount;
10285 return level->maybe_get_node ();
10286 }
10287
10288 /* Returns the TINST_LEVEL which gives the original instantiation
10289 context. */
10290
10291 struct tinst_level *
10292 outermost_tinst_level (void)
10293 {
10294 struct tinst_level *level = current_tinst_level;
10295 if (level)
10296 while (level->next)
10297 level = level->next;
10298 return level;
10299 }
10300
10301 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10302 vector of template arguments, as for tsubst.
10303
10304 Returns an appropriate tsubst'd friend declaration. */
10305
10306 static tree
10307 tsubst_friend_function (tree decl, tree args)
10308 {
10309 tree new_friend;
10310
10311 if (TREE_CODE (decl) == FUNCTION_DECL
10312 && DECL_TEMPLATE_INSTANTIATION (decl)
10313 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10314 /* This was a friend declared with an explicit template
10315 argument list, e.g.:
10316
10317 friend void f<>(T);
10318
10319 to indicate that f was a template instantiation, not a new
10320 function declaration. Now, we have to figure out what
10321 instantiation of what template. */
10322 {
10323 tree template_id, arglist, fns;
10324 tree new_args;
10325 tree tmpl;
10326 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10327
10328 /* Friend functions are looked up in the containing namespace scope.
10329 We must enter that scope, to avoid finding member functions of the
10330 current class with same name. */
10331 push_nested_namespace (ns);
10332 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10333 tf_warning_or_error, NULL_TREE,
10334 /*integral_constant_expression_p=*/false);
10335 pop_nested_namespace (ns);
10336 arglist = tsubst (DECL_TI_ARGS (decl), args,
10337 tf_warning_or_error, NULL_TREE);
10338 template_id = lookup_template_function (fns, arglist);
10339
10340 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10341 tmpl = determine_specialization (template_id, new_friend,
10342 &new_args,
10343 /*need_member_template=*/0,
10344 TREE_VEC_LENGTH (args),
10345 tsk_none);
10346 return instantiate_template (tmpl, new_args, tf_error);
10347 }
10348
10349 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10350
10351 /* The NEW_FRIEND will look like an instantiation, to the
10352 compiler, but is not an instantiation from the point of view of
10353 the language. For example, we might have had:
10354
10355 template <class T> struct S {
10356 template <class U> friend void f(T, U);
10357 };
10358
10359 Then, in S<int>, template <class U> void f(int, U) is not an
10360 instantiation of anything. */
10361 if (new_friend == error_mark_node)
10362 return error_mark_node;
10363
10364 DECL_USE_TEMPLATE (new_friend) = 0;
10365 if (TREE_CODE (decl) == TEMPLATE_DECL)
10366 {
10367 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10368 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10369 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10370 }
10371
10372 /* The mangled name for the NEW_FRIEND is incorrect. The function
10373 is not a template instantiation and should not be mangled like
10374 one. Therefore, we forget the mangling here; we'll recompute it
10375 later if we need it. */
10376 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10377 {
10378 SET_DECL_RTL (new_friend, NULL);
10379 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10380 }
10381
10382 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10383 {
10384 tree old_decl;
10385 tree new_friend_template_info;
10386 tree new_friend_result_template_info;
10387 tree ns;
10388 int new_friend_is_defn;
10389
10390 /* We must save some information from NEW_FRIEND before calling
10391 duplicate decls since that function will free NEW_FRIEND if
10392 possible. */
10393 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10394 new_friend_is_defn =
10395 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10396 (template_for_substitution (new_friend)))
10397 != NULL_TREE);
10398 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10399 {
10400 /* This declaration is a `primary' template. */
10401 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10402
10403 new_friend_result_template_info
10404 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10405 }
10406 else
10407 new_friend_result_template_info = NULL_TREE;
10408
10409 /* Inside pushdecl_namespace_level, we will push into the
10410 current namespace. However, the friend function should go
10411 into the namespace of the template. */
10412 ns = decl_namespace_context (new_friend);
10413 push_nested_namespace (ns);
10414 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10415 pop_nested_namespace (ns);
10416
10417 if (old_decl == error_mark_node)
10418 return error_mark_node;
10419
10420 if (old_decl != new_friend)
10421 {
10422 /* This new friend declaration matched an existing
10423 declaration. For example, given:
10424
10425 template <class T> void f(T);
10426 template <class U> class C {
10427 template <class T> friend void f(T) {}
10428 };
10429
10430 the friend declaration actually provides the definition
10431 of `f', once C has been instantiated for some type. So,
10432 old_decl will be the out-of-class template declaration,
10433 while new_friend is the in-class definition.
10434
10435 But, if `f' was called before this point, the
10436 instantiation of `f' will have DECL_TI_ARGS corresponding
10437 to `T' but not to `U', references to which might appear
10438 in the definition of `f'. Previously, the most general
10439 template for an instantiation of `f' was the out-of-class
10440 version; now it is the in-class version. Therefore, we
10441 run through all specialization of `f', adding to their
10442 DECL_TI_ARGS appropriately. In particular, they need a
10443 new set of outer arguments, corresponding to the
10444 arguments for this class instantiation.
10445
10446 The same situation can arise with something like this:
10447
10448 friend void f(int);
10449 template <class T> class C {
10450 friend void f(T) {}
10451 };
10452
10453 when `C<int>' is instantiated. Now, `f(int)' is defined
10454 in the class. */
10455
10456 if (!new_friend_is_defn)
10457 /* On the other hand, if the in-class declaration does
10458 *not* provide a definition, then we don't want to alter
10459 existing definitions. We can just leave everything
10460 alone. */
10461 ;
10462 else
10463 {
10464 tree new_template = TI_TEMPLATE (new_friend_template_info);
10465 tree new_args = TI_ARGS (new_friend_template_info);
10466
10467 /* Overwrite whatever template info was there before, if
10468 any, with the new template information pertaining to
10469 the declaration. */
10470 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10471
10472 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10473 {
10474 /* We should have called reregister_specialization in
10475 duplicate_decls. */
10476 gcc_assert (retrieve_specialization (new_template,
10477 new_args, 0)
10478 == old_decl);
10479
10480 /* Instantiate it if the global has already been used. */
10481 if (DECL_ODR_USED (old_decl))
10482 instantiate_decl (old_decl, /*defer_ok=*/true,
10483 /*expl_inst_class_mem_p=*/false);
10484 }
10485 else
10486 {
10487 tree t;
10488
10489 /* Indicate that the old function template is a partial
10490 instantiation. */
10491 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10492 = new_friend_result_template_info;
10493
10494 gcc_assert (new_template
10495 == most_general_template (new_template));
10496 gcc_assert (new_template != old_decl);
10497
10498 /* Reassign any specializations already in the hash table
10499 to the new more general template, and add the
10500 additional template args. */
10501 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10502 t != NULL_TREE;
10503 t = TREE_CHAIN (t))
10504 {
10505 tree spec = TREE_VALUE (t);
10506 spec_entry elt;
10507
10508 elt.tmpl = old_decl;
10509 elt.args = DECL_TI_ARGS (spec);
10510 elt.spec = NULL_TREE;
10511
10512 decl_specializations->remove_elt (&elt);
10513
10514 DECL_TI_ARGS (spec)
10515 = add_outermost_template_args (new_args,
10516 DECL_TI_ARGS (spec));
10517
10518 register_specialization
10519 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10520
10521 }
10522 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10523 }
10524 }
10525
10526 /* The information from NEW_FRIEND has been merged into OLD_DECL
10527 by duplicate_decls. */
10528 new_friend = old_decl;
10529 }
10530 }
10531 else
10532 {
10533 tree context = DECL_CONTEXT (new_friend);
10534 bool dependent_p;
10535
10536 /* In the code
10537 template <class T> class C {
10538 template <class U> friend void C1<U>::f (); // case 1
10539 friend void C2<T>::f (); // case 2
10540 };
10541 we only need to make sure CONTEXT is a complete type for
10542 case 2. To distinguish between the two cases, we note that
10543 CONTEXT of case 1 remains dependent type after tsubst while
10544 this isn't true for case 2. */
10545 ++processing_template_decl;
10546 dependent_p = dependent_type_p (context);
10547 --processing_template_decl;
10548
10549 if (!dependent_p
10550 && !complete_type_or_else (context, NULL_TREE))
10551 return error_mark_node;
10552
10553 if (COMPLETE_TYPE_P (context))
10554 {
10555 tree fn = new_friend;
10556 /* do_friend adds the TEMPLATE_DECL for any member friend
10557 template even if it isn't a member template, i.e.
10558 template <class T> friend A<T>::f();
10559 Look through it in that case. */
10560 if (TREE_CODE (fn) == TEMPLATE_DECL
10561 && !PRIMARY_TEMPLATE_P (fn))
10562 fn = DECL_TEMPLATE_RESULT (fn);
10563 /* Check to see that the declaration is really present, and,
10564 possibly obtain an improved declaration. */
10565 fn = check_classfn (context, fn, NULL_TREE);
10566
10567 if (fn)
10568 new_friend = fn;
10569 }
10570 }
10571
10572 return new_friend;
10573 }
10574
10575 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10576 template arguments, as for tsubst.
10577
10578 Returns an appropriate tsubst'd friend type or error_mark_node on
10579 failure. */
10580
10581 static tree
10582 tsubst_friend_class (tree friend_tmpl, tree args)
10583 {
10584 tree tmpl;
10585
10586 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10587 {
10588 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10589 return TREE_TYPE (tmpl);
10590 }
10591
10592 tree context = CP_DECL_CONTEXT (friend_tmpl);
10593 if (TREE_CODE (context) == NAMESPACE_DECL)
10594 push_nested_namespace (context);
10595 else
10596 {
10597 context = tsubst (context, args, tf_error, NULL_TREE);
10598 push_nested_class (context);
10599 }
10600
10601 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10602 /*non_class=*/false, /*block_p=*/false,
10603 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10604
10605 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10606 {
10607 /* The friend template has already been declared. Just
10608 check to see that the declarations match, and install any new
10609 default parameters. We must tsubst the default parameters,
10610 of course. We only need the innermost template parameters
10611 because that is all that redeclare_class_template will look
10612 at. */
10613 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10614 > TMPL_ARGS_DEPTH (args))
10615 {
10616 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10617 args, tf_warning_or_error);
10618 location_t saved_input_location = input_location;
10619 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10620 tree cons = get_constraints (tmpl);
10621 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10622 input_location = saved_input_location;
10623 }
10624 }
10625 else
10626 {
10627 /* The friend template has not already been declared. In this
10628 case, the instantiation of the template class will cause the
10629 injection of this template into the namespace scope. */
10630 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10631
10632 if (tmpl != error_mark_node)
10633 {
10634 /* The new TMPL is not an instantiation of anything, so we
10635 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10636 for the new type because that is supposed to be the
10637 corresponding template decl, i.e., TMPL. */
10638 DECL_USE_TEMPLATE (tmpl) = 0;
10639 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10640 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10641 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10642 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10643
10644 /* It is hidden. */
10645 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10646 DECL_ANTICIPATED (tmpl)
10647 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10648
10649 /* Inject this template into the enclosing namspace scope. */
10650 tmpl = pushdecl_namespace_level (tmpl, true);
10651 }
10652 }
10653
10654 if (TREE_CODE (context) == NAMESPACE_DECL)
10655 pop_nested_namespace (context);
10656 else
10657 pop_nested_class ();
10658
10659 return TREE_TYPE (tmpl);
10660 }
10661
10662 /* Returns zero if TYPE cannot be completed later due to circularity.
10663 Otherwise returns one. */
10664
10665 static int
10666 can_complete_type_without_circularity (tree type)
10667 {
10668 if (type == NULL_TREE || type == error_mark_node)
10669 return 0;
10670 else if (COMPLETE_TYPE_P (type))
10671 return 1;
10672 else if (TREE_CODE (type) == ARRAY_TYPE)
10673 return can_complete_type_without_circularity (TREE_TYPE (type));
10674 else if (CLASS_TYPE_P (type)
10675 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10676 return 0;
10677 else
10678 return 1;
10679 }
10680
10681 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10682 tsubst_flags_t, tree);
10683
10684 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10685 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10686
10687 static tree
10688 tsubst_attribute (tree t, tree *decl_p, tree args,
10689 tsubst_flags_t complain, tree in_decl)
10690 {
10691 gcc_assert (ATTR_IS_DEPENDENT (t));
10692
10693 tree val = TREE_VALUE (t);
10694 if (val == NULL_TREE)
10695 /* Nothing to do. */;
10696 else if ((flag_openmp || flag_openmp_simd)
10697 && is_attribute_p ("omp declare simd",
10698 get_attribute_name (t)))
10699 {
10700 tree clauses = TREE_VALUE (val);
10701 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10702 complain, in_decl);
10703 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10704 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10705 tree parms = DECL_ARGUMENTS (*decl_p);
10706 clauses
10707 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10708 if (clauses)
10709 val = build_tree_list (NULL_TREE, clauses);
10710 else
10711 val = NULL_TREE;
10712 }
10713 /* If the first attribute argument is an identifier, don't
10714 pass it through tsubst. Attributes like mode, format,
10715 cleanup and several target specific attributes expect it
10716 unmodified. */
10717 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10718 {
10719 tree chain
10720 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10721 /*integral_constant_expression_p=*/false);
10722 if (chain != TREE_CHAIN (val))
10723 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10724 }
10725 else if (PACK_EXPANSION_P (val))
10726 {
10727 /* An attribute pack expansion. */
10728 tree purp = TREE_PURPOSE (t);
10729 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10730 if (pack == error_mark_node)
10731 return error_mark_node;
10732 int len = TREE_VEC_LENGTH (pack);
10733 tree list = NULL_TREE;
10734 tree *q = &list;
10735 for (int i = 0; i < len; ++i)
10736 {
10737 tree elt = TREE_VEC_ELT (pack, i);
10738 *q = build_tree_list (purp, elt);
10739 q = &TREE_CHAIN (*q);
10740 }
10741 return list;
10742 }
10743 else
10744 val = tsubst_expr (val, args, complain, in_decl,
10745 /*integral_constant_expression_p=*/false);
10746
10747 if (val != TREE_VALUE (t))
10748 return build_tree_list (TREE_PURPOSE (t), val);
10749 return t;
10750 }
10751
10752 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10753 unchanged or a new TREE_LIST chain. */
10754
10755 static tree
10756 tsubst_attributes (tree attributes, tree args,
10757 tsubst_flags_t complain, tree in_decl)
10758 {
10759 tree last_dep = NULL_TREE;
10760
10761 for (tree t = attributes; t; t = TREE_CHAIN (t))
10762 if (ATTR_IS_DEPENDENT (t))
10763 {
10764 last_dep = t;
10765 attributes = copy_list (attributes);
10766 break;
10767 }
10768
10769 if (last_dep)
10770 for (tree *p = &attributes; *p; )
10771 {
10772 tree t = *p;
10773 if (ATTR_IS_DEPENDENT (t))
10774 {
10775 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10776 if (subst != t)
10777 {
10778 *p = subst;
10779 while (*p)
10780 p = &TREE_CHAIN (*p);
10781 *p = TREE_CHAIN (t);
10782 continue;
10783 }
10784 }
10785 p = &TREE_CHAIN (*p);
10786 }
10787
10788 return attributes;
10789 }
10790
10791 /* Apply any attributes which had to be deferred until instantiation
10792 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10793 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10794
10795 static void
10796 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10797 tree args, tsubst_flags_t complain, tree in_decl)
10798 {
10799 tree last_dep = NULL_TREE;
10800 tree t;
10801 tree *p;
10802
10803 if (attributes == NULL_TREE)
10804 return;
10805
10806 if (DECL_P (*decl_p))
10807 {
10808 if (TREE_TYPE (*decl_p) == error_mark_node)
10809 return;
10810 p = &DECL_ATTRIBUTES (*decl_p);
10811 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10812 to our attributes parameter. */
10813 gcc_assert (*p == attributes);
10814 }
10815 else
10816 {
10817 p = &TYPE_ATTRIBUTES (*decl_p);
10818 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10819 lookup_template_class_1, and should be preserved. */
10820 gcc_assert (*p != attributes);
10821 while (*p)
10822 p = &TREE_CHAIN (*p);
10823 }
10824
10825 for (t = attributes; t; t = TREE_CHAIN (t))
10826 if (ATTR_IS_DEPENDENT (t))
10827 {
10828 last_dep = t;
10829 attributes = copy_list (attributes);
10830 break;
10831 }
10832
10833 *p = attributes;
10834 if (last_dep)
10835 {
10836 tree late_attrs = NULL_TREE;
10837 tree *q = &late_attrs;
10838
10839 for (; *p; )
10840 {
10841 t = *p;
10842 if (ATTR_IS_DEPENDENT (t))
10843 {
10844 *p = TREE_CHAIN (t);
10845 TREE_CHAIN (t) = NULL_TREE;
10846 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10847 while (*q)
10848 q = &TREE_CHAIN (*q);
10849 }
10850 else
10851 p = &TREE_CHAIN (t);
10852 }
10853
10854 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10855 }
10856 }
10857
10858 /* Perform (or defer) access check for typedefs that were referenced
10859 from within the template TMPL code.
10860 This is a subroutine of instantiate_decl and instantiate_class_template.
10861 TMPL is the template to consider and TARGS is the list of arguments of
10862 that template. */
10863
10864 static void
10865 perform_typedefs_access_check (tree tmpl, tree targs)
10866 {
10867 location_t saved_location;
10868 unsigned i;
10869 qualified_typedef_usage_t *iter;
10870
10871 if (!tmpl
10872 || (!CLASS_TYPE_P (tmpl)
10873 && TREE_CODE (tmpl) != FUNCTION_DECL))
10874 return;
10875
10876 saved_location = input_location;
10877 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10878 {
10879 tree type_decl = iter->typedef_decl;
10880 tree type_scope = iter->context;
10881
10882 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10883 continue;
10884
10885 if (uses_template_parms (type_decl))
10886 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10887 if (uses_template_parms (type_scope))
10888 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10889
10890 /* Make access check error messages point to the location
10891 of the use of the typedef. */
10892 input_location = iter->locus;
10893 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10894 type_decl, type_decl,
10895 tf_warning_or_error);
10896 }
10897 input_location = saved_location;
10898 }
10899
10900 static tree
10901 instantiate_class_template_1 (tree type)
10902 {
10903 tree templ, args, pattern, t, member;
10904 tree typedecl;
10905 tree pbinfo;
10906 tree base_list;
10907 unsigned int saved_maximum_field_alignment;
10908 tree fn_context;
10909
10910 if (type == error_mark_node)
10911 return error_mark_node;
10912
10913 if (COMPLETE_OR_OPEN_TYPE_P (type)
10914 || uses_template_parms (type))
10915 return type;
10916
10917 /* Figure out which template is being instantiated. */
10918 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10919 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10920
10921 /* Mark the type as in the process of being defined. */
10922 TYPE_BEING_DEFINED (type) = 1;
10923
10924 /* We may be in the middle of deferred access check. Disable
10925 it now. */
10926 deferring_access_check_sentinel acs (dk_no_deferred);
10927
10928 /* Determine what specialization of the original template to
10929 instantiate. */
10930 t = most_specialized_partial_spec (type, tf_warning_or_error);
10931 if (t == error_mark_node)
10932 return error_mark_node;
10933 else if (t)
10934 {
10935 /* This TYPE is actually an instantiation of a partial
10936 specialization. We replace the innermost set of ARGS with
10937 the arguments appropriate for substitution. For example,
10938 given:
10939
10940 template <class T> struct S {};
10941 template <class T> struct S<T*> {};
10942
10943 and supposing that we are instantiating S<int*>, ARGS will
10944 presently be {int*} -- but we need {int}. */
10945 pattern = TREE_TYPE (t);
10946 args = TREE_PURPOSE (t);
10947 }
10948 else
10949 {
10950 pattern = TREE_TYPE (templ);
10951 args = CLASSTYPE_TI_ARGS (type);
10952 }
10953
10954 /* If the template we're instantiating is incomplete, then clearly
10955 there's nothing we can do. */
10956 if (!COMPLETE_TYPE_P (pattern))
10957 {
10958 /* We can try again later. */
10959 TYPE_BEING_DEFINED (type) = 0;
10960 return type;
10961 }
10962
10963 /* If we've recursively instantiated too many templates, stop. */
10964 if (! push_tinst_level (type))
10965 return type;
10966
10967 int saved_unevaluated_operand = cp_unevaluated_operand;
10968 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10969
10970 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10971 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10972 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10973 fn_context = error_mark_node;
10974 if (!fn_context)
10975 push_to_top_level ();
10976 else
10977 {
10978 cp_unevaluated_operand = 0;
10979 c_inhibit_evaluation_warnings = 0;
10980 }
10981 /* Use #pragma pack from the template context. */
10982 saved_maximum_field_alignment = maximum_field_alignment;
10983 maximum_field_alignment = TYPE_PRECISION (pattern);
10984
10985 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10986
10987 /* Set the input location to the most specialized template definition.
10988 This is needed if tsubsting causes an error. */
10989 typedecl = TYPE_MAIN_DECL (pattern);
10990 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10991 DECL_SOURCE_LOCATION (typedecl);
10992
10993 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10994 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10995 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10996 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10997 if (ANON_AGGR_TYPE_P (pattern))
10998 SET_ANON_AGGR_TYPE_P (type);
10999 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11000 {
11001 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11002 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11003 /* Adjust visibility for template arguments. */
11004 determine_visibility (TYPE_MAIN_DECL (type));
11005 }
11006 if (CLASS_TYPE_P (type))
11007 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11008
11009 pbinfo = TYPE_BINFO (pattern);
11010
11011 /* We should never instantiate a nested class before its enclosing
11012 class; we need to look up the nested class by name before we can
11013 instantiate it, and that lookup should instantiate the enclosing
11014 class. */
11015 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11016 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11017
11018 base_list = NULL_TREE;
11019 if (BINFO_N_BASE_BINFOS (pbinfo))
11020 {
11021 tree pbase_binfo;
11022 tree pushed_scope;
11023 int i;
11024
11025 /* We must enter the scope containing the type, as that is where
11026 the accessibility of types named in dependent bases are
11027 looked up from. */
11028 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11029
11030 /* Substitute into each of the bases to determine the actual
11031 basetypes. */
11032 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11033 {
11034 tree base;
11035 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11036 tree expanded_bases = NULL_TREE;
11037 int idx, len = 1;
11038
11039 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11040 {
11041 expanded_bases =
11042 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11043 args, tf_error, NULL_TREE);
11044 if (expanded_bases == error_mark_node)
11045 continue;
11046
11047 len = TREE_VEC_LENGTH (expanded_bases);
11048 }
11049
11050 for (idx = 0; idx < len; idx++)
11051 {
11052 if (expanded_bases)
11053 /* Extract the already-expanded base class. */
11054 base = TREE_VEC_ELT (expanded_bases, idx);
11055 else
11056 /* Substitute to figure out the base class. */
11057 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11058 NULL_TREE);
11059
11060 if (base == error_mark_node)
11061 continue;
11062
11063 base_list = tree_cons (access, base, base_list);
11064 if (BINFO_VIRTUAL_P (pbase_binfo))
11065 TREE_TYPE (base_list) = integer_type_node;
11066 }
11067 }
11068
11069 /* The list is now in reverse order; correct that. */
11070 base_list = nreverse (base_list);
11071
11072 if (pushed_scope)
11073 pop_scope (pushed_scope);
11074 }
11075 /* Now call xref_basetypes to set up all the base-class
11076 information. */
11077 xref_basetypes (type, base_list);
11078
11079 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11080 (int) ATTR_FLAG_TYPE_IN_PLACE,
11081 args, tf_error, NULL_TREE);
11082 fixup_attribute_variants (type);
11083
11084 /* Now that our base classes are set up, enter the scope of the
11085 class, so that name lookups into base classes, etc. will work
11086 correctly. This is precisely analogous to what we do in
11087 begin_class_definition when defining an ordinary non-template
11088 class, except we also need to push the enclosing classes. */
11089 push_nested_class (type);
11090
11091 /* Now members are processed in the order of declaration. */
11092 for (member = CLASSTYPE_DECL_LIST (pattern);
11093 member; member = TREE_CHAIN (member))
11094 {
11095 tree t = TREE_VALUE (member);
11096
11097 if (TREE_PURPOSE (member))
11098 {
11099 if (TYPE_P (t))
11100 {
11101 if (LAMBDA_TYPE_P (t))
11102 /* A closure type for a lambda in an NSDMI or default argument.
11103 Ignore it; it will be regenerated when needed. */
11104 continue;
11105
11106 /* Build new CLASSTYPE_NESTED_UTDS. */
11107
11108 tree newtag;
11109 bool class_template_p;
11110
11111 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11112 && TYPE_LANG_SPECIFIC (t)
11113 && CLASSTYPE_IS_TEMPLATE (t));
11114 /* If the member is a class template, then -- even after
11115 substitution -- there may be dependent types in the
11116 template argument list for the class. We increment
11117 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11118 that function will assume that no types are dependent
11119 when outside of a template. */
11120 if (class_template_p)
11121 ++processing_template_decl;
11122 newtag = tsubst (t, args, tf_error, NULL_TREE);
11123 if (class_template_p)
11124 --processing_template_decl;
11125 if (newtag == error_mark_node)
11126 continue;
11127
11128 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11129 {
11130 tree name = TYPE_IDENTIFIER (t);
11131
11132 if (class_template_p)
11133 /* Unfortunately, lookup_template_class sets
11134 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11135 instantiation (i.e., for the type of a member
11136 template class nested within a template class.)
11137 This behavior is required for
11138 maybe_process_partial_specialization to work
11139 correctly, but is not accurate in this case;
11140 the TAG is not an instantiation of anything.
11141 (The corresponding TEMPLATE_DECL is an
11142 instantiation, but the TYPE is not.) */
11143 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11144
11145 /* Now, we call pushtag to put this NEWTAG into the scope of
11146 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11147 pushtag calling push_template_decl. We don't have to do
11148 this for enums because it will already have been done in
11149 tsubst_enum. */
11150 if (name)
11151 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11152 pushtag (name, newtag, /*tag_scope=*/ts_current);
11153 }
11154 }
11155 else if (DECL_DECLARES_FUNCTION_P (t))
11156 {
11157 tree r;
11158
11159 if (TREE_CODE (t) == TEMPLATE_DECL)
11160 ++processing_template_decl;
11161 r = tsubst (t, args, tf_error, NULL_TREE);
11162 if (TREE_CODE (t) == TEMPLATE_DECL)
11163 --processing_template_decl;
11164 set_current_access_from_decl (r);
11165 finish_member_declaration (r);
11166 /* Instantiate members marked with attribute used. */
11167 if (r != error_mark_node && DECL_PRESERVE_P (r))
11168 mark_used (r);
11169 if (TREE_CODE (r) == FUNCTION_DECL
11170 && DECL_OMP_DECLARE_REDUCTION_P (r))
11171 cp_check_omp_declare_reduction (r);
11172 }
11173 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11174 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11175 /* A closure type for a lambda in an NSDMI or default argument.
11176 Ignore it; it will be regenerated when needed. */;
11177 else
11178 {
11179 /* Build new TYPE_FIELDS. */
11180 if (TREE_CODE (t) == STATIC_ASSERT)
11181 {
11182 tree condition;
11183
11184 ++c_inhibit_evaluation_warnings;
11185 condition =
11186 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11187 tf_warning_or_error, NULL_TREE,
11188 /*integral_constant_expression_p=*/true);
11189 --c_inhibit_evaluation_warnings;
11190
11191 finish_static_assert (condition,
11192 STATIC_ASSERT_MESSAGE (t),
11193 STATIC_ASSERT_SOURCE_LOCATION (t),
11194 /*member_p=*/true);
11195 }
11196 else if (TREE_CODE (t) != CONST_DECL)
11197 {
11198 tree r;
11199 tree vec = NULL_TREE;
11200 int len = 1;
11201
11202 /* The file and line for this declaration, to
11203 assist in error message reporting. Since we
11204 called push_tinst_level above, we don't need to
11205 restore these. */
11206 input_location = DECL_SOURCE_LOCATION (t);
11207
11208 if (TREE_CODE (t) == TEMPLATE_DECL)
11209 ++processing_template_decl;
11210 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11211 if (TREE_CODE (t) == TEMPLATE_DECL)
11212 --processing_template_decl;
11213
11214 if (TREE_CODE (r) == TREE_VEC)
11215 {
11216 /* A capture pack became multiple fields. */
11217 vec = r;
11218 len = TREE_VEC_LENGTH (vec);
11219 }
11220
11221 for (int i = 0; i < len; ++i)
11222 {
11223 if (vec)
11224 r = TREE_VEC_ELT (vec, i);
11225 if (VAR_P (r))
11226 {
11227 /* In [temp.inst]:
11228
11229 [t]he initialization (and any associated
11230 side-effects) of a static data member does
11231 not occur unless the static data member is
11232 itself used in a way that requires the
11233 definition of the static data member to
11234 exist.
11235
11236 Therefore, we do not substitute into the
11237 initialized for the static data member here. */
11238 finish_static_data_member_decl
11239 (r,
11240 /*init=*/NULL_TREE,
11241 /*init_const_expr_p=*/false,
11242 /*asmspec_tree=*/NULL_TREE,
11243 /*flags=*/0);
11244 /* Instantiate members marked with attribute used. */
11245 if (r != error_mark_node && DECL_PRESERVE_P (r))
11246 mark_used (r);
11247 }
11248 else if (TREE_CODE (r) == FIELD_DECL)
11249 {
11250 /* Determine whether R has a valid type and can be
11251 completed later. If R is invalid, then its type
11252 is replaced by error_mark_node. */
11253 tree rtype = TREE_TYPE (r);
11254 if (can_complete_type_without_circularity (rtype))
11255 complete_type (rtype);
11256
11257 if (!complete_or_array_type_p (rtype))
11258 {
11259 /* If R's type couldn't be completed and
11260 it isn't a flexible array member (whose
11261 type is incomplete by definition) give
11262 an error. */
11263 cxx_incomplete_type_error (r, rtype);
11264 TREE_TYPE (r) = error_mark_node;
11265 }
11266 else if (TREE_CODE (rtype) == ARRAY_TYPE
11267 && TYPE_DOMAIN (rtype) == NULL_TREE
11268 && (TREE_CODE (type) == UNION_TYPE
11269 || TREE_CODE (type) == QUAL_UNION_TYPE))
11270 {
11271 error ("flexible array member %qD in union", r);
11272 TREE_TYPE (r) = error_mark_node;
11273 }
11274 }
11275
11276 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11277 such a thing will already have been added to the field
11278 list by tsubst_enum in finish_member_declaration in the
11279 CLASSTYPE_NESTED_UTDS case above. */
11280 if (!(TREE_CODE (r) == TYPE_DECL
11281 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11282 && DECL_ARTIFICIAL (r)))
11283 {
11284 set_current_access_from_decl (r);
11285 finish_member_declaration (r);
11286 }
11287 }
11288 }
11289 }
11290 }
11291 else
11292 {
11293 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11294 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11295 {
11296 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11297
11298 tree friend_type = t;
11299 bool adjust_processing_template_decl = false;
11300
11301 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11302 {
11303 /* template <class T> friend class C; */
11304 friend_type = tsubst_friend_class (friend_type, args);
11305 adjust_processing_template_decl = true;
11306 }
11307 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11308 {
11309 /* template <class T> friend class C::D; */
11310 friend_type = tsubst (friend_type, args,
11311 tf_warning_or_error, NULL_TREE);
11312 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11313 friend_type = TREE_TYPE (friend_type);
11314 adjust_processing_template_decl = true;
11315 }
11316 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11317 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11318 {
11319 /* This could be either
11320
11321 friend class T::C;
11322
11323 when dependent_type_p is false or
11324
11325 template <class U> friend class T::C;
11326
11327 otherwise. */
11328 /* Bump processing_template_decl in case this is something like
11329 template <class T> friend struct A<T>::B. */
11330 ++processing_template_decl;
11331 friend_type = tsubst (friend_type, args,
11332 tf_warning_or_error, NULL_TREE);
11333 if (dependent_type_p (friend_type))
11334 adjust_processing_template_decl = true;
11335 --processing_template_decl;
11336 }
11337 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11338 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11339 && TYPE_HIDDEN_P (friend_type))
11340 {
11341 /* friend class C;
11342
11343 where C hasn't been declared yet. Let's lookup name
11344 from namespace scope directly, bypassing any name that
11345 come from dependent base class. */
11346 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11347
11348 /* The call to xref_tag_from_type does injection for friend
11349 classes. */
11350 push_nested_namespace (ns);
11351 friend_type =
11352 xref_tag_from_type (friend_type, NULL_TREE,
11353 /*tag_scope=*/ts_current);
11354 pop_nested_namespace (ns);
11355 }
11356 else if (uses_template_parms (friend_type))
11357 /* friend class C<T>; */
11358 friend_type = tsubst (friend_type, args,
11359 tf_warning_or_error, NULL_TREE);
11360 /* Otherwise it's
11361
11362 friend class C;
11363
11364 where C is already declared or
11365
11366 friend class C<int>;
11367
11368 We don't have to do anything in these cases. */
11369
11370 if (adjust_processing_template_decl)
11371 /* Trick make_friend_class into realizing that the friend
11372 we're adding is a template, not an ordinary class. It's
11373 important that we use make_friend_class since it will
11374 perform some error-checking and output cross-reference
11375 information. */
11376 ++processing_template_decl;
11377
11378 if (friend_type != error_mark_node)
11379 make_friend_class (type, friend_type, /*complain=*/false);
11380
11381 if (adjust_processing_template_decl)
11382 --processing_template_decl;
11383 }
11384 else
11385 {
11386 /* Build new DECL_FRIENDLIST. */
11387 tree r;
11388
11389 /* The file and line for this declaration, to
11390 assist in error message reporting. Since we
11391 called push_tinst_level above, we don't need to
11392 restore these. */
11393 input_location = DECL_SOURCE_LOCATION (t);
11394
11395 if (TREE_CODE (t) == TEMPLATE_DECL)
11396 {
11397 ++processing_template_decl;
11398 push_deferring_access_checks (dk_no_check);
11399 }
11400
11401 r = tsubst_friend_function (t, args);
11402 add_friend (type, r, /*complain=*/false);
11403 if (TREE_CODE (t) == TEMPLATE_DECL)
11404 {
11405 pop_deferring_access_checks ();
11406 --processing_template_decl;
11407 }
11408 }
11409 }
11410 }
11411
11412 if (fn_context)
11413 {
11414 /* Restore these before substituting into the lambda capture
11415 initializers. */
11416 cp_unevaluated_operand = saved_unevaluated_operand;
11417 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11418 }
11419
11420 /* Set the file and line number information to whatever is given for
11421 the class itself. This puts error messages involving generated
11422 implicit functions at a predictable point, and the same point
11423 that would be used for non-template classes. */
11424 input_location = DECL_SOURCE_LOCATION (typedecl);
11425
11426 unreverse_member_declarations (type);
11427 finish_struct_1 (type);
11428 TYPE_BEING_DEFINED (type) = 0;
11429
11430 /* We don't instantiate default arguments for member functions. 14.7.1:
11431
11432 The implicit instantiation of a class template specialization causes
11433 the implicit instantiation of the declarations, but not of the
11434 definitions or default arguments, of the class member functions,
11435 member classes, static data members and member templates.... */
11436
11437 /* Some typedefs referenced from within the template code need to be access
11438 checked at template instantiation time, i.e now. These types were
11439 added to the template at parsing time. Let's get those and perform
11440 the access checks then. */
11441 perform_typedefs_access_check (pattern, args);
11442 perform_deferred_access_checks (tf_warning_or_error);
11443 pop_nested_class ();
11444 maximum_field_alignment = saved_maximum_field_alignment;
11445 if (!fn_context)
11446 pop_from_top_level ();
11447 pop_tinst_level ();
11448
11449 /* The vtable for a template class can be emitted in any translation
11450 unit in which the class is instantiated. When there is no key
11451 method, however, finish_struct_1 will already have added TYPE to
11452 the keyed_classes. */
11453 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11454 vec_safe_push (keyed_classes, type);
11455
11456 return type;
11457 }
11458
11459 /* Wrapper for instantiate_class_template_1. */
11460
11461 tree
11462 instantiate_class_template (tree type)
11463 {
11464 tree ret;
11465 timevar_push (TV_TEMPLATE_INST);
11466 ret = instantiate_class_template_1 (type);
11467 timevar_pop (TV_TEMPLATE_INST);
11468 return ret;
11469 }
11470
11471 static tree
11472 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11473 {
11474 tree r;
11475
11476 if (!t)
11477 r = t;
11478 else if (TYPE_P (t))
11479 r = tsubst (t, args, complain, in_decl);
11480 else
11481 {
11482 if (!(complain & tf_warning))
11483 ++c_inhibit_evaluation_warnings;
11484 r = tsubst_expr (t, args, complain, in_decl,
11485 /*integral_constant_expression_p=*/true);
11486 if (!(complain & tf_warning))
11487 --c_inhibit_evaluation_warnings;
11488 }
11489 return r;
11490 }
11491
11492 /* Given a function parameter pack TMPL_PARM and some function parameters
11493 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11494 and set *SPEC_P to point at the next point in the list. */
11495
11496 tree
11497 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11498 {
11499 /* Collect all of the extra "packed" parameters into an
11500 argument pack. */
11501 tree parmvec;
11502 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11503 tree spec_parm = *spec_p;
11504 int i, len;
11505
11506 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11507 if (tmpl_parm
11508 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11509 break;
11510
11511 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11512 parmvec = make_tree_vec (len);
11513 spec_parm = *spec_p;
11514 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11515 {
11516 tree elt = spec_parm;
11517 if (DECL_PACK_P (elt))
11518 elt = make_pack_expansion (elt);
11519 TREE_VEC_ELT (parmvec, i) = elt;
11520 }
11521
11522 /* Build the argument packs. */
11523 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11524 *spec_p = spec_parm;
11525
11526 return argpack;
11527 }
11528
11529 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11530 NONTYPE_ARGUMENT_PACK. */
11531
11532 static tree
11533 make_fnparm_pack (tree spec_parm)
11534 {
11535 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11536 }
11537
11538 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11539 pack expansion with no extra args, 2 if it has extra args, or 0
11540 if it is not a pack expansion. */
11541
11542 static int
11543 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11544 {
11545 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11546 if (i >= TREE_VEC_LENGTH (vec))
11547 return 0;
11548 tree elt = TREE_VEC_ELT (vec, i);
11549 if (DECL_P (elt))
11550 /* A decl pack is itself an expansion. */
11551 elt = TREE_TYPE (elt);
11552 if (!PACK_EXPANSION_P (elt))
11553 return 0;
11554 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11555 return 2;
11556 return 1;
11557 }
11558
11559
11560 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11561
11562 static tree
11563 make_argument_pack_select (tree arg_pack, unsigned index)
11564 {
11565 tree aps = make_node (ARGUMENT_PACK_SELECT);
11566
11567 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11568 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11569
11570 return aps;
11571 }
11572
11573 /* This is a subroutine of tsubst_pack_expansion.
11574
11575 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11576 mechanism to store the (non complete list of) arguments of the
11577 substitution and return a non substituted pack expansion, in order
11578 to wait for when we have enough arguments to really perform the
11579 substitution. */
11580
11581 static bool
11582 use_pack_expansion_extra_args_p (tree parm_packs,
11583 int arg_pack_len,
11584 bool has_empty_arg)
11585 {
11586 /* If one pack has an expansion and another pack has a normal
11587 argument or if one pack has an empty argument and an another
11588 one hasn't then tsubst_pack_expansion cannot perform the
11589 substitution and need to fall back on the
11590 PACK_EXPANSION_EXTRA mechanism. */
11591 if (parm_packs == NULL_TREE)
11592 return false;
11593 else if (has_empty_arg)
11594 return true;
11595
11596 bool has_expansion_arg = false;
11597 for (int i = 0 ; i < arg_pack_len; ++i)
11598 {
11599 bool has_non_expansion_arg = false;
11600 for (tree parm_pack = parm_packs;
11601 parm_pack;
11602 parm_pack = TREE_CHAIN (parm_pack))
11603 {
11604 tree arg = TREE_VALUE (parm_pack);
11605
11606 int exp = argument_pack_element_is_expansion_p (arg, i);
11607 if (exp == 2)
11608 /* We can't substitute a pack expansion with extra args into
11609 our pattern. */
11610 return true;
11611 else if (exp)
11612 has_expansion_arg = true;
11613 else
11614 has_non_expansion_arg = true;
11615 }
11616
11617 if (has_expansion_arg && has_non_expansion_arg)
11618 return true;
11619 }
11620 return false;
11621 }
11622
11623 /* [temp.variadic]/6 says that:
11624
11625 The instantiation of a pack expansion [...]
11626 produces a list E1,E2, ..., En, where N is the number of elements
11627 in the pack expansion parameters.
11628
11629 This subroutine of tsubst_pack_expansion produces one of these Ei.
11630
11631 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11632 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11633 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11634 INDEX is the index 'i' of the element Ei to produce. ARGS,
11635 COMPLAIN, and IN_DECL are the same parameters as for the
11636 tsubst_pack_expansion function.
11637
11638 The function returns the resulting Ei upon successful completion,
11639 or error_mark_node.
11640
11641 Note that this function possibly modifies the ARGS parameter, so
11642 it's the responsibility of the caller to restore it. */
11643
11644 static tree
11645 gen_elem_of_pack_expansion_instantiation (tree pattern,
11646 tree parm_packs,
11647 unsigned index,
11648 tree args /* This parm gets
11649 modified. */,
11650 tsubst_flags_t complain,
11651 tree in_decl)
11652 {
11653 tree t;
11654 bool ith_elem_is_expansion = false;
11655
11656 /* For each parameter pack, change the substitution of the parameter
11657 pack to the ith argument in its argument pack, then expand the
11658 pattern. */
11659 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11660 {
11661 tree parm = TREE_PURPOSE (pack);
11662 tree arg_pack = TREE_VALUE (pack);
11663 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11664
11665 ith_elem_is_expansion |=
11666 argument_pack_element_is_expansion_p (arg_pack, index);
11667
11668 /* Select the Ith argument from the pack. */
11669 if (TREE_CODE (parm) == PARM_DECL
11670 || VAR_P (parm)
11671 || TREE_CODE (parm) == FIELD_DECL)
11672 {
11673 if (index == 0)
11674 {
11675 aps = make_argument_pack_select (arg_pack, index);
11676 if (!mark_used (parm, complain) && !(complain & tf_error))
11677 return error_mark_node;
11678 register_local_specialization (aps, parm);
11679 }
11680 else
11681 aps = retrieve_local_specialization (parm);
11682 }
11683 else
11684 {
11685 int idx, level;
11686 template_parm_level_and_index (parm, &level, &idx);
11687
11688 if (index == 0)
11689 {
11690 aps = make_argument_pack_select (arg_pack, index);
11691 /* Update the corresponding argument. */
11692 TMPL_ARG (args, level, idx) = aps;
11693 }
11694 else
11695 /* Re-use the ARGUMENT_PACK_SELECT. */
11696 aps = TMPL_ARG (args, level, idx);
11697 }
11698 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11699 }
11700
11701 /* Substitute into the PATTERN with the (possibly altered)
11702 arguments. */
11703 if (pattern == in_decl)
11704 /* Expanding a fixed parameter pack from
11705 coerce_template_parameter_pack. */
11706 t = tsubst_decl (pattern, args, complain);
11707 else if (pattern == error_mark_node)
11708 t = error_mark_node;
11709 else if (constraint_p (pattern))
11710 {
11711 if (processing_template_decl)
11712 t = tsubst_constraint (pattern, args, complain, in_decl);
11713 else
11714 t = (constraints_satisfied_p (pattern, args)
11715 ? boolean_true_node : boolean_false_node);
11716 }
11717 else if (!TYPE_P (pattern))
11718 t = tsubst_expr (pattern, args, complain, in_decl,
11719 /*integral_constant_expression_p=*/false);
11720 else
11721 t = tsubst (pattern, args, complain, in_decl);
11722
11723 /* If the Ith argument pack element is a pack expansion, then
11724 the Ith element resulting from the substituting is going to
11725 be a pack expansion as well. */
11726 if (ith_elem_is_expansion)
11727 t = make_pack_expansion (t, complain);
11728
11729 return t;
11730 }
11731
11732 /* When the unexpanded parameter pack in a fold expression expands to an empty
11733 sequence, the value of the expression is as follows; the program is
11734 ill-formed if the operator is not listed in this table.
11735
11736 && true
11737 || false
11738 , void() */
11739
11740 tree
11741 expand_empty_fold (tree t, tsubst_flags_t complain)
11742 {
11743 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11744 if (!FOLD_EXPR_MODIFY_P (t))
11745 switch (code)
11746 {
11747 case TRUTH_ANDIF_EXPR:
11748 return boolean_true_node;
11749 case TRUTH_ORIF_EXPR:
11750 return boolean_false_node;
11751 case COMPOUND_EXPR:
11752 return void_node;
11753 default:
11754 break;
11755 }
11756
11757 if (complain & tf_error)
11758 error_at (location_of (t),
11759 "fold of empty expansion over %O", code);
11760 return error_mark_node;
11761 }
11762
11763 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11764 form an expression that combines the two terms using the
11765 operator of T. */
11766
11767 static tree
11768 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11769 {
11770 tree op = FOLD_EXPR_OP (t);
11771 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11772
11773 // Handle compound assignment operators.
11774 if (FOLD_EXPR_MODIFY_P (t))
11775 return build_x_modify_expr (input_location, left, code, right, complain);
11776
11777 switch (code)
11778 {
11779 case COMPOUND_EXPR:
11780 return build_x_compound_expr (input_location, left, right, complain);
11781 case DOTSTAR_EXPR:
11782 return build_m_component_ref (left, right, complain);
11783 default:
11784 return build_x_binary_op (input_location, code,
11785 left, TREE_CODE (left),
11786 right, TREE_CODE (right),
11787 /*overload=*/NULL,
11788 complain);
11789 }
11790 }
11791
11792 /* Substitute ARGS into the pack of a fold expression T. */
11793
11794 static inline tree
11795 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11796 {
11797 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11798 }
11799
11800 /* Substitute ARGS into the pack of a fold expression T. */
11801
11802 static inline tree
11803 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11804 {
11805 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11806 }
11807
11808 /* Expand a PACK of arguments into a grouped as left fold.
11809 Given a pack containing elements A0, A1, ..., An and an
11810 operator @, this builds the expression:
11811
11812 ((A0 @ A1) @ A2) ... @ An
11813
11814 Note that PACK must not be empty.
11815
11816 The operator is defined by the original fold expression T. */
11817
11818 static tree
11819 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11820 {
11821 tree left = TREE_VEC_ELT (pack, 0);
11822 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11823 {
11824 tree right = TREE_VEC_ELT (pack, i);
11825 left = fold_expression (t, left, right, complain);
11826 }
11827 return left;
11828 }
11829
11830 /* Substitute into a unary left fold expression. */
11831
11832 static tree
11833 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11834 tree in_decl)
11835 {
11836 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11837 if (pack == error_mark_node)
11838 return error_mark_node;
11839 if (PACK_EXPANSION_P (pack))
11840 {
11841 tree r = copy_node (t);
11842 FOLD_EXPR_PACK (r) = pack;
11843 return r;
11844 }
11845 if (TREE_VEC_LENGTH (pack) == 0)
11846 return expand_empty_fold (t, complain);
11847 else
11848 return expand_left_fold (t, pack, complain);
11849 }
11850
11851 /* Substitute into a binary left fold expression.
11852
11853 Do ths by building a single (non-empty) vector of argumnts and
11854 building the expression from those elements. */
11855
11856 static tree
11857 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11858 tree in_decl)
11859 {
11860 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11861 if (pack == error_mark_node)
11862 return error_mark_node;
11863 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11864 if (init == error_mark_node)
11865 return error_mark_node;
11866
11867 if (PACK_EXPANSION_P (pack))
11868 {
11869 tree r = copy_node (t);
11870 FOLD_EXPR_PACK (r) = pack;
11871 FOLD_EXPR_INIT (r) = init;
11872 return r;
11873 }
11874
11875 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11876 TREE_VEC_ELT (vec, 0) = init;
11877 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11878 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11879
11880 return expand_left_fold (t, vec, complain);
11881 }
11882
11883 /* Expand a PACK of arguments into a grouped as right fold.
11884 Given a pack containing elementns A0, A1, ..., and an
11885 operator @, this builds the expression:
11886
11887 A0@ ... (An-2 @ (An-1 @ An))
11888
11889 Note that PACK must not be empty.
11890
11891 The operator is defined by the original fold expression T. */
11892
11893 tree
11894 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11895 {
11896 // Build the expression.
11897 int n = TREE_VEC_LENGTH (pack);
11898 tree right = TREE_VEC_ELT (pack, n - 1);
11899 for (--n; n != 0; --n)
11900 {
11901 tree left = TREE_VEC_ELT (pack, n - 1);
11902 right = fold_expression (t, left, right, complain);
11903 }
11904 return right;
11905 }
11906
11907 /* Substitute into a unary right fold expression. */
11908
11909 static tree
11910 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11911 tree in_decl)
11912 {
11913 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11914 if (pack == error_mark_node)
11915 return error_mark_node;
11916 if (PACK_EXPANSION_P (pack))
11917 {
11918 tree r = copy_node (t);
11919 FOLD_EXPR_PACK (r) = pack;
11920 return r;
11921 }
11922 if (TREE_VEC_LENGTH (pack) == 0)
11923 return expand_empty_fold (t, complain);
11924 else
11925 return expand_right_fold (t, pack, complain);
11926 }
11927
11928 /* Substitute into a binary right fold expression.
11929
11930 Do ths by building a single (non-empty) vector of arguments and
11931 building the expression from those elements. */
11932
11933 static tree
11934 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11935 tree in_decl)
11936 {
11937 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11938 if (pack == error_mark_node)
11939 return error_mark_node;
11940 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11941 if (init == error_mark_node)
11942 return error_mark_node;
11943
11944 if (PACK_EXPANSION_P (pack))
11945 {
11946 tree r = copy_node (t);
11947 FOLD_EXPR_PACK (r) = pack;
11948 FOLD_EXPR_INIT (r) = init;
11949 return r;
11950 }
11951
11952 int n = TREE_VEC_LENGTH (pack);
11953 tree vec = make_tree_vec (n + 1);
11954 for (int i = 0; i < n; ++i)
11955 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11956 TREE_VEC_ELT (vec, n) = init;
11957
11958 return expand_right_fold (t, vec, complain);
11959 }
11960
11961 /* Walk through the pattern of a pack expansion, adding everything in
11962 local_specializations to a list. */
11963
11964 struct el_data
11965 {
11966 hash_set<tree> internal;
11967 tree extra;
11968 tsubst_flags_t complain;
11969
11970 el_data (tsubst_flags_t c)
11971 : extra (NULL_TREE), complain (c) {}
11972 };
11973 static tree
11974 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11975 {
11976 el_data &data = *reinterpret_cast<el_data*>(data_);
11977 tree *extra = &data.extra;
11978 tsubst_flags_t complain = data.complain;
11979
11980 if (TYPE_P (*tp) && typedef_variant_p (*tp))
11981 /* Remember local typedefs (85214). */
11982 tp = &TYPE_NAME (*tp);
11983
11984 if (TREE_CODE (*tp) == DECL_EXPR)
11985 data.internal.add (DECL_EXPR_DECL (*tp));
11986 else if (tree spec = retrieve_local_specialization (*tp))
11987 {
11988 if (data.internal.contains (*tp))
11989 /* Don't mess with variables declared within the pattern. */
11990 return NULL_TREE;
11991 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11992 {
11993 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11994 tree args = ARGUMENT_PACK_ARGS (spec);
11995 if (TREE_VEC_LENGTH (args) == 1)
11996 {
11997 tree elt = TREE_VEC_ELT (args, 0);
11998 if (PACK_EXPANSION_P (elt))
11999 elt = PACK_EXPANSION_PATTERN (elt);
12000 if (DECL_PACK_P (elt))
12001 spec = elt;
12002 }
12003 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12004 {
12005 /* Handle lambda capture here, since we aren't doing any
12006 substitution now, and so tsubst_copy won't call
12007 process_outer_var_ref. */
12008 tree args = ARGUMENT_PACK_ARGS (spec);
12009 int len = TREE_VEC_LENGTH (args);
12010 for (int i = 0; i < len; ++i)
12011 {
12012 tree arg = TREE_VEC_ELT (args, i);
12013 tree carg = arg;
12014 if (outer_automatic_var_p (arg))
12015 carg = process_outer_var_ref (arg, complain);
12016 if (carg != arg)
12017 {
12018 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12019 proxies. */
12020 if (i == 0)
12021 {
12022 spec = copy_node (spec);
12023 args = copy_node (args);
12024 SET_ARGUMENT_PACK_ARGS (spec, args);
12025 register_local_specialization (spec, *tp);
12026 }
12027 TREE_VEC_ELT (args, i) = carg;
12028 }
12029 }
12030 }
12031 }
12032 if (outer_automatic_var_p (spec))
12033 spec = process_outer_var_ref (spec, complain);
12034 *extra = tree_cons (*tp, spec, *extra);
12035 }
12036 return NULL_TREE;
12037 }
12038 static tree
12039 extract_local_specs (tree pattern, tsubst_flags_t complain)
12040 {
12041 el_data data (complain);
12042 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12043 return data.extra;
12044 }
12045
12046 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12047 for use in PACK_EXPANSION_EXTRA_ARGS. */
12048
12049 tree
12050 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12051 {
12052 tree extra = args;
12053 if (local_specializations)
12054 if (tree locals = extract_local_specs (pattern, complain))
12055 extra = tree_cons (NULL_TREE, extra, locals);
12056 return extra;
12057 }
12058
12059 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12060 normal template args to ARGS. */
12061
12062 tree
12063 add_extra_args (tree extra, tree args)
12064 {
12065 if (extra && TREE_CODE (extra) == TREE_LIST)
12066 {
12067 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12068 {
12069 /* The partial instantiation involved local declarations collected in
12070 extract_local_specs; map from the general template to our local
12071 context. */
12072 tree gen = TREE_PURPOSE (elt);
12073 tree inst = TREE_VALUE (elt);
12074 if (DECL_P (inst))
12075 if (tree local = retrieve_local_specialization (inst))
12076 inst = local;
12077 /* else inst is already a full instantiation of the pack. */
12078 register_local_specialization (inst, gen);
12079 }
12080 gcc_assert (!TREE_PURPOSE (extra));
12081 extra = TREE_VALUE (extra);
12082 }
12083 return add_to_template_args (extra, args);
12084 }
12085
12086 /* Substitute ARGS into T, which is an pack expansion
12087 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12088 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12089 (if only a partial substitution could be performed) or
12090 ERROR_MARK_NODE if there was an error. */
12091 tree
12092 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12093 tree in_decl)
12094 {
12095 tree pattern;
12096 tree pack, packs = NULL_TREE;
12097 bool unsubstituted_packs = false;
12098 bool unsubstituted_fn_pack = false;
12099 int i, len = -1;
12100 tree result;
12101 hash_map<tree, tree> *saved_local_specializations = NULL;
12102 bool need_local_specializations = false;
12103 int levels;
12104
12105 gcc_assert (PACK_EXPANSION_P (t));
12106 pattern = PACK_EXPANSION_PATTERN (t);
12107
12108 /* Add in any args remembered from an earlier partial instantiation. */
12109 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12110
12111 levels = TMPL_ARGS_DEPTH (args);
12112
12113 /* Determine the argument packs that will instantiate the parameter
12114 packs used in the expansion expression. While we're at it,
12115 compute the number of arguments to be expanded and make sure it
12116 is consistent. */
12117 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12118 pack = TREE_CHAIN (pack))
12119 {
12120 tree parm_pack = TREE_VALUE (pack);
12121 tree arg_pack = NULL_TREE;
12122 tree orig_arg = NULL_TREE;
12123 int level = 0;
12124
12125 if (TREE_CODE (parm_pack) == BASES)
12126 {
12127 gcc_assert (parm_pack == pattern);
12128 if (BASES_DIRECT (parm_pack))
12129 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12130 args, complain,
12131 in_decl, false),
12132 complain);
12133 else
12134 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12135 args, complain, in_decl,
12136 false), complain);
12137 }
12138 else if (builtin_pack_call_p (parm_pack))
12139 {
12140 if (parm_pack != pattern)
12141 {
12142 if (complain & tf_error)
12143 sorry ("%qE is not the entire pattern of the pack expansion",
12144 parm_pack);
12145 return error_mark_node;
12146 }
12147 return expand_builtin_pack_call (parm_pack, args,
12148 complain, in_decl);
12149 }
12150 else if (TREE_CODE (parm_pack) == PARM_DECL)
12151 {
12152 /* We know we have correct local_specializations if this
12153 expansion is at function scope, or if we're dealing with a
12154 local parameter in a requires expression; for the latter,
12155 tsubst_requires_expr set it up appropriately. */
12156 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12157 arg_pack = retrieve_local_specialization (parm_pack);
12158 else
12159 /* We can't rely on local_specializations for a parameter
12160 name used later in a function declaration (such as in a
12161 late-specified return type). Even if it exists, it might
12162 have the wrong value for a recursive call. */
12163 need_local_specializations = true;
12164
12165 if (!arg_pack)
12166 {
12167 /* This parameter pack was used in an unevaluated context. Just
12168 make a dummy decl, since it's only used for its type. */
12169 ++cp_unevaluated_operand;
12170 arg_pack = tsubst_decl (parm_pack, args, complain);
12171 --cp_unevaluated_operand;
12172 if (arg_pack && DECL_PACK_P (arg_pack))
12173 /* Partial instantiation of the parm_pack, we can't build
12174 up an argument pack yet. */
12175 arg_pack = NULL_TREE;
12176 else
12177 arg_pack = make_fnparm_pack (arg_pack);
12178 }
12179 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12180 /* This argument pack isn't fully instantiated yet. We set this
12181 flag rather than clear arg_pack because we do want to do the
12182 optimization below, and we don't want to substitute directly
12183 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12184 where it isn't expected). */
12185 unsubstituted_fn_pack = true;
12186 }
12187 else if (is_capture_proxy (parm_pack))
12188 {
12189 arg_pack = retrieve_local_specialization (parm_pack);
12190 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12191 unsubstituted_fn_pack = true;
12192 }
12193 else
12194 {
12195 int idx;
12196 template_parm_level_and_index (parm_pack, &level, &idx);
12197
12198 if (level <= levels)
12199 arg_pack = TMPL_ARG (args, level, idx);
12200 }
12201
12202 orig_arg = arg_pack;
12203 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12204 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12205
12206 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12207 /* This can only happen if we forget to expand an argument
12208 pack somewhere else. Just return an error, silently. */
12209 {
12210 result = make_tree_vec (1);
12211 TREE_VEC_ELT (result, 0) = error_mark_node;
12212 return result;
12213 }
12214
12215 if (arg_pack)
12216 {
12217 int my_len =
12218 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12219
12220 /* Don't bother trying to do a partial substitution with
12221 incomplete packs; we'll try again after deduction. */
12222 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12223 return t;
12224
12225 if (len < 0)
12226 len = my_len;
12227 else if (len != my_len
12228 && !unsubstituted_fn_pack)
12229 {
12230 if (!(complain & tf_error))
12231 /* Fail quietly. */;
12232 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12233 error ("mismatched argument pack lengths while expanding %qT",
12234 pattern);
12235 else
12236 error ("mismatched argument pack lengths while expanding %qE",
12237 pattern);
12238 return error_mark_node;
12239 }
12240
12241 /* Keep track of the parameter packs and their corresponding
12242 argument packs. */
12243 packs = tree_cons (parm_pack, arg_pack, packs);
12244 TREE_TYPE (packs) = orig_arg;
12245 }
12246 else
12247 {
12248 /* We can't substitute for this parameter pack. We use a flag as
12249 well as the missing_level counter because function parameter
12250 packs don't have a level. */
12251 gcc_assert (processing_template_decl || is_auto (parm_pack));
12252 unsubstituted_packs = true;
12253 }
12254 }
12255
12256 /* If the expansion is just T..., return the matching argument pack, unless
12257 we need to call convert_from_reference on all the elements. This is an
12258 important optimization; see c++/68422. */
12259 if (!unsubstituted_packs
12260 && TREE_PURPOSE (packs) == pattern)
12261 {
12262 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12263
12264 /* If the argument pack is a single pack expansion, pull it out. */
12265 if (TREE_VEC_LENGTH (args) == 1
12266 && pack_expansion_args_count (args))
12267 return TREE_VEC_ELT (args, 0);
12268
12269 /* Types need no adjustment, nor does sizeof..., and if we still have
12270 some pack expansion args we won't do anything yet. */
12271 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12272 || PACK_EXPANSION_SIZEOF_P (t)
12273 || pack_expansion_args_count (args))
12274 return args;
12275 /* Also optimize expression pack expansions if we can tell that the
12276 elements won't have reference type. */
12277 tree type = TREE_TYPE (pattern);
12278 if (type && !TYPE_REF_P (type)
12279 && !PACK_EXPANSION_P (type)
12280 && !WILDCARD_TYPE_P (type))
12281 return args;
12282 /* Otherwise use the normal path so we get convert_from_reference. */
12283 }
12284
12285 /* We cannot expand this expansion expression, because we don't have
12286 all of the argument packs we need. */
12287 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12288 {
12289 /* We got some full packs, but we can't substitute them in until we
12290 have values for all the packs. So remember these until then. */
12291
12292 t = make_pack_expansion (pattern, complain);
12293 PACK_EXPANSION_EXTRA_ARGS (t)
12294 = build_extra_args (pattern, args, complain);
12295 return t;
12296 }
12297 else if (unsubstituted_packs)
12298 {
12299 /* There were no real arguments, we're just replacing a parameter
12300 pack with another version of itself. Substitute into the
12301 pattern and return a PACK_EXPANSION_*. The caller will need to
12302 deal with that. */
12303 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12304 t = tsubst_expr (pattern, args, complain, in_decl,
12305 /*integral_constant_expression_p=*/false);
12306 else
12307 t = tsubst (pattern, args, complain, in_decl);
12308 t = make_pack_expansion (t, complain);
12309 return t;
12310 }
12311
12312 gcc_assert (len >= 0);
12313
12314 if (need_local_specializations)
12315 {
12316 /* We're in a late-specified return type, so create our own local
12317 specializations map; the current map is either NULL or (in the
12318 case of recursive unification) might have bindings that we don't
12319 want to use or alter. */
12320 saved_local_specializations = local_specializations;
12321 local_specializations = new hash_map<tree, tree>;
12322 }
12323
12324 /* For each argument in each argument pack, substitute into the
12325 pattern. */
12326 result = make_tree_vec (len);
12327 tree elem_args = copy_template_args (args);
12328 for (i = 0; i < len; ++i)
12329 {
12330 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12331 i,
12332 elem_args, complain,
12333 in_decl);
12334 TREE_VEC_ELT (result, i) = t;
12335 if (t == error_mark_node)
12336 {
12337 result = error_mark_node;
12338 break;
12339 }
12340 }
12341
12342 /* Update ARGS to restore the substitution from parameter packs to
12343 their argument packs. */
12344 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12345 {
12346 tree parm = TREE_PURPOSE (pack);
12347
12348 if (TREE_CODE (parm) == PARM_DECL
12349 || VAR_P (parm)
12350 || TREE_CODE (parm) == FIELD_DECL)
12351 register_local_specialization (TREE_TYPE (pack), parm);
12352 else
12353 {
12354 int idx, level;
12355
12356 if (TREE_VALUE (pack) == NULL_TREE)
12357 continue;
12358
12359 template_parm_level_and_index (parm, &level, &idx);
12360
12361 /* Update the corresponding argument. */
12362 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12363 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12364 TREE_TYPE (pack);
12365 else
12366 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12367 }
12368 }
12369
12370 if (need_local_specializations)
12371 {
12372 delete local_specializations;
12373 local_specializations = saved_local_specializations;
12374 }
12375
12376 /* If the dependent pack arguments were such that we end up with only a
12377 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12378 if (len == 1 && TREE_CODE (result) == TREE_VEC
12379 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12380 return TREE_VEC_ELT (result, 0);
12381
12382 return result;
12383 }
12384
12385 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12386 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12387 parameter packs; all parms generated from a function parameter pack will
12388 have the same DECL_PARM_INDEX. */
12389
12390 tree
12391 get_pattern_parm (tree parm, tree tmpl)
12392 {
12393 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12394 tree patparm;
12395
12396 if (DECL_ARTIFICIAL (parm))
12397 {
12398 for (patparm = DECL_ARGUMENTS (pattern);
12399 patparm; patparm = DECL_CHAIN (patparm))
12400 if (DECL_ARTIFICIAL (patparm)
12401 && DECL_NAME (parm) == DECL_NAME (patparm))
12402 break;
12403 }
12404 else
12405 {
12406 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12407 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12408 gcc_assert (DECL_PARM_INDEX (patparm)
12409 == DECL_PARM_INDEX (parm));
12410 }
12411
12412 return patparm;
12413 }
12414
12415 /* Make an argument pack out of the TREE_VEC VEC. */
12416
12417 static tree
12418 make_argument_pack (tree vec)
12419 {
12420 tree pack;
12421 tree elt = TREE_VEC_ELT (vec, 0);
12422 if (TYPE_P (elt))
12423 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12424 else
12425 {
12426 pack = make_node (NONTYPE_ARGUMENT_PACK);
12427 TREE_CONSTANT (pack) = 1;
12428 }
12429 SET_ARGUMENT_PACK_ARGS (pack, vec);
12430 return pack;
12431 }
12432
12433 /* Return an exact copy of template args T that can be modified
12434 independently. */
12435
12436 static tree
12437 copy_template_args (tree t)
12438 {
12439 if (t == error_mark_node)
12440 return t;
12441
12442 int len = TREE_VEC_LENGTH (t);
12443 tree new_vec = make_tree_vec (len);
12444
12445 for (int i = 0; i < len; ++i)
12446 {
12447 tree elt = TREE_VEC_ELT (t, i);
12448 if (elt && TREE_CODE (elt) == TREE_VEC)
12449 elt = copy_template_args (elt);
12450 TREE_VEC_ELT (new_vec, i) = elt;
12451 }
12452
12453 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12454 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12455
12456 return new_vec;
12457 }
12458
12459 /* Substitute ARGS into the vector or list of template arguments T. */
12460
12461 static tree
12462 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12463 {
12464 tree orig_t = t;
12465 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12466 tree *elts;
12467
12468 if (t == error_mark_node)
12469 return error_mark_node;
12470
12471 len = TREE_VEC_LENGTH (t);
12472 elts = XALLOCAVEC (tree, len);
12473
12474 for (i = 0; i < len; i++)
12475 {
12476 tree orig_arg = TREE_VEC_ELT (t, i);
12477 tree new_arg;
12478
12479 if (TREE_CODE (orig_arg) == TREE_VEC)
12480 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12481 else if (PACK_EXPANSION_P (orig_arg))
12482 {
12483 /* Substitute into an expansion expression. */
12484 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12485
12486 if (TREE_CODE (new_arg) == TREE_VEC)
12487 /* Add to the expanded length adjustment the number of
12488 expanded arguments. We subtract one from this
12489 measurement, because the argument pack expression
12490 itself is already counted as 1 in
12491 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12492 the argument pack is empty. */
12493 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12494 }
12495 else if (ARGUMENT_PACK_P (orig_arg))
12496 {
12497 /* Substitute into each of the arguments. */
12498 new_arg = TYPE_P (orig_arg)
12499 ? cxx_make_type (TREE_CODE (orig_arg))
12500 : make_node (TREE_CODE (orig_arg));
12501
12502 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12503 args, complain, in_decl);
12504 if (pack_args == error_mark_node)
12505 new_arg = error_mark_node;
12506 else
12507 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12508
12509 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12510 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12511 }
12512 else
12513 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12514
12515 if (new_arg == error_mark_node)
12516 return error_mark_node;
12517
12518 elts[i] = new_arg;
12519 if (new_arg != orig_arg)
12520 need_new = 1;
12521 }
12522
12523 if (!need_new)
12524 return t;
12525
12526 /* Make space for the expanded arguments coming from template
12527 argument packs. */
12528 t = make_tree_vec (len + expanded_len_adjust);
12529 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12530 arguments for a member template.
12531 In that case each TREE_VEC in ORIG_T represents a level of template
12532 arguments, and ORIG_T won't carry any non defaulted argument count.
12533 It will rather be the nested TREE_VECs that will carry one.
12534 In other words, ORIG_T carries a non defaulted argument count only
12535 if it doesn't contain any nested TREE_VEC. */
12536 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12537 {
12538 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12539 count += expanded_len_adjust;
12540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12541 }
12542 for (i = 0, out = 0; i < len; i++)
12543 {
12544 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12545 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12546 && TREE_CODE (elts[i]) == TREE_VEC)
12547 {
12548 int idx;
12549
12550 /* Now expand the template argument pack "in place". */
12551 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12552 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12553 }
12554 else
12555 {
12556 TREE_VEC_ELT (t, out) = elts[i];
12557 out++;
12558 }
12559 }
12560
12561 return t;
12562 }
12563
12564 /* Substitute ARGS into one level PARMS of template parameters. */
12565
12566 static tree
12567 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12568 {
12569 if (parms == error_mark_node)
12570 return error_mark_node;
12571
12572 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12573
12574 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12575 {
12576 tree tuple = TREE_VEC_ELT (parms, i);
12577
12578 if (tuple == error_mark_node)
12579 continue;
12580
12581 TREE_VEC_ELT (new_vec, i) =
12582 tsubst_template_parm (tuple, args, complain);
12583 }
12584
12585 return new_vec;
12586 }
12587
12588 /* Return the result of substituting ARGS into the template parameters
12589 given by PARMS. If there are m levels of ARGS and m + n levels of
12590 PARMS, then the result will contain n levels of PARMS. For
12591 example, if PARMS is `template <class T> template <class U>
12592 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12593 result will be `template <int*, double, class V>'. */
12594
12595 static tree
12596 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12597 {
12598 tree r = NULL_TREE;
12599 tree* new_parms;
12600
12601 /* When substituting into a template, we must set
12602 PROCESSING_TEMPLATE_DECL as the template parameters may be
12603 dependent if they are based on one-another, and the dependency
12604 predicates are short-circuit outside of templates. */
12605 ++processing_template_decl;
12606
12607 for (new_parms = &r;
12608 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12609 new_parms = &(TREE_CHAIN (*new_parms)),
12610 parms = TREE_CHAIN (parms))
12611 {
12612 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12613 args, complain);
12614 *new_parms =
12615 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12616 - TMPL_ARGS_DEPTH (args)),
12617 new_vec, NULL_TREE);
12618 }
12619
12620 --processing_template_decl;
12621
12622 return r;
12623 }
12624
12625 /* Return the result of substituting ARGS into one template parameter
12626 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12627 parameter and which TREE_PURPOSE is the default argument of the
12628 template parameter. */
12629
12630 static tree
12631 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12632 {
12633 tree default_value, parm_decl;
12634
12635 if (args == NULL_TREE
12636 || t == NULL_TREE
12637 || t == error_mark_node)
12638 return t;
12639
12640 gcc_assert (TREE_CODE (t) == TREE_LIST);
12641
12642 default_value = TREE_PURPOSE (t);
12643 parm_decl = TREE_VALUE (t);
12644
12645 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12646 if (TREE_CODE (parm_decl) == PARM_DECL
12647 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12648 parm_decl = error_mark_node;
12649 default_value = tsubst_template_arg (default_value, args,
12650 complain, NULL_TREE);
12651
12652 return build_tree_list (default_value, parm_decl);
12653 }
12654
12655 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12656 type T. If T is not an aggregate or enumeration type, it is
12657 handled as if by tsubst. IN_DECL is as for tsubst. If
12658 ENTERING_SCOPE is nonzero, T is the context for a template which
12659 we are presently tsubst'ing. Return the substituted value. */
12660
12661 static tree
12662 tsubst_aggr_type (tree t,
12663 tree args,
12664 tsubst_flags_t complain,
12665 tree in_decl,
12666 int entering_scope)
12667 {
12668 if (t == NULL_TREE)
12669 return NULL_TREE;
12670
12671 switch (TREE_CODE (t))
12672 {
12673 case RECORD_TYPE:
12674 if (TYPE_PTRMEMFUNC_P (t))
12675 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12676
12677 /* Fall through. */
12678 case ENUMERAL_TYPE:
12679 case UNION_TYPE:
12680 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12681 {
12682 tree argvec;
12683 tree context;
12684 tree r;
12685
12686 /* In "sizeof(X<I>)" we need to evaluate "I". */
12687 cp_evaluated ev;
12688
12689 /* First, determine the context for the type we are looking
12690 up. */
12691 context = TYPE_CONTEXT (t);
12692 if (context && TYPE_P (context))
12693 {
12694 context = tsubst_aggr_type (context, args, complain,
12695 in_decl, /*entering_scope=*/1);
12696 /* If context is a nested class inside a class template,
12697 it may still need to be instantiated (c++/33959). */
12698 context = complete_type (context);
12699 }
12700
12701 /* Then, figure out what arguments are appropriate for the
12702 type we are trying to find. For example, given:
12703
12704 template <class T> struct S;
12705 template <class T, class U> void f(T, U) { S<U> su; }
12706
12707 and supposing that we are instantiating f<int, double>,
12708 then our ARGS will be {int, double}, but, when looking up
12709 S we only want {double}. */
12710 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12711 complain, in_decl);
12712 if (argvec == error_mark_node)
12713 r = error_mark_node;
12714 else
12715 {
12716 r = lookup_template_class (t, argvec, in_decl, context,
12717 entering_scope, complain);
12718 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12719 }
12720
12721 return r;
12722 }
12723 else
12724 /* This is not a template type, so there's nothing to do. */
12725 return t;
12726
12727 default:
12728 return tsubst (t, args, complain, in_decl);
12729 }
12730 }
12731
12732 static GTY((cache)) tree_cache_map *defarg_inst;
12733
12734 /* Substitute into the default argument ARG (a default argument for
12735 FN), which has the indicated TYPE. */
12736
12737 tree
12738 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12739 tsubst_flags_t complain)
12740 {
12741 int errs = errorcount + sorrycount;
12742
12743 /* This can happen in invalid code. */
12744 if (TREE_CODE (arg) == DEFAULT_ARG)
12745 return arg;
12746
12747 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12748 parm = chain_index (parmnum, parm);
12749 tree parmtype = TREE_TYPE (parm);
12750 if (DECL_BY_REFERENCE (parm))
12751 parmtype = TREE_TYPE (parmtype);
12752 if (parmtype == error_mark_node)
12753 return error_mark_node;
12754
12755 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12756
12757 tree *slot;
12758 if (defarg_inst && (slot = defarg_inst->get (parm)))
12759 return *slot;
12760
12761 /* This default argument came from a template. Instantiate the
12762 default argument here, not in tsubst. In the case of
12763 something like:
12764
12765 template <class T>
12766 struct S {
12767 static T t();
12768 void f(T = t());
12769 };
12770
12771 we must be careful to do name lookup in the scope of S<T>,
12772 rather than in the current class. */
12773 push_to_top_level ();
12774 push_access_scope (fn);
12775 start_lambda_scope (parm);
12776
12777 /* The default argument expression may cause implicitly defined
12778 member functions to be synthesized, which will result in garbage
12779 collection. We must treat this situation as if we were within
12780 the body of function so as to avoid collecting live data on the
12781 stack. */
12782 ++function_depth;
12783 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12784 complain, NULL_TREE,
12785 /*integral_constant_expression_p=*/false);
12786 --function_depth;
12787
12788 finish_lambda_scope ();
12789
12790 /* Make sure the default argument is reasonable. */
12791 arg = check_default_argument (type, arg, complain);
12792
12793 if (errorcount+sorrycount > errs
12794 && (complain & tf_warning_or_error))
12795 inform (input_location,
12796 " when instantiating default argument for call to %qD", fn);
12797
12798 pop_access_scope (fn);
12799 pop_from_top_level ();
12800
12801 if (arg != error_mark_node && !cp_unevaluated_operand)
12802 {
12803 if (!defarg_inst)
12804 defarg_inst = tree_cache_map::create_ggc (37);
12805 defarg_inst->put (parm, arg);
12806 }
12807
12808 return arg;
12809 }
12810
12811 /* Substitute into all the default arguments for FN. */
12812
12813 static void
12814 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12815 {
12816 tree arg;
12817 tree tmpl_args;
12818
12819 tmpl_args = DECL_TI_ARGS (fn);
12820
12821 /* If this function is not yet instantiated, we certainly don't need
12822 its default arguments. */
12823 if (uses_template_parms (tmpl_args))
12824 return;
12825 /* Don't do this again for clones. */
12826 if (DECL_CLONED_FUNCTION_P (fn))
12827 return;
12828
12829 int i = 0;
12830 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12831 arg;
12832 arg = TREE_CHAIN (arg), ++i)
12833 if (TREE_PURPOSE (arg))
12834 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12835 TREE_VALUE (arg),
12836 TREE_PURPOSE (arg),
12837 complain);
12838 }
12839
12840 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12841 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12842
12843 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12844
12845 void
12846 store_explicit_specifier (tree v, tree t)
12847 {
12848 if (!explicit_specifier_map)
12849 explicit_specifier_map = tree_cache_map::create_ggc (37);
12850 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12851 explicit_specifier_map->put (v, t);
12852 }
12853
12854 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12855
12856 static tree
12857 lookup_explicit_specifier (tree v)
12858 {
12859 return *explicit_specifier_map->get (v);
12860 }
12861
12862 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12863
12864 static tree
12865 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12866 tree lambda_fntype)
12867 {
12868 tree gen_tmpl, argvec;
12869 hashval_t hash = 0;
12870 tree in_decl = t;
12871
12872 /* Nobody should be tsubst'ing into non-template functions. */
12873 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12874
12875 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12876 {
12877 /* If T is not dependent, just return it. */
12878 if (!uses_template_parms (DECL_TI_ARGS (t))
12879 && !LAMBDA_FUNCTION_P (t))
12880 return t;
12881
12882 /* Calculate the most general template of which R is a
12883 specialization. */
12884 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12885
12886 /* We're substituting a lambda function under tsubst_lambda_expr but not
12887 directly from it; find the matching function we're already inside.
12888 But don't do this if T is a generic lambda with a single level of
12889 template parms, as in that case we're doing a normal instantiation. */
12890 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12891 && (!generic_lambda_fn_p (t)
12892 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12893 return enclosing_instantiation_of (t);
12894
12895 /* Calculate the complete set of arguments used to
12896 specialize R. */
12897 argvec = tsubst_template_args (DECL_TI_ARGS
12898 (DECL_TEMPLATE_RESULT
12899 (DECL_TI_TEMPLATE (t))),
12900 args, complain, in_decl);
12901 if (argvec == error_mark_node)
12902 return error_mark_node;
12903
12904 /* Check to see if we already have this specialization. */
12905 if (!lambda_fntype)
12906 {
12907 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12908 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12909 return spec;
12910 }
12911
12912 /* We can see more levels of arguments than parameters if
12913 there was a specialization of a member template, like
12914 this:
12915
12916 template <class T> struct S { template <class U> void f(); }
12917 template <> template <class U> void S<int>::f(U);
12918
12919 Here, we'll be substituting into the specialization,
12920 because that's where we can find the code we actually
12921 want to generate, but we'll have enough arguments for
12922 the most general template.
12923
12924 We also deal with the peculiar case:
12925
12926 template <class T> struct S {
12927 template <class U> friend void f();
12928 };
12929 template <class U> void f() {}
12930 template S<int>;
12931 template void f<double>();
12932
12933 Here, the ARGS for the instantiation of will be {int,
12934 double}. But, we only need as many ARGS as there are
12935 levels of template parameters in CODE_PATTERN. We are
12936 careful not to get fooled into reducing the ARGS in
12937 situations like:
12938
12939 template <class T> struct S { template <class U> void f(U); }
12940 template <class T> template <> void S<T>::f(int) {}
12941
12942 which we can spot because the pattern will be a
12943 specialization in this case. */
12944 int args_depth = TMPL_ARGS_DEPTH (args);
12945 int parms_depth =
12946 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12947
12948 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12949 args = get_innermost_template_args (args, parms_depth);
12950 }
12951 else
12952 {
12953 /* This special case arises when we have something like this:
12954
12955 template <class T> struct S {
12956 friend void f<int>(int, double);
12957 };
12958
12959 Here, the DECL_TI_TEMPLATE for the friend declaration
12960 will be an IDENTIFIER_NODE. We are being called from
12961 tsubst_friend_function, and we want only to create a
12962 new decl (R) with appropriate types so that we can call
12963 determine_specialization. */
12964 gen_tmpl = NULL_TREE;
12965 argvec = NULL_TREE;
12966 }
12967
12968 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12969 : NULL_TREE);
12970 tree ctx = closure ? closure : DECL_CONTEXT (t);
12971 bool member = ctx && TYPE_P (ctx);
12972
12973 if (member && !closure)
12974 ctx = tsubst_aggr_type (ctx, args,
12975 complain, t, /*entering_scope=*/1);
12976
12977 tree type = (lambda_fntype ? lambda_fntype
12978 : tsubst (TREE_TYPE (t), args,
12979 complain | tf_fndecl_type, in_decl));
12980 if (type == error_mark_node)
12981 return error_mark_node;
12982
12983 /* If we hit excessive deduction depth, the type is bogus even if
12984 it isn't error_mark_node, so don't build a decl. */
12985 if (excessive_deduction_depth)
12986 return error_mark_node;
12987
12988 /* We do NOT check for matching decls pushed separately at this
12989 point, as they may not represent instantiations of this
12990 template, and in any case are considered separate under the
12991 discrete model. */
12992 tree r = copy_decl (t);
12993 DECL_USE_TEMPLATE (r) = 0;
12994 TREE_TYPE (r) = type;
12995 /* Clear out the mangled name and RTL for the instantiation. */
12996 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12997 SET_DECL_RTL (r, NULL);
12998 /* Leave DECL_INITIAL set on deleted instantiations. */
12999 if (!DECL_DELETED_FN (r))
13000 DECL_INITIAL (r) = NULL_TREE;
13001 DECL_CONTEXT (r) = ctx;
13002
13003 /* Handle explicit(dependent-expr). */
13004 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13005 {
13006 tree spec = lookup_explicit_specifier (t);
13007 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13008 /*function_p=*/false,
13009 /*i_c_e_p=*/true);
13010 spec = build_explicit_specifier (spec, complain);
13011 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13012 }
13013
13014 /* OpenMP UDRs have the only argument a reference to the declared
13015 type. We want to diagnose if the declared type is a reference,
13016 which is invalid, but as references to references are usually
13017 quietly merged, diagnose it here. */
13018 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13019 {
13020 tree argtype
13021 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13022 argtype = tsubst (argtype, args, complain, in_decl);
13023 if (TYPE_REF_P (argtype))
13024 error_at (DECL_SOURCE_LOCATION (t),
13025 "reference type %qT in "
13026 "%<#pragma omp declare reduction%>", argtype);
13027 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13028 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13029 argtype);
13030 }
13031
13032 if (member && DECL_CONV_FN_P (r))
13033 /* Type-conversion operator. Reconstruct the name, in
13034 case it's the name of one of the template's parameters. */
13035 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13036
13037 tree parms = DECL_ARGUMENTS (t);
13038 if (closure)
13039 parms = DECL_CHAIN (parms);
13040 parms = tsubst (parms, args, complain, t);
13041 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13042 DECL_CONTEXT (parm) = r;
13043 if (closure)
13044 {
13045 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13046 DECL_CHAIN (tparm) = parms;
13047 parms = tparm;
13048 }
13049 DECL_ARGUMENTS (r) = parms;
13050 DECL_RESULT (r) = NULL_TREE;
13051
13052 TREE_STATIC (r) = 0;
13053 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13054 DECL_EXTERNAL (r) = 1;
13055 /* If this is an instantiation of a function with internal
13056 linkage, we already know what object file linkage will be
13057 assigned to the instantiation. */
13058 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13059 DECL_DEFER_OUTPUT (r) = 0;
13060 DECL_CHAIN (r) = NULL_TREE;
13061 DECL_PENDING_INLINE_INFO (r) = 0;
13062 DECL_PENDING_INLINE_P (r) = 0;
13063 DECL_SAVED_TREE (r) = NULL_TREE;
13064 DECL_STRUCT_FUNCTION (r) = NULL;
13065 TREE_USED (r) = 0;
13066 /* We'll re-clone as appropriate in instantiate_template. */
13067 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13068
13069 /* If we aren't complaining now, return on error before we register
13070 the specialization so that we'll complain eventually. */
13071 if ((complain & tf_error) == 0
13072 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13073 && !grok_op_properties (r, /*complain=*/false))
13074 return error_mark_node;
13075
13076 /* When instantiating a constrained member, substitute
13077 into the constraints to create a new constraint. */
13078 if (tree ci = get_constraints (t))
13079 if (member)
13080 {
13081 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13082 set_constraints (r, ci);
13083 }
13084
13085 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13086 this in the special friend case mentioned above where
13087 GEN_TMPL is NULL. */
13088 if (gen_tmpl && !closure)
13089 {
13090 DECL_TEMPLATE_INFO (r)
13091 = build_template_info (gen_tmpl, argvec);
13092 SET_DECL_IMPLICIT_INSTANTIATION (r);
13093
13094 tree new_r
13095 = register_specialization (r, gen_tmpl, argvec, false, hash);
13096 if (new_r != r)
13097 /* We instantiated this while substituting into
13098 the type earlier (template/friend54.C). */
13099 return new_r;
13100
13101 /* We're not supposed to instantiate default arguments
13102 until they are called, for a template. But, for a
13103 declaration like:
13104
13105 template <class T> void f ()
13106 { extern void g(int i = T()); }
13107
13108 we should do the substitution when the template is
13109 instantiated. We handle the member function case in
13110 instantiate_class_template since the default arguments
13111 might refer to other members of the class. */
13112 if (!member
13113 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13114 && !uses_template_parms (argvec))
13115 tsubst_default_arguments (r, complain);
13116 }
13117 else
13118 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13119
13120 /* Copy the list of befriending classes. */
13121 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13122 *friends;
13123 friends = &TREE_CHAIN (*friends))
13124 {
13125 *friends = copy_node (*friends);
13126 TREE_VALUE (*friends)
13127 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13128 }
13129
13130 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13131 {
13132 maybe_retrofit_in_chrg (r);
13133 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13134 return error_mark_node;
13135 /* If this is an instantiation of a member template, clone it.
13136 If it isn't, that'll be handled by
13137 clone_constructors_and_destructors. */
13138 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13139 clone_function_decl (r, /*update_methods=*/false);
13140 }
13141 else if ((complain & tf_error) != 0
13142 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13143 && !grok_op_properties (r, /*complain=*/true))
13144 return error_mark_node;
13145
13146 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13147 SET_DECL_FRIEND_CONTEXT (r,
13148 tsubst (DECL_FRIEND_CONTEXT (t),
13149 args, complain, in_decl));
13150
13151 /* Possibly limit visibility based on template args. */
13152 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13153 if (DECL_VISIBILITY_SPECIFIED (t))
13154 {
13155 DECL_VISIBILITY_SPECIFIED (r) = 0;
13156 DECL_ATTRIBUTES (r)
13157 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13158 }
13159 determine_visibility (r);
13160 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13161 && !processing_template_decl)
13162 defaulted_late_check (r);
13163
13164 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13165 args, complain, in_decl);
13166 return r;
13167 }
13168
13169 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13170
13171 static tree
13172 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13173 tree lambda_fntype)
13174 {
13175 /* We can get here when processing a member function template,
13176 member class template, or template template parameter. */
13177 tree decl = DECL_TEMPLATE_RESULT (t);
13178 tree in_decl = t;
13179 tree spec;
13180 tree tmpl_args;
13181 tree full_args;
13182 tree r;
13183 hashval_t hash = 0;
13184
13185 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13186 {
13187 /* Template template parameter is treated here. */
13188 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13189 if (new_type == error_mark_node)
13190 r = error_mark_node;
13191 /* If we get a real template back, return it. This can happen in
13192 the context of most_specialized_partial_spec. */
13193 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13194 r = new_type;
13195 else
13196 /* The new TEMPLATE_DECL was built in
13197 reduce_template_parm_level. */
13198 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13199 return r;
13200 }
13201
13202 if (!lambda_fntype)
13203 {
13204 /* We might already have an instance of this template.
13205 The ARGS are for the surrounding class type, so the
13206 full args contain the tsubst'd args for the context,
13207 plus the innermost args from the template decl. */
13208 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13209 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13210 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13211 /* Because this is a template, the arguments will still be
13212 dependent, even after substitution. If
13213 PROCESSING_TEMPLATE_DECL is not set, the dependency
13214 predicates will short-circuit. */
13215 ++processing_template_decl;
13216 full_args = tsubst_template_args (tmpl_args, args,
13217 complain, in_decl);
13218 --processing_template_decl;
13219 if (full_args == error_mark_node)
13220 return error_mark_node;
13221
13222 /* If this is a default template template argument,
13223 tsubst might not have changed anything. */
13224 if (full_args == tmpl_args)
13225 return t;
13226
13227 hash = hash_tmpl_and_args (t, full_args);
13228 spec = retrieve_specialization (t, full_args, hash);
13229 if (spec != NULL_TREE)
13230 return spec;
13231 }
13232
13233 /* Make a new template decl. It will be similar to the
13234 original, but will record the current template arguments.
13235 We also create a new function declaration, which is just
13236 like the old one, but points to this new template, rather
13237 than the old one. */
13238 r = copy_decl (t);
13239 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13240 DECL_CHAIN (r) = NULL_TREE;
13241
13242 // Build new template info linking to the original template decl.
13243 if (!lambda_fntype)
13244 {
13245 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13246 SET_DECL_IMPLICIT_INSTANTIATION (r);
13247 }
13248 else
13249 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13250
13251 /* The template parameters for this new template are all the
13252 template parameters for the old template, except the
13253 outermost level of parameters. */
13254 DECL_TEMPLATE_PARMS (r)
13255 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13256 complain);
13257
13258 if (TREE_CODE (decl) == TYPE_DECL
13259 && !TYPE_DECL_ALIAS_P (decl))
13260 {
13261 tree new_type;
13262 ++processing_template_decl;
13263 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13264 --processing_template_decl;
13265 if (new_type == error_mark_node)
13266 return error_mark_node;
13267
13268 TREE_TYPE (r) = new_type;
13269 /* For a partial specialization, we need to keep pointing to
13270 the primary template. */
13271 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13272 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13273 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13274 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13275 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13276 }
13277 else
13278 {
13279 tree new_decl;
13280 ++processing_template_decl;
13281 if (TREE_CODE (decl) == FUNCTION_DECL)
13282 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13283 else
13284 new_decl = tsubst (decl, args, complain, in_decl);
13285 --processing_template_decl;
13286 if (new_decl == error_mark_node)
13287 return error_mark_node;
13288
13289 DECL_TEMPLATE_RESULT (r) = new_decl;
13290 TREE_TYPE (r) = TREE_TYPE (new_decl);
13291 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13292 if (lambda_fntype)
13293 {
13294 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13295 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13296 }
13297 else
13298 {
13299 DECL_TI_TEMPLATE (new_decl) = r;
13300 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13301 }
13302 }
13303
13304 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13305 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13306
13307 if (PRIMARY_TEMPLATE_P (t))
13308 DECL_PRIMARY_TEMPLATE (r) = r;
13309
13310 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13311 && !lambda_fntype)
13312 /* Record this non-type partial instantiation. */
13313 register_specialization (r, t,
13314 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13315 false, hash);
13316
13317 return r;
13318 }
13319
13320 /* True if FN is the op() for a lambda in an uninstantiated template. */
13321
13322 bool
13323 lambda_fn_in_template_p (tree fn)
13324 {
13325 if (!fn || !LAMBDA_FUNCTION_P (fn))
13326 return false;
13327 tree closure = DECL_CONTEXT (fn);
13328 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13329 }
13330
13331 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13332 which the above is true. */
13333
13334 bool
13335 instantiated_lambda_fn_p (tree fn)
13336 {
13337 if (!fn || !LAMBDA_FUNCTION_P (fn))
13338 return false;
13339 tree closure = DECL_CONTEXT (fn);
13340 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13341 return LAMBDA_EXPR_INSTANTIATED (lam);
13342 }
13343
13344 /* We're instantiating a variable from template function TCTX. Return the
13345 corresponding current enclosing scope. This gets complicated because lambda
13346 functions in templates are regenerated rather than instantiated, but generic
13347 lambda functions are subsequently instantiated. */
13348
13349 static tree
13350 enclosing_instantiation_of (tree otctx)
13351 {
13352 tree tctx = otctx;
13353 tree fn = current_function_decl;
13354 int lambda_count = 0;
13355
13356 for (; tctx && lambda_fn_in_template_p (tctx);
13357 tctx = decl_function_context (tctx))
13358 ++lambda_count;
13359 for (; fn; fn = decl_function_context (fn))
13360 {
13361 tree ofn = fn;
13362 int flambda_count = 0;
13363 for (; fn && instantiated_lambda_fn_p (fn);
13364 fn = decl_function_context (fn))
13365 ++flambda_count;
13366 if ((fn && DECL_TEMPLATE_INFO (fn))
13367 ? most_general_template (fn) != most_general_template (tctx)
13368 : fn != tctx)
13369 continue;
13370 if (flambda_count != lambda_count)
13371 {
13372 gcc_assert (flambda_count > lambda_count);
13373 for (; flambda_count > lambda_count; --flambda_count)
13374 ofn = decl_function_context (ofn);
13375 }
13376 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13377 || DECL_CONV_FN_P (ofn));
13378 return ofn;
13379 }
13380 gcc_unreachable ();
13381 }
13382
13383 /* Substitute the ARGS into the T, which is a _DECL. Return the
13384 result of the substitution. Issue error and warning messages under
13385 control of COMPLAIN. */
13386
13387 static tree
13388 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13389 {
13390 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13391 location_t saved_loc;
13392 tree r = NULL_TREE;
13393 tree in_decl = t;
13394 hashval_t hash = 0;
13395
13396 /* Set the filename and linenumber to improve error-reporting. */
13397 saved_loc = input_location;
13398 input_location = DECL_SOURCE_LOCATION (t);
13399
13400 switch (TREE_CODE (t))
13401 {
13402 case TEMPLATE_DECL:
13403 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13404 break;
13405
13406 case FUNCTION_DECL:
13407 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13408 break;
13409
13410 case PARM_DECL:
13411 {
13412 tree type = NULL_TREE;
13413 int i, len = 1;
13414 tree expanded_types = NULL_TREE;
13415 tree prev_r = NULL_TREE;
13416 tree first_r = NULL_TREE;
13417
13418 if (DECL_PACK_P (t))
13419 {
13420 /* If there is a local specialization that isn't a
13421 parameter pack, it means that we're doing a "simple"
13422 substitution from inside tsubst_pack_expansion. Just
13423 return the local specialization (which will be a single
13424 parm). */
13425 tree spec = retrieve_local_specialization (t);
13426 if (spec
13427 && TREE_CODE (spec) == PARM_DECL
13428 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13429 RETURN (spec);
13430
13431 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13432 the parameters in this function parameter pack. */
13433 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13434 complain, in_decl);
13435 if (TREE_CODE (expanded_types) == TREE_VEC)
13436 {
13437 len = TREE_VEC_LENGTH (expanded_types);
13438
13439 /* Zero-length parameter packs are boring. Just substitute
13440 into the chain. */
13441 if (len == 0)
13442 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13443 TREE_CHAIN (t)));
13444 }
13445 else
13446 {
13447 /* All we did was update the type. Make a note of that. */
13448 type = expanded_types;
13449 expanded_types = NULL_TREE;
13450 }
13451 }
13452
13453 /* Loop through all of the parameters we'll build. When T is
13454 a function parameter pack, LEN is the number of expanded
13455 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13456 r = NULL_TREE;
13457 for (i = 0; i < len; ++i)
13458 {
13459 prev_r = r;
13460 r = copy_node (t);
13461 if (DECL_TEMPLATE_PARM_P (t))
13462 SET_DECL_TEMPLATE_PARM_P (r);
13463
13464 if (expanded_types)
13465 /* We're on the Ith parameter of the function parameter
13466 pack. */
13467 {
13468 /* Get the Ith type. */
13469 type = TREE_VEC_ELT (expanded_types, i);
13470
13471 /* Rename the parameter to include the index. */
13472 DECL_NAME (r)
13473 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13474 }
13475 else if (!type)
13476 /* We're dealing with a normal parameter. */
13477 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13478
13479 type = type_decays_to (type);
13480 TREE_TYPE (r) = type;
13481 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13482
13483 if (DECL_INITIAL (r))
13484 {
13485 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13486 DECL_INITIAL (r) = TREE_TYPE (r);
13487 else
13488 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13489 complain, in_decl);
13490 }
13491
13492 DECL_CONTEXT (r) = NULL_TREE;
13493
13494 if (!DECL_TEMPLATE_PARM_P (r))
13495 DECL_ARG_TYPE (r) = type_passed_as (type);
13496
13497 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13498 args, complain, in_decl);
13499
13500 /* Keep track of the first new parameter we
13501 generate. That's what will be returned to the
13502 caller. */
13503 if (!first_r)
13504 first_r = r;
13505
13506 /* Build a proper chain of parameters when substituting
13507 into a function parameter pack. */
13508 if (prev_r)
13509 DECL_CHAIN (prev_r) = r;
13510 }
13511
13512 /* If cp_unevaluated_operand is set, we're just looking for a
13513 single dummy parameter, so don't keep going. */
13514 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13515 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13516 complain, DECL_CHAIN (t));
13517
13518 /* FIRST_R contains the start of the chain we've built. */
13519 r = first_r;
13520 }
13521 break;
13522
13523 case FIELD_DECL:
13524 {
13525 tree type = NULL_TREE;
13526 tree vec = NULL_TREE;
13527 tree expanded_types = NULL_TREE;
13528 int len = 1;
13529
13530 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13531 {
13532 /* This field is a lambda capture pack. Return a TREE_VEC of
13533 the expanded fields to instantiate_class_template_1. */
13534 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13535 complain, in_decl);
13536 if (TREE_CODE (expanded_types) == TREE_VEC)
13537 {
13538 len = TREE_VEC_LENGTH (expanded_types);
13539 vec = make_tree_vec (len);
13540 }
13541 else
13542 {
13543 /* All we did was update the type. Make a note of that. */
13544 type = expanded_types;
13545 expanded_types = NULL_TREE;
13546 }
13547 }
13548
13549 for (int i = 0; i < len; ++i)
13550 {
13551 r = copy_decl (t);
13552 if (expanded_types)
13553 {
13554 type = TREE_VEC_ELT (expanded_types, i);
13555 DECL_NAME (r)
13556 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13557 }
13558 else if (!type)
13559 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13560
13561 if (type == error_mark_node)
13562 RETURN (error_mark_node);
13563 TREE_TYPE (r) = type;
13564 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13565
13566 if (DECL_C_BIT_FIELD (r))
13567 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13568 number of bits. */
13569 DECL_BIT_FIELD_REPRESENTATIVE (r)
13570 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13571 complain, in_decl,
13572 /*integral_constant_expression_p=*/true);
13573 if (DECL_INITIAL (t))
13574 {
13575 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13576 NSDMI in perform_member_init. Still set DECL_INITIAL
13577 so that we know there is one. */
13578 DECL_INITIAL (r) = void_node;
13579 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13580 retrofit_lang_decl (r);
13581 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13582 }
13583 /* We don't have to set DECL_CONTEXT here; it is set by
13584 finish_member_declaration. */
13585 DECL_CHAIN (r) = NULL_TREE;
13586
13587 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13588 args, complain, in_decl);
13589
13590 if (vec)
13591 TREE_VEC_ELT (vec, i) = r;
13592 }
13593
13594 if (vec)
13595 r = vec;
13596 }
13597 break;
13598
13599 case USING_DECL:
13600 /* We reach here only for member using decls. We also need to check
13601 uses_template_parms because DECL_DEPENDENT_P is not set for a
13602 using-declaration that designates a member of the current
13603 instantiation (c++/53549). */
13604 if (DECL_DEPENDENT_P (t)
13605 || uses_template_parms (USING_DECL_SCOPE (t)))
13606 {
13607 tree scope = USING_DECL_SCOPE (t);
13608 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13609 if (PACK_EXPANSION_P (scope))
13610 {
13611 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13612 int len = TREE_VEC_LENGTH (vec);
13613 r = make_tree_vec (len);
13614 for (int i = 0; i < len; ++i)
13615 {
13616 tree escope = TREE_VEC_ELT (vec, i);
13617 tree elt = do_class_using_decl (escope, name);
13618 if (!elt)
13619 {
13620 r = error_mark_node;
13621 break;
13622 }
13623 else
13624 {
13625 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13626 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13627 }
13628 TREE_VEC_ELT (r, i) = elt;
13629 }
13630 }
13631 else
13632 {
13633 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13634 complain, in_decl);
13635 r = do_class_using_decl (inst_scope, name);
13636 if (!r)
13637 r = error_mark_node;
13638 else
13639 {
13640 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13641 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13642 }
13643 }
13644 }
13645 else
13646 {
13647 r = copy_node (t);
13648 DECL_CHAIN (r) = NULL_TREE;
13649 }
13650 break;
13651
13652 case TYPE_DECL:
13653 case VAR_DECL:
13654 {
13655 tree argvec = NULL_TREE;
13656 tree gen_tmpl = NULL_TREE;
13657 tree spec;
13658 tree tmpl = NULL_TREE;
13659 tree ctx;
13660 tree type = NULL_TREE;
13661 bool local_p;
13662
13663 if (TREE_TYPE (t) == error_mark_node)
13664 RETURN (error_mark_node);
13665
13666 if (TREE_CODE (t) == TYPE_DECL
13667 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13668 {
13669 /* If this is the canonical decl, we don't have to
13670 mess with instantiations, and often we can't (for
13671 typename, template type parms and such). Note that
13672 TYPE_NAME is not correct for the above test if
13673 we've copied the type for a typedef. */
13674 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13675 if (type == error_mark_node)
13676 RETURN (error_mark_node);
13677 r = TYPE_NAME (type);
13678 break;
13679 }
13680
13681 /* Check to see if we already have the specialization we
13682 need. */
13683 spec = NULL_TREE;
13684 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13685 {
13686 /* T is a static data member or namespace-scope entity.
13687 We have to substitute into namespace-scope variables
13688 (not just variable templates) because of cases like:
13689
13690 template <class T> void f() { extern T t; }
13691
13692 where the entity referenced is not known until
13693 instantiation time. */
13694 local_p = false;
13695 ctx = DECL_CONTEXT (t);
13696 if (DECL_CLASS_SCOPE_P (t))
13697 {
13698 ctx = tsubst_aggr_type (ctx, args,
13699 complain,
13700 in_decl, /*entering_scope=*/1);
13701 /* If CTX is unchanged, then T is in fact the
13702 specialization we want. That situation occurs when
13703 referencing a static data member within in its own
13704 class. We can use pointer equality, rather than
13705 same_type_p, because DECL_CONTEXT is always
13706 canonical... */
13707 if (ctx == DECL_CONTEXT (t)
13708 /* ... unless T is a member template; in which
13709 case our caller can be willing to create a
13710 specialization of that template represented
13711 by T. */
13712 && !(DECL_TI_TEMPLATE (t)
13713 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13714 spec = t;
13715 }
13716
13717 if (!spec)
13718 {
13719 tmpl = DECL_TI_TEMPLATE (t);
13720 gen_tmpl = most_general_template (tmpl);
13721 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13722 if (argvec != error_mark_node)
13723 argvec = (coerce_innermost_template_parms
13724 (DECL_TEMPLATE_PARMS (gen_tmpl),
13725 argvec, t, complain,
13726 /*all*/true, /*defarg*/true));
13727 if (argvec == error_mark_node)
13728 RETURN (error_mark_node);
13729 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13730 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13731 }
13732 }
13733 else
13734 {
13735 /* A local variable. */
13736 local_p = true;
13737 /* Subsequent calls to pushdecl will fill this in. */
13738 ctx = NULL_TREE;
13739 /* Unless this is a reference to a static variable from an
13740 enclosing function, in which case we need to fill it in now. */
13741 if (TREE_STATIC (t))
13742 {
13743 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13744 if (fn != current_function_decl)
13745 ctx = fn;
13746 }
13747 spec = retrieve_local_specialization (t);
13748 }
13749 /* If we already have the specialization we need, there is
13750 nothing more to do. */
13751 if (spec)
13752 {
13753 r = spec;
13754 break;
13755 }
13756
13757 /* Create a new node for the specialization we need. */
13758 if (type == NULL_TREE)
13759 {
13760 if (is_typedef_decl (t))
13761 type = DECL_ORIGINAL_TYPE (t);
13762 else
13763 type = TREE_TYPE (t);
13764 if (VAR_P (t)
13765 && VAR_HAD_UNKNOWN_BOUND (t)
13766 && type != error_mark_node)
13767 type = strip_array_domain (type);
13768 tree sub_args = args;
13769 if (tree auto_node = type_uses_auto (type))
13770 {
13771 /* Mask off any template args past the variable's context so we
13772 don't replace the auto with an unrelated argument. */
13773 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13774 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13775 if (extra > 0)
13776 /* This should never happen with the new lambda instantiation
13777 model, but keep the handling just in case. */
13778 gcc_assert (!CHECKING_P),
13779 sub_args = strip_innermost_template_args (args, extra);
13780 }
13781 type = tsubst (type, sub_args, complain, in_decl);
13782 /* Substituting the type might have recursively instantiated this
13783 same alias (c++/86171). */
13784 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13785 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13786 {
13787 r = spec;
13788 break;
13789 }
13790 }
13791 r = copy_decl (t);
13792 if (VAR_P (r))
13793 {
13794 DECL_INITIALIZED_P (r) = 0;
13795 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13796 if (type == error_mark_node)
13797 RETURN (error_mark_node);
13798 if (TREE_CODE (type) == FUNCTION_TYPE)
13799 {
13800 /* It may seem that this case cannot occur, since:
13801
13802 typedef void f();
13803 void g() { f x; }
13804
13805 declares a function, not a variable. However:
13806
13807 typedef void f();
13808 template <typename T> void g() { T t; }
13809 template void g<f>();
13810
13811 is an attempt to declare a variable with function
13812 type. */
13813 error ("variable %qD has function type",
13814 /* R is not yet sufficiently initialized, so we
13815 just use its name. */
13816 DECL_NAME (r));
13817 RETURN (error_mark_node);
13818 }
13819 type = complete_type (type);
13820 /* Wait until cp_finish_decl to set this again, to handle
13821 circular dependency (template/instantiate6.C). */
13822 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13823 type = check_var_type (DECL_NAME (r), type);
13824
13825 if (DECL_HAS_VALUE_EXPR_P (t))
13826 {
13827 tree ve = DECL_VALUE_EXPR (t);
13828 ve = tsubst_expr (ve, args, complain, in_decl,
13829 /*constant_expression_p=*/false);
13830 if (REFERENCE_REF_P (ve))
13831 {
13832 gcc_assert (TYPE_REF_P (type));
13833 ve = TREE_OPERAND (ve, 0);
13834 }
13835 SET_DECL_VALUE_EXPR (r, ve);
13836 }
13837 if (CP_DECL_THREAD_LOCAL_P (r)
13838 && !processing_template_decl)
13839 set_decl_tls_model (r, decl_default_tls_model (r));
13840 }
13841 else if (DECL_SELF_REFERENCE_P (t))
13842 SET_DECL_SELF_REFERENCE_P (r);
13843 TREE_TYPE (r) = type;
13844 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13845 DECL_CONTEXT (r) = ctx;
13846 /* Clear out the mangled name and RTL for the instantiation. */
13847 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13848 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13849 SET_DECL_RTL (r, NULL);
13850 /* The initializer must not be expanded until it is required;
13851 see [temp.inst]. */
13852 DECL_INITIAL (r) = NULL_TREE;
13853 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13854 if (VAR_P (r))
13855 {
13856 if (DECL_LANG_SPECIFIC (r))
13857 SET_DECL_DEPENDENT_INIT_P (r, false);
13858
13859 SET_DECL_MODE (r, VOIDmode);
13860
13861 /* Possibly limit visibility based on template args. */
13862 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13863 if (DECL_VISIBILITY_SPECIFIED (t))
13864 {
13865 DECL_VISIBILITY_SPECIFIED (r) = 0;
13866 DECL_ATTRIBUTES (r)
13867 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13868 }
13869 determine_visibility (r);
13870 }
13871
13872 if (!local_p)
13873 {
13874 /* A static data member declaration is always marked
13875 external when it is declared in-class, even if an
13876 initializer is present. We mimic the non-template
13877 processing here. */
13878 DECL_EXTERNAL (r) = 1;
13879 if (DECL_NAMESPACE_SCOPE_P (t))
13880 DECL_NOT_REALLY_EXTERN (r) = 1;
13881
13882 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13883 SET_DECL_IMPLICIT_INSTANTIATION (r);
13884 register_specialization (r, gen_tmpl, argvec, false, hash);
13885 }
13886 else
13887 {
13888 if (DECL_LANG_SPECIFIC (r))
13889 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13890 if (!cp_unevaluated_operand)
13891 register_local_specialization (r, t);
13892 }
13893
13894 DECL_CHAIN (r) = NULL_TREE;
13895
13896 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13897 /*flags=*/0,
13898 args, complain, in_decl);
13899
13900 /* Preserve a typedef that names a type. */
13901 if (is_typedef_decl (r) && type != error_mark_node)
13902 {
13903 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13904 set_underlying_type (r);
13905 if (TYPE_DECL_ALIAS_P (r))
13906 /* An alias template specialization can be dependent
13907 even if its underlying type is not. */
13908 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13909 }
13910
13911 layout_decl (r, 0);
13912 }
13913 break;
13914
13915 default:
13916 gcc_unreachable ();
13917 }
13918 #undef RETURN
13919
13920 out:
13921 /* Restore the file and line information. */
13922 input_location = saved_loc;
13923
13924 return r;
13925 }
13926
13927 /* Substitute into the ARG_TYPES of a function type.
13928 If END is a TREE_CHAIN, leave it and any following types
13929 un-substituted. */
13930
13931 static tree
13932 tsubst_arg_types (tree arg_types,
13933 tree args,
13934 tree end,
13935 tsubst_flags_t complain,
13936 tree in_decl)
13937 {
13938 tree remaining_arg_types;
13939 tree type = NULL_TREE;
13940 int i = 1;
13941 tree expanded_args = NULL_TREE;
13942 tree default_arg;
13943
13944 if (!arg_types || arg_types == void_list_node || arg_types == end)
13945 return arg_types;
13946
13947 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13948 args, end, complain, in_decl);
13949 if (remaining_arg_types == error_mark_node)
13950 return error_mark_node;
13951
13952 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13953 {
13954 /* For a pack expansion, perform substitution on the
13955 entire expression. Later on, we'll handle the arguments
13956 one-by-one. */
13957 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13958 args, complain, in_decl);
13959
13960 if (TREE_CODE (expanded_args) == TREE_VEC)
13961 /* So that we'll spin through the parameters, one by one. */
13962 i = TREE_VEC_LENGTH (expanded_args);
13963 else
13964 {
13965 /* We only partially substituted into the parameter
13966 pack. Our type is TYPE_PACK_EXPANSION. */
13967 type = expanded_args;
13968 expanded_args = NULL_TREE;
13969 }
13970 }
13971
13972 while (i > 0) {
13973 --i;
13974
13975 if (expanded_args)
13976 type = TREE_VEC_ELT (expanded_args, i);
13977 else if (!type)
13978 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13979
13980 if (type == error_mark_node)
13981 return error_mark_node;
13982 if (VOID_TYPE_P (type))
13983 {
13984 if (complain & tf_error)
13985 {
13986 error ("invalid parameter type %qT", type);
13987 if (in_decl)
13988 error ("in declaration %q+D", in_decl);
13989 }
13990 return error_mark_node;
13991 }
13992 /* DR 657. */
13993 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13994 return error_mark_node;
13995
13996 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13997 top-level qualifiers as required. */
13998 type = cv_unqualified (type_decays_to (type));
13999
14000 /* We do not substitute into default arguments here. The standard
14001 mandates that they be instantiated only when needed, which is
14002 done in build_over_call. */
14003 default_arg = TREE_PURPOSE (arg_types);
14004
14005 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14006 since the new op() won't have any associated template arguments for us
14007 to refer to later. */
14008 if (lambda_fn_in_template_p (in_decl))
14009 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14010 false/*fn*/, false/*constexpr*/);
14011
14012 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
14013 {
14014 /* We've instantiated a template before its default arguments
14015 have been parsed. This can happen for a nested template
14016 class, and is not an error unless we require the default
14017 argument in a call of this function. */
14018 remaining_arg_types =
14019 tree_cons (default_arg, type, remaining_arg_types);
14020 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
14021 }
14022 else
14023 remaining_arg_types =
14024 hash_tree_cons (default_arg, type, remaining_arg_types);
14025 }
14026
14027 return remaining_arg_types;
14028 }
14029
14030 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14031 *not* handle the exception-specification for FNTYPE, because the
14032 initial substitution of explicitly provided template parameters
14033 during argument deduction forbids substitution into the
14034 exception-specification:
14035
14036 [temp.deduct]
14037
14038 All references in the function type of the function template to the
14039 corresponding template parameters are replaced by the specified tem-
14040 plate argument values. If a substitution in a template parameter or
14041 in the function type of the function template results in an invalid
14042 type, type deduction fails. [Note: The equivalent substitution in
14043 exception specifications is done only when the function is instanti-
14044 ated, at which point a program is ill-formed if the substitution
14045 results in an invalid type.] */
14046
14047 static tree
14048 tsubst_function_type (tree t,
14049 tree args,
14050 tsubst_flags_t complain,
14051 tree in_decl)
14052 {
14053 tree return_type;
14054 tree arg_types = NULL_TREE;
14055 tree fntype;
14056
14057 /* The TYPE_CONTEXT is not used for function/method types. */
14058 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14059
14060 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14061 failure. */
14062 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14063
14064 if (late_return_type_p)
14065 {
14066 /* Substitute the argument types. */
14067 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14068 complain, in_decl);
14069 if (arg_types == error_mark_node)
14070 return error_mark_node;
14071
14072 tree save_ccp = current_class_ptr;
14073 tree save_ccr = current_class_ref;
14074 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14075 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14076 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14077 if (do_inject)
14078 {
14079 /* DR 1207: 'this' is in scope in the trailing return type. */
14080 inject_this_parameter (this_type, cp_type_quals (this_type));
14081 }
14082
14083 /* Substitute the return type. */
14084 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14085
14086 if (do_inject)
14087 {
14088 current_class_ptr = save_ccp;
14089 current_class_ref = save_ccr;
14090 }
14091 }
14092 else
14093 /* Substitute the return type. */
14094 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14095
14096 if (return_type == error_mark_node)
14097 return error_mark_node;
14098 /* DR 486 clarifies that creation of a function type with an
14099 invalid return type is a deduction failure. */
14100 if (TREE_CODE (return_type) == ARRAY_TYPE
14101 || TREE_CODE (return_type) == FUNCTION_TYPE)
14102 {
14103 if (complain & tf_error)
14104 {
14105 if (TREE_CODE (return_type) == ARRAY_TYPE)
14106 error ("function returning an array");
14107 else
14108 error ("function returning a function");
14109 }
14110 return error_mark_node;
14111 }
14112 /* And DR 657. */
14113 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14114 return error_mark_node;
14115
14116 if (!late_return_type_p)
14117 {
14118 /* Substitute the argument types. */
14119 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14120 complain, in_decl);
14121 if (arg_types == error_mark_node)
14122 return error_mark_node;
14123 }
14124
14125 /* Construct a new type node and return it. */
14126 if (TREE_CODE (t) == FUNCTION_TYPE)
14127 {
14128 fntype = build_function_type (return_type, arg_types);
14129 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14130 }
14131 else
14132 {
14133 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14134 /* Don't pick up extra function qualifiers from the basetype. */
14135 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14136 if (! MAYBE_CLASS_TYPE_P (r))
14137 {
14138 /* [temp.deduct]
14139
14140 Type deduction may fail for any of the following
14141 reasons:
14142
14143 -- Attempting to create "pointer to member of T" when T
14144 is not a class type. */
14145 if (complain & tf_error)
14146 error ("creating pointer to member function of non-class type %qT",
14147 r);
14148 return error_mark_node;
14149 }
14150
14151 fntype = build_method_type_directly (r, return_type,
14152 TREE_CHAIN (arg_types));
14153 }
14154 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14155
14156 /* See comment above. */
14157 tree raises = NULL_TREE;
14158 cp_ref_qualifier rqual = type_memfn_rqual (t);
14159 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14160
14161 return fntype;
14162 }
14163
14164 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14165 ARGS into that specification, and return the substituted
14166 specification. If there is no specification, return NULL_TREE. */
14167
14168 static tree
14169 tsubst_exception_specification (tree fntype,
14170 tree args,
14171 tsubst_flags_t complain,
14172 tree in_decl,
14173 bool defer_ok)
14174 {
14175 tree specs;
14176 tree new_specs;
14177
14178 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14179 new_specs = NULL_TREE;
14180 if (specs && TREE_PURPOSE (specs))
14181 {
14182 /* A noexcept-specifier. */
14183 tree expr = TREE_PURPOSE (specs);
14184 if (TREE_CODE (expr) == INTEGER_CST)
14185 new_specs = expr;
14186 else if (defer_ok)
14187 {
14188 /* Defer instantiation of noexcept-specifiers to avoid
14189 excessive instantiations (c++/49107). */
14190 new_specs = make_node (DEFERRED_NOEXCEPT);
14191 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14192 {
14193 /* We already partially instantiated this member template,
14194 so combine the new args with the old. */
14195 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14196 = DEFERRED_NOEXCEPT_PATTERN (expr);
14197 DEFERRED_NOEXCEPT_ARGS (new_specs)
14198 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14199 }
14200 else
14201 {
14202 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14203 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14204 }
14205 }
14206 else
14207 {
14208 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14209 {
14210 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14211 args);
14212 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14213 }
14214 new_specs = tsubst_copy_and_build
14215 (expr, args, complain, in_decl, /*function_p=*/false,
14216 /*integral_constant_expression_p=*/true);
14217 }
14218 new_specs = build_noexcept_spec (new_specs, complain);
14219 }
14220 else if (specs)
14221 {
14222 if (! TREE_VALUE (specs))
14223 new_specs = specs;
14224 else
14225 while (specs)
14226 {
14227 tree spec;
14228 int i, len = 1;
14229 tree expanded_specs = NULL_TREE;
14230
14231 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14232 {
14233 /* Expand the pack expansion type. */
14234 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14235 args, complain,
14236 in_decl);
14237
14238 if (expanded_specs == error_mark_node)
14239 return error_mark_node;
14240 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14241 len = TREE_VEC_LENGTH (expanded_specs);
14242 else
14243 {
14244 /* We're substituting into a member template, so
14245 we got a TYPE_PACK_EXPANSION back. Add that
14246 expansion and move on. */
14247 gcc_assert (TREE_CODE (expanded_specs)
14248 == TYPE_PACK_EXPANSION);
14249 new_specs = add_exception_specifier (new_specs,
14250 expanded_specs,
14251 complain);
14252 specs = TREE_CHAIN (specs);
14253 continue;
14254 }
14255 }
14256
14257 for (i = 0; i < len; ++i)
14258 {
14259 if (expanded_specs)
14260 spec = TREE_VEC_ELT (expanded_specs, i);
14261 else
14262 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14263 if (spec == error_mark_node)
14264 return spec;
14265 new_specs = add_exception_specifier (new_specs, spec,
14266 complain);
14267 }
14268
14269 specs = TREE_CHAIN (specs);
14270 }
14271 }
14272 return new_specs;
14273 }
14274
14275 /* Take the tree structure T and replace template parameters used
14276 therein with the argument vector ARGS. IN_DECL is an associated
14277 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14278 Issue error and warning messages under control of COMPLAIN. Note
14279 that we must be relatively non-tolerant of extensions here, in
14280 order to preserve conformance; if we allow substitutions that
14281 should not be allowed, we may allow argument deductions that should
14282 not succeed, and therefore report ambiguous overload situations
14283 where there are none. In theory, we could allow the substitution,
14284 but indicate that it should have failed, and allow our caller to
14285 make sure that the right thing happens, but we don't try to do this
14286 yet.
14287
14288 This function is used for dealing with types, decls and the like;
14289 for expressions, use tsubst_expr or tsubst_copy. */
14290
14291 tree
14292 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14293 {
14294 enum tree_code code;
14295 tree type, r = NULL_TREE;
14296
14297 if (t == NULL_TREE || t == error_mark_node
14298 || t == integer_type_node
14299 || t == void_type_node
14300 || t == char_type_node
14301 || t == unknown_type_node
14302 || TREE_CODE (t) == NAMESPACE_DECL
14303 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14304 return t;
14305
14306 if (DECL_P (t))
14307 return tsubst_decl (t, args, complain);
14308
14309 if (args == NULL_TREE)
14310 return t;
14311
14312 code = TREE_CODE (t);
14313
14314 if (code == IDENTIFIER_NODE)
14315 type = IDENTIFIER_TYPE_VALUE (t);
14316 else
14317 type = TREE_TYPE (t);
14318
14319 gcc_assert (type != unknown_type_node);
14320
14321 /* Reuse typedefs. We need to do this to handle dependent attributes,
14322 such as attribute aligned. */
14323 if (TYPE_P (t)
14324 && typedef_variant_p (t))
14325 {
14326 tree decl = TYPE_NAME (t);
14327
14328 if (alias_template_specialization_p (t))
14329 {
14330 /* DECL represents an alias template and we want to
14331 instantiate it. */
14332 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14333 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14334 r = instantiate_alias_template (tmpl, gen_args, complain);
14335 }
14336 else if (DECL_CLASS_SCOPE_P (decl)
14337 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14338 && uses_template_parms (DECL_CONTEXT (decl)))
14339 {
14340 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14341 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14342 r = retrieve_specialization (tmpl, gen_args, 0);
14343 }
14344 else if (DECL_FUNCTION_SCOPE_P (decl)
14345 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14346 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14347 r = retrieve_local_specialization (decl);
14348 else
14349 /* The typedef is from a non-template context. */
14350 return t;
14351
14352 if (r)
14353 {
14354 r = TREE_TYPE (r);
14355 r = cp_build_qualified_type_real
14356 (r, cp_type_quals (t) | cp_type_quals (r),
14357 complain | tf_ignore_bad_quals);
14358 return r;
14359 }
14360 else
14361 {
14362 /* We don't have an instantiation yet, so drop the typedef. */
14363 int quals = cp_type_quals (t);
14364 t = DECL_ORIGINAL_TYPE (decl);
14365 t = cp_build_qualified_type_real (t, quals,
14366 complain | tf_ignore_bad_quals);
14367 }
14368 }
14369
14370 bool fndecl_type = (complain & tf_fndecl_type);
14371 complain &= ~tf_fndecl_type;
14372
14373 if (type
14374 && code != TYPENAME_TYPE
14375 && code != TEMPLATE_TYPE_PARM
14376 && code != TEMPLATE_PARM_INDEX
14377 && code != IDENTIFIER_NODE
14378 && code != FUNCTION_TYPE
14379 && code != METHOD_TYPE)
14380 type = tsubst (type, args, complain, in_decl);
14381 if (type == error_mark_node)
14382 return error_mark_node;
14383
14384 switch (code)
14385 {
14386 case RECORD_TYPE:
14387 case UNION_TYPE:
14388 case ENUMERAL_TYPE:
14389 return tsubst_aggr_type (t, args, complain, in_decl,
14390 /*entering_scope=*/0);
14391
14392 case ERROR_MARK:
14393 case IDENTIFIER_NODE:
14394 case VOID_TYPE:
14395 case REAL_TYPE:
14396 case COMPLEX_TYPE:
14397 case VECTOR_TYPE:
14398 case BOOLEAN_TYPE:
14399 case NULLPTR_TYPE:
14400 case LANG_TYPE:
14401 return t;
14402
14403 case INTEGER_TYPE:
14404 if (t == integer_type_node)
14405 return t;
14406
14407 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14408 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14409 return t;
14410
14411 {
14412 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14413
14414 max = tsubst_expr (omax, args, complain, in_decl,
14415 /*integral_constant_expression_p=*/false);
14416
14417 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14418 needed. */
14419 if (TREE_CODE (max) == NOP_EXPR
14420 && TREE_SIDE_EFFECTS (omax)
14421 && !TREE_TYPE (max))
14422 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14423
14424 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14425 with TREE_SIDE_EFFECTS that indicates this is not an integral
14426 constant expression. */
14427 if (processing_template_decl
14428 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14429 {
14430 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14431 TREE_SIDE_EFFECTS (max) = 1;
14432 }
14433
14434 return compute_array_index_type (NULL_TREE, max, complain);
14435 }
14436
14437 case TEMPLATE_TYPE_PARM:
14438 case TEMPLATE_TEMPLATE_PARM:
14439 case BOUND_TEMPLATE_TEMPLATE_PARM:
14440 case TEMPLATE_PARM_INDEX:
14441 {
14442 int idx;
14443 int level;
14444 int levels;
14445 tree arg = NULL_TREE;
14446
14447 /* Early in template argument deduction substitution, we don't
14448 want to reduce the level of 'auto', or it will be confused
14449 with a normal template parm in subsequent deduction. */
14450 if (is_auto (t) && (complain & tf_partial))
14451 return t;
14452
14453 r = NULL_TREE;
14454
14455 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14456 template_parm_level_and_index (t, &level, &idx);
14457
14458 levels = TMPL_ARGS_DEPTH (args);
14459 if (level <= levels
14460 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14461 {
14462 arg = TMPL_ARG (args, level, idx);
14463
14464 /* See through ARGUMENT_PACK_SELECT arguments. */
14465 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14466 arg = argument_pack_select_arg (arg);
14467 }
14468
14469 if (arg == error_mark_node)
14470 return error_mark_node;
14471 else if (arg != NULL_TREE)
14472 {
14473 if (ARGUMENT_PACK_P (arg))
14474 /* If ARG is an argument pack, we don't actually want to
14475 perform a substitution here, because substitutions
14476 for argument packs are only done
14477 element-by-element. We can get to this point when
14478 substituting the type of a non-type template
14479 parameter pack, when that type actually contains
14480 template parameter packs from an outer template, e.g.,
14481
14482 template<typename... Types> struct A {
14483 template<Types... Values> struct B { };
14484 }; */
14485 return t;
14486
14487 if (code == TEMPLATE_TYPE_PARM)
14488 {
14489 int quals;
14490 gcc_assert (TYPE_P (arg));
14491
14492 quals = cp_type_quals (arg) | cp_type_quals (t);
14493
14494 return cp_build_qualified_type_real
14495 (arg, quals, complain | tf_ignore_bad_quals);
14496 }
14497 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14498 {
14499 /* We are processing a type constructed from a
14500 template template parameter. */
14501 tree argvec = tsubst (TYPE_TI_ARGS (t),
14502 args, complain, in_decl);
14503 if (argvec == error_mark_node)
14504 return error_mark_node;
14505
14506 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14507 || TREE_CODE (arg) == TEMPLATE_DECL
14508 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14509
14510 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14511 /* Consider this code:
14512
14513 template <template <class> class Template>
14514 struct Internal {
14515 template <class Arg> using Bind = Template<Arg>;
14516 };
14517
14518 template <template <class> class Template, class Arg>
14519 using Instantiate = Template<Arg>; //#0
14520
14521 template <template <class> class Template,
14522 class Argument>
14523 using Bind =
14524 Instantiate<Internal<Template>::template Bind,
14525 Argument>; //#1
14526
14527 When #1 is parsed, the
14528 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14529 parameter `Template' in #0 matches the
14530 UNBOUND_CLASS_TEMPLATE representing the argument
14531 `Internal<Template>::template Bind'; We then want
14532 to assemble the type `Bind<Argument>' that can't
14533 be fully created right now, because
14534 `Internal<Template>' not being complete, the Bind
14535 template cannot be looked up in that context. So
14536 we need to "store" `Bind<Argument>' for later
14537 when the context of Bind becomes complete. Let's
14538 store that in a TYPENAME_TYPE. */
14539 return make_typename_type (TYPE_CONTEXT (arg),
14540 build_nt (TEMPLATE_ID_EXPR,
14541 TYPE_IDENTIFIER (arg),
14542 argvec),
14543 typename_type,
14544 complain);
14545
14546 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14547 are resolving nested-types in the signature of a
14548 member function templates. Otherwise ARG is a
14549 TEMPLATE_DECL and is the real template to be
14550 instantiated. */
14551 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14552 arg = TYPE_NAME (arg);
14553
14554 r = lookup_template_class (arg,
14555 argvec, in_decl,
14556 DECL_CONTEXT (arg),
14557 /*entering_scope=*/0,
14558 complain);
14559 return cp_build_qualified_type_real
14560 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14561 }
14562 else if (code == TEMPLATE_TEMPLATE_PARM)
14563 return arg;
14564 else
14565 /* TEMPLATE_PARM_INDEX. */
14566 return convert_from_reference (unshare_expr (arg));
14567 }
14568
14569 if (level == 1)
14570 /* This can happen during the attempted tsubst'ing in
14571 unify. This means that we don't yet have any information
14572 about the template parameter in question. */
14573 return t;
14574
14575 /* If we get here, we must have been looking at a parm for a
14576 more deeply nested template. Make a new version of this
14577 template parameter, but with a lower level. */
14578 switch (code)
14579 {
14580 case TEMPLATE_TYPE_PARM:
14581 case TEMPLATE_TEMPLATE_PARM:
14582 case BOUND_TEMPLATE_TEMPLATE_PARM:
14583 if (cp_type_quals (t))
14584 {
14585 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14586 r = cp_build_qualified_type_real
14587 (r, cp_type_quals (t),
14588 complain | (code == TEMPLATE_TYPE_PARM
14589 ? tf_ignore_bad_quals : 0));
14590 }
14591 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14592 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14593 && (r = (TEMPLATE_PARM_DESCENDANTS
14594 (TEMPLATE_TYPE_PARM_INDEX (t))))
14595 && (r = TREE_TYPE (r))
14596 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14597 /* Break infinite recursion when substituting the constraints
14598 of a constrained placeholder. */;
14599 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14600 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14601 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14602 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14603 r = TEMPLATE_PARM_DESCENDANTS (arg))
14604 && (TEMPLATE_PARM_LEVEL (r)
14605 == TEMPLATE_PARM_LEVEL (arg) - levels))
14606 /* Cache the simple case of lowering a type parameter. */
14607 r = TREE_TYPE (r);
14608 else
14609 {
14610 r = copy_type (t);
14611 TEMPLATE_TYPE_PARM_INDEX (r)
14612 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14613 r, levels, args, complain);
14614 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14615 TYPE_MAIN_VARIANT (r) = r;
14616 TYPE_POINTER_TO (r) = NULL_TREE;
14617 TYPE_REFERENCE_TO (r) = NULL_TREE;
14618
14619 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14620 {
14621 /* Propagate constraints on placeholders. */
14622 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14623 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14624 = tsubst_constraint (constr, args, complain, in_decl);
14625 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14626 {
14627 pl = tsubst_copy (pl, args, complain, in_decl);
14628 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14629 }
14630 }
14631
14632 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14633 /* We have reduced the level of the template
14634 template parameter, but not the levels of its
14635 template parameters, so canonical_type_parameter
14636 will not be able to find the canonical template
14637 template parameter for this level. Thus, we
14638 require structural equality checking to compare
14639 TEMPLATE_TEMPLATE_PARMs. */
14640 SET_TYPE_STRUCTURAL_EQUALITY (r);
14641 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14642 SET_TYPE_STRUCTURAL_EQUALITY (r);
14643 else
14644 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14645
14646 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14647 {
14648 tree tinfo = TYPE_TEMPLATE_INFO (t);
14649 /* We might need to substitute into the types of non-type
14650 template parameters. */
14651 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14652 complain, in_decl);
14653 if (tmpl == error_mark_node)
14654 return error_mark_node;
14655 tree argvec = tsubst (TI_ARGS (tinfo), args,
14656 complain, in_decl);
14657 if (argvec == error_mark_node)
14658 return error_mark_node;
14659
14660 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14661 = build_template_info (tmpl, argvec);
14662 }
14663 }
14664 break;
14665
14666 case TEMPLATE_PARM_INDEX:
14667 /* OK, now substitute the type of the non-type parameter. We
14668 couldn't do it earlier because it might be an auto parameter,
14669 and we wouldn't need to if we had an argument. */
14670 type = tsubst (type, args, complain, in_decl);
14671 if (type == error_mark_node)
14672 return error_mark_node;
14673 r = reduce_template_parm_level (t, type, levels, args, complain);
14674 break;
14675
14676 default:
14677 gcc_unreachable ();
14678 }
14679
14680 return r;
14681 }
14682
14683 case TREE_LIST:
14684 {
14685 tree purpose, value, chain;
14686
14687 if (t == void_list_node)
14688 return t;
14689
14690 purpose = TREE_PURPOSE (t);
14691 if (purpose)
14692 {
14693 purpose = tsubst (purpose, args, complain, in_decl);
14694 if (purpose == error_mark_node)
14695 return error_mark_node;
14696 }
14697 value = TREE_VALUE (t);
14698 if (value)
14699 {
14700 value = tsubst (value, args, complain, in_decl);
14701 if (value == error_mark_node)
14702 return error_mark_node;
14703 }
14704 chain = TREE_CHAIN (t);
14705 if (chain && chain != void_type_node)
14706 {
14707 chain = tsubst (chain, args, complain, in_decl);
14708 if (chain == error_mark_node)
14709 return error_mark_node;
14710 }
14711 if (purpose == TREE_PURPOSE (t)
14712 && value == TREE_VALUE (t)
14713 && chain == TREE_CHAIN (t))
14714 return t;
14715 return hash_tree_cons (purpose, value, chain);
14716 }
14717
14718 case TREE_BINFO:
14719 /* We should never be tsubsting a binfo. */
14720 gcc_unreachable ();
14721
14722 case TREE_VEC:
14723 /* A vector of template arguments. */
14724 gcc_assert (!type);
14725 return tsubst_template_args (t, args, complain, in_decl);
14726
14727 case POINTER_TYPE:
14728 case REFERENCE_TYPE:
14729 {
14730 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14731 return t;
14732
14733 /* [temp.deduct]
14734
14735 Type deduction may fail for any of the following
14736 reasons:
14737
14738 -- Attempting to create a pointer to reference type.
14739 -- Attempting to create a reference to a reference type or
14740 a reference to void.
14741
14742 Core issue 106 says that creating a reference to a reference
14743 during instantiation is no longer a cause for failure. We
14744 only enforce this check in strict C++98 mode. */
14745 if ((TYPE_REF_P (type)
14746 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14747 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14748 {
14749 static location_t last_loc;
14750
14751 /* We keep track of the last time we issued this error
14752 message to avoid spewing a ton of messages during a
14753 single bad template instantiation. */
14754 if (complain & tf_error
14755 && last_loc != input_location)
14756 {
14757 if (VOID_TYPE_P (type))
14758 error ("forming reference to void");
14759 else if (code == POINTER_TYPE)
14760 error ("forming pointer to reference type %qT", type);
14761 else
14762 error ("forming reference to reference type %qT", type);
14763 last_loc = input_location;
14764 }
14765
14766 return error_mark_node;
14767 }
14768 else if (TREE_CODE (type) == FUNCTION_TYPE
14769 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14770 || type_memfn_rqual (type) != REF_QUAL_NONE))
14771 {
14772 if (complain & tf_error)
14773 {
14774 if (code == POINTER_TYPE)
14775 error ("forming pointer to qualified function type %qT",
14776 type);
14777 else
14778 error ("forming reference to qualified function type %qT",
14779 type);
14780 }
14781 return error_mark_node;
14782 }
14783 else if (code == POINTER_TYPE)
14784 {
14785 r = build_pointer_type (type);
14786 if (TREE_CODE (type) == METHOD_TYPE)
14787 r = build_ptrmemfunc_type (r);
14788 }
14789 else if (TYPE_REF_P (type))
14790 /* In C++0x, during template argument substitution, when there is an
14791 attempt to create a reference to a reference type, reference
14792 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14793
14794 "If a template-argument for a template-parameter T names a type
14795 that is a reference to a type A, an attempt to create the type
14796 'lvalue reference to cv T' creates the type 'lvalue reference to
14797 A,' while an attempt to create the type type rvalue reference to
14798 cv T' creates the type T"
14799 */
14800 r = cp_build_reference_type
14801 (TREE_TYPE (type),
14802 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14803 else
14804 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14805 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14806
14807 if (r != error_mark_node)
14808 /* Will this ever be needed for TYPE_..._TO values? */
14809 layout_type (r);
14810
14811 return r;
14812 }
14813 case OFFSET_TYPE:
14814 {
14815 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14816 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14817 {
14818 /* [temp.deduct]
14819
14820 Type deduction may fail for any of the following
14821 reasons:
14822
14823 -- Attempting to create "pointer to member of T" when T
14824 is not a class type. */
14825 if (complain & tf_error)
14826 error ("creating pointer to member of non-class type %qT", r);
14827 return error_mark_node;
14828 }
14829 if (TYPE_REF_P (type))
14830 {
14831 if (complain & tf_error)
14832 error ("creating pointer to member reference type %qT", type);
14833 return error_mark_node;
14834 }
14835 if (VOID_TYPE_P (type))
14836 {
14837 if (complain & tf_error)
14838 error ("creating pointer to member of type void");
14839 return error_mark_node;
14840 }
14841 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14842 if (TREE_CODE (type) == FUNCTION_TYPE)
14843 {
14844 /* The type of the implicit object parameter gets its
14845 cv-qualifiers from the FUNCTION_TYPE. */
14846 tree memptr;
14847 tree method_type
14848 = build_memfn_type (type, r, type_memfn_quals (type),
14849 type_memfn_rqual (type));
14850 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14851 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14852 complain);
14853 }
14854 else
14855 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14856 cp_type_quals (t),
14857 complain);
14858 }
14859 case FUNCTION_TYPE:
14860 case METHOD_TYPE:
14861 {
14862 tree fntype;
14863 tree specs;
14864 fntype = tsubst_function_type (t, args, complain, in_decl);
14865 if (fntype == error_mark_node)
14866 return error_mark_node;
14867
14868 /* Substitute the exception specification. */
14869 specs = tsubst_exception_specification (t, args, complain, in_decl,
14870 /*defer_ok*/fndecl_type);
14871 if (specs == error_mark_node)
14872 return error_mark_node;
14873 if (specs)
14874 fntype = build_exception_variant (fntype, specs);
14875 return fntype;
14876 }
14877 case ARRAY_TYPE:
14878 {
14879 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14880 if (domain == error_mark_node)
14881 return error_mark_node;
14882
14883 /* As an optimization, we avoid regenerating the array type if
14884 it will obviously be the same as T. */
14885 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14886 return t;
14887
14888 /* These checks should match the ones in create_array_type_for_decl.
14889
14890 [temp.deduct]
14891
14892 The deduction may fail for any of the following reasons:
14893
14894 -- Attempting to create an array with an element type that
14895 is void, a function type, or a reference type, or [DR337]
14896 an abstract class type. */
14897 if (VOID_TYPE_P (type)
14898 || TREE_CODE (type) == FUNCTION_TYPE
14899 || (TREE_CODE (type) == ARRAY_TYPE
14900 && TYPE_DOMAIN (type) == NULL_TREE)
14901 || TYPE_REF_P (type))
14902 {
14903 if (complain & tf_error)
14904 error ("creating array of %qT", type);
14905 return error_mark_node;
14906 }
14907
14908 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14909 return error_mark_node;
14910
14911 r = build_cplus_array_type (type, domain);
14912
14913 if (!valid_array_size_p (input_location, r, in_decl,
14914 (complain & tf_error)))
14915 return error_mark_node;
14916
14917 if (TYPE_USER_ALIGN (t))
14918 {
14919 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14920 TYPE_USER_ALIGN (r) = 1;
14921 }
14922
14923 return r;
14924 }
14925
14926 case TYPENAME_TYPE:
14927 {
14928 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14929 in_decl, /*entering_scope=*/1);
14930 if (ctx == error_mark_node)
14931 return error_mark_node;
14932
14933 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14934 complain, in_decl);
14935 if (f == error_mark_node)
14936 return error_mark_node;
14937
14938 if (!MAYBE_CLASS_TYPE_P (ctx))
14939 {
14940 if (complain & tf_error)
14941 error ("%qT is not a class, struct, or union type", ctx);
14942 return error_mark_node;
14943 }
14944 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14945 {
14946 /* Normally, make_typename_type does not require that the CTX
14947 have complete type in order to allow things like:
14948
14949 template <class T> struct S { typename S<T>::X Y; };
14950
14951 But, such constructs have already been resolved by this
14952 point, so here CTX really should have complete type, unless
14953 it's a partial instantiation. */
14954 ctx = complete_type (ctx);
14955 if (!COMPLETE_TYPE_P (ctx))
14956 {
14957 if (complain & tf_error)
14958 cxx_incomplete_type_error (NULL_TREE, ctx);
14959 return error_mark_node;
14960 }
14961 }
14962
14963 f = make_typename_type (ctx, f, typename_type,
14964 complain | tf_keep_type_decl);
14965 if (f == error_mark_node)
14966 return f;
14967 if (TREE_CODE (f) == TYPE_DECL)
14968 {
14969 complain |= tf_ignore_bad_quals;
14970 f = TREE_TYPE (f);
14971 }
14972
14973 if (TREE_CODE (f) != TYPENAME_TYPE)
14974 {
14975 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14976 {
14977 if (complain & tf_error)
14978 error ("%qT resolves to %qT, which is not an enumeration type",
14979 t, f);
14980 else
14981 return error_mark_node;
14982 }
14983 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14984 {
14985 if (complain & tf_error)
14986 error ("%qT resolves to %qT, which is is not a class type",
14987 t, f);
14988 else
14989 return error_mark_node;
14990 }
14991 }
14992
14993 return cp_build_qualified_type_real
14994 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14995 }
14996
14997 case UNBOUND_CLASS_TEMPLATE:
14998 {
14999 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15000 in_decl, /*entering_scope=*/1);
15001 tree name = TYPE_IDENTIFIER (t);
15002 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15003
15004 if (ctx == error_mark_node || name == error_mark_node)
15005 return error_mark_node;
15006
15007 if (parm_list)
15008 parm_list = tsubst_template_parms (parm_list, args, complain);
15009 return make_unbound_class_template (ctx, name, parm_list, complain);
15010 }
15011
15012 case TYPEOF_TYPE:
15013 {
15014 tree type;
15015
15016 ++cp_unevaluated_operand;
15017 ++c_inhibit_evaluation_warnings;
15018
15019 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15020 complain, in_decl,
15021 /*integral_constant_expression_p=*/false);
15022
15023 --cp_unevaluated_operand;
15024 --c_inhibit_evaluation_warnings;
15025
15026 type = finish_typeof (type);
15027 return cp_build_qualified_type_real (type,
15028 cp_type_quals (t)
15029 | cp_type_quals (type),
15030 complain);
15031 }
15032
15033 case DECLTYPE_TYPE:
15034 {
15035 tree type;
15036
15037 ++cp_unevaluated_operand;
15038 ++c_inhibit_evaluation_warnings;
15039
15040 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15041 complain|tf_decltype, in_decl,
15042 /*function_p*/false,
15043 /*integral_constant_expression*/false);
15044
15045 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15046 {
15047 if (type == NULL_TREE)
15048 {
15049 if (complain & tf_error)
15050 error ("empty initializer in lambda init-capture");
15051 type = error_mark_node;
15052 }
15053 else if (TREE_CODE (type) == TREE_LIST)
15054 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15055 }
15056
15057 --cp_unevaluated_operand;
15058 --c_inhibit_evaluation_warnings;
15059
15060 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15061 type = lambda_capture_field_type (type,
15062 DECLTYPE_FOR_INIT_CAPTURE (t),
15063 DECLTYPE_FOR_REF_CAPTURE (t));
15064 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15065 type = lambda_proxy_type (type);
15066 else
15067 {
15068 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15069 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15070 && EXPR_P (type))
15071 /* In a template ~id could be either a complement expression
15072 or an unqualified-id naming a destructor; if instantiating
15073 it produces an expression, it's not an id-expression or
15074 member access. */
15075 id = false;
15076 type = finish_decltype_type (type, id, complain);
15077 }
15078 return cp_build_qualified_type_real (type,
15079 cp_type_quals (t)
15080 | cp_type_quals (type),
15081 complain | tf_ignore_bad_quals);
15082 }
15083
15084 case UNDERLYING_TYPE:
15085 {
15086 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15087 complain, in_decl);
15088 return finish_underlying_type (type);
15089 }
15090
15091 case TYPE_ARGUMENT_PACK:
15092 case NONTYPE_ARGUMENT_PACK:
15093 {
15094 tree r;
15095
15096 if (code == NONTYPE_ARGUMENT_PACK)
15097 r = make_node (code);
15098 else
15099 r = cxx_make_type (code);
15100
15101 tree pack_args = ARGUMENT_PACK_ARGS (t);
15102 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15103 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15104
15105 return r;
15106 }
15107
15108 case VOID_CST:
15109 case INTEGER_CST:
15110 case REAL_CST:
15111 case STRING_CST:
15112 case PLUS_EXPR:
15113 case MINUS_EXPR:
15114 case NEGATE_EXPR:
15115 case NOP_EXPR:
15116 case INDIRECT_REF:
15117 case ADDR_EXPR:
15118 case CALL_EXPR:
15119 case ARRAY_REF:
15120 case SCOPE_REF:
15121 /* We should use one of the expression tsubsts for these codes. */
15122 gcc_unreachable ();
15123
15124 default:
15125 sorry ("use of %qs in template", get_tree_code_name (code));
15126 return error_mark_node;
15127 }
15128 }
15129
15130 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15131 expression on the left-hand side of the "." or "->" operator. We
15132 only do the lookup if we had a dependent BASELINK. Otherwise we
15133 adjust it onto the instantiated heirarchy. */
15134
15135 static tree
15136 tsubst_baselink (tree baselink, tree object_type,
15137 tree args, tsubst_flags_t complain, tree in_decl)
15138 {
15139 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15140 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15141 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15142
15143 tree optype = BASELINK_OPTYPE (baselink);
15144 optype = tsubst (optype, args, complain, in_decl);
15145
15146 tree template_args = NULL_TREE;
15147 bool template_id_p = false;
15148 tree fns = BASELINK_FUNCTIONS (baselink);
15149 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15150 {
15151 template_id_p = true;
15152 template_args = TREE_OPERAND (fns, 1);
15153 fns = TREE_OPERAND (fns, 0);
15154 if (template_args)
15155 template_args = tsubst_template_args (template_args, args,
15156 complain, in_decl);
15157 }
15158
15159 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15160 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15161 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15162
15163 if (dependent_p)
15164 {
15165 tree name = OVL_NAME (fns);
15166 if (IDENTIFIER_CONV_OP_P (name))
15167 name = make_conv_op_name (optype);
15168
15169 if (name == complete_dtor_identifier)
15170 /* Treat as-if non-dependent below. */
15171 dependent_p = false;
15172
15173 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15174 if (!baselink)
15175 {
15176 if ((complain & tf_error)
15177 && constructor_name_p (name, qualifying_scope))
15178 error ("cannot call constructor %<%T::%D%> directly",
15179 qualifying_scope, name);
15180 return error_mark_node;
15181 }
15182
15183 if (BASELINK_P (baselink))
15184 fns = BASELINK_FUNCTIONS (baselink);
15185 }
15186 else
15187 /* We're going to overwrite pieces below, make a duplicate. */
15188 baselink = copy_node (baselink);
15189
15190 /* If lookup found a single function, mark it as used at this point.
15191 (If lookup found multiple functions the one selected later by
15192 overload resolution will be marked as used at that point.) */
15193 if (!template_id_p && !really_overloaded_fn (fns))
15194 {
15195 tree fn = OVL_FIRST (fns);
15196 bool ok = mark_used (fn, complain);
15197 if (!ok && !(complain & tf_error))
15198 return error_mark_node;
15199 if (ok && BASELINK_P (baselink))
15200 /* We might have instantiated an auto function. */
15201 TREE_TYPE (baselink) = TREE_TYPE (fn);
15202 }
15203
15204 if (BASELINK_P (baselink))
15205 {
15206 /* Add back the template arguments, if present. */
15207 if (template_id_p)
15208 BASELINK_FUNCTIONS (baselink)
15209 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15210
15211 /* Update the conversion operator type. */
15212 BASELINK_OPTYPE (baselink) = optype;
15213 }
15214
15215 if (!object_type)
15216 object_type = current_class_type;
15217
15218 if (qualified_p || !dependent_p)
15219 {
15220 baselink = adjust_result_of_qualified_name_lookup (baselink,
15221 qualifying_scope,
15222 object_type);
15223 if (!qualified_p)
15224 /* We need to call adjust_result_of_qualified_name_lookup in case the
15225 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15226 so that we still get virtual function binding. */
15227 BASELINK_QUALIFIED_P (baselink) = false;
15228 }
15229
15230 return baselink;
15231 }
15232
15233 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15234 true if the qualified-id will be a postfix-expression in-and-of
15235 itself; false if more of the postfix-expression follows the
15236 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15237 of "&". */
15238
15239 static tree
15240 tsubst_qualified_id (tree qualified_id, tree args,
15241 tsubst_flags_t complain, tree in_decl,
15242 bool done, bool address_p)
15243 {
15244 tree expr;
15245 tree scope;
15246 tree name;
15247 bool is_template;
15248 tree template_args;
15249 location_t loc = UNKNOWN_LOCATION;
15250
15251 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15252
15253 /* Figure out what name to look up. */
15254 name = TREE_OPERAND (qualified_id, 1);
15255 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15256 {
15257 is_template = true;
15258 loc = EXPR_LOCATION (name);
15259 template_args = TREE_OPERAND (name, 1);
15260 if (template_args)
15261 template_args = tsubst_template_args (template_args, args,
15262 complain, in_decl);
15263 if (template_args == error_mark_node)
15264 return error_mark_node;
15265 name = TREE_OPERAND (name, 0);
15266 }
15267 else
15268 {
15269 is_template = false;
15270 template_args = NULL_TREE;
15271 }
15272
15273 /* Substitute into the qualifying scope. When there are no ARGS, we
15274 are just trying to simplify a non-dependent expression. In that
15275 case the qualifying scope may be dependent, and, in any case,
15276 substituting will not help. */
15277 scope = TREE_OPERAND (qualified_id, 0);
15278 if (args)
15279 {
15280 scope = tsubst (scope, args, complain, in_decl);
15281 expr = tsubst_copy (name, args, complain, in_decl);
15282 }
15283 else
15284 expr = name;
15285
15286 if (dependent_scope_p (scope))
15287 {
15288 if (is_template)
15289 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15290 tree r = build_qualified_name (NULL_TREE, scope, expr,
15291 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15292 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15293 return r;
15294 }
15295
15296 if (!BASELINK_P (name) && !DECL_P (expr))
15297 {
15298 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15299 {
15300 /* A BIT_NOT_EXPR is used to represent a destructor. */
15301 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15302 {
15303 error ("qualifying type %qT does not match destructor name ~%qT",
15304 scope, TREE_OPERAND (expr, 0));
15305 expr = error_mark_node;
15306 }
15307 else
15308 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15309 /*is_type_p=*/0, false);
15310 }
15311 else
15312 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15313 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15314 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15315 {
15316 if (complain & tf_error)
15317 {
15318 error ("dependent-name %qE is parsed as a non-type, but "
15319 "instantiation yields a type", qualified_id);
15320 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15321 }
15322 return error_mark_node;
15323 }
15324 }
15325
15326 if (DECL_P (expr))
15327 {
15328 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15329 scope);
15330 /* Remember that there was a reference to this entity. */
15331 if (!mark_used (expr, complain) && !(complain & tf_error))
15332 return error_mark_node;
15333 }
15334
15335 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15336 {
15337 if (complain & tf_error)
15338 qualified_name_lookup_error (scope,
15339 TREE_OPERAND (qualified_id, 1),
15340 expr, input_location);
15341 return error_mark_node;
15342 }
15343
15344 if (is_template)
15345 {
15346 /* We may be repeating a check already done during parsing, but
15347 if it was well-formed and passed then, it will pass again
15348 now, and if it didn't, we wouldn't have got here. The case
15349 we want to catch is when we couldn't tell then, and can now,
15350 namely when templ prior to substitution was an
15351 identifier. */
15352 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15353 return error_mark_node;
15354
15355 if (variable_template_p (expr))
15356 expr = lookup_and_finish_template_variable (expr, template_args,
15357 complain);
15358 else
15359 expr = lookup_template_function (expr, template_args);
15360 }
15361
15362 if (expr == error_mark_node && complain & tf_error)
15363 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15364 expr, input_location);
15365 else if (TYPE_P (scope))
15366 {
15367 expr = (adjust_result_of_qualified_name_lookup
15368 (expr, scope, current_nonlambda_class_type ()));
15369 expr = (finish_qualified_id_expr
15370 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15371 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15372 /*template_arg_p=*/false, complain));
15373 }
15374
15375 /* Expressions do not generally have reference type. */
15376 if (TREE_CODE (expr) != SCOPE_REF
15377 /* However, if we're about to form a pointer-to-member, we just
15378 want the referenced member referenced. */
15379 && TREE_CODE (expr) != OFFSET_REF)
15380 expr = convert_from_reference (expr);
15381
15382 if (REF_PARENTHESIZED_P (qualified_id))
15383 expr = force_paren_expr (expr);
15384
15385 return expr;
15386 }
15387
15388 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15389 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15390 for tsubst. */
15391
15392 static tree
15393 tsubst_init (tree init, tree decl, tree args,
15394 tsubst_flags_t complain, tree in_decl)
15395 {
15396 if (!init)
15397 return NULL_TREE;
15398
15399 init = tsubst_expr (init, args, complain, in_decl, false);
15400
15401 if (!init && TREE_TYPE (decl) != error_mark_node)
15402 {
15403 /* If we had an initializer but it
15404 instantiated to nothing,
15405 value-initialize the object. This will
15406 only occur when the initializer was a
15407 pack expansion where the parameter packs
15408 used in that expansion were of length
15409 zero. */
15410 init = build_value_init (TREE_TYPE (decl),
15411 complain);
15412 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15413 init = get_target_expr_sfinae (init, complain);
15414 if (TREE_CODE (init) == TARGET_EXPR)
15415 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15416 }
15417
15418 return init;
15419 }
15420
15421 /* Like tsubst, but deals with expressions. This function just replaces
15422 template parms; to finish processing the resultant expression, use
15423 tsubst_copy_and_build or tsubst_expr. */
15424
15425 static tree
15426 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15427 {
15428 enum tree_code code;
15429 tree r;
15430
15431 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15432 return t;
15433
15434 code = TREE_CODE (t);
15435
15436 switch (code)
15437 {
15438 case PARM_DECL:
15439 r = retrieve_local_specialization (t);
15440
15441 if (r == NULL_TREE)
15442 {
15443 /* We get here for a use of 'this' in an NSDMI. */
15444 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15445 return current_class_ptr;
15446
15447 /* This can happen for a parameter name used later in a function
15448 declaration (such as in a late-specified return type). Just
15449 make a dummy decl, since it's only used for its type. */
15450 gcc_assert (cp_unevaluated_operand != 0);
15451 r = tsubst_decl (t, args, complain);
15452 /* Give it the template pattern as its context; its true context
15453 hasn't been instantiated yet and this is good enough for
15454 mangling. */
15455 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15456 }
15457
15458 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15459 r = argument_pack_select_arg (r);
15460 if (!mark_used (r, complain) && !(complain & tf_error))
15461 return error_mark_node;
15462 return r;
15463
15464 case CONST_DECL:
15465 {
15466 tree enum_type;
15467 tree v;
15468
15469 if (DECL_TEMPLATE_PARM_P (t))
15470 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15471 /* There is no need to substitute into namespace-scope
15472 enumerators. */
15473 if (DECL_NAMESPACE_SCOPE_P (t))
15474 return t;
15475 /* If ARGS is NULL, then T is known to be non-dependent. */
15476 if (args == NULL_TREE)
15477 return scalar_constant_value (t);
15478
15479 /* Unfortunately, we cannot just call lookup_name here.
15480 Consider:
15481
15482 template <int I> int f() {
15483 enum E { a = I };
15484 struct S { void g() { E e = a; } };
15485 };
15486
15487 When we instantiate f<7>::S::g(), say, lookup_name is not
15488 clever enough to find f<7>::a. */
15489 enum_type
15490 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15491 /*entering_scope=*/0);
15492
15493 for (v = TYPE_VALUES (enum_type);
15494 v != NULL_TREE;
15495 v = TREE_CHAIN (v))
15496 if (TREE_PURPOSE (v) == DECL_NAME (t))
15497 return TREE_VALUE (v);
15498
15499 /* We didn't find the name. That should never happen; if
15500 name-lookup found it during preliminary parsing, we
15501 should find it again here during instantiation. */
15502 gcc_unreachable ();
15503 }
15504 return t;
15505
15506 case FIELD_DECL:
15507 if (DECL_CONTEXT (t))
15508 {
15509 tree ctx;
15510
15511 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15512 /*entering_scope=*/1);
15513 if (ctx != DECL_CONTEXT (t))
15514 {
15515 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15516 if (!r)
15517 {
15518 if (complain & tf_error)
15519 error ("using invalid field %qD", t);
15520 return error_mark_node;
15521 }
15522 return r;
15523 }
15524 }
15525
15526 return t;
15527
15528 case VAR_DECL:
15529 case FUNCTION_DECL:
15530 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15531 r = tsubst (t, args, complain, in_decl);
15532 else if (local_variable_p (t)
15533 && uses_template_parms (DECL_CONTEXT (t)))
15534 {
15535 r = retrieve_local_specialization (t);
15536 if (r == NULL_TREE)
15537 {
15538 /* First try name lookup to find the instantiation. */
15539 r = lookup_name (DECL_NAME (t));
15540 if (r && !is_capture_proxy (r))
15541 {
15542 /* Make sure that the one we found is the one we want. */
15543 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15544 if (ctx != DECL_CONTEXT (r))
15545 r = NULL_TREE;
15546 }
15547
15548 if (r)
15549 /* OK */;
15550 else
15551 {
15552 /* This can happen for a variable used in a
15553 late-specified return type of a local lambda, or for a
15554 local static or constant. Building a new VAR_DECL
15555 should be OK in all those cases. */
15556 r = tsubst_decl (t, args, complain);
15557 if (local_specializations)
15558 /* Avoid infinite recursion (79640). */
15559 register_local_specialization (r, t);
15560 if (decl_maybe_constant_var_p (r))
15561 {
15562 /* We can't call cp_finish_decl, so handle the
15563 initializer by hand. */
15564 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15565 complain, in_decl);
15566 if (!processing_template_decl)
15567 init = maybe_constant_init (init);
15568 if (processing_template_decl
15569 ? potential_constant_expression (init)
15570 : reduced_constant_expression_p (init))
15571 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15572 = TREE_CONSTANT (r) = true;
15573 DECL_INITIAL (r) = init;
15574 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15575 TREE_TYPE (r)
15576 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15577 complain, adc_variable_type);
15578 }
15579 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15580 || decl_constant_var_p (r)
15581 || errorcount || sorrycount);
15582 if (!processing_template_decl
15583 && !TREE_STATIC (r))
15584 r = process_outer_var_ref (r, complain);
15585 }
15586 /* Remember this for subsequent uses. */
15587 if (local_specializations)
15588 register_local_specialization (r, t);
15589 }
15590 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15591 r = argument_pack_select_arg (r);
15592 }
15593 else
15594 r = t;
15595 if (!mark_used (r, complain))
15596 return error_mark_node;
15597 return r;
15598
15599 case NAMESPACE_DECL:
15600 return t;
15601
15602 case OVERLOAD:
15603 return t;
15604
15605 case BASELINK:
15606 return tsubst_baselink (t, current_nonlambda_class_type (),
15607 args, complain, in_decl);
15608
15609 case TEMPLATE_DECL:
15610 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15611 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15612 args, complain, in_decl);
15613 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15614 return tsubst (t, args, complain, in_decl);
15615 else if (DECL_CLASS_SCOPE_P (t)
15616 && uses_template_parms (DECL_CONTEXT (t)))
15617 {
15618 /* Template template argument like the following example need
15619 special treatment:
15620
15621 template <template <class> class TT> struct C {};
15622 template <class T> struct D {
15623 template <class U> struct E {};
15624 C<E> c; // #1
15625 };
15626 D<int> d; // #2
15627
15628 We are processing the template argument `E' in #1 for
15629 the template instantiation #2. Originally, `E' is a
15630 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15631 have to substitute this with one having context `D<int>'. */
15632
15633 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15634 if (dependent_scope_p (context))
15635 {
15636 /* When rewriting a constructor into a deduction guide, a
15637 non-dependent name can become dependent, so memtmpl<args>
15638 becomes context::template memtmpl<args>. */
15639 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15640 return build_qualified_name (type, context, DECL_NAME (t),
15641 /*template*/true);
15642 }
15643 return lookup_field (context, DECL_NAME(t), 0, false);
15644 }
15645 else
15646 /* Ordinary template template argument. */
15647 return t;
15648
15649 case NON_LVALUE_EXPR:
15650 case VIEW_CONVERT_EXPR:
15651 {
15652 /* Handle location wrappers by substituting the wrapped node
15653 first, *then* reusing the resulting type. Doing the type
15654 first ensures that we handle template parameters and
15655 parameter pack expansions. */
15656 if (location_wrapper_p (t))
15657 {
15658 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15659 complain, in_decl);
15660 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15661 }
15662 tree op = TREE_OPERAND (t, 0);
15663 if (code == VIEW_CONVERT_EXPR
15664 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15665 {
15666 /* Wrapper to make a C++20 template parameter object const. */
15667 op = tsubst_copy (op, args, complain, in_decl);
15668 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15669 {
15670 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15671 return build1 (code, type, op);
15672 }
15673 else
15674 {
15675 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15676 return op;
15677 }
15678 }
15679 /* We shouldn't see any other uses of these in templates. */
15680 gcc_unreachable ();
15681 }
15682
15683 case CAST_EXPR:
15684 case REINTERPRET_CAST_EXPR:
15685 case CONST_CAST_EXPR:
15686 case STATIC_CAST_EXPR:
15687 case DYNAMIC_CAST_EXPR:
15688 case IMPLICIT_CONV_EXPR:
15689 case CONVERT_EXPR:
15690 case NOP_EXPR:
15691 {
15692 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15693 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15694 return build1 (code, type, op0);
15695 }
15696
15697 case SIZEOF_EXPR:
15698 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15699 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15700 {
15701 tree expanded, op = TREE_OPERAND (t, 0);
15702 int len = 0;
15703
15704 if (SIZEOF_EXPR_TYPE_P (t))
15705 op = TREE_TYPE (op);
15706
15707 ++cp_unevaluated_operand;
15708 ++c_inhibit_evaluation_warnings;
15709 /* We only want to compute the number of arguments. */
15710 if (PACK_EXPANSION_P (op))
15711 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15712 else
15713 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15714 args, complain, in_decl);
15715 --cp_unevaluated_operand;
15716 --c_inhibit_evaluation_warnings;
15717
15718 if (TREE_CODE (expanded) == TREE_VEC)
15719 {
15720 len = TREE_VEC_LENGTH (expanded);
15721 /* Set TREE_USED for the benefit of -Wunused. */
15722 for (int i = 0; i < len; i++)
15723 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15724 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15725 }
15726
15727 if (expanded == error_mark_node)
15728 return error_mark_node;
15729 else if (PACK_EXPANSION_P (expanded)
15730 || (TREE_CODE (expanded) == TREE_VEC
15731 && pack_expansion_args_count (expanded)))
15732
15733 {
15734 if (PACK_EXPANSION_P (expanded))
15735 /* OK. */;
15736 else if (TREE_VEC_LENGTH (expanded) == 1)
15737 expanded = TREE_VEC_ELT (expanded, 0);
15738 else
15739 expanded = make_argument_pack (expanded);
15740
15741 if (TYPE_P (expanded))
15742 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15743 false,
15744 complain & tf_error);
15745 else
15746 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15747 complain & tf_error);
15748 }
15749 else
15750 return build_int_cst (size_type_node, len);
15751 }
15752 if (SIZEOF_EXPR_TYPE_P (t))
15753 {
15754 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15755 args, complain, in_decl);
15756 r = build1 (NOP_EXPR, r, error_mark_node);
15757 r = build1 (SIZEOF_EXPR,
15758 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15759 SIZEOF_EXPR_TYPE_P (r) = 1;
15760 return r;
15761 }
15762 /* Fall through */
15763
15764 case INDIRECT_REF:
15765 case NEGATE_EXPR:
15766 case TRUTH_NOT_EXPR:
15767 case BIT_NOT_EXPR:
15768 case ADDR_EXPR:
15769 case UNARY_PLUS_EXPR: /* Unary + */
15770 case ALIGNOF_EXPR:
15771 case AT_ENCODE_EXPR:
15772 case ARROW_EXPR:
15773 case THROW_EXPR:
15774 case TYPEID_EXPR:
15775 case REALPART_EXPR:
15776 case IMAGPART_EXPR:
15777 case PAREN_EXPR:
15778 {
15779 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15780 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15781 r = build1 (code, type, op0);
15782 if (code == ALIGNOF_EXPR)
15783 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15784 return r;
15785 }
15786
15787 case COMPONENT_REF:
15788 {
15789 tree object;
15790 tree name;
15791
15792 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15793 name = TREE_OPERAND (t, 1);
15794 if (TREE_CODE (name) == BIT_NOT_EXPR)
15795 {
15796 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15797 complain, in_decl);
15798 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15799 }
15800 else if (TREE_CODE (name) == SCOPE_REF
15801 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15802 {
15803 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15804 complain, in_decl);
15805 name = TREE_OPERAND (name, 1);
15806 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15807 complain, in_decl);
15808 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15809 name = build_qualified_name (/*type=*/NULL_TREE,
15810 base, name,
15811 /*template_p=*/false);
15812 }
15813 else if (BASELINK_P (name))
15814 name = tsubst_baselink (name,
15815 non_reference (TREE_TYPE (object)),
15816 args, complain,
15817 in_decl);
15818 else
15819 name = tsubst_copy (name, args, complain, in_decl);
15820 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15821 }
15822
15823 case PLUS_EXPR:
15824 case MINUS_EXPR:
15825 case MULT_EXPR:
15826 case TRUNC_DIV_EXPR:
15827 case CEIL_DIV_EXPR:
15828 case FLOOR_DIV_EXPR:
15829 case ROUND_DIV_EXPR:
15830 case EXACT_DIV_EXPR:
15831 case BIT_AND_EXPR:
15832 case BIT_IOR_EXPR:
15833 case BIT_XOR_EXPR:
15834 case TRUNC_MOD_EXPR:
15835 case FLOOR_MOD_EXPR:
15836 case TRUTH_ANDIF_EXPR:
15837 case TRUTH_ORIF_EXPR:
15838 case TRUTH_AND_EXPR:
15839 case TRUTH_OR_EXPR:
15840 case RSHIFT_EXPR:
15841 case LSHIFT_EXPR:
15842 case RROTATE_EXPR:
15843 case LROTATE_EXPR:
15844 case EQ_EXPR:
15845 case NE_EXPR:
15846 case MAX_EXPR:
15847 case MIN_EXPR:
15848 case LE_EXPR:
15849 case GE_EXPR:
15850 case LT_EXPR:
15851 case GT_EXPR:
15852 case COMPOUND_EXPR:
15853 case DOTSTAR_EXPR:
15854 case MEMBER_REF:
15855 case PREDECREMENT_EXPR:
15856 case PREINCREMENT_EXPR:
15857 case POSTDECREMENT_EXPR:
15858 case POSTINCREMENT_EXPR:
15859 {
15860 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15861 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15862 return build_nt (code, op0, op1);
15863 }
15864
15865 case SCOPE_REF:
15866 {
15867 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15868 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15869 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15870 QUALIFIED_NAME_IS_TEMPLATE (t));
15871 }
15872
15873 case ARRAY_REF:
15874 {
15875 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15876 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15877 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15878 }
15879
15880 case CALL_EXPR:
15881 {
15882 int n = VL_EXP_OPERAND_LENGTH (t);
15883 tree result = build_vl_exp (CALL_EXPR, n);
15884 int i;
15885 for (i = 0; i < n; i++)
15886 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15887 complain, in_decl);
15888 return result;
15889 }
15890
15891 case COND_EXPR:
15892 case MODOP_EXPR:
15893 case PSEUDO_DTOR_EXPR:
15894 case VEC_PERM_EXPR:
15895 {
15896 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15897 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15898 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15899 r = build_nt (code, op0, op1, op2);
15900 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15901 return r;
15902 }
15903
15904 case NEW_EXPR:
15905 {
15906 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15907 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15908 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15909 r = build_nt (code, op0, op1, op2);
15910 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15911 return r;
15912 }
15913
15914 case DELETE_EXPR:
15915 {
15916 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15917 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15918 r = build_nt (code, op0, op1);
15919 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15920 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15921 return r;
15922 }
15923
15924 case TEMPLATE_ID_EXPR:
15925 {
15926 /* Substituted template arguments */
15927 tree fn = TREE_OPERAND (t, 0);
15928 tree targs = TREE_OPERAND (t, 1);
15929
15930 fn = tsubst_copy (fn, args, complain, in_decl);
15931 if (targs)
15932 targs = tsubst_template_args (targs, args, complain, in_decl);
15933
15934 return lookup_template_function (fn, targs);
15935 }
15936
15937 case TREE_LIST:
15938 {
15939 tree purpose, value, chain;
15940
15941 if (t == void_list_node)
15942 return t;
15943
15944 purpose = TREE_PURPOSE (t);
15945 if (purpose)
15946 purpose = tsubst_copy (purpose, args, complain, in_decl);
15947 value = TREE_VALUE (t);
15948 if (value)
15949 value = tsubst_copy (value, args, complain, in_decl);
15950 chain = TREE_CHAIN (t);
15951 if (chain && chain != void_type_node)
15952 chain = tsubst_copy (chain, args, complain, in_decl);
15953 if (purpose == TREE_PURPOSE (t)
15954 && value == TREE_VALUE (t)
15955 && chain == TREE_CHAIN (t))
15956 return t;
15957 return tree_cons (purpose, value, chain);
15958 }
15959
15960 case RECORD_TYPE:
15961 case UNION_TYPE:
15962 case ENUMERAL_TYPE:
15963 case INTEGER_TYPE:
15964 case TEMPLATE_TYPE_PARM:
15965 case TEMPLATE_TEMPLATE_PARM:
15966 case BOUND_TEMPLATE_TEMPLATE_PARM:
15967 case TEMPLATE_PARM_INDEX:
15968 case POINTER_TYPE:
15969 case REFERENCE_TYPE:
15970 case OFFSET_TYPE:
15971 case FUNCTION_TYPE:
15972 case METHOD_TYPE:
15973 case ARRAY_TYPE:
15974 case TYPENAME_TYPE:
15975 case UNBOUND_CLASS_TEMPLATE:
15976 case TYPEOF_TYPE:
15977 case DECLTYPE_TYPE:
15978 case TYPE_DECL:
15979 return tsubst (t, args, complain, in_decl);
15980
15981 case USING_DECL:
15982 t = DECL_NAME (t);
15983 /* Fall through. */
15984 case IDENTIFIER_NODE:
15985 if (IDENTIFIER_CONV_OP_P (t))
15986 {
15987 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15988 return make_conv_op_name (new_type);
15989 }
15990 else
15991 return t;
15992
15993 case CONSTRUCTOR:
15994 /* This is handled by tsubst_copy_and_build. */
15995 gcc_unreachable ();
15996
15997 case VA_ARG_EXPR:
15998 {
15999 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16000 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16001 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16002 }
16003
16004 case CLEANUP_POINT_EXPR:
16005 /* We shouldn't have built any of these during initial template
16006 generation. Instead, they should be built during instantiation
16007 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16008 gcc_unreachable ();
16009
16010 case OFFSET_REF:
16011 {
16012 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16013 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16014 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16015 r = build2 (code, type, op0, op1);
16016 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16017 if (!mark_used (TREE_OPERAND (r, 1), complain)
16018 && !(complain & tf_error))
16019 return error_mark_node;
16020 return r;
16021 }
16022
16023 case EXPR_PACK_EXPANSION:
16024 error ("invalid use of pack expansion expression");
16025 return error_mark_node;
16026
16027 case NONTYPE_ARGUMENT_PACK:
16028 error ("use %<...%> to expand argument pack");
16029 return error_mark_node;
16030
16031 case VOID_CST:
16032 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16033 return t;
16034
16035 case INTEGER_CST:
16036 case REAL_CST:
16037 case STRING_CST:
16038 case COMPLEX_CST:
16039 {
16040 /* Instantiate any typedefs in the type. */
16041 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16042 r = fold_convert (type, t);
16043 gcc_assert (TREE_CODE (r) == code);
16044 return r;
16045 }
16046
16047 case PTRMEM_CST:
16048 /* These can sometimes show up in a partial instantiation, but never
16049 involve template parms. */
16050 gcc_assert (!uses_template_parms (t));
16051 return t;
16052
16053 case UNARY_LEFT_FOLD_EXPR:
16054 return tsubst_unary_left_fold (t, args, complain, in_decl);
16055 case UNARY_RIGHT_FOLD_EXPR:
16056 return tsubst_unary_right_fold (t, args, complain, in_decl);
16057 case BINARY_LEFT_FOLD_EXPR:
16058 return tsubst_binary_left_fold (t, args, complain, in_decl);
16059 case BINARY_RIGHT_FOLD_EXPR:
16060 return tsubst_binary_right_fold (t, args, complain, in_decl);
16061 case PREDICT_EXPR:
16062 return t;
16063
16064 case DEBUG_BEGIN_STMT:
16065 /* ??? There's no point in copying it for now, but maybe some
16066 day it will contain more information, such as a pointer back
16067 to the containing function, inlined copy or so. */
16068 return t;
16069
16070 default:
16071 /* We shouldn't get here, but keep going if !flag_checking. */
16072 if (flag_checking)
16073 gcc_unreachable ();
16074 return t;
16075 }
16076 }
16077
16078 /* Helper function for tsubst_omp_clauses, used for instantiation of
16079 OMP_CLAUSE_DECL of clauses. */
16080
16081 static tree
16082 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16083 tree in_decl, tree *iterator_cache)
16084 {
16085 if (decl == NULL_TREE)
16086 return NULL_TREE;
16087
16088 /* Handle OpenMP iterators. */
16089 if (TREE_CODE (decl) == TREE_LIST
16090 && TREE_PURPOSE (decl)
16091 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16092 {
16093 tree ret;
16094 if (iterator_cache[0] == TREE_PURPOSE (decl))
16095 ret = iterator_cache[1];
16096 else
16097 {
16098 tree *tp = &ret;
16099 begin_scope (sk_omp, NULL);
16100 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16101 {
16102 *tp = copy_node (it);
16103 TREE_VEC_ELT (*tp, 0)
16104 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16105 TREE_VEC_ELT (*tp, 1)
16106 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16107 /*integral_constant_expression_p=*/false);
16108 TREE_VEC_ELT (*tp, 2)
16109 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16110 /*integral_constant_expression_p=*/false);
16111 TREE_VEC_ELT (*tp, 3)
16112 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16113 /*integral_constant_expression_p=*/false);
16114 TREE_CHAIN (*tp) = NULL_TREE;
16115 tp = &TREE_CHAIN (*tp);
16116 }
16117 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16118 iterator_cache[0] = TREE_PURPOSE (decl);
16119 iterator_cache[1] = ret;
16120 }
16121 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16122 args, complain,
16123 in_decl, NULL));
16124 }
16125
16126 /* Handle an OpenMP array section represented as a TREE_LIST (or
16127 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16128 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16129 TREE_LIST. We can handle it exactly the same as an array section
16130 (purpose, value, and a chain), even though the nomenclature
16131 (low_bound, length, etc) is different. */
16132 if (TREE_CODE (decl) == TREE_LIST)
16133 {
16134 tree low_bound
16135 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16136 /*integral_constant_expression_p=*/false);
16137 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16138 /*integral_constant_expression_p=*/false);
16139 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16140 in_decl, NULL);
16141 if (TREE_PURPOSE (decl) == low_bound
16142 && TREE_VALUE (decl) == length
16143 && TREE_CHAIN (decl) == chain)
16144 return decl;
16145 tree ret = tree_cons (low_bound, length, chain);
16146 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16147 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16148 return ret;
16149 }
16150 tree ret = tsubst_expr (decl, args, complain, in_decl,
16151 /*integral_constant_expression_p=*/false);
16152 /* Undo convert_from_reference tsubst_expr could have called. */
16153 if (decl
16154 && REFERENCE_REF_P (ret)
16155 && !REFERENCE_REF_P (decl))
16156 ret = TREE_OPERAND (ret, 0);
16157 return ret;
16158 }
16159
16160 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16161
16162 static tree
16163 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16164 tree args, tsubst_flags_t complain, tree in_decl)
16165 {
16166 tree new_clauses = NULL_TREE, nc, oc;
16167 tree linear_no_step = NULL_TREE;
16168 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16169
16170 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16171 {
16172 nc = copy_node (oc);
16173 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16174 new_clauses = nc;
16175
16176 switch (OMP_CLAUSE_CODE (nc))
16177 {
16178 case OMP_CLAUSE_LASTPRIVATE:
16179 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16180 {
16181 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16182 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16183 in_decl, /*integral_constant_expression_p=*/false);
16184 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16185 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16186 }
16187 /* FALLTHRU */
16188 case OMP_CLAUSE_PRIVATE:
16189 case OMP_CLAUSE_SHARED:
16190 case OMP_CLAUSE_FIRSTPRIVATE:
16191 case OMP_CLAUSE_COPYIN:
16192 case OMP_CLAUSE_COPYPRIVATE:
16193 case OMP_CLAUSE_UNIFORM:
16194 case OMP_CLAUSE_DEPEND:
16195 case OMP_CLAUSE_FROM:
16196 case OMP_CLAUSE_TO:
16197 case OMP_CLAUSE_MAP:
16198 case OMP_CLAUSE_NONTEMPORAL:
16199 case OMP_CLAUSE_USE_DEVICE_PTR:
16200 case OMP_CLAUSE_IS_DEVICE_PTR:
16201 OMP_CLAUSE_DECL (nc)
16202 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16203 in_decl, iterator_cache);
16204 break;
16205 case OMP_CLAUSE_TILE:
16206 case OMP_CLAUSE_IF:
16207 case OMP_CLAUSE_NUM_THREADS:
16208 case OMP_CLAUSE_SCHEDULE:
16209 case OMP_CLAUSE_COLLAPSE:
16210 case OMP_CLAUSE_FINAL:
16211 case OMP_CLAUSE_DEVICE:
16212 case OMP_CLAUSE_DIST_SCHEDULE:
16213 case OMP_CLAUSE_NUM_TEAMS:
16214 case OMP_CLAUSE_THREAD_LIMIT:
16215 case OMP_CLAUSE_SAFELEN:
16216 case OMP_CLAUSE_SIMDLEN:
16217 case OMP_CLAUSE_NUM_TASKS:
16218 case OMP_CLAUSE_GRAINSIZE:
16219 case OMP_CLAUSE_PRIORITY:
16220 case OMP_CLAUSE_ORDERED:
16221 case OMP_CLAUSE_HINT:
16222 case OMP_CLAUSE_NUM_GANGS:
16223 case OMP_CLAUSE_NUM_WORKERS:
16224 case OMP_CLAUSE_VECTOR_LENGTH:
16225 case OMP_CLAUSE_WORKER:
16226 case OMP_CLAUSE_VECTOR:
16227 case OMP_CLAUSE_ASYNC:
16228 case OMP_CLAUSE_WAIT:
16229 OMP_CLAUSE_OPERAND (nc, 0)
16230 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16231 in_decl, /*integral_constant_expression_p=*/false);
16232 break;
16233 case OMP_CLAUSE_REDUCTION:
16234 case OMP_CLAUSE_IN_REDUCTION:
16235 case OMP_CLAUSE_TASK_REDUCTION:
16236 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16237 {
16238 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16239 if (TREE_CODE (placeholder) == SCOPE_REF)
16240 {
16241 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16242 complain, in_decl);
16243 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16244 = build_qualified_name (NULL_TREE, scope,
16245 TREE_OPERAND (placeholder, 1),
16246 false);
16247 }
16248 else
16249 gcc_assert (identifier_p (placeholder));
16250 }
16251 OMP_CLAUSE_DECL (nc)
16252 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16253 in_decl, NULL);
16254 break;
16255 case OMP_CLAUSE_GANG:
16256 case OMP_CLAUSE_ALIGNED:
16257 OMP_CLAUSE_DECL (nc)
16258 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16259 in_decl, NULL);
16260 OMP_CLAUSE_OPERAND (nc, 1)
16261 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16262 in_decl, /*integral_constant_expression_p=*/false);
16263 break;
16264 case OMP_CLAUSE_LINEAR:
16265 OMP_CLAUSE_DECL (nc)
16266 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16267 in_decl, NULL);
16268 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16269 {
16270 gcc_assert (!linear_no_step);
16271 linear_no_step = nc;
16272 }
16273 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16274 OMP_CLAUSE_LINEAR_STEP (nc)
16275 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16276 complain, in_decl, NULL);
16277 else
16278 OMP_CLAUSE_LINEAR_STEP (nc)
16279 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16280 in_decl,
16281 /*integral_constant_expression_p=*/false);
16282 break;
16283 case OMP_CLAUSE_NOWAIT:
16284 case OMP_CLAUSE_DEFAULT:
16285 case OMP_CLAUSE_UNTIED:
16286 case OMP_CLAUSE_MERGEABLE:
16287 case OMP_CLAUSE_INBRANCH:
16288 case OMP_CLAUSE_NOTINBRANCH:
16289 case OMP_CLAUSE_PROC_BIND:
16290 case OMP_CLAUSE_FOR:
16291 case OMP_CLAUSE_PARALLEL:
16292 case OMP_CLAUSE_SECTIONS:
16293 case OMP_CLAUSE_TASKGROUP:
16294 case OMP_CLAUSE_NOGROUP:
16295 case OMP_CLAUSE_THREADS:
16296 case OMP_CLAUSE_SIMD:
16297 case OMP_CLAUSE_DEFAULTMAP:
16298 case OMP_CLAUSE_INDEPENDENT:
16299 case OMP_CLAUSE_AUTO:
16300 case OMP_CLAUSE_SEQ:
16301 case OMP_CLAUSE_IF_PRESENT:
16302 case OMP_CLAUSE_FINALIZE:
16303 break;
16304 default:
16305 gcc_unreachable ();
16306 }
16307 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16308 switch (OMP_CLAUSE_CODE (nc))
16309 {
16310 case OMP_CLAUSE_SHARED:
16311 case OMP_CLAUSE_PRIVATE:
16312 case OMP_CLAUSE_FIRSTPRIVATE:
16313 case OMP_CLAUSE_LASTPRIVATE:
16314 case OMP_CLAUSE_COPYPRIVATE:
16315 case OMP_CLAUSE_LINEAR:
16316 case OMP_CLAUSE_REDUCTION:
16317 case OMP_CLAUSE_IN_REDUCTION:
16318 case OMP_CLAUSE_TASK_REDUCTION:
16319 case OMP_CLAUSE_USE_DEVICE_PTR:
16320 case OMP_CLAUSE_IS_DEVICE_PTR:
16321 /* tsubst_expr on SCOPE_REF results in returning
16322 finish_non_static_data_member result. Undo that here. */
16323 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16324 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16325 == IDENTIFIER_NODE))
16326 {
16327 tree t = OMP_CLAUSE_DECL (nc);
16328 tree v = t;
16329 while (v)
16330 switch (TREE_CODE (v))
16331 {
16332 case COMPONENT_REF:
16333 case MEM_REF:
16334 case INDIRECT_REF:
16335 CASE_CONVERT:
16336 case POINTER_PLUS_EXPR:
16337 v = TREE_OPERAND (v, 0);
16338 continue;
16339 case PARM_DECL:
16340 if (DECL_CONTEXT (v) == current_function_decl
16341 && DECL_ARTIFICIAL (v)
16342 && DECL_NAME (v) == this_identifier)
16343 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16344 /* FALLTHRU */
16345 default:
16346 v = NULL_TREE;
16347 break;
16348 }
16349 }
16350 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16351 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16352 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16353 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16354 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16355 {
16356 tree decl = OMP_CLAUSE_DECL (nc);
16357 if (VAR_P (decl))
16358 {
16359 retrofit_lang_decl (decl);
16360 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16361 }
16362 }
16363 break;
16364 default:
16365 break;
16366 }
16367 }
16368
16369 new_clauses = nreverse (new_clauses);
16370 if (ort != C_ORT_OMP_DECLARE_SIMD)
16371 {
16372 new_clauses = finish_omp_clauses (new_clauses, ort);
16373 if (linear_no_step)
16374 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16375 if (nc == linear_no_step)
16376 {
16377 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16378 break;
16379 }
16380 }
16381 return new_clauses;
16382 }
16383
16384 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16385
16386 static tree
16387 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16388 tree in_decl)
16389 {
16390 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16391
16392 tree purpose, value, chain;
16393
16394 if (t == NULL)
16395 return t;
16396
16397 if (TREE_CODE (t) != TREE_LIST)
16398 return tsubst_copy_and_build (t, args, complain, in_decl,
16399 /*function_p=*/false,
16400 /*integral_constant_expression_p=*/false);
16401
16402 if (t == void_list_node)
16403 return t;
16404
16405 purpose = TREE_PURPOSE (t);
16406 if (purpose)
16407 purpose = RECUR (purpose);
16408 value = TREE_VALUE (t);
16409 if (value)
16410 {
16411 if (TREE_CODE (value) != LABEL_DECL)
16412 value = RECUR (value);
16413 else
16414 {
16415 value = lookup_label (DECL_NAME (value));
16416 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16417 TREE_USED (value) = 1;
16418 }
16419 }
16420 chain = TREE_CHAIN (t);
16421 if (chain && chain != void_type_node)
16422 chain = RECUR (chain);
16423 return tree_cons (purpose, value, chain);
16424 #undef RECUR
16425 }
16426
16427 /* Used to temporarily communicate the list of #pragma omp parallel
16428 clauses to #pragma omp for instantiation if they are combined
16429 together. */
16430
16431 static tree *omp_parallel_combined_clauses;
16432
16433 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16434 tree *, unsigned int *);
16435
16436 /* Substitute one OMP_FOR iterator. */
16437
16438 static bool
16439 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16440 tree initv, tree condv, tree incrv, tree *clauses,
16441 tree args, tsubst_flags_t complain, tree in_decl,
16442 bool integral_constant_expression_p)
16443 {
16444 #define RECUR(NODE) \
16445 tsubst_expr ((NODE), args, complain, in_decl, \
16446 integral_constant_expression_p)
16447 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16448 bool ret = false;
16449
16450 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16451 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16452
16453 decl = TREE_OPERAND (init, 0);
16454 init = TREE_OPERAND (init, 1);
16455 tree decl_expr = NULL_TREE;
16456 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16457 if (range_for)
16458 {
16459 bool decomp = false;
16460 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16461 {
16462 tree v = DECL_VALUE_EXPR (decl);
16463 if (TREE_CODE (v) == ARRAY_REF
16464 && VAR_P (TREE_OPERAND (v, 0))
16465 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16466 {
16467 tree decomp_first = NULL_TREE;
16468 unsigned decomp_cnt = 0;
16469 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16470 maybe_push_decl (d);
16471 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16472 in_decl, &decomp_first, &decomp_cnt);
16473 decomp = true;
16474 if (d == error_mark_node)
16475 decl = error_mark_node;
16476 else
16477 for (unsigned int i = 0; i < decomp_cnt; i++)
16478 {
16479 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16480 {
16481 tree v = build_nt (ARRAY_REF, d,
16482 size_int (decomp_cnt - i - 1),
16483 NULL_TREE, NULL_TREE);
16484 SET_DECL_VALUE_EXPR (decomp_first, v);
16485 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16486 }
16487 fit_decomposition_lang_decl (decomp_first, d);
16488 decomp_first = DECL_CHAIN (decomp_first);
16489 }
16490 }
16491 }
16492 decl = tsubst_decl (decl, args, complain);
16493 if (!decomp)
16494 maybe_push_decl (decl);
16495 }
16496 else if (init && TREE_CODE (init) == DECL_EXPR)
16497 {
16498 /* We need to jump through some hoops to handle declarations in the
16499 init-statement, since we might need to handle auto deduction,
16500 but we need to keep control of initialization. */
16501 decl_expr = init;
16502 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16503 decl = tsubst_decl (decl, args, complain);
16504 }
16505 else
16506 {
16507 if (TREE_CODE (decl) == SCOPE_REF)
16508 {
16509 decl = RECUR (decl);
16510 if (TREE_CODE (decl) == COMPONENT_REF)
16511 {
16512 tree v = decl;
16513 while (v)
16514 switch (TREE_CODE (v))
16515 {
16516 case COMPONENT_REF:
16517 case MEM_REF:
16518 case INDIRECT_REF:
16519 CASE_CONVERT:
16520 case POINTER_PLUS_EXPR:
16521 v = TREE_OPERAND (v, 0);
16522 continue;
16523 case PARM_DECL:
16524 if (DECL_CONTEXT (v) == current_function_decl
16525 && DECL_ARTIFICIAL (v)
16526 && DECL_NAME (v) == this_identifier)
16527 {
16528 decl = TREE_OPERAND (decl, 1);
16529 decl = omp_privatize_field (decl, false);
16530 }
16531 /* FALLTHRU */
16532 default:
16533 v = NULL_TREE;
16534 break;
16535 }
16536 }
16537 }
16538 else
16539 decl = RECUR (decl);
16540 }
16541 init = RECUR (init);
16542
16543 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16544 {
16545 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16546 if (TREE_CODE (o) == TREE_LIST)
16547 TREE_VEC_ELT (orig_declv, i)
16548 = tree_cons (RECUR (TREE_PURPOSE (o)),
16549 RECUR (TREE_VALUE (o)),
16550 NULL_TREE);
16551 else
16552 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16553 }
16554
16555 if (range_for)
16556 {
16557 tree this_pre_body = NULL_TREE;
16558 tree orig_init = NULL_TREE;
16559 tree orig_decl = NULL_TREE;
16560 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16561 orig_init, cond, incr);
16562 if (orig_decl)
16563 {
16564 if (orig_declv == NULL_TREE)
16565 orig_declv = copy_node (declv);
16566 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16567 ret = true;
16568 }
16569 else if (orig_declv)
16570 TREE_VEC_ELT (orig_declv, i) = decl;
16571 }
16572
16573 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16574 if (!range_for && auto_node && init)
16575 TREE_TYPE (decl)
16576 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16577
16578 gcc_assert (!type_dependent_expression_p (decl));
16579
16580 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16581 {
16582 if (decl_expr)
16583 {
16584 /* Declare the variable, but don't let that initialize it. */
16585 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16586 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16587 RECUR (decl_expr);
16588 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16589 }
16590
16591 if (!range_for)
16592 {
16593 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16594 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16595 if (TREE_CODE (incr) == MODIFY_EXPR)
16596 {
16597 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16598 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16599 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16600 NOP_EXPR, rhs, complain);
16601 }
16602 else
16603 incr = RECUR (incr);
16604 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16605 TREE_VEC_ELT (orig_declv, i) = decl;
16606 }
16607 TREE_VEC_ELT (declv, i) = decl;
16608 TREE_VEC_ELT (initv, i) = init;
16609 TREE_VEC_ELT (condv, i) = cond;
16610 TREE_VEC_ELT (incrv, i) = incr;
16611 return ret;
16612 }
16613
16614 if (decl_expr)
16615 {
16616 /* Declare and initialize the variable. */
16617 RECUR (decl_expr);
16618 init = NULL_TREE;
16619 }
16620 else if (init)
16621 {
16622 tree *pc;
16623 int j;
16624 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16625 {
16626 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16627 {
16628 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16629 && OMP_CLAUSE_DECL (*pc) == decl)
16630 break;
16631 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16632 && OMP_CLAUSE_DECL (*pc) == decl)
16633 {
16634 if (j)
16635 break;
16636 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16637 tree c = *pc;
16638 *pc = OMP_CLAUSE_CHAIN (c);
16639 OMP_CLAUSE_CHAIN (c) = *clauses;
16640 *clauses = c;
16641 }
16642 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16643 && OMP_CLAUSE_DECL (*pc) == decl)
16644 {
16645 error ("iteration variable %qD should not be firstprivate",
16646 decl);
16647 *pc = OMP_CLAUSE_CHAIN (*pc);
16648 }
16649 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16650 && OMP_CLAUSE_DECL (*pc) == decl)
16651 {
16652 error ("iteration variable %qD should not be reduction",
16653 decl);
16654 *pc = OMP_CLAUSE_CHAIN (*pc);
16655 }
16656 else
16657 pc = &OMP_CLAUSE_CHAIN (*pc);
16658 }
16659 if (*pc)
16660 break;
16661 }
16662 if (*pc == NULL_TREE)
16663 {
16664 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16665 OMP_CLAUSE_DECL (c) = decl;
16666 c = finish_omp_clauses (c, C_ORT_OMP);
16667 if (c)
16668 {
16669 OMP_CLAUSE_CHAIN (c) = *clauses;
16670 *clauses = c;
16671 }
16672 }
16673 }
16674 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16675 if (COMPARISON_CLASS_P (cond))
16676 {
16677 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16678 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16679 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16680 }
16681 else
16682 cond = RECUR (cond);
16683 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16684 switch (TREE_CODE (incr))
16685 {
16686 case PREINCREMENT_EXPR:
16687 case PREDECREMENT_EXPR:
16688 case POSTINCREMENT_EXPR:
16689 case POSTDECREMENT_EXPR:
16690 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16691 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16692 break;
16693 case MODIFY_EXPR:
16694 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16695 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16696 {
16697 tree rhs = TREE_OPERAND (incr, 1);
16698 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16699 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16700 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16701 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16702 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16703 rhs0, rhs1));
16704 }
16705 else
16706 incr = RECUR (incr);
16707 break;
16708 case MODOP_EXPR:
16709 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16710 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16711 {
16712 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16713 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16714 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16715 TREE_TYPE (decl), lhs,
16716 RECUR (TREE_OPERAND (incr, 2))));
16717 }
16718 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16719 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16720 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16721 {
16722 tree rhs = TREE_OPERAND (incr, 2);
16723 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16724 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16725 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16726 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16727 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16728 rhs0, rhs1));
16729 }
16730 else
16731 incr = RECUR (incr);
16732 break;
16733 default:
16734 incr = RECUR (incr);
16735 break;
16736 }
16737
16738 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16739 TREE_VEC_ELT (orig_declv, i) = decl;
16740 TREE_VEC_ELT (declv, i) = decl;
16741 TREE_VEC_ELT (initv, i) = init;
16742 TREE_VEC_ELT (condv, i) = cond;
16743 TREE_VEC_ELT (incrv, i) = incr;
16744 return false;
16745 #undef RECUR
16746 }
16747
16748 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16749 of OMP_TARGET's body. */
16750
16751 static tree
16752 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16753 {
16754 *walk_subtrees = 0;
16755 switch (TREE_CODE (*tp))
16756 {
16757 case OMP_TEAMS:
16758 return *tp;
16759 case BIND_EXPR:
16760 case STATEMENT_LIST:
16761 *walk_subtrees = 1;
16762 break;
16763 default:
16764 break;
16765 }
16766 return NULL_TREE;
16767 }
16768
16769 /* Helper function for tsubst_expr. For decomposition declaration
16770 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16771 also the corresponding decls representing the identifiers
16772 of the decomposition declaration. Return DECL if successful
16773 or error_mark_node otherwise, set *FIRST to the first decl
16774 in the list chained through DECL_CHAIN and *CNT to the number
16775 of such decls. */
16776
16777 static tree
16778 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16779 tsubst_flags_t complain, tree in_decl, tree *first,
16780 unsigned int *cnt)
16781 {
16782 tree decl2, decl3, prev = decl;
16783 *cnt = 0;
16784 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16785 for (decl2 = DECL_CHAIN (pattern_decl);
16786 decl2
16787 && VAR_P (decl2)
16788 && DECL_DECOMPOSITION_P (decl2)
16789 && DECL_NAME (decl2);
16790 decl2 = DECL_CHAIN (decl2))
16791 {
16792 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16793 {
16794 gcc_assert (errorcount);
16795 return error_mark_node;
16796 }
16797 (*cnt)++;
16798 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16799 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16800 tree v = DECL_VALUE_EXPR (decl2);
16801 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16802 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16803 decl3 = tsubst (decl2, args, complain, in_decl);
16804 SET_DECL_VALUE_EXPR (decl2, v);
16805 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16806 if (VAR_P (decl3))
16807 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16808 else
16809 {
16810 gcc_assert (errorcount);
16811 decl = error_mark_node;
16812 continue;
16813 }
16814 maybe_push_decl (decl3);
16815 if (error_operand_p (decl3))
16816 decl = error_mark_node;
16817 else if (decl != error_mark_node
16818 && DECL_CHAIN (decl3) != prev
16819 && decl != prev)
16820 {
16821 gcc_assert (errorcount);
16822 decl = error_mark_node;
16823 }
16824 else
16825 prev = decl3;
16826 }
16827 *first = prev;
16828 return decl;
16829 }
16830
16831 /* Return the proper local_specialization for init-capture pack DECL. */
16832
16833 static tree
16834 lookup_init_capture_pack (tree decl)
16835 {
16836 /* We handle normal pack captures by forwarding to the specialization of the
16837 captured parameter. We can't do that for pack init-captures; we need them
16838 to have their own local_specialization. We created the individual
16839 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16840 when we process the DECL_EXPR for the pack init-capture in the template.
16841 So, how do we find them? We don't know the capture proxy pack when
16842 building the individual resulting proxies, and we don't know the
16843 individual proxies when instantiating the pack. What we have in common is
16844 the FIELD_DECL.
16845
16846 So...when we instantiate the FIELD_DECL, we stick the result in
16847 local_specializations. Then at the DECL_EXPR we look up that result, see
16848 how many elements it has, synthesize the names, and look them up. */
16849
16850 tree cname = DECL_NAME (decl);
16851 tree val = DECL_VALUE_EXPR (decl);
16852 tree field = TREE_OPERAND (val, 1);
16853 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16854 tree fpack = retrieve_local_specialization (field);
16855 if (fpack == error_mark_node)
16856 return error_mark_node;
16857
16858 int len = 1;
16859 tree vec = NULL_TREE;
16860 tree r = NULL_TREE;
16861 if (TREE_CODE (fpack) == TREE_VEC)
16862 {
16863 len = TREE_VEC_LENGTH (fpack);
16864 vec = make_tree_vec (len);
16865 r = make_node (NONTYPE_ARGUMENT_PACK);
16866 SET_ARGUMENT_PACK_ARGS (r, vec);
16867 }
16868 for (int i = 0; i < len; ++i)
16869 {
16870 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16871 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16872 if (vec)
16873 TREE_VEC_ELT (vec, i) = elt;
16874 else
16875 r = elt;
16876 }
16877 return r;
16878 }
16879
16880 /* Like tsubst_copy for expressions, etc. but also does semantic
16881 processing. */
16882
16883 tree
16884 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16885 bool integral_constant_expression_p)
16886 {
16887 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16888 #define RECUR(NODE) \
16889 tsubst_expr ((NODE), args, complain, in_decl, \
16890 integral_constant_expression_p)
16891
16892 tree stmt, tmp;
16893 tree r;
16894 location_t loc;
16895
16896 if (t == NULL_TREE || t == error_mark_node)
16897 return t;
16898
16899 loc = input_location;
16900 if (location_t eloc = cp_expr_location (t))
16901 input_location = eloc;
16902 if (STATEMENT_CODE_P (TREE_CODE (t)))
16903 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16904
16905 switch (TREE_CODE (t))
16906 {
16907 case STATEMENT_LIST:
16908 {
16909 tree_stmt_iterator i;
16910 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16911 RECUR (tsi_stmt (i));
16912 break;
16913 }
16914
16915 case CTOR_INITIALIZER:
16916 finish_mem_initializers (tsubst_initializer_list
16917 (TREE_OPERAND (t, 0), args));
16918 break;
16919
16920 case RETURN_EXPR:
16921 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16922 break;
16923
16924 case EXPR_STMT:
16925 tmp = RECUR (EXPR_STMT_EXPR (t));
16926 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16927 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16928 else
16929 finish_expr_stmt (tmp);
16930 break;
16931
16932 case USING_STMT:
16933 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16934 /*attribs=*/NULL_TREE);
16935 break;
16936
16937 case DECL_EXPR:
16938 {
16939 tree decl, pattern_decl;
16940 tree init;
16941
16942 pattern_decl = decl = DECL_EXPR_DECL (t);
16943 if (TREE_CODE (decl) == LABEL_DECL)
16944 finish_label_decl (DECL_NAME (decl));
16945 else if (TREE_CODE (decl) == USING_DECL)
16946 {
16947 tree scope = USING_DECL_SCOPE (decl);
16948 tree name = DECL_NAME (decl);
16949
16950 scope = tsubst (scope, args, complain, in_decl);
16951 decl = lookup_qualified_name (scope, name,
16952 /*is_type_p=*/false,
16953 /*complain=*/false);
16954 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16955 qualified_name_lookup_error (scope, name, decl, input_location);
16956 else
16957 finish_local_using_decl (decl, scope, name);
16958 }
16959 else if (is_capture_proxy (decl)
16960 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16961 {
16962 /* We're in tsubst_lambda_expr, we've already inserted a new
16963 capture proxy, so look it up and register it. */
16964 tree inst;
16965 if (!DECL_PACK_P (decl))
16966 {
16967 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
16968 /*nonclass*/1, /*block_p=*/true,
16969 /*ns_only*/0, LOOKUP_HIDDEN);
16970 gcc_assert (inst != decl && is_capture_proxy (inst));
16971 }
16972 else if (is_normal_capture_proxy (decl))
16973 {
16974 inst = (retrieve_local_specialization
16975 (DECL_CAPTURED_VARIABLE (decl)));
16976 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16977 }
16978 else
16979 inst = lookup_init_capture_pack (decl);
16980
16981 register_local_specialization (inst, decl);
16982 break;
16983 }
16984 else if (DECL_PRETTY_FUNCTION_P (decl))
16985 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
16986 DECL_NAME (decl),
16987 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16988 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16989 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16990 /* Don't copy the old closure; we'll create a new one in
16991 tsubst_lambda_expr. */
16992 break;
16993 else
16994 {
16995 init = DECL_INITIAL (decl);
16996 decl = tsubst (decl, args, complain, in_decl);
16997 if (decl != error_mark_node)
16998 {
16999 /* By marking the declaration as instantiated, we avoid
17000 trying to instantiate it. Since instantiate_decl can't
17001 handle local variables, and since we've already done
17002 all that needs to be done, that's the right thing to
17003 do. */
17004 if (VAR_P (decl))
17005 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17006 if (VAR_P (decl) && !DECL_NAME (decl)
17007 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17008 /* Anonymous aggregates are a special case. */
17009 finish_anon_union (decl);
17010 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17011 {
17012 DECL_CONTEXT (decl) = current_function_decl;
17013 if (DECL_NAME (decl) == this_identifier)
17014 {
17015 tree lam = DECL_CONTEXT (current_function_decl);
17016 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17017 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17018 }
17019 insert_capture_proxy (decl);
17020 }
17021 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17022 /* We already did a pushtag. */;
17023 else if (TREE_CODE (decl) == FUNCTION_DECL
17024 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17025 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17026 {
17027 DECL_CONTEXT (decl) = NULL_TREE;
17028 pushdecl (decl);
17029 DECL_CONTEXT (decl) = current_function_decl;
17030 cp_check_omp_declare_reduction (decl);
17031 }
17032 else
17033 {
17034 int const_init = false;
17035 unsigned int cnt = 0;
17036 tree first = NULL_TREE, ndecl = error_mark_node;
17037 maybe_push_decl (decl);
17038
17039 if (VAR_P (decl)
17040 && DECL_DECOMPOSITION_P (decl)
17041 && TREE_TYPE (pattern_decl) != error_mark_node)
17042 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17043 complain, in_decl, &first,
17044 &cnt);
17045
17046 init = tsubst_init (init, decl, args, complain, in_decl);
17047
17048 if (VAR_P (decl))
17049 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17050 (pattern_decl));
17051
17052 if (ndecl != error_mark_node)
17053 cp_maybe_mangle_decomp (ndecl, first, cnt);
17054
17055 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
17056
17057 if (ndecl != error_mark_node)
17058 cp_finish_decomp (ndecl, first, cnt);
17059 }
17060 }
17061 }
17062
17063 break;
17064 }
17065
17066 case FOR_STMT:
17067 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17068 RECUR (FOR_INIT_STMT (t));
17069 finish_init_stmt (stmt);
17070 tmp = RECUR (FOR_COND (t));
17071 finish_for_cond (tmp, stmt, false, 0);
17072 tmp = RECUR (FOR_EXPR (t));
17073 finish_for_expr (tmp, stmt);
17074 {
17075 bool prev = note_iteration_stmt_body_start ();
17076 RECUR (FOR_BODY (t));
17077 note_iteration_stmt_body_end (prev);
17078 }
17079 finish_for_stmt (stmt);
17080 break;
17081
17082 case RANGE_FOR_STMT:
17083 {
17084 /* Construct another range_for, if this is not a final
17085 substitution (for inside inside a generic lambda of a
17086 template). Otherwise convert to a regular for. */
17087 tree decl, expr;
17088 stmt = (processing_template_decl
17089 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17090 : begin_for_stmt (NULL_TREE, NULL_TREE));
17091 RECUR (RANGE_FOR_INIT_STMT (t));
17092 decl = RANGE_FOR_DECL (t);
17093 decl = tsubst (decl, args, complain, in_decl);
17094 maybe_push_decl (decl);
17095 expr = RECUR (RANGE_FOR_EXPR (t));
17096
17097 tree decomp_first = NULL_TREE;
17098 unsigned decomp_cnt = 0;
17099 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17100 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17101 complain, in_decl,
17102 &decomp_first, &decomp_cnt);
17103
17104 if (processing_template_decl)
17105 {
17106 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17107 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17108 finish_range_for_decl (stmt, decl, expr);
17109 if (decomp_first && decl != error_mark_node)
17110 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17111 }
17112 else
17113 {
17114 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17115 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17116 stmt = cp_convert_range_for (stmt, decl, expr,
17117 decomp_first, decomp_cnt,
17118 RANGE_FOR_IVDEP (t), unroll);
17119 }
17120
17121 bool prev = note_iteration_stmt_body_start ();
17122 RECUR (RANGE_FOR_BODY (t));
17123 note_iteration_stmt_body_end (prev);
17124 finish_for_stmt (stmt);
17125 }
17126 break;
17127
17128 case WHILE_STMT:
17129 stmt = begin_while_stmt ();
17130 tmp = RECUR (WHILE_COND (t));
17131 finish_while_stmt_cond (tmp, stmt, false, 0);
17132 {
17133 bool prev = note_iteration_stmt_body_start ();
17134 RECUR (WHILE_BODY (t));
17135 note_iteration_stmt_body_end (prev);
17136 }
17137 finish_while_stmt (stmt);
17138 break;
17139
17140 case DO_STMT:
17141 stmt = begin_do_stmt ();
17142 {
17143 bool prev = note_iteration_stmt_body_start ();
17144 RECUR (DO_BODY (t));
17145 note_iteration_stmt_body_end (prev);
17146 }
17147 finish_do_body (stmt);
17148 tmp = RECUR (DO_COND (t));
17149 finish_do_stmt (tmp, stmt, false, 0);
17150 break;
17151
17152 case IF_STMT:
17153 stmt = begin_if_stmt ();
17154 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17155 if (IF_STMT_CONSTEXPR_P (t))
17156 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17157 tmp = RECUR (IF_COND (t));
17158 tmp = finish_if_stmt_cond (tmp, stmt);
17159 if (IF_STMT_CONSTEXPR_P (t)
17160 && instantiation_dependent_expression_p (tmp))
17161 {
17162 /* We're partially instantiating a generic lambda, but the condition
17163 of the constexpr if is still dependent. Don't substitute into the
17164 branches now, just remember the template arguments. */
17165 do_poplevel (IF_SCOPE (stmt));
17166 IF_COND (stmt) = IF_COND (t);
17167 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17168 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17169 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17170 add_stmt (stmt);
17171 break;
17172 }
17173 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17174 /* Don't instantiate the THEN_CLAUSE. */;
17175 else
17176 {
17177 tree folded = fold_non_dependent_expr (tmp, complain);
17178 bool inhibit = integer_zerop (folded);
17179 if (inhibit)
17180 ++c_inhibit_evaluation_warnings;
17181 RECUR (THEN_CLAUSE (t));
17182 if (inhibit)
17183 --c_inhibit_evaluation_warnings;
17184 }
17185 finish_then_clause (stmt);
17186
17187 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17188 /* Don't instantiate the ELSE_CLAUSE. */;
17189 else if (ELSE_CLAUSE (t))
17190 {
17191 tree folded = fold_non_dependent_expr (tmp, complain);
17192 bool inhibit = integer_nonzerop (folded);
17193 begin_else_clause (stmt);
17194 if (inhibit)
17195 ++c_inhibit_evaluation_warnings;
17196 RECUR (ELSE_CLAUSE (t));
17197 if (inhibit)
17198 --c_inhibit_evaluation_warnings;
17199 finish_else_clause (stmt);
17200 }
17201
17202 finish_if_stmt (stmt);
17203 break;
17204
17205 case BIND_EXPR:
17206 if (BIND_EXPR_BODY_BLOCK (t))
17207 stmt = begin_function_body ();
17208 else
17209 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17210 ? BCS_TRY_BLOCK : 0);
17211
17212 RECUR (BIND_EXPR_BODY (t));
17213
17214 if (BIND_EXPR_BODY_BLOCK (t))
17215 finish_function_body (stmt);
17216 else
17217 finish_compound_stmt (stmt);
17218 break;
17219
17220 case BREAK_STMT:
17221 finish_break_stmt ();
17222 break;
17223
17224 case CONTINUE_STMT:
17225 finish_continue_stmt ();
17226 break;
17227
17228 case SWITCH_STMT:
17229 stmt = begin_switch_stmt ();
17230 tmp = RECUR (SWITCH_STMT_COND (t));
17231 finish_switch_cond (tmp, stmt);
17232 RECUR (SWITCH_STMT_BODY (t));
17233 finish_switch_stmt (stmt);
17234 break;
17235
17236 case CASE_LABEL_EXPR:
17237 {
17238 tree decl = CASE_LABEL (t);
17239 tree low = RECUR (CASE_LOW (t));
17240 tree high = RECUR (CASE_HIGH (t));
17241 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17242 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17243 {
17244 tree label = CASE_LABEL (l);
17245 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17246 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17247 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17248 }
17249 }
17250 break;
17251
17252 case LABEL_EXPR:
17253 {
17254 tree decl = LABEL_EXPR_LABEL (t);
17255 tree label;
17256
17257 label = finish_label_stmt (DECL_NAME (decl));
17258 if (TREE_CODE (label) == LABEL_DECL)
17259 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17260 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17261 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17262 }
17263 break;
17264
17265 case GOTO_EXPR:
17266 tmp = GOTO_DESTINATION (t);
17267 if (TREE_CODE (tmp) != LABEL_DECL)
17268 /* Computed goto's must be tsubst'd into. On the other hand,
17269 non-computed gotos must not be; the identifier in question
17270 will have no binding. */
17271 tmp = RECUR (tmp);
17272 else
17273 tmp = DECL_NAME (tmp);
17274 finish_goto_stmt (tmp);
17275 break;
17276
17277 case ASM_EXPR:
17278 {
17279 tree string = RECUR (ASM_STRING (t));
17280 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17281 complain, in_decl);
17282 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17283 complain, in_decl);
17284 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17285 complain, in_decl);
17286 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17287 complain, in_decl);
17288 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17289 clobbers, labels, ASM_INLINE_P (t));
17290 tree asm_expr = tmp;
17291 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17292 asm_expr = TREE_OPERAND (asm_expr, 0);
17293 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17294 }
17295 break;
17296
17297 case TRY_BLOCK:
17298 if (CLEANUP_P (t))
17299 {
17300 stmt = begin_try_block ();
17301 RECUR (TRY_STMTS (t));
17302 finish_cleanup_try_block (stmt);
17303 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17304 }
17305 else
17306 {
17307 tree compound_stmt = NULL_TREE;
17308
17309 if (FN_TRY_BLOCK_P (t))
17310 stmt = begin_function_try_block (&compound_stmt);
17311 else
17312 stmt = begin_try_block ();
17313
17314 RECUR (TRY_STMTS (t));
17315
17316 if (FN_TRY_BLOCK_P (t))
17317 finish_function_try_block (stmt);
17318 else
17319 finish_try_block (stmt);
17320
17321 RECUR (TRY_HANDLERS (t));
17322 if (FN_TRY_BLOCK_P (t))
17323 finish_function_handler_sequence (stmt, compound_stmt);
17324 else
17325 finish_handler_sequence (stmt);
17326 }
17327 break;
17328
17329 case HANDLER:
17330 {
17331 tree decl = HANDLER_PARMS (t);
17332
17333 if (decl)
17334 {
17335 decl = tsubst (decl, args, complain, in_decl);
17336 /* Prevent instantiate_decl from trying to instantiate
17337 this variable. We've already done all that needs to be
17338 done. */
17339 if (decl != error_mark_node)
17340 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17341 }
17342 stmt = begin_handler ();
17343 finish_handler_parms (decl, stmt);
17344 RECUR (HANDLER_BODY (t));
17345 finish_handler (stmt);
17346 }
17347 break;
17348
17349 case TAG_DEFN:
17350 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17351 if (CLASS_TYPE_P (tmp))
17352 {
17353 /* Local classes are not independent templates; they are
17354 instantiated along with their containing function. And this
17355 way we don't have to deal with pushing out of one local class
17356 to instantiate a member of another local class. */
17357 /* Closures are handled by the LAMBDA_EXPR. */
17358 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17359 complete_type (tmp);
17360 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17361 if ((VAR_P (fld)
17362 || (TREE_CODE (fld) == FUNCTION_DECL
17363 && !DECL_ARTIFICIAL (fld)))
17364 && DECL_TEMPLATE_INSTANTIATION (fld))
17365 instantiate_decl (fld, /*defer_ok=*/false,
17366 /*expl_inst_class=*/false);
17367 }
17368 break;
17369
17370 case STATIC_ASSERT:
17371 {
17372 tree condition;
17373
17374 ++c_inhibit_evaluation_warnings;
17375 condition =
17376 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17377 args,
17378 complain, in_decl,
17379 /*integral_constant_expression_p=*/true);
17380 --c_inhibit_evaluation_warnings;
17381
17382 finish_static_assert (condition,
17383 STATIC_ASSERT_MESSAGE (t),
17384 STATIC_ASSERT_SOURCE_LOCATION (t),
17385 /*member_p=*/false);
17386 }
17387 break;
17388
17389 case OACC_KERNELS:
17390 case OACC_PARALLEL:
17391 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17392 in_decl);
17393 stmt = begin_omp_parallel ();
17394 RECUR (OMP_BODY (t));
17395 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17396 break;
17397
17398 case OMP_PARALLEL:
17399 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17400 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17401 complain, in_decl);
17402 if (OMP_PARALLEL_COMBINED (t))
17403 omp_parallel_combined_clauses = &tmp;
17404 stmt = begin_omp_parallel ();
17405 RECUR (OMP_PARALLEL_BODY (t));
17406 gcc_assert (omp_parallel_combined_clauses == NULL);
17407 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17408 = OMP_PARALLEL_COMBINED (t);
17409 pop_omp_privatization_clauses (r);
17410 break;
17411
17412 case OMP_TASK:
17413 if (OMP_TASK_BODY (t) == NULL_TREE)
17414 {
17415 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17416 complain, in_decl);
17417 t = copy_node (t);
17418 OMP_TASK_CLAUSES (t) = tmp;
17419 add_stmt (t);
17420 break;
17421 }
17422 r = push_omp_privatization_clauses (false);
17423 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17424 complain, in_decl);
17425 stmt = begin_omp_task ();
17426 RECUR (OMP_TASK_BODY (t));
17427 finish_omp_task (tmp, stmt);
17428 pop_omp_privatization_clauses (r);
17429 break;
17430
17431 case OMP_FOR:
17432 case OMP_SIMD:
17433 case OMP_DISTRIBUTE:
17434 case OMP_TASKLOOP:
17435 case OACC_LOOP:
17436 {
17437 tree clauses, body, pre_body;
17438 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17439 tree orig_declv = NULL_TREE;
17440 tree incrv = NULL_TREE;
17441 enum c_omp_region_type ort = C_ORT_OMP;
17442 bool any_range_for = false;
17443 int i;
17444
17445 if (TREE_CODE (t) == OACC_LOOP)
17446 ort = C_ORT_ACC;
17447
17448 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17449 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17450 in_decl);
17451 if (OMP_FOR_INIT (t) != NULL_TREE)
17452 {
17453 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17454 if (OMP_FOR_ORIG_DECLS (t))
17455 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17456 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17457 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17458 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17459 }
17460
17461 keep_next_level (true);
17462 stmt = begin_omp_structured_block ();
17463
17464 pre_body = push_stmt_list ();
17465 RECUR (OMP_FOR_PRE_BODY (t));
17466 pre_body = pop_stmt_list (pre_body);
17467
17468 if (OMP_FOR_INIT (t) != NULL_TREE)
17469 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17470 any_range_for
17471 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17472 condv, incrv, &clauses, args,
17473 complain, in_decl,
17474 integral_constant_expression_p);
17475 omp_parallel_combined_clauses = NULL;
17476
17477 if (any_range_for)
17478 {
17479 gcc_assert (orig_declv);
17480 body = begin_omp_structured_block ();
17481 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17482 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17483 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17484 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17485 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17486 TREE_VEC_ELT (declv, i));
17487 }
17488 else
17489 body = push_stmt_list ();
17490 RECUR (OMP_FOR_BODY (t));
17491 if (any_range_for)
17492 body = finish_omp_structured_block (body);
17493 else
17494 body = pop_stmt_list (body);
17495
17496 if (OMP_FOR_INIT (t) != NULL_TREE)
17497 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17498 orig_declv, initv, condv, incrv, body, pre_body,
17499 NULL, clauses);
17500 else
17501 {
17502 t = make_node (TREE_CODE (t));
17503 TREE_TYPE (t) = void_type_node;
17504 OMP_FOR_BODY (t) = body;
17505 OMP_FOR_PRE_BODY (t) = pre_body;
17506 OMP_FOR_CLAUSES (t) = clauses;
17507 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17508 add_stmt (t);
17509 }
17510
17511 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17512 t));
17513 pop_omp_privatization_clauses (r);
17514 }
17515 break;
17516
17517 case OMP_SECTIONS:
17518 omp_parallel_combined_clauses = NULL;
17519 /* FALLTHRU */
17520 case OMP_SINGLE:
17521 case OMP_TEAMS:
17522 case OMP_CRITICAL:
17523 case OMP_TASKGROUP:
17524 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17525 && OMP_TEAMS_COMBINED (t));
17526 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17527 in_decl);
17528 if (TREE_CODE (t) == OMP_TEAMS)
17529 {
17530 keep_next_level (true);
17531 stmt = begin_omp_structured_block ();
17532 RECUR (OMP_BODY (t));
17533 stmt = finish_omp_structured_block (stmt);
17534 }
17535 else
17536 {
17537 stmt = push_stmt_list ();
17538 RECUR (OMP_BODY (t));
17539 stmt = pop_stmt_list (stmt);
17540 }
17541
17542 t = copy_node (t);
17543 OMP_BODY (t) = stmt;
17544 OMP_CLAUSES (t) = tmp;
17545 add_stmt (t);
17546 pop_omp_privatization_clauses (r);
17547 break;
17548
17549 case OMP_DEPOBJ:
17550 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17551 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17552 {
17553 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17554 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17555 {
17556 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17557 args, complain, in_decl);
17558 if (tmp == NULL_TREE)
17559 tmp = error_mark_node;
17560 }
17561 else
17562 {
17563 kind = (enum omp_clause_depend_kind)
17564 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17565 tmp = NULL_TREE;
17566 }
17567 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17568 }
17569 else
17570 finish_omp_depobj (EXPR_LOCATION (t), r,
17571 OMP_CLAUSE_DEPEND_SOURCE,
17572 OMP_DEPOBJ_CLAUSES (t));
17573 break;
17574
17575 case OACC_DATA:
17576 case OMP_TARGET_DATA:
17577 case OMP_TARGET:
17578 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17579 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17580 in_decl);
17581 keep_next_level (true);
17582 stmt = begin_omp_structured_block ();
17583
17584 RECUR (OMP_BODY (t));
17585 stmt = finish_omp_structured_block (stmt);
17586
17587 t = copy_node (t);
17588 OMP_BODY (t) = stmt;
17589 OMP_CLAUSES (t) = tmp;
17590 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17591 {
17592 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17593 if (teams)
17594 {
17595 /* For combined target teams, ensure the num_teams and
17596 thread_limit clause expressions are evaluated on the host,
17597 before entering the target construct. */
17598 tree c;
17599 for (c = OMP_TEAMS_CLAUSES (teams);
17600 c; c = OMP_CLAUSE_CHAIN (c))
17601 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17602 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17603 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17604 {
17605 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17606 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17607 if (expr == error_mark_node)
17608 continue;
17609 tmp = TARGET_EXPR_SLOT (expr);
17610 add_stmt (expr);
17611 OMP_CLAUSE_OPERAND (c, 0) = expr;
17612 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17613 OMP_CLAUSE_FIRSTPRIVATE);
17614 OMP_CLAUSE_DECL (tc) = tmp;
17615 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17616 OMP_TARGET_CLAUSES (t) = tc;
17617 }
17618 }
17619 }
17620 add_stmt (t);
17621 break;
17622
17623 case OACC_DECLARE:
17624 t = copy_node (t);
17625 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17626 complain, in_decl);
17627 OACC_DECLARE_CLAUSES (t) = tmp;
17628 add_stmt (t);
17629 break;
17630
17631 case OMP_TARGET_UPDATE:
17632 case OMP_TARGET_ENTER_DATA:
17633 case OMP_TARGET_EXIT_DATA:
17634 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17635 complain, in_decl);
17636 t = copy_node (t);
17637 OMP_STANDALONE_CLAUSES (t) = tmp;
17638 add_stmt (t);
17639 break;
17640
17641 case OACC_ENTER_DATA:
17642 case OACC_EXIT_DATA:
17643 case OACC_UPDATE:
17644 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17645 complain, in_decl);
17646 t = copy_node (t);
17647 OMP_STANDALONE_CLAUSES (t) = tmp;
17648 add_stmt (t);
17649 break;
17650
17651 case OMP_ORDERED:
17652 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17653 complain, in_decl);
17654 stmt = push_stmt_list ();
17655 RECUR (OMP_BODY (t));
17656 stmt = pop_stmt_list (stmt);
17657
17658 t = copy_node (t);
17659 OMP_BODY (t) = stmt;
17660 OMP_ORDERED_CLAUSES (t) = tmp;
17661 add_stmt (t);
17662 break;
17663
17664 case OMP_SECTION:
17665 case OMP_MASTER:
17666 stmt = push_stmt_list ();
17667 RECUR (OMP_BODY (t));
17668 stmt = pop_stmt_list (stmt);
17669
17670 t = copy_node (t);
17671 OMP_BODY (t) = stmt;
17672 add_stmt (t);
17673 break;
17674
17675 case OMP_ATOMIC:
17676 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17677 tmp = NULL_TREE;
17678 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17679 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17680 complain, in_decl);
17681 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17682 {
17683 tree op1 = TREE_OPERAND (t, 1);
17684 tree rhs1 = NULL_TREE;
17685 tree lhs, rhs;
17686 if (TREE_CODE (op1) == COMPOUND_EXPR)
17687 {
17688 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17689 op1 = TREE_OPERAND (op1, 1);
17690 }
17691 lhs = RECUR (TREE_OPERAND (op1, 0));
17692 rhs = RECUR (TREE_OPERAND (op1, 1));
17693 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17694 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17695 OMP_ATOMIC_MEMORY_ORDER (t));
17696 }
17697 else
17698 {
17699 tree op1 = TREE_OPERAND (t, 1);
17700 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17701 tree rhs1 = NULL_TREE;
17702 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17703 enum tree_code opcode = NOP_EXPR;
17704 if (code == OMP_ATOMIC_READ)
17705 {
17706 v = RECUR (TREE_OPERAND (op1, 0));
17707 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17708 }
17709 else if (code == OMP_ATOMIC_CAPTURE_OLD
17710 || code == OMP_ATOMIC_CAPTURE_NEW)
17711 {
17712 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17713 v = RECUR (TREE_OPERAND (op1, 0));
17714 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17715 if (TREE_CODE (op11) == COMPOUND_EXPR)
17716 {
17717 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17718 op11 = TREE_OPERAND (op11, 1);
17719 }
17720 lhs = RECUR (TREE_OPERAND (op11, 0));
17721 rhs = RECUR (TREE_OPERAND (op11, 1));
17722 opcode = TREE_CODE (op11);
17723 if (opcode == MODIFY_EXPR)
17724 opcode = NOP_EXPR;
17725 }
17726 else
17727 {
17728 code = OMP_ATOMIC;
17729 lhs = RECUR (TREE_OPERAND (op1, 0));
17730 rhs = RECUR (TREE_OPERAND (op1, 1));
17731 }
17732 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17733 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17734 }
17735 break;
17736
17737 case TRANSACTION_EXPR:
17738 {
17739 int flags = 0;
17740 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17741 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17742
17743 if (TRANSACTION_EXPR_IS_STMT (t))
17744 {
17745 tree body = TRANSACTION_EXPR_BODY (t);
17746 tree noex = NULL_TREE;
17747 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17748 {
17749 noex = MUST_NOT_THROW_COND (body);
17750 if (noex == NULL_TREE)
17751 noex = boolean_true_node;
17752 body = TREE_OPERAND (body, 0);
17753 }
17754 stmt = begin_transaction_stmt (input_location, NULL, flags);
17755 RECUR (body);
17756 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17757 }
17758 else
17759 {
17760 stmt = build_transaction_expr (EXPR_LOCATION (t),
17761 RECUR (TRANSACTION_EXPR_BODY (t)),
17762 flags, NULL_TREE);
17763 RETURN (stmt);
17764 }
17765 }
17766 break;
17767
17768 case MUST_NOT_THROW_EXPR:
17769 {
17770 tree op0 = RECUR (TREE_OPERAND (t, 0));
17771 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17772 RETURN (build_must_not_throw_expr (op0, cond));
17773 }
17774
17775 case EXPR_PACK_EXPANSION:
17776 error ("invalid use of pack expansion expression");
17777 RETURN (error_mark_node);
17778
17779 case NONTYPE_ARGUMENT_PACK:
17780 error ("use %<...%> to expand argument pack");
17781 RETURN (error_mark_node);
17782
17783 case COMPOUND_EXPR:
17784 tmp = RECUR (TREE_OPERAND (t, 0));
17785 if (tmp == NULL_TREE)
17786 /* If the first operand was a statement, we're done with it. */
17787 RETURN (RECUR (TREE_OPERAND (t, 1)));
17788 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17789 RECUR (TREE_OPERAND (t, 1)),
17790 complain));
17791
17792 case ANNOTATE_EXPR:
17793 tmp = RECUR (TREE_OPERAND (t, 0));
17794 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17795 TREE_TYPE (tmp), tmp,
17796 RECUR (TREE_OPERAND (t, 1)),
17797 RECUR (TREE_OPERAND (t, 2))));
17798
17799 case PREDICT_EXPR:
17800 RETURN (add_stmt (copy_node (t)));
17801
17802 default:
17803 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17804
17805 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17806 /*function_p=*/false,
17807 integral_constant_expression_p));
17808 }
17809
17810 RETURN (NULL_TREE);
17811 out:
17812 input_location = loc;
17813 return r;
17814 #undef RECUR
17815 #undef RETURN
17816 }
17817
17818 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17819 function. For description of the body see comment above
17820 cp_parser_omp_declare_reduction_exprs. */
17821
17822 static void
17823 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17824 {
17825 if (t == NULL_TREE || t == error_mark_node)
17826 return;
17827
17828 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17829
17830 tree_stmt_iterator tsi;
17831 int i;
17832 tree stmts[7];
17833 memset (stmts, 0, sizeof stmts);
17834 for (i = 0, tsi = tsi_start (t);
17835 i < 7 && !tsi_end_p (tsi);
17836 i++, tsi_next (&tsi))
17837 stmts[i] = tsi_stmt (tsi);
17838 gcc_assert (tsi_end_p (tsi));
17839
17840 if (i >= 3)
17841 {
17842 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17843 && TREE_CODE (stmts[1]) == DECL_EXPR);
17844 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17845 args, complain, in_decl);
17846 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17847 args, complain, in_decl);
17848 DECL_CONTEXT (omp_out) = current_function_decl;
17849 DECL_CONTEXT (omp_in) = current_function_decl;
17850 keep_next_level (true);
17851 tree block = begin_omp_structured_block ();
17852 tsubst_expr (stmts[2], args, complain, in_decl, false);
17853 block = finish_omp_structured_block (block);
17854 block = maybe_cleanup_point_expr_void (block);
17855 add_decl_expr (omp_out);
17856 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17857 TREE_NO_WARNING (omp_out) = 1;
17858 add_decl_expr (omp_in);
17859 finish_expr_stmt (block);
17860 }
17861 if (i >= 6)
17862 {
17863 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17864 && TREE_CODE (stmts[4]) == DECL_EXPR);
17865 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17866 args, complain, in_decl);
17867 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17868 args, complain, in_decl);
17869 DECL_CONTEXT (omp_priv) = current_function_decl;
17870 DECL_CONTEXT (omp_orig) = current_function_decl;
17871 keep_next_level (true);
17872 tree block = begin_omp_structured_block ();
17873 tsubst_expr (stmts[5], args, complain, in_decl, false);
17874 block = finish_omp_structured_block (block);
17875 block = maybe_cleanup_point_expr_void (block);
17876 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17877 add_decl_expr (omp_priv);
17878 add_decl_expr (omp_orig);
17879 finish_expr_stmt (block);
17880 if (i == 7)
17881 add_decl_expr (omp_orig);
17882 }
17883 }
17884
17885 /* T is a postfix-expression that is not being used in a function
17886 call. Return the substituted version of T. */
17887
17888 static tree
17889 tsubst_non_call_postfix_expression (tree t, tree args,
17890 tsubst_flags_t complain,
17891 tree in_decl)
17892 {
17893 if (TREE_CODE (t) == SCOPE_REF)
17894 t = tsubst_qualified_id (t, args, complain, in_decl,
17895 /*done=*/false, /*address_p=*/false);
17896 else
17897 t = tsubst_copy_and_build (t, args, complain, in_decl,
17898 /*function_p=*/false,
17899 /*integral_constant_expression_p=*/false);
17900
17901 return t;
17902 }
17903
17904 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17905 instantiation context. Instantiating a pack expansion containing a lambda
17906 might result in multiple lambdas all based on the same lambda in the
17907 template. */
17908
17909 tree
17910 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17911 {
17912 tree oldfn = lambda_function (t);
17913 in_decl = oldfn;
17914
17915 tree r = build_lambda_expr ();
17916
17917 LAMBDA_EXPR_LOCATION (r)
17918 = LAMBDA_EXPR_LOCATION (t);
17919 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17920 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17921 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17922 LAMBDA_EXPR_INSTANTIATED (r) = true;
17923
17924 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17925 /* A lambda in a default argument outside a class gets no
17926 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17927 tsubst_default_argument calls start_lambda_scope, so we need to
17928 specifically ignore it here, and use the global scope. */
17929 record_null_lambda_scope (r);
17930 else
17931 record_lambda_scope (r);
17932
17933 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17934 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17935
17936 vec<tree,va_gc>* field_packs = NULL;
17937
17938 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17939 cap = TREE_CHAIN (cap))
17940 {
17941 tree ofield = TREE_PURPOSE (cap);
17942 if (PACK_EXPANSION_P (ofield))
17943 ofield = PACK_EXPANSION_PATTERN (ofield);
17944 tree field = tsubst_decl (ofield, args, complain);
17945
17946 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
17947 {
17948 /* Remember these for when we've pushed local_specializations. */
17949 vec_safe_push (field_packs, ofield);
17950 vec_safe_push (field_packs, field);
17951 }
17952
17953 if (field == error_mark_node)
17954 return error_mark_node;
17955
17956 tree init = TREE_VALUE (cap);
17957 if (PACK_EXPANSION_P (init))
17958 init = tsubst_pack_expansion (init, args, complain, in_decl);
17959 else
17960 init = tsubst_copy_and_build (init, args, complain, in_decl,
17961 /*fn*/false, /*constexpr*/false);
17962
17963 if (TREE_CODE (field) == TREE_VEC)
17964 {
17965 int len = TREE_VEC_LENGTH (field);
17966 gcc_assert (TREE_CODE (init) == TREE_VEC
17967 && TREE_VEC_LENGTH (init) == len);
17968 for (int i = 0; i < len; ++i)
17969 LAMBDA_EXPR_CAPTURE_LIST (r)
17970 = tree_cons (TREE_VEC_ELT (field, i),
17971 TREE_VEC_ELT (init, i),
17972 LAMBDA_EXPR_CAPTURE_LIST (r));
17973 }
17974 else
17975 {
17976 LAMBDA_EXPR_CAPTURE_LIST (r)
17977 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17978
17979 if (id_equal (DECL_NAME (field), "__this"))
17980 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17981 }
17982 }
17983
17984 tree type = begin_lambda_type (r);
17985 if (type == error_mark_node)
17986 return error_mark_node;
17987
17988 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17989 determine_visibility (TYPE_NAME (type));
17990
17991 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17992
17993 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17994 ? DECL_TI_TEMPLATE (oldfn)
17995 : NULL_TREE);
17996
17997 tree fntype = static_fn_type (oldfn);
17998 if (oldtmpl)
17999 ++processing_template_decl;
18000 fntype = tsubst (fntype, args, complain, in_decl);
18001 if (oldtmpl)
18002 --processing_template_decl;
18003
18004 if (fntype == error_mark_node)
18005 r = error_mark_node;
18006 else
18007 {
18008 /* Fix the type of 'this'. */
18009 fntype = build_memfn_type (fntype, type,
18010 type_memfn_quals (fntype),
18011 type_memfn_rqual (fntype));
18012 tree fn, tmpl;
18013 if (oldtmpl)
18014 {
18015 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18016 fn = DECL_TEMPLATE_RESULT (tmpl);
18017 finish_member_declaration (tmpl);
18018 }
18019 else
18020 {
18021 tmpl = NULL_TREE;
18022 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18023 finish_member_declaration (fn);
18024 }
18025
18026 /* Let finish_function set this. */
18027 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18028
18029 /* The body of a lambda-expression is not a subexpression of the
18030 enclosing expression. */
18031 cp_evaluated ev;
18032
18033 bool nested = cfun;
18034 if (nested)
18035 push_function_context ();
18036 else
18037 /* Still increment function_depth so that we don't GC in the
18038 middle of an expression. */
18039 ++function_depth;
18040
18041 local_specialization_stack s (lss_copy);
18042
18043 tree body = start_lambda_function (fn, r);
18044
18045 /* Now record them for lookup_init_capture_pack. */
18046 int fplen = vec_safe_length (field_packs);
18047 for (int i = 0; i < fplen; )
18048 {
18049 tree pack = (*field_packs)[i++];
18050 tree inst = (*field_packs)[i++];
18051 register_local_specialization (inst, pack);
18052 }
18053 release_tree_vector (field_packs);
18054
18055 register_parameter_specializations (oldfn, fn);
18056
18057 if (oldtmpl)
18058 {
18059 /* We might not partially instantiate some parts of the function, so
18060 copy these flags from the original template. */
18061 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18062 current_function_returns_value = ol->returns_value;
18063 current_function_returns_null = ol->returns_null;
18064 current_function_returns_abnormally = ol->returns_abnormally;
18065 current_function_infinite_loop = ol->infinite_loop;
18066 }
18067
18068 /* [temp.deduct] A lambda-expression appearing in a function type or a
18069 template parameter is not considered part of the immediate context for
18070 the purposes of template argument deduction. */
18071 complain = tf_warning_or_error;
18072
18073 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18074 /*constexpr*/false);
18075
18076 finish_lambda_function (body);
18077
18078 if (nested)
18079 pop_function_context ();
18080 else
18081 --function_depth;
18082
18083 /* The capture list was built up in reverse order; fix that now. */
18084 LAMBDA_EXPR_CAPTURE_LIST (r)
18085 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18086
18087 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18088
18089 maybe_add_lambda_conv_op (type);
18090 }
18091
18092 finish_struct (type, /*attr*/NULL_TREE);
18093
18094 insert_pending_capture_proxies ();
18095
18096 return r;
18097 }
18098
18099 /* Like tsubst but deals with expressions and performs semantic
18100 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18101
18102 tree
18103 tsubst_copy_and_build (tree t,
18104 tree args,
18105 tsubst_flags_t complain,
18106 tree in_decl,
18107 bool function_p,
18108 bool integral_constant_expression_p)
18109 {
18110 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18111 #define RECUR(NODE) \
18112 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18113 /*function_p=*/false, \
18114 integral_constant_expression_p)
18115
18116 tree retval, op1;
18117 location_t loc;
18118
18119 if (t == NULL_TREE || t == error_mark_node)
18120 return t;
18121
18122 loc = input_location;
18123 if (location_t eloc = cp_expr_location (t))
18124 input_location = eloc;
18125
18126 /* N3276 decltype magic only applies to calls at the top level or on the
18127 right side of a comma. */
18128 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18129 complain &= ~tf_decltype;
18130
18131 switch (TREE_CODE (t))
18132 {
18133 case USING_DECL:
18134 t = DECL_NAME (t);
18135 /* Fall through. */
18136 case IDENTIFIER_NODE:
18137 {
18138 tree decl;
18139 cp_id_kind idk;
18140 bool non_integral_constant_expression_p;
18141 const char *error_msg;
18142
18143 if (IDENTIFIER_CONV_OP_P (t))
18144 {
18145 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18146 t = make_conv_op_name (new_type);
18147 }
18148
18149 /* Look up the name. */
18150 decl = lookup_name (t);
18151
18152 /* By convention, expressions use ERROR_MARK_NODE to indicate
18153 failure, not NULL_TREE. */
18154 if (decl == NULL_TREE)
18155 decl = error_mark_node;
18156
18157 decl = finish_id_expression (t, decl, NULL_TREE,
18158 &idk,
18159 integral_constant_expression_p,
18160 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18161 &non_integral_constant_expression_p,
18162 /*template_p=*/false,
18163 /*done=*/true,
18164 /*address_p=*/false,
18165 /*template_arg_p=*/false,
18166 &error_msg,
18167 input_location);
18168 if (error_msg)
18169 error (error_msg);
18170 if (!function_p && identifier_p (decl))
18171 {
18172 if (complain & tf_error)
18173 unqualified_name_lookup_error (decl);
18174 decl = error_mark_node;
18175 }
18176 RETURN (decl);
18177 }
18178
18179 case TEMPLATE_ID_EXPR:
18180 {
18181 tree object;
18182 tree templ = RECUR (TREE_OPERAND (t, 0));
18183 tree targs = TREE_OPERAND (t, 1);
18184
18185 if (targs)
18186 targs = tsubst_template_args (targs, args, complain, in_decl);
18187 if (targs == error_mark_node)
18188 RETURN (error_mark_node);
18189
18190 if (TREE_CODE (templ) == SCOPE_REF)
18191 {
18192 tree name = TREE_OPERAND (templ, 1);
18193 tree tid = lookup_template_function (name, targs);
18194 TREE_OPERAND (templ, 1) = tid;
18195 RETURN (templ);
18196 }
18197
18198 if (variable_template_p (templ))
18199 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18200
18201 if (TREE_CODE (templ) == COMPONENT_REF)
18202 {
18203 object = TREE_OPERAND (templ, 0);
18204 templ = TREE_OPERAND (templ, 1);
18205 }
18206 else
18207 object = NULL_TREE;
18208 templ = lookup_template_function (templ, targs);
18209
18210 if (object)
18211 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18212 object, templ, NULL_TREE));
18213 else
18214 RETURN (baselink_for_fns (templ));
18215 }
18216
18217 case INDIRECT_REF:
18218 {
18219 tree r = RECUR (TREE_OPERAND (t, 0));
18220
18221 if (REFERENCE_REF_P (t))
18222 {
18223 /* A type conversion to reference type will be enclosed in
18224 such an indirect ref, but the substitution of the cast
18225 will have also added such an indirect ref. */
18226 r = convert_from_reference (r);
18227 }
18228 else
18229 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18230 complain|decltype_flag);
18231
18232 if (REF_PARENTHESIZED_P (t))
18233 r = force_paren_expr (r);
18234
18235 RETURN (r);
18236 }
18237
18238 case NOP_EXPR:
18239 {
18240 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18241 tree op0 = RECUR (TREE_OPERAND (t, 0));
18242 RETURN (build_nop (type, op0));
18243 }
18244
18245 case IMPLICIT_CONV_EXPR:
18246 {
18247 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18248 tree expr = RECUR (TREE_OPERAND (t, 0));
18249 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18250 {
18251 retval = copy_node (t);
18252 TREE_TYPE (retval) = type;
18253 TREE_OPERAND (retval, 0) = expr;
18254 RETURN (retval);
18255 }
18256 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18257 /* We'll pass this to convert_nontype_argument again, we don't need
18258 to actually perform any conversion here. */
18259 RETURN (expr);
18260 int flags = LOOKUP_IMPLICIT;
18261 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18262 flags = LOOKUP_NORMAL;
18263 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18264 flags |= LOOKUP_NO_NARROWING;
18265 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18266 flags));
18267 }
18268
18269 case CONVERT_EXPR:
18270 {
18271 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18272 tree op0 = RECUR (TREE_OPERAND (t, 0));
18273 if (op0 == error_mark_node)
18274 RETURN (error_mark_node);
18275 RETURN (build1 (CONVERT_EXPR, type, op0));
18276 }
18277
18278 case CAST_EXPR:
18279 case REINTERPRET_CAST_EXPR:
18280 case CONST_CAST_EXPR:
18281 case DYNAMIC_CAST_EXPR:
18282 case STATIC_CAST_EXPR:
18283 {
18284 tree type;
18285 tree op, r = NULL_TREE;
18286
18287 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18288 if (integral_constant_expression_p
18289 && !cast_valid_in_integral_constant_expression_p (type))
18290 {
18291 if (complain & tf_error)
18292 error ("a cast to a type other than an integral or "
18293 "enumeration type cannot appear in a constant-expression");
18294 RETURN (error_mark_node);
18295 }
18296
18297 op = RECUR (TREE_OPERAND (t, 0));
18298
18299 warning_sentinel s(warn_useless_cast);
18300 warning_sentinel s2(warn_ignored_qualifiers);
18301 switch (TREE_CODE (t))
18302 {
18303 case CAST_EXPR:
18304 r = build_functional_cast (type, op, complain);
18305 break;
18306 case REINTERPRET_CAST_EXPR:
18307 r = build_reinterpret_cast (type, op, complain);
18308 break;
18309 case CONST_CAST_EXPR:
18310 r = build_const_cast (type, op, complain);
18311 break;
18312 case DYNAMIC_CAST_EXPR:
18313 r = build_dynamic_cast (type, op, complain);
18314 break;
18315 case STATIC_CAST_EXPR:
18316 r = build_static_cast (type, op, complain);
18317 break;
18318 default:
18319 gcc_unreachable ();
18320 }
18321
18322 RETURN (r);
18323 }
18324
18325 case POSTDECREMENT_EXPR:
18326 case POSTINCREMENT_EXPR:
18327 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18328 args, complain, in_decl);
18329 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18330 complain|decltype_flag));
18331
18332 case PREDECREMENT_EXPR:
18333 case PREINCREMENT_EXPR:
18334 case NEGATE_EXPR:
18335 case BIT_NOT_EXPR:
18336 case ABS_EXPR:
18337 case TRUTH_NOT_EXPR:
18338 case UNARY_PLUS_EXPR: /* Unary + */
18339 case REALPART_EXPR:
18340 case IMAGPART_EXPR:
18341 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18342 RECUR (TREE_OPERAND (t, 0)),
18343 complain|decltype_flag));
18344
18345 case FIX_TRUNC_EXPR:
18346 gcc_unreachable ();
18347
18348 case ADDR_EXPR:
18349 op1 = TREE_OPERAND (t, 0);
18350 if (TREE_CODE (op1) == LABEL_DECL)
18351 RETURN (finish_label_address_expr (DECL_NAME (op1),
18352 EXPR_LOCATION (op1)));
18353 if (TREE_CODE (op1) == SCOPE_REF)
18354 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18355 /*done=*/true, /*address_p=*/true);
18356 else
18357 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18358 in_decl);
18359 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18360 complain|decltype_flag));
18361
18362 case PLUS_EXPR:
18363 case MINUS_EXPR:
18364 case MULT_EXPR:
18365 case TRUNC_DIV_EXPR:
18366 case CEIL_DIV_EXPR:
18367 case FLOOR_DIV_EXPR:
18368 case ROUND_DIV_EXPR:
18369 case EXACT_DIV_EXPR:
18370 case BIT_AND_EXPR:
18371 case BIT_IOR_EXPR:
18372 case BIT_XOR_EXPR:
18373 case TRUNC_MOD_EXPR:
18374 case FLOOR_MOD_EXPR:
18375 case TRUTH_ANDIF_EXPR:
18376 case TRUTH_ORIF_EXPR:
18377 case TRUTH_AND_EXPR:
18378 case TRUTH_OR_EXPR:
18379 case RSHIFT_EXPR:
18380 case LSHIFT_EXPR:
18381 case RROTATE_EXPR:
18382 case LROTATE_EXPR:
18383 case EQ_EXPR:
18384 case NE_EXPR:
18385 case MAX_EXPR:
18386 case MIN_EXPR:
18387 case LE_EXPR:
18388 case GE_EXPR:
18389 case LT_EXPR:
18390 case GT_EXPR:
18391 case MEMBER_REF:
18392 case DOTSTAR_EXPR:
18393 {
18394 warning_sentinel s1(warn_type_limits);
18395 warning_sentinel s2(warn_div_by_zero);
18396 warning_sentinel s3(warn_logical_op);
18397 warning_sentinel s4(warn_tautological_compare);
18398 tree op0 = RECUR (TREE_OPERAND (t, 0));
18399 tree op1 = RECUR (TREE_OPERAND (t, 1));
18400 tree r = build_x_binary_op
18401 (input_location, TREE_CODE (t),
18402 op0,
18403 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18404 ? ERROR_MARK
18405 : TREE_CODE (TREE_OPERAND (t, 0))),
18406 op1,
18407 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18408 ? ERROR_MARK
18409 : TREE_CODE (TREE_OPERAND (t, 1))),
18410 /*overload=*/NULL,
18411 complain|decltype_flag);
18412 if (EXPR_P (r) && TREE_NO_WARNING (t))
18413 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18414
18415 RETURN (r);
18416 }
18417
18418 case POINTER_PLUS_EXPR:
18419 {
18420 tree op0 = RECUR (TREE_OPERAND (t, 0));
18421 tree op1 = RECUR (TREE_OPERAND (t, 1));
18422 RETURN (fold_build_pointer_plus (op0, op1));
18423 }
18424
18425 case SCOPE_REF:
18426 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18427 /*address_p=*/false));
18428 case ARRAY_REF:
18429 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18430 args, complain, in_decl);
18431 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18432 RECUR (TREE_OPERAND (t, 1)),
18433 complain|decltype_flag));
18434
18435 case SIZEOF_EXPR:
18436 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18437 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18438 RETURN (tsubst_copy (t, args, complain, in_decl));
18439 /* Fall through */
18440
18441 case ALIGNOF_EXPR:
18442 {
18443 tree r;
18444
18445 op1 = TREE_OPERAND (t, 0);
18446 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18447 op1 = TREE_TYPE (op1);
18448 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18449 && ALIGNOF_EXPR_STD_P (t));
18450 if (!args)
18451 {
18452 /* When there are no ARGS, we are trying to evaluate a
18453 non-dependent expression from the parser. Trying to do
18454 the substitutions may not work. */
18455 if (!TYPE_P (op1))
18456 op1 = TREE_TYPE (op1);
18457 }
18458 else
18459 {
18460 ++cp_unevaluated_operand;
18461 ++c_inhibit_evaluation_warnings;
18462 if (TYPE_P (op1))
18463 op1 = tsubst (op1, args, complain, in_decl);
18464 else
18465 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18466 /*function_p=*/false,
18467 /*integral_constant_expression_p=*/
18468 false);
18469 --cp_unevaluated_operand;
18470 --c_inhibit_evaluation_warnings;
18471 }
18472 if (TYPE_P (op1))
18473 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18474 complain & tf_error);
18475 else
18476 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18477 complain & tf_error);
18478 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18479 {
18480 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18481 {
18482 if (!processing_template_decl && TYPE_P (op1))
18483 {
18484 r = build_min (SIZEOF_EXPR, size_type_node,
18485 build1 (NOP_EXPR, op1, error_mark_node));
18486 SIZEOF_EXPR_TYPE_P (r) = 1;
18487 }
18488 else
18489 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18490 TREE_SIDE_EFFECTS (r) = 0;
18491 TREE_READONLY (r) = 1;
18492 }
18493 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18494 }
18495 RETURN (r);
18496 }
18497
18498 case AT_ENCODE_EXPR:
18499 {
18500 op1 = TREE_OPERAND (t, 0);
18501 ++cp_unevaluated_operand;
18502 ++c_inhibit_evaluation_warnings;
18503 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18504 /*function_p=*/false,
18505 /*integral_constant_expression_p=*/false);
18506 --cp_unevaluated_operand;
18507 --c_inhibit_evaluation_warnings;
18508 RETURN (objc_build_encode_expr (op1));
18509 }
18510
18511 case NOEXCEPT_EXPR:
18512 op1 = TREE_OPERAND (t, 0);
18513 ++cp_unevaluated_operand;
18514 ++c_inhibit_evaluation_warnings;
18515 ++cp_noexcept_operand;
18516 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18517 /*function_p=*/false,
18518 /*integral_constant_expression_p=*/false);
18519 --cp_unevaluated_operand;
18520 --c_inhibit_evaluation_warnings;
18521 --cp_noexcept_operand;
18522 RETURN (finish_noexcept_expr (op1, complain));
18523
18524 case MODOP_EXPR:
18525 {
18526 warning_sentinel s(warn_div_by_zero);
18527 tree lhs = RECUR (TREE_OPERAND (t, 0));
18528 tree rhs = RECUR (TREE_OPERAND (t, 2));
18529 tree r = build_x_modify_expr
18530 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18531 complain|decltype_flag);
18532 /* TREE_NO_WARNING must be set if either the expression was
18533 parenthesized or it uses an operator such as >>= rather
18534 than plain assignment. In the former case, it was already
18535 set and must be copied. In the latter case,
18536 build_x_modify_expr sets it and it must not be reset
18537 here. */
18538 if (TREE_NO_WARNING (t))
18539 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18540
18541 RETURN (r);
18542 }
18543
18544 case ARROW_EXPR:
18545 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18546 args, complain, in_decl);
18547 /* Remember that there was a reference to this entity. */
18548 if (DECL_P (op1)
18549 && !mark_used (op1, complain) && !(complain & tf_error))
18550 RETURN (error_mark_node);
18551 RETURN (build_x_arrow (input_location, op1, complain));
18552
18553 case NEW_EXPR:
18554 {
18555 tree placement = RECUR (TREE_OPERAND (t, 0));
18556 tree init = RECUR (TREE_OPERAND (t, 3));
18557 vec<tree, va_gc> *placement_vec;
18558 vec<tree, va_gc> *init_vec;
18559 tree ret;
18560
18561 if (placement == NULL_TREE)
18562 placement_vec = NULL;
18563 else
18564 {
18565 placement_vec = make_tree_vector ();
18566 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18567 vec_safe_push (placement_vec, TREE_VALUE (placement));
18568 }
18569
18570 /* If there was an initializer in the original tree, but it
18571 instantiated to an empty list, then we should pass a
18572 non-NULL empty vector to tell build_new that it was an
18573 empty initializer() rather than no initializer. This can
18574 only happen when the initializer is a pack expansion whose
18575 parameter packs are of length zero. */
18576 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18577 init_vec = NULL;
18578 else
18579 {
18580 init_vec = make_tree_vector ();
18581 if (init == void_node)
18582 gcc_assert (init_vec != NULL);
18583 else
18584 {
18585 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18586 vec_safe_push (init_vec, TREE_VALUE (init));
18587 }
18588 }
18589
18590 /* Avoid passing an enclosing decl to valid_array_size_p. */
18591 in_decl = NULL_TREE;
18592
18593 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18594 tree op2 = RECUR (TREE_OPERAND (t, 2));
18595 ret = build_new (&placement_vec, op1, op2, &init_vec,
18596 NEW_EXPR_USE_GLOBAL (t),
18597 complain);
18598
18599 if (placement_vec != NULL)
18600 release_tree_vector (placement_vec);
18601 if (init_vec != NULL)
18602 release_tree_vector (init_vec);
18603
18604 RETURN (ret);
18605 }
18606
18607 case DELETE_EXPR:
18608 {
18609 tree op0 = RECUR (TREE_OPERAND (t, 0));
18610 tree op1 = RECUR (TREE_OPERAND (t, 1));
18611 RETURN (delete_sanity (op0, op1,
18612 DELETE_EXPR_USE_VEC (t),
18613 DELETE_EXPR_USE_GLOBAL (t),
18614 complain));
18615 }
18616
18617 case COMPOUND_EXPR:
18618 {
18619 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18620 complain & ~tf_decltype, in_decl,
18621 /*function_p=*/false,
18622 integral_constant_expression_p);
18623 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18624 op0,
18625 RECUR (TREE_OPERAND (t, 1)),
18626 complain|decltype_flag));
18627 }
18628
18629 case CALL_EXPR:
18630 {
18631 tree function;
18632 vec<tree, va_gc> *call_args;
18633 unsigned int nargs, i;
18634 bool qualified_p;
18635 bool koenig_p;
18636 tree ret;
18637
18638 function = CALL_EXPR_FN (t);
18639 /* Internal function with no arguments. */
18640 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18641 RETURN (t);
18642
18643 /* When we parsed the expression, we determined whether or
18644 not Koenig lookup should be performed. */
18645 koenig_p = KOENIG_LOOKUP_P (t);
18646 if (function == NULL_TREE)
18647 {
18648 koenig_p = false;
18649 qualified_p = false;
18650 }
18651 else if (TREE_CODE (function) == SCOPE_REF)
18652 {
18653 qualified_p = true;
18654 function = tsubst_qualified_id (function, args, complain, in_decl,
18655 /*done=*/false,
18656 /*address_p=*/false);
18657 }
18658 else if (koenig_p && identifier_p (function))
18659 {
18660 /* Do nothing; calling tsubst_copy_and_build on an identifier
18661 would incorrectly perform unqualified lookup again.
18662
18663 Note that we can also have an IDENTIFIER_NODE if the earlier
18664 unqualified lookup found a member function; in that case
18665 koenig_p will be false and we do want to do the lookup
18666 again to find the instantiated member function.
18667
18668 FIXME but doing that causes c++/15272, so we need to stop
18669 using IDENTIFIER_NODE in that situation. */
18670 qualified_p = false;
18671 }
18672 else
18673 {
18674 if (TREE_CODE (function) == COMPONENT_REF)
18675 {
18676 tree op = TREE_OPERAND (function, 1);
18677
18678 qualified_p = (TREE_CODE (op) == SCOPE_REF
18679 || (BASELINK_P (op)
18680 && BASELINK_QUALIFIED_P (op)));
18681 }
18682 else
18683 qualified_p = false;
18684
18685 if (TREE_CODE (function) == ADDR_EXPR
18686 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18687 /* Avoid error about taking the address of a constructor. */
18688 function = TREE_OPERAND (function, 0);
18689
18690 function = tsubst_copy_and_build (function, args, complain,
18691 in_decl,
18692 !qualified_p,
18693 integral_constant_expression_p);
18694
18695 if (BASELINK_P (function))
18696 qualified_p = true;
18697 }
18698
18699 nargs = call_expr_nargs (t);
18700 call_args = make_tree_vector ();
18701 for (i = 0; i < nargs; ++i)
18702 {
18703 tree arg = CALL_EXPR_ARG (t, i);
18704
18705 if (!PACK_EXPANSION_P (arg))
18706 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18707 else
18708 {
18709 /* Expand the pack expansion and push each entry onto
18710 CALL_ARGS. */
18711 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18712 if (TREE_CODE (arg) == TREE_VEC)
18713 {
18714 unsigned int len, j;
18715
18716 len = TREE_VEC_LENGTH (arg);
18717 for (j = 0; j < len; ++j)
18718 {
18719 tree value = TREE_VEC_ELT (arg, j);
18720 if (value != NULL_TREE)
18721 value = convert_from_reference (value);
18722 vec_safe_push (call_args, value);
18723 }
18724 }
18725 else
18726 {
18727 /* A partial substitution. Add one entry. */
18728 vec_safe_push (call_args, arg);
18729 }
18730 }
18731 }
18732
18733 /* Stripped-down processing for a call in a thunk. Specifically, in
18734 the thunk template for a generic lambda. */
18735 if (CALL_FROM_THUNK_P (t))
18736 {
18737 tree thisarg = NULL_TREE;
18738 if (TREE_CODE (function) == COMPONENT_REF)
18739 {
18740 thisarg = TREE_OPERAND (function, 0);
18741 if (TREE_CODE (thisarg) == INDIRECT_REF)
18742 thisarg = TREE_OPERAND (thisarg, 0);
18743 function = TREE_OPERAND (function, 1);
18744 if (TREE_CODE (function) == BASELINK)
18745 function = BASELINK_FUNCTIONS (function);
18746 }
18747 /* We aren't going to do normal overload resolution, so force the
18748 template-id to resolve. */
18749 function = resolve_nondeduced_context (function, complain);
18750 for (unsigned i = 0; i < nargs; ++i)
18751 {
18752 /* In a thunk, pass through args directly, without any
18753 conversions. */
18754 tree arg = (*call_args)[i];
18755 while (TREE_CODE (arg) != PARM_DECL)
18756 arg = TREE_OPERAND (arg, 0);
18757 (*call_args)[i] = arg;
18758 }
18759 if (thisarg)
18760 {
18761 /* Shift the other args over to make room. */
18762 vec_safe_push (call_args, (*call_args)[nargs-1]);
18763 for (int i = nargs-1; i > 0; --i)
18764 (*call_args)[i] = (*call_args)[i-1];
18765 (*call_args)[0] = thisarg;
18766 }
18767 ret = build_call_a (function, call_args->length (),
18768 call_args->address ());
18769 /* The thunk location is not interesting. */
18770 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18771 CALL_FROM_THUNK_P (ret) = true;
18772 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18773 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18774
18775 release_tree_vector (call_args);
18776 RETURN (ret);
18777 }
18778
18779 /* We do not perform argument-dependent lookup if normal
18780 lookup finds a non-function, in accordance with the
18781 expected resolution of DR 218. */
18782 if (koenig_p
18783 && ((is_overloaded_fn (function)
18784 /* If lookup found a member function, the Koenig lookup is
18785 not appropriate, even if an unqualified-name was used
18786 to denote the function. */
18787 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18788 || identifier_p (function))
18789 /* Only do this when substitution turns a dependent call
18790 into a non-dependent call. */
18791 && type_dependent_expression_p_push (t)
18792 && !any_type_dependent_arguments_p (call_args))
18793 function = perform_koenig_lookup (function, call_args, tf_none);
18794
18795 if (function != NULL_TREE
18796 && identifier_p (function)
18797 && !any_type_dependent_arguments_p (call_args))
18798 {
18799 if (koenig_p && (complain & tf_warning_or_error))
18800 {
18801 /* For backwards compatibility and good diagnostics, try
18802 the unqualified lookup again if we aren't in SFINAE
18803 context. */
18804 tree unq = (tsubst_copy_and_build
18805 (function, args, complain, in_decl, true,
18806 integral_constant_expression_p));
18807 if (unq == error_mark_node)
18808 {
18809 release_tree_vector (call_args);
18810 RETURN (error_mark_node);
18811 }
18812
18813 if (unq != function)
18814 {
18815 /* In a lambda fn, we have to be careful to not
18816 introduce new this captures. Legacy code can't
18817 be using lambdas anyway, so it's ok to be
18818 stricter. */
18819 bool in_lambda = (current_class_type
18820 && LAMBDA_TYPE_P (current_class_type));
18821 char const *const msg
18822 = G_("%qD was not declared in this scope, "
18823 "and no declarations were found by "
18824 "argument-dependent lookup at the point "
18825 "of instantiation");
18826
18827 bool diag = true;
18828 if (in_lambda)
18829 error_at (cp_expr_loc_or_loc (t, input_location),
18830 msg, function);
18831 else
18832 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18833 msg, function);
18834 if (diag)
18835 {
18836 tree fn = unq;
18837
18838 if (INDIRECT_REF_P (fn))
18839 fn = TREE_OPERAND (fn, 0);
18840 if (is_overloaded_fn (fn))
18841 fn = get_first_fn (fn);
18842
18843 if (!DECL_P (fn))
18844 /* Can't say anything more. */;
18845 else if (DECL_CLASS_SCOPE_P (fn))
18846 {
18847 location_t loc = cp_expr_loc_or_loc (t,
18848 input_location);
18849 inform (loc,
18850 "declarations in dependent base %qT are "
18851 "not found by unqualified lookup",
18852 DECL_CLASS_CONTEXT (fn));
18853 if (current_class_ptr)
18854 inform (loc,
18855 "use %<this->%D%> instead", function);
18856 else
18857 inform (loc,
18858 "use %<%T::%D%> instead",
18859 current_class_name, function);
18860 }
18861 else
18862 inform (DECL_SOURCE_LOCATION (fn),
18863 "%qD declared here, later in the "
18864 "translation unit", fn);
18865 if (in_lambda)
18866 {
18867 release_tree_vector (call_args);
18868 RETURN (error_mark_node);
18869 }
18870 }
18871
18872 function = unq;
18873 }
18874 }
18875 if (identifier_p (function))
18876 {
18877 if (complain & tf_error)
18878 unqualified_name_lookup_error (function);
18879 release_tree_vector (call_args);
18880 RETURN (error_mark_node);
18881 }
18882 }
18883
18884 /* Remember that there was a reference to this entity. */
18885 if (function != NULL_TREE
18886 && DECL_P (function)
18887 && !mark_used (function, complain) && !(complain & tf_error))
18888 {
18889 release_tree_vector (call_args);
18890 RETURN (error_mark_node);
18891 }
18892
18893 /* Put back tf_decltype for the actual call. */
18894 complain |= decltype_flag;
18895
18896 if (function == NULL_TREE)
18897 switch (CALL_EXPR_IFN (t))
18898 {
18899 case IFN_LAUNDER:
18900 gcc_assert (nargs == 1);
18901 if (vec_safe_length (call_args) != 1)
18902 {
18903 error_at (cp_expr_loc_or_loc (t, input_location),
18904 "wrong number of arguments to "
18905 "%<__builtin_launder%>");
18906 ret = error_mark_node;
18907 }
18908 else
18909 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
18910 input_location),
18911 (*call_args)[0], complain);
18912 break;
18913
18914 case IFN_VEC_CONVERT:
18915 gcc_assert (nargs == 1);
18916 if (vec_safe_length (call_args) != 1)
18917 {
18918 error_at (cp_expr_loc_or_loc (t, input_location),
18919 "wrong number of arguments to "
18920 "%<__builtin_convertvector%>");
18921 ret = error_mark_node;
18922 break;
18923 }
18924 ret = cp_build_vec_convert ((*call_args)[0], input_location,
18925 tsubst (TREE_TYPE (t), args,
18926 complain, in_decl),
18927 complain);
18928 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
18929 {
18930 release_tree_vector (call_args);
18931 RETURN (ret);
18932 }
18933 break;
18934
18935 default:
18936 /* Unsupported internal function with arguments. */
18937 gcc_unreachable ();
18938 }
18939 else if (TREE_CODE (function) == OFFSET_REF
18940 || TREE_CODE (function) == DOTSTAR_EXPR
18941 || TREE_CODE (function) == MEMBER_REF)
18942 ret = build_offset_ref_call_from_tree (function, &call_args,
18943 complain);
18944 else if (TREE_CODE (function) == COMPONENT_REF)
18945 {
18946 tree instance = TREE_OPERAND (function, 0);
18947 tree fn = TREE_OPERAND (function, 1);
18948
18949 if (processing_template_decl
18950 && (type_dependent_expression_p (instance)
18951 || (!BASELINK_P (fn)
18952 && TREE_CODE (fn) != FIELD_DECL)
18953 || type_dependent_expression_p (fn)
18954 || any_type_dependent_arguments_p (call_args)))
18955 ret = build_min_nt_call_vec (function, call_args);
18956 else if (!BASELINK_P (fn))
18957 ret = finish_call_expr (function, &call_args,
18958 /*disallow_virtual=*/false,
18959 /*koenig_p=*/false,
18960 complain);
18961 else
18962 ret = (build_new_method_call
18963 (instance, fn,
18964 &call_args, NULL_TREE,
18965 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18966 /*fn_p=*/NULL,
18967 complain));
18968 }
18969 else
18970 ret = finish_call_expr (function, &call_args,
18971 /*disallow_virtual=*/qualified_p,
18972 koenig_p,
18973 complain);
18974
18975 release_tree_vector (call_args);
18976
18977 if (ret != error_mark_node)
18978 {
18979 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18980 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18981 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18982 if (op || ord || rev)
18983 {
18984 function = extract_call_expr (ret);
18985 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18986 CALL_EXPR_ORDERED_ARGS (function) = ord;
18987 CALL_EXPR_REVERSE_ARGS (function) = rev;
18988 }
18989 }
18990
18991 RETURN (ret);
18992 }
18993
18994 case COND_EXPR:
18995 {
18996 tree cond = RECUR (TREE_OPERAND (t, 0));
18997 cond = mark_rvalue_use (cond);
18998 tree folded_cond = fold_non_dependent_expr (cond, complain);
18999 tree exp1, exp2;
19000
19001 if (TREE_CODE (folded_cond) == INTEGER_CST)
19002 {
19003 if (integer_zerop (folded_cond))
19004 {
19005 ++c_inhibit_evaluation_warnings;
19006 exp1 = RECUR (TREE_OPERAND (t, 1));
19007 --c_inhibit_evaluation_warnings;
19008 exp2 = RECUR (TREE_OPERAND (t, 2));
19009 }
19010 else
19011 {
19012 exp1 = RECUR (TREE_OPERAND (t, 1));
19013 ++c_inhibit_evaluation_warnings;
19014 exp2 = RECUR (TREE_OPERAND (t, 2));
19015 --c_inhibit_evaluation_warnings;
19016 }
19017 cond = folded_cond;
19018 }
19019 else
19020 {
19021 exp1 = RECUR (TREE_OPERAND (t, 1));
19022 exp2 = RECUR (TREE_OPERAND (t, 2));
19023 }
19024
19025 warning_sentinel s(warn_duplicated_branches);
19026 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19027 cond, exp1, exp2, complain));
19028 }
19029
19030 case PSEUDO_DTOR_EXPR:
19031 {
19032 tree op0 = RECUR (TREE_OPERAND (t, 0));
19033 tree op1 = RECUR (TREE_OPERAND (t, 1));
19034 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19035 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19036 input_location));
19037 }
19038
19039 case TREE_LIST:
19040 {
19041 tree purpose, value, chain;
19042
19043 if (t == void_list_node)
19044 RETURN (t);
19045
19046 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19047 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19048 {
19049 /* We have pack expansions, so expand those and
19050 create a new list out of it. */
19051 tree purposevec = NULL_TREE;
19052 tree valuevec = NULL_TREE;
19053 tree chain;
19054 int i, len = -1;
19055
19056 /* Expand the argument expressions. */
19057 if (TREE_PURPOSE (t))
19058 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19059 complain, in_decl);
19060 if (TREE_VALUE (t))
19061 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19062 complain, in_decl);
19063
19064 /* Build the rest of the list. */
19065 chain = TREE_CHAIN (t);
19066 if (chain && chain != void_type_node)
19067 chain = RECUR (chain);
19068
19069 /* Determine the number of arguments. */
19070 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19071 {
19072 len = TREE_VEC_LENGTH (purposevec);
19073 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19074 }
19075 else if (TREE_CODE (valuevec) == TREE_VEC)
19076 len = TREE_VEC_LENGTH (valuevec);
19077 else
19078 {
19079 /* Since we only performed a partial substitution into
19080 the argument pack, we only RETURN (a single list
19081 node. */
19082 if (purposevec == TREE_PURPOSE (t)
19083 && valuevec == TREE_VALUE (t)
19084 && chain == TREE_CHAIN (t))
19085 RETURN (t);
19086
19087 RETURN (tree_cons (purposevec, valuevec, chain));
19088 }
19089
19090 /* Convert the argument vectors into a TREE_LIST */
19091 i = len;
19092 while (i > 0)
19093 {
19094 /* Grab the Ith values. */
19095 i--;
19096 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19097 : NULL_TREE;
19098 value
19099 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19100 : NULL_TREE;
19101
19102 /* Build the list (backwards). */
19103 chain = tree_cons (purpose, value, chain);
19104 }
19105
19106 RETURN (chain);
19107 }
19108
19109 purpose = TREE_PURPOSE (t);
19110 if (purpose)
19111 purpose = RECUR (purpose);
19112 value = TREE_VALUE (t);
19113 if (value)
19114 value = RECUR (value);
19115 chain = TREE_CHAIN (t);
19116 if (chain && chain != void_type_node)
19117 chain = RECUR (chain);
19118 if (purpose == TREE_PURPOSE (t)
19119 && value == TREE_VALUE (t)
19120 && chain == TREE_CHAIN (t))
19121 RETURN (t);
19122 RETURN (tree_cons (purpose, value, chain));
19123 }
19124
19125 case COMPONENT_REF:
19126 {
19127 tree object;
19128 tree object_type;
19129 tree member;
19130 tree r;
19131
19132 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19133 args, complain, in_decl);
19134 /* Remember that there was a reference to this entity. */
19135 if (DECL_P (object)
19136 && !mark_used (object, complain) && !(complain & tf_error))
19137 RETURN (error_mark_node);
19138 object_type = TREE_TYPE (object);
19139
19140 member = TREE_OPERAND (t, 1);
19141 if (BASELINK_P (member))
19142 member = tsubst_baselink (member,
19143 non_reference (TREE_TYPE (object)),
19144 args, complain, in_decl);
19145 else
19146 member = tsubst_copy (member, args, complain, in_decl);
19147 if (member == error_mark_node)
19148 RETURN (error_mark_node);
19149
19150 if (TREE_CODE (member) == FIELD_DECL)
19151 {
19152 r = finish_non_static_data_member (member, object, NULL_TREE);
19153 if (TREE_CODE (r) == COMPONENT_REF)
19154 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19155 RETURN (r);
19156 }
19157 else if (type_dependent_expression_p (object))
19158 /* We can't do much here. */;
19159 else if (!CLASS_TYPE_P (object_type))
19160 {
19161 if (scalarish_type_p (object_type))
19162 {
19163 tree s = NULL_TREE;
19164 tree dtor = member;
19165
19166 if (TREE_CODE (dtor) == SCOPE_REF)
19167 {
19168 s = TREE_OPERAND (dtor, 0);
19169 dtor = TREE_OPERAND (dtor, 1);
19170 }
19171 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19172 {
19173 dtor = TREE_OPERAND (dtor, 0);
19174 if (TYPE_P (dtor))
19175 RETURN (finish_pseudo_destructor_expr
19176 (object, s, dtor, input_location));
19177 }
19178 }
19179 }
19180 else if (TREE_CODE (member) == SCOPE_REF
19181 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19182 {
19183 /* Lookup the template functions now that we know what the
19184 scope is. */
19185 tree scope = TREE_OPERAND (member, 0);
19186 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19187 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19188 member = lookup_qualified_name (scope, tmpl,
19189 /*is_type_p=*/false,
19190 /*complain=*/false);
19191 if (BASELINK_P (member))
19192 {
19193 BASELINK_FUNCTIONS (member)
19194 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19195 args);
19196 member = (adjust_result_of_qualified_name_lookup
19197 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19198 object_type));
19199 }
19200 else
19201 {
19202 qualified_name_lookup_error (scope, tmpl, member,
19203 input_location);
19204 RETURN (error_mark_node);
19205 }
19206 }
19207 else if (TREE_CODE (member) == SCOPE_REF
19208 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19209 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19210 {
19211 if (complain & tf_error)
19212 {
19213 if (TYPE_P (TREE_OPERAND (member, 0)))
19214 error ("%qT is not a class or namespace",
19215 TREE_OPERAND (member, 0));
19216 else
19217 error ("%qD is not a class or namespace",
19218 TREE_OPERAND (member, 0));
19219 }
19220 RETURN (error_mark_node);
19221 }
19222
19223 r = finish_class_member_access_expr (object, member,
19224 /*template_p=*/false,
19225 complain);
19226 if (TREE_CODE (r) == COMPONENT_REF)
19227 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19228 RETURN (r);
19229 }
19230
19231 case THROW_EXPR:
19232 RETURN (build_throw
19233 (RECUR (TREE_OPERAND (t, 0))));
19234
19235 case CONSTRUCTOR:
19236 {
19237 vec<constructor_elt, va_gc> *n;
19238 constructor_elt *ce;
19239 unsigned HOST_WIDE_INT idx;
19240 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19241 bool process_index_p;
19242 int newlen;
19243 bool need_copy_p = false;
19244 tree r;
19245
19246 if (type == error_mark_node)
19247 RETURN (error_mark_node);
19248
19249 /* We do not want to process the index of aggregate
19250 initializers as they are identifier nodes which will be
19251 looked up by digest_init. */
19252 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19253
19254 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19255 newlen = vec_safe_length (n);
19256 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19257 {
19258 if (ce->index && process_index_p
19259 /* An identifier index is looked up in the type
19260 being initialized, not the current scope. */
19261 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19262 ce->index = RECUR (ce->index);
19263
19264 if (PACK_EXPANSION_P (ce->value))
19265 {
19266 /* Substitute into the pack expansion. */
19267 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19268 in_decl);
19269
19270 if (ce->value == error_mark_node
19271 || PACK_EXPANSION_P (ce->value))
19272 ;
19273 else if (TREE_VEC_LENGTH (ce->value) == 1)
19274 /* Just move the argument into place. */
19275 ce->value = TREE_VEC_ELT (ce->value, 0);
19276 else
19277 {
19278 /* Update the length of the final CONSTRUCTOR
19279 arguments vector, and note that we will need to
19280 copy.*/
19281 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19282 need_copy_p = true;
19283 }
19284 }
19285 else
19286 ce->value = RECUR (ce->value);
19287 }
19288
19289 if (need_copy_p)
19290 {
19291 vec<constructor_elt, va_gc> *old_n = n;
19292
19293 vec_alloc (n, newlen);
19294 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19295 {
19296 if (TREE_CODE (ce->value) == TREE_VEC)
19297 {
19298 int i, len = TREE_VEC_LENGTH (ce->value);
19299 for (i = 0; i < len; ++i)
19300 CONSTRUCTOR_APPEND_ELT (n, 0,
19301 TREE_VEC_ELT (ce->value, i));
19302 }
19303 else
19304 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19305 }
19306 }
19307
19308 r = build_constructor (init_list_type_node, n);
19309 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19310
19311 if (TREE_HAS_CONSTRUCTOR (t))
19312 {
19313 fcl_t cl = fcl_functional;
19314 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19315 cl = fcl_c99;
19316 RETURN (finish_compound_literal (type, r, complain, cl));
19317 }
19318
19319 TREE_TYPE (r) = type;
19320 RETURN (r);
19321 }
19322
19323 case TYPEID_EXPR:
19324 {
19325 tree operand_0 = TREE_OPERAND (t, 0);
19326 if (TYPE_P (operand_0))
19327 {
19328 operand_0 = tsubst (operand_0, args, complain, in_decl);
19329 RETURN (get_typeid (operand_0, complain));
19330 }
19331 else
19332 {
19333 operand_0 = RECUR (operand_0);
19334 RETURN (build_typeid (operand_0, complain));
19335 }
19336 }
19337
19338 case VAR_DECL:
19339 if (!args)
19340 RETURN (t);
19341 /* Fall through */
19342
19343 case PARM_DECL:
19344 {
19345 tree r = tsubst_copy (t, args, complain, in_decl);
19346 /* ??? We're doing a subset of finish_id_expression here. */
19347 if (VAR_P (r)
19348 && !processing_template_decl
19349 && !cp_unevaluated_operand
19350 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
19351 && CP_DECL_THREAD_LOCAL_P (r))
19352 {
19353 if (tree wrap = get_tls_wrapper_fn (r))
19354 /* Replace an evaluated use of the thread_local variable with
19355 a call to its wrapper. */
19356 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
19357 }
19358 else if (outer_automatic_var_p (r))
19359 r = process_outer_var_ref (r, complain);
19360
19361 if (!TYPE_REF_P (TREE_TYPE (t)))
19362 /* If the original type was a reference, we'll be wrapped in
19363 the appropriate INDIRECT_REF. */
19364 r = convert_from_reference (r);
19365 RETURN (r);
19366 }
19367
19368 case VA_ARG_EXPR:
19369 {
19370 tree op0 = RECUR (TREE_OPERAND (t, 0));
19371 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19372 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19373 }
19374
19375 case OFFSETOF_EXPR:
19376 {
19377 tree object_ptr
19378 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19379 in_decl, /*function_p=*/false,
19380 /*integral_constant_expression_p=*/false);
19381 RETURN (finish_offsetof (object_ptr,
19382 RECUR (TREE_OPERAND (t, 0)),
19383 EXPR_LOCATION (t)));
19384 }
19385
19386 case ADDRESSOF_EXPR:
19387 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19388 RECUR (TREE_OPERAND (t, 0)), complain));
19389
19390 case TRAIT_EXPR:
19391 {
19392 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19393 complain, in_decl);
19394
19395 tree type2 = TRAIT_EXPR_TYPE2 (t);
19396 if (type2 && TREE_CODE (type2) == TREE_LIST)
19397 type2 = RECUR (type2);
19398 else if (type2)
19399 type2 = tsubst (type2, args, complain, in_decl);
19400
19401 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19402 }
19403
19404 case STMT_EXPR:
19405 {
19406 tree old_stmt_expr = cur_stmt_expr;
19407 tree stmt_expr = begin_stmt_expr ();
19408
19409 cur_stmt_expr = stmt_expr;
19410 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19411 integral_constant_expression_p);
19412 stmt_expr = finish_stmt_expr (stmt_expr, false);
19413 cur_stmt_expr = old_stmt_expr;
19414
19415 /* If the resulting list of expression statement is empty,
19416 fold it further into void_node. */
19417 if (empty_expr_stmt_p (stmt_expr))
19418 stmt_expr = void_node;
19419
19420 RETURN (stmt_expr);
19421 }
19422
19423 case LAMBDA_EXPR:
19424 {
19425 if (complain & tf_partial)
19426 {
19427 /* We don't have a full set of template arguments yet; don't touch
19428 the lambda at all. */
19429 gcc_assert (processing_template_decl);
19430 return t;
19431 }
19432 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19433
19434 RETURN (build_lambda_object (r));
19435 }
19436
19437 case TARGET_EXPR:
19438 /* We can get here for a constant initializer of non-dependent type.
19439 FIXME stop folding in cp_parser_initializer_clause. */
19440 {
19441 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19442 complain);
19443 RETURN (r);
19444 }
19445
19446 case TRANSACTION_EXPR:
19447 RETURN (tsubst_expr(t, args, complain, in_decl,
19448 integral_constant_expression_p));
19449
19450 case PAREN_EXPR:
19451 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19452
19453 case VEC_PERM_EXPR:
19454 {
19455 tree op0 = RECUR (TREE_OPERAND (t, 0));
19456 tree op1 = RECUR (TREE_OPERAND (t, 1));
19457 tree op2 = RECUR (TREE_OPERAND (t, 2));
19458 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19459 complain));
19460 }
19461
19462 case REQUIRES_EXPR:
19463 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19464
19465 case RANGE_EXPR:
19466 /* No need to substitute further, a RANGE_EXPR will always be built
19467 with constant operands. */
19468 RETURN (t);
19469
19470 case NON_LVALUE_EXPR:
19471 case VIEW_CONVERT_EXPR:
19472 if (location_wrapper_p (t))
19473 /* We need to do this here as well as in tsubst_copy so we get the
19474 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19475 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19476 EXPR_LOCATION (t)));
19477 /* fallthrough. */
19478
19479 default:
19480 /* Handle Objective-C++ constructs, if appropriate. */
19481 {
19482 tree subst
19483 = objcp_tsubst_copy_and_build (t, args, complain,
19484 in_decl, /*function_p=*/false);
19485 if (subst)
19486 RETURN (subst);
19487 }
19488 RETURN (tsubst_copy (t, args, complain, in_decl));
19489 }
19490
19491 #undef RECUR
19492 #undef RETURN
19493 out:
19494 input_location = loc;
19495 return retval;
19496 }
19497
19498 /* Verify that the instantiated ARGS are valid. For type arguments,
19499 make sure that the type's linkage is ok. For non-type arguments,
19500 make sure they are constants if they are integral or enumerations.
19501 Emit an error under control of COMPLAIN, and return TRUE on error. */
19502
19503 static bool
19504 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19505 {
19506 if (dependent_template_arg_p (t))
19507 return false;
19508 if (ARGUMENT_PACK_P (t))
19509 {
19510 tree vec = ARGUMENT_PACK_ARGS (t);
19511 int len = TREE_VEC_LENGTH (vec);
19512 bool result = false;
19513 int i;
19514
19515 for (i = 0; i < len; ++i)
19516 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19517 result = true;
19518 return result;
19519 }
19520 else if (TYPE_P (t))
19521 {
19522 /* [basic.link]: A name with no linkage (notably, the name
19523 of a class or enumeration declared in a local scope)
19524 shall not be used to declare an entity with linkage.
19525 This implies that names with no linkage cannot be used as
19526 template arguments
19527
19528 DR 757 relaxes this restriction for C++0x. */
19529 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19530 : no_linkage_check (t, /*relaxed_p=*/false));
19531
19532 if (nt)
19533 {
19534 /* DR 488 makes use of a type with no linkage cause
19535 type deduction to fail. */
19536 if (complain & tf_error)
19537 {
19538 if (TYPE_UNNAMED_P (nt))
19539 error ("%qT is/uses unnamed type", t);
19540 else
19541 error ("template argument for %qD uses local type %qT",
19542 tmpl, t);
19543 }
19544 return true;
19545 }
19546 /* In order to avoid all sorts of complications, we do not
19547 allow variably-modified types as template arguments. */
19548 else if (variably_modified_type_p (t, NULL_TREE))
19549 {
19550 if (complain & tf_error)
19551 error ("%qT is a variably modified type", t);
19552 return true;
19553 }
19554 }
19555 /* Class template and alias template arguments should be OK. */
19556 else if (DECL_TYPE_TEMPLATE_P (t))
19557 ;
19558 /* A non-type argument of integral or enumerated type must be a
19559 constant. */
19560 else if (TREE_TYPE (t)
19561 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19562 && !REFERENCE_REF_P (t)
19563 && !TREE_CONSTANT (t))
19564 {
19565 if (complain & tf_error)
19566 error ("integral expression %qE is not constant", t);
19567 return true;
19568 }
19569 return false;
19570 }
19571
19572 static bool
19573 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19574 {
19575 int ix, len = DECL_NTPARMS (tmpl);
19576 bool result = false;
19577
19578 for (ix = 0; ix != len; ix++)
19579 {
19580 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19581 result = true;
19582 }
19583 if (result && (complain & tf_error))
19584 error (" trying to instantiate %qD", tmpl);
19585 return result;
19586 }
19587
19588 /* We're out of SFINAE context now, so generate diagnostics for the access
19589 errors we saw earlier when instantiating D from TMPL and ARGS. */
19590
19591 static void
19592 recheck_decl_substitution (tree d, tree tmpl, tree args)
19593 {
19594 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19595 tree type = TREE_TYPE (pattern);
19596 location_t loc = input_location;
19597
19598 push_access_scope (d);
19599 push_deferring_access_checks (dk_no_deferred);
19600 input_location = DECL_SOURCE_LOCATION (pattern);
19601 tsubst (type, args, tf_warning_or_error, d);
19602 input_location = loc;
19603 pop_deferring_access_checks ();
19604 pop_access_scope (d);
19605 }
19606
19607 /* Instantiate the indicated variable, function, or alias template TMPL with
19608 the template arguments in TARG_PTR. */
19609
19610 static tree
19611 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19612 {
19613 tree targ_ptr = orig_args;
19614 tree fndecl;
19615 tree gen_tmpl;
19616 tree spec;
19617 bool access_ok = true;
19618
19619 if (tmpl == error_mark_node)
19620 return error_mark_node;
19621
19622 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19623
19624 /* If this function is a clone, handle it specially. */
19625 if (DECL_CLONED_FUNCTION_P (tmpl))
19626 {
19627 tree spec;
19628 tree clone;
19629
19630 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19631 DECL_CLONED_FUNCTION. */
19632 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19633 targ_ptr, complain);
19634 if (spec == error_mark_node)
19635 return error_mark_node;
19636
19637 /* Look for the clone. */
19638 FOR_EACH_CLONE (clone, spec)
19639 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19640 return clone;
19641 /* We should always have found the clone by now. */
19642 gcc_unreachable ();
19643 return NULL_TREE;
19644 }
19645
19646 if (targ_ptr == error_mark_node)
19647 return error_mark_node;
19648
19649 /* Check to see if we already have this specialization. */
19650 gen_tmpl = most_general_template (tmpl);
19651 if (TMPL_ARGS_DEPTH (targ_ptr)
19652 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19653 /* targ_ptr only has the innermost template args, so add the outer ones
19654 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19655 the case of a non-dependent call within a template definition). */
19656 targ_ptr = (add_outermost_template_args
19657 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19658 targ_ptr));
19659
19660 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19661 but it doesn't seem to be on the hot path. */
19662 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19663
19664 gcc_assert (tmpl == gen_tmpl
19665 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19666 == spec)
19667 || fndecl == NULL_TREE);
19668
19669 if (spec != NULL_TREE)
19670 {
19671 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19672 {
19673 if (complain & tf_error)
19674 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19675 return error_mark_node;
19676 }
19677 return spec;
19678 }
19679
19680 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19681 complain))
19682 return error_mark_node;
19683
19684 /* We are building a FUNCTION_DECL, during which the access of its
19685 parameters and return types have to be checked. However this
19686 FUNCTION_DECL which is the desired context for access checking
19687 is not built yet. We solve this chicken-and-egg problem by
19688 deferring all checks until we have the FUNCTION_DECL. */
19689 push_deferring_access_checks (dk_deferred);
19690
19691 /* Instantiation of the function happens in the context of the function
19692 template, not the context of the overload resolution we're doing. */
19693 push_to_top_level ();
19694 /* If there are dependent arguments, e.g. because we're doing partial
19695 ordering, make sure processing_template_decl stays set. */
19696 if (uses_template_parms (targ_ptr))
19697 ++processing_template_decl;
19698 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19699 {
19700 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19701 complain, gen_tmpl, true);
19702 push_nested_class (ctx);
19703 }
19704
19705 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19706
19707 fndecl = NULL_TREE;
19708 if (VAR_P (pattern))
19709 {
19710 /* We need to determine if we're using a partial or explicit
19711 specialization now, because the type of the variable could be
19712 different. */
19713 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19714 tree elt = most_specialized_partial_spec (tid, complain);
19715 if (elt == error_mark_node)
19716 pattern = error_mark_node;
19717 else if (elt)
19718 {
19719 tree partial_tmpl = TREE_VALUE (elt);
19720 tree partial_args = TREE_PURPOSE (elt);
19721 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19722 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19723 }
19724 }
19725
19726 /* Substitute template parameters to obtain the specialization. */
19727 if (fndecl == NULL_TREE)
19728 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19729 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19730 pop_nested_class ();
19731 pop_from_top_level ();
19732
19733 if (fndecl == error_mark_node)
19734 {
19735 pop_deferring_access_checks ();
19736 return error_mark_node;
19737 }
19738
19739 /* The DECL_TI_TEMPLATE should always be the immediate parent
19740 template, not the most general template. */
19741 DECL_TI_TEMPLATE (fndecl) = tmpl;
19742 DECL_TI_ARGS (fndecl) = targ_ptr;
19743
19744 /* Now we know the specialization, compute access previously
19745 deferred. Do no access control for inheriting constructors,
19746 as we already checked access for the inherited constructor. */
19747 if (!(flag_new_inheriting_ctors
19748 && DECL_INHERITED_CTOR (fndecl)))
19749 {
19750 push_access_scope (fndecl);
19751 if (!perform_deferred_access_checks (complain))
19752 access_ok = false;
19753 pop_access_scope (fndecl);
19754 }
19755 pop_deferring_access_checks ();
19756
19757 /* If we've just instantiated the main entry point for a function,
19758 instantiate all the alternate entry points as well. We do this
19759 by cloning the instantiation of the main entry point, not by
19760 instantiating the template clones. */
19761 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19762 clone_function_decl (fndecl, /*update_methods=*/false);
19763
19764 if (!access_ok)
19765 {
19766 if (!(complain & tf_error))
19767 {
19768 /* Remember to reinstantiate when we're out of SFINAE so the user
19769 can see the errors. */
19770 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19771 }
19772 return error_mark_node;
19773 }
19774 return fndecl;
19775 }
19776
19777 /* Wrapper for instantiate_template_1. */
19778
19779 tree
19780 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19781 {
19782 tree ret;
19783 timevar_push (TV_TEMPLATE_INST);
19784 ret = instantiate_template_1 (tmpl, orig_args, complain);
19785 timevar_pop (TV_TEMPLATE_INST);
19786 return ret;
19787 }
19788
19789 /* Instantiate the alias template TMPL with ARGS. Also push a template
19790 instantiation level, which instantiate_template doesn't do because
19791 functions and variables have sufficient context established by the
19792 callers. */
19793
19794 static tree
19795 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19796 {
19797 if (tmpl == error_mark_node || args == error_mark_node)
19798 return error_mark_node;
19799 if (!push_tinst_level (tmpl, args))
19800 return error_mark_node;
19801
19802 args =
19803 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19804 args, tmpl, complain,
19805 /*require_all_args=*/true,
19806 /*use_default_args=*/true);
19807
19808 tree r = instantiate_template (tmpl, args, complain);
19809 pop_tinst_level ();
19810
19811 return r;
19812 }
19813
19814 /* PARM is a template parameter pack for FN. Returns true iff
19815 PARM is used in a deducible way in the argument list of FN. */
19816
19817 static bool
19818 pack_deducible_p (tree parm, tree fn)
19819 {
19820 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19821 for (; t; t = TREE_CHAIN (t))
19822 {
19823 tree type = TREE_VALUE (t);
19824 tree packs;
19825 if (!PACK_EXPANSION_P (type))
19826 continue;
19827 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19828 packs; packs = TREE_CHAIN (packs))
19829 if (template_args_equal (TREE_VALUE (packs), parm))
19830 {
19831 /* The template parameter pack is used in a function parameter
19832 pack. If this is the end of the parameter list, the
19833 template parameter pack is deducible. */
19834 if (TREE_CHAIN (t) == void_list_node)
19835 return true;
19836 else
19837 /* Otherwise, not. Well, it could be deduced from
19838 a non-pack parameter, but doing so would end up with
19839 a deduction mismatch, so don't bother. */
19840 return false;
19841 }
19842 }
19843 /* The template parameter pack isn't used in any function parameter
19844 packs, but it might be used deeper, e.g. tuple<Args...>. */
19845 return true;
19846 }
19847
19848 /* Subroutine of fn_type_unification: check non-dependent parms for
19849 convertibility. */
19850
19851 static int
19852 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19853 tree fn, unification_kind_t strict, int flags,
19854 struct conversion **convs, bool explain_p)
19855 {
19856 /* Non-constructor methods need to leave a conversion for 'this', which
19857 isn't included in nargs here. */
19858 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19859 && !DECL_CONSTRUCTOR_P (fn));
19860
19861 for (unsigned ia = 0;
19862 parms && parms != void_list_node && ia < nargs; )
19863 {
19864 tree parm = TREE_VALUE (parms);
19865
19866 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19867 && (!TREE_CHAIN (parms)
19868 || TREE_CHAIN (parms) == void_list_node))
19869 /* For a function parameter pack that occurs at the end of the
19870 parameter-declaration-list, the type A of each remaining
19871 argument of the call is compared with the type P of the
19872 declarator-id of the function parameter pack. */
19873 break;
19874
19875 parms = TREE_CHAIN (parms);
19876
19877 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19878 /* For a function parameter pack that does not occur at the
19879 end of the parameter-declaration-list, the type of the
19880 parameter pack is a non-deduced context. */
19881 continue;
19882
19883 if (!uses_template_parms (parm))
19884 {
19885 tree arg = args[ia];
19886 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19887 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19888
19889 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19890 conv_p, explain_p))
19891 return 1;
19892 }
19893
19894 ++ia;
19895 }
19896
19897 return 0;
19898 }
19899
19900 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19901 NARGS elements of the arguments that are being used when calling
19902 it. TARGS is a vector into which the deduced template arguments
19903 are placed.
19904
19905 Returns either a FUNCTION_DECL for the matching specialization of FN or
19906 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19907 true, diagnostics will be printed to explain why it failed.
19908
19909 If FN is a conversion operator, or we are trying to produce a specific
19910 specialization, RETURN_TYPE is the return type desired.
19911
19912 The EXPLICIT_TARGS are explicit template arguments provided via a
19913 template-id.
19914
19915 The parameter STRICT is one of:
19916
19917 DEDUCE_CALL:
19918 We are deducing arguments for a function call, as in
19919 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19920 deducing arguments for a call to the result of a conversion
19921 function template, as in [over.call.object].
19922
19923 DEDUCE_CONV:
19924 We are deducing arguments for a conversion function, as in
19925 [temp.deduct.conv].
19926
19927 DEDUCE_EXACT:
19928 We are deducing arguments when doing an explicit instantiation
19929 as in [temp.explicit], when determining an explicit specialization
19930 as in [temp.expl.spec], or when taking the address of a function
19931 template, as in [temp.deduct.funcaddr]. */
19932
19933 tree
19934 fn_type_unification (tree fn,
19935 tree explicit_targs,
19936 tree targs,
19937 const tree *args,
19938 unsigned int nargs,
19939 tree return_type,
19940 unification_kind_t strict,
19941 int flags,
19942 struct conversion **convs,
19943 bool explain_p,
19944 bool decltype_p)
19945 {
19946 tree parms;
19947 tree fntype;
19948 tree decl = NULL_TREE;
19949 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19950 bool ok;
19951 static int deduction_depth;
19952 /* type_unification_real will pass back any access checks from default
19953 template argument substitution. */
19954 vec<deferred_access_check, va_gc> *checks = NULL;
19955 /* We don't have all the template args yet. */
19956 bool incomplete = true;
19957
19958 tree orig_fn = fn;
19959 if (flag_new_inheriting_ctors)
19960 fn = strip_inheriting_ctors (fn);
19961
19962 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19963 tree r = error_mark_node;
19964
19965 tree full_targs = targs;
19966 if (TMPL_ARGS_DEPTH (targs)
19967 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19968 full_targs = (add_outermost_template_args
19969 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19970 targs));
19971
19972 if (decltype_p)
19973 complain |= tf_decltype;
19974
19975 /* In C++0x, it's possible to have a function template whose type depends
19976 on itself recursively. This is most obvious with decltype, but can also
19977 occur with enumeration scope (c++/48969). So we need to catch infinite
19978 recursion and reject the substitution at deduction time; this function
19979 will return error_mark_node for any repeated substitution.
19980
19981 This also catches excessive recursion such as when f<N> depends on
19982 f<N-1> across all integers, and returns error_mark_node for all the
19983 substitutions back up to the initial one.
19984
19985 This is, of course, not reentrant. */
19986 if (excessive_deduction_depth)
19987 return error_mark_node;
19988 ++deduction_depth;
19989
19990 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19991
19992 fntype = TREE_TYPE (fn);
19993 if (explicit_targs)
19994 {
19995 /* [temp.deduct]
19996
19997 The specified template arguments must match the template
19998 parameters in kind (i.e., type, nontype, template), and there
19999 must not be more arguments than there are parameters;
20000 otherwise type deduction fails.
20001
20002 Nontype arguments must match the types of the corresponding
20003 nontype template parameters, or must be convertible to the
20004 types of the corresponding nontype parameters as specified in
20005 _temp.arg.nontype_, otherwise type deduction fails.
20006
20007 All references in the function type of the function template
20008 to the corresponding template parameters are replaced by the
20009 specified template argument values. If a substitution in a
20010 template parameter or in the function type of the function
20011 template results in an invalid type, type deduction fails. */
20012 int i, len = TREE_VEC_LENGTH (tparms);
20013 location_t loc = input_location;
20014 incomplete = false;
20015
20016 if (explicit_targs == error_mark_node)
20017 goto fail;
20018
20019 if (TMPL_ARGS_DEPTH (explicit_targs)
20020 < TMPL_ARGS_DEPTH (full_targs))
20021 explicit_targs = add_outermost_template_args (full_targs,
20022 explicit_targs);
20023
20024 /* Adjust any explicit template arguments before entering the
20025 substitution context. */
20026 explicit_targs
20027 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20028 complain,
20029 /*require_all_args=*/false,
20030 /*use_default_args=*/false));
20031 if (explicit_targs == error_mark_node)
20032 goto fail;
20033
20034 /* Substitute the explicit args into the function type. This is
20035 necessary so that, for instance, explicitly declared function
20036 arguments can match null pointed constants. If we were given
20037 an incomplete set of explicit args, we must not do semantic
20038 processing during substitution as we could create partial
20039 instantiations. */
20040 for (i = 0; i < len; i++)
20041 {
20042 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20043 bool parameter_pack = false;
20044 tree targ = TREE_VEC_ELT (explicit_targs, i);
20045
20046 /* Dig out the actual parm. */
20047 if (TREE_CODE (parm) == TYPE_DECL
20048 || TREE_CODE (parm) == TEMPLATE_DECL)
20049 {
20050 parm = TREE_TYPE (parm);
20051 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20052 }
20053 else if (TREE_CODE (parm) == PARM_DECL)
20054 {
20055 parm = DECL_INITIAL (parm);
20056 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20057 }
20058
20059 if (!parameter_pack && targ == NULL_TREE)
20060 /* No explicit argument for this template parameter. */
20061 incomplete = true;
20062
20063 if (parameter_pack && pack_deducible_p (parm, fn))
20064 {
20065 /* Mark the argument pack as "incomplete". We could
20066 still deduce more arguments during unification.
20067 We remove this mark in type_unification_real. */
20068 if (targ)
20069 {
20070 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20071 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20072 = ARGUMENT_PACK_ARGS (targ);
20073 }
20074
20075 /* We have some incomplete argument packs. */
20076 incomplete = true;
20077 }
20078 }
20079
20080 if (incomplete)
20081 {
20082 if (!push_tinst_level (fn, explicit_targs))
20083 {
20084 excessive_deduction_depth = true;
20085 goto fail;
20086 }
20087 ++processing_template_decl;
20088 input_location = DECL_SOURCE_LOCATION (fn);
20089 /* Ignore any access checks; we'll see them again in
20090 instantiate_template and they might have the wrong
20091 access path at this point. */
20092 push_deferring_access_checks (dk_deferred);
20093 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20094 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20095 pop_deferring_access_checks ();
20096 input_location = loc;
20097 --processing_template_decl;
20098 pop_tinst_level ();
20099
20100 if (fntype == error_mark_node)
20101 goto fail;
20102 }
20103
20104 /* Place the explicitly specified arguments in TARGS. */
20105 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20106 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20107 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20108 if (!incomplete && CHECKING_P
20109 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20110 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20111 (targs, NUM_TMPL_ARGS (explicit_targs));
20112 }
20113
20114 if (return_type && strict != DEDUCE_CALL)
20115 {
20116 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20117 new_args[0] = return_type;
20118 memcpy (new_args + 1, args, nargs * sizeof (tree));
20119 args = new_args;
20120 ++nargs;
20121 }
20122
20123 if (!incomplete)
20124 goto deduced;
20125
20126 /* Never do unification on the 'this' parameter. */
20127 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20128
20129 if (return_type && strict == DEDUCE_CALL)
20130 {
20131 /* We're deducing for a call to the result of a template conversion
20132 function. The parms we really want are in return_type. */
20133 if (INDIRECT_TYPE_P (return_type))
20134 return_type = TREE_TYPE (return_type);
20135 parms = TYPE_ARG_TYPES (return_type);
20136 }
20137 else if (return_type)
20138 {
20139 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20140 }
20141
20142 /* We allow incomplete unification without an error message here
20143 because the standard doesn't seem to explicitly prohibit it. Our
20144 callers must be ready to deal with unification failures in any
20145 event. */
20146
20147 /* If we aren't explaining yet, push tinst context so we can see where
20148 any errors (e.g. from class instantiations triggered by instantiation
20149 of default template arguments) come from. If we are explaining, this
20150 context is redundant. */
20151 if (!explain_p && !push_tinst_level (fn, targs))
20152 {
20153 excessive_deduction_depth = true;
20154 goto fail;
20155 }
20156
20157 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20158 full_targs, parms, args, nargs, /*subr=*/0,
20159 strict, &checks, explain_p);
20160 if (!explain_p)
20161 pop_tinst_level ();
20162 if (!ok)
20163 goto fail;
20164
20165 /* Now that we have bindings for all of the template arguments,
20166 ensure that the arguments deduced for the template template
20167 parameters have compatible template parameter lists. We cannot
20168 check this property before we have deduced all template
20169 arguments, because the template parameter types of a template
20170 template parameter might depend on prior template parameters
20171 deduced after the template template parameter. The following
20172 ill-formed example illustrates this issue:
20173
20174 template<typename T, template<T> class C> void f(C<5>, T);
20175
20176 template<int N> struct X {};
20177
20178 void g() {
20179 f(X<5>(), 5l); // error: template argument deduction fails
20180 }
20181
20182 The template parameter list of 'C' depends on the template type
20183 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20184 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20185 time that we deduce 'C'. */
20186 if (!template_template_parm_bindings_ok_p
20187 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20188 {
20189 unify_inconsistent_template_template_parameters (explain_p);
20190 goto fail;
20191 }
20192
20193 /* DR 1391: All parameters have args, now check non-dependent parms for
20194 convertibility. */
20195 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20196 convs, explain_p))
20197 goto fail;
20198
20199 deduced:
20200 /* All is well so far. Now, check:
20201
20202 [temp.deduct]
20203
20204 When all template arguments have been deduced, all uses of
20205 template parameters in nondeduced contexts are replaced with
20206 the corresponding deduced argument values. If the
20207 substitution results in an invalid type, as described above,
20208 type deduction fails. */
20209 if (!push_tinst_level (fn, targs))
20210 {
20211 excessive_deduction_depth = true;
20212 goto fail;
20213 }
20214
20215 /* Also collect access checks from the instantiation. */
20216 reopen_deferring_access_checks (checks);
20217
20218 decl = instantiate_template (fn, targs, complain);
20219
20220 checks = get_deferred_access_checks ();
20221 pop_deferring_access_checks ();
20222
20223 pop_tinst_level ();
20224
20225 if (decl == error_mark_node)
20226 goto fail;
20227
20228 /* Now perform any access checks encountered during substitution. */
20229 push_access_scope (decl);
20230 ok = perform_access_checks (checks, complain);
20231 pop_access_scope (decl);
20232 if (!ok)
20233 goto fail;
20234
20235 /* If we're looking for an exact match, check that what we got
20236 is indeed an exact match. It might not be if some template
20237 parameters are used in non-deduced contexts. But don't check
20238 for an exact match if we have dependent template arguments;
20239 in that case we're doing partial ordering, and we already know
20240 that we have two candidates that will provide the actual type. */
20241 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20242 {
20243 tree substed = TREE_TYPE (decl);
20244 unsigned int i;
20245
20246 tree sarg
20247 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20248 if (return_type)
20249 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20250 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20251 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20252 {
20253 unify_type_mismatch (explain_p, args[i],
20254 TREE_VALUE (sarg));
20255 goto fail;
20256 }
20257 }
20258
20259 /* After doing deduction with the inherited constructor, actually return an
20260 instantiation of the inheriting constructor. */
20261 if (orig_fn != fn)
20262 decl = instantiate_template (orig_fn, targs, complain);
20263
20264 r = decl;
20265
20266 fail:
20267 --deduction_depth;
20268 if (excessive_deduction_depth)
20269 {
20270 if (deduction_depth == 0)
20271 /* Reset once we're all the way out. */
20272 excessive_deduction_depth = false;
20273 }
20274
20275 return r;
20276 }
20277
20278 /* Adjust types before performing type deduction, as described in
20279 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20280 sections are symmetric. PARM is the type of a function parameter
20281 or the return type of the conversion function. ARG is the type of
20282 the argument passed to the call, or the type of the value
20283 initialized with the result of the conversion function.
20284 ARG_EXPR is the original argument expression, which may be null. */
20285
20286 static int
20287 maybe_adjust_types_for_deduction (unification_kind_t strict,
20288 tree* parm,
20289 tree* arg,
20290 tree arg_expr)
20291 {
20292 int result = 0;
20293
20294 switch (strict)
20295 {
20296 case DEDUCE_CALL:
20297 break;
20298
20299 case DEDUCE_CONV:
20300 /* Swap PARM and ARG throughout the remainder of this
20301 function; the handling is precisely symmetric since PARM
20302 will initialize ARG rather than vice versa. */
20303 std::swap (parm, arg);
20304 break;
20305
20306 case DEDUCE_EXACT:
20307 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20308 too, but here handle it by stripping the reference from PARM
20309 rather than by adding it to ARG. */
20310 if (TYPE_REF_P (*parm)
20311 && TYPE_REF_IS_RVALUE (*parm)
20312 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20313 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20314 && TYPE_REF_P (*arg)
20315 && !TYPE_REF_IS_RVALUE (*arg))
20316 *parm = TREE_TYPE (*parm);
20317 /* Nothing else to do in this case. */
20318 return 0;
20319
20320 default:
20321 gcc_unreachable ();
20322 }
20323
20324 if (!TYPE_REF_P (*parm))
20325 {
20326 /* [temp.deduct.call]
20327
20328 If P is not a reference type:
20329
20330 --If A is an array type, the pointer type produced by the
20331 array-to-pointer standard conversion (_conv.array_) is
20332 used in place of A for type deduction; otherwise,
20333
20334 --If A is a function type, the pointer type produced by
20335 the function-to-pointer standard conversion
20336 (_conv.func_) is used in place of A for type deduction;
20337 otherwise,
20338
20339 --If A is a cv-qualified type, the top level
20340 cv-qualifiers of A's type are ignored for type
20341 deduction. */
20342 if (TREE_CODE (*arg) == ARRAY_TYPE)
20343 *arg = build_pointer_type (TREE_TYPE (*arg));
20344 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20345 *arg = build_pointer_type (*arg);
20346 else
20347 *arg = TYPE_MAIN_VARIANT (*arg);
20348 }
20349
20350 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20351 reference to a cv-unqualified template parameter that does not represent a
20352 template parameter of a class template (during class template argument
20353 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20354 an lvalue, the type "lvalue reference to A" is used in place of A for type
20355 deduction. */
20356 if (TYPE_REF_P (*parm)
20357 && TYPE_REF_IS_RVALUE (*parm)
20358 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20359 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20360 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20361 && (arg_expr ? lvalue_p (arg_expr)
20362 /* try_one_overload doesn't provide an arg_expr, but
20363 functions are always lvalues. */
20364 : TREE_CODE (*arg) == FUNCTION_TYPE))
20365 *arg = build_reference_type (*arg);
20366
20367 /* [temp.deduct.call]
20368
20369 If P is a cv-qualified type, the top level cv-qualifiers
20370 of P's type are ignored for type deduction. If P is a
20371 reference type, the type referred to by P is used for
20372 type deduction. */
20373 *parm = TYPE_MAIN_VARIANT (*parm);
20374 if (TYPE_REF_P (*parm))
20375 {
20376 *parm = TREE_TYPE (*parm);
20377 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20378 }
20379
20380 /* DR 322. For conversion deduction, remove a reference type on parm
20381 too (which has been swapped into ARG). */
20382 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20383 *arg = TREE_TYPE (*arg);
20384
20385 return result;
20386 }
20387
20388 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20389 template which doesn't contain any deducible template parameters; check if
20390 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20391 unify_one_argument. */
20392
20393 static int
20394 check_non_deducible_conversion (tree parm, tree arg, int strict,
20395 int flags, struct conversion **conv_p,
20396 bool explain_p)
20397 {
20398 tree type;
20399
20400 if (!TYPE_P (arg))
20401 type = TREE_TYPE (arg);
20402 else
20403 type = arg;
20404
20405 if (same_type_p (parm, type))
20406 return unify_success (explain_p);
20407
20408 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20409 if (strict == DEDUCE_CONV)
20410 {
20411 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20412 return unify_success (explain_p);
20413 }
20414 else if (strict != DEDUCE_EXACT)
20415 {
20416 bool ok = false;
20417 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20418 if (conv_p)
20419 /* Avoid recalculating this in add_function_candidate. */
20420 ok = (*conv_p
20421 = good_conversion (parm, type, conv_arg, flags, complain));
20422 else
20423 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20424 if (ok)
20425 return unify_success (explain_p);
20426 }
20427
20428 if (strict == DEDUCE_EXACT)
20429 return unify_type_mismatch (explain_p, parm, arg);
20430 else
20431 return unify_arg_conversion (explain_p, parm, type, arg);
20432 }
20433
20434 static bool uses_deducible_template_parms (tree type);
20435
20436 /* Returns true iff the expression EXPR is one from which a template
20437 argument can be deduced. In other words, if it's an undecorated
20438 use of a template non-type parameter. */
20439
20440 static bool
20441 deducible_expression (tree expr)
20442 {
20443 /* Strip implicit conversions. */
20444 while (CONVERT_EXPR_P (expr))
20445 expr = TREE_OPERAND (expr, 0);
20446 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20447 }
20448
20449 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20450 deducible way; that is, if it has a max value of <PARM> - 1. */
20451
20452 static bool
20453 deducible_array_bound (tree domain)
20454 {
20455 if (domain == NULL_TREE)
20456 return false;
20457
20458 tree max = TYPE_MAX_VALUE (domain);
20459 if (TREE_CODE (max) != MINUS_EXPR)
20460 return false;
20461
20462 return deducible_expression (TREE_OPERAND (max, 0));
20463 }
20464
20465 /* Returns true iff the template arguments ARGS use a template parameter
20466 in a deducible way. */
20467
20468 static bool
20469 deducible_template_args (tree args)
20470 {
20471 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20472 {
20473 bool deducible;
20474 tree elt = TREE_VEC_ELT (args, i);
20475 if (ARGUMENT_PACK_P (elt))
20476 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20477 else
20478 {
20479 if (PACK_EXPANSION_P (elt))
20480 elt = PACK_EXPANSION_PATTERN (elt);
20481 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20482 deducible = true;
20483 else if (TYPE_P (elt))
20484 deducible = uses_deducible_template_parms (elt);
20485 else
20486 deducible = deducible_expression (elt);
20487 }
20488 if (deducible)
20489 return true;
20490 }
20491 return false;
20492 }
20493
20494 /* Returns true iff TYPE contains any deducible references to template
20495 parameters, as per 14.8.2.5. */
20496
20497 static bool
20498 uses_deducible_template_parms (tree type)
20499 {
20500 if (PACK_EXPANSION_P (type))
20501 type = PACK_EXPANSION_PATTERN (type);
20502
20503 /* T
20504 cv-list T
20505 TT<T>
20506 TT<i>
20507 TT<> */
20508 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20509 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20510 return true;
20511
20512 /* T*
20513 T&
20514 T&& */
20515 if (INDIRECT_TYPE_P (type))
20516 return uses_deducible_template_parms (TREE_TYPE (type));
20517
20518 /* T[integer-constant ]
20519 type [i] */
20520 if (TREE_CODE (type) == ARRAY_TYPE)
20521 return (uses_deducible_template_parms (TREE_TYPE (type))
20522 || deducible_array_bound (TYPE_DOMAIN (type)));
20523
20524 /* T type ::*
20525 type T::*
20526 T T::*
20527 T (type ::*)()
20528 type (T::*)()
20529 type (type ::*)(T)
20530 type (T::*)(T)
20531 T (type ::*)(T)
20532 T (T::*)()
20533 T (T::*)(T) */
20534 if (TYPE_PTRMEM_P (type))
20535 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20536 || (uses_deducible_template_parms
20537 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20538
20539 /* template-name <T> (where template-name refers to a class template)
20540 template-name <i> (where template-name refers to a class template) */
20541 if (CLASS_TYPE_P (type)
20542 && CLASSTYPE_TEMPLATE_INFO (type)
20543 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20544 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20545 (CLASSTYPE_TI_ARGS (type)));
20546
20547 /* type (T)
20548 T()
20549 T(T) */
20550 if (TREE_CODE (type) == FUNCTION_TYPE
20551 || TREE_CODE (type) == METHOD_TYPE)
20552 {
20553 if (uses_deducible_template_parms (TREE_TYPE (type)))
20554 return true;
20555 tree parm = TYPE_ARG_TYPES (type);
20556 if (TREE_CODE (type) == METHOD_TYPE)
20557 parm = TREE_CHAIN (parm);
20558 for (; parm; parm = TREE_CHAIN (parm))
20559 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20560 return true;
20561 }
20562
20563 return false;
20564 }
20565
20566 /* Subroutine of type_unification_real and unify_pack_expansion to
20567 handle unification of a single P/A pair. Parameters are as
20568 for those functions. */
20569
20570 static int
20571 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20572 int subr, unification_kind_t strict,
20573 bool explain_p)
20574 {
20575 tree arg_expr = NULL_TREE;
20576 int arg_strict;
20577
20578 if (arg == error_mark_node || parm == error_mark_node)
20579 return unify_invalid (explain_p);
20580 if (arg == unknown_type_node)
20581 /* We can't deduce anything from this, but we might get all the
20582 template args from other function args. */
20583 return unify_success (explain_p);
20584
20585 /* Implicit conversions (Clause 4) will be performed on a function
20586 argument to convert it to the type of the corresponding function
20587 parameter if the parameter type contains no template-parameters that
20588 participate in template argument deduction. */
20589 if (strict != DEDUCE_EXACT
20590 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20591 /* For function parameters with no deducible template parameters,
20592 just return. We'll check non-dependent conversions later. */
20593 return unify_success (explain_p);
20594
20595 switch (strict)
20596 {
20597 case DEDUCE_CALL:
20598 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20599 | UNIFY_ALLOW_MORE_CV_QUAL
20600 | UNIFY_ALLOW_DERIVED);
20601 break;
20602
20603 case DEDUCE_CONV:
20604 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20605 break;
20606
20607 case DEDUCE_EXACT:
20608 arg_strict = UNIFY_ALLOW_NONE;
20609 break;
20610
20611 default:
20612 gcc_unreachable ();
20613 }
20614
20615 /* We only do these transformations if this is the top-level
20616 parameter_type_list in a call or declaration matching; in other
20617 situations (nested function declarators, template argument lists) we
20618 won't be comparing a type to an expression, and we don't do any type
20619 adjustments. */
20620 if (!subr)
20621 {
20622 if (!TYPE_P (arg))
20623 {
20624 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20625 if (type_unknown_p (arg))
20626 {
20627 /* [temp.deduct.type] A template-argument can be
20628 deduced from a pointer to function or pointer
20629 to member function argument if the set of
20630 overloaded functions does not contain function
20631 templates and at most one of a set of
20632 overloaded functions provides a unique
20633 match. */
20634 resolve_overloaded_unification (tparms, targs, parm,
20635 arg, strict,
20636 arg_strict, explain_p);
20637 /* If a unique match was not found, this is a
20638 non-deduced context, so we still succeed. */
20639 return unify_success (explain_p);
20640 }
20641
20642 arg_expr = arg;
20643 arg = unlowered_expr_type (arg);
20644 if (arg == error_mark_node)
20645 return unify_invalid (explain_p);
20646 }
20647
20648 arg_strict |=
20649 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20650 }
20651 else
20652 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20653 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20654 return unify_template_argument_mismatch (explain_p, parm, arg);
20655
20656 /* For deduction from an init-list we need the actual list. */
20657 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20658 arg = arg_expr;
20659 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20660 }
20661
20662 /* for_each_template_parm callback that always returns 0. */
20663
20664 static int
20665 zero_r (tree, void *)
20666 {
20667 return 0;
20668 }
20669
20670 /* for_each_template_parm any_fn callback to handle deduction of a template
20671 type argument from the type of an array bound. */
20672
20673 static int
20674 array_deduction_r (tree t, void *data)
20675 {
20676 tree_pair_p d = (tree_pair_p)data;
20677 tree &tparms = d->purpose;
20678 tree &targs = d->value;
20679
20680 if (TREE_CODE (t) == ARRAY_TYPE)
20681 if (tree dom = TYPE_DOMAIN (t))
20682 if (tree max = TYPE_MAX_VALUE (dom))
20683 {
20684 if (TREE_CODE (max) == MINUS_EXPR)
20685 max = TREE_OPERAND (max, 0);
20686 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20687 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20688 UNIFY_ALLOW_NONE, /*explain*/false);
20689 }
20690
20691 /* Keep walking. */
20692 return 0;
20693 }
20694
20695 /* Try to deduce any not-yet-deduced template type arguments from the type of
20696 an array bound. This is handled separately from unify because 14.8.2.5 says
20697 "The type of a type parameter is only deduced from an array bound if it is
20698 not otherwise deduced." */
20699
20700 static void
20701 try_array_deduction (tree tparms, tree targs, tree parm)
20702 {
20703 tree_pair_s data = { tparms, targs };
20704 hash_set<tree> visited;
20705 for_each_template_parm (parm, zero_r, &data, &visited,
20706 /*nondeduced*/false, array_deduction_r);
20707 }
20708
20709 /* Most parms like fn_type_unification.
20710
20711 If SUBR is 1, we're being called recursively (to unify the
20712 arguments of a function or method parameter of a function
20713 template).
20714
20715 CHECKS is a pointer to a vector of access checks encountered while
20716 substituting default template arguments. */
20717
20718 static int
20719 type_unification_real (tree tparms,
20720 tree full_targs,
20721 tree xparms,
20722 const tree *xargs,
20723 unsigned int xnargs,
20724 int subr,
20725 unification_kind_t strict,
20726 vec<deferred_access_check, va_gc> **checks,
20727 bool explain_p)
20728 {
20729 tree parm, arg;
20730 int i;
20731 int ntparms = TREE_VEC_LENGTH (tparms);
20732 int saw_undeduced = 0;
20733 tree parms;
20734 const tree *args;
20735 unsigned int nargs;
20736 unsigned int ia;
20737
20738 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20739 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20740 gcc_assert (ntparms > 0);
20741
20742 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20743
20744 /* Reset the number of non-defaulted template arguments contained
20745 in TARGS. */
20746 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20747
20748 again:
20749 parms = xparms;
20750 args = xargs;
20751 nargs = xnargs;
20752
20753 ia = 0;
20754 while (parms && parms != void_list_node
20755 && ia < nargs)
20756 {
20757 parm = TREE_VALUE (parms);
20758
20759 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20760 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20761 /* For a function parameter pack that occurs at the end of the
20762 parameter-declaration-list, the type A of each remaining
20763 argument of the call is compared with the type P of the
20764 declarator-id of the function parameter pack. */
20765 break;
20766
20767 parms = TREE_CHAIN (parms);
20768
20769 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20770 /* For a function parameter pack that does not occur at the
20771 end of the parameter-declaration-list, the type of the
20772 parameter pack is a non-deduced context. */
20773 continue;
20774
20775 arg = args[ia];
20776 ++ia;
20777
20778 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20779 explain_p))
20780 return 1;
20781 }
20782
20783 if (parms
20784 && parms != void_list_node
20785 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20786 {
20787 /* Unify the remaining arguments with the pack expansion type. */
20788 tree argvec;
20789 tree parmvec = make_tree_vec (1);
20790
20791 /* Allocate a TREE_VEC and copy in all of the arguments */
20792 argvec = make_tree_vec (nargs - ia);
20793 for (i = 0; ia < nargs; ++ia, ++i)
20794 TREE_VEC_ELT (argvec, i) = args[ia];
20795
20796 /* Copy the parameter into parmvec. */
20797 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20798 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20799 /*subr=*/subr, explain_p))
20800 return 1;
20801
20802 /* Advance to the end of the list of parameters. */
20803 parms = TREE_CHAIN (parms);
20804 }
20805
20806 /* Fail if we've reached the end of the parm list, and more args
20807 are present, and the parm list isn't variadic. */
20808 if (ia < nargs && parms == void_list_node)
20809 return unify_too_many_arguments (explain_p, nargs, ia);
20810 /* Fail if parms are left and they don't have default values and
20811 they aren't all deduced as empty packs (c++/57397). This is
20812 consistent with sufficient_parms_p. */
20813 if (parms && parms != void_list_node
20814 && TREE_PURPOSE (parms) == NULL_TREE)
20815 {
20816 unsigned int count = nargs;
20817 tree p = parms;
20818 bool type_pack_p;
20819 do
20820 {
20821 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20822 if (!type_pack_p)
20823 count++;
20824 p = TREE_CHAIN (p);
20825 }
20826 while (p && p != void_list_node);
20827 if (count != nargs)
20828 return unify_too_few_arguments (explain_p, ia, count,
20829 type_pack_p);
20830 }
20831
20832 if (!subr)
20833 {
20834 tsubst_flags_t complain = (explain_p
20835 ? tf_warning_or_error
20836 : tf_none);
20837 bool tried_array_deduction = (cxx_dialect < cxx17);
20838
20839 for (i = 0; i < ntparms; i++)
20840 {
20841 tree targ = TREE_VEC_ELT (targs, i);
20842 tree tparm = TREE_VEC_ELT (tparms, i);
20843
20844 /* Clear the "incomplete" flags on all argument packs now so that
20845 substituting them into later default arguments works. */
20846 if (targ && ARGUMENT_PACK_P (targ))
20847 {
20848 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20849 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20850 }
20851
20852 if (targ || tparm == error_mark_node)
20853 continue;
20854 tparm = TREE_VALUE (tparm);
20855
20856 if (TREE_CODE (tparm) == TYPE_DECL
20857 && !tried_array_deduction)
20858 {
20859 try_array_deduction (tparms, targs, xparms);
20860 tried_array_deduction = true;
20861 if (TREE_VEC_ELT (targs, i))
20862 continue;
20863 }
20864
20865 /* If this is an undeduced nontype parameter that depends on
20866 a type parameter, try another pass; its type may have been
20867 deduced from a later argument than the one from which
20868 this parameter can be deduced. */
20869 if (TREE_CODE (tparm) == PARM_DECL
20870 && uses_template_parms (TREE_TYPE (tparm))
20871 && saw_undeduced < 2)
20872 {
20873 saw_undeduced = 1;
20874 continue;
20875 }
20876
20877 /* Core issue #226 (C++0x) [temp.deduct]:
20878
20879 If a template argument has not been deduced, its
20880 default template argument, if any, is used.
20881
20882 When we are in C++98 mode, TREE_PURPOSE will either
20883 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20884 to explicitly check cxx_dialect here. */
20885 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20886 /* OK, there is a default argument. Wait until after the
20887 conversion check to do substitution. */
20888 continue;
20889
20890 /* If the type parameter is a parameter pack, then it will
20891 be deduced to an empty parameter pack. */
20892 if (template_parameter_pack_p (tparm))
20893 {
20894 tree arg;
20895
20896 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20897 {
20898 arg = make_node (NONTYPE_ARGUMENT_PACK);
20899 TREE_CONSTANT (arg) = 1;
20900 }
20901 else
20902 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20903
20904 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20905
20906 TREE_VEC_ELT (targs, i) = arg;
20907 continue;
20908 }
20909
20910 return unify_parameter_deduction_failure (explain_p, tparm);
20911 }
20912
20913 /* Now substitute into the default template arguments. */
20914 for (i = 0; i < ntparms; i++)
20915 {
20916 tree targ = TREE_VEC_ELT (targs, i);
20917 tree tparm = TREE_VEC_ELT (tparms, i);
20918
20919 if (targ || tparm == error_mark_node)
20920 continue;
20921 tree parm = TREE_VALUE (tparm);
20922 tree arg = TREE_PURPOSE (tparm);
20923 reopen_deferring_access_checks (*checks);
20924 location_t save_loc = input_location;
20925 if (DECL_P (parm))
20926 input_location = DECL_SOURCE_LOCATION (parm);
20927
20928 if (saw_undeduced == 1
20929 && TREE_CODE (parm) == PARM_DECL
20930 && uses_template_parms (TREE_TYPE (parm)))
20931 {
20932 /* The type of this non-type parameter depends on undeduced
20933 parameters. Don't try to use its default argument yet,
20934 since we might deduce an argument for it on the next pass,
20935 but do check whether the arguments we already have cause
20936 substitution failure, so that that happens before we try
20937 later default arguments (78489). */
20938 ++processing_template_decl;
20939 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20940 NULL_TREE);
20941 --processing_template_decl;
20942 if (type == error_mark_node)
20943 arg = error_mark_node;
20944 else
20945 arg = NULL_TREE;
20946 }
20947 else
20948 {
20949 tree substed = NULL_TREE;
20950 if (saw_undeduced == 1 && processing_template_decl == 0)
20951 {
20952 /* First instatiate in template context, in case we still
20953 depend on undeduced template parameters. */
20954 ++processing_template_decl;
20955 substed = tsubst_template_arg (arg, full_targs, complain,
20956 NULL_TREE);
20957 --processing_template_decl;
20958 if (substed != error_mark_node
20959 && !uses_template_parms (substed))
20960 /* We replaced all the tparms, substitute again out of
20961 template context. */
20962 substed = NULL_TREE;
20963 }
20964 if (!substed)
20965 substed = tsubst_template_arg (arg, full_targs, complain,
20966 NULL_TREE);
20967
20968 if (!uses_template_parms (substed))
20969 arg = convert_template_argument (parm, substed, full_targs,
20970 complain, i, NULL_TREE);
20971 else if (saw_undeduced == 1)
20972 arg = NULL_TREE;
20973 else
20974 arg = error_mark_node;
20975 }
20976
20977 input_location = save_loc;
20978 *checks = get_deferred_access_checks ();
20979 pop_deferring_access_checks ();
20980
20981 if (arg == error_mark_node)
20982 return 1;
20983 else if (arg)
20984 {
20985 TREE_VEC_ELT (targs, i) = arg;
20986 /* The position of the first default template argument,
20987 is also the number of non-defaulted arguments in TARGS.
20988 Record that. */
20989 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20991 }
20992 }
20993
20994 if (saw_undeduced++ == 1)
20995 goto again;
20996 }
20997
20998 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20999 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21000
21001 return unify_success (explain_p);
21002 }
21003
21004 /* Subroutine of type_unification_real. Args are like the variables
21005 at the call site. ARG is an overloaded function (or template-id);
21006 we try deducing template args from each of the overloads, and if
21007 only one succeeds, we go with that. Modifies TARGS and returns
21008 true on success. */
21009
21010 static bool
21011 resolve_overloaded_unification (tree tparms,
21012 tree targs,
21013 tree parm,
21014 tree arg,
21015 unification_kind_t strict,
21016 int sub_strict,
21017 bool explain_p)
21018 {
21019 tree tempargs = copy_node (targs);
21020 int good = 0;
21021 tree goodfn = NULL_TREE;
21022 bool addr_p;
21023
21024 if (TREE_CODE (arg) == ADDR_EXPR)
21025 {
21026 arg = TREE_OPERAND (arg, 0);
21027 addr_p = true;
21028 }
21029 else
21030 addr_p = false;
21031
21032 if (TREE_CODE (arg) == COMPONENT_REF)
21033 /* Handle `&x' where `x' is some static or non-static member
21034 function name. */
21035 arg = TREE_OPERAND (arg, 1);
21036
21037 if (TREE_CODE (arg) == OFFSET_REF)
21038 arg = TREE_OPERAND (arg, 1);
21039
21040 /* Strip baselink information. */
21041 if (BASELINK_P (arg))
21042 arg = BASELINK_FUNCTIONS (arg);
21043
21044 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21045 {
21046 /* If we got some explicit template args, we need to plug them into
21047 the affected templates before we try to unify, in case the
21048 explicit args will completely resolve the templates in question. */
21049
21050 int ok = 0;
21051 tree expl_subargs = TREE_OPERAND (arg, 1);
21052 arg = TREE_OPERAND (arg, 0);
21053
21054 for (lkp_iterator iter (arg); iter; ++iter)
21055 {
21056 tree fn = *iter;
21057 tree subargs, elem;
21058
21059 if (TREE_CODE (fn) != TEMPLATE_DECL)
21060 continue;
21061
21062 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21063 expl_subargs, NULL_TREE, tf_none,
21064 /*require_all_args=*/true,
21065 /*use_default_args=*/true);
21066 if (subargs != error_mark_node
21067 && !any_dependent_template_arguments_p (subargs))
21068 {
21069 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21070 if (try_one_overload (tparms, targs, tempargs, parm,
21071 elem, strict, sub_strict, addr_p, explain_p)
21072 && (!goodfn || !same_type_p (goodfn, elem)))
21073 {
21074 goodfn = elem;
21075 ++good;
21076 }
21077 }
21078 else if (subargs)
21079 ++ok;
21080 }
21081 /* If no templates (or more than one) are fully resolved by the
21082 explicit arguments, this template-id is a non-deduced context; it
21083 could still be OK if we deduce all template arguments for the
21084 enclosing call through other arguments. */
21085 if (good != 1)
21086 good = ok;
21087 }
21088 else if (TREE_CODE (arg) != OVERLOAD
21089 && TREE_CODE (arg) != FUNCTION_DECL)
21090 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21091 -- but the deduction does not succeed because the expression is
21092 not just the function on its own. */
21093 return false;
21094 else
21095 for (lkp_iterator iter (arg); iter; ++iter)
21096 {
21097 tree fn = *iter;
21098 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21099 strict, sub_strict, addr_p, explain_p)
21100 && (!goodfn || !decls_match (goodfn, fn)))
21101 {
21102 goodfn = fn;
21103 ++good;
21104 }
21105 }
21106
21107 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21108 to function or pointer to member function argument if the set of
21109 overloaded functions does not contain function templates and at most
21110 one of a set of overloaded functions provides a unique match.
21111
21112 So if we found multiple possibilities, we return success but don't
21113 deduce anything. */
21114
21115 if (good == 1)
21116 {
21117 int i = TREE_VEC_LENGTH (targs);
21118 for (; i--; )
21119 if (TREE_VEC_ELT (tempargs, i))
21120 {
21121 tree old = TREE_VEC_ELT (targs, i);
21122 tree new_ = TREE_VEC_ELT (tempargs, i);
21123 if (new_ && old && ARGUMENT_PACK_P (old)
21124 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21125 /* Don't forget explicit template arguments in a pack. */
21126 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21127 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21128 TREE_VEC_ELT (targs, i) = new_;
21129 }
21130 }
21131 if (good)
21132 return true;
21133
21134 return false;
21135 }
21136
21137 /* Core DR 115: In contexts where deduction is done and fails, or in
21138 contexts where deduction is not done, if a template argument list is
21139 specified and it, along with any default template arguments, identifies
21140 a single function template specialization, then the template-id is an
21141 lvalue for the function template specialization. */
21142
21143 tree
21144 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21145 {
21146 tree expr, offset, baselink;
21147 bool addr;
21148
21149 if (!type_unknown_p (orig_expr))
21150 return orig_expr;
21151
21152 expr = orig_expr;
21153 addr = false;
21154 offset = NULL_TREE;
21155 baselink = NULL_TREE;
21156
21157 if (TREE_CODE (expr) == ADDR_EXPR)
21158 {
21159 expr = TREE_OPERAND (expr, 0);
21160 addr = true;
21161 }
21162 if (TREE_CODE (expr) == OFFSET_REF)
21163 {
21164 offset = expr;
21165 expr = TREE_OPERAND (expr, 1);
21166 }
21167 if (BASELINK_P (expr))
21168 {
21169 baselink = expr;
21170 expr = BASELINK_FUNCTIONS (expr);
21171 }
21172
21173 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21174 {
21175 int good = 0;
21176 tree goodfn = NULL_TREE;
21177
21178 /* If we got some explicit template args, we need to plug them into
21179 the affected templates before we try to unify, in case the
21180 explicit args will completely resolve the templates in question. */
21181
21182 tree expl_subargs = TREE_OPERAND (expr, 1);
21183 tree arg = TREE_OPERAND (expr, 0);
21184 tree badfn = NULL_TREE;
21185 tree badargs = NULL_TREE;
21186
21187 for (lkp_iterator iter (arg); iter; ++iter)
21188 {
21189 tree fn = *iter;
21190 tree subargs, elem;
21191
21192 if (TREE_CODE (fn) != TEMPLATE_DECL)
21193 continue;
21194
21195 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21196 expl_subargs, NULL_TREE, tf_none,
21197 /*require_all_args=*/true,
21198 /*use_default_args=*/true);
21199 if (subargs != error_mark_node
21200 && !any_dependent_template_arguments_p (subargs))
21201 {
21202 elem = instantiate_template (fn, subargs, tf_none);
21203 if (elem == error_mark_node)
21204 {
21205 badfn = fn;
21206 badargs = subargs;
21207 }
21208 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21209 {
21210 goodfn = elem;
21211 ++good;
21212 }
21213 }
21214 }
21215 if (good == 1)
21216 {
21217 mark_used (goodfn);
21218 expr = goodfn;
21219 if (baselink)
21220 expr = build_baselink (BASELINK_BINFO (baselink),
21221 BASELINK_ACCESS_BINFO (baselink),
21222 expr, BASELINK_OPTYPE (baselink));
21223 if (offset)
21224 {
21225 tree base
21226 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21227 expr = build_offset_ref (base, expr, addr, complain);
21228 }
21229 if (addr)
21230 expr = cp_build_addr_expr (expr, complain);
21231 return expr;
21232 }
21233 else if (good == 0 && badargs && (complain & tf_error))
21234 /* There were no good options and at least one bad one, so let the
21235 user know what the problem is. */
21236 instantiate_template (badfn, badargs, complain);
21237 }
21238 return orig_expr;
21239 }
21240
21241 /* As above, but error out if the expression remains overloaded. */
21242
21243 tree
21244 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21245 {
21246 exp = resolve_nondeduced_context (exp, complain);
21247 if (type_unknown_p (exp))
21248 {
21249 if (complain & tf_error)
21250 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21251 return error_mark_node;
21252 }
21253 return exp;
21254 }
21255
21256 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21257 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21258 different overloads deduce different arguments for a given parm.
21259 ADDR_P is true if the expression for which deduction is being
21260 performed was of the form "& fn" rather than simply "fn".
21261
21262 Returns 1 on success. */
21263
21264 static int
21265 try_one_overload (tree tparms,
21266 tree orig_targs,
21267 tree targs,
21268 tree parm,
21269 tree arg,
21270 unification_kind_t strict,
21271 int sub_strict,
21272 bool addr_p,
21273 bool explain_p)
21274 {
21275 int nargs;
21276 tree tempargs;
21277 int i;
21278
21279 if (arg == error_mark_node)
21280 return 0;
21281
21282 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21283 to function or pointer to member function argument if the set of
21284 overloaded functions does not contain function templates and at most
21285 one of a set of overloaded functions provides a unique match.
21286
21287 So if this is a template, just return success. */
21288
21289 if (uses_template_parms (arg))
21290 return 1;
21291
21292 if (TREE_CODE (arg) == METHOD_TYPE)
21293 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21294 else if (addr_p)
21295 arg = build_pointer_type (arg);
21296
21297 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21298
21299 /* We don't copy orig_targs for this because if we have already deduced
21300 some template args from previous args, unify would complain when we
21301 try to deduce a template parameter for the same argument, even though
21302 there isn't really a conflict. */
21303 nargs = TREE_VEC_LENGTH (targs);
21304 tempargs = make_tree_vec (nargs);
21305
21306 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21307 return 0;
21308
21309 /* First make sure we didn't deduce anything that conflicts with
21310 explicitly specified args. */
21311 for (i = nargs; i--; )
21312 {
21313 tree elt = TREE_VEC_ELT (tempargs, i);
21314 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21315
21316 if (!elt)
21317 /*NOP*/;
21318 else if (uses_template_parms (elt))
21319 /* Since we're unifying against ourselves, we will fill in
21320 template args used in the function parm list with our own
21321 template parms. Discard them. */
21322 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21323 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21324 {
21325 /* Check that the argument at each index of the deduced argument pack
21326 is equivalent to the corresponding explicitly specified argument.
21327 We may have deduced more arguments than were explicitly specified,
21328 and that's OK. */
21329
21330 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21331 that's wrong if we deduce the same argument pack from multiple
21332 function arguments: it's only incomplete the first time. */
21333
21334 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21335 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21336
21337 if (TREE_VEC_LENGTH (deduced_pack)
21338 < TREE_VEC_LENGTH (explicit_pack))
21339 return 0;
21340
21341 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21342 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21343 TREE_VEC_ELT (deduced_pack, j)))
21344 return 0;
21345 }
21346 else if (oldelt && !template_args_equal (oldelt, elt))
21347 return 0;
21348 }
21349
21350 for (i = nargs; i--; )
21351 {
21352 tree elt = TREE_VEC_ELT (tempargs, i);
21353
21354 if (elt)
21355 TREE_VEC_ELT (targs, i) = elt;
21356 }
21357
21358 return 1;
21359 }
21360
21361 /* PARM is a template class (perhaps with unbound template
21362 parameters). ARG is a fully instantiated type. If ARG can be
21363 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21364 TARGS are as for unify. */
21365
21366 static tree
21367 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21368 bool explain_p)
21369 {
21370 tree copy_of_targs;
21371
21372 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21373 return NULL_TREE;
21374 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21375 /* Matches anything. */;
21376 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21377 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21378 return NULL_TREE;
21379
21380 /* We need to make a new template argument vector for the call to
21381 unify. If we used TARGS, we'd clutter it up with the result of
21382 the attempted unification, even if this class didn't work out.
21383 We also don't want to commit ourselves to all the unifications
21384 we've already done, since unification is supposed to be done on
21385 an argument-by-argument basis. In other words, consider the
21386 following pathological case:
21387
21388 template <int I, int J, int K>
21389 struct S {};
21390
21391 template <int I, int J>
21392 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21393
21394 template <int I, int J, int K>
21395 void f(S<I, J, K>, S<I, I, I>);
21396
21397 void g() {
21398 S<0, 0, 0> s0;
21399 S<0, 1, 2> s2;
21400
21401 f(s0, s2);
21402 }
21403
21404 Now, by the time we consider the unification involving `s2', we
21405 already know that we must have `f<0, 0, 0>'. But, even though
21406 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21407 because there are two ways to unify base classes of S<0, 1, 2>
21408 with S<I, I, I>. If we kept the already deduced knowledge, we
21409 would reject the possibility I=1. */
21410 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21411
21412 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21413 {
21414 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21415 return NULL_TREE;
21416 return arg;
21417 }
21418
21419 /* If unification failed, we're done. */
21420 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21421 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21422 return NULL_TREE;
21423
21424 return arg;
21425 }
21426
21427 /* Given a template type PARM and a class type ARG, find the unique
21428 base type in ARG that is an instance of PARM. We do not examine
21429 ARG itself; only its base-classes. If there is not exactly one
21430 appropriate base class, return NULL_TREE. PARM may be the type of
21431 a partial specialization, as well as a plain template type. Used
21432 by unify. */
21433
21434 static enum template_base_result
21435 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21436 bool explain_p, tree *result)
21437 {
21438 tree rval = NULL_TREE;
21439 tree binfo;
21440
21441 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21442
21443 binfo = TYPE_BINFO (complete_type (arg));
21444 if (!binfo)
21445 {
21446 /* The type could not be completed. */
21447 *result = NULL_TREE;
21448 return tbr_incomplete_type;
21449 }
21450
21451 /* Walk in inheritance graph order. The search order is not
21452 important, and this avoids multiple walks of virtual bases. */
21453 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21454 {
21455 tree r = try_class_unification (tparms, targs, parm,
21456 BINFO_TYPE (binfo), explain_p);
21457
21458 if (r)
21459 {
21460 /* If there is more than one satisfactory baseclass, then:
21461
21462 [temp.deduct.call]
21463
21464 If they yield more than one possible deduced A, the type
21465 deduction fails.
21466
21467 applies. */
21468 if (rval && !same_type_p (r, rval))
21469 {
21470 *result = NULL_TREE;
21471 return tbr_ambiguous_baseclass;
21472 }
21473
21474 rval = r;
21475 }
21476 }
21477
21478 *result = rval;
21479 return tbr_success;
21480 }
21481
21482 /* Returns the level of DECL, which declares a template parameter. */
21483
21484 static int
21485 template_decl_level (tree decl)
21486 {
21487 switch (TREE_CODE (decl))
21488 {
21489 case TYPE_DECL:
21490 case TEMPLATE_DECL:
21491 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21492
21493 case PARM_DECL:
21494 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21495
21496 default:
21497 gcc_unreachable ();
21498 }
21499 return 0;
21500 }
21501
21502 /* Decide whether ARG can be unified with PARM, considering only the
21503 cv-qualifiers of each type, given STRICT as documented for unify.
21504 Returns nonzero iff the unification is OK on that basis. */
21505
21506 static int
21507 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21508 {
21509 int arg_quals = cp_type_quals (arg);
21510 int parm_quals = cp_type_quals (parm);
21511
21512 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21513 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21514 {
21515 /* Although a CVR qualifier is ignored when being applied to a
21516 substituted template parameter ([8.3.2]/1 for example), that
21517 does not allow us to unify "const T" with "int&" because both
21518 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21519 It is ok when we're allowing additional CV qualifiers
21520 at the outer level [14.8.2.1]/3,1st bullet. */
21521 if ((TYPE_REF_P (arg)
21522 || TREE_CODE (arg) == FUNCTION_TYPE
21523 || TREE_CODE (arg) == METHOD_TYPE)
21524 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21525 return 0;
21526
21527 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21528 && (parm_quals & TYPE_QUAL_RESTRICT))
21529 return 0;
21530 }
21531
21532 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21533 && (arg_quals & parm_quals) != parm_quals)
21534 return 0;
21535
21536 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21537 && (parm_quals & arg_quals) != arg_quals)
21538 return 0;
21539
21540 return 1;
21541 }
21542
21543 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21544 void
21545 template_parm_level_and_index (tree parm, int* level, int* index)
21546 {
21547 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21548 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21549 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21550 {
21551 *index = TEMPLATE_TYPE_IDX (parm);
21552 *level = TEMPLATE_TYPE_LEVEL (parm);
21553 }
21554 else
21555 {
21556 *index = TEMPLATE_PARM_IDX (parm);
21557 *level = TEMPLATE_PARM_LEVEL (parm);
21558 }
21559 }
21560
21561 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21562 do { \
21563 if (unify (TP, TA, P, A, S, EP)) \
21564 return 1; \
21565 } while (0)
21566
21567 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21568 expansion at the end of PACKED_PARMS. Returns 0 if the type
21569 deduction succeeds, 1 otherwise. STRICT is the same as in
21570 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21571 function call argument list. We'll need to adjust the arguments to make them
21572 types. SUBR tells us if this is from a recursive call to
21573 type_unification_real, or for comparing two template argument
21574 lists. */
21575
21576 static int
21577 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21578 tree packed_args, unification_kind_t strict,
21579 bool subr, bool explain_p)
21580 {
21581 tree parm
21582 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21583 tree pattern = PACK_EXPANSION_PATTERN (parm);
21584 tree pack, packs = NULL_TREE;
21585 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21586
21587 /* Add in any args remembered from an earlier partial instantiation. */
21588 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21589 int levels = TMPL_ARGS_DEPTH (targs);
21590
21591 packed_args = expand_template_argument_pack (packed_args);
21592
21593 int len = TREE_VEC_LENGTH (packed_args);
21594
21595 /* Determine the parameter packs we will be deducing from the
21596 pattern, and record their current deductions. */
21597 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21598 pack; pack = TREE_CHAIN (pack))
21599 {
21600 tree parm_pack = TREE_VALUE (pack);
21601 int idx, level;
21602
21603 /* Only template parameter packs can be deduced, not e.g. function
21604 parameter packs or __bases or __integer_pack. */
21605 if (!TEMPLATE_PARM_P (parm_pack))
21606 continue;
21607
21608 /* Determine the index and level of this parameter pack. */
21609 template_parm_level_and_index (parm_pack, &level, &idx);
21610 if (level < levels)
21611 continue;
21612
21613 /* Keep track of the parameter packs and their corresponding
21614 argument packs. */
21615 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21616 TREE_TYPE (packs) = make_tree_vec (len - start);
21617 }
21618
21619 /* Loop through all of the arguments that have not yet been
21620 unified and unify each with the pattern. */
21621 for (i = start; i < len; i++)
21622 {
21623 tree parm;
21624 bool any_explicit = false;
21625 tree arg = TREE_VEC_ELT (packed_args, i);
21626
21627 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21628 or the element of its argument pack at the current index if
21629 this argument was explicitly specified. */
21630 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21631 {
21632 int idx, level;
21633 tree arg, pargs;
21634 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21635
21636 arg = NULL_TREE;
21637 if (TREE_VALUE (pack)
21638 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21639 && (i - start < TREE_VEC_LENGTH (pargs)))
21640 {
21641 any_explicit = true;
21642 arg = TREE_VEC_ELT (pargs, i - start);
21643 }
21644 TMPL_ARG (targs, level, idx) = arg;
21645 }
21646
21647 /* If we had explicit template arguments, substitute them into the
21648 pattern before deduction. */
21649 if (any_explicit)
21650 {
21651 /* Some arguments might still be unspecified or dependent. */
21652 bool dependent;
21653 ++processing_template_decl;
21654 dependent = any_dependent_template_arguments_p (targs);
21655 if (!dependent)
21656 --processing_template_decl;
21657 parm = tsubst (pattern, targs,
21658 explain_p ? tf_warning_or_error : tf_none,
21659 NULL_TREE);
21660 if (dependent)
21661 --processing_template_decl;
21662 if (parm == error_mark_node)
21663 return 1;
21664 }
21665 else
21666 parm = pattern;
21667
21668 /* Unify the pattern with the current argument. */
21669 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21670 explain_p))
21671 return 1;
21672
21673 /* For each parameter pack, collect the deduced value. */
21674 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21675 {
21676 int idx, level;
21677 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21678
21679 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21680 TMPL_ARG (targs, level, idx);
21681 }
21682 }
21683
21684 /* Verify that the results of unification with the parameter packs
21685 produce results consistent with what we've seen before, and make
21686 the deduced argument packs available. */
21687 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21688 {
21689 tree old_pack = TREE_VALUE (pack);
21690 tree new_args = TREE_TYPE (pack);
21691 int i, len = TREE_VEC_LENGTH (new_args);
21692 int idx, level;
21693 bool nondeduced_p = false;
21694
21695 /* By default keep the original deduced argument pack.
21696 If necessary, more specific code is going to update the
21697 resulting deduced argument later down in this function. */
21698 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21699 TMPL_ARG (targs, level, idx) = old_pack;
21700
21701 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21702 actually deduce anything. */
21703 for (i = 0; i < len && !nondeduced_p; ++i)
21704 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21705 nondeduced_p = true;
21706 if (nondeduced_p)
21707 continue;
21708
21709 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21710 {
21711 /* If we had fewer function args than explicit template args,
21712 just use the explicits. */
21713 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21714 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21715 if (len < explicit_len)
21716 new_args = explicit_args;
21717 }
21718
21719 if (!old_pack)
21720 {
21721 tree result;
21722 /* Build the deduced *_ARGUMENT_PACK. */
21723 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21724 {
21725 result = make_node (NONTYPE_ARGUMENT_PACK);
21726 TREE_CONSTANT (result) = 1;
21727 }
21728 else
21729 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21730
21731 SET_ARGUMENT_PACK_ARGS (result, new_args);
21732
21733 /* Note the deduced argument packs for this parameter
21734 pack. */
21735 TMPL_ARG (targs, level, idx) = result;
21736 }
21737 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21738 && (ARGUMENT_PACK_ARGS (old_pack)
21739 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21740 {
21741 /* We only had the explicitly-provided arguments before, but
21742 now we have a complete set of arguments. */
21743 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21744
21745 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21746 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21747 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21748 }
21749 else
21750 {
21751 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21752 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21753
21754 if (!comp_template_args (old_args, new_args,
21755 &bad_old_arg, &bad_new_arg))
21756 /* Inconsistent unification of this parameter pack. */
21757 return unify_parameter_pack_inconsistent (explain_p,
21758 bad_old_arg,
21759 bad_new_arg);
21760 }
21761 }
21762
21763 return unify_success (explain_p);
21764 }
21765
21766 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21767 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21768 parameters and return value are as for unify. */
21769
21770 static int
21771 unify_array_domain (tree tparms, tree targs,
21772 tree parm_dom, tree arg_dom,
21773 bool explain_p)
21774 {
21775 tree parm_max;
21776 tree arg_max;
21777 bool parm_cst;
21778 bool arg_cst;
21779
21780 /* Our representation of array types uses "N - 1" as the
21781 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21782 not an integer constant. We cannot unify arbitrarily
21783 complex expressions, so we eliminate the MINUS_EXPRs
21784 here. */
21785 parm_max = TYPE_MAX_VALUE (parm_dom);
21786 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21787 if (!parm_cst)
21788 {
21789 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21790 parm_max = TREE_OPERAND (parm_max, 0);
21791 }
21792 arg_max = TYPE_MAX_VALUE (arg_dom);
21793 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21794 if (!arg_cst)
21795 {
21796 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21797 trying to unify the type of a variable with the type
21798 of a template parameter. For example:
21799
21800 template <unsigned int N>
21801 void f (char (&) [N]);
21802 int g();
21803 void h(int i) {
21804 char a[g(i)];
21805 f(a);
21806 }
21807
21808 Here, the type of the ARG will be "int [g(i)]", and
21809 may be a SAVE_EXPR, etc. */
21810 if (TREE_CODE (arg_max) != MINUS_EXPR)
21811 return unify_vla_arg (explain_p, arg_dom);
21812 arg_max = TREE_OPERAND (arg_max, 0);
21813 }
21814
21815 /* If only one of the bounds used a MINUS_EXPR, compensate
21816 by adding one to the other bound. */
21817 if (parm_cst && !arg_cst)
21818 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21819 integer_type_node,
21820 parm_max,
21821 integer_one_node);
21822 else if (arg_cst && !parm_cst)
21823 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21824 integer_type_node,
21825 arg_max,
21826 integer_one_node);
21827
21828 return unify (tparms, targs, parm_max, arg_max,
21829 UNIFY_ALLOW_INTEGER, explain_p);
21830 }
21831
21832 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21833
21834 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21835
21836 static pa_kind_t
21837 pa_kind (tree t)
21838 {
21839 if (PACK_EXPANSION_P (t))
21840 t = PACK_EXPANSION_PATTERN (t);
21841 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21842 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21843 || DECL_TYPE_TEMPLATE_P (t))
21844 return pa_tmpl;
21845 else if (TYPE_P (t))
21846 return pa_type;
21847 else
21848 return pa_expr;
21849 }
21850
21851 /* Deduce the value of template parameters. TPARMS is the (innermost)
21852 set of template parameters to a template. TARGS is the bindings
21853 for those template parameters, as determined thus far; TARGS may
21854 include template arguments for outer levels of template parameters
21855 as well. PARM is a parameter to a template function, or a
21856 subcomponent of that parameter; ARG is the corresponding argument.
21857 This function attempts to match PARM with ARG in a manner
21858 consistent with the existing assignments in TARGS. If more values
21859 are deduced, then TARGS is updated.
21860
21861 Returns 0 if the type deduction succeeds, 1 otherwise. The
21862 parameter STRICT is a bitwise or of the following flags:
21863
21864 UNIFY_ALLOW_NONE:
21865 Require an exact match between PARM and ARG.
21866 UNIFY_ALLOW_MORE_CV_QUAL:
21867 Allow the deduced ARG to be more cv-qualified (by qualification
21868 conversion) than ARG.
21869 UNIFY_ALLOW_LESS_CV_QUAL:
21870 Allow the deduced ARG to be less cv-qualified than ARG.
21871 UNIFY_ALLOW_DERIVED:
21872 Allow the deduced ARG to be a template base class of ARG,
21873 or a pointer to a template base class of the type pointed to by
21874 ARG.
21875 UNIFY_ALLOW_INTEGER:
21876 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21877 case for more information.
21878 UNIFY_ALLOW_OUTER_LEVEL:
21879 This is the outermost level of a deduction. Used to determine validity
21880 of qualification conversions. A valid qualification conversion must
21881 have const qualified pointers leading up to the inner type which
21882 requires additional CV quals, except at the outer level, where const
21883 is not required [conv.qual]. It would be normal to set this flag in
21884 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21885 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21886 This is the outermost level of a deduction, and PARM can be more CV
21887 qualified at this point.
21888 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21889 This is the outermost level of a deduction, and PARM can be less CV
21890 qualified at this point. */
21891
21892 static int
21893 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21894 bool explain_p)
21895 {
21896 int idx;
21897 tree targ;
21898 tree tparm;
21899 int strict_in = strict;
21900 tsubst_flags_t complain = (explain_p
21901 ? tf_warning_or_error
21902 : tf_none);
21903
21904 /* I don't think this will do the right thing with respect to types.
21905 But the only case I've seen it in so far has been array bounds, where
21906 signedness is the only information lost, and I think that will be
21907 okay. */
21908 while (CONVERT_EXPR_P (parm))
21909 parm = TREE_OPERAND (parm, 0);
21910
21911 if (arg == error_mark_node)
21912 return unify_invalid (explain_p);
21913 if (arg == unknown_type_node
21914 || arg == init_list_type_node)
21915 /* We can't deduce anything from this, but we might get all the
21916 template args from other function args. */
21917 return unify_success (explain_p);
21918
21919 if (parm == any_targ_node || arg == any_targ_node)
21920 return unify_success (explain_p);
21921
21922 /* If PARM uses template parameters, then we can't bail out here,
21923 even if ARG == PARM, since we won't record unifications for the
21924 template parameters. We might need them if we're trying to
21925 figure out which of two things is more specialized. */
21926 if (arg == parm && !uses_template_parms (parm))
21927 return unify_success (explain_p);
21928
21929 /* Handle init lists early, so the rest of the function can assume
21930 we're dealing with a type. */
21931 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21932 {
21933 tree elt, elttype;
21934 unsigned i;
21935 tree orig_parm = parm;
21936
21937 /* Replace T with std::initializer_list<T> for deduction. */
21938 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21939 && flag_deduce_init_list)
21940 parm = listify (parm);
21941
21942 if (!is_std_init_list (parm)
21943 && TREE_CODE (parm) != ARRAY_TYPE)
21944 /* We can only deduce from an initializer list argument if the
21945 parameter is std::initializer_list or an array; otherwise this
21946 is a non-deduced context. */
21947 return unify_success (explain_p);
21948
21949 if (TREE_CODE (parm) == ARRAY_TYPE)
21950 elttype = TREE_TYPE (parm);
21951 else
21952 {
21953 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21954 /* Deduction is defined in terms of a single type, so just punt
21955 on the (bizarre) std::initializer_list<T...>. */
21956 if (PACK_EXPANSION_P (elttype))
21957 return unify_success (explain_p);
21958 }
21959
21960 if (strict != DEDUCE_EXACT
21961 && TYPE_P (elttype)
21962 && !uses_deducible_template_parms (elttype))
21963 /* If ELTTYPE has no deducible template parms, skip deduction from
21964 the list elements. */;
21965 else
21966 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21967 {
21968 int elt_strict = strict;
21969
21970 if (elt == error_mark_node)
21971 return unify_invalid (explain_p);
21972
21973 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21974 {
21975 tree type = TREE_TYPE (elt);
21976 if (type == error_mark_node)
21977 return unify_invalid (explain_p);
21978 /* It should only be possible to get here for a call. */
21979 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21980 elt_strict |= maybe_adjust_types_for_deduction
21981 (DEDUCE_CALL, &elttype, &type, elt);
21982 elt = type;
21983 }
21984
21985 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21986 explain_p);
21987 }
21988
21989 if (TREE_CODE (parm) == ARRAY_TYPE
21990 && deducible_array_bound (TYPE_DOMAIN (parm)))
21991 {
21992 /* Also deduce from the length of the initializer list. */
21993 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21994 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21995 if (idx == error_mark_node)
21996 return unify_invalid (explain_p);
21997 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21998 idx, explain_p);
21999 }
22000
22001 /* If the std::initializer_list<T> deduction worked, replace the
22002 deduced A with std::initializer_list<A>. */
22003 if (orig_parm != parm)
22004 {
22005 idx = TEMPLATE_TYPE_IDX (orig_parm);
22006 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22007 targ = listify (targ);
22008 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22009 }
22010 return unify_success (explain_p);
22011 }
22012
22013 /* If parm and arg aren't the same kind of thing (template, type, or
22014 expression), fail early. */
22015 if (pa_kind (parm) != pa_kind (arg))
22016 return unify_invalid (explain_p);
22017
22018 /* Immediately reject some pairs that won't unify because of
22019 cv-qualification mismatches. */
22020 if (TREE_CODE (arg) == TREE_CODE (parm)
22021 && TYPE_P (arg)
22022 /* It is the elements of the array which hold the cv quals of an array
22023 type, and the elements might be template type parms. We'll check
22024 when we recurse. */
22025 && TREE_CODE (arg) != ARRAY_TYPE
22026 /* We check the cv-qualifiers when unifying with template type
22027 parameters below. We want to allow ARG `const T' to unify with
22028 PARM `T' for example, when computing which of two templates
22029 is more specialized, for example. */
22030 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22031 && !check_cv_quals_for_unify (strict_in, arg, parm))
22032 return unify_cv_qual_mismatch (explain_p, parm, arg);
22033
22034 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22035 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22036 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22037 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22038 strict &= ~UNIFY_ALLOW_DERIVED;
22039 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22040 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22041
22042 switch (TREE_CODE (parm))
22043 {
22044 case TYPENAME_TYPE:
22045 case SCOPE_REF:
22046 case UNBOUND_CLASS_TEMPLATE:
22047 /* In a type which contains a nested-name-specifier, template
22048 argument values cannot be deduced for template parameters used
22049 within the nested-name-specifier. */
22050 return unify_success (explain_p);
22051
22052 case TEMPLATE_TYPE_PARM:
22053 case TEMPLATE_TEMPLATE_PARM:
22054 case BOUND_TEMPLATE_TEMPLATE_PARM:
22055 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22056 if (error_operand_p (tparm))
22057 return unify_invalid (explain_p);
22058
22059 if (TEMPLATE_TYPE_LEVEL (parm)
22060 != template_decl_level (tparm))
22061 /* The PARM is not one we're trying to unify. Just check
22062 to see if it matches ARG. */
22063 {
22064 if (TREE_CODE (arg) == TREE_CODE (parm)
22065 && (is_auto (parm) ? is_auto (arg)
22066 : same_type_p (parm, arg)))
22067 return unify_success (explain_p);
22068 else
22069 return unify_type_mismatch (explain_p, parm, arg);
22070 }
22071 idx = TEMPLATE_TYPE_IDX (parm);
22072 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22073 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22074 if (error_operand_p (tparm))
22075 return unify_invalid (explain_p);
22076
22077 /* Check for mixed types and values. */
22078 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22079 && TREE_CODE (tparm) != TYPE_DECL)
22080 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22081 && TREE_CODE (tparm) != TEMPLATE_DECL))
22082 gcc_unreachable ();
22083
22084 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22085 {
22086 if ((strict_in & UNIFY_ALLOW_DERIVED)
22087 && CLASS_TYPE_P (arg))
22088 {
22089 /* First try to match ARG directly. */
22090 tree t = try_class_unification (tparms, targs, parm, arg,
22091 explain_p);
22092 if (!t)
22093 {
22094 /* Otherwise, look for a suitable base of ARG, as below. */
22095 enum template_base_result r;
22096 r = get_template_base (tparms, targs, parm, arg,
22097 explain_p, &t);
22098 if (!t)
22099 return unify_no_common_base (explain_p, r, parm, arg);
22100 arg = t;
22101 }
22102 }
22103 /* ARG must be constructed from a template class or a template
22104 template parameter. */
22105 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22106 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22107 return unify_template_deduction_failure (explain_p, parm, arg);
22108
22109 /* Deduce arguments T, i from TT<T> or TT<i>. */
22110 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22111 return 1;
22112
22113 arg = TYPE_TI_TEMPLATE (arg);
22114
22115 /* Fall through to deduce template name. */
22116 }
22117
22118 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22119 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22120 {
22121 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22122
22123 /* Simple cases: Value already set, does match or doesn't. */
22124 if (targ != NULL_TREE && template_args_equal (targ, arg))
22125 return unify_success (explain_p);
22126 else if (targ)
22127 return unify_inconsistency (explain_p, parm, targ, arg);
22128 }
22129 else
22130 {
22131 /* If PARM is `const T' and ARG is only `int', we don't have
22132 a match unless we are allowing additional qualification.
22133 If ARG is `const int' and PARM is just `T' that's OK;
22134 that binds `const int' to `T'. */
22135 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22136 arg, parm))
22137 return unify_cv_qual_mismatch (explain_p, parm, arg);
22138
22139 /* Consider the case where ARG is `const volatile int' and
22140 PARM is `const T'. Then, T should be `volatile int'. */
22141 arg = cp_build_qualified_type_real
22142 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22143 if (arg == error_mark_node)
22144 return unify_invalid (explain_p);
22145
22146 /* Simple cases: Value already set, does match or doesn't. */
22147 if (targ != NULL_TREE && same_type_p (targ, arg))
22148 return unify_success (explain_p);
22149 else if (targ)
22150 return unify_inconsistency (explain_p, parm, targ, arg);
22151
22152 /* Make sure that ARG is not a variable-sized array. (Note
22153 that were talking about variable-sized arrays (like
22154 `int[n]'), rather than arrays of unknown size (like
22155 `int[]').) We'll get very confused by such a type since
22156 the bound of the array is not constant, and therefore
22157 not mangleable. Besides, such types are not allowed in
22158 ISO C++, so we can do as we please here. We do allow
22159 them for 'auto' deduction, since that isn't ABI-exposed. */
22160 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22161 return unify_vla_arg (explain_p, arg);
22162
22163 /* Strip typedefs as in convert_template_argument. */
22164 arg = canonicalize_type_argument (arg, tf_none);
22165 }
22166
22167 /* If ARG is a parameter pack or an expansion, we cannot unify
22168 against it unless PARM is also a parameter pack. */
22169 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22170 && !template_parameter_pack_p (parm))
22171 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22172
22173 /* If the argument deduction results is a METHOD_TYPE,
22174 then there is a problem.
22175 METHOD_TYPE doesn't map to any real C++ type the result of
22176 the deduction cannot be of that type. */
22177 if (TREE_CODE (arg) == METHOD_TYPE)
22178 return unify_method_type_error (explain_p, arg);
22179
22180 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22181 return unify_success (explain_p);
22182
22183 case TEMPLATE_PARM_INDEX:
22184 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22185 if (error_operand_p (tparm))
22186 return unify_invalid (explain_p);
22187
22188 if (TEMPLATE_PARM_LEVEL (parm)
22189 != template_decl_level (tparm))
22190 {
22191 /* The PARM is not one we're trying to unify. Just check
22192 to see if it matches ARG. */
22193 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22194 && cp_tree_equal (parm, arg));
22195 if (result)
22196 unify_expression_unequal (explain_p, parm, arg);
22197 return result;
22198 }
22199
22200 idx = TEMPLATE_PARM_IDX (parm);
22201 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22202
22203 if (targ)
22204 {
22205 if ((strict & UNIFY_ALLOW_INTEGER)
22206 && TREE_TYPE (targ) && TREE_TYPE (arg)
22207 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22208 /* We're deducing from an array bound, the type doesn't matter. */
22209 arg = fold_convert (TREE_TYPE (targ), arg);
22210 int x = !cp_tree_equal (targ, arg);
22211 if (x)
22212 unify_inconsistency (explain_p, parm, targ, arg);
22213 return x;
22214 }
22215
22216 /* [temp.deduct.type] If, in the declaration of a function template
22217 with a non-type template-parameter, the non-type
22218 template-parameter is used in an expression in the function
22219 parameter-list and, if the corresponding template-argument is
22220 deduced, the template-argument type shall match the type of the
22221 template-parameter exactly, except that a template-argument
22222 deduced from an array bound may be of any integral type.
22223 The non-type parameter might use already deduced type parameters. */
22224 tparm = TREE_TYPE (parm);
22225 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22226 /* We don't have enough levels of args to do any substitution. This
22227 can happen in the context of -fnew-ttp-matching. */;
22228 else
22229 {
22230 ++processing_template_decl;
22231 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22232 --processing_template_decl;
22233
22234 if (tree a = type_uses_auto (tparm))
22235 {
22236 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22237 if (tparm == error_mark_node)
22238 return 1;
22239 }
22240 }
22241
22242 if (!TREE_TYPE (arg))
22243 /* Template-parameter dependent expression. Just accept it for now.
22244 It will later be processed in convert_template_argument. */
22245 ;
22246 else if (same_type_p (non_reference (TREE_TYPE (arg)),
22247 non_reference (tparm)))
22248 /* OK */;
22249 else if ((strict & UNIFY_ALLOW_INTEGER)
22250 && CP_INTEGRAL_TYPE_P (tparm))
22251 /* Convert the ARG to the type of PARM; the deduced non-type
22252 template argument must exactly match the types of the
22253 corresponding parameter. */
22254 arg = fold (build_nop (tparm, arg));
22255 else if (uses_template_parms (tparm))
22256 {
22257 /* We haven't deduced the type of this parameter yet. */
22258 if (cxx_dialect >= cxx17
22259 /* We deduce from array bounds in try_array_deduction. */
22260 && !(strict & UNIFY_ALLOW_INTEGER))
22261 {
22262 /* Deduce it from the non-type argument. */
22263 tree atype = TREE_TYPE (arg);
22264 RECUR_AND_CHECK_FAILURE (tparms, targs,
22265 tparm, atype,
22266 UNIFY_ALLOW_NONE, explain_p);
22267 }
22268 else
22269 /* Try again later. */
22270 return unify_success (explain_p);
22271 }
22272 else
22273 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22274
22275 /* If ARG is a parameter pack or an expansion, we cannot unify
22276 against it unless PARM is also a parameter pack. */
22277 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22278 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22279 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22280
22281 {
22282 bool removed_attr = false;
22283 arg = strip_typedefs_expr (arg, &removed_attr);
22284 }
22285 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22286 return unify_success (explain_p);
22287
22288 case PTRMEM_CST:
22289 {
22290 /* A pointer-to-member constant can be unified only with
22291 another constant. */
22292 if (TREE_CODE (arg) != PTRMEM_CST)
22293 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22294
22295 /* Just unify the class member. It would be useless (and possibly
22296 wrong, depending on the strict flags) to unify also
22297 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22298 arg refer to the same variable, even if through different
22299 classes. For instance:
22300
22301 struct A { int x; };
22302 struct B : A { };
22303
22304 Unification of &A::x and &B::x must succeed. */
22305 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22306 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22307 }
22308
22309 case POINTER_TYPE:
22310 {
22311 if (!TYPE_PTR_P (arg))
22312 return unify_type_mismatch (explain_p, parm, arg);
22313
22314 /* [temp.deduct.call]
22315
22316 A can be another pointer or pointer to member type that can
22317 be converted to the deduced A via a qualification
22318 conversion (_conv.qual_).
22319
22320 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22321 This will allow for additional cv-qualification of the
22322 pointed-to types if appropriate. */
22323
22324 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22325 /* The derived-to-base conversion only persists through one
22326 level of pointers. */
22327 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22328
22329 return unify (tparms, targs, TREE_TYPE (parm),
22330 TREE_TYPE (arg), strict, explain_p);
22331 }
22332
22333 case REFERENCE_TYPE:
22334 if (!TYPE_REF_P (arg))
22335 return unify_type_mismatch (explain_p, parm, arg);
22336 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22337 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22338
22339 case ARRAY_TYPE:
22340 if (TREE_CODE (arg) != ARRAY_TYPE)
22341 return unify_type_mismatch (explain_p, parm, arg);
22342 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22343 != (TYPE_DOMAIN (arg) == NULL_TREE))
22344 return unify_type_mismatch (explain_p, parm, arg);
22345 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22346 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22347 if (TYPE_DOMAIN (parm) != NULL_TREE)
22348 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22349 TYPE_DOMAIN (arg), explain_p);
22350 return unify_success (explain_p);
22351
22352 case REAL_TYPE:
22353 case COMPLEX_TYPE:
22354 case VECTOR_TYPE:
22355 case INTEGER_TYPE:
22356 case BOOLEAN_TYPE:
22357 case ENUMERAL_TYPE:
22358 case VOID_TYPE:
22359 case NULLPTR_TYPE:
22360 if (TREE_CODE (arg) != TREE_CODE (parm))
22361 return unify_type_mismatch (explain_p, parm, arg);
22362
22363 /* We have already checked cv-qualification at the top of the
22364 function. */
22365 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22366 return unify_type_mismatch (explain_p, parm, arg);
22367
22368 /* As far as unification is concerned, this wins. Later checks
22369 will invalidate it if necessary. */
22370 return unify_success (explain_p);
22371
22372 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22373 /* Type INTEGER_CST can come from ordinary constant template args. */
22374 case INTEGER_CST:
22375 while (CONVERT_EXPR_P (arg))
22376 arg = TREE_OPERAND (arg, 0);
22377
22378 if (TREE_CODE (arg) != INTEGER_CST)
22379 return unify_template_argument_mismatch (explain_p, parm, arg);
22380 return (tree_int_cst_equal (parm, arg)
22381 ? unify_success (explain_p)
22382 : unify_template_argument_mismatch (explain_p, parm, arg));
22383
22384 case TREE_VEC:
22385 {
22386 int i, len, argslen;
22387 int parm_variadic_p = 0;
22388
22389 if (TREE_CODE (arg) != TREE_VEC)
22390 return unify_template_argument_mismatch (explain_p, parm, arg);
22391
22392 len = TREE_VEC_LENGTH (parm);
22393 argslen = TREE_VEC_LENGTH (arg);
22394
22395 /* Check for pack expansions in the parameters. */
22396 for (i = 0; i < len; ++i)
22397 {
22398 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22399 {
22400 if (i == len - 1)
22401 /* We can unify against something with a trailing
22402 parameter pack. */
22403 parm_variadic_p = 1;
22404 else
22405 /* [temp.deduct.type]/9: If the template argument list of
22406 P contains a pack expansion that is not the last
22407 template argument, the entire template argument list
22408 is a non-deduced context. */
22409 return unify_success (explain_p);
22410 }
22411 }
22412
22413 /* If we don't have enough arguments to satisfy the parameters
22414 (not counting the pack expression at the end), or we have
22415 too many arguments for a parameter list that doesn't end in
22416 a pack expression, we can't unify. */
22417 if (parm_variadic_p
22418 ? argslen < len - parm_variadic_p
22419 : argslen != len)
22420 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22421
22422 /* Unify all of the parameters that precede the (optional)
22423 pack expression. */
22424 for (i = 0; i < len - parm_variadic_p; ++i)
22425 {
22426 RECUR_AND_CHECK_FAILURE (tparms, targs,
22427 TREE_VEC_ELT (parm, i),
22428 TREE_VEC_ELT (arg, i),
22429 UNIFY_ALLOW_NONE, explain_p);
22430 }
22431 if (parm_variadic_p)
22432 return unify_pack_expansion (tparms, targs, parm, arg,
22433 DEDUCE_EXACT,
22434 /*subr=*/true, explain_p);
22435 return unify_success (explain_p);
22436 }
22437
22438 case RECORD_TYPE:
22439 case UNION_TYPE:
22440 if (TREE_CODE (arg) != TREE_CODE (parm))
22441 return unify_type_mismatch (explain_p, parm, arg);
22442
22443 if (TYPE_PTRMEMFUNC_P (parm))
22444 {
22445 if (!TYPE_PTRMEMFUNC_P (arg))
22446 return unify_type_mismatch (explain_p, parm, arg);
22447
22448 return unify (tparms, targs,
22449 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22450 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22451 strict, explain_p);
22452 }
22453 else if (TYPE_PTRMEMFUNC_P (arg))
22454 return unify_type_mismatch (explain_p, parm, arg);
22455
22456 if (CLASSTYPE_TEMPLATE_INFO (parm))
22457 {
22458 tree t = NULL_TREE;
22459
22460 if (strict_in & UNIFY_ALLOW_DERIVED)
22461 {
22462 /* First, we try to unify the PARM and ARG directly. */
22463 t = try_class_unification (tparms, targs,
22464 parm, arg, explain_p);
22465
22466 if (!t)
22467 {
22468 /* Fallback to the special case allowed in
22469 [temp.deduct.call]:
22470
22471 If P is a class, and P has the form
22472 template-id, then A can be a derived class of
22473 the deduced A. Likewise, if P is a pointer to
22474 a class of the form template-id, A can be a
22475 pointer to a derived class pointed to by the
22476 deduced A. */
22477 enum template_base_result r;
22478 r = get_template_base (tparms, targs, parm, arg,
22479 explain_p, &t);
22480
22481 if (!t)
22482 {
22483 /* Don't give the derived diagnostic if we're
22484 already dealing with the same template. */
22485 bool same_template
22486 = (CLASSTYPE_TEMPLATE_INFO (arg)
22487 && (CLASSTYPE_TI_TEMPLATE (parm)
22488 == CLASSTYPE_TI_TEMPLATE (arg)));
22489 return unify_no_common_base (explain_p && !same_template,
22490 r, parm, arg);
22491 }
22492 }
22493 }
22494 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22495 && (CLASSTYPE_TI_TEMPLATE (parm)
22496 == CLASSTYPE_TI_TEMPLATE (arg)))
22497 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22498 Then, we should unify `int' and `U'. */
22499 t = arg;
22500 else
22501 /* There's no chance of unification succeeding. */
22502 return unify_type_mismatch (explain_p, parm, arg);
22503
22504 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22505 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22506 }
22507 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22508 return unify_type_mismatch (explain_p, parm, arg);
22509 return unify_success (explain_p);
22510
22511 case METHOD_TYPE:
22512 case FUNCTION_TYPE:
22513 {
22514 unsigned int nargs;
22515 tree *args;
22516 tree a;
22517 unsigned int i;
22518
22519 if (TREE_CODE (arg) != TREE_CODE (parm))
22520 return unify_type_mismatch (explain_p, parm, arg);
22521
22522 /* CV qualifications for methods can never be deduced, they must
22523 match exactly. We need to check them explicitly here,
22524 because type_unification_real treats them as any other
22525 cv-qualified parameter. */
22526 if (TREE_CODE (parm) == METHOD_TYPE
22527 && (!check_cv_quals_for_unify
22528 (UNIFY_ALLOW_NONE,
22529 class_of_this_parm (arg),
22530 class_of_this_parm (parm))))
22531 return unify_cv_qual_mismatch (explain_p, parm, arg);
22532 if (TREE_CODE (arg) == FUNCTION_TYPE
22533 && type_memfn_quals (parm) != type_memfn_quals (arg))
22534 return unify_cv_qual_mismatch (explain_p, parm, arg);
22535 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22536 return unify_type_mismatch (explain_p, parm, arg);
22537
22538 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22539 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22540
22541 nargs = list_length (TYPE_ARG_TYPES (arg));
22542 args = XALLOCAVEC (tree, nargs);
22543 for (a = TYPE_ARG_TYPES (arg), i = 0;
22544 a != NULL_TREE && a != void_list_node;
22545 a = TREE_CHAIN (a), ++i)
22546 args[i] = TREE_VALUE (a);
22547 nargs = i;
22548
22549 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22550 args, nargs, 1, DEDUCE_EXACT,
22551 NULL, explain_p))
22552 return 1;
22553
22554 if (flag_noexcept_type)
22555 {
22556 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22557 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22558 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22559 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22560 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22561 && uses_template_parms (TREE_PURPOSE (pspec)))
22562 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22563 TREE_PURPOSE (aspec),
22564 UNIFY_ALLOW_NONE, explain_p);
22565 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22566 return unify_type_mismatch (explain_p, parm, arg);
22567 }
22568
22569 return 0;
22570 }
22571
22572 case OFFSET_TYPE:
22573 /* Unify a pointer to member with a pointer to member function, which
22574 deduces the type of the member as a function type. */
22575 if (TYPE_PTRMEMFUNC_P (arg))
22576 {
22577 /* Check top-level cv qualifiers */
22578 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22579 return unify_cv_qual_mismatch (explain_p, parm, arg);
22580
22581 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22582 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22583 UNIFY_ALLOW_NONE, explain_p);
22584
22585 /* Determine the type of the function we are unifying against. */
22586 tree fntype = static_fn_type (arg);
22587
22588 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22589 }
22590
22591 if (TREE_CODE (arg) != OFFSET_TYPE)
22592 return unify_type_mismatch (explain_p, parm, arg);
22593 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22594 TYPE_OFFSET_BASETYPE (arg),
22595 UNIFY_ALLOW_NONE, explain_p);
22596 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22597 strict, explain_p);
22598
22599 case CONST_DECL:
22600 if (DECL_TEMPLATE_PARM_P (parm))
22601 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22602 if (arg != scalar_constant_value (parm))
22603 return unify_template_argument_mismatch (explain_p, parm, arg);
22604 return unify_success (explain_p);
22605
22606 case FIELD_DECL:
22607 case TEMPLATE_DECL:
22608 /* Matched cases are handled by the ARG == PARM test above. */
22609 return unify_template_argument_mismatch (explain_p, parm, arg);
22610
22611 case VAR_DECL:
22612 /* We might get a variable as a non-type template argument in parm if the
22613 corresponding parameter is type-dependent. Make any necessary
22614 adjustments based on whether arg is a reference. */
22615 if (CONSTANT_CLASS_P (arg))
22616 parm = fold_non_dependent_expr (parm, complain);
22617 else if (REFERENCE_REF_P (arg))
22618 {
22619 tree sub = TREE_OPERAND (arg, 0);
22620 STRIP_NOPS (sub);
22621 if (TREE_CODE (sub) == ADDR_EXPR)
22622 arg = TREE_OPERAND (sub, 0);
22623 }
22624 /* Now use the normal expression code to check whether they match. */
22625 goto expr;
22626
22627 case TYPE_ARGUMENT_PACK:
22628 case NONTYPE_ARGUMENT_PACK:
22629 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22630 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22631
22632 case TYPEOF_TYPE:
22633 case DECLTYPE_TYPE:
22634 case UNDERLYING_TYPE:
22635 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22636 or UNDERLYING_TYPE nodes. */
22637 return unify_success (explain_p);
22638
22639 case ERROR_MARK:
22640 /* Unification fails if we hit an error node. */
22641 return unify_invalid (explain_p);
22642
22643 case INDIRECT_REF:
22644 if (REFERENCE_REF_P (parm))
22645 {
22646 bool pexp = PACK_EXPANSION_P (arg);
22647 if (pexp)
22648 arg = PACK_EXPANSION_PATTERN (arg);
22649 if (REFERENCE_REF_P (arg))
22650 arg = TREE_OPERAND (arg, 0);
22651 if (pexp)
22652 arg = make_pack_expansion (arg, complain);
22653 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22654 strict, explain_p);
22655 }
22656 /* FALLTHRU */
22657
22658 default:
22659 /* An unresolved overload is a nondeduced context. */
22660 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22661 return unify_success (explain_p);
22662 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22663 expr:
22664 /* We must be looking at an expression. This can happen with
22665 something like:
22666
22667 template <int I>
22668 void foo(S<I>, S<I + 2>);
22669
22670 This is a "nondeduced context":
22671
22672 [deduct.type]
22673
22674 The nondeduced contexts are:
22675
22676 --A type that is a template-id in which one or more of
22677 the template-arguments is an expression that references
22678 a template-parameter.
22679
22680 In these cases, we assume deduction succeeded, but don't
22681 actually infer any unifications. */
22682
22683 if (!uses_template_parms (parm)
22684 && !template_args_equal (parm, arg))
22685 return unify_expression_unequal (explain_p, parm, arg);
22686 else
22687 return unify_success (explain_p);
22688 }
22689 }
22690 #undef RECUR_AND_CHECK_FAILURE
22691 \f
22692 /* Note that DECL can be defined in this translation unit, if
22693 required. */
22694
22695 static void
22696 mark_definable (tree decl)
22697 {
22698 tree clone;
22699 DECL_NOT_REALLY_EXTERN (decl) = 1;
22700 FOR_EACH_CLONE (clone, decl)
22701 DECL_NOT_REALLY_EXTERN (clone) = 1;
22702 }
22703
22704 /* Called if RESULT is explicitly instantiated, or is a member of an
22705 explicitly instantiated class. */
22706
22707 void
22708 mark_decl_instantiated (tree result, int extern_p)
22709 {
22710 SET_DECL_EXPLICIT_INSTANTIATION (result);
22711
22712 /* If this entity has already been written out, it's too late to
22713 make any modifications. */
22714 if (TREE_ASM_WRITTEN (result))
22715 return;
22716
22717 /* For anonymous namespace we don't need to do anything. */
22718 if (decl_anon_ns_mem_p (result))
22719 {
22720 gcc_assert (!TREE_PUBLIC (result));
22721 return;
22722 }
22723
22724 if (TREE_CODE (result) != FUNCTION_DECL)
22725 /* The TREE_PUBLIC flag for function declarations will have been
22726 set correctly by tsubst. */
22727 TREE_PUBLIC (result) = 1;
22728
22729 /* This might have been set by an earlier implicit instantiation. */
22730 DECL_COMDAT (result) = 0;
22731
22732 if (extern_p)
22733 DECL_NOT_REALLY_EXTERN (result) = 0;
22734 else
22735 {
22736 mark_definable (result);
22737 mark_needed (result);
22738 /* Always make artificials weak. */
22739 if (DECL_ARTIFICIAL (result) && flag_weak)
22740 comdat_linkage (result);
22741 /* For WIN32 we also want to put explicit instantiations in
22742 linkonce sections. */
22743 else if (TREE_PUBLIC (result))
22744 maybe_make_one_only (result);
22745 if (TREE_CODE (result) == FUNCTION_DECL
22746 && DECL_TEMPLATE_INSTANTIATED (result))
22747 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22748 since start_preparsed_function wouldn't have if we had an earlier
22749 extern explicit instantiation. */
22750 DECL_EXTERNAL (result) = 0;
22751 }
22752
22753 /* If EXTERN_P, then this function will not be emitted -- unless
22754 followed by an explicit instantiation, at which point its linkage
22755 will be adjusted. If !EXTERN_P, then this function will be
22756 emitted here. In neither circumstance do we want
22757 import_export_decl to adjust the linkage. */
22758 DECL_INTERFACE_KNOWN (result) = 1;
22759 }
22760
22761 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22762 important template arguments. If any are missing, we check whether
22763 they're important by using error_mark_node for substituting into any
22764 args that were used for partial ordering (the ones between ARGS and END)
22765 and seeing if it bubbles up. */
22766
22767 static bool
22768 check_undeduced_parms (tree targs, tree args, tree end)
22769 {
22770 bool found = false;
22771 int i;
22772 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22773 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22774 {
22775 found = true;
22776 TREE_VEC_ELT (targs, i) = error_mark_node;
22777 }
22778 if (found)
22779 {
22780 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22781 if (substed == error_mark_node)
22782 return true;
22783 }
22784 return false;
22785 }
22786
22787 /* Given two function templates PAT1 and PAT2, return:
22788
22789 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22790 -1 if PAT2 is more specialized than PAT1.
22791 0 if neither is more specialized.
22792
22793 LEN indicates the number of parameters we should consider
22794 (defaulted parameters should not be considered).
22795
22796 The 1998 std underspecified function template partial ordering, and
22797 DR214 addresses the issue. We take pairs of arguments, one from
22798 each of the templates, and deduce them against each other. One of
22799 the templates will be more specialized if all the *other*
22800 template's arguments deduce against its arguments and at least one
22801 of its arguments *does* *not* deduce against the other template's
22802 corresponding argument. Deduction is done as for class templates.
22803 The arguments used in deduction have reference and top level cv
22804 qualifiers removed. Iff both arguments were originally reference
22805 types *and* deduction succeeds in both directions, an lvalue reference
22806 wins against an rvalue reference and otherwise the template
22807 with the more cv-qualified argument wins for that pairing (if
22808 neither is more cv-qualified, they both are equal). Unlike regular
22809 deduction, after all the arguments have been deduced in this way,
22810 we do *not* verify the deduced template argument values can be
22811 substituted into non-deduced contexts.
22812
22813 The logic can be a bit confusing here, because we look at deduce1 and
22814 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22815 can find template arguments for pat1 to make arg1 look like arg2, that
22816 means that arg2 is at least as specialized as arg1. */
22817
22818 int
22819 more_specialized_fn (tree pat1, tree pat2, int len)
22820 {
22821 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22822 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22823 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22824 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22825 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22826 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22827 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22828 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22829 tree origs1, origs2;
22830 bool lose1 = false;
22831 bool lose2 = false;
22832
22833 /* Remove the this parameter from non-static member functions. If
22834 one is a non-static member function and the other is not a static
22835 member function, remove the first parameter from that function
22836 also. This situation occurs for operator functions where we
22837 locate both a member function (with this pointer) and non-member
22838 operator (with explicit first operand). */
22839 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22840 {
22841 len--; /* LEN is the number of significant arguments for DECL1 */
22842 args1 = TREE_CHAIN (args1);
22843 if (!DECL_STATIC_FUNCTION_P (decl2))
22844 args2 = TREE_CHAIN (args2);
22845 }
22846 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22847 {
22848 args2 = TREE_CHAIN (args2);
22849 if (!DECL_STATIC_FUNCTION_P (decl1))
22850 {
22851 len--;
22852 args1 = TREE_CHAIN (args1);
22853 }
22854 }
22855
22856 /* If only one is a conversion operator, they are unordered. */
22857 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22858 return 0;
22859
22860 /* Consider the return type for a conversion function */
22861 if (DECL_CONV_FN_P (decl1))
22862 {
22863 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22864 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22865 len++;
22866 }
22867
22868 processing_template_decl++;
22869
22870 origs1 = args1;
22871 origs2 = args2;
22872
22873 while (len--
22874 /* Stop when an ellipsis is seen. */
22875 && args1 != NULL_TREE && args2 != NULL_TREE)
22876 {
22877 tree arg1 = TREE_VALUE (args1);
22878 tree arg2 = TREE_VALUE (args2);
22879 int deduce1, deduce2;
22880 int quals1 = -1;
22881 int quals2 = -1;
22882 int ref1 = 0;
22883 int ref2 = 0;
22884
22885 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22886 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22887 {
22888 /* When both arguments are pack expansions, we need only
22889 unify the patterns themselves. */
22890 arg1 = PACK_EXPANSION_PATTERN (arg1);
22891 arg2 = PACK_EXPANSION_PATTERN (arg2);
22892
22893 /* This is the last comparison we need to do. */
22894 len = 0;
22895 }
22896
22897 /* DR 1847: If a particular P contains no template-parameters that
22898 participate in template argument deduction, that P is not used to
22899 determine the ordering. */
22900 if (!uses_deducible_template_parms (arg1)
22901 && !uses_deducible_template_parms (arg2))
22902 goto next;
22903
22904 if (TYPE_REF_P (arg1))
22905 {
22906 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22907 arg1 = TREE_TYPE (arg1);
22908 quals1 = cp_type_quals (arg1);
22909 }
22910
22911 if (TYPE_REF_P (arg2))
22912 {
22913 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22914 arg2 = TREE_TYPE (arg2);
22915 quals2 = cp_type_quals (arg2);
22916 }
22917
22918 arg1 = TYPE_MAIN_VARIANT (arg1);
22919 arg2 = TYPE_MAIN_VARIANT (arg2);
22920
22921 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22922 {
22923 int i, len2 = remaining_arguments (args2);
22924 tree parmvec = make_tree_vec (1);
22925 tree argvec = make_tree_vec (len2);
22926 tree ta = args2;
22927
22928 /* Setup the parameter vector, which contains only ARG1. */
22929 TREE_VEC_ELT (parmvec, 0) = arg1;
22930
22931 /* Setup the argument vector, which contains the remaining
22932 arguments. */
22933 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22934 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22935
22936 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22937 argvec, DEDUCE_EXACT,
22938 /*subr=*/true, /*explain_p=*/false)
22939 == 0);
22940
22941 /* We cannot deduce in the other direction, because ARG1 is
22942 a pack expansion but ARG2 is not. */
22943 deduce2 = 0;
22944 }
22945 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22946 {
22947 int i, len1 = remaining_arguments (args1);
22948 tree parmvec = make_tree_vec (1);
22949 tree argvec = make_tree_vec (len1);
22950 tree ta = args1;
22951
22952 /* Setup the parameter vector, which contains only ARG1. */
22953 TREE_VEC_ELT (parmvec, 0) = arg2;
22954
22955 /* Setup the argument vector, which contains the remaining
22956 arguments. */
22957 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22958 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22959
22960 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22961 argvec, DEDUCE_EXACT,
22962 /*subr=*/true, /*explain_p=*/false)
22963 == 0);
22964
22965 /* We cannot deduce in the other direction, because ARG2 is
22966 a pack expansion but ARG1 is not.*/
22967 deduce1 = 0;
22968 }
22969
22970 else
22971 {
22972 /* The normal case, where neither argument is a pack
22973 expansion. */
22974 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22975 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22976 == 0);
22977 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22978 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22979 == 0);
22980 }
22981
22982 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22983 arg2, then arg2 is not as specialized as arg1. */
22984 if (!deduce1)
22985 lose2 = true;
22986 if (!deduce2)
22987 lose1 = true;
22988
22989 /* "If, for a given type, deduction succeeds in both directions
22990 (i.e., the types are identical after the transformations above)
22991 and both P and A were reference types (before being replaced with
22992 the type referred to above):
22993 - if the type from the argument template was an lvalue reference and
22994 the type from the parameter template was not, the argument type is
22995 considered to be more specialized than the other; otherwise,
22996 - if the type from the argument template is more cv-qualified
22997 than the type from the parameter template (as described above),
22998 the argument type is considered to be more specialized than the other;
22999 otherwise,
23000 - neither type is more specialized than the other." */
23001
23002 if (deduce1 && deduce2)
23003 {
23004 if (ref1 && ref2 && ref1 != ref2)
23005 {
23006 if (ref1 > ref2)
23007 lose1 = true;
23008 else
23009 lose2 = true;
23010 }
23011 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23012 {
23013 if ((quals1 & quals2) == quals2)
23014 lose2 = true;
23015 if ((quals1 & quals2) == quals1)
23016 lose1 = true;
23017 }
23018 }
23019
23020 if (lose1 && lose2)
23021 /* We've failed to deduce something in either direction.
23022 These must be unordered. */
23023 break;
23024
23025 next:
23026
23027 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23028 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23029 /* We have already processed all of the arguments in our
23030 handing of the pack expansion type. */
23031 len = 0;
23032
23033 args1 = TREE_CHAIN (args1);
23034 args2 = TREE_CHAIN (args2);
23035 }
23036
23037 /* "In most cases, all template parameters must have values in order for
23038 deduction to succeed, but for partial ordering purposes a template
23039 parameter may remain without a value provided it is not used in the
23040 types being used for partial ordering."
23041
23042 Thus, if we are missing any of the targs1 we need to substitute into
23043 origs1, then pat2 is not as specialized as pat1. This can happen when
23044 there is a nondeduced context. */
23045 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23046 lose2 = true;
23047 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23048 lose1 = true;
23049
23050 processing_template_decl--;
23051
23052 /* If both deductions succeed, the partial ordering selects the more
23053 constrained template. */
23054 if (!lose1 && !lose2)
23055 {
23056 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23057 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23058 lose1 = !subsumes_constraints (c1, c2);
23059 lose2 = !subsumes_constraints (c2, c1);
23060 }
23061
23062 /* All things being equal, if the next argument is a pack expansion
23063 for one function but not for the other, prefer the
23064 non-variadic function. FIXME this is bogus; see c++/41958. */
23065 if (lose1 == lose2
23066 && args1 && TREE_VALUE (args1)
23067 && args2 && TREE_VALUE (args2))
23068 {
23069 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23070 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23071 }
23072
23073 if (lose1 == lose2)
23074 return 0;
23075 else if (!lose1)
23076 return 1;
23077 else
23078 return -1;
23079 }
23080
23081 /* Determine which of two partial specializations of TMPL is more
23082 specialized.
23083
23084 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23085 to the first partial specialization. The TREE_PURPOSE is the
23086 innermost set of template parameters for the partial
23087 specialization. PAT2 is similar, but for the second template.
23088
23089 Return 1 if the first partial specialization is more specialized;
23090 -1 if the second is more specialized; 0 if neither is more
23091 specialized.
23092
23093 See [temp.class.order] for information about determining which of
23094 two templates is more specialized. */
23095
23096 static int
23097 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23098 {
23099 tree targs;
23100 int winner = 0;
23101 bool any_deductions = false;
23102
23103 tree tmpl1 = TREE_VALUE (pat1);
23104 tree tmpl2 = TREE_VALUE (pat2);
23105 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23106 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23107
23108 /* Just like what happens for functions, if we are ordering between
23109 different template specializations, we may encounter dependent
23110 types in the arguments, and we need our dependency check functions
23111 to behave correctly. */
23112 ++processing_template_decl;
23113 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23114 if (targs)
23115 {
23116 --winner;
23117 any_deductions = true;
23118 }
23119
23120 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23121 if (targs)
23122 {
23123 ++winner;
23124 any_deductions = true;
23125 }
23126 --processing_template_decl;
23127
23128 /* If both deductions succeed, the partial ordering selects the more
23129 constrained template. */
23130 if (!winner && any_deductions)
23131 return more_constrained (tmpl1, tmpl2);
23132
23133 /* In the case of a tie where at least one of the templates
23134 has a parameter pack at the end, the template with the most
23135 non-packed parameters wins. */
23136 if (winner == 0
23137 && any_deductions
23138 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23139 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23140 {
23141 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23142 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23143 int len1 = TREE_VEC_LENGTH (args1);
23144 int len2 = TREE_VEC_LENGTH (args2);
23145
23146 /* We don't count the pack expansion at the end. */
23147 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23148 --len1;
23149 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23150 --len2;
23151
23152 if (len1 > len2)
23153 return 1;
23154 else if (len1 < len2)
23155 return -1;
23156 }
23157
23158 return winner;
23159 }
23160
23161 /* Return the template arguments that will produce the function signature
23162 DECL from the function template FN, with the explicit template
23163 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23164 also match. Return NULL_TREE if no satisfactory arguments could be
23165 found. */
23166
23167 static tree
23168 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23169 {
23170 int ntparms = DECL_NTPARMS (fn);
23171 tree targs = make_tree_vec (ntparms);
23172 tree decl_type = TREE_TYPE (decl);
23173 tree decl_arg_types;
23174 tree *args;
23175 unsigned int nargs, ix;
23176 tree arg;
23177
23178 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23179
23180 /* Never do unification on the 'this' parameter. */
23181 decl_arg_types = skip_artificial_parms_for (decl,
23182 TYPE_ARG_TYPES (decl_type));
23183
23184 nargs = list_length (decl_arg_types);
23185 args = XALLOCAVEC (tree, nargs);
23186 for (arg = decl_arg_types, ix = 0;
23187 arg != NULL_TREE && arg != void_list_node;
23188 arg = TREE_CHAIN (arg), ++ix)
23189 args[ix] = TREE_VALUE (arg);
23190
23191 if (fn_type_unification (fn, explicit_args, targs,
23192 args, ix,
23193 (check_rettype || DECL_CONV_FN_P (fn)
23194 ? TREE_TYPE (decl_type) : NULL_TREE),
23195 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23196 /*explain_p=*/false,
23197 /*decltype*/false)
23198 == error_mark_node)
23199 return NULL_TREE;
23200
23201 return targs;
23202 }
23203
23204 /* Return the innermost template arguments that, when applied to a partial
23205 specialization SPEC_TMPL of TMPL, yield the ARGS.
23206
23207 For example, suppose we have:
23208
23209 template <class T, class U> struct S {};
23210 template <class T> struct S<T*, int> {};
23211
23212 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23213 partial specialization and the ARGS will be {double*, int}. The resulting
23214 vector will be {double}, indicating that `T' is bound to `double'. */
23215
23216 static tree
23217 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23218 {
23219 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23220 tree spec_args
23221 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23222 int i, ntparms = TREE_VEC_LENGTH (tparms);
23223 tree deduced_args;
23224 tree innermost_deduced_args;
23225
23226 innermost_deduced_args = make_tree_vec (ntparms);
23227 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23228 {
23229 deduced_args = copy_node (args);
23230 SET_TMPL_ARGS_LEVEL (deduced_args,
23231 TMPL_ARGS_DEPTH (deduced_args),
23232 innermost_deduced_args);
23233 }
23234 else
23235 deduced_args = innermost_deduced_args;
23236
23237 bool tried_array_deduction = (cxx_dialect < cxx17);
23238 again:
23239 if (unify (tparms, deduced_args,
23240 INNERMOST_TEMPLATE_ARGS (spec_args),
23241 INNERMOST_TEMPLATE_ARGS (args),
23242 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23243 return NULL_TREE;
23244
23245 for (i = 0; i < ntparms; ++i)
23246 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23247 {
23248 if (!tried_array_deduction)
23249 {
23250 try_array_deduction (tparms, innermost_deduced_args,
23251 INNERMOST_TEMPLATE_ARGS (spec_args));
23252 tried_array_deduction = true;
23253 if (TREE_VEC_ELT (innermost_deduced_args, i))
23254 goto again;
23255 }
23256 return NULL_TREE;
23257 }
23258
23259 if (!push_tinst_level (spec_tmpl, deduced_args))
23260 {
23261 excessive_deduction_depth = true;
23262 return NULL_TREE;
23263 }
23264
23265 /* Verify that nondeduced template arguments agree with the type
23266 obtained from argument deduction.
23267
23268 For example:
23269
23270 struct A { typedef int X; };
23271 template <class T, class U> struct C {};
23272 template <class T> struct C<T, typename T::X> {};
23273
23274 Then with the instantiation `C<A, int>', we can deduce that
23275 `T' is `A' but unify () does not check whether `typename T::X'
23276 is `int'. */
23277 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23278
23279 if (spec_args != error_mark_node)
23280 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23281 INNERMOST_TEMPLATE_ARGS (spec_args),
23282 tmpl, tf_none, false, false);
23283
23284 pop_tinst_level ();
23285
23286 if (spec_args == error_mark_node
23287 /* We only need to check the innermost arguments; the other
23288 arguments will always agree. */
23289 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23290 INNERMOST_TEMPLATE_ARGS (args)))
23291 return NULL_TREE;
23292
23293 /* Now that we have bindings for all of the template arguments,
23294 ensure that the arguments deduced for the template template
23295 parameters have compatible template parameter lists. See the use
23296 of template_template_parm_bindings_ok_p in fn_type_unification
23297 for more information. */
23298 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23299 return NULL_TREE;
23300
23301 return deduced_args;
23302 }
23303
23304 // Compare two function templates T1 and T2 by deducing bindings
23305 // from one against the other. If both deductions succeed, compare
23306 // constraints to see which is more constrained.
23307 static int
23308 more_specialized_inst (tree t1, tree t2)
23309 {
23310 int fate = 0;
23311 int count = 0;
23312
23313 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23314 {
23315 --fate;
23316 ++count;
23317 }
23318
23319 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23320 {
23321 ++fate;
23322 ++count;
23323 }
23324
23325 // If both deductions succeed, then one may be more constrained.
23326 if (count == 2 && fate == 0)
23327 fate = more_constrained (t1, t2);
23328
23329 return fate;
23330 }
23331
23332 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23333 Return the TREE_LIST node with the most specialized template, if
23334 any. If there is no most specialized template, the error_mark_node
23335 is returned.
23336
23337 Note that this function does not look at, or modify, the
23338 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23339 returned is one of the elements of INSTANTIATIONS, callers may
23340 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23341 and retrieve it from the value returned. */
23342
23343 tree
23344 most_specialized_instantiation (tree templates)
23345 {
23346 tree fn, champ;
23347
23348 ++processing_template_decl;
23349
23350 champ = templates;
23351 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23352 {
23353 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23354 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23355 if (fate == -1)
23356 champ = fn;
23357 else if (!fate)
23358 {
23359 /* Equally specialized, move to next function. If there
23360 is no next function, nothing's most specialized. */
23361 fn = TREE_CHAIN (fn);
23362 champ = fn;
23363 if (!fn)
23364 break;
23365 }
23366 }
23367
23368 if (champ)
23369 /* Now verify that champ is better than everything earlier in the
23370 instantiation list. */
23371 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23372 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23373 {
23374 champ = NULL_TREE;
23375 break;
23376 }
23377 }
23378
23379 processing_template_decl--;
23380
23381 if (!champ)
23382 return error_mark_node;
23383
23384 return champ;
23385 }
23386
23387 /* If DECL is a specialization of some template, return the most
23388 general such template. Otherwise, returns NULL_TREE.
23389
23390 For example, given:
23391
23392 template <class T> struct S { template <class U> void f(U); };
23393
23394 if TMPL is `template <class U> void S<int>::f(U)' this will return
23395 the full template. This function will not trace past partial
23396 specializations, however. For example, given in addition:
23397
23398 template <class T> struct S<T*> { template <class U> void f(U); };
23399
23400 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23401 `template <class T> template <class U> S<T*>::f(U)'. */
23402
23403 tree
23404 most_general_template (tree decl)
23405 {
23406 if (TREE_CODE (decl) != TEMPLATE_DECL)
23407 {
23408 if (tree tinfo = get_template_info (decl))
23409 decl = TI_TEMPLATE (tinfo);
23410 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23411 template friend, or a FIELD_DECL for a capture pack. */
23412 if (TREE_CODE (decl) != TEMPLATE_DECL)
23413 return NULL_TREE;
23414 }
23415
23416 /* Look for more and more general templates. */
23417 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23418 {
23419 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23420 (See cp-tree.h for details.) */
23421 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23422 break;
23423
23424 if (CLASS_TYPE_P (TREE_TYPE (decl))
23425 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23426 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23427 break;
23428
23429 /* Stop if we run into an explicitly specialized class template. */
23430 if (!DECL_NAMESPACE_SCOPE_P (decl)
23431 && DECL_CONTEXT (decl)
23432 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23433 break;
23434
23435 decl = DECL_TI_TEMPLATE (decl);
23436 }
23437
23438 return decl;
23439 }
23440
23441 /* Return the most specialized of the template partial specializations
23442 which can produce TARGET, a specialization of some class or variable
23443 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23444 a TEMPLATE_DECL node corresponding to the partial specialization, while
23445 the TREE_PURPOSE is the set of template arguments that must be
23446 substituted into the template pattern in order to generate TARGET.
23447
23448 If the choice of partial specialization is ambiguous, a diagnostic
23449 is issued, and the error_mark_node is returned. If there are no
23450 partial specializations matching TARGET, then NULL_TREE is
23451 returned, indicating that the primary template should be used. */
23452
23453 static tree
23454 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23455 {
23456 tree list = NULL_TREE;
23457 tree t;
23458 tree champ;
23459 int fate;
23460 bool ambiguous_p;
23461 tree outer_args = NULL_TREE;
23462 tree tmpl, args;
23463
23464 if (TYPE_P (target))
23465 {
23466 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23467 tmpl = TI_TEMPLATE (tinfo);
23468 args = TI_ARGS (tinfo);
23469 }
23470 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23471 {
23472 tmpl = TREE_OPERAND (target, 0);
23473 args = TREE_OPERAND (target, 1);
23474 }
23475 else if (VAR_P (target))
23476 {
23477 tree tinfo = DECL_TEMPLATE_INFO (target);
23478 tmpl = TI_TEMPLATE (tinfo);
23479 args = TI_ARGS (tinfo);
23480 }
23481 else
23482 gcc_unreachable ();
23483
23484 tree main_tmpl = most_general_template (tmpl);
23485
23486 /* For determining which partial specialization to use, only the
23487 innermost args are interesting. */
23488 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23489 {
23490 outer_args = strip_innermost_template_args (args, 1);
23491 args = INNERMOST_TEMPLATE_ARGS (args);
23492 }
23493
23494 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23495 {
23496 tree spec_args;
23497 tree spec_tmpl = TREE_VALUE (t);
23498
23499 if (outer_args)
23500 {
23501 /* Substitute in the template args from the enclosing class. */
23502 ++processing_template_decl;
23503 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23504 --processing_template_decl;
23505 }
23506
23507 if (spec_tmpl == error_mark_node)
23508 return error_mark_node;
23509
23510 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23511 if (spec_args)
23512 {
23513 if (outer_args)
23514 spec_args = add_to_template_args (outer_args, spec_args);
23515
23516 /* Keep the candidate only if the constraints are satisfied,
23517 or if we're not compiling with concepts. */
23518 if (!flag_concepts
23519 || constraints_satisfied_p (spec_tmpl, spec_args))
23520 {
23521 list = tree_cons (spec_args, TREE_VALUE (t), list);
23522 TREE_TYPE (list) = TREE_TYPE (t);
23523 }
23524 }
23525 }
23526
23527 if (! list)
23528 return NULL_TREE;
23529
23530 ambiguous_p = false;
23531 t = list;
23532 champ = t;
23533 t = TREE_CHAIN (t);
23534 for (; t; t = TREE_CHAIN (t))
23535 {
23536 fate = more_specialized_partial_spec (tmpl, champ, t);
23537 if (fate == 1)
23538 ;
23539 else
23540 {
23541 if (fate == 0)
23542 {
23543 t = TREE_CHAIN (t);
23544 if (! t)
23545 {
23546 ambiguous_p = true;
23547 break;
23548 }
23549 }
23550 champ = t;
23551 }
23552 }
23553
23554 if (!ambiguous_p)
23555 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23556 {
23557 fate = more_specialized_partial_spec (tmpl, champ, t);
23558 if (fate != 1)
23559 {
23560 ambiguous_p = true;
23561 break;
23562 }
23563 }
23564
23565 if (ambiguous_p)
23566 {
23567 const char *str;
23568 char *spaces = NULL;
23569 if (!(complain & tf_error))
23570 return error_mark_node;
23571 if (TYPE_P (target))
23572 error ("ambiguous template instantiation for %q#T", target);
23573 else
23574 error ("ambiguous template instantiation for %q#D", target);
23575 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23576 for (t = list; t; t = TREE_CHAIN (t))
23577 {
23578 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23579 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23580 "%s %#qS", spaces ? spaces : str, subst);
23581 spaces = spaces ? spaces : get_spaces (str);
23582 }
23583 free (spaces);
23584 return error_mark_node;
23585 }
23586
23587 return champ;
23588 }
23589
23590 /* Explicitly instantiate DECL. */
23591
23592 void
23593 do_decl_instantiation (tree decl, tree storage)
23594 {
23595 tree result = NULL_TREE;
23596 int extern_p = 0;
23597
23598 if (!decl || decl == error_mark_node)
23599 /* An error occurred, for which grokdeclarator has already issued
23600 an appropriate message. */
23601 return;
23602 else if (! DECL_LANG_SPECIFIC (decl))
23603 {
23604 error ("explicit instantiation of non-template %q#D", decl);
23605 return;
23606 }
23607 else if (DECL_DECLARED_CONCEPT_P (decl))
23608 {
23609 if (VAR_P (decl))
23610 error ("explicit instantiation of variable concept %q#D", decl);
23611 else
23612 error ("explicit instantiation of function concept %q#D", decl);
23613 return;
23614 }
23615
23616 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23617 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23618
23619 if (VAR_P (decl) && !var_templ)
23620 {
23621 /* There is an asymmetry here in the way VAR_DECLs and
23622 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23623 the latter, the DECL we get back will be marked as a
23624 template instantiation, and the appropriate
23625 DECL_TEMPLATE_INFO will be set up. This does not happen for
23626 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23627 should handle VAR_DECLs as it currently handles
23628 FUNCTION_DECLs. */
23629 if (!DECL_CLASS_SCOPE_P (decl))
23630 {
23631 error ("%qD is not a static data member of a class template", decl);
23632 return;
23633 }
23634 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23635 if (!result || !VAR_P (result))
23636 {
23637 error ("no matching template for %qD found", decl);
23638 return;
23639 }
23640 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23641 {
23642 error ("type %qT for explicit instantiation %qD does not match "
23643 "declared type %qT", TREE_TYPE (result), decl,
23644 TREE_TYPE (decl));
23645 return;
23646 }
23647 }
23648 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23649 {
23650 error ("explicit instantiation of %q#D", decl);
23651 return;
23652 }
23653 else
23654 result = decl;
23655
23656 /* Check for various error cases. Note that if the explicit
23657 instantiation is valid the RESULT will currently be marked as an
23658 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23659 until we get here. */
23660
23661 if (DECL_TEMPLATE_SPECIALIZATION (result))
23662 {
23663 /* DR 259 [temp.spec].
23664
23665 Both an explicit instantiation and a declaration of an explicit
23666 specialization shall not appear in a program unless the explicit
23667 instantiation follows a declaration of the explicit specialization.
23668
23669 For a given set of template parameters, if an explicit
23670 instantiation of a template appears after a declaration of an
23671 explicit specialization for that template, the explicit
23672 instantiation has no effect. */
23673 return;
23674 }
23675 else if (DECL_EXPLICIT_INSTANTIATION (result))
23676 {
23677 /* [temp.spec]
23678
23679 No program shall explicitly instantiate any template more
23680 than once.
23681
23682 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23683 the first instantiation was `extern' and the second is not,
23684 and EXTERN_P for the opposite case. */
23685 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23686 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23687 /* If an "extern" explicit instantiation follows an ordinary
23688 explicit instantiation, the template is instantiated. */
23689 if (extern_p)
23690 return;
23691 }
23692 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23693 {
23694 error ("no matching template for %qD found", result);
23695 return;
23696 }
23697 else if (!DECL_TEMPLATE_INFO (result))
23698 {
23699 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23700 return;
23701 }
23702
23703 if (storage == NULL_TREE)
23704 ;
23705 else if (storage == ridpointers[(int) RID_EXTERN])
23706 {
23707 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23708 pedwarn (input_location, OPT_Wpedantic,
23709 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23710 "instantiations");
23711 extern_p = 1;
23712 }
23713 else
23714 error ("storage class %qD applied to template instantiation", storage);
23715
23716 check_explicit_instantiation_namespace (result);
23717 mark_decl_instantiated (result, extern_p);
23718 if (! extern_p)
23719 instantiate_decl (result, /*defer_ok=*/true,
23720 /*expl_inst_class_mem_p=*/false);
23721 }
23722
23723 static void
23724 mark_class_instantiated (tree t, int extern_p)
23725 {
23726 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23727 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23728 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23729 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23730 if (! extern_p)
23731 {
23732 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23733 rest_of_type_compilation (t, 1);
23734 }
23735 }
23736
23737 /* Called from do_type_instantiation through binding_table_foreach to
23738 do recursive instantiation for the type bound in ENTRY. */
23739 static void
23740 bt_instantiate_type_proc (binding_entry entry, void *data)
23741 {
23742 tree storage = *(tree *) data;
23743
23744 if (MAYBE_CLASS_TYPE_P (entry->type)
23745 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23746 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23747 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23748 }
23749
23750 /* Perform an explicit instantiation of template class T. STORAGE, if
23751 non-null, is the RID for extern, inline or static. COMPLAIN is
23752 nonzero if this is called from the parser, zero if called recursively,
23753 since the standard is unclear (as detailed below). */
23754
23755 void
23756 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23757 {
23758 int extern_p = 0;
23759 int nomem_p = 0;
23760 int static_p = 0;
23761 int previous_instantiation_extern_p = 0;
23762
23763 if (TREE_CODE (t) == TYPE_DECL)
23764 t = TREE_TYPE (t);
23765
23766 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23767 {
23768 tree tmpl =
23769 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23770 if (tmpl)
23771 error ("explicit instantiation of non-class template %qD", tmpl);
23772 else
23773 error ("explicit instantiation of non-template type %qT", t);
23774 return;
23775 }
23776
23777 complete_type (t);
23778
23779 if (!COMPLETE_TYPE_P (t))
23780 {
23781 if (complain & tf_error)
23782 error ("explicit instantiation of %q#T before definition of template",
23783 t);
23784 return;
23785 }
23786
23787 if (storage != NULL_TREE)
23788 {
23789 if (!in_system_header_at (input_location))
23790 {
23791 if (storage == ridpointers[(int) RID_EXTERN])
23792 {
23793 if (cxx_dialect == cxx98)
23794 pedwarn (input_location, OPT_Wpedantic,
23795 "ISO C++ 1998 forbids the use of %<extern%> on "
23796 "explicit instantiations");
23797 }
23798 else
23799 pedwarn (input_location, OPT_Wpedantic,
23800 "ISO C++ forbids the use of %qE"
23801 " on explicit instantiations", storage);
23802 }
23803
23804 if (storage == ridpointers[(int) RID_INLINE])
23805 nomem_p = 1;
23806 else if (storage == ridpointers[(int) RID_EXTERN])
23807 extern_p = 1;
23808 else if (storage == ridpointers[(int) RID_STATIC])
23809 static_p = 1;
23810 else
23811 {
23812 error ("storage class %qD applied to template instantiation",
23813 storage);
23814 extern_p = 0;
23815 }
23816 }
23817
23818 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23819 {
23820 /* DR 259 [temp.spec].
23821
23822 Both an explicit instantiation and a declaration of an explicit
23823 specialization shall not appear in a program unless the explicit
23824 instantiation follows a declaration of the explicit specialization.
23825
23826 For a given set of template parameters, if an explicit
23827 instantiation of a template appears after a declaration of an
23828 explicit specialization for that template, the explicit
23829 instantiation has no effect. */
23830 return;
23831 }
23832 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23833 {
23834 /* [temp.spec]
23835
23836 No program shall explicitly instantiate any template more
23837 than once.
23838
23839 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23840 instantiation was `extern'. If EXTERN_P then the second is.
23841 These cases are OK. */
23842 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23843
23844 if (!previous_instantiation_extern_p && !extern_p
23845 && (complain & tf_error))
23846 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23847
23848 /* If we've already instantiated the template, just return now. */
23849 if (!CLASSTYPE_INTERFACE_ONLY (t))
23850 return;
23851 }
23852
23853 check_explicit_instantiation_namespace (TYPE_NAME (t));
23854 mark_class_instantiated (t, extern_p);
23855
23856 if (nomem_p)
23857 return;
23858
23859 /* In contrast to implicit instantiation, where only the
23860 declarations, and not the definitions, of members are
23861 instantiated, we have here:
23862
23863 [temp.explicit]
23864
23865 The explicit instantiation of a class template specialization
23866 implies the instantiation of all of its members not
23867 previously explicitly specialized in the translation unit
23868 containing the explicit instantiation.
23869
23870 Of course, we can't instantiate member template classes, since we
23871 don't have any arguments for them. Note that the standard is
23872 unclear on whether the instantiation of the members are
23873 *explicit* instantiations or not. However, the most natural
23874 interpretation is that it should be an explicit
23875 instantiation. */
23876 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23877 if ((VAR_P (fld)
23878 || (TREE_CODE (fld) == FUNCTION_DECL
23879 && !static_p
23880 && user_provided_p (fld)))
23881 && DECL_TEMPLATE_INSTANTIATION (fld))
23882 {
23883 mark_decl_instantiated (fld, extern_p);
23884 if (! extern_p)
23885 instantiate_decl (fld, /*defer_ok=*/true,
23886 /*expl_inst_class_mem_p=*/true);
23887 }
23888
23889 if (CLASSTYPE_NESTED_UTDS (t))
23890 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23891 bt_instantiate_type_proc, &storage);
23892 }
23893
23894 /* Given a function DECL, which is a specialization of TMPL, modify
23895 DECL to be a re-instantiation of TMPL with the same template
23896 arguments. TMPL should be the template into which tsubst'ing
23897 should occur for DECL, not the most general template.
23898
23899 One reason for doing this is a scenario like this:
23900
23901 template <class T>
23902 void f(const T&, int i);
23903
23904 void g() { f(3, 7); }
23905
23906 template <class T>
23907 void f(const T& t, const int i) { }
23908
23909 Note that when the template is first instantiated, with
23910 instantiate_template, the resulting DECL will have no name for the
23911 first parameter, and the wrong type for the second. So, when we go
23912 to instantiate the DECL, we regenerate it. */
23913
23914 static void
23915 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23916 {
23917 /* The arguments used to instantiate DECL, from the most general
23918 template. */
23919 tree code_pattern;
23920
23921 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23922
23923 /* Make sure that we can see identifiers, and compute access
23924 correctly. */
23925 push_access_scope (decl);
23926
23927 if (TREE_CODE (decl) == FUNCTION_DECL)
23928 {
23929 tree decl_parm;
23930 tree pattern_parm;
23931 tree specs;
23932 int args_depth;
23933 int parms_depth;
23934
23935 args_depth = TMPL_ARGS_DEPTH (args);
23936 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23937 if (args_depth > parms_depth)
23938 args = get_innermost_template_args (args, parms_depth);
23939
23940 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23941 args, tf_error, NULL_TREE,
23942 /*defer_ok*/false);
23943 if (specs && specs != error_mark_node)
23944 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23945 specs);
23946
23947 /* Merge parameter declarations. */
23948 decl_parm = skip_artificial_parms_for (decl,
23949 DECL_ARGUMENTS (decl));
23950 pattern_parm
23951 = skip_artificial_parms_for (code_pattern,
23952 DECL_ARGUMENTS (code_pattern));
23953 while (decl_parm && !DECL_PACK_P (pattern_parm))
23954 {
23955 tree parm_type;
23956 tree attributes;
23957
23958 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23959 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23960 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23961 NULL_TREE);
23962 parm_type = type_decays_to (parm_type);
23963 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23964 TREE_TYPE (decl_parm) = parm_type;
23965 attributes = DECL_ATTRIBUTES (pattern_parm);
23966 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23967 {
23968 DECL_ATTRIBUTES (decl_parm) = attributes;
23969 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23970 }
23971 decl_parm = DECL_CHAIN (decl_parm);
23972 pattern_parm = DECL_CHAIN (pattern_parm);
23973 }
23974 /* Merge any parameters that match with the function parameter
23975 pack. */
23976 if (pattern_parm && DECL_PACK_P (pattern_parm))
23977 {
23978 int i, len;
23979 tree expanded_types;
23980 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23981 the parameters in this function parameter pack. */
23982 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23983 args, tf_error, NULL_TREE);
23984 len = TREE_VEC_LENGTH (expanded_types);
23985 for (i = 0; i < len; i++)
23986 {
23987 tree parm_type;
23988 tree attributes;
23989
23990 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23991 /* Rename the parameter to include the index. */
23992 DECL_NAME (decl_parm) =
23993 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23994 parm_type = TREE_VEC_ELT (expanded_types, i);
23995 parm_type = type_decays_to (parm_type);
23996 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23997 TREE_TYPE (decl_parm) = parm_type;
23998 attributes = DECL_ATTRIBUTES (pattern_parm);
23999 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24000 {
24001 DECL_ATTRIBUTES (decl_parm) = attributes;
24002 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24003 }
24004 decl_parm = DECL_CHAIN (decl_parm);
24005 }
24006 }
24007 /* Merge additional specifiers from the CODE_PATTERN. */
24008 if (DECL_DECLARED_INLINE_P (code_pattern)
24009 && !DECL_DECLARED_INLINE_P (decl))
24010 DECL_DECLARED_INLINE_P (decl) = 1;
24011 }
24012 else if (VAR_P (decl))
24013 {
24014 start_lambda_scope (decl);
24015 DECL_INITIAL (decl) =
24016 tsubst_expr (DECL_INITIAL (code_pattern), args,
24017 tf_error, DECL_TI_TEMPLATE (decl),
24018 /*integral_constant_expression_p=*/false);
24019 finish_lambda_scope ();
24020 if (VAR_HAD_UNKNOWN_BOUND (decl))
24021 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24022 tf_error, DECL_TI_TEMPLATE (decl));
24023 }
24024 else
24025 gcc_unreachable ();
24026
24027 pop_access_scope (decl);
24028 }
24029
24030 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24031 substituted to get DECL. */
24032
24033 tree
24034 template_for_substitution (tree decl)
24035 {
24036 tree tmpl = DECL_TI_TEMPLATE (decl);
24037
24038 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24039 for the instantiation. This is not always the most general
24040 template. Consider, for example:
24041
24042 template <class T>
24043 struct S { template <class U> void f();
24044 template <> void f<int>(); };
24045
24046 and an instantiation of S<double>::f<int>. We want TD to be the
24047 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24048 while (/* An instantiation cannot have a definition, so we need a
24049 more general template. */
24050 DECL_TEMPLATE_INSTANTIATION (tmpl)
24051 /* We must also deal with friend templates. Given:
24052
24053 template <class T> struct S {
24054 template <class U> friend void f() {};
24055 };
24056
24057 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24058 so far as the language is concerned, but that's still
24059 where we get the pattern for the instantiation from. On
24060 other hand, if the definition comes outside the class, say:
24061
24062 template <class T> struct S {
24063 template <class U> friend void f();
24064 };
24065 template <class U> friend void f() {}
24066
24067 we don't need to look any further. That's what the check for
24068 DECL_INITIAL is for. */
24069 || (TREE_CODE (decl) == FUNCTION_DECL
24070 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24071 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24072 {
24073 /* The present template, TD, should not be a definition. If it
24074 were a definition, we should be using it! Note that we
24075 cannot restructure the loop to just keep going until we find
24076 a template with a definition, since that might go too far if
24077 a specialization was declared, but not defined. */
24078
24079 /* Fetch the more general template. */
24080 tmpl = DECL_TI_TEMPLATE (tmpl);
24081 }
24082
24083 return tmpl;
24084 }
24085
24086 /* Returns true if we need to instantiate this template instance even if we
24087 know we aren't going to emit it. */
24088
24089 bool
24090 always_instantiate_p (tree decl)
24091 {
24092 /* We always instantiate inline functions so that we can inline them. An
24093 explicit instantiation declaration prohibits implicit instantiation of
24094 non-inline functions. With high levels of optimization, we would
24095 normally inline non-inline functions -- but we're not allowed to do
24096 that for "extern template" functions. Therefore, we check
24097 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24098 return ((TREE_CODE (decl) == FUNCTION_DECL
24099 && (DECL_DECLARED_INLINE_P (decl)
24100 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24101 /* And we need to instantiate static data members so that
24102 their initializers are available in integral constant
24103 expressions. */
24104 || (VAR_P (decl)
24105 && decl_maybe_constant_var_p (decl)));
24106 }
24107
24108 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24109 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24110 error, true otherwise. */
24111
24112 bool
24113 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24114 {
24115 tree fntype, spec, noex, clone;
24116
24117 /* Don't instantiate a noexcept-specification from template context. */
24118 if (processing_template_decl
24119 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24120 return true;
24121
24122 if (DECL_CLONED_FUNCTION_P (fn))
24123 fn = DECL_CLONED_FUNCTION (fn);
24124 fntype = TREE_TYPE (fn);
24125 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24126
24127 if (!spec || !TREE_PURPOSE (spec))
24128 return true;
24129
24130 noex = TREE_PURPOSE (spec);
24131
24132 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24133 {
24134 static hash_set<tree>* fns = new hash_set<tree>;
24135 bool added = false;
24136 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24137 spec = get_defaulted_eh_spec (fn, complain);
24138 else if (!(added = !fns->add (fn)))
24139 {
24140 /* If hash_set::add returns true, the element was already there. */
24141 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24142 DECL_SOURCE_LOCATION (fn));
24143 error_at (loc,
24144 "exception specification of %qD depends on itself",
24145 fn);
24146 spec = noexcept_false_spec;
24147 }
24148 else if (push_tinst_level (fn))
24149 {
24150 push_access_scope (fn);
24151 push_deferring_access_checks (dk_no_deferred);
24152 input_location = DECL_SOURCE_LOCATION (fn);
24153 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24154 DEFERRED_NOEXCEPT_ARGS (noex),
24155 tf_warning_or_error, fn,
24156 /*function_p=*/false,
24157 /*integral_constant_expression_p=*/true);
24158 spec = build_noexcept_spec (noex, tf_warning_or_error);
24159 pop_deferring_access_checks ();
24160 pop_access_scope (fn);
24161 pop_tinst_level ();
24162 if (spec == error_mark_node)
24163 spec = noexcept_false_spec;
24164 }
24165 else
24166 spec = noexcept_false_spec;
24167
24168 if (added)
24169 fns->remove (fn);
24170
24171 if (spec == error_mark_node)
24172 return false;
24173
24174 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24175 }
24176
24177 FOR_EACH_CLONE (clone, fn)
24178 {
24179 if (TREE_TYPE (clone) == fntype)
24180 TREE_TYPE (clone) = TREE_TYPE (fn);
24181 else
24182 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24183 }
24184
24185 return true;
24186 }
24187
24188 /* We're starting to process the function INST, an instantiation of PATTERN;
24189 add their parameters to local_specializations. */
24190
24191 static void
24192 register_parameter_specializations (tree pattern, tree inst)
24193 {
24194 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24195 tree spec_parm = DECL_ARGUMENTS (inst);
24196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24197 {
24198 register_local_specialization (spec_parm, tmpl_parm);
24199 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24200 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24201 }
24202 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24203 {
24204 if (!DECL_PACK_P (tmpl_parm))
24205 {
24206 register_local_specialization (spec_parm, tmpl_parm);
24207 spec_parm = DECL_CHAIN (spec_parm);
24208 }
24209 else
24210 {
24211 /* Register the (value) argument pack as a specialization of
24212 TMPL_PARM, then move on. */
24213 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24214 register_local_specialization (argpack, tmpl_parm);
24215 }
24216 }
24217 gcc_assert (!spec_parm);
24218 }
24219
24220 /* Produce the definition of D, a _DECL generated from a template. If
24221 DEFER_OK is true, then we don't have to actually do the
24222 instantiation now; we just have to do it sometime. Normally it is
24223 an error if this is an explicit instantiation but D is undefined.
24224 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24225 instantiated class template. */
24226
24227 tree
24228 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24229 {
24230 tree tmpl = DECL_TI_TEMPLATE (d);
24231 tree gen_args;
24232 tree args;
24233 tree td;
24234 tree code_pattern;
24235 tree spec;
24236 tree gen_tmpl;
24237 bool pattern_defined;
24238 location_t saved_loc = input_location;
24239 int saved_unevaluated_operand = cp_unevaluated_operand;
24240 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24241 bool external_p;
24242 bool deleted_p;
24243
24244 /* This function should only be used to instantiate templates for
24245 functions and static member variables. */
24246 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24247
24248 /* A concept is never instantiated. */
24249 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24250
24251 /* Variables are never deferred; if instantiation is required, they
24252 are instantiated right away. That allows for better code in the
24253 case that an expression refers to the value of the variable --
24254 if the variable has a constant value the referring expression can
24255 take advantage of that fact. */
24256 if (VAR_P (d))
24257 defer_ok = false;
24258
24259 /* Don't instantiate cloned functions. Instead, instantiate the
24260 functions they cloned. */
24261 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24262 d = DECL_CLONED_FUNCTION (d);
24263
24264 if (DECL_TEMPLATE_INSTANTIATED (d)
24265 || (TREE_CODE (d) == FUNCTION_DECL
24266 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24267 || DECL_TEMPLATE_SPECIALIZATION (d))
24268 /* D has already been instantiated or explicitly specialized, so
24269 there's nothing for us to do here.
24270
24271 It might seem reasonable to check whether or not D is an explicit
24272 instantiation, and, if so, stop here. But when an explicit
24273 instantiation is deferred until the end of the compilation,
24274 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24275 the instantiation. */
24276 return d;
24277
24278 /* Check to see whether we know that this template will be
24279 instantiated in some other file, as with "extern template"
24280 extension. */
24281 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24282
24283 /* In general, we do not instantiate such templates. */
24284 if (external_p && !always_instantiate_p (d))
24285 return d;
24286
24287 gen_tmpl = most_general_template (tmpl);
24288 gen_args = DECL_TI_ARGS (d);
24289
24290 if (tmpl != gen_tmpl)
24291 /* We should already have the extra args. */
24292 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24293 == TMPL_ARGS_DEPTH (gen_args));
24294 /* And what's in the hash table should match D. */
24295 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24296 || spec == NULL_TREE);
24297
24298 /* This needs to happen before any tsubsting. */
24299 if (! push_tinst_level (d))
24300 return d;
24301
24302 timevar_push (TV_TEMPLATE_INST);
24303
24304 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24305 for the instantiation. */
24306 td = template_for_substitution (d);
24307 args = gen_args;
24308
24309 if (VAR_P (d))
24310 {
24311 /* Look up an explicit specialization, if any. */
24312 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24313 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24314 if (elt && elt != error_mark_node)
24315 {
24316 td = TREE_VALUE (elt);
24317 args = TREE_PURPOSE (elt);
24318 }
24319 }
24320
24321 code_pattern = DECL_TEMPLATE_RESULT (td);
24322
24323 /* We should never be trying to instantiate a member of a class
24324 template or partial specialization. */
24325 gcc_assert (d != code_pattern);
24326
24327 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24328 || DECL_TEMPLATE_SPECIALIZATION (td))
24329 /* In the case of a friend template whose definition is provided
24330 outside the class, we may have too many arguments. Drop the
24331 ones we don't need. The same is true for specializations. */
24332 args = get_innermost_template_args
24333 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24334
24335 if (TREE_CODE (d) == FUNCTION_DECL)
24336 {
24337 deleted_p = DECL_DELETED_FN (code_pattern);
24338 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24339 && DECL_INITIAL (code_pattern) != error_mark_node)
24340 || DECL_DEFAULTED_FN (code_pattern)
24341 || deleted_p);
24342 }
24343 else
24344 {
24345 deleted_p = false;
24346 if (DECL_CLASS_SCOPE_P (code_pattern))
24347 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
24348 || DECL_INLINE_VAR_P (code_pattern));
24349 else
24350 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24351 }
24352
24353 /* We may be in the middle of deferred access check. Disable it now. */
24354 push_deferring_access_checks (dk_no_deferred);
24355
24356 /* Unless an explicit instantiation directive has already determined
24357 the linkage of D, remember that a definition is available for
24358 this entity. */
24359 if (pattern_defined
24360 && !DECL_INTERFACE_KNOWN (d)
24361 && !DECL_NOT_REALLY_EXTERN (d))
24362 mark_definable (d);
24363
24364 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24365 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24366 input_location = DECL_SOURCE_LOCATION (d);
24367
24368 /* If D is a member of an explicitly instantiated class template,
24369 and no definition is available, treat it like an implicit
24370 instantiation. */
24371 if (!pattern_defined && expl_inst_class_mem_p
24372 && DECL_EXPLICIT_INSTANTIATION (d))
24373 {
24374 /* Leave linkage flags alone on instantiations with anonymous
24375 visibility. */
24376 if (TREE_PUBLIC (d))
24377 {
24378 DECL_NOT_REALLY_EXTERN (d) = 0;
24379 DECL_INTERFACE_KNOWN (d) = 0;
24380 }
24381 SET_DECL_IMPLICIT_INSTANTIATION (d);
24382 }
24383
24384 /* Defer all other templates, unless we have been explicitly
24385 forbidden from doing so. */
24386 if (/* If there is no definition, we cannot instantiate the
24387 template. */
24388 ! pattern_defined
24389 /* If it's OK to postpone instantiation, do so. */
24390 || defer_ok
24391 /* If this is a static data member that will be defined
24392 elsewhere, we don't want to instantiate the entire data
24393 member, but we do want to instantiate the initializer so that
24394 we can substitute that elsewhere. */
24395 || (external_p && VAR_P (d))
24396 /* Handle here a deleted function too, avoid generating
24397 its body (c++/61080). */
24398 || deleted_p)
24399 {
24400 /* The definition of the static data member is now required so
24401 we must substitute the initializer. */
24402 if (VAR_P (d)
24403 && !DECL_INITIAL (d)
24404 && DECL_INITIAL (code_pattern))
24405 {
24406 tree ns;
24407 tree init;
24408 bool const_init = false;
24409 bool enter_context = DECL_CLASS_SCOPE_P (d);
24410
24411 ns = decl_namespace_context (d);
24412 push_nested_namespace (ns);
24413 if (enter_context)
24414 push_nested_class (DECL_CONTEXT (d));
24415 init = tsubst_expr (DECL_INITIAL (code_pattern),
24416 args,
24417 tf_warning_or_error, NULL_TREE,
24418 /*integral_constant_expression_p=*/false);
24419 /* If instantiating the initializer involved instantiating this
24420 again, don't call cp_finish_decl twice. */
24421 if (!DECL_INITIAL (d))
24422 {
24423 /* Make sure the initializer is still constant, in case of
24424 circular dependency (template/instantiate6.C). */
24425 const_init
24426 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24427 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24428 /*asmspec_tree=*/NULL_TREE,
24429 LOOKUP_ONLYCONVERTING);
24430 }
24431 if (enter_context)
24432 pop_nested_class ();
24433 pop_nested_namespace (ns);
24434 }
24435
24436 /* We restore the source position here because it's used by
24437 add_pending_template. */
24438 input_location = saved_loc;
24439
24440 if (at_eof && !pattern_defined
24441 && DECL_EXPLICIT_INSTANTIATION (d)
24442 && DECL_NOT_REALLY_EXTERN (d))
24443 /* [temp.explicit]
24444
24445 The definition of a non-exported function template, a
24446 non-exported member function template, or a non-exported
24447 member function or static data member of a class template
24448 shall be present in every translation unit in which it is
24449 explicitly instantiated. */
24450 permerror (input_location, "explicit instantiation of %qD "
24451 "but no definition available", d);
24452
24453 /* If we're in unevaluated context, we just wanted to get the
24454 constant value; this isn't an odr use, so don't queue
24455 a full instantiation. */
24456 if (cp_unevaluated_operand != 0)
24457 goto out;
24458 /* ??? Historically, we have instantiated inline functions, even
24459 when marked as "extern template". */
24460 if (!(external_p && VAR_P (d)))
24461 add_pending_template (d);
24462 goto out;
24463 }
24464 /* Tell the repository that D is available in this translation unit
24465 -- and see if it is supposed to be instantiated here. */
24466 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24467 {
24468 /* In a PCH file, despite the fact that the repository hasn't
24469 requested instantiation in the PCH it is still possible that
24470 an instantiation will be required in a file that includes the
24471 PCH. */
24472 if (pch_file)
24473 add_pending_template (d);
24474 /* Instantiate inline functions so that the inliner can do its
24475 job, even though we'll not be emitting a copy of this
24476 function. */
24477 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24478 goto out;
24479 }
24480
24481 bool push_to_top, nested;
24482 tree fn_context;
24483 fn_context = decl_function_context (d);
24484 if (LAMBDA_FUNCTION_P (d))
24485 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24486 fn_context = NULL_TREE;
24487 nested = current_function_decl != NULL_TREE;
24488 push_to_top = !(nested && fn_context == current_function_decl);
24489
24490 vec<tree> omp_privatization_save;
24491 if (nested)
24492 save_omp_privatization_clauses (omp_privatization_save);
24493
24494 if (push_to_top)
24495 push_to_top_level ();
24496 else
24497 {
24498 gcc_assert (!processing_template_decl);
24499 push_function_context ();
24500 cp_unevaluated_operand = 0;
24501 c_inhibit_evaluation_warnings = 0;
24502 }
24503
24504 /* Mark D as instantiated so that recursive calls to
24505 instantiate_decl do not try to instantiate it again. */
24506 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24507
24508 /* Regenerate the declaration in case the template has been modified
24509 by a subsequent redeclaration. */
24510 regenerate_decl_from_template (d, td, args);
24511
24512 /* We already set the file and line above. Reset them now in case
24513 they changed as a result of calling regenerate_decl_from_template. */
24514 input_location = DECL_SOURCE_LOCATION (d);
24515
24516 if (VAR_P (d))
24517 {
24518 tree init;
24519 bool const_init = false;
24520
24521 /* Clear out DECL_RTL; whatever was there before may not be right
24522 since we've reset the type of the declaration. */
24523 SET_DECL_RTL (d, NULL);
24524 DECL_IN_AGGR_P (d) = 0;
24525
24526 /* The initializer is placed in DECL_INITIAL by
24527 regenerate_decl_from_template so we don't need to
24528 push/pop_access_scope again here. Pull it out so that
24529 cp_finish_decl can process it. */
24530 init = DECL_INITIAL (d);
24531 DECL_INITIAL (d) = NULL_TREE;
24532 DECL_INITIALIZED_P (d) = 0;
24533
24534 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24535 initializer. That function will defer actual emission until
24536 we have a chance to determine linkage. */
24537 DECL_EXTERNAL (d) = 0;
24538
24539 /* Enter the scope of D so that access-checking works correctly. */
24540 bool enter_context = DECL_CLASS_SCOPE_P (d);
24541 if (enter_context)
24542 push_nested_class (DECL_CONTEXT (d));
24543
24544 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24545 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24546
24547 if (enter_context)
24548 pop_nested_class ();
24549
24550 if (variable_template_p (gen_tmpl))
24551 note_variable_template_instantiation (d);
24552 }
24553 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24554 synthesize_method (d);
24555 else if (TREE_CODE (d) == FUNCTION_DECL)
24556 {
24557 /* Set up the list of local specializations. */
24558 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24559 tree block = NULL_TREE;
24560
24561 /* Set up context. */
24562 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24563 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24564 block = push_stmt_list ();
24565 else
24566 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24567
24568 /* Some typedefs referenced from within the template code need to be
24569 access checked at template instantiation time, i.e now. These
24570 types were added to the template at parsing time. Let's get those
24571 and perform the access checks then. */
24572 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24573 args);
24574
24575 /* Create substitution entries for the parameters. */
24576 register_parameter_specializations (code_pattern, d);
24577
24578 /* Substitute into the body of the function. */
24579 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24580 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24581 tf_warning_or_error, tmpl);
24582 else
24583 {
24584 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24585 tf_warning_or_error, tmpl,
24586 /*integral_constant_expression_p=*/false);
24587
24588 /* Set the current input_location to the end of the function
24589 so that finish_function knows where we are. */
24590 input_location
24591 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24592
24593 /* Remember if we saw an infinite loop in the template. */
24594 current_function_infinite_loop
24595 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24596 }
24597
24598 /* Finish the function. */
24599 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24600 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24601 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24602 else
24603 {
24604 d = finish_function (/*inline_p=*/false);
24605 expand_or_defer_fn (d);
24606 }
24607
24608 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24609 cp_check_omp_declare_reduction (d);
24610 }
24611
24612 /* We're not deferring instantiation any more. */
24613 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24614
24615 if (push_to_top)
24616 pop_from_top_level ();
24617 else
24618 pop_function_context ();
24619
24620 if (nested)
24621 restore_omp_privatization_clauses (omp_privatization_save);
24622
24623 out:
24624 pop_deferring_access_checks ();
24625 timevar_pop (TV_TEMPLATE_INST);
24626 pop_tinst_level ();
24627 input_location = saved_loc;
24628 cp_unevaluated_operand = saved_unevaluated_operand;
24629 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24630
24631 return d;
24632 }
24633
24634 /* Run through the list of templates that we wish we could
24635 instantiate, and instantiate any we can. RETRIES is the
24636 number of times we retry pending template instantiation. */
24637
24638 void
24639 instantiate_pending_templates (int retries)
24640 {
24641 int reconsider;
24642 location_t saved_loc = input_location;
24643
24644 /* Instantiating templates may trigger vtable generation. This in turn
24645 may require further template instantiations. We place a limit here
24646 to avoid infinite loop. */
24647 if (pending_templates && retries >= max_tinst_depth)
24648 {
24649 tree decl = pending_templates->tinst->maybe_get_node ();
24650
24651 fatal_error (input_location,
24652 "template instantiation depth exceeds maximum of %d"
24653 " instantiating %q+D, possibly from virtual table generation"
24654 " (use -ftemplate-depth= to increase the maximum)",
24655 max_tinst_depth, decl);
24656 if (TREE_CODE (decl) == FUNCTION_DECL)
24657 /* Pretend that we defined it. */
24658 DECL_INITIAL (decl) = error_mark_node;
24659 return;
24660 }
24661
24662 do
24663 {
24664 struct pending_template **t = &pending_templates;
24665 struct pending_template *last = NULL;
24666 reconsider = 0;
24667 while (*t)
24668 {
24669 tree instantiation = reopen_tinst_level ((*t)->tinst);
24670 bool complete = false;
24671
24672 if (TYPE_P (instantiation))
24673 {
24674 if (!COMPLETE_TYPE_P (instantiation))
24675 {
24676 instantiate_class_template (instantiation);
24677 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24678 for (tree fld = TYPE_FIELDS (instantiation);
24679 fld; fld = TREE_CHAIN (fld))
24680 if ((VAR_P (fld)
24681 || (TREE_CODE (fld) == FUNCTION_DECL
24682 && !DECL_ARTIFICIAL (fld)))
24683 && DECL_TEMPLATE_INSTANTIATION (fld))
24684 instantiate_decl (fld,
24685 /*defer_ok=*/false,
24686 /*expl_inst_class_mem_p=*/false);
24687
24688 if (COMPLETE_TYPE_P (instantiation))
24689 reconsider = 1;
24690 }
24691
24692 complete = COMPLETE_TYPE_P (instantiation);
24693 }
24694 else
24695 {
24696 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24697 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24698 {
24699 instantiation
24700 = instantiate_decl (instantiation,
24701 /*defer_ok=*/false,
24702 /*expl_inst_class_mem_p=*/false);
24703 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24704 reconsider = 1;
24705 }
24706
24707 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24708 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24709 }
24710
24711 if (complete)
24712 {
24713 /* If INSTANTIATION has been instantiated, then we don't
24714 need to consider it again in the future. */
24715 struct pending_template *drop = *t;
24716 *t = (*t)->next;
24717 set_refcount_ptr (drop->tinst);
24718 pending_template_freelist ().free (drop);
24719 }
24720 else
24721 {
24722 last = *t;
24723 t = &(*t)->next;
24724 }
24725 tinst_depth = 0;
24726 set_refcount_ptr (current_tinst_level);
24727 }
24728 last_pending_template = last;
24729 }
24730 while (reconsider);
24731
24732 input_location = saved_loc;
24733 }
24734
24735 /* Substitute ARGVEC into T, which is a list of initializers for
24736 either base class or a non-static data member. The TREE_PURPOSEs
24737 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24738 instantiate_decl. */
24739
24740 static tree
24741 tsubst_initializer_list (tree t, tree argvec)
24742 {
24743 tree inits = NULL_TREE;
24744 tree target_ctor = error_mark_node;
24745
24746 for (; t; t = TREE_CHAIN (t))
24747 {
24748 tree decl;
24749 tree init;
24750 tree expanded_bases = NULL_TREE;
24751 tree expanded_arguments = NULL_TREE;
24752 int i, len = 1;
24753
24754 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24755 {
24756 tree expr;
24757 tree arg;
24758
24759 /* Expand the base class expansion type into separate base
24760 classes. */
24761 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24762 tf_warning_or_error,
24763 NULL_TREE);
24764 if (expanded_bases == error_mark_node)
24765 continue;
24766
24767 /* We'll be building separate TREE_LISTs of arguments for
24768 each base. */
24769 len = TREE_VEC_LENGTH (expanded_bases);
24770 expanded_arguments = make_tree_vec (len);
24771 for (i = 0; i < len; i++)
24772 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24773
24774 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24775 expand each argument in the TREE_VALUE of t. */
24776 expr = make_node (EXPR_PACK_EXPANSION);
24777 PACK_EXPANSION_LOCAL_P (expr) = true;
24778 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24779 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24780
24781 if (TREE_VALUE (t) == void_type_node)
24782 /* VOID_TYPE_NODE is used to indicate
24783 value-initialization. */
24784 {
24785 for (i = 0; i < len; i++)
24786 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24787 }
24788 else
24789 {
24790 /* Substitute parameter packs into each argument in the
24791 TREE_LIST. */
24792 in_base_initializer = 1;
24793 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24794 {
24795 tree expanded_exprs;
24796
24797 /* Expand the argument. */
24798 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24799 expanded_exprs
24800 = tsubst_pack_expansion (expr, argvec,
24801 tf_warning_or_error,
24802 NULL_TREE);
24803 if (expanded_exprs == error_mark_node)
24804 continue;
24805
24806 /* Prepend each of the expanded expressions to the
24807 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24808 for (i = 0; i < len; i++)
24809 {
24810 TREE_VEC_ELT (expanded_arguments, i) =
24811 tree_cons (NULL_TREE,
24812 TREE_VEC_ELT (expanded_exprs, i),
24813 TREE_VEC_ELT (expanded_arguments, i));
24814 }
24815 }
24816 in_base_initializer = 0;
24817
24818 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24819 since we built them backwards. */
24820 for (i = 0; i < len; i++)
24821 {
24822 TREE_VEC_ELT (expanded_arguments, i) =
24823 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24824 }
24825 }
24826 }
24827
24828 for (i = 0; i < len; ++i)
24829 {
24830 if (expanded_bases)
24831 {
24832 decl = TREE_VEC_ELT (expanded_bases, i);
24833 decl = expand_member_init (decl);
24834 init = TREE_VEC_ELT (expanded_arguments, i);
24835 }
24836 else
24837 {
24838 tree tmp;
24839 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24840 tf_warning_or_error, NULL_TREE);
24841
24842 decl = expand_member_init (decl);
24843 if (decl && !DECL_P (decl))
24844 in_base_initializer = 1;
24845
24846 init = TREE_VALUE (t);
24847 tmp = init;
24848 if (init != void_type_node)
24849 init = tsubst_expr (init, argvec,
24850 tf_warning_or_error, NULL_TREE,
24851 /*integral_constant_expression_p=*/false);
24852 if (init == NULL_TREE && tmp != NULL_TREE)
24853 /* If we had an initializer but it instantiated to nothing,
24854 value-initialize the object. This will only occur when
24855 the initializer was a pack expansion where the parameter
24856 packs used in that expansion were of length zero. */
24857 init = void_type_node;
24858 in_base_initializer = 0;
24859 }
24860
24861 if (target_ctor != error_mark_node
24862 && init != error_mark_node)
24863 {
24864 error ("mem-initializer for %qD follows constructor delegation",
24865 decl);
24866 return inits;
24867 }
24868 /* Look for a target constructor. */
24869 if (init != error_mark_node
24870 && decl && CLASS_TYPE_P (decl)
24871 && same_type_p (decl, current_class_type))
24872 {
24873 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24874 if (inits)
24875 {
24876 error ("constructor delegation follows mem-initializer for %qD",
24877 TREE_PURPOSE (inits));
24878 continue;
24879 }
24880 target_ctor = init;
24881 }
24882
24883 if (decl)
24884 {
24885 init = build_tree_list (decl, init);
24886 TREE_CHAIN (init) = inits;
24887 inits = init;
24888 }
24889 }
24890 }
24891 return inits;
24892 }
24893
24894 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24895
24896 static void
24897 set_current_access_from_decl (tree decl)
24898 {
24899 if (TREE_PRIVATE (decl))
24900 current_access_specifier = access_private_node;
24901 else if (TREE_PROTECTED (decl))
24902 current_access_specifier = access_protected_node;
24903 else
24904 current_access_specifier = access_public_node;
24905 }
24906
24907 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24908 is the instantiation (which should have been created with
24909 start_enum) and ARGS are the template arguments to use. */
24910
24911 static void
24912 tsubst_enum (tree tag, tree newtag, tree args)
24913 {
24914 tree e;
24915
24916 if (SCOPED_ENUM_P (newtag))
24917 begin_scope (sk_scoped_enum, newtag);
24918
24919 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24920 {
24921 tree value;
24922 tree decl;
24923
24924 decl = TREE_VALUE (e);
24925 /* Note that in a template enum, the TREE_VALUE is the
24926 CONST_DECL, not the corresponding INTEGER_CST. */
24927 value = tsubst_expr (DECL_INITIAL (decl),
24928 args, tf_warning_or_error, NULL_TREE,
24929 /*integral_constant_expression_p=*/true);
24930
24931 /* Give this enumeration constant the correct access. */
24932 set_current_access_from_decl (decl);
24933
24934 /* Actually build the enumerator itself. Here we're assuming that
24935 enumerators can't have dependent attributes. */
24936 build_enumerator (DECL_NAME (decl), value, newtag,
24937 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24938 }
24939
24940 if (SCOPED_ENUM_P (newtag))
24941 finish_scope ();
24942
24943 finish_enum_value_list (newtag);
24944 finish_enum (newtag);
24945
24946 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24947 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24948 }
24949
24950 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24951 its type -- but without substituting the innermost set of template
24952 arguments. So, innermost set of template parameters will appear in
24953 the type. */
24954
24955 tree
24956 get_mostly_instantiated_function_type (tree decl)
24957 {
24958 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24959 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24960 }
24961
24962 /* Return truthvalue if we're processing a template different from
24963 the last one involved in diagnostics. */
24964 bool
24965 problematic_instantiation_changed (void)
24966 {
24967 return current_tinst_level != last_error_tinst_level;
24968 }
24969
24970 /* Remember current template involved in diagnostics. */
24971 void
24972 record_last_problematic_instantiation (void)
24973 {
24974 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24975 }
24976
24977 struct tinst_level *
24978 current_instantiation (void)
24979 {
24980 return current_tinst_level;
24981 }
24982
24983 /* Return TRUE if current_function_decl is being instantiated, false
24984 otherwise. */
24985
24986 bool
24987 instantiating_current_function_p (void)
24988 {
24989 return (current_instantiation ()
24990 && (current_instantiation ()->maybe_get_node ()
24991 == current_function_decl));
24992 }
24993
24994 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24995 type. Return false for ok, true for disallowed. Issue error and
24996 inform messages under control of COMPLAIN. */
24997
24998 static bool
24999 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25000 {
25001 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25002 return false;
25003 else if (TYPE_PTR_P (type))
25004 return false;
25005 else if (TYPE_REF_P (type)
25006 && !TYPE_REF_IS_RVALUE (type))
25007 return false;
25008 else if (TYPE_PTRMEM_P (type))
25009 return false;
25010 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25011 return false;
25012 else if (TREE_CODE (type) == TYPENAME_TYPE)
25013 return false;
25014 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25015 return false;
25016 else if (TREE_CODE (type) == NULLPTR_TYPE)
25017 return false;
25018 /* A bound template template parm could later be instantiated to have a valid
25019 nontype parm type via an alias template. */
25020 else if (cxx_dialect >= cxx11
25021 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25022 return false;
25023 else if (CLASS_TYPE_P (type))
25024 {
25025 if (cxx_dialect < cxx2a)
25026 {
25027 error ("non-type template parameters of class type only available "
25028 "with -std=c++2a or -std=gnu++2a");
25029 return true;
25030 }
25031 if (!complete_type_or_else (type, NULL_TREE))
25032 return true;
25033 if (!literal_type_p (type))
25034 {
25035 error ("%qT is not a valid type for a template non-type parameter "
25036 "because it is not literal", type);
25037 explain_non_literal_class (type);
25038 return true;
25039 }
25040 if (cp_has_mutable_p (type))
25041 {
25042 error ("%qT is not a valid type for a template non-type parameter "
25043 "because it has a mutable member", type);
25044 return true;
25045 }
25046 /* FIXME check op<=> and strong structural equality once spaceship is
25047 implemented. */
25048 return false;
25049 }
25050
25051 if (complain & tf_error)
25052 {
25053 if (type == error_mark_node)
25054 inform (input_location, "invalid template non-type parameter");
25055 else
25056 error ("%q#T is not a valid type for a template non-type parameter",
25057 type);
25058 }
25059 return true;
25060 }
25061
25062 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25063 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25064
25065 static bool
25066 dependent_type_p_r (tree type)
25067 {
25068 tree scope;
25069
25070 /* [temp.dep.type]
25071
25072 A type is dependent if it is:
25073
25074 -- a template parameter. Template template parameters are types
25075 for us (since TYPE_P holds true for them) so we handle
25076 them here. */
25077 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25078 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25079 return true;
25080 /* -- a qualified-id with a nested-name-specifier which contains a
25081 class-name that names a dependent type or whose unqualified-id
25082 names a dependent type. */
25083 if (TREE_CODE (type) == TYPENAME_TYPE)
25084 return true;
25085
25086 /* An alias template specialization can be dependent even if the
25087 resulting type is not. */
25088 if (dependent_alias_template_spec_p (type))
25089 return true;
25090
25091 /* -- a cv-qualified type where the cv-unqualified type is
25092 dependent.
25093 No code is necessary for this bullet; the code below handles
25094 cv-qualified types, and we don't want to strip aliases with
25095 TYPE_MAIN_VARIANT because of DR 1558. */
25096 /* -- a compound type constructed from any dependent type. */
25097 if (TYPE_PTRMEM_P (type))
25098 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25099 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25100 (type)));
25101 else if (INDIRECT_TYPE_P (type))
25102 return dependent_type_p (TREE_TYPE (type));
25103 else if (TREE_CODE (type) == FUNCTION_TYPE
25104 || TREE_CODE (type) == METHOD_TYPE)
25105 {
25106 tree arg_type;
25107
25108 if (dependent_type_p (TREE_TYPE (type)))
25109 return true;
25110 for (arg_type = TYPE_ARG_TYPES (type);
25111 arg_type;
25112 arg_type = TREE_CHAIN (arg_type))
25113 if (dependent_type_p (TREE_VALUE (arg_type)))
25114 return true;
25115 if (cxx_dialect >= cxx17)
25116 /* A value-dependent noexcept-specifier makes the type dependent. */
25117 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25118 if (tree noex = TREE_PURPOSE (spec))
25119 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25120 affect overload resolution and treating it as dependent breaks
25121 things. */
25122 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25123 && value_dependent_expression_p (noex))
25124 return true;
25125 return false;
25126 }
25127 /* -- an array type constructed from any dependent type or whose
25128 size is specified by a constant expression that is
25129 value-dependent.
25130
25131 We checked for type- and value-dependence of the bounds in
25132 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25133 if (TREE_CODE (type) == ARRAY_TYPE)
25134 {
25135 if (TYPE_DOMAIN (type)
25136 && dependent_type_p (TYPE_DOMAIN (type)))
25137 return true;
25138 return dependent_type_p (TREE_TYPE (type));
25139 }
25140
25141 /* -- a template-id in which either the template name is a template
25142 parameter ... */
25143 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25144 return true;
25145 /* ... or any of the template arguments is a dependent type or
25146 an expression that is type-dependent or value-dependent. */
25147 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25148 && (any_dependent_template_arguments_p
25149 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25150 return true;
25151
25152 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25153 dependent; if the argument of the `typeof' expression is not
25154 type-dependent, then it should already been have resolved. */
25155 if (TREE_CODE (type) == TYPEOF_TYPE
25156 || TREE_CODE (type) == DECLTYPE_TYPE
25157 || TREE_CODE (type) == UNDERLYING_TYPE)
25158 return true;
25159
25160 /* A template argument pack is dependent if any of its packed
25161 arguments are. */
25162 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25163 {
25164 tree args = ARGUMENT_PACK_ARGS (type);
25165 int i, len = TREE_VEC_LENGTH (args);
25166 for (i = 0; i < len; ++i)
25167 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25168 return true;
25169 }
25170
25171 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25172 be template parameters. */
25173 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25174 return true;
25175
25176 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25177 return true;
25178
25179 /* The standard does not specifically mention types that are local
25180 to template functions or local classes, but they should be
25181 considered dependent too. For example:
25182
25183 template <int I> void f() {
25184 enum E { a = I };
25185 S<sizeof (E)> s;
25186 }
25187
25188 The size of `E' cannot be known until the value of `I' has been
25189 determined. Therefore, `E' must be considered dependent. */
25190 scope = TYPE_CONTEXT (type);
25191 if (scope && TYPE_P (scope))
25192 return dependent_type_p (scope);
25193 /* Don't use type_dependent_expression_p here, as it can lead
25194 to infinite recursion trying to determine whether a lambda
25195 nested in a lambda is dependent (c++/47687). */
25196 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25197 && DECL_LANG_SPECIFIC (scope)
25198 && DECL_TEMPLATE_INFO (scope)
25199 && (any_dependent_template_arguments_p
25200 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25201 return true;
25202
25203 /* Other types are non-dependent. */
25204 return false;
25205 }
25206
25207 /* Returns TRUE if TYPE is dependent, in the sense of
25208 [temp.dep.type]. Note that a NULL type is considered dependent. */
25209
25210 bool
25211 dependent_type_p (tree type)
25212 {
25213 /* If there are no template parameters in scope, then there can't be
25214 any dependent types. */
25215 if (!processing_template_decl)
25216 {
25217 /* If we are not processing a template, then nobody should be
25218 providing us with a dependent type. */
25219 gcc_assert (type);
25220 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25221 return false;
25222 }
25223
25224 /* If the type is NULL, we have not computed a type for the entity
25225 in question; in that case, the type is dependent. */
25226 if (!type)
25227 return true;
25228
25229 /* Erroneous types can be considered non-dependent. */
25230 if (type == error_mark_node)
25231 return false;
25232
25233 /* Getting here with global_type_node means we improperly called this
25234 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25235 gcc_checking_assert (type != global_type_node);
25236
25237 /* If we have not already computed the appropriate value for TYPE,
25238 do so now. */
25239 if (!TYPE_DEPENDENT_P_VALID (type))
25240 {
25241 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25242 TYPE_DEPENDENT_P_VALID (type) = 1;
25243 }
25244
25245 return TYPE_DEPENDENT_P (type);
25246 }
25247
25248 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25249 lookup. In other words, a dependent type that is not the current
25250 instantiation. */
25251
25252 bool
25253 dependent_scope_p (tree scope)
25254 {
25255 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25256 && !currently_open_class (scope));
25257 }
25258
25259 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25260 an unknown base of 'this' (and is therefore instantiation-dependent). */
25261
25262 static bool
25263 unknown_base_ref_p (tree t)
25264 {
25265 if (!current_class_ptr)
25266 return false;
25267
25268 tree mem = TREE_OPERAND (t, 1);
25269 if (shared_member_p (mem))
25270 return false;
25271
25272 tree cur = current_nonlambda_class_type ();
25273 if (!any_dependent_bases_p (cur))
25274 return false;
25275
25276 tree ctx = TREE_OPERAND (t, 0);
25277 if (DERIVED_FROM_P (ctx, cur))
25278 return false;
25279
25280 return true;
25281 }
25282
25283 /* T is a SCOPE_REF; return whether we need to consider it
25284 instantiation-dependent so that we can check access at instantiation
25285 time even though we know which member it resolves to. */
25286
25287 static bool
25288 instantiation_dependent_scope_ref_p (tree t)
25289 {
25290 if (DECL_P (TREE_OPERAND (t, 1))
25291 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25292 && !unknown_base_ref_p (t)
25293 && accessible_in_template_p (TREE_OPERAND (t, 0),
25294 TREE_OPERAND (t, 1)))
25295 return false;
25296 else
25297 return true;
25298 }
25299
25300 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25301 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25302 expression. */
25303
25304 /* Note that this predicate is not appropriate for general expressions;
25305 only constant expressions (that satisfy potential_constant_expression)
25306 can be tested for value dependence. */
25307
25308 bool
25309 value_dependent_expression_p (tree expression)
25310 {
25311 if (!processing_template_decl || expression == NULL_TREE)
25312 return false;
25313
25314 /* A type-dependent expression is also value-dependent. */
25315 if (type_dependent_expression_p (expression))
25316 return true;
25317
25318 switch (TREE_CODE (expression))
25319 {
25320 case BASELINK:
25321 /* A dependent member function of the current instantiation. */
25322 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25323
25324 case FUNCTION_DECL:
25325 /* A dependent member function of the current instantiation. */
25326 if (DECL_CLASS_SCOPE_P (expression)
25327 && dependent_type_p (DECL_CONTEXT (expression)))
25328 return true;
25329 break;
25330
25331 case IDENTIFIER_NODE:
25332 /* A name that has not been looked up -- must be dependent. */
25333 return true;
25334
25335 case TEMPLATE_PARM_INDEX:
25336 /* A non-type template parm. */
25337 return true;
25338
25339 case CONST_DECL:
25340 /* A non-type template parm. */
25341 if (DECL_TEMPLATE_PARM_P (expression))
25342 return true;
25343 return value_dependent_expression_p (DECL_INITIAL (expression));
25344
25345 case VAR_DECL:
25346 /* A constant with literal type and is initialized
25347 with an expression that is value-dependent. */
25348 if (DECL_DEPENDENT_INIT_P (expression)
25349 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25350 || TYPE_REF_P (TREE_TYPE (expression)))
25351 return true;
25352 if (DECL_HAS_VALUE_EXPR_P (expression))
25353 {
25354 tree value_expr = DECL_VALUE_EXPR (expression);
25355 if (value_dependent_expression_p (value_expr))
25356 return true;
25357 }
25358 return false;
25359
25360 case DYNAMIC_CAST_EXPR:
25361 case STATIC_CAST_EXPR:
25362 case CONST_CAST_EXPR:
25363 case REINTERPRET_CAST_EXPR:
25364 case CAST_EXPR:
25365 case IMPLICIT_CONV_EXPR:
25366 /* These expressions are value-dependent if the type to which
25367 the cast occurs is dependent or the expression being casted
25368 is value-dependent. */
25369 {
25370 tree type = TREE_TYPE (expression);
25371
25372 if (dependent_type_p (type))
25373 return true;
25374
25375 /* A functional cast has a list of operands. */
25376 expression = TREE_OPERAND (expression, 0);
25377 if (!expression)
25378 {
25379 /* If there are no operands, it must be an expression such
25380 as "int()". This should not happen for aggregate types
25381 because it would form non-constant expressions. */
25382 gcc_assert (cxx_dialect >= cxx11
25383 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25384
25385 return false;
25386 }
25387
25388 if (TREE_CODE (expression) == TREE_LIST)
25389 return any_value_dependent_elements_p (expression);
25390
25391 return value_dependent_expression_p (expression);
25392 }
25393
25394 case SIZEOF_EXPR:
25395 if (SIZEOF_EXPR_TYPE_P (expression))
25396 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25397 /* FALLTHRU */
25398 case ALIGNOF_EXPR:
25399 case TYPEID_EXPR:
25400 /* A `sizeof' expression is value-dependent if the operand is
25401 type-dependent or is a pack expansion. */
25402 expression = TREE_OPERAND (expression, 0);
25403 if (PACK_EXPANSION_P (expression))
25404 return true;
25405 else if (TYPE_P (expression))
25406 return dependent_type_p (expression);
25407 return instantiation_dependent_uneval_expression_p (expression);
25408
25409 case AT_ENCODE_EXPR:
25410 /* An 'encode' expression is value-dependent if the operand is
25411 type-dependent. */
25412 expression = TREE_OPERAND (expression, 0);
25413 return dependent_type_p (expression);
25414
25415 case NOEXCEPT_EXPR:
25416 expression = TREE_OPERAND (expression, 0);
25417 return instantiation_dependent_uneval_expression_p (expression);
25418
25419 case SCOPE_REF:
25420 /* All instantiation-dependent expressions should also be considered
25421 value-dependent. */
25422 return instantiation_dependent_scope_ref_p (expression);
25423
25424 case COMPONENT_REF:
25425 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25426 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25427
25428 case NONTYPE_ARGUMENT_PACK:
25429 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25430 is value-dependent. */
25431 {
25432 tree values = ARGUMENT_PACK_ARGS (expression);
25433 int i, len = TREE_VEC_LENGTH (values);
25434
25435 for (i = 0; i < len; ++i)
25436 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25437 return true;
25438
25439 return false;
25440 }
25441
25442 case TRAIT_EXPR:
25443 {
25444 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25445
25446 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25447 return true;
25448
25449 if (!type2)
25450 return false;
25451
25452 if (TREE_CODE (type2) != TREE_LIST)
25453 return dependent_type_p (type2);
25454
25455 for (; type2; type2 = TREE_CHAIN (type2))
25456 if (dependent_type_p (TREE_VALUE (type2)))
25457 return true;
25458
25459 return false;
25460 }
25461
25462 case MODOP_EXPR:
25463 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25464 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25465
25466 case ARRAY_REF:
25467 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25468 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25469
25470 case ADDR_EXPR:
25471 {
25472 tree op = TREE_OPERAND (expression, 0);
25473 return (value_dependent_expression_p (op)
25474 || has_value_dependent_address (op));
25475 }
25476
25477 case REQUIRES_EXPR:
25478 /* Treat all requires-expressions as value-dependent so
25479 we don't try to fold them. */
25480 return true;
25481
25482 case TYPE_REQ:
25483 return dependent_type_p (TREE_OPERAND (expression, 0));
25484
25485 case CALL_EXPR:
25486 {
25487 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25488 return true;
25489 tree fn = get_callee_fndecl (expression);
25490 int i, nargs;
25491 nargs = call_expr_nargs (expression);
25492 for (i = 0; i < nargs; ++i)
25493 {
25494 tree op = CALL_EXPR_ARG (expression, i);
25495 /* In a call to a constexpr member function, look through the
25496 implicit ADDR_EXPR on the object argument so that it doesn't
25497 cause the call to be considered value-dependent. We also
25498 look through it in potential_constant_expression. */
25499 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25500 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25501 && TREE_CODE (op) == ADDR_EXPR)
25502 op = TREE_OPERAND (op, 0);
25503 if (value_dependent_expression_p (op))
25504 return true;
25505 }
25506 return false;
25507 }
25508
25509 case TEMPLATE_ID_EXPR:
25510 return variable_concept_p (TREE_OPERAND (expression, 0));
25511
25512 case CONSTRUCTOR:
25513 {
25514 unsigned ix;
25515 tree val;
25516 if (dependent_type_p (TREE_TYPE (expression)))
25517 return true;
25518 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25519 if (value_dependent_expression_p (val))
25520 return true;
25521 return false;
25522 }
25523
25524 case STMT_EXPR:
25525 /* Treat a GNU statement expression as dependent to avoid crashing
25526 under instantiate_non_dependent_expr; it can't be constant. */
25527 return true;
25528
25529 default:
25530 /* A constant expression is value-dependent if any subexpression is
25531 value-dependent. */
25532 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25533 {
25534 case tcc_reference:
25535 case tcc_unary:
25536 case tcc_comparison:
25537 case tcc_binary:
25538 case tcc_expression:
25539 case tcc_vl_exp:
25540 {
25541 int i, len = cp_tree_operand_length (expression);
25542
25543 for (i = 0; i < len; i++)
25544 {
25545 tree t = TREE_OPERAND (expression, i);
25546
25547 /* In some cases, some of the operands may be missing.
25548 (For example, in the case of PREDECREMENT_EXPR, the
25549 amount to increment by may be missing.) That doesn't
25550 make the expression dependent. */
25551 if (t && value_dependent_expression_p (t))
25552 return true;
25553 }
25554 }
25555 break;
25556 default:
25557 break;
25558 }
25559 break;
25560 }
25561
25562 /* The expression is not value-dependent. */
25563 return false;
25564 }
25565
25566 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25567 [temp.dep.expr]. Note that an expression with no type is
25568 considered dependent. Other parts of the compiler arrange for an
25569 expression with type-dependent subexpressions to have no type, so
25570 this function doesn't have to be fully recursive. */
25571
25572 bool
25573 type_dependent_expression_p (tree expression)
25574 {
25575 if (!processing_template_decl)
25576 return false;
25577
25578 if (expression == NULL_TREE || expression == error_mark_node)
25579 return false;
25580
25581 STRIP_ANY_LOCATION_WRAPPER (expression);
25582
25583 /* An unresolved name is always dependent. */
25584 if (identifier_p (expression)
25585 || TREE_CODE (expression) == USING_DECL
25586 || TREE_CODE (expression) == WILDCARD_DECL)
25587 return true;
25588
25589 /* A fold expression is type-dependent. */
25590 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25591 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25592 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25593 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25594 return true;
25595
25596 /* Some expression forms are never type-dependent. */
25597 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25598 || TREE_CODE (expression) == SIZEOF_EXPR
25599 || TREE_CODE (expression) == ALIGNOF_EXPR
25600 || TREE_CODE (expression) == AT_ENCODE_EXPR
25601 || TREE_CODE (expression) == NOEXCEPT_EXPR
25602 || TREE_CODE (expression) == TRAIT_EXPR
25603 || TREE_CODE (expression) == TYPEID_EXPR
25604 || TREE_CODE (expression) == DELETE_EXPR
25605 || TREE_CODE (expression) == VEC_DELETE_EXPR
25606 || TREE_CODE (expression) == THROW_EXPR
25607 || TREE_CODE (expression) == REQUIRES_EXPR)
25608 return false;
25609
25610 /* The types of these expressions depends only on the type to which
25611 the cast occurs. */
25612 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25613 || TREE_CODE (expression) == STATIC_CAST_EXPR
25614 || TREE_CODE (expression) == CONST_CAST_EXPR
25615 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25616 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25617 || TREE_CODE (expression) == CAST_EXPR)
25618 return dependent_type_p (TREE_TYPE (expression));
25619
25620 /* The types of these expressions depends only on the type created
25621 by the expression. */
25622 if (TREE_CODE (expression) == NEW_EXPR
25623 || TREE_CODE (expression) == VEC_NEW_EXPR)
25624 {
25625 /* For NEW_EXPR tree nodes created inside a template, either
25626 the object type itself or a TREE_LIST may appear as the
25627 operand 1. */
25628 tree type = TREE_OPERAND (expression, 1);
25629 if (TREE_CODE (type) == TREE_LIST)
25630 /* This is an array type. We need to check array dimensions
25631 as well. */
25632 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25633 || value_dependent_expression_p
25634 (TREE_OPERAND (TREE_VALUE (type), 1));
25635 else
25636 return dependent_type_p (type);
25637 }
25638
25639 if (TREE_CODE (expression) == SCOPE_REF)
25640 {
25641 tree scope = TREE_OPERAND (expression, 0);
25642 tree name = TREE_OPERAND (expression, 1);
25643
25644 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25645 contains an identifier associated by name lookup with one or more
25646 declarations declared with a dependent type, or...a
25647 nested-name-specifier or qualified-id that names a member of an
25648 unknown specialization. */
25649 return (type_dependent_expression_p (name)
25650 || dependent_scope_p (scope));
25651 }
25652
25653 if (TREE_CODE (expression) == TEMPLATE_DECL
25654 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25655 return uses_outer_template_parms (expression);
25656
25657 if (TREE_CODE (expression) == STMT_EXPR)
25658 expression = stmt_expr_value_expr (expression);
25659
25660 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25661 {
25662 tree elt;
25663 unsigned i;
25664
25665 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25666 {
25667 if (type_dependent_expression_p (elt))
25668 return true;
25669 }
25670 return false;
25671 }
25672
25673 /* A static data member of the current instantiation with incomplete
25674 array type is type-dependent, as the definition and specializations
25675 can have different bounds. */
25676 if (VAR_P (expression)
25677 && DECL_CLASS_SCOPE_P (expression)
25678 && dependent_type_p (DECL_CONTEXT (expression))
25679 && VAR_HAD_UNKNOWN_BOUND (expression))
25680 return true;
25681
25682 /* An array of unknown bound depending on a variadic parameter, eg:
25683
25684 template<typename... Args>
25685 void foo (Args... args)
25686 {
25687 int arr[] = { args... };
25688 }
25689
25690 template<int... vals>
25691 void bar ()
25692 {
25693 int arr[] = { vals... };
25694 }
25695
25696 If the array has no length and has an initializer, it must be that
25697 we couldn't determine its length in cp_complete_array_type because
25698 it is dependent. */
25699 if (VAR_P (expression)
25700 && TREE_TYPE (expression) != NULL_TREE
25701 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25702 && !TYPE_DOMAIN (TREE_TYPE (expression))
25703 && DECL_INITIAL (expression))
25704 return true;
25705
25706 /* A function or variable template-id is type-dependent if it has any
25707 dependent template arguments. */
25708 if (VAR_OR_FUNCTION_DECL_P (expression)
25709 && DECL_LANG_SPECIFIC (expression)
25710 && DECL_TEMPLATE_INFO (expression))
25711 {
25712 /* Consider the innermost template arguments, since those are the ones
25713 that come from the template-id; the template arguments for the
25714 enclosing class do not make it type-dependent unless they are used in
25715 the type of the decl. */
25716 if (instantiates_primary_template_p (expression)
25717 && (any_dependent_template_arguments_p
25718 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25719 return true;
25720 }
25721
25722 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25723 type-dependent. Checking this is important for functions with auto return
25724 type, which looks like a dependent type. */
25725 if (TREE_CODE (expression) == FUNCTION_DECL
25726 && !(DECL_CLASS_SCOPE_P (expression)
25727 && dependent_type_p (DECL_CONTEXT (expression)))
25728 && !(DECL_LANG_SPECIFIC (expression)
25729 && DECL_FRIEND_P (expression)
25730 && (!DECL_FRIEND_CONTEXT (expression)
25731 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25732 && !DECL_LOCAL_FUNCTION_P (expression))
25733 {
25734 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25735 || undeduced_auto_decl (expression));
25736 return false;
25737 }
25738
25739 /* Always dependent, on the number of arguments if nothing else. */
25740 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25741 return true;
25742
25743 if (TREE_TYPE (expression) == unknown_type_node)
25744 {
25745 if (TREE_CODE (expression) == ADDR_EXPR)
25746 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25747 if (TREE_CODE (expression) == COMPONENT_REF
25748 || TREE_CODE (expression) == OFFSET_REF)
25749 {
25750 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25751 return true;
25752 expression = TREE_OPERAND (expression, 1);
25753 if (identifier_p (expression))
25754 return false;
25755 }
25756 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25757 if (TREE_CODE (expression) == SCOPE_REF)
25758 return false;
25759
25760 if (BASELINK_P (expression))
25761 {
25762 if (BASELINK_OPTYPE (expression)
25763 && dependent_type_p (BASELINK_OPTYPE (expression)))
25764 return true;
25765 expression = BASELINK_FUNCTIONS (expression);
25766 }
25767
25768 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25769 {
25770 if (any_dependent_template_arguments_p
25771 (TREE_OPERAND (expression, 1)))
25772 return true;
25773 expression = TREE_OPERAND (expression, 0);
25774 if (identifier_p (expression))
25775 return true;
25776 }
25777
25778 gcc_assert (TREE_CODE (expression) == OVERLOAD
25779 || TREE_CODE (expression) == FUNCTION_DECL);
25780
25781 for (lkp_iterator iter (expression); iter; ++iter)
25782 if (type_dependent_expression_p (*iter))
25783 return true;
25784
25785 return false;
25786 }
25787
25788 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25789
25790 /* Dependent type attributes might not have made it from the decl to
25791 the type yet. */
25792 if (DECL_P (expression)
25793 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25794 return true;
25795
25796 return (dependent_type_p (TREE_TYPE (expression)));
25797 }
25798
25799 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25800 type-dependent if the expression refers to a member of the current
25801 instantiation and the type of the referenced member is dependent, or the
25802 class member access expression refers to a member of an unknown
25803 specialization.
25804
25805 This function returns true if the OBJECT in such a class member access
25806 expression is of an unknown specialization. */
25807
25808 bool
25809 type_dependent_object_expression_p (tree object)
25810 {
25811 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25812 dependent. */
25813 if (TREE_CODE (object) == IDENTIFIER_NODE)
25814 return true;
25815 tree scope = TREE_TYPE (object);
25816 return (!scope || dependent_scope_p (scope));
25817 }
25818
25819 /* walk_tree callback function for instantiation_dependent_expression_p,
25820 below. Returns non-zero if a dependent subexpression is found. */
25821
25822 static tree
25823 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25824 void * /*data*/)
25825 {
25826 if (TYPE_P (*tp))
25827 {
25828 /* We don't have to worry about decltype currently because decltype
25829 of an instantiation-dependent expr is a dependent type. This
25830 might change depending on the resolution of DR 1172. */
25831 *walk_subtrees = false;
25832 return NULL_TREE;
25833 }
25834 enum tree_code code = TREE_CODE (*tp);
25835 switch (code)
25836 {
25837 /* Don't treat an argument list as dependent just because it has no
25838 TREE_TYPE. */
25839 case TREE_LIST:
25840 case TREE_VEC:
25841 case NONTYPE_ARGUMENT_PACK:
25842 return NULL_TREE;
25843
25844 case TEMPLATE_PARM_INDEX:
25845 if (dependent_type_p (TREE_TYPE (*tp)))
25846 return *tp;
25847 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
25848 return *tp;
25849 /* We'll check value-dependence separately. */
25850 return NULL_TREE;
25851
25852 /* Handle expressions with type operands. */
25853 case SIZEOF_EXPR:
25854 case ALIGNOF_EXPR:
25855 case TYPEID_EXPR:
25856 case AT_ENCODE_EXPR:
25857 {
25858 tree op = TREE_OPERAND (*tp, 0);
25859 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25860 op = TREE_TYPE (op);
25861 if (TYPE_P (op))
25862 {
25863 if (dependent_type_p (op))
25864 return *tp;
25865 else
25866 {
25867 *walk_subtrees = false;
25868 return NULL_TREE;
25869 }
25870 }
25871 break;
25872 }
25873
25874 case COMPONENT_REF:
25875 if (identifier_p (TREE_OPERAND (*tp, 1)))
25876 /* In a template, finish_class_member_access_expr creates a
25877 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25878 type-dependent, so that we can check access control at
25879 instantiation time (PR 42277). See also Core issue 1273. */
25880 return *tp;
25881 break;
25882
25883 case SCOPE_REF:
25884 if (instantiation_dependent_scope_ref_p (*tp))
25885 return *tp;
25886 else
25887 break;
25888
25889 /* Treat statement-expressions as dependent. */
25890 case BIND_EXPR:
25891 return *tp;
25892
25893 /* Treat requires-expressions as dependent. */
25894 case REQUIRES_EXPR:
25895 return *tp;
25896
25897 case CALL_EXPR:
25898 /* Treat calls to function concepts as dependent. */
25899 if (function_concept_check_p (*tp))
25900 return *tp;
25901 break;
25902
25903 case TEMPLATE_ID_EXPR:
25904 /* And variable concepts. */
25905 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25906 return *tp;
25907 break;
25908
25909 case CONSTRUCTOR:
25910 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
25911 return *tp;
25912 break;
25913
25914 default:
25915 break;
25916 }
25917
25918 if (type_dependent_expression_p (*tp))
25919 return *tp;
25920 else
25921 return NULL_TREE;
25922 }
25923
25924 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25925 sense defined by the ABI:
25926
25927 "An expression is instantiation-dependent if it is type-dependent
25928 or value-dependent, or it has a subexpression that is type-dependent
25929 or value-dependent."
25930
25931 Except don't actually check value-dependence for unevaluated expressions,
25932 because in sizeof(i) we don't care about the value of i. Checking
25933 type-dependence will in turn check value-dependence of array bounds/template
25934 arguments as needed. */
25935
25936 bool
25937 instantiation_dependent_uneval_expression_p (tree expression)
25938 {
25939 tree result;
25940
25941 if (!processing_template_decl)
25942 return false;
25943
25944 if (expression == error_mark_node)
25945 return false;
25946
25947 result = cp_walk_tree_without_duplicates (&expression,
25948 instantiation_dependent_r, NULL);
25949 return result != NULL_TREE;
25950 }
25951
25952 /* As above, but also check value-dependence of the expression as a whole. */
25953
25954 bool
25955 instantiation_dependent_expression_p (tree expression)
25956 {
25957 return (instantiation_dependent_uneval_expression_p (expression)
25958 || value_dependent_expression_p (expression));
25959 }
25960
25961 /* Like type_dependent_expression_p, but it also works while not processing
25962 a template definition, i.e. during substitution or mangling. */
25963
25964 bool
25965 type_dependent_expression_p_push (tree expr)
25966 {
25967 bool b;
25968 ++processing_template_decl;
25969 b = type_dependent_expression_p (expr);
25970 --processing_template_decl;
25971 return b;
25972 }
25973
25974 /* Returns TRUE if ARGS contains a type-dependent expression. */
25975
25976 bool
25977 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25978 {
25979 unsigned int i;
25980 tree arg;
25981
25982 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25983 {
25984 if (type_dependent_expression_p (arg))
25985 return true;
25986 }
25987 return false;
25988 }
25989
25990 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25991 expressions) contains any type-dependent expressions. */
25992
25993 bool
25994 any_type_dependent_elements_p (const_tree list)
25995 {
25996 for (; list; list = TREE_CHAIN (list))
25997 if (type_dependent_expression_p (TREE_VALUE (list)))
25998 return true;
25999
26000 return false;
26001 }
26002
26003 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26004 expressions) contains any value-dependent expressions. */
26005
26006 bool
26007 any_value_dependent_elements_p (const_tree list)
26008 {
26009 for (; list; list = TREE_CHAIN (list))
26010 if (value_dependent_expression_p (TREE_VALUE (list)))
26011 return true;
26012
26013 return false;
26014 }
26015
26016 /* Returns TRUE if the ARG (a template argument) is dependent. */
26017
26018 bool
26019 dependent_template_arg_p (tree arg)
26020 {
26021 if (!processing_template_decl)
26022 return false;
26023
26024 /* Assume a template argument that was wrongly written by the user
26025 is dependent. This is consistent with what
26026 any_dependent_template_arguments_p [that calls this function]
26027 does. */
26028 if (!arg || arg == error_mark_node)
26029 return true;
26030
26031 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26032 arg = argument_pack_select_arg (arg);
26033
26034 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26035 return true;
26036 if (TREE_CODE (arg) == TEMPLATE_DECL)
26037 {
26038 if (DECL_TEMPLATE_PARM_P (arg))
26039 return true;
26040 /* A member template of a dependent class is not necessarily
26041 type-dependent, but it is a dependent template argument because it
26042 will be a member of an unknown specialization to that template. */
26043 tree scope = CP_DECL_CONTEXT (arg);
26044 return TYPE_P (scope) && dependent_type_p (scope);
26045 }
26046 else if (ARGUMENT_PACK_P (arg))
26047 {
26048 tree args = ARGUMENT_PACK_ARGS (arg);
26049 int i, len = TREE_VEC_LENGTH (args);
26050 for (i = 0; i < len; ++i)
26051 {
26052 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26053 return true;
26054 }
26055
26056 return false;
26057 }
26058 else if (TYPE_P (arg))
26059 return dependent_type_p (arg);
26060 else
26061 return (type_dependent_expression_p (arg)
26062 || value_dependent_expression_p (arg));
26063 }
26064
26065 /* Returns true if ARGS (a collection of template arguments) contains
26066 any types that require structural equality testing. */
26067
26068 bool
26069 any_template_arguments_need_structural_equality_p (tree args)
26070 {
26071 int i;
26072 int j;
26073
26074 if (!args)
26075 return false;
26076 if (args == error_mark_node)
26077 return true;
26078
26079 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26080 {
26081 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26082 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26083 {
26084 tree arg = TREE_VEC_ELT (level, j);
26085 tree packed_args = NULL_TREE;
26086 int k, len = 1;
26087
26088 if (ARGUMENT_PACK_P (arg))
26089 {
26090 /* Look inside the argument pack. */
26091 packed_args = ARGUMENT_PACK_ARGS (arg);
26092 len = TREE_VEC_LENGTH (packed_args);
26093 }
26094
26095 for (k = 0; k < len; ++k)
26096 {
26097 if (packed_args)
26098 arg = TREE_VEC_ELT (packed_args, k);
26099
26100 if (error_operand_p (arg))
26101 return true;
26102 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26103 continue;
26104 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26105 return true;
26106 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26107 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26108 return true;
26109 }
26110 }
26111 }
26112
26113 return false;
26114 }
26115
26116 /* Returns true if ARGS (a collection of template arguments) contains
26117 any dependent arguments. */
26118
26119 bool
26120 any_dependent_template_arguments_p (const_tree args)
26121 {
26122 int i;
26123 int j;
26124
26125 if (!args)
26126 return false;
26127 if (args == error_mark_node)
26128 return true;
26129
26130 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26131 {
26132 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26133 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26134 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26135 return true;
26136 }
26137
26138 return false;
26139 }
26140
26141 /* Returns true if ARGS contains any errors. */
26142
26143 bool
26144 any_erroneous_template_args_p (const_tree args)
26145 {
26146 int i;
26147 int j;
26148
26149 if (args == error_mark_node)
26150 return true;
26151
26152 if (args && TREE_CODE (args) != TREE_VEC)
26153 {
26154 if (tree ti = get_template_info (args))
26155 args = TI_ARGS (ti);
26156 else
26157 args = NULL_TREE;
26158 }
26159
26160 if (!args)
26161 return false;
26162
26163 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26164 {
26165 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26166 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26167 if (error_operand_p (TREE_VEC_ELT (level, j)))
26168 return true;
26169 }
26170
26171 return false;
26172 }
26173
26174 /* Returns TRUE if the template TMPL is type-dependent. */
26175
26176 bool
26177 dependent_template_p (tree tmpl)
26178 {
26179 if (TREE_CODE (tmpl) == OVERLOAD)
26180 {
26181 for (lkp_iterator iter (tmpl); iter; ++iter)
26182 if (dependent_template_p (*iter))
26183 return true;
26184 return false;
26185 }
26186
26187 /* Template template parameters are dependent. */
26188 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26189 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26190 return true;
26191 /* So are names that have not been looked up. */
26192 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26193 return true;
26194 return false;
26195 }
26196
26197 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26198
26199 bool
26200 dependent_template_id_p (tree tmpl, tree args)
26201 {
26202 return (dependent_template_p (tmpl)
26203 || any_dependent_template_arguments_p (args));
26204 }
26205
26206 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26207 are dependent. */
26208
26209 bool
26210 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26211 {
26212 int i;
26213
26214 if (!processing_template_decl)
26215 return false;
26216
26217 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26218 {
26219 tree decl = TREE_VEC_ELT (declv, i);
26220 tree init = TREE_VEC_ELT (initv, i);
26221 tree cond = TREE_VEC_ELT (condv, i);
26222 tree incr = TREE_VEC_ELT (incrv, i);
26223
26224 if (type_dependent_expression_p (decl)
26225 || TREE_CODE (decl) == SCOPE_REF)
26226 return true;
26227
26228 if (init && type_dependent_expression_p (init))
26229 return true;
26230
26231 if (cond == global_namespace)
26232 return true;
26233
26234 if (type_dependent_expression_p (cond))
26235 return true;
26236
26237 if (COMPARISON_CLASS_P (cond)
26238 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26239 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26240 return true;
26241
26242 if (TREE_CODE (incr) == MODOP_EXPR)
26243 {
26244 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26245 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26246 return true;
26247 }
26248 else if (type_dependent_expression_p (incr))
26249 return true;
26250 else if (TREE_CODE (incr) == MODIFY_EXPR)
26251 {
26252 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26253 return true;
26254 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26255 {
26256 tree t = TREE_OPERAND (incr, 1);
26257 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26258 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26259 return true;
26260
26261 /* If this loop has a class iterator with != comparison
26262 with increment other than i++/++i/i--/--i, make sure the
26263 increment is constant. */
26264 if (CLASS_TYPE_P (TREE_TYPE (decl))
26265 && TREE_CODE (cond) == NE_EXPR)
26266 {
26267 if (TREE_OPERAND (t, 0) == decl)
26268 t = TREE_OPERAND (t, 1);
26269 else
26270 t = TREE_OPERAND (t, 0);
26271 if (TREE_CODE (t) != INTEGER_CST)
26272 return true;
26273 }
26274 }
26275 }
26276 }
26277
26278 return false;
26279 }
26280
26281 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26282 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26283 no such TYPE can be found. Note that this function peers inside
26284 uninstantiated templates and therefore should be used only in
26285 extremely limited situations. ONLY_CURRENT_P restricts this
26286 peering to the currently open classes hierarchy (which is required
26287 when comparing types). */
26288
26289 tree
26290 resolve_typename_type (tree type, bool only_current_p)
26291 {
26292 tree scope;
26293 tree name;
26294 tree decl;
26295 int quals;
26296 tree pushed_scope;
26297 tree result;
26298
26299 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26300
26301 scope = TYPE_CONTEXT (type);
26302 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26303 gcc_checking_assert (uses_template_parms (scope));
26304
26305 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26306 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26307 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26308 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26309 identifier of the TYPENAME_TYPE anymore.
26310 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26311 TYPENAME_TYPE instead, we avoid messing up with a possible
26312 typedef variant case. */
26313 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26314
26315 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26316 it first before we can figure out what NAME refers to. */
26317 if (TREE_CODE (scope) == TYPENAME_TYPE)
26318 {
26319 if (TYPENAME_IS_RESOLVING_P (scope))
26320 /* Given a class template A with a dependent base with nested type C,
26321 typedef typename A::C::C C will land us here, as trying to resolve
26322 the initial A::C leads to the local C typedef, which leads back to
26323 A::C::C. So we break the recursion now. */
26324 return type;
26325 else
26326 scope = resolve_typename_type (scope, only_current_p);
26327 }
26328 /* If we don't know what SCOPE refers to, then we cannot resolve the
26329 TYPENAME_TYPE. */
26330 if (!CLASS_TYPE_P (scope))
26331 return type;
26332 /* If this is a typedef, we don't want to look inside (c++/11987). */
26333 if (typedef_variant_p (type))
26334 return type;
26335 /* If SCOPE isn't the template itself, it will not have a valid
26336 TYPE_FIELDS list. */
26337 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26338 /* scope is either the template itself or a compatible instantiation
26339 like X<T>, so look up the name in the original template. */
26340 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26341 /* If scope has no fields, it can't be a current instantiation. Check this
26342 before currently_open_class to avoid infinite recursion (71515). */
26343 if (!TYPE_FIELDS (scope))
26344 return type;
26345 /* If the SCOPE is not the current instantiation, there's no reason
26346 to look inside it. */
26347 if (only_current_p && !currently_open_class (scope))
26348 return type;
26349 /* Enter the SCOPE so that name lookup will be resolved as if we
26350 were in the class definition. In particular, SCOPE will no
26351 longer be considered a dependent type. */
26352 pushed_scope = push_scope (scope);
26353 /* Look up the declaration. */
26354 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26355 tf_warning_or_error);
26356
26357 result = NULL_TREE;
26358
26359 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26360 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26361 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26362 if (!decl)
26363 /*nop*/;
26364 else if (identifier_p (fullname)
26365 && TREE_CODE (decl) == TYPE_DECL)
26366 {
26367 result = TREE_TYPE (decl);
26368 if (result == error_mark_node)
26369 result = NULL_TREE;
26370 }
26371 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26372 && DECL_CLASS_TEMPLATE_P (decl))
26373 {
26374 /* Obtain the template and the arguments. */
26375 tree tmpl = TREE_OPERAND (fullname, 0);
26376 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26377 {
26378 /* We get here with a plain identifier because a previous tentative
26379 parse of the nested-name-specifier as part of a ptr-operator saw
26380 ::template X<A>. The use of ::template is necessary in a
26381 ptr-operator, but wrong in a declarator-id.
26382
26383 [temp.names]: In a qualified-id of a declarator-id, the keyword
26384 template shall not appear at the top level. */
26385 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26386 "keyword %<template%> not allowed in declarator-id");
26387 tmpl = decl;
26388 }
26389 tree args = TREE_OPERAND (fullname, 1);
26390 /* Instantiate the template. */
26391 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26392 /*entering_scope=*/true,
26393 tf_error | tf_user);
26394 if (result == error_mark_node)
26395 result = NULL_TREE;
26396 }
26397
26398 /* Leave the SCOPE. */
26399 if (pushed_scope)
26400 pop_scope (pushed_scope);
26401
26402 /* If we failed to resolve it, return the original typename. */
26403 if (!result)
26404 return type;
26405
26406 /* If lookup found a typename type, resolve that too. */
26407 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26408 {
26409 /* Ill-formed programs can cause infinite recursion here, so we
26410 must catch that. */
26411 TYPENAME_IS_RESOLVING_P (result) = 1;
26412 result = resolve_typename_type (result, only_current_p);
26413 TYPENAME_IS_RESOLVING_P (result) = 0;
26414 }
26415
26416 /* Qualify the resulting type. */
26417 quals = cp_type_quals (type);
26418 if (quals)
26419 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26420
26421 return result;
26422 }
26423
26424 /* EXPR is an expression which is not type-dependent. Return a proxy
26425 for EXPR that can be used to compute the types of larger
26426 expressions containing EXPR. */
26427
26428 tree
26429 build_non_dependent_expr (tree expr)
26430 {
26431 tree orig_expr = expr;
26432 tree inner_expr;
26433
26434 /* When checking, try to get a constant value for all non-dependent
26435 expressions in order to expose bugs in *_dependent_expression_p
26436 and constexpr. This can affect code generation, see PR70704, so
26437 only do this for -fchecking=2. */
26438 if (flag_checking > 1
26439 && cxx_dialect >= cxx11
26440 /* Don't do this during nsdmi parsing as it can lead to
26441 unexpected recursive instantiations. */
26442 && !parsing_nsdmi ()
26443 /* Don't do this during concept expansion either and for
26444 the same reason. */
26445 && !expanding_concept ())
26446 fold_non_dependent_expr (expr, tf_none);
26447
26448 STRIP_ANY_LOCATION_WRAPPER (expr);
26449
26450 /* Preserve OVERLOADs; the functions must be available to resolve
26451 types. */
26452 inner_expr = expr;
26453 if (TREE_CODE (inner_expr) == STMT_EXPR)
26454 inner_expr = stmt_expr_value_expr (inner_expr);
26455 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26456 inner_expr = TREE_OPERAND (inner_expr, 0);
26457 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26458 inner_expr = TREE_OPERAND (inner_expr, 1);
26459 if (is_overloaded_fn (inner_expr)
26460 || TREE_CODE (inner_expr) == OFFSET_REF)
26461 return orig_expr;
26462 /* There is no need to return a proxy for a variable or enumerator. */
26463 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26464 return orig_expr;
26465 /* Preserve string constants; conversions from string constants to
26466 "char *" are allowed, even though normally a "const char *"
26467 cannot be used to initialize a "char *". */
26468 if (TREE_CODE (expr) == STRING_CST)
26469 return orig_expr;
26470 /* Preserve void and arithmetic constants, as an optimization -- there is no
26471 reason to create a new node. */
26472 if (TREE_CODE (expr) == VOID_CST
26473 || TREE_CODE (expr) == INTEGER_CST
26474 || TREE_CODE (expr) == REAL_CST)
26475 return orig_expr;
26476 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26477 There is at least one place where we want to know that a
26478 particular expression is a throw-expression: when checking a ?:
26479 expression, there are special rules if the second or third
26480 argument is a throw-expression. */
26481 if (TREE_CODE (expr) == THROW_EXPR)
26482 return orig_expr;
26483
26484 /* Don't wrap an initializer list, we need to be able to look inside. */
26485 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26486 return orig_expr;
26487
26488 /* Don't wrap a dummy object, we need to be able to test for it. */
26489 if (is_dummy_object (expr))
26490 return orig_expr;
26491
26492 if (TREE_CODE (expr) == COND_EXPR)
26493 return build3 (COND_EXPR,
26494 TREE_TYPE (expr),
26495 TREE_OPERAND (expr, 0),
26496 (TREE_OPERAND (expr, 1)
26497 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26498 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26499 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26500 if (TREE_CODE (expr) == COMPOUND_EXPR
26501 && !COMPOUND_EXPR_OVERLOADED (expr))
26502 return build2 (COMPOUND_EXPR,
26503 TREE_TYPE (expr),
26504 TREE_OPERAND (expr, 0),
26505 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26506
26507 /* If the type is unknown, it can't really be non-dependent */
26508 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26509
26510 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26511 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26512 TREE_TYPE (expr), expr);
26513 }
26514
26515 /* ARGS is a vector of expressions as arguments to a function call.
26516 Replace the arguments with equivalent non-dependent expressions.
26517 This modifies ARGS in place. */
26518
26519 void
26520 make_args_non_dependent (vec<tree, va_gc> *args)
26521 {
26522 unsigned int ix;
26523 tree arg;
26524
26525 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26526 {
26527 tree newarg = build_non_dependent_expr (arg);
26528 if (newarg != arg)
26529 (*args)[ix] = newarg;
26530 }
26531 }
26532
26533 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26534 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26535 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26536
26537 static tree
26538 make_auto_1 (tree name, bool set_canonical)
26539 {
26540 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26541 TYPE_NAME (au) = build_decl (input_location,
26542 TYPE_DECL, name, au);
26543 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26544 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26545 (0, processing_template_decl + 1, processing_template_decl + 1,
26546 TYPE_NAME (au), NULL_TREE);
26547 if (set_canonical)
26548 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26549 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26550 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26551
26552 return au;
26553 }
26554
26555 tree
26556 make_decltype_auto (void)
26557 {
26558 return make_auto_1 (decltype_auto_identifier, true);
26559 }
26560
26561 tree
26562 make_auto (void)
26563 {
26564 return make_auto_1 (auto_identifier, true);
26565 }
26566
26567 /* Return a C++17 deduction placeholder for class template TMPL. */
26568
26569 tree
26570 make_template_placeholder (tree tmpl)
26571 {
26572 tree t = make_auto_1 (auto_identifier, true);
26573 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26574 return t;
26575 }
26576
26577 /* True iff T is a C++17 class template deduction placeholder. */
26578
26579 bool
26580 template_placeholder_p (tree t)
26581 {
26582 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26583 }
26584
26585 /* Make a "constrained auto" type-specifier. This is an
26586 auto type with constraints that must be associated after
26587 deduction. The constraint is formed from the given
26588 CONC and its optional sequence of arguments, which are
26589 non-null if written as partial-concept-id. */
26590
26591 tree
26592 make_constrained_auto (tree con, tree args)
26593 {
26594 tree type = make_auto_1 (auto_identifier, false);
26595
26596 /* Build the constraint. */
26597 tree tmpl = DECL_TI_TEMPLATE (con);
26598 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26599 expr = build_concept_check (expr, type, args);
26600
26601 tree constr = normalize_expression (expr);
26602 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26603
26604 /* Our canonical type depends on the constraint. */
26605 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26606
26607 /* Attach the constraint to the type declaration. */
26608 tree decl = TYPE_NAME (type);
26609 return decl;
26610 }
26611
26612 /* Given type ARG, return std::initializer_list<ARG>. */
26613
26614 static tree
26615 listify (tree arg)
26616 {
26617 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26618
26619 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26620 {
26621 gcc_rich_location richloc (input_location);
26622 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26623 error_at (&richloc,
26624 "deducing from brace-enclosed initializer list"
26625 " requires %<#include <initializer_list>%>");
26626
26627 return error_mark_node;
26628 }
26629 tree argvec = make_tree_vec (1);
26630 TREE_VEC_ELT (argvec, 0) = arg;
26631
26632 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26633 NULL_TREE, 0, tf_warning_or_error);
26634 }
26635
26636 /* Replace auto in TYPE with std::initializer_list<auto>. */
26637
26638 static tree
26639 listify_autos (tree type, tree auto_node)
26640 {
26641 tree init_auto = listify (auto_node);
26642 tree argvec = make_tree_vec (1);
26643 TREE_VEC_ELT (argvec, 0) = init_auto;
26644 if (processing_template_decl)
26645 argvec = add_to_template_args (current_template_args (), argvec);
26646 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26647 }
26648
26649 /* Hash traits for hashing possibly constrained 'auto'
26650 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26651
26652 struct auto_hash : default_hash_traits<tree>
26653 {
26654 static inline hashval_t hash (tree);
26655 static inline bool equal (tree, tree);
26656 };
26657
26658 /* Hash the 'auto' T. */
26659
26660 inline hashval_t
26661 auto_hash::hash (tree t)
26662 {
26663 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26664 /* Matching constrained-type-specifiers denote the same template
26665 parameter, so hash the constraint. */
26666 return hash_placeholder_constraint (c);
26667 else
26668 /* But unconstrained autos are all separate, so just hash the pointer. */
26669 return iterative_hash_object (t, 0);
26670 }
26671
26672 /* Compare two 'auto's. */
26673
26674 inline bool
26675 auto_hash::equal (tree t1, tree t2)
26676 {
26677 if (t1 == t2)
26678 return true;
26679
26680 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26681 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26682
26683 /* Two unconstrained autos are distinct. */
26684 if (!c1 || !c2)
26685 return false;
26686
26687 return equivalent_placeholder_constraints (c1, c2);
26688 }
26689
26690 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26691 constrained) auto, add it to the vector. */
26692
26693 static int
26694 extract_autos_r (tree t, void *data)
26695 {
26696 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26697 if (is_auto (t))
26698 {
26699 /* All the autos were built with index 0; fix that up now. */
26700 tree *p = hash.find_slot (t, INSERT);
26701 unsigned idx;
26702 if (*p)
26703 /* If this is a repeated constrained-type-specifier, use the index we
26704 chose before. */
26705 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26706 else
26707 {
26708 /* Otherwise this is new, so use the current count. */
26709 *p = t;
26710 idx = hash.elements () - 1;
26711 }
26712 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26713 }
26714
26715 /* Always keep walking. */
26716 return 0;
26717 }
26718
26719 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26720 says they can appear anywhere in the type. */
26721
26722 static tree
26723 extract_autos (tree type)
26724 {
26725 hash_set<tree> visited;
26726 hash_table<auto_hash> hash (2);
26727
26728 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26729
26730 tree tree_vec = make_tree_vec (hash.elements());
26731 for (hash_table<auto_hash>::iterator iter = hash.begin();
26732 iter != hash.end(); ++iter)
26733 {
26734 tree elt = *iter;
26735 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26736 TREE_VEC_ELT (tree_vec, i)
26737 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26738 }
26739
26740 return tree_vec;
26741 }
26742
26743 /* The stem for deduction guide names. */
26744 const char *const dguide_base = "__dguide_";
26745
26746 /* Return the name for a deduction guide for class template TMPL. */
26747
26748 tree
26749 dguide_name (tree tmpl)
26750 {
26751 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26752 tree tname = TYPE_IDENTIFIER (type);
26753 char *buf = (char *) alloca (1 + strlen (dguide_base)
26754 + IDENTIFIER_LENGTH (tname));
26755 memcpy (buf, dguide_base, strlen (dguide_base));
26756 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26757 IDENTIFIER_LENGTH (tname) + 1);
26758 tree dname = get_identifier (buf);
26759 TREE_TYPE (dname) = type;
26760 return dname;
26761 }
26762
26763 /* True if NAME is the name of a deduction guide. */
26764
26765 bool
26766 dguide_name_p (tree name)
26767 {
26768 return (TREE_CODE (name) == IDENTIFIER_NODE
26769 && TREE_TYPE (name)
26770 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26771 strlen (dguide_base)));
26772 }
26773
26774 /* True if FN is a deduction guide. */
26775
26776 bool
26777 deduction_guide_p (const_tree fn)
26778 {
26779 if (DECL_P (fn))
26780 if (tree name = DECL_NAME (fn))
26781 return dguide_name_p (name);
26782 return false;
26783 }
26784
26785 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26786
26787 bool
26788 copy_guide_p (const_tree fn)
26789 {
26790 gcc_assert (deduction_guide_p (fn));
26791 if (!DECL_ARTIFICIAL (fn))
26792 return false;
26793 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26794 return (TREE_CHAIN (parms) == void_list_node
26795 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26796 }
26797
26798 /* True if FN is a guide generated from a constructor template. */
26799
26800 bool
26801 template_guide_p (const_tree fn)
26802 {
26803 gcc_assert (deduction_guide_p (fn));
26804 if (!DECL_ARTIFICIAL (fn))
26805 return false;
26806 tree tmpl = DECL_TI_TEMPLATE (fn);
26807 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26808 return PRIMARY_TEMPLATE_P (org);
26809 return false;
26810 }
26811
26812 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26813 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26814 template parameter types. Note that the handling of template template
26815 parameters relies on current_template_parms being set appropriately for the
26816 new template. */
26817
26818 static tree
26819 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26820 tree tsubst_args, tsubst_flags_t complain)
26821 {
26822 if (olddecl == error_mark_node)
26823 return error_mark_node;
26824
26825 tree oldidx = get_template_parm_index (olddecl);
26826
26827 tree newtype;
26828 if (TREE_CODE (olddecl) == TYPE_DECL
26829 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26830 {
26831 tree oldtype = TREE_TYPE (olddecl);
26832 newtype = cxx_make_type (TREE_CODE (oldtype));
26833 TYPE_MAIN_VARIANT (newtype) = newtype;
26834 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26835 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26836 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26837 }
26838 else
26839 {
26840 newtype = TREE_TYPE (olddecl);
26841 if (type_uses_auto (newtype))
26842 {
26843 // Substitute once to fix references to other template parameters.
26844 newtype = tsubst (newtype, tsubst_args,
26845 complain|tf_partial, NULL_TREE);
26846 // Now substitute again to reduce the level of the auto.
26847 newtype = tsubst (newtype, current_template_args (),
26848 complain, NULL_TREE);
26849 }
26850 else
26851 newtype = tsubst (newtype, tsubst_args,
26852 complain, NULL_TREE);
26853 }
26854
26855 tree newdecl
26856 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26857 DECL_NAME (olddecl), newtype);
26858 SET_DECL_TEMPLATE_PARM_P (newdecl);
26859
26860 tree newidx;
26861 if (TREE_CODE (olddecl) == TYPE_DECL
26862 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26863 {
26864 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26865 = build_template_parm_index (index, level, level,
26866 newdecl, newtype);
26867 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26868 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26869 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26870 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26871
26872 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26873 {
26874 DECL_TEMPLATE_RESULT (newdecl)
26875 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26876 DECL_NAME (olddecl), newtype);
26877 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26878 // First create a copy (ttargs) of tsubst_args with an
26879 // additional level for the template template parameter's own
26880 // template parameters (ttparms).
26881 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26882 (DECL_TEMPLATE_PARMS (olddecl)));
26883 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26884 tree ttargs = make_tree_vec (depth + 1);
26885 for (int i = 0; i < depth; ++i)
26886 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26887 TREE_VEC_ELT (ttargs, depth)
26888 = template_parms_level_to_args (ttparms);
26889 // Substitute ttargs into ttparms to fix references to
26890 // other template parameters.
26891 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26892 complain|tf_partial);
26893 // Now substitute again with args based on tparms, to reduce
26894 // the level of the ttparms.
26895 ttargs = current_template_args ();
26896 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26897 complain);
26898 // Finally, tack the adjusted parms onto tparms.
26899 ttparms = tree_cons (size_int (depth), ttparms,
26900 current_template_parms);
26901 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26902 }
26903 }
26904 else
26905 {
26906 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26907 tree newconst
26908 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26909 TREE_CODE (oldconst),
26910 DECL_NAME (oldconst), newtype);
26911 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26912 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26913 SET_DECL_TEMPLATE_PARM_P (newconst);
26914 newidx = build_template_parm_index (index, level, level,
26915 newconst, newtype);
26916 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26917 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26918 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26919 }
26920
26921 return newdecl;
26922 }
26923
26924 /* Returns a C++17 class deduction guide template based on the constructor
26925 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26926 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26927
26928 static tree
26929 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26930 {
26931 tree type, tparms, targs, fparms, fargs, ci;
26932 bool memtmpl = false;
26933 bool explicit_p;
26934 location_t loc;
26935 tree fn_tmpl = NULL_TREE;
26936
26937 if (TYPE_P (ctor))
26938 {
26939 type = ctor;
26940 bool copy_p = TYPE_REF_P (type);
26941 if (copy_p)
26942 {
26943 type = TREE_TYPE (type);
26944 fparms = tree_cons (NULL_TREE, type, void_list_node);
26945 }
26946 else
26947 fparms = void_list_node;
26948
26949 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26950 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26951 targs = CLASSTYPE_TI_ARGS (type);
26952 ci = NULL_TREE;
26953 fargs = NULL_TREE;
26954 loc = DECL_SOURCE_LOCATION (ctmpl);
26955 explicit_p = false;
26956 }
26957 else
26958 {
26959 ++processing_template_decl;
26960 bool ok = true;
26961
26962 fn_tmpl
26963 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26964 : DECL_TI_TEMPLATE (ctor));
26965 if (outer_args)
26966 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26967 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26968
26969 type = DECL_CONTEXT (ctor);
26970
26971 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26972 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26973 fully specialized args for the enclosing class. Strip those off, as
26974 the deduction guide won't have those template parameters. */
26975 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26976 TMPL_PARMS_DEPTH (tparms));
26977 /* Discard the 'this' parameter. */
26978 fparms = FUNCTION_ARG_CHAIN (ctor);
26979 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26980 ci = get_constraints (ctor);
26981 loc = DECL_SOURCE_LOCATION (ctor);
26982 explicit_p = DECL_NONCONVERTING_P (ctor);
26983
26984 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26985 {
26986 memtmpl = true;
26987
26988 /* For a member template constructor, we need to flatten the two
26989 template parameter lists into one, and then adjust the function
26990 signature accordingly. This gets...complicated. */
26991 tree save_parms = current_template_parms;
26992
26993 /* For a member template we should have two levels of parms/args, one
26994 for the class and one for the constructor. We stripped
26995 specialized args for further enclosing classes above. */
26996 const int depth = 2;
26997 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26998
26999 /* Template args for translating references to the two-level template
27000 parameters into references to the one-level template parameters we
27001 are creating. */
27002 tree tsubst_args = copy_node (targs);
27003 TMPL_ARGS_LEVEL (tsubst_args, depth)
27004 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27005
27006 /* Template parms for the constructor template. */
27007 tree ftparms = TREE_VALUE (tparms);
27008 unsigned flen = TREE_VEC_LENGTH (ftparms);
27009 /* Template parms for the class template. */
27010 tparms = TREE_CHAIN (tparms);
27011 tree ctparms = TREE_VALUE (tparms);
27012 unsigned clen = TREE_VEC_LENGTH (ctparms);
27013 /* Template parms for the deduction guide start as a copy of the
27014 template parms for the class. We set current_template_parms for
27015 lookup_template_class_1. */
27016 current_template_parms = tparms = copy_node (tparms);
27017 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27018 for (unsigned i = 0; i < clen; ++i)
27019 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27020
27021 /* Now we need to rewrite the constructor parms to append them to the
27022 class parms. */
27023 for (unsigned i = 0; i < flen; ++i)
27024 {
27025 unsigned index = i + clen;
27026 unsigned level = 1;
27027 tree oldelt = TREE_VEC_ELT (ftparms, i);
27028 tree olddecl = TREE_VALUE (oldelt);
27029 tree newdecl = rewrite_template_parm (olddecl, index, level,
27030 tsubst_args, complain);
27031 if (newdecl == error_mark_node)
27032 ok = false;
27033 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27034 tsubst_args, complain, ctor);
27035 tree list = build_tree_list (newdef, newdecl);
27036 TEMPLATE_PARM_CONSTRAINTS (list)
27037 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27038 tsubst_args, complain, ctor);
27039 TREE_VEC_ELT (new_vec, index) = list;
27040 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27041 }
27042
27043 /* Now we have a final set of template parms to substitute into the
27044 function signature. */
27045 targs = template_parms_to_args (tparms);
27046 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27047 complain, ctor);
27048 if (fparms == error_mark_node)
27049 ok = false;
27050 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27051 if (ci)
27052 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27053
27054 current_template_parms = save_parms;
27055 }
27056
27057 --processing_template_decl;
27058 if (!ok)
27059 return error_mark_node;
27060 }
27061
27062 if (!memtmpl)
27063 {
27064 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27065 tparms = copy_node (tparms);
27066 INNERMOST_TEMPLATE_PARMS (tparms)
27067 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27068 }
27069
27070 tree fntype = build_function_type (type, fparms);
27071 tree ded_fn = build_lang_decl_loc (loc,
27072 FUNCTION_DECL,
27073 dguide_name (type), fntype);
27074 DECL_ARGUMENTS (ded_fn) = fargs;
27075 DECL_ARTIFICIAL (ded_fn) = true;
27076 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27077 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27078 DECL_ARTIFICIAL (ded_tmpl) = true;
27079 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27080 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27081 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27082 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27083 if (DECL_P (ctor))
27084 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27085 if (ci)
27086 set_constraints (ded_tmpl, ci);
27087
27088 return ded_tmpl;
27089 }
27090
27091 /* Deduce template arguments for the class template placeholder PTYPE for
27092 template TMPL based on the initializer INIT, and return the resulting
27093 type. */
27094
27095 static tree
27096 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27097 tsubst_flags_t complain)
27098 {
27099 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27100 {
27101 /* We should have handled this in the caller. */
27102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27103 return ptype;
27104 if (complain & tf_error)
27105 error ("non-class template %qT used without template arguments", tmpl);
27106 return error_mark_node;
27107 }
27108
27109 tree type = TREE_TYPE (tmpl);
27110
27111 bool try_list_ctor = false;
27112
27113 vec<tree,va_gc> *args;
27114 if (init == NULL_TREE
27115 || TREE_CODE (init) == TREE_LIST)
27116 args = make_tree_vector_from_list (init);
27117 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27118 {
27119 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27120 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27121 {
27122 /* As an exception, the first phase in 16.3.1.7 (considering the
27123 initializer list as a single argument) is omitted if the
27124 initializer list consists of a single expression of type cv U,
27125 where U is a specialization of C or a class derived from a
27126 specialization of C. */
27127 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27128 tree etype = TREE_TYPE (elt);
27129
27130 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
27131 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27132 int err = unify (tparms, targs, type, etype,
27133 UNIFY_ALLOW_DERIVED, /*explain*/false);
27134 if (err == 0)
27135 try_list_ctor = false;
27136 ggc_free (targs);
27137 }
27138 if (try_list_ctor || is_std_init_list (type))
27139 args = make_tree_vector_single (init);
27140 else
27141 args = make_tree_vector_from_ctor (init);
27142 }
27143 else
27144 args = make_tree_vector_single (init);
27145
27146 tree dname = dguide_name (tmpl);
27147 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27148 /*type*/false, /*complain*/false,
27149 /*hidden*/false);
27150 bool elided = false;
27151 if (cands == error_mark_node)
27152 cands = NULL_TREE;
27153
27154 /* Prune explicit deduction guides in copy-initialization context. */
27155 if (flags & LOOKUP_ONLYCONVERTING)
27156 {
27157 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27158 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27159 elided = true;
27160
27161 if (elided)
27162 {
27163 /* Found a nonconverting guide, prune the candidates. */
27164 tree pruned = NULL_TREE;
27165 for (lkp_iterator iter (cands); iter; ++iter)
27166 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27167 pruned = lookup_add (*iter, pruned);
27168
27169 cands = pruned;
27170 }
27171 }
27172
27173 tree outer_args = NULL_TREE;
27174 if (DECL_CLASS_SCOPE_P (tmpl)
27175 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
27176 {
27177 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27178 type = TREE_TYPE (most_general_template (tmpl));
27179 }
27180
27181 bool saw_ctor = false;
27182 // FIXME cache artificial deduction guides
27183 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27184 {
27185 /* Skip inherited constructors. */
27186 if (iter.using_p ())
27187 continue;
27188
27189 tree guide = build_deduction_guide (*iter, outer_args, complain);
27190 if (guide == error_mark_node)
27191 return error_mark_node;
27192 if ((flags & LOOKUP_ONLYCONVERTING)
27193 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27194 elided = true;
27195 else
27196 cands = lookup_add (guide, cands);
27197
27198 saw_ctor = true;
27199 }
27200
27201 tree call = error_mark_node;
27202
27203 /* If this is list-initialization and the class has a list constructor, first
27204 try deducing from the list as a single argument, as [over.match.list]. */
27205 tree list_cands = NULL_TREE;
27206 if (try_list_ctor && cands)
27207 for (lkp_iterator iter (cands); iter; ++iter)
27208 {
27209 tree dg = *iter;
27210 if (is_list_ctor (dg))
27211 list_cands = lookup_add (dg, list_cands);
27212 }
27213 if (list_cands)
27214 {
27215 ++cp_unevaluated_operand;
27216 call = build_new_function_call (list_cands, &args, tf_decltype);
27217 --cp_unevaluated_operand;
27218
27219 if (call == error_mark_node)
27220 {
27221 /* That didn't work, now try treating the list as a sequence of
27222 arguments. */
27223 release_tree_vector (args);
27224 args = make_tree_vector_from_ctor (init);
27225 }
27226 }
27227
27228 /* Maybe generate an implicit deduction guide. */
27229 if (call == error_mark_node && args->length () < 2)
27230 {
27231 tree gtype = NULL_TREE;
27232
27233 if (args->length () == 1)
27234 /* Generate a copy guide. */
27235 gtype = build_reference_type (type);
27236 else if (!saw_ctor)
27237 /* Generate a default guide. */
27238 gtype = type;
27239
27240 if (gtype)
27241 {
27242 tree guide = build_deduction_guide (gtype, outer_args, complain);
27243 if (guide == error_mark_node)
27244 return error_mark_node;
27245 cands = lookup_add (guide, cands);
27246 }
27247 }
27248
27249 if (elided && !cands)
27250 {
27251 error ("cannot deduce template arguments for copy-initialization"
27252 " of %qT, as it has no non-explicit deduction guides or "
27253 "user-declared constructors", type);
27254 return error_mark_node;
27255 }
27256 else if (!cands && call == error_mark_node)
27257 {
27258 error ("cannot deduce template arguments of %qT, as it has no viable "
27259 "deduction guides", type);
27260 return error_mark_node;
27261 }
27262
27263 if (call == error_mark_node)
27264 {
27265 ++cp_unevaluated_operand;
27266 call = build_new_function_call (cands, &args, tf_decltype);
27267 --cp_unevaluated_operand;
27268 }
27269
27270 if (call == error_mark_node && (complain & tf_warning_or_error))
27271 {
27272 error ("class template argument deduction failed:");
27273
27274 ++cp_unevaluated_operand;
27275 call = build_new_function_call (cands, &args, complain | tf_decltype);
27276 --cp_unevaluated_operand;
27277
27278 if (elided)
27279 inform (input_location, "explicit deduction guides not considered "
27280 "for copy-initialization");
27281 }
27282
27283 release_tree_vector (args);
27284
27285 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27286 }
27287
27288 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27289 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27290 The CONTEXT determines the context in which auto deduction is performed
27291 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27292 OUTER_TARGS are used during template argument deduction
27293 (context == adc_unify) to properly substitute the result, and is ignored
27294 in other contexts.
27295
27296 For partial-concept-ids, extra args may be appended to the list of deduced
27297 template arguments prior to determining constraint satisfaction. */
27298
27299 tree
27300 do_auto_deduction (tree type, tree init, tree auto_node,
27301 tsubst_flags_t complain, auto_deduction_context context,
27302 tree outer_targs, int flags)
27303 {
27304 tree targs;
27305
27306 if (init == error_mark_node)
27307 return error_mark_node;
27308
27309 if (init && type_dependent_expression_p (init)
27310 && context != adc_unify)
27311 /* Defining a subset of type-dependent expressions that we can deduce
27312 from ahead of time isn't worth the trouble. */
27313 return type;
27314
27315 /* Similarly, we can't deduce from another undeduced decl. */
27316 if (init && undeduced_auto_decl (init))
27317 return type;
27318
27319 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27320 /* C++17 class template argument deduction. */
27321 return do_class_deduction (type, tmpl, init, flags, complain);
27322
27323 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27324 /* Nothing we can do with this, even in deduction context. */
27325 return type;
27326
27327 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27328 with either a new invented type template parameter U or, if the
27329 initializer is a braced-init-list (8.5.4), with
27330 std::initializer_list<U>. */
27331 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27332 {
27333 if (!DIRECT_LIST_INIT_P (init))
27334 type = listify_autos (type, auto_node);
27335 else if (CONSTRUCTOR_NELTS (init) == 1)
27336 init = CONSTRUCTOR_ELT (init, 0)->value;
27337 else
27338 {
27339 if (complain & tf_warning_or_error)
27340 {
27341 if (permerror (input_location, "direct-list-initialization of "
27342 "%<auto%> requires exactly one element"))
27343 inform (input_location,
27344 "for deduction to %<std::initializer_list%>, use copy-"
27345 "list-initialization (i.e. add %<=%> before the %<{%>)");
27346 }
27347 type = listify_autos (type, auto_node);
27348 }
27349 }
27350
27351 if (type == error_mark_node)
27352 return error_mark_node;
27353
27354 init = resolve_nondeduced_context (init, complain);
27355
27356 if (context == adc_decomp_type
27357 && auto_node == type
27358 && init != error_mark_node
27359 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27360 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27361 and initializer has array type, deduce cv-qualified array type. */
27362 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27363 complain);
27364 else if (AUTO_IS_DECLTYPE (auto_node))
27365 {
27366 tree stripped_init = tree_strip_any_location_wrapper (init);
27367 bool id = (DECL_P (stripped_init)
27368 || ((TREE_CODE (init) == COMPONENT_REF
27369 || TREE_CODE (init) == SCOPE_REF)
27370 && !REF_PARENTHESIZED_P (init)));
27371 targs = make_tree_vec (1);
27372 TREE_VEC_ELT (targs, 0)
27373 = finish_decltype_type (init, id, tf_warning_or_error);
27374 if (type != auto_node)
27375 {
27376 if (complain & tf_error)
27377 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27378 return error_mark_node;
27379 }
27380 }
27381 else
27382 {
27383 tree parms = build_tree_list (NULL_TREE, type);
27384 tree tparms;
27385
27386 if (flag_concepts)
27387 tparms = extract_autos (type);
27388 else
27389 {
27390 tparms = make_tree_vec (1);
27391 TREE_VEC_ELT (tparms, 0)
27392 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27393 }
27394
27395 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27396 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27397 DEDUCE_CALL,
27398 NULL, /*explain_p=*/false);
27399 if (val > 0)
27400 {
27401 if (processing_template_decl)
27402 /* Try again at instantiation time. */
27403 return type;
27404 if (type && type != error_mark_node
27405 && (complain & tf_error))
27406 /* If type is error_mark_node a diagnostic must have been
27407 emitted by now. Also, having a mention to '<type error>'
27408 in the diagnostic is not really useful to the user. */
27409 {
27410 if (cfun && auto_node == current_function_auto_return_pattern
27411 && LAMBDA_FUNCTION_P (current_function_decl))
27412 error ("unable to deduce lambda return type from %qE", init);
27413 else
27414 error ("unable to deduce %qT from %qE", type, init);
27415 type_unification_real (tparms, targs, parms, &init, 1, 0,
27416 DEDUCE_CALL,
27417 NULL, /*explain_p=*/true);
27418 }
27419 return error_mark_node;
27420 }
27421 }
27422
27423 /* Check any placeholder constraints against the deduced type. */
27424 if (flag_concepts && !processing_template_decl)
27425 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27426 {
27427 /* Use the deduced type to check the associated constraints. If we
27428 have a partial-concept-id, rebuild the argument list so that
27429 we check using the extra arguments. */
27430 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27431 tree cargs = CHECK_CONSTR_ARGS (constr);
27432 if (TREE_VEC_LENGTH (cargs) > 1)
27433 {
27434 cargs = copy_node (cargs);
27435 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27436 }
27437 else
27438 cargs = targs;
27439 if (!constraints_satisfied_p (constr, cargs))
27440 {
27441 if (complain & tf_warning_or_error)
27442 {
27443 auto_diagnostic_group d;
27444 switch (context)
27445 {
27446 case adc_unspecified:
27447 case adc_unify:
27448 error("placeholder constraints not satisfied");
27449 break;
27450 case adc_variable_type:
27451 case adc_decomp_type:
27452 error ("deduced initializer does not satisfy "
27453 "placeholder constraints");
27454 break;
27455 case adc_return_type:
27456 error ("deduced return type does not satisfy "
27457 "placeholder constraints");
27458 break;
27459 case adc_requirement:
27460 error ("deduced expression type does not satisfy "
27461 "placeholder constraints");
27462 break;
27463 }
27464 diagnose_constraints (input_location, constr, targs);
27465 }
27466 return error_mark_node;
27467 }
27468 }
27469
27470 if (processing_template_decl && context != adc_unify)
27471 outer_targs = current_template_args ();
27472 targs = add_to_template_args (outer_targs, targs);
27473 return tsubst (type, targs, complain, NULL_TREE);
27474 }
27475
27476 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27477 result. */
27478
27479 tree
27480 splice_late_return_type (tree type, tree late_return_type)
27481 {
27482 if (is_auto (type))
27483 {
27484 if (late_return_type)
27485 return late_return_type;
27486
27487 tree idx = get_template_parm_index (type);
27488 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27489 /* In an abbreviated function template we didn't know we were dealing
27490 with a function template when we saw the auto return type, so update
27491 it to have the correct level. */
27492 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27493 }
27494 return type;
27495 }
27496
27497 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27498 'decltype(auto)' or a deduced class template. */
27499
27500 bool
27501 is_auto (const_tree type)
27502 {
27503 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27504 && (TYPE_IDENTIFIER (type) == auto_identifier
27505 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27506 return true;
27507 else
27508 return false;
27509 }
27510
27511 /* for_each_template_parm callback for type_uses_auto. */
27512
27513 int
27514 is_auto_r (tree tp, void */*data*/)
27515 {
27516 return is_auto (tp);
27517 }
27518
27519 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27520 a use of `auto'. Returns NULL_TREE otherwise. */
27521
27522 tree
27523 type_uses_auto (tree type)
27524 {
27525 if (type == NULL_TREE)
27526 return NULL_TREE;
27527 else if (flag_concepts)
27528 {
27529 /* The Concepts TS allows multiple autos in one type-specifier; just
27530 return the first one we find, do_auto_deduction will collect all of
27531 them. */
27532 if (uses_template_parms (type))
27533 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27534 /*visited*/NULL, /*nondeduced*/false);
27535 else
27536 return NULL_TREE;
27537 }
27538 else
27539 return find_type_usage (type, is_auto);
27540 }
27541
27542 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27543 concepts are enabled, auto is acceptable in template arguments, but
27544 only when TEMPL identifies a template class. Return TRUE if any
27545 such errors were reported. */
27546
27547 bool
27548 check_auto_in_tmpl_args (tree tmpl, tree args)
27549 {
27550 /* If there were previous errors, nevermind. */
27551 if (!args || TREE_CODE (args) != TREE_VEC)
27552 return false;
27553
27554 /* If TMPL is an identifier, we're parsing and we can't tell yet
27555 whether TMPL is supposed to be a type, a function or a variable.
27556 We'll only be able to tell during template substitution, so we
27557 expect to be called again then. If concepts are enabled and we
27558 know we have a type, we're ok. */
27559 if (flag_concepts
27560 && (identifier_p (tmpl)
27561 || (DECL_P (tmpl)
27562 && (DECL_TYPE_TEMPLATE_P (tmpl)
27563 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27564 return false;
27565
27566 /* Quickly search for any occurrences of auto; usually there won't
27567 be any, and then we'll avoid allocating the vector. */
27568 if (!type_uses_auto (args))
27569 return false;
27570
27571 bool errors = false;
27572
27573 tree vec = extract_autos (args);
27574 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27575 {
27576 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27577 error_at (DECL_SOURCE_LOCATION (xauto),
27578 "invalid use of %qT in template argument", xauto);
27579 errors = true;
27580 }
27581
27582 return errors;
27583 }
27584
27585 /* For a given template T, return the vector of typedefs referenced
27586 in T for which access check is needed at T instantiation time.
27587 T is either a FUNCTION_DECL or a RECORD_TYPE.
27588 Those typedefs were added to T by the function
27589 append_type_to_template_for_access_check. */
27590
27591 vec<qualified_typedef_usage_t, va_gc> *
27592 get_types_needing_access_check (tree t)
27593 {
27594 tree ti;
27595 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27596
27597 if (!t || t == error_mark_node)
27598 return NULL;
27599
27600 if (!(ti = get_template_info (t)))
27601 return NULL;
27602
27603 if (CLASS_TYPE_P (t)
27604 || TREE_CODE (t) == FUNCTION_DECL)
27605 {
27606 if (!TI_TEMPLATE (ti))
27607 return NULL;
27608
27609 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27610 }
27611
27612 return result;
27613 }
27614
27615 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27616 tied to T. That list of typedefs will be access checked at
27617 T instantiation time.
27618 T is either a FUNCTION_DECL or a RECORD_TYPE.
27619 TYPE_DECL is a TYPE_DECL node representing a typedef.
27620 SCOPE is the scope through which TYPE_DECL is accessed.
27621 LOCATION is the location of the usage point of TYPE_DECL.
27622
27623 This function is a subroutine of
27624 append_type_to_template_for_access_check. */
27625
27626 static void
27627 append_type_to_template_for_access_check_1 (tree t,
27628 tree type_decl,
27629 tree scope,
27630 location_t location)
27631 {
27632 qualified_typedef_usage_t typedef_usage;
27633 tree ti;
27634
27635 if (!t || t == error_mark_node)
27636 return;
27637
27638 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27639 || CLASS_TYPE_P (t))
27640 && type_decl
27641 && TREE_CODE (type_decl) == TYPE_DECL
27642 && scope);
27643
27644 if (!(ti = get_template_info (t)))
27645 return;
27646
27647 gcc_assert (TI_TEMPLATE (ti));
27648
27649 typedef_usage.typedef_decl = type_decl;
27650 typedef_usage.context = scope;
27651 typedef_usage.locus = location;
27652
27653 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27654 }
27655
27656 /* Append TYPE_DECL to the template TEMPL.
27657 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27658 At TEMPL instanciation time, TYPE_DECL will be checked to see
27659 if it can be accessed through SCOPE.
27660 LOCATION is the location of the usage point of TYPE_DECL.
27661
27662 e.g. consider the following code snippet:
27663
27664 class C
27665 {
27666 typedef int myint;
27667 };
27668
27669 template<class U> struct S
27670 {
27671 C::myint mi; // <-- usage point of the typedef C::myint
27672 };
27673
27674 S<char> s;
27675
27676 At S<char> instantiation time, we need to check the access of C::myint
27677 In other words, we need to check the access of the myint typedef through
27678 the C scope. For that purpose, this function will add the myint typedef
27679 and the scope C through which its being accessed to a list of typedefs
27680 tied to the template S. That list will be walked at template instantiation
27681 time and access check performed on each typedefs it contains.
27682 Note that this particular code snippet should yield an error because
27683 myint is private to C. */
27684
27685 void
27686 append_type_to_template_for_access_check (tree templ,
27687 tree type_decl,
27688 tree scope,
27689 location_t location)
27690 {
27691 qualified_typedef_usage_t *iter;
27692 unsigned i;
27693
27694 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27695
27696 /* Make sure we don't append the type to the template twice. */
27697 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27698 if (iter->typedef_decl == type_decl && scope == iter->context)
27699 return;
27700
27701 append_type_to_template_for_access_check_1 (templ, type_decl,
27702 scope, location);
27703 }
27704
27705 /* Convert the generic type parameters in PARM that match the types given in the
27706 range [START_IDX, END_IDX) from the current_template_parms into generic type
27707 packs. */
27708
27709 tree
27710 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27711 {
27712 tree current = current_template_parms;
27713 int depth = TMPL_PARMS_DEPTH (current);
27714 current = INNERMOST_TEMPLATE_PARMS (current);
27715 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27716
27717 for (int i = 0; i < start_idx; ++i)
27718 TREE_VEC_ELT (replacement, i)
27719 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27720
27721 for (int i = start_idx; i < end_idx; ++i)
27722 {
27723 /* Create a distinct parameter pack type from the current parm and add it
27724 to the replacement args to tsubst below into the generic function
27725 parameter. */
27726
27727 tree o = TREE_TYPE (TREE_VALUE
27728 (TREE_VEC_ELT (current, i)));
27729 tree t = copy_type (o);
27730 TEMPLATE_TYPE_PARM_INDEX (t)
27731 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27732 o, 0, 0, tf_none);
27733 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27734 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27735 TYPE_MAIN_VARIANT (t) = t;
27736 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27737 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27738 TREE_VEC_ELT (replacement, i) = t;
27739 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27740 }
27741
27742 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27743 TREE_VEC_ELT (replacement, i)
27744 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27745
27746 /* If there are more levels then build up the replacement with the outer
27747 template parms. */
27748 if (depth > 1)
27749 replacement = add_to_template_args (template_parms_to_args
27750 (TREE_CHAIN (current_template_parms)),
27751 replacement);
27752
27753 return tsubst (parm, replacement, tf_none, NULL_TREE);
27754 }
27755
27756 /* Entries in the decl_constraint hash table. */
27757 struct GTY((for_user)) constr_entry
27758 {
27759 tree decl;
27760 tree ci;
27761 };
27762
27763 /* Hashing function and equality for constraint entries. */
27764 struct constr_hasher : ggc_ptr_hash<constr_entry>
27765 {
27766 static hashval_t hash (constr_entry *e)
27767 {
27768 return (hashval_t)DECL_UID (e->decl);
27769 }
27770
27771 static bool equal (constr_entry *e1, constr_entry *e2)
27772 {
27773 return e1->decl == e2->decl;
27774 }
27775 };
27776
27777 /* A mapping from declarations to constraint information. Note that
27778 both templates and their underlying declarations are mapped to the
27779 same constraint information.
27780
27781 FIXME: This is defined in pt.c because garbage collection
27782 code is not being generated for constraint.cc. */
27783
27784 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27785
27786 /* Returns the template constraints of declaration T. If T is not
27787 constrained, return NULL_TREE. Note that T must be non-null. */
27788
27789 tree
27790 get_constraints (tree t)
27791 {
27792 if (!flag_concepts)
27793 return NULL_TREE;
27794
27795 gcc_assert (DECL_P (t));
27796 if (TREE_CODE (t) == TEMPLATE_DECL)
27797 t = DECL_TEMPLATE_RESULT (t);
27798 constr_entry elt = { t, NULL_TREE };
27799 constr_entry* found = decl_constraints->find (&elt);
27800 if (found)
27801 return found->ci;
27802 else
27803 return NULL_TREE;
27804 }
27805
27806 /* Associate the given constraint information CI with the declaration
27807 T. If T is a template, then the constraints are associated with
27808 its underlying declaration. Don't build associations if CI is
27809 NULL_TREE. */
27810
27811 void
27812 set_constraints (tree t, tree ci)
27813 {
27814 if (!ci)
27815 return;
27816 gcc_assert (t && flag_concepts);
27817 if (TREE_CODE (t) == TEMPLATE_DECL)
27818 t = DECL_TEMPLATE_RESULT (t);
27819 gcc_assert (!get_constraints (t));
27820 constr_entry elt = {t, ci};
27821 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27822 constr_entry* entry = ggc_alloc<constr_entry> ();
27823 *entry = elt;
27824 *slot = entry;
27825 }
27826
27827 /* Remove the associated constraints of the declaration T. */
27828
27829 void
27830 remove_constraints (tree t)
27831 {
27832 gcc_assert (DECL_P (t));
27833 if (TREE_CODE (t) == TEMPLATE_DECL)
27834 t = DECL_TEMPLATE_RESULT (t);
27835
27836 constr_entry elt = {t, NULL_TREE};
27837 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27838 if (slot)
27839 decl_constraints->clear_slot (slot);
27840 }
27841
27842 /* Memoized satisfaction results for declarations. This
27843 maps the pair (constraint_info, arguments) to the result computed
27844 by constraints_satisfied_p. */
27845
27846 struct GTY((for_user)) constraint_sat_entry
27847 {
27848 tree ci;
27849 tree args;
27850 tree result;
27851 };
27852
27853 /* Hashing function and equality for constraint entries. */
27854
27855 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27856 {
27857 static hashval_t hash (constraint_sat_entry *e)
27858 {
27859 hashval_t val = iterative_hash_object(e->ci, 0);
27860 return iterative_hash_template_arg (e->args, val);
27861 }
27862
27863 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27864 {
27865 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27866 }
27867 };
27868
27869 /* Memoized satisfaction results for concept checks. */
27870
27871 struct GTY((for_user)) concept_spec_entry
27872 {
27873 tree tmpl;
27874 tree args;
27875 tree result;
27876 };
27877
27878 /* Hashing function and equality for constraint entries. */
27879
27880 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27881 {
27882 static hashval_t hash (concept_spec_entry *e)
27883 {
27884 return hash_tmpl_and_args (e->tmpl, e->args);
27885 }
27886
27887 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27888 {
27889 ++comparing_specializations;
27890 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27891 --comparing_specializations;
27892 return eq;
27893 }
27894 };
27895
27896 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27897 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27898
27899 /* Search for a memoized satisfaction result. Returns one of the
27900 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27901
27902 tree
27903 lookup_constraint_satisfaction (tree ci, tree args)
27904 {
27905 constraint_sat_entry elt = { ci, args, NULL_TREE };
27906 constraint_sat_entry* found = constraint_memos->find (&elt);
27907 if (found)
27908 return found->result;
27909 else
27910 return NULL_TREE;
27911 }
27912
27913 /* Memoize the result of a satisfication test. Returns the saved result. */
27914
27915 tree
27916 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27917 {
27918 constraint_sat_entry elt = {ci, args, result};
27919 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27920 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27921 *entry = elt;
27922 *slot = entry;
27923 return result;
27924 }
27925
27926 /* Search for a memoized satisfaction result for a concept. */
27927
27928 tree
27929 lookup_concept_satisfaction (tree tmpl, tree args)
27930 {
27931 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27932 concept_spec_entry* found = concept_memos->find (&elt);
27933 if (found)
27934 return found->result;
27935 else
27936 return NULL_TREE;
27937 }
27938
27939 /* Memoize the result of a concept check. Returns the saved result. */
27940
27941 tree
27942 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27943 {
27944 concept_spec_entry elt = {tmpl, args, result};
27945 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27946 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27947 *entry = elt;
27948 *slot = entry;
27949 return result;
27950 }
27951
27952 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27953
27954 /* Returns a prior concept specialization. This returns the substituted
27955 and normalized constraints defined by the concept. */
27956
27957 tree
27958 get_concept_expansion (tree tmpl, tree args)
27959 {
27960 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27961 concept_spec_entry* found = concept_expansions->find (&elt);
27962 if (found)
27963 return found->result;
27964 else
27965 return NULL_TREE;
27966 }
27967
27968 /* Save a concept expansion for later. */
27969
27970 tree
27971 save_concept_expansion (tree tmpl, tree args, tree def)
27972 {
27973 concept_spec_entry elt = {tmpl, args, def};
27974 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27975 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27976 *entry = elt;
27977 *slot = entry;
27978 return def;
27979 }
27980
27981 static hashval_t
27982 hash_subsumption_args (tree t1, tree t2)
27983 {
27984 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27985 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27986 int val = 0;
27987 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27988 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27989 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27990 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27991 return val;
27992 }
27993
27994 /* Compare the constraints of two subsumption entries. The LEFT1 and
27995 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27996 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27997
27998 static bool
27999 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28000 {
28001 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28002 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28003 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28004 CHECK_CONSTR_ARGS (right1)))
28005 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28006 CHECK_CONSTR_ARGS (right2));
28007 return false;
28008 }
28009
28010 /* Key/value pair for learning and memoizing subsumption results. This
28011 associates a pair of check constraints (including arguments) with
28012 a boolean value indicating the result. */
28013
28014 struct GTY((for_user)) subsumption_entry
28015 {
28016 tree t1;
28017 tree t2;
28018 bool result;
28019 };
28020
28021 /* Hashing function and equality for constraint entries. */
28022
28023 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28024 {
28025 static hashval_t hash (subsumption_entry *e)
28026 {
28027 return hash_subsumption_args (e->t1, e->t2);
28028 }
28029
28030 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28031 {
28032 ++comparing_specializations;
28033 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28034 --comparing_specializations;
28035 return eq;
28036 }
28037 };
28038
28039 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28040
28041 /* Search for a previously cached subsumption result. */
28042
28043 bool*
28044 lookup_subsumption_result (tree t1, tree t2)
28045 {
28046 subsumption_entry elt = { t1, t2, false };
28047 subsumption_entry* found = subsumption_table->find (&elt);
28048 if (found)
28049 return &found->result;
28050 else
28051 return 0;
28052 }
28053
28054 /* Save a subsumption result. */
28055
28056 bool
28057 save_subsumption_result (tree t1, tree t2, bool result)
28058 {
28059 subsumption_entry elt = {t1, t2, result};
28060 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28061 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28062 *entry = elt;
28063 *slot = entry;
28064 return result;
28065 }
28066
28067 /* Set up the hash table for constraint association. */
28068
28069 void
28070 init_constraint_processing (void)
28071 {
28072 if (!flag_concepts)
28073 return;
28074
28075 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28076 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28077 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28078 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28079 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28080 }
28081
28082 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28083 0..N-1. */
28084
28085 void
28086 declare_integer_pack (void)
28087 {
28088 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28089 build_function_type_list (integer_type_node,
28090 integer_type_node,
28091 NULL_TREE),
28092 NULL_TREE, ECF_CONST);
28093 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28094 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
28095 DECL_FUNCTION_CODE (ipfn)
28096 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
28097 }
28098
28099 /* Set up the hash tables for template instantiations. */
28100
28101 void
28102 init_template_processing (void)
28103 {
28104 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
28105 type_specializations = hash_table<spec_hasher>::create_ggc (37);
28106
28107 if (cxx_dialect >= cxx11)
28108 declare_integer_pack ();
28109 }
28110
28111 /* Print stats about the template hash tables for -fstats. */
28112
28113 void
28114 print_template_statistics (void)
28115 {
28116 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28117 "%f collisions\n", (long) decl_specializations->size (),
28118 (long) decl_specializations->elements (),
28119 decl_specializations->collisions ());
28120 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28121 "%f collisions\n", (long) type_specializations->size (),
28122 (long) type_specializations->elements (),
28123 type_specializations->collisions ());
28124 }
28125
28126 #if CHECKING_P
28127
28128 namespace selftest {
28129
28130 /* Verify that build_non_dependent_expr () works, for various expressions,
28131 and that location wrappers don't affect the results. */
28132
28133 static void
28134 test_build_non_dependent_expr ()
28135 {
28136 location_t loc = BUILTINS_LOCATION;
28137
28138 /* Verify constants, without and with location wrappers. */
28139 tree int_cst = build_int_cst (integer_type_node, 42);
28140 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28141
28142 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28143 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28144 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28145
28146 tree string_lit = build_string (4, "foo");
28147 TREE_TYPE (string_lit) = char_array_type_node;
28148 string_lit = fix_string_type (string_lit);
28149 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28150
28151 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28152 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28153 ASSERT_EQ (wrapped_string_lit,
28154 build_non_dependent_expr (wrapped_string_lit));
28155 }
28156
28157 /* Verify that type_dependent_expression_p () works correctly, even
28158 in the presence of location wrapper nodes. */
28159
28160 static void
28161 test_type_dependent_expression_p ()
28162 {
28163 location_t loc = BUILTINS_LOCATION;
28164
28165 tree name = get_identifier ("foo");
28166
28167 /* If no templates are involved, nothing is type-dependent. */
28168 gcc_assert (!processing_template_decl);
28169 ASSERT_FALSE (type_dependent_expression_p (name));
28170
28171 ++processing_template_decl;
28172
28173 /* Within a template, an unresolved name is always type-dependent. */
28174 ASSERT_TRUE (type_dependent_expression_p (name));
28175
28176 /* Ensure it copes with NULL_TREE and errors. */
28177 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28178 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28179
28180 /* A USING_DECL in a template should be type-dependent, even if wrapped
28181 with a location wrapper (PR c++/83799). */
28182 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28183 TREE_TYPE (using_decl) = integer_type_node;
28184 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28185 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28186 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28187 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28188
28189 --processing_template_decl;
28190 }
28191
28192 /* Run all of the selftests within this file. */
28193
28194 void
28195 cp_pt_c_tests ()
28196 {
28197 test_build_non_dependent_expr ();
28198 test_type_dependent_expression_p ();
28199 }
28200
28201 } // namespace selftest
28202
28203 #endif /* #if CHECKING_P */
28204
28205 #include "gt-cp-pt.h"